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.
This commit is contained in:
Joel Rosdahl 2010-05-09 17:50:20 +02:00
parent f12d64243f
commit c609b16cc0
3 changed files with 19 additions and 2 deletions

View File

@ -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();

View File

@ -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);

14
util.c
View File

@ -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)
{