diff --git a/lsfs_disk_controller.h b/lsfs_disk_controller.h index 5fe7881..0a026f8 100644 --- a/lsfs_disk_controller.h +++ b/lsfs_disk_controller.h @@ -31,7 +31,7 @@ static partition_control p_control; int create_file_system(); int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind); -Directory_Table* lsfs_find_directory(const char path); +Directory_Table* lsfs_find_directory(const char* path, bool drop_filename); int lsfs_disk_getattr(lsfs_file* find_file, const char *path); int lsfs_disk_delete_file(lsfs_file_id file_id); int get_free_sectors_table(); @@ -166,76 +166,67 @@ typedef struct lsfs_file { } lsfs_file; -Directory_Table* lsfs_find_directory(const char path) +Directory_Table* lsfs_find_directory(const char *path, bool drop_filename) { + Directory_Table *dir_table = &p_control.master_table; lsfs_string_array split_path = lsfs_string_split_c(path, '/', false); - lsfs_string filename = split_path.strings[split_path.length-1]; + + int number_of_traversal = split_path.length; + + if (drop_filename) + { + number_of_traversal -= 1; + } // Start from the master table - int free_index = -1; // -1 is no index found. - Directory_Table *dir_table = &p_control.master_table; - lsfs_sector_offset table_disk_position = p_control.fsci.master_table_index; - for (int i = 0; i < split_path.length; ++i) + for (int i = 0; i < number_of_traversal; ++i) { for (int j = 0; j < DEFAULT_TABLE_SIZE; ++j) { - if (i == (split_path.length - 1)) - { - // Find free index and be sure that there dosent exist a file with the same name. - if (dir_table->entries[j].entry_kind == ENTRY_EMPTY) - { - // Set the free index, continue to see if the filename exist. - // if not -1, we have found a better index. - if (free_index == -1) - { - printf("Index found for file: %d\n", j); - free_index = j; - } - } - else if (strcmp(dir_table->entries[j].filename, split_path.strings[0].chars) == 0) - { - // Abort mission, we have a file with the same name. - return -EINVAL; - } - } - else + if (strcmp(dir_table->entries[j].filename, split_path.strings[i].chars) == 0) { - if (strcmp(dir_table->entries[j].filename, split_path.strings[0].chars) == 0) + if (i == 0) { - // We have found the next directory to traverse. - ; + // Alocate space, we refuse this further on + dir_table = malloc(sizeof(Directory_Table)); } + read_data_from_disk(dir_table->entries[j].data_pointer[0], DEFAULT_TABLE_SIZE, dir_table); + break; } } } - + return dir_table; } int lsfs_disk_getattr(lsfs_file* find_file, const char* path) { - int i = 0; - int found = 0; - while((p_control.master_table.entries[i].filename[0]) != 0 && !found) { - if(strcmp( (path + 1 ), p_control.master_table.entries[i].filename ) == 0) { + lsfs_string_array split_path = lsfs_string_split_c(path, '/', false); + lsfs_string filename = split_path.strings[split_path.length-1]; + + // Start from the master table + Directory_Table *dir_table = lsfs_find_directory(path, true); + + for (int i = 0; i < DEFAULT_TABLE_SIZE; ++i) + { + if(strcmp( filename.chars, dir_table->entries[i].filename ) == 0) { time_t current_time; time ( ¤t_time ); - find_file->file_id = p_control.master_table.entries[i].file_id; - find_file->entry_kind = p_control.master_table.entries[i].entry_kind; + find_file->file_id = dir_table->entries[i].file_id; + find_file->entry_kind = dir_table->entries[i].entry_kind; find_file->table_entry_pointer = i; - find_file->filename = p_control.master_table.entries[i].filename; + find_file->filename = dir_table->entries[i].filename; find_file->owner_id = getuid(); - find_file->size = p_control.master_table.entries[i].file_size; // p_control.master_table.entries[i].data_pointer[0]; //; + find_file->size = dir_table->entries[i].file_size; // dir_table->entries[i].data_pointer[0]; //; find_file->creation_date = (uint64_t) current_time; find_file->access_time = (uint64_t) current_time; find_file->modification_time = (uint64_t) current_time; - find_file->data = p_control.master_table.entries[i].data_pointer; + find_file->data = dir_table->entries[i].data_pointer; find_file->file_block_size = 1; // TODO: should be loaded from disk. - found = 1; + return 1; } - i++; } - return found; + return 0; } @@ -480,10 +471,11 @@ int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind) if (free_index == -1) { printf("Index found for file: %d\n", j); + table_disk_position += j; // Abselout index in file system free_index = j; } } - else if (strcmp(dir_table->entries[j].filename, split_path.strings[0].chars) == 0) + else if (strcmp(dir_table->entries[j].filename, split_path.strings[i].chars) == 0) { // Abort mission, we have a file with the same name. return -EINVAL; @@ -491,10 +483,18 @@ int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind) } else { - if (strcmp(dir_table->entries[j].filename, split_path.strings[0].chars) == 0) + if (strcmp(dir_table->entries[j].filename, split_path.strings[i].chars) == 0) { // We have found the next directory to traverse. - ; + if (i == 0) + { + // Alocate space, we refuse this further on + dir_table = malloc(sizeof(Directory_Table)); + } + printf("Get next dir\n"); + table_disk_position = dir_table->entries[j].data_pointer[0]; + read_data_from_disk(table_disk_position, DEFAULT_TABLE_SIZE, dir_table); + break; } } } @@ -515,11 +515,12 @@ int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind) if (entry_kind == ENTRY_DIRECTORY) { dir_table->entries[free_index].data_pointer[0] = get_free_sectors_table(); - dir_table->entries[free_index].file_size = 5120; + dir_table->entries[free_index].file_size = DEFAULT_TABLE_SIZE * SECTOR_SIZE; } else if (entry_kind == ENTRY_FILE) { // We assign one data pointer consiting of DEFAULT_FILE_SIZE sectors + dir_table->entries[free_index].file_size = 0; get_free_sectors_table(1, dir_table->entries[free_index].data_pointer); } else @@ -538,9 +539,7 @@ int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind) time ( ¤t_time ); */ - fseek ( disk , (table_disk_position + free_index) * SECTOR_SIZE, SEEK_SET ); - fwrite(&dir_table->entries[free_index], 1, SECTOR_SIZE, disk); - + write_data_to_disk(table_disk_position, 1, &dir_table->entries[free_index]); return 0; } @@ -559,7 +558,7 @@ int save_modified_file_information(lsfs_file* file) { int write_data_to_disk(lsfs_sector_offset index, uint32_t file_block_size, void* data_to_write) { fseek ( disk, (index * SECTOR_SIZE), SEEK_SET ); // SEEK_SET start offset at index 0 and move 1 * SECTOR_SIZE, and write here. - int written = fwrite(data_to_write, 1, (4 * SECTOR_SIZE), disk); + int written = fwrite(data_to_write, 1, (file_block_size * SECTOR_SIZE), disk); return written; } diff --git a/lsfs_fuse b/lsfs_fuse index 8049974..990342e 100755 Binary files a/lsfs_fuse and b/lsfs_fuse differ diff --git a/lsfs_fuse.c b/lsfs_fuse.c index 0a961c2..cb93b7d 100644 --- a/lsfs_fuse.c +++ b/lsfs_fuse.c @@ -145,20 +145,33 @@ int lsfs_readdir( const char *path, void *buf, fuse_fill_dir_t filler, off_t off (void) fi; printf("readdir: (path=%s)\n", path); - if(strcmp(path, "/") != 0) - return -ENOENT; - filler(buf, ".", NULL, 0); filler(buf, "..", NULL, 0); - Directory_Table *directory_table = lsfs_find_directory(path); + Directory_Table *directory_table; + if (strcmp(path, "/") != 0) + { + directory_table = lsfs_find_directory(path, false); + } + else + { + directory_table = &p_control.master_table; + } - for (int i = 0; i < DEFAULT_TABLE_SIZE; ++i) + if (directory_table != NULL) { - if (strcmp( "", directory_table.entries[i].filename ) != 0) + for (int i = 0; i < DEFAULT_TABLE_SIZE; ++i) { - filler(buf, directory_table.entries[i].filename, NULL, 0); + if (strcmp( "", directory_table->entries[i].filename ) != 0) + { + filler(buf, directory_table->entries[i].filename, NULL, 0); + } } + + } + if (strcmp(path, "/") != 0) + { + //free(directory_table); } return 0; } diff --git a/lsfs_fuse.o b/lsfs_fuse.o index 19abc30..15632f7 100644 Binary files a/lsfs_fuse.o and b/lsfs_fuse.o differ