/* cpio.c - a basic cpio * * Copyright 2013 Isaac Dunham * Copyright 2015 Frontier Silicon Ltd. * * see https://www.kernel.org/doc/Documentation/early-userspace/buffer-format.txt * and http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cpio.html * and http://pubs.opengroup.org/onlinepubs/7908799/xcu/cpio.html * * Yes, that's SUSv2, newer versions removed it, but RPM and initramfs use * this archive format. We implement (only) the modern "-H newc" variant which * expanded headers to 110 bytes (first field 6 bytes, rest are 8). * In order: magic ino mode uid gid nlink mtime filesize devmajor devminor * rdevmajor rdevminor namesize check * This is the equivalent of mode -H newc in other implementations. * * todo: export/import linux file list text format ala gen_initramfs_list.sh USE_CPIO(NEWTOY(cpio, "(quiet)(no-preserve-owner)md(make-directories)uH:p|i|t|F:v(verbose)o|[!pio][!pot][!pF]", TOYFLAG_BIN)) config CPIO bool "cpio" default y help usage: cpio -{o|t|i|p DEST} [-v] [--verbose] [-F FILE] [--no-preserve-owner] [ignored: -m -H newc] Copy files into and out of a "newc" format cpio archive. -F FILE Use archive FILE instead of stdin/stdout -p DEST Copy-pass mode, copy stdin file list to directory DEST -i Extract from archive into file system (stdin=archive) -o Create archive (stdin=list of files, stdout=archive) -t Test files (list only, stdin=archive, stdout=list of files) -d Create directories if needed -u unlink existing files when extracting -v Verbose --no-preserve-owner (don't set ownership during extract) */ #define FOR_cpio #include "toys.h" GLOBALS( char *F, *H; ) // Read strings, tail padded to 4 byte alignment. Argument "align" is amount // by which start of string isn't aligned (usually 0, but header is 110 bytes // which is 2 bytes off because the first field wasn't expanded from 6 to 8). static char *strpad(int fd, unsigned len, unsigned align) { char *str; align = (align + len) & 3; if (align) len += (4-align); xreadall(fd, str = xmalloc(len+1), len); str[len]=0; // redundant, in case archive is bad return str; } //convert hex to uint; mostly to allow using bits of non-terminated strings static unsigned x8u(char *hex) { unsigned val, inpos = 8, outpos; char pattern[6]; while (*hex == '0') { hex++; if (!--inpos) return 0; } // Because scanf gratuitously treats %*X differently than printf does. sprintf(pattern, "%%%dX%%n", inpos); sscanf(hex, pattern, &val, &outpos); if (inpos != outpos) error_exit("bad hex"); return val; } void cpio_main(void) { // Subtle bit: FLAG_o is 1 so we can just use it to select stdin/stdout. int pipe, afd = FLAG(o), empty = 1; pid_t pid = 0; // In passthrough mode, parent stays in original dir and generates archive // to pipe, child does chdir to new dir and reads archive from stdin (pipe). if (FLAG(p)) { if (FLAG(d)) { if (!*toys.optargs) error_exit("need directory for -p"); if (mkdir(*toys.optargs, 0700) == -1 && errno != EEXIST) perror_exit("mkdir %s", *toys.optargs); } if (toys.stacktop) { // xpopen() doesn't return from child due to vfork(), instead restarts // with !toys.stacktop pid = xpopen(0, &pipe, 0); afd = pipe; } else { // child toys.optflags |= FLAG_i; xchdir(*toys.optargs); } } if (TT.F) { int perm = FLAG(o) ? O_CREAT|O_WRONLY|O_TRUNC : O_RDONLY; afd = xcreate(TT.F, perm, 0644); } // read cpio archive if (FLAG(i) || FLAG(t)) for (;; empty = 0) { char *name, *tofree, *data; unsigned mode, uid, gid, timestamp; int test = FLAG(t), err = 0, size = 0, len; // read header, skipping arbitrary leading NUL bytes (concatenated archives) for (;;) { if (1>(len = readall(afd, toybuf+size, 110-size))) break; if (size || *toybuf) { size += len; break; } for (size = 0; size> 32) perror_msg("skipping >2G file '%s'", name); else { llen = sprintf(toybuf, "070701%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X", (int)st.st_ino, st.st_mode, st.st_uid, st.st_gid, (int)st.st_nlink, (int)st.st_mtime, (int)st.st_size, dev_major(st.st_dev), dev_minor(st.st_dev), dev_major(st.st_rdev), dev_minor(st.st_rdev), nlen, 0); xwrite(afd, toybuf, llen); xwrite(afd, name, nlen); // NUL Pad header up to 4 multiple bytes. llen = (llen + nlen) & 3; if (llen) xwrite(afd, &zero, 4-llen); // Write out body for symlink or regular file if (link) xwrite(afd, link, st.st_size); else for (llen = st.st_size; llen; llen -= nlen) { nlen = llen > sizeof(toybuf) ? sizeof(toybuf) : llen; // If read fails, write anyway (already wrote size in header) if (nlen != readall(fd, toybuf, nlen)) if (!error++) perror_msg("bad read from file '%s'", name); xwrite(afd, toybuf, nlen); } llen = st.st_size & 3; if (llen) xwrite(afd, &zero, 4-llen); } free(link); xclose(fd); } if (CFG_TOYBOX_FREE) free(name); // nlink=1, namesize=11, with padding dprintf(afd, "070701%040X%056X%08XTRAILER!!!%c%c%c%c", 1, 11, 0, 0, 0, 0,0); } if (TT.F) xclose(afd); if (FLAG(p) && pid) toys.exitval |= xpclose(pid, pipe); }