2
0

cpio.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer
  10. * in this position and unchanged.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "cpio_platform.h"
  27. __FBSDID("$FreeBSD: src/usr.bin/cpio/cpio.c,v 1.15 2008/12/06 07:30:40 kientzle Exp $");
  28. #include <sys/types.h>
  29. #include <archive.h>
  30. #include <archive_entry.h>
  31. #ifdef HAVE_SYS_MKDEV_H
  32. #include <sys/mkdev.h>
  33. #endif
  34. #ifdef HAVE_SYS_STAT_H
  35. #include <sys/stat.h>
  36. #endif
  37. #ifdef HAVE_SYS_TIME_H
  38. #include <sys/time.h>
  39. #endif
  40. #ifdef HAVE_ERRNO_H
  41. #include <errno.h>
  42. #endif
  43. #ifdef HAVE_FCNTL_H
  44. #include <fcntl.h>
  45. #endif
  46. #ifdef HAVE_GRP_H
  47. #include <grp.h>
  48. #endif
  49. #ifdef HAVE_LOCALE_H
  50. #include <locale.h>
  51. #endif
  52. #ifdef HAVE_PWD_H
  53. #include <pwd.h>
  54. #endif
  55. #ifdef HAVE_SIGNAL_H
  56. #include <signal.h>
  57. #endif
  58. #ifdef HAVE_STDARG_H
  59. #include <stdarg.h>
  60. #endif
  61. #ifdef HAVE_STDINT_H
  62. #include <stdint.h>
  63. #endif
  64. #include <stdio.h>
  65. #ifdef HAVE_STDLIB_H
  66. #include <stdlib.h>
  67. #endif
  68. #ifdef HAVE_STRING_H
  69. #include <string.h>
  70. #endif
  71. #ifdef HAVE_UNISTD_H
  72. #include <unistd.h>
  73. #endif
  74. #ifdef HAVE_TIME_H
  75. #include <time.h>
  76. #endif
  77. #include "cpio.h"
  78. #include "err.h"
  79. #include "line_reader.h"
  80. #include "passphrase.h"
  81. /* Fixed size of uname/gname caches. */
  82. #define name_cache_size 101
  83. #ifndef O_BINARY
  84. #define O_BINARY 0
  85. #endif
  86. struct name_cache {
  87. int probes;
  88. int hits;
  89. size_t size;
  90. struct {
  91. id_t id;
  92. char *name;
  93. } cache[name_cache_size];
  94. };
  95. static int extract_data(struct archive *, struct archive *);
  96. const char * cpio_i64toa(int64_t);
  97. static const char *cpio_rename(const char *name);
  98. static int entry_to_archive(struct cpio *, struct archive_entry *);
  99. static int file_to_archive(struct cpio *, const char *);
  100. static void free_cache(struct name_cache *cache);
  101. static void list_item_verbose(struct cpio *, struct archive_entry *);
  102. static void long_help(void) __LA_DEAD;
  103. static const char *lookup_gname(struct cpio *, gid_t gid);
  104. static int lookup_gname_helper(struct cpio *,
  105. const char **name, id_t gid);
  106. static const char *lookup_uname(struct cpio *, uid_t uid);
  107. static int lookup_uname_helper(struct cpio *,
  108. const char **name, id_t uid);
  109. static void mode_in(struct cpio *) __LA_DEAD;
  110. static void mode_list(struct cpio *) __LA_DEAD;
  111. static void mode_out(struct cpio *);
  112. static void mode_pass(struct cpio *, const char *);
  113. static const char *remove_leading_slash(const char *);
  114. static int restore_time(struct cpio *, struct archive_entry *,
  115. const char *, int fd);
  116. static void usage(void) __LA_DEAD;
  117. static void version(void) __LA_DEAD;
  118. static const char * passphrase_callback(struct archive *, void *);
  119. static void passphrase_free(char *);
  120. int
  121. main(int argc, char *argv[])
  122. {
  123. static char buff[16384];
  124. struct cpio _cpio; /* Allocated on stack. */
  125. struct cpio *cpio;
  126. const char *errmsg;
  127. char *tptr;
  128. int uid, gid;
  129. int opt, t;
  130. cpio = &_cpio;
  131. memset(cpio, 0, sizeof(*cpio));
  132. cpio->buff = buff;
  133. cpio->buff_size = sizeof(buff);
  134. #if defined(HAVE_SIGACTION) && defined(SIGPIPE)
  135. { /* Ignore SIGPIPE signals. */
  136. struct sigaction sa;
  137. sigemptyset(&sa.sa_mask);
  138. sa.sa_flags = 0;
  139. sa.sa_handler = SIG_IGN;
  140. sigaction(SIGPIPE, &sa, NULL);
  141. }
  142. #endif
  143. /* Set lafe_progname before calling lafe_warnc. */
  144. lafe_setprogname(*argv, "bsdcpio");
  145. #if HAVE_SETLOCALE
  146. if (setlocale(LC_ALL, "") == NULL)
  147. lafe_warnc(0, "Failed to set default locale");
  148. #endif
  149. cpio->uid_override = -1;
  150. cpio->gid_override = -1;
  151. cpio->argv = argv;
  152. cpio->argc = argc;
  153. cpio->mode = '\0';
  154. cpio->verbose = 0;
  155. cpio->compress = '\0';
  156. cpio->extract_flags = ARCHIVE_EXTRACT_NO_AUTODIR;
  157. cpio->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
  158. cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_SYMLINKS;
  159. cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_NODOTDOT;
  160. cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
  161. cpio->extract_flags |= ARCHIVE_EXTRACT_PERM;
  162. cpio->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
  163. cpio->extract_flags |= ARCHIVE_EXTRACT_ACL;
  164. #if !defined(_WIN32) && !defined(__CYGWIN__)
  165. if (geteuid() == 0)
  166. cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER;
  167. #endif
  168. cpio->bytes_per_block = 512;
  169. cpio->filename = NULL;
  170. cpio->matching = archive_match_new();
  171. if (cpio->matching == NULL)
  172. lafe_errc(1, 0, "Out of memory");
  173. while ((opt = cpio_getopt(cpio)) != -1) {
  174. switch (opt) {
  175. case '0': /* GNU convention: --null, -0 */
  176. cpio->option_null = 1;
  177. break;
  178. case '6': /* in/out: assume/create 6th edition (PWB) format */
  179. cpio->option_pwb = 1;
  180. break;
  181. case '7': /* out: create archive using 7th Edition binary format */
  182. cpio->format = "bin";
  183. break;
  184. case 'A': /* NetBSD/OpenBSD */
  185. cpio->option_append = 1;
  186. break;
  187. case 'a': /* POSIX 1997 */
  188. cpio->option_atime_restore = 1;
  189. break;
  190. case 'B': /* POSIX 1997 */
  191. cpio->bytes_per_block = 5120;
  192. break;
  193. case OPTION_B64ENCODE:
  194. cpio->add_filter = opt;
  195. break;
  196. case 'C': /* NetBSD/OpenBSD */
  197. errno = 0;
  198. tptr = NULL;
  199. t = (int)strtol(cpio->argument, &tptr, 10);
  200. if (errno || t <= 0 || *(cpio->argument) == '\0' ||
  201. tptr == NULL || *tptr != '\0') {
  202. lafe_errc(1, 0, "Invalid blocksize: %s",
  203. cpio->argument);
  204. }
  205. cpio->bytes_per_block = t;
  206. break;
  207. case 'c': /* POSIX 1997 */
  208. cpio->format = "odc";
  209. break;
  210. case 'd': /* POSIX 1997 */
  211. cpio->extract_flags &= ~ARCHIVE_EXTRACT_NO_AUTODIR;
  212. break;
  213. case 'E': /* NetBSD/OpenBSD */
  214. if (archive_match_include_pattern_from_file(
  215. cpio->matching, cpio->argument,
  216. cpio->option_null) != ARCHIVE_OK)
  217. lafe_errc(1, 0, "Error : %s",
  218. archive_error_string(cpio->matching));
  219. break;
  220. case 'F': /* NetBSD/OpenBSD/GNU cpio */
  221. cpio->filename = cpio->argument;
  222. break;
  223. case 'f': /* POSIX 1997 */
  224. if (archive_match_exclude_pattern(cpio->matching,
  225. cpio->argument) != ARCHIVE_OK)
  226. lafe_errc(1, 0, "Error : %s",
  227. archive_error_string(cpio->matching));
  228. break;
  229. case OPTION_GRZIP:
  230. cpio->compress = opt;
  231. break;
  232. case 'H': /* GNU cpio (also --format) */
  233. cpio->format = cpio->argument;
  234. break;
  235. case 'h':
  236. long_help();
  237. break;
  238. case 'I': /* NetBSD/OpenBSD */
  239. cpio->filename = cpio->argument;
  240. break;
  241. case 'i': /* POSIX 1997 */
  242. if (cpio->mode != '\0')
  243. lafe_errc(1, 0,
  244. "Cannot use both -i and -%c", cpio->mode);
  245. cpio->mode = opt;
  246. break;
  247. case 'J': /* GNU tar, others */
  248. cpio->compress = opt;
  249. break;
  250. case 'j': /* GNU tar, others */
  251. cpio->compress = opt;
  252. break;
  253. case OPTION_INSECURE:
  254. cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_SYMLINKS;
  255. cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT;
  256. cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
  257. break;
  258. case 'L': /* GNU cpio */
  259. cpio->option_follow_links = 1;
  260. break;
  261. case 'l': /* POSIX 1997 */
  262. cpio->option_link = 1;
  263. break;
  264. case OPTION_LRZIP:
  265. case OPTION_LZ4:
  266. case OPTION_LZMA: /* GNU tar, others */
  267. case OPTION_LZOP: /* GNU tar, others */
  268. case OPTION_ZSTD:
  269. cpio->compress = opt;
  270. break;
  271. case 'm': /* POSIX 1997 */
  272. cpio->extract_flags |= ARCHIVE_EXTRACT_TIME;
  273. break;
  274. case 'n': /* GNU cpio */
  275. cpio->option_numeric_uid_gid = 1;
  276. break;
  277. case OPTION_NO_PRESERVE_OWNER: /* GNU cpio */
  278. cpio->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
  279. break;
  280. case 'O': /* GNU cpio */
  281. cpio->filename = cpio->argument;
  282. break;
  283. case 'o': /* POSIX 1997 */
  284. if (cpio->mode != '\0')
  285. lafe_errc(1, 0,
  286. "Cannot use both -o and -%c", cpio->mode);
  287. cpio->mode = opt;
  288. break;
  289. case 'p': /* POSIX 1997 */
  290. if (cpio->mode != '\0')
  291. lafe_errc(1, 0,
  292. "Cannot use both -p and -%c", cpio->mode);
  293. cpio->mode = opt;
  294. cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT;
  295. cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
  296. break;
  297. case OPTION_PASSPHRASE:
  298. cpio->passphrase = cpio->argument;
  299. break;
  300. case OPTION_PRESERVE_OWNER:
  301. cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER;
  302. break;
  303. case OPTION_QUIET: /* GNU cpio */
  304. cpio->quiet = 1;
  305. break;
  306. case 'R': /* GNU cpio, also --owner */
  307. /* TODO: owner_parse should return uname/gname
  308. * also; use that to set [ug]name_override. */
  309. errmsg = owner_parse(cpio->argument, &uid, &gid);
  310. if (errmsg) {
  311. lafe_warnc(-1, "%s", errmsg);
  312. usage();
  313. }
  314. if (uid != -1) {
  315. cpio->uid_override = uid;
  316. cpio->uname_override = NULL;
  317. }
  318. if (gid != -1) {
  319. cpio->gid_override = gid;
  320. cpio->gname_override = NULL;
  321. }
  322. break;
  323. case 'r': /* POSIX 1997 */
  324. cpio->option_rename = 1;
  325. break;
  326. case 't': /* POSIX 1997 */
  327. cpio->option_list = 1;
  328. break;
  329. case 'u': /* POSIX 1997 */
  330. cpio->extract_flags
  331. &= ~ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
  332. break;
  333. case OPTION_UUENCODE:
  334. cpio->add_filter = opt;
  335. break;
  336. case 'v': /* POSIX 1997 */
  337. cpio->verbose++;
  338. break;
  339. case 'V': /* GNU cpio */
  340. cpio->dot++;
  341. break;
  342. case OPTION_VERSION: /* GNU convention */
  343. version();
  344. break;
  345. #if 0
  346. /*
  347. * cpio_getopt() handles -W specially, so it's not
  348. * available here.
  349. */
  350. case 'W': /* Obscure, but useful GNU convention. */
  351. break;
  352. #endif
  353. case 'y': /* tar convention */
  354. cpio->compress = opt;
  355. break;
  356. case 'Z': /* tar convention */
  357. cpio->compress = opt;
  358. break;
  359. case 'z': /* tar convention */
  360. cpio->compress = opt;
  361. break;
  362. default:
  363. usage();
  364. }
  365. }
  366. /*
  367. * Sanity-check args, error out on nonsensical combinations.
  368. */
  369. /* -t implies -i if no mode was specified. */
  370. if (cpio->option_list && cpio->mode == '\0')
  371. cpio->mode = 'i';
  372. /* -t requires -i */
  373. if (cpio->option_list && cpio->mode != 'i')
  374. lafe_errc(1, 0, "Option -t requires -i");
  375. /* -n requires -it */
  376. if (cpio->option_numeric_uid_gid && !cpio->option_list)
  377. lafe_errc(1, 0, "Option -n requires -it");
  378. /* Can only specify format when writing */
  379. if (cpio->format != NULL && cpio->mode != 'o')
  380. lafe_errc(1, 0, "Option --format requires -o");
  381. /* -l requires -p */
  382. if (cpio->option_link && cpio->mode != 'p')
  383. lafe_errc(1, 0, "Option -l requires -p");
  384. /* -v overrides -V */
  385. if (cpio->dot && cpio->verbose)
  386. cpio->dot = 0;
  387. /* TODO: Flag other nonsensical combinations. */
  388. switch (cpio->mode) {
  389. case 'o':
  390. if (cpio->format == NULL) {
  391. if (cpio->option_pwb)
  392. cpio->format = "pwb";
  393. else
  394. cpio->format = "cpio";
  395. }
  396. mode_out(cpio);
  397. break;
  398. case 'i':
  399. while (*cpio->argv != NULL) {
  400. if (archive_match_include_pattern(cpio->matching,
  401. *cpio->argv) != ARCHIVE_OK)
  402. lafe_errc(1, 0, "Error : %s",
  403. archive_error_string(cpio->matching));
  404. --cpio->argc;
  405. ++cpio->argv;
  406. }
  407. if (cpio->option_list)
  408. mode_list(cpio);
  409. else
  410. mode_in(cpio);
  411. break;
  412. case 'p':
  413. if (*cpio->argv == NULL || **cpio->argv == '\0')
  414. lafe_errc(1, 0,
  415. "-p mode requires a target directory");
  416. mode_pass(cpio, *cpio->argv);
  417. break;
  418. default:
  419. lafe_errc(1, 0,
  420. "Must specify at least one of -i, -o, or -p");
  421. }
  422. archive_match_free(cpio->matching);
  423. free_cache(cpio->gname_cache);
  424. free_cache(cpio->uname_cache);
  425. archive_read_close(cpio->archive_read_disk);
  426. archive_read_free(cpio->archive_read_disk);
  427. free(cpio->destdir);
  428. passphrase_free(cpio->ppbuff);
  429. return (cpio->return_value);
  430. }
  431. static void
  432. usage(void)
  433. {
  434. const char *p;
  435. p = lafe_getprogname();
  436. fprintf(stderr, "Brief Usage:\n");
  437. fprintf(stderr, " List: %s -it < archive\n", p);
  438. fprintf(stderr, " Extract: %s -i < archive\n", p);
  439. fprintf(stderr, " Create: %s -o < filenames > archive\n", p);
  440. fprintf(stderr, " Help: %s --help\n", p);
  441. exit(1);
  442. }
  443. static const char *long_help_msg =
  444. "First option must be a mode specifier:\n"
  445. " -i Input -o Output -p Pass\n"
  446. "Common Options:\n"
  447. " -v Verbose filenames -V one dot per file\n"
  448. "Create: %p -o [options] < [list of files] > [archive]\n"
  449. " -J,-y,-z,--lzma Compress archive with xz/bzip2/gzip/lzma\n"
  450. " --format {pwb|bin|odc|newc|ustar} Select archive format\n"
  451. "List: %p -it < [archive]\n"
  452. "Extract: %p -i [options] < [archive]\n";
  453. /*
  454. * Note that the word 'bsdcpio' will always appear in the first line
  455. * of output.
  456. *
  457. * In particular, /bin/sh scripts that need to test for the presence
  458. * of bsdcpio can use the following template:
  459. *
  460. * if (cpio --help 2>&1 | grep bsdcpio >/dev/null 2>&1 ) then \
  461. * echo bsdcpio; else echo not bsdcpio; fi
  462. */
  463. static void
  464. long_help(void)
  465. {
  466. const char *prog;
  467. const char *p;
  468. prog = lafe_getprogname();
  469. fflush(stderr);
  470. p = (strcmp(prog,"bsdcpio") != 0) ? "(bsdcpio)" : "";
  471. printf("%s%s: manipulate archive files\n", prog, p);
  472. for (p = long_help_msg; *p != '\0'; p++) {
  473. if (*p == '%') {
  474. if (p[1] == 'p') {
  475. fputs(prog, stdout);
  476. p++;
  477. } else
  478. putchar('%');
  479. } else
  480. putchar(*p);
  481. }
  482. version();
  483. }
  484. static void
  485. version(void)
  486. {
  487. fprintf(stdout,"bsdcpio %s - %s \n",
  488. BSDCPIO_VERSION_STRING,
  489. archive_version_details());
  490. exit(0);
  491. }
  492. static void
  493. mode_out(struct cpio *cpio)
  494. {
  495. struct archive_entry *entry, *spare;
  496. struct lafe_line_reader *lr;
  497. const char *p;
  498. int r;
  499. if (cpio->option_append)
  500. lafe_errc(1, 0, "Append mode not yet supported.");
  501. cpio->archive_read_disk = archive_read_disk_new();
  502. if (cpio->archive_read_disk == NULL)
  503. lafe_errc(1, 0, "Failed to allocate archive object");
  504. if (cpio->option_follow_links)
  505. archive_read_disk_set_symlink_logical(cpio->archive_read_disk);
  506. else
  507. archive_read_disk_set_symlink_physical(cpio->archive_read_disk);
  508. archive_read_disk_set_standard_lookup(cpio->archive_read_disk);
  509. cpio->archive = archive_write_new();
  510. if (cpio->archive == NULL)
  511. lafe_errc(1, 0, "Failed to allocate archive object");
  512. switch (cpio->compress) {
  513. case OPTION_GRZIP:
  514. r = archive_write_add_filter_grzip(cpio->archive);
  515. break;
  516. case 'J':
  517. r = archive_write_add_filter_xz(cpio->archive);
  518. break;
  519. case OPTION_LRZIP:
  520. r = archive_write_add_filter_lrzip(cpio->archive);
  521. break;
  522. case OPTION_LZ4:
  523. r = archive_write_add_filter_lz4(cpio->archive);
  524. break;
  525. case OPTION_LZMA:
  526. r = archive_write_add_filter_lzma(cpio->archive);
  527. break;
  528. case OPTION_LZOP:
  529. r = archive_write_add_filter_lzop(cpio->archive);
  530. break;
  531. case OPTION_ZSTD:
  532. r = archive_write_add_filter_zstd(cpio->archive);
  533. break;
  534. case 'j': case 'y':
  535. r = archive_write_add_filter_bzip2(cpio->archive);
  536. break;
  537. case 'z':
  538. r = archive_write_add_filter_gzip(cpio->archive);
  539. break;
  540. case 'Z':
  541. r = archive_write_add_filter_compress(cpio->archive);
  542. break;
  543. default:
  544. r = archive_write_add_filter_none(cpio->archive);
  545. break;
  546. }
  547. if (r < ARCHIVE_WARN)
  548. lafe_errc(1, 0, "Requested compression not available");
  549. switch (cpio->add_filter) {
  550. case 0:
  551. r = ARCHIVE_OK;
  552. break;
  553. case OPTION_B64ENCODE:
  554. r = archive_write_add_filter_b64encode(cpio->archive);
  555. break;
  556. case OPTION_UUENCODE:
  557. r = archive_write_add_filter_uuencode(cpio->archive);
  558. break;
  559. }
  560. if (r < ARCHIVE_WARN)
  561. lafe_errc(1, 0, "Requested filter not available");
  562. r = archive_write_set_format_by_name(cpio->archive, cpio->format);
  563. if (r != ARCHIVE_OK)
  564. lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
  565. archive_write_set_bytes_per_block(cpio->archive, cpio->bytes_per_block);
  566. cpio->linkresolver = archive_entry_linkresolver_new();
  567. archive_entry_linkresolver_set_strategy(cpio->linkresolver,
  568. archive_format(cpio->archive));
  569. if (cpio->passphrase != NULL)
  570. r = archive_write_set_passphrase(cpio->archive,
  571. cpio->passphrase);
  572. else
  573. r = archive_write_set_passphrase_callback(cpio->archive, cpio,
  574. &passphrase_callback);
  575. if (r != ARCHIVE_OK)
  576. lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
  577. /*
  578. * The main loop: Copy each file into the output archive.
  579. */
  580. r = archive_write_open_filename(cpio->archive, cpio->filename);
  581. if (r != ARCHIVE_OK)
  582. lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
  583. lr = lafe_line_reader("-", cpio->option_null);
  584. while ((p = lafe_line_reader_next(lr)) != NULL)
  585. file_to_archive(cpio, p);
  586. lafe_line_reader_free(lr);
  587. /*
  588. * The hardlink detection may have queued up a couple of entries
  589. * that can now be flushed.
  590. */
  591. entry = NULL;
  592. archive_entry_linkify(cpio->linkresolver, &entry, &spare);
  593. while (entry != NULL) {
  594. entry_to_archive(cpio, entry);
  595. archive_entry_free(entry);
  596. entry = NULL;
  597. archive_entry_linkify(cpio->linkresolver, &entry, &spare);
  598. }
  599. r = archive_write_close(cpio->archive);
  600. if (cpio->dot)
  601. fprintf(stderr, "\n");
  602. if (r != ARCHIVE_OK)
  603. lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
  604. if (!cpio->quiet) {
  605. int64_t blocks =
  606. (archive_filter_bytes(cpio->archive, 0) + 511)
  607. / 512;
  608. fprintf(stderr, "%lu %s\n", (unsigned long)blocks,
  609. blocks == 1 ? "block" : "blocks");
  610. }
  611. archive_write_free(cpio->archive);
  612. archive_entry_linkresolver_free(cpio->linkresolver);
  613. }
  614. static const char *
  615. remove_leading_slash(const char *p)
  616. {
  617. const char *rp;
  618. /* Remove leading "//./" or "//?/" or "//?/UNC/"
  619. * (absolute path prefixes used by Windows API) */
  620. if ((p[0] == '/' || p[0] == '\\') &&
  621. (p[1] == '/' || p[1] == '\\') &&
  622. (p[2] == '.' || p[2] == '?') &&
  623. (p[3] == '/' || p[3] == '\\'))
  624. {
  625. if (p[2] == '?' &&
  626. (p[4] == 'U' || p[4] == 'u') &&
  627. (p[5] == 'N' || p[5] == 'n') &&
  628. (p[6] == 'C' || p[6] == 'c') &&
  629. (p[7] == '/' || p[7] == '\\'))
  630. p += 8;
  631. else
  632. p += 4;
  633. }
  634. do {
  635. rp = p;
  636. /* Remove leading drive letter from archives created
  637. * on Windows. */
  638. if (((p[0] >= 'a' && p[0] <= 'z') ||
  639. (p[0] >= 'A' && p[0] <= 'Z')) &&
  640. p[1] == ':') {
  641. p += 2;
  642. }
  643. /* Remove leading "/../", "//", etc. */
  644. while (p[0] == '/' || p[0] == '\\') {
  645. if (p[1] == '.' && p[2] == '.' &&
  646. (p[3] == '/' || p[3] == '\\')) {
  647. p += 3; /* Remove "/..", leave "/"
  648. * for next pass. */
  649. } else
  650. p += 1; /* Remove "/". */
  651. }
  652. } while (rp != p);
  653. return (p);
  654. }
  655. /*
  656. * This is used by both out mode (to copy objects from disk into
  657. * an archive) and pass mode (to copy objects from disk to
  658. * an archive_write_disk "archive").
  659. */
  660. static int
  661. file_to_archive(struct cpio *cpio, const char *srcpath)
  662. {
  663. const char *destpath;
  664. struct archive_entry *entry, *spare;
  665. size_t len;
  666. int r;
  667. /*
  668. * Create an archive_entry describing the source file.
  669. *
  670. */
  671. entry = archive_entry_new();
  672. if (entry == NULL)
  673. lafe_errc(1, 0, "Couldn't allocate entry");
  674. archive_entry_copy_sourcepath(entry, srcpath);
  675. r = archive_read_disk_entry_from_file(cpio->archive_read_disk,
  676. entry, -1, NULL);
  677. if (r < ARCHIVE_FAILED)
  678. lafe_errc(1, 0, "%s",
  679. archive_error_string(cpio->archive_read_disk));
  680. if (r < ARCHIVE_OK)
  681. lafe_warnc(0, "%s",
  682. archive_error_string(cpio->archive_read_disk));
  683. if (r <= ARCHIVE_FAILED) {
  684. archive_entry_free(entry);
  685. cpio->return_value = 1;
  686. return (r);
  687. }
  688. if (cpio->uid_override >= 0) {
  689. archive_entry_set_uid(entry, cpio->uid_override);
  690. archive_entry_set_uname(entry, cpio->uname_override);
  691. }
  692. if (cpio->gid_override >= 0) {
  693. archive_entry_set_gid(entry, cpio->gid_override);
  694. archive_entry_set_gname(entry, cpio->gname_override);
  695. }
  696. /*
  697. * Generate a destination path for this entry.
  698. * "destination path" is the name to which it will be copied in
  699. * pass mode or the name that will go into the archive in
  700. * output mode.
  701. */
  702. destpath = srcpath;
  703. if (cpio->destdir) {
  704. len = cpio->destdir_len + strlen(srcpath) + 8;
  705. if (len >= cpio->pass_destpath_alloc) {
  706. while (len >= cpio->pass_destpath_alloc) {
  707. cpio->pass_destpath_alloc += 512;
  708. cpio->pass_destpath_alloc *= 2;
  709. }
  710. free(cpio->pass_destpath);
  711. cpio->pass_destpath = malloc(cpio->pass_destpath_alloc);
  712. if (cpio->pass_destpath == NULL)
  713. lafe_errc(1, ENOMEM,
  714. "Can't allocate path buffer");
  715. }
  716. strcpy(cpio->pass_destpath, cpio->destdir);
  717. strcat(cpio->pass_destpath, remove_leading_slash(srcpath));
  718. destpath = cpio->pass_destpath;
  719. }
  720. if (cpio->option_rename)
  721. destpath = cpio_rename(destpath);
  722. if (destpath == NULL) {
  723. archive_entry_free(entry);
  724. return (0);
  725. }
  726. archive_entry_copy_pathname(entry, destpath);
  727. /*
  728. * If we're trying to preserve hardlinks, match them here.
  729. */
  730. spare = NULL;
  731. if (cpio->linkresolver != NULL
  732. && archive_entry_filetype(entry) != AE_IFDIR) {
  733. archive_entry_linkify(cpio->linkresolver, &entry, &spare);
  734. }
  735. if (entry != NULL) {
  736. r = entry_to_archive(cpio, entry);
  737. archive_entry_free(entry);
  738. if (spare != NULL) {
  739. if (r == 0)
  740. r = entry_to_archive(cpio, spare);
  741. archive_entry_free(spare);
  742. }
  743. }
  744. return (r);
  745. }
  746. static int
  747. entry_to_archive(struct cpio *cpio, struct archive_entry *entry)
  748. {
  749. const char *destpath = archive_entry_pathname(entry);
  750. const char *srcpath = archive_entry_sourcepath(entry);
  751. int fd = -1;
  752. ssize_t bytes_read;
  753. int r;
  754. /* Print out the destination name to the user. */
  755. if (cpio->verbose)
  756. fprintf(stderr,"%s", destpath);
  757. if (cpio->dot)
  758. fprintf(stderr, ".");
  759. /*
  760. * Option_link only makes sense in pass mode and for
  761. * regular files. Also note: if a link operation fails
  762. * because of cross-device restrictions, we'll fall back
  763. * to copy mode for that entry.
  764. *
  765. * TODO: Test other cpio implementations to see if they
  766. * hard-link anything other than regular files here.
  767. */
  768. if (cpio->option_link
  769. && archive_entry_filetype(entry) == AE_IFREG)
  770. {
  771. struct archive_entry *t;
  772. /* Save the original entry in case we need it later. */
  773. t = archive_entry_clone(entry);
  774. if (t == NULL)
  775. lafe_errc(1, ENOMEM, "Can't create link");
  776. /* Note: link(2) doesn't create parent directories,
  777. * so we use archive_write_header() instead as a
  778. * convenience. */
  779. archive_entry_set_hardlink(t, srcpath);
  780. /* This is a straight link that carries no data. */
  781. archive_entry_set_size(t, 0);
  782. r = archive_write_header(cpio->archive, t);
  783. archive_entry_free(t);
  784. if (r != ARCHIVE_OK)
  785. lafe_warnc(archive_errno(cpio->archive),
  786. "%s", archive_error_string(cpio->archive));
  787. if (r == ARCHIVE_FATAL)
  788. exit(1);
  789. #ifdef EXDEV
  790. if (r != ARCHIVE_OK && archive_errno(cpio->archive) == EXDEV) {
  791. /* Cross-device link: Just fall through and use
  792. * the original entry to copy the file over. */
  793. lafe_warnc(0, "Copying file instead");
  794. } else
  795. #endif
  796. return (0);
  797. }
  798. /*
  799. * Make sure we can open the file (if necessary) before
  800. * trying to write the header.
  801. */
  802. if (archive_entry_filetype(entry) == AE_IFREG) {
  803. if (archive_entry_size(entry) > 0) {
  804. fd = open(srcpath, O_RDONLY | O_BINARY);
  805. if (fd < 0) {
  806. lafe_warnc(errno,
  807. "%s: could not open file", srcpath);
  808. goto cleanup;
  809. }
  810. }
  811. } else {
  812. archive_entry_set_size(entry, 0);
  813. }
  814. r = archive_write_header(cpio->archive, entry);
  815. if (r != ARCHIVE_OK)
  816. lafe_warnc(archive_errno(cpio->archive),
  817. "%s: %s",
  818. srcpath,
  819. archive_error_string(cpio->archive));
  820. if (r == ARCHIVE_FATAL)
  821. exit(1);
  822. if (r >= ARCHIVE_WARN && archive_entry_size(entry) > 0 && fd >= 0) {
  823. bytes_read = read(fd, cpio->buff, (unsigned)cpio->buff_size);
  824. while (bytes_read > 0) {
  825. ssize_t bytes_write;
  826. bytes_write = archive_write_data(cpio->archive,
  827. cpio->buff, bytes_read);
  828. if (bytes_write < 0)
  829. lafe_errc(1, archive_errno(cpio->archive),
  830. "%s", archive_error_string(cpio->archive));
  831. if (bytes_write < bytes_read) {
  832. lafe_warnc(0,
  833. "Truncated write; file may have "
  834. "grown while being archived.");
  835. }
  836. bytes_read = read(fd, cpio->buff,
  837. (unsigned)cpio->buff_size);
  838. }
  839. }
  840. fd = restore_time(cpio, entry, srcpath, fd);
  841. cleanup:
  842. if (cpio->verbose)
  843. fprintf(stderr,"\n");
  844. if (fd >= 0)
  845. close(fd);
  846. return (0);
  847. }
  848. static int
  849. restore_time(struct cpio *cpio, struct archive_entry *entry,
  850. const char *name, int fd)
  851. {
  852. #ifndef HAVE_UTIMES
  853. static int warned = 0;
  854. (void)cpio; /* UNUSED */
  855. (void)entry; /* UNUSED */
  856. (void)name; /* UNUSED */
  857. if (!warned)
  858. lafe_warnc(0, "Can't restore access times on this platform");
  859. warned = 1;
  860. return (fd);
  861. #else
  862. #if defined(_WIN32) && !defined(__CYGWIN__)
  863. struct __timeval times[2];
  864. #else
  865. struct timeval times[2];
  866. #endif
  867. if (!cpio->option_atime_restore)
  868. return (fd);
  869. times[1].tv_sec = archive_entry_mtime(entry);
  870. times[1].tv_usec = archive_entry_mtime_nsec(entry) / 1000;
  871. times[0].tv_sec = archive_entry_atime(entry);
  872. times[0].tv_usec = archive_entry_atime_nsec(entry) / 1000;
  873. #if defined(HAVE_FUTIMES) && !defined(__CYGWIN__)
  874. if (fd >= 0 && futimes(fd, times) == 0)
  875. return (fd);
  876. #endif
  877. /*
  878. * Some platform cannot restore access times if the file descriptor
  879. * is still opened.
  880. */
  881. if (fd >= 0) {
  882. close(fd);
  883. fd = -1;
  884. }
  885. #ifdef HAVE_LUTIMES
  886. if (lutimes(name, times) != 0)
  887. #else
  888. if ((AE_IFLNK != archive_entry_filetype(entry))
  889. && utimes(name, times) != 0)
  890. #endif
  891. lafe_warnc(errno, "Can't update time for %s", name);
  892. #endif
  893. return (fd);
  894. }
  895. static void
  896. mode_in(struct cpio *cpio)
  897. {
  898. struct archive *a;
  899. struct archive_entry *entry;
  900. struct archive *ext;
  901. const char *destpath;
  902. int r;
  903. ext = archive_write_disk_new();
  904. if (ext == NULL)
  905. lafe_errc(1, 0, "Couldn't allocate restore object");
  906. r = archive_write_disk_set_options(ext, cpio->extract_flags);
  907. if (r != ARCHIVE_OK)
  908. lafe_errc(1, 0, "%s", archive_error_string(ext));
  909. a = archive_read_new();
  910. if (a == NULL)
  911. lafe_errc(1, 0, "Couldn't allocate archive object");
  912. archive_read_support_filter_all(a);
  913. archive_read_support_format_all(a);
  914. if (cpio->option_pwb)
  915. archive_read_set_options(a, "pwb");
  916. if (cpio->passphrase != NULL)
  917. r = archive_read_add_passphrase(a, cpio->passphrase);
  918. else
  919. r = archive_read_set_passphrase_callback(a, cpio,
  920. &passphrase_callback);
  921. if (r != ARCHIVE_OK)
  922. lafe_errc(1, 0, "%s", archive_error_string(a));
  923. if (archive_read_open_filename(a, cpio->filename,
  924. cpio->bytes_per_block))
  925. lafe_errc(1, archive_errno(a),
  926. "%s", archive_error_string(a));
  927. for (;;) {
  928. r = archive_read_next_header(a, &entry);
  929. if (r == ARCHIVE_EOF)
  930. break;
  931. if (r != ARCHIVE_OK) {
  932. lafe_errc(1, archive_errno(a),
  933. "%s", archive_error_string(a));
  934. }
  935. if (archive_match_path_excluded(cpio->matching, entry))
  936. continue;
  937. if (cpio->option_rename) {
  938. destpath = cpio_rename(archive_entry_pathname(entry));
  939. archive_entry_set_pathname(entry, destpath);
  940. } else
  941. destpath = archive_entry_pathname(entry);
  942. if (destpath == NULL)
  943. continue;
  944. if (cpio->verbose)
  945. fprintf(stderr, "%s\n", destpath);
  946. if (cpio->dot)
  947. fprintf(stderr, ".");
  948. if (cpio->uid_override >= 0)
  949. archive_entry_set_uid(entry, cpio->uid_override);
  950. if (cpio->gid_override >= 0)
  951. archive_entry_set_gid(entry, cpio->gid_override);
  952. r = archive_write_header(ext, entry);
  953. if (r != ARCHIVE_OK) {
  954. fprintf(stderr, "%s: %s\n",
  955. archive_entry_pathname(entry),
  956. archive_error_string(ext));
  957. } else if (!archive_entry_size_is_set(entry)
  958. || archive_entry_size(entry) > 0) {
  959. r = extract_data(a, ext);
  960. if (r != ARCHIVE_OK)
  961. cpio->return_value = 1;
  962. }
  963. }
  964. r = archive_read_close(a);
  965. if (cpio->dot)
  966. fprintf(stderr, "\n");
  967. if (r != ARCHIVE_OK)
  968. lafe_errc(1, 0, "%s", archive_error_string(a));
  969. r = archive_write_close(ext);
  970. if (r != ARCHIVE_OK)
  971. lafe_errc(1, 0, "%s", archive_error_string(ext));
  972. if (!cpio->quiet) {
  973. int64_t blocks = (archive_filter_bytes(a, 0) + 511)
  974. / 512;
  975. fprintf(stderr, "%lu %s\n", (unsigned long)blocks,
  976. blocks == 1 ? "block" : "blocks");
  977. }
  978. archive_read_free(a);
  979. archive_write_free(ext);
  980. exit(cpio->return_value);
  981. }
  982. /*
  983. * Exits if there's a fatal error. Returns ARCHIVE_OK
  984. * if everything is kosher.
  985. */
  986. static int
  987. extract_data(struct archive *ar, struct archive *aw)
  988. {
  989. int r;
  990. size_t size;
  991. const void *block;
  992. int64_t offset;
  993. for (;;) {
  994. r = archive_read_data_block(ar, &block, &size, &offset);
  995. if (r == ARCHIVE_EOF)
  996. return (ARCHIVE_OK);
  997. if (r != ARCHIVE_OK) {
  998. lafe_warnc(archive_errno(ar),
  999. "%s", archive_error_string(ar));
  1000. exit(1);
  1001. }
  1002. r = (int)archive_write_data_block(aw, block, size, offset);
  1003. if (r != ARCHIVE_OK) {
  1004. lafe_warnc(archive_errno(aw),
  1005. "%s", archive_error_string(aw));
  1006. return (r);
  1007. }
  1008. }
  1009. }
  1010. static void
  1011. mode_list(struct cpio *cpio)
  1012. {
  1013. struct archive *a;
  1014. struct archive_entry *entry;
  1015. int r;
  1016. a = archive_read_new();
  1017. if (a == NULL)
  1018. lafe_errc(1, 0, "Couldn't allocate archive object");
  1019. archive_read_support_filter_all(a);
  1020. archive_read_support_format_all(a);
  1021. if (cpio->option_pwb)
  1022. archive_read_set_options(a, "pwb");
  1023. if (cpio->passphrase != NULL)
  1024. r = archive_read_add_passphrase(a, cpio->passphrase);
  1025. else
  1026. r = archive_read_set_passphrase_callback(a, cpio,
  1027. &passphrase_callback);
  1028. if (r != ARCHIVE_OK)
  1029. lafe_errc(1, 0, "%s", archive_error_string(a));
  1030. if (archive_read_open_filename(a, cpio->filename,
  1031. cpio->bytes_per_block))
  1032. lafe_errc(1, archive_errno(a),
  1033. "%s", archive_error_string(a));
  1034. for (;;) {
  1035. r = archive_read_next_header(a, &entry);
  1036. if (r == ARCHIVE_EOF)
  1037. break;
  1038. if (r != ARCHIVE_OK) {
  1039. lafe_errc(1, archive_errno(a),
  1040. "%s", archive_error_string(a));
  1041. }
  1042. if (archive_match_path_excluded(cpio->matching, entry))
  1043. continue;
  1044. if (cpio->verbose)
  1045. list_item_verbose(cpio, entry);
  1046. else
  1047. fprintf(stdout, "%s\n", archive_entry_pathname(entry));
  1048. }
  1049. r = archive_read_close(a);
  1050. if (r != ARCHIVE_OK)
  1051. lafe_errc(1, 0, "%s", archive_error_string(a));
  1052. if (!cpio->quiet) {
  1053. int64_t blocks = (archive_filter_bytes(a, 0) + 511)
  1054. / 512;
  1055. fprintf(stderr, "%lu %s\n", (unsigned long)blocks,
  1056. blocks == 1 ? "block" : "blocks");
  1057. }
  1058. archive_read_free(a);
  1059. exit(0);
  1060. }
  1061. /*
  1062. * Display information about the current file.
  1063. *
  1064. * The format here roughly duplicates the output of 'ls -l'.
  1065. * This is based on SUSv2, where 'tar tv' is documented as
  1066. * listing additional information in an "unspecified format,"
  1067. * and 'pax -l' is documented as using the same format as 'ls -l'.
  1068. */
  1069. static void
  1070. list_item_verbose(struct cpio *cpio, struct archive_entry *entry)
  1071. {
  1072. char size[32];
  1073. char date[32];
  1074. char uids[16], gids[16];
  1075. const char *uname, *gname;
  1076. FILE *out = stdout;
  1077. const char *fmt;
  1078. time_t mtime;
  1079. static time_t now;
  1080. struct tm *ltime;
  1081. #if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)
  1082. struct tm tmbuf;
  1083. #endif
  1084. if (!now)
  1085. time(&now);
  1086. if (cpio->option_numeric_uid_gid) {
  1087. /* Format numeric uid/gid for display. */
  1088. strcpy(uids, cpio_i64toa(archive_entry_uid(entry)));
  1089. uname = uids;
  1090. strcpy(gids, cpio_i64toa(archive_entry_gid(entry)));
  1091. gname = gids;
  1092. } else {
  1093. /* Use uname if it's present, else lookup name from uid. */
  1094. uname = archive_entry_uname(entry);
  1095. if (uname == NULL)
  1096. uname = lookup_uname(cpio, (uid_t)archive_entry_uid(entry));
  1097. /* Use gname if it's present, else lookup name from gid. */
  1098. gname = archive_entry_gname(entry);
  1099. if (gname == NULL)
  1100. gname = lookup_gname(cpio, (uid_t)archive_entry_gid(entry));
  1101. }
  1102. /* Print device number or file size. */
  1103. if (archive_entry_filetype(entry) == AE_IFCHR
  1104. || archive_entry_filetype(entry) == AE_IFBLK) {
  1105. snprintf(size, sizeof(size), "%lu,%lu",
  1106. (unsigned long)archive_entry_rdevmajor(entry),
  1107. (unsigned long)archive_entry_rdevminor(entry));
  1108. } else {
  1109. strcpy(size, cpio_i64toa(archive_entry_size(entry)));
  1110. }
  1111. /* Format the time using 'ls -l' conventions. */
  1112. mtime = archive_entry_mtime(entry);
  1113. #if defined(_WIN32) && !defined(__CYGWIN__)
  1114. /* Windows' strftime function does not support %e format. */
  1115. if (mtime - now > 365*86400/2
  1116. || mtime - now < -365*86400/2)
  1117. fmt = cpio->day_first ? "%d %b %Y" : "%b %d %Y";
  1118. else
  1119. fmt = cpio->day_first ? "%d %b %H:%M" : "%b %d %H:%M";
  1120. #else
  1121. if (mtime - now > 365*86400/2
  1122. || mtime - now < -365*86400/2)
  1123. fmt = cpio->day_first ? "%e %b %Y" : "%b %e %Y";
  1124. else
  1125. fmt = cpio->day_first ? "%e %b %H:%M" : "%b %e %H:%M";
  1126. #endif
  1127. #if defined(HAVE_LOCALTIME_S)
  1128. ltime = localtime_s(&tmbuf, &mtime) ? NULL : &tmbuf;
  1129. #elif defined(HAVE_LOCALTIME_R)
  1130. ltime = localtime_r(&mtime, &tmbuf);
  1131. #else
  1132. ltime = localtime(&mtime);
  1133. #endif
  1134. strftime(date, sizeof(date), fmt, ltime);
  1135. fprintf(out, "%s%3d %-8s %-8s %8s %12s %s",
  1136. archive_entry_strmode(entry),
  1137. archive_entry_nlink(entry),
  1138. uname, gname, size, date,
  1139. archive_entry_pathname(entry));
  1140. /* Extra information for links. */
  1141. if (archive_entry_hardlink(entry)) /* Hard link */
  1142. fprintf(out, " link to %s", archive_entry_hardlink(entry));
  1143. else if (archive_entry_symlink(entry)) /* Symbolic link */
  1144. fprintf(out, " -> %s", archive_entry_symlink(entry));
  1145. fprintf(out, "\n");
  1146. }
  1147. static void
  1148. mode_pass(struct cpio *cpio, const char *destdir)
  1149. {
  1150. struct lafe_line_reader *lr;
  1151. const char *p;
  1152. int r;
  1153. /* Ensure target dir has a trailing '/' to simplify path surgery. */
  1154. cpio->destdir_len = strlen(destdir);
  1155. cpio->destdir = malloc(cpio->destdir_len + 8);
  1156. memcpy(cpio->destdir, destdir, cpio->destdir_len);
  1157. if (cpio->destdir_len == 0 || destdir[cpio->destdir_len - 1] != '/')
  1158. cpio->destdir[cpio->destdir_len++] = '/';
  1159. cpio->destdir[cpio->destdir_len] = '\0';
  1160. cpio->archive = archive_write_disk_new();
  1161. if (cpio->archive == NULL)
  1162. lafe_errc(1, 0, "Failed to allocate archive object");
  1163. r = archive_write_disk_set_options(cpio->archive, cpio->extract_flags);
  1164. if (r != ARCHIVE_OK)
  1165. lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
  1166. cpio->linkresolver = archive_entry_linkresolver_new();
  1167. archive_write_disk_set_standard_lookup(cpio->archive);
  1168. cpio->archive_read_disk = archive_read_disk_new();
  1169. if (cpio->archive_read_disk == NULL)
  1170. lafe_errc(1, 0, "Failed to allocate archive object");
  1171. if (cpio->option_follow_links)
  1172. archive_read_disk_set_symlink_logical(cpio->archive_read_disk);
  1173. else
  1174. archive_read_disk_set_symlink_physical(cpio->archive_read_disk);
  1175. archive_read_disk_set_standard_lookup(cpio->archive_read_disk);
  1176. lr = lafe_line_reader("-", cpio->option_null);
  1177. while ((p = lafe_line_reader_next(lr)) != NULL)
  1178. file_to_archive(cpio, p);
  1179. lafe_line_reader_free(lr);
  1180. archive_entry_linkresolver_free(cpio->linkresolver);
  1181. r = archive_write_close(cpio->archive);
  1182. if (cpio->dot)
  1183. fprintf(stderr, "\n");
  1184. if (r != ARCHIVE_OK)
  1185. lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
  1186. if (!cpio->quiet) {
  1187. int64_t blocks =
  1188. (archive_filter_bytes(cpio->archive, 0) + 511)
  1189. / 512;
  1190. fprintf(stderr, "%lu %s\n", (unsigned long)blocks,
  1191. blocks == 1 ? "block" : "blocks");
  1192. }
  1193. archive_write_free(cpio->archive);
  1194. free(cpio->pass_destpath);
  1195. }
  1196. /*
  1197. * Prompt for a new name for this entry. Returns a pointer to the
  1198. * new name or NULL if the entry should not be copied. This
  1199. * implements the semantics defined in POSIX.1-1996, which specifies
  1200. * that an input of '.' means the name should be unchanged. GNU cpio
  1201. * treats '.' as a literal new name.
  1202. */
  1203. static const char *
  1204. cpio_rename(const char *name)
  1205. {
  1206. static char buff[1024];
  1207. FILE *t;
  1208. char *p, *ret;
  1209. #if defined(_WIN32) && !defined(__CYGWIN__)
  1210. FILE *to;
  1211. t = fopen("CONIN$", "r");
  1212. if (t == NULL)
  1213. return (name);
  1214. to = fopen("CONOUT$", "w");
  1215. if (to == NULL) {
  1216. fclose(t);
  1217. return (name);
  1218. }
  1219. fprintf(to, "%s (Enter/./(new name))? ", name);
  1220. fclose(to);
  1221. #else
  1222. t = fopen("/dev/tty", "r+");
  1223. if (t == NULL)
  1224. return (name);
  1225. fprintf(t, "%s (Enter/./(new name))? ", name);
  1226. fflush(t);
  1227. #endif
  1228. p = fgets(buff, sizeof(buff), t);
  1229. fclose(t);
  1230. if (p == NULL)
  1231. /* End-of-file is a blank line. */
  1232. return (NULL);
  1233. while (*p == ' ' || *p == '\t')
  1234. ++p;
  1235. if (*p == '\n' || *p == '\0')
  1236. /* Empty line. */
  1237. return (NULL);
  1238. if (*p == '.' && p[1] == '\n')
  1239. /* Single period preserves original name. */
  1240. return (name);
  1241. ret = p;
  1242. /* Trim the final newline. */
  1243. while (*p != '\0' && *p != '\n')
  1244. ++p;
  1245. /* Overwrite the final \n with a null character. */
  1246. *p = '\0';
  1247. return (ret);
  1248. }
  1249. static void
  1250. free_cache(struct name_cache *cache)
  1251. {
  1252. size_t i;
  1253. if (cache != NULL) {
  1254. for (i = 0; i < cache->size; i++)
  1255. free(cache->cache[i].name);
  1256. free(cache);
  1257. }
  1258. }
  1259. /*
  1260. * Lookup uname/gname from uid/gid, return NULL if no match.
  1261. */
  1262. static const char *
  1263. lookup_name(struct cpio *cpio, struct name_cache **name_cache_variable,
  1264. int (*lookup_fn)(struct cpio *, const char **, id_t), id_t id)
  1265. {
  1266. char asnum[16];
  1267. struct name_cache *cache;
  1268. const char *name;
  1269. int slot;
  1270. if (*name_cache_variable == NULL) {
  1271. *name_cache_variable = calloc(1, sizeof(struct name_cache));
  1272. if (*name_cache_variable == NULL)
  1273. lafe_errc(1, ENOMEM, "No more memory");
  1274. (*name_cache_variable)->size = name_cache_size;
  1275. }
  1276. cache = *name_cache_variable;
  1277. cache->probes++;
  1278. slot = id % cache->size;
  1279. if (cache->cache[slot].name != NULL) {
  1280. if (cache->cache[slot].id == id) {
  1281. cache->hits++;
  1282. return (cache->cache[slot].name);
  1283. }
  1284. free(cache->cache[slot].name);
  1285. cache->cache[slot].name = NULL;
  1286. }
  1287. if (lookup_fn(cpio, &name, id)) {
  1288. /* If lookup failed, format it as a number. */
  1289. snprintf(asnum, sizeof(asnum), "%u", (unsigned)id);
  1290. name = asnum;
  1291. }
  1292. cache->cache[slot].name = strdup(name);
  1293. if (cache->cache[slot].name != NULL) {
  1294. cache->cache[slot].id = id;
  1295. return (cache->cache[slot].name);
  1296. }
  1297. /*
  1298. * Conveniently, NULL marks an empty slot, so
  1299. * if the strdup() fails, we've just failed to
  1300. * cache it. No recovery necessary.
  1301. */
  1302. return (NULL);
  1303. }
  1304. static const char *
  1305. lookup_uname(struct cpio *cpio, uid_t uid)
  1306. {
  1307. return (lookup_name(cpio, &cpio->uname_cache,
  1308. &lookup_uname_helper, (id_t)uid));
  1309. }
  1310. static int
  1311. lookup_uname_helper(struct cpio *cpio, const char **name, id_t id)
  1312. {
  1313. struct passwd *pwent;
  1314. (void)cpio; /* UNUSED */
  1315. errno = 0;
  1316. pwent = getpwuid((uid_t)id);
  1317. if (pwent == NULL) {
  1318. if (errno && errno != ENOENT)
  1319. lafe_warnc(errno, "getpwuid(%s) failed",
  1320. cpio_i64toa((int64_t)id));
  1321. return 1;
  1322. }
  1323. *name = pwent->pw_name;
  1324. return 0;
  1325. }
  1326. static const char *
  1327. lookup_gname(struct cpio *cpio, gid_t gid)
  1328. {
  1329. return (lookup_name(cpio, &cpio->gname_cache,
  1330. &lookup_gname_helper, (id_t)gid));
  1331. }
  1332. static int
  1333. lookup_gname_helper(struct cpio *cpio, const char **name, id_t id)
  1334. {
  1335. struct group *grent;
  1336. (void)cpio; /* UNUSED */
  1337. errno = 0;
  1338. grent = getgrgid((gid_t)id);
  1339. if (grent == NULL) {
  1340. if (errno && errno != ENOENT)
  1341. lafe_warnc(errno, "getgrgid(%s) failed",
  1342. cpio_i64toa((int64_t)id));
  1343. return 1;
  1344. }
  1345. *name = grent->gr_name;
  1346. return 0;
  1347. }
  1348. /*
  1349. * It would be nice to just use printf() for formatting large numbers,
  1350. * but the compatibility problems are a big headache. Hence the
  1351. * following simple utility function.
  1352. */
  1353. const char *
  1354. cpio_i64toa(int64_t n0)
  1355. {
  1356. /* 2^64 =~ 1.8 * 10^19, so 20 decimal digits suffice.
  1357. * We also need 1 byte for '-' and 1 for '\0'.
  1358. */
  1359. static char buff[22];
  1360. int64_t n = n0 < 0 ? -n0 : n0;
  1361. char *p = buff + sizeof(buff);
  1362. *--p = '\0';
  1363. do {
  1364. *--p = '0' + (int)(n % 10);
  1365. n /= 10;
  1366. } while (n > 0);
  1367. if (n0 < 0)
  1368. *--p = '-';
  1369. return p;
  1370. }
  1371. #define PPBUFF_SIZE 1024
  1372. static const char *
  1373. passphrase_callback(struct archive *a, void *_client_data)
  1374. {
  1375. struct cpio *cpio = (struct cpio *)_client_data;
  1376. (void)a; /* UNUSED */
  1377. if (cpio->ppbuff == NULL) {
  1378. cpio->ppbuff = malloc(PPBUFF_SIZE);
  1379. if (cpio->ppbuff == NULL)
  1380. lafe_errc(1, errno, "Out of memory");
  1381. }
  1382. return lafe_readpassphrase("Enter passphrase:",
  1383. cpio->ppbuff, PPBUFF_SIZE);
  1384. }
  1385. static void
  1386. passphrase_free(char *ppbuff)
  1387. {
  1388. if (ppbuff != NULL) {
  1389. memset(ppbuff, 0, PPBUFF_SIZE);
  1390. free(ppbuff);
  1391. }
  1392. }