Skip to content

Implements seekDir and getNextFileName on FS Lib to improve performance #7229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions libraries/FS/src/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,22 @@ File File::openNextFile(const char* mode)
return _p->openNextFile(mode);
}

boolean File::seekDir(long position){
if(!_p){
return false;
}
return _p->seekDir(position);
}

String File::getNextFileName(void)
{
if (!_p) {
return "";
}
return _p->getNextFileName();

}

void File::rewindDirectory(void)
{
if (!*this) {
Expand Down
2 changes: 2 additions & 0 deletions libraries/FS/src/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ class File : public Stream
const char* name() const;

boolean isDirectory(void);
boolean seekDir(long position);
File openNextFile(const char* mode = FILE_READ);
String getNextFileName(void);
void rewindDirectory(void);

protected:
Expand Down
2 changes: 2 additions & 0 deletions libraries/FS/src/FSImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class FileImpl
virtual const char* name() const = 0;
virtual boolean isDirectory(void) = 0;
virtual FileImplPtr openNextFile(const char* mode) = 0;
virtual boolean seekDir(long position);
virtual String getNextFileName(void);
virtual void rewindDirectory(void) = 0;
virtual operator bool() = 0;
};
Expand Down
30 changes: 30 additions & 0 deletions libraries/FS/src/vfs_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,36 @@ FileImplPtr VFSFileImpl::openNextFile(const char* mode)
return std::make_shared<VFSFileImpl>(_fs, name.c_str(), mode);
}

boolean VFSFileImpl::seekDir(long position){
if(!_d){
return false;
}
seekdir(_d, position);
return true;
}


String VFSFileImpl::getNextFileName()
{
if (!_isDirectory || !_d) {
return "";
}
struct dirent *file = readdir(_d);
if (file == NULL) {
return "";
}
if (file->d_type != DT_REG && file->d_type != DT_DIR) {
return "";
}
Comment on lines +497 to +499
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behaviour is inconsistent with openNextFile() which skips to the next file.

It's not possible to tell the difference between "end of file list" and "something that is not a file or directory".

String fname = String(file->d_name);
String name = String(_path);
if (!fname.startsWith("/") && !name.endsWith("/")) {
name += "/";
}
name += fname;
Comment on lines +500 to +505
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the "path", not the "name" of the file.

This results in more effort by users of this function to remove the directory name (which they already have) from every filename.

(The openNextFile() function returns a File so it's possible to use name() or path())

return name;
}

void VFSFileImpl::rewindDirectory(void)
{
if(!_isDirectory || !_d) {
Expand Down
2 changes: 2 additions & 0 deletions libraries/FS/src/vfs_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class VFSFileImpl : public FileImpl
const char* name() const override;
time_t getLastWrite() override;
boolean isDirectory(void) override;
boolean seekDir(long position) override;
String getNextFileName(void) override;
FileImplPtr openNextFile(const char* mode) override;
void rewindDirectory(void) override;
operator bool();
Expand Down