From c609b16cc0896ae0ee7f8e1b5eb60f07264204e8 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sun, 9 May 2010 17:50:20 +0200 Subject: [PATCH] Rename temporary object file to cached object file if possible This avoids copying the object file unnecessarily in the common case when we store object files uncompressed in the cache. --- ccache.c | 5 +++-- ccache.h | 2 ++ util.c | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/ccache.c b/ccache.c index b2a55c2..fe22981 100644 --- a/ccache.c +++ b/ccache.c @@ -594,7 +594,8 @@ static void to_cache(ARGS *args) failed(); } if (st.st_size > 0) { - if (move_file(tmp_stderr, cached_stderr, compress) != 0) { + if (move_uncompressed_file(tmp_stderr, cached_stderr, + compress) != 0) { cc_log("Failed to move %s to %s", tmp_stderr, cached_stderr); stats_update(STATS_ERROR); @@ -604,7 +605,7 @@ static void to_cache(ARGS *args) } else { unlink(tmp_stderr); } - if (move_file(tmp_obj, cached_obj, compress) != 0) { + if (move_uncompressed_file(tmp_obj, cached_obj, compress) != 0) { cc_log("Failed to move %s to %s", tmp_obj, cached_obj); stats_update(STATS_ERROR); failed(); diff --git a/ccache.h b/ccache.h index 2277344..9aae3ff 100644 --- a/ccache.h +++ b/ccache.h @@ -70,6 +70,8 @@ void fatal(const char *format, ...) ATTR_FORMAT(printf, 1, 2); void copy_fd(int fd_in, int fd_out); int copy_file(const char *src, const char *dest, int compress_dest); int move_file(const char *src, const char *dest, int compress_dest); +int move_uncompressed_file(const char *src, const char *dest, + int compress_dest); int test_if_compressed(const char *filename); int create_dir(const char *dir); diff --git a/util.c b/util.c index 7ddd505..4ac7109 100644 --- a/util.c +++ b/util.c @@ -259,6 +259,20 @@ int move_file(const char *src, const char *dest, int compress_dest) return ret; } +/* + * Like move_file(), but assumes that src is uncompressed and that src and dest + * are on the same file system. + */ +int +move_uncompressed_file(const char *src, const char *dest, int compress_dest) +{ + if (compress_dest) { + return move_file(src, dest, compress_dest); + } else { + return rename(src, dest); + } +} + /* test if a file is zlib compressed */ int test_if_compressed(const char *filename) {