Replace _unlink() + rename() with boost::filesystem::rename()

refs #7101
This commit is contained in:
Alexander A. Klimov 2019-04-10 13:44:13 +02:00 committed by Michael Friedrich
parent f1f7d0c4d6
commit 5a17722c1f
13 changed files with 32 additions and 192 deletions

View File

@ -505,16 +505,7 @@ void ConfigObject::DumpObjects(const String& filename, int attributeTypes)
fp.close();
#ifdef _WIN32
_unlink(filename.CStr());
#endif /* _WIN32 */
if (rename(tempFilename.CStr(), filename.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempFilename));
}
Utility::RenameFile(tempFilename, filename);
}
void ConfigObject::RestoreObject(const String& message, int attributeTypes)

View File

@ -111,15 +111,6 @@ void ScriptGlobal::WriteToFile(const String& filename)
fp.close();
#ifdef _WIN32
_unlink(filename.CStr());
#endif /* _WIN32 */
if (rename(tempFilename.CStr(), filename.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempFilename));
}
Utility::RenameFile(tempFilename, filename);
}

View File

@ -720,6 +720,17 @@ void Utility::CopyFile(const String& source, const String& target)
fs::copy_file(fs::path(source.Begin(), source.End()), fs::path(target.Begin(), target.End()), fs::copy_option::overwrite_if_exists);
}
/*
* Renames a source file to a target location.
* Caller must ensure that the target's base directory exists and is writable.
*/
void Utility::RenameFile(const String& source, const String& target)
{
namespace fs = boost::filesystem;
fs::rename(fs::path(source.Begin(), source.End()), fs::path(target.Begin(), target.End()));
}
/*
* Set file permissions
*/
@ -1293,11 +1304,7 @@ void Utility::SaveJsonFile(const String& path, int mode, const Value& value)
fp << JsonEncode(value);
fp.close();
#ifdef _WIN32
_unlink(path.CStr());
#endif /* _WIN32 */
fs::rename(fs::path(tempFilename.Begin(), tempFilename.End()), fs::path(path.Begin(), path.End()));
RenameFile(tempFilename, path);
}
static void HexEncode(char ch, std::ostream& os)

View File

@ -113,6 +113,7 @@ public:
static void RemoveDirRecursive(const String& path);
static void CopyFile(const String& source, const String& target);
static void RenameFile(const String& source, const String& target);
static Value LoadJsonFile(const String& path);
static void SaveJsonFile(const String& path, int mode, const Value& value);

View File

@ -174,16 +174,7 @@ bool ApiSetupUtility::SetupMasterApiUser()
fp.close();
#ifdef _WIN32
_unlink(apiUsersPath.CStr());
#endif /* _WIN32 */
if (rename(tempFilename.CStr(), apiUsersPath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempFilename));
}
Utility::RenameFile(tempFilename, apiUsersPath);
return true;
}

View File

@ -207,16 +207,7 @@ int NodeSetupCommand::SetupMaster(const boost::program_options::variables_map& v
fp.close();
#ifdef _WIN32
_unlink(apipath.CStr());
#endif /* _WIN32 */
if (rename(tempApiPath.CStr(), apipath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempApiPath));
}
Utility::RenameFile(tempApiPath, apipath);
/* update constants.conf with NodeName = CN + TicketSalt = random value */
if (cn != Utility::GetFQDN()) {
@ -472,17 +463,7 @@ int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm,
fp.close();
#ifdef _WIN32
_unlink(apipath.CStr());
#endif /* _WIN32 */
if (rename(tempApiPath.CStr(), apipath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempApiPath));
}
Utility::RenameFile(tempApiPath, apipath);
/* Generate zones configuration. */
Log(LogInformation, "cli", "Generating zone and object configuration.");
@ -543,16 +524,7 @@ int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm,
fp.close();
#ifdef _WIN32
_unlink(ticketPath.CStr());
#endif /* _WIN32 */
if (rename(tempTicketPath.CStr(), ticketPath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempTicketPath));
}
Utility::RenameFile(tempTicketPath, ticketPath);
}
/* If no parent connection was made, the user must supply the ca.crt before restarting Icinga 2.*/

View File

@ -181,16 +181,7 @@ bool NodeUtility::WriteNodeConfigObjects(const String& filename, const Array::Pt
fp << std::endl;
fp.close();
#ifdef _WIN32
_unlink(filename.CStr());
#endif /* _WIN32 */
if (rename(tempFilename.CStr(), filename.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempFilename));
}
Utility::RenameFile(tempFilename, filename);
return true;
}
@ -360,16 +351,7 @@ bool NodeUtility::UpdateConfiguration(const String& value, bool include, bool re
ifp.close();
ofp.close();
#ifdef _WIN32
_unlink(configurationFile.CStr());
#endif /* _WIN32 */
if (rename(tempFile.CStr(), configurationFile.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(configurationFile));
}
Utility::RenameFile(tempFile, configurationFile);
return (found || include);
}
@ -404,14 +386,5 @@ void NodeUtility::UpdateConstant(const String& name, const String& value)
ifp.close();
ofp.close();
#ifdef _WIN32
_unlink(constantsConfPath.CStr());
#endif /* _WIN32 */
if (rename(tempFile.CStr(), constantsConfPath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(constantsConfPath));
}
Utility::RenameFile(tempFile, constantsConfPath);
}

View File

@ -470,16 +470,7 @@ wizard_ticket:
fp.close();
#ifdef _WIN32
_unlink(apiConfPath.CStr());
#endif /* _WIN32 */
if (rename(tempApiConfPath.CStr(), apiConfPath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempApiConfPath));
}
Utility::RenameFile(tempApiConfPath, apiConfPath);
/* Zones configuration. */
Log(LogInformation, "cli", "Generating local zones.conf.");
@ -578,16 +569,7 @@ wizard_global_zone_loop_start:
fp.close();
#ifdef _WIN32
_unlink(ticketPath.CStr());
#endif /* _WIN32 */
if (rename(tempTicketPath.CStr(), ticketPath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempTicketPath));
}
Utility::RenameFile(tempTicketPath, ticketPath);
}
/* If no parent connection was made, the user must supply the ca.crt before restarting Icinga 2.*/
@ -782,16 +764,7 @@ wizard_global_zone_loop_start:
fp.close();
#ifdef _WIN32
_unlink(apiConfPath.CStr());
#endif /* _WIN32 */
if (rename(tempApiConfPath.CStr(), apiConfPath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempApiConfPath));
}
Utility::RenameFile(tempApiConfPath, apiConfPath);
/* update constants.conf with NodeName = CN + TicketSalt = random value */
if (cn != Utility::GetFQDN()) {

View File

@ -763,16 +763,7 @@ void StatusDataWriter::UpdateObjectsCache()
objectfp.close();
#ifdef _WIN32
_unlink(objectsPath.CStr());
#endif /* _WIN32 */
if (rename(tempObjectsPath.CStr(), objectsPath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempObjectsPath));
}
Utility::RenameFile(tempObjectsPath, objectsPath);
}
/**
@ -845,16 +836,7 @@ void StatusDataWriter::StatusTimerHandler()
statusfp.close();
#ifdef _WIN32
_unlink(statusPath.CStr());
#endif /* _WIN32 */
if (rename(tempStatusPath.CStr(), statusPath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempStatusPath));
}
Utility::RenameFile(tempStatusPath, statusPath);
Log(LogNotice, "StatusDataWriter")
<< "Writing status.dat file took " << Utility::FormatDuration(Utility::GetTime() - start);

View File

@ -62,15 +62,6 @@ void ConfigCompilerContext::FinishObjectsFile()
delete m_ObjectsFP;
m_ObjectsFP = nullptr;
#ifdef _WIN32
_unlink(m_ObjectsPath.CStr());
#endif /* _WIN32 */
if (rename(m_ObjectsTempFile.CStr(), m_ObjectsPath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(m_ObjectsTempFile));
}
Utility::RenameFile(m_ObjectsTempFile, m_ObjectsPath);
}

View File

@ -178,16 +178,7 @@ void IcingaApplication::DumpModifiedAttributes()
fp.close();
#ifdef _WIN32
_unlink(path.CStr());
#endif /* _WIN32 */
if (rename(tempFilename.CStr(), path.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempFilename));
}
Utility::RenameFile(tempFilename, path);
}
IcingaApplication::Ptr IcingaApplication::GetInstance()

View File

@ -155,12 +155,7 @@ void PerfdataWriter::RotateFile(std::ofstream& output, const String& temp_path,
Log(LogDebug, "PerfdataWriter")
<< "Closed output file and renaming into '" << finalFile << "'.";
if (rename(temp_path.CStr(), finalFile.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(temp_path));
}
Utility::RenameFile(temp_path, finalFile);
}
}

View File

@ -334,16 +334,7 @@ Value UpdateCertificateHandler(const MessageOrigin::Ptr& origin, const Dictionar
cafp << ca;
cafp.close();
#ifdef _WIN32
_unlink(caPath.CStr());
#endif /* _WIN32 */
if (rename(tempCaPath.CStr(), caPath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempCaPath));
}
Utility::RenameFile(tempCaPath, caPath);
/* Update signed certificate. */
String certPath = listener->GetDefaultCertPath();
@ -356,16 +347,7 @@ Value UpdateCertificateHandler(const MessageOrigin::Ptr& origin, const Dictionar
certfp << cert;
certfp.close();
#ifdef _WIN32
_unlink(certPath.CStr());
#endif /* _WIN32 */
if (rename(tempCertPath.CStr(), certPath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempCertPath));
}
Utility::RenameFile(tempCertPath, certPath);
/* Remove ticket for successful signing request. */
String ticketPath = ApiListener::GetCertsDir() + "/ticket";