36 #include "FileFinder.hpp" 43 #include "GetCurrentWorkingDirectory.hpp" 44 #include "OutputFileHandler.hpp" 45 #include "Warnings.hpp" 46 #include "PosixPathFixer.hpp" 54 #define UNSET_PATH "UNSET!" 68 #define CONVERT_ERROR(code) \ 71 } catch (const fs::filesystem_error& e) { \ 72 EXCEPTION(e.what()); \ 76 : mAbsPath(UNSET_PATH)
82 SetPath(rRelativePath, relativeTo);
87 SetPath(rLeafName, rParentOrSibling);
116 mAbsPath = GetCurrentWorkingDirectory() +
"/" + rRelativePath;
130 mAbsPath = GetCurrentWorkingDirectory() +
"/" + rRelativePath;
147 std::string::iterator it =
mAbsPath.end();
148 while (it !=
mAbsPath.begin() && *(--it) ==
'/')
161 if (!rParentOrSibling.
Exists())
165 if (rParentOrSibling.
IsDir())
200 empty = (fs::file_size(
mAbsPath) == 0u);
204 fs::directory_iterator end_iter;
205 for (fs::directory_iterator dir_iter(
mAbsPath); dir_iter != end_iter; ++dir_iter)
233 assert(rOtherEntity.
Exists());
234 return fs::last_write_time(
mAbsPath) > fs::last_write_time(rOtherEntity.
mAbsPath);
256 return FileFinder(our_path.branch_path().string(),
265 if (our_path.substr(0, base_path.length()) != base_path)
267 EXCEPTION(
"The path '" << our_path <<
"' is not relative to '" << base_path <<
"'.");
269 return our_path.substr(base_path.length());
278 void RecursiveCopy(
const fs::path& rFromPath,
const fs::path& rToPath);
280 void RecursiveCopy(
const fs::path& rFromPath,
const fs::path& rToPath)
282 fs::path dest = rToPath;
284 if (fs::is_directory(dest))
286 dest /= rFromPath.leaf();
289 if (fs::is_directory(rFromPath))
293 fs::create_directory(dest);
295 fs::directory_iterator end_iter;
296 for (fs::directory_iterator dir_iter(rFromPath); dir_iter != end_iter; ++dir_iter)
298 RecursiveCopy(dir_iter->path(), dest);
303 fs::copy_file(rFromPath, dest);
318 to_path /= from_path.leaf();
320 if (fs::exists(to_path))
324 CONVERT_ERROR(fs::remove(to_path));
328 EXCEPTION(
"Cannot copy '" <<
mAbsPath <<
"' to '" << to_path <<
"' as it would overwrite an existing file.");
331 CONVERT_ERROR(RecursiveCopy(from_path, to_path));
340 void RemoveAll(
const fs::path& rPath);
342 void RemoveAll(
const fs::path& rPath)
345 if (fs::is_directory(rPath))
347 fs::directory_iterator end_iter;
348 for (fs::directory_iterator dir_iter(rPath); dir_iter != end_iter; ++dir_iter)
350 RemoveAll(dir_iter->path());
363 bool in_testoutput = (absolute_path.substr(0, test_output_path.length()) == test_output_path);
371 bool in_source = (absolute_path.substr(0, source_folder_path.length()) == source_folder_path);
375 bool in_build = (absolute_path.substr(0, build_folder_path.length()) == build_folder_path);
377 if (!(in_source || in_build))
380 <<
"' as it is not located within the Chaste test output folder (" 381 << test_output_path <<
"), the Chaste source folder (" 382 << source_folder_path <<
") or the Chaste build folder (" 383 << build_folder_path <<
").");
389 <<
"' as it is not located within the Chaste test output folder (" 390 << test_output_path <<
").");
394 if (
mAbsPath.find(
"..") != std::string::npos)
397 <<
"' as it contains a dangerous path component.");
407 sig_file.remove_leaf();
410 if (!fs::exists(sig_file))
412 EXCEPTION(
"Cannot remove location '" <<
mAbsPath <<
"' because the signature file '" 437 EXCEPTION(
"Cannot search for matching files in '" <<
mAbsPath <<
"' as it is not a directory.");
439 size_t len = rPattern.length();
440 size_t inner_star_pos = rPattern.find(
'*', 1);
441 if (inner_star_pos != std::string::npos && inner_star_pos < len - 1)
443 WARNING(
"A '*' only has special meaning at the start or end of a pattern.");
447 std::string pattern(rPattern);
448 bool star_fini =
false;
449 if (!pattern.empty() && *(pattern.rbegin()) ==
'*')
452 pattern = pattern.substr(0, len-1);
455 bool star_init =
false;
456 if (!pattern.empty() && pattern[0] ==
'*')
459 pattern = pattern.substr(1);
462 bool has_query = (pattern.find(
'?') != std::string::npos);
464 if (star_init && star_fini && has_query)
466 EXCEPTION(
"The '*' wildcard may not be used at both the start and end of the pattern if the '?' wildcard is also used.");
470 std::vector<FileFinder> results;
471 if (!rPattern.empty())
473 fs::directory_iterator end_iter;
475 for (fs::directory_iterator dir_iter(our_path); dir_iter != end_iter; ++dir_iter)
478 size_t leaf_len = leafname.length();
479 if (leafname[0] !=
'.' 484 size_t pos = leafname.find(pattern);
485 if ((star_init || pos == 0) && (star_fini || pos + len == leaf_len))
487 results.push_back(
FileFinder(our_path / leafname));
496 match = leafname.substr(leaf_len - len);
501 match = leafname.substr(0, len);
504 for (std::string::const_iterator it_p=pattern.begin(), it_m=match.begin();
505 it_p != pattern.end();
508 if (*it_p !=
'?' && *it_p != *it_m)
516 results.push_back(
FileFinder(our_path / leafname));
528 return fs::path(rPath).is_complete();
533 for (std::string::iterator it = rPath.begin(); it != rPath.end(); ++it)
544 for (std::string::iterator it = rPath.begin(); it != rPath.end(); ++it)
void DangerousRemove() const
static const std::string SIG_FILE_NAME
void PrivateRemove(bool dangerous=false) const
static bool IsAbsolutePath(const std::string &rPath)
const char * ChasteSourceRootDir()
std::string GetAbsolutePath() const
#define EXCEPTION(message)
FileFinder CopyTo(const FileFinder &rDest) const
static RelativeTo::Value msFakeWhat
static std::string msFakePath
std::string GetExtension() const
#define EXCEPT_IF_NOT(test)
#define PATH_LEAF_NAME(path)
static std::string ToPosix(const fs::path path)
std::vector< FileFinder > FindMatches(const std::string &rPattern) const
const char * ChasteBuildRootDir()
FileFinder GetParent() const
static void ReplaceUnderscoresWithSpaces(std::string &rPath)
std::string GetLeafNameNoExtension() const
virtual void SetPath(const std::string &rPath, RelativeTo::Value relativeTo)
static std::string GetChasteTestOutputDirectory()
std::string GetLeafName() const
static void FakePath(RelativeTo::Value fakeWhat, const std::string &rFakePath)
std::string GetRelativePath(const FileFinder &rBasePath) const
bool IsNewerThan(const FileFinder &rOtherEntity) const
static void ReplaceSpacesWithUnderscores(std::string &rPath)