From 442e28316e9584f99f0daae520ccea9b349a07e3 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 18 Oct 2018 12:48:07 +0700 Subject: [PATCH] full filepath is stored in File, name() only return basename correct open subdirectory file --- libraries/FileSystem/src/FileIO.cpp | 16 +++++----- libraries/FileSystem/src/FileIO.h | 6 ++-- libraries/FileSystem/src/InternalFS.cpp | 39 +++++++++++++++---------- libraries/FileSystem/src/InternalFS.h | 8 ++--- 4 files changed, 39 insertions(+), 30 deletions(-) diff --git a/libraries/FileSystem/src/FileIO.cpp b/libraries/FileSystem/src/FileIO.cpp index fe37e143..dcbf5fab 100644 --- a/libraries/FileSystem/src/FileIO.cpp +++ b/libraries/FileSystem/src/FileIO.cpp @@ -44,7 +44,7 @@ File::File (FileSystemClass &fs) { _fs = &fs; _hdl = NULL; - _name = NULL; + _path = NULL; _is_dir = false; } @@ -52,7 +52,7 @@ File::File (char const *filename, uint8_t mode, FileSystemClass &fs) { _fs = &fs; _hdl = NULL; - _name = NULL; + _path = NULL; _is_dir = false; open(filename, mode); @@ -143,10 +143,10 @@ void File::close (void) rtos_free(_hdl); } - if ( _name ) rtos_free(_name); + if ( _path ) rtos_free(_path); _hdl = NULL; - _name = NULL; + _path = NULL; } File::operator bool (void) @@ -154,9 +154,11 @@ File::operator bool (void) return _hdl != NULL; } -const char* File::name (void) +char const* File::name (void) { - return _name; + // return barename only + char* barename = strrchr(_path, '/'); + return barename ? (barename + 1) : _path; } bool File::isDirectory (void) @@ -167,7 +169,7 @@ bool File::isDirectory (void) File File::openNextFile (uint8_t mode) { if ( !_is_dir ) return File(*_fs); - return _fs->_f_openNextFile(_hdl, mode); + return _fs->_f_openNextFile(_hdl, _path, mode); } void File::rewindDirectory (void) diff --git a/libraries/FileSystem/src/FileIO.h b/libraries/FileSystem/src/FileIO.h index 3713671f..85e66607 100644 --- a/libraries/FileSystem/src/FileIO.h +++ b/libraries/FileSystem/src/FileIO.h @@ -72,7 +72,7 @@ class File: public Stream void close (void); operator bool (void); - const char* name (void); + char const* name (void); bool isDirectory (void); File openNextFile (uint8_t mode = FILE_READ); @@ -83,7 +83,7 @@ class File: public Stream private: FileSystemClass* _fs; void* _hdl; - char* _name; + char* _path; bool _is_dir; friend class ::LittleFS; @@ -124,7 +124,7 @@ class FileSystemClass virtual uint32_t _f_position (void* fhdl) = 0; virtual uint32_t _f_size (void* fhdl) = 0; - virtual File _f_openNextFile (void* fhdl, uint8_t mode) = 0; + virtual File _f_openNextFile (void* fhdl, char const* cwd, uint8_t mode) = 0; virtual void _f_rewindDirectory (void* fhdl) = 0; }; diff --git a/libraries/FileSystem/src/InternalFS.cpp b/libraries/FileSystem/src/InternalFS.cpp index cf1deea5..3cd07573 100644 --- a/libraries/FileSystem/src/InternalFS.cpp +++ b/libraries/FileSystem/src/InternalFS.cpp @@ -52,7 +52,7 @@ #define LFS_BLOCK_PER_PAGE (LFS_PAGE_SIZE/LFS_BLOCK_SIZE) //--------------------------------------------------------------------+ -// +// LFS Disk IO //--------------------------------------------------------------------+ #if !CFG_DEBUG @@ -164,7 +164,7 @@ int _iflash_sync(const struct lfs_config *c) } //--------------------------------------------------------------------+ -// +// Implementation //--------------------------------------------------------------------+ LittleFS InternalFS; @@ -204,7 +204,7 @@ bool LittleFS::begin (void) return true; } -BluefuritLib::File LittleFS::_open_file (char const *filename, uint8_t mode) +BluefuritLib::File LittleFS::_open_file (char const *filepath, uint8_t mode) { BluefuritLib::File file(*this); @@ -215,11 +215,12 @@ BluefuritLib::File LittleFS::_open_file (char const *filename, uint8_t mode) if ( flags ) { lfs_file_t* fhdl = (lfs_file_t*) rtos_malloc(sizeof(lfs_file_t)); - int rc = lfs_file_open(&_lfs, fhdl, filename, flags); + int rc = lfs_file_open(&_lfs, fhdl, filepath, flags); if ( rc ) { rtos_free(fhdl); + PRINT_STR(filepath); VERIFY_MESS(rc, dbg_strerr_lfs); } else @@ -227,20 +228,20 @@ BluefuritLib::File LittleFS::_open_file (char const *filename, uint8_t mode) file._hdl = fhdl; file._is_dir = false; - file._name = (char*) rtos_malloc(strlen(filename) + 1); - strcpy(file._name, filename); + file._path = (char*) rtos_malloc(strlen(filepath) + 1); + strcpy(file._path, filepath); } } return file; } -BluefuritLib::File LittleFS::_open_dir (char const *filename) +BluefuritLib::File LittleFS::_open_dir (char const *filepath) { BluefuritLib::File file(*this); lfs_dir_t* fhdl = (lfs_dir_t*) rtos_malloc(sizeof(lfs_dir_t)); - int rc = lfs_dir_open(&_lfs, fhdl, filename); + int rc = lfs_dir_open(&_lfs, fhdl, filepath); if ( rc ) { @@ -252,28 +253,28 @@ BluefuritLib::File LittleFS::_open_dir (char const *filename) file._hdl = fhdl; file._is_dir = true; - file._name = (char*) rtos_malloc(strlen(filename) + 1); - strcpy(file._name, filename); + file._path = (char*) rtos_malloc(strlen(filepath) + 1); + strcpy(file._path, filepath); } return file; } -BluefuritLib::File LittleFS::open (char const *filename, uint8_t mode) +BluefuritLib::File LittleFS::open (char const *filepath, uint8_t mode) { BluefuritLib::File file(*this); struct lfs_info info; - int rc = lfs_stat(&_lfs, filename, &info); + int rc = lfs_stat(&_lfs, filepath, &info); if ( LFS_ERR_OK == rc ) { // file existed, open file or directory accordingly - file = (info.type == LFS_TYPE_REG) ? _open_file(filename, mode) : _open_dir(filename); + file = (info.type == LFS_TYPE_REG) ? _open_file(filepath, mode) : _open_dir(filepath); } else if ( LFS_ERR_NOENT == rc ) { // file not existed, only proceed with FILE_WRITE mode - if ( mode == FILE_WRITE ) file = _open_file(filename, mode); + if ( mode == FILE_WRITE ) file = _open_file(filepath, mode); } else { @@ -354,7 +355,7 @@ void LittleFS::_f_close (void* fhdl, bool is_dir) } } -File LittleFS::_f_openNextFile (void* fhdl, uint8_t mode) +File LittleFS::_f_openNextFile (void* fhdl, char const* cwd, uint8_t mode) { BluefuritLib::File file(*this); struct lfs_info info; @@ -364,7 +365,13 @@ File LittleFS::_f_openNextFile (void* fhdl, uint8_t mode) if ( rc == 1 ) { - file = (info.type == LFS_TYPE_REG) ? _open_file(info.name, mode) : _open_dir(info.name); + // string cat name with current folder + char filepath[strlen(cwd) + 1 + strlen(info.name) + 1]; + strcpy(filepath, cwd); + strcat(filepath, "/"); + strcat(filepath, info.name); + + file = (info.type == LFS_TYPE_REG) ? _open_file(filepath, mode) : _open_dir(filepath); } else if ( rc < 0 ) { diff --git a/libraries/FileSystem/src/InternalFS.h b/libraries/FileSystem/src/InternalFS.h index 6e979192..9ae527c4 100644 --- a/libraries/FileSystem/src/InternalFS.h +++ b/libraries/FileSystem/src/InternalFS.h @@ -50,7 +50,7 @@ class LittleFS: public BluefuritLib::FileSystemClass virtual ~LittleFS (); bool begin (void); - BluefuritLib::File open (char const *filename, uint8_t mode = FILE_READ); + BluefuritLib::File open (char const *filepath, uint8_t mode = FILE_READ); bool exists (char const *filepath); bool mkdir (char const *filepath); bool remove (char const *filepath); @@ -64,12 +64,12 @@ class LittleFS: public BluefuritLib::FileSystemClass virtual uint32_t _f_position (void* fhdl); virtual uint32_t _f_size (void* fhdl); - virtual File _f_openNextFile (void* fhdl, uint8_t mode); + virtual File _f_openNextFile (void* fhdl, char const* cwd, uint8_t mode); virtual void _f_rewindDirectory (void* fhdl); private: - BluefuritLib::File _open_file (char const *filename, uint8_t mode); - BluefuritLib::File _open_dir (char const *filename); + BluefuritLib::File _open_file (char const *filepath, uint8_t mode); + BluefuritLib::File _open_dir (char const *filepath); struct lfs_config _lfs_cfg; lfs_t _lfs;