mirror of
https://git.planet-casio.com/Lephenixnoir/gint.git
synced 2025-05-18 15:29:16 +02:00
fygue: support read() primitive
This commit is contained in:
parent
20cb87c0c1
commit
d9b03e7052
9 changed files with 152 additions and 5 deletions
|
@ -271,6 +271,7 @@ set(SOURCES
|
||||||
src/fs/fygue/fygue_file_lseek.c
|
src/fs/fygue/fygue_file_lseek.c
|
||||||
src/fs/fygue/fygue_file_write.c
|
src/fs/fygue/fygue_file_write.c
|
||||||
src/fs/fygue/fygue_file_close.c
|
src/fs/fygue/fygue_file_close.c
|
||||||
|
src/fs/fygue/fygue_file_read.c
|
||||||
src/fs/fygue/fat/cluster.c
|
src/fs/fygue/fat/cluster.c
|
||||||
src/fs/fygue/fat/initialize.c
|
src/fs/fygue/fat/initialize.c
|
||||||
src/fs/fygue/fat/fat_dir_read.c
|
src/fs/fygue/fat/fat_dir_read.c
|
||||||
|
@ -278,6 +279,8 @@ set(SOURCES
|
||||||
src/fs/fygue/fat/fat_dir_stat.c
|
src/fs/fygue/fat/fat_dir_stat.c
|
||||||
src/fs/fygue/fat/fat_file_stat.c
|
src/fs/fygue/fat/fat_file_stat.c
|
||||||
src/fs/fygue/fat/fat_file_close.c
|
src/fs/fygue/fat/fat_file_close.c
|
||||||
|
src/fs/fygue/fat/fat_file_read.c
|
||||||
|
src/fs/fygue/fat/fat_file_seek.c
|
||||||
src/fs/fygue/fat/resolve.c
|
src/fs/fygue/fat/resolve.c
|
||||||
src/fs/fygue/fat/sector.c
|
src/fs/fygue/fat/sector.c
|
||||||
src/fs/fygue/flash/cluster.c
|
src/fs/fygue/flash/cluster.c
|
||||||
|
|
|
@ -89,6 +89,8 @@ struct fygue_fat_file
|
||||||
int cluster_count;
|
int cluster_count;
|
||||||
} *chunk;
|
} *chunk;
|
||||||
int chunk_count;
|
int chunk_count;
|
||||||
|
int chunk_rd_index;
|
||||||
|
int chunk_rd_offset;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* fygue_fat_resolve: internal resolve information */
|
/* fygue_fat_resolve: internal resolve information */
|
||||||
|
@ -145,14 +147,29 @@ extern int fygue_fat_dir_stat(
|
||||||
// File interface
|
// File interface
|
||||||
//---
|
//---
|
||||||
|
|
||||||
/* fygue_fat_file_stat(): directory stat primitive */
|
/* fygue_fat_file_read(): read primitive */
|
||||||
|
extern int fygue_fat_file_read(
|
||||||
|
struct fygue_fat *fat,
|
||||||
|
struct fygue_fat_file *file,
|
||||||
|
void *buffer,
|
||||||
|
size_t size
|
||||||
|
);
|
||||||
|
|
||||||
|
/* fygue_fat_file_stat(): stat primitive */
|
||||||
extern int fygue_fat_file_stat(
|
extern int fygue_fat_file_stat(
|
||||||
struct fygue_fat *fat,
|
struct fygue_fat *fat,
|
||||||
struct fygue_fat_file *file,
|
struct fygue_fat_file *file,
|
||||||
struct stat *statbuf
|
struct stat *statbuf
|
||||||
);
|
);
|
||||||
|
|
||||||
/* fygue_fat_file_close(): closedir primitive */
|
/* fygue_fat_file_seek(): lseek primitive */
|
||||||
|
extern int fygue_fat_file_seek(
|
||||||
|
struct fygue_fat *fat,
|
||||||
|
struct fygue_fat_file *file,
|
||||||
|
off_t offset
|
||||||
|
);
|
||||||
|
|
||||||
|
/* fygue_fat_file_close(): close primitive */
|
||||||
extern int fygue_fat_file_close(
|
extern int fygue_fat_file_close(
|
||||||
struct fygue_fat *fat,
|
struct fygue_fat *fat,
|
||||||
struct fygue_fat_file *dir
|
struct fygue_fat_file *dir
|
||||||
|
|
47
src/fs/fygue/fat/fat_file_read.c
Normal file
47
src/fs/fygue/fat/fat_file_read.c
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "fat.h"
|
||||||
|
|
||||||
|
/* fygue_fat_file_read(): read primitive */
|
||||||
|
int fygue_fat_file_read(
|
||||||
|
struct fygue_fat *fat,
|
||||||
|
struct fygue_fat_file *file,
|
||||||
|
void *buffer,
|
||||||
|
size_t size
|
||||||
|
) {
|
||||||
|
void *chunk_data_addr;
|
||||||
|
size_t chunk_data_available;
|
||||||
|
size_t chunk_data_offset;
|
||||||
|
size_t read;
|
||||||
|
|
||||||
|
if (fat == NULL || file == NULL || buffer == NULL) {
|
||||||
|
errno = EIO;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (file->chunk == NULL) {
|
||||||
|
errno = EIO;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
read = 0;
|
||||||
|
chunk_data_offset = file->chunk_rd_offset;
|
||||||
|
while (read < size)
|
||||||
|
{
|
||||||
|
if (file->chunk_rd_index >= file->chunk_count)
|
||||||
|
break;
|
||||||
|
chunk_data_addr = (void*)file->chunk[file->chunk_rd_index].addr;
|
||||||
|
chunk_data_available = file->chunk[file->chunk_rd_index].size;
|
||||||
|
chunk_data_available -= chunk_data_offset;
|
||||||
|
if (read + chunk_data_available > size)
|
||||||
|
{
|
||||||
|
chunk_data_offset = size - read;
|
||||||
|
memcpy(buffer, chunk_data_addr, chunk_data_offset);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
memcpy(buffer, chunk_data_addr, chunk_data_available);
|
||||||
|
read += chunk_data_available;
|
||||||
|
file->chunk_rd_index += 1;
|
||||||
|
chunk_data_offset = 0;
|
||||||
|
}
|
||||||
|
file->chunk_rd_offset = chunk_data_offset;
|
||||||
|
return read;
|
||||||
|
}
|
30
src/fs/fygue/fat/fat_file_seek.c
Normal file
30
src/fs/fygue/fat/fat_file_seek.c
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
#include <errno.h>
|
||||||
|
#include "fat.h"
|
||||||
|
|
||||||
|
/* fygue_fat_file_seek(): seek primitive */
|
||||||
|
int fygue_fat_file_seek(
|
||||||
|
struct fygue_fat *fat,
|
||||||
|
struct fygue_fat_file *file,
|
||||||
|
off_t offset
|
||||||
|
) {
|
||||||
|
off_t offset_test;
|
||||||
|
|
||||||
|
if (fat == NULL || file == NULL) {
|
||||||
|
errno = EIO;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
offset_test = 0;
|
||||||
|
file->chunk_rd_index = 0;
|
||||||
|
file->chunk_rd_offset = 0;
|
||||||
|
for (int i = 0; i < file->chunk_count; i++)
|
||||||
|
{
|
||||||
|
offset_test += file->chunk[i].size;
|
||||||
|
if (offset_test > offset) {
|
||||||
|
file->chunk_rd_offset = offset - offset_test;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
file->chunk_rd_index += 1;
|
||||||
|
}
|
||||||
|
errno = EIO;
|
||||||
|
return -1;
|
||||||
|
}
|
|
@ -84,7 +84,7 @@ int fygue_resolve(char const * const path, struct fygue_resolve *resolve)
|
||||||
//---
|
//---
|
||||||
|
|
||||||
const fs_descriptor_type_t fygue_descriptor_type = {
|
const fs_descriptor_type_t fygue_descriptor_type = {
|
||||||
.read = NULL,
|
.read = (void*)&fygue_file_read,
|
||||||
.write = (void*)&fygue_file_write,
|
.write = (void*)&fygue_file_write,
|
||||||
.lseek = (void*)&fygue_file_lseek,
|
.lseek = (void*)&fygue_file_lseek,
|
||||||
.close = (void*)&fygue_file_close,
|
.close = (void*)&fygue_file_close,
|
||||||
|
|
|
@ -114,7 +114,7 @@ extern off_t fygue_dir_lseek(
|
||||||
);
|
);
|
||||||
|
|
||||||
/* fygue_dir_write(): write directory (EISDIR) */
|
/* fygue_dir_write(): write directory (EISDIR) */
|
||||||
ssize_t fygue_dir_write(
|
extern ssize_t fygue_dir_write(
|
||||||
struct fygue_descriptor *desc,
|
struct fygue_descriptor *desc,
|
||||||
void *buffer,
|
void *buffer,
|
||||||
size_t size
|
size_t size
|
||||||
|
@ -127,6 +127,13 @@ extern int fygue_dir_close(struct fygue_descriptor *desc);
|
||||||
// File interface
|
// File interface
|
||||||
//---
|
//---
|
||||||
|
|
||||||
|
/* fygue_file_read(): read primitive */
|
||||||
|
extern ssize_t fygue_file_read(
|
||||||
|
struct fygue_descriptor *desc,
|
||||||
|
void *buffer,
|
||||||
|
size_t size
|
||||||
|
);
|
||||||
|
|
||||||
/* fygue_file_lseek(): seek directory */
|
/* fygue_file_lseek(): seek directory */
|
||||||
extern off_t fygue_file_lseek(
|
extern off_t fygue_file_lseek(
|
||||||
struct fygue_descriptor *desc,
|
struct fygue_descriptor *desc,
|
||||||
|
|
|
@ -24,7 +24,7 @@ static int _fygue_dir_fat_read(
|
||||||
|
|
||||||
rc = fygue_fat_dir_read(
|
rc = fygue_fat_dir_read(
|
||||||
&(fsinfo->fat),
|
&(fsinfo->fat),
|
||||||
&desc->resolve.dir.fat,
|
&(desc->resolve.dir.fat),
|
||||||
&fat_dirent
|
&fat_dirent
|
||||||
);
|
);
|
||||||
if (rc <= -3)
|
if (rc <= -3)
|
||||||
|
|
|
@ -6,6 +6,8 @@ off_t fygue_file_lseek(
|
||||||
off_t offset,
|
off_t offset,
|
||||||
int whence
|
int whence
|
||||||
) {
|
) {
|
||||||
|
struct fygue_fsinfo *fsinfo;
|
||||||
|
|
||||||
ENOTSUP_IF_NOT_FYGUE(-1);
|
ENOTSUP_IF_NOT_FYGUE(-1);
|
||||||
if (desc == NULL || desc->resolve.type != FYGUE_FILE_TYPE_FILE) {
|
if (desc == NULL || desc->resolve.type != FYGUE_FILE_TYPE_FILE) {
|
||||||
errno = EBADF;
|
errno = EBADF;
|
||||||
|
@ -15,6 +17,10 @@ off_t fygue_file_lseek(
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
if (fygue_mount(&fsinfo, true) != 0) {
|
||||||
|
errno = EIO;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
if(whence == SEEK_CUR)
|
if(whence == SEEK_CUR)
|
||||||
offset += desc->resolve.dir.pos;
|
offset += desc->resolve.dir.pos;
|
||||||
if(whence == SEEK_END)
|
if(whence == SEEK_END)
|
||||||
|
@ -24,5 +30,10 @@ off_t fygue_file_lseek(
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
desc->resolve.file.cursor = offset;
|
desc->resolve.file.cursor = offset;
|
||||||
|
fygue_fat_file_seek(
|
||||||
|
&(fsinfo->fat),
|
||||||
|
&(desc->resolve.file.fat),
|
||||||
|
offset
|
||||||
|
);
|
||||||
return desc->resolve.file.cursor;
|
return desc->resolve.file.cursor;
|
||||||
}
|
}
|
||||||
|
|
32
src/fs/fygue/fygue_file_read.c
Normal file
32
src/fs/fygue/fygue_file_read.c
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "fygue.h"
|
||||||
|
|
||||||
|
/* fygue_file_read(): write directory (EISDIR) */
|
||||||
|
ssize_t fygue_file_read(
|
||||||
|
struct fygue_descriptor *desc,
|
||||||
|
void *buffer,
|
||||||
|
size_t size
|
||||||
|
) {
|
||||||
|
struct fygue_fsinfo *fsinfo;
|
||||||
|
|
||||||
|
ENOTSUP_IF_NOT_FYGUE(-1);
|
||||||
|
if (desc == NULL || desc->resolve.type != FYGUE_FILE_TYPE_FILE) {
|
||||||
|
errno = EBADF;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (buffer == NULL) {
|
||||||
|
errno = EFAULT;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (fygue_mount(&fsinfo, true) != 0) {
|
||||||
|
errno = EIO;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return fygue_fat_file_read(
|
||||||
|
&(fsinfo->fat),
|
||||||
|
&(desc->resolve.file.fat),
|
||||||
|
buffer,
|
||||||
|
size
|
||||||
|
);
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue