]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
C++-ify create_cachedir_tag
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 24 Aug 2019 12:31:51 +0000 (14:31 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Thu, 5 Sep 2019 20:04:43 +0000 (22:04 +0200)
src/ccache.cpp
src/ccache.hpp
src/util.cpp

index 9ce4f1c4c0ec86027f423b1fe127ae4ee854d5e2..96163a0a1f1990d092fb68a0aa1178e93e1ad81c 100644 (file)
@@ -38,6 +38,8 @@
 #include "result.hpp"
 #include "unify.hpp"
 
+#include <fstream>
+
 // Global variables used by other compilation units.
 extern char* primary_config_path;
 extern char* secondary_config_path;
@@ -1213,6 +1215,38 @@ update_cached_result_globals(struct digest* result_name)
     format("%s/%c/stats", g_config.cache_dir().c_str(), result_name_string[0]);
 }
 
+static bool
+create_cachedir_tag(const std::string& dir)
+{
+  static char const cachedir_tag[] =
+    "Signature: 8a477f597d28d172789f06886806bc55\n"
+    "# This file is a cache directory tag created by ccache.\n"
+    "# For information about cache directory tags, see:\n"
+    "#\thttp://www.brynosaurus.com/cachedir/\n";
+
+  std::string filename = fmt::format("{}/CACHEDIR.TAG", dir);
+  struct stat st;
+
+  if (stat(filename.c_str(), &st) == 0) {
+    if (S_ISREG(st.st_mode)) {
+      return true;
+    }
+    errno = EEXIST;
+    return false;
+  }
+
+  std::ofstream f(filename);
+  if (!f) {
+    return false;
+  }
+  f << cachedir_tag;
+  if (!f) {
+    return false;
+  }
+
+  return true;
+}
+
 // Run the real compiler and put the result in cache.
 static void
 to_cache(struct args* args, struct hash* depend_mode_hash)
@@ -1447,8 +1481,8 @@ to_cache(struct args* args, struct hash* depend_mode_hash)
   // save the stat call if we exit early.
   {
     char* first_level_dir = x_dirname(stats_file);
-    if (create_cachedirtag(first_level_dir) != 0) {
-      cc_log("Failed to create %s/CACHEDIR.TAG (%s)\n",
+    if (!create_cachedir_tag(first_level_dir) != 0) {
+      cc_log("Failed to create %s/CACHEDIR.TAG (%s)",
              first_level_dir,
              strerror(errno));
     }
index b159a236c4f5edf08a1cb7bf97b3d7d19df73000..b34374e9a05918a36c3be44c3fdd46f6b876b789 100644 (file)
@@ -167,7 +167,6 @@ int create_dir(const char* dir);
 int create_parent_dirs(const char* path);
 const char* get_hostname(void);
 const char* tmp_string(void);
-int create_cachedirtag(const char* dir);
 char* format(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
 void format_hex(const uint8_t* data, size_t size, char* buffer);
 void reformat(char** ptr, const char* format, ...) ATTR_FORMAT(printf, 2, 3);
index d7226ebc6f959c4e39ac5681a6e83f3c7266141b..5ab75757542a0fcce9c05bc41005bbee77448779 100644 (file)
@@ -648,46 +648,6 @@ tmp_string(void)
   return ret;
 }
 
-static char const CACHEDIR_TAG[] =
-  "Signature: 8a477f597d28d172789f06886806bc55\n"
-  "# This file is a cache directory tag created by ccache.\n"
-  "# For information about cache directory tags, see:\n"
-  "#\thttp://www.brynosaurus.com/cachedir/\n";
-
-int
-create_cachedirtag(const char* dir)
-{
-  char* filename = format("%s/CACHEDIR.TAG", dir);
-  FILE* f;
-  struct stat st;
-
-  if (stat(filename, &st) == 0) {
-    if (S_ISREG(st.st_mode)) {
-      goto success;
-    }
-    errno = EEXIST;
-    goto error;
-  }
-
-  f = fopen(filename, "w");
-  if (!f) {
-    goto error;
-  }
-  if (fwrite(CACHEDIR_TAG, sizeof(CACHEDIR_TAG) - 1, 1, f) != 1) {
-    fclose(f);
-    goto error;
-  }
-  if (fclose(f)) {
-    goto error;
-  }
-success:
-  free(filename);
-  return 0;
-error:
-  free(filename);
-  return 1;
-}
-
 // Construct a string according to a format. Caller frees.
 char*
 format(const char* format, ...)