Commit 7263d31f authored by Kenton Varda's avatar Kenton Varda Committed by GitHub

Merge pull request #473 from sandstorm-io/sort-files-by-name

kj::Directory::list*() should return sorted lists to avoid non-determinism.
parents 7185b54a b8d0765f
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "vector.h" #include "vector.h"
#include "miniposix.h" #include "miniposix.h"
#include <algorithm>
#if __linux__ #if __linux__
#include <linux/fs.h> #include <linux/fs.h>
...@@ -599,7 +600,8 @@ public: ...@@ -599,7 +600,8 @@ public:
} }
KJ_DEFER(closedir(dir)); KJ_DEFER(closedir(dir));
kj::Vector<Decay<decltype(func(instance<StringPtr>(), instance<FsNode::Type>()))>> entries; typedef Decay<decltype(func(instance<StringPtr>(), instance<FsNode::Type>()))> Entry;
kj::Vector<Entry> entries;
for (;;) { for (;;) {
errno = 0; errno = 0;
...@@ -634,7 +636,9 @@ public: ...@@ -634,7 +636,9 @@ public:
} }
} }
return entries.releaseAsArray(); auto result = entries.releaseAsArray();
std::sort(result.begin(), result.end());
return result;
} }
Array<String> listNames() { Array<String> listNames() {
......
...@@ -1287,7 +1287,11 @@ private: ...@@ -1287,7 +1287,11 @@ private:
}; };
Clock& clock; Clock& clock;
std::map<StringPtr, EntryImpl> entries; std::map<StringPtr, EntryImpl> entries;
// Note: If this changes to a non-sorted map, listNames() and listEntries() must be updated to
// sort their results.
Date lastModified; Date lastModified;
template <typename T> template <typename T>
......
...@@ -426,6 +426,12 @@ public: ...@@ -426,6 +426,12 @@ public:
struct Entry { struct Entry {
FsNode::Type type; FsNode::Type type;
String name; String name;
inline bool operator< (const Entry& other) const { return name < other.name; }
inline bool operator> (const Entry& other) const { return name > other.name; }
inline bool operator<=(const Entry& other) const { return name <= other.name; }
inline bool operator>=(const Entry& other) const { return name >= other.name; }
// Convenience comparison operators to sort entries by name.
}; };
virtual Array<Entry> listEntries() = 0; virtual Array<Entry> listEntries() = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment