|
|
@ -10,8 +10,9 @@ |
|
|
|
#include <time.h> |
|
|
|
#include <unistd.h> |
|
|
|
|
|
|
|
typedef struct Directory_Table Directory_Table; |
|
|
|
typedef struct struct_table_entry struct_table_entry; |
|
|
|
typedef struct struct_partition_control partition_control; |
|
|
|
typedef struct struct_table_entry table_entry; |
|
|
|
typedef struct struct_partition_control partition_control; |
|
|
|
typedef struct File_System_Control_Information FSCI; |
|
|
|
typedef struct meta_information_format mif; |
|
|
@ -58,10 +59,6 @@ int save_modified_file_information(lsfs_file* file); |
|
|
|
//#define MAX_NUM_TWO_LEVEL_DATA 94 // First Mib of a file.
|
|
|
|
//#define MAX_NUM_THREE_LEVEL_DATA 94 // First Mib of a file.
|
|
|
|
|
|
|
|
typedef struct struct_partition_control{ |
|
|
|
FSCI* fsci; |
|
|
|
table_entry* master_table; |
|
|
|
} __attribute__((packed)) partition_control; |
|
|
|
|
|
|
|
typedef struct Partition_Entry |
|
|
|
{ |
|
|
@ -74,13 +71,35 @@ typedef struct Partition_Entry |
|
|
|
|
|
|
|
} __attribute__((packed)) Partition_Entry; |
|
|
|
|
|
|
|
typedef struct Master_Boot_record { |
|
|
|
typedef struct Master_Boot_record |
|
|
|
{ |
|
|
|
uint8_t offset_on_disk[446]; // The code for the bootloader
|
|
|
|
Partition_Entry partitions[4]; |
|
|
|
uint8_t master_tag_records[2]; // Signature
|
|
|
|
} __attribute__((packed)) Master_Boot_record; |
|
|
|
|
|
|
|
typedef struct File_System_Control_Information { |
|
|
|
typedef struct struct_table_entry |
|
|
|
{ |
|
|
|
char filename[256]; |
|
|
|
lsfs_file_id file_id; |
|
|
|
uint64_t file_size; |
|
|
|
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.
|
|
|
|
uint8_t entry_kind; |
|
|
|
uint8_t extra_control_bits1; |
|
|
|
uint8_t extra_control_bits2; |
|
|
|
uint8_t extra_control_bits3; |
|
|
|
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; |
|
|
|
|
|
|
|
typedef struct Directory_Table |
|
|
|
{ |
|
|
|
struct_table_entry entries[10]; |
|
|
|
|
|
|
|
} __attribute__((packed)) Directory_Table; |
|
|
|
|
|
|
|
typedef struct File_System_Control_Information |
|
|
|
{ |
|
|
|
uint64_t offset_on_disk; |
|
|
|
uint64_t next_partition; |
|
|
|
uint64_t maximum_sectors_on_partition; |
|
|
@ -91,23 +110,18 @@ typedef struct File_System_Control_Information { |
|
|
|
uint64_t master_tag_records[16]; |
|
|
|
} __attribute__((packed)) FSCI; |
|
|
|
|
|
|
|
typedef struct struct_table_entry { |
|
|
|
char filename[256]; |
|
|
|
lsfs_file_id file_id; |
|
|
|
uint64_t file_size; |
|
|
|
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 { |
|
|
|
uint32_t is_filename : 1; |
|
|
|
} control_bits; |
|
|
|
lsfs_sector_offset data_pointer[NUM_DATA_POINTERS]; |
|
|
|
} __attribute__((packed)) table_entry; |
|
|
|
typedef struct struct_partition_control |
|
|
|
{ |
|
|
|
FSCI fsci; |
|
|
|
Directory_Table master_table; |
|
|
|
} __attribute__((packed)) partition_control; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct meta_information_format { |
|
|
|
char filename[246]; // remeber that the 246 bytes has to be a /0 terminator..
|
|
|
|
uint32_t owner_id; |
|
|
|
lsfs_file_id tags[32]; |
|
|
|
lsfs_file_id tags[32]; |
|
|
|
uint64_t file_size; |
|
|
|
uint32_t control_bits; |
|
|
|
/* not pressent - Permission key table 64 bytes sha-265 pr. key*/ |
|
|
@ -152,20 +166,20 @@ typedef struct 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) { |
|
|
|
while((p_control.master_table.entries[i].filename[0]) != 0 && !found) { |
|
|
|
if(strcmp( (path + 1 ), p_control.master_table.entries[i].filename ) == 0) { |
|
|
|
time_t current_time; |
|
|
|
time ( ¤t_time ); |
|
|
|
|
|
|
|
find_file->file_id = p_control.master_table[i].file_id; |
|
|
|
find_file->file_id = p_control.master_table.entries[i].file_id; |
|
|
|
find_file->table_entry_pointer = i; |
|
|
|
find_file->filename = p_control.master_table[i].filename; |
|
|
|
find_file->filename = p_control.master_table.entries[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->size = p_control.master_table.entries[i].file_size; // p_control.master_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[i].data_pointer; |
|
|
|
find_file->data = p_control.master_table.entries[i].data_pointer; |
|
|
|
find_file->file_block_size = 1; // TODO: should be loaded from disk.
|
|
|
|
found = 1; |
|
|
|
} |
|
|
@ -224,7 +238,7 @@ int lsfs_disk_write_data_to_file(lsfs_file *file, int data_length, char *data) { |
|
|
|
|
|
|
|
for (;;) { |
|
|
|
assert(current_sector <= NUM_DATA_POINTERS); |
|
|
|
written = written + write_data_to_disk(p_control.master_table[file->file_id].data_pointer[current_sector], 4, tmp_buffer); |
|
|
|
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; |
|
|
@ -255,7 +269,7 @@ time_t lsfs_disk_truncate_file(lsfs_file *file, off_t offset) { |
|
|
|
//read_data_from_disk(file_id, &file_mif);
|
|
|
|
|
|
|
|
time_t result = lsfs_disk_update_timestamps(file); |
|
|
|
file->size = (int) offset; // p_control.master_table[i].data_pointer[0]; //;
|
|
|
|
file->size = (int) offset; // p_control.master_table.entries[i].data_pointer[0]; //;
|
|
|
|
|
|
|
|
save_modified_file_information(file); |
|
|
|
//write_data_to_disk(file->file_id, 4, NULL);
|
|
|
@ -265,16 +279,16 @@ time_t lsfs_disk_truncate_file(lsfs_file *file, off_t offset) { |
|
|
|
// 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);
|
|
|
|
// if(p_control.master_table.entries[i].file_id == file_id) {
|
|
|
|
// memset(p_control.master_table.entries[i].filename, 0, sizeof(p_control.master_table.entries[i].filename));
|
|
|
|
// sprintf(p_control.master_table.entries[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 );
|
|
|
|
// 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;
|
|
|
|
// }
|
|
|
@ -330,13 +344,13 @@ 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].file_id == file_id) { |
|
|
|
p_control.master_table[i] = p_control.master_table[i+1]; |
|
|
|
if(p_control.master_table.entries[i].file_id == file_id) { |
|
|
|
p_control.master_table.entries[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]; |
|
|
|
p_control.master_table.entries[i] = p_control.master_table[i+1]; |
|
|
|
if (p_control.master_table[i+1].file_id == 0) { |
|
|
|
break; |
|
|
|
} |
|
|
@ -345,7 +359,7 @@ int lsfs_disk_delete_file(lsfs_file_id file_id) { |
|
|
|
// 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 ); |
|
|
|
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); |
|
|
|
free(tag_table); |
|
|
|
return 1; |
|
|
@ -460,7 +474,7 @@ int lsfs_disk_delete_file(lsfs_file_id file_id) { |
|
|
|
//printf("Sector number: %lu, is assignt to you \n", (*free_sectors));
|
|
|
|
//char* data = "red_file_1\nred_file_2\n";
|
|
|
|
//char* sector_to_write = write_mechanism_new_buffer(data);
|
|
|
|
fseek ( disk , (p_control.fsci->master_tag_records[0] * SECTOR_SIZE) , SEEK_SET ); |
|
|
|
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); |
|
|
|
|
|
|
|
fseek ( disk , ((*free_sectors) * SECTOR_SIZE) , SEEK_SET ); |
|
|
@ -485,17 +499,17 @@ int get_free_sectors(int num_sectors_needed, lsfs_sector_offset* output_array) { |
|
|
|
if (num_sectors_needed > 1) { |
|
|
|
for (int i = 0; i < num_sectors_needed; ++i) |
|
|
|
{ |
|
|
|
output_array[i] = p_control.fsci->next_free_sector; |
|
|
|
p_control.fsci->next_free_sector++; |
|
|
|
output_array[i] = p_control.fsci.next_free_sector; |
|
|
|
p_control.fsci.next_free_sector++; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
output_array[0] = p_control.fsci->next_free_sector; |
|
|
|
p_control.fsci->next_free_sector++; |
|
|
|
output_array[0] = p_control.fsci.next_free_sector; |
|
|
|
p_control.fsci.next_free_sector++; |
|
|
|
} |
|
|
|
printf("Sector %lu is assignt\n", output_array[0]); |
|
|
|
write_data_to_disk(0, 4, p_control.fsci); |
|
|
|
return p_control.fsci->next_free_sector; |
|
|
|
write_data_to_disk(0, 4, &p_control.fsci); |
|
|
|
return p_control.fsci.next_free_sector; |
|
|
|
} |
|
|
|
|
|
|
|
int create_file_system() { |
|
|
@ -547,7 +561,7 @@ int lsfs_disk_load_disk() { |
|
|
|
printf("%d\n", mbr.partitions[i].LBA_abs_first_sector); |
|
|
|
// First we find the Mater Table.
|
|
|
|
fseek ( disk , mbr.partitions[i].LBA_abs_first_sector * SECTOR_SIZE, SEEK_SET ); |
|
|
|
fread(p_control.master_table, 1, sizeof(table_entry) * DEFAULT_MASTER_TABLE_SIZE , disk); |
|
|
|
fread(&p_control.master_table, 1, sizeof(table_entry) * DEFAULT_MASTER_TABLE_SIZE , disk); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
} |
|
|
@ -616,12 +630,12 @@ lsfs_file_id lsfs_disk_create_file(char* filename, lsfs_file_id* tags, void* fil |
|
|
|
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[file->table_entry_pointer].file_id = file->file_id; |
|
|
|
memcpy(p_control.master_table[file->table_entry_pointer].filename, file->filename, 256); |
|
|
|
p_control.master_table[file->table_entry_pointer].file_size = file->size; // p_control.master_table[i].data_pointer[0]; //;
|
|
|
|
//p_control.master_table[i].data_pointer = find_file->data;
|
|
|
|
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]; //;
|
|
|
|
//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[file->table_entry_pointer]); |
|
|
|
write_data_to_disk(file->table_entry_pointer, file->file_block_size, &p_control.master_table.entries[file->table_entry_pointer]); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|