]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
stats: Fix statistics update with full filesystem
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 1 Nov 2019 20:30:20 +0000 (21:30 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 1 Nov 2019 21:24:02 +0000 (22:24 +0100)
This is an improved version of the corresponding 3.x series fix in
1a432329.

src/stats.cpp

index 81544d14853b11331a81604045bb11182e9f5256..820dbcc82ccfac5e0729eca041b248052df93bc3 100644 (file)
@@ -20,6 +20,7 @@
 // Routines to handle the stats files. The stats file is stored one per cache
 // subdirectory to make this more scalable.
 
+#include "AtomicFile.hpp"
 #include "ccache.hpp"
 #include "cleanup.hpp"
 #include "hashutil.hpp"
@@ -223,16 +224,11 @@ parse_stats(struct counters* counters, const char* buf)
 void
 stats_write(const char* path, struct counters* counters)
 {
-  char* tmp_file = format("%s.tmp", path);
-  FILE* f = create_tmp_file(&tmp_file, "wb");
-  for (size_t i = 0; i < counters->size; i++) {
-    if (fprintf(f, "%u\n", counters->data[i]) < 0) {
-      fatal("Failed to write to %s", tmp_file);
-    }
+  AtomicFile file(path, AtomicFile::Mode::text);
+  for (size_t i = 0; i < counters->size; ++i) {
+    file.write(fmt::format("{}\n", counters->data[i]));
   }
-  fclose(f);
-  x_rename(tmp_file, path);
-  free(tmp_file);
+  file.commit();
 }
 
 static void