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.
pull/5503/head
Colin Hogben 2017-11-14 21:23:02 +00:00
parent 41eb565d9c
commit 452e290821
1 changed files with 19 additions and 2 deletions

View File

@ -660,7 +660,24 @@ void FATFileSystem::dir_seek(fs_dir_t dir, off_t offset) {
FATFS_DIR *dh = static_cast<FATFS_DIR*>(dir);
lock();
dh->index = offset;
if (offset < dh->index) {
f_rewinddir(dh);
}
while (dh->index < offset) {
FILINFO finfo;
FRESULT res;
#if _USE_LFN
char lfname[NAME_MAX];
finfo.lfname = lfname;
finfo.lfsize = NAME_MAX;
#endif // _USE_LFN
res = f_readdir(dh, &finfo);
if (res != FR_OK) {
break;
} else if (finfo.fname[0] == 0) {
break;
}
}
unlock();
}
@ -678,7 +695,7 @@ void FATFileSystem::dir_rewind(fs_dir_t dir) {
FATFS_DIR *dh = static_cast<FATFS_DIR*>(dir);
lock();
dh->index = 0;
f_rewinddir(dh);
unlock();
}