diff --git a/include/system.hpp b/include/system.hpp index c482f0b2..77a38cbc 100644 --- a/include/system.hpp +++ b/include/system.hpp @@ -13,6 +13,7 @@ namespace system { /** Returns a list of all entries (directories, files, symbols) in a directory. */ std::list getEntries(const std::string& path); +std::list getEntriesRecursive(const std::string &path, int depth); /** Returns whether the given path is a file. */ bool isFile(const std::string& path); /** Returns whether the given path is a directory. */ diff --git a/src/system.cpp b/src/system.cpp index f0d78093..7ff0d323 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -53,6 +53,25 @@ std::list getEntries(const std::string& path) { } +std::list getEntriesRecursive(const std::string &path, int depth) { + std::list entries = getEntries(path); + if (depth > 0) { + // Don't iterate using iterators because the list will be growing. + size_t limit = entries.size(); + auto it = entries.begin(); + for (size_t i = 0; i < limit; i++) { + const std::string &entry = *it++; + if (isDirectory(entry)) { + std::list subEntries = getEntriesRecursive(entry, depth - 1); + // Append subEntries to entries + entries.splice(entries.end(), subEntries); + } + } + } + return entries; +} + + bool isFile(const std::string& path) { struct stat statbuf; if (stat(path.c_str(), &statbuf))