From 8e55ba6929d1365e3f3f9441adcc94c92bc35330 Mon Sep 17 00:00:00 2001 From: Emin Martinian Date: Fri, 31 Jul 2020 23:22:19 -0400 Subject: Make it so that the include directive sorts when using wildcards. Before this commit, doing something like 'include data/*.dat' would produce undesired behaviour because the matches for 'data/*.dat' would not be sorted correctly. See https://github.com/ledger/ledger/issues/1659 for details. --- src/textual.cc | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/textual.cc b/src/textual.cc index c3bb2c2f..a629048f 100644 --- a/src/textual.cc +++ b/src/textual.cc @@ -40,6 +40,7 @@ #include "query.h" #include "pstream.h" #include "pool.h" +#include #if HAVE_BOOST_PYTHON #include "pyinterp.h" #endif @@ -751,12 +752,19 @@ void instance_t::include_directive(char * line) bool files_found = false; if (exists(parent_path)) { filesystem::directory_iterator end; - for (filesystem::directory_iterator iter(parent_path); - iter != end; - ++iter) { + + // Sort parent_path since on some file systems it is unsorted. + std::vector sorted_parent_path; + std::copy(filesystem::directory_iterator(parent_path), + filesystem::directory_iterator(), + std::back_inserter(sorted_parent_path)); + std::sort(sorted_parent_path.begin(), sorted_parent_path.end()); + + for (std::vector::const_iterator iter(sorted_parent_path.begin()), + it_end(sorted_parent_path.end()); iter != it_end; ++iter) { if (is_regular_file(*iter)) { - string base = (*iter).path().filename().string(); + string base = (*iter).filename().string(); if (glob.match(base)) { journal_t * journal = context.journal; account_t * master = top_account(); -- cgit v1.2.3