39#include "FileFinder.hpp"
42#include "FilesystemPermissions.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())
190 return fs::is_regular_file(
mAbsPath);
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)
210 if ((dir_iter->path().filename().string()).substr(0, 1) !=
".")
236 assert(rOtherEntity.
Exists());
237 return fs::last_write_time(
mAbsPath) > fs::last_write_time(rOtherEntity.
mAbsPath);
242 return fs::path(
mAbsPath).filename().string();
247 return fs::path(
mAbsPath).stem().string();
252 return fs::path(
mAbsPath).extension().string();
260 assert(!our_path.parent_path().empty());
261 return FileFinder(our_path.parent_path().string(),
269 if (our_path.substr(0, base_path.length()) != base_path)
271 EXCEPTION(
"The path '" << our_path <<
"' is not relative to '" << base_path <<
"'.");
273 return our_path.substr(base_path.length());
281void RecursiveCopy(
const fs::path& rFromPath,
const fs::path& rToPath);
283void RecursiveCopy(
const fs::path& rFromPath,
const fs::path& rToPath)
285 fs::path dest = rToPath;
287 if (fs::is_directory(dest))
289 dest /= rFromPath.filename();
292 if (fs::is_directory(rFromPath))
298 fs::directory_iterator end_iter;
299 for (fs::directory_iterator dir_iter(rFromPath); dir_iter != end_iter; ++dir_iter)
301 RecursiveCopy(dir_iter->path(), dest);
320 to_path /= from_path.filename();
322 if (fs::exists(to_path))
326 CONVERT_ERROR(fs::remove(to_path));
330 EXCEPTION(
"Cannot copy '" <<
mAbsPath <<
"' to '" << to_path <<
"' as it would overwrite an existing file.");
333 CONVERT_ERROR(RecursiveCopy(from_path, to_path));
341void RemoveAll(
const fs::path& rPath);
343void RemoveAll(
const fs::path& rPath)
346 if (fs::is_directory(rPath))
348 fs::directory_iterator end_iter;
349 for (fs::directory_iterator dir_iter(rPath); dir_iter != end_iter; ++dir_iter)
351 RemoveAll(dir_iter->path());
364 bool in_testoutput = (absolute_path.substr(0, test_output_path.length()) == test_output_path);
372 bool in_source = (absolute_path.substr(0, source_folder_path.length()) == source_folder_path);
376 bool in_build = (absolute_path.substr(0, build_folder_path.length()) == build_folder_path);
378 if (!(in_source || in_build))
381 <<
"' as it is not located within the Chaste test output folder ("
382 << test_output_path <<
"), the Chaste source folder ("
383 << source_folder_path <<
") or the Chaste build folder ("
384 << build_folder_path <<
").");
390 <<
"' as it is not located within the Chaste test output folder ("
391 << test_output_path <<
").");
395 if (
mAbsPath.find(
"..") != std::string::npos)
398 <<
"' as it contains a dangerous path component.");
408 sig_file.remove_filename();
411 if (!fs::exists(sig_file))
413 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)
477 std::string leafname = dir_iter->path().filename().string();
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));
523 std::sort(results.begin(), results.end());
529 return fs::path(rPath).is_absolute();
534 for (std::string::iterator it = rPath.begin(); it != rPath.end(); ++it)
545 for (std::string::iterator it = rPath.begin(); it != rPath.end(); ++it)
const char * ChasteSourceRootDir()
const char * ChasteBuildRootDir()
#define EXCEPTION(message)
static std::string ToPosix(const fs::path path)
std::string GetLeafNameNoExtension() const
std::string GetRelativePath(const FileFinder &rBasePath) const
bool IsNewerThan(const FileFinder &rOtherEntity) const
static void ReplaceSpacesWithUnderscores(std::string &rPath)
std::string GetAbsolutePath() const
static bool IsAbsolutePath(const std::string &rPath)
void PrivateRemove(bool dangerous=false) const
void DangerousRemove() const
std::string GetExtension() const
std::vector< FileFinder > FindMatches(const std::string &rPattern) const
FileFinder GetParent() const
static void FakePath(RelativeTo::Value fakeWhat, const std::string &rFakePath)
static RelativeTo::Value msFakeWhat
static void ReplaceUnderscoresWithSpaces(std::string &rPath)
bool operator<(const FileFinder &otherFinder) const
std::string GetLeafName() const
virtual void SetPath(const std::string &rPath, RelativeTo::Value relativeTo)
static std::string msFakePath
FileFinder CopyTo(const FileFinder &rDest) const
static void CopyFileWithPermissions(const fs::path &rFromPath, const fs::path &rToPath)
static bool CreateDirectoryWithPermissions(const fs::path &rPath)
static std::string GetChasteTestOutputDirectory()
static const std::string SIG_FILE_NAME