fcreate now works for virtual files and directories, and mounted filesystems
This commit is contained in:
parent
167a00738f
commit
21137ee62b
|
|
@ -6,7 +6,9 @@ OUTPUT_FORMAT(elf32-i386)
|
|||
SECTIONS{
|
||||
. = 0xc0000000;
|
||||
.text : AT(ADDR(.text) - 0xc0000000){
|
||||
. = ALIGN(8);
|
||||
_code = .;
|
||||
KEEP(*(.multiboot))
|
||||
*(.text)
|
||||
*(.rodata)
|
||||
. = ALIGN(4096);
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ void map(void *vaddr, void *paddr, uint32_t flags){
|
|||
void unmap(void *vaddr){
|
||||
uint32_t pd_index = (uint32_t)vaddr >> 22;
|
||||
uint32_t pt_index = (uint32_t)vaddr >> 12 & 0x3ff;
|
||||
printf("unmap called");
|
||||
// printf("unmap called");
|
||||
uint32_t *pd = (uint32_t *)0xfffff000;
|
||||
if(!pd[pd_index]){
|
||||
return;
|
||||
|
|
@ -103,6 +103,7 @@ void unmap(void *vaddr){
|
|||
// paddr ^= (paddr & 0xfff);
|
||||
// pm_free(paddr);
|
||||
asm volatile("invlpg (%0) " : : "b"(vaddr) : "memory");
|
||||
return;
|
||||
}
|
||||
void map_4mb(void *vaddr, void *paddr, uint32_t flags){
|
||||
//just wanted to get the prototype out
|
||||
|
|
|
|||
|
|
@ -27,13 +27,13 @@ extern void kmain(kernel_info_t *kernel_info){
|
|||
pm_init(kernel_info);
|
||||
mlog("KERNEL", "Initializing IDT\n", MLOG_PRINT);
|
||||
idt_load();
|
||||
pic_init(32);
|
||||
pic_init(0x20);
|
||||
pic_disable();
|
||||
pic_setmask(0xfe, PIC1_DATA);
|
||||
vfs_init();
|
||||
fcreate("/test", 0);
|
||||
//TODO: Map and load filesystem and disk modules
|
||||
//TODO: Start Initial Process
|
||||
fcreate("/dev", VFILE_DIRECTORY, kmalloc(1), 1);
|
||||
fcreate("/dev/test", VFILE_POINTER, kmalloc(1), 1);
|
||||
fcreate("/dev/test", VFILE_POINTER, kmalloc(1), 1);
|
||||
mlog("KERNEL", "Initializing Scheduler & starting PID 1\n", MLOG_PRINT);
|
||||
boot_info = kernel_info;
|
||||
scheduler_init();
|
||||
|
|
|
|||
|
|
@ -11,33 +11,61 @@ void vfs_init(){
|
|||
root_dir.access.data.size_pgs = 1;
|
||||
}
|
||||
|
||||
vfile_t **find_free_dirent(vfile_t *current_dir){
|
||||
|
||||
}
|
||||
|
||||
void fcreate(char *name, VFILE_TYPE type, ...){
|
||||
char *dir = strtok(name, '/');
|
||||
char *tmp = dir;
|
||||
char *last = strtok(name + (name[0] == '/'), '/');
|
||||
char *tmp = last;
|
||||
uint32_t filename_offset = 0;
|
||||
vfile_t *current_dir = &root_dir;
|
||||
vfile_t *tmpfile;
|
||||
|
||||
va_list args;
|
||||
va_start(args, type);
|
||||
while(tmp != 0){
|
||||
dir = tmp;
|
||||
// dir = tmp;
|
||||
tmpfile = search_dir(tmp, *current_dir);
|
||||
if(!tmpfile){
|
||||
//create file in folder;
|
||||
vfile_t *vfile = kmalloc(1);
|
||||
vfile->type = type;
|
||||
|
||||
switch(type){
|
||||
case VFILE_POINTER:
|
||||
case VFILE_DIRECTORY:
|
||||
void *ptr = va_arg(args, void*);
|
||||
vfile->access.data.ptr = ptr;
|
||||
vfile->access.data.size_pgs = va_arg(args, uint32_t);
|
||||
strcpy(tmp, vfile->name);
|
||||
vfile_t **dir_data = current_dir->access.data.ptr;
|
||||
int i = 0;
|
||||
while(dir_data[i] && (i * sizeof(vfile_t *))/4096 < current_dir->access.data.size_pgs){
|
||||
i++;
|
||||
}
|
||||
dir_data[i] = vfile;
|
||||
// printf("Created File: %s\n", vfile->name);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
switch(tmpfile->type){
|
||||
case VFILE_DIRECTORY:
|
||||
current_dir = tmpfile;
|
||||
break;
|
||||
case VFILE_MOUNT:
|
||||
return ((mount_t*)(tmpfile->access.data.ptr))->create(name + filename_offset, type == VFILE_DIRECTORY ? FS_FILE_IS_DIR : 0);
|
||||
((mount_t*)(tmpfile->access.data.ptr))->create(name + filename_offset, type == VFILE_DIRECTORY ? FS_FILE_IS_DIR : 0);
|
||||
return;
|
||||
default:
|
||||
mlog(MODULE_NAME, "Error: Cannot create file with same name as another file! %d\n", MLOG_ERR, tmpfile->type);
|
||||
return;
|
||||
break;
|
||||
}
|
||||
filename_offset += strlen(tmp) + 1;
|
||||
kfree(tmp);
|
||||
tmp = strtok(0, '/');
|
||||
}
|
||||
return 0;
|
||||
return;
|
||||
}
|
||||
void fdelete();
|
||||
|
||||
|
|
@ -50,28 +78,25 @@ uint32_t fwrite(vfile_t *file_entry, void *byte_array, uint32_t offset, uint32_t
|
|||
// search through root directory
|
||||
switch(file_entry->type){
|
||||
case VFILE_DEVICE:
|
||||
file_entry->access.funcs.write(byte_array, offset, count);
|
||||
file_entry->access.funcs.write(file_entry, byte_array, offset, count);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t fread();
|
||||
|
||||
vfile_t *search_dir(char *name, vfile_t dir){
|
||||
vfile_t *dir_data = (vfile_t *)dir.access.data.ptr;
|
||||
vfile_t **dir_data = (vfile_t **)dir.access.data.ptr;
|
||||
uint32_t i = 0;
|
||||
while(dir_data[i].name[0]){
|
||||
if(!strcmp(dir_data[i].name, name)) return &dir_data[i];
|
||||
while(dir_data[i]){
|
||||
if(!strcmp(dir_data[i]->name, name)){
|
||||
return dir_data[i];
|
||||
}
|
||||
i++;
|
||||
}
|
||||
mlog(MODULE_NAME, "File not found\n", MLOG_ERR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
file_t vfile_to_file(vfile_t *file){
|
||||
file_t out = {-1, &file};
|
||||
}
|
||||
|
||||
int fopen(char *name, file_t *file){
|
||||
int fopen(char *name, vfile_t *file){
|
||||
char *dir = strtok(name, '/');
|
||||
char *tmp = dir;
|
||||
uint32_t filename_offset = 0;
|
||||
|
|
@ -81,7 +106,6 @@ int fopen(char *name, file_t *file){
|
|||
dir = tmp;
|
||||
tmpfile = search_dir(tmp, *current_dir);
|
||||
if(!tmpfile){
|
||||
kfree(tmp);
|
||||
return -1;
|
||||
}
|
||||
switch(tmpfile->type){
|
||||
|
|
@ -91,15 +115,12 @@ int fopen(char *name, file_t *file){
|
|||
case VFILE_MOUNT:
|
||||
return ((mount_t*)(tmpfile->access.data.ptr))->open(name + filename_offset, file);
|
||||
default:
|
||||
*file = vfile_to_file(tmpfile);
|
||||
kfree(tmp);
|
||||
*file = *tmpfile;
|
||||
return 0;
|
||||
}
|
||||
filename_offset += strlen(tmp) + 1;
|
||||
kfree(tmp);
|
||||
tmp = strtok(0, '/');
|
||||
}
|
||||
*file = vfile_to_file(tmpfile);
|
||||
kfree(tmp);
|
||||
*file = *tmpfile;
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -6,31 +6,28 @@ typedef enum vfile_type{
|
|||
VFILE_DEVICE,
|
||||
VFILE_MOUNT,
|
||||
VFILE_DIRECTORY,
|
||||
VFILE_FILE,
|
||||
VFILE_PDIR,//used for directories in physical filesystems.
|
||||
VFILE_FILE
|
||||
}VFILE_TYPE;
|
||||
|
||||
//this code is gonna be ***really*** unsafe
|
||||
typedef struct virtual_file_t{
|
||||
typedef struct virtual_file{
|
||||
char name[20];
|
||||
VFILE_TYPE type;
|
||||
uint32_t id;//to be assigned by driver;
|
||||
uint32_t mount_id;
|
||||
union{
|
||||
struct{
|
||||
void (*read)(void *data, uint32_t offset, uint32_t count);
|
||||
void (*write)(void *data, uint32_t offset, uint32_t count);
|
||||
void (*read)(struct virtual_file *file, void *data, uint32_t offset, uint32_t count);
|
||||
void (*write)(struct virtual_file *file, void *data, uint32_t offset, uint32_t count);
|
||||
}funcs;
|
||||
struct{
|
||||
void *ptr;
|
||||
uint16_t size_pgs;
|
||||
uint16_t free_bytes;
|
||||
uint32_t size_pgs;
|
||||
}__attribute__((packed))data;
|
||||
}access;
|
||||
}vfile_t;
|
||||
|
||||
typedef struct file{
|
||||
uint32_t mount_index;
|
||||
void *dirent;
|
||||
}file_t;
|
||||
|
||||
typedef enum fs_flags{
|
||||
FS_FILE_IS_DIR = 1,
|
||||
FS_FILE_IS_SYSTEM = 2,
|
||||
|
|
@ -38,10 +35,10 @@ typedef enum fs_flags{
|
|||
}FS_FILE_FLAGS;
|
||||
|
||||
typedef struct mount_funcs{
|
||||
void (*write)(file_t *file, void *data, uint32_t offset, uint32_t count);
|
||||
void (*read)(file_t *file, void *data, uint32_t offset, uint32_t count);
|
||||
int (*open)(char *filename, file_t *file);
|
||||
void (*delete)(file_t *file);
|
||||
void (*write)(vfile_t *file, void *data, uint32_t offset, uint32_t count);
|
||||
void (*read)(vfile_t *file, void *data, uint32_t offset, uint32_t count);
|
||||
int (*open)(char *filename, vfile_t *file);
|
||||
void (*delete)(vfile_t *file);
|
||||
void (*create)(char *filename, FS_FILE_FLAGS flags);
|
||||
}mount_t;
|
||||
|
||||
|
|
@ -50,5 +47,5 @@ void fcreate(char *name, VFILE_TYPE type, ...);
|
|||
void fdelete();
|
||||
uint32_t fwrite();
|
||||
uint32_t fread();
|
||||
vfile_t *search_dir(char *name, vfile_t dir){
|
||||
int fopen(char *name, file_t *file);
|
||||
vfile_t *search_dir(char *name, vfile_t dir);
|
||||
int fopen(char *name, vfile_t *file);
|
||||
|
|
@ -30,5 +30,24 @@ typedef enum vfile_type{
|
|||
VFILE_DEVICE,
|
||||
VFILE_MOUNT,
|
||||
VFILE_DIRECTORY,
|
||||
VFILE_PDIR,//used for directories in physical filesystems.
|
||||
VFILE_FILE,
|
||||
}VFILE_TYPE;
|
||||
}VFILE_TYPE;
|
||||
|
||||
typedef struct virtual_file{
|
||||
char name[20];
|
||||
VFILE_TYPE type;
|
||||
uint32_t id;//to be assigned by driver;
|
||||
uint32_t mount_id;
|
||||
union{
|
||||
struct{
|
||||
void (*read)(struct virtual_file *file, void *data, uint32_t offset, uint32_t count);
|
||||
void (*write)(struct virtual_file *file, void *data, uint32_t offset, uint32_t count);
|
||||
}funcs;
|
||||
struct{
|
||||
void *ptr;
|
||||
uint16_t size_pgs;
|
||||
uint16_t free_bytes;
|
||||
}__attribute__((packed))data;
|
||||
}access;
|
||||
}vfile_t;
|
||||
Loading…
Reference in New Issue