From: Thomas Otto Date: Sat, 25 Jan 2020 09:24:35 +0000 (+0100) Subject: Context: convert stats_file to an std::string X-Git-Tag: v4.0~619^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=49774284430f1d27e8f2d0d020cd117531aa98d2;p=thirdparty%2Fccache.git Context: convert stats_file to an std::string Also adapted downstream stats functions. --- diff --git a/src/Context.cpp b/src/Context.cpp index 5403f3772..27ab9c146 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -20,5 +20,4 @@ Context::~Context() { - free(stats_file); } diff --git a/src/Context.hpp b/src/Context.hpp index a12f34885..19821227b 100644 --- a/src/Context.hpp +++ b/src/Context.hpp @@ -34,5 +34,5 @@ struct Context : NonCopyable // Full path to the statistics file in the subdirectory where the cached // result belongs (//stats). - char* stats_file = nullptr; + std::string stats_file; }; diff --git a/src/ccache.cpp b/src/ccache.cpp index 610732e0c..12829cbc7 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -1082,7 +1082,7 @@ update_manifest_file(Context& ctx) } else { auto st = Stat::stat(manifest_path, Stat::OnError::log); stats_update_size(ctx, - manifest_stats_file, + from_cstr(manifest_stats_file), st.size_on_disk() - old_st.size_on_disk(), !old_st && st ? 1 : 0); } @@ -1101,12 +1101,12 @@ update_cached_result_globals(Context& ctx, struct digest* result_name) result_name_string, ".result") .c_str()); - ctx.stats_file = format( - "%s/%c/stats", ctx.config.cache_dir().c_str(), result_name_string[0]); + ctx.stats_file = + fmt::format("{}/{}/stats", ctx.config.cache_dir(), result_name_string[0]); } static bool -create_cachedir_tag(const std::string& dir) +create_cachedir_tag(nonstd::string_view dir) { static char const cachedir_tag[] = "Signature: 8a477f597d28d172789f06886806bc55\n" @@ -1373,7 +1373,7 @@ to_cache(Context& ctx, failed(); } stats_update_size(ctx, - ctx.stats_file, + ctx.stats_file.c_str(), new_dest_stat.size_on_disk() - orig_dest_stat.size_on_disk(), orig_dest_stat ? 0 : 1); @@ -1386,13 +1386,12 @@ to_cache(Context& ctx, // be done almost anywhere, but we might as well do it near the end as we // save the stat call if we exit early. { - char* first_level_dir = x_dirname(ctx.stats_file); + std::string first_level_dir = std::string(Util::dir_name(ctx.stats_file)); if (!create_cachedir_tag(first_level_dir) != 0) { cc_log("Failed to create %s/CACHEDIR.TAG (%s)", - first_level_dir, + first_level_dir.c_str(), strerror(errno)); } - free(first_level_dir); // Remove any CACHEDIR.TAG on the cache_dir level where it was located in // previous ccache versions. diff --git a/src/result.cpp b/src/result.cpp index 7ac21bd85..747b5bfa7 100644 --- a/src/result.cpp +++ b/src/result.cpp @@ -414,7 +414,7 @@ write_raw_file_entry(Context& ctx, auto new_stat = Stat::stat(raw_file); stats_update_size(ctx, - ctx.stats_file, + ctx.stats_file.c_str(), new_stat.size_on_disk() - old_stat.size_on_disk(), (new_stat ? 1 : 0) - (old_stat ? 1 : 0)); } diff --git a/src/stats.cpp b/src/stats.cpp index 4e2fff0b8..5cf6eb0ad 100644 --- a/src/stats.cpp +++ b/src/stats.cpp @@ -30,6 +30,8 @@ #include "lockfile.hpp" #include "logging.hpp" +#include "third_party/fmt/core.h" + #include #include #include @@ -54,7 +56,7 @@ typedef char* (*format_fn)(uint64_t value); static char* format_size_times_1024(uint64_t size); static char* format_timestamp(uint64_t timestamp); static void stats_flush_to_file(const Config& config, - const char* sfile, + std::string sfile, struct counters* updates); // Statistics fields in display order. @@ -227,7 +229,7 @@ parse_stats(struct counters* counters, const char* buf) // Write out a stats file. void -stats_write(const char* path, struct counters* counters) +stats_write(const std::string& path, struct counters* counters) { AtomicFile file(path, AtomicFile::Mode::text); for (size_t i = 0; i < counters->size; ++i) { @@ -297,7 +299,10 @@ stats_collect(const Config& config, // Record that a number of bytes and files have been added to the cache. Size // is in bytes. void -stats_update_size(Context& ctx, const char* sfile, int64_t size, int files) +stats_update_size(Context& ctx, + const std::string& sfile, + int64_t size, + int files) { if (size == 0 && files == 0) { return; @@ -320,9 +325,9 @@ stats_update_size(Context& ctx, const char* sfile, int64_t size, int files) // Read in the stats from one directory and add to the counters. void -stats_read(const char* sfile, struct counters* counters) +stats_read(const std::string& sfile, struct counters* counters) { - char* data = read_text_file(sfile, 1024); + char* data = read_text_file(sfile.c_str(), 1024); if (data) { parse_stats(counters, data); } @@ -332,7 +337,7 @@ stats_read(const char* sfile, struct counters* counters) // Write counter updates in updates to sfile. static void stats_flush_to_file(const Config& config, - const char* sfile, + std::string sfile, struct counters* updates) { if (!updates) { @@ -369,18 +374,14 @@ stats_flush_to_file(const Config& config, return; } - if (!sfile) { - char* stats_dir; - - // A NULL sfile means that we didn't get past calculate_object_hash(), so + if (sfile.empty()) { + // An empty sfile means that we didn't get past calculate_object_hash(), so // we just choose one of stats files in the 16 subdirectories. - stats_dir = - format("%s/%x", config.cache_dir().c_str(), hash_from_int(getpid()) % 16); - sfile = format("%s/stats", stats_dir); - free(stats_dir); + sfile = fmt::format( + "{}/{:x}/stats", config.cache_dir(), hash_from_int(getpid()) % 16); } - if (!lockfile_acquire(sfile, lock_staleness_limit)) { + if (!lockfile_acquire(sfile.c_str(), lock_staleness_limit)) { return; } @@ -390,15 +391,15 @@ stats_flush_to_file(const Config& config, counters->data[i] += updates->data[i]; } stats_write(sfile, counters); - lockfile_release(sfile); + lockfile_release(sfile.c_str()); - char* subdir = x_dirname(sfile); + std::string subdir = std::string(Util::dir_name(sfile)); bool need_cleanup = false; if (config.max_files() != 0 && counters->data[STATS_NUMFILES] > config.max_files() / 16) { cc_log("Need to clean up %s since it holds %u files (limit: %u files)", - subdir, + subdir.c_str(), counters->data[STATS_NUMFILES], config.max_files() / 16); need_cleanup = true; @@ -406,7 +407,7 @@ stats_flush_to_file(const Config& config, if (config.max_size() != 0 && counters->data[STATS_TOTALSIZE] > config.max_size() / 1024 / 16) { cc_log("Need to clean up %s since it holds %u KiB (limit: %lu KiB)", - subdir, + subdir.c_str(), counters->data[STATS_TOTALSIZE], (unsigned long)config.max_size() / 1024 / 16); need_cleanup = true; @@ -419,7 +420,6 @@ stats_flush_to_file(const Config& config, clean_up_dir(subdir, max_size, max_files, [](double) {}); } - free(subdir); counters_free(counters); } diff --git a/src/stats.hpp b/src/stats.hpp index 27950619a..1a0df5fcb 100644 --- a/src/stats.hpp +++ b/src/stats.hpp @@ -20,6 +20,8 @@ #include "system.hpp" +#include + class Config; struct Context; @@ -68,13 +70,15 @@ unsigned stats_get_pending(enum stats stat); void stats_zero(const Config& config); void stats_summary(const Config& config); void stats_print(const Config& config); -void -stats_update_size(Context& ctx, const char* sfile, int64_t size, int files); +void stats_update_size(Context& ctx, + const std::string& sfile, + int64_t size, + int files); void stats_get_obsolete_limits(const char* dir, unsigned* maxfiles, uint64_t* maxsize); void stats_set_sizes(const char* dir, unsigned num_files, uint64_t total_size); void stats_add_cleanup(const char* dir, unsigned count); void stats_timestamp(time_t time, struct counters* counters); -void stats_read(const char* path, struct counters* counters); -void stats_write(const char* path, struct counters* counters); +void stats_read(const std::string& path, struct counters* counters); +void stats_write(const std::string& path, struct counters* counters);