Browse Source

Folder strucres works, write works, truncate works

master
Jørn Guldberg 6 years ago
parent
commit
157105952b
  1. BIN
      SingOS.img
  2. BIN
      a.out
  3. 146
      lsfs_disk_controller.h
  4. BIN
      lsfs_fuse
  5. 26
      lsfs_fuse.c
  6. BIN
      lsfs_fuse.o

BIN
SingOS.img

Binary file not shown.

BIN
a.out

Binary file not shown.

146
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_table();
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* file, 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* 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_rename_file(lsfs_file* file, const char* new_filename);
int lsfs_disk_load_disk(); 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 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 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); int save_modified_file_information(lsfs_file* file);
#define SPACE_MBR_RECORD 2048 // Sectors #define SPACE_MBR_RECORD 2048 // Sectors
#define SPACE_VBR_RECORD 2048 // Sectors #define SPACE_VBR_RECORD 2048 // Sectors
#define SIZE_FSCI_RECORD 1 // Sectors #define SIZE_FSCI_RECORD 1 // Sectors
#define DEFAULT_ENTRY_SIZE 1 // Sectors
#define SECTOR_SIZE 512 // BYTES #define SECTOR_SIZE 512 // BYTES
#define NUMBER_OF_MBR_PARTITIONS 4 #define NUMBER_OF_MBR_PARTITIONS 4
#define DEFAULT_FILE_SIZE 4 // This is in sectors #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 DEFAULT_TABLE_SIZE 10
#define NUM_DATA_POINTERS 28 #define NUM_DATA_POINTERS 27
typedef enum Table_Entry_Kind typedef enum Table_Entry_Kind
{ {
@ -92,8 +94,9 @@ typedef struct struct_table_entry
uint8_t extra_control_bits1; uint8_t extra_control_bits1;
uint8_t extra_control_bits2; uint8_t extra_control_bits2;
uint8_t extra_control_bits3; 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. 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 typedef struct Directory_Table
{ {
@ -166,7 +169,8 @@ typedef struct lsfs_file {
uint64_t access_time; uint64_t access_time;
uint64_t modification_time; uint64_t modification_time;
uint32_t file_block_size; 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; } 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) 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) 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]; int index_sector = dir_table->entries[j].data_pointer[0];
if (i == 0) if (i == 0)
{ {
// Alocate space, we refuse this further on // 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); read_data_from_disk(index_sector, DEFAULT_TABLE_SIZE, dir_table);
break; 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->entry_kind = dir_table->entries[i].entry_kind;
find_file->table_entry_pointer = i; find_file->table_entry_pointer = i;
find_file->filename = dir_table->entries[i].filename; 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->owner_id = getuid();
find_file->size = dir_table->entries[i].file_size; // dir_table->entries[i].data_pointer[0]; //; 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->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 = 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. find_file->file_block_size = 1; // TODO: should be loaded from disk.
return 1; 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; int return_val = 0;
for (int i = 0; i < NUM_DATA_POINTERS; ++i) { for (int i = 0; i < NUM_DATA_POINTERS; ++i) {
if(file->data[i] == 0) { if(file->data_pointer[i] == 0) {
break; 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; //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)) #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 lsfs_disk_write_data_to_file(lsfs_file *file, int data_length, char *data, size_t offset_to_next_entry) {
int written; int new_filesize = data_length + offset_to_next_entry;
int amount_written = data_length; 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); int data_pointer_index = 0; // start at first data pointer.
assert(tmp_buffer); while(data_length > 0) // We have more to write
{
read_data_from_disk(file->data[current_sector], file->file_block_size, tmp_buffer); 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); if (offset_to_next_entry == 0)
data_length -= SECTOR_SIZE-offset_in_sector; {
char *tmp_buffer = calloc(DEFAULT_DATA_POINTER_SIZE, SECTOR_SIZE);
assert(tmp_buffer);
if (data_length < 0) { memcpy(tmp_buffer, data, data_length);
data_length = 0; 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; write_data_to_disk(file->data_pointer[data_pointer_index], DEFAULT_DATA_POINTER_SIZE, tmp_buffer);
if (data_length >= SECTOR_SIZE) { data_pointer_index++;
memcpy(tmp_buffer, data, SECTOR_SIZE); free(tmp_buffer);
data_length -= SECTOR_SIZE;
} }
else { else if (offset_to_next_entry <= (DEFAULT_DATA_POINTER_SIZE * SECTOR_SIZE))
memset(tmp_buffer, 0, 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); 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 ( &current_time );
//lsfs_disk_update_timestamps(&mif_record); //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); save_modified_file_information(file);
//write_data_to_disk(file->file_id, 4, &p_control.master_table[file->file_id]); //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; return amount_written;
} }
@ -337,7 +365,7 @@ int lsfs_disk_rename_file(lsfs_file* file, const char* new_filename) {
save_modified_file_information(file); save_modified_file_information(file);
return 1; return 0;
} }
int lsfs_disk_delete_file(lsfs_file_id file_id) { 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: // First check if file exist:
lsfs_file *file; lsfs_file *file = calloc(1, sizeof(lsfs_file));
if (lsfs_disk_getattr(file, path)) if (lsfs_disk_getattr(file, path))
{ {
return -EINVAL; return -EINVAL;
} }
free(file);
// Start from the master table // Start from the master table
int free_index = -1; // -1 is no index found. 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_array split_path = lsfs_string_split_c(path, '/', false);
lsfs_string filename = split_path.strings[split_path.length-1]; 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) 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) if (strcmp(dir_table->entries[j].filename, split_path.strings[i].chars) == 0)
{ {
// We have found the next directory to traverse. // 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]; table_disk_position = dir_table->entries[j].data_pointer[0];
if (i == 0) if (i == 0)
{ {
// Alocate space, we refuse this further on // Alocate space, we refuse this further on
dir_table = malloc(sizeof(Directory_Table)); 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); read_data_from_disk(table_disk_position, DEFAULT_TABLE_SIZE, dir_table);
break; break;
} }
@ -544,13 +573,6 @@ int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind)
} }
} }
if (free_index == -1) if (free_index == -1)
{ {
// The table is full, and we cannot create an entry // 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); sprintf(dir_table->entries[free_index].filename, "%s", filename.chars);
dir_table->entries[free_index].entry_kind = entry_kind; 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) if (entry_kind == ENTRY_DIRECTORY)
{ {
dir_table->entries[free_index].data_pointer[0] = get_free_sectors_table(); 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 current_time;
time ( &current_time ); time ( &current_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]); write_data_to_disk(table_disk_position, 1, &dir_table->entries[free_index]);
return 0; 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) { int save_modified_file_information(lsfs_file* file) {
// Write the file struct into the table_entry, such that we can save the data correct. // 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; Table_Entry *entry = calloc(1, sizeof(Table_Entry));
memcpy(p_control.master_table.entries[file->table_entry_pointer].filename, file->filename, 256); read_data_from_disk(file->table_entry_sector_index, DEFAULT_ENTRY_SIZE, entry);
p_control.master_table.entries[file->table_entry_pointer].file_size = file->size; // p_control.master_table.entries[i].data_pointer[0]; //;
//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; //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; return 0;
} }
@ -613,9 +641,9 @@ int write_data_to_disk(lsfs_sector_offset index, uint32_t file_block_size, void*
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, 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. 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 written = fwrite(data_to_write, 1, ((file_block_size * SECTOR_SIZE) - offset), disk);
return written; return written;
} }

BIN
lsfs_fuse

Binary file not shown.

26
lsfs_fuse.c

@ -39,7 +39,7 @@ static struct fuse_operations lsfs_oper = {
.read = lsfs_read, .read = lsfs_read,
.release = lsfs_release, .release = lsfs_release,
.write = lsfs_write, .write = lsfs_write,
.rename = NULL, .rename = lsfs_rename,
.utime = lsfs_utime_STUB, .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) { int lsfs_rename(const char *path, const char *to) {
(void)path; (void)path;
(void)to; (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 lsfs_getattr( const char *path, struct stat *stbuf ) {
int res = 0; int res = 0;
lsfs_file *found_file = calloc(1, sizeof(lsfs_file)); 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)); memset(stbuf, 0, sizeof(struct stat));
if( strcmp( path, "/" ) == 0 ) { 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) { 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; //(void) file_info;
int res; int res;
//printf("WRITE: offset id: %d\n", offset_to_next_entry);
// printf("read: (path=%s)\n", path); // printf("read: (path=%s)\n", path);
time_t current_time; //time_t current_time;
time ( &current_time ); //time ( &current_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)->size += res;
((lsfs_file*) file_info->fh)->access_time = current_time; //((lsfs_file*) file_info->fh)->access_time = current_time;
((lsfs_file*) file_info->fh)->modification_time = current_time; //((lsfs_file*) file_info->fh)->modification_time = current_time;
return res; 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 ) { 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) offset;
(void) fi; (void) fi;
printf("readdir: (path=%s)\n", path); //printf("readdir: (path=%s)\n", path);
filler(buf, ".", NULL, 0); filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0); filler(buf, "..", NULL, 0);

BIN
lsfs_fuse.o

Binary file not shown.
Loading…
Cancel
Save