1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
// file : libbuild2/functions-filesystem.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#include <libbutl/filesystem.mxx>
#include <libbutl/path-pattern.mxx>
#include <libbuild2/function.hxx>
#include <libbuild2/variable.hxx>
using namespace std;
namespace build2
{
// Return paths of filesystem entries that match the pattern. See
// path_search() overloads (below) for details.
//
static names
path_search (const path& pattern, const optional<dir_path>& start)
{
names r;
auto add = [&r] (path&& p, const std::string&, bool interm) -> bool
{
// Canonicalizing paths seems to be the right thing to do. Otherwise, we
// can end up with different separators in the same path on Windows.
//
if (!interm)
r.emplace_back (
value_traits<path>::reverse (move (p.canonicalize ())));
return true;
};
// Print paths "as is" in the diagnostics.
//
try
{
if (pattern.absolute ())
path_search (pattern, add);
else
{
// An absolute start directory must be specified for the relative
// pattern.
//
if (!start || start->relative ())
{
diag_record dr (fail);
if (!start)
dr << "start directory is not specified";
else
dr << "start directory '" << start->representation ()
<< "' is relative";
dr << info << "pattern '" << pattern.representation ()
<< "' is relative";
}
path_search (pattern, add, *start);
}
}
catch (const system_error& e)
{
diag_record d (fail);
d << "unable to scan";
// If the pattern is absolute, then the start directory is not used, and
// so printing it would be misleading.
//
if (start && pattern.relative ())
d << " '" << start->representation () << "'";
d << ": " << e
<< info << "pattern: '" << pattern.representation () << "'";
}
return r;
}
using butl::path_match;
// Return true if a path for a filesystem entry matches the pattern. See
// path_match() overloads (below) for details.
//
static bool
path_match (const path& entry,
const path& pattern,
const optional<dir_path>& start)
{
// If pattern and entry are both either absolute or relative and
// non-empty, and the first pattern component is not a self-matching
// wildcard, then ignore the start directory.
//
bool rel (pattern.relative () == entry.relative () &&
!pattern.empty () && !entry.empty ());
if (rel && !path_pattern_self_matching (pattern))
return path_match (entry, pattern);
// The start directory must be specified and be absolute.
//
if (!start || start->relative ())
{
diag_record dr (fail);
// Print paths "as is".
//
if (!start)
dr << "start directory is not specified";
else
dr << "start directory path '" << start->representation ()
<< "' is relative";
dr << info << "pattern: '" << pattern.representation () << "'"
<< info << "entry: '" << entry.representation () << "'";
}
return path_match (entry, pattern, *start);
}
void
filesystem_functions (function_map& m)
{
function_family f (m, "filesystem");
// path_search
//
// Return filesystem paths that match the pattern. If the pattern is an
// absolute path, then the start directory is ignored (if present).
// Otherwise, the start directory must be specified and be absolute.
//
f["path_search"] = [](path pattern, optional<dir_path> start)
{
return path_search (pattern, start);
};
f["path_search"] = [](path pattern, names start)
{
return path_search (pattern, convert<dir_path> (move (start)));
};
f["path_search"] = [](names pattern, optional<dir_path> start)
{
return path_search (convert<path> (move (pattern)), start);
};
f["path_search"] = [](names pattern, names start)
{
return path_search (convert<path> (move (pattern)),
convert<dir_path> (move (start)));
};
// path_match
//
// Match a filesystem entry name against a name pattern (both are strings),
// or a filesystem entry path against a path pattern. For the latter case
// the start directory may also be required (see below). The semantics of
// the pattern and name/entry arguments is determined according to the
// following rules:
//
// - The arguments must be of the string or path types, or be untyped.
//
// - If one of the arguments is typed, then the other one must be of the
// same type or be untyped. In the later case, an untyped argument is
// converted to the type of the other argument.
//
// - If both arguments are untyped and the start directory is specified,
// then the arguments are converted to the path type.
//
// - If both arguments are untyped and the start directory is not
// specified, then, if one of the arguments is syntactically a path (the
// value contains a directory separator), convert them to the path type,
// otherwise to the string type (match as names).
//
// If pattern and entry paths are both either absolute or relative and
// non-empty, and the first pattern component is not a self-matching
// wildcard (doesn't contain ***), then the start directory is not
// required, and is ignored if specified. Otherwise, the start directory
// must be specified and be an absolute path.
//
// Name matching.
//
f["path_match"] = [](string name, string pattern)
{
return path_match (name, pattern);
};
// Path matching.
//
f["path_match"] = [](path ent, path pat, optional<dir_path> start)
{
return path_match (ent, pat, start);
};
// The semantics depends on the presence of the start directory or the
// first two argument syntactic representation.
//
f["path_match"] = [](names ent, names pat, optional<names> start)
{
auto path_arg = [] (const names& a) -> bool
{
return a.size () == 1 &&
(a[0].directory () ||
a[0].value.find_first_of (path::traits_type::directory_separators) !=
string::npos);
};
return start || path_arg (pat) || path_arg (ent)
? path_match (convert<path> (move (ent)), // Match as paths.
convert<path> (move (pat)),
start
? convert<dir_path> (move (*start))
: optional<dir_path> ())
: path_match (convert<string> (move (ent)), // Match as strings.
convert<string> (move (pat)));
};
}
}
|