Index: head/usr.bin/tar/bsdtar.c =================================================================== --- head/usr.bin/tar/bsdtar.c (revision 130514) +++ head/usr.bin/tar/bsdtar.c (revision 130515) @@ -1,610 +1,614 @@ /*- * Copyright (c) 2003-2004 Tim Kientzle * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer * in this position and unchanged. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "bsdtar_platform.h" __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #ifdef HAVE_GETOPT_LONG #include #endif #include #include #include #include #include #include #include #include "bsdtar.h" static void long_help(struct bsdtar *); static void only_mode(struct bsdtar *, char mode, const char *opt, const char *valid); static char ** rewrite_argv(struct bsdtar *, int *argc, char ** src_argv, const char *optstring); const char *tar_opts = "Bb:C:cF:f:HhjkLlmnOoPprtT:UuvwXxyZz"; #ifdef HAVE_GETOPT_LONG /* * These long options are deliberately not documented. They are * provided only to make life easier for people using GNU tar. The * only long options documented in the manual page are the ones with * no corresponding short option (currently, --exclude, --nodump, and * --fast-read). * * XXX TODO: Provide short options for --exclude, --nodump and --fast-read * so that bsdtar is usable on systems that do not have (or do not want * to use) getopt_long(). */ #define OPTION_EXCLUDE 1 #define OPTION_FAST_READ 2 #define OPTION_NODUMP 3 #define OPTION_HELP 4 #define OPTION_INCLUDE 5 #define OPTION_ONE_FILE_SYSTEM 6 const struct option tar_longopts[] = { { "absolute-paths", no_argument, NULL, 'P' }, { "append", no_argument, NULL, 'r' }, { "block-size", required_argument, NULL, 'b' }, { "bunzip2", no_argument, NULL, 'j' }, { "bzip", no_argument, NULL, 'j' }, { "bzip2", no_argument, NULL, 'j' }, { "cd", required_argument, NULL, 'C' }, { "confirmation", no_argument, NULL, 'w' }, { "create", no_argument, NULL, 'c' }, { "dereference", no_argument, NULL, 'L' }, { "directory", required_argument, NULL, 'C' }, { "exclude", required_argument, NULL, OPTION_EXCLUDE }, + { "exclude-from", required_argument, NULL, 'X' }, { "extract", no_argument, NULL, 'x' }, { "fast-read", no_argument, NULL, OPTION_FAST_READ }, { "file", required_argument, NULL, 'f' }, { "format", required_argument, NULL, 'F' }, { "gunzip", no_argument, NULL, 'z' }, { "gzip", no_argument, NULL, 'z' }, { "help", no_argument, NULL, OPTION_HELP }, { "include", required_argument, NULL, OPTION_INCLUDE }, { "interactive", no_argument, NULL, 'w' }, { "keep-old-files", no_argument, NULL, 'k' }, { "list", no_argument, NULL, 't' }, { "modification-time", no_argument, NULL, 'm' }, { "nodump", no_argument, NULL, OPTION_NODUMP }, { "norecurse", no_argument, NULL, 'n' }, { "no-same-owner", no_argument, NULL, 'o' }, { "one-file-system", no_argument, NULL, OPTION_ONE_FILE_SYSTEM }, { "preserve-permissions", no_argument, NULL, 'p' }, { "read-full-blocks", no_argument, NULL, 'B' }, { "same-permissions", no_argument, NULL, 'p' }, { "to-stdout", no_argument, NULL, 'O' }, { "unlink", no_argument, NULL, 'U' }, { "unlink-first", no_argument, NULL, 'U' }, { "update", no_argument, NULL, 'u' }, { "verbose", no_argument, NULL, 'v' }, { NULL, 0, NULL, 0 } }; #endif int main(int argc, char **argv) { struct bsdtar *bsdtar, bsdtar_storage; struct passwd *pwent; int opt; char mode; char buff[16]; char possible_help_request; /* * Use a pointer for consistency, but stack-allocated storage * for ease of cleanup. */ bsdtar = &bsdtar_storage; memset(bsdtar, 0, sizeof(*bsdtar)); bsdtar->fd = -1; /* Mark as "unused" */ if (setlocale(LC_ALL, "") == NULL) bsdtar_warnc(bsdtar, 0, "Failed to set default locale"); mode = '\0'; possible_help_request = 0; /* Look up uid/uname of current user for future reference */ bsdtar->user_uid = geteuid(); bsdtar->user_uname = NULL; if ((pwent = getpwuid(bsdtar->user_uid))) { bsdtar->user_uname = (char *)malloc(strlen(pwent->pw_name)+1); if (bsdtar->user_uname) strcpy(bsdtar->user_uname, pwent->pw_name); } /* Default: open tape drive. */ bsdtar->filename = getenv("TAPE"); if (bsdtar->filename == NULL) bsdtar->filename = _PATH_DEFTAPE; /* Default: preserve mod time on extract */ bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME; if (bsdtar->user_uid == 0) bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER; bsdtar->progname = strrchr(*argv, '/'); if (bsdtar->progname != NULL) bsdtar->progname++; else bsdtar->progname = *argv; /* Rewrite traditional-style tar arguments, if used. */ argv = rewrite_argv(bsdtar, &argc, argv, tar_opts); bsdtar->argv = argv; bsdtar->argc = argc; /* Process all remaining arguments now. */ #ifdef HAVE_GETOPT_LONG while ((opt = getopt_long(bsdtar->argc, bsdtar->argv, tar_opts, tar_longopts, NULL)) != -1) { #else while ((opt = getopt(bsdtar->argc, bsdtar->argv, tar_opts)) != -1) { #endif switch (opt) { case 'B': /* GNU tar */ /* * bsdtar is stream-based internally, so this * option has no effect. Just ignore it. */ break; case 'b': /* SUSv2 */ bsdtar->bytes_per_block = 512 * atoi(optarg); break; case 'C': /* GNU tar */ /* XXX How should multiple -C options be handled? */ bsdtar->start_dir = optarg; break; case 'c': /* SUSv2 */ if (mode != '\0') bsdtar_errc(bsdtar, 1, 0, "Can't specify both -%c and -%c", opt, mode); mode = opt; break; #ifdef HAVE_GETOPT_LONG case OPTION_EXCLUDE: /* GNU tar */ exclude(bsdtar, optarg); break; #endif case 'F': bsdtar->create_format = optarg; break; case 'f': /* SUSv2 */ bsdtar->filename = optarg; if (strcmp(bsdtar->filename, "-") == 0) bsdtar->filename = NULL; break; #ifdef HAVE_GETOPT_LONG case OPTION_FAST_READ: /* GNU tar */ bsdtar->option_fast_read = 1; break; #endif case 'H': /* BSD convention */ bsdtar->symlink_mode = 'H'; break; case 'h': /* Linux Standards Base, gtar; synonym for -L */ bsdtar->symlink_mode = 'L'; /* Hack: -h by itself is the "help" command. */ possible_help_request = 1; break; #ifdef HAVE_GETOPT_LONG case OPTION_HELP: long_help(bsdtar); exit(0); break; #endif #ifdef HAVE_GETOPT_LONG case OPTION_INCLUDE: include(bsdtar, optarg); break; #endif case 'j': /* GNU tar */ if (bsdtar->create_compression != '\0') bsdtar_errc(bsdtar, 1, 0, "Can't specify both -%c and -%c", opt, bsdtar->create_compression); bsdtar->create_compression = opt; break; case 'k': /* GNU tar */ bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE; break; case 'L': /* BSD convention */ bsdtar->symlink_mode = 'L'; break; case 'l': /* SUSv2; note that GNU -l conflicts */ bsdtar->option_warn_links = 1; break; case 'm': /* SUSv2 */ bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME; break; case 'n': /* GNU tar */ bsdtar->option_no_subdirs = 1; break; #ifdef HAVE_GETOPT_LONG case OPTION_NODUMP: /* star */ bsdtar->option_honor_nodump = 1; break; #endif case 'O': /* GNU tar */ bsdtar->option_stdout = 1; break; case 'o': /* SUSv2; note that GNU -o conflicts */ bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER; break; #if HAVE_GETOPT_LONG case OPTION_ONE_FILE_SYSTEM: /* -l in GNU tar */ bsdtar->option_dont_traverse_mounts = 1; break; #endif #if 0 /* * The common BSD -P option is not necessary, since * our default is to archive symlinks, not follow * them. This is convenient, as -P conflicts with GNU * tar anyway. */ case 'P': /* BSD convention */ /* Default behavior, no option necessary. */ break; #endif case 'P': /* GNU tar */ bsdtar->option_absolute_paths = 1; break; case 'p': /* GNU tar, star */ umask(0); bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM; break; case 'r': /* SUSv2 */ if (mode != '\0') bsdtar_errc(bsdtar, 1, 0, "Can't specify both -%c and -%c", opt, mode); mode = opt; break; case 't': /* SUSv2 */ if (mode != '\0') bsdtar_errc(bsdtar, 1, 0, "Can't specify both -%c and -%c", opt, mode); mode = opt; bsdtar->verbose++; break; case 'T': /* GNU tar */ bsdtar->names_from_file = optarg; break; case 'U': /* GNU tar */ bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK; bsdtar->option_unlink_first = 1; break; case 'u': /* SUSv2 */ if (mode != '\0') bsdtar_errc(bsdtar, 1, 0, "Can't specify both -%c and -%c", opt, mode); mode = opt; break; case 'v': /* SUSv2 */ bsdtar->verbose++; break; case 'w': /* SUSv2 */ bsdtar->option_interactive = 1; + break; + case 'X': /* GNU tar */ + exclude_from_file(bsdtar, optarg); break; case 'x': /* SUSv2 */ if (mode != '\0') bsdtar_errc(bsdtar, 1, 0, "Can't specify both -%c and -%c", opt, mode); mode = opt; break; case 'y': /* FreeBSD version of GNU tar */ if (bsdtar->create_compression != '\0') bsdtar_errc(bsdtar, 1, 0, "Can't specify both -%c and -%c", opt, bsdtar->create_compression); bsdtar->create_compression = opt; break; case 'Z': /* GNU tar */ if (bsdtar->create_compression != '\0') bsdtar_errc(bsdtar, 1, 0, "Can't specify both -%c and -%c", opt, bsdtar->create_compression); bsdtar->create_compression = opt; break; case 'z': /* GNU tar, star */ if (bsdtar->create_compression != '\0') bsdtar_errc(bsdtar, 1, 0, "Can't specify both -%c and -%c", opt, bsdtar->create_compression); bsdtar->create_compression = opt; break; default: usage(bsdtar); } } /* * Sanity-check options. */ if (mode == '\0' && possible_help_request) { long_help(bsdtar); exit(0); } if (mode == '\0') bsdtar_errc(bsdtar, 1, 0, "Must specify one of -c, -r, -t, -u, -x"); /* Check boolean options only permitted in certain modes. */ if (bsdtar->option_absolute_paths) only_mode(bsdtar, mode, "-P", "xcru"); if (bsdtar->option_dont_traverse_mounts) only_mode(bsdtar, mode, "-X", "cru"); if (bsdtar->option_fast_read) only_mode(bsdtar, mode, "--fast-read", "xt"); if (bsdtar->option_honor_nodump) only_mode(bsdtar, mode, "--nodump", "cru"); if (bsdtar->option_no_subdirs) only_mode(bsdtar, mode, "-n", "cru"); if (bsdtar->option_stdout) only_mode(bsdtar, mode, "-O", "x"); if (bsdtar->option_warn_links) only_mode(bsdtar, mode, "-l", "cr"); /* Check other parameters only permitted in certain modes. */ if (bsdtar->create_compression == 'Z' && mode == 'c') { bsdtar_warnc(bsdtar, 0, ".Z compression not supported"); usage(bsdtar); } if (bsdtar->create_compression != '\0') { strcpy(buff, "-?"); buff[1] = bsdtar->create_compression; only_mode(bsdtar, mode, buff, "cxt"); } if (bsdtar->create_format != NULL) only_mode(bsdtar, mode, "-F", "c"); if (bsdtar->names_from_file != NULL) only_mode(bsdtar, mode, "-T", "cru"); if (bsdtar->symlink_mode != '\0') { strcpy(buff, "-X"); buff[1] = bsdtar->symlink_mode; only_mode(bsdtar, mode, buff, "cru"); } bsdtar->argc -= optind; bsdtar->argv += optind; switch(mode) { case 'c': tar_mode_c(bsdtar); break; case 'r': tar_mode_r(bsdtar); break; case 't': tar_mode_t(bsdtar); break; case 'u': tar_mode_u(bsdtar); break; case 'x': tar_mode_x(bsdtar); break; } if (bsdtar->user_uname != NULL) free(bsdtar->user_uname); cleanup_exclusions(bsdtar); return (bsdtar->return_value); } /* * Verify that the mode is correct. */ static void only_mode(struct bsdtar *bsdtar, char mode, const char *opt, const char *valid_modes) { if (strchr(valid_modes, mode) == NULL) bsdtar_errc(bsdtar, 1, 0, "Option %s is not permitted in mode -%c", opt, mode); } /*- * Convert traditional tar arguments into new-style. * For example, * tar tvfb file.tar 32 --exclude FOO * must be converted to * tar -t -v -f file.tar -b 32 --exclude FOO * * This requires building a new argv array. The initial bundled word * gets expanded into a new string that looks like "-t\0-v\0-f\0-b\0". * The new argv array has pointers into this string intermingled with * pointers to the existing arguments. Arguments are moved to * immediately follow their options. * * The optstring argument here is the same one passed to getopt(3). * It is used to determine which option letters have trailing arguments. */ char ** rewrite_argv(struct bsdtar *bsdtar, int *argc, char ** src_argv, const char *optstring) { char **new_argv, **dest_argv; const char *p; char *src, *dest; if (src_argv[1] == NULL || src_argv[1][0] == '-') return (src_argv); *argc += strlen(src_argv[1]) - 1; new_argv = malloc((*argc + 1) * sizeof(new_argv[0])); if (new_argv == NULL) bsdtar_errc(bsdtar, 1, errno, "No Memory"); dest_argv = new_argv; *dest_argv++ = *src_argv++; dest = malloc(strlen(*src_argv) * 3); if (dest == NULL) bsdtar_errc(bsdtar, 1, errno, "No memory"); for (src = *src_argv++; *src != '\0'; src++) { *dest_argv++ = dest; *dest++ = '-'; *dest++ = *src; *dest++ = '\0'; /* If option takes an argument, insert that into the list. */ for (p = optstring; p != NULL && *p != '\0'; p++) { if (*p != *src) continue; if (p[1] != ':') /* No arg required, done. */ break; if (*src_argv == NULL) /* No arg available? Error. */ bsdtar_errc(bsdtar, 1, 0, "Option %c requires an argument", *src); *dest_argv++ = *src_argv++; break; } } /* Copy remaining arguments, including trailing NULL. */ while ((*dest_argv++ = *src_argv++) != NULL) ; return (new_argv); } void usage(struct bsdtar *bsdtar) { const char *p; p = bsdtar->progname; fprintf(stderr, "Usage:\n"); fprintf(stderr, " List: %s -tf \n", p); fprintf(stderr, " Extract: %s -xf \n", p); fprintf(stderr, " Create: %s -cf [filenames...]\n", p); #ifdef HAVE_GETOPT_LONG fprintf(stderr, " Help: %s --help\n", p); #else fprintf(stderr, " Help: %s -h\n", p); #endif exit(1); } static const char *long_help_msg[] = { "First option must be a mode specifier:\n", " -c Create -r Add/Replace -t List -u Update -x Extract\n", "Common Options:\n", " -b # Use # 512-byte records per I/O block\n", " -f Location of archive (default " _PATH_DEFTAPE ")\n", " -v Verbose\n", " -w Interactive\n", "Create: %p -c [options] [ | | @ | C= ]\n", " , add these items to archive\n", " -z, -j Compress archive with gzip/bzip2\n", " -F {ustar|pax|cpio|shar} Select archive format\n", #ifdef HAVE_GETOPT_LONG " --exclude Skip files that match pattern\n", #endif " C= Change to before processing remaining files\n", " @ Add entries from to output\n", "List: %p -t [options] []\n", " If specified, list only entries that match\n", "Extract: %p -x [options] []\n", " If specified, extract only entries that match\n", " -k Keep (don't overwrite) existing files\n", " -m Don't restore modification times\n", " -O Write entries to stdout, don't restore to disk\n", " -p Restore permissions (including ACLs, owner, file flags)\n", NULL }; /* * Note that the word 'bsdtar' will always appear in the first line * of output. * * In particular, /bin/sh scripts that need to test for the presence * of bsdtar can use the following template: * * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \ * echo bsdtar; else echo not bsdtar; fi */ static void long_help(struct bsdtar *bsdtar) { const char *prog; const char *p; const char **msg; prog = bsdtar->progname; fflush(stderr); if (strcmp(prog,"bsdtar")!=0) p = "(bsdtar)"; else p = ""; fprintf(stderr, "%s%s: manipulate archive files\n", prog, p); for (msg = long_help_msg; *msg != NULL; msg++) { for (p = *msg; p != NULL; p++) { if (*p == '\0') break; else if (*p == '%') { if (p[1] == 'p') { fputs(prog, stderr); p++; } else putchar('%'); } else putchar(*p); } } fflush(stderr); } Index: head/usr.bin/tar/bsdtar.h =================================================================== --- head/usr.bin/tar/bsdtar.h (revision 130514) +++ head/usr.bin/tar/bsdtar.h (revision 130515) @@ -1,109 +1,110 @@ /*- * Copyright (c) 2003-2004 Tim Kientzle * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer * in this position and unchanged. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name(s) of the author(s) may not be used to endorse or promote * products derived from this software without specific prior written * permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ */ #include #include #define DEFAULT_BYTES_PER_BLOCK (20*512) /* * The internal state for the "bsdtar" program. * * Keeping all of the state in a structure like this simplifies memory * leak testing (at exit, anything left on the heap is suspect). A * pointer to this structure is passed to most bsdtar internal * functions. */ struct bsdtar { /* Options */ const char *filename; /* -f filename */ const char *create_format; /* -F format */ const char *start_dir; /* -C dir */ const char *names_from_file; /* -T file */ int bytes_per_block; /* -b block_size */ int verbose; /* -v */ int extract_flags; /* Flags for extract operation */ char symlink_mode; /* H or L, per BSD conventions */ char create_compression; /* j, y, or z */ char option_absolute_paths; /* -P */ char option_dont_traverse_mounts; /* -X */ char option_fast_read; /* --fast-read */ char option_honor_nodump; /* --nodump */ char option_interactive; /* -w */ char option_no_subdirs; /* -d */ char option_stdout; /* -p */ char option_unlink_first; /* -U */ char option_warn_links; /* -l */ /* If >= 0, then close this when done. */ int fd; /* Miscellaneous state information */ const char *progname; int argc; char **argv; size_t gs_width; /* For 'list_item' in read.c */ size_t u_width; /* for 'list_item' in read.c */ char *user_uname; /* User running this program */ uid_t user_uid; /* UID running this program */ int return_value; /* Value returned by main() */ char warned_lead_slash; /* Already displayed warning */ /* * Data for various subsystems. Full definitions are located in * the file where they are used. */ struct archive_dir *archive_dir; /* for write.c */ struct name_cache *gname_cache; /* for write.c */ struct links_cache *links_cache; /* for write.c */ struct matching *matching; /* for matching.c */ struct security *security; /* for read.c */ struct name_cache *uname_cache; /* for write.c */ }; void bsdtar_errc(struct bsdtar *, int _eval, int _code, const char *fmt, ...); void bsdtar_strmode(struct archive_entry *entry, char *bp); void bsdtar_warnc(struct bsdtar *, int _code, const char *fmt, ...); void cleanup_exclusions(struct bsdtar *); void exclude(struct bsdtar *, const char *pattern); +void exclude_from_file(struct bsdtar *, const char *pathname); int excluded(struct bsdtar *, const char *pathname); void include(struct bsdtar *, const char *pattern); void safe_fprintf(FILE *, const char *fmt, ...); void tar_mode_c(struct bsdtar *bsdtar); void tar_mode_r(struct bsdtar *bsdtar); void tar_mode_t(struct bsdtar *bsdtar); void tar_mode_u(struct bsdtar *bsdtar); void tar_mode_x(struct bsdtar *bsdtar); int unmatched_inclusions(struct bsdtar *bsdtar); void usage(struct bsdtar *); int yes(const char *fmt, ...); Index: head/usr.bin/tar/matching.c =================================================================== --- head/usr.bin/tar/matching.c (revision 130514) +++ head/usr.bin/tar/matching.c (revision 130515) @@ -1,244 +1,282 @@ /*- * Copyright (c) 2003-2004 Tim Kientzle * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer * in this position and unchanged. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "bsdtar_platform.h" __FBSDID("$FreeBSD$"); #include #include #include #include #include "bsdtar.h" struct match { struct match *next; int matches; char pattern[1]; }; struct matching { struct match *exclusions; int exclusions_count; struct match *inclusions; int inclusions_count; int inclusions_unmatched_count; }; static void add_pattern(struct bsdtar *, struct match **list, const char *pattern); static void initialize_matching(struct bsdtar *); static int match_exclusion(struct match *, const char *pathname); static int match_inclusion(struct match *, const char *pathname); /* * The matching logic here needs to be re-thought. I started * out to try to mimic gtar's matching logic, but found it wasn't * really consistent. In particular 'tar -t' and 'tar -x' interpret * patterns on the command line as anchored, but --exclude doesn't. */ /* * Utility functions to manage exclusion/inclusion patterns */ void exclude(struct bsdtar *bsdtar, const char *pattern) { struct matching *matching; if (bsdtar->matching == NULL) initialize_matching(bsdtar); matching = bsdtar->matching; add_pattern(bsdtar, &(matching->exclusions), pattern); matching->exclusions_count++; } +/* + * Read lines from file and exclude() each one. This uses + * a self-sizing buffer to handle arbitrarily-long lines. + */ +void +exclude_from_file(struct bsdtar *bsdtar, const char *pathname) +{ + FILE *f; + char *buff; + size_t buff_length, line_length; + + f = fopen(pathname, "r"); + if (f == NULL) + bsdtar_errc(bsdtar, 1, errno, "Couldn't open %s", pathname); + buff_length = 256; + buff = malloc(buff_length); + if (buff == NULL) + bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read %s", pathname); + while(fgets(buff, buff_length, f) != NULL) { + line_length = strlen(buff); + while (buff[line_length - 1] != '\n') { + buff = realloc(buff, buff_length *= 2); + if (buff == NULL) + bsdtar_errc(bsdtar, 1, ENOMEM, + "Line too long in %s", pathname); + if (fgets(buff + line_length, + buff_length - line_length, f) == NULL) + bsdtar_errc(bsdtar, 1, 0, + "Bad input line in %s", pathname); + line_length = strlen(buff); + } + buff[line_length - 1] = '\0'; + exclude(bsdtar, buff); + } + free(buff); + fclose(f); +} + void include(struct bsdtar *bsdtar, const char *pattern) { struct matching *matching; if (bsdtar->matching == NULL) initialize_matching(bsdtar); matching = bsdtar->matching; add_pattern(bsdtar, &(matching->inclusions), pattern); matching->inclusions_count++; matching->inclusions_unmatched_count++; } static void add_pattern(struct bsdtar *bsdtar, struct match **list, const char *pattern) { struct match *match; match = malloc(sizeof(*match) + strlen(pattern) + 1); if (match == NULL) bsdtar_errc(bsdtar, 1, errno, "Out of memory"); if (pattern[0] == '/') pattern++; strcpy(match->pattern, pattern); /* Both "foo/" and "foo" should match "foo/bar". */ if (match->pattern[strlen(match->pattern)-1] == '/') match->pattern[strlen(match->pattern)-1] = '\0'; match->next = *list; *list = match; match->matches = 0; } int excluded(struct bsdtar *bsdtar, const char *pathname) { struct matching *matching; struct match *match; struct match *matched; matching = bsdtar->matching; if (matching == NULL) return (0); /* Exclusions take priority */ for (match = matching->exclusions; match != NULL; match = match->next){ if (match_exclusion(match, pathname)) return (1); } /* Then check for inclusions */ matched = NULL; for (match = matching->inclusions; match != NULL; match = match->next){ if (match_inclusion(match, pathname)) { /* * If this pattern has never been matched, * then we're done. */ if (match->matches == 0) { match->matches++; matching->inclusions_unmatched_count++; return (0); } /* * Otherwise, remember the match but keep checking * in case we can tick off an unmatched pattern. */ matched = match; } } /* * We didn't find a pattern that had never been matched, but * we did find a match, so count it and exit. */ if (matched != NULL) { matched->matches++; return (0); } /* If there were inclusions, default is to exclude. */ if (matching->inclusions != NULL) return (1); /* No explicit inclusions, default is to match. */ return (0); } /* * This is a little odd, but it matches the default behavior of * gtar. In particular, 'a*b' will match 'foo/a1111/222b/bar' * * XXX TODO: fnmatch isn't the most portable thing around, and even * worse, FNM_LEADING_DIR is a non-POSIX extension. Thus, the * following two functions need to eventually be replaced with code * that does not rely on fnmatch(). */ int match_exclusion(struct match *match, const char *pathname) { const char *p; if (*match->pattern == '*' || *match->pattern == '/') return (fnmatch(match->pattern, pathname, FNM_LEADING_DIR) == 0); for (p = pathname; p != NULL; p = strchr(p, '/')) { if (*p == '/') p++; if (fnmatch(match->pattern, p, FNM_LEADING_DIR) == 0) return (1); } return (0); } /* * Again, mimic gtar: inclusions are always anchored (have to match * the beginning of the path) even though exclusions are not anchored. */ int match_inclusion(struct match *match, const char *pathname) { return (fnmatch(match->pattern, pathname, FNM_LEADING_DIR) == 0); } void cleanup_exclusions(struct bsdtar *bsdtar) { struct match *p, *q; if (bsdtar->matching) { p = bsdtar->matching->inclusions; while (p != NULL) { q = p; p = p->next; free(q); } p = bsdtar->matching->exclusions; while (p != NULL) { q = p; p = p->next; free(q); } free(bsdtar->matching); } } static void initialize_matching(struct bsdtar *bsdtar) { bsdtar->matching = malloc(sizeof(*bsdtar->matching)); if (bsdtar->matching == NULL) bsdtar_errc(bsdtar, 1, errno, "No memory"); memset(bsdtar->matching, 0, sizeof(*bsdtar->matching)); } int unmatched_inclusions(struct bsdtar *bsdtar) { struct matching *matching; matching = bsdtar->matching; if (matching == NULL) return (0); return (matching->inclusions_unmatched_count); }