} 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);
}
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"
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);
// 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.
#include "lockfile.hpp"
#include "logging.hpp"
+#include "third_party/fmt/core.h"
+
#include <cmath>
#include <fcntl.h>
#include <stdio.h>
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.
// 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) {
// 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;
// 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);
}
// 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) {
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;
}
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;
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;
clean_up_dir(subdir, max_size, max_files, [](double) {});
}
- free(subdir);
counters_free(counters);
}
#include "system.hpp"
+#include <string>
+
class Config;
struct Context;
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);