fat_io_lib port WIP
All checks were successful
Build documentation / build-and-deploy (push) Successful in 3m12s

This commit is contained in:
2026-02-26 23:33:03 +01:00
parent 9758d79303
commit baa13fb695
15 changed files with 3607 additions and 18 deletions

139
kernel/fs/fatfs.c Normal file
View File

@@ -0,0 +1,139 @@
#include <desc.h>
#include <fs/fat1.h>
#include <fs/fatfs.h>
#include <fs/fatfs_ctx.h>
#include <fs/path.h>
#include <fs/vfs.h>
#include <libk/align.h>
#include <libk/minmax.h>
#include <libk/printf.h>
#include <libk/std.h>
#include <libk/string.h>
#include <mm/liballoc.h>
#include <path_defs.h>
#include <status.h>
#include <sys/debug.h>
#include <xdrv_device.h>
static int fat1_diskio_read (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* buffer,
uint32_t sector_count) {
struct vfs_volume* volume = ctx->udata;
struct device* back_device = volume->back_device;
size_t sector_size;
size_t phys_sector, phys_sector_count;
int ret;
spin_lock (&back_device->lock);
ret = device_op (back_device, XDRV_GET_SECTOR_SIZE, NULL, NULL, &sector_size);
if (ret < 0) {
spin_unlock (&back_device->lock);
return -1;
}
vfs_translate (sector, sector_count, FAT_SECTOR_SIZE, sector_size, &phys_sector,
&phys_sector_count);
ret = device_op (back_device, XDRV_READ, NULL, NULL, &phys_sector, &phys_sector_count, buffer);
if (ret < 0) {
spin_unlock (&back_device->lock);
return -1;
}
spin_unlock (&back_device->lock);
if (ret < 0)
return -1;
return 0;
}
int fatfs_mount (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx) {
(void)proc, (void)rctx;
struct fatfs_ctx* fatfs_ctx = malloc (sizeof (*fatfs_ctx));
if (fatfs_ctx == NULL)
return -ST_OOM_ERROR;
memset (fatfs_ctx, 0, sizeof (*fatfs_ctx));
fl_attach_media (fatfs_ctx, &fat1_diskio_read, NULL);
fatfs_ctx->udata = volume;
volume->udata = fatfs_ctx;
return ST_OK;
}
int fatfs_describe (struct vfs_volume* volume, const char* path, struct desc* desc) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
if (fl_is_dir (fatfs_ctx, path)) {
FL_DIR dir;
if (fl_opendir (fatfs_ctx, path, &dir) == NULL)
return -ST_NOT_FOUND;
desc->type = FS_DIR;
desc->size = 0;
fl_dirent dirent;
while (fl_readdir (fatfs_ctx, &dir, &dirent) == 0)
desc->size++;
fl_closedir (fatfs_ctx, &dir);
} else {
FL_FILE* file = fl_fopen (fatfs_ctx, path, "r");
if (file == NULL)
return -ST_NOT_FOUND;
desc->type = FS_FILE;
desc->size = file->filelength;
fl_fclose (fatfs_ctx, file);
}
return ST_OK;
}
int fatfs_read (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
FL_FILE* file = fl_fopen (fatfs_ctx, path, "wb+");
if (file == NULL)
return -ST_NOT_FOUND;
fl_fseek (fatfs_ctx, file, off, SEEK_SET);
fl_fread (fatfs_ctx, buffer, 1, size, file);
fl_fclose (fatfs_ctx, file);
return ST_OK;
}
int fatfs_read_dir_entry (struct vfs_volume* volume, const char* path, struct dir_entry* entry,
size_t entry_num) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
FL_DIR dir;
if (fl_opendir (fatfs_ctx, path, &dir) == NULL)
return -ST_NOT_FOUND;
fl_dirent dirent;
size_t dirent_num = 0;
while (fl_readdir (fatfs_ctx, &dir, &dirent) == 0) {
if (dirent_num == entry_num) {
strncat (entry->path, path, PATH_MAX);
strncat (entry->path, dirent.filename, PATH_MAX);
break;
}
dirent_num++;
}
fl_closedir (fatfs_ctx, &dir);
return ST_OK;
}