diff --git a/SingOS.img b/SingOS.img deleted file mode 100644 index ff92620..0000000 Binary files a/SingOS.img and /dev/null differ diff --git a/a.out b/a.out index 466bfbe..1838447 100755 Binary files a/a.out and b/a.out differ diff --git a/lsfs_disk_controller.h b/lsfs_disk_controller.h index 06f35e1..d5b844c 100644 --- a/lsfs_disk_controller.h +++ b/lsfs_disk_controller.h @@ -38,22 +38,24 @@ int lsfs_disk_delete_file(lsfs_file_id file_id); int get_free_sectors_table(); int get_free_sectors(int num_sectors_needed, lsfs_sector_offset* output_array); int lsfs_disk_read_data_from_file(lsfs_file* file, int buffer_size, void* buffer_for_data); -int lsfs_disk_write_data_to_file(lsfs_file* file, int data_length, char *data); +int lsfs_disk_write_data_to_file(lsfs_file* file, int data_length, char *data, size_t offset_to_next_entry); int lsfs_disk_rename_file(lsfs_file* file, const char* new_filename); int lsfs_disk_load_disk(); int write_data_to_disk(lsfs_sector_offset at_sector, uint32_t file_block_size, void* data_to_write); int read_data_from_disk(lsfs_sector_offset index, uint32_t file_block_size, void* data_buffer); -int write_data_to_disk_off(lsfs_sector_offset index, void* data_to_write, int offset); +int write_data_to_disk_off(lsfs_sector_offset index, uint32_t file_block_size, void* data_to_write, int offset); int save_modified_file_information(lsfs_file* file); #define SPACE_MBR_RECORD 2048 // Sectors #define SPACE_VBR_RECORD 2048 // Sectors #define SIZE_FSCI_RECORD 1 // Sectors +#define DEFAULT_ENTRY_SIZE 1 // Sectors #define SECTOR_SIZE 512 // BYTES #define NUMBER_OF_MBR_PARTITIONS 4 #define DEFAULT_FILE_SIZE 4 // This is in sectors +#define DEFAULT_DATA_POINTER_SIZE 4 // This is in sectors #define DEFAULT_TABLE_SIZE 10 -#define NUM_DATA_POINTERS 28 +#define NUM_DATA_POINTERS 27 typedef enum Table_Entry_Kind { @@ -92,8 +94,9 @@ typedef struct struct_table_entry uint8_t extra_control_bits1; uint8_t extra_control_bits2; uint8_t extra_control_bits3; + lsfs_sector_offset table_entry_sector_index; lsfs_sector_offset data_pointer[NUM_DATA_POINTERS]; // if it is a directory, the first pointer will be to the next table. -} __attribute__((packed)) table_entry; +} __attribute__((packed)) Table_Entry; typedef struct Directory_Table { @@ -166,7 +169,8 @@ typedef struct lsfs_file { uint64_t access_time; uint64_t modification_time; uint32_t file_block_size; - lsfs_sector_offset *data; + lsfs_sector_offset table_entry_sector_index; + lsfs_sector_offset data_pointer[NUM_DATA_POINTERS]; } lsfs_file; @@ -188,15 +192,18 @@ Directory_Table* lsfs_find_directory(const char *path, bool drop_filename) { for (int j = 0; j < DEFAULT_TABLE_SIZE; ++j) { - printf("find dir: %s == %s\n", dir_table->entries[j].filename, split_path.strings[i].chars); if (strcmp(dir_table->entries[j].filename, split_path.strings[i].chars) == 0) { - printf("Latterlig data pointer: %d\n", dir_table->entries[j].data_pointer[0]); int index_sector = dir_table->entries[j].data_pointer[0]; if (i == 0) { // Alocate space, we refuse this further on - dir_table = malloc(sizeof(Directory_Table)); + dir_table = calloc(1, sizeof(Directory_Table)); + } + else + { + free(dir_table); + dir_table = calloc(1, sizeof(Directory_Table)); } read_data_from_disk(index_sector, DEFAULT_TABLE_SIZE, dir_table); break; @@ -223,12 +230,13 @@ int lsfs_disk_getattr(lsfs_file* find_file, const char* path) { find_file->entry_kind = dir_table->entries[i].entry_kind; find_file->table_entry_pointer = i; find_file->filename = dir_table->entries[i].filename; + find_file->table_entry_sector_index = dir_table->entries[i].table_entry_sector_index; find_file->owner_id = getuid(); 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 = dir_table->entries[i].data_pointer; + memcpy(find_file->data_pointer, dir_table->entries[i].data_pointer, DEFAULT_DATA_POINTER_SIZE * 8); find_file->file_block_size = 1; // TODO: should be loaded from disk. return 1; } @@ -244,10 +252,10 @@ int lsfs_disk_read_data_from_file(lsfs_file *file, int buffer_size, void* buffer int return_val = 0; for (int i = 0; i < NUM_DATA_POINTERS; ++i) { - if(file->data[i] == 0) { + if(file->data_pointer[i] == 0) { break; } - return_val += read_data_from_disk(file->data[i], file->file_block_size, buffer_for_data + (SECTOR_SIZE * i)); + return_val += read_data_from_disk(file->data_pointer[i], file->file_block_size, buffer_for_data + (SECTOR_SIZE * i)); } //time_t current_time; @@ -265,50 +273,70 @@ static inline time_t lsfs_disk_update_timestamps(lsfs_file *file) { #define lsfs_num_sectors_for_size(x) (((x)+SECTOR_SIZE-1)&~(SECTOR_SIZE-1)) -int lsfs_disk_write_data_to_file(lsfs_file *file, int data_length, char *data) { - int written; +int lsfs_disk_write_data_to_file(lsfs_file *file, int data_length, char *data, size_t offset_to_next_entry) { + int new_filesize = data_length + offset_to_next_entry; int amount_written = data_length; + //printf("Data length: %d\n", data_length); + //printf("Offset length: %d\n", offset_to_next_entry); - lsfs_sector_offset current_sector = file->size / SECTOR_SIZE; - unsigned int offset_in_sector = file->size % SECTOR_SIZE; - char *tmp_buffer = calloc(file->file_block_size, SECTOR_SIZE); - assert(tmp_buffer); - - read_data_from_disk(file->data[current_sector], file->file_block_size, tmp_buffer); + int data_pointer_index = 0; // start at first data pointer. + while(data_length > 0) // We have more to write + { + if (file->data_pointer[data_pointer_index] == 0) + { + // we have to assign a free sector + get_free_sectors(1, (lsfs_sector_offset*) file->data_pointer[data_pointer_index]); + } - memcpy(tmp_buffer + offset_in_sector, data, SECTOR_SIZE-offset_in_sector); - data_length -= SECTOR_SIZE-offset_in_sector; + if (offset_to_next_entry == 0) + { + char *tmp_buffer = calloc(DEFAULT_DATA_POINTER_SIZE, SECTOR_SIZE); + assert(tmp_buffer); - if (data_length < 0) { - data_length = 0; - } + memcpy(tmp_buffer, data, data_length); + data_length -= (DEFAULT_DATA_POINTER_SIZE * SECTOR_SIZE); - for (;;) { - assert(current_sector <= NUM_DATA_POINTERS); - written = written + write_data_to_disk(p_control.master_table.entries[file->file_id].data_pointer[current_sector], 4, tmp_buffer); - if (data_length <= 0) break; - data += SECTOR_SIZE; - if (data_length >= SECTOR_SIZE) { - memcpy(tmp_buffer, data, SECTOR_SIZE); - data_length -= SECTOR_SIZE; + write_data_to_disk(file->data_pointer[data_pointer_index], DEFAULT_DATA_POINTER_SIZE, tmp_buffer); + data_pointer_index++; + free(tmp_buffer); } - else { - memset(tmp_buffer, 0, SECTOR_SIZE); + else if (offset_to_next_entry <= (DEFAULT_DATA_POINTER_SIZE * SECTOR_SIZE)) + { + char *tmp_buffer = calloc(1, ((DEFAULT_DATA_POINTER_SIZE * SECTOR_SIZE) - offset_to_next_entry)); + assert(tmp_buffer); + memcpy(tmp_buffer, data, data_length); - data_length = 0; + data_length -= ((DEFAULT_DATA_POINTER_SIZE * SECTOR_SIZE) - offset_to_next_entry); + + + write_data_to_disk_off(file->data_pointer[data_pointer_index], DEFAULT_DATA_POINTER_SIZE, tmp_buffer, offset_to_next_entry); + data_pointer_index++; + free(tmp_buffer); + } + else + { + // We have to skip a whole data pointer: + offset_to_next_entry -= (DEFAULT_DATA_POINTER_SIZE * SECTOR_SIZE); + data_pointer_index++; } - } - amount_written -= data_length; + } - free(tmp_buffer); + time_t current_time; + time ( ¤t_time ); //lsfs_disk_update_timestamps(&mif_record); - file->size += amount_written; // update file size + file->size = new_filesize; // update file size + + file->access_time = current_time; + file->modification_time = current_time; + save_modified_file_information(file); //write_data_to_disk(file->file_id, 4, &p_control.master_table[file->file_id]); + + // Should return the total new file size return amount_written; } @@ -337,7 +365,7 @@ int lsfs_disk_rename_file(lsfs_file* file, const char* new_filename) { save_modified_file_information(file); - return 1; + return 0; } int lsfs_disk_delete_file(lsfs_file_id file_id) { @@ -489,11 +517,12 @@ int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind) { // First check if file exist: - lsfs_file *file; + lsfs_file *file = calloc(1, sizeof(lsfs_file)); if (lsfs_disk_getattr(file, path)) { return -EINVAL; } + free(file); // Start from the master table int free_index = -1; // -1 is no index found. @@ -504,7 +533,7 @@ int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind) lsfs_string_array split_path = lsfs_string_split_c(path, '/', false); lsfs_string filename = split_path.strings[split_path.length-1]; - printf("spilt length: %d\n", split_path.length); + //printf("spilt length: %d\n", split_path.length); for (int i = 0; i < split_path.length -1; ++i) { @@ -513,14 +542,14 @@ int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind) if (strcmp(dir_table->entries[j].filename, split_path.strings[i].chars) == 0) { // We have found the next directory to traverse. - printf("Get next dir at sector: "); + //printf("Get next dir at sector: "); table_disk_position = dir_table->entries[j].data_pointer[0]; if (i == 0) { // Alocate space, we refuse this further on dir_table = malloc(sizeof(Directory_Table)); } - printf("%d\n", table_disk_position); + //printf("%d\n", table_disk_position); read_data_from_disk(table_disk_position, DEFAULT_TABLE_SIZE, dir_table); break; } @@ -544,13 +573,6 @@ int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind) } } - - - - - - - if (free_index == -1) { // The table is full, and we cannot create an entry @@ -563,6 +585,7 @@ int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind) sprintf(dir_table->entries[free_index].filename, "%s", filename.chars); dir_table->entries[free_index].entry_kind = entry_kind; + dir_table->entries[free_index].table_entry_sector_index = table_disk_position; if (entry_kind == ENTRY_DIRECTORY) { dir_table->entries[free_index].data_pointer[0] = get_free_sectors_table(); @@ -589,7 +612,7 @@ int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind) time_t current_time; time ( ¤t_time ); */ - printf("File is written to sector: %d\n", table_disk_position); + //printf("File is written to sector: %d\n", table_disk_position); write_data_to_disk(table_disk_position, 1, &dir_table->entries[free_index]); return 0; } @@ -597,12 +620,17 @@ int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind) int save_modified_file_information(lsfs_file* file) { // Write the file struct into the table_entry, such that we can save the data correct. - p_control.master_table.entries[file->table_entry_pointer].file_id = file->file_id; - memcpy(p_control.master_table.entries[file->table_entry_pointer].filename, file->filename, 256); - p_control.master_table.entries[file->table_entry_pointer].file_size = file->size; // p_control.master_table.entries[i].data_pointer[0]; //; + Table_Entry *entry = calloc(1, sizeof(Table_Entry)); + read_data_from_disk(file->table_entry_sector_index, DEFAULT_ENTRY_SIZE, entry); + + + //entry.file_id = file->file_id; + memcpy(entry->filename, file->filename, 256); + printf("Filse size: %d\n", file->size); + entry->file_size = file->size; // p_control.master_table.entries[i].data_pointer[0]; //; //p_control.master_table.entries[i].data_pointer = find_file->data; - write_data_to_disk(file->table_entry_pointer, file->file_block_size, &p_control.master_table.entries[file->table_entry_pointer]); + write_data_to_disk(file->table_entry_sector_index, DEFAULT_ENTRY_SIZE, entry); return 0; } @@ -613,9 +641,9 @@ int write_data_to_disk(lsfs_sector_offset index, uint32_t file_block_size, void* return written; } -int write_data_to_disk_off(lsfs_sector_offset index, void* data_to_write, int offset) { - fseek ( disk, (index * SECTOR_SIZE) + offset, 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 write_data_to_disk_off(lsfs_sector_offset index, uint32_t file_block_size, void* data_to_write, int offset) { + fseek ( disk, ((index * SECTOR_SIZE) + offset), SEEK_SET ); // SEEK_SET start offset at index 0 and move 1 * SECTOR_SIZE, and write here. + int written = fwrite(data_to_write, 1, ((file_block_size * SECTOR_SIZE) - offset), disk); return written; } diff --git a/lsfs_fuse b/lsfs_fuse index 683a28f..e6a53dd 100755 Binary files a/lsfs_fuse and b/lsfs_fuse differ diff --git a/lsfs_fuse.c b/lsfs_fuse.c index 952cec0..c87672d 100644 --- a/lsfs_fuse.c +++ b/lsfs_fuse.c @@ -39,7 +39,7 @@ static struct fuse_operations lsfs_oper = { .read = lsfs_read, .release = lsfs_release, .write = lsfs_write, - .rename = NULL, + .rename = lsfs_rename, .utime = lsfs_utime_STUB, }; @@ -80,13 +80,16 @@ int lsfs_truncate(const char *path, off_t offset) { int lsfs_rename(const char *path, const char *to) { (void)path; (void)to; - return 0; + lsfs_file *found_file = calloc(1, sizeof(lsfs_file)); + + lsfs_disk_getattr(found_file, path); + return lsfs_disk_rename_file(found_file, 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); + //printf("getattr: (path=%s)\n", path); memset(stbuf, 0, sizeof(struct stat)); if( strcmp( path, "/" ) == 0 ) { @@ -124,26 +127,27 @@ int lsfs_getattr( const char *path, struct stat *stbuf ) { } int lsfs_write(const char *path, const char *content, size_t content_length, off_t offset_to_next_entry, struct fuse_file_info *file_info) { - (void) offset_to_next_entry; + //(void) offset_to_next_entry; //(void) file_info; int res; + //printf("WRITE: offset id: %d\n", offset_to_next_entry); // printf("read: (path=%s)\n", path); - time_t current_time; - time ( ¤t_time ); + //time_t current_time; + //time ( ¤t_time ); - res = lsfs_disk_write_data_to_file(((lsfs_file*) file_info->fh), content_length, (void*) content); + res = lsfs_disk_write_data_to_file(((lsfs_file*) file_info->fh), content_length, (void*) content, offset_to_next_entry); - ((lsfs_file*) file_info->fh)->size += res; - ((lsfs_file*) file_info->fh)->access_time = current_time; - ((lsfs_file*) file_info->fh)->modification_time = current_time; + //((lsfs_file*) file_info->fh)->size += res; + //((lsfs_file*) file_info->fh)->access_time = current_time; + //((lsfs_file*) file_info->fh)->modification_time = current_time; return res; } int lsfs_readdir( const char *path, void *buf, fuse_fill_dir_t filler, off_t offset_to_next_entry, struct fuse_file_info *fi ) { //(void) offset; (void) fi; - printf("readdir: (path=%s)\n", path); + //printf("readdir: (path=%s)\n", path); filler(buf, ".", NULL, 0); filler(buf, "..", NULL, 0); diff --git a/lsfs_fuse.o b/lsfs_fuse.o index b774a3f..66369a8 100644 Binary files a/lsfs_fuse.o and b/lsfs_fuse.o differ