+
+struct exclusion_tag
+{
+ const char *name;
+ size_t length;
+ enum exclusion_tag_type type;
+ bool (*predicate) (int fd);
+ struct exclusion_tag *next;
+};
+
+static struct exclusion_tag *exclusion_tags;
+
+void
+add_exclusion_tag (const char *name, enum exclusion_tag_type type,
+ bool (*predicate) (int fd))
+{
+ struct exclusion_tag *tag = xmalloc (sizeof tag[0]);
+ tag->next = exclusion_tags;
+ tag->name = name;
+ tag->type = type;
+ tag->predicate = predicate;
+ tag->length = strlen (name);
+ exclusion_tags = tag;
+}
+
+void
+exclusion_tag_warning (const char *dirname, const char *tagname,
+ const char *message)
+{
+ if (verbose_option)
+ WARNOPT (WARN_CACHEDIR,
+ (0, 0,
+ _("%s: contains a cache directory tag %s; %s"),
+ quotearg_colon (dirname),
+ quotearg_n (1, tagname),
+ message));
+}
+
+enum exclusion_tag_type
+check_exclusion_tags (struct tar_stat_info const *st, char const **tag_file_name)
+{
+ struct exclusion_tag *tag;
+
+ for (tag = exclusion_tags; tag; tag = tag->next)
+ {
+ int tagfd = subfile_open (st, tag->name, open_read_flags);
+ if (0 <= tagfd)
+ {
+ bool satisfied = !tag->predicate || tag->predicate (tagfd);
+ close (tagfd);
+ if (satisfied)
+ {
+ if (tag_file_name)
+ *tag_file_name = tag->name;
+ return tag->type;
+ }
+ }
+ }
+
+ return exclusion_tag_none;
+}
+
+/* Exclusion predicate to test if the named file (usually "CACHEDIR.TAG")
+ contains a valid header, as described at:
+ http://www.brynosaurus.com/cachedir
+ Applications can write this file into directories they create
+ for use as caches containing purely regenerable, non-precious data,
+ allowing us to avoid archiving them if --exclude-caches is specified. */
+
+#define CACHEDIR_SIGNATURE "Signature: 8a477f597d28d172789f06886806bc55"
+#define CACHEDIR_SIGNATURE_SIZE (sizeof CACHEDIR_SIGNATURE - 1)
+
+bool
+cachedir_file_p (int fd)
+{
+ char tagbuf[CACHEDIR_SIGNATURE_SIZE];
+
+ return
+ (read (fd, tagbuf, CACHEDIR_SIGNATURE_SIZE) == CACHEDIR_SIGNATURE_SIZE
+ && memcmp (tagbuf, CACHEDIR_SIGNATURE, CACHEDIR_SIGNATURE_SIZE) == 0);
+}
+