]> granicus.if.org Git - icinga2/commitdiff
Replace unlink() with boost::filesystem::remove() 7102/head
authorAlexander A. Klimov <alexander.klimov@icinga.com>
Wed, 10 Apr 2019 12:16:39 +0000 (14:16 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Thu, 25 Apr 2019 07:53:02 +0000 (09:53 +0200)
refs #7101

lib/base/utility.cpp
lib/base/utility.hpp
lib/compat/checkresultreader.cpp
lib/compat/externalcommandlistener.cpp
lib/remote/apilistener.cpp
lib/remote/configobjectutility.cpp
lib/remote/jsonrpcconnection-pki.cpp

index 98a896501291000a18fe61c23bc2504a830f8e79..c946a0d44949022fd8e3241b0232bb25fef0792a 100644 (file)
@@ -702,6 +702,13 @@ void Utility::MkDirP(const String& path, int mode)
        }
 }
 
+void Utility::Remove(const String& path)
+{
+       namespace fs = boost::filesystem;
+
+       (void)fs::remove(fs::path(path.Begin(), path.End()));
+}
+
 void Utility::RemoveDirRecursive(const String& path)
 {
        namespace fs = boost::filesystem;
index 73f8f2dd769176a1491d321938887382cb9e6f45..2c1b0cdc3a81fe2b8a692c7f4bf56114584c74c4 100644 (file)
@@ -111,6 +111,7 @@ public:
 
        static bool PathExists(const String& path);
 
+       static void Remove(const String& path);
        static void RemoveDirRecursive(const String& path);
        static void CopyFile(const String& source, const String& target);
        static void RenameFile(const String& source, const String& target);
index 42087f58c5083f607f51216040b5060cf1a95f0b..2b3f05ccddd6d4006ed4520b1df99d5c90ae0be8 100644 (file)
@@ -107,17 +107,9 @@ void CheckResultReader::ProcessCheckResultFile(const String& path) const
        }
 
        /* Remove the checkresult files. */
-       if (unlink(path.CStr()) < 0)
-               BOOST_THROW_EXCEPTION(posix_error()
-                       << boost::errinfo_api_function("unlink")
-                       << boost::errinfo_errno(errno)
-                       << boost::errinfo_file_name(path));
-
-       if (unlink(crfile.CStr()) < 0)
-               BOOST_THROW_EXCEPTION(posix_error()
-                       << boost::errinfo_api_function("unlink")
-                       << boost::errinfo_errno(errno)
-                       << boost::errinfo_file_name(crfile));
+       Utility::Remove(path);
+
+       Utility::Remove(crfile);
 
        Checkable::Ptr checkable;
 
index f6479d6f25f80b748b964634a1d7b3dc3d48b86a..d5bdd63b5d5ff8f97c659e419d1b7395953dcb94 100644 (file)
@@ -67,12 +67,7 @@ void ExternalCommandListener::CommandPipeThread(const String& commandPath)
                if (S_ISFIFO(statbuf.st_mode) && access(commandPath.CStr(), R_OK) >= 0) {
                        fifo_ok = true;
                } else {
-                       if (unlink(commandPath.CStr()) < 0) {
-                               BOOST_THROW_EXCEPTION(posix_error()
-                                       << boost::errinfo_api_function("unlink")
-                                       << boost::errinfo_errno(errno)
-                                       << boost::errinfo_file_name(commandPath));
-                       }
+                       Utility::Remove(commandPath);
                }
        }
 
index 2ef4013d15afacac11bd5738323ec8824ba2b868..39d82ef2f033fa92a6164e525fc6458597a7c885 100644 (file)
@@ -1605,12 +1605,5 @@ void ApiListener::RemoveStatusFile()
 {
        String path = Configuration::CacheDir + "/api-state.json";
 
-       if (Utility::PathExists(path)) {
-               if (unlink(path.CStr()) < 0 && errno != ENOENT) {
-                       BOOST_THROW_EXCEPTION(posix_error()
-                               << boost::errinfo_api_function("unlink")
-                               << boost::errinfo_errno(errno)
-                               << boost::errinfo_file_name(path));
-               }
-       }
+       Utility::Remove(path);
 }
index d16f887a7604f1b04ecf4a3b638415511bc006d0..d71f5e47348c560dd2a345b9d5ad9fb5b1a4f3a1 100644 (file)
@@ -124,12 +124,7 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full
                /* Disable logging for object creation, but do so ourselves later on. */
                if (!ConfigItem::CommitItems(ascope.GetContext(), upq, newItems, true) || !ConfigItem::ActivateItems(upq, newItems, true, true)) {
                        if (errors) {
-                               if (unlink(path.CStr()) < 0 && errno != ENOENT) {
-                                       BOOST_THROW_EXCEPTION(posix_error()
-                                               << boost::errinfo_api_function("unlink")
-                                               << boost::errinfo_errno(errno)
-                                               << boost::errinfo_file_name(path));
-                               }
+                               Utility::Remove(path);
 
                                for (const boost::exception_ptr& ex : upq.GetExceptions()) {
                                        errors->Add(DiagnosticInformation(ex, false));
@@ -154,12 +149,7 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full
                        << "Created and activated object '" << fullName << "' of type '" << type->GetName() << "'.";
 
        } catch (const std::exception& ex) {
-               if (unlink(path.CStr()) < 0 && errno != ENOENT) {
-                       BOOST_THROW_EXCEPTION(posix_error()
-                               << boost::errinfo_api_function("unlink")
-                               << boost::errinfo_errno(errno)
-                               << boost::errinfo_file_name(path));
-               }
+               Utility::Remove(path);
 
                if (errors)
                        errors->Add(DiagnosticInformation(ex, false));
@@ -226,14 +216,7 @@ bool ConfigObjectUtility::DeleteObjectHelper(const ConfigObject::Ptr& object, bo
 
        String path = GetObjectConfigPath(object->GetReflectionType(), name);
 
-       if (Utility::PathExists(path)) {
-               if (unlink(path.CStr()) < 0 && errno != ENOENT) {
-                       BOOST_THROW_EXCEPTION(posix_error()
-                               << boost::errinfo_api_function("unlink")
-                               << boost::errinfo_errno(errno)
-                               << boost::errinfo_file_name(path));
-               }
-       }
+       Utility::Remove(path);
 
        return true;
 }
index e7897e8504b0710d95b12f8831e72b0911e10e50..9b537d13864a44b7b8527c39b37e250c3476366d 100644 (file)
@@ -352,12 +352,7 @@ Value UpdateCertificateHandler(const MessageOrigin::Ptr& origin, const Dictionar
        /* Remove ticket for successful signing request. */
        String ticketPath = ApiListener::GetCertsDir() + "/ticket";
 
-       if (unlink(ticketPath.CStr()) < 0 && errno != ENOENT) {
-               BOOST_THROW_EXCEPTION(posix_error()
-                       << boost::errinfo_api_function("unlink")
-                       << boost::errinfo_errno(errno)
-                       << boost::errinfo_file_name(ticketPath));
-       }
+       Utility::Remove(ticketPath);
 
        /* Update the certificates at runtime and reconnect all endpoints. */
        Log(LogInformation, "JsonRpcConnection")