throw Error(
fmt::format("failed to write data to {}: {}", m_path, strerror(errno)));
}
- if (x_rename(m_tmp_path.c_str(), m_path.c_str()) != 0) {
- throw Error(fmt::format("failed to rename {} to {}", m_tmp_path, m_path));
- }
+ Util::rename(m_tmp_path, m_path);
}
# include <sys/param.h>
#endif
+#ifdef _WIN32
+# include "Win32Util.hpp"
+#endif
+
#ifdef __linux__
# ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
dest_fd.close();
src_fd.close();
- if (via_tmp_file && x_rename(tmp_file.c_str(), dest.c_str()) != 0) {
- throw Error(strerror(errno));
+ if (via_tmp_file) {
+ Util::rename(tmp_file, dest);
}
# elif defined(__APPLE__)
(void)via_tmp_file;
dest_fd.close();
src_fd.close();
- if (via_tmp_file && x_rename(tmp_file.c_str(), dest.c_str()) != 0) {
- throw Error(strerror(errno));
+ if (via_tmp_file) {
+ Util::rename(tmp_file, dest);
}
}
return path.substr(0, path.length() - get_extension(path).length());
}
+void
+rename(const std::string& oldpath, const std::string& newpath)
+{
+#ifndef _WIN32
+ if (::rename(oldpath.c_str(), newpath.c_str()) != 0) {
+ throw Error(fmt::format(
+ "failed to rename {} to {}: {}", oldpath, newpath, strerror(errno)));
+ }
+#else
+ // Windows' rename() won't overwrite an existing file, so need to use
+ // MoveFileEx instead.
+ if (!MoveFileExA(
+ oldpath.c_str(), newpath.c_str(), MOVEFILE_REPLACE_EXISTING)) {
+ DWORD error = GetLastError();
+ throw Error(fmt::format("failed to rename {} to {}: {}",
+ oldpath,
+ newpath,
+ Win32Util::error_message(error)));
+ }
+#endif
+}
+
bool
same_program_name(const std::string& program_name,
const std::string& canonical_program_name)
std::string tmp_name = path + ".ccache.rm.tmp";
bool success = true;
- if (x_rename(path.c_str(), tmp_name.c_str()) != 0) {
+ try {
+ Util::rename(path, tmp_name);
+ } catch (Error&) {
success = false;
saved_errno = errno;
- } else if (unlink(tmp_name.c_str()) != 0) {
+ }
+ if (success && unlink(tmp_name.c_str()) != 0) {
// It's OK if it was unlinked in a race.
if (errno != ENOENT && errno != ESTALE) {
success = false;
saved_errno = errno;
}
}
-
if (success || unlink_log == UnlinkLog::log_failure) {
cc_log("Unlink %s via %s", path.c_str(), tmp_name.c_str());
if (!success) {
// extension as determined by `get_extension()`.
nonstd::string_view remove_extension(nonstd::string_view path);
+// Rename `oldpath` to `newpath` (deleting `newpath`). Throws `Error` on error.
+void rename(const std::string& oldpath, const std::string& newpath);
+
// Detmine if `program_name` is equal to `canonical_program_name`. On Windows,
// this means performing a case insensitive equality check with and without a
// ".exe" suffix. On non-Windows, it is a simple equality check.
}
std::string tmp_file = output_dep + ".tmp";
- try {
- Util::write_file(tmp_file, adjusted_file_content);
- } catch (const Error& e) {
- cc_log(
- "Error writing temporary dependency file %s (%s), skip relative path"
- " usage",
- tmp_file.c_str(),
- e.what());
- Util::unlink_safe(tmp_file);
- return;
- }
-
- if (x_rename(tmp_file.c_str(), output_dep.c_str()) != 0) {
- cc_log(
- "Error renaming dependency file: %s -> %s (%s), skip relative path usage",
- tmp_file.c_str(),
- output_dep.c_str(),
- strerror(errno));
- Util::unlink_safe(tmp_file);
- } else {
- cc_log("Renamed dependency file: %s -> %s",
- tmp_file.c_str(),
- output_dep.c_str());
- }
+ Util::write_file(tmp_file, adjusted_file_content);
+ Util::rename(tmp_file, output_dep);
}
// Extract the used includes from the dependency file. Note that we cannot
// thing correctly
ctx.i_tmpfile =
fmt::format("{}.{}", stdout_path, ctx.config.cpp_extension());
- x_rename(stdout_path.c_str(), ctx.i_tmpfile.c_str());
+ Util::rename(stdout_path, ctx.i_tmpfile);
ctx.register_pending_tmp_file(ctx.i_tmpfile);
}
# include <sys/time.h>
#endif
-// Rename oldpath to newpath (deleting newpath).
-int
-x_rename(const char* oldpath, const char* newpath)
-{
-#ifndef _WIN32
- return rename(oldpath, newpath);
-#else
- // Windows' rename() refuses to overwrite an existing file.
- // If the function succeeds, the return value is nonzero.
- if (MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING) == 0) {
- DWORD error = GetLastError();
- cc_log("failed to rename %s to %s: %s (%lu)",
- oldpath,
- newpath,
- Win32Util::error_message(error).c_str(),
- error);
- return -1;
- } else {
- return 0;
- }
-#endif
-}
-
void
set_cloexec_flag(int fd)
{
#include <string>
-int x_rename(const char* oldpath, const char* newpath);
void set_cloexec_flag(int fd);
double time_seconds();