[Cmake-commits] [cmake-commits] hoffman committed CMakeLists.txt NONE 1.1 Makefile NONE 1.1 bsdcpio.1 NONE 1.1 cmdline.c NONE 1.1 config_freebsd.h NONE 1.1 cpio.c NONE 1.1 cpio.h NONE 1.1 cpio_platform.h NONE 1.1 cpio_windows.c NONE 1.1 cpio_windows.h NONE 1.1

cmake-commits at cmake.org cmake-commits at cmake.org
Fri Oct 30 13:09:47 EDT 2009


Update of /cvsroot/CMake/CMake/Utilities/cmlibarchive/cpio
In directory public:/mounts/ram/cvs-serv26614/Utilities/cmlibarchive/cpio

Added Files:
	CMakeLists.txt Makefile bsdcpio.1 cmdline.c config_freebsd.h 
	cpio.c cpio.h cpio_platform.h cpio_windows.c cpio_windows.h 
Log Message:
Switch to using libarchive from libtar for cpack and cmake -E tar

This allows for a built in bzip and zip capability, so external tools 
will not be needed for these packagers.  The cmake -E tar xf should be
able to handle all compression types now as well.



--- NEW FILE: cpio_platform.h ---
/*-
 * 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/cpio_platform.h,v 1.2 2008/12/06 07:15:42 kientzle Exp $
 */

/*
 * This header is the first thing included in any of the cpio
 * source files.  As far as possible, platform-specific issues should
 * be dealt with here and not within individual source files.
 */

#ifndef CPIO_PLATFORM_H_INCLUDED
#define CPIO_PLATFORM_H_INCLUDED

#if defined(PLATFORM_CONFIG_H)
/* Use hand-built config.h in environments that need it. */
#include PLATFORM_CONFIG_H
#else
/* 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 */
#elif !defined(__FBSDID)
/* Just leaving this macro replacement empty leads to a dangling semicolon. */
#define __FBSDID(a)     struct _undefined_hack
#endif

#ifdef HAVE_LIBARCHIVE
/* If we're using the platform libarchive, include system headers. */
#include <archive.h>
#include <archive_entry.h>
#else
/* Otherwise, include user headers. */
#include "archive.h"
#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 defined(_WIN32) && !defined(__CYGWIN__)
#define CPIO_FILESIZE_TYPE  __int64
#define CPIO_FILESIZE_PRINTF    "%I64u"
#elif HAVE_UINTMAX_T
#define CPIO_FILESIZE_TYPE  uintmax_t
#define CPIO_FILESIZE_PRINTF    "%ju"
#elif 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"
#endif


/* How to mark functions that don't return. */
#if defined(__GNUC__) && (__GNUC__ > 2 || \
                          (__GNUC__ == 2 && __GNUC_MINOR__ >= 5))
#define __LA_DEAD       __attribute__((__noreturn__))
#else
#define __LA_DEAD
#endif

#if defined(_WIN32) && !defined(__CYGWIN__)
#include "cpio_windows.h"
#endif

#endif /* !CPIO_PLATFORM_H_INCLUDED */

--- NEW FILE: Makefile ---
# $FreeBSD: src/usr.bin/cpio/Makefile,v 1.6 2008/12/06 07:30:40 kientzle Exp $

.include <bsd.own.mk>
.PATH: ${.CURDIR}/../libarchive_fe

PROG=   bsdcpio
BSDCPIO_VERSION_STRING=2.7.900a
SRCS=   cpio.c cmdline.c err.c line_reader.c matching.c pathmatch.c
WARNS?= 6
DPADD=  ${LIBARCHIVE} ${LIBZ} ${LIBBZ2}
CFLAGS+= -DBSDCPIO_VERSION_STRING=\"${BSDCPIO_VERSION_STRING}\"
CFLAGS+= -DPLATFORM_CONFIG_H=\"config_freebsd.h\"
CFLAGS+= -I../libarchive_fe -I${.CURDIR}
LDADD+= -larchive -lz -lbz2 -lmd -lcrypto

.if ${MK_GNU_CPIO} != "yes"
SYMLINKS=bsdcpio ${BINDIR}/cpio
MLINKS= bsdcpio.1 cpio.1
.endif

.PHONY: check test

check test: $(PROG) bsdcpio.1.gz
    cd ${.CURDIR}/test && make clean test

.include <bsd.prog.mk>

--- NEW FILE: cmdline.c ---
/*-
 * 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
 *    in this position and unchanged.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


#include "cpio_platform.h"
__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_GRP_H
#include <grp.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include "cpio.h"
#include "err.h"

/*
 * Short options for cpio.  Please keep this sorted.
 */
static const char *short_options = "0AaBC:cdE:F:f:H:hI:iJjLlmnO:opR:rtuvW:yZz";

/*
 * Long options for cpio.  Please keep this sorted.
 */
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 }
};

/*
 * 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)
{
    enum { state_start = 0, state_next_word, state_short, state_long };
    static int state = state_start;
    static char *opt_word;

    const struct option *popt, *match = NULL, *match2 = NULL;
    const char *p, *long_prefix = "--";
    size_t optlength;
    int opt = '?';
    int required = 0;

    cpio->optarg = NULL;

    /* 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 {
            /* Set up short option parser. */
            state = state_short;
            ++opt_word;  /* Skip leading '-' */
        }
    }

    /*
     * 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);
        }

        /* Does this option take an argument? */
        p = strchr(short_options, opt);
        if (p == NULL)
            return ('?');
        if (p[1] == ':')
            required = 1;

        /* 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 {
                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;
                }
            }
        }

        /* 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 ('?');
        }

        /* 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 {
            /* 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);
}


/*
 * Parse the argument to the -R or --owner flag.
 *
 * The format is one of the following:
 *   <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 return as appropriate, -1 indicates uid/gid not specified.
 *
 * Returns NULL if no error, otherwise returns error string for display.
 *
 */
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
     *  ue -> first char after username (colon, period, or \0)
     *  g -> first char of group name
     */
    if (*spec == ':' || *spec == '.') {
        /* If spec starts with ':' or '.', then just group. */
        ue = u = NULL;
        g = spec + 1;
    } else {
        /* Otherwise, [user] or [user][:] or [user][:][group] */
        ue = u = spec;
        while (*ue != ':' && *ue != '.' && *ue != '\0')
            ++ue;
        g = ue;
        if (*g != '\0') /* Skip : or . to find first char of group. */
            ++g;
    }

    if (u != NULL) {
        /* Look up user: ue is first char after end of user. */
        char *user;
        struct passwd *pwent;

        user = (char *)malloc(ue - u + 1);
        if (user == NULL)
            return ("Couldn't allocate memory");
        memcpy(user, u, ue - u);
        user[ue - u] = '\0';
        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);
    }

    if (*g != '\0') {
        struct group *grp;
        if ((grp = getgrnam(g)) != NULL) {
            *gid = grp->gr_gid;
        } 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 (NULL);
}

--- NEW FILE: config_freebsd.h ---
/*-
 * 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_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_UTIMES 1


--- NEW FILE: cpio.h ---
/*-
 * 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/cpio.h,v 1.7 2008/12/06 07:30:40 kientzle Exp $
 */

#ifndef CPIO_H_INCLUDED
#define CPIO_H_INCLUDED

#include "cpio_platform.h"
#include <stdio.h>

#include "matching.h"

/*
 * The internal state for the "cpio" program.
 *
 * Keeping all of the state in a structure like this simplifies memory
 * leak testing (at exit, anything left on the heap is suspect).  A
 * pointer to this structure is passed to most cpio internal
 * functions.
 */
struct cpio {
    /* Option parsing */
    const char   *optarg;

    /* Options */
    const char   *filename;
    char          mode; /* -i -o -p */
    char          compress; /* -j, -y, or -z */
    const char   *format; /* -H format */
    int       bytes_per_block; /* -b block_size */
    int       verbose;   /* -v */
    int       quiet;   /* --quiet */
    int       extract_flags; /* Flags for extract operation */
    char          symlink_mode; /* H or L, per BSD conventions */
    const char   *compress_program;
    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 lafe_matching  *matching;
    char         *buff;
    size_t        buff_size;
};

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
};

int cpio_getopt(struct cpio *cpio);

#endif

--- NEW FILE: cpio_windows.h ---
/*-
 * 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 */

--- NEW FILE: CMakeLists.txt ---
############################################
#
# 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})
  ENDIF(ENABLE_CPIO_SHARED)
  # On Windows, DLL must end up in same dir with EXEs
  IF(WIN32 AND NOT CYGWIN)
    SET_TARGET_PROPERTIES(bsdcpio PROPERTIES
      RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
  ENDIF(WIN32 AND NOT CYGWIN)
  # 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)

--- NEW FILE: cpio_windows.c ---
/*-
 * 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>
#include <sys/utime.h>
#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;
    size_t l, len, slen, alloclen;
    int unc;

    len = 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

--- NEW FILE: bsdcpio.1 ---
.\" 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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$
.\"
.Dd December 21, 2007
.Dt BSDCPIO 1
.Os
.Sh NAME
.Nm cpio
.Nd copy files to and from archives
.Sh SYNOPSIS
.Nm
.Brq Fl i
.Op Ar options
.Op Ar pattern ...
.Op Ar < archive
.Nm
.Brq Fl o
.Op Ar options
.Ar < name-list
.Op Ar > archive
.Nm
.Brq Fl p
.Op Ar options
.Ar dest-dir
.Ar < name-list
.Sh DESCRIPTION
.Nm
copies files between archives and directories.
This implementation can extract from tar, pax, cpio, zip, jar, ar,
and ISO 9660 cdrom images and can create tar, pax, cpio, ar,
and shar archives.
.Pp
The first option to
.Nm
is a mode indicator from the following list:
.Bl -tag -compact -width indent
.It Fl i
Input.
Read an archive from standard input (unless overriden) and extract the
contents to disk or (if the
.Fl t
option is specified)
list the contents to standard output.
If one or more file patterns are specified, only files matching
one of the patterns will be extracted.
.It Fl o
Output.
Read a list of filenames from standard input and produce a new archive
on standard output (unless overriden) containing the specified items.
.It Fl p
Pass-through.
Read a list of filenames from standard input and copy the files to the
specified directory.
.El
.Pp
.Sh OPTIONS
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.
(Not yet implemented.)
.It Fl a
(o and p modes)
Reset access times on files after they are read.
.It Fl B
(o mode only)
Block output to records of 5120 bytes.
.It Fl C Ar size
(o mode only)
Block output to records of
.Ar size
bytes.
.It Fl c
(o mode only)
Use the old POSIX portable character format.
Equivalent to
.Fl -format Ar odc .
.It Fl d
(i and p modes)
Create directories as necessary.
.It Fl E Ar file
(i mode only)
Read list of file name patterns from
.Ar file
to list and extract.
.It Fl F Ar file
Read archive from or write archive to
.Ar file .
.It Fl f Ar pattern
(i mode only)
Ignore files that match
.Ar pattern .
.It Fl -format Ar format
(o mode only)
Produce the output archive in the specified format.
Supported formats include:
.Pp
.Bl -tag -width "iso9660" -compact
.It Ar cpio
Synonym for
.Ar odc .
.It Ar newc
The SVR4 portable cpio format.
.It Ar odc
The old POSIX.1 portable octet-oriented cpio format.
.It Ar pax
The POSIX.1 pax format, an extension of the ustar format.
.It Ar ustar
The POSIX.1 tar format.
.El
.Pp
The default format is
.Ar odc .
See
.Xr libarchive_formats 5
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 .
.It Fl i
Input mode.
See above for description.
.It Fl -insecure
(i and p mode only)
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.
Normally, symbolic links are archived and copied as symbolic links.
With this option, the target of the link will be archived or copied instead.
.It Fl l
(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 .
.It Fl o
Output mode.
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
Set the owner and/or group on files in the output.
If group is specified with no user
(for example,
.Fl R Ar :wheel )
then the group will be set but not the user.
If the user is specified with a trailing colon and no group
(for example,
.Fl R Ar root: )
then the group will be set to the user's default group.
If the user is specified with no trailing colon, then
the user will be set but not the group.
In
.Fl i
and
.Fl p
modes, this option can only be used by the super-user.
(For compatibility, a period can be used in place of the colon.)
.It Fl r
(All modes.)
Rename files interactively.
For each file, a prompt is written to
.Pa /dev/tty
containing the name of the file and a line is read from
.Pa /dev/tty .
If the line read is blank, the file is skipped.
If the line contains a single period, the file is processed normally.
Otherwise, the line is taken to be the new name of the file.
.It Fl t
(i mode only)
List the contents of the archive to stdout;
do not restore the contents to disk.
.It Fl u
(i and p modes)
Unconditionally overwrite existing files.
Ordinarily, an older file will not overwrite a newer file on disk.
.It Fl v
Print the name of each file to stderr as it is processed.
With
.Fl t ,
provide a detailed listing of each file.
.It Fl -version
Print the program version information and exit.
.It Fl y
(o mode only)
Compress the archive with bzip2-compatible compression before writing it.
In input mode, this option is ignored;
bzip2 compression is recognized automatically on input.
.It Fl Z
(o mode only)
Compress the archive with compress-compatible compression before writing it.
In input mode, this option is ignored;
compression is recognized automatically on input.
.It Fl z
(o mode only)
Compress the archive with gzip-compatible compression before writing it.
In input mode, this option is ignored;
gzip compression is recognized automatically on input.
.El
.Sh ENVIRONMENT
The following environment variables affect the execution of
.Nm :
.Bl -tag -width ".Ev BLOCKSIZE"
.It Ev LANG
The locale to use.
See
.Xr environ 7
for more information.
.It Ev TZ
The timezone to use when displaying dates.
See
.Xr environ 7
for more information.
.El
.Sh EXIT STATUS
.Ex -std
.Sh EXAMPLES
The
.Nm
command is traditionally used to copy file heirarchies in conjunction
with the
.Xr find 1
command.
The first example here simply copies all files from
.Pa src
to
.Pa dest :
.Dl Nm find Pa src | Nm Fl pmud Pa dest
.Pp
By carefully selecting options to the
.Xr find 1
command and combining it with other standard utilities,
it is possible to exercise very fine control over which files are copied.
This next example copies files from
.Pa src
to
.Pa dest
that are more than 2 days old and whose names match a particular pattern:
.Dl Nm find Pa src Fl mtime Ar +2 | Nm grep foo[bar] | Nm Fl pdmu Pa dest
.Pp
This example copies files from
.Pa src
to
.Pa dest
that are more than 2 days old and which contain the word
.Do foobar Dc :
.Dl Nm find Pa src Fl mtime Ar +2 | Nm xargs Nm grep -l foobar | Nm Fl pdmu Pa dest
.Sh COMPATIBILITY
The mode options i, o, and p and the options
a, B, c, d, f, l, m, r, t, u, and v comply with SUSv2.
.Pp
The old POSIX.1 standard specified that only
.Fl i ,
.Fl o ,
and
.Fl p
were interpreted as command-line options.
Each took a single argument of a list of modifier
characters.
For example, the standard syntax allows
.Fl imu
but does not support
.Fl miu
or
.Fl i Fl m Fl u ,
since
.Ar m
and
.Ar u
are only modifiers to
.Fl i ,
they are not command-line options in their own right.
The syntax supported by this implementation is backwards-compatible
with the standard.
For best compatibility, scripts should limit themselves to the
standard syntax.
.Sh SEE ALSO
.Xr bzip2 1 ,
.Xr tar 1 ,
.Xr gzip 1 ,
.Xr mt 1 ,
.Xr pax 1 ,
.Xr libarchive 3 ,
.Xr cpio 5 ,
.Xr libarchive-formats 5 ,
.Xr tar 5
.Sh STANDARDS
There is no current POSIX standard for the cpio command; it appeared
in
.St -p1003.1-96
but was dropped from
.St -p1003.1-2001 .
.Pp
The cpio, ustar, and pax interchange file formats are defined by
.St -p1003.1-2001
for the pax command.
.Sh HISTORY
The original
.Nm cpio
and
.Nm find
utilities were written by Dick Haight
while working in AT&T's Unix Support Group.
They first appeared in 1977 in PWB/UNIX 1.0, the
.Dq Programmer's Work Bench
system developed for use within AT&T.
They were first released outside of AT&T as part of System III Unix in 1981.
As a result,
.Nm cpio
actually predates
.Nm tar ,
even though it was not well-known outside of AT&T until some time later.
.Pp
This is a complete re-implementation based on the
.Xr libarchive 3
library.
.Sh BUGS
The cpio archive format has several basic limitations:
It does not store user and group names, only numbers.
As a result, it cannot be reliably used to transfer
files between systems with dissimilar user and group numbering.
Older cpio formats limit the user and group numbers to
16 or 18 bits, which is insufficient for modern systems.
The cpio archive formats cannot support files over 4 gigabytes,
except for the
.Dq odc
variant, which can support files up to 8 gigabytes.

--- NEW FILE: cpio.c ---
/*-
 * 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
 *    in this position and unchanged.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
[...1194 lines suppressed...]

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);
}



More information about the Cmake-commits mailing list