Skip to content
Snippets Groups Projects
Commit df7205c4 authored by Giovanna Lazzari Miotto's avatar Giovanna Lazzari Miotto :mushroom:
Browse files

ref: tools: Move dir creation helper

parent e5470a81
No related branches found
No related tags found
1 merge request!109Introduce pipeline synchronization, output template and parallel sink
......@@ -73,34 +73,11 @@ int OutputFileHandler::StageBuffer(char* buffer, size_t size_bytes, uint32_t siz
return outputFile_.Write(buffer, size_bytes, size_orbits);
}
void OutputFileHandler::create_output_directory_maybe(std::string& output_directory) {
struct stat sb;
LOG(TRACE) << "checking if working directory " << output_directory << " exists ";
/* check if path exists and is a directory */
if (stat(output_directory.c_str(), &sb) == 0) {
if (S_ISDIR(sb.st_mode)) { // output directory already exists
return;
}
std::string err = "ERROR The output directory path '" + output_directory +
"' exists, but the path is not a directory!";
LOG(ERROR) << err;
throw std::runtime_error(err);
}
if (!tools::filesystem::create_directories(output_directory)) {
std::string err =
tools::strerror("ERROR when creating the output directory '" + output_directory + "'");
LOG(ERROR) << err;
throw std::runtime_error(err);
}
LOG(TRACE) << "Created output directory: " << output_directory << "'.";
}
void OutputFileHandler::open_new_file() {
// Create a new file
uint32_t ls = 1 + static_cast<uint32_t>(current_index_ / (ls_.max_index + 1));
auto working_dir = loc_.GetWorkingDir(run_.number);
create_output_directory_maybe(working_dir);
tools::CreateDirectory(working_dir);
LOG(TRACE) << "opening file with index " << current_index_ << ", in lumisection " << ls;
outputFile_ =
......
......@@ -98,8 +98,6 @@ class OutputFileHandler {
void CommitEnqueued();
private:
void create_output_directory_maybe(std::string &output_directory);
void open_new_file();
std::string FormatFilename(uint32_t run_number, uint32_t index, uint32_t ls);
......
......@@ -24,6 +24,28 @@ std::string FormatFileIndex(uint32_t index) {
return ss.str();
}
void CreateDirectory(std::string &dir_path) {
struct stat sb;
LOG(TRACE) << "checking if working directory " << dir_path << " exists ";
/* check if path exists and is a directory */
if (stat(dir_path.c_str(), &sb) == 0) {
if (S_ISDIR(sb.st_mode)) { // output directory already exists
return;
}
std::string err = "ERROR The output directory path '" + dir_path +
"' exists, but the path is not a directory!";
LOG(ERROR) << err;
throw std::runtime_error(err);
}
if (!filesystem::create_directories(dir_path)) {
std::string err = strerror("ERROR when creating the output directory '" + dir_path + "'");
LOG(ERROR) << err;
throw std::runtime_error(err);
}
LOG(TRACE) << "Created output directory: " << dir_path << "'.";
}
namespace log {
/* Definitions of forward declarations from log.h */
......
......@@ -22,6 +22,8 @@ std::string FormatRun(uint32_t run_number);
std::string FormatLumisection(uint32_t ls);
std::string FormatFileIndex(uint32_t index);
void CreateDirectory(std::string &dir_path);
/*
* A thread-safe version of strerror: Returns a C++ std::string describing
* error_code
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment