rulimine/stage2/fs/file.c

83 lines
2.1 KiB
C
Raw Normal View History

2020-04-14 06:20:55 +03:00
#include <stddef.h>
#include <stdint.h>
#include <fs/file.h>
#include <fs/echfs.h>
#include <fs/ext2.h>
2020-05-01 18:19:29 +03:00
#include <fs/fat32.h>
2020-04-14 06:20:55 +03:00
#include <lib/blib.h>
2020-09-20 13:03:44 +03:00
#include <mm/pmm.h>
2020-11-01 23:25:35 +03:00
#include <lib/part.h>
bool fs_get_guid(struct guid *guid, struct part *part) {
if (echfs_check_signature(part)) {
return echfs_get_guid(guid, part);
}
if (ext2_check_signature(part)) {
return ext2_get_guid(guid, part);
}
return false;
}
2020-04-14 06:20:55 +03:00
int fopen(struct file_handle *ret, int disk, int partition, const char *filename) {
2020-11-01 23:25:35 +03:00
struct part part;
if (get_part(&part, disk, partition)) {
panic("Invalid partition");
}
if (echfs_check_signature(&part)) {
2020-09-20 13:03:44 +03:00
struct echfs_file_handle *fd = conv_mem_alloc(sizeof(struct echfs_file_handle));
2020-04-14 06:20:55 +03:00
int r = echfs_open(fd, disk, partition, filename);
if (r)
return r;
ret->fd = (void *)fd;
ret->read = (void *)echfs_read;
ret->disk = disk;
ret->partition = partition;
ret->size = fd->dir_entry.size;
return 0;
}
2020-11-01 23:25:35 +03:00
if (ext2_check_signature(&part)) {
2020-09-20 13:03:44 +03:00
struct ext2_file_handle *fd = conv_mem_alloc(sizeof(struct ext2_file_handle));
int r = ext2_open(fd, disk, partition, filename);
2020-04-15 23:34:09 +03:00
if (r)
return r;
ret->fd = (void *)fd;
ret->read = (void *)ext2_read;
ret->disk = disk;
ret->partition = partition;
ret->size = fd->size;
return 0;
}
2020-04-14 06:20:55 +03:00
2020-11-01 23:25:35 +03:00
if (fat32_check_signature(&part)) {
2020-09-20 13:03:44 +03:00
struct fat32_file_handle *fd = conv_mem_alloc(sizeof(struct fat32_file_handle));
2020-05-01 18:19:29 +03:00
int r = fat32_open(fd, disk, partition, filename);
if (r)
return r;
ret->fd = (void *)fd;
ret->read = (void *)fat32_read;
ret->disk = disk;
ret->partition = partition;
ret->size = fd->size_bytes;
return 0;
}
2020-04-14 06:20:55 +03:00
return -1;
}
int fread(struct file_handle *fd, void *buf, uint64_t loc, uint64_t count) {
return fd->read(fd->fd, buf, loc, count);
}