From a33dd11f03d71730423d79024a28b9b475574a1b Mon Sep 17 00:00:00 2001 From: sfja Date: Tue, 24 Nov 2026 03:33:57 +0100 Subject: [PATCH] finish new filesystem --- CMakeLists.txt | 23 +- src/fs2.c | 855 ++++++++++++++++++++++++++++++++++++++++--------- src/fs2.h | 29 +- 3 files changed, 756 insertions(+), 151 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a41f5da..1424cfe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,18 +27,29 @@ block() set(fs_file ${CMAKE_CURRENT_BINARY_DIR}/fs.bin) set(disk_file ${CMAKE_CURRENT_BINARY_DIR}/disk.bin) + # add_custom_command( + # OUTPUT ${fs_file} + # COMMAND fstool ${fs_file} mkfile /kernel.bin + # COMMAND fstool ${fs_file} import ${kernel_file} /kernel.bin + # COMMAND fstool ${fs_file} mkdir /docs + # COMMAND fstool ${fs_file} mkfile /docs/readme.txt + # COMMAND fstool ${fs_file} importb ${CMAKE_CURRENT_SOURCE_DIR}/readme.txt /docs/readme.txt + # DEPENDS fstool ${kernel_file} + # VERBATIM + # USES_TERMINAL + # ) + add_custom_command( OUTPUT ${fs_file} - COMMAND fstool ${fs_file} mkfile /kernel.bin - COMMAND fstool ${fs_file} import ${kernel_file} /kernel.bin - COMMAND fstool ${fs_file} mkdir /docs - COMMAND fstool ${fs_file} mkfile /docs/readme.txt - COMMAND fstool ${fs_file} importb ${CMAKE_CURRENT_SOURCE_DIR}/readme.txt /docs/readme.txt - DEPENDS fstool ${kernel_file} + COMMAND fs2 -d ${fs_file} -c importw2w ${kernel_file} /kernel.bin + COMMAND fs2 -d ${fs_file} -c mkdir /docs + COMMAND fs2 -d ${fs_file} -c importb2w ${CMAKE_CURRENT_SOURCE_DIR}/readme.txt /docs/readme.txt + DEPENDS fs2 ${kernel_file} VERBATIM USES_TERMINAL ) + add_custom_command( OUTPUT ${disk_file} COMMAND mkdisk ${disk_file} ${boot_file} ${fs_file}:0800 diff --git a/src/fs2.c b/src/fs2.c index 2c33e49..c81fbd9 100644 --- a/src/fs2.c +++ b/src/fs2.c @@ -29,12 +29,12 @@ static word word_strlen(const word *str) return len; } -static bool word_streq(const word *a, const word *b) +static bool word_strneq(const word *a, const word *b, word len) { - for (; *a && *b; ++a, ++b) + for (; len; ++a, ++b, --len) if (*a != *b) return false; - return *a == *b; + return true; } static word word_min(word a, word b) @@ -68,7 +68,7 @@ static size_t direntry_size(const DirEntry *entry) return (sizeof(entry->inode_id) + sizeof(entry->next) + sizeof(entry->name_len) + sizeof(entry->name[0]) * entry->name_len) - / sizeof(uint16_t); + / sizeof(word); } static word allocate_block(Fs *fs) @@ -80,6 +80,8 @@ static word allocate_block(Fs *fs) assert(block_id < fs_blocks_cap); + if (!bitmap_is_set(fs->block_bitmap, block_id)) + fs->sb->blocks_count += 1; bitmap_set(fs->block_bitmap, block_id); fs->out_of_sync = true; @@ -88,6 +90,8 @@ static word allocate_block(Fs *fs) static void deallocate_block(Fs *fs, word block_id) { + if (bitmap_is_set(fs->block_bitmap, block_id)) + fs->sb->blocks_count -= 1; bitmap_unset(fs->block_bitmap, block_id); fs->out_of_sync = true; } @@ -161,6 +165,15 @@ static void load_inode_block(Fs *fs, word block_id) fs->current_inode_block_id = block_id; } +static void load_data_block(Fs *fs, word block_id) +{ + if (fs->current_data_block_id == block_id) + return; + + read_block(fs, fs->data_block, block_id); + fs->current_data_block_id = block_id; +} + static void write_inode(Fs *fs, const INode *inode) { word inode_block_idx = inode->id / fs_inodes_per_block; @@ -186,11 +199,11 @@ static void read_inode(Fs *fs, INode *out_inode, word inode_id) memcpy(out_inode, &fs->inode_block[block_offset], sizeof(INode)); } -static uint16_t inode_allocate_block(Fs *fs, INode *inode) +static word inode_allocate_block(Fs *fs, INode *inode) { assert(inode->blocks_count < fs_inode_blocks_cap); - uint16_t block_id = allocate_block(fs); + word block_id = allocate_block(fs); if (inode->blocks_count < fs_inode_ndir_blocks_count) { inode->blocks[inode->blocks_count] = block_id; @@ -228,7 +241,8 @@ static bool dirinode_entry_exists(Fs *fs, INode *dirinode, const word *name) fs_opendir(fs, &cursor, dirinode); DirEntry *entry; while ((entry = fs_readdir(fs, &cursor))) { - if (word_streq(entry->name, name)) { + if (entry->name_len == word_strlen(name) + && word_strneq(entry->name, name, entry->name_len)) { exists = true; break; } @@ -244,7 +258,7 @@ static void make_dir_inode( .id = inode_id, .mode = INode_Dir, .blocks_count = 0, - .last_block_size = 0, + .last_block_size = fs_block_size, .blocks = { }, }; @@ -265,16 +279,24 @@ static void make_dir_inode( }; uint16_t entry_block_id = inode_allocate_block(fs, out_inode); + load_direntry_block(fs, entry_block_id); + memset(fs->direntry_block, 0, fs_block_size * sizeof(word)); memcpy(fs->direntry_block, &self_entry, self_entry_size * sizeof(word)); memcpy(&fs->direntry_block[self_entry_size], &parent_entry, direntry_size(&parent_entry) * sizeof(word)); write_block(fs, fs->direntry_block, entry_block_id); - out_inode->last_block_size = self_entry_size + direntry_size(&parent_entry); write_inode(fs, out_inode); } +static void read_direntry_at_offset( + DirEntry *out_entry, const word *block, word offset) +{ + word read_size = word_min(fs_direntry_max_size, fs_block_size - offset); + memcpy(out_entry, &block[offset], read_size * sizeof(word)); +} + static void dirinode_append_direntry( Fs *fs, INode *inode, const DirEntry *direntry) { @@ -285,43 +307,36 @@ static void dirinode_append_direntry( = *inode_block_at(inode, inode->blocks_count - 1, fs->ind_block); load_direntry_block(fs, block_id); - DirEntry entry = { }; word offset = 0; - memcpy(&entry, - &fs->direntry_block[offset], - fs_direntry_max_size * sizeof(word)); - - while (entry.next) { + DirEntry entry = { }; + do { offset = entry.next; - memcpy(&entry, - &fs->direntry_block[offset], - fs_direntry_max_size * sizeof(word)); - } + read_direntry_at_offset(&entry, fs->direntry_block, offset); + } while (entry.next); - if (offset + direntry_size(direntry) <= fs_block_size) { - uint16_t new_offset = offset + direntry_size(&entry); + word new_offset = offset + direntry_size(&entry); + + if (new_offset + direntry_size(direntry) <= fs_block_size) { entry.next = new_offset; - memcpy(&fs->direntry_block[offset], &entry, - direntry_size(&entry) * sizeof(uint16_t)); + direntry_size(&entry) * sizeof(word)); + memcpy(&fs->direntry_block[new_offset], direntry, - direntry_size(direntry) * sizeof(uint16_t)); - inode->last_block_size = new_offset + direntry_size(direntry); + direntry_size(direntry) * sizeof(word)); write_block(fs, fs->direntry_block, block_id); return; } word new_block_id = inode_allocate_block(fs, inode); load_direntry_block(fs, new_block_id); - memcpy(fs->direntry_block, - direntry, - direntry_size(direntry) * sizeof(uint16_t)); - inode->last_block_size = direntry_size(direntry); + memcpy( + fs->direntry_block, direntry, direntry_size(direntry) * sizeof(word)); write_block(fs, fs->direntry_block, new_block_id); } +// Add inode to dir-inode. Writes `dir_inode`. static void add_inode_to_dir( Fs *fs, INode *dir_inode, const INode *inode, const word *name) { @@ -340,13 +355,6 @@ static void add_inode_to_dir( write_inode(fs, dir_inode); } -static void read_direntry_at_offset( - DirEntry *out_entry, const word *block, word offset) -{ - word read_size = word_min(fs_direntry_max_size, fs_block_size - offset); - memcpy(out_entry, &block[offset], read_size * sizeof(word)); -} - static void remove_inode_from_dir(Fs *fs, INode *dir_inode, word inode_id) { if (inode_has_ind_block(dir_inode)) @@ -354,82 +362,61 @@ static void remove_inode_from_dir(Fs *fs, INode *dir_inode, word inode_id) word block_idx = 0; word block_id; + word prev_offset; word offset; word next_offset; - word last_size; for (; block_idx < dir_inode->blocks_count; ++block_idx) { block_id = *inode_block_at(dir_inode, block_idx, fs->ind_block); load_direntry_block(fs, block_id); + prev_offset = 0; offset = 0; + next_offset = 0; do { - DirEntry entry = { }; read_direntry_at_offset(&entry, fs->direntry_block, offset); next_offset = entry.next; - last_size = direntry_size(&entry); if (entry.inode_id == inode_id) - goto entry_found; + goto break_block_loop; + prev_offset = offset; offset = next_offset; } while (offset); } - assert(false); -entry_found: + assert(!"not found"); - word prev_next_offset = offset; +break_block_loop: - while (next_offset) { - DirEntry entry = { }; - read_direntry_at_offset(&entry, fs->direntry_block, next_offset); + DirEntry entry = { }; + read_direntry_at_offset(&entry, fs->direntry_block, prev_offset); + entry.next = next_offset; + memcpy(&fs->direntry_block[prev_offset], + &entry, + direntry_size(&entry) * sizeof(word)); - word next_next_offset = entry.next; - entry.next = next_offset; + write_block(fs, fs->direntry_block, block_id); + write_inode(fs, dir_inode); +} - memcpy(&fs->direntry_block[prev_next_offset], - &entry, - direntry_size(&entry) * sizeof(word)); - last_size = direntry_size(&entry); +static void inode_deallocate_blocks(Fs *fs, INode *inode) +{ + if (inode_has_ind_block(inode)) + load_ind_block(fs, inode->blocks[fs_inode_ind_block_idx]); - prev_next_offset = next_offset; - next_offset = next_next_offset; + for (word i = 0; i < inode->blocks_count; ++i) { + word *block_id = inode_block_at(inode, i, fs->ind_block); + deallocate_block(fs, *block_id); + *block_id = 0; } - if (block_idx == dir_inode->blocks_count - 1) - dir_inode->last_block_size = prev_next_offset + last_size; - - if (!offset && !next_offset) { - if (block_idx == dir_inode->blocks_count - 1) { - word offset = 0; - word prev_offset = 0; - word last_size = 0; - DirEntry *entry; - do { - entry = (DirEntry *)&fs->direntry_block[offset]; - last_size = direntry_size(entry); - prev_offset = offset; - offset = entry->next; - } while (offset); - dir_inode->last_block_size = prev_offset + last_size; - } - - for (word i = block_id; i < dir_inode->blocks_count - 1; ++i) { - word id = *inode_block_at(dir_inode, i + 1, fs->ind_block); - *inode_block_at(dir_inode, i, fs->ind_block) = id; - } - *inode_block_at(dir_inode, dir_inode->blocks_count - 1, fs->ind_block) - = 0; - dir_inode->blocks_count -= 1; - - if (dir_inode->blocks_count == fs_inode_ndir_blocks_count) { - deallocate_block(fs, dir_inode->blocks[fs_inode_ind_block_idx]); - dir_inode->blocks[fs_inode_ind_block_idx] = 0; - } + if (inode_has_ind_block(inode)) { + deallocate_block(fs, inode->blocks[fs_inode_ind_block_idx]); + inode->blocks[fs_inode_ind_block_idx] = 0; } } @@ -443,6 +430,7 @@ static void init_fs(Fs *fs, Disk *disk, word block_offset) .ind_block = calloc(fs_block_size, sizeof(word)), .direntry_block = calloc(fs_block_size, sizeof(word)), .inode_block = calloc(fs_block_size, sizeof(word)), + .data_block = calloc(fs_block_size, sizeof(word)), .sb = nullptr, .current_ind_block_id = 0, .current_direntry_block_id = 0, @@ -471,6 +459,7 @@ void fs_umount(Fs *fs) free(fs->ind_block); free(fs->direntry_block); free(fs->inode_block); + free(fs->data_block); } void fs_sync(Fs *fs) @@ -492,15 +481,21 @@ void fs_inode(Fs *fs, INode *out_inode, word inode_id) read_inode(fs, out_inode, inode_id); } -void fs_opendir(Fs *fs, DirCursor *out_cursor, INode *inode) +word fs_opendir(Fs *fs, DirCursor *out_cursor, INode *inode) { (void)fs; + + if (inode->mode != INode_Dir) + return -1; // Not a directory. + *out_cursor = (DirCursor) { .inode = inode, .direntry = { }, .current_block_idx = 0, .current_offset = 0, }; + + return 0; } void fs_closedir(Fs *fs, DirCursor *cursor) @@ -546,13 +541,11 @@ word fs_mkdir(Fs *fs, INode *out_inode, INode *parent, const word *name) if (!name_is_valid(name)) return -1; // Invalid name. - bool exists = dirinode_entry_exists(fs, parent, name); - if (exists) + if (dirinode_entry_exists(fs, parent, name)) return -2; // Entry exists. word inode_id = allocate_inode(fs); make_dir_inode(fs, out_inode, inode_id, parent->id); - write_inode(fs, out_inode); add_inode_to_dir(fs, parent, out_inode, name); return 0; @@ -583,7 +576,7 @@ word fs_rmdir(Fs *fs, INode *inode) return -2; // Not empty; // We have cleaned up correctly. - assert(inode->blocks_count == 1 && inode->last_block_size == 9); + assert(inode->blocks_count == 1); if (parent_id == inode->id) return -3; // Root. @@ -597,6 +590,180 @@ word fs_rmdir(Fs *fs, INode *inode) return 0; } +word fs_mkfile(Fs *fs, INode *out_inode, INode *parent, const word *name) +{ + if (!name_is_valid(name)) + return -1; // Invalid name. + + if (dirinode_entry_exists(fs, parent, name)) + return -2; // Entry exists. + + word inode_id = allocate_inode(fs); + + *out_inode = (INode) { + .id = inode_id, + .mode = INode_File, + .blocks_count = 0, + .last_block_size = 0, + .blocks = { }, + }; + + write_inode(fs, out_inode); + add_inode_to_dir(fs, parent, out_inode, name); + + return 0; +} + +word fs_unlink(Fs *fs, INode *inode, INode *dirinode) +{ + if (inode->mode != INode_File) + return -1; // Not a file. + + remove_inode_from_dir(fs, dirinode, inode->id); + write_inode(fs, dirinode); + + inode_deallocate_blocks(fs, inode); + + deallocate_inode(fs, inode->id); + + return 0; +} + +word fs_open(Fs *fs, FileCursor *out_cursor, INode *inode, FileMode mode) +{ + if (inode->mode != INode_File) + return -1; // Not a file. + + if ((mode & File_Trunc) && !(mode & File_Write)) + return -2; // Invalid mode. + + if (mode & File_Trunc) + inode_deallocate_blocks(fs, inode); + + *out_cursor = (FileCursor) { + .inode = inode, + .mode = mode, + .current_block_idx = 0, + .current_offset = 0, + }; + + return 0; +} + +void fs_close(Fs *fs, FileCursor *cursor) +{ + if (cursor->mode & File_Write) + write_inode(fs, cursor->inode); +} + +iword fs_read(Fs *fs, FileCursor *cursor, word *buffer, word count) +{ + INode *inode = cursor->inode; + + if (!(cursor->mode & File_Read)) + return -1; // Invalid mode. + + if (inode_has_ind_block(inode)) + load_ind_block(fs, inode->blocks[fs_inode_ind_block_idx]); + + word words_read = 0; + + while ( + words_read < count && cursor->current_block_idx < inode->blocks_count) { + + word block_id + = *inode_block_at(inode, cursor->current_block_idx, fs->ind_block); + + load_data_block(fs, block_id); + + word block_size = cursor->current_block_idx == inode->blocks_count - 1 + ? inode->last_block_size + : fs_block_size; + word block_read_size + = word_min(block_size - cursor->current_offset, count - words_read); + + memcpy(buffer, + &fs->data_block[cursor->current_offset], + block_read_size * sizeof(word)); + + words_read += block_read_size; + cursor->current_offset += block_read_size; + + if (cursor->current_offset >= block_size) { + cursor->current_block_idx += 1; + cursor->current_offset = 0; + } + } + + return words_read; +} + +iword fs_write(Fs *fs, FileCursor *cursor, const word *buffer, word count) +{ + INode *inode = cursor->inode; + + if (!(cursor->mode & File_Write)) + return -1; // Invalid mode. + + if (inode_has_ind_block(inode)) + load_ind_block(fs, inode->blocks[fs_inode_ind_block_idx]); + + word words_written = 0; + + while (words_written < count) { + + word block_id; + if (cursor->current_block_idx >= inode->blocks_count) + block_id = inode_allocate_block(fs, inode); + else + block_id = *inode_block_at( + inode, cursor->current_block_idx, fs->ind_block); + + load_data_block(fs, block_id); + + word block_write_size = word_min( + fs_block_size - cursor->current_offset, count - words_written); + + memcpy(fs->data_block, buffer, block_write_size * sizeof(word)); + + words_written += block_write_size; + cursor->current_offset += block_write_size; + + if (cursor->current_offset >= fs_block_size) { + cursor->current_block_idx += 1; + cursor->current_offset = 0; + } + } + + if (cursor->current_block_idx == inode->blocks_count - 1) + inode->last_block_size = cursor->current_offset; + + write_inode(fs, inode); + + return words_written; +} + +int fs_tell(Fs *fs, FileCursor *cursor) +{ + (void)fs; + return (int)cursor->current_block_idx << 16 | cursor->current_offset; +} + +void fs_seek(Fs *fs, FileCursor *cursor, int pos) +{ + (void)fs; + INode *inode = cursor->inode; + + if (pos < 0) { + int size + = (int)(inode->blocks_count - 1) << 16 | inode->last_block_size; + pos = size - pos; + } + + cursor->current_block_idx = pos >> 16; + cursor->current_offset = pos & 0xffff; +} + void fs_mkfs(Fs *fs, Disk *disk, word block_offset) { init_fs(fs, disk, block_offset); @@ -614,32 +781,164 @@ void fs_mkfs(Fs *fs, Disk *disk, word block_offset) fs_umount(fs); } -static int resolve_path(Fs *fs, INode *inode, const word *path) +static void fs_dump(Fs *fs) { - if (!*path) - return 0; - if (*path == '/') { - fs_root(fs, inode); - return resolve_path(fs, inode, path + 1); + printf("fs:\n"); + printf(" block_offset = %u\n", fs->block_offset); + printf(" current_ind_block_id = %u\n", fs->current_ind_block_id); + printf(" current_direntry_block_id = %u\n", fs->current_direntry_block_id); + printf(" current_inode_block_id = %u\n", fs->current_inode_block_id); + printf(" current_data_block_id = %u\n", fs->current_data_block_id); + printf(" block_bitmap = {"); + for (uint i = 0; i < fs_blocks_cap; ++i) + if (bitmap_is_set(fs->block_bitmap, i)) + printf("%s%u", i ? ", " : "", i); + printf("}\n"); + printf(" out_of_sync = %u\n", fs->out_of_sync); + Superblock *sb = fs->sb; + printf("superblock:\n"); + printf(" magic = 0x%x\n", sb->magic); + printf(" blocks_count = %u\n", sb->blocks_count); + printf(" inodeblocks_count = %u\n", sb->inodeblocks_count); + printf(" inodeblocks = ["); + for (uint i = 0; i < sb->inodeblocks_count; ++i) + printf("%s%u", i ? ", " : "", sb->inodeblocks[i]); + printf("]\n"); + printf(" inodebitmap = {"); + for (uint i = 0; i < fs_inodebitmap_size; ++i) + if (bitmap_is_set(sb->inodebitmap, i)) + printf("%s%u", i ? ", " : "", i); + printf("}\n"); + + const size_t ids_in_allocated_blocks + = sb->inodeblocks_count * fs_inodes_per_block; + for (word inode_id = 0; inode_id < ids_in_allocated_blocks; ++inode_id) { + if (!bitmap_is_set(sb->inodebitmap, inode_id)) + continue; + + word inode_block_idx = inode_id / fs_inodes_per_block; + word block_offset = inode_id % fs_inodes_per_block * fs_inode_size; + + word block_id = sb->inodeblocks[inode_block_idx]; + word block[fs_block_size] = { }; + read_block(fs, block, block_id); + + INode *inode = (INode *)&block[block_offset]; + + printf("inode %zu/%zu\n", + inode_id / fs_inodes_per_block, + inode_id % fs_inodes_per_block); + printf(" id = %u\n", inode->id); + printf(" mode = %u\n", inode->mode); + printf(" blocks_count = %u\n", inode->blocks_count); + printf(" last_block_size = %u\n", inode->last_block_size); + printf(" blocks = ["); + for (uint i = 0; i < inode->blocks_count; ++i) + printf("%s%u", i ? ", " : "", inode->blocks[i]); + printf("]\n"); + + if (inode->mode != INode_Dir) + continue; + + printf(" dir entries:\n"); + printf(" inode off next name_len name\n"); + + word ind_block[fs_block_size] = { }; + if (inode_has_ind_block(inode)) + read_block(fs, ind_block, inode->blocks[fs_inode_ind_block_idx]); + + for (word i = 0; i < inode->blocks_count; ++i) { + word block_id = *inode_block_at(inode, i, ind_block); + word block[fs_block_size] = { }; + read_block(fs, block, block_id); + word off = 0; + DirEntry *entry; + do { + entry = (DirEntry *)&block[off]; + printf(" %-5u %-3u %-4u %-8u \"", + entry->inode_id, + off, + entry->next, + entry->name_len); + for (word j = 0; j < entry->name_len; ++j) + printf("%c", entry->name[j]); + printf("\"\n"); + off = entry->next; + } while (off); + } } +} - const word *name = path; - for (; *path && *path != '/'; ++path) - ; - for (; *path == '/'; ++path) +static char *next_path_seg(char **path, bool *is_dir) +{ + char *seg = *path; + for (; **path && **path != '/'; ++*path) ; + if (is_dir) + *is_dir = **path == '/'; + for (; **path == '/'; ++*path) + **path = '\0'; + return *seg ? seg : nullptr; +} +static bool direntry_name_is_cstr(const DirEntry *entry, const char *name) +{ + if (entry->name_len != strlen(name)) + return false; + for (size_t i = 0; i < entry->name_len; ++i) + if (entry->name[i] != name[i]) + return false; + return true; +} + +static int lookup_dir( + Fs *fs, INode *out_inode, INode *dirinode, const char *name) +{ DirCursor cursor; - fs_opendir(fs, &cursor, inode); + fs_opendir(fs, &cursor, dirinode); DirEntry *entry; while ((entry = fs_readdir(fs, &cursor))) - if (word_streq(entry->name, name)) + if (direntry_name_is_cstr(entry, name)) break; if (!entry) return -1; // Not found. - fs_inode(fs, inode, entry->inode_id); + fs_inode(fs, out_inode, entry->inode_id); fs_closedir(fs, &cursor); - return resolve_path(fs, inode, path); + return 0; +} + +static int resolve_path(Fs *fs, INode *inode, char **path, bool *is_dir) +{ + if (**path == '/') { + fs_root(fs, inode); + *path += 1; + } + + int res; + char *seg; + while ((seg = next_path_seg(path, is_dir))) + if ((res = lookup_dir(fs, inode, inode, seg) != 0)) + return res; + return 0; +} + +static int resolve_parent_path(Fs *fs, INode *inode, char **path, bool *is_dir) +{ + if (**path == '/') { + fs_root(fs, inode); + *path += 1; + } + + int res; + char *prev = next_path_seg(path, is_dir); + char *seg; + while ((seg = next_path_seg(path, is_dir))) { + if ((res = lookup_dir(fs, inode, inode, prev) != 0)) + return res; + prev = seg; + } + *path = prev; + return 0; } typedef struct { @@ -649,18 +948,14 @@ typedef struct { int argc; } Shell; -static void do_ls(Shell *s) +static int do_ls(Shell *s) { INode inode = *s->cwd; if (s->argc >= 2) { - word *path = calloc(strlen(s->argv[1]) + 1, sizeof(word)); - for (size_t i = 0; s->argv[1][i]; ++i) - path[i] = s->argv[1][i]; - if (resolve_path(s->fs, &inode, path) != 0) { - printf("error occured\n"); - return; + if (resolve_path(s->fs, &inode, &s->argv[1], nullptr) != 0) { + printf("not found\n"); + return 1; } - free(path); } DirCursor cursor; @@ -668,7 +963,7 @@ static void do_ls(Shell *s) DirEntry *entry; while ((entry = fs_readdir(s->fs, &cursor))) { - char name[64]; + char name[64] = { }; size_t i = 0; for (; i < entry->name_len; ++i) name[i] = entry->name[i]; @@ -676,42 +971,313 @@ static void do_ls(Shell *s) } fs_closedir(s->fs, &cursor); + return 0; } -static void do_mkdir(Shell *s) +static int do_mkdir(Shell *s) { assert(s->argc >= 2); - const char *byte_name = s->argv[1]; - word name[fs_name_max_len] = { }; - for (size_t i = 0; byte_name[i]; ++i) - name[i] = byte_name[i]; + + char *path = s->argv[1]; + INode parent = *s->cwd; + if (resolve_parent_path(s->fs, &parent, &path, nullptr) != 0) { + printf("not found\n"); + return 1; + } + + word name[fs_name_max_len + 1] = { }; + for (size_t i = 0; path[i]; ++i) + name[i] = path[i]; INode inode = { }; - word result = fs_mkdir(s->fs, &inode, s->cwd, name); - if (result != 0) + word result = fs_mkdir(s->fs, &inode, &parent, name); + if (result == (word)-2) { + printf("already exists\n"); + return 0; + } else if (result != 0) { printf("error occured\n"); + return 1; + } + return 0; } -static void do_rmdir(Shell *s) +static int do_rmdir(Shell *s) { if (s->argc < 2) { printf("expected path\n"); - return; + return 1; } INode inode = *s->cwd; - word *path = calloc(strlen(s->argv[1]) + 1, sizeof(word)); - for (size_t i = 0; s->argv[1][i]; ++i) - path[i] = s->argv[1][i]; - if (resolve_path(s->fs, &inode, path) != 0) { - printf("error occured\n"); - return; + if (resolve_path(s->fs, &inode, &s->argv[1], nullptr) != 0) { + printf("not found\n"); + return 1; } - free(path); word result = fs_rmdir(s->fs, &inode); if (result != 0) printf("error occured\n"); + return 0; +} + +static int do_edit(Shell *s) +{ + if (s->argc < 2) { + printf("expected path\n"); + return 1; + } + + char *path = s->argv[1]; + INode parent = *s->cwd; + bool is_dir; + if (resolve_parent_path(s->fs, &parent, &path, &is_dir) != 0 || is_dir) { + printf("invalid path\n"); + return 1; + } + + word name[fs_name_max_len + 1] = { }; + for (size_t i = 0; path[i]; ++i) + name[i] = path[i]; + + int status; + + INode inode = { }; + if (lookup_dir(s->fs, &inode, &parent, path) != 0) + if ((status = fs_mkfile(s->fs, &inode, &parent, name)) != 0) + goto leave_error; + + FileCursor cursor; + if ((status = fs_open(s->fs, &cursor, &inode, File_Write)) != 0) + goto leave_error; + + constexpr size_t buffer_cap = 64; + word buffer[buffer_cap] = { }; + size_t buffer_len = 0; + + int words_written = 0; + + int ch; + while ((ch = fgetc(stdin)) != EOF) { + buffer[buffer_len] = (word)ch; + buffer_len += 1; + + if (buffer_len >= buffer_cap) { + int written = fs_write(s->fs, &cursor, buffer, buffer_len); + if ((status = written) < 0) + goto leave_error; + words_written += written; + buffer_len = 0; + } + } + + if (buffer_len) { + int written = fs_write(s->fs, &cursor, buffer, buffer_len); + if ((status = written) < 0) + goto leave_error; + words_written += written; + } + + if (feof(stdin)) + clearerr(stdin); + + fs_close(s->fs, &cursor); + + printf("\n-- wrote %d words --\n", words_written); + + return 0; +leave_error: + printf("error occured (status %d)\n", status); + return 1; +} + +static int do_cat(Shell *s) +{ + if (s->argc < 2) { + printf("expected path\n"); + return 1; + } + + INode inode = *s->cwd; + bool is_dir; + if (resolve_path(s->fs, &inode, &s->argv[1], &is_dir) != 0 || is_dir) { + printf("invalid path\n"); + return 1; + } + + int status; + + FileCursor cursor; + if ((status = fs_open(s->fs, &cursor, &inode, File_Read)) != 0) + goto leave_error; + + constexpr size_t buffer_cap = 64; + word buffer[buffer_cap] = { }; + + int words_read = 0; + + int read; + while ((read = fs_read(s->fs, &cursor, buffer, buffer_cap)) > 0) { + printf("read = %d\n", read); + for (int i = 0; i < read; ++i) + fputc(buffer[i], stdout); + words_read += read; + } + if (read < 0) { + status = read; + goto leave_error; + } + + fs_close(s->fs, &cursor); + + printf("\n-- read %d words --\n", words_read); + return 0; +leave_error: + printf("error occured (status %d)\n", status); + return 1; +} + +static int do_importw2w(Shell *s) +{ + if (s->argc < 3) { + printf("expected \n"); + return 1; + } + + char *ext_path = s->argv[1]; + + char *in_path = s->argv[2]; + INode parent = *s->cwd; + bool is_dir; + if (resolve_parent_path(s->fs, &parent, &in_path, &is_dir) != 0 || is_dir) { + printf("invalid path\n"); + return 1; + } + + word name[fs_name_max_len + 1] = { }; + for (size_t i = 0; in_path[i]; ++i) + name[i] = in_path[i]; + + int status; + INode inode = { }; + if (lookup_dir(s->fs, &inode, &parent, in_path) != 0) + if ((status = fs_mkfile(s->fs, &inode, &parent, name)) != 0) + goto leave_error; + FileCursor cursor; + if ((status = fs_open(s->fs, &cursor, &inode, File_Write)) != 0) + goto leave_error; + + FILE *file = fopen(ext_path, "rb"); + assert(file); + + constexpr size_t buffer_cap = 64; + word buffer[buffer_cap] = { }; + int words_written = 0; + + size_t words_read; + while ((words_read = fread(buffer, sizeof(word), buffer_cap, file))) { + if ((status = fs_write(s->fs, &cursor, buffer, words_read)) < 0) + goto leave_error; + words_written += words_read; + } + + fclose(file); + fs_close(s->fs, &cursor); + + printf("wrote %d words\n", words_written); + return 0; +leave_error: + printf("error occured (status %d)\n", status); + return 1; +} + +static int do_importb2w(Shell *s) +{ + if (s->argc < 3) { + printf("expected \n"); + return 1; + } + + char *ext_path = s->argv[1]; + + char *in_path = s->argv[2]; + INode parent = *s->cwd; + bool is_dir; + if (resolve_parent_path(s->fs, &parent, &in_path, &is_dir) != 0 || is_dir) { + printf("invalid path\n"); + return 1; + } + + word name[fs_name_max_len + 1] = { }; + for (size_t i = 0; in_path[i]; ++i) + name[i] = in_path[i]; + + int status; + INode inode = { }; + if (lookup_dir(s->fs, &inode, &parent, in_path) != 0) + if ((status = fs_mkfile(s->fs, &inode, &parent, name)) != 0) + goto leave_error; + FileCursor cursor; + if ((status = fs_open(s->fs, &cursor, &inode, File_Write)) != 0) + goto leave_error; + + FILE *file = fopen(ext_path, "rb"); + assert(file); + + constexpr size_t buffer_cap = 64; + word word_buffer[buffer_cap] = { }; + char byte_buffer[buffer_cap] = { }; + int words_written = 0; + + size_t words_read; + while ((words_read = fread(byte_buffer, sizeof(char), buffer_cap, file))) { + for (size_t i = 0; i < words_read; ++i) + word_buffer[i] = byte_buffer[i]; + + if ((status = fs_write(s->fs, &cursor, word_buffer, words_read)) < 0) + goto leave_error; + words_written += words_read; + } + + fclose(file); + fs_close(s->fs, &cursor); + + printf("wrote %d words\n", words_written); + return 0; +leave_error: + printf("error occured (status %d)\n", status); + return 1; +} + +static int run_command(Shell *s) +{ + assert(s->argc >= 1); + char *cmd = s->argv[0]; + size_t cmd_len = strlen(cmd); + + if (strcmp(cmd, "exit") == 0 || strncmp(cmd, "quit", cmd_len) == 0) { + return 2; + } else if (strcmp(cmd, "ls") == 0) { + return do_ls(s); + } else if (strcmp(cmd, "mkdir") == 0) { + return do_mkdir(s); + } else if (strcmp(cmd, "rmdir") == 0) { + return do_rmdir(s); + } else if (strcmp(cmd, "edit") == 0) { + return do_edit(s); + } else if (strcmp(cmd, "cat") == 0) { + return do_cat(s); + } else if (strcmp(cmd, "importw2w") == 0) { + return do_importw2w(s); + } else if (strcmp(cmd, "importb2w") == 0) { + return do_importb2w(s); + } else if (strcmp(cmd, "dump") == 0) { + fs_dump(s->fs); + } else { + printf("unknown command '%s'\n", cmd); + return 1; + } + + return 0; } static void run_shell(Fs *fs) @@ -733,27 +1299,14 @@ static void run_shell(Fs *fs) continue; int argc = 1; - char *argv[10]; + char *argv[10] = { }; argv[0] = strtok(line, " "); while ((argv[argc] = strtok(nullptr, " "))) argc += 1; Shell shell = { fs, &cwd, argv, argc }; - - char *cmd = argv[0]; - size_t cmd_len = strlen(cmd); - - if (strcmp(cmd, "exit") == 0 || strncmp(cmd, "quit", cmd_len) == 0) { + if (run_command(&shell) == 2) break; - } else if (strcmp(cmd, "ls") == 0) { - do_ls(&shell); - } else if (strcmp(cmd, "mkdir") == 0) { - do_mkdir(&shell); - } else if (strcmp(cmd, "rmdir") == 0) { - do_rmdir(&shell); - } else { - printf("unknown command '%s'\n", cmd); - } } } @@ -814,12 +1367,16 @@ int main(int argc, char *argv[]) const char *disk_filename = nullptr; bool init = false; word offset = 0; + int cmd_off = 0; for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "-d") == 0) { assert(argc >= i + 1); i += 1; disk_filename = argv[i]; + } else if (strcmp(argv[i], "-c") == 0) { + cmd_off = i + 1; + break; } else if (strcmp(argv[i], "--init") == 0) { init = true; } else if (strcmp(argv[i], "--offset") == 0) { @@ -865,7 +1422,15 @@ int main(int argc, char *argv[]) fs_mount(&fs, disk, offset); - run_shell(&fs); + int status = 0; + if (cmd_off) { + INode cwd; + fs_root(&fs, &cwd); + Shell shell = { &fs, &cwd, &argv[cmd_off], argc - cmd_off }; + status = run_command(&shell); + } else { + run_shell(&fs); + } fs_umount(&fs); @@ -874,4 +1439,6 @@ int main(int argc, char *argv[]) } else { free(disk_impl.mem_disk.blocks); } + + return status; } diff --git a/src/fs2.h b/src/fs2.h index 7c11628..4ff8752 100644 --- a/src/fs2.h +++ b/src/fs2.h @@ -25,6 +25,7 @@ constexpr size_t fs_inode_ind_block_idx = fs_inode_ndir_blocks_count; constexpr uint16_t fs_superblock_magic = 0x5a5a; typedef uint16_t word; +typedef int16_t iword; enum : word { INode_File, @@ -65,6 +66,21 @@ typedef struct { word current_offset; } DirCursor; +typedef enum : word { + File_Read = 1 << 0, + File_Write = 1 << 1, + File_Trunc = 1 << 2, + + File_ReadWrite = File_Read | File_Write, +} FileMode; + +typedef struct { + INode *inode; + word mode; + word current_block_idx; + word current_offset; +} FileCursor; + typedef struct Disk Disk; struct Disk { @@ -80,10 +96,12 @@ typedef struct { word *ind_block; word *direntry_block; word *inode_block; + word *data_block; Superblock *sb; word current_ind_block_id; word current_direntry_block_id; word current_inode_block_id; + word current_data_block_id; bool out_of_sync; } Fs; @@ -93,12 +111,21 @@ void fs_sync(Fs *fs); void fs_root(Fs *fs, INode *out_inode); void fs_inode(Fs *fs, INode *out_inode, word inode_id); -void fs_opendir(Fs *fs, DirCursor *out_cursor, INode *inode); +word fs_opendir(Fs *fs, DirCursor *out_cursor, INode *inode); void fs_closedir(Fs *fs, DirCursor *cursor); DirEntry *fs_readdir(Fs *fs, DirCursor *cursor); word fs_mkdir(Fs *fs, INode *out_inode, INode *parent, const word *name); word fs_rmdir(Fs *fs, INode *inode); +word fs_mkfile(Fs *fs, INode *out_inode, INode *parent, const word *name); +word fs_unlink(Fs *fs, INode *inode, INode *dirinode); +word fs_open(Fs *fs, FileCursor *out_cursor, INode *inode, FileMode mode); +void fs_close(Fs *fs, FileCursor *cursor); +iword fs_read(Fs *fs, FileCursor *cursor, word *buffer, word count); +iword fs_write(Fs *fs, FileCursor *cursor, const word *buffer, word count); +int fs_tell(Fs *fs, FileCursor *cursor); +void fs_seek(Fs *fs, FileCursor *cursor, int pos); + void fs_mkfs(Fs *fs, Disk *disk, word block_offset); #endif