36 #include "FileFinder.hpp"
43 #include "GetCurrentWorkingDirectory.hpp"
44 #include "OutputFileHandler.hpp"
45 #include "PosixPathFixer.hpp"
46 #include "Warnings.hpp"
54 #define UNSET_PATH "UNSET!"
68 #define CONVERT_ERROR(code) \
73 catch (const fs::filesystem_error& e) \
75 EXCEPTION(e.what()); \
79 : mAbsPath(UNSET_PATH)
85 SetPath(rRelativePath, relativeTo);
90 SetPath(rLeafName, rParentOrSibling);
119 mAbsPath = GetCurrentWorkingDirectory() +
"/" + rRelativePath;
133 mAbsPath = GetCurrentWorkingDirectory() +
"/" + rRelativePath;
150 std::string::iterator it =
mAbsPath.end();
151 while (it !=
mAbsPath.begin() && *(--it) ==
'/')
164 if (!rParentOrSibling.
Exists())
168 if (rParentOrSibling.
IsDir())
203 empty = (fs::file_size(
mAbsPath) == 0u);
207 fs::directory_iterator end_iter;
208 for (fs::directory_iterator dir_iter(
mAbsPath); dir_iter != end_iter; ++dir_iter)
236 assert(rOtherEntity.
Exists());
237 return fs::last_write_time(
mAbsPath) > fs::last_write_time(rOtherEntity.
mAbsPath);
259 return FileFinder(our_path.branch_path().string(),
267 if (our_path.substr(0, base_path.length()) != base_path)
269 EXCEPTION(
"The path '" << our_path <<
"' is not relative to '" << base_path <<
"'.");
271 return our_path.substr(base_path.length());
279 void RecursiveCopy(
const fs::path& rFromPath,
const fs::path& rToPath);
281 void RecursiveCopy(
const fs::path& rFromPath,
const fs::path& rToPath)
283 fs::path dest = rToPath;
285 if (fs::is_directory(dest))
287 dest /= rFromPath.leaf();
290 if (fs::is_directory(rFromPath))
294 fs::create_directory(dest);
296 fs::directory_iterator end_iter;
297 for (fs::directory_iterator dir_iter(rFromPath); dir_iter != end_iter; ++dir_iter)
299 RecursiveCopy(dir_iter->path(), dest);
304 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));
339 void RemoveAll(
const fs::path& rPath);
341 void RemoveAll(
const fs::path& rPath)
344 if (fs::is_directory(rPath))
346 fs::directory_iterator end_iter;
347 for (fs::directory_iterator dir_iter(rPath); dir_iter != end_iter; ++dir_iter)
349 RemoveAll(dir_iter->path());
362 bool in_testoutput = (absolute_path.substr(0, test_output_path.length()) == test_output_path);
370 bool in_source = (absolute_path.substr(0, source_folder_path.length()) == source_folder_path);
374 bool in_build = (absolute_path.substr(0, build_folder_path.length()) == build_folder_path);
376 if (!(in_source || in_build))
379 <<
"' as it is not located within the Chaste test output folder ("
380 << test_output_path <<
"), the Chaste source folder ("
381 << source_folder_path <<
") or the Chaste build folder ("
382 << build_folder_path <<
").");
388 <<
"' as it is not located within the Chaste test output folder ("
389 << test_output_path <<
").");
393 if (
mAbsPath.find(
"..") != std::string::npos)
396 <<
"' as it contains a dangerous path component.");
406 sig_file.remove_leaf();
409 if (!fs::exists(sig_file))
411 EXCEPTION(
"Cannot remove location '" <<
mAbsPath <<
"' because the signature file '"
435 EXCEPTION(
"Cannot search for matching files in '" <<
mAbsPath <<
"' as it is not a directory.");
437 size_t len = rPattern.length();
438 size_t inner_star_pos = rPattern.find(
'*', 1);
439 if (inner_star_pos != std::string::npos && inner_star_pos < len - 1)
441 WARNING(
"A '*' only has special meaning at the start or end of a pattern.");
445 std::string pattern(rPattern);
446 bool star_fini =
false;
447 if (!pattern.empty() && *(pattern.rbegin()) ==
'*')
450 pattern = pattern.substr(0, len - 1);
453 bool star_init =
false;
454 if (!pattern.empty() && pattern[0] ==
'*')
457 pattern = pattern.substr(1);
460 bool has_query = (pattern.find(
'?') != std::string::npos);
462 if (star_init && star_fini && has_query)
464 EXCEPTION(
"The '*' wildcard may not be used at both the start and end of the pattern if the '?' wildcard is also used.");
468 std::vector<FileFinder> results;
469 if (!rPattern.empty())
471 fs::directory_iterator end_iter;
473 for (fs::directory_iterator dir_iter(our_path); dir_iter != end_iter; ++dir_iter)
476 size_t leaf_len = leafname.length();
477 if (leafname[0] !=
'.'
482 size_t pos = leafname.find(pattern);
483 if ((star_init || pos == 0) && (star_fini || pos + len == leaf_len))
485 results.push_back(
FileFinder(our_path / leafname));
494 match = leafname.substr(leaf_len - len);
499 match = leafname.substr(0, len);
502 for (std::string::const_iterator it_p = pattern.begin(), it_m = match.begin();
503 it_p != pattern.end();
506 if (*it_p !=
'?' && *it_p != *it_m)
514 results.push_back(
FileFinder(our_path / leafname));
521 std::sort(results.begin(), results.end());
527 return fs::path(rPath).is_complete();
532 for (std::string::iterator it = rPath.begin(); it != rPath.end(); ++it)
543 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
bool operator<(const FileFinder &otherFinder) const
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)