fgerlits commented on code in PR #2069:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2069#discussion_r2547231652


##########
libminifi/src/properties/Properties.cpp:
##########
@@ -173,55 +180,82 @@ void PropertiesImpl::loadConfigureFile(const 
std::filesystem::path& configuratio
   }
 
   std::error_code ec;
-  properties_file_ = std::filesystem::canonical(configuration_file, ec);
+  base_properties_file_ = std::filesystem::canonical(configuration_file, ec);
 
   if (ec.value() != 0) {
     logger_->log_warn("Configuration file '{}' does not exist, and it could 
not be created", configuration_file);
     return;
   }
 
-  logger_->log_info("Using configuration file to load configuration for {} 
from {} (located at {})",
-                    getName().c_str(), configuration_file.string(), 
properties_file_.string());
+  properties_files_ = { base_properties_file_ };
+
+  const auto extra_properties_files_dir = extra_properties_files_dir_name();
+  std::vector<std::filesystem::path> extra_properties_file_names;
+  if (utils::file::exists(extra_properties_files_dir) && 
utils::file::is_directory(extra_properties_files_dir)) {
+    utils::file::list_dir(extra_properties_files_dir, [&](const 
std::filesystem::path&, const std::filesystem::path& file_name) {
+      if (!file_name.string().ends_with(".bak")) {
+        extra_properties_file_names.push_back(file_name);
+      }
+      return true;
+    }, logger_, /* recursive = */ false);
+  }
+  std::ranges::sort(extra_properties_file_names);
+  for (const auto& file_name : extra_properties_file_names) {
+    properties_files_.push_back(extra_properties_files_dir / file_name);
+  }
 
-  std::ifstream file(properties_file_, std::ifstream::in);
-  if (!file.good()) {
-    logger_->log_error("load configure file failed {}", properties_file_);
-    return;
+  logger_->log_info("Using configuration file to load configuration for {} 
from {} (located at {})",
+                    getName().c_str(), configuration_file.string(), 
base_properties_file_.string());
+  if (!extra_properties_file_names.empty()) {
+    auto list_of_files = utils::string::join(", ", 
extra_properties_file_names, [](const auto& path) { return path.string(); });
+    logger_->log_info("Also reading configuration from files {} in {}", 
list_of_files, extra_properties_files_dir.string());
   }
+
   properties_.clear();
   dirty_ = false;
-  for (const auto& line : PropertiesFile{file}) {
-    auto key = line.getKey();
-    auto persisted_value = line.getValue();
-    auto value = utils::string::replaceEnvironmentVariables(persisted_value);
-    bool need_to_persist_new_value = false;
-    fixValidatedProperty(std::string(prefix) + key, persisted_value, value, 
need_to_persist_new_value, *logger_);
-    dirty_ = dirty_ || need_to_persist_new_value;
-    properties_[key] = {persisted_value, value, need_to_persist_new_value};
+  for (const auto& properties_file : properties_files_) {
+    std::ifstream file(properties_file, std::ifstream::in);
+    if (!file.good()) {
+      logger_->log_error("load configure file failed {}", properties_file);
+      continue;
+    }
+    for (const auto& line : PropertiesFile{file}) {
+      auto key = line.getKey();
+      auto persisted_value = line.getValue();
+      auto value = utils::string::replaceEnvironmentVariables(persisted_value);
+      bool need_to_persist_new_value = false;
+      fixValidatedProperty(std::string(prefix) + key, persisted_value, value, 
need_to_persist_new_value, *logger_);
+      dirty_ = dirty_ || need_to_persist_new_value;
+      properties_[key] = {persisted_value, value, need_to_persist_new_value};
+    }
   }
-  checksum_calculator_.setFileLocation(properties_file_);
+
+  checksum_calculator_.setFileLocations(properties_files_);
 }
 
 std::filesystem::path PropertiesImpl::getFilePath() const {
   std::lock_guard<std::mutex> lock(mutex_);
-  return properties_file_;
+  return base_properties_file_;
 }
 
 bool PropertiesImpl::commitChanges() {
   std::lock_guard<std::mutex> lock(mutex_);
   if (!dirty_) {
-    logger_->log_info("Attempt to persist, but properties are not updated");
+    logger_->log_debug("commitChanges() called, but properties have not 
changed, nothing to do");
     return true;
   }
-  std::ifstream file(properties_file_, std::ifstream::in);
+  const auto output_file = (persist_to_ == PersistTo::SingleFile ? 
base_properties_file_ : extra_properties_files_dir_name() / 
C2PropertiesFileName);
+  if (!std::filesystem::exists(output_file)) {
+    logger_->log_debug("Configuration file {} does not exist yet, creating 
it", output_file);
+    utils::file::create_dir(output_file.parent_path(), /* recursive = */ true);
+    std::ofstream file{output_file};
+  }
+
+  std::ifstream file(output_file, std::ifstream::in);
   if (!file) {
-    logger_->log_error("load configure file failed {}", properties_file_);
+    logger_->log_error("Failed to load configuration file {}", output_file);
     return false;
   }
-
-  auto new_file = properties_file_;
-  new_file += ".new";
-
   PropertiesFile current_content{file};
   for (const auto& prop : properties_) {
     if (!prop.second.need_to_persist_new_value) {

Review Comment:
   Pulling out the file-reading part is not so easy because of the `return 
false` in line 219/257.
   
   I have pulled out the next section, which sets the changed properties in 
`current_content`: 9d355e80fe1b4175f804cf3d4690aac9c1fb7693



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to