#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;
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)
// 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));
}
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);
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, ...)