3
0

tar.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini tar implementation for busybox
  4. *
  5. * Modified to use common extraction code used by ar, cpio, dpkg-deb, dpkg
  6. * by Glenn McGrath
  7. *
  8. * Note, that as of BusyBox-0.43, tar has been completely rewritten from the
  9. * ground up. It still has remnants of the old code lying about, but it is
  10. * very different now (i.e., cleaner, less global variables, etc.)
  11. *
  12. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  13. *
  14. * Based in part in the tar implementation in sash
  15. * Copyright (c) 1999 by David I. Bell
  16. * Permission is granted to use, distribute, or modify this source,
  17. * provided that this copyright notice remains intact.
  18. * Permission to distribute sash derived code under GPL has been granted.
  19. *
  20. * Based in part on the tar implementation from busybox-0.28
  21. * Copyright (C) 1995 Bruce Perens
  22. *
  23. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  24. */
  25. //config:config TAR
  26. //config: bool "tar (39 kb)"
  27. //config: default y
  28. //config: help
  29. //config: tar is an archiving program. It's commonly used with gzip to
  30. //config: create compressed archives. It's probably the most widely used
  31. //config: UNIX archive program.
  32. //config:
  33. //config:config FEATURE_TAR_LONG_OPTIONS
  34. //config: bool "Enable long options"
  35. //config: default y
  36. //config: depends on TAR && LONG_OPTS
  37. //config:
  38. //config:config FEATURE_TAR_CREATE
  39. //config: bool "Enable -c (archive creation)"
  40. //config: default y
  41. //config: depends on TAR
  42. //config:
  43. //config:config FEATURE_TAR_AUTODETECT
  44. //config: bool "Autodetect compressed tarballs"
  45. //config: default y
  46. //config: depends on TAR && (FEATURE_SEAMLESS_Z || FEATURE_SEAMLESS_GZ || FEATURE_SEAMLESS_BZ2 || FEATURE_SEAMLESS_LZMA || FEATURE_SEAMLESS_XZ)
  47. //config: help
  48. //config: With this option tar can automatically detect compressed
  49. //config: tarballs. Currently it works only on files (not pipes etc).
  50. //config:
  51. //config:config FEATURE_TAR_FROM
  52. //config: bool "Enable -X (exclude from) and -T (include from) options"
  53. //config: default y
  54. //config: depends on TAR
  55. //config: help
  56. //config: If you enable this option you'll be able to specify
  57. //config: a list of files to include or exclude from an archive.
  58. //config:
  59. //config:config FEATURE_TAR_OLDGNU_COMPATIBILITY
  60. //config: bool "Support old tar header format"
  61. //config: default y
  62. //config: depends on TAR || DPKG
  63. //config: help
  64. //config: This option is required to unpack archives created in
  65. //config: the old GNU format; help to kill this old format by
  66. //config: repacking your ancient archives with the new format.
  67. //config:
  68. //config:config FEATURE_TAR_OLDSUN_COMPATIBILITY
  69. //config: bool "Enable untarring of tarballs with checksums produced by buggy Sun tar"
  70. //config: default y
  71. //config: depends on TAR || DPKG
  72. //config: help
  73. //config: This option is required to unpack archives created by some old
  74. //config: version of Sun's tar (it was calculating checksum using signed
  75. //config: arithmetic). It is said to be fixed in newer Sun tar, but "old"
  76. //config: tarballs still exist.
  77. //config:
  78. //config:config FEATURE_TAR_GNU_EXTENSIONS
  79. //config: bool "Support GNU tar extensions (long filenames)"
  80. //config: default y
  81. //config: depends on TAR || DPKG
  82. //config:
  83. //config:config FEATURE_TAR_TO_COMMAND
  84. //config: bool "Support writing to an external program (--to-command)"
  85. //config: default y
  86. //config: depends on TAR && FEATURE_TAR_LONG_OPTIONS
  87. //config: help
  88. //config: If you enable this option you'll be able to instruct tar to send
  89. //config: the contents of each extracted file to the standard input of an
  90. //config: external program.
  91. //config:
  92. //config:config FEATURE_TAR_UNAME_GNAME
  93. //config: bool "Enable use of user and group names"
  94. //config: default y
  95. //config: depends on TAR
  96. //config: help
  97. //config: Enable use of user and group names in tar. This affects contents
  98. //config: listings (-t) and preserving permissions when unpacking (-p).
  99. //config: +200 bytes.
  100. //config:
  101. //config:config FEATURE_TAR_NOPRESERVE_TIME
  102. //config: bool "Enable -m (do not preserve time) GNU option"
  103. //config: default y
  104. //config: depends on TAR
  105. //config:
  106. //config:config FEATURE_TAR_SELINUX
  107. //config: bool "Support extracting SELinux labels"
  108. //config: default n
  109. //config: depends on TAR && SELINUX
  110. //config: help
  111. //config: With this option busybox supports restoring SELinux labels
  112. //config: when extracting files from tar archives.
  113. //applet:IF_TAR(APPLET(tar, BB_DIR_BIN, BB_SUID_DROP))
  114. //kbuild:lib-$(CONFIG_TAR) += tar.o
  115. #include <fnmatch.h>
  116. #include "libbb.h"
  117. #include "common_bufsiz.h"
  118. #include "bb_archive.h"
  119. /* FIXME: Stop using this non-standard feature */
  120. #ifndef FNM_LEADING_DIR
  121. # define FNM_LEADING_DIR 0
  122. #endif
  123. #if 0
  124. # define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
  125. #else
  126. # define DBG(...) ((void)0)
  127. #endif
  128. #define DBG_OPTION_PARSING 0
  129. #define block_buf bb_common_bufsiz1
  130. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  131. #if ENABLE_FEATURE_TAR_CREATE
  132. /*
  133. ** writeTarFile(), writeFileToTarball(), and writeTarHeader() are
  134. ** the only functions that deal with the HardLinkInfo structure.
  135. ** Even these functions use the xxxHardLinkInfo() functions.
  136. */
  137. typedef struct HardLinkInfo {
  138. struct HardLinkInfo *next; /* Next entry in list */
  139. dev_t dev; /* Device number */
  140. ino_t ino; /* Inode number */
  141. // short linkCount; /* (Hard) Link Count */
  142. char name[1]; /* Start of filename (must be last) */
  143. } HardLinkInfo;
  144. /* Some info to be carried along when creating a new tarball */
  145. typedef struct TarBallInfo {
  146. int tarFd; /* Open-for-write file descriptor
  147. * for the tarball */
  148. int verboseFlag; /* Whether to print extra stuff or not */
  149. # if ENABLE_FEATURE_TAR_FROM
  150. const llist_t *excludeList; /* List of files to not include */
  151. # endif
  152. HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */
  153. HardLinkInfo *hlInfo; /* Hard Link Info for the current file */
  154. //TODO: save only st_dev + st_ino
  155. struct stat tarFileStatBuf; /* Stat info for the tarball, letting
  156. * us know the inode and device that the
  157. * tarball lives, so we can avoid trying
  158. * to include the tarball into itself */
  159. } TarBallInfo;
  160. /* A nice enum with all the possible tar file content types */
  161. enum {
  162. REGTYPE = '0', /* regular file */
  163. REGTYPE0 = '\0', /* regular file (ancient bug compat) */
  164. LNKTYPE = '1', /* hard link */
  165. SYMTYPE = '2', /* symbolic link */
  166. CHRTYPE = '3', /* character special */
  167. BLKTYPE = '4', /* block special */
  168. DIRTYPE = '5', /* directory */
  169. FIFOTYPE = '6', /* FIFO special */
  170. CONTTYPE = '7', /* reserved */
  171. GNULONGLINK = 'K', /* GNU long (>100 chars) link name */
  172. GNULONGNAME = 'L', /* GNU long (>100 chars) file name */
  173. };
  174. /* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
  175. static void addHardLinkInfo(HardLinkInfo **hlInfoHeadPtr,
  176. struct stat *statbuf,
  177. const char *fileName)
  178. {
  179. /* Note: hlInfoHeadPtr can never be NULL! */
  180. HardLinkInfo *hlInfo;
  181. hlInfo = xmalloc(sizeof(HardLinkInfo) + strlen(fileName));
  182. hlInfo->next = *hlInfoHeadPtr;
  183. *hlInfoHeadPtr = hlInfo;
  184. hlInfo->dev = statbuf->st_dev;
  185. hlInfo->ino = statbuf->st_ino;
  186. // hlInfo->linkCount = statbuf->st_nlink;
  187. strcpy(hlInfo->name, fileName);
  188. }
  189. static void freeHardLinkInfo(HardLinkInfo **hlInfoHeadPtr)
  190. {
  191. HardLinkInfo *hlInfo;
  192. HardLinkInfo *hlInfoNext;
  193. if (hlInfoHeadPtr) {
  194. hlInfo = *hlInfoHeadPtr;
  195. while (hlInfo) {
  196. hlInfoNext = hlInfo->next;
  197. free(hlInfo);
  198. hlInfo = hlInfoNext;
  199. }
  200. *hlInfoHeadPtr = NULL;
  201. }
  202. }
  203. /* Might be faster (and bigger) if the dev/ino were stored in numeric order ;) */
  204. static HardLinkInfo *findHardLinkInfo(HardLinkInfo *hlInfo, struct stat *statbuf)
  205. {
  206. while (hlInfo) {
  207. if (statbuf->st_ino == hlInfo->ino
  208. && statbuf->st_dev == hlInfo->dev
  209. ) {
  210. DBG("found hardlink:'%s'", hlInfo->name);
  211. break;
  212. }
  213. hlInfo = hlInfo->next;
  214. }
  215. return hlInfo;
  216. }
  217. /* Put an octal string into the specified buffer.
  218. * The number is zero padded and possibly NUL terminated.
  219. * Stores low-order bits only if whole value does not fit. */
  220. static void putOctal(char *cp, int len, off_t value)
  221. {
  222. char tempBuffer[sizeof(off_t)*3 + 1];
  223. char *tempString = tempBuffer;
  224. int width;
  225. width = sprintf(tempBuffer, "%0*"OFF_FMT"o", len, value);
  226. tempString += (width - len);
  227. /* If string has leading zeroes, we can drop one */
  228. /* and field will have trailing '\0' */
  229. /* (increases chances of compat with other tars) */
  230. if (tempString[0] == '0')
  231. tempString++;
  232. /* Copy the string to the field */
  233. memcpy(cp, tempString, len);
  234. }
  235. #define PUT_OCTAL(a, b) putOctal((a), sizeof(a), (b))
  236. static void chksum_and_xwrite(int fd, struct tar_header_t* hp)
  237. {
  238. /* POSIX says that checksum is done on unsigned bytes
  239. * (Sun and HP-UX gets it wrong... more details in
  240. * GNU tar source) */
  241. const unsigned char *cp;
  242. int chksum, size;
  243. strcpy(hp->magic, "ustar ");
  244. /* Calculate and store the checksum (i.e., the sum of all of the bytes of
  245. * the header). The checksum field must be filled with blanks for the
  246. * calculation. The checksum field is formatted differently from the
  247. * other fields: it has 6 digits, a null, then a space -- rather than
  248. * digits, followed by a null like the other fields... */
  249. memset(hp->chksum, ' ', sizeof(hp->chksum));
  250. cp = (const unsigned char *) hp;
  251. chksum = 0;
  252. size = sizeof(*hp);
  253. do { chksum += *cp++; } while (--size);
  254. putOctal(hp->chksum, sizeof(hp->chksum)-1, chksum);
  255. /* Now write the header out to disk */
  256. xwrite(fd, hp, sizeof(*hp));
  257. }
  258. # if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  259. static void writeLongname(int fd, int type, const char *name, int dir)
  260. {
  261. struct prefilled {
  262. char mode[8]; /* 100-107 */
  263. char uid[8]; /* 108-115 */
  264. char gid[8]; /* 116-123 */
  265. char size[12]; /* 124-135 */
  266. char mtime[12]; /* 136-147 */
  267. };
  268. struct tar_header_t header;
  269. int size;
  270. memset(&header, 0, sizeof(header));
  271. header.typeflag = type;
  272. strcpy(header.name, "././@LongLink");
  273. /* This sets mode/uid/gid/mtime to "00...00<NUL>" strings */
  274. memset((char*)&header + offsetof(struct tar_header_t, mode), /* make gcc-9.x happy */
  275. '0', sizeof(struct prefilled));
  276. header.mode [sizeof(header.mode ) - 1] = '\0';
  277. header.uid [sizeof(header.uid ) - 1] = '\0';
  278. header.gid [sizeof(header.gid ) - 1] = '\0';
  279. /* header.size is filled by '0' now, will be corrected below */
  280. header.mtime[sizeof(header.mtime) - 1] = '\0';
  281. dir = !!dir; /* normalize: 0/1 */
  282. size = strlen(name) + 1 + dir; /* GNU tar uses strlen+1 */
  283. /* + dir: account for possible '/' */
  284. PUT_OCTAL(header.size, size);
  285. chksum_and_xwrite(fd, &header);
  286. /* Write filename[/] and pad the block. */
  287. /* dir=0: writes 'name<NUL>', pads */
  288. /* dir=1: writes 'name', writes '/<NUL>', pads */
  289. dir *= 2;
  290. xwrite(fd, name, size - dir);
  291. xwrite(fd, "/", dir);
  292. size = (-size) & (TAR_BLOCK_SIZE-1);
  293. memset(&header, 0, size);
  294. xwrite(fd, &header, size);
  295. }
  296. # endif
  297. /* Write out a tar header for the specified file/directory/whatever */
  298. static int writeTarHeader(struct TarBallInfo *tbInfo,
  299. const char *header_name, const char *fileName, struct stat *statbuf)
  300. {
  301. struct tar_header_t header;
  302. memset(&header, 0, sizeof(header));
  303. strncpy(header.name, header_name, sizeof(header.name));
  304. /* POSIX says to mask mode with 07777. */
  305. PUT_OCTAL(header.mode, statbuf->st_mode & 07777);
  306. PUT_OCTAL(header.uid, statbuf->st_uid);
  307. PUT_OCTAL(header.gid, statbuf->st_gid);
  308. memset(header.size, '0', sizeof(header.size)-1); /* Regular file size is handled later */
  309. /* users report that files with negative st_mtime cause trouble, so: */
  310. PUT_OCTAL(header.mtime, statbuf->st_mtime >= 0 ? statbuf->st_mtime : 0);
  311. /* Enter the user and group names */
  312. safe_strncpy(header.uname, get_cached_username(statbuf->st_uid), sizeof(header.uname));
  313. safe_strncpy(header.gname, get_cached_groupname(statbuf->st_gid), sizeof(header.gname));
  314. if (tbInfo->hlInfo) {
  315. /* This is a hard link */
  316. header.typeflag = LNKTYPE;
  317. strncpy(header.linkname, tbInfo->hlInfo->name,
  318. sizeof(header.linkname));
  319. # if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  320. /* Write out long linkname if needed */
  321. if (header.linkname[sizeof(header.linkname)-1])
  322. writeLongname(tbInfo->tarFd, GNULONGLINK,
  323. tbInfo->hlInfo->name, 0);
  324. # endif
  325. } else if (S_ISLNK(statbuf->st_mode)) {
  326. char *lpath = xmalloc_readlink_or_warn(fileName);
  327. if (!lpath)
  328. return FALSE;
  329. header.typeflag = SYMTYPE;
  330. strncpy(header.linkname, lpath, sizeof(header.linkname));
  331. # if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  332. /* Write out long linkname if needed */
  333. if (header.linkname[sizeof(header.linkname)-1])
  334. writeLongname(tbInfo->tarFd, GNULONGLINK, lpath, 0);
  335. # else
  336. /* If it is larger than 100 bytes, bail out */
  337. if (header.linkname[sizeof(header.linkname)-1]) {
  338. free(lpath);
  339. bb_simple_error_msg("names longer than "NAME_SIZE_STR" chars not supported");
  340. return FALSE;
  341. }
  342. # endif
  343. free(lpath);
  344. } else if (S_ISDIR(statbuf->st_mode)) {
  345. header.typeflag = DIRTYPE;
  346. /* Append '/' only if there is a space for it */
  347. if (!header.name[sizeof(header.name)-1])
  348. header.name[strlen(header.name)] = '/';
  349. } else if (S_ISCHR(statbuf->st_mode)) {
  350. header.typeflag = CHRTYPE;
  351. PUT_OCTAL(header.devmajor, major(statbuf->st_rdev));
  352. PUT_OCTAL(header.devminor, minor(statbuf->st_rdev));
  353. } else if (S_ISBLK(statbuf->st_mode)) {
  354. header.typeflag = BLKTYPE;
  355. PUT_OCTAL(header.devmajor, major(statbuf->st_rdev));
  356. PUT_OCTAL(header.devminor, minor(statbuf->st_rdev));
  357. } else if (S_ISFIFO(statbuf->st_mode)) {
  358. header.typeflag = FIFOTYPE;
  359. } else if (S_ISREG(statbuf->st_mode)) {
  360. /* header.size field is 12 bytes long */
  361. /* Does octal-encoded size fit? */
  362. uoff_t filesize = statbuf->st_size;
  363. if (sizeof(filesize) <= 4
  364. || filesize <= (uoff_t)0777777777777LL
  365. ) {
  366. PUT_OCTAL(header.size, filesize);
  367. }
  368. /* Does base256-encoded size fit?
  369. * It always does unless off_t is wider than 64 bits.
  370. */
  371. else if (ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  372. # if ULLONG_MAX > 0xffffffffffffffffLL /* 2^64-1 */
  373. && (filesize <= 0x3fffffffffffffffffffffffLL)
  374. # endif
  375. ) {
  376. /* GNU tar uses "base-256 encoding" for very large numbers.
  377. * Encoding is binary, with highest bit always set as a marker
  378. * and sign in next-highest bit:
  379. * 80 00 .. 00 - zero
  380. * bf ff .. ff - largest positive number
  381. * ff ff .. ff - minus 1
  382. * c0 00 .. 00 - smallest negative number
  383. */
  384. char *p8 = header.size + sizeof(header.size);
  385. do {
  386. *--p8 = (uint8_t)filesize;
  387. filesize >>= 8;
  388. } while (p8 != header.size);
  389. *p8 |= 0x80;
  390. } else {
  391. bb_error_msg_and_die("can't store file '%s' "
  392. "of size %"OFF_FMT"u, aborting",
  393. fileName, statbuf->st_size);
  394. }
  395. header.typeflag = REGTYPE;
  396. } else {
  397. bb_error_msg("%s: unknown file type", fileName);
  398. return FALSE;
  399. }
  400. # if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  401. /* Write out long name if needed */
  402. /* (we, like GNU tar, output long linkname *before* long name) */
  403. if (header.name[sizeof(header.name)-1])
  404. writeLongname(tbInfo->tarFd, GNULONGNAME,
  405. header_name, S_ISDIR(statbuf->st_mode));
  406. # endif
  407. /* Now write the header out to disk */
  408. chksum_and_xwrite(tbInfo->tarFd, &header);
  409. /* Now do the verbose thing (or not) */
  410. if (tbInfo->verboseFlag) {
  411. FILE *vbFd = stdout;
  412. /* If archive goes to stdout, verbose goes to stderr */
  413. if (tbInfo->tarFd == STDOUT_FILENO)
  414. vbFd = stderr;
  415. /* GNU "tar cvvf" prints "extended" listing a-la "ls -l" */
  416. /* We don't have such excesses here: for us "v" == "vv" */
  417. /* '/' is probably a GNUism */
  418. fprintf(vbFd, "%s%s\n", header_name,
  419. S_ISDIR(statbuf->st_mode) ? "/" : "");
  420. }
  421. return TRUE;
  422. }
  423. # if ENABLE_FEATURE_TAR_FROM
  424. static int exclude_file(const llist_t *excluded_files, const char *file)
  425. {
  426. while (excluded_files) {
  427. if (excluded_files->data[0] == '/') {
  428. if (fnmatch(excluded_files->data, file,
  429. FNM_PATHNAME | FNM_LEADING_DIR) == 0)
  430. return 1;
  431. } else {
  432. const char *p;
  433. for (p = file; p[0] != '\0'; p++) {
  434. if ((p == file || p[-1] == '/')
  435. && p[0] != '/'
  436. && fnmatch(excluded_files->data, p,
  437. FNM_PATHNAME | FNM_LEADING_DIR) == 0
  438. ) {
  439. return 1;
  440. }
  441. }
  442. }
  443. excluded_files = excluded_files->link;
  444. }
  445. return 0;
  446. }
  447. # else
  448. # define exclude_file(excluded_files, file) 0
  449. # endif
  450. static int FAST_FUNC writeFileToTarball(struct recursive_state *state,
  451. const char *fileName,
  452. struct stat *statbuf)
  453. {
  454. struct TarBallInfo *tbInfo = (struct TarBallInfo *) state->userData;
  455. const char *header_name;
  456. int inputFileFd = -1;
  457. DBG("writeFileToTarball('%s')", fileName);
  458. /* Strip leading '/' and such (must be before memorizing hardlink's name) */
  459. header_name = strip_unsafe_prefix(fileName);
  460. if (header_name[0] == '\0')
  461. return TRUE;
  462. /* It is against the rules to archive a socket */
  463. if (S_ISSOCK(statbuf->st_mode)) {
  464. bb_error_msg("%s: socket ignored", fileName);
  465. return TRUE;
  466. }
  467. /*
  468. * Check to see if we are dealing with a hard link.
  469. * If so -
  470. * Treat the first occurrence of a given dev/inode as a file while
  471. * treating any additional occurrences as hard links. This is done
  472. * by adding the file information to the HardLinkInfo linked list.
  473. */
  474. tbInfo->hlInfo = NULL;
  475. if (!S_ISDIR(statbuf->st_mode) && statbuf->st_nlink > 1) {
  476. DBG("'%s': st_nlink > 1", header_name);
  477. tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf);
  478. if (tbInfo->hlInfo == NULL) {
  479. DBG("'%s': addHardLinkInfo", header_name);
  480. addHardLinkInfo(&tbInfo->hlInfoHead, statbuf, header_name);
  481. }
  482. }
  483. /* It is a bad idea to store the archive we are in the process of creating,
  484. * so check the device and inode to be sure that this particular file isn't
  485. * the new tarball */
  486. if (tbInfo->tarFileStatBuf.st_dev == statbuf->st_dev
  487. && tbInfo->tarFileStatBuf.st_ino == statbuf->st_ino
  488. ) {
  489. bb_error_msg("%s: file is the archive; skipping", fileName);
  490. return TRUE;
  491. }
  492. if (exclude_file(tbInfo->excludeList, header_name))
  493. return SKIP;
  494. # if !ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  495. if (strlen(header_name) >= NAME_SIZE) {
  496. bb_simple_error_msg("names longer than "NAME_SIZE_STR" chars not supported");
  497. return TRUE;
  498. }
  499. # endif
  500. /* Is this a regular file? */
  501. if (tbInfo->hlInfo == NULL && S_ISREG(statbuf->st_mode)) {
  502. /* open the file we want to archive, and make sure all is well */
  503. inputFileFd = open_or_warn(fileName, O_RDONLY);
  504. if (inputFileFd < 0) {
  505. return FALSE;
  506. }
  507. }
  508. /* Add an entry to the tarball */
  509. if (writeTarHeader(tbInfo, header_name, fileName, statbuf) == FALSE) {
  510. return FALSE;
  511. }
  512. /* If it was a regular file, write out the body */
  513. if (inputFileFd >= 0) {
  514. size_t readSize;
  515. /* Write the file to the archive. */
  516. /* We record size into header first, */
  517. /* and then write out file. If file shrinks in between, */
  518. /* tar will be corrupted. So we don't allow for that. */
  519. /* NB: GNU tar 1.16 warns and pads with zeroes */
  520. /* or even seeks back and updates header */
  521. bb_copyfd_exact_size(inputFileFd, tbInfo->tarFd, statbuf->st_size);
  522. ////off_t readSize;
  523. ////readSize = bb_copyfd_size(inputFileFd, tbInfo->tarFd, statbuf->st_size);
  524. ////if (readSize != statbuf->st_size && readSize >= 0) {
  525. //// bb_error_msg_and_die("short read from %s, aborting", fileName);
  526. ////}
  527. /* Check that file did not grow in between? */
  528. /* if (safe_read(inputFileFd, 1) == 1) warn but continue? */
  529. close(inputFileFd);
  530. /* Pad the file up to the tar block size */
  531. /* (a few tricks here in the name of code size) */
  532. readSize = (-(int)statbuf->st_size) & (TAR_BLOCK_SIZE-1);
  533. memset(block_buf, 0, readSize);
  534. xwrite(tbInfo->tarFd, block_buf, readSize);
  535. }
  536. return TRUE;
  537. }
  538. # if SEAMLESS_COMPRESSION
  539. /* Don't inline: vfork scares gcc and pessimizes code */
  540. static void NOINLINE vfork_compressor(int tar_fd, const char *gzip)
  541. {
  542. // On Linux, vfork never unpauses parent early, although standard
  543. // allows for that. Do we want to waste bytes checking for it?
  544. # define WAIT_FOR_CHILD 0
  545. volatile int vfork_exec_errno = 0;
  546. struct fd_pair data;
  547. # if WAIT_FOR_CHILD
  548. struct fd_pair status;
  549. xpiped_pair(status);
  550. # endif
  551. xpiped_pair(data);
  552. signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */
  553. if (xvfork() == 0) {
  554. /* child */
  555. int tfd;
  556. /* NB: close _first_, then move fds! */
  557. close(data.wr);
  558. # if WAIT_FOR_CHILD
  559. close(status.rd);
  560. /* status.wr will close only on exec -
  561. * parent waits for this close to happen */
  562. fcntl(status.wr, F_SETFD, FD_CLOEXEC);
  563. # endif
  564. /* copy it: parent's tar_fd variable must not change */
  565. tfd = tar_fd;
  566. if (tfd == 0) {
  567. /* Output tar fd may be zero.
  568. * xmove_fd(data.rd, 0) would destroy it.
  569. * Reproducer:
  570. * exec 0>&-
  571. * exec 1>&-
  572. * tar czf Z.tar.gz FILE
  573. * Swapping move_fd's order wouldn't work:
  574. * data.rd is 1 and _it_ would be destroyed.
  575. */
  576. tfd = dup(tfd);
  577. }
  578. xmove_fd(data.rd, 0);
  579. xmove_fd(tfd, 1);
  580. /* exec gzip/bzip2/... program */
  581. //BB_EXECLP(gzip, gzip, "-f", (char *)0); - WRONG for "xz",
  582. // if xz is an enabled applet, it'll be a version which
  583. // can only decompress. We do need to execute external
  584. // program, not applet.
  585. execlp(gzip, gzip, "-f", (char *)0);
  586. vfork_exec_errno = errno;
  587. _exit(EXIT_FAILURE);
  588. }
  589. /* parent */
  590. xmove_fd(data.wr, tar_fd);
  591. close(data.rd);
  592. # if WAIT_FOR_CHILD
  593. close(status.wr);
  594. while (1) {
  595. /* Wait until child execs (or fails to) */
  596. char buf;
  597. int n = full_read(status.rd, &buf, 1);
  598. if (n < 0 /* && errno == EAGAIN */)
  599. continue; /* try it again */
  600. }
  601. close(status.rd);
  602. # endif
  603. if (vfork_exec_errno) {
  604. errno = vfork_exec_errno;
  605. bb_perror_msg_and_die("can't execute '%s'", gzip);
  606. }
  607. }
  608. # endif /* SEAMLESS_COMPRESSION */
  609. # if !SEAMLESS_COMPRESSION
  610. /* Do not pass gzip flag to writeTarFile() */
  611. #define writeTarFile(tbInfo, recurseFlags, filelist, gzip) \
  612. writeTarFile(tbInfo, recurseFlags, filelist)
  613. # endif
  614. /* gcc 4.2.1 inlines it, making code bigger */
  615. static NOINLINE int writeTarFile(
  616. struct TarBallInfo *tbInfo,
  617. int recurseFlags,
  618. const llist_t *filelist,
  619. const char *gzip)
  620. {
  621. int errorFlag = FALSE;
  622. /*tbInfo->hlInfoHead = NULL; - already is */
  623. /* Store the stat info for the tarball's file, so
  624. * can avoid including the tarball into itself.... */
  625. xfstat(tbInfo->tarFd, &tbInfo->tarFileStatBuf, "can't stat tar file");
  626. # if SEAMLESS_COMPRESSION
  627. if (gzip)
  628. vfork_compressor(tbInfo->tarFd, gzip);
  629. # endif
  630. /* Read the directory/files and iterate over them one at a time */
  631. while (filelist) {
  632. if (!recursive_action(filelist->data, recurseFlags,
  633. writeFileToTarball, writeFileToTarball, tbInfo)
  634. ) {
  635. errorFlag = TRUE;
  636. }
  637. filelist = filelist->link;
  638. }
  639. /* Write two empty blocks to the end of the archive */
  640. memset(block_buf, 0, 2*TAR_BLOCK_SIZE);
  641. xwrite(tbInfo->tarFd, block_buf, 2*TAR_BLOCK_SIZE);
  642. /* To be pedantically correct, we would check if the tarball
  643. * is smaller than 20 tar blocks, and pad it if it was smaller,
  644. * but that isn't necessary for GNU tar interoperability, and
  645. * so is considered a waste of space */
  646. /* Close so the child process (if any) will exit */
  647. close(tbInfo->tarFd);
  648. /* Hang up the tools, close up shop, head home */
  649. if (ENABLE_FEATURE_CLEAN_UP)
  650. freeHardLinkInfo(&tbInfo->hlInfoHead);
  651. if (errorFlag)
  652. bb_simple_error_msg("error exit delayed from previous errors");
  653. # if SEAMLESS_COMPRESSION
  654. if (gzip) {
  655. int status;
  656. if (safe_waitpid(-1, &status, 0) == -1)
  657. bb_simple_perror_msg("waitpid");
  658. else if (!WIFEXITED(status) || WEXITSTATUS(status))
  659. /* gzip was killed or has exited with nonzero! */
  660. errorFlag = TRUE;
  661. }
  662. # endif
  663. return errorFlag;
  664. }
  665. #endif /* FEATURE_TAR_CREATE */
  666. #if ENABLE_FEATURE_TAR_FROM
  667. static llist_t *append_file_list_to_list(llist_t *list)
  668. {
  669. llist_t *newlist = NULL;
  670. while (list) {
  671. FILE *src_stream;
  672. char *line;
  673. src_stream = xfopen_stdin(llist_pop(&list));
  674. while ((line = xmalloc_fgetline(src_stream)) != NULL) {
  675. /* kill trailing '/' unless the string is just "/" */
  676. char *cp = last_char_is(line, '/');
  677. if (cp > line)
  678. *cp = '\0';
  679. llist_add_to_end(&newlist, line);
  680. }
  681. fclose(src_stream);
  682. }
  683. return newlist;
  684. }
  685. #endif
  686. //usage:#define tar_trivial_usage
  687. //usage: IF_FEATURE_TAR_CREATE("c|") "x|t [-"
  688. //usage: IF_FEATURE_SEAMLESS_Z("Z")
  689. //usage: IF_FEATURE_SEAMLESS_GZ("z")
  690. //usage: IF_FEATURE_SEAMLESS_XZ("J")
  691. //usage: IF_FEATURE_SEAMLESS_BZ2("j")
  692. //usage: "a"
  693. //usage: IF_FEATURE_TAR_CREATE("h")
  694. //usage: IF_FEATURE_TAR_NOPRESERVE_TIME("m")
  695. //usage: "vokO] "
  696. //usage: "[-f TARFILE] [-C DIR] "
  697. //usage: IF_FEATURE_TAR_FROM("[-T FILE] [-X FILE] "IF_FEATURE_TAR_LONG_OPTIONS("[OPTION]... "))
  698. //usage: "[FILE]..."
  699. //usage:#define tar_full_usage "\n\n"
  700. //usage: IF_FEATURE_TAR_CREATE("Create, extract, ")
  701. //usage: IF_NOT_FEATURE_TAR_CREATE("Extract ")
  702. //usage: "or list files from a tar file"
  703. //usage: "\n"
  704. //usage: IF_FEATURE_TAR_CREATE(
  705. //usage: "\n c Create"
  706. //usage: )
  707. //usage: "\n x Extract"
  708. //usage: "\n t List"
  709. //usage: "\n -f FILE Name of TARFILE ('-' for stdin/out)"
  710. //usage: "\n -C DIR Change to DIR before operation"
  711. //usage: "\n -v Verbose"
  712. //usage: "\n -O Extract to stdout"
  713. //usage: IF_FEATURE_TAR_NOPRESERVE_TIME(
  714. //usage: "\n -m Don't restore mtime"
  715. //usage: )
  716. //usage: "\n -o Don't restore user:group"
  717. ///////:-p - accepted but ignored, restores mode (aliases in GNU tar: --preserve-permissions, --same-permissions)
  718. //usage: "\n -k Don't replace existing files"
  719. //usage: IF_FEATURE_SEAMLESS_Z(
  720. //usage: "\n -Z (De)compress using compress"
  721. //usage: )
  722. //usage: IF_FEATURE_SEAMLESS_GZ(
  723. //usage: "\n -z (De)compress using gzip"
  724. //usage: )
  725. //usage: IF_FEATURE_SEAMLESS_XZ(
  726. //usage: "\n -J (De)compress using xz"
  727. //usage: )
  728. //usage: IF_FEATURE_SEAMLESS_BZ2(
  729. //usage: "\n -j (De)compress using bzip2"
  730. //usage: )
  731. //usage: IF_FEATURE_SEAMLESS_LZMA(
  732. //usage: IF_FEATURE_TAR_LONG_OPTIONS(
  733. //usage: "\n --lzma (De)compress using lzma"
  734. //usage: )
  735. //usage: )
  736. //usage: "\n -a (De)compress based on extension"
  737. //usage: IF_FEATURE_TAR_CREATE(
  738. //usage: "\n -h Follow symlinks"
  739. //usage: )
  740. //usage: IF_FEATURE_TAR_FROM(
  741. //usage: "\n -T FILE File with names to include"
  742. //usage: "\n -X FILE File with glob patterns to exclude"
  743. //usage: IF_FEATURE_TAR_LONG_OPTIONS(
  744. //usage: "\n --exclude PATTERN Glob pattern to exclude"
  745. //usage: )
  746. //usage: )
  747. //usage: IF_FEATURE_TAR_LONG_OPTIONS(
  748. //usage: "\n --overwrite Replace existing files"
  749. //usage: "\n --strip-components NUM NUM of leading components to strip"
  750. //usage: "\n --no-recursion Don't descend in directories"
  751. //usage: "\n --numeric-owner Use numeric user:group"
  752. //usage: "\n --no-same-permissions Don't restore access permissions"
  753. //usage: IF_FEATURE_TAR_TO_COMMAND(
  754. //usage: "\n --to-command COMMAND Pipe files to COMMAND"
  755. //usage: )
  756. //usage: )
  757. //usage:
  758. //usage:#define tar_example_usage
  759. //usage: "$ zcat /tmp/tarball.tar.gz | tar -xf -\n"
  760. //usage: "$ tar -cf /tmp/tarball.tar /usr/local\n"
  761. enum {
  762. OPTBIT_KEEP_OLD = 8,
  763. IF_FEATURE_TAR_CREATE( OPTBIT_CREATE ,)
  764. IF_FEATURE_TAR_CREATE( OPTBIT_DEREFERENCE ,)
  765. IF_FEATURE_SEAMLESS_BZ2( OPTBIT_BZIP2 ,)
  766. IF_FEATURE_TAR_FROM( OPTBIT_INCLUDE_FROM,)
  767. IF_FEATURE_TAR_FROM( OPTBIT_EXCLUDE_FROM,)
  768. IF_FEATURE_SEAMLESS_GZ( OPTBIT_GZIP ,)
  769. IF_FEATURE_SEAMLESS_XZ( OPTBIT_XZ ,)
  770. IF_FEATURE_SEAMLESS_Z( OPTBIT_COMPRESS ,) // 16th bit
  771. OPTBIT_AUTOCOMPRESS_BY_EXT,
  772. IF_FEATURE_TAR_NOPRESERVE_TIME(OPTBIT_NOPRESERVE_TIME,)
  773. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  774. OPTBIT_STRIP_COMPONENTS,
  775. IF_FEATURE_SEAMLESS_LZMA(OPTBIT_LZMA ,)
  776. OPTBIT_NORECURSION,
  777. IF_FEATURE_TAR_TO_COMMAND(OPTBIT_2COMMAND ,)
  778. OPTBIT_NUMERIC_OWNER,
  779. OPTBIT_NOPRESERVE_PERM,
  780. OPTBIT_OVERWRITE,
  781. #endif
  782. OPT_TEST = 1 << 0, // t
  783. OPT_EXTRACT = 1 << 1, // x
  784. OPT_BASEDIR = 1 << 2, // C
  785. OPT_TARNAME = 1 << 3, // f
  786. OPT_2STDOUT = 1 << 4, // O
  787. OPT_NOPRESERVE_OWNER = 1 << 5, // o == no-same-owner
  788. OPT_P = 1 << 6, // p
  789. OPT_VERBOSE = 1 << 7, // v
  790. OPT_KEEP_OLD = 1 << 8, // k
  791. OPT_CREATE = IF_FEATURE_TAR_CREATE( (1 << OPTBIT_CREATE )) + 0, // c
  792. OPT_DEREFERENCE = IF_FEATURE_TAR_CREATE( (1 << OPTBIT_DEREFERENCE )) + 0, // h
  793. OPT_BZIP2 = IF_FEATURE_SEAMLESS_BZ2( (1 << OPTBIT_BZIP2 )) + 0, // j
  794. OPT_INCLUDE_FROM = IF_FEATURE_TAR_FROM( (1 << OPTBIT_INCLUDE_FROM)) + 0, // T
  795. OPT_EXCLUDE_FROM = IF_FEATURE_TAR_FROM( (1 << OPTBIT_EXCLUDE_FROM)) + 0, // X
  796. OPT_GZIP = IF_FEATURE_SEAMLESS_GZ( (1 << OPTBIT_GZIP )) + 0, // z
  797. OPT_XZ = IF_FEATURE_SEAMLESS_XZ( (1 << OPTBIT_XZ )) + 0, // J
  798. OPT_COMPRESS = IF_FEATURE_SEAMLESS_Z( (1 << OPTBIT_COMPRESS )) + 0, // Z
  799. OPT_AUTOCOMPRESS_BY_EXT = 1 << OPTBIT_AUTOCOMPRESS_BY_EXT, // a
  800. OPT_NOPRESERVE_TIME = IF_FEATURE_TAR_NOPRESERVE_TIME((1 << OPTBIT_NOPRESERVE_TIME)) + 0, // m
  801. OPT_STRIP_COMPONENTS = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_STRIP_COMPONENTS)) + 0, // strip-components
  802. OPT_LZMA = IF_FEATURE_TAR_LONG_OPTIONS(IF_FEATURE_SEAMLESS_LZMA((1 << OPTBIT_LZMA))) + 0, // lzma
  803. OPT_NORECURSION = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NORECURSION )) + 0, // no-recursion
  804. OPT_2COMMAND = IF_FEATURE_TAR_TO_COMMAND( (1 << OPTBIT_2COMMAND )) + 0, // to-command
  805. OPT_NUMERIC_OWNER = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NUMERIC_OWNER )) + 0, // numeric-owner
  806. OPT_NOPRESERVE_PERM = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NOPRESERVE_PERM)) + 0, // no-same-permissions
  807. OPT_OVERWRITE = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_OVERWRITE )) + 0, // overwrite
  808. OPT_ANY_COMPRESS = (OPT_BZIP2 | OPT_LZMA | OPT_GZIP | OPT_XZ | OPT_COMPRESS),
  809. };
  810. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  811. static const char tar_longopts[] ALIGN1 =
  812. "list\0" No_argument "t"
  813. "extract\0" No_argument "x"
  814. "directory\0" Required_argument "C"
  815. "file\0" Required_argument "f"
  816. "to-stdout\0" No_argument "O"
  817. /* do not restore owner */
  818. /* Note: GNU tar handles 'o' as no-same-owner only on extract,
  819. * on create, 'o' is --old-archive. We do not support --old-archive. */
  820. "no-same-owner\0" No_argument "o"
  821. "same-permissions\0" No_argument "p"
  822. "verbose\0" No_argument "v"
  823. "keep-old\0" No_argument "k"
  824. # if ENABLE_FEATURE_TAR_CREATE
  825. "create\0" No_argument "c"
  826. "dereference\0" No_argument "h"
  827. # endif
  828. # if ENABLE_FEATURE_SEAMLESS_BZ2
  829. "bzip2\0" No_argument "j"
  830. # endif
  831. # if ENABLE_FEATURE_TAR_FROM
  832. "files-from\0" Required_argument "T"
  833. "exclude-from\0" Required_argument "X"
  834. # endif
  835. # if ENABLE_FEATURE_SEAMLESS_GZ
  836. "gzip\0" No_argument "z"
  837. # endif
  838. # if ENABLE_FEATURE_SEAMLESS_XZ
  839. "xz\0" No_argument "J"
  840. # endif
  841. # if ENABLE_FEATURE_SEAMLESS_Z
  842. "compress\0" No_argument "Z"
  843. # endif
  844. "auto-compress\0" No_argument "a"
  845. # if ENABLE_FEATURE_TAR_NOPRESERVE_TIME
  846. "touch\0" No_argument "m"
  847. # endif
  848. "strip-components\0" Required_argument "\xf8"
  849. # if ENABLE_FEATURE_SEAMLESS_LZMA
  850. "lzma\0" No_argument "\xf9"
  851. # endif
  852. "no-recursion\0" No_argument "\xfa"
  853. # if ENABLE_FEATURE_TAR_TO_COMMAND
  854. "to-command\0" Required_argument "\xfb"
  855. # endif
  856. /* use numeric uid/gid from tar header, not textual */
  857. "numeric-owner\0" No_argument "\xfc"
  858. /* do not restore mode */
  859. "no-same-permissions\0" No_argument "\xfd"
  860. /* on unpack, open with O_TRUNC and !O_EXCL */
  861. "overwrite\0" No_argument "\xfe"
  862. /* --exclude takes next bit position in option mask, */
  863. /* therefore we have to put it _after_ --no-same-permissions */
  864. # if ENABLE_FEATURE_TAR_FROM
  865. "exclude\0" Required_argument "\xff"
  866. # endif
  867. ;
  868. # define GETOPT32 getopt32long
  869. # define LONGOPTS ,tar_longopts
  870. #else
  871. # define GETOPT32 getopt32
  872. # define LONGOPTS
  873. #endif
  874. int tar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  875. int tar_main(int argc UNUSED_PARAM, char **argv)
  876. {
  877. archive_handle_t *tar_handle;
  878. char *base_dir = NULL;
  879. const char *tar_filename = "-";
  880. unsigned opt;
  881. int verboseFlag = 0;
  882. #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
  883. llist_t *excludes = NULL;
  884. #endif
  885. INIT_G();
  886. /* Initialise default values */
  887. tar_handle = init_handle();
  888. tar_handle->ah_flags = ARCHIVE_CREATE_LEADING_DIRS
  889. | ARCHIVE_RESTORE_DATE
  890. | ARCHIVE_UNLINK_OLD;
  891. /* Apparently only root's tar preserves perms (see bug 3844) */
  892. if (getuid() != 0)
  893. tar_handle->ah_flags |= ARCHIVE_DONT_RESTORE_PERM;
  894. #if ENABLE_DESKTOP
  895. /* Lie to buildroot when it starts asking stupid questions. */
  896. if (argv[1] && strcmp(argv[1], "--version") == 0) {
  897. // Output of 'tar --version' examples:
  898. // tar (GNU tar) 1.15.1
  899. // tar (GNU tar) 1.25
  900. // bsdtar 2.8.3 - libarchive 2.8.3
  901. puts("tar (busybox) " BB_VER);
  902. return 0;
  903. }
  904. #endif
  905. if (argv[1] && argv[1][0] != '-' && argv[1][0] != '\0') {
  906. /* Compat:
  907. * 1st argument without dash handles options with parameters
  908. * differently from dashed one: it takes *next argv[i]*
  909. * as parameter even if there are more chars in 1st argument:
  910. * "tar fx TARFILE" - "x" is not taken as f's param
  911. * but is interpreted as -x option
  912. * "tar -xf TARFILE" - dashed equivalent of the above
  913. * "tar -fx ..." - "x" is taken as f's param
  914. * getopt32 wouldn't handle 1st command correctly.
  915. * Unfortunately, people do use such commands.
  916. * We massage argv[1] to work around it by moving 'f'
  917. * to the end of the string.
  918. * More contrived "tar fCx TARFILE DIR" still fails,
  919. * but such commands are much less likely to be used.
  920. */
  921. char *f = strchr(argv[1], 'f');
  922. if (f) {
  923. while (f[1] != '\0') {
  924. *f = f[1];
  925. f++;
  926. }
  927. *f = 'f';
  928. }
  929. /* Prepend '-' to the first argument */
  930. argv[1] = xasprintf("-%s", argv[1]);
  931. }
  932. opt = GETOPT32(argv, "^"
  933. "txC:f:Oopvk"
  934. IF_FEATURE_TAR_CREATE( "ch" )
  935. IF_FEATURE_SEAMLESS_BZ2( "j" )
  936. IF_FEATURE_TAR_FROM( "T:*X:*")
  937. IF_FEATURE_SEAMLESS_GZ( "z" )
  938. IF_FEATURE_SEAMLESS_XZ( "J" )
  939. IF_FEATURE_SEAMLESS_Z( "Z" )
  940. "a"
  941. IF_FEATURE_TAR_NOPRESERVE_TIME("m")
  942. IF_FEATURE_TAR_LONG_OPTIONS("\xf8:") // --strip-components
  943. "\0"
  944. "tt:vv:" // count -t,-v
  945. #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
  946. "\xff::" // --exclude=PATTERN is a list
  947. #endif
  948. IF_FEATURE_TAR_CREATE("c:") "t:x:" // at least one of these is reqd
  949. IF_FEATURE_TAR_CREATE("c--tx:t--cx:x--ct") // mutually exclusive
  950. IF_NOT_FEATURE_TAR_CREATE("t--x:x--t") // mutually exclusive
  951. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  952. ":\xf8+" // --strip-components=NUM
  953. #endif
  954. LONGOPTS
  955. , &base_dir // -C dir
  956. , &tar_filename // -f filename
  957. IF_FEATURE_TAR_FROM(, &(tar_handle->accept)) // T
  958. IF_FEATURE_TAR_FROM(, &(tar_handle->reject)) // X
  959. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  960. , &tar_handle->tar__strip_components // --strip-components
  961. #endif
  962. IF_FEATURE_TAR_TO_COMMAND(, &(tar_handle->tar__to_command)) // --to-command
  963. #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
  964. , &excludes // --exclude
  965. #endif
  966. , &verboseFlag // combined count for -t and -v
  967. , &verboseFlag // combined count for -t and -v
  968. );
  969. #if DBG_OPTION_PARSING
  970. bb_error_msg("opt: 0x%08x", opt);
  971. # define showopt(o) bb_error_msg("opt & %s(%x):\t%x", #o, o, opt & o);
  972. showopt(OPT_TEST );
  973. showopt(OPT_EXTRACT );
  974. showopt(OPT_BASEDIR );
  975. showopt(OPT_TARNAME );
  976. showopt(OPT_2STDOUT );
  977. showopt(OPT_NOPRESERVE_OWNER);
  978. showopt(OPT_P );
  979. showopt(OPT_VERBOSE );
  980. showopt(OPT_KEEP_OLD );
  981. showopt(OPT_CREATE );
  982. showopt(OPT_DEREFERENCE );
  983. showopt(OPT_BZIP2 );
  984. showopt(OPT_INCLUDE_FROM );
  985. showopt(OPT_EXCLUDE_FROM );
  986. showopt(OPT_GZIP );
  987. showopt(OPT_XZ );
  988. showopt(OPT_COMPRESS );
  989. showopt(OPT_AUTOCOMPRESS_BY_EXT);
  990. showopt(OPT_NOPRESERVE_TIME );
  991. showopt(OPT_STRIP_COMPONENTS);
  992. showopt(OPT_LZMA );
  993. showopt(OPT_NORECURSION );
  994. showopt(OPT_2COMMAND );
  995. showopt(OPT_NUMERIC_OWNER );
  996. showopt(OPT_NOPRESERVE_PERM );
  997. showopt(OPT_OVERWRITE );
  998. showopt(OPT_ANY_COMPRESS );
  999. bb_error_msg("base_dir:'%s'", base_dir);
  1000. bb_error_msg("tar_filename:'%s'", tar_filename);
  1001. bb_error_msg("verboseFlag:%d", verboseFlag);
  1002. bb_error_msg("tar_handle->tar__to_command:'%s'", tar_handle->tar__to_command);
  1003. bb_error_msg("tar_handle->tar__strip_components:%u", tar_handle->tar__strip_components);
  1004. return 0;
  1005. # undef showopt
  1006. #endif
  1007. argv += optind;
  1008. if (verboseFlag)
  1009. tar_handle->action_header = header_verbose_list;
  1010. if (verboseFlag == 1)
  1011. tar_handle->action_header = header_list;
  1012. if (opt & OPT_EXTRACT)
  1013. tar_handle->action_data = data_extract_all;
  1014. if (opt & OPT_2STDOUT)
  1015. tar_handle->action_data = data_extract_to_stdout;
  1016. if (opt & OPT_2COMMAND) {
  1017. putenv((char*)"TAR_FILETYPE=f");
  1018. signal(SIGPIPE, SIG_IGN);
  1019. tar_handle->action_data = data_extract_to_command;
  1020. IF_FEATURE_TAR_TO_COMMAND(tar_handle->tar__to_command_shell = xstrdup(get_shell_name());)
  1021. }
  1022. if (opt & OPT_KEEP_OLD)
  1023. tar_handle->ah_flags &= ~ARCHIVE_UNLINK_OLD;
  1024. if (opt & OPT_NUMERIC_OWNER)
  1025. tar_handle->ah_flags |= ARCHIVE_NUMERIC_OWNER;
  1026. if (opt & OPT_NOPRESERVE_OWNER)
  1027. tar_handle->ah_flags |= ARCHIVE_DONT_RESTORE_OWNER;
  1028. if (opt & OPT_NOPRESERVE_PERM)
  1029. tar_handle->ah_flags |= ARCHIVE_DONT_RESTORE_PERM;
  1030. if (opt & OPT_OVERWRITE) {
  1031. tar_handle->ah_flags &= ~ARCHIVE_UNLINK_OLD;
  1032. tar_handle->ah_flags |= ARCHIVE_O_TRUNC;
  1033. }
  1034. if (opt & OPT_NOPRESERVE_TIME)
  1035. tar_handle->ah_flags &= ~ARCHIVE_RESTORE_DATE;
  1036. #if ENABLE_FEATURE_TAR_FROM
  1037. tar_handle->reject = append_file_list_to_list(tar_handle->reject);
  1038. # if ENABLE_FEATURE_TAR_LONG_OPTIONS
  1039. /* Append excludes to reject */
  1040. while (excludes) {
  1041. llist_t *next = excludes->link;
  1042. excludes->link = tar_handle->reject;
  1043. tar_handle->reject = excludes;
  1044. excludes = next;
  1045. }
  1046. # endif
  1047. tar_handle->accept = append_file_list_to_list(tar_handle->accept);
  1048. #endif
  1049. /* Setup an array of filenames to work with */
  1050. /* TODO: This is the same as in ar, make a separate function? */
  1051. while (*argv) {
  1052. /* kill trailing '/' unless the string is just "/" */
  1053. char *cp = last_char_is(*argv, '/');
  1054. if (cp > *argv)
  1055. *cp = '\0';
  1056. llist_add_to_end(&tar_handle->accept, *argv);
  1057. argv++;
  1058. }
  1059. if (tar_handle->accept || tar_handle->reject)
  1060. tar_handle->filter = filter_accept_reject_list;
  1061. /* Open the tar file */
  1062. {
  1063. int tar_fd = STDIN_FILENO;
  1064. int flags = O_RDONLY;
  1065. if (opt & OPT_CREATE) {
  1066. /* Make sure there is at least one file to tar up */
  1067. if (tar_handle->accept == NULL)
  1068. bb_simple_error_msg_and_die("empty archive");
  1069. tar_fd = STDOUT_FILENO;
  1070. /* Mimicking GNU tar 1.15.1: */
  1071. flags = O_WRONLY | O_CREAT | O_TRUNC;
  1072. }
  1073. if (LONE_DASH(tar_filename)) {
  1074. tar_handle->src_fd = tar_fd;
  1075. tar_handle->seek = seek_by_read;
  1076. } else
  1077. if (ENABLE_FEATURE_TAR_AUTODETECT
  1078. && ENABLE_FEATURE_SEAMLESS_LZMA
  1079. && flags == O_RDONLY
  1080. && !(opt & OPT_ANY_COMPRESS)
  1081. && is_suffixed_with(tar_filename, ".lzma")
  1082. /* We do this only for .lzma files, they have no signature.
  1083. * All other compression formats are recognized in
  1084. * get_header_tar() when first tar block has invalid format.
  1085. * Doing it here for all filenames would falsely trigger
  1086. * on e.g. tarball with 1st file named "BZh5".
  1087. */
  1088. ) {
  1089. tar_handle->src_fd = open_zipped(tar_filename, /*fail_if_not_compressed:*/ 0);
  1090. if (tar_handle->src_fd < 0)
  1091. bb_perror_msg_and_die("can't open '%s'", tar_filename);
  1092. } else {
  1093. tar_handle->src_fd = xopen(tar_filename, flags);
  1094. #if ENABLE_FEATURE_TAR_CREATE
  1095. if ((OPT_GZIP | OPT_BZIP2 | OPT_XZ | OPT_LZMA) != 0 /* at least one is config-enabled */
  1096. && (opt & OPT_AUTOCOMPRESS_BY_EXT)
  1097. && flags != O_RDONLY
  1098. ) {
  1099. if (OPT_GZIP != 0 && is_suffixed_with(tar_filename, "gz"))
  1100. opt |= OPT_GZIP;
  1101. if (OPT_BZIP2 != 0 && is_suffixed_with(tar_filename, "bz2"))
  1102. opt |= OPT_BZIP2;
  1103. if (OPT_XZ != 0 && is_suffixed_with(tar_filename, "xz"))
  1104. opt |= OPT_XZ;
  1105. if (OPT_LZMA != 0 && is_suffixed_with(tar_filename, "lzma"))
  1106. opt |= OPT_LZMA;
  1107. }
  1108. #endif
  1109. }
  1110. }
  1111. if (base_dir)
  1112. xchdir(base_dir);
  1113. #if ENABLE_FEATURE_TAR_CREATE
  1114. /* Create an archive */
  1115. if (opt & OPT_CREATE) {
  1116. struct TarBallInfo *tbInfo;
  1117. # if SEAMLESS_COMPRESSION
  1118. const char *zipMode = NULL;
  1119. if (opt & OPT_COMPRESS)
  1120. zipMode = "compress";
  1121. if (opt & OPT_GZIP)
  1122. zipMode = "gzip";
  1123. if (opt & OPT_BZIP2)
  1124. zipMode = "bzip2";
  1125. if (opt & OPT_LZMA)
  1126. zipMode = "lzma";
  1127. if (opt & OPT_XZ)
  1128. zipMode = "xz";
  1129. # endif
  1130. tbInfo = xzalloc(sizeof(*tbInfo));
  1131. tbInfo->tarFd = tar_handle->src_fd;
  1132. tbInfo->verboseFlag = verboseFlag;
  1133. # if ENABLE_FEATURE_TAR_FROM
  1134. tbInfo->excludeList = tar_handle->reject;
  1135. # endif
  1136. /* NB: writeTarFile() closes tar_handle->src_fd */
  1137. return writeTarFile(tbInfo,
  1138. (opt & OPT_DEREFERENCE ? ACTION_FOLLOWLINKS : 0)
  1139. | (opt & OPT_NORECURSION ? 0 : ACTION_RECURSE),
  1140. tar_handle->accept,
  1141. zipMode);
  1142. }
  1143. #endif
  1144. if (opt & OPT_ANY_COMPRESS) {
  1145. USE_FOR_MMU(IF_DESKTOP(long long) int FAST_FUNC (*xformer)(transformer_state_t *xstate);)
  1146. USE_FOR_NOMMU(const char *xformer_prog;)
  1147. if (opt & OPT_COMPRESS) {
  1148. USE_FOR_MMU(IF_FEATURE_SEAMLESS_Z(xformer = unpack_Z_stream;))
  1149. USE_FOR_NOMMU(xformer_prog = "uncompress";)
  1150. }
  1151. if (opt & OPT_GZIP) {
  1152. USE_FOR_MMU(IF_FEATURE_SEAMLESS_GZ(xformer = unpack_gz_stream;))
  1153. USE_FOR_NOMMU(xformer_prog = "gunzip";)
  1154. }
  1155. if (opt & OPT_BZIP2) {
  1156. USE_FOR_MMU(IF_FEATURE_SEAMLESS_BZ2(xformer = unpack_bz2_stream;))
  1157. USE_FOR_NOMMU(xformer_prog = "bunzip2";)
  1158. }
  1159. if (opt & OPT_LZMA) {
  1160. USE_FOR_MMU(IF_FEATURE_SEAMLESS_LZMA(xformer = unpack_lzma_stream;))
  1161. USE_FOR_NOMMU(xformer_prog = "unlzma";)
  1162. }
  1163. if (opt & OPT_XZ) {
  1164. USE_FOR_MMU(IF_FEATURE_SEAMLESS_XZ(xformer = unpack_xz_stream;))
  1165. USE_FOR_NOMMU(xformer_prog = "unxz";)
  1166. }
  1167. fork_transformer_with_sig(tar_handle->src_fd, xformer, xformer_prog);
  1168. /* Can't lseek over pipes */
  1169. tar_handle->seek = seek_by_read;
  1170. /*tar_handle->offset = 0; - already is */
  1171. }
  1172. /* Zero processed headers (== empty file) is not a valid tarball.
  1173. * We (ab)use bb_got_signal as exitcode here,
  1174. * because check_errors_in_children() uses _it_ as error indicator.
  1175. */
  1176. bb_got_signal = EXIT_FAILURE;
  1177. while (get_header_tar(tar_handle) == EXIT_SUCCESS)
  1178. bb_got_signal = EXIT_SUCCESS; /* saw at least one header, good */
  1179. create_links_from_list(tar_handle->link_placeholders);
  1180. /* Check that every file that should have been extracted was */
  1181. while (tar_handle->accept) {
  1182. if (!find_list_entry(tar_handle->reject, tar_handle->accept->data)
  1183. && !find_list_entry(tar_handle->passed, tar_handle->accept->data)
  1184. ) {
  1185. bb_error_msg_and_die("%s: not found in archive",
  1186. tar_handle->accept->data);
  1187. }
  1188. tar_handle->accept = tar_handle->accept->link;
  1189. }
  1190. if (ENABLE_FEATURE_CLEAN_UP /* && tar_handle->src_fd != STDIN_FILENO */)
  1191. close(tar_handle->src_fd);
  1192. if (SEAMLESS_COMPRESSION || OPT_COMPRESS) {
  1193. /* Set bb_got_signal to 1 if a child died with !0 exitcode */
  1194. check_errors_in_children(0);
  1195. }
  1196. return bb_got_signal;
  1197. }