Commit 3d73e46b authored by Reinhard Biegel's avatar Reinhard Biegel
Browse files

Merge branch 'fix/generation-of-temporary-files' into 'develop'

Fix generation of temporary files

See merge request !243
parents 5d11d946 d2cbee1a
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ add_openpass_target(
    observation_LogGlobal.h
    observation_log.h
    observation_logImplementation.h
    temporaryFilePathGenerator.h
    temporaryFileGenerator.h

  SOURCES
    observationCyclics.cpp
+10 −8
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@
#include "include/dataBufferInterface.h"
#include "observationCyclics.h"
#include "observationLogConstants.h"
#include "temporaryFilePathGenerator.h"
#include "temporaryFileGenerator.h"

class RunResultInterface;

@@ -57,15 +57,20 @@ void ObservationFileHandler::WriteStartOfFile(const std::string& frameworkVersio
                             + "\" does not exist");
  }

  if (fs::exists(finalPath))
  {
    fs::remove(finalPath);
  }

  std::string filePath = folder.string() + "/" + finalFilename.string();
  TemporaryFilePathGenerator filePathCreator(filePath);
  tmpFilePath = filePathCreator.CreateTemporaryFilePath();
  TemporaryFileGenerator fileCreator(filePath);
  tmpFilePath = folder / fileCreator.CreateTemporaryFile();
  xmlFile.open(tmpFilePath.string());

  if (!xmlFile.is_open())
  {
    std::stringstream stream;
    stream << COMPONENTNAME << ": could not create file: " << tmpFilePath.string();
    stream << COMPONENTNAME << ": Unable to create temporary output file: " << tmpFilePath.filename().string();
    throw std::runtime_error(stream.str());
  }

@@ -158,11 +163,8 @@ void ObservationFileHandler::WriteEndOfFile()
  xmlFile.close();

  // finalize results
  if (fs::exists(tmpFilePath) && !fs::exists(finalPath))
  {
  fs::rename(tmpFilePath, finalPath);
}
}

void ObservationFileHandler::AddEvents(const Events& events, const ObservationCyclics& cyclicsActiveComponent)
{
+0 −1
Original line number Diff line number Diff line
@@ -140,7 +140,6 @@ protected:
  OutputTags outputTags;              ///< Output tags

  std::filesystem::path folder;         ///< Output directory
  std::filesystem::path tmpFilename;    ///< File name temporary
  std::filesystem::path finalFilename;  ///< Name of output file
  std::filesystem::path tmpFilePath;    ///< Temporary path of the output directory
  std::filesystem::path finalPath;      ///< Final path of the output directory
+57 −0
Original line number Diff line number Diff line
/********************************************************************************
 * Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
 * Copyright (c) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
@@ -19,40 +19,37 @@

namespace fs = std::filesystem;

//! @class TemporaryFilePathGenerator
//! @brief A class for generating temporary file paths with unique names.
class TemporaryFilePathGenerator
//! @class TemporaryFileGenerator
//! @brief A class for generating temporary files with unique names.
class TemporaryFileGenerator
{
public:
  //! @brief Constructor for TemporaryFilePathGenerator.
  //! @brief Constructor for TemporaryFileGenerator.
  //! @param filePath The base path for generating temporary file names.
  explicit TemporaryFilePathGenerator(fs::path filePath) : filePath(std::move(filePath)) {}
  explicit TemporaryFileGenerator(fs::path filePath) : filePath(std::move(filePath)) {}

  ~TemporaryFilePathGenerator() = default;
  ~TemporaryFileGenerator() = default;

  //! @brief Create a unique temporary file path.
  //! @brief Create a unique temporary file.
  //!
  //! If the provided file path exists, this method generates a new file name by appending
  //! a 6-digit number to the original file name.
  //!
  //! @return The generated unique temporary file path.
  fs::path CreateTemporaryFilePath()
  {
    if (fs::exists(filePath))
  //! @return The generated unique temporary file.
  fs::path CreateTemporaryFile()
  {
    int counter = 0;
    do
    {
        // Generate a new filename with a 6-digit number appended
      //! Generate a new filename with a 6-digit number appended
      std::ostringstream newFilenameStream;
      newFilenameStream << std::left << std::setw(6) << std::setfill('0') << counter % 1000000;
      std::string newFilename = filePath.stem().string() + "_" + newFilenameStream.str() + ".tmp";
        filePath = fs::path(newFilename);
      filePath.replace_filename(newFilename);
      counter++;
    } while (fs::exists(filePath));
    }

    return filePath;
    return filePath.filename();
  }

private:
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ add_openpass_target(
    observation_LogGlobal.h
    observation_log.h
    observation_logImplementation.h
    temporaryFilePathGenerator.h
    temporaryFileGenerator.h

  SOURCES
    observationCyclics.cpp
Loading