39#include "FileFinder.hpp"
42#include "GetCurrentWorkingDirectory.hpp"
43#include "OutputFileHandler.hpp"
44#include "PosixPathFixer.hpp"
45#include "Warnings.hpp"
53#define UNSET_PATH "UNSET!"
67#define CONVERT_ERROR(code) \
72 catch (const fs::filesystem_error& e) \
74 EXCEPTION(e.what()); \
78 : mAbsPath(UNSET_PATH)
84 SetPath(rRelativePath, relativeTo);
89 SetPath(rLeafName, rParentOrSibling);
118 mAbsPath = GetCurrentWorkingDirectory() +
"/" + rRelativePath;
132 mAbsPath = GetCurrentWorkingDirectory() +
"/" + rRelativePath;
149 std::string::iterator it =
mAbsPath.end();
150 while (it !=
mAbsPath.begin() && *(--it) ==
'/')
163 if (!rParentOrSibling.
Exists())
167 if (rParentOrSibling.
IsDir())
189 return fs::is_regular_file(
mAbsPath);
202 empty = (fs::file_size(
mAbsPath) == 0u);
206 fs::directory_iterator end_iter;
207 for (fs::directory_iterator dir_iter(
mAbsPath); dir_iter != end_iter; ++dir_iter)
209 if ((dir_iter->path().filename().string()).substr(0, 1) !=
".")
235 assert(rOtherEntity.
Exists());
236 return fs::last_write_time(
mAbsPath) > fs::last_write_time(rOtherEntity.
mAbsPath);
241 return fs::path(
mAbsPath).filename().string();
246 return fs::path(
mAbsPath).stem().string();
251 return fs::path(
mAbsPath).extension().string();
259 assert(!our_path.parent_path().empty());
260 return FileFinder(our_path.parent_path().string(),
268 if (our_path.substr(0, base_path.length()) != base_path)
270 EXCEPTION(
"The path '" << our_path <<
"' is not relative to '" << base_path <<
"'.");
272 return our_path.substr(base_path.length());
280void RecursiveCopy(
const fs::path& rFromPath,
const fs::path& rToPath);
282void RecursiveCopy(
const fs::path& rFromPath,
const fs::path& rToPath)
284 fs::path dest = rToPath;
286 if (fs::is_directory(dest))
288 dest /= rFromPath.filename();
291 if (fs::is_directory(rFromPath))
295 fs::create_directory(dest);
297 fs::directory_iterator end_iter;
298 for (fs::directory_iterator dir_iter(rFromPath); dir_iter != end_iter; ++dir_iter)
300 RecursiveCopy(dir_iter->path(), dest);
305 fs::copy_file(rFromPath, dest);
319 to_path /= from_path.filename();
321 if (fs::exists(to_path))
325 CONVERT_ERROR(fs::remove(to_path));
329 EXCEPTION(
"Cannot copy '" <<
mAbsPath <<
"' to '" << to_path <<
"' as it would overwrite an existing file.");
332 CONVERT_ERROR(RecursiveCopy(from_path, to_path));
340void RemoveAll(
const fs::path& rPath);
342void 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_filename();
410 if (!fs::exists(sig_file))
412 EXCEPTION(
"Cannot remove location '" <<
mAbsPath <<
"' because the signature file '"
436 EXCEPTION(
"Cannot search for matching files in '" <<
mAbsPath <<
"' as it is not a directory.");
438 size_t len = rPattern.length();
439 size_t inner_star_pos = rPattern.find(
'*', 1);
440 if (inner_star_pos != std::string::npos && inner_star_pos < len - 1)
442 WARNING(
"A '*' only has special meaning at the start or end of a pattern.");
446 std::string pattern(rPattern);
447 bool star_fini =
false;
448 if (!pattern.empty() && *(pattern.rbegin()) ==
'*')
451 pattern = pattern.substr(0, len - 1);
454 bool star_init =
false;
455 if (!pattern.empty() && pattern[0] ==
'*')
458 pattern = pattern.substr(1);
461 bool has_query = (pattern.find(
'?') != std::string::npos);
463 if (star_init && star_fini && has_query)
465 EXCEPTION(
"The '*' wildcard may not be used at both the start and end of the pattern if the '?' wildcard is also used.");
469 std::vector<FileFinder> results;
470 if (!rPattern.empty())
472 fs::directory_iterator end_iter;
474 for (fs::directory_iterator dir_iter(our_path); dir_iter != end_iter; ++dir_iter)
476 std::string leafname = dir_iter->path().filename().string();
477 size_t leaf_len = leafname.length();
478 if (leafname[0] !=
'.'
483 size_t pos = leafname.find(pattern);
484 if ((star_init || pos == 0) && (star_fini || pos + len == leaf_len))
486 results.push_back(
FileFinder(our_path / leafname));
495 match = leafname.substr(leaf_len - len);
500 match = leafname.substr(0, len);
503 for (std::string::const_iterator it_p = pattern.begin(), it_m = match.begin();
504 it_p != pattern.end();
507 if (*it_p !=
'?' && *it_p != *it_m)
515 results.push_back(
FileFinder(our_path / leafname));
522 std::sort(results.begin(), results.end());
528 return fs::path(rPath).is_absolute();
533 for (std::string::iterator it = rPath.begin(); it != rPath.end(); ++it)
544 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 std::string GetChasteTestOutputDirectory()
static const std::string SIG_FILE_NAME