From 956c3bee6c3386397e3722a5adce59ec9f8135e9 Mon Sep 17 00:00:00 2001
From: Giovanna Lazzari Miotto <giovanna.lazzari.miotto@cern.ch>
Date: Wed, 28 Feb 2024 18:00:01 +0100
Subject: [PATCH] fix: Rename origin path

---
 src/OutputByOrbit.cc     |  7 +++++--
 src/OutputFile.h         | 14 +++++++++++---
 src/OutputFileHandler.cc | 24 +++++++++++++++++-------
 src/OutputFileHandler.h  |  2 +-
 4 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/src/OutputByOrbit.cc b/src/OutputByOrbit.cc
index b9a36517..f29b9489 100644
--- a/src/OutputByOrbit.cc
+++ b/src/OutputByOrbit.cc
@@ -28,7 +28,8 @@ OutputByOrbitStream::OutputByOrbitStream(ctrl &control, const ConfigView &conf_v
 void OutputByOrbitStream::OutputFixedOrbits(Slice &out) {
   uint32_t orbitN = out.get_firstOrbitN();
   auto index = uint32_t(orbitN / conf_.num_orbits_per_file_);
-  size_t n = 0;
+  //  LOG(FATAL) << "This is orbit index=" << index << ", N=" << orbitN << " out of "
+  //             << conf_.num_orbits_per_file_;
 
   if ((out.get_counts() != 0) || conf_.support_cmssw_headers_) {
     if (control_.running.load(std::memory_order_acquire) ||
@@ -36,8 +37,10 @@ void OutputByOrbitStream::OutputFixedOrbits(Slice &out) {
       if (!out.isInitialized()) {
         return;
       }
+      //      LOG(FATAL) << "Getting file handle and writing " << out.size() << " bytes";
 
-      n = output_file_handler_.getFile(control_.run_number, index).Write(out.begin(), out.size());
+      auto file = output_file_handler_.getFile(control_.run_number, index);
+      size_t n = file->Write(out.begin(), out.size());
       //      n = fwrite(out.begin(), 1, out.size(),
       //                 output_file_handler_.getFile(control_.run_number, index).getFilePtr());
       output_file_handler_.upFileSize(n);
diff --git a/src/OutputFile.h b/src/OutputFile.h
index 83516fb8..ed35f5dd 100644
--- a/src/OutputFile.h
+++ b/src/OutputFile.h
@@ -43,7 +43,11 @@ class OutputFile {
         lumisection_(num_lumisection),
         index_(file_index) {
     try {
-      file_.open(filepath_, std::fstream::in | std::fstream::out | std::fstream::binary);
+      LOG(FATAL) << "Attempting open file: '" << filepath_ << "'";
+      file_.open(filepath_, std::ios_base::out | std::ios_base::binary);
+      if (!file_.is_open()) {
+        LOG(FATAL) << "Could not open file: '" << filepath_ << "'";
+      }
     } catch (std::exception& e) {
       LOG(FATAL) << "Could not open file: '" << filepath_ << "', exception: " << e.what();
     }
@@ -92,17 +96,21 @@ class OutputFile {
 
   bool exists() { return file_.is_open(); }
 
-  std::string GetFullPath() const { return fs::path(rundir_) / filepath_ / fileName_; }
+  std::string GetFullPath() const { return filepath_; }
 
-  void Rename(const std::string& new_path) const {
+  void Rename(const std::string& new_path) {
     fs::path from = GetFullPath();
     fs::path to = new_path;
 
+    LOG(FATAL) << "Renaming from '" << from << "' to '" << to << "' now.";
+
     try {
       fs::rename(from, to);
     } catch (fs::filesystem_error& err) {
       LOG(FATAL) << "Error renaming file: " << err.what();
     }
+
+    filepath_ = new_path;
   }
 
   size_t Write(char* buf, size_t size) {
diff --git a/src/OutputFileHandler.cc b/src/OutputFileHandler.cc
index da8d2296..8e4ec470 100644
--- a/src/OutputFileHandler.cc
+++ b/src/OutputFileHandler.cc
@@ -19,6 +19,11 @@ tbb::concurrent_bounded_queue<std::shared_ptr<OutputFile>> OutputFileHandler::fi
 std::atomic<bool> OutputFileHandler::file_handler_running_{true};
 
 void OutputFileHandler::enqueue_current_file_for_close_and_move_maybe() {
+  LOG(FATAL) << "____ Enqueue called.";
+  LOG(FATAL) << "____ Is outputFile? " << (outputFile_ != nullptr);
+
+  if (outputFile_) LOG(FATAL) << "____ Does outputFile exist()? " << (outputFile_->exists());
+
   if (outputFile_ && outputFile_->exists()) {
     LOG(TRACE) << "queueing file: " << outputFile_->getFileName() << " for closing, queue size now "
                << files_to_close_.size();
@@ -46,7 +51,7 @@ void OutputFileHandler::enqueue_current_file_for_close_and_move_maybe() {
   }
 }
 
-OutputFile& OutputFileHandler::getFile(uint32_t run, uint32_t index) {
+std::shared_ptr<OutputFile>& OutputFileHandler::getFile(uint32_t run, uint32_t index) {
   bool is_new_run = (current_run_number_ != static_cast<int>(run));
   bool is_new_index = (current_index_ != static_cast<int>(index));
 
@@ -59,10 +64,10 @@ OutputFile& OutputFileHandler::getFile(uint32_t run, uint32_t index) {
     enqueue_current_file_for_close_and_move_maybe();
     if (is_new_run) {
       run_NOrbits_ = 0;
-      LOG(TRACE) << "Previous run: " << current_run_number_ << " | New run: " << run;
+      LOG(FATAL) << "Previous run: " << current_run_number_ << " | New run: " << run;
     }
     if (is_new_index) {
-      LOG(TRACE) << "Previous index: " << current_index_ << " | New index: " << index;
+      LOG(FATAL) << "Previous index: " << current_index_ << " | New index: " << index;
     }
 
     current_index_ = static_cast<int>(index);
@@ -74,7 +79,7 @@ OutputFile& OutputFileHandler::getFile(uint32_t run, uint32_t index) {
     working_files_base_path_ = path_ss.str();
     open_new_file();
   }
-  return std::ref(*outputFile_);
+  return outputFile_;
 }
 
 FRDFileHeader_v2 OutputFileHandler::createFileHeader(uint32_t ls) {  // file header for CMSSW input
@@ -88,6 +93,7 @@ FRDFileHeader_v2 OutputFileHandler::createFileHeader(uint32_t ls) {  // file hea
 }
 
 void OutputFileHandler::create_output_directory_maybe(std::string& output_directory) {
+  LOG(FATAL) << "Called Create_output_dir";
   struct stat sb;
   LOG(TRACE) << "checking if working directory " << output_directory << " exists ";
   /* check if path exists and is a directory */
@@ -101,6 +107,8 @@ void OutputFileHandler::create_output_directory_maybe(std::string& output_direct
     throw std::runtime_error(err);
   }
 
+  LOG(FATAL) << "Attempt create " << output_directory << " directory.";
+
   if (!tools::filesystem::create_directories(output_directory)) {
     std::string err =
         tools::strerror("ERROR when creating the output directory '" + output_directory + "'");
@@ -112,11 +120,12 @@ void OutputFileHandler::create_output_directory_maybe(std::string& output_direct
 
 void OutputFileHandler::open_new_file() {
   // Create a new file
+  LOG(FATAL) << "Called Open_new_file";
   uint32_t ls = 1 + static_cast<uint32_t>(current_index_ / (max_index_per_ls_ + 1));
   create_output_directory_maybe(working_files_base_path_);
   std::string filename = format_filename(current_run_number_, current_index_, ls);
   std::string full_filename = working_files_base_path_ + "/" + filename;
-  LOG(TRACE) << "opening file with index " << current_index_ << ", in lumisection " << ls;
+  LOG(FATAL) << "opening file with index " << current_index_ << ", in lumisection " << ls;
   //  outputFile_ = std::make_shared<OutputFile>(fopen(full_filename.c_str(), "wbx"), filename,
   //                                             createFileHeader(ls), ls,
   //                                             current_index_ % (max_index_per_ls_ + 1),
@@ -156,7 +165,7 @@ std::string OutputFileHandler::format_filename(uint32_t run_number, uint32_t ind
 
 void OutputFileHandler::close_and_rename::operator()() const {
   std::shared_ptr<OutputFile> outputFile;
-  while (file_handler_running_ && (files_to_close_.size() > 0)) {
+  while (file_handler_running_ || (files_to_close_.size() > 0)) {
     LOG(TRACE) << "try pop now. queue size now " << files_to_close_.size();
     try {
       files_to_close_.pop(outputFile);
@@ -171,7 +180,8 @@ void OutputFileHandler::close_and_rename::operator()() const {
       LOG(ERROR) << tools::strerror("File close failed");
     }
 
-    outputFile->Rename(outputFile->getRunDir() + "/" + outputFile->getFileName());
+    std::string permanent_path(outputFile->getRunDir() + "/" + outputFile->getFileName());
+    outputFile->Rename(permanent_path);
     //    std::string from = outputFile->getRunDir() + "/" + working_dir_ + "/" +
     //    outputFile.getFileName(); std::string to = outputFile->getRunDir() + "/" +
     //    outputFile.getFileName(); if (rename(from.c_str(), to.c_str()) != 0) {
diff --git a/src/OutputFileHandler.h b/src/OutputFileHandler.h
index 3909a4ca..5d8dfe7b 100644
--- a/src/OutputFileHandler.h
+++ b/src/OutputFileHandler.h
@@ -53,7 +53,7 @@ class OutputFileHandler {
 
   void enqueue_current_file_for_close_and_move_maybe();
 
-  OutputFile &getFile(uint32_t run, uint32_t index);
+  std::shared_ptr<OutputFile> &getFile(uint32_t run, uint32_t index);
 
   bool hasFile() { return outputFile_->exists(); }
 
-- 
GitLab