mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-28 08:04:14 +02:00
Use AtomicFile
where applicable in ConfigPackageUtility
This commit is contained in:
parent
35f42fa5a3
commit
1ac4d83963
@ -3,6 +3,7 @@
|
|||||||
#include "remote/configpackageutility.hpp"
|
#include "remote/configpackageutility.hpp"
|
||||||
#include "remote/apilistener.hpp"
|
#include "remote/apilistener.hpp"
|
||||||
#include "base/application.hpp"
|
#include "base/application.hpp"
|
||||||
|
#include "base/atomic-file.hpp"
|
||||||
#include "base/exception.hpp"
|
#include "base/exception.hpp"
|
||||||
#include "base/utility.hpp"
|
#include "base/utility.hpp"
|
||||||
#include <boost/algorithm/string.hpp>
|
#include <boost/algorithm/string.hpp>
|
||||||
@ -119,14 +120,9 @@ String ConfigPackageUtility::CreateStage(const String& packageName, const Dictio
|
|||||||
void ConfigPackageUtility::WritePackageConfig(const String& packageName)
|
void ConfigPackageUtility::WritePackageConfig(const String& packageName)
|
||||||
{
|
{
|
||||||
String stageName = GetActiveStage(packageName);
|
String stageName = GetActiveStage(packageName);
|
||||||
|
AtomicFile::Write(GetPackageDir() + "/" + packageName + "/include.conf", 0644, "include \"*/include.conf\"\n");
|
||||||
|
|
||||||
String includePath = GetPackageDir() + "/" + packageName + "/include.conf";
|
AtomicFile fpActive(GetPackageDir() + "/" + packageName + "/active.conf", 0644);
|
||||||
std::ofstream fpInclude(includePath.CStr(), std::ofstream::out | std::ostream::binary | std::ostream::trunc);
|
|
||||||
fpInclude << "include \"*/include.conf\"\n";
|
|
||||||
fpInclude.close();
|
|
||||||
|
|
||||||
String activePath = GetPackageDir() + "/" + packageName + "/active.conf";
|
|
||||||
std::ofstream fpActive(activePath.CStr(), std::ofstream::out | std::ostream::binary | std::ostream::trunc);
|
|
||||||
fpActive << "if (!globals.contains(\"ActiveStages\")) {\n"
|
fpActive << "if (!globals.contains(\"ActiveStages\")) {\n"
|
||||||
<< " globals.ActiveStages = {}\n"
|
<< " globals.ActiveStages = {}\n"
|
||||||
<< "}\n"
|
<< "}\n"
|
||||||
@ -145,19 +141,18 @@ void ConfigPackageUtility::WritePackageConfig(const String& packageName)
|
|||||||
<< "if (!ActiveStages.contains(\"" << packageName << "\")) {\n"
|
<< "if (!ActiveStages.contains(\"" << packageName << "\")) {\n"
|
||||||
<< " ActiveStages[\"" << packageName << "\"] = \"" << stageName << "\"\n"
|
<< " ActiveStages[\"" << packageName << "\"] = \"" << stageName << "\"\n"
|
||||||
<< "}\n";
|
<< "}\n";
|
||||||
fpActive.close();
|
fpActive.Commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigPackageUtility::WriteStageConfig(const String& packageName, const String& stageName)
|
void ConfigPackageUtility::WriteStageConfig(const String& packageName, const String& stageName)
|
||||||
{
|
{
|
||||||
String path = GetPackageDir() + "/" + packageName + "/" + stageName + "/include.conf";
|
AtomicFile fp(GetPackageDir() + "/" + packageName + "/" + stageName + "/include.conf", 0644);
|
||||||
std::ofstream fp(path.CStr(), std::ofstream::out | std::ostream::binary | std::ostream::trunc);
|
|
||||||
fp << "include \"../active.conf\"\n"
|
fp << "include \"../active.conf\"\n"
|
||||||
<< "if (ActiveStages[\"" << packageName << "\"] == \"" << stageName << "\") {\n"
|
<< "if (ActiveStages[\"" << packageName << "\"] == \"" << stageName << "\") {\n"
|
||||||
<< " include_recursive \"conf.d\"\n"
|
<< " include_recursive \"conf.d\"\n"
|
||||||
<< " include_zones \"" << packageName << "\", \"zones.d\"\n"
|
<< " include_zones \"" << packageName << "\", \"zones.d\"\n"
|
||||||
<< "}\n";
|
<< "}\n";
|
||||||
fp.close();
|
fp.Commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigPackageUtility::ActivateStage(const String& packageName, const String& stageName)
|
void ConfigPackageUtility::ActivateStage(const String& packageName, const String& stageName)
|
||||||
@ -284,12 +279,7 @@ String ConfigPackageUtility::GetActiveStageFromFile(const String& packageName)
|
|||||||
void ConfigPackageUtility::SetActiveStageToFile(const String& packageName, const String& stageName)
|
void ConfigPackageUtility::SetActiveStageToFile(const String& packageName, const String& stageName)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(GetStaticActiveStageMutex());
|
std::unique_lock<std::mutex> lock(GetStaticActiveStageMutex());
|
||||||
|
AtomicFile::Write(GetPackageDir() + "/" + packageName + "/active-stage", 0644, stageName);
|
||||||
String activeStagePath = GetPackageDir() + "/" + packageName + "/active-stage";
|
|
||||||
|
|
||||||
std::ofstream fpActiveStage(activeStagePath.CStr(), std::ofstream::out | std::ostream::binary | std::ostream::trunc); //TODO: fstream exceptions
|
|
||||||
fpActiveStage << stageName;
|
|
||||||
fpActiveStage.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String ConfigPackageUtility::GetActiveStage(const String& packageName)
|
String ConfigPackageUtility::GetActiveStage(const String& packageName)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user