Implemented ramfs functions, just missing close

This commit is contained in:
notsomeidiot123 2026-07-06 17:16:21 -04:00
parent ecdeadc4f3
commit 616edd6bb3
5 changed files with 66 additions and 14 deletions

View File

@ -33,6 +33,23 @@ void vector_set(uint32_t pos, vector_t *vector, void *new_element);
void vector_push(vector_t *vector, void *new_element);
void vector_pop(uint32_t pos, vector_t *vector, void *element);
uint32_t atoi(char *str, uint32_t base);
inline int32_t signed_max(int32_t i1, int32_t i2){
if(i1 > i2) return i1;
else return i2;
}
inline uint32_t unsigned_max(uint32_t i1, uint32_t i2){
if(i1 > i2) return i1;
else return i2;
}
inline int32_t signed_min(int32_t i1, int32_t i2){
if(i1 < i2) return i1;
else return i2;
}
inline uint32_t unsigned_min(uint32_t i1, uint32_t i2){
if(i1 < i2) return i1;
else return i2;
}

View File

@ -11,7 +11,7 @@ fileops_t ramfs_ops =
ramfs_delete,
ramfs_write,
ramfs_read,
ramfs_open,
// ramfs_open,
ramfs_close,
ramfs_rfopen,
};
@ -117,6 +117,7 @@ int ramfs_resize(vfile_t *file, uint32_t new_size_bytes){
file->private = new_ptr;
}while(0);
file->size = new_size_bytes;
// printf("New file size for file: %s, %d\n", file->name, file->size);
return new_size_bytes;
}
@ -132,7 +133,7 @@ vfile_t *ramfs_create(vfile_t *root, char *path, FS_FILE_FLAGS flags){
path--;
return 0;
}
char *new_name = get_filename(path);
char *new_name = path;
// printf("%d\n", parent->size);
vfile_t **dirents = (vfile_t **)parent->private;
uint32_t insert_index = 0;
@ -162,21 +163,44 @@ vfile_t *ramfs_create(vfile_t *root, char *path, FS_FILE_FLAGS flags){
dirents[parent->size/(sizeof(vfile_t*)) - 1] = new_file;
new_file->fileops = &ramfs_ops;
path--;
// printf("Returning file with name: %s\n", new_file->name);
return new_file;
}
int ramfs_translate_dir(vfile_t *file, void *buffer, uint32_t offset, uint32_t count){
uint32_t dirents_requested = count / sizeof(vfile_t);
uint32_t dirents_availible = file->size / sizeof(vfile_t *);
printf("Sizeof vfile_t %d with %d entries fitting in buffer, and %d entries in file %s\n", sizeof(vfile_t), dirents_requested, dirents_availible, file->name);
uint32_t to_copy = unsigned_min(dirents_availible, dirents_requested);
vfile_t **dirents = (vfile_t **)file->private;
for(uint32_t i = 0; i < to_copy; i++){
// printf("From: %x to %x\n", dirents[i], buffer + i * sizeof(vfile_t));
memcpy(dirents[i], buffer + i * sizeof(vfile_t), sizeof(vfile_t));
}
return count;
}
int ramfs_delete(vfile_t *file){
return 0;
}
int ramfs_write(vfile_t *file, char *buffer, uint32_t offset, uint32_t count){
return 0;
//file->private can be null here, as ramfs_resize will handle assigning if it is not already assigned
if(!file || !buffer || (file->flags & (FS_FILE_READ_ONLY | FS_FILE_IS_DIR | FS_FILE_MOUNT))) return 0;
if(!ramfs_resize(file, count + offset)) return 0;
memcpy(buffer, file->private + offset, count);
return count;
}
int ramfs_read(vfile_t *file, char *buffer, uint32_t offset, uint32_t count){
return 0;
}
vfile_t *ramfs_open(char *path){
return 0;
if(!file || !buffer || !file->private || file->flags & (FS_FILE_MOUNT)) return 0;
if(file->flags & FS_FILE_IS_DIR){
return ramfs_translate_dir(file, buffer, offset, count);
}
memcpy(file->private + offset, buffer, count);
return count;
}
// vfile_t *ramfs_open(char *path){
// return 0;
// }
void ramfs_close(vfile_t *file){
file->refcount--;
if(file->refcount < 0) file->refcount = 0;

View File

@ -9,6 +9,6 @@ vfile_t *get_root_dir();
int ramfs_delete(vfile_t *file);
int ramfs_write(vfile_t *file, char *buffer, uint32_t offset, uint32_t count);
int ramfs_read(vfile_t *file, char *buffer, uint32_t offset, uint32_t count);
vfile_t *ramfs_open(char *path);
// vfile_t *ramfs_open(char *path);
void ramfs_close(vfile_t *file);
vfile_t *ramfs_rfopen(char *name, vfile_t *parent);

View File

@ -52,7 +52,7 @@ vfile_t *vfcreate(vfile_t *parent_dir, char *relpath, FS_FILE_FLAGS flags){
// printf("%d, %s,", subpath_start, name + subpath_start);
if(name_index >= name_length){//if there is no other directory
// printf("Creating file; name: %s\n", name + subpath_start);
// printf("Creating file; name: %s\n", name);
new_file = parent_dir->fileops->create(parent_dir, name, flags);
}
else{
@ -80,7 +80,8 @@ int fdelete(vfile_t *file_entry){
spinlock_release(vfs_lock);
return file_entry->fileops->delete(file_entry);
}
//Is expected to overwrite, not append if it is an actual file
//Devices and anything Not A File is exempt (i.e. Blockdevs, chardevs, etc)
int fwrite(vfile_t *file_entry, void *byte_array, uint32_t offset, uint32_t count){
if(!file_entry || !file_entry->fileops || !file_entry->fileops->write ||!byte_array || count == 0){
return 0;
@ -112,7 +113,10 @@ vfile_t *fopen(char *name){
return 0;
}
vfile_t *root = get_root_dir();
return root->fileops->open(name + (name[0] == '/'));
if(!strcmp(name, "/")){
return root;
}
return root->fileops->rfopen(name + (name[0] == '/'), root);
}
vfile_t *fclose(vfile_t *file){

View File

@ -1,6 +1,6 @@
#pragma once
#include <stdint.h>
#include "../shared/spinlock.h"
typedef enum fs_flags{
FS_FILE_READ_ONLY = 1,
FS_FILE_HIDDEN = 2,
@ -17,16 +17,17 @@ typedef struct fileops{
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);
// 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 *(*rfopen)(char *name, struct virtual_file *parent);
} fileops_t;
//note: this is EXACTLY 256 bytes. This is for simplicity's sake.
//PLEASE if you MUST reorganize or add fields, try and keep it to a power of 2?
typedef struct virtual_file{
char name[212];
uint16_t flags;
fileops_t *fileops;
uint32_t refcount; //filesystem MUST remain operational until all child refcounts == 0
@ -35,6 +36,12 @@ typedef struct virtual_file{
uint32_t size; //should be in bytes
uint32_t offset; //for use in drivers
uint8_t owner_uid;
uint8_t owner_gid;
uint32_t last_modified;
uint32_t created;
uint16_t permissions; //same format as linux
spinlock_t lock;
}vfile_t;
void vfs_init();