]> granicus.if.org Git - icinga2/commitdiff
Replace _unlink() + rename() with boost::filesystem::rename()
authorAlexander A. Klimov <alexander.klimov@icinga.com>
Wed, 10 Apr 2019 11:44:13 +0000 (13:44 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Thu, 25 Apr 2019 07:53:02 +0000 (09:53 +0200)
refs #7101

13 files changed:
lib/base/configobject.cpp
lib/base/scriptglobal.cpp
lib/base/utility.cpp
lib/base/utility.hpp
lib/cli/apisetuputility.cpp
lib/cli/nodesetupcommand.cpp
lib/cli/nodeutility.cpp
lib/cli/nodewizardcommand.cpp
lib/compat/statusdatawriter.cpp
lib/config/configcompilercontext.cpp
lib/icinga/icingaapplication.cpp
lib/perfdata/perfdatawriter.cpp
lib/remote/jsonrpcconnection-pki.cpp

index f60c52325dd27f8fe43829779e609654b9453520..e070796589f91ce8c99892550e1b7276b186738d 100644 (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)
index d4ac81e9ffa027d6153de7d1a687f8feef3bd5f6..13c21a7824c5f7d7328c6ea286b23d8bd296d3cd 100644 (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);
 }
 
index 46f0a4bce7e81bb65b5e7933c187085ac2e56067..98a896501291000a18fe61c23bc2504a830f8e79 100644 (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)
index 5b5de46fc2ccf611ee601670e05c334a368b53ec..73f8f2dd769176a1491d321938887382cb9e6f45 100644 (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);
index cee4e1ee3ddc1518b50f5c80d553e3a9660c5ba5..b2107e5fabea037f106eb70c08a00c3489d91e4a 100644 (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;
 }
index cd588a95ccd0b83423d55cd9224a067e82a7b35e..20cb512cdbd7668e2cb03248e81cff979ce17e46 100644 (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.*/
index da9167c36bdd8aed1176e14995e4d53536792165..bd343ab4c54959d3d8d6af879e51762331ebd041 100644 (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);
 }
index 3a6719ae665a5a0afdb011af7d47b698e9bdbf8d..07e7314eed676bebd5beada2a5d6cf50e5634267 100644 (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()) {
index 577722ab6ce734c008d4b8cb29412efca014beb9..a75763c62574b64a7da16a916f5561eac8e580a5 100644 (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);
index 4a04b258b8c89bd3af312fb3d80acb9205ac915a..03752b068f9f7f7681e49268d43641f5dcbb36ad 100644 (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);
 }
 
index d9518abc887247eb83e654bdba5e180aa2869517..f8c283a0d1db9c42dda3267ff1a34e4489425b09 100644 (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()
index 78d44cff1693cfc7cc54effda1de34e6ff74b908..91a220612553739419be620962bee655da830dc9 100644 (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);
                }
        }
 
index 2f66eb7b555b309705bb360872a690732776475c..e7897e8504b0710d95b12f8831e72b0911e10e50 100644 (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";