2929#include " utils.h"
3030
3131#include < cstring>
32+ #include < iostream>
3233#include < iterator>
3334#include < memory>
3435
4546// When compiling Unicode targets WinAPI automatically uses *W Unicode versions
4647// of called functions. Thus, we explicitly call *A versions of the functions.
4748
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 )
4950{
5051 const std::string cleanedPath = Path::toNativeSeparators (path);
5152
@@ -85,8 +86,9 @@ static std::string addFiles2(std::list<FileWithDetails>&files, const std::string
8586 HANDLE hFind = FindFirstFileA (searchPattern.c_str (), &ffd);
8687 if (INVALID_HANDLE_VALUE == hFind) {
8788 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+ {
9092 return " " ;
9193 }
9294 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
106108 if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0 ) {
107109 // File
108110 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);
111114
112- // Limitation: file sizes are assumed to fit in a 'size_t'
115+ // Limitation: file sizes are assumed to fit in a 'size_t'
113116#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 ;
115118#else
116- const std::size_t filesize = ffd.nFileSizeLow ;
119+ const std::size_t filesize = ffd.nFileSizeLow ;
117120
118121#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+ }
120128 }
121129 } else {
122130 // Directory
123131 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 + ' /' )) {
125135 std::list<FileWithDetails> filesSorted;
126136
127137 std::string err = addFiles2 (filesSorted, fname, extra, recursive, ignored);
@@ -135,6 +145,10 @@ static std::string addFiles2(std::list<FileWithDetails>&files, const std::string
135145
136146 files.insert (files.end (), std::make_move_iterator (filesSorted.begin ()), std::make_move_iterator (filesSorted.end ()));
137147 }
148+ else if (debug)
149+ {
150+ std::cout << " ignored path: " << fname << std::endl;
151+ }
138152 }
139153 }
140154 }
@@ -151,14 +165,14 @@ static std::string addFiles2(std::list<FileWithDetails>&files, const std::string
151165 return " " ;
152166}
153167
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 )
155169{
156170 if (path.empty ())
157171 return " no path specified" ;
158172
159173 std::list<FileWithDetails> filesSorted;
160174
161- std::string err = addFiles2 (filesSorted, path, extra, recursive, ignored);
175+ std::string err = addFiles2 (filesSorted, path, extra, recursive, ignored, debug );
162176
163177 // files need to be sorted as the filesystems dosn't provide a stable order
164178 filesSorted.sort ([](const FileWithDetails& a, const FileWithDetails& b) {
@@ -183,11 +197,15 @@ static std::string addFiles2(std::list<FileWithDetails> &files,
183197 const std::string &path,
184198 const std::set<std::string> &extra,
185199 bool recursive,
186- const PathMatch& ignored
187- )
200+ const PathMatch& ignored,
201+ bool debug )
188202{
189203 if (ignored.match (path))
204+ {
205+ if (debug)
206+ std::cout << " ignored path: " << path << std::endl;
190207 return " " ;
208+ }
191209
192210 struct stat file_stat;
193211 if (stat (path.c_str (), &file_stat) == -1 )
@@ -224,28 +242,43 @@ static std::string addFiles2(std::list<FileWithDetails> &files,
224242 const bool path_is_directory = Path::isDirectory (new_path);
225243#endif
226244 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;
231257 }
232258 }
233259 } else {
234260 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;
239273 }
240- files.emplace_back (new_path, lang, file_stat.st_size );
241274 }
242275 }
243276 }
244277
245278 return " " ;
246279}
247280
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 )
249282{
250283 if (path.empty ())
251284 return " no path specified" ;
@@ -256,7 +289,7 @@ std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::s
256289
257290 std::list<FileWithDetails> filesSorted;
258291
259- std::string err = addFiles2 (filesSorted, corrected_path, extra, recursive, ignored);
292+ std::string err = addFiles2 (filesSorted, corrected_path, extra, recursive, ignored, debug );
260293
261294 // files need to be sorted as the filesystems dosn't provide a stable order
262295 filesSorted.sort ([](const FileWithDetails& a, const FileWithDetails& b) {
@@ -269,7 +302,7 @@ std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::s
269302
270303#endif
271304
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 )
273306{
274- return addFiles (files, path, extra, true , ignored);
307+ return addFiles (files, path, extra, true , ignored, debug );
275308}
0 commit comments