Skip to content

Commit c4649af

Browse files
committed
Filesystem: Last minute changes due to feedback on directory iteration
- Changed to use dirent structure type - Fixed memory leak in closedir
1 parent 7ca4eab commit c4649af

File tree

7 files changed

+28
-72
lines changed

7 files changed

+28
-72
lines changed

features/filesystem/Dir.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,11 @@ int Dir::close()
5858
return err;
5959
}
6060

61-
ssize_t Dir::read(char *path, size_t len)
61+
ssize_t Dir::read(struct dirent *ent)
6262
{
6363
MBED_ASSERT(_fs);
64-
return _fs->dir_read(_dir, path, len);
65-
}
66-
67-
ssize_t Dir::read(char *path, size_t len, uint8_t *type)
68-
{
69-
MBED_ASSERT(_fs);
70-
return _fs->dir_read(_dir, path, len, type);
64+
memset(ent, 0, sizeof(struct dirent));
65+
return _fs->dir_read(_dir, ent);
7166
}
7267

7368
void Dir::seek(off_t offset)

features/filesystem/Dir.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,10 @@ class Dir {
6464
/** Read the next directory entry
6565
*
6666
* @param path The buffer to read the null terminated path name in to
67-
* @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX
67+
* @param ent The directory entry to fill out
6868
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
6969
*/
70-
virtual ssize_t read(char *path, size_t len);
71-
72-
/** Read the next directory entry
73-
*
74-
* @param dir Dir handle
75-
* @param path The buffer to read the null terminated path name in to
76-
* @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX
77-
* @param type The type of the file, one of DT_DIR, DT_REG, etc...
78-
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
79-
*/
80-
virtual ssize_t read(char *path, size_t len, uint8_t *type);
70+
virtual ssize_t read(struct dirent *ent);
8171

8272
/** Set the current position of the directory
8373
*

features/filesystem/FileSystem.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,11 @@ int FileSystem::dir_close(fs_dir_t dir)
6868
return -ENOSYS;
6969
}
7070

71-
ssize_t FileSystem::dir_read(fs_dir_t dir, char *path, size_t len)
71+
ssize_t FileSystem::dir_read(fs_dir_t dir, struct dirent *ent)
7272
{
7373
return -ENOSYS;
7474
}
7575

76-
ssize_t FileSystem::dir_read(fs_dir_t dir, char *path, size_t len, uint8_t *type)
77-
{
78-
*type = DT_UNKNOWN;
79-
return dir_read(dir, path, len);
80-
}
81-
8276
void FileSystem::dir_seek(fs_dir_t dir, off_t offset)
8377
{
8478
}
@@ -98,10 +92,11 @@ size_t FileSystem::dir_size(fs_dir_t dir)
9892
{
9993
off_t off = dir_tell(dir);
10094
size_t size = 0;
95+
struct dirent *ent = new struct dirent;
10196

10297
dir_rewind(dir);
10398
while (true) {
104-
int res = dir_read(dir, NULL, 0);
99+
int res = dir_read(dir, ent);
105100
if (res <= 0) {
106101
break;
107102
}
@@ -110,6 +105,7 @@ size_t FileSystem::dir_size(fs_dir_t dir)
110105
}
111106
dir_seek(dir, off);
112107

108+
delete ent;
113109
return size;
114110
}
115111

features/filesystem/FileSystem.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,10 @@ class FileSystem : public FileBase {
195195
/** Read the next directory entry
196196
*
197197
* @param dir Dir handle
198-
* @param path The buffer to read the null terminated path name in to
199-
* @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX
198+
* @param ent The directory entry to fill out
200199
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
201200
*/
202-
virtual ssize_t dir_read(fs_dir_t dir, char *path, size_t len);
203-
204-
/** Read the next directory entry
205-
*
206-
* @param dir Dir handle
207-
* @param path The buffer to read the null terminated path name in to
208-
* @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX
209-
* @param type The type of the file, one of DT_DIR, DT_REG, etc...
210-
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
211-
*/
212-
virtual ssize_t dir_read(fs_dir_t dir, char *path, size_t len, uint8_t *type);
201+
virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent);
213202

214203
/** Set the current position of the directory
215204
*

features/filesystem/fat/FATFileSystem.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -545,17 +545,13 @@ int FATFileSystem::dir_close(fs_dir_t dir) {
545545
return fat_error_remap(res);
546546
}
547547

548-
ssize_t FATFileSystem::dir_read(fs_dir_t dir, char *path, size_t len) {
549-
return dir_read(dir, path, len, NULL);
550-
}
551-
552-
ssize_t FATFileSystem::dir_read(fs_dir_t dir, char *path, size_t len, uint8_t *type) {
548+
ssize_t FATFileSystem::dir_read(fs_dir_t dir, struct dirent *ent) {
553549
FATFS_DIR *dh = static_cast<FATFS_DIR*>(dir);
554550
FILINFO finfo;
555551

556552
#if _USE_LFN
557-
finfo.lfname = path;
558-
finfo.lfsize = len;
553+
finfo.lfname = ent->d_name;
554+
finfo.lfsize = NAME_MAX;
559555
#endif // _USE_LFN
560556

561557
lock();
@@ -565,23 +561,21 @@ ssize_t FATFileSystem::dir_read(fs_dir_t dir, char *path, size_t len, uint8_t *t
565561
if (res != FR_OK) {
566562
return fat_error_remap(res);
567563
} else if (finfo.fname[0] == 0) {
568-
return -EINVAL;
564+
return 0;
569565
}
570566

571-
if (type) {
572-
*type = (finfo.fattrib & AM_DIR) ? DT_DIR : DT_REG;
573-
}
567+
ent->d_type = (finfo.fattrib & AM_DIR) ? DT_DIR : DT_REG;
574568

575569
#if _USE_LFN
576-
if (path[0] == 0) {
570+
if (ent->d_name[0] == 0) {
577571
// No long filename so use short filename.
578-
strncpy(path, finfo.fname, len);
572+
strncpy(ent->d_name, finfo.fname, NAME_MAX);
579573
}
580574
#else
581-
strncpy(path, finfo.fname, len);
575+
strncpy(end->d_name, finfo.fname, len);
582576
#endif
583577

584-
return 0;
578+
return 1;
585579
}
586580

587581
void FATFileSystem::dir_seek(fs_dir_t dir, off_t offset) {

features/filesystem/fat/FATFileSystem.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,10 @@ class FATFileSystem : public FileSystem {
199199
/** Read the next directory entry
200200
*
201201
* @param dir Dir handle
202-
* @param path The buffer to read the null terminated path name in to
203-
* @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX
202+
* @param ent The directory entry to fill out
204203
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
205204
*/
206-
virtual ssize_t dir_read(fs_dir_t dir, char *path, size_t len);
207-
208-
/** Read the next directory entry
209-
*
210-
* @param dir Dir handle
211-
* @param path The buffer to read the null terminated path name in to
212-
* @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX
213-
* @param type The type of the file, one of DT_DIR, DT_REG, etc...
214-
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
215-
*/
216-
virtual ssize_t dir_read(fs_dir_t dir, char *path, size_t len, uint8_t *type);
205+
virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent);
217206

218207
/** Set the current position of the directory
219208
*

platform/mbed_retarget.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,11 @@ extern "C" DIR *opendir(const char *path) {
554554
extern "C" struct dirent *readdir(DIR *dir) {
555555
#if MBED_CONF_FILESYSTEM_PRESENT
556556
static struct dirent ent;
557-
int err = dir->read(ent.d_name, NAME_MAX, &ent.d_type);
558-
if (err < 0) {
559-
errno = -err;
557+
int err = dir->read(&ent);
558+
if (err < 1) {
559+
if (err < 0) {
560+
errno = -err;
561+
}
560562
return NULL;
561563
}
562564

@@ -570,6 +572,7 @@ extern "C" struct dirent *readdir(DIR *dir) {
570572
extern "C" int closedir(DIR *dir) {
571573
#if MBED_CONF_FILESYSTEM_PRESENT
572574
int err = dir->close();
575+
delete dir;
573576
if (err < 0) {
574577
errno = -err;
575578
return -1;

0 commit comments

Comments
 (0)