diff --git a/features/filesystem/Dir.cpp b/features/filesystem/Dir.cpp index 4714e9d07e..867105b96a 100644 --- a/features/filesystem/Dir.cpp +++ b/features/filesystem/Dir.cpp @@ -58,16 +58,11 @@ int Dir::close() return err; } -ssize_t Dir::read(char *path, size_t len) +ssize_t Dir::read(struct dirent *ent) { MBED_ASSERT(_fs); - return _fs->dir_read(_dir, path, len); -} - -ssize_t Dir::read(char *path, size_t len, uint8_t *type) -{ - MBED_ASSERT(_fs); - return _fs->dir_read(_dir, path, len, type); + memset(ent, 0, sizeof(struct dirent)); + return _fs->dir_read(_dir, ent); } void Dir::seek(off_t offset) diff --git a/features/filesystem/Dir.h b/features/filesystem/Dir.h index 4d7a465d75..b4dc4133d1 100644 --- a/features/filesystem/Dir.h +++ b/features/filesystem/Dir.h @@ -64,20 +64,10 @@ public: /** Read the next directory entry * * @param path The buffer to read the null terminated path name in to - * @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX + * @param ent The directory entry to fill out * @return 1 on reading a filename, 0 at end of directory, negative error on failure */ - virtual ssize_t read(char *path, size_t len); - - /** Read the next directory entry - * - * @param dir Dir handle - * @param path The buffer to read the null terminated path name in to - * @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX - * @param type The type of the file, one of DT_DIR, DT_REG, etc... - * @return 1 on reading a filename, 0 at end of directory, negative error on failure - */ - virtual ssize_t read(char *path, size_t len, uint8_t *type); + virtual ssize_t read(struct dirent *ent); /** Set the current position of the directory * diff --git a/features/filesystem/FileSystem.cpp b/features/filesystem/FileSystem.cpp index 47747a7545..0a3d2bc615 100644 --- a/features/filesystem/FileSystem.cpp +++ b/features/filesystem/FileSystem.cpp @@ -68,17 +68,11 @@ int FileSystem::dir_close(fs_dir_t dir) return -ENOSYS; } -ssize_t FileSystem::dir_read(fs_dir_t dir, char *path, size_t len) +ssize_t FileSystem::dir_read(fs_dir_t dir, struct dirent *ent) { return -ENOSYS; } -ssize_t FileSystem::dir_read(fs_dir_t dir, char *path, size_t len, uint8_t *type) -{ - *type = DT_UNKNOWN; - return dir_read(dir, path, len); -} - void FileSystem::dir_seek(fs_dir_t dir, off_t offset) { } @@ -98,10 +92,11 @@ size_t FileSystem::dir_size(fs_dir_t dir) { off_t off = dir_tell(dir); size_t size = 0; + struct dirent *ent = new struct dirent; dir_rewind(dir); while (true) { - int res = dir_read(dir, NULL, 0); + int res = dir_read(dir, ent); if (res <= 0) { break; } @@ -110,6 +105,7 @@ size_t FileSystem::dir_size(fs_dir_t dir) } dir_seek(dir, off); + delete ent; return size; } diff --git a/features/filesystem/FileSystem.h b/features/filesystem/FileSystem.h index e783e2b4ac..cfc84225d1 100644 --- a/features/filesystem/FileSystem.h +++ b/features/filesystem/FileSystem.h @@ -195,21 +195,10 @@ protected: /** Read the next directory entry * * @param dir Dir handle - * @param path The buffer to read the null terminated path name in to - * @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX + * @param ent The directory entry to fill out * @return 1 on reading a filename, 0 at end of directory, negative error on failure */ - virtual ssize_t dir_read(fs_dir_t dir, char *path, size_t len); - - /** Read the next directory entry - * - * @param dir Dir handle - * @param path The buffer to read the null terminated path name in to - * @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX - * @param type The type of the file, one of DT_DIR, DT_REG, etc... - * @return 1 on reading a filename, 0 at end of directory, negative error on failure - */ - virtual ssize_t dir_read(fs_dir_t dir, char *path, size_t len, uint8_t *type); + virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent); /** Set the current position of the directory * diff --git a/features/filesystem/fat/FATFileSystem.cpp b/features/filesystem/fat/FATFileSystem.cpp index 1b3f0aa6de..ebecdaad34 100644 --- a/features/filesystem/fat/FATFileSystem.cpp +++ b/features/filesystem/fat/FATFileSystem.cpp @@ -545,17 +545,13 @@ int FATFileSystem::dir_close(fs_dir_t dir) { return fat_error_remap(res); } -ssize_t FATFileSystem::dir_read(fs_dir_t dir, char *path, size_t len) { - return dir_read(dir, path, len, NULL); -} - -ssize_t FATFileSystem::dir_read(fs_dir_t dir, char *path, size_t len, uint8_t *type) { +ssize_t FATFileSystem::dir_read(fs_dir_t dir, struct dirent *ent) { FATFS_DIR *dh = static_cast(dir); FILINFO finfo; #if _USE_LFN - finfo.lfname = path; - finfo.lfsize = len; + finfo.lfname = ent->d_name; + finfo.lfsize = NAME_MAX; #endif // _USE_LFN lock(); @@ -565,23 +561,21 @@ ssize_t FATFileSystem::dir_read(fs_dir_t dir, char *path, size_t len, uint8_t *t if (res != FR_OK) { return fat_error_remap(res); } else if (finfo.fname[0] == 0) { - return -EINVAL; + return 0; } - if (type) { - *type = (finfo.fattrib & AM_DIR) ? DT_DIR : DT_REG; - } + ent->d_type = (finfo.fattrib & AM_DIR) ? DT_DIR : DT_REG; #if _USE_LFN - if (path[0] == 0) { + if (ent->d_name[0] == 0) { // No long filename so use short filename. - strncpy(path, finfo.fname, len); + strncpy(ent->d_name, finfo.fname, NAME_MAX); } #else - strncpy(path, finfo.fname, len); + strncpy(end->d_name, finfo.fname, len); #endif - return 0; + return 1; } void FATFileSystem::dir_seek(fs_dir_t dir, off_t offset) { diff --git a/features/filesystem/fat/FATFileSystem.h b/features/filesystem/fat/FATFileSystem.h index 6fcc6300bf..1b39a795ce 100644 --- a/features/filesystem/fat/FATFileSystem.h +++ b/features/filesystem/fat/FATFileSystem.h @@ -199,21 +199,10 @@ protected: /** Read the next directory entry * * @param dir Dir handle - * @param path The buffer to read the null terminated path name in to - * @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX + * @param ent The directory entry to fill out * @return 1 on reading a filename, 0 at end of directory, negative error on failure */ - virtual ssize_t dir_read(fs_dir_t dir, char *path, size_t len); - - /** Read the next directory entry - * - * @param dir Dir handle - * @param path The buffer to read the null terminated path name in to - * @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX - * @param type The type of the file, one of DT_DIR, DT_REG, etc... - * @return 1 on reading a filename, 0 at end of directory, negative error on failure - */ - virtual ssize_t dir_read(fs_dir_t dir, char *path, size_t len, uint8_t *type); + virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent); /** Set the current position of the directory * diff --git a/platform/mbed_retarget.cpp b/platform/mbed_retarget.cpp index cabb40867e..a38544147b 100644 --- a/platform/mbed_retarget.cpp +++ b/platform/mbed_retarget.cpp @@ -554,9 +554,11 @@ extern "C" DIR *opendir(const char *path) { extern "C" struct dirent *readdir(DIR *dir) { #if MBED_CONF_FILESYSTEM_PRESENT static struct dirent ent; - int err = dir->read(ent.d_name, NAME_MAX, &ent.d_type); - if (err < 0) { - errno = -err; + int err = dir->read(&ent); + if (err < 1) { + if (err < 0) { + errno = -err; + } return NULL; } @@ -570,6 +572,7 @@ extern "C" struct dirent *readdir(DIR *dir) { extern "C" int closedir(DIR *dir) { #if MBED_CONF_FILESYSTEM_PRESENT int err = dir->close(); + delete dir; if (err < 0) { errno = -err; return -1;