aboutsummaryrefslogtreecommitdiffstats
path: root/src/file_util.cpp
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2022-07-29 16:32:34 +0200
committerSven Gothel <[email protected]>2022-07-29 16:32:34 +0200
commit4dce23836ac967527794e4e418020147abfcaeac (patch)
tree6c64b148d8a249d5916aa5c0de5ee2d478277bd2 /src/file_util.cpp
parent5dec9ae8d9583920b6718ba3ee399769bb882f30 (diff)
Add traverse_options::lexicographical_order as required when computing an order dependent outcome like a hash value
Implementation only performs the default sort algo on the dir_item::basename() for each directory, when recursing through the directories.
Diffstat (limited to 'src/file_util.cpp')
-rw-r--r--src/file_util.cpp8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/file_util.cpp b/src/file_util.cpp
index f1aad97..b283cf3 100644
--- a/src/file_util.cpp
+++ b/src/file_util.cpp
@@ -929,6 +929,7 @@ std::string jau::fs::to_string(const traverse_event mask) noexcept {
#define TRAVERSEOPTIONS_ENUM(X,M) \
X(traverse_options,recursive,M) \
X(traverse_options,follow_symlinks,M) \
+ X(traverse_options,lexicographical_order,M) \
X(traverse_options,dir_entry,M) \
X(traverse_options,dir_exit,M)
@@ -940,6 +941,10 @@ std::string jau::fs::to_string(const traverse_options mask) noexcept {
return out;
}
+static bool _dir_item_basename_compare(const dir_item& a, const dir_item& b) {
+ return a.basename() < b.basename();
+}
+
static bool _visit(const file_stats& item_stats, const traverse_options topts, const path_visitor& visitor, std::vector<int>& dirfds) noexcept {
if( item_stats.is_dir() ) {
if( item_stats.is_link() && !is_set(topts, traverse_options::follow_symlinks) ) {
@@ -971,6 +976,9 @@ static bool _visit(const file_stats& item_stats, const traverse_options topts, c
( [](std::vector<dir_item>* receiver, const dir_item& item) -> void { receiver->push_back( item ); } )
);
if( get_dir_content(this_dirfd, item_stats.path(), cs) && content.size() > 0 ) {
+ if( is_set(topts, traverse_options::lexicographical_order) ) {
+ std::sort(content.begin(), content.end(), _dir_item_basename_compare);
+ }
for (const dir_item& element : content) {
const file_stats element_stats( this_dirfd, element, true /* dirfd_is_item_dirname */ );
if( element_stats.is_dir() ) { // an OK dir