diff --git a/lsfs_disk_controller.h b/lsfs_disk_controller.h index cd029d8..dc4aa24 100644 --- a/lsfs_disk_controller.h +++ b/lsfs_disk_controller.h @@ -16,6 +16,7 @@ typedef struct struct_partition_control partition_control; typedef struct File_System_Control_Information FSCI; typedef struct meta_information_format mif; typedef struct tag_record tag_record; +typedef struct lsfs_file lsfs_file; typedef uint64_t lsfs_sector_offset; typedef lsfs_sector_offset lsfs_file_id; @@ -27,6 +28,7 @@ static partition_control p_control; int create_file_system(); //lsfs_sector_offset lsfs_disk_create_tag(char* tag_name, bool is_filename); lsfs_sector_offset lsfs_disk_create_file(char* filename, lsfs_file_id* tags, void* file_data); +int lsfs_disk_getattr(lsfs_file* find_file, const char *path); //int lsfs_disk_untag_file(lsfs_file_id file_id, lsfs_file_id file_id); //int lsfs_disk_tag_file(lsfs_file_id file_id, lsfs_file_id file_id); //int lsfs_disk_delete_tag(lsfs_file_id file_id); @@ -72,7 +74,7 @@ typedef struct File_System_Control_Information { typedef struct struct_table_entry { char filename[256]; - lsfs_file_id tag_table_index; + lsfs_file_id file_id; uint64_t file_size; mif* ext_file_data; struct { @@ -113,26 +115,61 @@ typedef struct tag_record { } __attribute__((packed)) tag_record; +typedef struct lsfs_file { + lsfs_file_id file_id; + char* filename; + uint32_t owner_id; + uint64_t size; + uint64_t creation_date; + uint64_t access_time; + uint64_t modification_time; + lsfs_sector_offset *data; +} lsfs_file; + + +int lsfs_disk_getattr(lsfs_file* find_file, const char* path) { + int i = 0; + int found = 0; + while((p_control.master_table[i].filename[0]) != 0 && !found) { + if(strcmp( (path + 1 ), p_control.master_table[i].filename ) == 0) { + time_t current_time; + time ( ¤t_time ); + + //find_file = calloc(1, sizeof(lsfs_file)); + find_file->file_id = p_control.master_table[i].file_id; + find_file->filename = p_control.master_table[i].filename; + find_file->owner_id = getuid(); + find_file->size = p_control.master_table[i].file_size; // p_control.master_table[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; + found = 1; + } + i++; + } + return found; +} + + int lsfs_disk_read_data_from_file(lsfs_file_id file_id, int buffer_size, void* buffer_for_data) { // TODO some offset, to tell where in the file we want to write - mif* mif_record = calloc(1, SECTOR_SIZE); - read_data_from_disk(file_id, mif_record); + int return_val = 0; + for (int i = 0; i < NUM_DATA_POINTERS; ++i) { - if(mif_record->one_level_pointer_data[i] == 0) { + if(p_control.master_table[file_id].data_pointer[i] == 0) { break; } - return_val += read_data_from_disk(mif_record->one_level_pointer_data[i], buffer_for_data + (SECTOR_SIZE * i)); - i++; + return_val += read_data_from_disk(p_control.master_table[file_id].data_pointer[i], buffer_for_data + (SECTOR_SIZE * i)); } - time_t current_time; - time ( ¤t_time ); + //time_t current_time; + //time ( ¤t_time ); - mif_record->last_access_date = (uint64_t) current_time; - write_data_to_disk(file_id, mif_record); - free(mif_record); + //mif_record->last_access_date = (uint64_t) current_time; + //write_data_to_disk(file_id, mif_record); + //free(mif_record); return return_val; } @@ -165,7 +202,7 @@ int lsfs_disk_write_data_to_file(lsfs_file_id file_id, int data_length, char *da for (;;) { assert(current_sector <= NUM_DATA_POINTERS); - write_data_to_disk(mif_record.one_level_pointer_data[current_sector++], tmp_buffer); + write_data_to_disk(p_control.master_table[file_id].data_pointer[current_sector], tmp_buffer); if (data_length <= 0) break; data += SECTOR_SIZE; @@ -184,10 +221,10 @@ int lsfs_disk_write_data_to_file(lsfs_file_id file_id, int data_length, char *da free(tmp_buffer); - lsfs_disk_update_timestamps(&mif_record); - mif_record.file_size += amount_written; // update file size + //lsfs_disk_update_timestamps(&mif_record); + p_control.master_table[file_id].file_size += amount_written; // update file size - write_data_to_disk(file_id, &mif_record); + write_data_to_disk(file_id, &p_control.master_table[file_id]); return amount_written; } @@ -203,7 +240,7 @@ time_t lsfs_disk_truncate_file(lsfs_file_id file_id) { int lsfs_disk_rename_tag(lsfs_file_id file_id, char* new_filename) { for (int i = 0; i < MAX_MASTER_TAGS; ++i) { - if(p_control.master_table[i].tag_table_index == file_id) { + if(p_control.master_table[i].file_id == file_id) { memset(p_control.master_table[i].filename, 0, sizeof(p_control.master_table[i].filename)); sprintf(p_control.master_table[i].filename, "%s", new_filename); break; @@ -270,14 +307,14 @@ int lsfs_disk_delete_file(lsfs_file_id file_id) { for (int i = 0; i < MAX_MASTER_TAGS; ++i) { - if(p_control.master_table[i].tag_table_index == file_id) { + if(p_control.master_table[i].file_id == file_id) { p_control.master_table[i] = p_control.master_table[i+1]; truncate_table = 1; printf("Tag deleted from master table - TagID: %lu\n", file_id); } else if (truncate_table) { p_control.master_table[i] = p_control.master_table[i+1]; - if (p_control.master_table[i+1].tag_table_index == 0) { + if (p_control.master_table[i+1].file_id == 0) { break; } } @@ -383,13 +420,13 @@ int lsfs_disk_delete_file(lsfs_file_id file_id) { // Insert tag in the master tag, table an set the pointer to the first of the tag table int index_in_mtt = 0; - while(p_control.master_table[index_in_mtt].tag_table_index != 0) { + while(p_control.master_table[index_in_mtt].file_id != 0) { //TODO also have to count if we enter next data section. index_in_mtt++; } - p_control.master_table[index_in_mtt].tag_table_index = free_sectors[0]; + p_control.master_table[index_in_mtt].file_id = free_sectors[0]; printf("%lu\n", free_sectors[0]); sprintf(p_control.master_table[index_in_mtt].filename, "%s", tag_name); p_control.master_table[index_in_mtt].control_bits.is_filename = is_filename; diff --git a/lsfs_fuse.c b/lsfs_fuse.c index 3327824..0fd3408 100644 --- a/lsfs_fuse.c +++ b/lsfs_fuse.c @@ -90,18 +90,46 @@ int lsfs_rename(const char *path, const char *to) { int lsfs_getattr( const char *path, struct stat *stbuf ) { int res = 0; + lsfs_file *found_file = calloc(1, sizeof(lsfs_file)); printf("getattr: (path=%s)\n", path); memset(stbuf, 0, sizeof(struct stat)); if( strcmp( path, "/" ) == 0 ) { stbuf->st_mode = S_IFDIR | 0755; stbuf->st_nlink = 2; - } else if( strcmp( path, "/hello" ) == 0 ) { - stbuf->st_mode = S_IFREG | 0777; - stbuf->st_nlink = 1; - stbuf->st_size = 12; - } else - res = -ENOENT; + } else { + if(lsfs_disk_getattr(found_file, path)) { + stbuf->st_mode = S_IFREG | 0777; // @Hardcode + stbuf->st_nlink = 1; // @Hardcode + stbuf->st_size = found_file->size; + stbuf->st_uid = found_file->owner_id; + stbuf->st_gid = found_file->owner_id; + stbuf->st_atime = found_file->access_time; + stbuf->st_mtime = found_file->modification_time; + //free(found_file); + } + else { + res = -ENOENT; + } + } + + + + /*else { + if (lsfs_disk_getattr(found_file, path)) { + stbuf->st_mode = S_IFREG | 0777; // @Hardcode + stbuf->st_nlink = 1; // @Hardcode + stbuf->st_size = found_file->size; + stbuf->st_uid = found_file->owner_id; + stbuf->st_gid = found_file->owner_id; + stbuf->st_atime = found_file->access_time; + stbuf->st_mtime = found_file->modification_time; + free(found_file); + } + else { + res = -ENOENT; + } + } */ return res; // printf("getattr: (path=%s)\n", path); @@ -168,7 +196,7 @@ int lsfs_write(const char *path, const char *content, size_t content_length, off time_t current_time; time ( ¤t_time ); - //res = lsfs_disk_write_data_to_file(((lsfs_file*) file_info->fh)->file_id, content_length, (void*) content); + res = lsfs_disk_write_data_to_file(((lsfs_file*) file_info->fh)->file_id, content_length, (void*) content); //((lsfs_file*) file_info->fh)->size += res; //((lsfs_file*) file_info->fh)->access_time = current_time; @@ -186,11 +214,12 @@ int lsfs_readdir( const char *path, void *buf, fuse_fill_dir_t filler, off_t off filler(buf, ".", NULL, 0); filler(buf, "..", NULL, 0); - filler(buf, "hello", NULL, 0); - filler(buf, p_control.master_table[0].filename, NULL, 0); - filler(buf, p_control.master_table[1].filename, NULL, 0); - filler(buf, p_control.master_table[2].filename, NULL, 0); - + + int i = 0; + while(strcmp( "", p_control.master_table[i].filename ) != 0) { + filler(buf, p_control.master_table[i].filename, NULL, 0); + i++; + } return 0; } @@ -202,29 +231,12 @@ int lsfs_open( const char *path, struct fuse_file_info *fi ) { // https://libfuse.github.io/doxygen/structfuse__operations.html // printf("read: (path=%s)\n", path); - /* - lsfs_string_array split_path = lsfs_string_split_c(path, '/', false); + lsfs_file *found_file = calloc(1, sizeof(lsfs_file)); - lsfs_string filename = split_path.strings[split_path.length-1]; - - lsfs_tag *filename_tag = lsfs_hash_find(globals.tag_table, filename); - - if (filename_tag) { - if (filename_tag->is_filename) { - lsfs_file *found_file = lsfs_find_unique_file(filename_tag, split_path); - - if (found_file) { - if (found_file != &dummy_ambiguous_file) { - //(void*) file_data_buffer = calloc(1, SECTOR_SIZE); - fi->fh = (uint64_t) found_file; - //lsfs_disk_read_data_from_file(found_file->file_id, (256*4096), buf); - } - } - } + if (lsfs_disk_getattr(found_file, path)) { + fi->fh = (uint64_t) found_file; } - lsfs_destroy_string_array(split_path); - */ return 0; } @@ -234,9 +246,9 @@ int lsfs_read( const char *path, char *buf, size_t size, off_t offset_to_next_en time_t current_time; time ( ¤t_time ); - //int res = lsfs_disk_read_data_from_file( ((lsfs_file*) fi->fh)->file_id, size, buf); - //((lsfs_file*) fi->fh)->access_time = current_time; - return 0; + int res = lsfs_disk_read_data_from_file( ((lsfs_file*) fi->fh)->file_id, size, buf); + ((lsfs_file*) fi->fh)->access_time = current_time; + return res; } int lsfs_release(const char *path, struct fuse_file_info *fi) { @@ -295,8 +307,8 @@ end: } int main( int argc, char *argv[] ) { - - disk = fopen ( "/home/rhodez-x/Documents/github/SingOS/bin/SingOS.img" , "r+b" ); + // "/home/rhodez-x/Documents/github/SingOS/bin/SingOS.img" + disk = fopen ("/home/rhodez-x/Documents/github/SingOS/bin/SingOS.img", "r+b"); p_control.fsci = malloc(sizeof(FSCI)); p_control.master_table = malloc(sizeof(table_entry) * DEFAULT_MASTER_TABLE_SIZE); lsfs_disk_load_disk();