From: Joel Rosdahl Date: Mon, 9 Jun 2014 20:49:17 +0000 (+0200) Subject: Refactor code for putting/getting files to/from the cache into functions X-Git-Tag: v3.2~22 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3f27086fab0d38bd838a8380a6df276d5702f313;p=thirdparty%2Fccache.git Refactor code for putting/getting files to/from the cache into functions --- diff --git a/ccache.c b/ccache.c index 8f0912864..e583329d2 100644 --- a/ccache.c +++ b/ccache.c @@ -659,6 +659,71 @@ process_preprocessed_file(struct mdfour *hash, const char *path) return true; } +/* Copy or link a file to the cache. */ +static void +put_file_in_cache(const char *source, const char *dest) +{ + int ret; + struct stat st; + bool do_link = conf->hard_link && !conf->compression; + + if (do_link) { + x_unlink(dest); + ret = link(source, dest); + } else { + ret = copy_file(source, dest, conf->compression); + } + if (ret != 0) { + cc_log("Failed to %s %s to %s: %s", + do_link ? "link" : "copy", + source, + dest, + strerror(errno)); + stats_update(STATS_ERROR); + failed(); + } + cc_log("Stored in cache: %s -> %s", source, dest); + if (stat(dest, &st) != 0) { + cc_log("Failed to stat %s: %s", dest, strerror(errno)); + stats_update(STATS_ERROR); + failed(); + } + stats_update_size(file_size(&st), 1); +} + +/* Copy or link a file from the cache. */ +static void +get_file_from_cache(const char *source, const char *dest) +{ + int ret; + bool do_link = conf->hard_link && !file_is_compressed(source); + + if (do_link) { + x_unlink(dest); + ret = link(source, dest); + } else { + ret = copy_file(source, dest, 0); + } + + if (ret == -1) { + if (errno == ENOENT) { + /* Someone removed the file just before we began copying? */ + cc_log("Cache file %s just disappeared from cache", source); + stats_update(STATS_MISSING); + } else { + cc_log("Failed to %s %s to %s: %s", + do_link ? "link" : "copy", + source, + dest, + strerror(errno)); + stats_update(STATS_ERROR); + } + failed(); + } + + cc_log("Created from cache: %s -> %s", source, dest); +} + /* run the real compiler and put the result in cache */ static void to_cache(struct args *args) @@ -666,8 +731,6 @@ to_cache(struct args *args) char *tmp_stdout, *tmp_stderr, *tmp_dia; struct stat st; int status; - size_t added_bytes = 0; - unsigned added_files = 0; if (create_parent_dirs(cached_obj) != 0) { fatal("Failed to create parent directory for %s: %s", @@ -840,8 +903,7 @@ to_cache(struct args *args) if (conf->compression) { stat(cached_stderr, &st); } - added_bytes += file_size(&st); - added_files += 1; + stats_update_size(file_size(&st), 1); } else { tmp_unlink(tmp_stderr); if (conf->recache) { @@ -857,54 +919,12 @@ to_cache(struct args *args) failed(); } if (st.st_size > 0) { - int ret; - if (conf->hard_link && !conf->compression) { - ret = link(tmp_dia, cached_dia); - } else { - ret = copy_file(tmp_dia, cached_dia, conf->compression); - } - if (ret != 0) { - cc_log("Failed to copy/link %s to %s: %s", - tmp_dia, cached_dia, strerror(errno)); - stats_update(STATS_ERROR); - failed(); - } - cc_log("Stored in cache: %s", cached_dia); - stat(cached_dia, &st); - added_bytes += file_size(&st); - added_files += 1; + put_file_in_cache(tmp_dia, cached_dia); } } - int ret; - if (conf->hard_link && !conf->compression) { - x_unlink(cached_obj); - ret = link(output_obj, cached_obj); - } else { - ret = copy_file(output_obj, cached_obj, conf->compression); - } - if (ret != 0) { - cc_log("Failed to copy/link %s to %s: %s", - output_obj, cached_obj, strerror(errno)); - stats_update(STATS_ERROR); - failed(); - } - cc_log("Stored in cache: %s", cached_obj); - stat(cached_obj, &st); - added_bytes += file_size(&st); - added_files += 1; - - /* - * Do an extra stat on the potentially compressed object file for the - * size statistics. - */ - if (stat(cached_obj, &st) != 0) { - cc_log("Failed to stat %s: %s", cached_obj, strerror(errno)); - stats_update(STATS_ERROR); - failed(); - } - - stats_update_size(STATS_TOCACHE, added_bytes, added_files); + put_file_in_cache(output_obj, cached_obj); + stats_update(STATS_TOCACHE); /* Make sure we have a CACHEDIR.TAG * This can be almost anywhere, but might as well do it near the end @@ -1371,7 +1391,6 @@ static void from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest) { int fd_stderr; - int ret; struct stat st; bool produce_dep_file; @@ -1414,110 +1433,16 @@ from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest) return; } - if (str_eq(output_obj, "/dev/null")) { - ret = 0; - } else { - x_unlink(output_obj); - /* only make a hardlink if the cache file is uncompressed */ - if (conf->hard_link && !file_is_compressed(cached_obj)) { - ret = link(cached_obj, output_obj); - } else { - ret = copy_file(cached_obj, output_obj, 0); - } - } - - if (ret == -1) { - if (errno == ENOENT) { - /* Someone removed the file just before we began copying? */ - cc_log("Object file %s just disappeared from cache", cached_obj); - stats_update(STATS_MISSING); - } else { - cc_log("Failed to copy/link %s to %s: %s", - cached_obj, output_obj, strerror(errno)); - stats_update(STATS_ERROR); - failed(); - } - x_unlink(output_obj); - x_unlink(cached_stderr); - x_unlink(cached_obj); - x_unlink(cached_dep); - x_unlink(cached_dia); - return; - } else { - cc_log("Created %s from %s", output_obj, cached_obj); + if (!str_eq(output_obj, "/dev/null")) { + get_file_from_cache(cached_obj, output_obj); } - if (produce_dep_file) { - x_unlink(output_dep); - /* only make a hardlink if the cache file is uncompressed */ - if (conf->hard_link && !file_is_compressed(cached_dep)) { - ret = link(cached_dep, output_dep); - } else { - ret = copy_file(cached_dep, output_dep, 0); - } - if (ret == -1) { - if (errno == ENOENT) { - /* - * Someone removed the file just before we - * began copying? - */ - cc_log("Dependency file %s just disappeared from cache", output_obj); - stats_update(STATS_MISSING); - } else { - cc_log("Failed to copy/link %s to %s: %s", - cached_dep, output_dep, strerror(errno)); - stats_update(STATS_ERROR); - failed(); - } - x_unlink(output_obj); - x_unlink(output_dep); - x_unlink(cached_stderr); - x_unlink(cached_obj); - x_unlink(cached_dep); - x_unlink(cached_dia); - return; - } else { - cc_log("Created %s from %s", output_dep, cached_dep); - } + get_file_from_cache(cached_dep, output_dep); } - if (output_dia) { - x_unlink(output_dia); - /* only make a hardlink if the cache file is uncompressed */ - if (conf->hard_link && !file_is_compressed(cached_dia)) { - ret = link(cached_dia, output_dia); - } else { - ret = copy_file(cached_dia, output_dia, 0); - } - - if (ret == -1) { - if (errno == ENOENT) { - /* - * Someone removed the file just before we - * began copying? - */ - cc_log("Diagnostic file %s just disappeared from cache", output_dia); - stats_update(STATS_MISSING); - } else { - cc_log("Failed to copy/link %s to %s: %s", - cached_dia, output_dia, strerror(errno)); - stats_update(STATS_ERROR); - failed(); - } - x_unlink(output_obj); - x_unlink(output_dep); - x_unlink(output_dia); - x_unlink(cached_stderr); - x_unlink(cached_obj); - x_unlink(cached_dep); - x_unlink(cached_dia); - return; - } else { - cc_log("Created %s from %s", output_dia, cached_dia); - } + get_file_from_cache(cached_dia, output_dia); } - /* Update modification timestamps to save files from LRU cleanup. Also gives files a sensible mtime when hard-linking. */ update_mtime(cached_obj); @@ -1530,17 +1455,7 @@ from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest) } if (generating_dependencies && mode != FROMCACHE_DIRECT_MODE) { - /* Store the dependency file in the cache. */ - ret = copy_file(output_dep, cached_dep, conf->compression); - if (ret == -1) { - cc_log("Failed to copy %s to %s: %s", output_dep, cached_dep, - strerror(errno)); - /* Continue despite the error. */ - } else { - cc_log("Stored in cache: %s", cached_dep); - stat(cached_dep, &st); - stats_update_size(STATS_NONE, file_size(&st), 1); - } + put_file_in_cache(output_dep, cached_dep); } /* Send the stderr, if any. */ @@ -1564,9 +1479,7 @@ from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest) cc_log("Added object file hash to %s", manifest_path); update_mtime(manifest_path); stat(manifest_path, &st); - stats_update_size(STATS_NONE, - (file_size(&st) - old_size), - old_size == 0 ? 1 : 0); + stats_update_size(file_size(&st) - old_size, old_size == 0 ? 1 : 0); } else { cc_log("Failed to add object file hash to %s", manifest_path); } diff --git a/ccache.h b/ccache.h index 73f69b907..6f60f1a35 100644 --- a/ccache.h +++ b/ccache.h @@ -178,7 +178,7 @@ void stats_flush(void); unsigned stats_get_pending(enum stats stat); void stats_zero(void); void stats_summary(struct conf *conf); -void stats_update_size(enum stats stat, uint64_t size, unsigned files); +void stats_update_size(uint64_t size, unsigned files); void stats_get_obsolete_limits(const char *dir, unsigned *maxfiles, uint64_t *maxsize); void stats_set_sizes(const char *dir, size_t num_files, size_t total_size); diff --git a/stats.c b/stats.c index c4321086a..5754db7ac 100644 --- a/stats.c +++ b/stats.c @@ -163,16 +163,13 @@ init_counter_updates(void) } /* - * Update a statistics counter (unless it's STATS_NONE) and also record that a - * number of bytes and files have been added to the cache. Size is in KiB. + * Record that a number of bytes and files have been added to the cache. Size + * is in KiB. */ void -stats_update_size(enum stats stat, uint64_t size, unsigned files) +stats_update_size(uint64_t size, unsigned files) { init_counter_updates(); - if (stat != STATS_NONE) { - counter_updates->data[stat]++; - } counter_updates->data[STATS_NUMFILES] += files; counter_updates->data[STATS_TOTALSIZE] += size / 1024; } @@ -273,7 +270,9 @@ stats_flush(void) void stats_update(enum stats stat) { - stats_update_size(stat, 0, 0); + assert(stat > STATS_NONE && stat < STATS_END); + init_counter_updates(); + counter_updates->data[stat]++; } /* Get the pending update of a counter value. */