Skip to content

Commit 452e290

Browse files
author
Colin Hogben
committed
FATFileSystem: provide working dir_rewind and dir_seek
The index field of FATFS_DIR does not encapsulate all the context required to reposition the directory traversal. ChaN provides f_rewinddir() but no directory seek, so rewind if necessary then step through until the desired index is reached.
1 parent 41eb565 commit 452e290

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

features/filesystem/fat/FATFileSystem.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,24 @@ void FATFileSystem::dir_seek(fs_dir_t dir, off_t offset) {
660660
FATFS_DIR *dh = static_cast<FATFS_DIR*>(dir);
661661

662662
lock();
663-
dh->index = offset;
663+
if (offset < dh->index) {
664+
f_rewinddir(dh);
665+
}
666+
while (dh->index < offset) {
667+
FILINFO finfo;
668+
FRESULT res;
669+
#if _USE_LFN
670+
char lfname[NAME_MAX];
671+
finfo.lfname = lfname;
672+
finfo.lfsize = NAME_MAX;
673+
#endif // _USE_LFN
674+
res = f_readdir(dh, &finfo);
675+
if (res != FR_OK) {
676+
break;
677+
} else if (finfo.fname[0] == 0) {
678+
break;
679+
}
680+
}
664681
unlock();
665682
}
666683

@@ -678,7 +695,7 @@ void FATFileSystem::dir_rewind(fs_dir_t dir) {
678695
FATFS_DIR *dh = static_cast<FATFS_DIR*>(dir);
679696

680697
lock();
681-
dh->index = 0;
698+
f_rewinddir(dh);
682699
unlock();
683700
}
684701

0 commit comments

Comments
 (0)