From 4dce23836ac967527794e4e418020147abfcaeac Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 29 Jul 2022 16:32:34 +0200 Subject: 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. --- src/file_util.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/file_util.cpp') 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& 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* 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 -- cgit v1.2.3