From: Joel Rosdahl Date: Wed, 21 Jul 2010 13:15:42 +0000 (+0200) Subject: Introduce x_rename(), which unlink()s destination if necessary before rename() X-Git-Tag: v3.1~135 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0ea9d9e0a379db26417f4d55e807d2a933208f2b;p=thirdparty%2Fccache.git Introduce x_rename(), which unlink()s destination if necessary before rename() --- diff --git a/ccache.c b/ccache.c index a1ca78cd0..c0200462a 100644 --- 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(); } diff --git a/ccache.h b/ccache.h index f07f3a3b1..5b8d287d9 100644 --- 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 */ diff --git a/manifest.c b/manifest.c index 4e8b949ae..4518fab8c 100644 --- a/manifest.c +++ b/manifest.c @@ -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 e9946eaa5..2455748b5 100644 --- 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); +}