]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Introduce x_rename(), which unlink()s destination if necessary before rename()
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 21 Jul 2010 13:15:42 +0000 (15:15 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 21 Jul 2010 13:15:42 +0000 (15:15 +0200)
ccache.c
ccache.h
manifest.c
util.c

index a1ca78cd0f537ba29d0dcd1cc60cd583bb144d57..c0200462ae0eaa5d445e45ae55fc6351f67611fa 100644 (file)
--- a/ccache.c
+++ b/ccache.c
@@ -634,8 +634,7 @@ static void to_cache(struct args *args)
                char *tmp_stderr2;
 
                tmp_stderr2 = format("%s.tmp.stderr2.%s", cached_obj, tmp_string());
-               unlink(tmp_stderr2);
-               if (rename(tmp_stderr, tmp_stderr2)) {
+               if (x_rename(tmp_stderr, tmp_stderr2)) {
                        cc_log("Failed to rename %s to %s", tmp_stderr, tmp_stderr2);
                        failed();
                }
index f07f3a3b13e8e17a6867a11056c90ef56155aff0..5b8d287d9a7f7c45f96d0a94daf68cc3fe2c48fc 100644 (file)
--- a/ccache.h
+++ b/ccache.h
@@ -119,6 +119,7 @@ int is_full_path(const char *path);
 void update_mtime(const char *path);
 void *x_fmmap(const char *fname, off_t *size, const char *errstr);
 int x_munmap(void *addr, size_t length);
+int x_rename(const char *oldpath, const char *newpath);
 
 /* ------------------------------------------------------------------------- */
 /* stats.c */
index 4e8b949ae1b5dd41310480c978dcb34e851c1ae3..4518fab8c223403b6757d0bb5c17bc8f7ad40f1f 100644 (file)
@@ -682,8 +682,7 @@ int manifest_put(const char *manifest_path, struct file_hash *object_hash,
        if (write_manifest(f2, mf)) {
                gzclose(f2);
                f2 = NULL;
-               unlink(manifest_path);
-               if (rename(tmp_file, manifest_path) == 0) {
+               if (x_rename(tmp_file, manifest_path) == 0) {
                        ret = 1;
                } else {
                        cc_log("Failed to rename %s to %s",
diff --git a/util.c b/util.c
index e9946eaa5a441ce0ba6bac58963149d9fc4d0827..2455748b5a2c9983af6451a80d2d0ac5fa18fde1 100644 (file)
--- a/util.c
+++ b/util.c
@@ -299,9 +299,7 @@ int copy_file(const char *src, const char *dest, int compress_dest)
                goto error;
        }
 
-       unlink(dest);
-
-       if (rename(tmp_name, dest) == -1) {
+       if (x_rename(tmp_name, dest) == -1) {
                cc_log("rename error: %s", strerror(errno));
                goto error;
        }
@@ -347,8 +345,7 @@ move_uncompressed_file(const char *src, const char *dest, int compress_dest)
        if (compress_dest) {
                return move_file(src, dest, compress_dest);
        } else {
-               unlink(dest);
-               return rename(src, dest);
+               return x_rename(src, dest);
        }
 }
 
@@ -1105,3 +1102,15 @@ int x_munmap(void *addr, size_t length)
        return munmap(addr, length);
 #endif
 }
+
+/*
+ * Rename oldpath to newpath (deleting newpath).
+ */
+int x_rename(const char *oldpath, const char *newpath)
+{
+#ifdef _WIN32
+       /* Windows' rename() refuses to overwrite an existing file. */
+       unlink(newpath);
+#endif
+       return rename(oldpath, newpath);
+}