+#include <system.h>
+
+#include <quotearg.h>
+
+#include "common.h"
+#include <hash.h>
+
+/* Error number to use when an impostor is discovered.
+ Pretend the impostor isn't there. */
+enum { IMPOSTOR_ERRNO = ENOENT };
+
+struct link
+ {
+ dev_t dev;
+ ino_t ino;
+ nlink_t nlink;
+ char name[1];
+ };
+
+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);
+}
+
+\f
+/* The maximum uintmax_t value that can be represented with DIGITS digits,
+ assuming that each digit is BITS_PER_DIGIT wide. */
+#define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
+ ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
+ ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
+ : (uintmax_t) -1)
+
+/* The maximum uintmax_t value that can be represented with octal
+ digits and a trailing NUL in BUFFER. */
+#define MAX_OCTAL_VAL(buffer) MAX_VAL_WITH_DIGITS (sizeof (buffer) - 1, LG_8)
+
+/* Convert VALUE to an octal representation suitable for tar headers.
+ Output to buffer WHERE with size SIZE.
+ The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
+
+static void
+to_octal (uintmax_t value, char *where, size_t size)
+{
+ uintmax_t v = value;
+ size_t i = size;
+
+ do
+ {
+ where[--i] = '0' + (v & ((1 << LG_8) - 1));
+ v >>= LG_8;
+ }
+ while (i);
+}
+
+/* Copy at most LEN bytes from the string SRC to DST. Terminate with
+ NUL unless SRC is LEN or more bytes long. */
+
+static void
+tar_copy_str (char *dst, const char *src, size_t len)
+{
+ size_t i;
+ for (i = 0; i < len; i++)
+ if (! (dst[i] = src[i]))
+ break;
+}
+
+/* Same as tar_copy_str, but always terminate with NUL if using
+ is OLDGNU format */
+
+static void
+tar_name_copy_str (char *dst, const char *src, size_t len)
+{
+ tar_copy_str (dst, src, len);
+ if (archive_format == OLDGNU_FORMAT)
+ dst[len-1] = 0;
+}
+
+/* Convert NEGATIVE VALUE to a base-256 representation suitable for
+ tar headers. NEGATIVE is 1 if VALUE was negative before being cast
+ to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
+ The result is undefined if SIZE is 0 or if VALUE is too large to
+ fit. */
+
+static void
+to_base256 (int negative, uintmax_t value, char *where, size_t size)
+{
+ uintmax_t v = value;
+ uintmax_t propagated_sign_bits =
+ ((uintmax_t) - negative << (CHAR_BIT * sizeof v - LG_256));
+ size_t i = size;
+
+ do
+ {
+ where[--i] = v & ((1 << LG_256) - 1);
+ v = propagated_sign_bits | (v >> LG_256);
+ }
+ while (i);
+}
+
+#define GID_TO_CHARS(val, where) gid_to_chars (val, where, sizeof (where))
+#define MAJOR_TO_CHARS(val, where) major_to_chars (val, where, sizeof (where))
+#define MINOR_TO_CHARS(val, where) minor_to_chars (val, where, sizeof (where))
+#define MODE_TO_CHARS(val, where) mode_to_chars (val, where, sizeof (where))
+#define UID_TO_CHARS(val, where) uid_to_chars (val, where, sizeof (where))
+
+#define UNAME_TO_CHARS(name,buf) string_to_chars (name, buf, sizeof(buf))
+#define GNAME_TO_CHARS(name,buf) string_to_chars (name, buf, sizeof(buf))
+
+static bool
+to_chars (int negative, uintmax_t value, size_t valsize,
+ uintmax_t (*substitute) (int *),
+ char *where, size_t size, const char *type);