|
|
@ -34,12 +34,12 @@ int lsfs_disk_getattr(lsfs_file* find_file, const char *path); |
|
|
|
//int lsfs_disk_delete_tag(lsfs_file_id file_id);
|
|
|
|
int lsfs_disk_delete_file(lsfs_file_id file_id); |
|
|
|
int get_free_sectors(int num_sectors_needed, lsfs_sector_offset* output_array); |
|
|
|
int lsfs_disk_read_data_from_file(lsfs_file_id file_id, int buffer_size, void* buffer_for_data); |
|
|
|
int lsfs_disk_write_data_to_file(lsfs_file_id file_id, int data_length, char *data); |
|
|
|
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_rename_tag(lsfs_file_id file_id, char* new_filename);
|
|
|
|
int lsfs_disk_rename_file(lsfs_file_id file_id, const char* new_filename); |
|
|
|
int lsfs_disk_load_disk(); |
|
|
|
int write_data_to_disk(lsfs_sector_offset at_sector, void* data_to_write); |
|
|
|
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, void* data_buffer); |
|
|
|
int write_data_to_disk_off(lsfs_sector_offset index, void* data_to_write, int offset); |
|
|
|
|
|
|
@ -76,9 +76,10 @@ typedef struct struct_table_entry { |
|
|
|
char filename[256]; |
|
|
|
lsfs_file_id file_id; |
|
|
|
uint64_t file_size; |
|
|
|
mif* ext_file_data; |
|
|
|
mif* ext_file_data; |
|
|
|
uint32_t file_block_size; // This tells how many block there are allocated for the specific file. eg. we read this amount of bloks for the file.
|
|
|
|
struct { |
|
|
|
uint64_t is_filename : 1; |
|
|
|
uint32_t is_filename : 1; |
|
|
|
} control_bits; |
|
|
|
lsfs_sector_offset data_pointer[NUM_DATA_POINTERS] |
|
|
|
} __attribute__((packed)) table_entry; |
|
|
@ -120,7 +121,7 @@ typedef struct lsfs_file { |
|
|
|
char* filename; |
|
|
|
uint32_t owner_id; |
|
|
|
uint64_t size; |
|
|
|
uint64_t creation_date; |
|
|
|
uint64_t creation_date; |
|
|
|
uint64_t access_time; |
|
|
|
uint64_t modification_time; |
|
|
|
lsfs_sector_offset *data; |
|
|
@ -143,6 +144,7 @@ int lsfs_disk_getattr(lsfs_file* find_file, const char* path) { |
|
|
|
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[i].data_pointer; |
|
|
|
found = 1; |
|
|
|
} |
|
|
|
i++; |
|
|
@ -152,16 +154,16 @@ int lsfs_disk_getattr(lsfs_file* find_file, const char* path) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int lsfs_disk_read_data_from_file(lsfs_file_id file_id, int buffer_size, void* buffer_for_data) { |
|
|
|
int lsfs_disk_read_data_from_file(lsfs_file *file, int buffer_size, void* buffer_for_data) { |
|
|
|
// TODO some offset, to tell where in the file we want to write
|
|
|
|
|
|
|
|
int return_val = 0; |
|
|
|
|
|
|
|
for (int i = 0; i < NUM_DATA_POINTERS; ++i) { |
|
|
|
if(p_control.master_table[file_id].data_pointer[i] == 0) { |
|
|
|
if(file->data[i] == 0) { |
|
|
|
break; |
|
|
|
} |
|
|
|
return_val += read_data_from_disk(p_control.master_table[file_id].data_pointer[i], buffer_for_data + (SECTOR_SIZE * i)); |
|
|
|
return_val += read_data_from_disk(file->data[i], buffer_for_data + (SECTOR_SIZE * i)); |
|
|
|
} |
|
|
|
|
|
|
|
//time_t current_time;
|
|
|
@ -173,25 +175,22 @@ int lsfs_disk_read_data_from_file(lsfs_file_id file_id, int buffer_size, void* b |
|
|
|
return return_val; |
|
|
|
} |
|
|
|
|
|
|
|
static inline time_t lsfs_disk_update_timestamps(mif *file) { |
|
|
|
return file->last_modification_data = file->last_access_date = time(NULL); |
|
|
|
static inline time_t lsfs_disk_update_timestamps(lsfs_file *file) { |
|
|
|
return file->modification_time = file->access_time = time(NULL); |
|
|
|
} |
|
|
|
|
|
|
|
#define lsfs_num_sectors_for_size(x) (((x)+SECTOR_SIZE-1)&~(SECTOR_SIZE-1)) |
|
|
|
|
|
|
|
int lsfs_disk_write_data_to_file(lsfs_file_id file_id, int data_length, char *data) { |
|
|
|
int lsfs_disk_write_data_to_file(lsfs_file *file, int data_length, char *data) { |
|
|
|
int amount_written = data_length; |
|
|
|
|
|
|
|
mif mif_record; |
|
|
|
read_data_from_disk(file_id, &mif_record); |
|
|
|
|
|
|
|
lsfs_sector_offset current_sector = mif_record.file_size / SECTOR_SIZE; |
|
|
|
unsigned int offset_in_sector = mif_record.file_size % SECTOR_SIZE; |
|
|
|
lsfs_sector_offset current_sector = file->size / SECTOR_SIZE; |
|
|
|
unsigned int offset_in_sector = file->size % SECTOR_SIZE; |
|
|
|
|
|
|
|
char *tmp_buffer = malloc(SECTOR_SIZE); |
|
|
|
assert(tmp_buffer); |
|
|
|
|
|
|
|
read_data_from_disk(mif_record.one_level_pointer_data[current_sector], tmp_buffer); |
|
|
|
read_data_from_disk(file->data[current_sector], tmp_buffer); |
|
|
|
|
|
|
|
memcpy(tmp_buffer + offset_in_sector, data, SECTOR_SIZE-offset_in_sector); |
|
|
|
data_length -= SECTOR_SIZE-offset_in_sector; |
|
|
@ -202,7 +201,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(p_control.master_table[file_id].data_pointer[current_sector], tmp_buffer); |
|
|
|
write_data_to_disk(p_control.master_table[file->file_id].data_pointer[current_sector], 4, tmp_buffer); |
|
|
|
if (data_length <= 0) break; |
|
|
|
|
|
|
|
data += SECTOR_SIZE; |
|
|
@ -222,38 +221,41 @@ 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);
|
|
|
|
p_control.master_table[file_id].file_size += amount_written; // update file size
|
|
|
|
p_control.master_table[file->file_id].file_size += amount_written; // update file size
|
|
|
|
|
|
|
|
write_data_to_disk(file_id, &p_control.master_table[file_id]); |
|
|
|
write_data_to_disk(file->file_id, 4, &p_control.master_table[file->file_id]); |
|
|
|
return amount_written; |
|
|
|
} |
|
|
|
|
|
|
|
time_t lsfs_disk_truncate_file(lsfs_file_id file_id) { |
|
|
|
mif file_mif; |
|
|
|
read_data_from_disk(file_id, &file_mif); |
|
|
|
time_t result = lsfs_disk_update_timestamps(&file_mif); |
|
|
|
file_mif.file_size = 0; |
|
|
|
write_data_to_disk(file_id, &file_mif); |
|
|
|
return result; |
|
|
|
} |
|
|
|
time_t lsfs_disk_truncate_file(lsfs_file *file, off_t offset) { |
|
|
|
//mif file_mif;
|
|
|
|
//read_data_from_disk(file_id, &file_mif);
|
|
|
|
|
|
|
|
time_t result = lsfs_disk_update_timestamps(file); |
|
|
|
|
|
|
|
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].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; |
|
|
|
} |
|
|
|
} |
|
|
|
// free the sectors including the tag table
|
|
|
|
file->size = (int) offset; // p_control.master_table[i].data_pointer[0]; //;
|
|
|
|
|
|
|
|
// Save the changes to disk
|
|
|
|
fseek ( disk , (p_control.fsci->master_tag_records[0] * SECTOR_SIZE) , SEEK_SET ); |
|
|
|
fwrite(p_control.master_table, 1, sizeof(table_entry) * DEFAULT_MASTER_TABLE_SIZE, disk); |
|
|
|
return 1; |
|
|
|
write_data_to_disk(file->file_id, 4, NULL); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
// 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].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;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// // free the sectors including the tag table
|
|
|
|
|
|
|
|
// // Save the changes to disk
|
|
|
|
// fseek ( disk , (p_control.fsci->master_tag_records[0] * SECTOR_SIZE) , SEEK_SET );
|
|
|
|
// fwrite(p_control.master_table, 1, sizeof(table_entry) * DEFAULT_MASTER_TABLE_SIZE, disk);
|
|
|
|
// return 1;
|
|
|
|
// }
|
|
|
|
|
|
|
|
int lsfs_disk_rename_file(lsfs_file_id file_id, const char* new_filename) { |
|
|
|
mif* mif_record = calloc(1, SECTOR_SIZE); |
|
|
|
read_data_from_disk(file_id, mif_record); |
|
|
@ -267,7 +269,7 @@ int lsfs_disk_rename_file(lsfs_file_id file_id, const char* new_filename) { |
|
|
|
mif_record->last_modification_data = (uint64_t) current_time; |
|
|
|
mif_record->last_access_date = (uint64_t) current_time; |
|
|
|
|
|
|
|
write_data_to_disk(file_id, mif_record); |
|
|
|
write_data_to_disk(file_id, 4, mif_record); |
|
|
|
free(mif_record); |
|
|
|
return 1; |
|
|
|
} |
|
|
@ -471,7 +473,7 @@ int get_free_sectors(int num_sectors_needed, lsfs_sector_offset* output_array) { |
|
|
|
p_control.fsci->next_free_sector++; |
|
|
|
} |
|
|
|
printf("Sector %lu is assignt\n", output_array[0]); |
|
|
|
write_data_to_disk(0, p_control.fsci); |
|
|
|
write_data_to_disk(0, 4, p_control.fsci); |
|
|
|
return p_control.fsci->next_free_sector; |
|
|
|
} |
|
|
|
|
|
|
@ -580,21 +582,21 @@ lsfs_file_id lsfs_disk_create_file(char* filename, lsfs_file_id* tags, void* fil |
|
|
|
return return_id; |
|
|
|
} |
|
|
|
|
|
|
|
int write_data_to_disk(lsfs_sector_offset index, void* data_to_write) { |
|
|
|
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, SECTOR_SIZE, disk); |
|
|
|
int written = fwrite(data_to_write, 1, (4 * SECTOR_SIZE), disk); |
|
|
|
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, SECTOR_SIZE, disk); |
|
|
|
int written = fwrite(data_to_write, 1, (4 * SECTOR_SIZE), disk); |
|
|
|
return written; |
|
|
|
} |
|
|
|
|
|
|
|
int read_data_from_disk(lsfs_sector_offset index, void* data_buffer) { |
|
|
|
fseek ( disk, (index * SECTOR_SIZE ), SEEK_SET ); // SEEK_SET start offset at index 0 and move 1 * SECTOR_SIZE, and write here.
|
|
|
|
int read = fread(data_buffer, 1, SECTOR_SIZE, disk); |
|
|
|
int read = fread(data_buffer, 1, (4 * SECTOR_SIZE), disk); |
|
|
|
return read; |
|
|
|
} |
|
|
|
|
|
|
|