diff options
Diffstat (limited to 'archivers/libarchive/files/cpio')
41 files changed, 4611 insertions, 1464 deletions
diff --git a/archivers/libarchive/files/cpio/CMakeLists.txt b/archivers/libarchive/files/cpio/CMakeLists.txt new file mode 100644 index 00000000000..ce500b1cc6b --- /dev/null +++ b/archivers/libarchive/files/cpio/CMakeLists.txt @@ -0,0 +1,51 @@ +############################################ +# +# How to build bsdcpio +# +############################################ +IF(ENABLE_CPIO) + + SET(bsdcpio_SOURCES + cmdline.c + cpio.c + cpio.h + cpio_platform.h + ../libarchive_fe/err.c + ../libarchive_fe/err.h + ../libarchive_fe/lafe_platform.h + ../libarchive_fe/line_reader.c + ../libarchive_fe/line_reader.h + ../libarchive_fe/matching.c + ../libarchive_fe/matching.h + ../libarchive_fe/pathmatch.c + ../libarchive_fe/pathmatch.h + ) + INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../libarchive_fe) + IF(WIN32 AND NOT CYGWIN) + LIST(APPEND bsdcpio_SOURCES cpio_windows.c) + LIST(APPEND bsdcpio_SOURCES cpio_windows.h) + ENDIF(WIN32 AND NOT CYGWIN) + + # bsdcpio documentation + SET(bsdcpio_MANS bsdcpio.1) + + # How to build bsdcpio + ADD_EXECUTABLE(bsdcpio ${bsdcpio_SOURCES}) + IF(ENABLE_CPIO_SHARED) + TARGET_LINK_LIBRARIES(bsdcpio archive ${ADDITIONAL_LIBS}) + ELSE(ENABLE_CPIO_SHARED) + TARGET_LINK_LIBRARIES(bsdcpio archive_static ${ADDITIONAL_LIBS}) + SET_TARGET_PROPERTIES(bsdcpio PROPERTIES COMPILE_DEFINITIONS + LIBARCHIVE_STATIC) + ENDIF(ENABLE_CPIO_SHARED) + # Full path to the compiled executable (used by test suite) + GET_TARGET_PROPERTY(BSDCPIO bsdcpio LOCATION) + + # Installation rules + INSTALL(TARGETS bsdcpio RUNTIME DESTINATION bin) + INSTALL_MAN(${bsdcpio_MANS}) + +ENDIF(ENABLE_CPIO) + +# Test suite +add_subdirectory(test) diff --git a/archivers/libarchive/files/cpio/bsdcpio.1 b/archivers/libarchive/files/cpio/bsdcpio.1 index d4a18578528..79b6997ef4a 100644 --- a/archivers/libarchive/files/cpio/bsdcpio.1 +++ b/archivers/libarchive/files/cpio/bsdcpio.1 @@ -80,6 +80,9 @@ specified directory. Unless specifically stated otherwise, options are applicable in all operating modes. .Bl -tag -width indent +.It Fl 0 +Read filenames separated by NUL characters instead of newlines. +This is necessary if any of the filenames being read might contain newlines. .It Fl A (o mode only) Append to the specified archive. @@ -142,6 +145,11 @@ for more complete information about the formats currently supported by the underlying .Xr libarchive 3 library. +.It Fl H Ar format +Synonym for +.Fl -format . +.It Fl h , Fl -help +Print usage information. .It Fl I Ar file Read archive from .Ar file . @@ -154,6 +162,14 @@ Disable security checks during extraction or copying. This allows extraction via symbolic links and path names containing .Sq .. in the name. +.It Fl J +(o mode only) +Compress the file with xz-compatible compression before writing it. +In input mode, this option is ignored; xz compression is recognized +automatically on input. +.It Fl j +Synonym for +.Fl y . .It Fl L (o and p modes) All symbolic links will be followed. @@ -163,10 +179,28 @@ With this option, the target of the link will be archived or copied instead. (p mode only) Create links from the target directory to the original files, instead of copying. +.It Fl lzma +(o mode only) +Compress the file with lzma-compatible compression before writing it. +In input mode, this option is ignored; lzma compression is recognized +automatically on input. .It Fl m (i and p modes) Set file modification time on created files to match those in the source. +.It Fl n +(i mode, only with +.Fl t ) +Display numeric uid and gid. +By default, +.Nm +displays the user and group names when they are provided in the +archive, or looks up the user and group names in the system +password database. +.It Fl no-preserve-owner +(i mode only) +Do not attempt to restore file ownership. +This is the default when run by non-root users. .It Fl O Ar file Write archive to .Ar file . @@ -176,6 +210,10 @@ See above for description. .It Fl p Pass-through mode. See above for description. +.It Fl preserve-owner +(i mode only) +Restore file ownership. +This is the default when run by the root user. .It Fl -quiet Suppress unnecessary messages. .It Fl R Oo user Oc Ns Oo : Oc Ns Oo group Oc diff --git a/archivers/libarchive/files/cpio/cmdline.c b/archivers/libarchive/files/cpio/cmdline.c index a602e39c179..2223798c124 100644 --- a/archivers/libarchive/files/cpio/cmdline.c +++ b/archivers/libarchive/files/cpio/cmdline.c @@ -26,23 +26,11 @@ #include "cpio_platform.h" -__FBSDID("$FreeBSD: src/usr.bin/cpio/cmdline.c,v 1.3 2008/06/21 02:20:20 kientzle Exp $"); +__FBSDID("$FreeBSD: src/usr.bin/cpio/cmdline.c,v 1.5 2008/12/06 07:30:40 kientzle Exp $"); #ifdef HAVE_ERRNO_H #include <errno.h> #endif -#ifdef HAVE_GETOPT_LONG -#include <getopt.h> -#else -struct option { - const char *name; - int has_arg; - int *flag; - int val; -}; -#define no_argument 0 -#define required_argument 1 -#endif #ifdef HAVE_GRP_H #include <grp.h> #endif @@ -58,120 +46,222 @@ struct option { #endif #include "cpio.h" +#include "err.h" /* - * - * Option parsing routines for bsdcpio. - * - */ - - -static const char *cpio_opts = "0AaBC:F:O:cdE:f:H:hijLlmopR:rtuvW:yZz"; - -/* - * On systems that lack getopt_long, long options can be specified - * using -W longopt and -W longopt=value, e.g. "-W version" is the - * same as "--version" and "-W format=ustar" is the same as "--format - * ustar". This does not rely the GNU getopt() "W;" extension, so - * should work correctly on any system with a POSIX-compliant - * getopt(). + * Short options for cpio. Please keep this sorted. */ +static const char *short_options = "0AaBC:cdE:F:f:H:hI:iJjLlmnO:opR:rtuvW:yZz"; /* - * If you add anything, be very careful to keep this list properly - * sorted, as the -W logic below relies on it. + * Long options for cpio. Please keep this sorted. */ -static const struct option cpio_longopts[] = { - { "create", no_argument, NULL, 'o' }, - { "extract", no_argument, NULL, 'i' }, - { "file", required_argument, NULL, 'F' }, - { "format", required_argument, NULL, 'H' }, - { "help", no_argument, NULL, 'h' }, - { "insecure", no_argument, NULL, OPTION_INSECURE }, - { "link", no_argument, NULL, 'l' }, - { "list", no_argument, NULL, 't' }, - { "make-directories", no_argument, NULL, 'd' }, - { "null", no_argument, NULL, '0' }, - { "owner", required_argument, NULL, 'R' }, - { "pass-through", no_argument, NULL, 'p' }, - { "preserve-modification-time", no_argument, NULL, 'm' }, - { "quiet", no_argument, NULL, OPTION_QUIET }, - { "unconditional", no_argument, NULL, 'u' }, - { "verbose", no_argument, NULL, 'v' }, - { "version", no_argument, NULL, OPTION_VERSION }, - { NULL, 0, NULL, 0 } +static const struct option { + const char *name; + int required; /* 1 if this option requires an argument */ + int equivalent; /* Equivalent short option. */ +} cpio_longopts[] = { + { "create", 0, 'o' }, + { "extract", 0, 'i' }, + { "file", 1, 'F' }, + { "format", 1, 'H' }, + { "help", 0, 'h' }, + { "insecure", 0, OPTION_INSECURE }, + { "link", 0, 'l' }, + { "list", 0, 't' }, + { "lzma", 0, OPTION_LZMA }, + { "make-directories", 0, 'd' }, + { "no-preserve-owner", 0, OPTION_NO_PRESERVE_OWNER }, + { "null", 0, '0' }, + { "numeric-uid-gid", 0, 'n' }, + { "owner", 1, 'R' }, + { "pass-through", 0, 'p' }, + { "preserve-modification-time", 0, 'm' }, + { "preserve-owner", 0, OPTION_PRESERVE_OWNER }, + { "quiet", 0, OPTION_QUIET }, + { "unconditional", 0, 'u' }, + { "verbose", 0, 'v' }, + { "version", 0, OPTION_VERSION }, + { "xz", 0, 'J' }, + { NULL, 0, 0 } }; /* - * Parse command-line options using system-provided getopt() or getopt_long(). - * If option is -W, then parse argument as a long option. + * I used to try to select platform-provided getopt() or + * getopt_long(), but that caused a lot of headaches. In particular, + * I couldn't consistently use long options in the test harness + * because not all platforms have getopt_long(). That in turn led to + * overuse of the -W hack in the test harness, which made it rough to + * run the test harness against GNU cpio. (I periodically run the + * test harness here against GNU cpio as a sanity-check. Yes, + * I've found a couple of bugs in GNU cpio that way.) */ int cpio_getopt(struct cpio *cpio) { - char *p, *q; - const struct option *option, *option2; - int opt; - int option_index; - size_t option_length; + enum { state_start = 0, state_next_word, state_short, state_long }; + static int state = state_start; + static char *opt_word; - option_index = -1; + const struct option *popt, *match = NULL, *match2 = NULL; + const char *p, *long_prefix = "--"; + size_t optlength; + int opt = '?'; + int required = 0; -#ifdef HAVE_GETOPT_LONG - opt = getopt_long(cpio->argc, cpio->argv, cpio_opts, - cpio_longopts, &option_index); -#else - opt = getopt(cpio->argc, cpio->argv, cpio_opts); -#endif + cpio->optarg = NULL; - /* Support long options through -W longopt=value */ - if (opt == 'W') { - p = optarg; - q = strchr(optarg, '='); - if (q != NULL) { - option_length = (size_t)(q - p); - optarg = q + 1; + /* First time through, initialize everything. */ + if (state == state_start) { + /* Skip program name. */ + ++cpio->argv; + --cpio->argc; + state = state_next_word; + } + + /* + * We're ready to look at the next word in argv. + */ + if (state == state_next_word) { + /* No more arguments, so no more options. */ + if (cpio->argv[0] == NULL) + return (-1); + /* Doesn't start with '-', so no more options. */ + if (cpio->argv[0][0] != '-') + return (-1); + /* "--" marks end of options; consume it and return. */ + if (strcmp(cpio->argv[0], "--") == 0) { + ++cpio->argv; + --cpio->argc; + return (-1); + } + /* Get next word for parsing. */ + opt_word = *cpio->argv++; + --cpio->argc; + if (opt_word[1] == '-') { + /* Set up long option parser. */ + state = state_long; + opt_word += 2; /* Skip leading '--' */ } else { - option_length = strlen(p); - optarg = NULL; + /* Set up short option parser. */ + state = state_short; + ++opt_word; /* Skip leading '-' */ } - option = cpio_longopts; - while (option->name != NULL && - (strlen(option->name) < option_length || - strncmp(p, option->name, option_length) != 0 )) { - option++; + } + + /* + * We're parsing a group of POSIX-style single-character options. + */ + if (state == state_short) { + /* Peel next option off of a group of short options. */ + opt = *opt_word++; + if (opt == '\0') { + /* End of this group; recurse to get next option. */ + state = state_next_word; + return cpio_getopt(cpio); } - if (option->name != NULL) { - option2 = option; - opt = option->val; + /* Does this option take an argument? */ + p = strchr(short_options, opt); + if (p == NULL) + return ('?'); + if (p[1] == ':') + required = 1; - /* If the first match was exact, we're done. */ - if (strncmp(p, option->name, strlen(option->name)) == 0) { - while (option->name != NULL) - option++; + /* If it takes an argument, parse that. */ + if (required) { + /* If arg is run-in, opt_word already points to it. */ + if (opt_word[0] == '\0') { + /* Otherwise, pick up the next word. */ + opt_word = *cpio->argv; + if (opt_word == NULL) { + lafe_warnc(0, + "Option -%c requires an argument", + opt); + return ('?'); + } + ++cpio->argv; + --cpio->argc; + } + if (opt == 'W') { + state = state_long; + long_prefix = "-W "; /* For clearer errors. */ } else { - /* Check if there's another match. */ - option++; - while (option->name != NULL && - (strlen(option->name) < option_length || - strncmp(p, option->name, option_length) != 0)) { - option++; + state = state_next_word; + cpio->optarg = opt_word; + } + } + } + + /* We're reading a long option, including -W long=arg convention. */ + if (state == state_long) { + /* After this long option, we'll be starting a new word. */ + state = state_next_word; + + /* Option name ends at '=' if there is one. */ + p = strchr(opt_word, '='); + if (p != NULL) { + optlength = (size_t)(p - opt_word); + cpio->optarg = (char *)(uintptr_t)(p + 1); + } else { + optlength = strlen(opt_word); + } + + /* Search the table for an unambiguous match. */ + for (popt = cpio_longopts; popt->name != NULL; popt++) { + /* Short-circuit if first chars don't match. */ + if (popt->name[0] != opt_word[0]) + continue; + /* If option is a prefix of name in table, record it.*/ + if (strncmp(opt_word, popt->name, optlength) == 0) { + match2 = match; /* Record up to two matches. */ + match = popt; + /* If it's an exact match, we're done. */ + if (strlen(popt->name) == optlength) { + match2 = NULL; /* Forget the others. */ + break; } } - if (option->name != NULL) - cpio_errc(1, 0, - "Ambiguous option %s " - "(matches both %s and %s)", - p, option2->name, option->name); + } + + /* Fail if there wasn't a unique match. */ + if (match == NULL) { + lafe_warnc(0, + "Option %s%s is not supported", + long_prefix, opt_word); + return ('?'); + } + if (match2 != NULL) { + lafe_warnc(0, + "Ambiguous option %s%s (matches --%s and --%s)", + long_prefix, opt_word, match->name, match2->name); + return ('?'); + } - if (option2->has_arg == required_argument - && optarg == NULL) - cpio_errc(1, 0, - "Option \"%s\" requires argument", p); + /* We've found a unique match; does it need an argument? */ + if (match->required) { + /* Argument required: get next word if necessary. */ + if (cpio->optarg == NULL) { + cpio->optarg = *cpio->argv; + if (cpio->optarg == NULL) { + lafe_warnc(0, + "Option %s%s requires an argument", + long_prefix, match->name); + return ('?'); + } + ++cpio->argv; + --cpio->argc; + } } else { - opt = '?'; + /* Argument forbidden: fail if there is one. */ + if (cpio->optarg != NULL) { + lafe_warnc(0, + "Option %s%s does not allow an argument", + long_prefix, match->name); + return ('?'); + } } + return (match->equivalent); } return (opt); @@ -182,24 +272,35 @@ cpio_getopt(struct cpio *cpio) * Parse the argument to the -R or --owner flag. * * The format is one of the following: - * <user> - Override user but not group - * <user>: - Override both, group is user's default group - * <user>:<group> - Override both - * :<group> - Override group but not user + * <username|uid> - Override user but not group + * <username>: - Override both, group is user's default group + * <uid>: - Override user but not group + * <username|uid>:<groupname|gid> - Override both + * :<groupname|gid> - Override group but not user + * + * Where uid/gid are decimal representations and groupname/username + * are names to be looked up in system database. Note that we try + * to look up an argument as a name first, then try numeric parsing. * * A period can be used instead of the colon. * - * Sets uid/gid as appropriate, -1 indicates uid/gid not specified. + * Sets uid/gid return as appropriate, -1 indicates uid/gid not specified. + * + * Returns NULL if no error, otherwise returns error string for display. * */ -int +const char * owner_parse(const char *spec, int *uid, int *gid) { + static char errbuff[128]; const char *u, *ue, *g; *uid = -1; *gid = -1; + if (spec[0] == '\0') + return ("Invalid empty user/group spec"); + /* * Split spec into [user][:.][group] * u -> first char of username, NULL if no username @@ -226,31 +327,43 @@ owner_parse(const char *spec, int *uid, int *gid) struct passwd *pwent; user = (char *)malloc(ue - u + 1); - if (user == NULL) { - cpio_warnc(errno, "Couldn't allocate memory"); - return (1); - } + if (user == NULL) + return ("Couldn't allocate memory"); memcpy(user, u, ue - u); user[ue - u] = '\0'; - pwent = getpwnam(user); - if (pwent == NULL) { - cpio_warnc(errno, "Couldn't lookup user ``%s''", user); - return (1); + if ((pwent = getpwnam(user)) != NULL) { + *uid = pwent->pw_uid; + if (*ue != '\0') + *gid = pwent->pw_gid; + } else { + char *end; + errno = 0; + *uid = strtoul(user, &end, 10); + if (errno || *end != '\0') { + snprintf(errbuff, sizeof(errbuff), + "Couldn't lookup user ``%s''", user); + errbuff[sizeof(errbuff) - 1] = '\0'; + return (errbuff); + } } free(user); - *uid = pwent->pw_uid; - if (*ue != '\0' && *g == '\0') - *gid = pwent->pw_gid; } + if (*g != '\0') { struct group *grp; - grp = getgrnam(g); - if (grp != NULL) + if ((grp = getgrnam(g)) != NULL) { *gid = grp->gr_gid; - else { - cpio_warnc(errno, "Couldn't look up group ``%s''", g); - return (1); + } else { + char *end; + errno = 0; + *gid = strtoul(g, &end, 10); + if (errno || *end != '\0') { + snprintf(errbuff, sizeof(errbuff), + "Couldn't lookup group ``%s''", g); + errbuff[sizeof(errbuff) - 1] = '\0'; + return (errbuff); + } } } - return (0); + return (NULL); } diff --git a/archivers/libarchive/files/cpio/config_freebsd.h b/archivers/libarchive/files/cpio/config_freebsd.h new file mode 100644 index 00000000000..00c4c737f73 --- /dev/null +++ b/archivers/libarchive/files/cpio/config_freebsd.h @@ -0,0 +1,56 @@ +/*- + * Copyright (c) 2003-2007 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. + * 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. + * + * $FreeBSD: src/usr.bin/cpio/config_freebsd.h,v 1.3 2008/12/06 07:30:40 kientzle Exp $ + */ + +/* A hand-tooled configuration for FreeBSD. */ + +#include <sys/param.h> /* __FreeBSD_version */ + +#define HAVE_DIRENT_H 1 +#define HAVE_ERRNO_H 1 +#define HAVE_FCNTL_H 1 +#define HAVE_FUTIMES 1 +#define HAVE_GRP_H 1 +#define HAVE_LIBARCHIVE 1 +#define HAVE_LINK 1 +#define HAVE_LSTAT 1 +#define HAVE_LUTIMES 1 +#define HAVE_PWD_H 1 +#define HAVE_READLINK 1 +#define HAVE_STDARG_H 1 +#define HAVE_STDLIB_H 1 +#define HAVE_STRING_H 1 +#define HAVE_SYMLINK 1 +#define HAVE_SYS_CDEFS_H 1 +#define HAVE_SYS_STAT_H 1 +#define HAVE_SYS_TIME_H 1 +#define HAVE_TIME_H 1 +#define HAVE_UINTMAX_T 1 +#define HAVE_UNISTD_H 1 +#define HAVE_UNSIGNED_LONG_LONG 1 +#define HAVE_UTIME_H 1 +#define HAVE_UTIMES 1 + diff --git a/archivers/libarchive/files/cpio/cpio.c b/archivers/libarchive/files/cpio/cpio.c index 76a91cf7152..7d5031bbcd8 100644 --- a/archivers/libarchive/files/cpio/cpio.c +++ b/archivers/libarchive/files/cpio/cpio.c @@ -26,24 +26,39 @@ #include "cpio_platform.h" -__FBSDID("$FreeBSD: src/usr.bin/cpio/cpio.c,v 1.4 2008/06/24 15:18:40 kientzle Exp $"); +__FBSDID("$FreeBSD: src/usr.bin/cpio/cpio.c,v 1.15 2008/12/06 07:30:40 kientzle Exp $"); #include <sys/types.h> #include <archive.h> #include <archive_entry.h> +#ifdef HAVE_SYS_MKDEV_H +#include <sys/mkdev.h> +#endif #ifdef HAVE_SYS_STAT_H #include <sys/stat.h> #endif +#ifdef HAVE_SYS_TIME_H +#include <sys/time.h> +#endif #ifdef HAVE_ERRNO_H #include <errno.h> #endif #ifdef HAVE_FCNTL_H #include <fcntl.h> #endif +#ifdef HAVE_GRP_H +#include <grp.h> +#endif +#ifdef HAVE_PWD_H +#include <pwd.h> +#endif #ifdef HAVE_STDARG_H #include <stdarg.h> #endif +#ifdef HAVE_STDINT_H +#include <stdint.h> +#endif #include <stdio.h> #ifdef HAVE_STDLIB_H #include <stdlib.h> @@ -54,20 +69,54 @@ __FBSDID("$FreeBSD: src/usr.bin/cpio/cpio.c,v 1.4 2008/06/24 15:18:40 kientzle E #ifdef HAVE_UNISTD_H #include <unistd.h> #endif +#ifdef HAVE_SYS_TIME_H +#include <sys/time.h> +#endif +#ifdef HAVE_TIME_H +#include <time.h> +#endif #include "cpio.h" +#include "err.h" +#include "line_reader.h" #include "matching.h" -static int copy_data(struct archive *, struct archive *); +/* Fixed size of uname/gname caches. */ +#define name_cache_size 101 + +#ifndef O_BINARY +#define O_BINARY 0 +#endif + +struct name_cache { + int probes; + int hits; + size_t size; + struct { + id_t id; + char *name; + } cache[name_cache_size]; +}; + +static int extract_data(struct archive *, struct archive *); +const char * cpio_i64toa(int64_t); static const char *cpio_rename(const char *name); static int entry_to_archive(struct cpio *, struct archive_entry *); static int file_to_archive(struct cpio *, const char *); +static void free_cache(struct name_cache *cache); +static void list_item_verbose(struct cpio *, struct archive_entry *); static void long_help(void); +static const char *lookup_gname(struct cpio *, gid_t gid); +static int lookup_gname_helper(struct cpio *, + const char **name, id_t gid); +static const char *lookup_uname(struct cpio *, uid_t uid); +static int lookup_uname_helper(struct cpio *, + const char **name, id_t uid); static void mode_in(struct cpio *); static void mode_list(struct cpio *); static void mode_out(struct cpio *); static void mode_pass(struct cpio *, const char *); -static void restore_time(struct cpio *, struct archive_entry *, +static int restore_time(struct cpio *, struct archive_entry *, const char *, int fd); static void usage(void); static void version(void); @@ -78,6 +127,7 @@ main(int argc, char *argv[]) static char buff[16384]; struct cpio _cpio; /* Allocated on stack. */ struct cpio *cpio; + const char *errmsg; int uid, gid; int opt; @@ -86,39 +136,46 @@ main(int argc, char *argv[]) cpio->buff = buff; cpio->buff_size = sizeof(buff); - /* Need cpio_progname before calling cpio_warnc. */ + /* Need lafe_progname before calling lafe_warnc. */ if (*argv == NULL) - cpio_progname = "bsdcpio"; + lafe_progname = "bsdcpio"; else { - cpio_progname = strrchr(*argv, '/'); - if (cpio_progname != NULL) - cpio_progname++; +#if defined(_WIN32) && !defined(__CYGWIN__) + lafe_progname = strrchr(*argv, '\\'); +#else + lafe_progname = strrchr(*argv, '/'); +#endif + if (lafe_progname != NULL) + lafe_progname++; else - cpio_progname = *argv; + lafe_progname = *argv; } cpio->uid_override = -1; cpio->gid_override = -1; cpio->argv = argv; cpio->argc = argc; - cpio->line_separator = '\n'; cpio->mode = '\0'; cpio->verbose = 0; cpio->compress = '\0'; - /* TODO: Implement old binary format in libarchive, use that here. */ - cpio->format = "odc"; /* Default format */ cpio->extract_flags = ARCHIVE_EXTRACT_NO_AUTODIR; cpio->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER; cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_SYMLINKS; cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_NODOTDOT; - /* TODO: If run by root, set owner as well. */ + cpio->extract_flags |= ARCHIVE_EXTRACT_PERM; + cpio->extract_flags |= ARCHIVE_EXTRACT_FFLAGS; + cpio->extract_flags |= ARCHIVE_EXTRACT_ACL; +#if !defined(_WIN32) && !defined(__CYGWIN__) + if (geteuid() == 0) + cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER; +#endif cpio->bytes_per_block = 512; cpio->filename = NULL; while ((opt = cpio_getopt(cpio)) != -1) { switch (opt) { case '0': /* GNU convention: --null, -0 */ - cpio->line_separator = '\0'; + cpio->option_null = 1; break; case 'A': /* NetBSD/OpenBSD */ cpio->option_append = 1; @@ -130,9 +187,9 @@ main(int argc, char *argv[]) cpio->bytes_per_block = 5120; break; case 'C': /* NetBSD/OpenBSD */ - cpio->bytes_per_block = atoi(optarg); + cpio->bytes_per_block = atoi(cpio->optarg); if (cpio->bytes_per_block <= 0) - cpio_errc(1, 0, "Invalid blocksize %s", optarg); + lafe_errc(1, 0, "Invalid blocksize %s", cpio->optarg); break; case 'c': /* POSIX 1997 */ cpio->format = "odc"; @@ -141,26 +198,36 @@ main(int argc, char *argv[]) cpio->extract_flags &= ~ARCHIVE_EXTRACT_NO_AUTODIR; break; case 'E': /* NetBSD/OpenBSD */ - include_from_file(cpio, optarg); + lafe_include_from_file(&cpio->matching, + cpio->optarg, cpio->option_null); break; case 'F': /* NetBSD/OpenBSD/GNU cpio */ - cpio->filename = optarg; + cpio->filename = cpio->optarg; break; case 'f': /* POSIX 1997 */ - exclude(cpio, optarg); + lafe_exclude(&cpio->matching, cpio->optarg); break; case 'H': /* GNU cpio (also --format) */ - cpio->format = optarg; + cpio->format = cpio->optarg; break; case 'h': long_help(); break; case 'I': /* NetBSD/OpenBSD */ - cpio->filename = optarg; + cpio->filename = cpio->optarg; break; case 'i': /* POSIX 1997 */ + if (cpio->mode != '\0') + lafe_errc(1, 0, + "Cannot use both -i and -%c", cpio->mode); cpio->mode = opt; break; + case 'J': /* GNU tar, others */ + cpio->compress = opt; + break; + case 'j': /* GNU tar, others */ + cpio->compress = opt; + break; case OPTION_INSECURE: cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_SYMLINKS; cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT; @@ -171,25 +238,46 @@ main(int argc, char *argv[]) case 'l': /* POSIX 1997 */ cpio->option_link = 1; break; + case OPTION_LZMA: /* GNU tar, others */ + cpio->compress = opt; + break; case 'm': /* POSIX 1997 */ cpio->extract_flags |= ARCHIVE_EXTRACT_TIME; break; + case 'n': /* GNU cpio */ + cpio->option_numeric_uid_gid = 1; + break; + case OPTION_NO_PRESERVE_OWNER: /* GNU cpio */ + cpio->extract_flags &= ~ARCHIVE_EXTRACT_OWNER; + break; case 'O': /* GNU cpio */ - cpio->filename = optarg; + cpio->filename = cpio->optarg; break; case 'o': /* POSIX 1997 */ + if (cpio->mode != '\0') + lafe_errc(1, 0, + "Cannot use both -o and -%c", cpio->mode); cpio->mode = opt; break; case 'p': /* POSIX 1997 */ + if (cpio->mode != '\0') + lafe_errc(1, 0, + "Cannot use both -p and -%c", cpio->mode); cpio->mode = opt; cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT; break; + case OPTION_PRESERVE_OWNER: + cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER; + break; case OPTION_QUIET: /* GNU cpio */ cpio->quiet = 1; break; case 'R': /* GNU cpio, also --owner */ - if (owner_parse(optarg, &uid, &gid)) + errmsg = owner_parse(cpio->optarg, &uid, &gid); + if (errmsg) { + lafe_warnc(-1, "%s", errmsg); usage(); + } if (uid != -1) cpio->uid_override = uid; if (gid != -1) @@ -233,18 +321,38 @@ main(int argc, char *argv[]) } } - /* TODO: Sanity-check args, error out on nonsensical combinations. */ - - cpio->argc -= optind; - cpio->argv += optind; + /* + * Sanity-check args, error out on nonsensical combinations. + */ + /* -t implies -i if no mode was specified. */ + if (cpio->option_list && cpio->mode == '\0') + cpio->mode = 'i'; + /* -t requires -i */ + if (cpio->option_list && cpio->mode != 'i') + lafe_errc(1, 0, "Option -t requires -i"); + /* -n requires -it */ + if (cpio->option_numeric_uid_gid && !cpio->option_list) + lafe_errc(1, 0, "Option -n requires -it"); + /* Can only specify format when writing */ + if (cpio->format != NULL && cpio->mode != 'o') + lafe_errc(1, 0, "Option --format requires -o"); + /* -l requires -p */ + if (cpio->option_link && cpio->mode != 'p') + lafe_errc(1, 0, "Option -l requires -p"); + /* TODO: Flag other nonsensical combinations. */ switch (cpio->mode) { case 'o': + /* TODO: Implement old binary format in libarchive, + use that here. */ + if (cpio->format == NULL) + cpio->format = "odc"; /* Default format */ + mode_out(cpio); break; case 'i': while (*cpio->argv != NULL) { - include(cpio, *cpio->argv); + lafe_include(&cpio->matching, *cpio->argv); --cpio->argc; ++cpio->argv; } @@ -255,34 +363,32 @@ main(int argc, char *argv[]) break; case 'p': if (*cpio->argv == NULL || **cpio->argv == '\0') - cpio_errc(1, 0, + lafe_errc(1, 0, "-p mode requires a target directory"); mode_pass(cpio, *cpio->argv); break; default: - cpio_errc(1, 0, + lafe_errc(1, 0, "Must specify at least one of -i, -o, or -p"); } - return (0); + free_cache(cpio->gname_cache); + free_cache(cpio->uname_cache); + return (cpio->return_value); } -void +static void usage(void) { const char *p; - p = cpio_progname; + p = lafe_progname; fprintf(stderr, "Brief Usage:\n"); fprintf(stderr, " List: %s -it < archive\n", p); fprintf(stderr, " Extract: %s -i < archive\n", p); fprintf(stderr, " Create: %s -o < filenames > archive\n", p); -#ifdef HAVE_GETOPT_LONG fprintf(stderr, " Help: %s --help\n", p); -#else - fprintf(stderr, " Help: %s -h\n", p); -#endif exit(1); } @@ -292,7 +398,7 @@ static const char *long_help_msg = "Common Options:\n" " -v Verbose\n" "Create: %p -o [options] < [list of files] > [archive]\n" - " -z, -y Compress archive with gzip/bzip2\n" + " -J,-y,-z,--lzma Compress archive with xz/bzip2/gzip/lzma\n" " --format {odc|newc|ustar} Select archive format\n" "List: %p -it < [archive]\n" "Extract: %p -i [options] < [archive]\n"; @@ -314,7 +420,7 @@ long_help(void) const char *prog; const char *p; - prog = cpio_progname; + prog = lafe_progname; fflush(stderr); @@ -346,46 +452,66 @@ version(void) static void mode_out(struct cpio *cpio) { - unsigned long blocks; struct archive_entry *entry, *spare; - struct line_reader *lr; + struct lafe_line_reader *lr; const char *p; int r; if (cpio->option_append) - cpio_errc(1, 0, "Append mode not yet supported."); + lafe_errc(1, 0, "Append mode not yet supported."); + + cpio->archive_read_disk = archive_read_disk_new(); + if (cpio->archive_read_disk == NULL) + lafe_errc(1, 0, "Failed to allocate archive object"); + if (cpio->option_follow_links) + archive_read_disk_set_symlink_logical(cpio->archive_read_disk); + else + archive_read_disk_set_symlink_physical(cpio->archive_read_disk); + archive_read_disk_set_standard_lookup(cpio->archive_read_disk); + cpio->archive = archive_write_new(); if (cpio->archive == NULL) - cpio_errc(1, 0, "Failed to allocate archive object"); + lafe_errc(1, 0, "Failed to allocate archive object"); switch (cpio->compress) { + case 'J': + r = archive_write_set_compression_xz(cpio->archive); + break; + case OPTION_LZMA: + r = archive_write_set_compression_lzma(cpio->archive); + break; case 'j': case 'y': - archive_write_set_compression_bzip2(cpio->archive); + r = archive_write_set_compression_bzip2(cpio->archive); break; case 'z': - archive_write_set_compression_gzip(cpio->archive); + r = archive_write_set_compression_gzip(cpio->archive); break; case 'Z': - archive_write_set_compression_compress(cpio->archive); + r = archive_write_set_compression_compress(cpio->archive); break; default: - archive_write_set_compression_none(cpio->archive); + r = archive_write_set_compression_none(cpio->archive); break; } + if (r < ARCHIVE_WARN) + lafe_errc(1, 0, "Requested compression not available"); r = archive_write_set_format_by_name(cpio->archive, cpio->format); if (r != ARCHIVE_OK) - cpio_errc(1, 0, archive_error_string(cpio->archive)); + lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); archive_write_set_bytes_per_block(cpio->archive, cpio->bytes_per_block); cpio->linkresolver = archive_entry_linkresolver_new(); archive_entry_linkresolver_set_strategy(cpio->linkresolver, archive_format(cpio->archive)); + /* + * The main loop: Copy each file into the output archive. + */ r = archive_write_open_file(cpio->archive, cpio->filename); if (r != ARCHIVE_OK) - cpio_errc(1, 0, archive_error_string(cpio->archive)); - lr = process_lines_init("-", cpio->line_separator); - while ((p = process_lines_next(lr)) != NULL) + lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); + lr = lafe_line_reader("-", cpio->option_null); + while ((p = lafe_line_reader_next(lr)) != NULL) file_to_archive(cpio, p); - process_lines_free(lr); + lafe_line_reader_free(lr); /* * The hardlink detection may have queued up a couple of entries @@ -402,12 +528,13 @@ mode_out(struct cpio *cpio) r = archive_write_close(cpio->archive); if (r != ARCHIVE_OK) - cpio_errc(1, 0, archive_error_string(cpio->archive)); + lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); if (!cpio->quiet) { - blocks = (archive_position_uncompressed(cpio->archive) + 511) - / 512; - fprintf(stderr, "%lu %s\n", blocks, + int64_t blocks = + (archive_position_uncompressed(cpio->archive) + 511) + / 512; + fprintf(stderr, "%lu %s\n", (unsigned long)blocks, blocks == 1 ? "block" : "blocks"); } archive_write_finish(cpio->archive); @@ -421,51 +548,37 @@ mode_out(struct cpio *cpio) static int file_to_archive(struct cpio *cpio, const char *srcpath) { - struct stat st; const char *destpath; struct archive_entry *entry, *spare; size_t len; const char *p; - int lnklen; int r; /* * Create an archive_entry describing the source file. + * */ entry = archive_entry_new(); if (entry == NULL) - cpio_errc(1, 0, "Couldn't allocate entry"); + lafe_errc(1, 0, "Couldn't allocate entry"); archive_entry_copy_sourcepath(entry, srcpath); - - /* Get stat information. */ - if (cpio->option_follow_links) - r = stat(srcpath, &st); - else - r = lstat(srcpath, &st); - if (r != 0) { - cpio_warnc(errno, "Couldn't stat \"%s\"", srcpath); - archive_entry_free(entry); - return (0); + r = archive_read_disk_entry_from_file(cpio->archive_read_disk, + entry, -1, NULL); + if (r < ARCHIVE_FAILED) + lafe_errc(1, 0, "%s", + archive_error_string(cpio->archive_read_disk)); + if (r < ARCHIVE_OK) + lafe_warnc(0, "%s", + archive_error_string(cpio->archive_read_disk)); + if (r <= ARCHIVE_FAILED) { + cpio->return_value = 1; + return (r); } if (cpio->uid_override >= 0) - st.st_uid = cpio->uid_override; + archive_entry_set_uid(entry, cpio->uid_override); if (cpio->gid_override >= 0) - st.st_gid = cpio->uid_override; - archive_entry_copy_stat(entry, &st); - - /* If its a symlink, pull the target. */ - if (S_ISLNK(st.st_mode)) { - lnklen = readlink(srcpath, cpio->buff, cpio->buff_size); - if (lnklen < 0) { - cpio_warnc(errno, - "%s: Couldn't read symbolic link", srcpath); - archive_entry_free(entry); - return (0); - } - cpio->buff[lnklen] = 0; - archive_entry_set_symlink(entry, cpio->buff); - } + archive_entry_set_gid(entry, cpio->gid_override); /* * Generate a destination path for this entry. @@ -484,7 +597,7 @@ file_to_archive(struct cpio *cpio, const char *srcpath) free(cpio->pass_destpath); cpio->pass_destpath = malloc(cpio->pass_destpath_alloc); if (cpio->pass_destpath == NULL) - cpio_errc(1, ENOMEM, + lafe_errc(1, ENOMEM, "Can't allocate path buffer"); } strcpy(cpio->pass_destpath, cpio->destdir); @@ -505,18 +618,18 @@ file_to_archive(struct cpio *cpio, const char *srcpath) */ spare = NULL; if (cpio->linkresolver != NULL - && !S_ISDIR(st.st_mode)) { + && archive_entry_filetype(entry) != AE_IFDIR) { archive_entry_linkify(cpio->linkresolver, &entry, &spare); } if (entry != NULL) { r = entry_to_archive(cpio, entry); archive_entry_free(entry); - } - if (spare != NULL) { - if (r == 0) - r = entry_to_archive(cpio, spare); - archive_entry_free(spare); + if (spare != NULL) { + if (r == 0) + r = entry_to_archive(cpio, spare); + archive_entry_free(spare); + } } return (r); } @@ -550,7 +663,7 @@ entry_to_archive(struct cpio *cpio, struct archive_entry *entry) /* Save the original entry in case we need it later. */ t = archive_entry_clone(entry); if (t == NULL) - cpio_errc(1, ENOMEM, "Can't create link"); + lafe_errc(1, ENOMEM, "Can't create link"); /* Note: link(2) doesn't create parent directories, * so we use archive_write_header() instead as a * convenience. */ @@ -560,15 +673,15 @@ entry_to_archive(struct cpio *cpio, struct archive_entry *entry) r = archive_write_header(cpio->archive, t); archive_entry_free(t); if (r != ARCHIVE_OK) - cpio_warnc(archive_errno(cpio->archive), - archive_error_string(cpio->archive)); + lafe_warnc(archive_errno(cpio->archive), + "%s", archive_error_string(cpio->archive)); if (r == ARCHIVE_FATAL) exit(1); #ifdef EXDEV if (r != ARCHIVE_OK && archive_errno(cpio->archive) == EXDEV) { /* Cross-device link: Just fall through and use * the original entry to copy the file over. */ - cpio_warnc(0, "Copying file instead"); + lafe_warnc(0, "Copying file instead"); } else #endif return (0); @@ -580,9 +693,9 @@ entry_to_archive(struct cpio *cpio, struct archive_entry *entry) */ if (archive_entry_filetype(entry) == AE_IFREG) { if (archive_entry_size(entry) > 0) { - fd = open(srcpath, O_RDONLY); + fd = open(srcpath, O_RDONLY | O_BINARY); if (fd < 0) { - cpio_warnc(errno, + lafe_warnc(errno, "%s: could not open file", srcpath); goto cleanup; } @@ -594,9 +707,9 @@ entry_to_archive(struct cpio *cpio, struct archive_entry *entry) r = archive_write_header(cpio->archive, entry); if (r != ARCHIVE_OK) - cpio_warnc(archive_errno(cpio->archive), + lafe_warnc(archive_errno(cpio->archive), "%s: %s", - destpath, + srcpath, archive_error_string(cpio->archive)); if (r == ARCHIVE_FATAL) @@ -608,17 +721,17 @@ entry_to_archive(struct cpio *cpio, struct archive_entry *entry) r = archive_write_data(cpio->archive, cpio->buff, bytes_read); if (r < 0) - cpio_errc(1, archive_errno(cpio->archive), - archive_error_string(cpio->archive)); + lafe_errc(1, archive_errno(cpio->archive), + "%s", archive_error_string(cpio->archive)); if (r < bytes_read) { - cpio_warnc(0, + lafe_warnc(0, "Truncated write; file may have grown while being archived."); } bytes_read = read(fd, cpio->buff, cpio->buff_size); } } - restore_time(cpio, entry, srcpath, fd); + fd = restore_time(cpio, entry, srcpath, fd); cleanup: if (cpio->verbose) @@ -628,7 +741,7 @@ cleanup: return (0); } -static void +static int restore_time(struct cpio *cpio, struct archive_entry *entry, const char *name, int fd) { @@ -638,17 +751,20 @@ restore_time(struct cpio *cpio, struct archive_entry *entry, (void)cpio; /* UNUSED */ (void)entry; /* UNUSED */ (void)name; /* UNUSED */ - (void)fd; /* UNUSED */ if (!warned) - cpio_warnc(0, "Can't restore access times on this platform"); + lafe_warnc(0, "Can't restore access times on this platform"); warned = 1; - return; + return (fd); +#else +#if defined(_WIN32) && !defined(__CYGWIN__) + struct __timeval times[2]; #else struct timeval times[2]; +#endif if (!cpio->option_atime_restore) - return; + return (fd); times[1].tv_sec = archive_entry_mtime(entry); times[1].tv_usec = archive_entry_mtime_nsec(entry) / 1000; @@ -656,18 +772,28 @@ restore_time(struct cpio *cpio, struct archive_entry *entry, times[0].tv_sec = archive_entry_atime(entry); times[0].tv_usec = archive_entry_atime_nsec(entry) / 1000; -#ifdef HAVE_FUTIMES +#if defined(HAVE_FUTIMES) && !defined(__CYGWIN__) if (fd >= 0 && futimes(fd, times) == 0) - return; + return (fd); #endif + /* + * Some platform cannot restore access times if the file descriptor + * is still opened. + */ + if (fd >= 0) { + close(fd); + fd = -1; + } #ifdef HAVE_LUTIMES if (lutimes(name, times) != 0) #else - if (!S_ISLNK(archive_entry_mode(entry)) && utimes(name, times) != 0) + if ((AE_IFLNK != archive_entry_filetype(entry)) + && utimes(name, times) != 0) #endif - cpio_warnc(errno, "Can't update time for %s", name); + lafe_warnc(errno, "Can't update time for %s", name); #endif + return (fd); } @@ -678,33 +804,32 @@ mode_in(struct cpio *cpio) struct archive_entry *entry; struct archive *ext; const char *destpath; - unsigned long blocks; int r; ext = archive_write_disk_new(); if (ext == NULL) - cpio_errc(1, 0, "Couldn't allocate restore object"); + lafe_errc(1, 0, "Couldn't allocate restore object"); r = archive_write_disk_set_options(ext, cpio->extract_flags); if (r != ARCHIVE_OK) - cpio_errc(1, 0, archive_error_string(ext)); + lafe_errc(1, 0, "%s", archive_error_string(ext)); a = archive_read_new(); if (a == NULL) - cpio_errc(1, 0, "Couldn't allocate archive object"); + lafe_errc(1, 0, "Couldn't allocate archive object"); archive_read_support_compression_all(a); archive_read_support_format_all(a); if (archive_read_open_file(a, cpio->filename, cpio->bytes_per_block)) - cpio_errc(1, archive_errno(a), - archive_error_string(a)); + lafe_errc(1, archive_errno(a), + "%s", archive_error_string(a)); for (;;) { r = archive_read_next_header(a, &entry); if (r == ARCHIVE_EOF) break; if (r != ARCHIVE_OK) { - cpio_errc(1, archive_errno(a), - archive_error_string(a)); + lafe_errc(1, archive_errno(a), + "%s", archive_error_string(a)); } - if (excluded(cpio, archive_entry_pathname(entry))) + if (lafe_excluded(cpio->matching, archive_entry_pathname(entry))) continue; if (cpio->option_rename) { destpath = cpio_rename(archive_entry_pathname(entry)); @@ -725,28 +850,34 @@ mode_in(struct cpio *cpio) archive_entry_pathname(entry), archive_error_string(ext)); } else if (archive_entry_size(entry) > 0) { - r = copy_data(a, ext); + r = extract_data(a, ext); + if (r != ARCHIVE_OK) + cpio->return_value = 1; } } r = archive_read_close(a); if (r != ARCHIVE_OK) - cpio_errc(1, 0, archive_error_string(a)); + lafe_errc(1, 0, "%s", archive_error_string(a)); r = archive_write_close(ext); if (r != ARCHIVE_OK) - cpio_errc(1, 0, archive_error_string(ext)); + lafe_errc(1, 0, "%s", archive_error_string(ext)); if (!cpio->quiet) { - blocks = (archive_position_uncompressed(a) + 511) + int64_t blocks = (archive_position_uncompressed(a) + 511) / 512; - fprintf(stderr, "%lu %s\n", blocks, + fprintf(stderr, "%lu %s\n", (unsigned long)blocks, blocks == 1 ? "block" : "blocks"); } archive_read_finish(a); archive_write_finish(ext); - exit(0); + exit(cpio->return_value); } +/* + * Exits if there's a fatal error. Returns ARCHIVE_OK + * if everything is kosher. + */ static int -copy_data(struct archive *ar, struct archive *aw) +extract_data(struct archive *ar, struct archive *aw) { int r; size_t size; @@ -758,14 +889,14 @@ copy_data(struct archive *ar, struct archive *aw) if (r == ARCHIVE_EOF) return (ARCHIVE_OK); if (r != ARCHIVE_OK) { - cpio_warnc(archive_errno(ar), + lafe_warnc(archive_errno(ar), "%s", archive_error_string(ar)); - return (r); + exit(1); } r = archive_write_data_block(aw, block, size, offset); if (r != ARCHIVE_OK) { - cpio_warnc(archive_errno(aw), - archive_error_string(aw)); + lafe_warnc(archive_errno(aw), + "%s", archive_error_string(aw)); return (r); } } @@ -776,59 +907,130 @@ mode_list(struct cpio *cpio) { struct archive *a; struct archive_entry *entry; - unsigned long blocks; int r; a = archive_read_new(); if (a == NULL) - cpio_errc(1, 0, "Couldn't allocate archive object"); + lafe_errc(1, 0, "Couldn't allocate archive object"); archive_read_support_compression_all(a); archive_read_support_format_all(a); if (archive_read_open_file(a, cpio->filename, cpio->bytes_per_block)) - cpio_errc(1, archive_errno(a), - archive_error_string(a)); + lafe_errc(1, archive_errno(a), + "%s", archive_error_string(a)); for (;;) { r = archive_read_next_header(a, &entry); if (r == ARCHIVE_EOF) break; if (r != ARCHIVE_OK) { - cpio_errc(1, archive_errno(a), - archive_error_string(a)); + lafe_errc(1, archive_errno(a), + "%s", archive_error_string(a)); } - if (excluded(cpio, archive_entry_pathname(entry))) + if (lafe_excluded(cpio->matching, archive_entry_pathname(entry))) continue; - if (cpio->verbose) { - /* TODO: uname/gname lookups */ - /* TODO: Clean this up. */ - fprintf(stdout, - "%s%3d %8s%8s " CPIO_FILESIZE_PRINTF " %s\n", - archive_entry_strmode(entry), - archive_entry_nlink(entry), - archive_entry_uname(entry), - archive_entry_gname(entry), - (CPIO_FILESIZE_TYPE)archive_entry_size(entry), - archive_entry_pathname(entry)); - } else + if (cpio->verbose) + list_item_verbose(cpio, entry); + else fprintf(stdout, "%s\n", archive_entry_pathname(entry)); } r = archive_read_close(a); if (r != ARCHIVE_OK) - cpio_errc(1, 0, archive_error_string(a)); + lafe_errc(1, 0, "%s", archive_error_string(a)); if (!cpio->quiet) { - blocks = (archive_position_uncompressed(a) + 511) + int64_t blocks = (archive_position_uncompressed(a) + 511) / 512; - fprintf(stderr, "%lu %s\n", blocks, + fprintf(stderr, "%lu %s\n", (unsigned long)blocks, blocks == 1 ? "block" : "blocks"); } archive_read_finish(a); exit(0); } +/* + * Display information about the current file. + * + * The format here roughly duplicates the output of 'ls -l'. + * This is based on SUSv2, where 'tar tv' is documented as + * listing additional information in an "unspecified format," + * and 'pax -l' is documented as using the same format as 'ls -l'. + */ +static void +list_item_verbose(struct cpio *cpio, struct archive_entry *entry) +{ + char size[32]; + char date[32]; + char uids[16], gids[16]; + const char *uname, *gname; + FILE *out = stdout; + const char *fmt; + time_t mtime; + static time_t now; + + if (!now) + time(&now); + + if (cpio->option_numeric_uid_gid) { + /* Format numeric uid/gid for display. */ + strcpy(uids, cpio_i64toa(archive_entry_uid(entry))); + uname = uids; + strcpy(gids, cpio_i64toa(archive_entry_gid(entry))); + gname = gids; + } else { + /* Use uname if it's present, else lookup name from uid. */ + uname = archive_entry_uname(entry); + if (uname == NULL) + uname = lookup_uname(cpio, archive_entry_uid(entry)); + /* Use gname if it's present, else lookup name from gid. */ + gname = archive_entry_gname(entry); + if (gname == NULL) + gname = lookup_gname(cpio, archive_entry_gid(entry)); + } + + /* Print device number or file size. */ + if (archive_entry_filetype(entry) == AE_IFCHR + || archive_entry_filetype(entry) == AE_IFBLK) { + snprintf(size, sizeof(size), "%lu,%lu", + (unsigned long)archive_entry_rdevmajor(entry), + (unsigned long)archive_entry_rdevminor(entry)); + } else { + strcpy(size, cpio_i64toa(archive_entry_size(entry))); + } + + /* Format the time using 'ls -l' conventions. */ + mtime = archive_entry_mtime(entry); +#if defined(_WIN32) && !defined(__CYGWIN__) + /* Windows' strftime function does not support %e format. */ + if (mtime - now > 365*86400/2 + || mtime - now < -365*86400/2) + fmt = cpio->day_first ? "%d %b %Y" : "%b %d %Y"; + else + fmt = cpio->day_first ? "%d %b %H:%M" : "%b %d %H:%M"; +#else + if (abs(mtime - now) > (365/2)*86400) + fmt = cpio->day_first ? "%e %b %Y" : "%b %e %Y"; + else + fmt = cpio->day_first ? "%e %b %H:%M" : "%b %e %H:%M"; +#endif + strftime(date, sizeof(date), fmt, localtime(&mtime)); + + fprintf(out, "%s%3d %-8s %-8s %8s %12s %s", + archive_entry_strmode(entry), + archive_entry_nlink(entry), + uname, gname, size, date, + archive_entry_pathname(entry)); + + /* Extra information for links. */ + if (archive_entry_hardlink(entry)) /* Hard link */ + fprintf(out, " link to %s", archive_entry_hardlink(entry)); + else if (archive_entry_symlink(entry)) /* Symbolic link */ + fprintf(out, " -> %s", archive_entry_symlink(entry)); + fprintf(out, "\n"); +} + static void mode_pass(struct cpio *cpio, const char *destdir) { - struct line_reader *lr; + struct lafe_line_reader *lr; const char *p; int r; @@ -840,21 +1042,40 @@ mode_pass(struct cpio *cpio, const char *destdir) cpio->archive = archive_write_disk_new(); if (cpio->archive == NULL) - cpio_errc(1, 0, "Failed to allocate archive object"); + lafe_errc(1, 0, "Failed to allocate archive object"); r = archive_write_disk_set_options(cpio->archive, cpio->extract_flags); if (r != ARCHIVE_OK) - cpio_errc(1, 0, archive_error_string(cpio->archive)); + lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); cpio->linkresolver = archive_entry_linkresolver_new(); archive_write_disk_set_standard_lookup(cpio->archive); - lr = process_lines_init("-", cpio->line_separator); - while ((p = process_lines_next(lr)) != NULL) + + cpio->archive_read_disk = archive_read_disk_new(); + if (cpio->archive_read_disk == NULL) + lafe_errc(1, 0, "Failed to allocate archive object"); + if (cpio->option_follow_links) + archive_read_disk_set_symlink_logical(cpio->archive_read_disk); + else + archive_read_disk_set_symlink_physical(cpio->archive_read_disk); + archive_read_disk_set_standard_lookup(cpio->archive_read_disk); + + lr = lafe_line_reader("-", cpio->option_null); + while ((p = lafe_line_reader_next(lr)) != NULL) file_to_archive(cpio, p); - process_lines_free(lr); + lafe_line_reader_free(lr); archive_entry_linkresolver_free(cpio->linkresolver); r = archive_write_close(cpio->archive); if (r != ARCHIVE_OK) - cpio_errc(1, 0, archive_error_string(cpio->archive)); + lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); + + if (!cpio->quiet) { + int64_t blocks = + (archive_position_uncompressed(cpio->archive) + 511) + / 512; + fprintf(stderr, "%lu %s\n", (unsigned long)blocks, + blocks == 1 ? "block" : "blocks"); + } + archive_write_finish(cpio->archive); } @@ -901,126 +1122,146 @@ cpio_rename(const char *name) return (ret); } +static void +free_cache(struct name_cache *cache) +{ + size_t i; + + if (cache != NULL) { + for (i = 0; i < cache->size; i++) + free(cache->cache[i].name); + free(cache); + } +} /* - * Read lines from file and do something with each one. If option_null - * is set, lines are terminated with zero bytes; otherwise, they're - * terminated with newlines. - * - * This uses a self-sizing buffer to handle arbitrarily-long lines. + * Lookup uname/gname from uid/gid, return NULL if no match. */ -struct line_reader { - FILE *f; - char *buff, *buff_end, *line_start, *line_end, *p; - char *pathname; - size_t buff_length; - int separator; - int ret; -}; - -struct line_reader * -process_lines_init(const char *pathname, char separator) +static const char * +lookup_name(struct cpio *cpio, struct name_cache **name_cache_variable, + int (*lookup_fn)(struct cpio *, const char **, id_t), id_t id) { - struct line_reader *lr; + char asnum[16]; + struct name_cache *cache; + const char *name; + int slot; + + + if (*name_cache_variable == NULL) { + *name_cache_variable = malloc(sizeof(struct name_cache)); + if (*name_cache_variable == NULL) + lafe_errc(1, ENOMEM, "No more memory"); + memset(*name_cache_variable, 0, sizeof(struct name_cache)); + (*name_cache_variable)->size = name_cache_size; + } - lr = calloc(1, sizeof(*lr)); - if (lr == NULL) - cpio_errc(1, ENOMEM, "Can't open %s", pathname); + cache = *name_cache_variable; + cache->probes++; - lr->separator = separator; - lr->pathname = strdup(pathname); + slot = id % cache->size; + if (cache->cache[slot].name != NULL) { + if (cache->cache[slot].id == id) { + cache->hits++; + return (cache->cache[slot].name); + } + free(cache->cache[slot].name); + cache->cache[slot].name = NULL; + } - if (strcmp(pathname, "-") == 0) - lr->f = stdin; - else - lr->f = fopen(pathname, "r"); - if (lr->f == NULL) - cpio_errc(1, errno, "Couldn't open %s", pathname); - lr->buff_length = 8192; - lr->buff = malloc(lr->buff_length); - if (lr->buff == NULL) - cpio_errc(1, ENOMEM, "Can't read %s", pathname); - lr->line_start = lr->line_end = lr->buff_end = lr->buff; - - return (lr); + if (lookup_fn(cpio, &name, id) == 0) { + if (name == NULL || name[0] == '\0') { + /* If lookup failed, format it as a number. */ + snprintf(asnum, sizeof(asnum), "%u", (unsigned)id); + name = asnum; + } + cache->cache[slot].name = strdup(name); + if (cache->cache[slot].name != NULL) { + cache->cache[slot].id = id; + return (cache->cache[slot].name); + } + /* + * Conveniently, NULL marks an empty slot, so + * if the strdup() fails, we've just failed to + * cache it. No recovery necessary. + */ + } + return (NULL); } -const char * -process_lines_next(struct line_reader *lr) +static const char * +lookup_uname(struct cpio *cpio, uid_t uid) { - size_t bytes_wanted, bytes_read, new_buff_size; - char *line_start, *p; + return (lookup_name(cpio, &cpio->uname_cache, + &lookup_uname_helper, (id_t)uid)); +} - for (;;) { - /* If there's a line in the buffer, return it immediately. */ - while (lr->line_end < lr->buff_end) { - if (*lr->line_end == lr->separator) { - *lr->line_end = '\0'; - line_start = lr->line_start; - lr->line_start = lr->line_end + 1; - lr->line_end = lr->line_start; - return (line_start); - } else - lr->line_end++; - } +static int +lookup_uname_helper(struct cpio *cpio, const char **name, id_t id) +{ + struct passwd *pwent; - /* If we're at end-of-file, process the final data. */ - if (lr->f == NULL) { - /* If there's more text, return one last line. */ - if (lr->line_end > lr->line_start) { - *lr->line_end = '\0'; - line_start = lr->line_start; - lr->line_start = lr->line_end + 1; - lr->line_end = lr->line_start; - return (line_start); - } - /* Otherwise, we're done. */ - return (NULL); - } + (void)cpio; /* UNUSED */ - /* Buffer only has part of a line. */ - if (lr->line_start > lr->buff) { - /* Move a leftover fractional line to the beginning. */ - memmove(lr->buff, lr->line_start, - lr->buff_end - lr->line_start); - lr->buff_end -= lr->line_start - lr->buff; - lr->line_end -= lr->line_start - lr->buff; - lr->line_start = lr->buff; - } else { - /* Line is too big; enlarge the buffer. */ - new_buff_size = lr->buff_length * 2; - if (new_buff_size <= lr->buff_length) - cpio_errc(1, ENOMEM, - "Line too long in %s", lr->pathname); - lr->buff_length = new_buff_size; - p = realloc(lr->buff, new_buff_size); - if (p == NULL) - cpio_errc(1, ENOMEM, - "Line too long in %s", lr->pathname); - lr->buff_end = p + (lr->buff_end - lr->buff); - lr->line_end = p + (lr->line_end - lr->buff); - lr->line_start = lr->buff = p; - } + errno = 0; + pwent = getpwuid((uid_t)id); + if (pwent == NULL) { + *name = NULL; + if (errno != 0 && errno != ENOENT) + lafe_warnc(errno, "getpwuid(%d) failed", id); + return (errno); + } - /* Get some more data into the buffer. */ - bytes_wanted = lr->buff + lr->buff_length - lr->buff_end; - bytes_read = fread(lr->buff_end, 1, bytes_wanted, lr->f); - lr->buff_end += bytes_read; - - if (ferror(lr->f)) - cpio_errc(1, errno, "Can't read %s", lr->pathname); - if (feof(lr->f)) { - if (lr->f != stdin) - fclose(lr->f); - lr->f = NULL; - } + *name = pwent->pw_name; + return (0); +} + +static const char * +lookup_gname(struct cpio *cpio, gid_t gid) +{ + return (lookup_name(cpio, &cpio->gname_cache, + &lookup_gname_helper, (id_t)gid)); +} + +static int +lookup_gname_helper(struct cpio *cpio, const char **name, id_t id) +{ + struct group *grent; + + (void)cpio; /* UNUSED */ + + errno = 0; + grent = getgrgid((gid_t)id); + if (grent == NULL) { + *name = NULL; + if (errno != 0) + lafe_warnc(errno, "getgrgid(%d) failed", id); + return (errno); } + + *name = grent->gr_name; + return (0); } -void -process_lines_free(struct line_reader *lr) +/* + * It would be nice to just use printf() for formatting large numbers, + * but the compatibility problems are a big headache. Hence the + * following simple utility function. + */ +const char * +cpio_i64toa(int64_t n0) { - free(lr->buff); - free(lr->pathname); - free(lr); + // 2^64 =~ 1.8 * 10^19, so 20 decimal digits suffice. + // We also need 1 byte for '-' and 1 for '\0'. + static char buff[22]; + int64_t n = n0 < 0 ? -n0 : n0; + char *p = buff + sizeof(buff); + + *--p = '\0'; + do { + *--p = '0' + (int)(n % 10); + n /= 10; + } while (n > 0); + if (n0 < 0) + *--p = '-'; + return p; } diff --git a/archivers/libarchive/files/cpio/cpio.h b/archivers/libarchive/files/cpio/cpio.h index 3afd9520309..3eed83494b8 100644 --- a/archivers/libarchive/files/cpio/cpio.h +++ b/archivers/libarchive/files/cpio/cpio.h @@ -22,7 +22,7 @@ * (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: src/usr.bin/cpio/cpio.h,v 1.2 2008/06/21 02:20:20 kientzle Exp $ + * $FreeBSD: src/usr.bin/cpio/cpio.h,v 1.7 2008/12/06 07:30:40 kientzle Exp $ */ #ifndef CPIO_H_INCLUDED @@ -31,7 +31,7 @@ #include "cpio_platform.h" #include <stdio.h> -#define DEFAULT_BYTES_PER_BLOCK (20*512) +#include "matching.h" /* * The internal state for the "cpio" program. @@ -42,8 +42,11 @@ * functions. */ struct cpio { + /* Option parsing */ + const char *optarg; + /* Options */ - char *filename; + const char *filename; char mode; /* -i -o -p */ char compress; /* -j, -y, or -z */ const char *format; /* -H format */ @@ -53,58 +56,54 @@ struct cpio { int extract_flags; /* Flags for extract operation */ char symlink_mode; /* H or L, per BSD conventions */ const char *compress_program; - char line_separator; /* --null ? '\0' : '\n' */ int option_append; /* -A, only relevant for -o */ int option_atime_restore; /* -a */ int option_follow_links; /* -L */ int option_link; /* -l */ int option_list; /* -t */ + char option_null; /* --null */ + int option_numeric_uid_gid; /* -n */ int option_rename; /* -r */ char *destdir; size_t pass_destpath_alloc; char *pass_destpath; int uid_override; int gid_override; + int day_first; /* true if locale prefers day/mon */ /* If >= 0, then close this when done. */ int fd; /* Miscellaneous state information */ struct archive *archive; + struct archive *archive_read_disk; int argc; char **argv; int return_value; /* Value returned by main() */ struct archive_entry_linkresolver *linkresolver; + struct name_cache *uname_cache; + struct name_cache *gname_cache; + /* Work data. */ - struct matching *matching; + struct lafe_matching *matching; char *buff; size_t buff_size; }; -/* Name of this program; used in error reporting, initialized in main(). */ -const char *cpio_progname; - -void cpio_errc(int _eval, int _code, const char *fmt, ...); -void cpio_warnc(int _code, const char *fmt, ...); - -int owner_parse(const char *, int *, int *); +const char *owner_parse(const char *, int *, int *); /* Fake short equivalents for long options that otherwise lack them. */ enum { OPTION_INSECURE = 1, + OPTION_LZMA, + OPTION_NO_PRESERVE_OWNER, + OPTION_PRESERVE_OWNER, OPTION_QUIET, OPTION_VERSION }; -struct line_reader; - -struct line_reader *process_lines_init(const char *, char separator); -const char *process_lines_next(struct line_reader *); -void process_lines_free(struct line_reader *); - int cpio_getopt(struct cpio *cpio); -int include_from_file(struct cpio *, const char *); #endif diff --git a/archivers/libarchive/files/cpio/cpio_platform.h b/archivers/libarchive/files/cpio/cpio_platform.h index 31451982982..31d9a738fcb 100644 --- a/archivers/libarchive/files/cpio/cpio_platform.h +++ b/archivers/libarchive/files/cpio/cpio_platform.h @@ -22,7 +22,7 @@ * (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$ + * $FreeBSD: src/usr.bin/cpio/cpio_platform.h,v 1.2 2008/12/06 07:15:42 kientzle Exp $ */ /* @@ -37,19 +37,18 @@ #if defined(PLATFORM_CONFIG_H) /* Use hand-built config.h in environments that need it. */ #include PLATFORM_CONFIG_H -#elif defined(HAVE_CONFIG_H) -/* Most POSIX platforms use the 'configure' script to build config.h */ -#include "../config.h" #else -/* Warn if cpio hasn't been (automatically or manually) configured. */ -#error Oops: No config.h and no built-in configuration in cpio_platform.h. -#endif /* !HAVE_CONFIG_H */ +/* Read config.h or die trying. */ +#include "config.h" +#endif -/* No non-FreeBSD platform will have __FBSDID, so just define it here. */ -#ifdef __FreeBSD__ -#include <sys/cdefs.h> /* For __FBSDID */ -#else -/* Just leaving this macro replacement empty leads to a dangling semicolon. */ +/* Get a real definition for __FBSDID if we can */ +#if HAVE_SYS_CDEFS_H +#include <sys/cdefs.h> +#endif + +/* If not, define it so as to avoid dangling semicolons. */ +#ifndef __FBSDID #define __FBSDID(a) struct _undefined_hack #endif @@ -63,22 +62,16 @@ #include "archive_entry.h" #endif -/* - * We need to be able to display a filesize using printf(). The type - * and format string here must be compatible with one another and - * large enough for any file. - */ -#if HAVE_UINTMAX_T -#define CPIO_FILESIZE_TYPE uintmax_t -#define CPIO_FILESIZE_PRINTF "%ju" +/* How to mark functions that don't return. */ +#if defined(__GNUC__) && (__GNUC__ > 2 || \ + (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)) +#define __LA_DEAD __attribute__((__noreturn__)) #else -#if HAVE_UNSIGNED_LONG_LONG -#define CPIO_FILESIZE_TYPE unsigned long long -#define CPIO_FILESIZE_PRINTF "%llu" -#else -#define CPIO_FILESIZE_TYPE unsigned long -#define CPIO_FILESIZE_PRINTF "%lu" +#define __LA_DEAD #endif + +#if defined(_WIN32) && !defined(__CYGWIN__) +#include "cpio_windows.h" #endif #endif /* !CPIO_PLATFORM_H_INCLUDED */ diff --git a/archivers/libarchive/files/cpio/cpio_windows.c b/archivers/libarchive/files/cpio/cpio_windows.c new file mode 100644 index 00000000000..420e01d5e19 --- /dev/null +++ b/archivers/libarchive/files/cpio/cpio_windows.c @@ -0,0 +1,338 @@ +/*- + * Copyright (c) 2009 Michihiro NAKAJIMA + * 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. + * 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. + * + * $FreeBSD$ + */ + +#if defined(_WIN32) && !defined(__CYGWIN__) + +#include "cpio_platform.h" +#include <ctype.h> +#include <errno.h> +#include <fcntl.h> +#include <io.h> +#include <stddef.h> +#ifdef HAVE_SYS_UTIME_H +#include <sys/utime.h> +#endif +#include <sys/stat.h> +#include <process.h> +#include <stdlib.h> +#include <wchar.h> +#include <windows.h> +#include <sddl.h> + +#include "cpio.h" +#include "err.h" + +#define EPOC_TIME (116444736000000000ULL) + +static void cpio_dosmaperr(unsigned long); + +/* + * Prepend "\\?\" to the path name and convert it to unicode to permit + * an extended-length path for a maximum total path length of 32767 + * characters. + * see also http://msdn.microsoft.com/en-us/library/aa365247.aspx + */ +static wchar_t * +permissive_name(const char *name) +{ + wchar_t *wn, *wnp; + wchar_t *ws, *wsp; + DWORD l, len, slen, alloclen; + int unc; + + len = (DWORD)strlen(name); + wn = malloc((len + 1) * sizeof(wchar_t)); + if (wn == NULL) + return (NULL); + l = MultiByteToWideChar(CP_ACP, 0, name, len, wn, len); + if (l == 0) { + free(wn); + return (NULL); + } + wn[l] = L'\0'; + + /* Get a full path names */ + l = GetFullPathNameW(wn, 0, NULL, NULL); + if (l == 0) { + free(wn); + return (NULL); + } + wnp = malloc(l * sizeof(wchar_t)); + if (wnp == NULL) { + free(wn); + return (NULL); + } + len = GetFullPathNameW(wn, l, wnp, NULL); + free(wn); + wn = wnp; + + if (wnp[0] == L'\\' && wnp[1] == L'\\' && + wnp[2] == L'?' && wnp[3] == L'\\') + /* We have already permissive names. */ + return (wn); + + if (wnp[0] == L'\\' && wnp[1] == L'\\' && + wnp[2] == L'.' && wnp[3] == L'\\') { + /* Device names */ + if (((wnp[4] >= L'a' && wnp[4] <= L'z') || + (wnp[4] >= L'A' && wnp[4] <= L'Z')) && + wnp[5] == L':' && wnp[6] == L'\\') + wnp[2] = L'?';/* Not device names. */ + return (wn); + } + + unc = 0; + if (wnp[0] == L'\\' && wnp[1] == L'\\' && wnp[2] != L'\\') { + wchar_t *p = &wnp[2]; + + /* Skip server-name letters. */ + while (*p != L'\\' && *p != L'\0') + ++p; + if (*p == L'\\') { + wchar_t *rp = ++p; + /* Skip share-name letters. */ + while (*p != L'\\' && *p != L'\0') + ++p; + if (*p == L'\\' && p != rp) { + /* Now, match patterns such as + * "\\server-name\share-name\" */ + wnp += 2; + len -= 2; + unc = 1; + } + } + } + + alloclen = slen = 4 + (unc * 4) + len + 1; + ws = wsp = malloc(slen * sizeof(wchar_t)); + if (ws == NULL) { + free(wn); + return (NULL); + } + /* prepend "\\?\" */ + wcsncpy(wsp, L"\\\\?\\", 4); + wsp += 4; + slen -= 4; + if (unc) { + /* append "UNC\" ---> "\\?\UNC\" */ + wcsncpy(wsp, L"UNC\\", 4); + wsp += 4; + slen -= 4; + } + wcsncpy(wsp, wnp, slen); + free(wn); + ws[alloclen - 1] = L'\0'; + return (ws); +} + +static HANDLE +cpio_CreateFile(const char *path, DWORD dwDesiredAccess, DWORD dwShareMode, + LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, + DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) +{ + wchar_t *wpath; + HANDLE handle; + + handle = CreateFileA(path, dwDesiredAccess, dwShareMode, + lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, + hTemplateFile); + if (handle != INVALID_HANDLE_VALUE) + return (handle); + if (GetLastError() != ERROR_PATH_NOT_FOUND) + return (handle); + wpath = permissive_name(path); + if (wpath == NULL) + return (handle); + handle = CreateFileW(wpath, dwDesiredAccess, dwShareMode, + lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, + hTemplateFile); + free(wpath); + return (handle); +} + +#define WINTIME(sec, usec) ((Int32x32To64(sec, 10000000) + EPOC_TIME) + (usec * 10)) +static int +__hutimes(HANDLE handle, const struct __timeval *times) +{ + ULARGE_INTEGER wintm; + FILETIME fatime, fmtime; + + wintm.QuadPart = WINTIME(times[0].tv_sec, times[0].tv_usec); + fatime.dwLowDateTime = wintm.LowPart; + fatime.dwHighDateTime = wintm.HighPart; + wintm.QuadPart = WINTIME(times[1].tv_sec, times[1].tv_usec); + fmtime.dwLowDateTime = wintm.LowPart; + fmtime.dwHighDateTime = wintm.HighPart; + if (SetFileTime(handle, NULL, &fatime, &fmtime) == 0) { + errno = EINVAL; + return (-1); + } + return (0); +} + +int +futimes(int fd, const struct __timeval *times) +{ + + return (__hutimes((HANDLE)_get_osfhandle(fd), times)); +} + +int +utimes(const char *name, const struct __timeval *times) +{ + int ret; + HANDLE handle; + + handle = cpio_CreateFile(name, GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS, NULL); + if (handle == INVALID_HANDLE_VALUE) { + cpio_dosmaperr(GetLastError()); + return (-1); + } + ret = __hutimes(handle, times); + CloseHandle(handle); + return (ret); +} + +/* + * The following function was modified from PostgreSQL sources and is + * subject to the copyright below. + */ +/*------------------------------------------------------------------------- + * + * win32error.c + * Map win32 error codes to errno values + * + * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group + * + * IDENTIFICATION + * $PostgreSQL: pgsql/src/port/win32error.c,v 1.4 2008/01/01 19:46:00 momjian Exp $ + * + *------------------------------------------------------------------------- + */ +/* +PostgreSQL Database Management System +(formerly known as Postgres, then as Postgres95) + +Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group + +Portions Copyright (c) 1994, The Regents of the University of California + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose, without fee, and without a written agreement +is hereby granted, provided that the above copyright notice and this +paragraph and the following two paragraphs appear in all copies. + +IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR +DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING +LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS +DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS +ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO +PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. +*/ + +static const struct { + DWORD winerr; + int doserr; +} doserrors[] = +{ + { ERROR_INVALID_FUNCTION, EINVAL }, + { ERROR_FILE_NOT_FOUND, ENOENT }, + { ERROR_PATH_NOT_FOUND, ENOENT }, + { ERROR_TOO_MANY_OPEN_FILES, EMFILE }, + { ERROR_ACCESS_DENIED, EACCES }, + { ERROR_INVALID_HANDLE, EBADF }, + { ERROR_ARENA_TRASHED, ENOMEM }, + { ERROR_NOT_ENOUGH_MEMORY, ENOMEM }, + { ERROR_INVALID_BLOCK, ENOMEM }, + { ERROR_BAD_ENVIRONMENT, E2BIG }, + { ERROR_BAD_FORMAT, ENOEXEC }, + { ERROR_INVALID_ACCESS, EINVAL }, + { ERROR_INVALID_DATA, EINVAL }, + { ERROR_INVALID_DRIVE, ENOENT }, + { ERROR_CURRENT_DIRECTORY, EACCES }, + { ERROR_NOT_SAME_DEVICE, EXDEV }, + { ERROR_NO_MORE_FILES, ENOENT }, + { ERROR_LOCK_VIOLATION, EACCES }, + { ERROR_SHARING_VIOLATION, EACCES }, + { ERROR_BAD_NETPATH, ENOENT }, + { ERROR_NETWORK_ACCESS_DENIED, EACCES }, + { ERROR_BAD_NET_NAME, ENOENT }, + { ERROR_FILE_EXISTS, EEXIST }, + { ERROR_CANNOT_MAKE, EACCES }, + { ERROR_FAIL_I24, EACCES }, + { ERROR_INVALID_PARAMETER, EINVAL }, + { ERROR_NO_PROC_SLOTS, EAGAIN }, + { ERROR_DRIVE_LOCKED, EACCES }, + { ERROR_BROKEN_PIPE, EPIPE }, + { ERROR_DISK_FULL, ENOSPC }, + { ERROR_INVALID_TARGET_HANDLE, EBADF }, + { ERROR_INVALID_HANDLE, EINVAL }, + { ERROR_WAIT_NO_CHILDREN, ECHILD }, + { ERROR_CHILD_NOT_COMPLETE, ECHILD }, + { ERROR_DIRECT_ACCESS_HANDLE, EBADF }, + { ERROR_NEGATIVE_SEEK, EINVAL }, + { ERROR_SEEK_ON_DEVICE, EACCES }, + { ERROR_DIR_NOT_EMPTY, ENOTEMPTY }, + { ERROR_NOT_LOCKED, EACCES }, + { ERROR_BAD_PATHNAME, ENOENT }, + { ERROR_MAX_THRDS_REACHED, EAGAIN }, + { ERROR_LOCK_FAILED, EACCES }, + { ERROR_ALREADY_EXISTS, EEXIST }, + { ERROR_FILENAME_EXCED_RANGE, ENOENT }, + { ERROR_NESTING_NOT_ALLOWED, EAGAIN }, + { ERROR_NOT_ENOUGH_QUOTA, ENOMEM } +}; + +static void +cpio_dosmaperr(unsigned long e) +{ + int i; + + if (e == 0) { + errno = 0; + return; + } + + for (i = 0; i < sizeof(doserrors); i++) { + if (doserrors[i].winerr == e) { + errno = doserrors[i].doserr; + return; + } + } + + /* fprintf(stderr, "unrecognized win32 error code: %lu", e); */ + errno = EINVAL; + return; +} +#endif diff --git a/archivers/libarchive/files/cpio/cpio_windows.h b/archivers/libarchive/files/cpio/cpio_windows.h new file mode 100644 index 00000000000..105bf69991d --- /dev/null +++ b/archivers/libarchive/files/cpio/cpio_windows.h @@ -0,0 +1,72 @@ +/*- + * Copyright (c) 2009 Michihiro NAKAJIMA + * 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. + * 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. + * + * $FreeBSD$ + */ +#ifndef CPIO_WINDOWS_H +#define CPIO_WINDOWS_H 1 + +#include <io.h> +#include <string.h> + +#define getgrgid(id) NULL +#define getgrnam(name) NULL +#define getpwnam(name) NULL +#define getpwuid(id) NULL + +#ifdef _MSC_VER +#define snprintf sprintf_s +#define strdup _strdup +#define open _open +#define read _read +#define close _close +#endif + +struct passwd { + char *pw_name; + uid_t pw_uid; + gid_t pw_gid; +}; + +struct group { + char *gr_name; + gid_t gr_gid; +}; + +struct _timeval64i32 { + time_t tv_sec; + long tv_usec; +}; +#define __timeval _timeval64i32 + +extern int futimes(int fd, const struct __timeval *times); +#ifndef HAVE_FUTIMES +#define HAVE_FUTIMES 1 +#endif +extern int utimes(const char *name, const struct __timeval *times); +#ifndef HAVE_UTIMES +#define HAVE_UTIMES 1 +#endif + +#endif /* CPIO_WINDOWS_H */ diff --git a/archivers/libarchive/files/cpio/test/CMakeLists.txt b/archivers/libarchive/files/cpio/test/CMakeLists.txt new file mode 100644 index 00000000000..a822bcdfba5 --- /dev/null +++ b/archivers/libarchive/files/cpio/test/CMakeLists.txt @@ -0,0 +1,79 @@ +############################################ +# +# How to build bsdcpio_test +# +############################################ +IF(ENABLE_CPIO AND ENABLE_TEST) + SET(bsdcpio_test_SOURCES + ../cmdline.c + ../../libarchive_fe/err.c + ../../libarchive_fe/pathmatch.c + main.c + test.h + test_0.c + test_basic.c + test_cmdline.c + test_format_newc.c + test_gcpio_compat.c + test_option_B_upper.c + test_option_C_upper.c + test_option_J_upper.c + test_option_L_upper.c + test_option_Z_upper.c + test_option_a.c + test_option_c.c + test_option_d.c + test_option_f.c + test_option_help.c + test_option_l.c + test_option_lzma.c + test_option_m.c + test_option_t.c + test_option_u.c + test_option_version.c + test_option_y.c + test_option_z.c + test_owner_parse.c + test_passthrough_dotdot.c + test_passthrough_reverse.c + test_pathmatch.c + ) + IF(WIN32 AND NOT CYGWIN) + LIST(APPEND bsdcpio_test_SOURCES ../cpio_windows.h) + ENDIF(WIN32 AND NOT CYGWIN) + + # + # Register target + # + ADD_EXECUTABLE(bsdcpio_test ${bsdcpio_test_SOURCES}) + SET_PROPERTY(TARGET bsdcpio_test PROPERTY COMPILE_DEFINITIONS LIST_H) + + # + # Generate list.h by grepping DEFINE_TEST() lines out of the C sources. + # + GENERATE_LIST_H(${CMAKE_CURRENT_BINARY_DIR}/list.h + ${CMAKE_CURRENT_LIST_FILE} ${bsdcpio_test_SOURCES}) + SET_PROPERTY(DIRECTORY APPEND PROPERTY INCLUDE_DIRECTORIES + ${CMAKE_CURRENT_BINARY_DIR}) + + # list.h has a line DEFINE_TEST(testname) for every + # test. We can use that to define the tests for cmake by + # defining a DEFINE_TEST macro and reading list.h in. + MACRO (DEFINE_TEST _testname) + ADD_TEST_28( + NAME bsdcpio_${_testname} + COMMAND bsdcpio_test -vv + -p $<TARGET_FILE:bsdcpio> + -r ${CMAKE_CURRENT_SOURCE_DIR} + ${_testname}) + ENDMACRO (DEFINE_TEST _testname) + + INCLUDE(${CMAKE_CURRENT_BINARY_DIR}/list.h) + + # Experimental new test handling + ADD_CUSTOM_TARGET(run_bsdcpio_test + COMMAND bsdcpio_test -p ${BSDCPIO} -r ${CMAKE_CURRENT_SOURCE_DIR}) + ADD_DEPENDENCIES(run_bsdcpio_test bsdcpio) + ADD_DEPENDENCIES(run_all_tests run_bsdcpio_test) +ENDIF(ENABLE_CPIO AND ENABLE_TEST) + diff --git a/archivers/libarchive/files/cpio/test/list.h b/archivers/libarchive/files/cpio/test/list.h index 42cc5588218..55968c363fa 100644 --- a/archivers/libarchive/files/cpio/test/list.h +++ b/archivers/libarchive/files/cpio/test/list.h @@ -1,15 +1,20 @@ DEFINE_TEST(test_0) DEFINE_TEST(test_basic) +DEFINE_TEST(test_cmdline) DEFINE_TEST(test_format_newc) DEFINE_TEST(test_gcpio_compat) -DEFINE_TEST(test_option_B) -DEFINE_TEST(test_option_L) +DEFINE_TEST(test_option_B_upper) +DEFINE_TEST(test_option_C_upper) +DEFINE_TEST(test_option_J_upper) +DEFINE_TEST(test_option_L_upper) +DEFINE_TEST(test_option_Z_upper) DEFINE_TEST(test_option_a) DEFINE_TEST(test_option_c) DEFINE_TEST(test_option_d) -DEFINE_TEST(test_option_ell) DEFINE_TEST(test_option_f) DEFINE_TEST(test_option_help) +DEFINE_TEST(test_option_l) +DEFINE_TEST(test_option_lzma) DEFINE_TEST(test_option_m) DEFINE_TEST(test_option_t) DEFINE_TEST(test_option_u) @@ -17,4 +22,6 @@ DEFINE_TEST(test_option_version) DEFINE_TEST(test_option_y) DEFINE_TEST(test_option_z) DEFINE_TEST(test_owner_parse) +DEFINE_TEST(test_passthrough_dotdot) +DEFINE_TEST(test_passthrough_reverse) DEFINE_TEST(test_pathmatch) diff --git a/archivers/libarchive/files/cpio/test/main.c b/archivers/libarchive/files/cpio/test/main.c index e0f8c2e1175..a8b68146eb7 100644 --- a/archivers/libarchive/files/cpio/test/main.c +++ b/archivers/libarchive/files/cpio/test/main.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003-2007 Tim Kientzle + * Copyright (c) 2003-2009 Tim Kientzle * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -23,12 +23,7 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* - * Various utility routines useful for test programs. - * Each test program is linked against this file. - */ #include "test.h" - #include <errno.h> #include <locale.h> #include <stdarg.h> @@ -38,148 +33,318 @@ * This same file is used pretty much verbatim for all test harnesses. * * The next few lines are the only differences. + * TODO: Move this into a separate configuration header, have all test + * suites share one copy of this file. */ -#define PROGRAM "bsdcpio" /* Name of program being tested. */ +__FBSDID("$FreeBSD: src/usr.bin/cpio/test/main.c,v 1.3 2008/08/24 04:58:22 kientzle Exp $"); +#define KNOWNREF "test_option_f.cpio.uu" #define ENVBASE "BSDCPIO" /* Prefix for environment variables. */ +#define PROGRAM "bsdcpio" /* Name of program being tested. */ +#undef LIBRARY /* Not testing a library. */ #undef EXTRA_DUMP /* How to dump extra data */ /* How to generate extra version info. */ #define EXTRA_VERSION (systemf("%s --version", testprog) ? "" : "") -__FBSDID("$FreeBSD: src/usr.bin/cpio/test/main.c,v 1.2 2008/06/21 02:17:18 kientzle Exp $"); /* - * "list.h" is simply created by "grep DEFINE_TEST"; it has - * a line like - * DEFINE_TEST(test_function) - * for each test. - * Include it here with a suitable DEFINE_TEST to declare all of the - * test functions. + * + * Windows support routines + * + * Note: Configuration is a tricky issue. Using HAVE_* feature macros + * in the test harness is dangerous because they cover up + * configuration errors. The classic example of this is omitting a + * configure check. If libarchive and libarchive_test both look for + * the same feature macro, such errors are hard to detect. Platform + * macros (e.g., _WIN32 or __GNUC__) are a little better, but can + * easily lead to very messy code. It's best to limit yourself + * to only the most generic programming techniques in the test harness + * and thus avoid conditionals altogether. Where that's not possible, + * try to minimize conditionals by grouping platform-specific tests in + * one place (e.g., test_acl_freebsd) or by adding new assert() + * functions (e.g., assertMakeHardlink()) to cover up platform + * differences. Platform-specific coding in libarchive_test is often + * a symptom that some capability is missing from libarchive itself. */ -#undef DEFINE_TEST -#define DEFINE_TEST(name) void name(void); -#include "list.h" +#if defined(_WIN32) && !defined(__CYGWIN__) +#include <io.h> +#include <windows.h> +#ifndef F_OK +#define F_OK (0) +#endif +#ifndef S_ISDIR +#define S_ISDIR(m) ((m) & _S_IFDIR) +#endif +#ifndef S_ISREG +#define S_ISREG(m) ((m) & _S_IFREG) +#endif +#if !defined(__BORLANDC__) +#define access _access +#define chdir _chdir +#endif +#ifndef fileno +#define fileno _fileno +#endif +/*#define fstat _fstat64*/ +#if !defined(__BORLANDC__) +#define getcwd _getcwd +#endif +#define lstat stat +/*#define lstat _stat64*/ +/*#define stat _stat64*/ +#define rmdir _rmdir +#if !defined(__BORLANDC__) +#define strdup _strdup +#define umask _umask +#endif +#define int64_t __int64 +#endif + +#if defined(HAVE__CrtSetReportMode) +# include <crtdbg.h> +#endif + +#if defined(_WIN32) && !defined(__CYGWIN__) +void *GetFunctionKernel32(const char *name) +{ + static HINSTANCE lib; + static int set; + if (!set) { + set = 1; + lib = LoadLibrary("kernel32.dll"); + } + if (lib == NULL) { + fprintf(stderr, "Can't load kernel32.dll?!\n"); + exit(1); + } + return (void *)GetProcAddress(lib, name); +} + +static int +my_CreateSymbolicLinkA(const char *linkname, const char *target, int flags) +{ + static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD); + static int set; + if (!set) { + set = 1; + f = GetFunctionKernel32("CreateSymbolicLinkA"); + } + return f == NULL ? 0 : (*f)(linkname, target, flags); +} + +static int +my_CreateHardLinkA(const char *linkname, const char *target) +{ + static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES); + static int set; + if (!set) { + set = 1; + f = GetFunctionKernel32("CreateHardLinkA"); + } + return f == NULL ? 0 : (*f)(linkname, target, NULL); +} + +int +my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi) +{ + HANDLE h; + int r; + + memset(bhfi, 0, sizeof(*bhfi)); + h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (h == INVALID_HANDLE_VALUE) + return (0); + r = GetFileInformationByHandle(h, bhfi); + CloseHandle(h); + return (r); +} +#endif -/* Interix doesn't define these in a standard header. */ -#if __INTERIX__ -extern char *optarg; -extern int optind; +#if defined(HAVE__CrtSetReportMode) +static void +invalid_parameter_handler(const wchar_t * expression, + const wchar_t * function, const wchar_t * file, + unsigned int line, uintptr_t pReserved) +{ + /* nop */ +} #endif +/* + * + * OPTIONS FLAGS + * + */ + /* Enable core dump on failure. */ static int dump_on_failure = 0; -/* Default is to remove temp dirs for successful tests. */ +/* Default is to remove temp dirs and log data for successful tests. */ static int keep_temp_files = 0; -/* Default is to print some basic information about each test. */ -static int quiet_flag = 0; -/* Default is to summarize repeated failures. */ -static int verbose = 0; -/* Cumulative count of component failures. */ +/* Default is to just report pass/fail for each test. */ +static int verbosity = 0; +#define VERBOSITY_SUMMARY_ONLY -1 /* -q */ +#define VERBOSITY_PASSFAIL 0 /* Default */ +#define VERBOSITY_LIGHT_REPORT 1 /* -v */ +#define VERBOSITY_FULL 2 /* -vv */ +/* A few places generate even more output for verbosity > VERBOSITY_FULL, + * mostly for debugging the test harness itself. */ +/* Cumulative count of assertion failures. */ static int failures = 0; -/* Cumulative count of skipped component tests. */ +/* Cumulative count of reported skips. */ static int skips = 0; -/* Cumulative count of assertions. */ +/* Cumulative count of assertions checked. */ static int assertions = 0; /* Directory where uuencoded reference files can be found. */ -static char *refdir; - -/* - * My own implementation of the standard assert() macro emits the - * message in the same format as GCC (file:line: message). - * It also includes some additional useful information. - * This makes it a lot easier to skim through test failures in - * Emacs. ;-) - * - * It also supports a few special features specifically to simplify - * test harnesses: - * failure(fmt, args) -- Stores a text string that gets - * printed if the following assertion fails, good for - * explaining subtle tests. - */ -static char msg[4096]; +static const char *refdir; /* - * For each test source file, we remember how many times each - * failure was reported. + * Report log information selectively to console and/or disk log. */ -static const char *failed_filename = NULL; -static struct line { - int line; - int count; -} failed_lines[1000]; - -/* - * Count this failure; return the number of previous failures. - */ -static int -previous_failures(const char *filename, int line) +static int log_console = 0; +static FILE *logfile; +static void +vlogprintf(const char *fmt, va_list ap) { - unsigned int i; - int count; +#ifdef va_copy + va_list lfap; + va_copy(lfap, ap); +#endif + if (log_console) + vfprintf(stdout, fmt, ap); + if (logfile != NULL) +#ifdef va_copy + vfprintf(logfile, fmt, lfap); + va_end(lfap); +#else + vfprintf(logfile, fmt, ap); +#endif +} - if (failed_filename == NULL || strcmp(failed_filename, filename) != 0) - memset(failed_lines, 0, sizeof(failed_lines)); - failed_filename = filename; +static void +logprintf(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vlogprintf(fmt, ap); + va_end(ap); +} - for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) { - if (failed_lines[i].line == line) { - count = failed_lines[i].count; - failed_lines[i].count++; - return (count); - } - if (failed_lines[i].line == 0) { - failed_lines[i].line = line; - failed_lines[i].count = 1; - return (0); - } - } - return (0); +/* Set up a message to display only if next assertion fails. */ +static char msgbuff[4096]; +static const char *msg, *nextmsg; +void +failure(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vsprintf(msgbuff, fmt, ap); + va_end(ap); + nextmsg = msgbuff; } /* * Copy arguments into file-local variables. + * This was added to permit vararg assert() functions without needing + * variadic wrapper macros. Turns out that the vararg capability is almost + * never used, so almost all of the vararg assertions can be simplified + * by removing the vararg capability and reworking the wrapper macro to + * pass __FILE__, __LINE__ directly into the function instead of using + * this hook. I suspect this machinery is used so rarely that we + * would be better off just removing it entirely. That would simplify + * the code here noticably. */ static const char *test_filename; static int test_line; static void *test_extra; -void test_setup(const char *filename, int line) +void assertion_setup(const char *filename, int line) { test_filename = filename; test_line = line; } +/* Called at the beginning of each assert() function. */ +static void +assertion_count(const char *file, int line) +{ + (void)file; /* UNUSED */ + (void)line; /* UNUSED */ + ++assertions; + /* Proper handling of "failure()" message. */ + msg = nextmsg; + nextmsg = NULL; + /* Uncomment to print file:line after every assertion. + * Verbose, but occasionally useful in tracking down crashes. */ + /* printf("Checked %s:%d\n", file, line); */ +} + /* - * Inform user that we're skipping a test. + * For each test source file, we remember how many times each + * assertion was reported. Cleared before each new test, + * used by test_summarize(). */ -void -test_skipping(const char *fmt, ...) +static struct line { + int count; + int skip; +} failed_lines[10000]; + +/* Count this failure, setup up log destination and handle initial report. */ +static void +failure_start(const char *filename, int line, const char *fmt, ...) { va_list ap; - if (previous_failures(test_filename, test_line)) - return; + /* Record another failure for this line. */ + ++failures; + /* test_filename = filename; */ + failed_lines[line].count++; + + /* Determine whether to log header to console. */ + switch (verbosity) { + case VERBOSITY_FULL: + log_console = 1; + break; + case VERBOSITY_LIGHT_REPORT: + log_console = (failed_lines[line].count < 2); + break; + default: + log_console = 0; + } + /* Log file:line header for this failure */ va_start(ap, fmt); - fprintf(stderr, " *** SKIPPING: "); - vfprintf(stderr, fmt, ap); - fprintf(stderr, "\n"); +#if _MSC_VER + logprintf("%s(%d): ", filename, line); +#else + logprintf("%s:%d: ", filename, line); +#endif + vlogprintf(fmt, ap); va_end(ap); - ++skips; + logprintf("\n"); + + if (msg != NULL && msg[0] != '\0') { + logprintf(" Description: %s\n", msg); + msg = NULL; + } + + /* Determine whether to log details to console. */ + if (verbosity == VERBOSITY_LIGHT_REPORT) + log_console = 0; } -/* Common handling of failed tests. */ +/* Complete reporting of failed tests. */ +/* + * The 'extra' hook here is used by libarchive to include libarchive + * error messages with assertion failures. It could also be used + * to add strerror() output, for example. Just define the EXTRA_DUMP() + * macro appropriately. + */ static void -report_failure(void *extra) +failure_finish(void *extra) { - if (msg[0] != '\0') { - fprintf(stderr, " Description: %s\n", msg); - msg[0] = '\0'; - } - + (void)extra; /* UNUSED (maybe) */ #ifdef EXTRA_DUMP if (extra != NULL) - fprintf(stderr, " detail: %s\n", EXTRA_DUMP(extra)); -#else - (void)extra; /* UNUSED */ + logprintf(" detail: %s\n", EXTRA_DUMP(extra)); #endif if (dump_on_failure) { @@ -190,203 +355,154 @@ report_failure(void *extra) } } -/* - * Summarize repeated failures in the just-completed test file. - * The reports above suppress multiple failures from the same source - * line; this reports on any tests that did fail multiple times. - */ -static int -summarize_comparator(const void *a0, const void *b0) -{ - const struct line *a = a0, *b = b0; - if (a->line == 0 && b->line == 0) - return (0); - if (a->line == 0) - return (1); - if (b->line == 0) - return (-1); - return (a->line - b->line); -} - -static void -summarize(void) -{ - unsigned int i; - - qsort(failed_lines, sizeof(failed_lines)/sizeof(failed_lines[0]), - sizeof(failed_lines[0]), summarize_comparator); - for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) { - if (failed_lines[i].line == 0) - break; - if (failed_lines[i].count > 1) - fprintf(stderr, "%s:%d: Failed %d times\n", - failed_filename, failed_lines[i].line, - failed_lines[i].count); - } - /* Clear the failure history for the next file. */ - memset(failed_lines, 0, sizeof(failed_lines)); -} - -/* Set up a message to display only after a test fails. */ +/* Inform user that we're skipping some checks. */ void -failure(const char *fmt, ...) +test_skipping(const char *fmt, ...) { + char buff[1024]; va_list ap; + va_start(ap, fmt); - vsprintf(msg, fmt, ap); + vsprintf(buff, fmt, ap); va_end(ap); + /* failure_start() isn't quite right, but is awfully convenient. */ + failure_start(test_filename, test_line, "SKIPPING: %s", buff); + --failures; /* Undo failures++ in failure_start() */ + /* Don't failure_finish() here. */ + /* Mark as skip, so doesn't count as failed test. */ + failed_lines[test_line].skip = 1; + ++skips; } +/* + * + * ASSERTIONS + * + */ + /* Generic assert() just displays the failed condition. */ int -test_assert(const char *file, int line, int value, const char *condition, void *extra) +assertion_assert(const char *file, int line, int value, + const char *condition, void *extra) { - ++assertions; - if (value) { - msg[0] = '\0'; - return (value); + assertion_count(file, line); + if (!value) { + failure_start(file, line, "Assertion failed: %s", condition); + failure_finish(extra); } - failures ++; - if (!verbose && previous_failures(file, line)) - return (value); - fprintf(stderr, "%s:%d: Assertion failed\n", file, line); - fprintf(stderr, " Condition: %s\n", condition); - report_failure(extra); return (value); } -/* assertEqualInt() displays the values of the two integers. */ +/* chdir() and report any errors */ int -test_assert_equal_int(const char *file, int line, - int v1, const char *e1, int v2, const char *e2, void *extra) +assertion_chdir(const char *file, int line, const char *pathname) { - ++assertions; - if (v1 == v2) { - msg[0] = '\0'; + assertion_count(file, line); + if (chdir(pathname) == 0) return (1); - } - failures ++; - if (!verbose && previous_failures(file, line)) - return (0); - fprintf(stderr, "%s:%d: Assertion failed: Ints not equal\n", - file, line); - fprintf(stderr, " %s=%d\n", e1, v1); - fprintf(stderr, " %s=%d\n", e2, v2); - report_failure(extra); + failure_start(file, line, "chdir(\"%s\")", pathname); + failure_finish(NULL); + return (0); + +} + +/* Verify two integers are equal. */ +int +assertion_equal_int(const char *file, int line, + long long v1, const char *e1, long long v2, const char *e2, void *extra) +{ + assertion_count(file, line); + if (v1 == v2) + return (1); + failure_start(file, line, "%s != %s", e1, e2); + logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1); + logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2); + failure_finish(extra); return (0); } -static void strdump(const char *p) +static void strdump(const char *e, const char *p) { + const char *q = p; + + logprintf(" %s = ", e); if (p == NULL) { - fprintf(stderr, "(null)"); + logprintf("NULL"); return; } - fprintf(stderr, "\""); + logprintf("\""); while (*p != '\0') { unsigned int c = 0xff & *p++; switch (c) { - case '\a': fprintf(stderr, "\a"); break; - case '\b': fprintf(stderr, "\b"); break; - case '\n': fprintf(stderr, "\n"); break; - case '\r': fprintf(stderr, "\r"); break; + case '\a': printf("\a"); break; + case '\b': printf("\b"); break; + case '\n': printf("\n"); break; + case '\r': printf("\r"); break; default: if (c >= 32 && c < 127) - fprintf(stderr, "%c", c); + logprintf("%c", c); else - fprintf(stderr, "\\x%02X", c); + logprintf("\\x%02X", c); } } - fprintf(stderr, "\""); + logprintf("\""); + logprintf(" (length %d)\n", q == NULL ? -1 : (int)strlen(q)); } -/* assertEqualString() displays the values of the two strings. */ +/* Verify two strings are equal, dump them if not. */ int -test_assert_equal_string(const char *file, int line, +assertion_equal_string(const char *file, int line, const char *v1, const char *e1, const char *v2, const char *e2, void *extra) { - ++assertions; - if (v1 == NULL || v2 == NULL) { - if (v1 == v2) { - msg[0] = '\0'; - return (1); - } - } else if (strcmp(v1, v2) == 0) { - msg[0] = '\0'; + assertion_count(file, line); + if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0)) return (1); - } - failures ++; - if (!verbose && previous_failures(file, line)) - return (0); - fprintf(stderr, "%s:%d: Assertion failed: Strings not equal\n", - file, line); - fprintf(stderr, " %s = ", e1); - strdump(v1); - fprintf(stderr, " (length %d)\n", v1 == NULL ? 0 : strlen(v1)); - fprintf(stderr, " %s = ", e2); - strdump(v2); - fprintf(stderr, " (length %d)\n", v2 == NULL ? 0 : strlen(v2)); - report_failure(extra); + failure_start(file, line, "%s != %s", e1, e2); + strdump(e1, v1); + strdump(e2, v2); + failure_finish(extra); return (0); } -static void wcsdump(const wchar_t *w) +static void +wcsdump(const char *e, const wchar_t *w) { + logprintf(" %s = ", e); if (w == NULL) { - fprintf(stderr, "(null)"); + logprintf("(null)"); return; } - fprintf(stderr, "\""); + logprintf("\""); while (*w != L'\0') { unsigned int c = *w++; if (c >= 32 && c < 127) - fprintf(stderr, "%c", c); + logprintf("%c", c); else if (c < 256) - fprintf(stderr, "\\x%02X", c); + logprintf("\\x%02X", c); else if (c < 0x10000) - fprintf(stderr, "\\u%04X", c); + logprintf("\\u%04X", c); else - fprintf(stderr, "\\U%08X", c); + logprintf("\\U%08X", c); } - fprintf(stderr, "\""); + logprintf("\"\n"); } -/* assertEqualWString() displays the values of the two strings. */ +/* Verify that two wide strings are equal, dump them if not. */ int -test_assert_equal_wstring(const char *file, int line, +assertion_equal_wstring(const char *file, int line, const wchar_t *v1, const char *e1, const wchar_t *v2, const char *e2, void *extra) { - ++assertions; - if (v1 == NULL) { - if (v2 == NULL) { - msg[0] = '\0'; - return (1); - } - } else if (v2 == NULL) { - if (v1 == NULL) { - msg[0] = '\0'; - return (1); - } - } else if (wcscmp(v1, v2) == 0) { - msg[0] = '\0'; + assertion_count(file, line); + if (v1 == v2 || wcscmp(v1, v2) == 0) return (1); - } - failures ++; - if (!verbose && previous_failures(file, line)) - return (0); - fprintf(stderr, "%s:%d: Assertion failed: Unicode strings not equal\n", - file, line); - fprintf(stderr, " %s = ", e1); - wcsdump(v1); - fprintf(stderr, "\n"); - fprintf(stderr, " %s = ", e2); - wcsdump(v2); - fprintf(stderr, "\n"); - report_failure(extra); + failure_start(file, line, "%s != %s", e1, e2); + wcsdump(e1, v1); + wcsdump(e2, v2); + failure_finish(extra); return (0); } @@ -401,226 +517,956 @@ hexdump(const char *p, const char *ref, size_t l, size_t offset) size_t i, j; char sep; + if (p == NULL) { + logprintf("(null)\n"); + return; + } for(i=0; i < l; i+=16) { - fprintf(stderr, "%04x", i + offset); + logprintf("%04x", (unsigned)(i + offset)); sep = ' '; for (j = 0; j < 16 && i + j < l; j++) { if (ref != NULL && p[i + j] != ref[i + j]) sep = '_'; - fprintf(stderr, "%c%02x", sep, 0xff & (int)p[i+j]); + logprintf("%c%02x", sep, 0xff & (int)p[i+j]); if (ref != NULL && p[i + j] == ref[i + j]) sep = ' '; } for (; j < 16; j++) { - fprintf(stderr, "%c ", sep); + logprintf("%c ", sep); sep = ' '; } - fprintf(stderr, "%c", sep); + logprintf("%c", sep); for (j=0; j < 16 && i + j < l; j++) { int c = p[i + j]; if (c >= ' ' && c <= 126) - fprintf(stderr, "%c", c); + logprintf("%c", c); else - fprintf(stderr, "."); + logprintf("."); } - fprintf(stderr, "\n"); + logprintf("\n"); } } -/* assertEqualMem() displays the values of the two memory blocks. */ -/* TODO: For long blocks, hexdump the first bytes that actually differ. */ +/* Verify that two blocks of memory are the same, display the first + * block of differences if they're not. */ int -test_assert_equal_mem(const char *file, int line, - const char *v1, const char *e1, - const char *v2, const char *e2, +assertion_equal_mem(const char *file, int line, + const void *_v1, const char *e1, + const void *_v2, const char *e2, size_t l, const char *ld, void *extra) { - ++assertions; - if (v1 == NULL || v2 == NULL) { - if (v1 == v2) { - msg[0] = '\0'; - return (1); - } - } else if (memcmp(v1, v2, l) == 0) { - msg[0] = '\0'; + const char *v1 = (const char *)_v1; + const char *v2 = (const char *)_v2; + size_t offset; + + assertion_count(file, line); + if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0)) return (1); + + failure_start(file, line, "%s != %s", e1, e2); + logprintf(" size %s = %d\n", ld, (int)l); + /* Dump 48 bytes (3 lines) so that the first difference is + * in the second line. */ + offset = 0; + while (l > 64 && memcmp(v1, v2, 32) == 0) { + /* Two lines agree, so step forward one line. */ + v1 += 16; + v2 += 16; + l -= 16; + offset += 16; } - failures ++; - if (!verbose && previous_failures(file, line)) - return (0); - fprintf(stderr, "%s:%d: Assertion failed: memory not equal\n", - file, line); - fprintf(stderr, " size %s = %d\n", ld, (int)l); - fprintf(stderr, " Dump of %s\n", e1); - hexdump(v1, v2, l < 32 ? l : 32, 0); - fprintf(stderr, " Dump of %s\n", e2); - hexdump(v2, v1, l < 32 ? l : 32, 0); - fprintf(stderr, "\n"); - report_failure(extra); + logprintf(" Dump of %s\n", e1); + hexdump(v1, v2, l < 64 ? l : 64, offset); + logprintf(" Dump of %s\n", e2); + hexdump(v2, v1, l < 64 ? l : 64, offset); + logprintf("\n"); + failure_finish(extra); return (0); } +/* Verify that the named file exists and is empty. */ int -test_assert_empty_file(const char *f1fmt, ...) +assertion_empty_file(const char *f1fmt, ...) { char buff[1024]; char f1[1024]; struct stat st; va_list ap; ssize_t s; - int fd; - + FILE *f; + assertion_count(test_filename, test_line); va_start(ap, f1fmt); vsprintf(f1, f1fmt, ap); va_end(ap); if (stat(f1, &st) != 0) { - fprintf(stderr, "%s:%d: Could not stat: %s\n", test_filename, test_line, f1); - report_failure(NULL); + failure_start(test_filename, test_line, "Stat failed: %s", f1); + failure_finish(NULL); return (0); } if (st.st_size == 0) return (1); - failures ++; - if (!verbose && previous_failures(test_filename, test_line)) - return (0); - - fprintf(stderr, "%s:%d: File not empty: %s\n", test_filename, test_line, f1); - fprintf(stderr, " File size: %d\n", (int)st.st_size); - fprintf(stderr, " Contents:\n"); - fd = open(f1, O_RDONLY); - if (fd < 0) { - fprintf(stderr, " Unable to open %s\n", f1); + failure_start(test_filename, test_line, "File should be empty: %s", f1); + logprintf(" File size: %d\n", (int)st.st_size); + logprintf(" Contents:\n"); + f = fopen(f1, "rb"); + if (f == NULL) { + logprintf(" Unable to open %s\n", f1); } else { - s = sizeof(buff) < st.st_size ? sizeof(buff) : st.st_size; - s = read(fd, buff, s); + s = ((off_t)sizeof(buff) < st.st_size) ? + (ssize_t)sizeof(buff) : (ssize_t)st.st_size; + s = fread(buff, 1, s, f); hexdump(buff, NULL, s, 0); + fclose(f); } - report_failure(NULL); + failure_finish(NULL); return (0); } -/* assertEqualFile() asserts that two files have the same contents. */ +/* Verify that the named file exists and is not empty. */ +int +assertion_non_empty_file(const char *f1fmt, ...) +{ + char f1[1024]; + struct stat st; + va_list ap; + + assertion_count(test_filename, test_line); + va_start(ap, f1fmt); + vsprintf(f1, f1fmt, ap); + va_end(ap); + + if (stat(f1, &st) != 0) { + failure_start(test_filename, test_line, "Stat failed: %s", f1); + failure_finish(NULL); + return (0); + } + if (st.st_size == 0) { + failure_start(test_filename, test_line, "File empty: %s", f1); + failure_finish(NULL); + return (0); + } + return (1); +} + +/* Verify that two files have the same contents. */ /* TODO: hexdump the first bytes that actually differ. */ int -test_assert_equal_file(const char *f1, const char *f2pattern, ...) +assertion_equal_file(const char *fn1, const char *f2pattern, ...) { - char f2[1024]; + char fn2[1024]; va_list ap; char buff1[1024]; char buff2[1024]; - int fd1, fd2; + FILE *f1, *f2; int n1, n2; + assertion_count(test_filename, test_line); va_start(ap, f2pattern); - vsprintf(f2, f2pattern, ap); + vsprintf(fn2, f2pattern, ap); va_end(ap); - fd1 = open(f1, O_RDONLY); - fd2 = open(f2, O_RDONLY); + f1 = fopen(fn1, "rb"); + f2 = fopen(fn2, "rb"); for (;;) { - n1 = read(fd1, buff1, sizeof(buff1)); - n2 = read(fd2, buff2, sizeof(buff2)); + n1 = fread(buff1, 1, sizeof(buff1), f1); + n2 = fread(buff2, 1, sizeof(buff2), f2); if (n1 != n2) break; - if (n1 == 0 && n2 == 0) + if (n1 == 0 && n2 == 0) { + fclose(f1); + fclose(f2); return (1); + } if (memcmp(buff1, buff2, n1) != 0) break; } - failures ++; - if (!verbose && previous_failures(test_filename, test_line)) - return (0); - fprintf(stderr, "%s:%d: Files are not identical\n", - test_filename, test_line); - fprintf(stderr, " file1=\"%s\"\n", f1); - fprintf(stderr, " file2=\"%s\"\n", f2); - report_failure(test_extra); + fclose(f1); + fclose(f2); + failure_start(test_filename, test_line, "Files not identical"); + logprintf(" file1=\"%s\"\n", fn1); + logprintf(" file2=\"%s\"\n", fn2); + failure_finish(test_extra); return (0); } +/* Verify that the named file does exist. */ int -test_assert_file_exists(const char *fpattern, ...) +assertion_file_exists(const char *fpattern, ...) { char f[1024]; va_list ap; + assertion_count(test_filename, test_line); va_start(ap, fpattern); vsprintf(f, fpattern, ap); va_end(ap); +#if defined(_WIN32) && !defined(__CYGWIN__) + if (!_access(f, 0)) + return (1); +#else if (!access(f, F_OK)) return (1); - if (!previous_failures(test_filename, test_line)) { - fprintf(stderr, "%s:%d: File doesn't exist\n", - test_filename, test_line); - fprintf(stderr, " file=\"%s\"\n", f); - report_failure(test_extra); - } +#endif + failure_start(test_filename, test_line, "File should exist: %s", f); + failure_finish(test_extra); return (0); } +/* Verify that the named file doesn't exist. */ int -test_assert_file_not_exists(const char *fpattern, ...) +assertion_file_not_exists(const char *fpattern, ...) { char f[1024]; va_list ap; + assertion_count(test_filename, test_line); va_start(ap, fpattern); vsprintf(f, fpattern, ap); va_end(ap); +#if defined(_WIN32) && !defined(__CYGWIN__) + if (_access(f, 0)) + return (1); +#else if (access(f, F_OK)) return (1); - if (!previous_failures(test_filename, test_line)) { - fprintf(stderr, "%s:%d: File exists and shouldn't\n", - test_filename, test_line); - fprintf(stderr, " file=\"%s\"\n", f); - report_failure(test_extra); - } +#endif + failure_start(test_filename, test_line, "File should not exist: %s", f); + failure_finish(test_extra); return (0); } -/* assertFileContents() asserts the contents of a file. */ +/* Compare the contents of a file to a block of memory. */ int -test_assert_file_contents(const void *buff, int s, const char *fpattern, ...) +assertion_file_contents(const void *buff, int s, const char *fpattern, ...) { - char f[1024]; + char fn[1024]; va_list ap; char *contents; - int fd; + FILE *f; int n; + assertion_count(test_filename, test_line); va_start(ap, fpattern); - vsprintf(f, fpattern, ap); + vsprintf(fn, fpattern, ap); va_end(ap); - fd = open(f, O_RDONLY); + f = fopen(fn, "rb"); + if (f == NULL) { + failure_start(test_filename, test_line, + "File should exist: %s", fn); + failure_finish(test_extra); + return (0); + } contents = malloc(s * 2); - n = read(fd, contents, s * 2); + n = fread(contents, 1, s * 2, f); + fclose(f); if (n == s && memcmp(buff, contents, s) == 0) { free(contents); return (1); } - failures ++; - if (!previous_failures(test_filename, test_line)) { - fprintf(stderr, "%s:%d: File contents don't match\n", - test_filename, test_line); - fprintf(stderr, " file=\"%s\"\n", f); - if (n > 0) - hexdump(contents, buff, n, 0); - else { - fprintf(stderr, " File empty, contents should be:\n"); - hexdump(buff, NULL, s, 0); + failure_start(test_filename, test_line, "File contents don't match"); + logprintf(" file=\"%s\"\n", fn); + if (n > 0) + hexdump(contents, buff, n > 512 ? 512 : n, 0); + else { + logprintf(" File empty, contents should be:\n"); + hexdump(buff, NULL, s > 512 ? 512 : n, 0); + } + failure_finish(test_extra); + free(contents); + return (0); +} + +/* Check the contents of a text file, being tolerant of line endings. */ +int +assertion_text_file_contents(const char *buff, const char *fn) +{ + char *contents; + const char *btxt, *ftxt; + FILE *f; + int n, s; + + assertion_count(test_filename, test_line); + f = fopen(fn, "r"); + if (f == NULL) { + failure_start(test_filename, test_line, + "File doesn't exist: %s", fn); + failure_finish(test_extra); + return (0); + } + s = strlen(buff); + contents = malloc(s * 2 + 128); + n = fread(contents, 1, s * 2 + 128 - 1, f); + if (n >= 0) + contents[n] = '\0'; + fclose(f); + /* Compare texts. */ + btxt = buff; + ftxt = (const char *)contents; + while (*btxt != '\0' && *ftxt != '\0') { + if (*btxt == *ftxt) { + ++btxt; + ++ftxt; + continue; } - report_failure(test_extra); + if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') { + /* Pass over different new line characters. */ + ++btxt; + ftxt += 2; + continue; + } + break; + } + if (*btxt == '\0' && *ftxt == '\0') { + free(contents); + return (1); } + failure_start(test_filename, test_line, "Contents don't match"); + logprintf(" file=\"%s\"\n", fn); + if (n > 0) + hexdump(contents, buff, n, 0); + else { + logprintf(" File empty, contents should be:\n"); + hexdump(buff, NULL, s, 0); + } + failure_finish(test_extra); free(contents); return (0); } +/* Test that two paths point to the same file. */ +/* As a side-effect, asserts that both files exist. */ +static int +is_hardlink(const char *file, int line, + const char *path1, const char *path2) +{ +#if defined(_WIN32) && !defined(__CYGWIN__) + BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2; + int r; + + assertion_count(file, line); + r = my_GetFileInformationByName(path1, &bhfi1); + if (r == 0) { + failure_start(file, line, "File %s can't be inspected?", path1); + failure_finish(NULL); + return (0); + } + r = my_GetFileInformationByName(path2, &bhfi2); + if (r == 0) { + failure_start(file, line, "File %s can't be inspected?", path2); + failure_finish(NULL); + return (0); + } + return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber + && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh + && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow); +#else + struct stat st1, st2; + int r; + + assertion_count(file, line); + r = lstat(path1, &st1); + if (r != 0) { + failure_start(file, line, "File should exist: %s", path1); + failure_finish(NULL); + return (0); + } + r = lstat(path2, &st2); + if (r != 0) { + failure_start(file, line, "File should exist: %s", path2); + failure_finish(NULL); + return (0); + } + return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev); +#endif +} + +int +assertion_is_hardlink(const char *file, int line, + const char *path1, const char *path2) +{ + if (is_hardlink(file, line, path1, path2)) + return (1); + failure_start(file, line, + "Files %s and %s are not hardlinked", path1, path2); + failure_finish(NULL); + return (0); +} + +int +assertion_is_not_hardlink(const char *file, int line, + const char *path1, const char *path2) +{ + if (!is_hardlink(file, line, path1, path2)) + return (1); + failure_start(file, line, + "Files %s and %s should not be hardlinked", path1, path2); + failure_finish(NULL); + return (0); +} + +/* Verify a/b/mtime of 'pathname'. */ +/* If 'recent', verify that it's within last 10 seconds. */ +static int +assertion_file_time(const char *file, int line, + const char *pathname, long t, long nsec, char type, int recent) +{ + long long filet, filet_nsec; + int r; + +#if defined(_WIN32) && !defined(__CYGWIN__) +#define EPOC_TIME (116444736000000000ULL) + FILETIME ftime, fbirthtime, fatime, fmtime; + ULARGE_INTEGER wintm; + HANDLE h; + ftime.dwLowDateTime = 0; + ftime.dwHighDateTime = 0; + + assertion_count(file, line); + h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (h == INVALID_HANDLE_VALUE) { + failure_start(file, line, "Can't access %s\n", pathname); + failure_finish(NULL); + return (0); + } + r = GetFileTime(h, &fbirthtime, &fatime, &fmtime); + switch (type) { + case 'a': ftime = fatime; break; + case 'b': ftime = fbirthtime; break; + case 'm': ftime = fmtime; break; + } + CloseHandle(h); + if (r == 0) { + failure_start(file, line, "Can't GetFileTime %s\n", pathname); + failure_finish(NULL); + return (0); + } + wintm.LowPart = ftime.dwLowDateTime; + wintm.HighPart = ftime.dwHighDateTime; + filet = (wintm.QuadPart - EPOC_TIME) / 10000000; + filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100; + nsec = (nsec / 100) * 100; /* Round the request */ +#else + struct stat st; + + assertion_count(file, line); + r = lstat(pathname, &st); + if (r != 0) { + failure_start(file, line, "Can't stat %s\n", pathname); + failure_finish(NULL); + return (0); + } + switch (type) { + case 'a': filet = st.st_atime; break; + case 'm': filet = st.st_mtime; break; + case 'b': filet = 0; break; + default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); + exit(1); + } +#if defined(__FreeBSD__) + switch (type) { + case 'a': filet_nsec = st.st_atimespec.tv_nsec; break; + case 'b': filet = st.st_birthtime; + filet_nsec = st.st_birthtimespec.tv_nsec; break; + case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break; + default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); + exit(1); + } + /* FreeBSD generally only stores to microsecond res, so round. */ + filet_nsec = (filet_nsec / 1000) * 1000; + nsec = (nsec / 1000) * 1000; +#else + filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */ + if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */ +#if defined(__HAIKU__) + if (type == 'a') return (1); /* Haiku doesn't have atime. */ +#endif +#endif +#endif + if (recent) { + /* Check that requested time is up-to-date. */ + time_t now = time(NULL); + if (filet < now - 10 || filet > now + 1) { + failure_start(file, line, + "File %s has %ctime %ld, %ld seconds ago\n", + pathname, type, filet, now - filet); + failure_finish(NULL); + return (0); + } + } else if (filet != t || filet_nsec != nsec) { + failure_start(file, line, + "File %s has %ctime %ld.%09ld, expected %ld.%09ld", + pathname, type, filet, filet_nsec, t, nsec); + failure_finish(NULL); + return (0); + } + return (1); +} + +/* Verify atime of 'pathname'. */ +int +assertion_file_atime(const char *file, int line, + const char *pathname, long t, long nsec) +{ + return assertion_file_time(file, line, pathname, t, nsec, 'a', 0); +} + +/* Verify atime of 'pathname' is up-to-date. */ +int +assertion_file_atime_recent(const char *file, int line, const char *pathname) +{ + return assertion_file_time(file, line, pathname, 0, 0, 'a', 1); +} + +/* Verify birthtime of 'pathname'. */ +int +assertion_file_birthtime(const char *file, int line, + const char *pathname, long t, long nsec) +{ + return assertion_file_time(file, line, pathname, t, nsec, 'b', 0); +} + +/* Verify birthtime of 'pathname' is up-to-date. */ +int +assertion_file_birthtime_recent(const char *file, int line, + const char *pathname) +{ + return assertion_file_time(file, line, pathname, 0, 0, 'b', 1); +} + +/* Verify mtime of 'pathname'. */ +int +assertion_file_mtime(const char *file, int line, + const char *pathname, long t, long nsec) +{ + return assertion_file_time(file, line, pathname, t, nsec, 'm', 0); +} + +/* Verify mtime of 'pathname' is up-to-date. */ +int +assertion_file_mtime_recent(const char *file, int line, const char *pathname) +{ + return assertion_file_time(file, line, pathname, 0, 0, 'm', 1); +} + +/* Verify number of links to 'pathname'. */ +int +assertion_file_nlinks(const char *file, int line, + const char *pathname, int nlinks) +{ +#if defined(_WIN32) && !defined(__CYGWIN__) + BY_HANDLE_FILE_INFORMATION bhfi; + int r; + + assertion_count(file, line); + r = my_GetFileInformationByName(pathname, &bhfi); + if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks) + return (1); + failure_start(file, line, "File %s has %d links, expected %d", + pathname, bhfi.nNumberOfLinks, nlinks); + failure_finish(NULL); + return (0); +#else + struct stat st; + int r; + + assertion_count(file, line); + r = lstat(pathname, &st); + if (r == 0 && st.st_nlink == nlinks) + return (1); + failure_start(file, line, "File %s has %d links, expected %d", + pathname, st.st_nlink, nlinks); + failure_finish(NULL); + return (0); +#endif +} + +/* Verify size of 'pathname'. */ +int +assertion_file_size(const char *file, int line, const char *pathname, long size) +{ + int64_t filesize; + int r; + + assertion_count(file, line); +#if defined(_WIN32) && !defined(__CYGWIN__) + { + BY_HANDLE_FILE_INFORMATION bhfi; + r = !my_GetFileInformationByName(pathname, &bhfi); + filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow; + } +#else + { + struct stat st; + r = lstat(pathname, &st); + filesize = st.st_size; + } +#endif + if (r == 0 && filesize == size) + return (1); + failure_start(file, line, "File %s has size %ld, expected %ld", + pathname, (long)filesize, (long)size); + failure_finish(NULL); + return (0); +} + +/* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */ +int +assertion_is_dir(const char *file, int line, const char *pathname, int mode) +{ + struct stat st; + int r; + +#if defined(_WIN32) && !defined(__CYGWIN__) + (void)mode; /* UNUSED */ +#endif + assertion_count(file, line); + r = lstat(pathname, &st); + if (r != 0) { + failure_start(file, line, "Dir should exist: %s", pathname); + failure_finish(NULL); + return (0); + } + if (!S_ISDIR(st.st_mode)) { + failure_start(file, line, "%s is not a dir", pathname); + failure_finish(NULL); + return (0); + } +#if !defined(_WIN32) || defined(__CYGWIN__) + /* Windows doesn't handle permissions the same way as POSIX, + * so just ignore the mode tests. */ + /* TODO: Can we do better here? */ + if (mode >= 0 && mode != (st.st_mode & 07777)) { + failure_start(file, line, "Dir %s has wrong mode", pathname); + logprintf(" Expected: 0%3o\n", mode); + logprintf(" Found: 0%3o\n", st.st_mode & 07777); + failure_finish(NULL); + return (0); + } +#endif + return (1); +} + +/* Verify that 'pathname' is a regular file. If 'mode' is >= 0, + * verify that too. */ +int +assertion_is_reg(const char *file, int line, const char *pathname, int mode) +{ + struct stat st; + int r; + +#if defined(_WIN32) && !defined(__CYGWIN__) + (void)mode; /* UNUSED */ +#endif + assertion_count(file, line); + r = lstat(pathname, &st); + if (r != 0 || !S_ISREG(st.st_mode)) { + failure_start(file, line, "File should exist: %s", pathname); + failure_finish(NULL); + return (0); + } +#if !defined(_WIN32) || defined(__CYGWIN__) + /* Windows doesn't handle permissions the same way as POSIX, + * so just ignore the mode tests. */ + /* TODO: Can we do better here? */ + if (mode >= 0 && mode != (st.st_mode & 07777)) { + failure_start(file, line, "File %s has wrong mode", pathname); + logprintf(" Expected: 0%3o\n", mode); + logprintf(" Found: 0%3o\n", st.st_mode & 07777); + failure_finish(NULL); + return (0); + } +#endif + return (1); +} + +/* Check whether 'pathname' is a symbolic link. If 'contents' is + * non-NULL, verify that the symlink has those contents. */ +static int +is_symlink(const char *file, int line, + const char *pathname, const char *contents) +{ +#if defined(_WIN32) && !defined(__CYGWIN__) + (void)pathname; /* UNUSED */ + (void)contents; /* UNUSED */ + assertion_count(file, line); + /* Windows sort-of has real symlinks, but they're only usable + * by privileged users and are crippled even then, so there's + * really not much point in bothering with this. */ + return (0); +#else + char buff[300]; + struct stat st; + ssize_t linklen; + int r; + + assertion_count(file, line); + r = lstat(pathname, &st); + if (r != 0) { + failure_start(file, line, + "Symlink should exist: %s", pathname); + failure_finish(NULL); + return (0); + } + if (!S_ISLNK(st.st_mode)) + return (0); + if (contents == NULL) + return (1); + linklen = readlink(pathname, buff, sizeof(buff)); + if (linklen < 0) { + failure_start(file, line, "Can't read symlink %s", pathname); + failure_finish(NULL); + return (0); + } + buff[linklen] = '\0'; + if (strcmp(buff, contents) != 0) + return (0); + return (1); +#endif +} + +/* Assert that path is a symlink that (optionally) contains contents. */ +int +assertion_is_symlink(const char *file, int line, + const char *path, const char *contents) +{ + if (is_symlink(file, line, path, contents)) + return (1); + if (contents) + failure_start(file, line, "File %s is not a symlink to %s", + path, contents); + else + failure_start(file, line, "File %s is not a symlink", path); + failure_finish(NULL); + return (0); +} + + +/* Create a directory and report any errors. */ +int +assertion_make_dir(const char *file, int line, const char *dirname, int mode) +{ + assertion_count(file, line); +#if defined(_WIN32) && !defined(__CYGWIN__) + (void)mode; /* UNUSED */ + if (0 == _mkdir(dirname)) + return (1); +#else + if (0 == mkdir(dirname, mode)) + return (1); +#endif + failure_start(file, line, "Could not create directory %s", dirname); + failure_finish(NULL); + return(0); +} + +/* Create a file with the specified contents and report any failures. */ +int +assertion_make_file(const char *file, int line, + const char *path, int mode, const char *contents) +{ +#if defined(_WIN32) && !defined(__CYGWIN__) + /* TODO: Rework this to set file mode as well. */ + FILE *f; + (void)mode; /* UNUSED */ + assertion_count(file, line); + f = fopen(path, "wb"); + if (f == NULL) { + failure_start(file, line, "Could not create file %s", path); + failure_finish(NULL); + return (0); + } + if (contents != NULL) { + if (strlen(contents) + != fwrite(contents, 1, strlen(contents), f)) { + fclose(f); + failure_start(file, line, + "Could not write file %s", path); + failure_finish(NULL); + return (0); + } + } + fclose(f); + return (1); +#else + int fd; + assertion_count(file, line); + fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644); + if (fd < 0) { + failure_start(file, line, "Could not create %s", path); + failure_finish(NULL); + return (0); + } + if (contents != NULL) { + if ((ssize_t)strlen(contents) + != write(fd, contents, strlen(contents))) { + close(fd); + failure_start(file, line, "Could not write to %s", path); + failure_finish(NULL); + return (0); + } + } + close(fd); + return (1); +#endif +} + +/* Create a hardlink and report any failures. */ +int +assertion_make_hardlink(const char *file, int line, + const char *newpath, const char *linkto) +{ + int succeeded; + + assertion_count(file, line); +#if defined(_WIN32) && !defined(__CYGWIN__) + succeeded = my_CreateHardLinkA(newpath, linkto); +#elif HAVE_LINK + succeeded = !link(linkto, newpath); +#else + succeeded = 0; +#endif + if (succeeded) + return (1); + failure_start(file, line, "Could not create hardlink"); + logprintf(" New link: %s\n", newpath); + logprintf(" Old name: %s\n", linkto); + failure_finish(NULL); + return(0); +} + +/* Create a symlink and report any failures. */ +int +assertion_make_symlink(const char *file, int line, + const char *newpath, const char *linkto) +{ +#if defined(_WIN32) && !defined(__CYGWIN__) + int targetIsDir = 0; /* TODO: Fix this */ + assertion_count(file, line); + if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir)) + return (1); +#elif HAVE_SYMLINK + assertion_count(file, line); + if (0 == symlink(linkto, newpath)) + return (1); +#endif + failure_start(file, line, "Could not create symlink"); + logprintf(" New link: %s\n", newpath); + logprintf(" Old name: %s\n", linkto); + failure_finish(NULL); + return(0); +} + +/* Set umask, report failures. */ +int +assertion_umask(const char *file, int line, int mask) +{ + assertion_count(file, line); + (void)file; /* UNUSED */ + (void)line; /* UNUSED */ + umask(mask); + return (1); +} + +/* + * + * UTILITIES for use by tests. + * + */ + +/* + * Check whether platform supports symlinks. This is intended + * for tests to use in deciding whether to bother testing symlink + * support; if the platform doesn't support symlinks, there's no point + * in checking whether the program being tested can create them. + * + * Note that the first time this test is called, we actually go out to + * disk to create and verify a symlink. This is necessary because + * symlink support is actually a property of a particular filesystem + * and can thus vary between directories on a single system. After + * the first call, this returns the cached result from memory, so it's + * safe to call it as often as you wish. + */ +int +canSymlink(void) +{ + /* Remember the test result */ + static int value = 0, tested = 0; + if (tested) + return (value); + + ++tested; + assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, "a"); + /* Note: Cygwin has its own symlink() emulation that does not + * use the Win32 CreateSymbolicLink() function. */ +#if defined(_WIN32) && !defined(__CYGWIN__) + value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0) + && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0"); +#elif HAVE_SYMLINK + value = (0 == symlink("canSymlink.0", "canSymlink.1")) + && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0"); +#endif + return (value); +} + +/* + * Can this platform run the gzip program? + */ +/* Platform-dependent options for hiding the output of a subcommand. */ +#if defined(_WIN32) && !defined(__CYGWIN__) +static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */ +#else +static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */ +#endif +int +canGzip(void) +{ + static int tested = 0, value = 0; + if (!tested) { + tested = 1; + if (systemf("gzip -V %s", redirectArgs) == 0) + value = 1; + } + return (value); +} + +/* + * Can this platform run the gunzip program? + */ +int +canGunzip(void) +{ + static int tested = 0, value = 0; + if (!tested) { + tested = 1; + if (systemf("gunzip -V %s", redirectArgs) == 0) + value = 1; + } + return (value); +} + +/* + * Sleep as needed; useful for verifying disk timestamp changes by + * ensuring that the wall-clock time has actually changed before we + * go back to re-read something from disk. + */ +void +sleepUntilAfter(time_t t) +{ + while (t >= time(NULL)) +#if defined(_WIN32) && !defined(__CYGWIN__) + Sleep(500); +#else + sleep(1); +#endif +} + /* * Call standard system() call, but build up the command line using * sprintf() conventions. @@ -634,6 +1480,8 @@ systemf(const char *fmt, ...) va_start(ap, fmt); vsprintf(buff, fmt, ap); + if (verbosity > VERBOSITY_FULL) + logprintf("Cmd: %s\n", buff); r = system(buff); va_end(ap); return (r); @@ -652,112 +1500,263 @@ slurpfile(size_t * sizep, const char *fmt, ...) va_list ap; char *p; ssize_t bytes_read; - int fd; + FILE *f; int r; va_start(ap, fmt); vsprintf(filename, fmt, ap); va_end(ap); - fd = open(filename, O_RDONLY); - if (fd < 0) { + f = fopen(filename, "rb"); + if (f == NULL) { /* Note: No error; non-existent file is okay here. */ return (NULL); } - r = fstat(fd, &st); + r = fstat(fileno(f), &st); if (r != 0) { - fprintf(stderr, "Can't stat file %s\n", filename); - close(fd); + logprintf("Can't stat file %s\n", filename); + fclose(f); return (NULL); } - p = malloc(st.st_size + 1); + p = malloc((size_t)st.st_size + 1); if (p == NULL) { - fprintf(stderr, "Can't allocate %ld bytes of memory to read file %s\n", (long int)st.st_size, filename); - close(fd); + logprintf("Can't allocate %ld bytes of memory to read file %s\n", + (long int)st.st_size, filename); + fclose(f); return (NULL); } - bytes_read = read(fd, p, st.st_size); + bytes_read = fread(p, 1, (size_t)st.st_size, f); if (bytes_read < st.st_size) { - fprintf(stderr, "Can't read file %s\n", filename); - close(fd); + logprintf("Can't read file %s\n", filename); + fclose(f); free(p); return (NULL); } p[st.st_size] = '\0'; if (sizep != NULL) *sizep = (size_t)st.st_size; - close(fd); + fclose(f); return (p); } +/* Read a uuencoded file from the reference directory, decode, and + * write the result into the current directory. */ +#define UUDECODE(c) (((c) - 0x20) & 0x3f) +void +extract_reference_file(const char *name) +{ + char buff[1024]; + FILE *in, *out; + + sprintf(buff, "%s/%s.uu", refdir, name); + in = fopen(buff, "r"); + failure("Couldn't open reference file %s", buff); + assert(in != NULL); + if (in == NULL) + return; + /* Read up to and including the 'begin' line. */ + for (;;) { + if (fgets(buff, sizeof(buff), in) == NULL) { + /* TODO: This is a failure. */ + return; + } + if (memcmp(buff, "begin ", 6) == 0) + break; + } + /* Now, decode the rest and write it. */ + /* Not a lot of error checking here; the input better be right. */ + out = fopen(name, "wb"); + while (fgets(buff, sizeof(buff), in) != NULL) { + char *p = buff; + int bytes; + + if (memcmp(buff, "end", 3) == 0) + break; + + bytes = UUDECODE(*p++); + while (bytes > 0) { + int n = 0; + /* Write out 1-3 bytes from that. */ + if (bytes > 0) { + n = UUDECODE(*p++) << 18; + n |= UUDECODE(*p++) << 12; + fputc(n >> 16, out); + --bytes; + } + if (bytes > 0) { + n |= UUDECODE(*p++) << 6; + fputc((n >> 8) & 0xFF, out); + --bytes; + } + if (bytes > 0) { + n |= UUDECODE(*p++); + fputc(n & 0xFF, out); + --bytes; + } + } + } + fclose(out); + fclose(in); +} + /* - * "list.h" is automatically generated; it just has a lot of lines like: - * DEFINE_TEST(function_name) - * It's used above to declare all of the test functions. - * We reuse it here to define a list of all tests (functions and names). + * + * TEST management + * + */ + +/* + * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has + * a line like + * DEFINE_TEST(test_function) + * for each test. */ + +/* Use "list.h" to declare all of the test functions. */ +#undef DEFINE_TEST +#define DEFINE_TEST(name) void name(void); +#include "list.h" + +/* Use "list.h" to create a list of all tests (functions and names). */ #undef DEFINE_TEST -#define DEFINE_TEST(n) { n, #n }, -struct { void (*func)(void); const char *name; } tests[] = { +#define DEFINE_TEST(n) { n, #n, 0 }, +struct { void (*func)(void); const char *name; int failures; } tests[] = { #include "list.h" }; /* - * Each test is run in a private work dir. Those work dirs - * do have consistent and predictable names, in case a group - * of tests need to collaborate. However, there is no provision - * for requiring that tests run in a certain order. + * Summarize repeated failures in the just-completed test. */ -static int test_run(int i, const char *tmpdir) +static void +test_summarize(const char *filename, int failed) { - int failures_before = failures; + unsigned int i; - if (!quiet_flag) { - printf("%d: %s\n", i, tests[i].name); + switch (verbosity) { + case VERBOSITY_SUMMARY_ONLY: + printf(failed ? "E" : "."); fflush(stdout); + break; + case VERBOSITY_PASSFAIL: + printf(failed ? "FAIL\n" : "ok\n"); + break; } - /* - * Always explicitly chdir() in case the last test moved us to - * a strange place. - */ - if (chdir(tmpdir)) { - fprintf(stderr, - "ERROR: Couldn't chdir to temp dir %s\n", - tmpdir); - exit(1); + log_console = (verbosity == VERBOSITY_LIGHT_REPORT); + + for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) { + if (failed_lines[i].count > 1 && !failed_lines[i].skip) + logprintf("%s:%d: Summary: Failed %d times\n", + filename, i, failed_lines[i].count); + } + /* Clear the failure history for the next file. */ + memset(failed_lines, 0, sizeof(failed_lines)); +} + +/* + * Actually run a single test, with appropriate setup and cleanup. + */ +static int +test_run(int i, const char *tmpdir) +{ + char logfilename[64]; + int failures_before = failures; + int oldumask; + + switch (verbosity) { + case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */ + break; + case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */ + printf("%3d: %-50s", i, tests[i].name); + fflush(stdout); + break; + default: /* Title of test, details will follow */ + printf("%3d: %s\n", i, tests[i].name); } - /* Create a temp directory for this specific test. */ - if (mkdir(tests[i].name, 0755)) { + + /* Chdir to the top-level work directory. */ + if (!assertChdir(tmpdir)) { fprintf(stderr, - "ERROR: Couldn't create temp dir ``%s''\n", - tests[i].name); + "ERROR: Can't chdir to top work dir %s\n", tmpdir); exit(1); } - /* Chdir() to that work directory. */ - if (chdir(tests[i].name)) { + /* Create a log file for this test. */ + sprintf(logfilename, "%s.log", tests[i].name); + logfile = fopen(logfilename, "w"); + fprintf(logfile, "%s\n\n", tests[i].name); + /* Chdir() to a work dir for this specific test. */ + if (!assertMakeDir(tests[i].name, 0755) + || !assertChdir(tests[i].name)) { fprintf(stderr, - "ERROR: Couldn't chdir to temp dir ``%s''\n", - tests[i].name); + "ERROR: Can't chdir to work dir %s/%s\n", + tmpdir, tests[i].name); exit(1); } /* Explicitly reset the locale before each test. */ setlocale(LC_ALL, "C"); - /* Run the actual test. */ + /* Record the umask before we run the test. */ + umask(oldumask = umask(0)); + /* + * Run the actual test. + */ (*tests[i].func)(); - /* Summarize the results of this test. */ - summarize(); - /* If there were no failures, we can remove the work dir. */ - if (failures == failures_before) { - if (!keep_temp_files && chdir(tmpdir) == 0) { + /* + * Clean up and report afterwards. + */ + /* Restore umask */ + umask(oldumask); + /* Reset locale. */ + setlocale(LC_ALL, "C"); + /* Reset directory. */ + if (!assertChdir(tmpdir)) { + fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n", + tmpdir); + exit(1); + } + /* Report per-test summaries. */ + tests[i].failures = failures - failures_before; + test_summarize(test_filename, tests[i].failures); + /* Close the per-test log file. */ + fclose(logfile); + logfile = NULL; + /* If there were no failures, we can remove the work dir and logfile. */ + if (tests[i].failures == 0) { + if (!keep_temp_files && assertChdir(tmpdir)) { +#if defined(_WIN32) && !defined(__CYGWIN__) + /* Make sure not to leave empty directories. + * Sometimes a processing of closing files used by tests + * is not done, then rmdir will be failed and it will + * leave a empty test directory. So we should wait a few + * seconds and retry rmdir. */ + int r, t; + for (t = 0; t < 10; t++) { + if (t > 0) + Sleep(1000); + r = systemf("rmdir /S /Q %s", tests[i].name); + if (r == 0) + break; + } + systemf("del %s", logfilename); +#else systemf("rm -rf %s", tests[i].name); + systemf("rm %s", logfilename); +#endif } } /* Return appropriate status. */ - return (failures == failures_before ? 0 : 1); + return (tests[i].failures); } -static void usage(const char *program) +/* + * + * + * MAIN and support routines. + * + * + */ + +static void +usage(const char *program) { static const int limit = sizeof(tests) / sizeof(tests[0]); int i; @@ -783,92 +1782,129 @@ static void usage(const char *program) exit(1); } -#define UUDECODE(c) (((c) - 0x20) & 0x3f) - -void -extract_reference_file(const char *name) +static char * +get_refdir(const char *d) { - char buff[1024]; - FILE *in, *out; + char tried[512] = { '\0' }; + char buff[128]; + char *pwd, *p; + + /* If a dir was specified, try that */ + if (d != NULL) { + pwd = NULL; + snprintf(buff, sizeof(buff), "%s", d); + p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); + if (p != NULL) goto success; + strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); + strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); + goto failure; + } - sprintf(buff, "%s/%s.uu", refdir, name); - in = fopen(buff, "r"); - failure("Couldn't open reference file %s", buff); - assert(in != NULL); - if (in == NULL) - return; - /* Read up to and including the 'begin' line. */ - for (;;) { - if (fgets(buff, sizeof(buff), in) == NULL) { - /* TODO: This is a failure. */ - return; - } - if (memcmp(buff, "begin ", 6) == 0) - break; + /* Get the current dir. */ + pwd = getcwd(NULL, 0); + while (pwd[strlen(pwd) - 1] == '\n') + pwd[strlen(pwd) - 1] = '\0'; + + /* Look for a known file. */ + snprintf(buff, sizeof(buff), "%s", pwd); + p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); + if (p != NULL) goto success; + strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); + strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); + + snprintf(buff, sizeof(buff), "%s/test", pwd); + p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); + if (p != NULL) goto success; + strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); + strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); + +#if defined(LIBRARY) + snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY); +#else + snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM); +#endif + p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); + if (p != NULL) goto success; + strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); + strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); + + if (memcmp(pwd, "/usr/obj", 8) == 0) { + snprintf(buff, sizeof(buff), "%s", pwd + 8); + p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); + if (p != NULL) goto success; + strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); + strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); + + snprintf(buff, sizeof(buff), "%s/test", pwd + 8); + p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); + if (p != NULL) goto success; + strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); + strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); } - /* Now, decode the rest and write it. */ - /* Not a lot of error checking here; the input better be right. */ - out = fopen(name, "w"); - while (fgets(buff, sizeof(buff), in) != NULL) { - char *p = buff; - int bytes; - if (memcmp(buff, "end", 3) == 0) - break; +failure: + printf("Unable to locate known reference file %s\n", KNOWNREF); + printf(" Checked following directories:\n%s\n", tried); +#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG) + DebugBreak(); +#endif + exit(1); - bytes = UUDECODE(*p++); - while (bytes > 0) { - int n = 0; - /* Write out 1-3 bytes from that. */ - if (bytes > 0) { - n = UUDECODE(*p++) << 18; - n |= UUDECODE(*p++) << 12; - fputc(n >> 16, out); - --bytes; - } - if (bytes > 0) { - n |= UUDECODE(*p++) << 6; - fputc((n >> 8) & 0xFF, out); - --bytes; - } - if (bytes > 0) { - n |= UUDECODE(*p++); - fputc(n & 0xFF, out); - --bytes; - } - } - } - fclose(out); - fclose(in); +success: + free(p); + free(pwd); + return strdup(buff); } - -int main(int argc, char **argv) +int +main(int argc, char **argv) { static const int limit = sizeof(tests) / sizeof(tests[0]); - int i, tests_run = 0, tests_failed = 0, opt; + int i, tests_run = 0, tests_failed = 0, option; time_t now; char *refdir_alloc = NULL; - char *progname, *p; + const char *progname; + const char *tmp, *option_arg, *p; char tmpdir[256]; char tmpdir_timestamp[256]; + (void)argc; /* UNUSED */ + +#if defined(HAVE__CrtSetReportMode) + /* To stop to run the default invalid parameter handler. */ + _set_invalid_parameter_handler(invalid_parameter_handler); + /* Disable annoying assertion message box. */ + _CrtSetReportMode(_CRT_ASSERT, 0); +#endif + /* * Name of this program, used to build root of our temp directory * tree. */ progname = p = argv[0]; while (*p != '\0') { - if (*p == '/') + /* Support \ or / dir separators for Windows compat. */ + if (*p == '/' || *p == '\\') progname = p + 1; ++p; } #ifdef PROGRAM /* Get the target program from environment, if available. */ - testprog = getenv(ENVBASE); + testprogfile = getenv(ENVBASE); #endif + if (getenv("TMPDIR") != NULL) + tmp = getenv("TMPDIR"); + else if (getenv("TMP") != NULL) + tmp = getenv("TMP"); + else if (getenv("TEMP") != NULL) + tmp = getenv("TEMP"); + else if (getenv("TEMPDIR") != NULL) + tmp = getenv("TEMPDIR"); + else + tmp = "/tmp"; + /* Allow -d to be controlled through the environment. */ if (getenv(ENVBASE "_DEBUG") != NULL) dump_on_failure = 1; @@ -877,46 +1913,86 @@ int main(int argc, char **argv) refdir = getenv(ENVBASE "_TEST_FILES"); /* - * Parse options. + * Parse options, without using getopt(), which isn't available + * on all platforms. */ - while ((opt = getopt(argc, argv, "dkp:qr:v")) != -1) { - switch (opt) { - case 'd': - dump_on_failure = 1; + ++argv; /* Skip program name */ + while (*argv != NULL) { + if (**argv != '-') break; - case 'k': - keep_temp_files = 1; - break; - case 'p': + p = *argv++; + ++p; /* Skip '-' */ + while (*p != '\0') { + option = *p++; + option_arg = NULL; + /* If 'opt' takes an argument, parse that. */ + if (option == 'p' || option == 'r') { + if (*p != '\0') + option_arg = p; + else if (*argv == NULL) { + fprintf(stderr, + "Option -%c requires argument.\n", + option); + usage(progname); + } else + option_arg = *argv++; + p = ""; /* End of this option word. */ + } + + /* Now, handle the option. */ + switch (option) { + case 'd': + dump_on_failure = 1; + break; + case 'k': + keep_temp_files = 1; + break; + case 'p': #ifdef PROGRAM - testprog = optarg; + testprogfile = option_arg; #else - usage(progname); + usage(progname); #endif - break; - case 'q': - quiet_flag++; - break; - case 'r': - refdir = optarg; - break; - case 'v': - verbose = 1; - break; - case '?': - default: - usage(progname); + break; + case 'q': + verbosity--; + break; + case 'r': + refdir = option_arg; + break; + case 'v': + verbosity++; + break; + default: + usage(progname); + } } } - argc -= optind; - argv += optind; /* * Sanity-check that our options make sense. */ #ifdef PROGRAM - if (testprog == NULL) + if (testprogfile == NULL) usage(progname); + { + char *testprg; +#if defined(_WIN32) && !defined(__CYGWIN__) + /* Command.com sometimes rejects '/' separators. */ + testprg = strdup(testprogfile); + for (i = 0; testprg[i] != '\0'; i++) { + if (testprg[i] == '/') + testprg[i] = '\\'; + } + testprogfile = testprg; +#endif + /* Quote the name that gets put into shell command lines. */ + testprg = malloc(strlen(testprogfile) + 3); + strcpy(testprg, "\""); + strcat(testprg, testprogfile); + strcat(testprg, "\""); + testprog = testprg; + } #endif /* @@ -925,40 +2001,37 @@ int main(int argc, char **argv) * to make it easier to track the results of multiple tests. */ now = time(NULL); - for (i = 0; i < 1000; i++) { + for (i = 0; ; i++) { strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp), "%Y-%m-%dT%H.%M.%S", localtime(&now)); - sprintf(tmpdir, "/tmp/%s.%s-%03d", progname, tmpdir_timestamp, i); - if (mkdir(tmpdir,0755) == 0) + sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname, + tmpdir_timestamp, i); + if (assertMakeDir(tmpdir,0755)) break; - if (errno == EEXIST) - continue; - fprintf(stderr, "ERROR: Unable to create temp directory %s\n", - tmpdir); - exit(1); + if (i >= 999) { + fprintf(stderr, + "ERROR: Unable to create temp directory %s\n", + tmpdir); + exit(1); + } } /* * If the user didn't specify a directory for locating - * reference files, use the current directory for that. + * reference files, try to find the reference files in + * the "usual places." */ - if (refdir == NULL) { - systemf("/bin/pwd > %s/refdir", tmpdir); - refdir = refdir_alloc = slurpfile(NULL, "%s/refdir", tmpdir); - p = refdir + strlen(refdir); - while (p[-1] == '\n') { - --p; - *p = '\0'; - } - systemf("rm %s/refdir", tmpdir); - } + refdir = refdir_alloc = get_refdir(refdir); /* * Banner with basic information. */ - if (!quiet_flag) { - printf("Running tests in: %s\n", tmpdir); + printf("\n"); + printf("If tests fail or crash, details will be in:\n"); + printf(" %s\n", tmpdir); + printf("\n"); + if (verbosity > VERBOSITY_SUMMARY_ONLY) { printf("Reference files will be read from: %s\n", refdir); #ifdef PROGRAM printf("Running tests on: %s\n", testprog); @@ -966,12 +2039,15 @@ int main(int argc, char **argv) printf("Exercising: "); fflush(stdout); printf("%s\n", EXTRA_VERSION); + } else { + printf("Running "); + fflush(stdout); } /* * Run some or all of the individual tests. */ - if (argc == 0) { + if (*argv == NULL) { /* Default: Run all tests. */ for (i = 0; i < limit; i++) { if (test_run(i, tmpdir)) @@ -980,15 +2056,30 @@ int main(int argc, char **argv) } } else { while (*(argv) != NULL) { - i = atoi(*argv); - if (**argv < '0' || **argv > '9' || i < 0 || i >= limit) { - printf("*** INVALID Test %s\n", *argv); - usage(progname); + if (**argv >= '0' && **argv <= '9') { + i = atoi(*argv); + if (i < 0 || i >= limit) { + printf("*** INVALID Test %s\n", *argv); + free(refdir_alloc); + usage(progname); + /* usage() never returns */ + } } else { - if (test_run(i, tmpdir)) - tests_failed++; - tests_run++; + for (i = 0; i < limit; ++i) { + if (strcmp(*argv, tests[i].name) == 0) + break; + } + if (i >= limit) { + printf("*** INVALID Test ``%s''\n", + *argv); + free(refdir_alloc); + usage(progname); + /* usage() never returns */ + } } + if (test_run(i, tmpdir)) + tests_failed++; + tests_run++; argv++; } } @@ -996,20 +2087,38 @@ int main(int argc, char **argv) /* * Report summary statistics. */ - if (!quiet_flag) { + if (verbosity > VERBOSITY_SUMMARY_ONLY) { + printf("\n"); + printf("Totals:\n"); + printf(" Tests run: %8d\n", tests_run); + printf(" Tests failed: %8d\n", tests_failed); + printf(" Assertions checked:%8d\n", assertions); + printf(" Assertions failed: %8d\n", failures); + printf(" Skips reported: %8d\n", skips); + } + if (failures) { + printf("\n"); + printf("Failing tests:\n"); + for (i = 0; i < limit; ++i) { + if (tests[i].failures) + printf(" %d: %s (%d failures)\n", i, + tests[i].name, tests[i].failures); + } printf("\n"); - printf("%d of %d tests reported failures\n", - tests_failed, tests_run); - printf(" Total of %d assertions checked.\n", assertions); - printf(" Total of %d assertions failed.\n", failures); - printf(" Total of %d assertions skipped.\n", skips); + printf("Details for failing tests: %s\n", tmpdir); + printf("\n"); + } else { + if (verbosity == VERBOSITY_SUMMARY_ONLY) + printf("\n"); + printf("%d tests passed, no failures\n", tests_run); } free(refdir_alloc); /* If the final tmpdir is empty, we can remove it. */ /* This should be the usual case when all tests succeed. */ + assertChdir(".."); rmdir(tmpdir); - return (tests_failed); + return (tests_failed ? 1 : 0); } diff --git a/archivers/libarchive/files/cpio/test/test.h b/archivers/libarchive/files/cpio/test/test.h index 431d5d7c4d5..c7515501ba7 100644 --- a/archivers/libarchive/files/cpio/test/test.h +++ b/archivers/libarchive/files/cpio/test/test.h @@ -33,39 +33,95 @@ */ #if defined(HAVE_CONFIG_H) /* Most POSIX platforms use the 'configure' script to build config.h */ -#include "../../config.h" +#include "config.h" #elif defined(__FreeBSD__) /* Building as part of FreeBSD system requires a pre-built config.h. */ -#include "../config_freebsd.h" -#elif defined(_WIN32) +#include "config_freebsd.h" +#elif defined(_WIN32) && !defined(__CYGWIN__) /* Win32 can't run the 'configure' script. */ -#include "../config_windows.h" +#include "config_windows.h" #else /* Warn if the library hasn't been (automatically or manually) configured. */ #error Oops: No config.h and no pre-built configuration in test.h. #endif +#include <sys/types.h> /* Windows requires this before sys/stat.h */ +#include <sys/stat.h> + +#ifdef USE_DMALLOC +#include <dmalloc.h> +#endif +#if HAVE_DIRENT_H #include <dirent.h> +#endif +#ifdef HAVE_DIRECT_H +#include <direct.h> +#define dirent direct +#endif #include <errno.h> #include <fcntl.h> +#ifdef HAVE_IO_H +#include <io.h> +#endif #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <sys/stat.h> -#ifndef _WIN32 +#include <time.h> +#ifdef HAVE_UNISTD_H #include <unistd.h> #endif #include <wchar.h> - -#ifdef USE_DMALLOC -#include <dmalloc.h> +#ifdef HAVE_WINDOWS_H +#include <windows.h> #endif -/* No non-FreeBSD platform will have __FBSDID, so just define it here. */ -#ifdef __FreeBSD__ -#include <sys/cdefs.h> /* For __FBSDID */ +/* + * System-specific tweaks. We really want to minimize these + * as much as possible, since they make it harder to understand + * the mainline code. + */ + +/* Windows (including Visual Studio and MinGW but not Cygwin) */ +#if defined(_WIN32) && !defined(__CYGWIN__) +#include "../cpio_windows.h" +#if !defined(__BORLANDC__) +#define strdup _strdup +#endif +#define LOCALE_DE "deu" #else -#define __FBSDID(a) /* null */ +#define LOCALE_DE "de_DE.UTF-8" +#endif + +/* Visual Studio */ +#ifdef _MSC_VER +#define snprintf sprintf_s +#endif + +/* Cygwin */ +#if defined(__CYGWIN__) +/* Cygwin-1.7.x is lazy about populating nlinks, so don't + * expect it to be accurate. */ +# define NLINKS_INACCURATE_FOR_DIRS +#endif + +/* Haiku OS */ +#if defined(__HAIKU__) +/* Haiku has typedefs in stdint.h (needed for int64_t) */ +#include <stdint.h> +#endif + +/* Get a real definition for __FBSDID if we can */ +#if HAVE_SYS_CDEFS_H +#include <sys/cdefs.h> +#endif + +/* If not, define it so as to avoid dangling semicolons. */ +#ifndef __FBSDID +#define __FBSDID(a) struct _undefined_hack +#endif + +#ifndef O_BINARY +#define O_BINARY 0 #endif /* @@ -75,38 +131,81 @@ #define DEFINE_TEST(name) void name(void); void name(void) /* An implementation of the standard assert() macro */ -#define assert(e) test_assert(__FILE__, __LINE__, (e), #e, NULL) - +#define assert(e) assertion_assert(__FILE__, __LINE__, (e), #e, NULL) +/* chdir() and error if it fails */ +#define assertChdir(path) \ + assertion_chdir(__FILE__, __LINE__, path) /* Assert two integers are the same. Reports value of each one if not. */ -#define assertEqualInt(v1,v2) \ - test_assert_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL) - +#define assertEqualInt(v1,v2) \ + assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL) /* Assert two strings are the same. Reports value of each one if not. */ #define assertEqualString(v1,v2) \ - test_assert_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL) + assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL) /* As above, but v1 and v2 are wchar_t * */ #define assertEqualWString(v1,v2) \ - test_assert_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL) + assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL) /* As above, but raw blocks of bytes. */ #define assertEqualMem(v1, v2, l) \ - test_assert_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL) + assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL) /* Assert two files are the same; allow printf-style expansion of second name. * See below for comments about variable arguments here... */ #define assertEqualFile \ - test_setup(__FILE__, __LINE__);test_assert_equal_file + assertion_setup(__FILE__, __LINE__);assertion_equal_file /* Assert that a file is empty; supports printf-style arguments. */ #define assertEmptyFile \ - test_setup(__FILE__, __LINE__);test_assert_empty_file + assertion_setup(__FILE__, __LINE__);assertion_empty_file +/* Assert that a file is not empty; supports printf-style arguments. */ +#define assertNonEmptyFile \ + assertion_setup(__FILE__, __LINE__);assertion_non_empty_file +#define assertFileAtime(pathname, sec, nsec) \ + assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec) +#define assertFileAtimeRecent(pathname) \ + assertion_file_atime_recent(__FILE__, __LINE__, pathname) +#define assertFileBirthtime(pathname, sec, nsec) \ + assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec) +#define assertFileBirthtimeRecent(pathname) \ + assertion_file_birthtime_recent(__FILE__, __LINE__, pathname) /* Assert that a file exists; supports printf-style arguments. */ #define assertFileExists \ - test_setup(__FILE__, __LINE__);test_assert_file_exists + assertion_setup(__FILE__, __LINE__);assertion_file_exists /* Assert that a file exists; supports printf-style arguments. */ #define assertFileNotExists \ - test_setup(__FILE__, __LINE__);test_assert_file_not_exists + assertion_setup(__FILE__, __LINE__);assertion_file_not_exists /* Assert that file contents match a string; supports printf-style arguments. */ #define assertFileContents \ - test_setup(__FILE__, __LINE__);test_assert_file_contents + assertion_setup(__FILE__, __LINE__);assertion_file_contents +#define assertFileMtime(pathname, sec, nsec) \ + assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec) +#define assertFileMtimeRecent(pathname) \ + assertion_file_mtime_recent(__FILE__, __LINE__, pathname) +#define assertFileNLinks(pathname, nlinks) \ + assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks) +#define assertFileSize(pathname, size) \ + assertion_file_size(__FILE__, __LINE__, pathname, size) +#define assertTextFileContents \ + assertion_setup(__FILE__, __LINE__);assertion_text_file_contents +#define assertIsDir(pathname, mode) \ + assertion_is_dir(__FILE__, __LINE__, pathname, mode) +#define assertIsHardlink(path1, path2) \ + assertion_is_hardlink(__FILE__, __LINE__, path1, path2) +#define assertIsNotHardlink(path1, path2) \ + assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2) +#define assertIsReg(pathname, mode) \ + assertion_is_reg(__FILE__, __LINE__, pathname, mode) +#define assertIsSymlink(pathname, contents) \ + assertion_is_symlink(__FILE__, __LINE__, pathname, contents) +/* Create a directory, report error if it fails. */ +#define assertMakeDir(dirname, mode) \ + assertion_make_dir(__FILE__, __LINE__, dirname, mode) +#define assertMakeFile(path, mode, contents) \ + assertion_make_file(__FILE__, __LINE__, path, mode, contents) +#define assertMakeHardlink(newfile, oldfile) \ + assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile) +#define assertMakeSymlink(newfile, linkto) \ + assertion_make_symlink(__FILE__, __LINE__, newfile, linkto) +#define assertUmask(mask) \ + assertion_umask(__FILE__, __LINE__, mask) /* * This would be simple with C99 variadic macros, but I don't want to @@ -115,26 +214,60 @@ * but effective. */ #define skipping \ - test_setup(__FILE__, __LINE__);test_skipping + assertion_setup(__FILE__, __LINE__);test_skipping /* Function declarations. These are defined in test_utility.c. */ void failure(const char *fmt, ...); -void test_setup(const char *, int); +int assertion_assert(const char *, int, int, const char *, void *); +int assertion_chdir(const char *, int, const char *); +int assertion_empty_file(const char *, ...); +int assertion_equal_file(const char *, const char *, ...); +int assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *); +int assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *); +int assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *); +int assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *); +int assertion_file_atime(const char *, int, const char *, long, long); +int assertion_file_atime_recent(const char *, int, const char *); +int assertion_file_birthtime(const char *, int, const char *, long, long); +int assertion_file_birthtime_recent(const char *, int, const char *); +int assertion_file_contents(const void *, int, const char *, ...); +int assertion_file_exists(const char *, ...); +int assertion_file_mtime(const char *, int, const char *, long, long); +int assertion_file_mtime_recent(const char *, int, const char *); +int assertion_file_nlinks(const char *, int, const char *, int); +int assertion_file_not_exists(const char *, ...); +int assertion_file_size(const char *, int, const char *, long); +int assertion_is_dir(const char *, int, const char *, int); +int assertion_is_hardlink(const char *, int, const char *, const char *); +int assertion_is_not_hardlink(const char *, int, const char *, const char *); +int assertion_is_reg(const char *, int, const char *, int); +int assertion_is_symlink(const char *, int, const char *, const char *); +int assertion_make_dir(const char *, int, const char *, int); +int assertion_make_file(const char *, int, const char *, int, const char *); +int assertion_make_hardlink(const char *, int, const char *newpath, const char *); +int assertion_make_symlink(const char *, int, const char *newpath, const char *); +int assertion_non_empty_file(const char *, ...); +int assertion_text_file_contents(const char *buff, const char *f); +int assertion_umask(const char *, int, int); +void assertion_setup(const char *, int); + void test_skipping(const char *fmt, ...); -int test_assert(const char *, int, int, const char *, void *); -int test_assert_empty_file(const char *, ...); -int test_assert_equal_file(const char *, const char *, ...); -int test_assert_equal_int(const char *, int, int, const char *, int, const char *, void *); -int test_assert_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *); -int test_assert_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *); -int test_assert_equal_mem(const char *, int, const char *, const char *, const char *, const char *, size_t, const char *, void *); -int test_assert_file_contents(const void *, int, const char *, ...); -int test_assert_file_exists(const char *, ...); -int test_assert_file_not_exists(const char *, ...); /* Like sprintf, then system() */ int systemf(const char * fmt, ...); +/* Delay until time() returns a value after this. */ +void sleepUntilAfter(time_t); + +/* Return true if this platform can create symlinks. */ +int canSymlink(void); + +/* Return true if this platform can run the "gzip" program. */ +int canGzip(void); + +/* Return true if this platform can run the "gunzip" program. */ +int canGunzip(void); + /* Suck file into string allocated via malloc(). Call free() when done. */ /* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */ char *slurpfile(size_t *, const char *fmt, ...); @@ -147,4 +280,7 @@ void extract_reference_file(const char *); */ /* Pathname of exe to be tested. */ -char *testprog; +const char *testprogfile; +/* Name of exe to use in printf-formatted command strings. */ +/* On Windows, this includes leading/trailing quotes. */ +const char *testprog; diff --git a/archivers/libarchive/files/cpio/test/test_0.c b/archivers/libarchive/files/cpio/test/test_0.c index 7a72af1c8ed..75a1437fd70 100644 --- a/archivers/libarchive/files/cpio/test/test_0.c +++ b/archivers/libarchive/files/cpio/test/test_0.c @@ -29,16 +29,21 @@ __FBSDID("$FreeBSD$"); * This first test does basic sanity checks on the environment. For * most of these, we just exit on failure. */ +#if !defined(_WIN32) || defined(__CYGWIN__) +#define DEV_NULL "/dev/null" +#else +#define DEV_NULL "NUL" +#endif DEFINE_TEST(test_0) { struct stat st; - failure("File %s does not exist?!", testprog); - if (!assertEqualInt(0, stat(testprog, &st))) + failure("File %s does not exist?!", testprogfile); + if (!assertEqualInt(0, stat(testprogfile, &st))) exit(1); - failure("%s is not executable?!", testprog); + failure("%s is not executable?!", testprogfile); if (!assert((st.st_mode & 0111) != 0)) exit(1); @@ -46,9 +51,9 @@ DEFINE_TEST(test_0) * Try to succesfully run the program; this requires that * we know some option that will succeed. */ - if (0 == systemf("%s --version >/dev/null", testprog)) { + if (0 == systemf("%s --version >" DEV_NULL, testprog)) { /* This worked. */ - } else if (0 == systemf("%s -W version >/dev/null", testprog)) { + } else if (0 == systemf("%s -W version >" DEV_NULL, testprog)) { /* This worked. */ } else { failure("Unable to successfully run any of the following:\n" diff --git a/archivers/libarchive/files/cpio/test/test_basic.c b/archivers/libarchive/files/cpio/test/test_basic.c index 7f1fe6de9f7..852b069a16e 100644 --- a/archivers/libarchive/files/cpio/test/test_basic.c +++ b/archivers/libarchive/files/cpio/test/test_basic.c @@ -23,71 +23,35 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "test.h" -__FBSDID("$FreeBSD$"); +__FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_basic.c,v 1.4 2008/08/25 06:39:29 kientzle Exp $"); static void -verify_files(const char *target) +verify_files(const char *msg) { - struct stat st, st2; - char buff[128]; - int r; - /* * Verify unpacked files. */ /* Regular file with 2 links. */ - r = lstat("file", &st); - failure("Failed to stat file %s/file, errno=%d", target, errno); - assertEqualInt(r, 0); - if (r == 0) { - assert(S_ISREG(st.st_mode)); - assertEqualInt(0644, st.st_mode & 0777); - assertEqualInt(10, st.st_size); - failure("file %s/file should have 2 links", target); - assertEqualInt(2, st.st_nlink); - } + assertIsReg("file", 0644); + failure(msg); + assertFileSize("file", 10); + assertFileNLinks("file", 2); /* Another name for the same file. */ - r = lstat("linkfile", &st2); - failure("Failed to stat file %s/linkfile, errno=%d", target, errno); - assertEqualInt(r, 0); - if (r == 0) { - assert(S_ISREG(st2.st_mode)); - assertEqualInt(0644, st2.st_mode & 0777); - assertEqualInt(10, st2.st_size); - failure("file %s/linkfile should have 2 links", target); - assertEqualInt(2, st2.st_nlink); - /* Verify that the two are really hardlinked. */ - assertEqualInt(st.st_dev, st2.st_dev); - failure("%s/linkfile and %s/file should be hardlinked", - target, target); - assertEqualInt(st.st_ino, st2.st_ino); - } + assertIsHardlink("linkfile", "file"); /* Symlink */ - r = lstat("symlink", &st); - failure("Failed to stat file %s/symlink, errno=%d", target, errno); - assertEqualInt(r, 0); - if (r == 0) { - failure("symlink should be a symlink; actual mode is %o", - st.st_mode); - assert(S_ISLNK(st.st_mode)); - if (S_ISLNK(st.st_mode)) { - r = readlink("symlink", buff, sizeof(buff)); - assertEqualInt(r, 4); - buff[r] = '\0'; - assertEqualString(buff, "file"); - } - } + if (canSymlink()) + assertIsSymlink("symlink", "file"); + + /* Another file with 1 link and different permissions. */ + assertIsReg("file2", 0777); + assertFileSize("file2", 10); + assertFileNLinks("file2", 1); /* dir */ - r = lstat("dir", &st); - if (r == 0) { - assertEqualInt(r, 0); - assert(S_ISDIR(st.st_mode)); - assertEqualInt(0775, st.st_mode & 0777); - } + assertIsDir("dir", 0775); } static void @@ -98,7 +62,7 @@ basic_cpio(const char *target, { int r; - if (!assertEqualInt(0, mkdir(target, 0775))) + if (!assertMakeDir(target, 0775)) return; /* Use the cpio program to create an archive. */ @@ -107,11 +71,11 @@ basic_cpio(const char *target, failure("Error invoking %s -o %s", testprog, pack_options); assertEqualInt(r, 0); - chdir(target); + assertChdir(target); /* Verify stderr. */ failure("Expected: %s, options=%s", se, pack_options); - assertFileContents(se, strlen(se), "pack.err"); + assertTextFileContents(se, "pack.err"); /* * Use cpio to unpack the archive into another directory. @@ -123,11 +87,11 @@ basic_cpio(const char *target, /* Verify stderr. */ failure("Error invoking %s -i %s in dir %s", testprog, unpack_options, target); - assertFileContents(se, strlen(se), "unpack.err"); + assertTextFileContents(se, "unpack.err"); - verify_files(target); + verify_files(pack_options); - chdir(".."); + assertChdir(".."); } static void @@ -135,70 +99,75 @@ passthrough(const char *target) { int r; - if (!assertEqualInt(0, mkdir(target, 0775))) + if (!assertMakeDir(target, 0775)) return; /* * Use cpio passthrough mode to copy files to another directory. */ - r = systemf("%s -p -W quiet %s <filelist >%s/stdout 2>%s/stderr", + r = systemf("%s -p %s <filelist >%s/stdout 2>%s/stderr", testprog, target, target, target); failure("Error invoking %s -p", testprog); assertEqualInt(r, 0); - chdir(target); + assertChdir(target); /* Verify stderr. */ failure("Error invoking %s -p in dir %s", testprog, target); - assertEmptyFile("stderr"); + assertTextFileContents("1 block\n", "stderr"); - verify_files(target); - chdir(".."); + verify_files("passthrough"); + assertChdir(".."); } DEFINE_TEST(test_basic) { - int fd; - int filelist; - int oldumask; + FILE *filelist; + const char *msg; - oldumask = umask(0); + assertUmask(0); /* * Create an assortment of files on disk. */ - filelist = open("filelist", O_CREAT | O_WRONLY, 0644); + filelist = fopen("filelist", "w"); /* File with 10 bytes content. */ - fd = open("file", O_CREAT | O_WRONLY, 0644); - assert(fd >= 0); - assertEqualInt(10, write(fd, "123456789", 10)); - close(fd); - write(filelist, "file\n", 5); + assertMakeFile("file", 0644, "1234567890"); + fprintf(filelist, "file\n"); /* hardlink to above file. */ - assertEqualInt(0, link("file", "linkfile")); - write(filelist, "linkfile\n", 9); + assertMakeHardlink("linkfile", "file"); + fprintf(filelist, "linkfile\n"); /* Symlink to above file. */ - assertEqualInt(0, symlink("file", "symlink")); - write(filelist, "symlink\n", 8); + if (canSymlink()) { + assertMakeSymlink("symlink", "file"); + fprintf(filelist, "symlink\n"); + } + + /* Another file with different permissions. */ + assertMakeFile("file2", 0777, "1234567890"); + fprintf(filelist, "file2\n"); /* Directory. */ - assertEqualInt(0, mkdir("dir", 0775)); - write(filelist, "dir\n", 4); + assertMakeDir("dir", 0775); + fprintf(filelist, "dir\n"); /* All done. */ - close(filelist); + fclose(filelist); + + assertUmask(022); /* Archive/dearchive with a variety of options. */ - basic_cpio("copy", "", "", "1 block\n"); - basic_cpio("copy_odc", "--format=odc", "", "1 block\n"); + msg = canSymlink() ? "2 blocks\n" : "1 block\n"; + basic_cpio("copy", "", "", msg); + basic_cpio("copy_odc", "--format=odc", "", msg); basic_cpio("copy_newc", "-H newc", "", "2 blocks\n"); - basic_cpio("copy_cpio", "-H odc", "", "1 block\n"); - basic_cpio("copy_ustar", "-H ustar", "", "7 blocks\n"); + basic_cpio("copy_cpio", "-H odc", "", msg); + msg = canSymlink() ? "9 blocks\n" : "8 blocks\n"; + basic_cpio("copy_ustar", "-H ustar", "", msg); + /* Copy in one step using -p */ passthrough("passthrough"); - - umask(oldumask); } diff --git a/archivers/libarchive/files/cpio/test/test_cmdline.c b/archivers/libarchive/files/cpio/test/test_cmdline.c new file mode 100644 index 00000000000..2dd7d651dc5 --- /dev/null +++ b/archivers/libarchive/files/cpio/test/test_cmdline.c @@ -0,0 +1,107 @@ +/*- + * Copyright (c) 2003-2009 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. + * 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 "test.h" +__FBSDID("$FreeBSD$"); + +/* + * Test the command-line parsing. + */ + +DEFINE_TEST(test_cmdline) +{ + FILE *f; + + /* Create an empty file. */ + f = fopen("empty", "wb"); + assert(f != NULL); + fclose(f); + + failure("-Q is an invalid option on every cpio program I know of"); + assert(0 != systemf("%s -i -Q <empty >1.out 2>1.err", testprog)); + assertEmptyFile("1.out"); + + failure("-f requires an argument"); + assert(0 != systemf("%s -if <empty >2.out 2>2.err", testprog)); + assertEmptyFile("2.out"); + + failure("-f requires an argument"); + assert(0 != systemf("%s -i -f <empty >3.out 2>3.err", testprog)); + assertEmptyFile("3.out"); + + failure("--format requires an argument"); + assert(0 != systemf("%s -i --format <empty >4.out 2>4.err", testprog)); + assertEmptyFile("4.out"); + + failure("--badopt is an invalid option"); + assert(0 != systemf("%s -i --badop <empty >5.out 2>5.err", testprog)); + assertEmptyFile("5.out"); + + failure("--badopt is an invalid option"); + assert(0 != systemf("%s -i --badopt <empty >6.out 2>6.err", testprog)); + assertEmptyFile("6.out"); + + failure("--n is ambiguous"); + assert(0 != systemf("%s -i --n <empty >7.out 2>7.err", testprog)); + assertEmptyFile("7.out"); + + failure("--create forbids an argument"); + assert(0 != systemf("%s --create=arg <empty >8.out 2>8.err", testprog)); + assertEmptyFile("8.out"); + + failure("-i with empty input should succeed"); + assert(0 == systemf("%s -i <empty >9.out 2>9.err", testprog)); + assertEmptyFile("9.out"); + + failure("-o with empty input should succeed"); + assert(0 == systemf("%s -o <empty >10.out 2>10.err", testprog)); + + failure("-i -p is nonsense"); + assert(0 != systemf("%s -i -p <empty >11.out 2>11.err", testprog)); + assertEmptyFile("11.out"); + + failure("-p -i is nonsense"); + assert(0 != systemf("%s -p -i <empty >12.out 2>12.err", testprog)); + assertEmptyFile("12.out"); + + failure("-i -o is nonsense"); + assert(0 != systemf("%s -i -o <empty >13.out 2>13.err", testprog)); + assertEmptyFile("13.out"); + + failure("-o -i is nonsense"); + assert(0 != systemf("%s -o -i <empty >14.out 2>14.err", testprog)); + assertEmptyFile("14.out"); + + failure("-o -p is nonsense"); + assert(0 != systemf("%s -o -p <empty >15.out 2>15.err", testprog)); + assertEmptyFile("15.out"); + + failure("-p -o is nonsense"); + assert(0 != systemf("%s -p -o <empty >16.out 2>16.err", testprog)); + assertEmptyFile("16.out"); + + failure("-p with empty input should fail"); + assert(0 != systemf("%s -p <empty >17.out 2>17.err", testprog)); + assertEmptyFile("17.out"); +} diff --git a/archivers/libarchive/files/cpio/test/test_format_newc.c b/archivers/libarchive/files/cpio/test/test_format_newc.c index 0714bd427e2..06749a2f9ca 100644 --- a/archivers/libarchive/files/cpio/test/test_format_newc.c +++ b/archivers/libarchive/files/cpio/test/test_format_newc.c @@ -23,7 +23,14 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "test.h" -__FBSDID("$FreeBSD$"); +__FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_format_newc.c,v 1.2 2008/08/22 02:09:10 kientzle Exp $"); + +/* Number of bytes needed to pad 'n' to multiple of 'block', assuming + * that 'block' is a power of two. This trick can be more easily + * remembered as -n & (block - 1), but many compilers quite reasonably + * warn about "-n" when n is an unsigned value. (~(n) + 1) is the + * same thing, but written in a way that won't offend anyone. */ +#define PAD(n, block) ((~(n) + 1) & ((block) - 1)) static int is_hex(const char *p, size_t l) @@ -63,60 +70,67 @@ from_hex(const char *p, size_t l) DEFINE_TEST(test_format_newc) { - int fd, list; + FILE *list; int r; int devmajor, devminor, ino, gid; + int uid = -1; time_t t, t2, now; char *p, *e; - size_t s; - mode_t oldmask; + size_t s, fs, ns; + + assertUmask(0); - oldmask = umask(0); +#if !defined(_WIN32) + uid = getuid(); +#endif /* * Create an assortment of files. * TODO: Extend this to cover more filetypes. */ - list = open("list", O_CREAT | O_WRONLY, 0644); + list = fopen("list", "w"); /* "file1" */ - fd = open("file1", O_CREAT | O_WRONLY, 0644); - assert(fd >= 0); - assertEqualInt(10, write(fd, "123456789", 10)); - close(fd); - assertEqualInt(6, write(list, "file1\n", 6)); + assertMakeFile("file1", 0644, "1234567890"); + fprintf(list, "file1\n"); /* "hardlink" */ - assertEqualInt(0, link("file1", "hardlink")); - assertEqualInt(9, write(list, "hardlink\n", 9)); + assertMakeHardlink("hardlink", "file1"); + fprintf(list, "hardlink\n"); /* Another hardlink, but this one won't be archived. */ - assertEqualInt(0, link("file1", "hardlink2")); + assertMakeHardlink("hardlink2", "file1"); /* "symlink" */ - assertEqualInt(0, symlink("file1", "symlink")); - assertEqualInt(8, write(list, "symlink\n", 8)); + if (canSymlink()) { + assertMakeSymlink("symlink", "file1"); + fprintf(list, "symlink\n"); + } /* "dir" */ - assertEqualInt(0, mkdir("dir", 0775)); - assertEqualInt(4, write(list, "dir\n", 4)); + assertMakeDir("dir", 0775); + fprintf(list, "dir\n"); /* Record some facts about what we just created: */ now = time(NULL); /* They were all created w/in last two seconds. */ /* Use the cpio program to create an archive. */ - close(list); + fclose(list); r = systemf("%s -o --format=newc <list >newc.out 2>newc.err", testprog); if (!assertEqualInt(r, 0)) return; /* Verify that nothing went to stderr. */ - assertFileContents("2 blocks\n", 9, "newc.err"); + if (canSymlink()) { + assertTextFileContents("2 blocks\n", "newc.err"); + } else { + assertTextFileContents("1 block\n", "newc.err"); + } /* Verify that stdout is a well-formed cpio file in "newc" format. */ p = slurpfile(&s, "newc.out"); - assertEqualInt(s, 1024); + assertEqualInt(s, canSymlink() ? 1024 : 512); e = p; /* @@ -128,8 +142,15 @@ DEFINE_TEST(test_format_newc) assert(is_hex(e, 110)); /* Entire header is octal digits. */ assertEqualMem(e + 0, "070701", 6); /* Magic */ ino = from_hex(e + 6, 8); /* ino */ +#if defined(_WIN32) && !defined(__CYGWIN__) + /* Group members bits and others bits do not work. */ + assertEqualInt(0x8180, from_hex(e + 14, 8) & 0xffc0); /* Mode */ +#else assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */ - assertEqualInt(from_hex(e + 22, 8), getuid()); /* uid */ +#endif + if (uid < 0) + uid = from_hex(e + 22, 8); + assertEqualInt(from_hex(e + 22, 8), uid); /* uid */ gid = from_hex(e + 30, 8); /* gid */ assertEqualMem(e + 38, "00000003", 8); /* nlink */ t = from_hex(e + 46, 8); /* mtime */ @@ -141,60 +162,81 @@ DEFINE_TEST(test_format_newc) " first appearance should be empty, so this file size\n" " field should be zero"); assertEqualInt(0, from_hex(e + 54, 8)); /* File size */ + fs = from_hex(e + 54, 8); + fs += PAD(fs, 4); devmajor = from_hex(e + 62, 8); /* devmajor */ devminor = from_hex(e + 70, 8); /* devminor */ assert(is_hex(e + 78, 8)); /* rdevmajor */ assert(is_hex(e + 86, 8)); /* rdevminor */ assertEqualMem(e + 94, "00000006", 8); /* Name size */ + ns = from_hex(e + 94, 8); + ns += PAD(ns + 2, 4); assertEqualInt(0, from_hex(e + 102, 8)); /* check field */ assertEqualMem(e + 110, "file1\0", 6); /* Name contents */ /* Since there's another link, no file contents here. */ /* But add in file size so that an error here doesn't cascade. */ - e += 116 + from_hex(e + 54, 8) + (3 & -from_hex(e + 54, 8)); - /* "symlink" pointing to "file1" */ - assert(is_hex(e, 110)); - assertEqualMem(e + 0, "070701", 6); /* Magic */ - assert(is_hex(e + 6, 8)); /* ino */ - assertEqualInt(0xa1ff, from_hex(e + 14, 8)); /* Mode */ - assertEqualInt(from_hex(e + 22, 8), getuid()); /* uid */ - assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */ - assertEqualMem(e + 38, "00000001", 8); /* nlink */ - t2 = from_hex(e + 46, 8); /* mtime */ - failure("First entry created at t=0x%08x this entry created at t2=0x%08x", t, t2); - assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */ - assertEqualMem(e + 54, "00000005", 8); /* File size */ - assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */ - assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */ - assert(is_hex(e + 78, 8)); /* rdevmajor */ - assert(is_hex(e + 86, 8)); /* rdevminor */ - assertEqualMem(e + 94, "00000008", 8); /* Name size */ - assertEqualInt(0, from_hex(e + 102, 8)); /* check field */ - assertEqualMem(e + 110, "symlink\0\0\0", 10); /* Name contents */ - assertEqualMem(e + 120, "file1\0\0\0", 8); /* symlink target */ - e += 120 + from_hex(e + 54, 8) + (3 & -from_hex(e + 54, 8)); + e += 110 + fs + ns; + + if (canSymlink()) { + /* "symlink" pointing to "file1" */ + assert(is_hex(e, 110)); + assertEqualMem(e + 0, "070701", 6); /* Magic */ + assert(is_hex(e + 6, 8)); /* ino */ + assertEqualInt(0xa1ff, from_hex(e + 14, 8)); /* Mode */ + assertEqualInt(from_hex(e + 22, 8), uid); /* uid */ + assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */ + assertEqualMem(e + 38, "00000001", 8); /* nlink */ + t2 = from_hex(e + 46, 8); /* mtime */ + failure("First entry created at t=0x%08x this entry created at t2=0x%08x", t, t2); + assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */ + assertEqualMem(e + 54, "00000005", 8); /* File size */ + fs = from_hex(e + 54, 8); + fs += PAD(fs, 4); + assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */ + assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */ + assert(is_hex(e + 78, 8)); /* rdevmajor */ + assert(is_hex(e + 86, 8)); /* rdevminor */ + assertEqualMem(e + 94, "00000008", 8); /* Name size */ + ns = from_hex(e + 94, 8); + ns += PAD(ns + 2, 4); + assertEqualInt(0, from_hex(e + 102, 8)); /* check field */ + assertEqualMem(e + 110, "symlink\0\0\0", 10); /* Name contents */ + assertEqualMem(e + 110 + ns, "file1\0\0\0", 8); /* symlink target */ + e += 110 + fs + ns; + } /* "dir" */ assert(is_hex(e, 110)); assertEqualMem(e + 0, "070701", 6); /* Magic */ assert(is_hex(e + 6, 8)); /* ino */ - assertEqualInt(0x41fd, from_hex(e + 14, 8)); /* Mode */ - assertEqualInt(from_hex(e + 22, 8), getuid()); /* uid */ +#if defined(_WIN32) && !defined(__CYGWIN__) + /* Group members bits and others bits do not work. */ + assertEqualInt(0x41c0, from_hex(e + 14, 8) & 0xffc0); /* Mode */ +#else + /* Mode: sgid bit sometimes propagates from parent dirs, ignore it. */ + assertEqualInt(040775, from_hex(e + 14, 8) & ~02000); +#endif + assertEqualInt(from_hex(e + 22, 8), uid); /* uid */ assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */ +#ifndef NLINKS_INACCURATE_FOR_DIRS assertEqualMem(e + 38, "00000002", 8); /* nlink */ +#endif t2 = from_hex(e + 46, 8); /* mtime */ failure("First entry created at t=0x%08x this entry created at t2=0x%08x", t, t2); assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */ assertEqualMem(e + 54, "00000000", 8); /* File size */ + fs = from_hex(e + 54, 8); + fs += PAD(fs, 4); assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */ assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */ assert(is_hex(e + 78, 8)); /* rdevmajor */ assert(is_hex(e + 86, 8)); /* rdevminor */ assertEqualMem(e + 94, "00000004", 8); /* Name size */ + ns = from_hex(e + 94, 8); + ns += PAD(ns + 2, 4); assertEqualInt(0, from_hex(e + 102, 8)); /* check field */ assertEqualMem(e + 110, "dir\0\0\0", 6); /* Name contents */ - e += 116; - - /* TODO: Verify other types of entries. */ + e += 110 + fs + ns; /* Hardlink identical to "file1" */ /* Since we only wrote two of the three links to this @@ -203,23 +245,32 @@ DEFINE_TEST(test_format_newc) assertEqualMem(e + 0, "070701", 6); /* Magic */ failure("If these aren't the same, then the hardlink detection failed to match them."); assertEqualInt(ino, from_hex(e + 6, 8)); /* ino */ +#if defined(_WIN32) && !defined(__CYGWIN__) + /* Group members bits and others bits do not work. */ + assertEqualInt(0x8180, from_hex(e + 14, 8) & 0xffc0); /* Mode */ +#else assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */ - assertEqualInt(from_hex(e + 22, 8), getuid()); /* uid */ +#endif + assertEqualInt(from_hex(e + 22, 8), uid); /* uid */ assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */ assertEqualMem(e + 38, "00000003", 8); /* nlink */ t2 = from_hex(e + 46, 8); /* mtime */ failure("First entry created at t=0x%08x this entry created at t2=0x%08x", t, t2); assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */ assertEqualInt(10, from_hex(e + 54, 8)); /* File size */ + fs = from_hex(e + 54, 8); + fs += PAD(fs, 4); assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */ assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */ assert(is_hex(e + 78, 8)); /* rdevmajor */ assert(is_hex(e + 86, 8)); /* rdevminor */ assertEqualMem(e + 94, "00000009", 8); /* Name size */ + ns = from_hex(e + 94, 8); + ns += PAD(ns + 2, 4); assertEqualInt(0, from_hex(e + 102, 8)); /* check field */ assertEqualMem(e + 110, "hardlink\0\0", 10); /* Name contents */ - assertEqualMem(e + 120, "123456789\0\0\0", 12); /* File contents */ - e += 120 + from_hex(e + 54, 8) + (3 & -from_hex(e + 54, 8)); + assertEqualMem(e + 110 + ns, "1234567890\0\0", 12); /* File contents */ + e += 110 + ns + fs; /* Last entry is end-of-archive marker. */ assert(is_hex(e, 110)); @@ -240,6 +291,4 @@ DEFINE_TEST(test_format_newc) assertEqualMem(e + 110, "TRAILER!!!\0\0", 12); /* Name */ free(p); - - umask(oldmask); } diff --git a/archivers/libarchive/files/cpio/test/test_gcpio_compat.c b/archivers/libarchive/files/cpio/test/test_gcpio_compat.c index 4c69abb6368..461e427c2e8 100644 --- a/archivers/libarchive/files/cpio/test/test_gcpio_compat.c +++ b/archivers/libarchive/files/cpio/test/test_gcpio_compat.c @@ -23,19 +23,16 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "test.h" -__FBSDID("$FreeBSD$"); - +__FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_gcpio_compat.c,v 1.2 2008/08/22 02:27:06 kientzle Exp $"); static void unpack_test(const char *from, const char *options, const char *se) { - struct stat st, st2; - char buff[128]; int r; /* Create a work dir named after the file we're unpacking. */ - assertEqualInt(0, mkdir(from, 0775)); - chdir(from); + assertMakeDir(from, 0775); + assertChdir(from); /* * Use cpio to unpack the sample archive @@ -48,80 +45,64 @@ unpack_test(const char *from, const char *options, const char *se) assertEqualInt(r, 0); /* Verify that nothing went to stderr. */ - assertFileContents(se, strlen(se), "unpack.err"); + if (canSymlink()) { + failure("Error invoking %s -i %s < %s", + testprog, options, from); + assertTextFileContents(se, "unpack.err"); + } /* * Verify unpacked files. */ /* Regular file with 2 links. */ - r = lstat("file", &st); - failure("Failed to stat file %s/file, errno=%d", from, errno); - assertEqualInt(r, 0); - if (r == 0) { - assert(S_ISREG(st.st_mode)); - assertEqualInt(0644, st.st_mode & 0777); - failure("file %s/file", from); - assertEqualInt(10, st.st_size); - failure("file %s/file", from); - assertEqualInt(2, st.st_nlink); - } + assertIsReg("file", 0644); + failure("%s", from); + assertFileSize("file", 10); + assertFileSize("linkfile", 10); + failure("%s", from); + assertFileNLinks("file", 2); /* Another name for the same file. */ - r = lstat("linkfile", &st2); - failure("Failed to stat file %s/linkfile, errno=%d", from, errno); - assertEqualInt(r, 0); - if (r == 0) { - assert(S_ISREG(st2.st_mode)); - assertEqualInt(0644, st2.st_mode & 0777); - failure("file %s/file", from); - assertEqualInt(10, st2.st_size); - failure("file %s/file", from); - assertEqualInt(2, st2.st_nlink); - failure("file and linkfile should be hardlinked"); - assertEqualInt(st.st_dev, st2.st_dev); - failure("file %s/file", from); - assertEqualInt(st.st_ino, st2.st_ino); - } + failure("%s", from); + assertIsHardlink("linkfile", "file"); + assertFileSize("file", 10); + assertFileSize("linkfile", 10); /* Symlink */ - r = lstat("symlink", &st); - failure("Failed to stat file %s/symlink, errno=%d", from, errno); - assertEqualInt(r, 0); - if (r == 0) { - failure("symlink should be a symlink; actual mode is %o", - st.st_mode); - assert(S_ISLNK(st.st_mode)); - if (S_ISLNK(st.st_mode)) { - r = readlink("symlink", buff, sizeof(buff)); - assertEqualInt(r, 4); - buff[r] = '\0'; - assertEqualString(buff, "file"); - } - } + if (canSymlink()) + assertIsSymlink("symlink", "file"); /* dir */ - r = lstat("dir", &st); - if (r == 0) { - assertEqualInt(r, 0); - assert(S_ISDIR(st.st_mode)); - assertEqualInt(0775, st.st_mode & 0777); - } + assertIsDir("dir", 0775); - chdir(".."); + assertChdir(".."); } DEFINE_TEST(test_gcpio_compat) { - int oldumask; - - oldumask = umask(0); + assertUmask(0); /* Dearchive sample files with a variety of options. */ - unpack_test("test_gcpio_compat_ref.bin", "", "1 block\n"); - unpack_test("test_gcpio_compat_ref.crc", "", "2 blocks\n"); - unpack_test("test_gcpio_compat_ref.newc", "", "2 blocks\n"); - unpack_test("test_gcpio_compat_ref.ustar", "", "7 blocks\n"); - - umask(oldumask); + if (canSymlink()) { + unpack_test("test_gcpio_compat_ref.bin", + "--no-preserve-owner", "1 block\n"); + unpack_test("test_gcpio_compat_ref.crc", + "--no-preserve-owner", "2 blocks\n"); + unpack_test("test_gcpio_compat_ref.newc", + "--no-preserve-owner", "2 blocks\n"); + /* gcpio-2.9 only reads 6 blocks here */ + unpack_test("test_gcpio_compat_ref.ustar", + "--no-preserve-owner", "7 blocks\n"); + } else { + unpack_test("test_gcpio_compat_ref_nosym.bin", + "--no-preserve-owner", "1 block\n"); + unpack_test("test_gcpio_compat_ref_nosym.crc", + "--no-preserve-owner", "2 blocks\n"); + unpack_test("test_gcpio_compat_ref_nosym.newc", + "--no-preserve-owner", "2 blocks\n"); + /* gcpio-2.9 only reads 6 blocks here */ + unpack_test("test_gcpio_compat_ref_nosym.ustar", + "--no-preserve-owner", "7 blocks\n"); + } } diff --git a/archivers/libarchive/files/cpio/test/test_option_B_upper.c b/archivers/libarchive/files/cpio/test/test_option_B_upper.c new file mode 100644 index 00000000000..b040354b605 --- /dev/null +++ b/archivers/libarchive/files/cpio/test/test_option_B_upper.c @@ -0,0 +1,52 @@ +/*- + * Copyright (c) 2003-2007 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. + * 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 "test.h" +__FBSDID("$FreeBSD$"); + + +DEFINE_TEST(test_option_B_upper) +{ + struct stat st; + int r; + + /* + * Create a file on disk. + */ + assertMakeFile("file", 0644, NULL); + + /* Create an archive without -B; this should be 512 bytes. */ + r = systemf("echo file | %s -o > small.cpio 2>small.err", testprog); + assertEqualInt(r, 0); + assertTextFileContents("1 block\n", "small.err"); + assertEqualInt(0, stat("small.cpio", &st)); + assertEqualInt(512, st.st_size); + + /* Create an archive with -B; this should be 5120 bytes. */ + r = systemf("echo file | %s -oB > large.cpio 2>large.err", testprog); + assertEqualInt(r, 0); + assertTextFileContents("1 block\n", "large.err"); + assertEqualInt(0, stat("large.cpio", &st)); + assertEqualInt(5120, st.st_size); +} diff --git a/archivers/libarchive/files/cpio/test/test_option_C_upper.c b/archivers/libarchive/files/cpio/test/test_option_C_upper.c new file mode 100644 index 00000000000..c8e63fd34fb --- /dev/null +++ b/archivers/libarchive/files/cpio/test/test_option_C_upper.c @@ -0,0 +1,62 @@ +/*- + * Copyright (c) 2003-2007 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. + * 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 "test.h" +__FBSDID("$FreeBSD$"); + + +DEFINE_TEST(test_option_C_upper) +{ + int r; + + /* + * Create a file on disk. + */ + assertMakeFile("file", 0644, NULL); + + /* Create an archive without -C; this should be 512 bytes. */ + r = systemf("echo file | %s -o > small.cpio 2>small.err", testprog); + assertEqualInt(r, 0); + assertTextFileContents("1 block\n", "small.err"); + assertFileSize("small.cpio", 512); + + /* Create an archive with -C 513; this should be 513 bytes. */ + r = systemf("echo file | %s -o -C 513 > 513.cpio 2>513.err", + testprog); + assertEqualInt(r, 0); + assertTextFileContents("1 block\n", "513.err"); + assertFileSize("513.cpio", 513); + + /* Create an archive with -C 12345; this should be 12345 bytes. */ + r = systemf("echo file | %s -o -C12345 > 12345.cpio 2>12345.err", + testprog); + assertEqualInt(r, 0); + assertTextFileContents("1 block\n", "12345.err"); + assertFileSize("12345.cpio", 12345); + + /* Create an archive with invalid -C request */ + assert(0 != systemf("echo file | %s -o -C > bad.cpio 2>bad.err", + testprog)); + assertEmptyFile("bad.cpio"); +} diff --git a/archivers/libarchive/files/cpio/test/test_option_J_upper.c b/archivers/libarchive/files/cpio/test/test_option_J_upper.c new file mode 100644 index 00000000000..532aacf39cc --- /dev/null +++ b/archivers/libarchive/files/cpio/test/test_option_J_upper.c @@ -0,0 +1,56 @@ +/*- + * Copyright (c) 2003-2009 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. + * 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 "test.h" +__FBSDID("$FreeBSD$"); + +DEFINE_TEST(test_option_J_upper) +{ + char *p; + int r; + size_t s; + + /* Create a file. */ + assertMakeFile("f", 0644, "a"); + + /* Archive it with xz compression. */ + r = systemf("echo f | %s -o -J >archive.out 2>archive.err", + testprog); + p = slurpfile(&s, "archive.err"); + p[s] = '\0'; + if (r != 0) { + if (strstr(p, "compression not available") != NULL) { + skipping("This version of bsdcpio was compiled " + "without xz support"); + return; + } + failure("-J option is broken"); + assertEqualInt(r, 0); + return; + } + /* Check that the archive file has an xz signature. */ + p = slurpfile(&s, "archive.out"); + assert(s > 2); + assertEqualMem(p, "\3757zXZ", 5); +} diff --git a/archivers/libarchive/files/cpio/test/test_option_L_upper.c b/archivers/libarchive/files/cpio/test/test_option_L_upper.c new file mode 100644 index 00000000000..1774343f673 --- /dev/null +++ b/archivers/libarchive/files/cpio/test/test_option_L_upper.c @@ -0,0 +1,101 @@ +/*- + * Copyright (c) 2003-2007 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. + * 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 "test.h" +__FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_option_L.c,v 1.2 2008/08/24 06:21:00 kientzle Exp $"); + +/* This is a little pointless, as Windows doesn't support symlinks + * (except for the seriously crippled CreateSymbolicLink API) so these + * tests won't run on Windows. */ +#if defined(_WIN32) && !defined(__CYGWIN__) +#define CAT "type" +#else +#define CAT "cat" +#endif + +DEFINE_TEST(test_option_L_upper) +{ + FILE *filelist; + int r; + + if (!canSymlink()) { + skipping("Symlink tests"); + return; + } + + filelist = fopen("filelist", "w"); + + /* Create a file and a symlink to the file. */ + assertMakeFile("file", 0644, "1234567890"); + fprintf(filelist, "file\n"); + + /* Symlink to above file. */ + assertMakeSymlink("symlink", "file"); + fprintf(filelist, "symlink\n"); + + fclose(filelist); + + r = systemf(CAT " filelist | %s -pd copy >copy.out 2>copy.err", testprog); + assertEqualInt(r, 0); + assertTextFileContents("1 block\n", "copy.err"); + + failure("Regular -p without -L should preserve symlinks."); + assertIsSymlink("copy/symlink", NULL); + + r = systemf(CAT " filelist | %s -pd -L copy-L >copy-L.out 2>copy-L.err", testprog); + assertEqualInt(r, 0); + assertEmptyFile("copy-L.out"); + assertTextFileContents("1 block\n", "copy-L.err"); + failure("-pdL should dereference symlinks and turn them into files."); + assertIsReg("copy-L/symlink", -1); + + r = systemf(CAT " filelist | %s -o >archive.out 2>archive.err", testprog); + failure("Error invoking %s -o ", testprog); + assertEqualInt(r, 0); + assertTextFileContents("1 block\n", "archive.err"); + + assertMakeDir("unpack", 0755); + assertChdir("unpack"); + r = systemf(CAT " ../archive.out | %s -i >unpack.out 2>unpack.err", testprog); + failure("Error invoking %s -i", testprog); + assertEqualInt(r, 0); + assertTextFileContents("1 block\n", "unpack.err"); + assertChdir(".."); + + assertIsSymlink("unpack/symlink", NULL); + + r = systemf(CAT " filelist | %s -oL >archive-L.out 2>archive-L.err", testprog); + failure("Error invoking %s -oL", testprog); + assertEqualInt(r, 0); + assertTextFileContents("1 block\n", "archive-L.err"); + + assertMakeDir("unpack-L", 0755); + assertChdir("unpack-L"); + r = systemf(CAT " ../archive-L.out | %s -i >unpack-L.out 2>unpack-L.err", testprog); + failure("Error invoking %s -i < archive-L.out", testprog); + assertEqualInt(r, 0); + assertTextFileContents("1 block\n", "unpack-L.err"); + assertChdir(".."); + assertIsReg("unpack-L/symlink", -1); +} diff --git a/archivers/libarchive/files/cpio/test/test_option_Z_upper.c b/archivers/libarchive/files/cpio/test/test_option_Z_upper.c new file mode 100644 index 00000000000..936ce0c2d3e --- /dev/null +++ b/archivers/libarchive/files/cpio/test/test_option_Z_upper.c @@ -0,0 +1,56 @@ +/*- + * Copyright (c) 2003-2009 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. + * 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 "test.h" +__FBSDID("$FreeBSD$"); + +DEFINE_TEST(test_option_Z_upper) +{ + char *p; + int r; + size_t s; + + /* Create a file. */ + assertMakeFile("f", 0644, "a"); + + /* Archive it with compress compression. */ + r = systemf("echo f | %s -oZ >archive.out 2>archive.err", + testprog); + p = slurpfile(&s, "archive.err"); + p[s] = '\0'; + if (r != 0) { + if (strstr(p, "compression not available") != NULL) { + skipping("This version of bsdcpio was compiled " + "without compress support"); + return; + } + failure("-Z option is broken"); + assertEqualInt(r, 0); + return; + } + /* Check that the archive file has a compress signature. */ + p = slurpfile(&s, "archive.out"); + assert(s > 2); + assertEqualMem(p, "\x1f\x9d", 2); +} diff --git a/archivers/libarchive/files/cpio/test/test_option_a.c b/archivers/libarchive/files/cpio/test/test_option_a.c index c063280f7d7..606de606e2c 100644 --- a/archivers/libarchive/files/cpio/test/test_option_a.c +++ b/archivers/libarchive/files/cpio/test/test_option_a.c @@ -23,8 +23,12 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "test.h" +#if defined(HAVE_UTIME_H) #include <utime.h> -__FBSDID("$FreeBSD$"); +#elif defined(HAVE_SYS_UTIME_H) +#include <sys/utime.h> +#endif +__FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_option_a.c,v 1.3 2008/08/24 06:21:00 kientzle Exp $"); static struct { const char *name; @@ -53,19 +57,15 @@ test_create(void) struct utimbuf times; static const int numfiles = sizeof(files) / sizeof(files[0]); int i; - int fd; for (i = 0; i < numfiles; ++i) { - fd = open(files[i].name, O_CREAT | O_WRONLY, 0644); - assert(fd >= 0); /* * Note: Have to write at least one byte to the file. * cpio doesn't bother reading the file if it's zero length, * so the atime never gets changed in that case, which * makes the tests below rather pointless. */ - assertEqualInt(1, write(fd, "a", 1)); - close(fd); + assertMakeFile(files[i].name, 0644, "a"); /* If utime() isn't supported on your platform, just * #ifdef this section out. Most of the test below is @@ -83,26 +83,21 @@ test_create(void) } /* Wait until the atime on the last file is actually in the past. */ - /* If utime() is supported above, there's no sleep here which - * makes the test faster. */ - while (files[numfiles - 1].atime_sec >= time(NULL)) - sleep(1); + sleepUntilAfter(files[numfiles - 1].atime_sec); } DEFINE_TEST(test_option_a) { struct stat st; int r; - int f; - char buff[64]; + char *p; /* Create all of the test files. */ test_create(); /* Sanity check; verify that atimes really do get modified. */ - f = open(files[0].name, O_RDONLY); - assertEqualInt(1, read(f,buff, 1)); - assertEqualInt(0, close(f)); + assert((p = slurpfile(NULL, "f0")) != NULL); + free(p); assertEqualInt(0, stat("f0", &st)); if (st.st_atime == files[0].atime_sec) { skipping("Cannot verify -a option\n" @@ -118,7 +113,7 @@ DEFINE_TEST(test_option_a) /* Copy the file without -a; should change the atime. */ r = systemf("echo %s | %s -pd copy-no-a > copy-no-a.out 2>copy-no-a.err", files[1].name, testprog); assertEqualInt(r, 0); - assertEmptyFile("copy-no-a.err"); + assertTextFileContents("1 block\n", "copy-no-a.err"); assertEmptyFile("copy-no-a.out"); assertEqualInt(0, stat(files[1].name, &st)); failure("Copying file without -a should have changed atime."); @@ -127,7 +122,7 @@ DEFINE_TEST(test_option_a) /* Archive the file without -a; should change the atime. */ r = systemf("echo %s | %s -o > archive-no-a.out 2>archive-no-a.err", files[2].name, testprog); assertEqualInt(r, 0); - assertEmptyFile("copy-no-a.err"); + assertTextFileContents("1 block\n", "copy-no-a.err"); assertEqualInt(0, stat(files[2].name, &st)); failure("Archiving file without -a should have changed atime."); assert(st.st_atime != files[2].atime_sec); @@ -142,7 +137,7 @@ DEFINE_TEST(test_option_a) r = systemf("echo %s | %s -pad copy-a > copy-a.out 2>copy-a.err", files[3].name, testprog); assertEqualInt(r, 0); - assertEmptyFile("copy-a.err"); + assertTextFileContents("1 block\n", "copy-a.err"); assertEmptyFile("copy-a.out"); assertEqualInt(0, stat(files[3].name, &st)); failure("Copying file with -a should not have changed atime."); @@ -152,7 +147,7 @@ DEFINE_TEST(test_option_a) r = systemf("echo %s | %s -oa > archive-a.out 2>archive-a.err", files[4].name, testprog); assertEqualInt(r, 0); - assertEmptyFile("copy-a.err"); + assertTextFileContents("1 block\n", "copy-a.err"); assertEqualInt(0, stat(files[4].name, &st)); failure("Archiving file with -a should not have changed atime."); assertEqualInt(st.st_atime, files[4].atime_sec); diff --git a/archivers/libarchive/files/cpio/test/test_option_c.c b/archivers/libarchive/files/cpio/test/test_option_c.c index 05b48c29cc3..241bcf67bb1 100644 --- a/archivers/libarchive/files/cpio/test/test_option_c.c +++ b/archivers/libarchive/files/cpio/test/test_option_c.c @@ -53,44 +53,47 @@ from_octal(const char *p, size_t l) DEFINE_TEST(test_option_c) { - int fd, filelist; + FILE *filelist; int r; + int uid = -1; int dev, ino, gid; time_t t, now; char *p, *e; size_t s; - mode_t oldmask; - oldmask = umask(0); + assertUmask(0); + +#if !defined(_WIN32) + uid = getuid(); +#endif /* * Create an assortment of files. * TODO: Extend this to cover more filetypes. */ - filelist = open("filelist", O_CREAT | O_WRONLY, 0644); + filelist = fopen("filelist", "w"); /* "file" */ - fd = open("file", O_CREAT | O_WRONLY, 0644); - assert(fd >= 0); - assertEqualInt(10, write(fd, "123456789", 10)); - close(fd); - assertEqualInt(5, write(filelist, "file\n", 5)); + assertMakeFile("file", 0644, "1234567890"); + fprintf(filelist, "file\n"); /* "symlink" */ - assertEqualInt(0, symlink("file", "symlink")); - assertEqualInt(8, write(filelist, "symlink\n", 8)); + if (canSymlink()) { + assertMakeSymlink("symlink", "file"); + fprintf(filelist, "symlink\n"); + } /* "dir" */ - assertEqualInt(0, mkdir("dir", 0775)); + assertMakeDir("dir", 0775); /* Record some facts about what we just created: */ now = time(NULL); /* They were all created w/in last two seconds. */ - assertEqualInt(4, write(filelist, "dir\n", 4)); + fprintf(filelist, "dir\n"); /* Use the cpio program to create an archive. */ - close(filelist); + fclose(filelist); r = systemf("%s -oc <filelist >basic.out 2>basic.err", testprog); /* Verify that nothing went to stderr. */ - assertFileContents("1 block\n", 8, "basic.err"); + assertTextFileContents("1 block\n", "basic.err"); /* Assert that the program finished. */ failure("%s -oc crashed", testprog); @@ -114,8 +117,15 @@ DEFINE_TEST(test_option_c) dev = from_octal(e + 6, 6); assert(is_octal(e + 12, 6)); /* ino */ ino = from_octal(e + 12, 6); +#if defined(_WIN32) && !defined(__CYGWIN__) + /* Group members bits and others bits do not work. */ + assertEqualMem(e + 18, "100666", 6); /* Mode */ +#else assertEqualMem(e + 18, "100644", 6); /* Mode */ - assertEqualInt(from_octal(e + 24, 6), getuid()); /* uid */ +#endif + if (uid < 0) + uid = from_octal(e + 24, 6); + assertEqualInt(from_octal(e + 24, 6), uid); /* uid */ assert(is_octal(e + 30, 6)); /* gid */ gid = from_octal(e + 30, 6); assertEqualMem(e + 36, "000001", 6); /* nlink */ @@ -128,31 +138,37 @@ DEFINE_TEST(test_option_c) assertEqualMem(e + 59, "000005", 6); /* Name size */ assertEqualMem(e + 65, "00000000012", 11); /* File size */ assertEqualMem(e + 76, "file\0", 5); /* Name contents */ - assertEqualMem(e + 81, "123456789\0", 10); /* File contents */ + assertEqualMem(e + 81, "1234567890", 10); /* File contents */ e += 91; - /* Second entry is "symlink" pointing to "file" */ - assert(is_octal(e, 76)); /* Entire header is octal digits. */ - assertEqualMem(e + 0, "070707", 6); /* Magic */ - assertEqualInt(dev, from_octal(e + 6, 6)); /* dev */ - assert(dev != from_octal(e + 12, 6)); /* ino */ - assertEqualMem(e + 18, "120777", 6); /* Mode */ - assertEqualInt(from_octal(e + 24, 6), getuid()); /* uid */ - assertEqualInt(gid, from_octal(e + 30, 6)); /* gid */ - assertEqualMem(e + 36, "000001", 6); /* nlink */ - failure("file entries should have rdev == 0 (dev was 0%o)", - from_octal(e + 6, 6)); - assertEqualMem(e + 42, "000000", 6); /* rdev */ - t = from_octal(e + 48, 11); /* mtime */ - assert(t <= now); /* File wasn't created in future. */ - assert(t >= now - 2); /* File was created w/in last 2 secs. */ - assertEqualMem(e + 59, "000010", 6); /* Name size */ - assertEqualMem(e + 65, "00000000004", 11); /* File size */ - assertEqualMem(e + 76, "symlink\0", 8); /* Name contents */ - assertEqualMem(e + 84, "file", 4); /* Symlink target. */ - e += 88; + /* "symlink" pointing to "file" */ + if (canSymlink()) { + assert(is_octal(e, 76)); /* Entire header is octal digits. */ + assertEqualMem(e + 0, "070707", 6); /* Magic */ + assertEqualInt(dev, from_octal(e + 6, 6)); /* dev */ + assert(ino != from_octal(e + 12, 6)); /* ino */ +#if !defined(_WIN32) || defined(__CYGWIN__) + /* On Windows, symbolic link and group members bits and + * others bits do not work. */ + assertEqualMem(e + 18, "120777", 6); /* Mode */ +#endif + assertEqualInt(from_octal(e + 24, 6), uid); /* uid */ + assertEqualInt(gid, from_octal(e + 30, 6)); /* gid */ + assertEqualMem(e + 36, "000001", 6); /* nlink */ + failure("file entries should have rdev == 0 (dev was 0%o)", + from_octal(e + 6, 6)); + assertEqualMem(e + 42, "000000", 6); /* rdev */ + t = from_octal(e + 48, 11); /* mtime */ + assert(t <= now); /* File wasn't created in future. */ + assert(t >= now - 2); /* File was created w/in last 2 secs. */ + assertEqualMem(e + 59, "000010", 6); /* Name size */ + assertEqualMem(e + 65, "00000000004", 11); /* File size */ + assertEqualMem(e + 76, "symlink\0", 8); /* Name contents */ + assertEqualMem(e + 84, "file", 4); /* Symlink target. */ + e += 88; + } - /* Second entry is "dir" */ + /* "dir" */ assert(is_octal(e, 76)); assertEqualMem(e + 0, "070707", 6); /* Magic */ /* Dev should be same as first entry. */ @@ -161,12 +177,21 @@ DEFINE_TEST(test_option_c) /* Ino must be different from first entry. */ assert(is_octal(e + 12, 6)); /* ino */ assert(dev != from_octal(e + 12, 6)); - assertEqualMem(e + 18, "040775", 6); /* Mode */ - assertEqualInt(from_octal(e + 24, 6), getuid()); /* uid */ +#if defined(_WIN32) && !defined(__CYGWIN__) + /* Group members bits and others bits do not work. */ + assertEqualMem(e + 18, "040777", 6); /* Mode */ +#else + /* Accept 042775 to accomodate systems where sgid bit propagates. */ + if (memcmp(e + 18, "042775", 6) != 0) + assertEqualMem(e + 18, "040775", 6); /* Mode */ +#endif + assertEqualInt(from_octal(e + 24, 6), uid); /* uid */ /* Gid should be same as first entry. */ assert(is_octal(e + 30, 6)); /* gid */ assertEqualInt(gid, from_octal(e + 30, 6)); +#ifndef NLINKS_INACCURATE_FOR_DIRS assertEqualMem(e + 36, "000002", 6); /* Nlink */ +#endif t = from_octal(e + 48, 11); /* mtime */ assert(t <= now); /* File wasn't created in future. */ assert(t >= now - 2); /* File was created w/in last 2 secs. */ @@ -193,6 +218,4 @@ DEFINE_TEST(test_option_c) assertEqualMem(e + 76, "TRAILER!!!\0", 11); /* Name */ free(p); - - umask(oldmask); } diff --git a/archivers/libarchive/files/cpio/test/test_option_d.c b/archivers/libarchive/files/cpio/test/test_option_d.c index 370c82ae2d0..9ff14539698 100644 --- a/archivers/libarchive/files/cpio/test/test_option_d.c +++ b/archivers/libarchive/files/cpio/test/test_option_d.c @@ -28,41 +28,37 @@ __FBSDID("$FreeBSD$"); DEFINE_TEST(test_option_d) { - struct stat st; - int r, fd; + int r; /* * Create a file in a directory. */ - assertEqualInt(0, mkdir("dir", 0755)); - fd = open("dir/file", O_CREAT | O_WRONLY, 0644); - assert(fd >= 0); - close(fd); + assertMakeDir("dir", 0755); + assertMakeFile("dir/file", 0644, NULL); /* Create an archive. */ r = systemf("echo dir/file | %s -o > archive.cpio 2>archive.err", testprog); assertEqualInt(r, 0); - assertFileContents("1 block\n", 8, "archive.err"); - assertEqualInt(0, stat("archive.cpio", &st)); - assertEqualInt(512, st.st_size); + assertTextFileContents("1 block\n", "archive.err"); + assertFileSize("archive.cpio", 512); /* Dearchive without -d, this should fail. */ - assertEqualInt(0, mkdir("without-d", 0755)); - assertEqualInt(0, chdir("without-d")); + assertMakeDir("without-d", 0755); + assertChdir("without-d"); r = systemf("%s -i < ../archive.cpio >out 2>err", testprog); assertEqualInt(r, 0); assertEmptyFile("out"); /* And the file should not be restored. */ - assert(0 != stat("dir/file", &st)); + assertFileNotExists("dir/file"); /* Dearchive with -d, this should succeed. */ - assertEqualInt(0, chdir("..")); - assertEqualInt(0, mkdir("with-d", 0755)); - assertEqualInt(0, chdir("with-d")); + assertChdir(".."); + assertMakeDir("with-d", 0755); + assertChdir("with-d"); r = systemf("%s -id < ../archive.cpio >out 2>err", testprog); assertEqualInt(r, 0); assertEmptyFile("out"); - assertFileContents("1 block\n", 8, "err"); + assertTextFileContents("1 block\n", "err"); /* And the file should be restored. */ - assertEqualInt(0, stat("dir/file", &st)); + assertFileExists("dir/file"); } diff --git a/archivers/libarchive/files/cpio/test/test_option_f.c b/archivers/libarchive/files/cpio/test/test_option_f.c index d1af91290d4..784e0858431 100644 --- a/archivers/libarchive/files/cpio/test/test_option_f.c +++ b/archivers/libarchive/files/cpio/test/test_option_f.c @@ -33,34 +33,44 @@ unpack(const char *dirname, const char *option) { int r; - assertEqualInt(0, mkdir(dirname, 0755)); - assertEqualInt(0, chdir(dirname)); + assertMakeDir(dirname, 0755); + assertChdir(dirname); extract_reference_file("test_option_f.cpio"); r = systemf("%s -i %s < test_option_f.cpio > copy-no-a.out 2>copy-no-a.err", testprog, option); assertEqualInt(0, r); - assertEqualInt(0, chdir("..")); + assertChdir(".."); } DEFINE_TEST(test_option_f) { /* Calibrate: No -f option, so everything should be extracted. */ - unpack("t0", ""); - assertEqualInt(0, access("t0/a123", F_OK)); - assertEqualInt(0, access("t0/a234", F_OK)); - assertEqualInt(0, access("t0/b123", F_OK)); - assertEqualInt(0, access("t0/b234", F_OK)); + unpack("t0", "--no-preserve-owner"); + assertFileExists("t0/a123"); + assertFileExists("t0/a234"); + assertFileExists("t0/b123"); + assertFileExists("t0/b234"); /* Don't extract 'a*' files. */ - unpack("t1", "-f 'a*'"); - assert(0 != access("t1/a123", F_OK)); - assert(0 != access("t1/a234", F_OK)); - assertEqualInt(0, access("t1/b123", F_OK)); - assertEqualInt(0, access("t1/b234", F_OK)); +#if defined(_WIN32) && !defined(__CYGWIN__) + /* Single quotes isn't used by command.exe. */ + unpack("t1", "--no-preserve-owner -f a*"); +#else + unpack("t1", "--no-preserve-owner -f 'a*'"); +#endif + assertFileNotExists("t1/a123"); + assertFileNotExists("t1/a234"); + assertFileExists("t1/b123"); + assertFileExists("t1/b234"); /* Don't extract 'b*' files. */ - unpack("t2", "-f 'b*'"); - assertEqualInt(0, access("t2/a123", F_OK)); - assertEqualInt(0, access("t2/a234", F_OK)); - assert(0 != access("t2/b123", F_OK)); - assert(0 != access("t2/b234", F_OK)); +#if defined(_WIN32) && !defined(__CYGWIN__) + /* Single quotes isn't used by command.exe. */ + unpack("t2", "--no-preserve-owner -f b*"); +#else + unpack("t2", "--no-preserve-owner -f 'b*'"); +#endif + assertFileExists("t2/a123"); + assertFileExists("t2/a234"); + assertFileNotExists("t2/b123"); + assertFileNotExists("t2/b234"); } diff --git a/archivers/libarchive/files/cpio/test/test_option_help.c b/archivers/libarchive/files/cpio/test/test_option_help.c index b9433a43a68..56234306f53 100644 --- a/archivers/libarchive/files/cpio/test/test_option_help.c +++ b/archivers/libarchive/files/cpio/test/test_option_help.c @@ -51,6 +51,7 @@ DEFINE_TEST(test_option_help) /* Exercise --help option. */ r = systemf("%s --help >help.stdout 2>help.stderr", testprog); + assertEqualInt(r, 0); failure("--help should generate nothing to stderr."); assertEmptyFile("help.stderr"); /* Help message should start with name of program. */ @@ -67,6 +68,7 @@ DEFINE_TEST(test_option_help) /* -h option should generate the same output. */ r = systemf("%s -h >h.stdout 2>h.stderr", testprog); + assertEqualInt(r, 0); failure("-h should generate nothing to stderr."); assertEmptyFile("h.stderr"); failure("stdout should be same for -h and --help"); @@ -74,6 +76,7 @@ DEFINE_TEST(test_option_help) /* -W help should be another synonym. */ r = systemf("%s -W help >Whelp.stdout 2>Whelp.stderr", testprog); + assertEqualInt(r, 0); failure("-W help should generate nothing to stderr."); assertEmptyFile("Whelp.stderr"); failure("stdout should be same for -W help and --help"); diff --git a/archivers/libarchive/files/cpio/test/test_option_l.c b/archivers/libarchive/files/cpio/test/test_option_l.c new file mode 100644 index 00000000000..5c76e688e6e --- /dev/null +++ b/archivers/libarchive/files/cpio/test/test_option_l.c @@ -0,0 +1,50 @@ +/*- + * Copyright (c) 2003-2007 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. + * 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 "test.h" +__FBSDID("$FreeBSD$"); + +DEFINE_TEST(test_option_l) +{ + int r; + + /* Create a file. */ + assertMakeFile("f", 0644, "a"); + + /* Copy the file to the "copy" dir. */ + r = systemf("echo f | %s -pd copy >copy.out 2>copy.err", + testprog); + assertEqualInt(r, 0); + + /* Check that the copy is a true copy and not a link. */ + assertIsNotHardlink("f", "copy/f"); + + /* Copy the file to the "link" dir with the -l option. */ + r = systemf("echo f | %s -pld link >link.out 2>link.err", + testprog); + assertEqualInt(r, 0); + + /* Check that this is a link and not a copy. */ + assertIsHardlink("f", "link/f"); +} diff --git a/archivers/libarchive/files/cpio/test/test_option_lzma.c b/archivers/libarchive/files/cpio/test/test_option_lzma.c new file mode 100644 index 00000000000..c6e33530150 --- /dev/null +++ b/archivers/libarchive/files/cpio/test/test_option_lzma.c @@ -0,0 +1,56 @@ +/*- + * Copyright (c) 2003-2007 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. + * 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 "test.h" +__FBSDID("$FreeBSD$"); + +DEFINE_TEST(test_option_lzma) +{ + char *p; + int r; + size_t s; + + /* Create a file. */ + assertMakeFile("f", 0644, "a"); + + /* Archive it with lzma compression. */ + r = systemf("echo f | %s -o --lzma >archive.out 2>archive.err", + testprog); + p = slurpfile(&s, "archive.err"); + p[s] = '\0'; + if (r != 0) { + if (strstr(p, "compression not available") != NULL) { + skipping("This version of bsdcpio was compiled " + "without lzma support"); + return; + } + failure("--lzma option is broken"); + assertEqualInt(r, 0); + return; + } + /* Check that the archive file has an lzma signature. */ + p = slurpfile(&s, "archive.out"); + assert(s > 2); + assertEqualMem(p, "\x5d\00\00", 3); +} diff --git a/archivers/libarchive/files/cpio/test/test_option_m.c b/archivers/libarchive/files/cpio/test/test_option_m.c index d5bbad2e902..de880b2883e 100644 --- a/archivers/libarchive/files/cpio/test/test_option_m.c +++ b/archivers/libarchive/files/cpio/test/test_option_m.c @@ -28,9 +28,7 @@ __FBSDID("$FreeBSD$"); DEFINE_TEST(test_option_m) { - struct stat st; int r; - time_t now; /* * The reference archive has one file with an mtime in 1970, 1 @@ -38,33 +36,28 @@ DEFINE_TEST(test_option_m) */ /* Restored without -m, the result should have a current mtime. */ - assertEqualInt(0, mkdir("without-m", 0755)); - assertEqualInt(0, chdir("without-m")); + assertMakeDir("without-m", 0755); + assertChdir("without-m"); extract_reference_file("test_option_m.cpio"); - r = systemf("%s -i < test_option_m.cpio >out 2>err", testprog); - now = time(NULL); + r = systemf("%s --no-preserve-owner -i < test_option_m.cpio >out 2>err", testprog); assertEqualInt(r, 0); assertEmptyFile("out"); - assertFileContents("1 block\n", 8, "err"); - assertEqualInt(0, stat("file", &st)); + assertTextFileContents("1 block\n", "err"); /* Should have been created within the last few seconds. */ - assert(st.st_mtime <= now); - assert(st.st_mtime > now - 5); + assertFileMtimeRecent("file"); /* With -m, it should have an mtime in 1970. */ - assertEqualInt(0, chdir("..")); - assertEqualInt(0, mkdir("with-m", 0755)); - assertEqualInt(0, chdir("with-m")); + assertChdir(".."); + assertMakeDir("with-m", 0755); + assertChdir("with-m"); extract_reference_file("test_option_m.cpio"); - r = systemf("%s -im < test_option_m.cpio >out 2>err", testprog); - now = time(NULL); + r = systemf("%s --no-preserve-owner -im < test_option_m.cpio >out 2>err", testprog); assertEqualInt(r, 0); assertEmptyFile("out"); - assertFileContents("1 block\n", 8, "err"); - assertEqualInt(0, stat("file", &st)); + assertTextFileContents("1 block\n", "err"); /* * mtime in reference archive is '1' == 1 second after * midnight Jan 1, 1970 UTC. */ - assertEqualInt(1, st.st_mtime); + assertFileMtime("file", 1, 0); } diff --git a/archivers/libarchive/files/cpio/test/test_option_t.c b/archivers/libarchive/files/cpio/test/test_option_t.c index c9cdd0bc8a2..4427bb3a6cd 100644 --- a/archivers/libarchive/files/cpio/test/test_option_t.c +++ b/archivers/libarchive/files/cpio/test/test_option_t.c @@ -28,20 +28,68 @@ __FBSDID("$FreeBSD$"); DEFINE_TEST(test_option_t) { + char *p; int r; /* List reference archive, make sure the TOC is correct. */ extract_reference_file("test_option_t.cpio"); - r = systemf("%s -it < test_option_t.cpio >t.out 2>t.err", testprog); + r = systemf("%s -it < test_option_t.cpio >it.out 2>it.err", testprog); assertEqualInt(r, 0); - assertFileContents("1 block\n", 8, "t.err"); + assertTextFileContents("1 block\n", "it.err"); extract_reference_file("test_option_t.stdout"); - assertEqualFile("t.out", "test_option_t.stdout"); + p = slurpfile(NULL, "test_option_t.stdout"); + assertTextFileContents(p, "it.out"); + free(p); + + /* We accept plain "-t" as a synonym for "-it" */ + r = systemf("%s -t < test_option_t.cpio >t.out 2>t.err", testprog); + assertEqualInt(r, 0); + assertTextFileContents("1 block\n", "t.err"); + extract_reference_file("test_option_t.stdout"); + p = slurpfile(NULL, "test_option_t.stdout"); + assertTextFileContents(p, "t.out"); + free(p); + + /* But "-ot" is an error. */ + assert(0 != systemf("%s -ot < test_option_t.cpio >ot.out 2>ot.err", + testprog)); + assertEmptyFile("ot.out"); /* List reference archive verbosely, make sure the TOC is correct. */ r = systemf("%s -itv < test_option_t.cpio >tv.out 2>tv.err", testprog); assertEqualInt(r, 0); - assertFileContents("1 block\n", 8, "tv.err"); + assertTextFileContents("1 block\n", "tv.err"); extract_reference_file("test_option_tv.stdout"); - assertEqualFile("tv.out", "test_option_tv.stdout"); + + /* This doesn't work because the usernames on different systems + * are different and cpio now looks up numeric UIDs on + * the local system. */ + /* assertEqualFile("tv.out", "test_option_tv.stdout"); */ + + /* List reference archive with numeric IDs, verify TOC is correct. */ + r = systemf("%s -itnv < test_option_t.cpio >itnv.out 2>itnv.err", + testprog); + assertEqualInt(r, 0); + assertTextFileContents("1 block\n", "itnv.err"); + p = slurpfile(NULL, "itnv.out"); + /* Since -n uses numeric UID/GID, this part should be the + * same on every system. */ + assertEqualMem(p, "-rw-r--r-- 1 1000 1000 0 ",42); + /* Date varies depending on local timezone. */ + if (memcmp(p + 42, "Dec 31 1969", 12) == 0) { + /* East of Greenwich we get Dec 31, 1969. */ + } else { + /* West of Greenwich get Jan 1, 1970 */ + assertEqualMem(p + 42, "Jan ", 4); + /* Some systems format "Jan 01", some "Jan 1" */ + assert(p[46] == ' ' || p[46] == '0'); + assertEqualMem(p + 47, "1 1970 ", 8); + } + assertEqualMem(p + 54, " file", 5); + free(p); + + /* But "-n" without "-t" is an error. */ + assert(0 != systemf("%s -in < test_option_t.cpio >in.out 2>in.err", + testprog)); + assertEmptyFile("in.out"); } diff --git a/archivers/libarchive/files/cpio/test/test_option_tv.stdout.uu b/archivers/libarchive/files/cpio/test/test_option_tv.stdout.uu index baead3ee324..e28888ac474 100644 --- a/archivers/libarchive/files/cpio/test/test_option_tv.stdout.uu +++ b/archivers/libarchive/files/cpio/test/test_option_tv.stdout.uu @@ -1,5 +1,6 @@ -$FreeBSD$ +$FreeBSD: src/usr.bin/cpio/test/test_option_tv.stdout.uu,v 1.2 2008/11/29 20:22:02 kientzle Exp $ begin 644 test_option_tv.stdout -G+7)W+7(M+7(M+2`@(#$@("`H;G5L;"D@("AN=6QL*2`P(&9I;&4* +M+7)W+7(M+7(M+2`@(#$@=&EM("`@("`@=&EM("`@("`@("`@("`@(#`@1&5C +/(#,Q("`Q.38Y(&9I;&4* ` end diff --git a/archivers/libarchive/files/cpio/test/test_option_u.c b/archivers/libarchive/files/cpio/test/test_option_u.c index 7abd7f0f0ef..08058aa4539 100644 --- a/archivers/libarchive/files/cpio/test/test_option_u.c +++ b/archivers/libarchive/files/cpio/test/test_option_u.c @@ -23,7 +23,11 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "test.h" +#if defined(HAVE_UTIME_H) #include <utime.h> +#elif defined(HAVE_SYS_UTIME_H) +#include <sys/utime.h> +#endif __FBSDID("$FreeBSD$"); DEFINE_TEST(test_option_u) @@ -31,14 +35,10 @@ DEFINE_TEST(test_option_u) struct utimbuf times; char *p; size_t s; - int fd; int r; /* Create a file. */ - fd = open("f", O_CREAT | O_WRONLY, 0644); - assert(fd >= 0); - assertEqualInt(1, write(fd, "a", 1)); - close(fd); + assertMakeFile("f", 0644, "a"); /* Copy the file to the "copy" dir. */ r = systemf("echo f | %s -pd copy >copy.out 2>copy.err", @@ -51,10 +51,7 @@ DEFINE_TEST(test_option_u) assertEqualMem(p, "a", 1); /* Recreate the file with a single "b" */ - fd = open("f", O_CREAT | O_TRUNC | O_WRONLY, 0644); - assert(fd >= 0); - assertEqualInt(1, write(fd, "b", 1)); - close(fd); + assertMakeFile("f", 0644, "b"); /* Set the mtime to the distant past. */ memset(×, 0, sizeof(times)); diff --git a/archivers/libarchive/files/cpio/test/test_option_version.c b/archivers/libarchive/files/cpio/test/test_option_version.c index 95258eb7e82..7345da1e515 100644 --- a/archivers/libarchive/files/cpio/test/test_option_version.c +++ b/archivers/libarchive/files/cpio/test/test_option_version.c @@ -74,10 +74,14 @@ verify(const char *p, size_t s) /* Skip a single trailing a,b,c, or d. */ if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd') ++q; - /* All terminated by a newline. */ + /* All terminated by end-of-line: \r, \r\n, or \n */ assert(s >= 1); failure("Version: %s", p); - assertEqualMem(q, "\n", 1); + if (*q == '\x0d') { + if (q[1] != '\0') + assertEqualMem(q, "\x0d\x0a", 2); + } else + assertEqualMem(q, "\x0a", 1); } diff --git a/archivers/libarchive/files/cpio/test/test_option_y.c b/archivers/libarchive/files/cpio/test/test_option_y.c index 56852a805f4..58734966ce6 100644 --- a/archivers/libarchive/files/cpio/test/test_option_y.c +++ b/archivers/libarchive/files/cpio/test/test_option_y.c @@ -23,30 +23,35 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "test.h" -__FBSDID("$FreeBSD$"); +__FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_option_y.c,v 1.2 2008/08/24 06:21:00 kientzle Exp $"); DEFINE_TEST(test_option_y) { char *p; - int fd; int r; size_t s; /* Create a file. */ - fd = open("f", O_CREAT | O_WRONLY, 0644); - assert(fd >= 0); - assertEqualInt(1, write(fd, "a", 1)); - close(fd); + assertMakeFile("f", 0644, "a"); /* Archive it with bzip2 compression. */ r = systemf("echo f | %s -oy >archive.out 2>archive.err", testprog); - assertFileContents("1 block\n", 8, "archive.err"); - failure("-y (bzip) option seems to be broken"); - if (assertEqualInt(r, 0)) { - /* Check that the archive file has a bzip2 signature. */ - p = slurpfile(&s, "archive.out"); - assert(s > 2); - assertEqualMem(p, "BZh9", 4); + p = slurpfile(&s, "archive.err"); + p[s] = '\0'; + if (r != 0) { + if (strstr(p, "compression not available") != NULL) { + skipping("This version of bsdcpio was compiled " + "without bzip2 support"); + return; + } + failure("-y option is broken"); + assertEqualInt(r, 0); + return; } + assertTextFileContents("1 block\n", "archive.err"); + /* Check that the archive file has a bzip2 signature. */ + p = slurpfile(&s, "archive.out"); + assert(s > 2); + assertEqualMem(p, "BZh9", 4); } diff --git a/archivers/libarchive/files/cpio/test/test_option_z.c b/archivers/libarchive/files/cpio/test/test_option_z.c index 15337a4ca15..91d37ac1983 100644 --- a/archivers/libarchive/files/cpio/test/test_option_z.c +++ b/archivers/libarchive/files/cpio/test/test_option_z.c @@ -28,25 +28,29 @@ __FBSDID("$FreeBSD$"); DEFINE_TEST(test_option_z) { char *p; - int fd; int r; size_t s; /* Create a file. */ - fd = open("f", O_CREAT | O_WRONLY, 0644); - assert(fd >= 0); - assertEqualInt(1, write(fd, "a", 1)); - close(fd); + assertMakeFile("f", 0644, "a"); /* Archive it with gzip compression. */ r = systemf("echo f | %s -oz >archive.out 2>archive.err", testprog); - failure("-z option seems to be broken"); - assertEqualInt(r, 0); - if (r == 0) { - /* Check that the archive file has a gzip signature. */ - p = slurpfile(&s, "archive.out"); - assert(s > 2); - assertEqualMem(p, "\x1f\x8b\x08\x00", 4); + p = slurpfile(&s, "archive.err"); + p[s] = '\0'; + if (r != 0) { + if (strstr(p, "compression not available") != NULL) { + skipping("This version of bsdcpio was compiled " + "without gzip support"); + return; + } + failure("-z option is broken"); + assertEqualInt(r, 0); + return; } + /* Check that the archive file has a gzip signature. */ + p = slurpfile(&s, "archive.out"); + assert(s > 4); + assertEqualMem(p, "\x1f\x8b\x08\x00", 4); } diff --git a/archivers/libarchive/files/cpio/test/test_owner_parse.c b/archivers/libarchive/files/cpio/test/test_owner_parse.c index 1adbce4a9fc..9875e91e4ad 100644 --- a/archivers/libarchive/files/cpio/test/test_owner_parse.c +++ b/archivers/libarchive/files/cpio/test/test_owner_parse.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2003-2007 Tim Kientzle + * Copyright (c) 2003-2009 Tim Kientzle * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,25 +26,86 @@ __FBSDID("$FreeBSD$"); #include "../cpio.h" +#include "err.h" + +#if !defined(_WIN32) +#define ROOT "root" +static int root_uids[] = { 0 }; +static int root_gids[] = { 0 }; +#elif defined(__CYGWIN__) +/* On cygwin, the Administrator user most likely exists (unless + * it has been renamed or is in a non-English localization), but + * its primary group membership depends on how the user set up + * their /etc/passwd. Likely values are 513 (None), 545 (Users), + * or 544 (Administrators). Just check for one of those... + * TODO: Handle non-English localizations...e.g. French 'Administrateur' + * Use CreateWellKnownSID() and LookupAccountName()? + */ +#define ROOT "Administrator" +static int root_uids[] = { 500 }; +static int root_gids[] = { 513, 545, 544 }; +#endif + +#if defined(ROOT) +static int +int_in_list(int i, int *l, size_t n) +{ + while (n-- > 0) + if (*l++ == i) + return (1); + failure("%d", i); + return (0); +} +#endif DEFINE_TEST(test_owner_parse) { +#if !defined(ROOT) + skipping("No uid/gid configuration for this OS"); +#else int uid, gid; - cpio_progname = "Ignore this message"; + assert(NULL == owner_parse(ROOT, &uid, &gid)); + assert(int_in_list(uid, root_uids, + sizeof(root_uids)/sizeof(root_uids[0]))); + assertEqualInt(-1, gid); + - assertEqualInt(0, owner_parse("root", &uid, &gid)); - assertEqualInt(0, uid); + assert(NULL == owner_parse(ROOT ":", &uid, &gid)); + assert(int_in_list(uid, root_uids, + sizeof(root_uids)/sizeof(root_uids[0]))); + assert(int_in_list(gid, root_gids, + sizeof(root_gids)/sizeof(root_gids[0]))); + + assert(NULL == owner_parse(ROOT ".", &uid, &gid)); + assert(int_in_list(uid, root_uids, + sizeof(root_uids)/sizeof(root_uids[0]))); + assert(int_in_list(gid, root_gids, + sizeof(root_gids)/sizeof(root_gids[0]))); + + assert(NULL == owner_parse("111", &uid, &gid)); + assertEqualInt(111, uid); assertEqualInt(-1, gid); + assert(NULL == owner_parse("112:", &uid, &gid)); + assertEqualInt(112, uid); + /* Can't assert gid, since we don't know gid for user #112. */ + + assert(NULL == owner_parse("113.", &uid, &gid)); + assertEqualInt(113, uid); + /* Can't assert gid, since we don't know gid for user #113. */ + + assert(NULL == owner_parse(":114", &uid, &gid)); + assertEqualInt(-1, uid); + assertEqualInt(114, gid); - assertEqualInt(0, owner_parse("root:", &uid, &gid)); - assertEqualInt(0, uid); - assertEqualInt(0, gid); + assert(NULL == owner_parse(".115", &uid, &gid)); + assertEqualInt(-1, uid); + assertEqualInt(115, gid); - assertEqualInt(0, owner_parse("root.", &uid, &gid)); - assertEqualInt(0, uid); - assertEqualInt(0, gid); + assert(NULL == owner_parse("116:117", &uid, &gid)); + assertEqualInt(116, uid); + assertEqualInt(117, gid); /* * TODO: Lookup current user/group name, build strings and @@ -52,17 +113,9 @@ DEFINE_TEST(test_owner_parse) * users. */ - /* - * TODO: Rework owner_parse to either return a char * pointing - * to an error message or accept a function pointer to an - * error-reporting routine so that the following tests don't - * generate any output. - * - * Alternatively, redirect stderr temporarily to suppress the output. - */ - - assertEqualInt(1, owner_parse(":nonexistentgroup", &uid, &gid)); - assertEqualInt(1, owner_parse("root:nonexistentgroup", &uid, &gid)); - assertEqualInt(1, + assert(NULL != owner_parse(":nonexistentgroup", &uid, &gid)); + assert(NULL != owner_parse(ROOT ":nonexistentgroup", &uid, &gid)); + assert(NULL != owner_parse("nonexistentuser:nonexistentgroup", &uid, &gid)); +#endif } diff --git a/archivers/libarchive/files/cpio/test/test_passthrough_dotdot.c b/archivers/libarchive/files/cpio/test/test_passthrough_dotdot.c new file mode 100644 index 00000000000..bb04341a4c7 --- /dev/null +++ b/archivers/libarchive/files/cpio/test/test_passthrough_dotdot.c @@ -0,0 +1,76 @@ +/*- + * Copyright (c) 2003-2007 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. + * 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 "test.h" +__FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_passthrough_dotdot.c,v 1.4 2008/08/24 06:21:00 kientzle Exp $"); + +/* + * Verify that "cpio -p .." works. + */ + +DEFINE_TEST(test_passthrough_dotdot) +{ + int r; + FILE *filelist; + + assertUmask(0); + + /* + * Create an assortment of files on disk. + */ + filelist = fopen("filelist", "w"); + + /* Directory. */ + assertMakeDir("dir", 0755); + assertChdir("dir"); + + fprintf(filelist, ".\n"); + + /* File with 10 bytes content. */ + assertMakeFile("file", 0642, "1234567890"); + fprintf(filelist, "file\n"); + + /* All done. */ + fclose(filelist); + + + /* + * Use cpio passthrough mode to copy files to another directory. + */ + r = systemf("%s -pdvm .. <../filelist >../stdout 2>../stderr", + testprog); + failure("Error invoking %s -pd ..", testprog); + assertEqualInt(r, 0); + + assertChdir(".."); + + /* Verify stderr and stdout. */ + assertTextFileContents("../.\n../file\n1 block\n", "stderr"); + assertEmptyFile("stdout"); + + /* Regular file. */ + assertIsReg("file", 0642); + assertFileSize("file", 10); + assertFileNLinks("file", 1); +} diff --git a/archivers/libarchive/files/cpio/test/test_passthrough_reverse.c b/archivers/libarchive/files/cpio/test/test_passthrough_reverse.c new file mode 100644 index 00000000000..674e52bc0a1 --- /dev/null +++ b/archivers/libarchive/files/cpio/test/test_passthrough_reverse.c @@ -0,0 +1,85 @@ +/*- + * Copyright (c) 2003-2007 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. + * 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 "test.h" +__FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_passthrough_reverse.c,v 1.2 2008/08/24 06:21:00 kientzle Exp $"); + +/* + * As reported by Bernd Walter: Some people are in the habit of + * using "find -d" to generate a list for cpio -p because that + * copies the top-level dir last, which preserves owner and mode + * information. That's not necessary for bsdcpio (libarchive defers + * restoring directory information), but bsdcpio should still generate + * the correct results with this usage. + */ + +DEFINE_TEST(test_passthrough_reverse) +{ + int r; + FILE *filelist; + + assertUmask(0); + + /* + * Create an assortment of files on disk. + */ + filelist = fopen("filelist", "w"); + + /* Directory. */ + assertMakeDir("dir", 0743); + + /* File with 10 bytes content. */ + assertMakeFile("dir/file", 0644, "1234567890"); + fprintf(filelist, "dir/file\n"); + + /* Write dir last. */ + fprintf(filelist, "dir\n"); + + /* All done. */ + fclose(filelist); + + + /* + * Use cpio passthrough mode to copy files to another directory. + */ + r = systemf("%s -pdvm out <filelist >stdout 2>stderr", testprog); + failure("Error invoking %s -pd out", testprog); + assertEqualInt(r, 0); + + assertChdir("out"); + + /* Verify stderr and stdout. */ + assertTextFileContents("out/dir/file\nout/dir\n1 block\n", + "../stderr"); + assertEmptyFile("../stdout"); + + /* dir */ + assertIsDir("dir", 0743); + + + /* Regular file. */ + assertIsReg("dir/file", 0644); + assertFileSize("dir/file", 10); + assertFileNLinks("dir/file", 1); +} diff --git a/archivers/libarchive/files/cpio/test/test_pathmatch.c b/archivers/libarchive/files/cpio/test/test_pathmatch.c index 83528c01ffb..177c2bcc4af 100644 --- a/archivers/libarchive/files/cpio/test/test_pathmatch.c +++ b/archivers/libarchive/files/cpio/test/test_pathmatch.c @@ -25,7 +25,7 @@ #include "test.h" __FBSDID("$FreeBSD$"); -#include "../pathmatch.h" +#include "pathmatch.h" /* * Verify that the pattern matcher implements the wildcard logic specified @@ -38,128 +38,206 @@ __FBSDID("$FreeBSD$"); * * The specification in SUSv2 is a bit incomplete, I assume the following: * Trailing '-' in [...] is not special. + * + * TODO: Figure out if there's a good way to extend this to handle + * Windows paths that use '\' as a path separator. <sigh> */ DEFINE_TEST(test_pathmatch) { - assertEqualInt(1, pathmatch("*","", 0)); - assertEqualInt(1, pathmatch("*","a", 0)); - assertEqualInt(1, pathmatch("*","abcd", 0)); + assertEqualInt(1, lafe_pathmatch("a/b/c", "a/b/c", 0)); + assertEqualInt(0, lafe_pathmatch("a/b/", "a/b/c", 0)); + assertEqualInt(0, lafe_pathmatch("a/b", "a/b/c", 0)); + assertEqualInt(0, lafe_pathmatch("a/b/c", "a/b/", 0)); + assertEqualInt(0, lafe_pathmatch("a/b/c", "a/b", 0)); + + /* Empty pattern only matches empty string. */ + assertEqualInt(1, lafe_pathmatch("","", 0)); + assertEqualInt(0, lafe_pathmatch("","a", 0)); + assertEqualInt(1, lafe_pathmatch("*","", 0)); + assertEqualInt(1, lafe_pathmatch("*","a", 0)); + assertEqualInt(1, lafe_pathmatch("*","abcd", 0)); /* SUSv2: * matches / */ - assertEqualInt(1, pathmatch("*","abcd/efgh/ijkl", 0)); - assertEqualInt(1, pathmatch("abcd*efgh/ijkl","abcd/efgh/ijkl", 0)); - assertEqualInt(1, pathmatch("abcd***efgh/ijkl","abcd/efgh/ijkl", 0)); - assertEqualInt(1, pathmatch("abcd***/efgh/ijkl","abcd/efgh/ijkl", 0)); - assertEqualInt(0, pathmatch("?", "", 0)); - assertEqualInt(0, pathmatch("?", "\0", 0)); - assertEqualInt(1, pathmatch("?", "a", 0)); - assertEqualInt(0, pathmatch("?", "ab", 0)); - assertEqualInt(1, pathmatch("?", ".", 0)); - assertEqualInt(1, pathmatch("?", "?", 0)); - assertEqualInt(1, pathmatch("a", "a", 0)); - assertEqualInt(0, pathmatch("a", "ab", 0)); - assertEqualInt(0, pathmatch("a", "ab", 0)); - assertEqualInt(1, pathmatch("a?c", "abc", 0)); + assertEqualInt(1, lafe_pathmatch("*","abcd/efgh/ijkl", 0)); + assertEqualInt(1, lafe_pathmatch("abcd*efgh/ijkl","abcd/efgh/ijkl", 0)); + assertEqualInt(1, lafe_pathmatch("abcd***efgh/ijkl","abcd/efgh/ijkl", 0)); + assertEqualInt(1, lafe_pathmatch("abcd***/efgh/ijkl","abcd/efgh/ijkl", 0)); + assertEqualInt(0, lafe_pathmatch("?", "", 0)); + assertEqualInt(0, lafe_pathmatch("?", "\0", 0)); + assertEqualInt(1, lafe_pathmatch("?", "a", 0)); + assertEqualInt(0, lafe_pathmatch("?", "ab", 0)); + assertEqualInt(1, lafe_pathmatch("?", ".", 0)); + assertEqualInt(1, lafe_pathmatch("?", "?", 0)); + assertEqualInt(1, lafe_pathmatch("a", "a", 0)); + assertEqualInt(0, lafe_pathmatch("a", "ab", 0)); + assertEqualInt(0, lafe_pathmatch("a", "ab", 0)); + assertEqualInt(1, lafe_pathmatch("a?c", "abc", 0)); /* SUSv2: ? matches / */ - assertEqualInt(1, pathmatch("a?c", "a/c", 0)); - assertEqualInt(1, pathmatch("a?*c*", "a/c", 0)); - assertEqualInt(1, pathmatch("*a*", "a/c", 0)); - assertEqualInt(1, pathmatch("*a*", "/a/c", 0)); - assertEqualInt(1, pathmatch("*a*", "defaaaaaaa", 0)); - assertEqualInt(0, pathmatch("a*", "defghi", 0)); - assertEqualInt(0, pathmatch("*a*", "defghi", 0)); - assertEqualInt(1, pathmatch("abc[def", "abc[def", 0)); - assertEqualInt(0, pathmatch("abc[def]", "abc[def", 0)); - assertEqualInt(0, pathmatch("abc[def", "abcd", 0)); - assertEqualInt(1, pathmatch("abc[def]", "abcd", 0)); - assertEqualInt(1, pathmatch("abc[def]", "abce", 0)); - assertEqualInt(1, pathmatch("abc[def]", "abcf", 0)); - assertEqualInt(0, pathmatch("abc[def]", "abcg", 0)); - assertEqualInt(1, pathmatch("abc[d*f]", "abcd", 0)); - assertEqualInt(1, pathmatch("abc[d*f]", "abc*", 0)); - assertEqualInt(0, pathmatch("abc[d*f]", "abcdefghi", 0)); - assertEqualInt(0, pathmatch("abc[d*", "abcdefghi", 0)); - assertEqualInt(1, pathmatch("abc[d*", "abc[defghi", 0)); - assertEqualInt(1, pathmatch("abc[d-f]", "abcd", 0)); - assertEqualInt(1, pathmatch("abc[d-f]", "abce", 0)); - assertEqualInt(1, pathmatch("abc[d-f]", "abcf", 0)); - assertEqualInt(0, pathmatch("abc[d-f]", "abcg", 0)); - assertEqualInt(1, pathmatch("abc[d-fh-k]", "abcd", 0)); - assertEqualInt(1, pathmatch("abc[d-fh-k]", "abce", 0)); - assertEqualInt(1, pathmatch("abc[d-fh-k]", "abcf", 0)); - assertEqualInt(0, pathmatch("abc[d-fh-k]", "abcg", 0)); - assertEqualInt(1, pathmatch("abc[d-fh-k]", "abch", 0)); - assertEqualInt(1, pathmatch("abc[d-fh-k]", "abci", 0)); - assertEqualInt(1, pathmatch("abc[d-fh-k]", "abcj", 0)); - assertEqualInt(1, pathmatch("abc[d-fh-k]", "abck", 0)); - assertEqualInt(0, pathmatch("abc[d-fh-k]", "abcl", 0)); - assertEqualInt(0, pathmatch("abc[d-fh-k]", "abc-", 0)); + assertEqualInt(1, lafe_pathmatch("a?c", "a/c", 0)); + assertEqualInt(1, lafe_pathmatch("a?*c*", "a/c", 0)); + assertEqualInt(1, lafe_pathmatch("*a*", "a/c", 0)); + assertEqualInt(1, lafe_pathmatch("*a*", "/a/c", 0)); + assertEqualInt(1, lafe_pathmatch("*a*", "defaaaaaaa", 0)); + assertEqualInt(0, lafe_pathmatch("a*", "defghi", 0)); + assertEqualInt(0, lafe_pathmatch("*a*", "defghi", 0)); + + /* Character classes */ + assertEqualInt(1, lafe_pathmatch("abc[def", "abc[def", 0)); + assertEqualInt(0, lafe_pathmatch("abc[def]", "abc[def", 0)); + assertEqualInt(0, lafe_pathmatch("abc[def", "abcd", 0)); + assertEqualInt(1, lafe_pathmatch("abc[def]", "abcd", 0)); + assertEqualInt(1, lafe_pathmatch("abc[def]", "abce", 0)); + assertEqualInt(1, lafe_pathmatch("abc[def]", "abcf", 0)); + assertEqualInt(0, lafe_pathmatch("abc[def]", "abcg", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d*f]", "abcd", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d*f]", "abc*", 0)); + assertEqualInt(0, lafe_pathmatch("abc[d*f]", "abcdefghi", 0)); + assertEqualInt(0, lafe_pathmatch("abc[d*", "abcdefghi", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d*", "abc[defghi", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d-f]", "abcd", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d-f]", "abce", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d-f]", "abcf", 0)); + assertEqualInt(0, lafe_pathmatch("abc[d-f]", "abcg", 0)); + assertEqualInt(0, lafe_pathmatch("abc[d-fh-k]", "abca", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d-fh-k]", "abcd", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d-fh-k]", "abce", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d-fh-k]", "abcf", 0)); + assertEqualInt(0, lafe_pathmatch("abc[d-fh-k]", "abcg", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d-fh-k]", "abch", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d-fh-k]", "abci", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d-fh-k]", "abcj", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d-fh-k]", "abck", 0)); + assertEqualInt(0, lafe_pathmatch("abc[d-fh-k]", "abcl", 0)); + assertEqualInt(0, lafe_pathmatch("abc[d-fh-k]", "abc-", 0)); + + /* [] matches nothing, [!] is the same as ? */ + assertEqualInt(0, lafe_pathmatch("abc[]efg", "abcdefg", 0)); + assertEqualInt(0, lafe_pathmatch("abc[]efg", "abcqefg", 0)); + assertEqualInt(0, lafe_pathmatch("abc[]efg", "abcefg", 0)); + assertEqualInt(1, lafe_pathmatch("abc[!]efg", "abcdefg", 0)); + assertEqualInt(1, lafe_pathmatch("abc[!]efg", "abcqefg", 0)); + assertEqualInt(0, lafe_pathmatch("abc[!]efg", "abcefg", 0)); /* I assume: Trailing '-' is non-special. */ - assertEqualInt(0, pathmatch("abc[d-fh-]", "abcl", 0)); - assertEqualInt(1, pathmatch("abc[d-fh-]", "abch", 0)); - assertEqualInt(1, pathmatch("abc[d-fh-]", "abc-", 0)); - assertEqualInt(1, pathmatch("abc[d-fh-]", "abc-", 0)); + assertEqualInt(0, lafe_pathmatch("abc[d-fh-]", "abcl", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d-fh-]", "abch", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d-fh-]", "abc-", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d-fh-]", "abc-", 0)); /* ']' can be backslash-quoted within a character class. */ - assertEqualInt(1, pathmatch("abc[\\]]", "abc]", 0)); - assertEqualInt(1, pathmatch("abc[\\]d]", "abc]", 0)); - assertEqualInt(1, pathmatch("abc[\\]d]", "abcd", 0)); - assertEqualInt(1, pathmatch("abc[d\\]]", "abc]", 0)); - assertEqualInt(1, pathmatch("abc[d\\]]", "abcd", 0)); - assertEqualInt(1, pathmatch("abc[d]e]", "abcde]", 0)); - assertEqualInt(1, pathmatch("abc[d\\]e]", "abc]", 0)); - assertEqualInt(0, pathmatch("abc[d\\]e]", "abcd]e", 0)); - assertEqualInt(0, pathmatch("abc[d]e]", "abc]", 0)); + assertEqualInt(1, lafe_pathmatch("abc[\\]]", "abc]", 0)); + assertEqualInt(1, lafe_pathmatch("abc[\\]d]", "abc]", 0)); + assertEqualInt(1, lafe_pathmatch("abc[\\]d]", "abcd", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d\\]]", "abc]", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d\\]]", "abcd", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d]e]", "abcde]", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d\\]e]", "abc]", 0)); + assertEqualInt(0, lafe_pathmatch("abc[d\\]e]", "abcd]e", 0)); + assertEqualInt(0, lafe_pathmatch("abc[d]e]", "abc]", 0)); /* backslash-quoted chars can appear as either end of a range. */ - assertEqualInt(1, pathmatch("abc[\\d-f]gh", "abcegh", 0)); - assertEqualInt(0, pathmatch("abc[\\d-f]gh", "abcggh", 0)); - assertEqualInt(0, pathmatch("abc[\\d-f]gh", "abc\\gh", 0)); - assertEqualInt(1, pathmatch("abc[d-\\f]gh", "abcegh", 0)); - assertEqualInt(1, pathmatch("abc[\\d-\\f]gh", "abcegh", 0)); - assertEqualInt(1, pathmatch("abc[\\d-\\f]gh", "abcegh", 0)); + assertEqualInt(1, lafe_pathmatch("abc[\\d-f]gh", "abcegh", 0)); + assertEqualInt(0, lafe_pathmatch("abc[\\d-f]gh", "abcggh", 0)); + assertEqualInt(0, lafe_pathmatch("abc[\\d-f]gh", "abc\\gh", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d-\\f]gh", "abcegh", 0)); + assertEqualInt(1, lafe_pathmatch("abc[\\d-\\f]gh", "abcegh", 0)); + assertEqualInt(1, lafe_pathmatch("abc[\\d-\\f]gh", "abcegh", 0)); /* backslash-quoted '-' isn't special. */ - assertEqualInt(0, pathmatch("abc[d\\-f]gh", "abcegh", 0)); - assertEqualInt(1, pathmatch("abc[d\\-f]gh", "abc-gh", 0)); + assertEqualInt(0, lafe_pathmatch("abc[d\\-f]gh", "abcegh", 0)); + assertEqualInt(1, lafe_pathmatch("abc[d\\-f]gh", "abc-gh", 0)); /* Leading '!' negates a character class. */ - assertEqualInt(0, pathmatch("abc[!d]", "abcd", 0)); - assertEqualInt(1, pathmatch("abc[!d]", "abce", 0)); - assertEqualInt(1, pathmatch("abc[!d]", "abcc", 0)); - assertEqualInt(0, pathmatch("abc[!d-z]", "abcq", 0)); - assertEqualInt(1, pathmatch("abc[!d-gi-z]", "abch", 0)); - assertEqualInt(1, pathmatch("abc[!fgijkl]", "abch", 0)); - assertEqualInt(0, pathmatch("abc[!fghijkl]", "abch", 0)); + assertEqualInt(0, lafe_pathmatch("abc[!d]", "abcd", 0)); + assertEqualInt(1, lafe_pathmatch("abc[!d]", "abce", 0)); + assertEqualInt(1, lafe_pathmatch("abc[!d]", "abcc", 0)); + assertEqualInt(0, lafe_pathmatch("abc[!d-z]", "abcq", 0)); + assertEqualInt(1, lafe_pathmatch("abc[!d-gi-z]", "abch", 0)); + assertEqualInt(1, lafe_pathmatch("abc[!fgijkl]", "abch", 0)); + assertEqualInt(0, lafe_pathmatch("abc[!fghijkl]", "abch", 0)); /* Backslash quotes next character. */ - assertEqualInt(0, pathmatch("abc\\[def]", "abc\\d", 0)); - assertEqualInt(1, pathmatch("abc\\[def]", "abc[def]", 0)); - assertEqualInt(0, pathmatch("abc\\\\[def]", "abc[def]", 0)); - assertEqualInt(0, pathmatch("abc\\\\[def]", "abc\\[def]", 0)); - assertEqualInt(1, pathmatch("abc\\\\[def]", "abc\\d", 0)); + assertEqualInt(0, lafe_pathmatch("abc\\[def]", "abc\\d", 0)); + assertEqualInt(1, lafe_pathmatch("abc\\[def]", "abc[def]", 0)); + assertEqualInt(0, lafe_pathmatch("abc\\\\[def]", "abc[def]", 0)); + assertEqualInt(0, lafe_pathmatch("abc\\\\[def]", "abc\\[def]", 0)); + assertEqualInt(1, lafe_pathmatch("abc\\\\[def]", "abc\\d", 0)); + assertEqualInt(1, lafe_pathmatch("abcd\\", "abcd\\", 0)); + assertEqualInt(0, lafe_pathmatch("abcd\\", "abcd\\[", 0)); + assertEqualInt(0, lafe_pathmatch("abcd\\", "abcde", 0)); + assertEqualInt(0, lafe_pathmatch("abcd\\[", "abcd\\", 0)); /* * Because '.' and '/' have special meanings, we can * identify many equivalent paths even if they're expressed - * differently. + * differently. (But quoting a character with '\\' suppresses + * special meanings!) */ - assertEqualInt(1, pathmatch("./abc/./def/", "abc/def/", 0)); - assertEqualInt(1, pathmatch("abc/def", "./././abc/./def", 0)); - assertEqualInt(1, pathmatch("abc/def/././//", "./././abc/./def/", 0)); - assertEqualInt(1, pathmatch(".////abc/.//def", "./././abc/./def", 0)); - assertEqualInt(1, pathmatch("./abc?def/", "abc/def/", 0)); + assertEqualInt(0, lafe_pathmatch("a/b/", "a/bc", 0)); + assertEqualInt(1, lafe_pathmatch("a/./b", "a/b", 0)); + assertEqualInt(0, lafe_pathmatch("a\\/./b", "a/b", 0)); + assertEqualInt(0, lafe_pathmatch("a/\\./b", "a/b", 0)); + assertEqualInt(0, lafe_pathmatch("a/.\\/b", "a/b", 0)); + assertEqualInt(0, lafe_pathmatch("a\\/\\.\\/b", "a/b", 0)); + assertEqualInt(1, lafe_pathmatch("./abc/./def/", "abc/def/", 0)); + assertEqualInt(1, lafe_pathmatch("abc/def", "./././abc/./def", 0)); + assertEqualInt(1, lafe_pathmatch("abc/def/././//", "./././abc/./def/", 0)); + assertEqualInt(1, lafe_pathmatch(".////abc/.//def", "./././abc/./def", 0)); + assertEqualInt(1, lafe_pathmatch("./abc?def/", "abc/def/", 0)); failure("\"?./\" is not the same as \"/./\""); - assertEqualInt(0, pathmatch("./abc?./def/", "abc/def/", 0)); + assertEqualInt(0, lafe_pathmatch("./abc?./def/", "abc/def/", 0)); failure("Trailing '/' should match no trailing '/'"); - assertEqualInt(1, pathmatch("./abc/./def/", "abc/def", 0)); + assertEqualInt(1, lafe_pathmatch("./abc/./def/", "abc/def", 0)); failure("Trailing '/./' is still the same directory."); - assertEqualInt(1, pathmatch("./abc/./def/./", "abc/def", 0)); + assertEqualInt(1, lafe_pathmatch("./abc/./def/./", "abc/def", 0)); failure("Trailing '/.' is still the same directory."); - assertEqualInt(1, pathmatch("./abc/./def/.", "abc/def", 0)); - assertEqualInt(1, pathmatch("./abc/./def", "abc/def/", 0)); + assertEqualInt(1, lafe_pathmatch("./abc/./def/.", "abc/def", 0)); + assertEqualInt(1, lafe_pathmatch("./abc/./def", "abc/def/", 0)); failure("Trailing '/./' is still the same directory."); - assertEqualInt(1, pathmatch("./abc/./def", "abc/def/./", 0)); + assertEqualInt(1, lafe_pathmatch("./abc/./def", "abc/def/./", 0)); failure("Trailing '/.' is still the same directory."); - assertEqualInt(1, pathmatch("./abc*/./def", "abc/def/.", 0)); + assertEqualInt(1, lafe_pathmatch("./abc*/./def", "abc/def/.", 0)); + + /* Matches not anchored at beginning. */ + assertEqualInt(0, + lafe_pathmatch("bcd", "abcd", PATHMATCH_NO_ANCHOR_START)); + assertEqualInt(1, + lafe_pathmatch("abcd", "abcd", PATHMATCH_NO_ANCHOR_START)); + assertEqualInt(0, + lafe_pathmatch("^bcd", "abcd", PATHMATCH_NO_ANCHOR_START)); + assertEqualInt(1, + lafe_pathmatch("b/c/d", "a/b/c/d", PATHMATCH_NO_ANCHOR_START)); + assertEqualInt(0, + lafe_pathmatch("b/c", "a/b/c/d", PATHMATCH_NO_ANCHOR_START)); + assertEqualInt(0, + lafe_pathmatch("^b/c", "a/b/c/d", PATHMATCH_NO_ANCHOR_START)); + + /* Matches not anchored at end. */ + assertEqualInt(0, + lafe_pathmatch("bcd", "abcd", PATHMATCH_NO_ANCHOR_END)); + assertEqualInt(1, + lafe_pathmatch("abcd", "abcd", PATHMATCH_NO_ANCHOR_END)); + assertEqualInt(1, + lafe_pathmatch("abcd", "abcd/", PATHMATCH_NO_ANCHOR_END)); + assertEqualInt(1, + lafe_pathmatch("abcd", "abcd/.", PATHMATCH_NO_ANCHOR_END)); + assertEqualInt(0, + lafe_pathmatch("abc", "abcd", PATHMATCH_NO_ANCHOR_END)); + assertEqualInt(1, + lafe_pathmatch("a/b/c", "a/b/c/d", PATHMATCH_NO_ANCHOR_END)); + assertEqualInt(0, + lafe_pathmatch("a/b/c$", "a/b/c/d", PATHMATCH_NO_ANCHOR_END)); + assertEqualInt(1, + lafe_pathmatch("a/b/c$", "a/b/c", PATHMATCH_NO_ANCHOR_END)); + assertEqualInt(1, + lafe_pathmatch("a/b/c$", "a/b/c/", PATHMATCH_NO_ANCHOR_END)); + assertEqualInt(1, + lafe_pathmatch("a/b/c/", "a/b/c/d", PATHMATCH_NO_ANCHOR_END)); + assertEqualInt(0, + lafe_pathmatch("a/b/c/$", "a/b/c/d", PATHMATCH_NO_ANCHOR_END)); + assertEqualInt(1, + lafe_pathmatch("a/b/c/$", "a/b/c/", PATHMATCH_NO_ANCHOR_END)); + assertEqualInt(1, + lafe_pathmatch("a/b/c/$", "a/b/c", PATHMATCH_NO_ANCHOR_END)); + assertEqualInt(0, + lafe_pathmatch("b/c", "a/b/c/d", PATHMATCH_NO_ANCHOR_END)); } |