syscalls: improve working with files using vfs
This commit is contained in:
+45
-2
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user