syscalls: improve working with files using vfs

This commit is contained in:
2025-08-17 16:03:41 +03:00
parent caa4005990
commit 9fdc551308
8 changed files with 58 additions and 21 deletions
+45 -2
View File
@@ -2,8 +2,10 @@
#include "../include/stdio.h"
#include "../include/string.h"
#include "../include/paging.h"
#include "../include/liballoc.h"
l9660_fs global_fs;
l9660_fs* global_fs;
l9660_dir* root_dir;
bool read_sector_callback(l9660_fs *fs, void *buf, uint32_t sector) {
// The 'fs' parameter is not needed for our simple case, so we can ignore it.
@@ -20,13 +22,26 @@ bool read_sector_callback(l9660_fs *fs, void *buf, uint32_t sector) {
void mount_fs()
{
l9660_status status = l9660_openfs(&global_fs, read_sector_callback);
global_fs = malloc(sizeof(l9660_fs));
l9660_status status = l9660_openfs(global_fs, read_sector_callback);
if (status != L9660_OK) {
printf("Error opening ISO9660 filesystem!\n");
return;
}
printf("FS mounted\n");
root_dir = malloc(sizeof(l9660_dir));
status = l9660_fs_open_root(root_dir, global_fs);
if(status != L9660_OK)
{
printf("Error opening root dir\n");
return;
}
printf("root dir opened\n");
}
void list_files(l9660_dir *dir)
@@ -73,5 +88,33 @@ size_t vfs_read_file(l9660_file *file, uint8_t* buffer)
total_read += read;
}
return total_read;
}
size_t vfs_read_file_length(l9660_file *file, uint8_t* buffer, size_t length)
{
size_t total_read = 0;
l9660_status status;
while(total_read < length) {
char buf[128];
size_t to_read = (length - total_read) < 128 ? (length - total_read) : 128;
size_t read = 0;
status = l9660_read(file, buf, to_read, &read);
if (status != L9660_OK) {
printf("An error occurred during file read!\n");
break;
}
//check for EOF
if (read == 0)
break;
memcpy(buffer + total_read, buf, read);
total_read += read;
}
return total_read;
}
+3 -10
View File
@@ -28,21 +28,14 @@ void apply_pic_masks()
void test_iso9660()
{
l9660_dir g_root_dir;
l9660_file g_test_file;
l9660_status status;
// 2. Open the root directory
status = l9660_fs_open_root(&g_root_dir, &global_fs);
if (status != L9660_OK) {
printf("Error opening root directory!\n");
return;
}
printf("Files in root directory:\n");
for (;;) {
l9660_dirent *dent;
status = l9660_readdir(&g_root_dir, &dent);
status = l9660_readdir(root_dir, &dent);
if (dent == NULL || status != L9660_OK) {
break; // End of directory or error
@@ -58,11 +51,11 @@ void test_iso9660()
printf("\nReading content of /TEST.TXT:\n");
// Re-open the root directory to reset its position before searching
l9660_fs_open_root(&g_root_dir, &global_fs);
l9660_fs_open_root(root_dir, global_fs);
l9660_dir boot_dir;
l9660_dir grub_dir;
status = l9660_opendirat(&boot_dir, &g_root_dir, "boot");
status = l9660_opendirat(&boot_dir, root_dir, "boot");
printf("STATUS %X\n", status);
status = l9660_opendirat(&grub_dir, &boot_dir, "grub");
printf("STATUS %X\n", status);
+1 -5
View File
@@ -49,13 +49,9 @@ void exec_from_file(const char* filename, const char* dirname)
uint8_t* file;
l9660_dir dir;
l9660_dir root_dir;
l9660_status status;
status = l9660_fs_open_root(&root_dir, &global_fs);
debug_log("l9660_fs_open_root status: 0x%X\n", status);
status = l9660_opendirat(&dir, &root_dir, dirname);
status = l9660_opendirat(&dir, root_dir, dirname);
debug_log("l9660_opendirat status: 0x%X\n", status);
//list_files(&dir);
+5 -2
View File
@@ -6,6 +6,7 @@
#include "../include/string.h"
#include "../include/screen.h"
#include "../include/fat.h"
#include "../include/vfs.h"
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
@@ -157,7 +158,8 @@ int sys_read(TrapFrame *tf)
{
return -EBADF;
}
return 0;//fat_fread(current->file_descriptors[tf->ebx], (void*)tf->ecx, tf->edx);
return vfs_read_file_length(current->file_descriptors[tf->ebx], (uint8_t*)tf->ecx, tf->edx);
//return 0;//fat_fread(current->file_descriptors[tf->ebx], (void*)tf->ecx, tf->edx);
}
}
@@ -208,7 +210,8 @@ int sys_open(TrapFrame *tf)
//debug_log("\n====i: %X\n", i);
if(current->file_descriptors[i] == NULL)
{
current->file_descriptors[i] = malloc(sizeof(file_t)); // Kernel malloc
current->file_descriptors[i] = (l9660_file*)malloc(sizeof(l9660_file)); // Kernel malloc
l9660_openat(current->file_descriptors[i], root_dir, filename);
//debug_log("\n====FILE_DESCRIPTOR: %X\n", i);
int result = 0;//fat_fopen(current->file_descriptors[i], filename, mode_str);
if (result < 0) {