From 1c8b853f8d05dd2203beca45b3b47354a6e58de8 Mon Sep 17 00:00:00 2001 From: daniel-j Date: Tue, 19 Nov 2024 17:51:56 +0100 Subject: [PATCH 1/7] remove duplicate function --- src/filesystem/vfs.c | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/src/filesystem/vfs.c b/src/filesystem/vfs.c index ebf4a67..de59012 100644 --- a/src/filesystem/vfs.c +++ b/src/filesystem/vfs.c @@ -562,29 +562,6 @@ off_t _lseek(int fildes, off_t offset, int whence) { return _error_remap(pos); } -off_t _ftello_r(struct _reent *ptr, register FILE *fp) { - (void)ptr; - int fildes = fp->_file; - auto_init_recursive_mutex(_mutex); - recursive_mutex_enter_blocking(&_mutex); - - if (!is_valid_file_descriptor(fildes)) { - recursive_mutex_exit(&_mutex); - return _error_remap(-EBADF); - } - fs_file_t *file = file_descriptor[FILENO_INDEX(fildes)].file; - filesystem_t *fs = file_descriptor[FILENO_INDEX(fildes)].filesystem; - if (fs == NULL) { - recursive_mutex_exit(&_mutex); - return _error_remap(-EBADF); - } - - off_t pos = fs->file_tell(fs, file); - recursive_mutex_exit(&_mutex); - - return _error_remap(pos); -} - int ftruncate(int fildes, off_t length) { auto_init_recursive_mutex(_mutex); recursive_mutex_enter_blocking(&_mutex); From 9b95465f774063a17e30c33f457ca0247da212ab Mon Sep 17 00:00:00 2001 From: daniel-j Date: Tue, 19 Nov 2024 18:10:31 +0100 Subject: [PATCH 2/7] rename define to not collide --- include/filesystem/filesystem.h | 2 +- src/filesystem/fat.c | 16 ++++++++-------- src/filesystem/vfs.c | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/include/filesystem/filesystem.h b/include/filesystem/filesystem.h index decff94..bb728c1 100644 --- a/include/filesystem/filesystem.h +++ b/include/filesystem/filesystem.h @@ -19,7 +19,7 @@ extern "C" { #include #include "blockdevice/blockdevice.h" -#define PATH_MAX 256 +#define FS_PATH_MAX 256 enum { FILESYSTEM_TYPE_FAT, diff --git a/src/filesystem/fat.c b/src/filesystem/fat.c index 309183e..fa76917 100644 --- a/src/filesystem/fat.c +++ b/src/filesystem/fat.c @@ -320,7 +320,7 @@ static const char *fat_path_prefix(char *dist, int id, const char *path) { static int file_remove(filesystem_t *fs, const char *path) { filesystem_fat_context_t *context = fs->context; - char fpath[PATH_MAX]; + char fpath[FS_PATH_MAX]; fat_path_prefix(fpath, context->id, path); mutex_enter_blocking(&context->_mutex); @@ -337,8 +337,8 @@ static int file_remove(filesystem_t *fs, const char *path) { static int file_rename(filesystem_t *fs, const char *oldpath, const char *newpath) { filesystem_fat_context_t *context = fs->context; - char oldfpath[PATH_MAX]; - char newfpath[PATH_MAX]; + char oldfpath[FS_PATH_MAX]; + char newfpath[FS_PATH_MAX]; fat_path_prefix(oldfpath, context->id, oldpath); fat_path_prefix(newfpath, context->id, newpath); @@ -355,7 +355,7 @@ static int file_rename(filesystem_t *fs, const char *oldpath, const char *newpat static int file_mkdir(filesystem_t *fs, const char *path, mode_t mode) { (void)mode; filesystem_fat_context_t *context = fs->context; - char fpath[PATH_MAX]; + char fpath[FS_PATH_MAX]; fat_path_prefix(fpath, context->id, path); mutex_enter_blocking(&context->_mutex); @@ -370,7 +370,7 @@ static int file_mkdir(filesystem_t *fs, const char *path, mode_t mode) { static int file_rmdir(filesystem_t *fs, const char *path) { filesystem_fat_context_t *context = fs->context; - char fpath[PATH_MAX]; + char fpath[FS_PATH_MAX]; fat_path_prefix(fpath, context->id, path); mutex_enter_blocking(&context->_mutex); @@ -385,7 +385,7 @@ static int file_rmdir(filesystem_t *fs, const char *path) { static int file_stat(filesystem_t *fs, const char *path, struct stat *st) { filesystem_fat_context_t *context = fs->context; - char fpath[PATH_MAX]; + char fpath[FS_PATH_MAX]; fat_path_prefix(fpath, context->id, path); FILINFO f = {0}; @@ -422,7 +422,7 @@ static int file_open(filesystem_t *fs, fs_file_t *file, const char *path, int fl if (flags & O_APPEND) open_mode |= FA_OPEN_APPEND; - char fpath[PATH_MAX]; + char fpath[FS_PATH_MAX]; filesystem_fat_context_t *context = fs->context; fat_path_prefix(fpath, context->id, path); fat_file_t *fat_file = file->context = calloc(1, sizeof(fat_file_t)); @@ -586,7 +586,7 @@ static int file_truncate(filesystem_t *fs, fs_file_t *file, off_t length) { static int dir_open(filesystem_t *fs, fs_dir_t *dir, const char *path) { filesystem_fat_context_t *context = fs->context; - char fpath[PATH_MAX]; + char fpath[FS_PATH_MAX]; fat_path_prefix(fpath, context->id, path); FATFS_DIR *dh = calloc(1, sizeof(FATFS_DIR)); diff --git a/src/filesystem/vfs.c b/src/filesystem/vfs.c index de59012..bec1dea 100644 --- a/src/filesystem/vfs.c +++ b/src/filesystem/vfs.c @@ -21,7 +21,7 @@ typedef struct { typedef struct { fs_file_t *file; filesystem_t *filesystem; - char path[PATH_MAX + 1]; + char path[FS_PATH_MAX + 1]; } file_descriptor_t; typedef struct { @@ -434,7 +434,7 @@ int _open(const char *path, int oflags, ...) { return _error_remap(err); } file_descriptor[FILENO_INDEX(fd)].filesystem = fs; - strncpy(file_descriptor[FILENO_INDEX(fd)].path, path, PATH_MAX); + strncpy(file_descriptor[FILENO_INDEX(fd)].path, path, FS_PATH_MAX); recursive_mutex_exit(&_mutex); From 0c1dd4d078507affec03bad0eac8ccade7815883 Mon Sep 17 00:00:00 2001 From: daniel-j Date: Wed, 20 Nov 2024 18:58:11 +0100 Subject: [PATCH 3/7] add fatfs last modified timestamp to stat --- src/filesystem/fat.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/filesystem/fat.c b/src/filesystem/fat.c index fa76917..919dd77 100644 --- a/src/filesystem/fat.c +++ b/src/filesystem/fat.c @@ -396,8 +396,18 @@ static int file_stat(filesystem_t *fs, const char *path, struct stat *st) { if (res != FR_OK) { return fat_error_remap(res); } + + struct tm mtime = {0}; + mtime.tm_year = (f.fdate >> 9) + 80; + mtime.tm_mon = (f.fdate >> 5) & 0b1111; + mtime.tm_mday = f.fdate & 0b11111; + mtime.tm_hour = f.ftime >> 11; + mtime.tm_min = (f.ftime >> 5) & 0b111111; + mtime.tm_sec = (f.ftime & 0b11111) << 1; + st->st_size = f.fsize; st->st_mode = 0; + st->st_mtime = mktime(&mtime); st->st_mode |= (f.fattrib & AM_DIR) ? S_IFDIR : S_IFREG; st->st_mode |= (f.fattrib & AM_RDO) ? (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) : From b65496b972bc1303d81efdd6db7e5ba97a86ea3d Mon Sep 17 00:00:00 2001 From: daniel-j Date: Wed, 20 Nov 2024 19:15:17 +0100 Subject: [PATCH 4/7] reorder lines --- src/filesystem/fat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/filesystem/fat.c b/src/filesystem/fat.c index 919dd77..683fdff 100644 --- a/src/filesystem/fat.c +++ b/src/filesystem/fat.c @@ -407,11 +407,11 @@ static int file_stat(filesystem_t *fs, const char *path, struct stat *st) { st->st_size = f.fsize; st->st_mode = 0; - st->st_mtime = mktime(&mtime); st->st_mode |= (f.fattrib & AM_DIR) ? S_IFDIR : S_IFREG; st->st_mode |= (f.fattrib & AM_RDO) ? (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) : (S_IRWXU | S_IRWXG | S_IRWXO); + st->st_mtime = mktime(&mtime); return 0; } From 6ef08d974918694f51b0491edf9e6c9d73770aab Mon Sep 17 00:00:00 2001 From: daniel-j Date: Mon, 16 Jun 2025 17:11:28 +0200 Subject: [PATCH 5/7] use PATH_MAX from limits.h --- include/filesystem/filesystem.h | 3 +-- src/filesystem/fat.c | 16 ++++++++-------- src/filesystem/vfs.c | 4 ++-- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/include/filesystem/filesystem.h b/include/filesystem/filesystem.h index bb728c1..4458642 100644 --- a/include/filesystem/filesystem.h +++ b/include/filesystem/filesystem.h @@ -17,10 +17,9 @@ extern "C" { #include #include #include +#include #include "blockdevice/blockdevice.h" -#define FS_PATH_MAX 256 - enum { FILESYSTEM_TYPE_FAT, FILESYSTEM_TYPE_LITTLEFS, diff --git a/src/filesystem/fat.c b/src/filesystem/fat.c index 683fdff..ede17b6 100644 --- a/src/filesystem/fat.c +++ b/src/filesystem/fat.c @@ -320,7 +320,7 @@ static const char *fat_path_prefix(char *dist, int id, const char *path) { static int file_remove(filesystem_t *fs, const char *path) { filesystem_fat_context_t *context = fs->context; - char fpath[FS_PATH_MAX]; + char fpath[PATH_MAX]; fat_path_prefix(fpath, context->id, path); mutex_enter_blocking(&context->_mutex); @@ -337,8 +337,8 @@ static int file_remove(filesystem_t *fs, const char *path) { static int file_rename(filesystem_t *fs, const char *oldpath, const char *newpath) { filesystem_fat_context_t *context = fs->context; - char oldfpath[FS_PATH_MAX]; - char newfpath[FS_PATH_MAX]; + char oldfpath[PATH_MAX]; + char newfpath[PATH_MAX]; fat_path_prefix(oldfpath, context->id, oldpath); fat_path_prefix(newfpath, context->id, newpath); @@ -355,7 +355,7 @@ static int file_rename(filesystem_t *fs, const char *oldpath, const char *newpat static int file_mkdir(filesystem_t *fs, const char *path, mode_t mode) { (void)mode; filesystem_fat_context_t *context = fs->context; - char fpath[FS_PATH_MAX]; + char fpath[PATH_MAX]; fat_path_prefix(fpath, context->id, path); mutex_enter_blocking(&context->_mutex); @@ -370,7 +370,7 @@ static int file_mkdir(filesystem_t *fs, const char *path, mode_t mode) { static int file_rmdir(filesystem_t *fs, const char *path) { filesystem_fat_context_t *context = fs->context; - char fpath[FS_PATH_MAX]; + char fpath[PATH_MAX]; fat_path_prefix(fpath, context->id, path); mutex_enter_blocking(&context->_mutex); @@ -385,7 +385,7 @@ static int file_rmdir(filesystem_t *fs, const char *path) { static int file_stat(filesystem_t *fs, const char *path, struct stat *st) { filesystem_fat_context_t *context = fs->context; - char fpath[FS_PATH_MAX]; + char fpath[PATH_MAX]; fat_path_prefix(fpath, context->id, path); FILINFO f = {0}; @@ -432,7 +432,7 @@ static int file_open(filesystem_t *fs, fs_file_t *file, const char *path, int fl if (flags & O_APPEND) open_mode |= FA_OPEN_APPEND; - char fpath[FS_PATH_MAX]; + char fpath[PATH_MAX]; filesystem_fat_context_t *context = fs->context; fat_path_prefix(fpath, context->id, path); fat_file_t *fat_file = file->context = calloc(1, sizeof(fat_file_t)); @@ -596,7 +596,7 @@ static int file_truncate(filesystem_t *fs, fs_file_t *file, off_t length) { static int dir_open(filesystem_t *fs, fs_dir_t *dir, const char *path) { filesystem_fat_context_t *context = fs->context; - char fpath[FS_PATH_MAX]; + char fpath[PATH_MAX]; fat_path_prefix(fpath, context->id, path); FATFS_DIR *dh = calloc(1, sizeof(FATFS_DIR)); diff --git a/src/filesystem/vfs.c b/src/filesystem/vfs.c index bec1dea..de59012 100644 --- a/src/filesystem/vfs.c +++ b/src/filesystem/vfs.c @@ -21,7 +21,7 @@ typedef struct { typedef struct { fs_file_t *file; filesystem_t *filesystem; - char path[FS_PATH_MAX + 1]; + char path[PATH_MAX + 1]; } file_descriptor_t; typedef struct { @@ -434,7 +434,7 @@ int _open(const char *path, int oflags, ...) { return _error_remap(err); } file_descriptor[FILENO_INDEX(fd)].filesystem = fs; - strncpy(file_descriptor[FILENO_INDEX(fd)].path, path, FS_PATH_MAX); + strncpy(file_descriptor[FILENO_INDEX(fd)].path, path, PATH_MAX); recursive_mutex_exit(&_mutex); From 6937cdd6e626c66b9f17d0225a69294db2f43af0 Mon Sep 17 00:00:00 2001 From: daniel-j Date: Wed, 18 Jun 2025 02:51:26 +0200 Subject: [PATCH 6/7] small fix to stat mtime --- src/filesystem/fat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/filesystem/fat.c b/src/filesystem/fat.c index ede17b6..b772b2a 100644 --- a/src/filesystem/fat.c +++ b/src/filesystem/fat.c @@ -399,7 +399,7 @@ static int file_stat(filesystem_t *fs, const char *path, struct stat *st) { struct tm mtime = {0}; mtime.tm_year = (f.fdate >> 9) + 80; - mtime.tm_mon = (f.fdate >> 5) & 0b1111; + mtime.tm_mon = ((f.fdate >> 5) & 0b1111) - 1; mtime.tm_mday = f.fdate & 0b11111; mtime.tm_hour = f.ftime >> 11; mtime.tm_min = (f.ftime >> 5) & 0b111111; From f83a210f14c1aa4ed0f07a7404d3eaad2bee0bc5 Mon Sep 17 00:00:00 2001 From: daniel-j Date: Wed, 18 Jun 2025 17:42:10 +0200 Subject: [PATCH 7/7] fix _ftello_r by wrapping it --- CMakeLists.txt | 2 +- src/filesystem/vfs.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 146f9ef..91f14ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,7 +108,7 @@ target_include_directories(filesystem_vfs INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include ) target_compile_options(filesystem_vfs INTERFACE -Werror -Wall -Wextra -Wnull-dereference) -target_link_libraries(filesystem_vfs INTERFACE pico_sync) +target_link_libraries(filesystem_vfs INTERFACE pico_sync -Wl,--wrap=_ftello_r) # Default file system library add_library(filesystem_default INTERFACE) diff --git a/src/filesystem/vfs.c b/src/filesystem/vfs.c index de59012..7ded859 100644 --- a/src/filesystem/vfs.c +++ b/src/filesystem/vfs.c @@ -562,6 +562,29 @@ off_t _lseek(int fildes, off_t offset, int whence) { return _error_remap(pos); } +off_t __wrap__ftello_r(struct _reent *ptr, register FILE *fp) { + (void)ptr; + int fildes = fp->_file; + auto_init_recursive_mutex(_mutex); + recursive_mutex_enter_blocking(&_mutex); + + if (!is_valid_file_descriptor(fildes)) { + recursive_mutex_exit(&_mutex); + return _error_remap(-EBADF); + } + fs_file_t *file = file_descriptor[FILENO_INDEX(fildes)].file; + filesystem_t *fs = file_descriptor[FILENO_INDEX(fildes)].filesystem; + if (fs == NULL) { + recursive_mutex_exit(&_mutex); + return _error_remap(-EBADF); + } + + off_t pos = fs->file_tell(fs, file); + recursive_mutex_exit(&_mutex); + + return _error_remap(pos); +} + int ftruncate(int fildes, off_t length) { auto_init_recursive_mutex(_mutex); recursive_mutex_enter_blocking(&_mutex);