From 5afef1015d60412fd48f2e0b7259bf5a6cf380ce Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 10 Apr 2019 14:16:39 +0200 Subject: [PATCH] Replace unlink() with boost::filesystem::remove() refs #7101 --- lib/base/utility.cpp | 7 +++++++ lib/base/utility.hpp | 1 + lib/compat/checkresultreader.cpp | 14 +++----------- lib/compat/externalcommandlistener.cpp | 7 +------ lib/remote/apilistener.cpp | 9 +-------- lib/remote/configobjectutility.cpp | 23 +++-------------------- lib/remote/jsonrpcconnection-pki.cpp | 7 +------ 7 files changed, 17 insertions(+), 51 deletions(-) diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index 98a896501..c946a0d44 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -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; diff --git a/lib/base/utility.hpp b/lib/base/utility.hpp index 73f8f2dd7..2c1b0cdc3 100644 --- a/lib/base/utility.hpp +++ b/lib/base/utility.hpp @@ -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); diff --git a/lib/compat/checkresultreader.cpp b/lib/compat/checkresultreader.cpp index 42087f58c..2b3f05ccd 100644 --- a/lib/compat/checkresultreader.cpp +++ b/lib/compat/checkresultreader.cpp @@ -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; diff --git a/lib/compat/externalcommandlistener.cpp b/lib/compat/externalcommandlistener.cpp index f6479d6f2..d5bdd63b5 100644 --- a/lib/compat/externalcommandlistener.cpp +++ b/lib/compat/externalcommandlistener.cpp @@ -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); } } diff --git a/lib/remote/apilistener.cpp b/lib/remote/apilistener.cpp index 2ef4013d1..39d82ef2f 100644 --- a/lib/remote/apilistener.cpp +++ b/lib/remote/apilistener.cpp @@ -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); } diff --git a/lib/remote/configobjectutility.cpp b/lib/remote/configobjectutility.cpp index d16f887a7..d71f5e473 100644 --- a/lib/remote/configobjectutility.cpp +++ b/lib/remote/configobjectutility.cpp @@ -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; } diff --git a/lib/remote/jsonrpcconnection-pki.cpp b/lib/remote/jsonrpcconnection-pki.cpp index e7897e850..9b537d138 100644 --- a/lib/remote/jsonrpcconnection-pki.cpp +++ b/lib/remote/jsonrpcconnection-pki.cpp @@ -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") -- 2.40.0