diff --git a/CMakeLists.txt b/CMakeLists.txt index a98f375..2a37596 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -270,12 +270,14 @@ set(SOURCES src/fs/fygue/fygue_dir_close.c src/fs/fygue/fygue_file_lseek.c src/fs/fygue/fygue_file_write.c + src/fs/fygue/fygue_file_close.c src/fs/fygue/fat/cluster.c src/fs/fygue/fat/initialize.c src/fs/fygue/fat/fat_dir_read.c src/fs/fygue/fat/fat_dir_close.c src/fs/fygue/fat/fat_dir_stat.c src/fs/fygue/fat/fat_file_stat.c + src/fs/fygue/fat/fat_file_close.c src/fs/fygue/fat/resolve.c src/fs/fygue/fat/sector.c src/fs/fygue/flash/cluster.c diff --git a/src/fs/fygue/fat/fat.h b/src/fs/fygue/fat/fat.h index 681a0d1..83e221c 100644 --- a/src/fs/fygue/fat/fat.h +++ b/src/fs/fygue/fat/fat.h @@ -152,6 +152,12 @@ extern int fygue_fat_file_stat( struct stat *statbuf ); +/* fygue_fat_file_close(): closedir primitive */ +extern int fygue_fat_file_close( + struct fygue_fat *fat, + struct fygue_fat_file *dir +); + //--- // Cluster interface //--- diff --git a/src/fs/fygue/fat/fat_dir_close.c b/src/fs/fygue/fat/fat_dir_close.c index 19319a1..8591bba 100644 --- a/src/fs/fygue/fat/fat_dir_close.c +++ b/src/fs/fygue/fat/fat_dir_close.c @@ -1,3 +1,4 @@ +#include #include "fat.h" /* fygue_fat_dir_close(): closedir primitive */ @@ -6,6 +7,7 @@ int fygue_fat_dir_close(struct fygue_fat *fat, struct fygue_fat_dir *dir) if (fat == NULL || dir == NULL) return -1; // nothing to do here... + memset(dir, 0x00, sizeof(struct fygue_fat_file)); return 0; } diff --git a/src/fs/fygue/fat/fat_file_close.c b/src/fs/fygue/fat/fat_file_close.c new file mode 100644 index 0000000..4de0c27 --- /dev/null +++ b/src/fs/fygue/fat/fat_file_close.c @@ -0,0 +1,14 @@ +#include +#include +#include "fat.h" + +/* fygue_fat_file_close(): close primitive */ +int fygue_fat_file_close(struct fygue_fat *fat, struct fygue_fat_file *file) +{ + if (fat == NULL || file == NULL) + return -1; + if (file->chunk != NULL) + free(file->chunk); + memset(file, 0x00, sizeof(struct fygue_fat_file)); + return 0; +} diff --git a/src/fs/fygue/fygue.c b/src/fs/fygue/fygue.c index 2a42d48..b532cdf 100644 --- a/src/fs/fygue/fygue.c +++ b/src/fs/fygue/fygue.c @@ -87,7 +87,7 @@ const fs_descriptor_type_t fygue_descriptor_type = { .read = NULL, .write = (void*)&fygue_file_write, .lseek = (void*)&fygue_file_lseek, - .close = NULL, + .close = (void*)&fygue_file_close, }; const fs_descriptor_type_t fygue_dir_descriptor_type = { diff --git a/src/fs/fygue/fygue.h b/src/fs/fygue/fygue.h index cad2fe9..1f919a9 100644 --- a/src/fs/fygue/fygue.h +++ b/src/fs/fygue/fygue.h @@ -141,6 +141,9 @@ extern ssize_t fygue_file_write( size_t size ); +/* fygue_file_close(): close directory */ +extern int fygue_file_close(struct fygue_descriptor *desc); + #ifdef __cplusplus } #endif diff --git a/src/fs/fygue/fygue_file_close.c b/src/fs/fygue/fygue_file_close.c new file mode 100644 index 0000000..8f2ad8a --- /dev/null +++ b/src/fs/fygue/fygue_file_close.c @@ -0,0 +1,23 @@ +#include +#include +#include "fygue.h" +#include "fat/fat.h" + +/* fygue_file_close(): close directory */ +int fygue_file_close(struct fygue_descriptor *desc) +{ + struct fygue_fsinfo *fsinfo; + + ENOTSUP_IF_NOT_FYGUE(-1); + if (desc == NULL || desc->resolve.type != FYGUE_FILE_TYPE_FILE) { + errno = EBADF; + return -1; + } + if (fygue_mount(&fsinfo, true) != 0) { + errno = EIO; + return -1; + } + fygue_fat_file_close(&(fsinfo->fat), &desc->resolve.file.fat); + memset(desc, 0x00, sizeof(struct fygue_descriptor)); + return 0; +}