toybox/toys/posix/cpio.c
<<
>>
Prefs
   1/* cpio.c - a basic cpio
   2 *
   3 * Copyright 2013 Isaac Dunham <ibid.ag@gmail.com>
   4 * Copyright 2015 Frontier Silicon Ltd.
   5 *
   6 * see https://www.kernel.org/doc/Documentation/early-userspace/buffer-format.txt
   7 * and http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cpio.html
   8 * and http://pubs.opengroup.org/onlinepubs/7908799/xcu/cpio.html
   9 *
  10 * Yes, that's SUSv2, newer versions removed it, but RPM and initramfs use
  11 * this archive format. We implement (only) the modern "-H newc" variant which
  12 * expanded headers to 110 bytes (first field 6 bytes, rest are 8).
  13 * In order: magic ino mode uid gid nlink mtime filesize devmajor devminor
  14 * rdevmajor rdevminor namesize check
  15 * This is the equivalent of mode -H newc in other implementations.
  16 *
  17 * todo: export/import linux file list text format ala gen_initramfs_list.sh
  18
  19USE_CPIO(NEWTOY(cpio, "(ignore-devno)(renumber-inodes)(quiet)(no-preserve-owner)md(make-directories)uH:p|i|t|F:v(verbose)o|[!pio][!pot][!pF]", TOYFLAG_BIN))
  20
  21config CPIO
  22  bool "cpio"
  23  default y
  24  help
  25    usage: cpio -{o|t|i|p DEST} [-v] [--verbose] [-F FILE] [--no-preserve-owner]
  26           [ignored: -m -H newc]
  27
  28    Copy files into and out of a "newc" format cpio archive.
  29
  30    -F FILE     Use archive FILE instead of stdin/stdout
  31    -p DEST     Copy-pass mode, copy stdin file list to directory DEST
  32    -i  Extract from archive into file system (stdin=archive)
  33    -o  Create archive (stdin=list of files, stdout=archive)
  34    -t  Test files (list only, stdin=archive, stdout=list of files)
  35    -d  Create directories if needed
  36    -u  unlink existing files when extracting
  37    -v  Verbose
  38    --no-preserve-owner (don't set ownership during extract)
  39*/
  40
  41#define FOR_cpio
  42#include "toys.h"
  43
  44GLOBALS(
  45  char *F, *H;
  46)
  47
  48// Read strings, tail padded to 4 byte alignment. Argument "align" is amount
  49// by which start of string isn't aligned (usually 0, but header is 110 bytes
  50// which is 2 bytes off because the first field wasn't expanded from 6 to 8).
  51static char *strpad(int fd, unsigned len, unsigned align)
  52{
  53  char *str;
  54
  55  align = (align + len) & 3;
  56  if (align) len += (4-align);
  57  xreadall(fd, str = xmalloc(len+1), len);
  58  str[len]=0; // redundant, in case archive is bad
  59
  60  return str;
  61}
  62
  63//convert hex to uint; mostly to allow using bits of non-terminated strings
  64static unsigned x8u(char *hex)
  65{
  66  unsigned val, inpos = 8, outpos;
  67  char pattern[6];
  68
  69  while (*hex == '0') {
  70    hex++;
  71    if (!--inpos) return 0;
  72  }
  73  // Because scanf gratuitously treats %*X differently than printf does.
  74  sprintf(pattern, "%%%dX%%n", inpos);
  75  sscanf(hex, pattern, &val, &outpos);
  76  if (inpos != outpos) error_exit("bad hex");
  77
  78  return val;
  79}
  80
  81void cpio_main(void)
  82{
  83  // Subtle bit: FLAG_o is 1 so we can just use it to select stdin/stdout.
  84  int pipe, afd = FLAG(o), empty = 1;
  85  pid_t pid = 0;
  86
  87  // In passthrough mode, parent stays in original dir and generates archive
  88  // to pipe, child does chdir to new dir and reads archive from stdin (pipe).
  89  if (FLAG(p)) {
  90    if (FLAG(d)) {
  91      if (!*toys.optargs) error_exit("need directory for -p");
  92      if (mkdir(*toys.optargs, 0700) == -1 && errno != EEXIST)
  93        perror_exit("mkdir %s", *toys.optargs);
  94    }
  95    if (toys.stacktop) {
  96      // xpopen() doesn't return from child due to vfork(), instead restarts
  97      // with !toys.stacktop
  98      pid = xpopen(0, &pipe, 0);
  99      afd = pipe;
 100    } else {
 101      // child
 102      toys.optflags |= FLAG_i;
 103      xchdir(*toys.optargs);
 104    }
 105  }
 106
 107  if (TT.F) {
 108    int perm = FLAG(o) ? O_CREAT|O_WRONLY|O_TRUNC : O_RDONLY;
 109
 110    afd = xcreate(TT.F, perm, 0644);
 111  }
 112
 113  // read cpio archive
 114
 115  if (FLAG(i) || FLAG(t)) for (;; empty = 0) {
 116    char *name, *tofree, *data;
 117    unsigned mode, uid, gid, timestamp;
 118    int test = FLAG(t), err = 0, size = 0, len;
 119
 120    // read header, skipping arbitrary leading NUL bytes (concatenated archives)
 121    for (;;) {
 122      if (1>(len = readall(afd, toybuf+size, 110-size))) break;
 123      if (size || *toybuf) {
 124        size += len;
 125        break;
 126      }
 127      for (size = 0; size<len; size++) if (toybuf[size]) break;
 128      memmove(toybuf, toybuf+size, len-size);
 129      size = len-size;
 130    }
 131    if (!size) {
 132      if (empty) error_exit("empty archive");
 133      else break;
 134    }
 135    if (size != 110 || memcmp(toybuf, "070701", 6)) error_exit("bad header");
 136    tofree = name = strpad(afd, x8u(toybuf+94), 110);
 137    if (!strcmp("TRAILER!!!", name)) {
 138      free(tofree);
 139      continue;
 140    }
 141
 142    // If you want to extract absolute paths, "cd /" and run cpio.
 143    while (*name == '/') name++;
 144    // TODO: remove .. entries
 145
 146    size = x8u(toybuf+54);
 147    mode = x8u(toybuf+14);
 148    uid = x8u(toybuf+22);
 149    gid = x8u(toybuf+30);
 150    timestamp = x8u(toybuf+46); // unsigned 32 bit, so year 2100 problem
 151
 152    // (This output is unaffected by --quiet.)
 153    if (FLAG(t) || FLAG(v)) puts(name);
 154
 155    if (FLAG(u) && !test) if (unlink(name) && errno == EISDIR) rmdir(name);
 156
 157    if (!test && FLAG(d) && strrchr(name, '/') && mkpath(name)) {
 158      perror_msg("mkpath '%s'", name);
 159      test++;
 160    }
 161
 162    // Consume entire record even if it couldn't create file, so we're
 163    // properly aligned with next file.
 164
 165    if (S_ISDIR(mode)) {
 166      if (!test) err = mkdir(name, mode) && !FLAG(u);
 167    } else if (S_ISLNK(mode)) {
 168      data = strpad(afd, size, 0);
 169      if (!test) {
 170        err = symlink(data, name);
 171        // Can't get a filehandle to a symlink, so do special chown
 172        if (!err && !geteuid() && !FLAG(no_preserve_owner))
 173          err = lchown(name, uid, gid);
 174      }
 175      free(data);
 176    } else if (S_ISREG(mode)) {
 177      int fd = test ? 0 : open(name, O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, mode);
 178
 179      // If write fails, we still need to read/discard data to continue with
 180      // archive. Since doing so overwrites errno, report error now
 181      if (fd < 0) {
 182        perror_msg("create %s", name);
 183        test++;
 184      }
 185
 186      data = toybuf;
 187      while (size) {
 188        if (size < sizeof(toybuf)) data = strpad(afd, size, 0);
 189        else xreadall(afd, toybuf, sizeof(toybuf));
 190        if (!test) xwrite(fd, data, data == toybuf ? sizeof(toybuf) : size);
 191        if (data != toybuf) {
 192          free(data);
 193          break;
 194        }
 195        size -= sizeof(toybuf);
 196      }
 197
 198      if (!test) {
 199        // set owner, restore dropped suid bit
 200        if (!geteuid() && !FLAG(no_preserve_owner)) {
 201          err = fchown(fd, uid, gid);
 202          if (!err) err = fchmod(fd, mode);
 203        }
 204        close(fd);
 205      }
 206    } else if (!test)
 207      err = mknod(name, mode, dev_makedev(x8u(toybuf+78), x8u(toybuf+86)));
 208
 209    // Set ownership and timestamp.
 210    if (!test && !err) {
 211      // Creading dir/dev doesn't give us a filehandle, we have to refer to it
 212      // by name to chown/utime, but how do we know it's the same item?
 213      // Check that we at least have the right type of entity open, and do
 214      // NOT restore dropped suid bit in this case.
 215      if (!S_ISREG(mode) && !S_ISLNK(mode) && !geteuid()
 216          && !FLAG(no_preserve_owner))
 217      {
 218        int fd = open(name, O_RDONLY|O_NOFOLLOW);
 219        struct stat st;
 220
 221        if (fd != -1 && !fstat(fd, &st) && (st.st_mode&S_IFMT) == (mode&S_IFMT))
 222          err = fchown(fd, uid, gid);
 223        else err = 1;
 224
 225        close(fd);
 226      }
 227
 228      // set timestamp
 229      if (!err) {
 230        struct timespec times[2];
 231
 232        memset(times, 0, sizeof(struct timespec)*2);
 233        times[0].tv_sec = times[1].tv_sec = timestamp;
 234        err = utimensat(AT_FDCWD, name, times, AT_SYMLINK_NOFOLLOW);
 235      }
 236    }
 237
 238    if (err) perror_msg_raw(name);
 239    free(tofree);
 240
 241  // Output cpio archive
 242
 243  } else {
 244    char *name = 0;
 245    size_t size = 0;
 246    unsigned inode = 0;
 247
 248    for (;;) {
 249      struct stat st;
 250      unsigned nlen, error = 0, zero = 0;
 251      int len, fd = -1;
 252      char *link = 0;
 253      ssize_t llen;
 254
 255      len = getline(&name, &size, stdin);
 256      if (len<1) break;
 257      if (name[len-1] == '\n') name[--len] = 0;
 258      nlen = len+1;
 259      if (lstat(name, &st) || (S_ISREG(st.st_mode)
 260          && st.st_size && (fd = open(name, O_RDONLY))<0)
 261          || (S_ISLNK(st.st_mode) && !(link = xreadlink(name))))
 262      {
 263        perror_msg_raw(name);
 264        continue;
 265      }
 266      // encrypted filesystems can stat the wrong link size
 267      if (link) st.st_size = strlen(link);
 268
 269      if (FLAG(no_preserve_owner)) st.st_uid = st.st_gid = 0;
 270      if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) st.st_size = 0;
 271      if (st.st_size >> 32) perror_msg("skipping >2G file '%s'", name);
 272      else {
 273        if (FLAG(renumber_inodes)) st.st_ino = ++inode;
 274        if (FLAG(ignore_devno)) st.st_rdev = 0;
 275        llen = sprintf(toybuf,
 276          "070701%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
 277          (int)st.st_ino, st.st_mode, st.st_uid, st.st_gid, (int)st.st_nlink,
 278          (int)st.st_mtime, (int)st.st_size, dev_major(st.st_dev),
 279          dev_minor(st.st_dev), dev_major(st.st_rdev), dev_minor(st.st_rdev),
 280          nlen, 0);
 281        xwrite(afd, toybuf, llen);
 282        xwrite(afd, name, nlen);
 283
 284        // NUL Pad header up to 4 multiple bytes.
 285        llen = (llen + nlen) & 3;
 286        if (llen) xwrite(afd, &zero, 4-llen);
 287
 288        // Write out body for symlink or regular file
 289        if (link) xwrite(afd, link, st.st_size);
 290        else for (llen = st.st_size; llen; llen -= nlen) {
 291          nlen = llen > sizeof(toybuf) ? sizeof(toybuf) : llen;
 292          // If read fails, write anyway (already wrote size in header)
 293          if (nlen != readall(fd, toybuf, nlen))
 294            if (!error++) perror_msg("bad read from file '%s'", name);
 295          xwrite(afd, toybuf, nlen);
 296        }
 297        llen = st.st_size & 3;
 298        if (llen) xwrite(afd, &zero, 4-llen);
 299      }
 300      free(link);
 301      xclose(fd);
 302    }
 303    if (CFG_TOYBOX_FREE) free(name);
 304
 305    // nlink=1, namesize=11, with padding
 306    dprintf(afd, "070701%040X%056X%08XTRAILER!!!%c%c%c%c", 1, 11, 0, 0, 0, 0,0);
 307  }
 308  if (TT.F) xclose(afd);
 309
 310  if (FLAG(p) && pid) toys.exitval |= xpclose(pid, pipe);
 311}
 312