tar.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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. # if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  237. static void writeLongname(int fd, int type, const char *name, int dir)
  238. {
  239. struct prefilled {
  240. char mode[8]; /* 100-107 */
  241. char uid[8]; /* 108-115 */
  242. char gid[8]; /* 116-123 */
  243. char size[12]; /* 124-135 */
  244. char mtime[12]; /* 136-147 */
  245. };
  246. struct tar_header_t header;
  247. int size;
  248. memset(&header, 0, sizeof(header));
  249. header.typeflag = type;
  250. strcpy(header.name, "././@LongLink");
  251. /* This sets mode/uid/gid/mtime to "00...00<NUL>" strings */
  252. memset((char*)&header + offsetof(struct tar_header_t, mode), /* make gcc-9.x happy */
  253. '0', sizeof(struct prefilled));
  254. header.mode [sizeof(header.mode ) - 1] = '\0';
  255. header.uid [sizeof(header.uid ) - 1] = '\0';
  256. header.gid [sizeof(header.gid ) - 1] = '\0';
  257. /* header.size is filled by '0' now, will be corrected below */
  258. header.mtime[sizeof(header.mtime) - 1] = '\0';
  259. dir = !!dir; /* normalize: 0/1 */
  260. size = strlen(name) + 1 + dir; /* GNU tar uses strlen+1 */
  261. /* + dir: account for possible '/' */
  262. PUT_OCTAL(header.size, size);
  263. chksum_and_xwrite_tar_header(fd, &header);
  264. /* Write filename[/] and pad the block. */
  265. /* dir=0: writes 'name<NUL>', pads */
  266. /* dir=1: writes 'name', writes '/<NUL>', pads */
  267. dir *= 2;
  268. xwrite(fd, name, size - dir);
  269. xwrite(fd, "/", dir);
  270. size = (-size) & (TAR_BLOCK_SIZE-1);
  271. memset(&header, 0, size);
  272. xwrite(fd, &header, size);
  273. }
  274. # endif
  275. /* Write out a tar header for the specified file/directory/whatever */
  276. static int writeTarHeader(struct TarBallInfo *tbInfo,
  277. const char *header_name, const char *fileName, struct stat *statbuf)
  278. {
  279. struct tar_header_t header;
  280. memset(&header, 0, sizeof(header));
  281. strncpy(header.name, header_name, sizeof(header.name));
  282. /* POSIX says to mask mode with 07777. */
  283. PUT_OCTAL(header.mode, statbuf->st_mode & 07777);
  284. PUT_OCTAL(header.uid, statbuf->st_uid);
  285. PUT_OCTAL(header.gid, statbuf->st_gid);
  286. memset(header.size, '0', sizeof(header.size)-1); /* Regular file size is handled later */
  287. /* users report that files with negative st_mtime cause trouble, so: */
  288. PUT_OCTAL(header.mtime, statbuf->st_mtime >= 0 ? statbuf->st_mtime : 0);
  289. /* Enter the user and group names */
  290. safe_strncpy(header.uname, get_cached_username(statbuf->st_uid), sizeof(header.uname));
  291. safe_strncpy(header.gname, get_cached_groupname(statbuf->st_gid), sizeof(header.gname));
  292. if (tbInfo->hlInfo) {
  293. /* This is a hard link */
  294. header.typeflag = LNKTYPE;
  295. strncpy(header.linkname, tbInfo->hlInfo->name,
  296. sizeof(header.linkname));
  297. # if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  298. /* Write out long linkname if needed */
  299. if (header.linkname[sizeof(header.linkname)-1])
  300. writeLongname(tbInfo->tarFd, GNULONGLINK,
  301. tbInfo->hlInfo->name, 0);
  302. # endif
  303. } else if (S_ISLNK(statbuf->st_mode)) {
  304. char *lpath = xmalloc_readlink_or_warn(fileName);
  305. if (!lpath)
  306. return FALSE;
  307. header.typeflag = SYMTYPE;
  308. strncpy(header.linkname, lpath, sizeof(header.linkname));
  309. # if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  310. /* Write out long linkname if needed */
  311. if (header.linkname[sizeof(header.linkname)-1])
  312. writeLongname(tbInfo->tarFd, GNULONGLINK, lpath, 0);
  313. # else
  314. /* If it is larger than 100 bytes, bail out */
  315. if (header.linkname[sizeof(header.linkname)-1]) {
  316. free(lpath);
  317. bb_simple_error_msg("names longer than "NAME_SIZE_STR" chars not supported");
  318. return FALSE;
  319. }
  320. # endif
  321. free(lpath);
  322. } else if (S_ISDIR(statbuf->st_mode)) {
  323. header.typeflag = DIRTYPE;
  324. /* Append '/' only if there is a space for it */
  325. if (!header.name[sizeof(header.name)-1])
  326. header.name[strlen(header.name)] = '/';
  327. } else if (S_ISCHR(statbuf->st_mode)) {
  328. header.typeflag = CHRTYPE;
  329. PUT_OCTAL(header.devmajor, major(statbuf->st_rdev));
  330. PUT_OCTAL(header.devminor, minor(statbuf->st_rdev));
  331. } else if (S_ISBLK(statbuf->st_mode)) {
  332. header.typeflag = BLKTYPE;
  333. PUT_OCTAL(header.devmajor, major(statbuf->st_rdev));
  334. PUT_OCTAL(header.devminor, minor(statbuf->st_rdev));
  335. } else if (S_ISFIFO(statbuf->st_mode)) {
  336. header.typeflag = FIFOTYPE;
  337. } else if (S_ISREG(statbuf->st_mode)) {
  338. /* header.size field is 12 bytes long */
  339. /* Does octal-encoded size fit? */
  340. uoff_t filesize = statbuf->st_size;
  341. if (sizeof(filesize) <= 4
  342. || filesize <= (uoff_t)0777777777777LL
  343. ) {
  344. PUT_OCTAL(header.size, filesize);
  345. }
  346. /* Does base256-encoded size fit?
  347. * It always does unless off_t is wider than 64 bits.
  348. */
  349. else if (ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  350. # if ULLONG_MAX > 0xffffffffffffffffLL /* 2^64-1 */
  351. && (filesize <= 0x3fffffffffffffffffffffffLL)
  352. # endif
  353. ) {
  354. /* GNU tar uses "base-256 encoding" for very large numbers.
  355. * Encoding is binary, with highest bit always set as a marker
  356. * and sign in next-highest bit:
  357. * 80 00 .. 00 - zero
  358. * bf ff .. ff - largest positive number
  359. * ff ff .. ff - minus 1
  360. * c0 00 .. 00 - smallest negative number
  361. */
  362. char *p8 = header.size + sizeof(header.size);
  363. do {
  364. *--p8 = (uint8_t)filesize;
  365. filesize >>= 8;
  366. } while (p8 != header.size);
  367. *p8 |= 0x80;
  368. } else {
  369. bb_error_msg_and_die("can't store file '%s' "
  370. "of size %"OFF_FMT"u, aborting",
  371. fileName, statbuf->st_size);
  372. }
  373. header.typeflag = REGTYPE;
  374. } else {
  375. bb_error_msg("%s: unknown file type", fileName);
  376. return FALSE;
  377. }
  378. # if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  379. /* Write out long name if needed */
  380. /* (we, like GNU tar, output long linkname *before* long name) */
  381. if (header.name[sizeof(header.name)-1])
  382. writeLongname(tbInfo->tarFd, GNULONGNAME,
  383. header_name, S_ISDIR(statbuf->st_mode));
  384. # endif
  385. chksum_and_xwrite_tar_header(tbInfo->tarFd, &header);
  386. /* Now do the verbose thing (or not) */
  387. if (tbInfo->verboseFlag) {
  388. FILE *vbFd = stdout;
  389. /* If archive goes to stdout, verbose goes to stderr */
  390. if (tbInfo->tarFd == STDOUT_FILENO)
  391. vbFd = stderr;
  392. /* GNU "tar cvvf" prints "extended" listing a-la "ls -l" */
  393. /* We don't have such excesses here: for us "v" == "vv" */
  394. /* '/' is probably a GNUism */
  395. fprintf(vbFd, "%s%s\n", header_name,
  396. S_ISDIR(statbuf->st_mode) ? "/" : "");
  397. }
  398. return TRUE;
  399. }
  400. # if ENABLE_FEATURE_TAR_FROM
  401. static int exclude_file(const llist_t *excluded_files, const char *file)
  402. {
  403. while (excluded_files) {
  404. if (excluded_files->data[0] == '/') {
  405. if (fnmatch(excluded_files->data, file,
  406. FNM_PATHNAME | FNM_LEADING_DIR) == 0)
  407. return 1;
  408. } else {
  409. const char *p;
  410. for (p = file; p[0] != '\0'; p++) {
  411. if ((p == file || p[-1] == '/')
  412. && p[0] != '/'
  413. && fnmatch(excluded_files->data, p,
  414. FNM_PATHNAME | FNM_LEADING_DIR) == 0
  415. ) {
  416. return 1;
  417. }
  418. }
  419. }
  420. excluded_files = excluded_files->link;
  421. }
  422. return 0;
  423. }
  424. # else
  425. # define exclude_file(excluded_files, file) 0
  426. # endif
  427. static int FAST_FUNC writeFileToTarball(struct recursive_state *state,
  428. const char *fileName,
  429. struct stat *statbuf)
  430. {
  431. struct TarBallInfo *tbInfo = (struct TarBallInfo *) state->userData;
  432. const char *header_name;
  433. int inputFileFd = -1;
  434. DBG("writeFileToTarball('%s')", fileName);
  435. /* Strip leading '/' and such (must be before memorizing hardlink's name) */
  436. header_name = strip_unsafe_prefix(fileName);
  437. if (header_name[0] == '\0')
  438. return TRUE;
  439. if (exclude_file(tbInfo->excludeList, header_name))
  440. return SKIP; /* "do not recurse on this directory", no error message printed */
  441. /* It is against the rules to archive a socket */
  442. if (S_ISSOCK(statbuf->st_mode)) {
  443. bb_error_msg("%s: socket ignored", fileName);
  444. return TRUE;
  445. }
  446. /*
  447. * Check to see if we are dealing with a hard link.
  448. * If so -
  449. * Treat the first occurrence of a given dev/inode as a file while
  450. * treating any additional occurrences as hard links. This is done
  451. * by adding the file information to the HardLinkInfo linked list.
  452. */
  453. tbInfo->hlInfo = NULL;
  454. if (!S_ISDIR(statbuf->st_mode) && statbuf->st_nlink > 1) {
  455. DBG("'%s': st_nlink > 1", header_name);
  456. tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf);
  457. if (tbInfo->hlInfo == NULL) {
  458. DBG("'%s': addHardLinkInfo", header_name);
  459. addHardLinkInfo(&tbInfo->hlInfoHead, statbuf, header_name);
  460. }
  461. }
  462. /* It is a bad idea to store the archive we are in the process of creating,
  463. * so check the device and inode to be sure that this particular file isn't
  464. * the new tarball */
  465. if (tbInfo->tarFileStatBuf.st_dev == statbuf->st_dev
  466. && tbInfo->tarFileStatBuf.st_ino == statbuf->st_ino
  467. ) {
  468. bb_error_msg("%s: file is the archive; skipping", fileName);
  469. return TRUE;
  470. }
  471. # if !ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  472. if (strlen(header_name) >= NAME_SIZE) {
  473. bb_simple_error_msg("names longer than "NAME_SIZE_STR" chars not supported");
  474. return TRUE;
  475. }
  476. # endif
  477. /* Is this a regular file? */
  478. if (tbInfo->hlInfo == NULL && S_ISREG(statbuf->st_mode)) {
  479. /* open the file we want to archive, and make sure all is well */
  480. inputFileFd = open_or_warn(fileName, O_RDONLY);
  481. if (inputFileFd < 0) {
  482. return FALSE; /* make recursive_action() return FALSE */
  483. }
  484. }
  485. /* Add an entry to the tarball */
  486. if (writeTarHeader(tbInfo, header_name, fileName, statbuf) == FALSE) {
  487. return FALSE; /* make recursive_action() return FALSE */
  488. }
  489. /* If it was a regular file, write out the body */
  490. if (inputFileFd >= 0) {
  491. size_t readSize;
  492. /* Write the file to the archive. */
  493. /* We record size into header first, */
  494. /* and then write out file. If file shrinks in between, */
  495. /* tar will be corrupted. So we don't allow for that. */
  496. /* NB: GNU tar 1.16 warns and pads with zeroes */
  497. /* or even seeks back and updates header */
  498. bb_copyfd_exact_size(inputFileFd, tbInfo->tarFd, statbuf->st_size);
  499. ////off_t readSize;
  500. ////readSize = bb_copyfd_size(inputFileFd, tbInfo->tarFd, statbuf->st_size);
  501. ////if (readSize != statbuf->st_size && readSize >= 0) {
  502. //// bb_error_msg_and_die("short read from %s, aborting", fileName);
  503. ////}
  504. /* Check that file did not grow in between? */
  505. /* if (safe_read(inputFileFd, 1) == 1) warn but continue? */
  506. close(inputFileFd);
  507. /* Pad the file up to the tar block size */
  508. /* (a few tricks here in the name of code size) */
  509. readSize = (-(int)statbuf->st_size) & (TAR_BLOCK_SIZE-1);
  510. memset(block_buf, 0, readSize);
  511. xwrite(tbInfo->tarFd, block_buf, readSize);
  512. }
  513. return TRUE;
  514. }
  515. # if SEAMLESS_COMPRESSION
  516. /* Don't inline: vfork scares gcc and pessimizes code */
  517. static void NOINLINE vfork_compressor(int tar_fd, const char *gzip)
  518. {
  519. // On Linux, vfork never unpauses parent early, although standard
  520. // allows for that. Do we want to waste bytes checking for it?
  521. # define WAIT_FOR_CHILD 0
  522. volatile int vfork_exec_errno = 0;
  523. struct fd_pair data;
  524. # if WAIT_FOR_CHILD
  525. struct fd_pair status;
  526. xpiped_pair(status);
  527. # endif
  528. xpiped_pair(data);
  529. signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */
  530. if (xvfork() == 0) {
  531. /* child */
  532. int tfd;
  533. /* NB: close _first_, then move fds! */
  534. close(data.wr);
  535. # if WAIT_FOR_CHILD
  536. close(status.rd);
  537. /* status.wr will close only on exec -
  538. * parent waits for this close to happen */
  539. fcntl(status.wr, F_SETFD, FD_CLOEXEC);
  540. # endif
  541. /* copy it: parent's tar_fd variable must not change */
  542. tfd = tar_fd;
  543. if (tfd == 0) {
  544. /* Output tar fd may be zero.
  545. * xmove_fd(data.rd, 0) would destroy it.
  546. * Reproducer:
  547. * exec 0>&-
  548. * exec 1>&-
  549. * tar czf Z.tar.gz FILE
  550. * Swapping move_fd's order wouldn't work:
  551. * data.rd is 1 and _it_ would be destroyed.
  552. */
  553. tfd = dup(tfd);
  554. }
  555. xmove_fd(data.rd, 0);
  556. xmove_fd(tfd, 1);
  557. /* exec gzip/bzip2/... program */
  558. //BB_EXECLP(gzip, gzip, "-f", (char *)0); - WRONG for "xz",
  559. // if xz is an enabled applet, it'll be a version which
  560. // can only decompress. We do need to execute external
  561. // program, not applet.
  562. execlp(gzip, gzip, "-f", (char *)0);
  563. vfork_exec_errno = errno;
  564. _exit_FAILURE();
  565. }
  566. /* parent */
  567. xmove_fd(data.wr, tar_fd);
  568. close(data.rd);
  569. # if WAIT_FOR_CHILD
  570. close(status.wr);
  571. while (1) {
  572. /* Wait until child execs (or fails to) */
  573. char buf;
  574. int n = full_read(status.rd, &buf, 1);
  575. if (n < 0 /* && errno == EAGAIN */)
  576. continue; /* try it again */
  577. }
  578. close(status.rd);
  579. # endif
  580. if (vfork_exec_errno) {
  581. errno = vfork_exec_errno;
  582. bb_perror_msg_and_die("can't execute '%s'", gzip);
  583. }
  584. }
  585. # endif /* SEAMLESS_COMPRESSION */
  586. # if !SEAMLESS_COMPRESSION
  587. /* Do not pass gzip flag to writeTarFile() */
  588. #define writeTarFile(tbInfo, recurseFlags, filelist, gzip) \
  589. writeTarFile(tbInfo, recurseFlags, filelist)
  590. # endif
  591. /* gcc 4.2.1 inlines it, making code bigger */
  592. static NOINLINE int writeTarFile(
  593. struct TarBallInfo *tbInfo,
  594. int recurseFlags,
  595. const llist_t *filelist,
  596. const char *gzip)
  597. {
  598. int errorFlag = FALSE;
  599. /*tbInfo->hlInfoHead = NULL; - already is */
  600. /* Store the stat info for the tarball's file, so
  601. * can avoid including the tarball into itself.... */
  602. xfstat(tbInfo->tarFd, &tbInfo->tarFileStatBuf, "can't stat tar file");
  603. # if SEAMLESS_COMPRESSION
  604. if (gzip)
  605. vfork_compressor(tbInfo->tarFd, gzip);
  606. # endif
  607. /* Read the directory/files and iterate over them one at a time */
  608. while (filelist) {
  609. if (!recursive_action(filelist->data, recurseFlags,
  610. writeFileToTarball, writeFileToTarball, tbInfo)
  611. ) {
  612. errorFlag = TRUE;
  613. }
  614. filelist = filelist->link;
  615. }
  616. /* Write two empty blocks to the end of the archive */
  617. memset(block_buf, 0, 2*TAR_BLOCK_SIZE);
  618. xwrite(tbInfo->tarFd, block_buf, 2*TAR_BLOCK_SIZE);
  619. /* To be pedantically correct, we would check if the tarball
  620. * is smaller than 20 tar blocks, and pad it if it was smaller,
  621. * but that isn't necessary for GNU tar interoperability, and
  622. * so is considered a waste of space */
  623. /* Close so the child process (if any) will exit */
  624. close(tbInfo->tarFd);
  625. /* Hang up the tools, close up shop, head home */
  626. if (ENABLE_FEATURE_CLEAN_UP)
  627. freeHardLinkInfo(&tbInfo->hlInfoHead);
  628. if (errorFlag)
  629. bb_simple_error_msg("error exit delayed from previous errors");
  630. # if SEAMLESS_COMPRESSION
  631. if (gzip) {
  632. int status;
  633. if (safe_waitpid(-1, &status, 0) == -1)
  634. bb_simple_perror_msg("waitpid");
  635. else if (!WIFEXITED(status) || WEXITSTATUS(status))
  636. /* gzip was killed or has exited with nonzero! */
  637. errorFlag = TRUE;
  638. }
  639. # endif
  640. return errorFlag;
  641. }
  642. #endif /* FEATURE_TAR_CREATE */
  643. #if ENABLE_FEATURE_TAR_FROM
  644. static llist_t *append_file_list_to_list(llist_t *list)
  645. {
  646. llist_t *newlist = NULL;
  647. while (list) {
  648. FILE *src_stream;
  649. char *line;
  650. src_stream = xfopen_stdin(llist_pop(&list));
  651. while ((line = xmalloc_fgetline(src_stream)) != NULL) {
  652. /* kill trailing '/' unless the string is just "/" */
  653. char *cp = last_char_is(line, '/');
  654. if (cp > line)
  655. *cp = '\0';
  656. llist_add_to_end(&newlist, line);
  657. }
  658. fclose(src_stream);
  659. }
  660. return newlist;
  661. }
  662. #endif
  663. //usage:#define tar_trivial_usage
  664. //usage: IF_FEATURE_TAR_CREATE("c|") "x|t [-"
  665. //usage: IF_FEATURE_SEAMLESS_Z("Z")
  666. //usage: IF_FEATURE_SEAMLESS_GZ("z")
  667. //usage: IF_FEATURE_SEAMLESS_XZ("J")
  668. //usage: IF_FEATURE_SEAMLESS_BZ2("j")
  669. //usage: "a"
  670. //usage: IF_FEATURE_TAR_CREATE("h")
  671. //usage: IF_FEATURE_TAR_NOPRESERVE_TIME("m")
  672. //usage: "vokO] "
  673. //usage: "[-f TARFILE] [-C DIR] "
  674. //usage: IF_FEATURE_TAR_FROM("[-T FILE] [-X FILE] "IF_FEATURE_TAR_LONG_OPTIONS("[LONGOPT]... "))
  675. //usage: "[FILE]..."
  676. //usage:#define tar_full_usage "\n\n"
  677. //usage: IF_FEATURE_TAR_CREATE("Create, extract, ")
  678. //usage: IF_NOT_FEATURE_TAR_CREATE("Extract ")
  679. //usage: "or list files from a tar file"
  680. //usage: "\n"
  681. //usage: IF_FEATURE_TAR_CREATE(
  682. //usage: "\n c Create"
  683. //usage: )
  684. //usage: "\n x Extract"
  685. //usage: "\n t List"
  686. //usage: "\n -f FILE Name of TARFILE ('-' for stdin/out)"
  687. //usage: "\n -C DIR Change to DIR before operation"
  688. //usage: "\n -v Verbose"
  689. //usage: "\n -O Extract to stdout"
  690. //usage: IF_FEATURE_TAR_NOPRESERVE_TIME(
  691. //usage: "\n -m Don't restore mtime"
  692. //usage: )
  693. //usage: "\n -o Don't restore user:group"
  694. ///////:-p - accepted but ignored, restores mode (aliases in GNU tar: --preserve-permissions, --same-permissions)
  695. //usage: "\n -k Don't replace existing files"
  696. //usage: IF_FEATURE_SEAMLESS_Z(
  697. //usage: "\n -Z (De)compress using compress"
  698. //usage: )
  699. //usage: IF_FEATURE_SEAMLESS_GZ(
  700. //usage: "\n -z (De)compress using gzip"
  701. //usage: )
  702. //usage: IF_FEATURE_SEAMLESS_XZ(
  703. //usage: "\n -J (De)compress using xz"
  704. //usage: )
  705. //usage: IF_FEATURE_SEAMLESS_BZ2(
  706. //usage: "\n -j (De)compress using bzip2"
  707. //usage: )
  708. //usage: IF_FEATURE_SEAMLESS_LZMA(
  709. //usage: IF_FEATURE_TAR_LONG_OPTIONS(
  710. //usage: "\n --lzma (De)compress using lzma"
  711. //usage: )
  712. //usage: )
  713. //usage: "\n -a (De)compress based on extension"
  714. //usage: IF_FEATURE_TAR_CREATE(
  715. //usage: "\n -h Follow symlinks"
  716. //usage: )
  717. //usage: IF_FEATURE_TAR_FROM(
  718. //usage: "\n -T FILE File with names to include"
  719. //usage: "\n -X FILE File with glob patterns to exclude"
  720. //usage: IF_FEATURE_TAR_LONG_OPTIONS(
  721. //usage: "\n --exclude PATTERN Glob pattern to exclude"
  722. //usage: )
  723. //usage: )
  724. //usage: IF_FEATURE_TAR_LONG_OPTIONS(
  725. //usage: "\n --overwrite Replace existing files"
  726. //usage: "\n --strip-components NUM NUM of leading components to strip"
  727. //usage: "\n --no-recursion Don't descend in directories"
  728. //usage: "\n --numeric-owner Use numeric user:group"
  729. //usage: "\n --no-same-permissions Don't restore access permissions"
  730. //usage: IF_FEATURE_TAR_TO_COMMAND(
  731. //usage: "\n --to-command COMMAND Pipe files to COMMAND"
  732. //usage: )
  733. //usage: )
  734. //usage:
  735. //usage:#define tar_example_usage
  736. //usage: "$ zcat /tmp/tarball.tar.gz | tar -xf -\n"
  737. //usage: "$ tar -cf /tmp/tarball.tar /usr/local\n"
  738. enum {
  739. OPTBIT_KEEP_OLD = 8,
  740. IF_FEATURE_TAR_CREATE( OPTBIT_CREATE ,)
  741. IF_FEATURE_TAR_CREATE( OPTBIT_DEREFERENCE ,)
  742. IF_FEATURE_SEAMLESS_BZ2( OPTBIT_BZIP2 ,)
  743. IF_FEATURE_TAR_FROM( OPTBIT_INCLUDE_FROM,)
  744. IF_FEATURE_TAR_FROM( OPTBIT_EXCLUDE_FROM,)
  745. IF_FEATURE_SEAMLESS_GZ( OPTBIT_GZIP ,)
  746. IF_FEATURE_SEAMLESS_XZ( OPTBIT_XZ ,)
  747. IF_FEATURE_SEAMLESS_Z( OPTBIT_COMPRESS ,) // 16th bit
  748. OPTBIT_AUTOCOMPRESS_BY_EXT,
  749. IF_FEATURE_TAR_NOPRESERVE_TIME(OPTBIT_NOPRESERVE_TIME,)
  750. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  751. OPTBIT_STRIP_COMPONENTS,
  752. IF_FEATURE_SEAMLESS_LZMA(OPTBIT_LZMA ,)
  753. OPTBIT_NORECURSION,
  754. IF_FEATURE_TAR_TO_COMMAND(OPTBIT_2COMMAND ,)
  755. OPTBIT_NUMERIC_OWNER,
  756. OPTBIT_NOPRESERVE_PERM,
  757. OPTBIT_OVERWRITE,
  758. #endif
  759. OPT_TEST = 1 << 0, // t
  760. OPT_EXTRACT = 1 << 1, // x
  761. OPT_BASEDIR = 1 << 2, // C
  762. OPT_TARNAME = 1 << 3, // f
  763. OPT_2STDOUT = 1 << 4, // O
  764. OPT_NOPRESERVE_OWNER = 1 << 5, // o == no-same-owner
  765. OPT_P = 1 << 6, // p
  766. OPT_VERBOSE = 1 << 7, // v
  767. OPT_KEEP_OLD = 1 << 8, // k
  768. OPT_CREATE = IF_FEATURE_TAR_CREATE( (1 << OPTBIT_CREATE )) + 0, // c
  769. OPT_DEREFERENCE = IF_FEATURE_TAR_CREATE( (1 << OPTBIT_DEREFERENCE )) + 0, // h
  770. OPT_BZIP2 = IF_FEATURE_SEAMLESS_BZ2( (1 << OPTBIT_BZIP2 )) + 0, // j
  771. OPT_INCLUDE_FROM = IF_FEATURE_TAR_FROM( (1 << OPTBIT_INCLUDE_FROM)) + 0, // T
  772. OPT_EXCLUDE_FROM = IF_FEATURE_TAR_FROM( (1 << OPTBIT_EXCLUDE_FROM)) + 0, // X
  773. OPT_GZIP = IF_FEATURE_SEAMLESS_GZ( (1 << OPTBIT_GZIP )) + 0, // z
  774. OPT_XZ = IF_FEATURE_SEAMLESS_XZ( (1 << OPTBIT_XZ )) + 0, // J
  775. OPT_COMPRESS = IF_FEATURE_SEAMLESS_Z( (1 << OPTBIT_COMPRESS )) + 0, // Z
  776. OPT_AUTOCOMPRESS_BY_EXT = 1 << OPTBIT_AUTOCOMPRESS_BY_EXT, // a
  777. OPT_NOPRESERVE_TIME = IF_FEATURE_TAR_NOPRESERVE_TIME((1 << OPTBIT_NOPRESERVE_TIME)) + 0, // m
  778. OPT_STRIP_COMPONENTS = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_STRIP_COMPONENTS)) + 0, // strip-components
  779. OPT_LZMA = IF_FEATURE_TAR_LONG_OPTIONS(IF_FEATURE_SEAMLESS_LZMA((1 << OPTBIT_LZMA))) + 0, // lzma
  780. OPT_NORECURSION = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NORECURSION )) + 0, // no-recursion
  781. OPT_2COMMAND = IF_FEATURE_TAR_TO_COMMAND( (1 << OPTBIT_2COMMAND )) + 0, // to-command
  782. OPT_NUMERIC_OWNER = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NUMERIC_OWNER )) + 0, // numeric-owner
  783. OPT_NOPRESERVE_PERM = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NOPRESERVE_PERM)) + 0, // no-same-permissions
  784. OPT_OVERWRITE = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_OVERWRITE )) + 0, // overwrite
  785. OPT_ANY_COMPRESS = (OPT_BZIP2 | OPT_LZMA | OPT_GZIP | OPT_XZ | OPT_COMPRESS),
  786. };
  787. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  788. static const char tar_longopts[] ALIGN1 =
  789. "list\0" No_argument "t"
  790. "extract\0" No_argument "x"
  791. "directory\0" Required_argument "C"
  792. "file\0" Required_argument "f"
  793. "to-stdout\0" No_argument "O"
  794. /* do not restore owner */
  795. /* Note: GNU tar handles 'o' as no-same-owner only on extract,
  796. * on create, 'o' is --old-archive. We do not support --old-archive. */
  797. "no-same-owner\0" No_argument "o"
  798. "same-permissions\0" No_argument "p"
  799. "verbose\0" No_argument "v"
  800. "keep-old\0" No_argument "k"
  801. # if ENABLE_FEATURE_TAR_CREATE
  802. "create\0" No_argument "c"
  803. "dereference\0" No_argument "h"
  804. # endif
  805. # if ENABLE_FEATURE_SEAMLESS_BZ2
  806. "bzip2\0" No_argument "j"
  807. # endif
  808. # if ENABLE_FEATURE_TAR_FROM
  809. "files-from\0" Required_argument "T"
  810. "exclude-from\0" Required_argument "X"
  811. # endif
  812. # if ENABLE_FEATURE_SEAMLESS_GZ
  813. "gzip\0" No_argument "z"
  814. # endif
  815. # if ENABLE_FEATURE_SEAMLESS_XZ
  816. "xz\0" No_argument "J"
  817. # endif
  818. # if ENABLE_FEATURE_SEAMLESS_Z
  819. "compress\0" No_argument "Z"
  820. # endif
  821. "auto-compress\0" No_argument "a"
  822. # if ENABLE_FEATURE_TAR_NOPRESERVE_TIME
  823. "touch\0" No_argument "m"
  824. # endif
  825. "strip-components\0" Required_argument "\xf8"
  826. # if ENABLE_FEATURE_SEAMLESS_LZMA
  827. "lzma\0" No_argument "\xf9"
  828. # endif
  829. "no-recursion\0" No_argument "\xfa"
  830. # if ENABLE_FEATURE_TAR_TO_COMMAND
  831. "to-command\0" Required_argument "\xfb"
  832. # endif
  833. /* use numeric uid/gid from tar header, not textual */
  834. "numeric-owner\0" No_argument "\xfc"
  835. /* do not restore mode */
  836. "no-same-permissions\0" No_argument "\xfd"
  837. /* on unpack, open with O_TRUNC and !O_EXCL */
  838. "overwrite\0" No_argument "\xfe"
  839. /* --exclude takes next bit position in option mask, */
  840. /* therefore we have to put it _after_ --no-same-permissions */
  841. # if ENABLE_FEATURE_TAR_FROM
  842. "exclude\0" Required_argument "\xff"
  843. # endif
  844. ;
  845. # define GETOPT32 getopt32long
  846. # define LONGOPTS ,tar_longopts
  847. #else
  848. # define GETOPT32 getopt32
  849. # define LONGOPTS
  850. #endif
  851. int tar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  852. int tar_main(int argc UNUSED_PARAM, char **argv)
  853. {
  854. archive_handle_t *tar_handle;
  855. char *base_dir = NULL;
  856. const char *tar_filename = "-";
  857. unsigned opt;
  858. int verboseFlag = 0;
  859. #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
  860. llist_t *excludes = NULL;
  861. #endif
  862. INIT_G();
  863. /* Initialise default values */
  864. tar_handle = init_handle();
  865. tar_handle->ah_flags = ARCHIVE_CREATE_LEADING_DIRS
  866. | ARCHIVE_RESTORE_DATE
  867. | ARCHIVE_UNLINK_OLD;
  868. /* Apparently only root's tar preserves perms (see bug 3844) */
  869. if (getuid() != 0)
  870. tar_handle->ah_flags |= ARCHIVE_DONT_RESTORE_PERM;
  871. #if ENABLE_DESKTOP
  872. /* Lie to buildroot when it starts asking stupid questions. */
  873. if (argv[1] && strcmp(argv[1], "--version") == 0) {
  874. // Output of 'tar --version' examples:
  875. // tar (GNU tar) 1.15.1
  876. // tar (GNU tar) 1.25
  877. // bsdtar 2.8.3 - libarchive 2.8.3
  878. puts("tar (busybox) " BB_VER);
  879. return 0;
  880. }
  881. #endif
  882. if (argv[1] && argv[1][0] != '-' && argv[1][0] != '\0') {
  883. /* Compat:
  884. * 1st argument without dash handles options with parameters
  885. * differently from dashed one: it takes *next argv[i]*
  886. * as parameter even if there are more chars in 1st argument:
  887. * "tar fx TARFILE" - "x" is not taken as f's param
  888. * but is interpreted as -x option
  889. * "tar -xf TARFILE" - dashed equivalent of the above
  890. * "tar -fx ..." - "x" is taken as f's param
  891. * getopt32 wouldn't handle 1st command correctly.
  892. * Unfortunately, people do use such commands.
  893. * We massage argv[1] to work around it by moving 'f'
  894. * to the end of the string.
  895. * More contrived "tar fCx TARFILE DIR" still fails,
  896. * but such commands are much less likely to be used.
  897. */
  898. char *f = strchr(argv[1], 'f');
  899. if (f) {
  900. while (f[1] != '\0') {
  901. *f = f[1];
  902. f++;
  903. }
  904. *f = 'f';
  905. }
  906. /* Prepend '-' to the first argument */
  907. argv[1] = xasprintf("-%s", argv[1]);
  908. }
  909. opt = GETOPT32(argv, "^"
  910. "txC:f:Oopvk"
  911. IF_FEATURE_TAR_CREATE( "ch" )
  912. IF_FEATURE_SEAMLESS_BZ2( "j" )
  913. IF_FEATURE_TAR_FROM( "T:*X:*")
  914. IF_FEATURE_SEAMLESS_GZ( "z" )
  915. IF_FEATURE_SEAMLESS_XZ( "J" )
  916. IF_FEATURE_SEAMLESS_Z( "Z" )
  917. "a"
  918. IF_FEATURE_TAR_NOPRESERVE_TIME("m")
  919. IF_FEATURE_TAR_LONG_OPTIONS("\xf8:") // --strip-components
  920. "\0"
  921. "tt:vv:" // count -t,-v
  922. #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
  923. "\xff::" // --exclude=PATTERN is a list
  924. #endif
  925. IF_FEATURE_TAR_CREATE("c:") "t:x:" // at least one of these is reqd
  926. IF_FEATURE_TAR_CREATE("c--tx:t--cx:x--ct") // mutually exclusive
  927. IF_NOT_FEATURE_TAR_CREATE("t--x:x--t") // mutually exclusive
  928. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  929. ":\xf8+" // --strip-components=NUM
  930. #endif
  931. LONGOPTS
  932. , &base_dir // -C dir
  933. , &tar_filename // -f filename
  934. IF_FEATURE_TAR_FROM(, &(tar_handle->accept)) // T
  935. IF_FEATURE_TAR_FROM(, &(tar_handle->reject)) // X
  936. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  937. , &tar_handle->tar__strip_components // --strip-components
  938. #endif
  939. IF_FEATURE_TAR_TO_COMMAND(, &(tar_handle->tar__to_command)) // --to-command
  940. #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
  941. , &excludes // --exclude
  942. #endif
  943. , &verboseFlag // combined count for -t and -v
  944. , &verboseFlag // combined count for -t and -v
  945. );
  946. #if DBG_OPTION_PARSING
  947. bb_error_msg("opt: 0x%08x", opt);
  948. # define showopt(o) bb_error_msg("opt & %s(%x):\t%x", #o, o, opt & o);
  949. showopt(OPT_TEST );
  950. showopt(OPT_EXTRACT );
  951. showopt(OPT_BASEDIR );
  952. showopt(OPT_TARNAME );
  953. showopt(OPT_2STDOUT );
  954. showopt(OPT_NOPRESERVE_OWNER);
  955. showopt(OPT_P );
  956. showopt(OPT_VERBOSE );
  957. showopt(OPT_KEEP_OLD );
  958. showopt(OPT_CREATE );
  959. showopt(OPT_DEREFERENCE );
  960. showopt(OPT_BZIP2 );
  961. showopt(OPT_INCLUDE_FROM );
  962. showopt(OPT_EXCLUDE_FROM );
  963. showopt(OPT_GZIP );
  964. showopt(OPT_XZ );
  965. showopt(OPT_COMPRESS );
  966. showopt(OPT_AUTOCOMPRESS_BY_EXT);
  967. showopt(OPT_NOPRESERVE_TIME );
  968. showopt(OPT_STRIP_COMPONENTS);
  969. showopt(OPT_LZMA );
  970. showopt(OPT_NORECURSION );
  971. showopt(OPT_2COMMAND );
  972. showopt(OPT_NUMERIC_OWNER );
  973. showopt(OPT_NOPRESERVE_PERM );
  974. showopt(OPT_OVERWRITE );
  975. showopt(OPT_ANY_COMPRESS );
  976. bb_error_msg("base_dir:'%s'", base_dir);
  977. bb_error_msg("tar_filename:'%s'", tar_filename);
  978. bb_error_msg("verboseFlag:%d", verboseFlag);
  979. bb_error_msg("tar_handle->tar__to_command:'%s'", tar_handle->tar__to_command);
  980. bb_error_msg("tar_handle->tar__strip_components:%u", tar_handle->tar__strip_components);
  981. return 0;
  982. # undef showopt
  983. #endif
  984. argv += optind;
  985. if (verboseFlag)
  986. tar_handle->action_header = header_verbose_list;
  987. if (verboseFlag == 1)
  988. tar_handle->action_header = header_list;
  989. if (opt & OPT_EXTRACT)
  990. tar_handle->action_data = data_extract_all;
  991. if (opt & OPT_2STDOUT)
  992. tar_handle->action_data = data_extract_to_stdout;
  993. if (opt & OPT_2COMMAND) {
  994. putenv((char*)"TAR_FILETYPE=f");
  995. signal(SIGPIPE, SIG_IGN);
  996. tar_handle->action_data = data_extract_to_command;
  997. IF_FEATURE_TAR_TO_COMMAND(tar_handle->tar__to_command_shell = xstrdup(get_shell_name());)
  998. }
  999. if (opt & OPT_KEEP_OLD)
  1000. tar_handle->ah_flags &= ~ARCHIVE_UNLINK_OLD;
  1001. if (opt & OPT_NUMERIC_OWNER)
  1002. tar_handle->ah_flags |= ARCHIVE_NUMERIC_OWNER;
  1003. if (opt & OPT_NOPRESERVE_OWNER)
  1004. tar_handle->ah_flags |= ARCHIVE_DONT_RESTORE_OWNER;
  1005. if (opt & OPT_NOPRESERVE_PERM)
  1006. tar_handle->ah_flags |= ARCHIVE_DONT_RESTORE_PERM;
  1007. if (opt & OPT_OVERWRITE) {
  1008. tar_handle->ah_flags &= ~ARCHIVE_UNLINK_OLD;
  1009. tar_handle->ah_flags |= ARCHIVE_O_TRUNC;
  1010. }
  1011. if (opt & OPT_NOPRESERVE_TIME)
  1012. tar_handle->ah_flags &= ~ARCHIVE_RESTORE_DATE;
  1013. #if ENABLE_FEATURE_TAR_FROM
  1014. /* Convert each -X EXCLFILE to list of to-be-rejected glob patterns */
  1015. tar_handle->reject = append_file_list_to_list(tar_handle->reject);
  1016. # if ENABLE_FEATURE_TAR_LONG_OPTIONS
  1017. /* Append --exclude=GLOBPATTERNs to reject */
  1018. if (excludes) {
  1019. llist_t **p2next = &tar_handle->reject;
  1020. while (*p2next)
  1021. p2next = &((*p2next)->link);
  1022. *p2next = excludes;
  1023. }
  1024. # endif
  1025. tar_handle->accept = append_file_list_to_list(tar_handle->accept);
  1026. #endif
  1027. /* Setup an array of filenames to work with */
  1028. /* TODO: This is the same as in ar, make a separate function? */
  1029. while (*argv) {
  1030. /* kill trailing '/' unless the string is just "/" */
  1031. char *cp = last_char_is(*argv, '/');
  1032. if (cp > *argv)
  1033. *cp = '\0';
  1034. llist_add_to_end(&tar_handle->accept, *argv);
  1035. argv++;
  1036. }
  1037. if (tar_handle->accept || tar_handle->reject)
  1038. tar_handle->filter = filter_accept_reject_list;
  1039. /* Open the tar file */
  1040. {
  1041. int tar_fd = STDIN_FILENO;
  1042. int flags = O_RDONLY;
  1043. if (opt & OPT_CREATE) {
  1044. /* Make sure there is at least one file to tar up */
  1045. if (tar_handle->accept == NULL)
  1046. bb_simple_error_msg_and_die("empty archive");
  1047. tar_fd = STDOUT_FILENO;
  1048. /* Mimicking GNU tar 1.15.1: */
  1049. flags = O_WRONLY | O_CREAT | O_TRUNC;
  1050. }
  1051. if (LONE_DASH(tar_filename)) {
  1052. tar_handle->src_fd = tar_fd;
  1053. tar_handle->seek = seek_by_read;
  1054. } else
  1055. if (ENABLE_FEATURE_TAR_AUTODETECT
  1056. && ENABLE_FEATURE_SEAMLESS_LZMA
  1057. && flags == O_RDONLY
  1058. && !(opt & OPT_ANY_COMPRESS)
  1059. && is_suffixed_with(tar_filename, ".lzma")
  1060. /* We do this only for .lzma files, they have no signature.
  1061. * All other compression formats are recognized in
  1062. * get_header_tar() when first tar block has invalid format.
  1063. * Doing it here for all filenames would falsely trigger
  1064. * on e.g. tarball with 1st file named "BZh5".
  1065. */
  1066. ) {
  1067. tar_handle->src_fd = open_zipped(tar_filename, /*fail_if_not_compressed:*/ 0);
  1068. if (tar_handle->src_fd < 0)
  1069. bb_perror_msg_and_die("can't open '%s'", tar_filename);
  1070. } else {
  1071. tar_handle->src_fd = xopen(tar_filename, flags);
  1072. #if ENABLE_FEATURE_TAR_CREATE
  1073. if ((OPT_GZIP | OPT_BZIP2 | OPT_XZ | OPT_LZMA) != 0 /* at least one is config-enabled */
  1074. && (opt & OPT_AUTOCOMPRESS_BY_EXT)
  1075. && flags != O_RDONLY
  1076. ) {
  1077. if (OPT_GZIP != 0 && is_suffixed_with(tar_filename, "gz"))
  1078. opt |= OPT_GZIP;
  1079. if (OPT_BZIP2 != 0 && is_suffixed_with(tar_filename, "bz2"))
  1080. opt |= OPT_BZIP2;
  1081. if (OPT_XZ != 0 && is_suffixed_with(tar_filename, "xz"))
  1082. opt |= OPT_XZ;
  1083. if (OPT_LZMA != 0 && is_suffixed_with(tar_filename, "lzma"))
  1084. opt |= OPT_LZMA;
  1085. }
  1086. #endif
  1087. }
  1088. }
  1089. if (base_dir)
  1090. xchdir(base_dir);
  1091. #if ENABLE_FEATURE_TAR_CREATE
  1092. /* Create an archive */
  1093. if (opt & OPT_CREATE) {
  1094. struct TarBallInfo *tbInfo;
  1095. # if SEAMLESS_COMPRESSION
  1096. const char *zipMode = NULL;
  1097. if (opt & OPT_COMPRESS)
  1098. zipMode = "compress";
  1099. if (opt & OPT_GZIP)
  1100. zipMode = "gzip";
  1101. if (opt & OPT_BZIP2)
  1102. zipMode = "bzip2";
  1103. if (opt & OPT_LZMA)
  1104. zipMode = "lzma";
  1105. if (opt & OPT_XZ)
  1106. zipMode = "xz";
  1107. # endif
  1108. tbInfo = xzalloc(sizeof(*tbInfo));
  1109. tbInfo->tarFd = tar_handle->src_fd;
  1110. tbInfo->verboseFlag = verboseFlag;
  1111. # if ENABLE_FEATURE_TAR_FROM
  1112. tbInfo->excludeList = tar_handle->reject;
  1113. # endif
  1114. /* NB: writeTarFile() closes tar_handle->src_fd */
  1115. return writeTarFile(tbInfo,
  1116. (opt & OPT_DEREFERENCE ? ACTION_FOLLOWLINKS : 0)
  1117. | (opt & OPT_NORECURSION ? 0 : ACTION_RECURSE),
  1118. tar_handle->accept,
  1119. zipMode);
  1120. }
  1121. #endif
  1122. if (opt & OPT_ANY_COMPRESS) {
  1123. USE_FOR_MMU(IF_DESKTOP(long long) int FAST_FUNC (*xformer)(transformer_state_t *xstate);)
  1124. USE_FOR_NOMMU(const char *xformer_prog;)
  1125. if (opt & OPT_COMPRESS) {
  1126. USE_FOR_MMU(IF_FEATURE_SEAMLESS_Z(xformer = unpack_Z_stream;))
  1127. USE_FOR_NOMMU(xformer_prog = "uncompress";)
  1128. }
  1129. if (opt & OPT_GZIP) {
  1130. USE_FOR_MMU(IF_FEATURE_SEAMLESS_GZ(xformer = unpack_gz_stream;))
  1131. USE_FOR_NOMMU(xformer_prog = "gunzip";)
  1132. }
  1133. if (opt & OPT_BZIP2) {
  1134. USE_FOR_MMU(IF_FEATURE_SEAMLESS_BZ2(xformer = unpack_bz2_stream;))
  1135. USE_FOR_NOMMU(xformer_prog = "bunzip2";)
  1136. }
  1137. if (opt & OPT_LZMA) {
  1138. USE_FOR_MMU(IF_FEATURE_SEAMLESS_LZMA(xformer = unpack_lzma_stream;))
  1139. USE_FOR_NOMMU(xformer_prog = "unlzma";)
  1140. }
  1141. if (opt & OPT_XZ) {
  1142. USE_FOR_MMU(IF_FEATURE_SEAMLESS_XZ(xformer = unpack_xz_stream;))
  1143. USE_FOR_NOMMU(xformer_prog = "unxz";)
  1144. }
  1145. fork_transformer_with_sig(tar_handle->src_fd, xformer, xformer_prog);
  1146. /* Can't lseek over pipes */
  1147. tar_handle->seek = seek_by_read;
  1148. /*tar_handle->offset = 0; - already is */
  1149. }
  1150. /* Zero processed headers (== empty file) is not a valid tarball.
  1151. * We (ab)use bb_got_signal as exitcode here,
  1152. * because check_errors_in_children() uses _it_ as error indicator.
  1153. */
  1154. bb_got_signal = EXIT_FAILURE;
  1155. while (get_header_tar(tar_handle) == EXIT_SUCCESS)
  1156. bb_got_signal = EXIT_SUCCESS; /* saw at least one header, good */
  1157. create_links_from_list(tar_handle->link_placeholders);
  1158. /* Check that every file that should have been extracted was */
  1159. while (tar_handle->accept) {
  1160. if (!find_list_entry(tar_handle->reject, tar_handle->accept->data)
  1161. && !find_list_entry(tar_handle->passed, tar_handle->accept->data)
  1162. ) {
  1163. bb_error_msg_and_die("%s: not found in archive",
  1164. tar_handle->accept->data);
  1165. }
  1166. tar_handle->accept = tar_handle->accept->link;
  1167. }
  1168. if (ENABLE_FEATURE_CLEAN_UP /* && tar_handle->src_fd != STDIN_FILENO */)
  1169. close(tar_handle->src_fd);
  1170. if (SEAMLESS_COMPRESSION || OPT_COMPRESS) {
  1171. /* Set bb_got_signal to 1 if a child died with !0 exitcode */
  1172. check_errors_in_children(0);
  1173. }
  1174. return bb_got_signal;
  1175. }