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)
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);
}
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
*/
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)
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);
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;
}
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()) {
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.");
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.*/
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;
}
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);
}
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);
}
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.");
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.*/
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()) {
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);
}
/**
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);
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);
}
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()
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);
}
}
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();
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";