29
29
#include " utils.h"
30
30
31
31
#include < cstring>
32
+ #include < iostream>
32
33
#include < iterator>
33
34
#include < memory>
34
35
45
46
// When compiling Unicode targets WinAPI automatically uses *W Unicode versions
46
47
// of called functions. Thus, we explicitly call *A versions of the functions.
47
48
48
- static std::string addFiles2 (std::list<FileWithDetails>&files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
49
+ static std::string addFiles2 (std::list<FileWithDetails>&files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored, bool debug = false )
49
50
{
50
51
const std::string cleanedPath = Path::toNativeSeparators (path);
51
52
@@ -85,8 +86,9 @@ static std::string addFiles2(std::list<FileWithDetails>&files, const std::string
85
86
HANDLE hFind = FindFirstFileA (searchPattern.c_str (), &ffd);
86
87
if (INVALID_HANDLE_VALUE == hFind) {
87
88
const DWORD err = GetLastError ();
88
- if (err == ERROR_FILE_NOT_FOUND) {
89
- // no files matched
89
+ if (err == ERROR_FILE_NOT_FOUND || // the pattern did not match anything
90
+ err == ERROR_PATH_NOT_FOUND) // the given search path does not exist
91
+ {
90
92
return " " ;
91
93
}
92
94
return " finding files failed. Search pattern: '" + searchPattern + " '. (error: " + std::to_string (err) + " )" ;
@@ -106,22 +108,30 @@ static std::string addFiles2(std::list<FileWithDetails>&files, const std::string
106
108
if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0 ) {
107
109
// File
108
110
Standards::Language lang = Standards::Language::None;
109
- if ((!checkAllFilesInDir || Path::acceptFile (fname, extra, &lang)) && !ignored.match (fname)) {
110
- std::string nativename = Path::fromNativeSeparators (fname);
111
+ if ((!checkAllFilesInDir || Path::acceptFile (fname, extra, &lang))) {
112
+ if (!ignored.match (fname)) {
113
+ std::string nativename = Path::fromNativeSeparators (fname);
111
114
112
- // Limitation: file sizes are assumed to fit in a 'size_t'
115
+ // Limitation: file sizes are assumed to fit in a 'size_t'
113
116
#ifdef _WIN64
114
- const std::size_t filesize = (static_cast <std::size_t >(ffd.nFileSizeHigh ) << 32 ) | ffd.nFileSizeLow ;
117
+ const std::size_t filesize = (static_cast <std::size_t >(ffd.nFileSizeHigh ) << 32 ) | ffd.nFileSizeLow ;
115
118
#else
116
- const std::size_t filesize = ffd.nFileSizeLow ;
119
+ const std::size_t filesize = ffd.nFileSizeLow ;
117
120
118
121
#endif
119
- files.emplace_back (std::move (nativename), lang, filesize);
122
+ files.emplace_back (std::move (nativename), lang, filesize);
123
+ }
124
+ else if (debug)
125
+ {
126
+ std::cout << " ignored path: " << fname << std::endl;
127
+ }
120
128
}
121
129
} else {
122
130
// Directory
123
131
if (recursive) {
124
- if (!ignored.match (fname)) {
132
+ // append a slash if it is a directory since that is what we are doing for mIgnoredPaths directory entries.
133
+ // otherwise we would ignore all its contents individually instead as a whole.
134
+ if (!ignored.match (fname + ' /' )) {
125
135
std::list<FileWithDetails> filesSorted;
126
136
127
137
std::string err = addFiles2 (filesSorted, fname, extra, recursive, ignored);
@@ -135,6 +145,10 @@ static std::string addFiles2(std::list<FileWithDetails>&files, const std::string
135
145
136
146
files.insert (files.end (), std::make_move_iterator (filesSorted.begin ()), std::make_move_iterator (filesSorted.end ()));
137
147
}
148
+ else if (debug)
149
+ {
150
+ std::cout << " ignored path: " << fname << std::endl;
151
+ }
138
152
}
139
153
}
140
154
}
@@ -151,14 +165,14 @@ static std::string addFiles2(std::list<FileWithDetails>&files, const std::string
151
165
return " " ;
152
166
}
153
167
154
- std::string FileLister::addFiles (std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
168
+ std::string FileLister::addFiles (std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored, bool debug )
155
169
{
156
170
if (path.empty ())
157
171
return " no path specified" ;
158
172
159
173
std::list<FileWithDetails> filesSorted;
160
174
161
- std::string err = addFiles2 (filesSorted, path, extra, recursive, ignored);
175
+ std::string err = addFiles2 (filesSorted, path, extra, recursive, ignored, debug );
162
176
163
177
// files need to be sorted as the filesystems dosn't provide a stable order
164
178
filesSorted.sort ([](const FileWithDetails& a, const FileWithDetails& b) {
@@ -183,11 +197,15 @@ static std::string addFiles2(std::list<FileWithDetails> &files,
183
197
const std::string &path,
184
198
const std::set<std::string> &extra,
185
199
bool recursive,
186
- const PathMatch& ignored
187
- )
200
+ const PathMatch& ignored,
201
+ bool debug )
188
202
{
189
203
if (ignored.match (path))
204
+ {
205
+ if (debug)
206
+ std::cout << " ignored path: " << path << std::endl;
190
207
return " " ;
208
+ }
191
209
192
210
struct stat file_stat;
193
211
if (stat (path.c_str (), &file_stat) == -1 )
@@ -224,28 +242,43 @@ static std::string addFiles2(std::list<FileWithDetails> &files,
224
242
const bool path_is_directory = Path::isDirectory (new_path);
225
243
#endif
226
244
if (path_is_directory) {
227
- if (recursive && !ignored.match (new_path)) {
228
- std::string err = addFiles2 (files, new_path, extra, recursive, ignored);
229
- if (!err.empty ()) {
230
- return err;
245
+ if (recursive) {
246
+ // append a slash if it is a directory since that is what we are doing for mIgnoredPaths directory entries.
247
+ // otherwise we would ignore all its contents individually instead as a whole.
248
+ if (!ignored.match (new_path + ' /' )) {
249
+ std::string err = addFiles2 (files, new_path, extra, recursive, ignored, debug);
250
+ if (!err.empty ()) {
251
+ return err;
252
+ }
253
+ }
254
+ else if (debug)
255
+ {
256
+ std::cout << " ignored path: " << new_path << std::endl;
231
257
}
232
258
}
233
259
} else {
234
260
Standards::Language lang = Standards::Language::None;
235
- if (Path::acceptFile (new_path, extra, &lang) && !ignored.match (new_path)) {
236
- if (stat (new_path.c_str (), &file_stat) == -1 ) {
237
- const int err = errno;
238
- return " could not stat file '" + new_path + " ' (errno: " + std::to_string (err) + " )" ;
261
+ if (Path::acceptFile (new_path, extra, &lang)) {
262
+ if (!ignored.match (new_path))
263
+ {
264
+ if (stat (new_path.c_str (), &file_stat) == -1 ) {
265
+ const int err = errno;
266
+ return " could not stat file '" + new_path + " ' (errno: " + std::to_string (err) + " )" ;
267
+ }
268
+ files.emplace_back (new_path, lang, file_stat.st_size );
269
+ }
270
+ else if (debug)
271
+ {
272
+ std::cout << " ignored path: " << new_path << std::endl;
239
273
}
240
- files.emplace_back (new_path, lang, file_stat.st_size );
241
274
}
242
275
}
243
276
}
244
277
245
278
return " " ;
246
279
}
247
280
248
- std::string FileLister::addFiles (std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
281
+ std::string FileLister::addFiles (std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored, bool debug )
249
282
{
250
283
if (path.empty ())
251
284
return " no path specified" ;
@@ -256,7 +289,7 @@ std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::s
256
289
257
290
std::list<FileWithDetails> filesSorted;
258
291
259
- std::string err = addFiles2 (filesSorted, corrected_path, extra, recursive, ignored);
292
+ std::string err = addFiles2 (filesSorted, corrected_path, extra, recursive, ignored, debug );
260
293
261
294
// files need to be sorted as the filesystems dosn't provide a stable order
262
295
filesSorted.sort ([](const FileWithDetails& a, const FileWithDetails& b) {
@@ -269,7 +302,7 @@ std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::s
269
302
270
303
#endif
271
304
272
- std::string FileLister::recursiveAddFiles (std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
305
+ std::string FileLister::recursiveAddFiles (std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored, bool debug )
273
306
{
274
- return addFiles (files, path, extra, true , ignored);
307
+ return addFiles (files, path, extra, true , ignored, debug );
275
308
}
0 commit comments