]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Context: convert stats_file to an std::string
authorThomas Otto <thomas.otto@pdv-fs.de>
Sat, 25 Jan 2020 09:24:35 +0000 (10:24 +0100)
committerThomas Otto <thomas.otto@pdv-fs.de>
Sun, 16 Feb 2020 12:20:15 +0000 (13:20 +0100)
Also adapted downstream stats functions.

src/Context.cpp
src/Context.hpp
src/ccache.cpp
src/result.cpp
src/stats.cpp
src/stats.hpp

index 5403f377209e4363bfdba61c823aeaa092ae8d75..27ab9c146c2e3dcf557ee101df8a1ac0c3dac3f1 100644 (file)
@@ -20,5 +20,4 @@
 
 Context::~Context()
 {
-  free(stats_file);
 }
index a12f34885019e1f4639289ff2ab3594dc27f8f31..19821227b1c9cb64163a2b59a69e75af3e209bf3 100644 (file)
@@ -34,5 +34,5 @@ struct Context : NonCopyable
 
   // Full path to the statistics file in the subdirectory where the cached
   // result belongs (<cache_dir>/<x>/stats).
-  char* stats_file = nullptr;
+  std::string stats_file;
 };
index 610732e0c3ae41a0cad4ebd8968a0c7762ee09b5..12829cbc7470aa8a112b0c8adbda9de3d2fb0c4c 100644 (file)
@@ -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.
index 7ac21bd85d8350eb10ee1a9deb82c4ab80467c6d..747b5bfa7f334e59fe2b1c66df4e9cfd8167e80d 100644 (file)
@@ -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));
 }
index 4e2fff0b8bf3e54c4f6fbd09a57309f870612231..5cf6eb0ad64dee33d0d41cf2bff88e7eab96ed38 100644 (file)
@@ -30,6 +30,8 @@
 #include "lockfile.hpp"
 #include "logging.hpp"
 
+#include "third_party/fmt/core.h"
+
 #include <cmath>
 #include <fcntl.h>
 #include <stdio.h>
@@ -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);
 }
 
index 27950619a6977aee83bc4eef8f57749b6b55ec26..1a0df5fcba325aed66cbdce148897ed7ee81494a 100644 (file)
@@ -20,6 +20,8 @@
 
 #include "system.hpp"
 
+#include <string>
+
 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);