Skip to content

Commit

Permalink
Common: fixed DirectoryRecursiveIterator
Browse files Browse the repository at this point in the history
The error was that when saving last subdirectory search position for the current level, it was saving a "find any entries" iterator instead, which is already past end at this point.
As a consequence, whenever returning from a subdir, it will look like all subdirs are already exhausted, and search is stopped.
Only first subdirectory and first subdirectories in it will be searched.
  • Loading branch information
ivan-mogilko committed Aug 24, 2024
1 parent ae6cd86 commit 1642521
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
12 changes: 6 additions & 6 deletions Common/util/directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ DirectoryRecursiveIterator DirectoryRecursiveIterator::Open(const String &path,

void DirectoryRecursiveIterator::Close()
{
while (!_dirStack.empty()) _dirStack.pop();
while (!_subdirStack.empty()) _subdirStack.pop();
_dir.Close();
_subSearch.Close();
_fullDir = "";
Expand Down Expand Up @@ -404,7 +404,7 @@ bool DirectoryRecursiveIterator::Next()

bool DirectoryRecursiveIterator::PushDir()
{
if (_dirStack.size() == _maxLevel)
if (_subdirStack.size() == _maxLevel)
return false; // no more nesting allowed

const FileEntry entry = _subSearch.GetEntry();
Expand All @@ -416,7 +416,7 @@ bool DirectoryRecursiveIterator::PushDir()
if (dir.AtEnd())
return false; // dir is empty, or error
DirectoryIterator dir_sub = DirectoryIterator::Open(path);
_dirStack.push(std::move(_dir)); // save previous dir iterator
_subdirStack.push(std::move(_subSearch)); // save previous subsearch iterator
_dir = std::move(dir);
_subSearch = std::move(dir_sub);
_fullDir = path;
Expand All @@ -426,11 +426,11 @@ bool DirectoryRecursiveIterator::PushDir()

bool DirectoryRecursiveIterator::PopDir()
{
if (_dirStack.empty())
if (_subdirStack.empty())
return false; // no more parent levels
// restore parent level
_subSearch = std::move(_dirStack.top());
_dirStack.pop();
_subSearch = std::move(_subdirStack.top());
_subdirStack.pop();
_fullDir = Path::GetParent(_fullDir);
_curDir = Path::GetParent(_curDir);
if (_curDir.Compare(".") == 0)
Expand Down
17 changes: 13 additions & 4 deletions Common/util/directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ struct FileEntry
//
// DirectoryIterator iterates entries in the directory.
// The order of iteration is undefined.
// These entries may be files or subdirectories, they are iterated through
// altogether. If you require only certain type, check current FileEntry's
// properties using GetEntry() method.
//
class DirectoryIterator
{
Expand Down Expand Up @@ -106,6 +109,9 @@ class DirectoryIterator
// and all the subdirectories. The order of iteration among subitems
// is undefined, but each subdirectory is passed in one go before
// going to others.
// These entries may be files or subdirectories, they are iterated through
// altogether. If you require only certain type, check current FileEntry's
// properties using GetEntry() method.
//
class DirectoryRecursiveIterator
{
Expand All @@ -130,10 +136,13 @@ class DirectoryRecursiveIterator
bool PushDir();
bool PopDir();

// A stack of directory iteration positions
std::stack<DirectoryIterator> _dirStack;
DirectoryIterator _dir; // current dir iterator
DirectoryIterator _subSearch; // current subdirectories searcher
DirectoryIterator _dir; // current dir iterator (used to iterate all entries)
DirectoryIterator _subSearch; // current subdirectories searcher (on this level)
// A stack of subdirectories iterators saved for upper nesting levels.
// Whenever we go one level down, we save current subSearch iterator;
// whenever we return one level up, we restore a saved subSearch iterator;
// this lets us continue iterating subdirs from the position we were at last time.
std::stack<DirectoryIterator> _subdirStack;
// max nesting level, SIZE_MAX for unrestricted
size_t _maxLevel = SIZE_MAX;
String _fullDir; // full directory path
Expand Down

0 comments on commit 1642521

Please sign in to comment.