Defined file abstraction, separated ramfs into system/ramfs.c

This commit is contained in:
notsomeidiot123 2026-06-25 22:26:17 -04:00
parent c79aeedae2
commit 2062a2dff3
9 changed files with 100 additions and 53 deletions

View File

@ -45,6 +45,14 @@ void pci_read_file(vfile_t *file, uint32_t *buffer, uint32_t offset, uint32_t co
void pci_write_file(vfile_t *file, uint32_t *buffer, uint32_t offset, uint32_t count){
return;
}
fileops_t pci_fileops = {
0,
0,
pci_write_file,
pci_read_file,
};
void pci_make_file(uint32_t class, uint8_t bus, uint8_t slot, uint8_t func){
char str[256] = "/dev/pci/\0";
char *dev = classes[class >> 8][class & 0xf];
@ -76,8 +84,9 @@ void pci_make_file(uint32_t class, uint8_t bus, uint8_t slot, uint8_t func){
strcpy(str, cpy);
strcpy(num, cpy + strlen(cpy));
file = fcreate(cpy, 0);
file->read = pci_read_file;
file->write = pci_write_file;
// file->read = pci_read_file;
// file->write = pci_write_file;
file->fileops = &pci_fileops;
if(tries >= 254){
mlog(MODULE_NAME, "Failed to make PCI file for device!\n", MLOG_PRINT);
return;

View File

@ -40,10 +40,10 @@ int read_initrd(initrd_t *initrd){
if(!tmp){
mlog("INITRD", "Failed to read critical boot file: %s\n", MLOG_PRINT, file->filename);
}
if(tmp->ptr){
kfree(tmp->ptr);
if(tmp->private){
kfree(tmp->private);
}
tmp->ptr = (archive + offset + sizeof(USTAR_file_t));
tmp->private = (archive + offset + sizeof(USTAR_file_t));
tmp->size = fsize_pgs * 4096;
// printf("%s, %d, %d\n", final_fname, filesize, fsize_pgs);
offset += (((filesize + 511) / 512) + 1) * 512;

View File

@ -12,6 +12,7 @@
#include "system/vfs.h"
#include "drivers/ustar.h"
#include "system/initrc.h"
#include "system/ramfs.h"
kernel_info_t *boot_info = 0;
@ -49,7 +50,7 @@ void sysinit(){
extern void kmain(kernel_info_t *kernel_info){
serial_init();
pm_init(kernel_info);
vfs_init();
ramfs_init();
mlog("KERNEL", "Initializing IDT\n", MLOG_PRINT);
idt_load();
pic_init(0x20);

View File

@ -17,7 +17,7 @@ int is_num(char c){
void initrc_read(vfile_t *file){
mlog("KERNEL", "Reading initrc:\n", MLOG_PRINT);
char *ptr = file->ptr;
char *ptr = file->private;
if(!file || !ptr){
mlog("KERNEL", "Failed to read initrc!\n", MLOG_PRINT);
return;
@ -67,7 +67,7 @@ void initrc_read(vfile_t *file){
printf("Error: Could not find module in Initrc.conf: %s\n", module_name);
continue;
}
module_start(module->ptr);
module_start(module->private);
}
else if(!strcmp(statement, "MOUNT")){
char mount_src_name[512] = {0};

28
src/kernel/system/ramfs.c Normal file
View File

@ -0,0 +1,28 @@
#include "vfs.h"
#include "ramfs.h"
#include "../shared/kstdlib.h"
#include "../shared/memory.h"
#include "../shared/string.h"
#define MODULE_NAME "KVFS"
fileops_t ramfs_ops =
{
ramfs_create,
};
vfile_t root_dir = {"/", FS_FILE_IS_DIR | FS_FILE_SYSTEM, &ramfs_ops};
void ramfs_init(){
mlog(MODULE_NAME, "Initializing VFS\n", MLOG_PRINT);
root_dir.private = kmalloc(1);
root_dir.size = 4096;//one page is 4096 bytes
}
void ramfs_create(char *path, FS_FILE_FLAGS flags){
return 0;
}
vfile_t *get_root_dir(){
return &root_dir;
}

View File

@ -0,0 +1,6 @@
#pragma once
#include <stdint.h>
void ramfs_init();
void ramfs_create(char *path, FS_FILE_FLAGS flags);
vfile_t *get_root_dir();

View File

@ -3,37 +3,32 @@
#include "../shared/memory.h"
#include "../shared/string.h"
#define MODULE_NAME "KVFS"
vfile_t root_dir = {"/", FS_FILE_IS_DIR | FS_FILE_SYSTEM};
// !TODO: Modify code to become thread-safe
void vfs_init(){
mlog(MODULE_NAME, "Initializing VFS\n", MLOG_PRINT);
root_dir.ptr = kmalloc(1);
root_dir.size = 4096;//one page is 4096 bytes
}
void add_file(vfile_t *vfile, vfile_t *current_dir){
return;
}
vfile_t *fcreate(char *path, FS_FILE_FLAGS flags){
if(!path){
vfcreate(get_root_dir(), path, flags);
}
vfile_t *vfcreate(vfile_t *parent_dir, char *relpath, FS_FILE_FLAGS flags){
if(!relpath){
return;
}
char *name = kmalloc((strlen(path) + 4095)/4096);//don't make modifications to the original string
strcpy(path, name);
// printf("Sizeof: %d\n", sizeof(vfile_t));
char *name = kmalloc((strlen(relpath) + 4095)/4096);//don't make modifications to the original string
strcpy(relpath, name);
if(name[0] == '/') name++;//first slash just indicates that it's an absolute path, we don't want to include that into the filename.
uint32_t name_length = strlen(name);
// printf("%s, %d: ", name, strlen(name));
uint32_t name_index = 0;
uint32_t subpath_start = 0;
vfile_t *new_file = 0;
while(name[name_index]){//this condition shouldn't ever be triggered, but it's here to stop us if we're at the end.
subpath_start = name_index;
while(name[name_index] && name[name_index] != '/'){//split the string at directories
name_index++;
@ -41,14 +36,18 @@ vfile_t *fcreate(char *path, FS_FILE_FLAGS flags){
name[name_index] = 0;
// printf("%d, %s,", subpath_start, name + subpath_start);
if(name_index >= name_length){//this should be the only exit condition
printf("Creating file in dir; name: %s", name + subpath_start);
break;
if(name_index >= name_length){//if there is no other directory
// printf("Creating file; name: %s\n", name + subpath_start);
new_file = parent_dir->fileops->create(name, flags);
}
name_index++;
else{
vfile_t *new_parent = lookup(name, parent_dir);
if(!new_parent){
kfree(name);
return 0;
}
new_file = vfcreate(new_parent, name+name_index+1, flags);
}
printf("\n");
kfree(name);
return new_file;
}
@ -65,9 +64,9 @@ int fread(vfile_t *file_entry, void *byte_array, uint32_t offset, uint32_t count
return 0;
}
// vfile_t *lookup(char *name, vfile_t dir){
// return 0;
// }
vfile_t *lookup(char *name, vfile_t *dir){
return 0;
}
int readdir(vfile_t* file, vfile_t* buffer, uint32_t offset, uint32_t count){
return 0;
@ -77,8 +76,3 @@ vfile_t *fopen(char *name){
return 0;
}
//create file in vfs
vfile_t *vfs_create(char *path, FS_FILE_FLAGS flags){
}

View File

@ -9,33 +9,42 @@ typedef enum fs_flags{
FS_FILE_ARCHIVE = 0x20,
FS_FILE_PIPE = 0x40,
FS_FILE_LINK = 0x80,
FS_FILE_MOUNT = 0x100,
FS_FILE_MOUNT = 0x100, // MUST be assigned to any file that represents a physical filesystem or physical device.
}FS_FILE_FLAGS;
typedef struct fileops{
struct virtual_file *(*create)(char *path, FS_FILE_FLAGS flags);
int (*delete)(struct virtual_file *file_entry);
int (*write)(struct virtual_file *file_entry, void *data, uint32_t offset, uint32_t count);
int (*read)(struct virtual_file *file_entry, void *data, uint32_t offset, uint32_t count);
struct virtual_file *(*open)(char *path);
void (*close)(struct virtual_file *file);
int (*readdir)(struct virtual_file* file, struct virtual_file *buffer, uint32_t count, uint32_t offset);
// struct virtual_file **(*lookup)(char *name);
} fileops_t;
typedef struct virtual_file{
char name[212];
uint16_t flags;
int (*delete)(struct virtual_file *file_entry);
struct virtual_file *(*create)(char *path, FS_FILE_FLAGS flags);
int (*write)(struct virtual_file *file_entry, void *data, uint32_t offset, uint32_t count);
int (*read)(struct virtual_file *file_entry, void *data, uint32_t offset, uint32_t count);
struct virtual_file *(*open)(char *path);
// struct virtual_file **(*lookup)(char *name);
int (*readdir)(struct virtual_file* file, struct virtual_file *buffer, uint32_t count, uint32_t offset);
fileops_t *fileops;
uint32_t refcount; //filesystem MUST remain operational until all child refcounts == 0
uint32_t id;//for use in drivers
void *ptr; //also for use in drivers
void *private; //also for use in drivers
uint32_t size;
uint32_t offset; //for use in drivers
}vfile_t;
// vfile_t *lookup(char *name, vfile_t dir);
void vfs_init();
vfile_t *fcreate(char *name, FS_FILE_FLAGS flags);
vfile_t *fcreate(char *path, FS_FILE_FLAGS flags);
int fdelete(vfile_t* file_entry);
int fwrite(vfile_t *file_entry, void *byte_array, uint32_t offset, uint32_t count);
int fread(vfile_t *file_entry, void *byte_array, uint32_t offset, uint32_t count);
int readdir(vfile_t* file, vfile_t *buffer, uint32_t offset, uint32_t count);
vfile_t *vfcreate(vfile_t *parent, char *relpath, FS_FILE_FLAGS flags);
vfile_t *lookup(char *name, vfile_t *dir);
vfile_t *fopen(char *path);

View File

@ -76,7 +76,7 @@ typedef struct virtual_file{
int (*readdir)(struct virtual_file* file, struct virtual_file *buffer, uint32_t count, uint32_t offset);
uint32_t id;//for use in drivers
void *ptr; //also for use in drivers
void *private; //also for use in drivers
uint32_t size;
uint32_t offset; //for use in drivers