vxBoot/src/fs/smemfs/pread.c

78 lines
1.5 KiB
C

#include "vxBoot/fs/smemfs.h"
#include <gint/bfile.h>
#include <gint/gint.h>
#include <string.h>
/* internal struct */
struct __file_info {
uint16_t pathname[256];
void *buf;
size_t count;
off_t pos;
};
/* generate_abolute_path(): Generate abolute path
This function will generate the absolute path of a file because Casio's open
primitive doesn't handle cannonical path */
static void generate_absolute_path(uint16_t *pathname,
struct smemfs_inode *inode, int *pos)
{
if (inode == NULL) {
memcpy(pathname, u"\\\\fls0", 12);
*pos = 6;
return;
}
generate_absolute_path(pathname, inode->parent, pos);
pathname[(*pos)++] = '\\';
for (int i = 0; inode->name[i] != '\0'; ) {
pathname[*pos] = inode->name[i];
*pos = *pos + 1;
i = i + 1;
}
pathname[*pos] = '\0';
}
/* __smemfs_pread() : involved in Casio's world */
static void __smemfs_pread(struct __file_info *info, ssize_t *read)
{
int handle;
*read = -1;
handle = BFile_Open(info->pathname, BFile_ReadOnly);
if (handle >= 0) {
*read = BFile_Read(handle, info->buf, info->count, info->pos);
BFile_Close(handle);
}
}
/* smemfs_read(): Read primitive */
ssize_t smemfs_pread(struct smemfs_inode *inode,
void *buf, size_t count, off_t pos)
{
struct __file_info file_info = {
.buf = buf,
.count = count,
.pos = pos
};
ssize_t read;
int tmp;
if (inode == NULL)
return (-1);
tmp = 0;
generate_absolute_path(file_info.pathname, inode, &tmp);
gint_world_switch(GINT_CALL(
(void*)&__smemfs_pread,
(void*)&file_info, &read
));
return (read);
}