diff options
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/fs/namev.c | 1 | ||||
-rw-r--r-- | kernel/fs/s5fs/s5fs_subr.c | 73 |
2 files changed, 37 insertions, 37 deletions
diff --git a/kernel/fs/namev.c b/kernel/fs/namev.c index dd44450..8e355d6 100644 --- a/kernel/fs/namev.c +++ b/kernel/fs/namev.c @@ -289,6 +289,7 @@ long namev_dir(vnode_t *base, const char *path, vnode_t **res_vnode, *namelen = len; return 0; } + /* * Open the file specified by `base` and `path`, or create it, if necessary. * Return the file's vnode via `res_vnode`, which should be returned unlocked diff --git a/kernel/fs/s5fs/s5fs_subr.c b/kernel/fs/s5fs/s5fs_subr.c index 0d12030..a9fa638 100644 --- a/kernel/fs/s5fs/s5fs_subr.c +++ b/kernel/fs/s5fs/s5fs_subr.c @@ -160,25 +160,25 @@ long s5_file_block_to_disk_block(s5_node_t *sn, size_t file_blocknum, } // Else, allocate the block - disk_blocknum = s5_alloc_block(VNODE_TO_S5FS(&sn->vnode)); + long alloced_blocknum = s5_alloc_block(VNODE_TO_S5FS(&sn->vnode)); // Propogate errors from s5_alloc_block - if (disk_blocknum < 0) + if (alloced_blocknum < 0) { - return disk_blocknum; + return alloced_blocknum; } // Update the inode - sn->inode.s5_direct_blocks[file_blocknum] = disk_blocknum; + sn->inode.s5_direct_blocks[file_blocknum] = alloced_blocknum; sn->dirtied_inode = 1; // set ret params and return *newp = 1; - return disk_blocknum; + return alloced_blocknum; } // Case 2: Indirect block is not allocated but alloc is set else if (!is_indirect_block_allocated && alloc) { - size_t disk_blocknum = s5_alloc_block(VNODE_TO_S5FS(&sn->vnode)); + long disk_blocknum = s5_alloc_block(VNODE_TO_S5FS(&sn->vnode)); // Propogate errors from s5_alloc_block if (disk_blocknum < 0) { @@ -228,24 +228,25 @@ long s5_file_block_to_disk_block(s5_node_t *sn, size_t file_blocknum, } // Else, allocate the block - disk_blocknum = s5_alloc_block(VNODE_TO_S5FS(&sn->vnode)); + long alloced_blocknum = s5_alloc_block(VNODE_TO_S5FS(&sn->vnode)); // Propogate errors from s5_alloc_block - if (disk_blocknum < 0) + if (alloced_blocknum < 0) { - return disk_blocknum; + return alloced_blocknum; } // Update the inode - indirect_block[indirect_block_index] = disk_blocknum; + indirect_block[indirect_block_index] = alloced_blocknum; sn->dirtied_inode = 1; // set ret params and return *newp = 1; - return disk_blocknum; + return alloced_blocknum; } // Case 4: The indirect block has not been allocated and alloc is clear else { + // TODO: check if this is correct return 0; } } @@ -289,31 +290,30 @@ ssize_t s5_read_file(s5_node_t *sn, size_t pos, char *buf, size_t len) } // Calculate the number of bytes to read - size_t bytes_to_read = min(len, sn->inode.s5_un.s5_size - pos); + size_t tmp = sn->inode.s5_un.s5_size - pos; + size_t bytes_to_read = len < tmp ? len : tmp; // Initialize the number of bytes read size_t bytes_read = 0; while (bytes_read < bytes_to_read) { - // Get the block number of the desired file block - int new_block; size_t file_blocknum = pos / S5_BLOCK_SIZE; size_t block_offset = pos % S5_BLOCK_SIZE; - size_t disk_blocknum = s5_file_block_to_disk_block(sn, file_blocknum, 0, &new_block); - - // Propogate errors from s5_file_block_to_disk_block - if (disk_blocknum < 0) - { - return disk_blocknum; - } // Get the pframe containing the block data pframe_t *pf; - s5_get_file_block(sn, file_blocknum, 0, &pf); + long err = s5_get_file_block(sn, file_blocknum, 0, &pf); + // Propogate errors from s5_get_file_block + if (err < 0) + { + return err; + } // Calculate the number of bytes to read in the current iteration - size_t bytes_read_in_iteration = min(bytes_to_read - bytes_read, S5_BLOCK_SIZE - block_offset); + size_t tmp1 = bytes_to_read - bytes_read; + size_t tmp2 = S5_BLOCK_SIZE - block_offset; + size_t bytes_read_in_iteration = tmp1 < tmp2 ? tmp1 : tmp2; // Copy the data from the block to the buffer memcpy(buf + bytes_read, pf->pf_addr + block_offset, bytes_read_in_iteration); @@ -372,31 +372,29 @@ ssize_t s5_write_file(s5_node_t *sn, size_t pos, const char *buf, size_t len) } // Calculate the number of bytes to write - size_t bytes_to_write = min(len, S5_MAX_FILE_SIZE - pos); + size_t bytes_to_write = len < S5_MAX_FILE_SIZE - pos ? len : S5_MAX_FILE_SIZE - pos; // Initialize the number of bytes written size_t bytes_written = 0; while (bytes_written < bytes_to_write) { - // Get the block number of the desired file block - int new_block; size_t file_blocknum = pos / S5_BLOCK_SIZE; size_t block_offset = pos % S5_BLOCK_SIZE; - size_t disk_blocknum = s5_file_block_to_disk_block(sn, file_blocknum, 1, &new_block); - - // Propogate errors from s5_file_block_to_disk_block - if (disk_blocknum < 0) - { - return disk_blocknum; - } // Get the pframe containing the block data pframe_t *pf; - s5_get_file_block(sn, file_blocknum, 1, &pf); + long err = s5_get_file_block(sn, file_blocknum, 1, &pf); + // Propogate errors from s5_get_file_block + if (err < 0) + { + return err; + } // Calculate the number of bytes to write in the current iteration - size_t bytes_written_in_iteration = min(bytes_to_write - bytes_written, S5_BLOCK_SIZE - block_offset); + size_t tmp1 = bytes_to_write - bytes_written; + size_t tmp2 = S5_BLOCK_SIZE - block_offset; + size_t bytes_written_in_iteration = tmp1 < tmp2 ? tmp1 : tmp2; // Copy the data from the buffer to the block memcpy(pf->pf_addr + block_offset, buf + bytes_written, bytes_written_in_iteration); @@ -410,8 +408,9 @@ ssize_t s5_write_file(s5_node_t *sn, size_t pos, const char *buf, size_t len) // Update the position pos += bytes_written_in_iteration; - // Update the inode's s5_size and mark the inode dirty - sn->inode.s5_un.s5_size = max(sn->inode.s5_un.s5_size, pos); + // Update the inode + sn->inode.s5_un.s5_size = sn->inode.s5_un.s5_size > pos ? sn->inode.s5_un.s5_size : pos; + sn->vnode.vn_len = sn->inode.s5_un.s5_size; sn->dirtied_inode = 1; } |