Process child directories first in Filesystem->readDirectory call

This commit is contained in:
Onha Choe 2017-04-12 12:48:59 -07:00 committed by onhachoe
parent 0ecebca6f4
commit 37fa106537
4 changed files with 11 additions and 24 deletions

View File

@ -32,8 +32,8 @@ TEST(DirectoryDependencyInfo, Deserialize)
EXPECT_EQ(info->dependencyInfo().inputs(), std::vector<std::string>({ EXPECT_EQ(info->dependencyInfo().inputs(), std::vector<std::string>({
"/root/file1", "/root/file1",
"/root/file2", "/root/file2",
"/root/dir",
"/root/dir/file3", "/root/dir/file3",
"/root/dir",
})); }));
EXPECT_TRUE(info->dependencyInfo().outputs().empty()); EXPECT_TRUE(info->dependencyInfo().outputs().empty());
} }

View File

@ -494,28 +494,19 @@ readDirectory(std::string const &path, bool recursive, std::function<void(std::s
} }
std::string path = (relative ? *relative + "/" + entry->d_name : entry->d_name); std::string path = (relative ? *relative + "/" + entry->d_name : entry->d_name);
cb(path);
}
/* Process subdirectories. */
if (recursive) {
::rewinddir(dp);
while (struct dirent *entry = ::readdir(dp)) {
if (::strcmp(entry->d_name, ".") == 0 || ::strcmp(entry->d_name, "..") == 0) {
continue;
}
/* Process subdirectories first. */
if (recursive) {
std::string full = absolute + "/" + entry->d_name; std::string full = absolute + "/" + entry->d_name;
if (this->type(full) == Type::Directory) { if (this->type(full) == Type::Directory) {
std::string path = (relative ? *relative + "/" + entry->d_name : entry->d_name);
if (!process(full, path)) { if (!process(full, path)) {
::closedir(dp); ::closedir(dp);
return false; return false;
} }
} }
} }
cb(path);
} }
::closedir(dp); ::closedir(dp);

View File

@ -385,17 +385,13 @@ readDirectory(std::string const &path, bool recursive, std::function<void(std::s
/* Report children. */ /* Report children. */
for (MemoryFilesystem::Entry const &child : entry->children()) { for (MemoryFilesystem::Entry const &child : entry->children()) {
std::string path = (subpath ? *subpath + "/" + child.name() : child.name()); std::string path = (subpath ? *subpath + "/" + child.name() : child.name());
cb(path);
}
/* Process subdirectories. */ /* Process subdirectories first. */
if (recursive) { if (recursive) {
for (MemoryFilesystem::Entry const &child : entry->children()) { process(path, &child);
if (child.type() == Type::Directory) {
std::string path = (subpath ? *subpath + "/" + child.name() : child.name());
process(path, &child);
}
} }
cb(path);
} }
}; };

View File

@ -302,7 +302,7 @@ TEST(MemoryFilesystem, ReadDirectory)
/* List root contents, recursive. */ /* List root contents, recursive. */
files.clear(); files.clear();
EXPECT_TRUE(filesystem.readDirectory("/", true, accumulate)); EXPECT_TRUE(filesystem.readDirectory("/", true, accumulate));
EXPECT_EQ(files, std::vector<std::string>({ "file1", "dir1", "dir2", "dir1/file2", "dir2/file2", "dir2/dir3" })); EXPECT_EQ(files, std::vector<std::string>({ "file1", "dir1/file2", "dir1", "dir2/file2", "dir2/dir3", "dir2" }));
/* List file. */ /* List file. */
files.clear(); files.clear();