36#include "OutputFileHandler.hpp"
43#include "ArchiveLocationInfo.hpp"
46#include "FileFinder.hpp"
47#include "FilesystemPermissions.hpp"
48#include "GetCurrentWorkingDirectory.hpp"
61void CleanFolder(
const fs::path& rPath,
bool isTop=
true);
63void CleanFolder(
const fs::path& rPath,
bool isTop)
65 assert(fs::is_directory(rPath));
66 fs::directory_iterator end_iter;
68 for (fs::directory_iterator dir_iter(rPath); dir_iter != end_iter; ++dir_iter)
70 if (fs::is_directory(dir_iter->status()))
72 CleanFolder(dir_iter->path(),
false);
76 const fs::path& r_item_path(dir_iter->path());
77 if (!isTop || (r_item_path.filename().string())[0] !=
'.')
79 fs::remove(r_item_path);
92 bool cleanOutputDirectory)
99 bool cleanOutputDirectory)
102 std::string relative_path;
109 EXCEPTION(
"The location provided to OutputFileHandler must be inside CHASTE_TEST_OUTPUT; '"
113 if (*output_root.
GetAbsolutePath().rbegin() !=
'/' && !relative_path.empty())
115 assert(*relative_path.begin() ==
'/');
116 relative_path.erase(0, 1);
123 bool cleanOutputDirectory)
126 if (rDirectory.find(
"..") != std::string::npos)
128 EXCEPTION(
"Will not create directory: " + rDirectory +
129 " due to it potentially being above, and cleaning, CHASTE_TEST_OUTPUT.");
136 if (fs::path(rDirectory).has_root_path())
138 EXCEPTION(
"The constructor argument to OutputFileHandler must be a relative path; '"
139 << rDirectory <<
"' is absolute.");
145 if (rDirectory !=
"" && cleanOutputDirectory)
148 if (!signature_file.
Exists())
165 char* chaste_test_output = std::getenv(
"CHASTE_TEST_OUTPUT");
169 if (chaste_test_output ==
nullptr || *chaste_test_output == 0)
179 std::string chaste_test_output_directory = directory_root.
GetAbsolutePath();
181 return chaste_test_output_directory;
187 fs::path rel_path(rDirectory);
195 if (!rel_path.empty())
197 assert( (*(--rel_path.end())) !=
".");
211 fs::path next_folder(output_root);
212 for (fs::path::iterator path_iter = rel_path.begin(); path_iter != rel_path.end(); ++path_iter)
214 next_folder /= *path_iter;
220 std::ofstream sig_file(sig_file_path);
227 catch (
const fs::filesystem_error& e)
229 TERMINATE(
"Error making test output folder: " << e.what());
237 std::string path_with_slash = (output_root / rel_path).
string();
239 return path_with_slash;
251 if (!relative_path.empty() && *relative_path.rbegin() ==
'/')
253 relative_path.erase(--relative_path.end());
255 return relative_path;
259 std::ios_base::openmode mode)
const
263 fs::path file_name(rFileName);
264 if (file_name.has_root_path())
266 file_name = file_name.relative_path();
268 fs::path output_file = fs::path(
mDirectory) / file_name;
270 out_stream p_output_file(
new std::ofstream(output_file, mode));
271 if (!p_output_file->is_open())
276 return p_output_file;
281 const std::string& rFileFormat,
282 std::ios_base::openmode mode)
const
284 std::stringstream string_stream;
285 string_stream << rFileName << number << rFileFormat;
298 if (rDirectory!=
"" && !( *(rDirectory.end()-1) ==
'/'))
300 rDirectory = rDirectory +
"/";
306 if (!rSourceFile.
IsFile())
312 to_path /= from_path.filename();
320 catch (
const fs::filesystem_error& e)
std::string DefaultChasteTestOutput()
#define TERMINATE(message)
#define EXCEPTION(message)
static void SetArchiveDirectory(const FileFinder &rDirectory)
std::string GetRelativePath(const FileFinder &rBasePath) const
std::string GetAbsolutePath() const
virtual void SetPath(const std::string &rPath, RelativeTo::Value relativeTo)
static bool CreateDirectoriesWithPermissions(const fs::path &rPath)
static void CopyFileWithPermissions(const fs::path &rFromPath, const fs::path &rToPath)
static bool CreateDirectoryWithPermissions(const fs::path &rPath)
static void SetFilePermissions(const fs::path &rPath)
FileFinder CopyFileTo(const FileFinder &rSourceFile) const
static void AddTrailingSlash(std::string &rDirectory)
static std::string GetChasteTestOutputDirectory()
static const std::string SIG_FILE_NAME
std::string GetOutputDirectoryFullPath() const
void SetArchiveDirectory() const
FileFinder FindFile(std::string leafName) const
std::string MakeFoldersAndReturnFullPath(const std::string &rDirectory) const
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
void CommonConstructor(const std::string &rDirectory, bool cleanOutputDirectory)
std::string mDirectory
The directory to store output files in (always ends in "/")
std::string GetRelativePath() const
OutputFileHandler(const std::string &rDirectory, bool cleanOutputDirectory=true)