This commit is contained in:
Arnau Camprubí 2022-07-03 12:30:00 +02:00
pare 5c0ebf563c
commit a4f4838f0a
S'han modificat 6 arxius amb 76 adicions i 96 eliminacions

Veure arxiu

@ -1,44 +0,0 @@
#include <stddef.h>
#include "fs.h"
fs_node_t *fs_root = NULL;
uint32_t fs_read(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer){
if(node->read != NULL){
return node->read(node, offset, size, buffer);
}
return 0;
}
uint32_t fs_write(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer){
if(node->write != NULL){
return node->write(node, offset, size, buffer);
}
return 0;
}
void fs_open(fs_node_t *node, uint8_t read, uint8_t write){
if(node->open != NULL){
return node->open(node);
}
}
void fs_close(fs_node_t *node){
if(node->close != NULL){
return node->close(node);
}
}
dirent_t *fs_readdir(fs_node_t *node, uint32_t index){
if(node->readdir != NULL && (node->flags & 0x7 == FS_DIRECTORY)){
return node->readdir(node, index);
}
return 0;
}
fs_node_t *fs_finddir(fs_node_t *node, char *name){
if(node->finddir != NULL && (node->flags & 0x7 == FS_DIRECTORY)){
return node->finddir(node, name);
}
return 0;
}

Veure arxiu

@ -1,52 +0,0 @@
#pragma once
#include <stdint.h>
#define FS_FILE 0x01
#define FS_DIRECTORY 0x02
#define FS_CHARDEVICE 0x03
#define FS_BLOCKDEVICE 0x04
#define FS_PIPE 0x05
#define FS_SYMLINK 0x06
#define FS_MOUNTPOINT 0x08
typedef struct fs_node fs_node_t;
typedef struct dirent{
char name[128];
uint32_t inode;
} dirent_t;
typedef uint32_t (*fs_read_fn_t)(fs_node_t *, uint32_t, uint32_t, uint8_t *);
typedef uint32_t (*fs_write_fn_t)(fs_node_t *, uint32_t, uint32_t, uint8_t *);
typedef void (*fs_open_fn_t)(fs_node_t *);
typedef void (*fs_close_fn_t)(fs_node_t *);
typedef dirent_t *(*fs_readdir_fn_t)(fs_node_t *, uint32_t);
typedef fs_node_t *(*fs_finddir_fn_t)(fs_node_t *, char *);
struct fs_node{
char name[128];
uint32_t mask;
uint32_t uid;
uint32_t gid;
uint32_t flags;
uint32_t inode;
uint32_t length;
uint32_t impl;
fs_read_fn_t read;
fs_write_fn_t write;
fs_open_fn_t open;
fs_close_fn_t close;
fs_readdir_fn_t readdir;
fs_finddir_fn_t finddir;
struct fs_node *ptr;
};
extern fs_node_t *fs_root;
uint32_t fs_read(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer);
uint32_t fs_write(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer);
void fs_open(fs_node_t *node, uint8_t read, uint8_t write);
void fs_close(fs_node_t *node);
dirent_t *fs_readdir(fs_node_t *node, uint32_t index);
fs_node_t *fs_finddir(fs_node_t *node, char *name);

3
src/kernel/fs/qrofs.c Normal file
Veure arxiu

@ -0,0 +1,3 @@
#include "qrofs.h"

4
src/kernel/fs/qrofs.h Normal file
Veure arxiu

@ -0,0 +1,4 @@
#pragma once
#define QROFS_FILE 0x01
#define QROFS_DIRECTORY 0x02

48
src/kernel/fs/vfs.c Normal file
Veure arxiu

@ -0,0 +1,48 @@
#include <stddef.h>
#include <stdbool.h>
#include "vfs.h"
// Return some OR-ed flags for the given mode string
static uint8_t flags_from_mode(const char *mode){
uint8_t flags = 0;
bool read = false;
bool write = false;
while(mode){
switch(*mode){
case 'r':
read = true;
break;
case 'w':
write = true;
break;
case 'a':
flags |= VFS_FLAG_O_APPEND;
break;
case '+':
case 'c':
flags |= VFS_FLAG_O_CREAT;
break;
default:
break;
}
mode++;
}
if(read && write) flags |= VFS_FLAG_O_RDWR;
else if(read) flags |= VFS_FLAG_O_RDONLY;
else if(write) flags |= VFS_FLAG_O_WRONLY;
return flags;
}
file_descriptor_t *vfs_open(const char *path, const char *mode){
uint8_t flags = flags_from_mode(mode);
for(int i = 0; i < 256; i++){
if(!*g_fds[i].name){ // If empty
g_fds[i].flags = flags;
strcpy(g_fds[i].name, path);
return &g_fds[i];
}
}
return NULL; // No free file descriptors
}

21
src/kernel/fs/vfs.h Normal file
Veure arxiu

@ -0,0 +1,21 @@
#pragma once
#include <stdint.h>
// Only one!
#define VFS_FLAG_O_RDONLY 0x00
#define VFS_FLAG_O_WRONLY 0x01
#define VFS_FLAG_O_RDWR 0x02
// Combinable
#define VFS_FLAG_O_APPEND 0x04
#define VFS_FLAG_O_CREAT 0x08
typedef struct{
char name[256];
uint8_t flags;
} file_descriptor_t;
file_descriptor_t g_fds[256];
file_descriptor_t *vfs_open(const char *path, const char *mode);