My Project
Loading...
Searching...
No Matches
vfs.c
Go to the documentation of this file.
1/*
2 * PROJECT: MatanelOS Kernel
3 * LICENSE: GPLv3
4 * PURPOSE: Virtual File System (VFS) Implementation.
5 */
6
7#include "vfs.h"
8
10#include "../fat32/fat32.h"
12
13typedef struct MOUNTED_FS {
15 uint8_t device_id;
16 const char* mount_point; // e.g., "/", "/ext2"
18
19#define MAX_MOUNTS 4
20static MOUNTED_FS mounted_fs[MAX_MOUNTS];
21static uint8_t mount_count = 0;
22
23#define MAIN_FS_DEVICE 0
24
25// Adapter for VFS FS_DRIVER
26static MTSTATUS fat32_fs_init(uint8_t device_id) {
27 return fat32_init(device_id);
28}
29
31 .init = fat32_fs_init,
32 .read = fat32_read_file,
33 .write = fat32_write_file,
34 .delete = fat32_delete_file,
35 .listdir = fat32_list_directory,
38 .is_dir_empty = fat32_directory_is_empty,
39 .listrootdir = fat32_list_root,
40};
41
43 // First initialize other FS Related stuff (FAT32, AHCI, etc..)
44 MTSTATUS status = ahci_init();
45 if (MT_FAILURE(status)) {
46 gop_printf(COLOR_RED, "AHCI | Status failure: %x", status);
47 FREEZE();
48 return status;
49 }
50 // Mount FAT32 on MAIN_FS_DEVICE
51 status = fat32_driver.init(MAIN_FS_DEVICE);
52 if (MT_FAILURE(status)) {
53 gop_printf(COLOR_RED, "FAT32 | Status failure: %x", status);
54 FREEZE();
55 return status;
56 }
57 mounted_fs[mount_count++] = (MOUNTED_FS){ .driver = &fat32_driver, .device_id = MAIN_FS_DEVICE, .mount_point = "/" };
58
59 return MT_SUCCESS;
60}
61
62static MOUNTED_FS* vfs_find_fs_for_path(const char* path) {
63 if (!path) return NULL;
64 for (uint8_t i = 0; i < mount_count; i++) {
65 const char* mount = mounted_fs[i].mount_point;
66 // root mount should match anything
67 if (mount[0] == '/' && mount[1] == '\0') return &mounted_fs[i];
68
69 // compute mount_len
70 size_t mount_len = 0;
71 while (mount[mount_len]) mount_len++;
72
73 // path must be at least mount_len
74 size_t path_len = 0;
75 while (path[path_len]) path_len++;
76 if (path_len < mount_len) continue;
77
78 bool match = true;
79 for (size_t j = 0; j < mount_len; j++) {
80 if (path[j] != mount[j]) { match = false; break; }
81 }
82 if (match) return &mounted_fs[i];
83 }
84 return NULL;
85}
86
87MTSTATUS vfs_read(const char* filename, uint32_t* file_size_out, void** buffer_out) {
88 MOUNTED_FS* fs = vfs_find_fs_for_path(filename);
89 if (!fs || !fs->driver || !fs->driver->read) return MT_NOT_IMPLEMENTED;
90
91 return fs->driver->read(filename, file_size_out, buffer_out);
92}
93
94MTSTATUS vfs_write(const char* path, const void* data, uint32_t size, FS_WRITE_MODES write_mode) {
95 MOUNTED_FS* fs = vfs_find_fs_for_path(path);
96 if (!fs || !fs->driver || !fs->driver->write) return MT_NOT_IMPLEMENTED;
97
98 return fs->driver->write(path, data, size, (uint32_t)write_mode);
99}
100
101MTSTATUS vfs_delete(const char* path) {
102 MOUNTED_FS* fs = vfs_find_fs_for_path(path);
103 if (!fs || !fs->driver || !fs->driver->delete) return MT_NOT_IMPLEMENTED;
104
105 return fs->driver->delete(path);
106}
107
108MTSTATUS vfs_listdir(const char* path, char* listings, size_t max_len) {
109 MOUNTED_FS* fs = vfs_find_fs_for_path(path);
110 if (!fs || !fs->driver || !fs->driver->listdir) return MT_NOT_IMPLEMENTED;
111
112 return fs->driver->listdir(path, listings, max_len);
113}
114
115MTSTATUS vfs_mkdir(const char* path) {
116 MOUNTED_FS* fs = vfs_find_fs_for_path(path);
117 if (!fs || !fs->driver || !fs->driver->mkdir) return MT_NOT_IMPLEMENTED;
118
119 return fs->driver->mkdir(path);
120}
121
122MTSTATUS vfs_rmdir(const char* path) {
123 MOUNTED_FS* fs = vfs_find_fs_for_path(path);
124 if (!fs || !fs->driver || !fs->driver->rmdir) return MT_NOT_IMPLEMENTED;
125
126 return fs->driver->rmdir(path);
127}
128
129bool vfs_is_dir_empty(const char* path) {
130 MOUNTED_FS* fs = vfs_find_fs_for_path(path);
131 if (!fs || !fs->driver || !fs->driver->is_dir_empty) return false;
132
133 return fs->driver->is_dir_empty(path);
134}
135
136void vfs_listrootdir(void) {
137 MOUNTED_FS* fs = vfs_find_fs_for_path("/");
138 if (!fs || !fs->driver || !fs->driver->listrootdir) return;
139
140 fs->driver->listrootdir();
141 return;
142}
MTSTATUS ahci_init(void)
define AHCI_DEBUG_PRINT
Definition ahci.c:292
MTSTATUS fat32_init(int disk_index)
Definition fat32.c:745
MTSTATUS fat32_create_directory(const char *path)
Creates a new directory (/testdir/ or /testdir are both allowed to create 'testdir' inside of 'root')
Definition fat32.c:1047
MTSTATUS fat32_list_directory(const char *path, char *listings, size_t max_len)
Lists the directory given.
Definition fat32.c:1579
MTSTATUS fat32_delete_directory(const char *path)
This function deletes the directory given to the function from the system.
Definition fat32.c:1922
MTSTATUS fat32_read_file(const char *filename, uint32_t *file_size_out, void **buffer_out)
A FAT32 Function that reads the file requested into a dynamically allocated buffer.
Definition fat32.c:921
MTSTATUS fat32_write_file(const char *path, const void *data, uint32_t size, uint32_t mode)
Creates a new file and writes data to it.
Definition fat32.c:1275
MTSTATUS fat32_delete_file(const char *path)
This function deletes the file given to the function from the system.
Definition fat32.c:1954
void fat32_list_root(void)
Definition fat32.c:769
bool fat32_directory_is_empty(const char *path)
This function returns if the directory given to the function is empty (e.g, has only '....
Definition fat32.c:1663
void gop_printf(uint32_t color, const char *fmt,...)
Definition gop.c:694
#define FREEZE()
Definition macros.h:49
#define COLOR_RED
Colors definitions for easier access.
Definition mg.h:29
#define MT_SUCCESS
Definition mtstatus.h:22
#define MT_NOT_IMPLEMENTED
Definition mtstatus.h:23
#define MT_FAILURE(Status)
Definition mtstatus.h:16
int32_t MTSTATUS
Definition mtstatus.h:12
FS_DRIVER * driver
Definition vfs.c:14
const char * mount_point
Definition vfs.c:16
uint8_t device_id
Definition vfs.c:15
#define MAX_MOUNTS
Definition vfs.c:19
MTSTATUS vfs_read(const char *filename, uint32_t *file_size_out, void **buffer_out)
Reads the file into a buffer.
Definition vfs.c:87
MTSTATUS vfs_rmdir(const char *path)
This function deletes the directory given to the function from the system along with its file (marks ...
Definition vfs.c:122
#define MAIN_FS_DEVICE
Definition vfs.c:23
void vfs_listrootdir(void)
This function will list the root directory of the main mount device.
Definition vfs.c:136
MTSTATUS vfs_listdir(const char *path, char *listings, size_t max_len)
Lists the directory given.
Definition vfs.c:108
MTSTATUS vfs_mkdir(const char *path)
Creates a new directory.
Definition vfs.c:115
bool vfs_is_dir_empty(const char *path)
This function returns if the directory given to the function is empty (e.g, has only '....
Definition vfs.c:129
MTSTATUS vfs_delete(const char *path)
This function deletes the file given to the function from the system.
Definition vfs.c:101
MTSTATUS vfs_init(void)
Initialize the Virtual File System (initializes other filesystem needed services as well)
Definition vfs.c:42
FS_DRIVER fat32_driver
Definition vfs.c:30
MTSTATUS vfs_write(const char *path, const void *data, uint32_t size, FS_WRITE_MODES write_mode)
Creates a new file (or opens existing) and writes data to it.
Definition vfs.c:94
enum _FS_WRITE_MODES FS_WRITE_MODES