Browse Source

Fixed compile warnings, improved CLI interface

master
Jørn Guldberg 3 years ago
parent
commit
54ec569121
6 changed files with 75 additions and 46 deletions
  1. +2
    -0
      build.sh
  2. BIN
      disk_manager
  3. +11
    -7
      disk_manager_utility.c
  4. +46
    -29
      lsfs_disk_controller.h
  5. +10
    -10
      lsfs_string.h
  6. +6
    -0
      lsfs_vbr/build.sh

+ 2
- 0
build.sh View File

@ -0,0 +1,2 @@
gcc -Wall -Wextra -Wpedantic disk_manager_utility.c -o disk_manager

BIN
disk_manager View File


+ 11
- 7
disk_manager_utility.c View File

@ -22,7 +22,7 @@ int dmu_print_file(char *path) {
printf("Filename: %s\n", file->filename);
printf("File size: %lu\n", file->size);
printf("Entry kind: %d\n", file->entry_kind);
printf("Index in directory table: %d\n", file->table_entry_pointer);
printf("Index in directory table: %lu\n", file->table_entry_pointer);
printf("Sector index on disk: %lu\n", file->table_entry_sector_index);
printf("\nData pointers:\n");
for (int i = 0; i < NUM_DATA_POINTERS; ++i)
@ -109,12 +109,12 @@ int dmu_install_SingOS(char* disk_name) {
int dmu_create_file_system(char* disk_name) {
uint64_t filesystem_size_in_MB = 0;
char hdd_or_partition; // 1: is harddisk, 2: is partition
char hdd_or_partition = 0; // 1: is harddisk, 2: is partition
char input_size_file_system[64]; // in MB
do {
printf("Create as 1: harddrive or 2: as a single partition (enter 1 or 2): \n");
scanf("%c", hdd_or_partition);
scanf("%c", &hdd_or_partition);
} while ((hdd_or_partition != '1') && (hdd_or_partition != '2'));
@ -137,8 +137,7 @@ int dmu_create_file_system(char* disk_name) {
int dmu_load_file_system(char* disk_name)
{
disk = fopen ( disk_name , "r+b" );
lsfs_disk_load_disk();
lsfs_disk_load_disk(disk_name);
disk_is_loaded = 1;
return 1;
}
@ -162,7 +161,7 @@ int dmu_print_mtt(char *path) {
printf("\n|--------Filename------------|Not used----------------------|Entry ID----|\n");
for (int i = 0; i < DEFAULT_TABLE_SIZE; ++i) {
printf("|%-28s|%-30lu|%-12d| \n", directory_table->entries[i].filename, directory_table->entries[i].file_id, directory_table->entries[i].file_id);
printf("|%-28s|%-30lu|%-12lu| \n", directory_table->entries[i].filename, directory_table->entries[i].file_id, directory_table->entries[i].file_id);
printf("|----------------------------|------------------------------|------------|\n");
}
@ -274,6 +273,11 @@ int main (int argc, char *argv[])
{
lsfs_disk_install_bootloader(settings->install_bootloader_path);
}
sleep(1);
if (settings->install_vbr_path != NULL)
{
lsfs_disk_install_vbr(settings->install_vbr_path);
}
}
@ -339,7 +343,7 @@ int main (int argc, char *argv[])
printf("Enter Directory:\n");
scanf("%s", input_buffer);
int new_id = lsfs_disk_create_entry(input_buffer, ENTRY_DIRECTORY );
lsfs_disk_create_entry(input_buffer, ENTRY_DIRECTORY );
//lsfs_disk_write_data_to_file(new_id, strlen(new_file_data_d), new_file_data_d);
}

+ 46
- 29
lsfs_disk_controller.h View File

@ -20,13 +20,20 @@ typedef struct File_System_Control_Information FSCI;
typedef struct meta_information_format mif;
typedef struct tag_record tag_record;
typedef struct lsfs_file lsfs_file;
typedef enum Table_Entry_Kind Table_Entry_Kind;
typedef uint64_t lsfs_sector_offset;
typedef lsfs_sector_offset lsfs_file_id;
typedef enum
{
// These are specific values since, is has to corrospond to the implementation in assembly
ENTRY_EMPTY = 0,
ENTRY_FILE = 1,
ENTRY_DIRECTORY = 2,
} Table_Entry_Kind;
//typedef uint64_t sector_index;
static FILE* disk;
static FILE* disk = NULL;
static partition_control p_control;
static time_t timestamp_loading;
@ -36,11 +43,11 @@ Directory_Table* lsfs_find_directory(const char* path, bool drop_filename);
int lsfs_disk_getattr(lsfs_file* find_file, const char *path);
int lsfs_disk_delete_entry(lsfs_file *file);
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 data_length, char *data, size_t offset_to_next_entry);
int lsfs_disk_write_data_to_file(lsfs_file* file, int data_length, char *data, size_t offset_to_next_entry);
int get_free_sectors(int num_sectors_needed, struct_table_entry* table_entry);
int lsfs_disk_read_data_from_file(lsfs_file *file, int data_length, char *data, int64_t offset_to_next_entry);
int lsfs_disk_write_data_to_file(lsfs_file* file, int data_length, char *data, int64_t offset_to_next_entry);
int lsfs_disk_rename_file(const char* old_filename_, const char* new_filename);
int lsfs_disk_load_disk();
int lsfs_disk_load_disk(char* diskname);
int write_data_to_disk(lsfs_sector_offset at_sector, uint32_t number_sectors, void* data_to_write);
int write_data_to_disk_off(lsfs_sector_offset index, uint32_t number_sectors, void* data_to_write, int offset);
int read_data_from_disk(lsfs_sector_offset index, uint32_t number_sectors, void* data_buffer);
@ -57,14 +64,6 @@ int save_modified_file_information(lsfs_file* file);
#define DEFAULT_TABLE_SIZE 8 // 16
#define NUM_DATA_POINTERS 27
typedef enum Table_Entry_Kind
{
// These are specific values since, is has to corrospond to the implementation in assembly
ENTRY_EMPTY = 0,
ENTRY_FILE = 1,
ENTRY_DIRECTORY = 2,
} Table_Entry_Kind;
typedef struct Partition_Entry
{
uint8_t active_falg; // This has value 0x80 if it is a bootable partition / it is an active partition.
@ -171,6 +170,7 @@ typedef struct tag_record {
typedef struct lsfs_file {
lsfs_file_id file_id;
lsfs_sector_offset table_entry_pointer;
struct_table_entry* table_backpointer;
Table_Entry_Kind entry_kind;
char* filename;
uint32_t owner_id;
@ -188,7 +188,7 @@ Directory_Table* lsfs_find_directory(const char *path, bool drop_filename)
{
Directory_Table *dir_table = calloc(1, sizeof(Directory_Table));
printf("Table index: %lu \n",p_control.fsci.master_table_index );
// printf("Table index: %lu \n",p_control.fsci.master_table_index );
read_data_from_disk(p_control.fsci.master_table_index, DEFAULT_TABLE_SIZE, dir_table);
lsfs_string_array split_path = lsfs_string_split_c(path, '/', false);
@ -207,7 +207,7 @@ Directory_Table* lsfs_find_directory(const char *path, bool drop_filename)
if (strcmp(dir_table->entries[j].filename, split_path.strings[i].chars) == 0)
{
int index_sector = dir_table->entries[j].data_pointer[0];
printf("Table index: %lu \n",index_sector );
// printf("Table index: %lu \n",index_sector );
read_data_from_disk(index_sector, DEFAULT_TABLE_SIZE, dir_table);
break;
}
@ -225,7 +225,9 @@ int lsfs_disk_getattr(lsfs_file* find_file, const char* path) {
for (int i = 0; i < DEFAULT_TABLE_SIZE; ++i)
{
if(strcmp( filename.chars, dir_table->entries[i].filename ) == 0) {
if(strcmp( filename.chars, dir_table->entries[i].filename ) == 0)
{
find_file->table_backpointer = &(dir_table->entries[i]);
find_file->file_id = dir_table->entries[i].file_id;
find_file->entry_kind = dir_table->entries[i].entry_kind;
find_file->table_entry_pointer = i;
@ -246,7 +248,7 @@ int lsfs_disk_getattr(lsfs_file* find_file, const char* path) {
int lsfs_disk_read_data_from_file(lsfs_file *file, int buffer_size, char *data, size_t offset_to_next_entry)
int lsfs_disk_read_data_from_file(lsfs_file *file, int buffer_size, char *data, int64_t offset_to_next_entry)
{
int data_length = file->size - offset_to_next_entry;
int amount_read = 0;
@ -339,7 +341,7 @@ 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, size_t offset_to_next_entry)
int lsfs_disk_write_data_to_file(lsfs_file *file, int data_length, char *data, int64_t offset_to_next_entry)
{
int new_filesize = data_length + offset_to_next_entry;
int amount_written = 0;
@ -353,7 +355,7 @@ int lsfs_disk_write_data_to_file(lsfs_file *file, int data_length, char *data, s
while (file->data_pointer[data_pointer_index] == 0)
{
// we have to assign a free sector
if (get_free_sectors(1, file->data_pointer))
if (get_free_sectors(1, file->table_backpointer))
{
// This is a fail case, we cannot assign a new sector:
return amount_written;
@ -535,7 +537,7 @@ int get_free_sectors_table() {
return return_index;
}
int get_free_sectors(int num_sectors_needed, lsfs_sector_offset* output_array) {
int get_free_sectors(int num_sectors_needed, struct_table_entry* table_entry) {
if ((p_control.fsci.next_free_sector + num_sectors_needed) > p_control.fsci.last_sector_index_on_partition )
{
@ -551,10 +553,10 @@ int get_free_sectors(int num_sectors_needed, lsfs_sector_offset* output_array) {
return -EINVAL; // We don't have any more data pointers.
}
if (output_array[i] == 0)
if (table_entry->data_pointer[i] == 0)
{
// If free we can assign:
output_array[i] = p_control.fsci.next_free_sector;
table_entry->data_pointer[i] = p_control.fsci.next_free_sector;
p_control.fsci.next_free_sector += DEFAULT_DATA_POINTER_SIZE;
num_sectors_needed--;
}
@ -597,7 +599,7 @@ int create_file_system(char* disk_name, char hdd_or_partition, uint64_t filesyst
{
// This is just a single partition
// And then the file system is the only thing in the system.
sprintf(fsci->filesystem_information, "LSFS v1.0.0-a1\r\n(LessSimpelFileSystem)(Generated by the disk_manager_utility.c)\r\nDeveloped to SingOS\r\nby Jorn Guldberg\r\n");
sprintf(fsci->filesystem_information, "LSFS v1.0.0-a4\r\n(LessSimpelFileSystem)(Generated by the disk_manager_utility.c)\r\nDeveloped to SingOS and QuasiOS\r\nby Jorn Guldberg\r\n");
if (hdd_or_partition == '1')
{
@ -624,6 +626,7 @@ int create_file_system(char* disk_name, char hdd_or_partition, uint64_t filesyst
write_data_to_disk(fsci->this_partition_offset_on_disk, 1, fsci);
lsfs_disk_load_disk(NULL); // Reload disk
return 0;
}
@ -639,6 +642,9 @@ int lsfs_disk_install_bootloader(char *bootloader_name)
fread(mbr, 1, SECTOR_SIZE, disk);
memcpy(mbr->code, bootloader_mbr->code, 446);
write_data_to_disk(0, 1, mbr); // Write this to the first sector of the disk.
lsfs_disk_load_disk(NULL); // Reload disk
return 0;
}
@ -662,9 +668,9 @@ int lsfs_disk_install_vbr(char *vbr_path)
vbr_first_sector->vbr_LBA_FSCI_position = p_control.fsci.this_partition_offset_on_disk;
vbr_first_sector->vbr_signature = 0x1818;
printf("VBR size: %d\n", vbr_first_sector->vbr_size_in_bytes);
printf("VBR lba address: %d\n", vbr_first_sector->vbr_LBA_address);
printf("VBR FSCI: %d\n", vbr_first_sector->vbr_LBA_FSCI_position);
printf("VBR size: %lu\n", vbr_first_sector->vbr_size_in_bytes);
printf("VBR lba address: %lu\n", vbr_first_sector->vbr_LBA_address);
printf("VBR FSCI: %lu\n", vbr_first_sector->vbr_LBA_FSCI_position);
write_data_to_disk((p_control.fsci.this_partition_offset_on_disk - SPACE_VBR_RECORD), 1, vbr_first_sector); // Write this to the first sector of the disk.
write_data_to_disk((p_control.fsci.this_partition_offset_on_disk - SPACE_VBR_RECORD + 1), (SPACE_VBR_RECORD - 1), vbr_buffer_rest); // Write this to the first sector of the disk.
@ -688,10 +694,21 @@ int lsfs_disk_install_vbr(char *vbr_path)
}
int lsfs_disk_load_disk() {
int lsfs_disk_load_disk(char* diskname)
{
// Find the partition talbe:
// This makes is BIOS dependent.
// UEFI is not supported.
if (diskname != NULL)
{
disk = fopen ( diskname , "r+b" );
}
else if (disk == NULL )
{
return -1;
}
time(&timestamp_loading);
Master_Boot_record mbr;
@ -811,7 +828,7 @@ int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind)
{
// We assign one data pointer consiting of DEFAULT_DATA_POINTER_SIZE sectors
dir_table->entries[free_index].file_size = 0;
get_free_sectors(1, dir_table->entries[free_index].data_pointer);
get_free_sectors(1, &(dir_table->entries[free_index]));
}
else
{

+ 10
- 10
lsfs_string.h View File

@ -5,12 +5,12 @@
typedef struct lsfs_string {
bool dynamic;
unsigned int length;
int length;
char *chars;
} lsfs_string;
typedef struct lsfs_string_array {
unsigned int length;
int length;
lsfs_string *strings;
} lsfs_string_array;
@ -33,7 +33,7 @@ static inline lsfs_string lsfs_create_id_string(uint64_t id) {
};
}
static inline lsfs_string lsfs_make_string(unsigned int length, const char *chars) {
static inline lsfs_string lsfs_make_string(int length, const char *chars) {
return (lsfs_string){
.dynamic = false,
.length = length,
@ -45,7 +45,7 @@ static inline lsfs_string lsfs_make_string_c(const char *cstring) {
return lsfs_make_string(strlen(cstring), cstring);
}
static inline lsfs_string lsfs_create_string(unsigned int length, const char *chars) {
static inline lsfs_string lsfs_create_string(int length, const char *chars) {
char *copy = malloc(length + 1); // Space for null terminator
memcpy(copy, chars, length);
@ -79,7 +79,7 @@ lsfs_string_array lsfs_create_string_array(size_t array_size) {
}
static inline void lsfs_destroy_string_array(lsfs_string_array array) {
for (unsigned int i = 0; i < array.length; ++i) {
for (int i = 0; i < array.length; ++i) {
lsfs_destroy_string(array.strings[i]);
}
free(array.strings);
@ -87,10 +87,10 @@ static inline void lsfs_destroy_string_array(lsfs_string_array array) {
lsfs_string_array lsfs_string_split(lsfs_string string, char delim, bool keep_delim) {
unsigned int i;
unsigned int last;
int i;
int last;
unsigned int count = 0;
int count = 0;
i = 0;
last = 0;
@ -109,7 +109,7 @@ lsfs_string_array lsfs_string_split(lsfs_string string, char delim, bool keep_de
lsfs_string_array result = lsfs_create_string_array(count);
unsigned int insert_index = 0;
int insert_index = 0;
int k = keep_delim ? 0 : 1;
i = 0;
@ -141,7 +141,7 @@ char *dbg_strarr(lsfs_string_array strings) {
int where = 0;
where += sprintf(temp+where, "{");
for (unsigned int i = 0; i < strings.length; ++i) {
for (int i = 0; i < strings.length; ++i) {
where += sprintf(temp+where, "<'%.*s'>", strings.strings[i].length, strings.strings[i].chars);
}
where += sprintf(temp+where, "}");

+ 6
- 0
lsfs_vbr/build.sh View File

@ -0,0 +1,6 @@
# First build the 16-bit LSFS implementation which is include in the VBR
cd lsfs_16-bit
bash build.sh
mv disk.out ../disk.out
cd ..
nasm -f bin vbr_lsfs.nasm -o vbr.bin

Loading…
Cancel
Save