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());
365 bool in_testoutput = (absolute_path.substr(0, test_output_path.length()) == test_output_path);
373 bool in_source = (absolute_path.substr(0, source_folder_path.length()) == source_folder_path);
377 bool in_build = (absolute_path.substr(0, build_folder_path.length()) == build_folder_path);
379 if (!(in_source || in_build))
382 <<
"' as it is not located within the Chaste test output folder ("
383 << test_output_path <<
"), the Chaste source folder ("
384 << source_folder_path <<
") or the Chaste build folder ("
385 << build_folder_path <<
").");
391 <<
"' as it is not located within the Chaste test output folder ("
392 << test_output_path <<
").");
396 if (
mAbsPath.find(
"..") != std::string::npos)
399 <<
"' as it contains a dangerous path component.");
409 sig_file.remove_leaf();
412 if (!fs::exists(sig_file))
414 EXCEPTION(
"Cannot remove location '" <<
mAbsPath <<
"' because the signature file '"
439 EXCEPTION(
"Cannot search for matching files in '" <<
mAbsPath <<
"' as it is not a directory.");
441 size_t len = rPattern.length();
442 size_t inner_star_pos = rPattern.find(
'*', 1);
443 if (inner_star_pos != std::string::npos && inner_star_pos < len - 1)
445 WARNING(
"A '*' only has special meaning at the start or end of a pattern.");
449 std::string pattern(rPattern);
450 bool star_fini =
false;
451 if (!pattern.empty() && *(pattern.rbegin()) ==
'*')
454 pattern = pattern.substr(0, len-1);
457 bool star_init =
false;
458 if (!pattern.empty() && pattern[0] ==
'*')
461 pattern = pattern.substr(1);
464 bool has_query = (pattern.find(
'?') != std::string::npos);
466 if (star_init && star_fini && has_query)
468 EXCEPTION(
"The '*' wildcard may not be used at both the start and end of the pattern if the '?' wildcard is also used.");
472 std::vector<FileFinder> results;
473 if (!rPattern.empty())
475 fs::directory_iterator end_iter;
477 for (fs::directory_iterator dir_iter(our_path); dir_iter != end_iter; ++dir_iter)
480 size_t leaf_len = leafname.length();
481 if (leafname[0] !=
'.'
486 size_t pos = leafname.find(pattern);
487 if ((star_init || pos == 0) && (star_fini || pos + len == leaf_len))
489 results.push_back(
FileFinder(our_path / leafname));
498 match = leafname.substr(leaf_len - len);
503 match = leafname.substr(0, len);
506 for (std::string::const_iterator it_p=pattern.begin(), it_m=match.begin();
507 it_p != pattern.end();
510 if (*it_p !=
'?' && *it_p != *it_m)
518 results.push_back(
FileFinder(our_path / leafname));
530 return fs::path(rPath).is_complete();
535 for (std::string::iterator it = rPath.begin(); it != rPath.end(); ++it)
546 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)