From: Karl Chen Date: Sun, 1 Nov 2009 18:39:58 +0000 (+0100) Subject: Create CACHEDIR.TAG file X-Git-Tag: v3.0pre0~186 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=feed6077d211dbd20edabdad410dfec8c27d6b24;p=thirdparty%2Fccache.git Create CACHEDIR.TAG file See . --- diff --git a/ccache.c b/ccache.c index b80a0de9e..47f81bd07 100644 --- a/ccache.c +++ b/ccache.c @@ -1146,6 +1146,14 @@ int main(int argc, char *argv[]) exit(1); } + if (!getenv("CCACHE_READONLY")) { + if (create_cachedirtag(cache_dir) != 0) { + fprintf(stderr,"ccache: failed to create %s/CACHEDIR.TAG (%s)\n", + cache_dir, strerror(errno)); + exit(1); + } + } + ccache(argc, argv); return 1; } diff --git a/ccache.h b/ccache.h index dc09d71f8..5e8946957 100644 --- a/ccache.h +++ b/ccache.h @@ -94,6 +94,7 @@ int move_file(const char *src, const char *dest); int test_if_compressed(const char *filename); int create_dir(const char *dir); +int create_cachedirtag(const char *dir); void x_asprintf(char **ptr, const char *format, ...); char *x_strdup(const char *s); void *x_realloc(void *ptr, size_t size); diff --git a/util.c b/util.c index d8437e72d..4c54efb8d 100644 --- a/util.c +++ b/util.c @@ -329,6 +329,39 @@ int create_dir(const char *dir) return 0; } +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" + "# http://www.brynosaurus.com/cachedir/\n"; + +int create_cachedirtag(const char *dir) +{ + char *filename; + struct stat st; + FILE *f; + x_asprintf(&filename, "%s/CACHEDIR.TAG", dir); + 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) { + goto error; + } + if (fclose(f)) goto error; +success: + free(filename); + return 0; +error: + free(filename); + return 1; +} + /* this is like asprintf() but dies if the malloc fails note that we use vsnprintf in a rather poor way to make this more portable