2008-05-26 17:15:35 +00:00
|
|
|
/*-
|
|
|
|
* 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$");
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <archive.h>
|
|
|
|
#include <archive_entry.h>
|
|
|
|
|
2009-04-17 04:04:57 +00:00
|
|
|
#ifdef HAVE_SYS_MKDEV_H
|
|
|
|
#include <sys/mkdev.h>
|
|
|
|
#endif
|
2008-05-26 17:15:35 +00:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2009-04-17 04:04:57 +00:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2008-05-26 17:15:35 +00:00
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2008-11-29 20:22:02 +00:00
|
|
|
#ifdef HAVE_GRP_H
|
|
|
|
#include <grp.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_PWD_H
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
2008-05-26 17:15:35 +00:00
|
|
|
#ifdef HAVE_STDARG_H
|
|
|
|
#include <stdarg.h>
|
|
|
|
#endif
|
2010-05-08 16:47:33 +00:00
|
|
|
#ifdef HAVE_STDINT_H
|
|
|
|
#include <stdint.h>
|
|
|
|
#endif
|
2008-05-26 17:15:35 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef HAVE_STDLIB_H
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2009-04-17 04:04:57 +00:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TIME_H
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
2008-05-26 17:15:35 +00:00
|
|
|
|
|
|
|
#include "cpio.h"
|
2010-05-08 16:47:33 +00:00
|
|
|
#include "err.h"
|
|
|
|
#include "line_reader.h"
|
2008-05-26 17:15:35 +00:00
|
|
|
#include "matching.h"
|
|
|
|
|
2008-11-29 20:22:02 +00:00
|
|
|
/* Fixed size of uname/gname caches. */
|
|
|
|
#define name_cache_size 101
|
|
|
|
|
2010-05-08 16:47:33 +00:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2008-11-29 20:22:02 +00:00
|
|
|
struct name_cache {
|
|
|
|
int probes;
|
|
|
|
int hits;
|
|
|
|
size_t size;
|
|
|
|
struct {
|
|
|
|
id_t id;
|
|
|
|
char *name;
|
|
|
|
} cache[name_cache_size];
|
|
|
|
};
|
|
|
|
|
2010-05-08 16:47:33 +00:00
|
|
|
static int extract_data(struct archive *, struct archive *);
|
|
|
|
const char * cpio_i64toa(int64_t);
|
2008-05-26 17:15:35 +00:00
|
|
|
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 *);
|
2008-11-29 20:22:02 +00:00
|
|
|
static void free_cache(struct name_cache *cache);
|
|
|
|
static void list_item_verbose(struct cpio *, struct archive_entry *);
|
2008-05-26 17:15:35 +00:00
|
|
|
static void long_help(void);
|
2008-11-29 20:22:02 +00:00
|
|
|
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);
|
2008-05-26 17:15:35 +00:00
|
|
|
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 *);
|
2009-04-17 04:04:57 +00:00
|
|
|
static int restore_time(struct cpio *, struct archive_entry *,
|
2008-05-26 17:15:35 +00:00
|
|
|
const char *, int fd);
|
|
|
|
static void usage(void);
|
|
|
|
static void version(void);
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
static char buff[16384];
|
|
|
|
struct cpio _cpio; /* Allocated on stack. */
|
|
|
|
struct cpio *cpio;
|
2010-05-08 16:47:33 +00:00
|
|
|
const char *errmsg;
|
2008-05-26 17:15:35 +00:00
|
|
|
int uid, gid;
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
cpio = &_cpio;
|
|
|
|
memset(cpio, 0, sizeof(*cpio));
|
|
|
|
cpio->buff = buff;
|
|
|
|
cpio->buff_size = sizeof(buff);
|
|
|
|
|
2011-07-17 21:42:22 +00:00
|
|
|
/* Need lafe_progname before calling lafe_warnc. */
|
2008-05-26 17:15:35 +00:00
|
|
|
if (*argv == NULL)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_progname = "bsdcpio";
|
2008-05-26 17:15:35 +00:00
|
|
|
else {
|
2009-04-17 04:04:57 +00:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_progname = strrchr(*argv, '\\');
|
2009-04-17 04:04:57 +00:00
|
|
|
#else
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_progname = strrchr(*argv, '/');
|
2009-04-17 04:04:57 +00:00
|
|
|
#endif
|
2011-07-17 21:42:22 +00:00
|
|
|
if (lafe_progname != NULL)
|
|
|
|
lafe_progname++;
|
2008-05-26 17:15:35 +00:00
|
|
|
else
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_progname = *argv;
|
2008-05-26 17:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cpio->uid_override = -1;
|
|
|
|
cpio->gid_override = -1;
|
|
|
|
cpio->argv = argv;
|
|
|
|
cpio->argc = argc;
|
|
|
|
cpio->mode = '\0';
|
|
|
|
cpio->verbose = 0;
|
|
|
|
cpio->compress = '\0';
|
|
|
|
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;
|
2008-07-30 03:35:45 +00:00
|
|
|
cpio->extract_flags |= ARCHIVE_EXTRACT_PERM;
|
2008-08-20 16:39:18 +00:00
|
|
|
cpio->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
|
|
|
|
cpio->extract_flags |= ARCHIVE_EXTRACT_ACL;
|
2010-05-08 16:47:33 +00:00
|
|
|
#if !defined(_WIN32) && !defined(__CYGWIN__)
|
2008-07-06 16:39:18 +00:00
|
|
|
if (geteuid() == 0)
|
|
|
|
cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER;
|
2010-05-08 16:47:33 +00:00
|
|
|
#endif
|
2008-05-26 17:15:35 +00:00
|
|
|
cpio->bytes_per_block = 512;
|
|
|
|
cpio->filename = NULL;
|
|
|
|
|
|
|
|
while ((opt = cpio_getopt(cpio)) != -1) {
|
|
|
|
switch (opt) {
|
2008-06-21 02:20:20 +00:00
|
|
|
case '0': /* GNU convention: --null, -0 */
|
2010-05-08 16:47:33 +00:00
|
|
|
cpio->option_null = 1;
|
2008-06-21 02:20:20 +00:00
|
|
|
break;
|
2008-05-26 17:15:35 +00:00
|
|
|
case 'A': /* NetBSD/OpenBSD */
|
|
|
|
cpio->option_append = 1;
|
|
|
|
break;
|
|
|
|
case 'a': /* POSIX 1997 */
|
|
|
|
cpio->option_atime_restore = 1;
|
|
|
|
break;
|
|
|
|
case 'B': /* POSIX 1997 */
|
|
|
|
cpio->bytes_per_block = 5120;
|
|
|
|
break;
|
|
|
|
case 'C': /* NetBSD/OpenBSD */
|
2008-12-06 07:30:40 +00:00
|
|
|
cpio->bytes_per_block = atoi(cpio->optarg);
|
2008-05-26 17:15:35 +00:00
|
|
|
if (cpio->bytes_per_block <= 0)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "Invalid blocksize %s", cpio->optarg);
|
2008-05-26 17:15:35 +00:00
|
|
|
break;
|
|
|
|
case 'c': /* POSIX 1997 */
|
|
|
|
cpio->format = "odc";
|
|
|
|
break;
|
|
|
|
case 'd': /* POSIX 1997 */
|
|
|
|
cpio->extract_flags &= ~ARCHIVE_EXTRACT_NO_AUTODIR;
|
|
|
|
break;
|
|
|
|
case 'E': /* NetBSD/OpenBSD */
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_include_from_file(&cpio->matching,
|
2010-05-08 16:47:33 +00:00
|
|
|
cpio->optarg, cpio->option_null);
|
2008-05-26 17:15:35 +00:00
|
|
|
break;
|
|
|
|
case 'F': /* NetBSD/OpenBSD/GNU cpio */
|
2008-12-06 07:30:40 +00:00
|
|
|
cpio->filename = cpio->optarg;
|
2008-05-26 17:15:35 +00:00
|
|
|
break;
|
|
|
|
case 'f': /* POSIX 1997 */
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_exclude(&cpio->matching, cpio->optarg);
|
2008-05-26 17:15:35 +00:00
|
|
|
break;
|
|
|
|
case 'H': /* GNU cpio (also --format) */
|
2008-12-06 07:30:40 +00:00
|
|
|
cpio->format = cpio->optarg;
|
2008-05-26 17:15:35 +00:00
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
long_help();
|
|
|
|
break;
|
|
|
|
case 'I': /* NetBSD/OpenBSD */
|
2008-12-06 07:30:40 +00:00
|
|
|
cpio->filename = cpio->optarg;
|
2008-05-26 17:15:35 +00:00
|
|
|
break;
|
|
|
|
case 'i': /* POSIX 1997 */
|
2009-04-17 04:04:57 +00:00
|
|
|
if (cpio->mode != '\0')
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0,
|
2009-04-17 04:04:57 +00:00
|
|
|
"Cannot use both -i and -%c", cpio->mode);
|
2008-05-26 17:15:35 +00:00
|
|
|
cpio->mode = opt;
|
|
|
|
break;
|
2010-05-08 16:47:33 +00:00
|
|
|
case 'J': /* GNU tar, others */
|
|
|
|
cpio->compress = opt;
|
|
|
|
break;
|
|
|
|
case 'j': /* GNU tar, others */
|
|
|
|
cpio->compress = opt;
|
|
|
|
break;
|
2008-05-26 17:15:35 +00:00
|
|
|
case OPTION_INSECURE:
|
|
|
|
cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_SYMLINKS;
|
|
|
|
cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT;
|
|
|
|
break;
|
|
|
|
case 'L': /* GNU cpio */
|
|
|
|
cpio->option_follow_links = 1;
|
|
|
|
break;
|
|
|
|
case 'l': /* POSIX 1997 */
|
|
|
|
cpio->option_link = 1;
|
|
|
|
break;
|
2010-05-08 16:47:33 +00:00
|
|
|
case OPTION_LZMA: /* GNU tar, others */
|
|
|
|
cpio->compress = opt;
|
|
|
|
break;
|
2008-05-26 17:15:35 +00:00
|
|
|
case 'm': /* POSIX 1997 */
|
|
|
|
cpio->extract_flags |= ARCHIVE_EXTRACT_TIME;
|
|
|
|
break;
|
2009-04-17 04:04:57 +00:00
|
|
|
case 'n': /* GNU cpio */
|
|
|
|
cpio->option_numeric_uid_gid = 1;
|
|
|
|
break;
|
2008-07-29 15:23:31 +00:00
|
|
|
case OPTION_NO_PRESERVE_OWNER: /* GNU cpio */
|
|
|
|
cpio->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
|
|
|
|
break;
|
2008-05-26 17:15:35 +00:00
|
|
|
case 'O': /* GNU cpio */
|
2008-12-06 07:30:40 +00:00
|
|
|
cpio->filename = cpio->optarg;
|
2008-05-26 17:15:35 +00:00
|
|
|
break;
|
|
|
|
case 'o': /* POSIX 1997 */
|
2009-04-17 04:04:57 +00:00
|
|
|
if (cpio->mode != '\0')
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0,
|
2009-04-17 04:04:57 +00:00
|
|
|
"Cannot use both -o and -%c", cpio->mode);
|
2008-05-26 17:15:35 +00:00
|
|
|
cpio->mode = opt;
|
|
|
|
break;
|
|
|
|
case 'p': /* POSIX 1997 */
|
2009-04-17 04:04:57 +00:00
|
|
|
if (cpio->mode != '\0')
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0,
|
2009-04-17 04:04:57 +00:00
|
|
|
"Cannot use both -p and -%c", cpio->mode);
|
2008-05-26 17:15:35 +00:00
|
|
|
cpio->mode = opt;
|
2008-06-24 15:18:40 +00:00
|
|
|
cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT;
|
2008-05-26 17:15:35 +00:00
|
|
|
break;
|
2010-05-08 16:47:33 +00:00
|
|
|
case OPTION_PRESERVE_OWNER:
|
|
|
|
cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER;
|
|
|
|
break;
|
2008-05-26 17:15:35 +00:00
|
|
|
case OPTION_QUIET: /* GNU cpio */
|
|
|
|
cpio->quiet = 1;
|
|
|
|
break;
|
|
|
|
case 'R': /* GNU cpio, also --owner */
|
2010-08-08 01:25:33 +00:00
|
|
|
/* TODO: owner_parse should return uname/gname
|
|
|
|
* also; use that to set [ug]name_override. */
|
2010-05-08 16:47:33 +00:00
|
|
|
errmsg = owner_parse(cpio->optarg, &uid, &gid);
|
|
|
|
if (errmsg) {
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_warnc(-1, "%s", errmsg);
|
2008-05-26 17:15:35 +00:00
|
|
|
usage();
|
2010-05-08 16:47:33 +00:00
|
|
|
}
|
2010-08-08 01:25:33 +00:00
|
|
|
if (uid != -1) {
|
2008-05-26 17:15:35 +00:00
|
|
|
cpio->uid_override = uid;
|
2010-08-08 01:25:33 +00:00
|
|
|
cpio->uname_override = NULL;
|
|
|
|
}
|
|
|
|
if (gid != -1) {
|
2008-05-26 17:15:35 +00:00
|
|
|
cpio->gid_override = gid;
|
2010-08-08 01:25:33 +00:00
|
|
|
cpio->gname_override = NULL;
|
|
|
|
}
|
2008-05-26 17:15:35 +00:00
|
|
|
break;
|
|
|
|
case 'r': /* POSIX 1997 */
|
|
|
|
cpio->option_rename = 1;
|
|
|
|
break;
|
|
|
|
case 't': /* POSIX 1997 */
|
|
|
|
cpio->option_list = 1;
|
|
|
|
break;
|
|
|
|
case 'u': /* POSIX 1997 */
|
|
|
|
cpio->extract_flags
|
|
|
|
&= ~ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
|
|
|
|
break;
|
|
|
|
case 'v': /* POSIX 1997 */
|
|
|
|
cpio->verbose++;
|
|
|
|
break;
|
|
|
|
case OPTION_VERSION: /* GNU convention */
|
|
|
|
version();
|
|
|
|
break;
|
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* cpio_getopt() handles -W specially, so it's not
|
|
|
|
* available here.
|
|
|
|
*/
|
|
|
|
case 'W': /* Obscure, but useful GNU convention. */
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case 'y': /* tar convention */
|
|
|
|
cpio->compress = opt;
|
|
|
|
break;
|
|
|
|
case 'Z': /* tar convention */
|
|
|
|
cpio->compress = opt;
|
|
|
|
break;
|
|
|
|
case 'z': /* tar convention */
|
|
|
|
cpio->compress = opt;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-17 04:04:57 +00:00
|
|
|
/*
|
|
|
|
* 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')
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "Option -t requires -i");
|
2009-04-17 04:04:57 +00:00
|
|
|
/* -n requires -it */
|
|
|
|
if (cpio->option_numeric_uid_gid && !cpio->option_list)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "Option -n requires -it");
|
2009-04-17 04:04:57 +00:00
|
|
|
/* Can only specify format when writing */
|
|
|
|
if (cpio->format != NULL && cpio->mode != 'o')
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "Option --format requires -o");
|
2009-04-17 04:04:57 +00:00
|
|
|
/* -l requires -p */
|
|
|
|
if (cpio->option_link && cpio->mode != 'p')
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "Option -l requires -p");
|
2009-04-17 04:04:57 +00:00
|
|
|
/* TODO: Flag other nonsensical combinations. */
|
2008-05-26 17:15:35 +00:00
|
|
|
|
|
|
|
switch (cpio->mode) {
|
|
|
|
case 'o':
|
2009-04-17 04:04:57 +00:00
|
|
|
/* TODO: Implement old binary format in libarchive,
|
|
|
|
use that here. */
|
|
|
|
if (cpio->format == NULL)
|
|
|
|
cpio->format = "odc"; /* Default format */
|
|
|
|
|
2008-05-26 17:15:35 +00:00
|
|
|
mode_out(cpio);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
while (*cpio->argv != NULL) {
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_include(&cpio->matching, *cpio->argv);
|
2008-05-26 17:15:35 +00:00
|
|
|
--cpio->argc;
|
|
|
|
++cpio->argv;
|
|
|
|
}
|
|
|
|
if (cpio->option_list)
|
|
|
|
mode_list(cpio);
|
|
|
|
else
|
|
|
|
mode_in(cpio);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
if (*cpio->argv == NULL || **cpio->argv == '\0')
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0,
|
2008-05-26 17:15:35 +00:00
|
|
|
"-p mode requires a target directory");
|
|
|
|
mode_pass(cpio, *cpio->argv);
|
|
|
|
break;
|
|
|
|
default:
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0,
|
2008-05-26 17:15:35 +00:00
|
|
|
"Must specify at least one of -i, -o, or -p");
|
|
|
|
}
|
|
|
|
|
2008-11-29 20:22:02 +00:00
|
|
|
free_cache(cpio->gname_cache);
|
|
|
|
free_cache(cpio->uname_cache);
|
2010-05-08 16:47:33 +00:00
|
|
|
return (cpio->return_value);
|
2008-05-26 17:15:35 +00:00
|
|
|
}
|
|
|
|
|
2010-05-08 16:47:33 +00:00
|
|
|
static void
|
2008-05-26 17:15:35 +00:00
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
2011-07-17 21:42:22 +00:00
|
|
|
p = lafe_progname;
|
2008-05-26 17:15:35 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
fprintf(stderr, " Help: %s --help\n", p);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *long_help_msg =
|
|
|
|
"First option must be a mode specifier:\n"
|
|
|
|
" -i Input -o Output -p Pass\n"
|
|
|
|
"Common Options:\n"
|
2010-05-08 16:47:33 +00:00
|
|
|
" -v Verbose\n"
|
2008-05-26 17:15:35 +00:00
|
|
|
"Create: %p -o [options] < [list of files] > [archive]\n"
|
2010-05-08 16:47:33 +00:00
|
|
|
" -J,-y,-z,--lzma Compress archive with xz/bzip2/gzip/lzma\n"
|
2008-05-26 17:15:35 +00:00
|
|
|
" --format {odc|newc|ustar} Select archive format\n"
|
|
|
|
"List: %p -it < [archive]\n"
|
|
|
|
"Extract: %p -i [options] < [archive]\n";
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that the word 'bsdcpio' will always appear in the first line
|
|
|
|
* of output.
|
|
|
|
*
|
|
|
|
* In particular, /bin/sh scripts that need to test for the presence
|
|
|
|
* of bsdcpio can use the following template:
|
|
|
|
*
|
|
|
|
* if (cpio --help 2>&1 | grep bsdcpio >/dev/null 2>&1 ) then \
|
|
|
|
* echo bsdcpio; else echo not bsdcpio; fi
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
long_help(void)
|
|
|
|
{
|
|
|
|
const char *prog;
|
|
|
|
const char *p;
|
|
|
|
|
2011-07-17 21:42:22 +00:00
|
|
|
prog = lafe_progname;
|
2008-05-26 17:15:35 +00:00
|
|
|
|
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
p = (strcmp(prog,"bsdcpio") != 0) ? "(bsdcpio)" : "";
|
|
|
|
printf("%s%s: manipulate archive files\n", prog, p);
|
|
|
|
|
|
|
|
for (p = long_help_msg; *p != '\0'; p++) {
|
|
|
|
if (*p == '%') {
|
|
|
|
if (p[1] == 'p') {
|
|
|
|
fputs(prog, stdout);
|
|
|
|
p++;
|
|
|
|
} else
|
|
|
|
putchar('%');
|
|
|
|
} else
|
|
|
|
putchar(*p);
|
|
|
|
}
|
|
|
|
version();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
version(void)
|
|
|
|
{
|
|
|
|
fprintf(stdout,"bsdcpio %s -- %s\n",
|
|
|
|
BSDCPIO_VERSION_STRING,
|
|
|
|
archive_version());
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mode_out(struct cpio *cpio)
|
|
|
|
{
|
2008-06-21 02:20:20 +00:00
|
|
|
struct archive_entry *entry, *spare;
|
2011-07-17 21:42:22 +00:00
|
|
|
struct lafe_line_reader *lr;
|
2008-06-21 02:20:20 +00:00
|
|
|
const char *p;
|
2008-05-26 17:15:35 +00:00
|
|
|
int r;
|
|
|
|
|
|
|
|
if (cpio->option_append)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "Append mode not yet supported.");
|
2010-05-08 16:47:33 +00:00
|
|
|
|
|
|
|
cpio->archive_read_disk = archive_read_disk_new();
|
|
|
|
if (cpio->archive_read_disk == NULL)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "Failed to allocate archive object");
|
2010-05-08 16:47:33 +00:00
|
|
|
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);
|
|
|
|
|
2008-05-26 17:15:35 +00:00
|
|
|
cpio->archive = archive_write_new();
|
|
|
|
if (cpio->archive == NULL)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "Failed to allocate archive object");
|
2008-05-26 17:15:35 +00:00
|
|
|
switch (cpio->compress) {
|
2010-05-08 16:47:33 +00:00
|
|
|
case 'J':
|
|
|
|
r = archive_write_set_compression_xz(cpio->archive);
|
|
|
|
break;
|
|
|
|
case OPTION_LZMA:
|
|
|
|
r = archive_write_set_compression_lzma(cpio->archive);
|
|
|
|
break;
|
2008-05-26 17:15:35 +00:00
|
|
|
case 'j': case 'y':
|
2009-04-19 06:59:12 +00:00
|
|
|
r = archive_write_set_compression_bzip2(cpio->archive);
|
2008-05-26 17:15:35 +00:00
|
|
|
break;
|
|
|
|
case 'z':
|
2009-04-19 06:59:12 +00:00
|
|
|
r = archive_write_set_compression_gzip(cpio->archive);
|
2008-05-26 17:15:35 +00:00
|
|
|
break;
|
|
|
|
case 'Z':
|
2009-04-19 06:59:12 +00:00
|
|
|
r = archive_write_set_compression_compress(cpio->archive);
|
2008-05-26 17:15:35 +00:00
|
|
|
break;
|
2010-05-08 16:47:33 +00:00
|
|
|
default:
|
2009-04-19 06:59:12 +00:00
|
|
|
r = archive_write_set_compression_none(cpio->archive);
|
2008-05-26 17:15:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-05-08 16:47:33 +00:00
|
|
|
if (r < ARCHIVE_WARN)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "Requested compression not available");
|
2008-05-26 17:15:35 +00:00
|
|
|
r = archive_write_set_format_by_name(cpio->archive, cpio->format);
|
|
|
|
if (r != ARCHIVE_OK)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
|
2008-05-26 17:15:35 +00:00
|
|
|
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));
|
|
|
|
|
2010-05-08 16:47:33 +00:00
|
|
|
/*
|
|
|
|
* The main loop: Copy each file into the output archive.
|
|
|
|
*/
|
2008-05-26 17:15:35 +00:00
|
|
|
r = archive_write_open_file(cpio->archive, cpio->filename);
|
|
|
|
if (r != ARCHIVE_OK)
|
2011-07-17 21:42:22 +00:00
|
|
|
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)
|
2008-06-21 02:20:20 +00:00
|
|
|
file_to_archive(cpio, p);
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_line_reader_free(lr);
|
2008-05-26 17:15:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The hardlink detection may have queued up a couple of entries
|
|
|
|
* that can now be flushed.
|
|
|
|
*/
|
|
|
|
entry = NULL;
|
|
|
|
archive_entry_linkify(cpio->linkresolver, &entry, &spare);
|
|
|
|
while (entry != NULL) {
|
|
|
|
entry_to_archive(cpio, entry);
|
|
|
|
archive_entry_free(entry);
|
|
|
|
entry = NULL;
|
|
|
|
archive_entry_linkify(cpio->linkresolver, &entry, &spare);
|
|
|
|
}
|
|
|
|
|
|
|
|
r = archive_write_close(cpio->archive);
|
|
|
|
if (r != ARCHIVE_OK)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
|
2008-05-26 17:15:35 +00:00
|
|
|
|
|
|
|
if (!cpio->quiet) {
|
2010-05-08 16:47:33 +00:00
|
|
|
int64_t blocks =
|
|
|
|
(archive_position_uncompressed(cpio->archive) + 511)
|
|
|
|
/ 512;
|
|
|
|
fprintf(stderr, "%lu %s\n", (unsigned long)blocks,
|
2008-05-26 17:15:35 +00:00
|
|
|
blocks == 1 ? "block" : "blocks");
|
|
|
|
}
|
|
|
|
archive_write_finish(cpio->archive);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is used by both out mode (to copy objects from disk into
|
|
|
|
* an archive) and pass mode (to copy objects from disk to
|
|
|
|
* an archive_write_disk "archive").
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
file_to_archive(struct cpio *cpio, const char *srcpath)
|
|
|
|
{
|
|
|
|
const char *destpath;
|
|
|
|
struct archive_entry *entry, *spare;
|
|
|
|
size_t len;
|
|
|
|
const char *p;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create an archive_entry describing the source file.
|
2009-04-17 04:04:57 +00:00
|
|
|
*
|
2008-05-26 17:15:35 +00:00
|
|
|
*/
|
|
|
|
entry = archive_entry_new();
|
|
|
|
if (entry == NULL)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "Couldn't allocate entry");
|
2008-05-26 17:15:35 +00:00
|
|
|
archive_entry_copy_sourcepath(entry, srcpath);
|
2010-05-08 16:47:33 +00:00
|
|
|
r = archive_read_disk_entry_from_file(cpio->archive_read_disk,
|
|
|
|
entry, -1, NULL);
|
|
|
|
if (r < ARCHIVE_FAILED)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "%s",
|
2010-05-08 16:47:33 +00:00
|
|
|
archive_error_string(cpio->archive_read_disk));
|
|
|
|
if (r < ARCHIVE_OK)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_warnc(0, "%s",
|
2010-05-08 16:47:33 +00:00
|
|
|
archive_error_string(cpio->archive_read_disk));
|
|
|
|
if (r <= ARCHIVE_FAILED) {
|
|
|
|
cpio->return_value = 1;
|
|
|
|
return (r);
|
2008-05-26 17:15:35 +00:00
|
|
|
}
|
|
|
|
|
2010-08-08 01:25:33 +00:00
|
|
|
if (cpio->uid_override >= 0) {
|
2010-05-08 16:47:33 +00:00
|
|
|
archive_entry_set_uid(entry, cpio->uid_override);
|
2010-08-08 01:25:33 +00:00
|
|
|
archive_entry_set_uname(entry, cpio->uname_override);
|
|
|
|
}
|
|
|
|
if (cpio->gid_override >= 0) {
|
2010-05-08 16:47:33 +00:00
|
|
|
archive_entry_set_gid(entry, cpio->gid_override);
|
2010-08-08 01:25:33 +00:00
|
|
|
archive_entry_set_gname(entry, cpio->gname_override);
|
|
|
|
}
|
2008-05-26 17:15:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a destination path for this entry.
|
|
|
|
* "destination path" is the name to which it will be copied in
|
|
|
|
* pass mode or the name that will go into the archive in
|
|
|
|
* output mode.
|
|
|
|
*/
|
|
|
|
destpath = srcpath;
|
|
|
|
if (cpio->destdir) {
|
|
|
|
len = strlen(cpio->destdir) + strlen(srcpath) + 8;
|
|
|
|
if (len >= cpio->pass_destpath_alloc) {
|
|
|
|
while (len >= cpio->pass_destpath_alloc) {
|
|
|
|
cpio->pass_destpath_alloc += 512;
|
|
|
|
cpio->pass_destpath_alloc *= 2;
|
|
|
|
}
|
|
|
|
free(cpio->pass_destpath);
|
|
|
|
cpio->pass_destpath = malloc(cpio->pass_destpath_alloc);
|
|
|
|
if (cpio->pass_destpath == NULL)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, ENOMEM,
|
2008-05-26 17:15:35 +00:00
|
|
|
"Can't allocate path buffer");
|
|
|
|
}
|
|
|
|
strcpy(cpio->pass_destpath, cpio->destdir);
|
|
|
|
p = srcpath;
|
|
|
|
while (p[0] == '/')
|
|
|
|
++p;
|
|
|
|
strcat(cpio->pass_destpath, p);
|
|
|
|
destpath = cpio->pass_destpath;
|
|
|
|
}
|
|
|
|
if (cpio->option_rename)
|
|
|
|
destpath = cpio_rename(destpath);
|
|
|
|
if (destpath == NULL)
|
|
|
|
return (0);
|
|
|
|
archive_entry_copy_pathname(entry, destpath);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're trying to preserve hardlinks, match them here.
|
|
|
|
*/
|
|
|
|
spare = NULL;
|
|
|
|
if (cpio->linkresolver != NULL
|
2010-05-08 16:47:33 +00:00
|
|
|
&& archive_entry_filetype(entry) != AE_IFDIR) {
|
2008-05-26 17:15:35 +00:00
|
|
|
archive_entry_linkify(cpio->linkresolver, &entry, &spare);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry != NULL) {
|
|
|
|
r = entry_to_archive(cpio, entry);
|
|
|
|
archive_entry_free(entry);
|
2010-05-08 16:47:33 +00:00
|
|
|
if (spare != NULL) {
|
|
|
|
if (r == 0)
|
|
|
|
r = entry_to_archive(cpio, spare);
|
|
|
|
archive_entry_free(spare);
|
|
|
|
}
|
2008-05-26 17:15:35 +00:00
|
|
|
}
|
|
|
|
return (r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
entry_to_archive(struct cpio *cpio, struct archive_entry *entry)
|
|
|
|
{
|
|
|
|
const char *destpath = archive_entry_pathname(entry);
|
|
|
|
const char *srcpath = archive_entry_sourcepath(entry);
|
|
|
|
int fd = -1;
|
|
|
|
ssize_t bytes_read;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
/* Print out the destination name to the user. */
|
|
|
|
if (cpio->verbose)
|
|
|
|
fprintf(stderr,"%s", destpath);
|
|
|
|
|
|
|
|
/*
|
2008-07-05 05:16:23 +00:00
|
|
|
* Option_link only makes sense in pass mode and for
|
|
|
|
* regular files. Also note: if a link operation fails
|
|
|
|
* because of cross-device restrictions, we'll fall back
|
|
|
|
* to copy mode for that entry.
|
|
|
|
*
|
|
|
|
* TODO: Test other cpio implementations to see if they
|
|
|
|
* hard-link anything other than regular files here.
|
2008-05-26 17:15:35 +00:00
|
|
|
*/
|
2008-07-01 05:45:03 +00:00
|
|
|
if (cpio->option_link
|
2008-07-05 05:16:23 +00:00
|
|
|
&& archive_entry_filetype(entry) == AE_IFREG)
|
2008-07-01 05:45:03 +00:00
|
|
|
{
|
2008-06-21 17:47:56 +00:00
|
|
|
struct archive_entry *t;
|
|
|
|
/* Save the original entry in case we need it later. */
|
|
|
|
t = archive_entry_clone(entry);
|
|
|
|
if (t == NULL)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, ENOMEM, "Can't create link");
|
2008-06-21 17:47:56 +00:00
|
|
|
/* Note: link(2) doesn't create parent directories,
|
2008-07-01 05:45:03 +00:00
|
|
|
* so we use archive_write_header() instead as a
|
|
|
|
* convenience. */
|
2008-06-21 17:47:56 +00:00
|
|
|
archive_entry_set_hardlink(t, srcpath);
|
2008-06-26 15:46:01 +00:00
|
|
|
/* This is a straight link that carries no data. */
|
|
|
|
archive_entry_set_size(t, 0);
|
2008-06-21 17:47:56 +00:00
|
|
|
r = archive_write_header(cpio->archive, t);
|
|
|
|
archive_entry_free(t);
|
2008-05-26 17:15:35 +00:00
|
|
|
if (r != ARCHIVE_OK)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_warnc(archive_errno(cpio->archive),
|
2010-05-08 16:47:33 +00:00
|
|
|
"%s", archive_error_string(cpio->archive));
|
2008-05-26 17:15:35 +00:00
|
|
|
if (r == ARCHIVE_FATAL)
|
|
|
|
exit(1);
|
2008-06-21 17:47:56 +00:00
|
|
|
#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. */
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_warnc(0, "Copying file instead");
|
2008-06-21 17:47:56 +00:00
|
|
|
} else
|
|
|
|
#endif
|
2008-05-26 17:15:35 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure we can open the file (if necessary) before
|
|
|
|
* trying to write the header.
|
|
|
|
*/
|
|
|
|
if (archive_entry_filetype(entry) == AE_IFREG) {
|
|
|
|
if (archive_entry_size(entry) > 0) {
|
2010-05-08 16:47:33 +00:00
|
|
|
fd = open(srcpath, O_RDONLY | O_BINARY);
|
2008-05-26 17:15:35 +00:00
|
|
|
if (fd < 0) {
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_warnc(errno,
|
2008-05-26 17:15:35 +00:00
|
|
|
"%s: could not open file", srcpath);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
archive_entry_set_size(entry, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
r = archive_write_header(cpio->archive, entry);
|
|
|
|
|
|
|
|
if (r != ARCHIVE_OK)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_warnc(archive_errno(cpio->archive),
|
2008-05-26 17:15:35 +00:00
|
|
|
"%s: %s",
|
2009-04-17 04:04:57 +00:00
|
|
|
srcpath,
|
2008-05-26 17:15:35 +00:00
|
|
|
archive_error_string(cpio->archive));
|
|
|
|
|
|
|
|
if (r == ARCHIVE_FATAL)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
if (r >= ARCHIVE_WARN && fd >= 0) {
|
|
|
|
bytes_read = read(fd, cpio->buff, cpio->buff_size);
|
|
|
|
while (bytes_read > 0) {
|
|
|
|
r = archive_write_data(cpio->archive,
|
|
|
|
cpio->buff, bytes_read);
|
|
|
|
if (r < 0)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, archive_errno(cpio->archive),
|
2010-05-08 16:47:33 +00:00
|
|
|
"%s", archive_error_string(cpio->archive));
|
2008-05-26 17:15:35 +00:00
|
|
|
if (r < bytes_read) {
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_warnc(0,
|
2008-05-26 17:15:35 +00:00
|
|
|
"Truncated write; file may have grown while being archived.");
|
|
|
|
}
|
|
|
|
bytes_read = read(fd, cpio->buff, cpio->buff_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-17 04:04:57 +00:00
|
|
|
fd = restore_time(cpio, entry, srcpath, fd);
|
2008-05-26 17:15:35 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (cpio->verbose)
|
|
|
|
fprintf(stderr,"\n");
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-04-17 04:04:57 +00:00
|
|
|
static int
|
2008-05-26 17:15:35 +00:00
|
|
|
restore_time(struct cpio *cpio, struct archive_entry *entry,
|
|
|
|
const char *name, int fd)
|
|
|
|
{
|
|
|
|
#ifndef HAVE_UTIMES
|
|
|
|
static int warned = 0;
|
|
|
|
|
|
|
|
(void)cpio; /* UNUSED */
|
|
|
|
(void)entry; /* UNUSED */
|
|
|
|
(void)name; /* UNUSED */
|
|
|
|
|
|
|
|
if (!warned)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_warnc(0, "Can't restore access times on this platform");
|
2008-05-26 17:15:35 +00:00
|
|
|
warned = 1;
|
2009-04-17 04:04:57 +00:00
|
|
|
return (fd);
|
|
|
|
#else
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
struct __timeval times[2];
|
2008-05-26 17:15:35 +00:00
|
|
|
#else
|
|
|
|
struct timeval times[2];
|
2009-04-17 04:04:57 +00:00
|
|
|
#endif
|
2008-05-26 17:15:35 +00:00
|
|
|
|
|
|
|
if (!cpio->option_atime_restore)
|
2009-04-17 04:04:57 +00:00
|
|
|
return (fd);
|
2008-05-26 17:15:35 +00:00
|
|
|
|
|
|
|
times[1].tv_sec = archive_entry_mtime(entry);
|
|
|
|
times[1].tv_usec = archive_entry_mtime_nsec(entry) / 1000;
|
|
|
|
|
|
|
|
times[0].tv_sec = archive_entry_atime(entry);
|
|
|
|
times[0].tv_usec = archive_entry_atime_nsec(entry) / 1000;
|
|
|
|
|
2010-05-08 16:47:33 +00:00
|
|
|
#if defined(HAVE_FUTIMES) && !defined(__CYGWIN__)
|
2008-05-26 17:15:35 +00:00
|
|
|
if (fd >= 0 && futimes(fd, times) == 0)
|
2009-04-17 04:04:57 +00:00
|
|
|
return (fd);
|
2008-05-26 17:15:35 +00:00
|
|
|
#endif
|
2009-04-17 04:04:57 +00:00
|
|
|
/*
|
|
|
|
* Some platform cannot restore access times if the file descriptor
|
|
|
|
* is still opened.
|
|
|
|
*/
|
|
|
|
if (fd >= 0) {
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
2008-05-26 17:15:35 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_LUTIMES
|
|
|
|
if (lutimes(name, times) != 0)
|
|
|
|
#else
|
2010-05-08 16:47:33 +00:00
|
|
|
if ((AE_IFLNK != archive_entry_filetype(entry))
|
|
|
|
&& utimes(name, times) != 0)
|
2008-05-26 17:15:35 +00:00
|
|
|
#endif
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_warnc(errno, "Can't update time for %s", name);
|
2008-05-26 17:15:35 +00:00
|
|
|
#endif
|
2009-04-17 04:04:57 +00:00
|
|
|
return (fd);
|
2008-05-26 17:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
mode_in(struct cpio *cpio)
|
|
|
|
{
|
|
|
|
struct archive *a;
|
|
|
|
struct archive_entry *entry;
|
|
|
|
struct archive *ext;
|
|
|
|
const char *destpath;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
ext = archive_write_disk_new();
|
|
|
|
if (ext == NULL)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "Couldn't allocate restore object");
|
2008-05-26 17:15:35 +00:00
|
|
|
r = archive_write_disk_set_options(ext, cpio->extract_flags);
|
|
|
|
if (r != ARCHIVE_OK)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "%s", archive_error_string(ext));
|
2008-05-26 17:15:35 +00:00
|
|
|
a = archive_read_new();
|
|
|
|
if (a == NULL)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "Couldn't allocate archive object");
|
2008-05-26 17:15:35 +00:00
|
|
|
archive_read_support_compression_all(a);
|
|
|
|
archive_read_support_format_all(a);
|
|
|
|
|
|
|
|
if (archive_read_open_file(a, cpio->filename, cpio->bytes_per_block))
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, archive_errno(a),
|
2010-05-08 16:47:33 +00:00
|
|
|
"%s", archive_error_string(a));
|
2008-05-26 17:15:35 +00:00
|
|
|
for (;;) {
|
|
|
|
r = archive_read_next_header(a, &entry);
|
|
|
|
if (r == ARCHIVE_EOF)
|
|
|
|
break;
|
|
|
|
if (r != ARCHIVE_OK) {
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, archive_errno(a),
|
2010-05-08 16:47:33 +00:00
|
|
|
"%s", archive_error_string(a));
|
2008-05-26 17:15:35 +00:00
|
|
|
}
|
2011-07-17 21:42:22 +00:00
|
|
|
if (lafe_excluded(cpio->matching, archive_entry_pathname(entry)))
|
2008-05-26 17:15:35 +00:00
|
|
|
continue;
|
|
|
|
if (cpio->option_rename) {
|
|
|
|
destpath = cpio_rename(archive_entry_pathname(entry));
|
|
|
|
archive_entry_set_pathname(entry, destpath);
|
|
|
|
} else
|
|
|
|
destpath = archive_entry_pathname(entry);
|
|
|
|
if (destpath == NULL)
|
|
|
|
continue;
|
|
|
|
if (cpio->verbose)
|
2010-05-08 16:47:33 +00:00
|
|
|
fprintf(stdout, "%s\n", destpath);
|
2008-05-26 17:15:35 +00:00
|
|
|
if (cpio->uid_override >= 0)
|
|
|
|
archive_entry_set_uid(entry, cpio->uid_override);
|
|
|
|
if (cpio->gid_override >= 0)
|
|
|
|
archive_entry_set_gid(entry, cpio->gid_override);
|
|
|
|
r = archive_write_header(ext, entry);
|
|
|
|
if (r != ARCHIVE_OK) {
|
|
|
|
fprintf(stderr, "%s: %s\n",
|
|
|
|
archive_entry_pathname(entry),
|
|
|
|
archive_error_string(ext));
|
|
|
|
} else if (archive_entry_size(entry) > 0) {
|
2010-05-08 16:47:33 +00:00
|
|
|
r = extract_data(a, ext);
|
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
cpio->return_value = 1;
|
2008-05-26 17:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
r = archive_read_close(a);
|
|
|
|
if (r != ARCHIVE_OK)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "%s", archive_error_string(a));
|
2008-05-26 17:15:35 +00:00
|
|
|
r = archive_write_close(ext);
|
|
|
|
if (r != ARCHIVE_OK)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "%s", archive_error_string(ext));
|
2008-05-26 17:15:35 +00:00
|
|
|
if (!cpio->quiet) {
|
2010-05-08 16:47:33 +00:00
|
|
|
int64_t blocks = (archive_position_uncompressed(a) + 511)
|
2008-05-26 17:15:35 +00:00
|
|
|
/ 512;
|
2010-05-08 16:47:33 +00:00
|
|
|
fprintf(stderr, "%lu %s\n", (unsigned long)blocks,
|
2008-05-26 17:15:35 +00:00
|
|
|
blocks == 1 ? "block" : "blocks");
|
|
|
|
}
|
|
|
|
archive_read_finish(a);
|
|
|
|
archive_write_finish(ext);
|
2010-05-08 16:47:33 +00:00
|
|
|
exit(cpio->return_value);
|
2008-05-26 17:15:35 +00:00
|
|
|
}
|
|
|
|
|
2010-05-08 16:47:33 +00:00
|
|
|
/*
|
|
|
|
* Exits if there's a fatal error. Returns ARCHIVE_OK
|
|
|
|
* if everything is kosher.
|
|
|
|
*/
|
2008-05-26 17:15:35 +00:00
|
|
|
static int
|
2010-05-08 16:47:33 +00:00
|
|
|
extract_data(struct archive *ar, struct archive *aw)
|
2008-05-26 17:15:35 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
size_t size;
|
|
|
|
const void *block;
|
|
|
|
off_t offset;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
r = archive_read_data_block(ar, &block, &size, &offset);
|
|
|
|
if (r == ARCHIVE_EOF)
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
if (r != ARCHIVE_OK) {
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_warnc(archive_errno(ar),
|
2008-05-26 17:15:35 +00:00
|
|
|
"%s", archive_error_string(ar));
|
2010-05-08 16:47:33 +00:00
|
|
|
exit(1);
|
2008-05-26 17:15:35 +00:00
|
|
|
}
|
|
|
|
r = archive_write_data_block(aw, block, size, offset);
|
|
|
|
if (r != ARCHIVE_OK) {
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_warnc(archive_errno(aw),
|
2010-05-08 16:47:33 +00:00
|
|
|
"%s", archive_error_string(aw));
|
2008-05-26 17:15:35 +00:00
|
|
|
return (r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mode_list(struct cpio *cpio)
|
|
|
|
{
|
|
|
|
struct archive *a;
|
|
|
|
struct archive_entry *entry;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
a = archive_read_new();
|
|
|
|
if (a == NULL)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "Couldn't allocate archive object");
|
2008-05-26 17:15:35 +00:00
|
|
|
archive_read_support_compression_all(a);
|
|
|
|
archive_read_support_format_all(a);
|
|
|
|
|
|
|
|
if (archive_read_open_file(a, cpio->filename, cpio->bytes_per_block))
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, archive_errno(a),
|
2010-05-08 16:47:33 +00:00
|
|
|
"%s", archive_error_string(a));
|
2008-05-26 17:15:35 +00:00
|
|
|
for (;;) {
|
|
|
|
r = archive_read_next_header(a, &entry);
|
|
|
|
if (r == ARCHIVE_EOF)
|
|
|
|
break;
|
|
|
|
if (r != ARCHIVE_OK) {
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, archive_errno(a),
|
2010-05-08 16:47:33 +00:00
|
|
|
"%s", archive_error_string(a));
|
2008-05-26 17:15:35 +00:00
|
|
|
}
|
2011-07-17 21:42:22 +00:00
|
|
|
if (lafe_excluded(cpio->matching, archive_entry_pathname(entry)))
|
2008-05-26 17:15:35 +00:00
|
|
|
continue;
|
2008-11-29 20:22:02 +00:00
|
|
|
if (cpio->verbose)
|
|
|
|
list_item_verbose(cpio, entry);
|
|
|
|
else
|
2008-05-26 17:15:35 +00:00
|
|
|
fprintf(stdout, "%s\n", archive_entry_pathname(entry));
|
|
|
|
}
|
|
|
|
r = archive_read_close(a);
|
|
|
|
if (r != ARCHIVE_OK)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "%s", archive_error_string(a));
|
2008-05-26 17:15:35 +00:00
|
|
|
if (!cpio->quiet) {
|
2010-05-08 16:47:33 +00:00
|
|
|
int64_t blocks = (archive_position_uncompressed(a) + 511)
|
2008-05-26 17:15:35 +00:00
|
|
|
/ 512;
|
2010-05-08 16:47:33 +00:00
|
|
|
fprintf(stderr, "%lu %s\n", (unsigned long)blocks,
|
2008-05-26 17:15:35 +00:00
|
|
|
blocks == 1 ? "block" : "blocks");
|
|
|
|
}
|
|
|
|
archive_read_finish(a);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2008-11-29 20:22:02 +00:00
|
|
|
/*
|
|
|
|
* 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];
|
2009-04-17 04:04:57 +00:00
|
|
|
char uids[16], gids[16];
|
2008-11-29 20:22:02 +00:00
|
|
|
const char *uname, *gname;
|
|
|
|
FILE *out = stdout;
|
|
|
|
const char *fmt;
|
2010-05-08 16:47:33 +00:00
|
|
|
time_t mtime;
|
2008-11-29 20:22:02 +00:00
|
|
|
static time_t now;
|
|
|
|
|
|
|
|
if (!now)
|
|
|
|
time(&now);
|
|
|
|
|
2009-04-17 04:04:57 +00:00
|
|
|
if (cpio->option_numeric_uid_gid) {
|
|
|
|
/* Format numeric uid/gid for display. */
|
2010-05-08 16:47:33 +00:00
|
|
|
strcpy(uids, cpio_i64toa(archive_entry_uid(entry)));
|
2009-04-17 04:04:57 +00:00
|
|
|
uname = uids;
|
2010-05-08 16:47:33 +00:00
|
|
|
strcpy(gids, cpio_i64toa(archive_entry_gid(entry)));
|
2009-04-17 04:04:57 +00:00
|
|
|
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));
|
|
|
|
}
|
2008-11-29 20:22:02 +00:00
|
|
|
|
|
|
|
/* Print device number or file size. */
|
2010-05-08 16:47:33 +00:00
|
|
|
if (archive_entry_filetype(entry) == AE_IFCHR
|
|
|
|
|| archive_entry_filetype(entry) == AE_IFBLK) {
|
2008-11-29 20:22:02 +00:00
|
|
|
snprintf(size, sizeof(size), "%lu,%lu",
|
2010-05-08 16:47:33 +00:00
|
|
|
(unsigned long)archive_entry_rdevmajor(entry),
|
|
|
|
(unsigned long)archive_entry_rdevminor(entry));
|
2008-11-29 20:22:02 +00:00
|
|
|
} else {
|
2010-05-08 16:47:33 +00:00
|
|
|
strcpy(size, cpio_i64toa(archive_entry_size(entry)));
|
2008-11-29 20:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Format the time using 'ls -l' conventions. */
|
2010-05-08 16:47:33 +00:00
|
|
|
mtime = archive_entry_mtime(entry);
|
2009-04-17 04:04:57 +00:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
/* Windows' strftime function does not support %e format. */
|
2010-05-08 16:47:33 +00:00
|
|
|
if (mtime - now > 365*86400/2
|
|
|
|
|| mtime - now < -365*86400/2)
|
2009-04-17 04:04:57 +00:00
|
|
|
fmt = cpio->day_first ? "%d %b %Y" : "%b %d %Y";
|
|
|
|
else
|
|
|
|
fmt = cpio->day_first ? "%d %b %H:%M" : "%b %d %H:%M";
|
|
|
|
#else
|
2010-05-08 16:47:33 +00:00
|
|
|
if (abs(mtime - now) > (365/2)*86400)
|
2008-11-29 20:22:02 +00:00
|
|
|
fmt = cpio->day_first ? "%e %b %Y" : "%b %e %Y";
|
|
|
|
else
|
|
|
|
fmt = cpio->day_first ? "%e %b %H:%M" : "%b %e %H:%M";
|
2009-04-17 04:04:57 +00:00
|
|
|
#endif
|
2010-05-08 16:47:33 +00:00
|
|
|
strftime(date, sizeof(date), fmt, localtime(&mtime));
|
2008-11-29 20:22:02 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2008-05-26 17:15:35 +00:00
|
|
|
static void
|
|
|
|
mode_pass(struct cpio *cpio, const char *destdir)
|
|
|
|
{
|
2011-07-17 21:42:22 +00:00
|
|
|
struct lafe_line_reader *lr;
|
2008-06-21 02:20:20 +00:00
|
|
|
const char *p;
|
2008-05-26 17:15:35 +00:00
|
|
|
int r;
|
|
|
|
|
|
|
|
/* Ensure target dir has a trailing '/' to simplify path surgery. */
|
|
|
|
cpio->destdir = malloc(strlen(destdir) + 8);
|
|
|
|
strcpy(cpio->destdir, destdir);
|
|
|
|
if (destdir[strlen(destdir) - 1] != '/')
|
|
|
|
strcat(cpio->destdir, "/");
|
|
|
|
|
|
|
|
cpio->archive = archive_write_disk_new();
|
|
|
|
if (cpio->archive == NULL)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "Failed to allocate archive object");
|
2008-05-26 17:15:35 +00:00
|
|
|
r = archive_write_disk_set_options(cpio->archive, cpio->extract_flags);
|
|
|
|
if (r != ARCHIVE_OK)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
|
2008-05-26 17:15:35 +00:00
|
|
|
cpio->linkresolver = archive_entry_linkresolver_new();
|
|
|
|
archive_write_disk_set_standard_lookup(cpio->archive);
|
2010-05-08 16:47:33 +00:00
|
|
|
|
|
|
|
cpio->archive_read_disk = archive_read_disk_new();
|
|
|
|
if (cpio->archive_read_disk == NULL)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "Failed to allocate archive object");
|
2010-05-08 16:47:33 +00:00
|
|
|
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);
|
|
|
|
|
2011-07-17 21:42:22 +00:00
|
|
|
lr = lafe_line_reader("-", cpio->option_null);
|
|
|
|
while ((p = lafe_line_reader_next(lr)) != NULL)
|
2008-06-21 02:20:20 +00:00
|
|
|
file_to_archive(cpio, p);
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_line_reader_free(lr);
|
2008-05-26 17:15:35 +00:00
|
|
|
|
|
|
|
archive_entry_linkresolver_free(cpio->linkresolver);
|
|
|
|
r = archive_write_close(cpio->archive);
|
|
|
|
if (r != ARCHIVE_OK)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
|
2008-08-24 06:21:00 +00:00
|
|
|
|
|
|
|
if (!cpio->quiet) {
|
2010-05-08 16:47:33 +00:00
|
|
|
int64_t blocks =
|
|
|
|
(archive_position_uncompressed(cpio->archive) + 511)
|
|
|
|
/ 512;
|
|
|
|
fprintf(stderr, "%lu %s\n", (unsigned long)blocks,
|
2008-08-24 06:21:00 +00:00
|
|
|
blocks == 1 ? "block" : "blocks");
|
|
|
|
}
|
|
|
|
|
2008-09-04 05:20:46 +00:00
|
|
|
archive_write_finish(cpio->archive);
|
2008-05-26 17:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prompt for a new name for this entry. Returns a pointer to the
|
|
|
|
* new name or NULL if the entry should not be copied. This
|
|
|
|
* implements the semantics defined in POSIX.1-1996, which specifies
|
|
|
|
* that an input of '.' means the name should be unchanged. GNU cpio
|
|
|
|
* treats '.' as a literal new name.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
cpio_rename(const char *name)
|
|
|
|
{
|
|
|
|
static char buff[1024];
|
|
|
|
FILE *t;
|
|
|
|
char *p, *ret;
|
|
|
|
|
|
|
|
t = fopen("/dev/tty", "r+");
|
|
|
|
if (t == NULL)
|
|
|
|
return (name);
|
|
|
|
fprintf(t, "%s (Enter/./(new name))? ", name);
|
|
|
|
fflush(t);
|
|
|
|
|
|
|
|
p = fgets(buff, sizeof(buff), t);
|
|
|
|
fclose(t);
|
|
|
|
if (p == NULL)
|
|
|
|
/* End-of-file is a blank line. */
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
++p;
|
|
|
|
if (*p == '\n' || *p == '\0')
|
|
|
|
/* Empty line. */
|
|
|
|
return (NULL);
|
|
|
|
if (*p == '.' && p[1] == '\n')
|
|
|
|
/* Single period preserves original name. */
|
|
|
|
return (name);
|
|
|
|
ret = p;
|
|
|
|
/* Trim the final newline. */
|
|
|
|
while (*p != '\0' && *p != '\n')
|
|
|
|
++p;
|
|
|
|
/* Overwrite the final \n with a null character. */
|
|
|
|
*p = '\0';
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2008-11-29 20:22:02 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup uname/gname from uid/gid, return NULL if no match.
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_errc(1, ENOMEM, "No more memory");
|
2008-11-29 20:22:02 +00:00
|
|
|
memset(*name_cache_variable, 0, sizeof(struct name_cache));
|
|
|
|
(*name_cache_variable)->size = name_cache_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
cache = *name_cache_variable;
|
|
|
|
cache->probes++;
|
|
|
|
|
|
|
|
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 (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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
lookup_uname(struct cpio *cpio, uid_t uid)
|
|
|
|
{
|
|
|
|
return (lookup_name(cpio, &cpio->uname_cache,
|
|
|
|
&lookup_uname_helper, (id_t)uid));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
lookup_uname_helper(struct cpio *cpio, const char **name, id_t id)
|
|
|
|
{
|
|
|
|
struct passwd *pwent;
|
|
|
|
|
|
|
|
(void)cpio; /* UNUSED */
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
pwent = getpwuid((uid_t)id);
|
|
|
|
if (pwent == NULL) {
|
|
|
|
*name = NULL;
|
2010-05-08 16:47:33 +00:00
|
|
|
if (errno != 0 && errno != ENOENT)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_warnc(errno, "getpwuid(%d) failed", id);
|
2008-11-29 20:22:02 +00:00
|
|
|
return (errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
*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)
|
2011-07-17 21:42:22 +00:00
|
|
|
lafe_warnc(errno, "getgrgid(%d) failed", id);
|
2008-11-29 20:22:02 +00:00
|
|
|
return (errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
*name = grent->gr_name;
|
|
|
|
return (0);
|
|
|
|
}
|
2010-05-08 16:47:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|