fs_truncate(): trim EOF block down to size

master
A 2020-05-03 17:08:10 -07:00
parent ff58324790
commit 1538ab79b9
1 changed files with 6 additions and 5 deletions

View File

@ -41,9 +41,6 @@ int make_fs(char * disk_name)
for (int i = 0; i < super->data_block_offset; i++) for (int i = 0; i < super->data_block_offset; i++)
fat->table[i] = FAT_RESERVED; fat->table[i] = FAT_RESERVED;
// create a file real quick
fs_create("example");
if (write_blocks(disk, 0, DISK_BLOCKS) < 0) if (write_blocks(disk, 0, DISK_BLOCKS) < 0)
return -1; return -1;
@ -187,7 +184,7 @@ int fs_create(char * name)
// finally, create a file attrib entry // finally, create a file attrib entry
attrib = &dir->attributes[dir->size]; attrib = &dir->attributes[dir->size];
strncpy(attrib->name, name, MAX_FILENAME); memcpy(attrib->name, name, MAX_FILENAME);
attrib->size = 0; attrib->size = 0;
attrib->offset = fat_idx; attrib->offset = fat_idx;
@ -428,9 +425,13 @@ int fs_truncate(int fildes, off_t length)
descriptors[idx].attr->size = length; descriptors[idx].attr->size = length;
// ptr to final byte of truncated file // ptr to final byte of truncated file
file_ptr = disk + fat_idx * BLOCK_SIZE file_ptr = disk + eof_idx * BLOCK_SIZE
+ descriptors[idx].attr->size % BLOCK_SIZE; + descriptors[idx].attr->size % BLOCK_SIZE;
// trim the rest of the EOF block
memset(file_ptr + 1, 0,
BLOCK_SIZE - descriptors[idx].attr->size & BLOCK_SIZE);
// reset file ptr if it's pointing past the truncated end // reset file ptr if it's pointing past the truncated end
if (file_ptr < descriptors[idx].ptr) if (file_ptr < descriptors[idx].ptr)
return fs_lseek(fildes, descriptors[idx].attr->size); return fs_lseek(fildes, descriptors[idx].attr->size);