cpio.c 9.8 KB

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