Rewrite for passing file struct instead of just the ID
This commit is contained in:
1 parent
78be21e224
commit
0ff7baf2d6
2 files changed
+57
-54
No files matched your search
+48
-46
@@ -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_tag(lsfs_file_id file_id);
|
||||||
int lsfs_disk_delete_file(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 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_read_data_from_file(lsfs_file* file, 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_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_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_rename_file(lsfs_file_id file_id, const char* new_filename);
|
||||||
int lsfs_disk_load_disk();
|
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 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);
|
int write_data_to_disk_off(lsfs_sector_offset index, void* data_to_write, int offset);
|
||||||
|
|
||||||
@@ -77,8 +77,9 @@ typedef struct struct_table_entry {
|
|||||||
lsfs_file_id file_id;
|
lsfs_file_id file_id;
|
||||||
uint64_t file_size;
|
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 {
|
struct {
|
||||||
uint64_t is_filename : 1;
|
uint32_t is_filename : 1;
|
||||||
} control_bits;
|
} control_bits;
|
||||||
lsfs_sector_offset data_pointer[NUM_DATA_POINTERS]
|
lsfs_sector_offset data_pointer[NUM_DATA_POINTERS]
|
||||||
} __attribute__((packed)) table_entry;
|
} __attribute__((packed)) table_entry;
|
||||||
@@ -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->creation_date = (uint64_t) current_time;
|
||||||
find_file->access_time = (uint64_t) current_time;
|
find_file->access_time = (uint64_t) current_time;
|
||||||
find_file->modification_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;
|
found = 1;
|
||||||
}
|
}
|
||||||
i++;
|
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
|
// TODO some offset, to tell where in the file we want to write
|
||||||
|
|
||||||
int return_val = 0;
|
int return_val = 0;
|
||||||
|
|
||||||
for (int i = 0; i < NUM_DATA_POINTERS; ++i) {
|
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;
|
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;
|
//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;
|
return return_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline time_t lsfs_disk_update_timestamps(mif *file) {
|
static inline time_t lsfs_disk_update_timestamps(lsfs_file *file) {
|
||||||
return file->last_modification_data = file->last_access_date = time(NULL);
|
return file->modification_time = file->access_time = time(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define lsfs_num_sectors_for_size(x) (((x)+SECTOR_SIZE-1)&~(SECTOR_SIZE-1))
|
#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;
|
int amount_written = data_length;
|
||||||
|
|
||||||
mif mif_record;
|
lsfs_sector_offset current_sector = file->size / SECTOR_SIZE;
|
||||||
read_data_from_disk(file_id, &mif_record);
|
unsigned int offset_in_sector = file->size % SECTOR_SIZE;
|
||||||
|
|
||||||
lsfs_sector_offset current_sector = mif_record.file_size / SECTOR_SIZE;
|
|
||||||
unsigned int offset_in_sector = mif_record.file_size % SECTOR_SIZE;
|
|
||||||
|
|
||||||
char *tmp_buffer = malloc(SECTOR_SIZE);
|
char *tmp_buffer = malloc(SECTOR_SIZE);
|
||||||
assert(tmp_buffer);
|
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);
|
memcpy(tmp_buffer + offset_in_sector, data, SECTOR_SIZE-offset_in_sector);
|
||||||
data_length -= 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 (;;) {
|
for (;;) {
|
||||||
assert(current_sector <= NUM_DATA_POINTERS);
|
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;
|
if (data_length <= 0) break;
|
||||||
|
|
||||||
data += SECTOR_SIZE;
|
data += SECTOR_SIZE;
|
||||||
@@ -222,37 +221,40 @@ int lsfs_disk_write_data_to_file(lsfs_file_id file_id, int data_length, char *da
|
|||||||
free(tmp_buffer);
|
free(tmp_buffer);
|
||||||
|
|
||||||
//lsfs_disk_update_timestamps(&mif_record);
|
//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;
|
return amount_written;
|
||||||
}
|
}
|
||||||
|
|
||||||
time_t lsfs_disk_truncate_file(lsfs_file_id file_id) {
|
time_t lsfs_disk_truncate_file(lsfs_file *file, off_t offset) {
|
||||||
mif file_mif;
|
//mif file_mif;
|
||||||
read_data_from_disk(file_id, &file_mif);
|
//read_data_from_disk(file_id, &file_mif);
|
||||||
time_t result = lsfs_disk_update_timestamps(&file_mif);
|
|
||||||
file_mif.file_size = 0;
|
time_t result = lsfs_disk_update_timestamps(file);
|
||||||
write_data_to_disk(file_id, &file_mif);
|
|
||||||
|
file->size = (int) offset; // p_control.master_table[i].data_pointer[0]; //;
|
||||||
|
|
||||||
|
write_data_to_disk(file->file_id, 4, NULL);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int lsfs_disk_rename_tag(lsfs_file_id file_id, char* new_filename) {
|
// int lsfs_disk_rename_tag(lsfs_file_id file_id, char* new_filename) {
|
||||||
for (int i = 0; i < MAX_MASTER_TAGS; ++i)
|
// for (int i = 0; i < MAX_MASTER_TAGS; ++i)
|
||||||
{
|
// {
|
||||||
if(p_control.master_table[i].file_id == 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));
|
// 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);
|
// sprintf(p_control.master_table[i].filename, "%s", new_filename);
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
// free the sectors including the tag table
|
// // free the sectors including the tag table
|
||||||
|
|
||||||
// Save the changes to disk
|
// // 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);
|
// fwrite(p_control.master_table, 1, sizeof(table_entry) * DEFAULT_MASTER_TABLE_SIZE, disk);
|
||||||
return 1;
|
// return 1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
int lsfs_disk_rename_file(lsfs_file_id file_id, const char* new_filename) {
|
int lsfs_disk_rename_file(lsfs_file_id file_id, const char* new_filename) {
|
||||||
mif* mif_record = calloc(1, SECTOR_SIZE);
|
mif* mif_record = calloc(1, SECTOR_SIZE);
|
||||||
@@ -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_modification_data = (uint64_t) current_time;
|
||||||
mif_record->last_access_date = (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);
|
free(mif_record);
|
||||||
return 1;
|
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++;
|
p_control.fsci->next_free_sector++;
|
||||||
}
|
}
|
||||||
printf("Sector %lu is assignt\n", output_array[0]);
|
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;
|
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;
|
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.
|
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;
|
return written;
|
||||||
}
|
}
|
||||||
|
|
||||||
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, 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.
|
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;
|
return written;
|
||||||
}
|
}
|
||||||
|
|
||||||
int read_data_from_disk(lsfs_sector_offset index, void* data_buffer) {
|
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.
|
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;
|
return read;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+9
-8
@@ -71,12 +71,13 @@ int lsfs_unlink(const char *path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int lsfs_truncate(const char *path, off_t offset) {
|
int lsfs_truncate(const char *path, off_t offset) {
|
||||||
(void)offset;
|
(void)offset; // Truncate to this byte?
|
||||||
|
|
||||||
|
lsfs_file *found_file = calloc(1, sizeof(lsfs_file));
|
||||||
|
|
||||||
//found_file->modification_time = modification_time
|
lsfs_disk_getattr(found_file, path);
|
||||||
|
lsfs_disk_truncate_file(found_file, offset);
|
||||||
// Truncate a file
|
free(found_file);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -196,7 +197,7 @@ int lsfs_write(const char *path, const char *content, size_t content_length, off
|
|||||||
time_t current_time;
|
time_t current_time;
|
||||||
time ( ¤t_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), content_length, (void*) content);
|
||||||
|
|
||||||
//((lsfs_file*) file_info->fh)->size += res;
|
//((lsfs_file*) file_info->fh)->size += res;
|
||||||
//((lsfs_file*) file_info->fh)->access_time = current_time;
|
//((lsfs_file*) file_info->fh)->access_time = current_time;
|
||||||
@@ -246,7 +247,7 @@ int lsfs_read( const char *path, char *buf, size_t size, off_t offset_to_next_en
|
|||||||
time_t current_time;
|
time_t current_time;
|
||||||
time ( ¤t_time );
|
time ( ¤t_time );
|
||||||
|
|
||||||
int res = lsfs_disk_read_data_from_file( ((lsfs_file*) fi->fh)->file_id, size, buf);
|
int res = lsfs_disk_read_data_from_file( ((lsfs_file*) fi->fh), size, buf);
|
||||||
((lsfs_file*) fi->fh)->access_time = current_time;
|
((lsfs_file*) fi->fh)->access_time = current_time;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -307,8 +308,8 @@ end:
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main( int argc, char *argv[] ) {
|
int main( int argc, char *argv[] ) {
|
||||||
// "/home/rhodez-x/Documents/github/SingOS/bin/SingOS.img"
|
// "/home/rhodez-x/Documents/github/SingOS/SingOS.img"
|
||||||
disk = fopen ("/home/rhodez-x/Documents/github/SingOS/bin/SingOS.img", "r+b");
|
disk = fopen ("/home/rhodez-x/Documents/github/SingOS/SingOS.img", "r+b");
|
||||||
p_control.fsci = malloc(sizeof(FSCI));
|
p_control.fsci = malloc(sizeof(FSCI));
|
||||||
p_control.master_table = malloc(sizeof(table_entry) * DEFAULT_MASTER_TABLE_SIZE);
|
p_control.master_table = malloc(sizeof(table_entry) * DEFAULT_MASTER_TABLE_SIZE);
|
||||||
lsfs_disk_load_disk();
|
lsfs_disk_load_disk();
|
||||||
|
|||||||
Reference in new issue
Block a user