| 1 | #include <errno.h> |
| 2 | #include <fs/ext2.h> |
| 3 | #include <stdbool.h> |
| 4 | #include <stdint.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | enum errno ext2_write_file(struct ext2_fs *fs, struct ext2_full_inode *inode, |
| 8 | uint32_t offset, const uint8_t *src, uint32_t size) { |
| 9 | if (!fs || !inode || !src) |
| 10 | return ERR_INVAL; |
| 11 | |
| 12 | if (inode->node.mode & EXT2_S_IFDIR) |
| 13 | return ERR_IS_DIR; |
| 14 | |
| 15 | uint32_t bytes_written = 0; |
| 16 | uint32_t new_block_counter = 0; |
| 17 | while (bytes_written < size) { |
| 18 | bool new_block = false; |
| 19 | uint32_t file_offset = offset + bytes_written; |
| 20 | uint32_t block_index = file_offset / fs->block_size; |
| 21 | uint32_t block_offset = file_offset % fs->block_size; |
| 22 | |
| 23 | bool allocate = (size - bytes_written > 0); |
| 24 | uint32_t block_num = ext2_get_or_set_block( |
| 25 | fs, inode: &inode->node, block_index, new_block_num: 0, allocate, was_allocated: &new_block); |
| 26 | |
| 27 | if (new_block) |
| 28 | new_block_counter += 1; |
| 29 | |
| 30 | if (block_num == 0 && allocate) |
| 31 | return ERR_FS_NO_INODE; |
| 32 | |
| 33 | if (block_num == 0) { |
| 34 | bytes_written += |
| 35 | MIN(fs->block_size - block_offset, size - bytes_written); |
| 36 | continue; |
| 37 | } |
| 38 | |
| 39 | uint32_t to_write = fs->block_size - block_offset; |
| 40 | if (to_write > size - bytes_written) |
| 41 | to_write = size - bytes_written; |
| 42 | |
| 43 | struct bcache_entry *ent; |
| 44 | uint8_t *block_buf = ext2_block_read(fs, block_num, out: &ent); |
| 45 | if (!block_buf) |
| 46 | return ERR_IO; |
| 47 | |
| 48 | memcpy(block_buf + block_offset, src + bytes_written, to_write); |
| 49 | bcache_ent_release(ent); |
| 50 | |
| 51 | ext2_block_write(fs, ent, EXT2_PRIO_DATA); |
| 52 | |
| 53 | bytes_written += to_write; |
| 54 | } |
| 55 | |
| 56 | if (offset + size > inode->node.size) { |
| 57 | inode->node.size = offset + size; |
| 58 | } |
| 59 | |
| 60 | inode->node.blocks += |
| 61 | new_block_counter * (fs->block_size / fs->drive->sector_size); |
| 62 | |
| 63 | bool status = ext2_inode_write(fs, inode_num: inode->inode_num, inode: &inode->node); |
| 64 | return status ? ERR_OK : ERR_IO; |
| 65 | } |
| 66 | |
| 67 | struct file_read_ctx { |
| 68 | struct ext2_fs *fs; |
| 69 | struct ext2_inode *inode; |
| 70 | uint32_t offset; |
| 71 | uint32_t length; |
| 72 | uint8_t *buffer; |
| 73 | uint32_t bytes_read; |
| 74 | }; |
| 75 | |
| 76 | static void file_read_visitor(struct ext2_fs *fs, struct ext2_inode *inode, |
| 77 | uint32_t depth, uint32_t *block_ptr, |
| 78 | void *user_data) { |
| 79 | (void) depth; |
| 80 | struct file_read_ctx *ctx = (struct file_read_ctx *) user_data; |
| 81 | |
| 82 | if (ctx->bytes_read >= ctx->length) |
| 83 | return; |
| 84 | |
| 85 | if (*block_ptr == 0) |
| 86 | return; |
| 87 | |
| 88 | uint32_t block_size = fs->block_size; |
| 89 | struct bcache_entry *ent; |
| 90 | uint8_t *block_buf = ext2_block_read(fs, block_num: *block_ptr, out: &ent); |
| 91 | if (!block_buf) |
| 92 | return; |
| 93 | |
| 94 | uint32_t file_offset = ctx->bytes_read + ctx->offset; |
| 95 | uint32_t block_offset = file_offset % block_size; |
| 96 | |
| 97 | if ((ctx->bytes_read + ctx->offset) >= inode->size) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | uint32_t remaining = ctx->length - ctx->bytes_read; |
| 102 | uint32_t in_block = block_size - block_offset; |
| 103 | uint32_t to_copy = (remaining < in_block) ? remaining : in_block; |
| 104 | |
| 105 | if ((ctx->bytes_read + ctx->offset + to_copy) > inode->size) |
| 106 | to_copy = inode->size - (ctx->bytes_read + ctx->offset); |
| 107 | |
| 108 | memcpy(ctx->buffer + ctx->bytes_read, block_buf + block_offset, to_copy); |
| 109 | ctx->bytes_read += to_copy; |
| 110 | bcache_ent_release(ent); |
| 111 | } |
| 112 | |
| 113 | enum errno ext2_read_file(struct ext2_fs *fs, struct ext2_full_inode *inode, |
| 114 | uint32_t offset, uint8_t *buffer, uint64_t length) { |
| 115 | if (!fs || !inode || !buffer || offset >= inode->node.size) |
| 116 | return ERR_INVAL; |
| 117 | |
| 118 | if (inode->node.mode & EXT2_S_IFDIR) |
| 119 | return ERR_IS_DIR; |
| 120 | |
| 121 | if (offset + length > inode->node.size) |
| 122 | length = inode->node.size - offset; |
| 123 | |
| 124 | struct file_read_ctx ctx = {.fs = fs, |
| 125 | .inode = &inode->node, |
| 126 | .offset = offset, |
| 127 | .length = length, |
| 128 | .buffer = buffer, |
| 129 | .bytes_read = 0}; |
| 130 | |
| 131 | ext2_traverse_inode_blocks(fs, inode: &inode->node, visitor: file_read_visitor, user_data: &ctx, true); |
| 132 | inode->node.atime = time_get_unix(); |
| 133 | ext2_inode_write(fs, inode_num: inode->inode_num, inode: &inode->node); |
| 134 | return ERR_OK; |
| 135 | } |
| 136 | |
| 137 | enum errno ext2_chmod(struct ext2_fs *fs, struct ext2_full_inode *node, |
| 138 | uint16_t new_mode) { |
| 139 | if (!fs || !node) |
| 140 | return ERR_INVAL; |
| 141 | |
| 142 | uint16_t ftype = node->node.mode & EXT2_S_IFMT; |
| 143 | node->node.mode = ftype | (new_mode & EXT2_S_PERMS); |
| 144 | |
| 145 | if (!ext2_inode_write(fs, inode_num: node->inode_num, inode: &node->node)) |
| 146 | return ERR_FS_INTERNAL; |
| 147 | |
| 148 | return ERR_OK; |
| 149 | } |
| 150 | |
| 151 | enum errno ext2_chown(struct ext2_fs *fs, struct ext2_full_inode *node, |
| 152 | uint32_t new_uid, uint32_t new_gid) { |
| 153 | if (!fs || !node) |
| 154 | return ERR_INVAL; |
| 155 | |
| 156 | if (new_uid != (uint32_t) -1) |
| 157 | node->node.uid = new_uid; |
| 158 | |
| 159 | if (new_gid != (uint32_t) -1) |
| 160 | node->node.gid = new_gid; |
| 161 | |
| 162 | if (!ext2_inode_write(fs, inode_num: node->inode_num, inode: &node->node)) |
| 163 | return ERR_FS_INTERNAL; |
| 164 | |
| 165 | return ERR_OK; |
| 166 | } |
| 167 | |