aboutsummaryrefslogtreecommitdiff
path: root/libbutl
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2018-11-23 00:23:23 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2018-11-30 17:13:11 +0300
commitabfee51c362cb1ed2e8eb62fec12b3eb5ca03fb0 (patch)
tree3f08e73ecc447a9d05d340eeab169affb9e517b6 /libbutl
parentb55143ecaa986aa3ba93dc6a078ed7d9cf495b1c (diff)
Add match_absent flag for path_{search,match}() functions
Diffstat (limited to 'libbutl')
-rw-r--r--libbutl/filesystem.cxx54
-rw-r--r--libbutl/filesystem.ixx26
-rw-r--r--libbutl/filesystem.mxx61
3 files changed, 116 insertions, 25 deletions
diff --git a/libbutl/filesystem.cxx b/libbutl/filesystem.cxx
index e87669d..736426b 100644
--- a/libbutl/filesystem.cxx
+++ b/libbutl/filesystem.cxx
@@ -1594,10 +1594,13 @@ namespace butl
search (
path pattern,
dir_path pattern_dir,
- bool follow_symlinks,
+ path_match_flags fl,
const function<bool (path&&, const string& pattern, bool interm)>& func,
FS& filesystem)
{
+ bool follow_symlinks ((fl & path_match_flags::follow_symlinks) !=
+ path_match_flags::none);
+
// Fast-forward the leftmost pattern non-wildcard components. So, for
// example, search for foo/f* in /bar/ becomes search for f* in /bar/foo/.
//
@@ -1724,12 +1727,43 @@ namespace butl
//
if (!simple && !search (pattern.leaf (pc),
pattern_dir / path_cast<dir_path> (move (p)),
- follow_symlinks,
+ fl,
func,
filesystem))
return false;
}
+ // If requested, also search with the absent-matching pattern path
+ // component omitted, unless this is the only pattern component.
+ //
+ if ((fl & path_match_flags::match_absent) != path_match_flags::none &&
+ pc.string ().find_first_not_of ('*') == string::npos &&
+ (!pattern_dir.empty () || !simple))
+ {
+ // Stripping the (leading) absent-matching pattern component and calling
+ // search() with the resulting pattern and the same pattern dir works in
+ // most cases, except for a simple pattern. In the latter case, the
+ // pattern becomes empty and its type information gets lost. In other
+ // words, the patterns a/b/*/ and a/b/* become indistinguishable. Thus,
+ // for such a corner case we will strip the leaf from the pattern dir
+ // and use it as a pattern, stripping the trailing separator, if
+ // required. So for the above examples the search() calls will be as
+ // follows:
+ //
+ // search(b/, a/)
+ // search(b, a/)
+ //
+ const dir_path& d (!simple ? pattern_dir : pattern_dir.directory ());
+
+ const path& p (
+ !simple ? pattern.leaf (pc) :
+ pattern.to_directory () ? pattern_dir.leaf () :
+ path (pattern_dir.leaf ().string ())); // Strip the trailing separator.
+
+ if (!search (p, d, fl, func, filesystem))
+ return false;
+ }
+
return true;
}
@@ -1945,10 +1979,10 @@ namespace butl
const path& pattern,
const function<bool (path&&, const string& pattern, bool interm)>& func,
const dir_path& start,
- bool follow_symlinks)
+ path_match_flags flags)
{
real_filesystem fs (pattern.relative () ? start : empty_dir);
- search (pattern, dir_path (), follow_symlinks, func, fs);
+ search (pattern, dir_path (), flags, func, fs);
}
// Search path in the directory tree represented by a path.
@@ -2161,14 +2195,18 @@ namespace butl
const path& pattern,
const path& entry,
const function<bool (path&&, const string& pattern, bool interm)>& func,
- const dir_path& start)
+ const dir_path& start,
+ path_match_flags flags)
{
path_filesystem fs (start, entry);
- search (pattern, dir_path (), true, func, fs);
+ search (pattern, dir_path (), flags, func, fs);
}
bool
- path_match (const path& pattern, const path& entry, const dir_path& start)
+ path_match (const path& pattern,
+ const path& entry,
+ const dir_path& start,
+ path_match_flags flags)
{
bool r (false);
@@ -2186,7 +2224,7 @@ namespace butl
return true;
};
- path_search (pattern, entry, match, start);
+ path_search (pattern, entry, match, start, flags);
return r;
}
}
diff --git a/libbutl/filesystem.ixx b/libbutl/filesystem.ixx
index 97891d5..866d603 100644
--- a/libbutl/filesystem.ixx
+++ b/libbutl/filesystem.ixx
@@ -105,6 +105,32 @@ namespace butl
static_cast<std::uint16_t> (y));
}
+ // path_match_flags
+ //
+ inline path_match_flags operator& (path_match_flags x, path_match_flags y)
+ {
+ return x &= y;
+ }
+
+ inline path_match_flags operator| (path_match_flags x, path_match_flags y)
+ {
+ return x |= y;
+ }
+
+ inline path_match_flags operator&= (path_match_flags& x, path_match_flags y)
+ {
+ return x = static_cast<path_match_flags> (
+ static_cast<std::uint16_t> (x) &
+ static_cast<std::uint16_t> (y));
+ }
+
+ inline path_match_flags operator|= (path_match_flags& x, path_match_flags y)
+ {
+ return x = static_cast<path_match_flags> (
+ static_cast<std::uint16_t> (x) |
+ static_cast<std::uint16_t> (y));
+ }
+
// dir_entry
//
inline entry_type dir_entry::
diff --git a/libbutl/filesystem.mxx b/libbutl/filesystem.mxx
index 2a02c3a..f05e569 100644
--- a/libbutl/filesystem.mxx
+++ b/libbutl/filesystem.mxx
@@ -698,6 +698,35 @@ LIBBUTL_MODEXPORT namespace butl
// * - match any number of characters (including zero)
// ? - match any single character
+ // Path match/search flags.
+ //
+ enum class path_match_flags: std::uint16_t
+ {
+ // Follow symlinks. This only applies to symlinks that are matched against
+ // the rightmost component of the pattern. In particular, this mean that
+ // such symlinks will never match a directory pattern and some results can
+ // be missing for the recursive rightmost component.
+ //
+ follow_symlinks = 0x1,
+
+ // Make wildcard-only pattern component (e.g., `*/...`, `.../*/...`, or
+ // `.../*`) match absent path component. For example, with this flag
+ // set, the `a/*/b` pattern matches not only `a/x/b` path, but also `a/b`.
+ //
+ // Note that this does not apply to single-component patterns and the
+ // pattern type is always preserved. In particular, the `a/b/*` pattern
+ // matches `a/b` but not `a/b/`.
+ //
+ match_absent = 0x2,
+
+ none = 0
+ };
+
+ inline path_match_flags operator& (path_match_flags, path_match_flags);
+ inline path_match_flags operator| (path_match_flags, path_match_flags);
+ inline path_match_flags operator&= (path_match_flags&, path_match_flags);
+ inline path_match_flags operator|= (path_match_flags&, path_match_flags);
+
// Return true if name matches pattern. Both must be single path components,
// possibly with a trailing directory separator to indicate a directory.
//
@@ -714,10 +743,14 @@ LIBBUTL_MODEXPORT namespace butl
// start directory is used if the first pattern component is a self-matching
// wildcard (see below for the start directory and wildcard semantics).
//
+ // In addition to the wildcard characters, it also recognizes the ** and ***
+ // wildcard sequences (see path_search() for details).
+ //
LIBBUTL_SYMEXPORT bool
path_match (const path& pattern,
const path& entry,
- const dir_path& start = dir_path ());
+ const dir_path& start = dir_path (),
+ path_match_flags = path_match_flags::none);
// Search for paths matching the pattern calling the specified function for
// each matching path (see below for details).
@@ -730,14 +763,13 @@ LIBBUTL_MODEXPORT namespace butl
// The pattern may contain multiple components that include wildcards. On
// Windows the drive letter may not be a wildcard.
//
- // In addition to the wildcard characters listed in path_match(),
- // path_search() also recognizes the ** and *** wildcard sequences. If a
- // path component contains **, then it is matched just like * but in all the
- // subdirectories, recursively. The *** wildcard behaves like ** but also
- // matches the start directory itself. Note that if the first pattern
- // component contains ***, then the start directory must be empty or be
- // terminated with a "meaningful" component (e.g., probably not '.' or
- // '..').
+ // In addition to the wildcard characters, path_search() also recognizes the
+ // ** and *** wildcard sequences. If a path component contains **, then it
+ // is matched just like * but in all the subdirectories, recursively. The
+ // *** wildcard behaves like ** but also matches the start directory itself.
+ // Note that if the first pattern component contains ***, then the start
+ // directory must be empty or be terminated with a "meaningful" component
+ // (e.g., probably not '.' or '..').
//
// So, for example, foo/bar-**.txt will return all the files matching the
// bar-*.txt pattern in all the subdirectoris of foo/. And foo/f***/ will
@@ -783,12 +815,6 @@ LIBBUTL_MODEXPORT namespace butl
// (a/b/, b*/, true)
// (a/b/c/, c*/, false)
//
- // Symlinks are not followed if the follow_symlinks argument is false. This
- // rule is only applied for symlinks that are matched against the rightmost
- // component of the pattern. In particular, this mean that such symlinks will
- // never match a directory pattern, and some results can be missing for the
- // recursive rightmost component.
- //
// Note that recursive iterating through directories currently goes
// depth-first which make sense for the cleanup use cases. In future we may
// want to make it controllable.
@@ -799,7 +825,7 @@ LIBBUTL_MODEXPORT namespace butl
const std::string& pattern,
bool interm)>&,
const dir_path& start = dir_path (),
- bool follow_symlinks = true);
+ path_match_flags = path_match_flags::follow_symlinks);
// Same as above, but behaves as if the directory tree being searched
// through contains only the specified entry. The start directory is used if
@@ -816,7 +842,8 @@ LIBBUTL_MODEXPORT namespace butl
const std::function<bool (path&&,
const std::string& pattern,
bool interm)>&,
- const dir_path& start = dir_path ());
+ const dir_path& start = dir_path (),
+ path_match_flags = path_match_flags::none);
}
#include <libbutl/filesystem.ixx>