tar.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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 the 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 tarball for details.
  24. */
  25. #include <fnmatch.h>
  26. #include <getopt.h>
  27. #include "libbb.h"
  28. #include "unarchive.h"
  29. #define block_buf bb_common_bufsiz1
  30. #if ENABLE_FEATURE_TAR_CREATE
  31. /* Tar file constants */
  32. #define TAR_BLOCK_SIZE 512
  33. /* POSIX tar Header Block, from POSIX 1003.1-1990 */
  34. #define NAME_SIZE 100
  35. #define NAME_SIZE_STR "100"
  36. typedef struct TarHeader TarHeader;
  37. struct TarHeader { /* byte offset */
  38. char name[NAME_SIZE]; /* 0-99 */
  39. char mode[8]; /* 100-107 */
  40. char uid[8]; /* 108-115 */
  41. char gid[8]; /* 116-123 */
  42. char size[12]; /* 124-135 */
  43. char mtime[12]; /* 136-147 */
  44. char chksum[8]; /* 148-155 */
  45. char typeflag; /* 156-156 */
  46. char linkname[NAME_SIZE]; /* 157-256 */
  47. /* POSIX: "ustar" NUL "00" */
  48. /* GNU tar: "ustar " NUL */
  49. char magic[8]; /* 257-264 */
  50. char uname[32]; /* 265-296 */
  51. char gname[32]; /* 297-328 */
  52. char devmajor[8]; /* 329-336 */
  53. char devminor[8]; /* 337-344 */
  54. char prefix[155]; /* 345-499 */
  55. char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */
  56. };
  57. /*
  58. ** writeTarFile(), writeFileToTarball(), and writeTarHeader() are
  59. ** the only functions that deal with the HardLinkInfo structure.
  60. ** Even these functions use the xxxHardLinkInfo() functions.
  61. */
  62. typedef struct HardLinkInfo HardLinkInfo;
  63. struct HardLinkInfo {
  64. HardLinkInfo *next; /* Next entry in list */
  65. dev_t dev; /* Device number */
  66. ino_t ino; /* Inode number */
  67. short linkCount; /* (Hard) Link Count */
  68. char name[1]; /* Start of filename (must be last) */
  69. };
  70. /* Some info to be carried along when creating a new tarball */
  71. typedef struct TarBallInfo TarBallInfo;
  72. struct TarBallInfo {
  73. int tarFd; /* Open-for-write file descriptor
  74. for the tarball */
  75. struct stat statBuf; /* Stat info for the tarball, letting
  76. us know the inode and device that the
  77. tarball lives, so we can avoid trying
  78. to include the tarball into itself */
  79. int verboseFlag; /* Whether to print extra stuff or not */
  80. const llist_t *excludeList; /* List of files to not include */
  81. HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */
  82. HardLinkInfo *hlInfo; /* Hard Link Info for the current file */
  83. };
  84. /* A nice enum with all the possible tar file content types */
  85. enum TarFileType {
  86. REGTYPE = '0', /* regular file */
  87. REGTYPE0 = '\0', /* regular file (ancient bug compat) */
  88. LNKTYPE = '1', /* hard link */
  89. SYMTYPE = '2', /* symbolic link */
  90. CHRTYPE = '3', /* character special */
  91. BLKTYPE = '4', /* block special */
  92. DIRTYPE = '5', /* directory */
  93. FIFOTYPE = '6', /* FIFO special */
  94. CONTTYPE = '7', /* reserved */
  95. GNULONGLINK = 'K', /* GNU long (>100 chars) link name */
  96. GNULONGNAME = 'L', /* GNU long (>100 chars) file name */
  97. };
  98. typedef enum TarFileType TarFileType;
  99. /* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
  100. static void addHardLinkInfo(HardLinkInfo **hlInfoHeadPtr,
  101. struct stat *statbuf,
  102. const char *fileName)
  103. {
  104. /* Note: hlInfoHeadPtr can never be NULL! */
  105. HardLinkInfo *hlInfo;
  106. hlInfo = xmalloc(sizeof(HardLinkInfo) + strlen(fileName));
  107. hlInfo->next = *hlInfoHeadPtr;
  108. *hlInfoHeadPtr = hlInfo;
  109. hlInfo->dev = statbuf->st_dev;
  110. hlInfo->ino = statbuf->st_ino;
  111. hlInfo->linkCount = statbuf->st_nlink;
  112. strcpy(hlInfo->name, fileName);
  113. }
  114. static void freeHardLinkInfo(HardLinkInfo **hlInfoHeadPtr)
  115. {
  116. HardLinkInfo *hlInfo;
  117. HardLinkInfo *hlInfoNext;
  118. if (hlInfoHeadPtr) {
  119. hlInfo = *hlInfoHeadPtr;
  120. while (hlInfo) {
  121. hlInfoNext = hlInfo->next;
  122. free(hlInfo);
  123. hlInfo = hlInfoNext;
  124. }
  125. *hlInfoHeadPtr = NULL;
  126. }
  127. }
  128. /* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
  129. static HardLinkInfo *findHardLinkInfo(HardLinkInfo *hlInfo, struct stat *statbuf)
  130. {
  131. while (hlInfo) {
  132. if ((statbuf->st_ino == hlInfo->ino) && (statbuf->st_dev == hlInfo->dev))
  133. break;
  134. hlInfo = hlInfo->next;
  135. }
  136. return hlInfo;
  137. }
  138. /* Put an octal string into the specified buffer.
  139. * The number is zero padded and possibly null terminated.
  140. * Stores low-order bits only if whole value does not fit. */
  141. static void putOctal(char *cp, int len, off_t value)
  142. {
  143. char tempBuffer[sizeof(off_t)*3+1];
  144. char *tempString = tempBuffer;
  145. int width;
  146. width = sprintf(tempBuffer, "%0*"OFF_FMT"o", len, value);
  147. tempString += (width - len);
  148. /* If string has leading zeroes, we can drop one */
  149. /* and field will have trailing '\0' */
  150. /* (increases chances of compat with other tars) */
  151. if (tempString[0] == '0')
  152. tempString++;
  153. /* Copy the string to the field */
  154. memcpy(cp, tempString, len);
  155. }
  156. #define PUT_OCTAL(a, b) putOctal((a), sizeof(a), (b))
  157. static void chksum_and_xwrite(int fd, struct TarHeader* hp)
  158. {
  159. /* POSIX says that checksum is done on unsigned bytes
  160. * (Sun and HP-UX gets it wrong... more details in
  161. * GNU tar source) */
  162. const unsigned char *cp;
  163. int chksum, size;
  164. strcpy(hp->magic, "ustar ");
  165. /* Calculate and store the checksum (i.e., the sum of all of the bytes of
  166. * the header). The checksum field must be filled with blanks for the
  167. * calculation. The checksum field is formatted differently from the
  168. * other fields: it has 6 digits, a null, then a space -- rather than
  169. * digits, followed by a null like the other fields... */
  170. memset(hp->chksum, ' ', sizeof(hp->chksum));
  171. cp = (const unsigned char *) hp;
  172. chksum = 0;
  173. size = sizeof(*hp);
  174. do { chksum += *cp++; } while (--size);
  175. putOctal(hp->chksum, sizeof(hp->chksum)-1, chksum);
  176. /* Now write the header out to disk */
  177. xwrite(fd, hp, sizeof(*hp));
  178. }
  179. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  180. static void writeLongname(int fd, int type, const char *name, int dir)
  181. {
  182. static const struct {
  183. char mode[8]; /* 100-107 */
  184. char uid[8]; /* 108-115 */
  185. char gid[8]; /* 116-123 */
  186. char size[12]; /* 124-135 */
  187. char mtime[12]; /* 136-147 */
  188. } prefilled = {
  189. "0000000",
  190. "0000000",
  191. "0000000",
  192. "00000000000",
  193. "00000000000",
  194. };
  195. struct TarHeader header;
  196. int size;
  197. dir = !!dir; /* normalize: 0/1 */
  198. size = strlen(name) + 1 + dir; /* GNU tar uses strlen+1 */
  199. /* + dir: account for possible '/' */
  200. memset(&header, 0, sizeof(header));
  201. strcpy(header.name, "././@LongLink");
  202. memcpy(header.mode, prefilled.mode, sizeof(prefilled));
  203. PUT_OCTAL(header.size, size);
  204. header.typeflag = type;
  205. chksum_and_xwrite(fd, &header);
  206. /* Write filename[/] and pad the block. */
  207. /* dir=0: writes 'name<NUL>', pads */
  208. /* dir=1: writes 'name', writes '/<NUL>', pads */
  209. dir *= 2;
  210. xwrite(fd, name, size - dir);
  211. xwrite(fd, "/", dir);
  212. size = (-size) & (TAR_BLOCK_SIZE-1);
  213. memset(&header, 0, size);
  214. xwrite(fd, &header, size);
  215. }
  216. #endif
  217. /* Write out a tar header for the specified file/directory/whatever */
  218. void BUG_tar_header_size(void);
  219. static int writeTarHeader(struct TarBallInfo *tbInfo,
  220. const char *header_name, const char *fileName, struct stat *statbuf)
  221. {
  222. struct TarHeader header;
  223. if (sizeof(header) != 512)
  224. BUG_tar_header_size();
  225. memset(&header, 0, sizeof(struct TarHeader));
  226. strncpy(header.name, header_name, sizeof(header.name));
  227. /* POSIX says to mask mode with 07777. */
  228. PUT_OCTAL(header.mode, statbuf->st_mode & 07777);
  229. PUT_OCTAL(header.uid, statbuf->st_uid);
  230. PUT_OCTAL(header.gid, statbuf->st_gid);
  231. memset(header.size, '0', sizeof(header.size)-1); /* Regular file size is handled later */
  232. PUT_OCTAL(header.mtime, statbuf->st_mtime);
  233. /* Enter the user and group names */
  234. safe_strncpy(header.uname, get_cached_username(statbuf->st_uid), sizeof(header.uname));
  235. safe_strncpy(header.gname, get_cached_groupname(statbuf->st_gid), sizeof(header.gname));
  236. if (tbInfo->hlInfo) {
  237. /* This is a hard link */
  238. header.typeflag = LNKTYPE;
  239. strncpy(header.linkname, tbInfo->hlInfo->name,
  240. sizeof(header.linkname));
  241. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  242. /* Write out long linkname if needed */
  243. if (header.linkname[sizeof(header.linkname)-1])
  244. writeLongname(tbInfo->tarFd, GNULONGLINK,
  245. tbInfo->hlInfo->name, 0);
  246. #endif
  247. } else if (S_ISLNK(statbuf->st_mode)) {
  248. char *lpath = xmalloc_readlink_or_warn(fileName);
  249. if (!lpath)
  250. return FALSE;
  251. header.typeflag = SYMTYPE;
  252. strncpy(header.linkname, lpath, sizeof(header.linkname));
  253. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  254. /* Write out long linkname if needed */
  255. if (header.linkname[sizeof(header.linkname)-1])
  256. writeLongname(tbInfo->tarFd, GNULONGLINK, lpath, 0);
  257. #else
  258. /* If it is larger than 100 bytes, bail out */
  259. if (header.linkname[sizeof(header.linkname)-1]) {
  260. free(lpath);
  261. bb_error_msg("names longer than "NAME_SIZE_STR" chars not supported");
  262. return FALSE;
  263. }
  264. #endif
  265. free(lpath);
  266. } else if (S_ISDIR(statbuf->st_mode)) {
  267. header.typeflag = DIRTYPE;
  268. /* Append '/' only if there is a space for it */
  269. if (!header.name[sizeof(header.name)-1])
  270. header.name[strlen(header.name)] = '/';
  271. } else if (S_ISCHR(statbuf->st_mode)) {
  272. header.typeflag = CHRTYPE;
  273. PUT_OCTAL(header.devmajor, major(statbuf->st_rdev));
  274. PUT_OCTAL(header.devminor, minor(statbuf->st_rdev));
  275. } else if (S_ISBLK(statbuf->st_mode)) {
  276. header.typeflag = BLKTYPE;
  277. PUT_OCTAL(header.devmajor, major(statbuf->st_rdev));
  278. PUT_OCTAL(header.devminor, minor(statbuf->st_rdev));
  279. } else if (S_ISFIFO(statbuf->st_mode)) {
  280. header.typeflag = FIFOTYPE;
  281. } else if (S_ISREG(statbuf->st_mode)) {
  282. if (sizeof(statbuf->st_size) > 4
  283. && statbuf->st_size > (off_t)0777777777777LL
  284. ) {
  285. bb_error_msg_and_die("cannot store file '%s' "
  286. "of size %"OFF_FMT"d, aborting",
  287. fileName, statbuf->st_size);
  288. }
  289. header.typeflag = REGTYPE;
  290. PUT_OCTAL(header.size, statbuf->st_size);
  291. } else {
  292. bb_error_msg("%s: unknown file type", fileName);
  293. return FALSE;
  294. }
  295. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  296. /* Write out long name if needed */
  297. /* (we, like GNU tar, output long linkname *before* long name) */
  298. if (header.name[sizeof(header.name)-1])
  299. writeLongname(tbInfo->tarFd, GNULONGNAME,
  300. header_name, S_ISDIR(statbuf->st_mode));
  301. #endif
  302. /* Now write the header out to disk */
  303. chksum_and_xwrite(tbInfo->tarFd, &header);
  304. /* Now do the verbose thing (or not) */
  305. if (tbInfo->verboseFlag) {
  306. FILE *vbFd = stdout;
  307. if (tbInfo->tarFd == STDOUT_FILENO) /* If the archive goes to stdout, verbose to stderr */
  308. vbFd = stderr;
  309. /* GNU "tar cvvf" prints "extended" listing a-la "ls -l" */
  310. /* We don't have such excesses here: for us "v" == "vv" */
  311. /* '/' is probably a GNUism */
  312. fprintf(vbFd, "%s%s\n", header_name,
  313. S_ISDIR(statbuf->st_mode) ? "/" : "");
  314. }
  315. return TRUE;
  316. }
  317. #if ENABLE_FEATURE_TAR_FROM
  318. static int exclude_file(const llist_t *excluded_files, const char *file)
  319. {
  320. while (excluded_files) {
  321. if (excluded_files->data[0] == '/') {
  322. if (fnmatch(excluded_files->data, file,
  323. FNM_PATHNAME | FNM_LEADING_DIR) == 0)
  324. return 1;
  325. } else {
  326. const char *p;
  327. for (p = file; p[0] != '\0'; p++) {
  328. if ((p == file || p[-1] == '/') && p[0] != '/' &&
  329. fnmatch(excluded_files->data, p,
  330. FNM_PATHNAME | FNM_LEADING_DIR) == 0)
  331. return 1;
  332. }
  333. }
  334. excluded_files = excluded_files->link;
  335. }
  336. return 0;
  337. }
  338. #else
  339. #define exclude_file(excluded_files, file) 0
  340. #endif
  341. static int writeFileToTarball(const char *fileName, struct stat *statbuf,
  342. void *userData, int depth ATTRIBUTE_UNUSED)
  343. {
  344. struct TarBallInfo *tbInfo = (struct TarBallInfo *) userData;
  345. const char *header_name;
  346. int inputFileFd = -1;
  347. /* Strip leading '/' (must be before memorizing hardlink's name) */
  348. header_name = fileName;
  349. while (header_name[0] == '/') {
  350. static smallint warned;
  351. if (!warned) {
  352. bb_error_msg("removing leading '/' from member names");
  353. warned = 1;
  354. }
  355. header_name++;
  356. }
  357. if (header_name[0] == '\0')
  358. return TRUE;
  359. /* It is against the rules to archive a socket */
  360. if (S_ISSOCK(statbuf->st_mode)) {
  361. bb_error_msg("%s: socket ignored", fileName);
  362. return TRUE;
  363. }
  364. /*
  365. * Check to see if we are dealing with a hard link.
  366. * If so -
  367. * Treat the first occurance of a given dev/inode as a file while
  368. * treating any additional occurances as hard links. This is done
  369. * by adding the file information to the HardLinkInfo linked list.
  370. */
  371. tbInfo->hlInfo = NULL;
  372. if (statbuf->st_nlink > 1) {
  373. tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf);
  374. if (tbInfo->hlInfo == NULL)
  375. addHardLinkInfo(&tbInfo->hlInfoHead, statbuf, header_name);
  376. }
  377. /* It is a bad idea to store the archive we are in the process of creating,
  378. * so check the device and inode to be sure that this particular file isn't
  379. * the new tarball */
  380. if (tbInfo->statBuf.st_dev == statbuf->st_dev
  381. && tbInfo->statBuf.st_ino == statbuf->st_ino
  382. ) {
  383. bb_error_msg("%s: file is the archive; skipping", fileName);
  384. return TRUE;
  385. }
  386. if (exclude_file(tbInfo->excludeList, header_name))
  387. return SKIP;
  388. #if !ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  389. if (strlen(header_name) >= NAME_SIZE) {
  390. bb_error_msg("names longer than "NAME_SIZE_STR" chars not supported");
  391. return TRUE;
  392. }
  393. #endif
  394. /* Is this a regular file? */
  395. if (tbInfo->hlInfo == NULL && S_ISREG(statbuf->st_mode)) {
  396. /* open the file we want to archive, and make sure all is well */
  397. inputFileFd = open_or_warn(fileName, O_RDONLY);
  398. if (inputFileFd < 0) {
  399. return FALSE;
  400. }
  401. }
  402. /* Add an entry to the tarball */
  403. if (writeTarHeader(tbInfo, header_name, fileName, statbuf) == FALSE) {
  404. return FALSE;
  405. }
  406. /* If it was a regular file, write out the body */
  407. if (inputFileFd >= 0) {
  408. size_t readSize;
  409. /* Write the file to the archive. */
  410. /* We record size into header first, */
  411. /* and then write out file. If file shrinks in between, */
  412. /* tar will be corrupted. So we don't allow for that. */
  413. /* NB: GNU tar 1.16 warns and pads with zeroes */
  414. /* or even seeks back and updates header */
  415. bb_copyfd_exact_size(inputFileFd, tbInfo->tarFd, statbuf->st_size);
  416. ////off_t readSize;
  417. ////readSize = bb_copyfd_size(inputFileFd, tbInfo->tarFd, statbuf->st_size);
  418. ////if (readSize != statbuf->st_size && readSize >= 0) {
  419. //// bb_error_msg_and_die("short read from %s, aborting", fileName);
  420. ////}
  421. /* Check that file did not grow in between? */
  422. /* if (safe_read(inputFileFd, 1) == 1) warn but continue? */
  423. close(inputFileFd);
  424. /* Pad the file up to the tar block size */
  425. /* (a few tricks here in the name of code size) */
  426. readSize = (-(int)statbuf->st_size) & (TAR_BLOCK_SIZE-1);
  427. memset(block_buf, 0, readSize);
  428. xwrite(tbInfo->tarFd, block_buf, readSize);
  429. }
  430. return TRUE;
  431. }
  432. static int writeTarFile(const int tar_fd, const int verboseFlag,
  433. const unsigned long dereferenceFlag, const llist_t *include,
  434. const llist_t *exclude, const int gzip)
  435. {
  436. pid_t gzipPid = 0;
  437. int errorFlag = FALSE;
  438. struct TarBallInfo tbInfo;
  439. tbInfo.hlInfoHead = NULL;
  440. fchmod(tar_fd, 0644);
  441. tbInfo.tarFd = tar_fd;
  442. tbInfo.verboseFlag = verboseFlag;
  443. /* Store the stat info for the tarball's file, so
  444. * can avoid including the tarball into itself.... */
  445. if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
  446. bb_perror_msg_and_die("cannot stat tar file");
  447. if ((ENABLE_FEATURE_TAR_GZIP || ENABLE_FEATURE_TAR_BZIP2) && gzip) {
  448. // On Linux, vfork never unpauses parent early, although standard
  449. // allows for that. Do we want to waste bytes checking for it?
  450. #define WAIT_FOR_CHILD 0
  451. volatile int vfork_exec_errno = 0;
  452. #if WAIT_FOR_CHILD
  453. struct { int rd; int wr; } gzipStatusPipe;
  454. #endif
  455. struct { int rd; int wr; } gzipDataPipe;
  456. const char *zip_exec = (gzip == 1) ? "gzip" : "bzip2";
  457. xpipe(&gzipDataPipe.rd);
  458. #if WAIT_FOR_CHILD
  459. xpipe(&gzipStatusPipe.rd);
  460. #endif
  461. signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */
  462. #if defined(__GNUC__) && __GNUC__
  463. /* Avoid vfork clobbering */
  464. (void) &include;
  465. (void) &errorFlag;
  466. (void) &zip_exec;
  467. #endif
  468. gzipPid = vfork();
  469. if (gzipPid < 0)
  470. bb_perror_msg_and_die("vfork gzip");
  471. if (gzipPid == 0) {
  472. /* child */
  473. xmove_fd(tbInfo.tarFd, 1);
  474. xmove_fd(gzipDataPipe.rd, 0);
  475. close(gzipDataPipe.wr);
  476. #if WAIT_FOR_CHILD
  477. close(gzipStatusPipe.rd);
  478. fcntl(gzipStatusPipe.wr, F_SETFD, FD_CLOEXEC);
  479. #endif
  480. /* exec gzip/bzip2 program/applet */
  481. BB_EXECLP(zip_exec, zip_exec, "-f", NULL);
  482. vfork_exec_errno = errno;
  483. _exit(1);
  484. }
  485. /* parent */
  486. xmove_fd(gzipDataPipe.wr, tbInfo.tarFd);
  487. close(gzipDataPipe.rd);
  488. #if WAIT_FOR_CHILD
  489. close(gzipStatusPipe.wr);
  490. while (1) {
  491. char buf;
  492. int n;
  493. /* Wait until child execs (or fails to) */
  494. n = full_read(gzipStatusPipe.rd, &buf, 1);
  495. if ((n < 0) && (/*errno == EAGAIN ||*/ errno == EINTR))
  496. continue; /* try it again */
  497. }
  498. close(gzipStatusPipe.rd);
  499. #endif
  500. if (vfork_exec_errno) {
  501. errno = vfork_exec_errno;
  502. bb_perror_msg_and_die("cannot exec %s", zip_exec);
  503. }
  504. }
  505. tbInfo.excludeList = exclude;
  506. /* Read the directory/files and iterate over them one at a time */
  507. while (include) {
  508. if (!recursive_action(include->data, ACTION_RECURSE |
  509. (dereferenceFlag ? ACTION_FOLLOWLINKS : 0),
  510. writeFileToTarball, writeFileToTarball, &tbInfo, 0))
  511. {
  512. errorFlag = TRUE;
  513. }
  514. include = include->link;
  515. }
  516. /* Write two empty blocks to the end of the archive */
  517. memset(block_buf, 0, 2*TAR_BLOCK_SIZE);
  518. xwrite(tbInfo.tarFd, block_buf, 2*TAR_BLOCK_SIZE);
  519. /* To be pedantically correct, we would check if the tarball
  520. * is smaller than 20 tar blocks, and pad it if it was smaller,
  521. * but that isn't necessary for GNU tar interoperability, and
  522. * so is considered a waste of space */
  523. /* Close so the child process (if any) will exit */
  524. close(tbInfo.tarFd);
  525. /* Hang up the tools, close up shop, head home */
  526. if (ENABLE_FEATURE_CLEAN_UP)
  527. freeHardLinkInfo(&tbInfo.hlInfoHead);
  528. if (errorFlag)
  529. bb_error_msg("error exit delayed from previous errors");
  530. if (gzipPid) {
  531. int status;
  532. if (waitpid(gzipPid, &status, 0) == -1)
  533. bb_perror_msg("waitpid");
  534. else if (!WIFEXITED(status) || WEXITSTATUS(status))
  535. /* gzip was killed or has exited with nonzero! */
  536. errorFlag = TRUE;
  537. }
  538. return errorFlag;
  539. }
  540. #else
  541. int writeTarFile(const int tar_fd, const int verboseFlag,
  542. const unsigned long dereferenceFlag, const llist_t *include,
  543. const llist_t *exclude, const int gzip);
  544. #endif /* FEATURE_TAR_CREATE */
  545. #if ENABLE_FEATURE_TAR_FROM
  546. static llist_t *append_file_list_to_list(llist_t *list)
  547. {
  548. FILE *src_stream;
  549. llist_t *cur = list;
  550. llist_t *tmp;
  551. char *line;
  552. llist_t *newlist = NULL;
  553. while (cur) {
  554. src_stream = xfopen(cur->data, "r");
  555. tmp = cur;
  556. cur = cur->link;
  557. free(tmp);
  558. while ((line = xmalloc_getline(src_stream)) != NULL) {
  559. /* kill trailing '/' unless the string is just "/" */
  560. char *cp = last_char_is(line, '/');
  561. if (cp > line)
  562. *cp = '\0';
  563. llist_add_to(&newlist, line);
  564. }
  565. fclose(src_stream);
  566. }
  567. return newlist;
  568. }
  569. #else
  570. #define append_file_list_to_list(x) 0
  571. #endif
  572. #if ENABLE_FEATURE_TAR_COMPRESS
  573. static char get_header_tar_Z(archive_handle_t *archive_handle)
  574. {
  575. /* Can't lseek over pipes */
  576. archive_handle->seek = seek_by_read;
  577. /* do the decompression, and cleanup */
  578. if (xread_char(archive_handle->src_fd) != 0x1f
  579. || xread_char(archive_handle->src_fd) != 0x9d
  580. ) {
  581. bb_error_msg_and_die("invalid magic");
  582. }
  583. archive_handle->src_fd = open_transformer(archive_handle->src_fd, uncompress, "uncompress", "uncompress", "-cf", "-", NULL);
  584. archive_handle->offset = 0;
  585. while (get_header_tar(archive_handle) == EXIT_SUCCESS)
  586. continue;
  587. /* Can only do one file at a time */
  588. return EXIT_FAILURE;
  589. }
  590. #else
  591. #define get_header_tar_Z NULL
  592. #endif
  593. #ifdef CHECK_FOR_CHILD_EXITCODE
  594. /* Looks like it isn't needed - tar detects malformed (truncated)
  595. * archive if e.g. bunzip2 fails */
  596. static int child_error;
  597. static void handle_SIGCHLD(int status)
  598. {
  599. /* Actually, 'status' is a signo. We reuse it for other needs */
  600. /* Wait for any child without blocking */
  601. if (waitpid(-1, &status, WNOHANG) < 0)
  602. /* wait failed?! I'm confused... */
  603. return;
  604. if (WIFEXITED(status) && WEXITSTATUS(status)==0)
  605. /* child exited with 0 */
  606. return;
  607. /* Cannot happen?
  608. if (!WIFSIGNALED(status) && !WIFEXITED(status)) return; */
  609. child_error = 1;
  610. }
  611. #endif
  612. enum {
  613. OPTBIT_KEEP_OLD = 7,
  614. USE_FEATURE_TAR_CREATE( OPTBIT_CREATE ,)
  615. USE_FEATURE_TAR_CREATE( OPTBIT_DEREFERENCE ,)
  616. USE_FEATURE_TAR_BZIP2( OPTBIT_BZIP2 ,)
  617. USE_FEATURE_TAR_LZMA( OPTBIT_LZMA ,)
  618. USE_FEATURE_TAR_FROM( OPTBIT_INCLUDE_FROM,)
  619. USE_FEATURE_TAR_FROM( OPTBIT_EXCLUDE_FROM,)
  620. USE_FEATURE_TAR_GZIP( OPTBIT_GZIP ,)
  621. USE_FEATURE_TAR_COMPRESS(OPTBIT_COMPRESS ,)
  622. OPTBIT_NOPRESERVE_OWN,
  623. OPTBIT_NOPRESERVE_PERM,
  624. OPT_TEST = 1 << 0, // t
  625. OPT_EXTRACT = 1 << 1, // x
  626. OPT_BASEDIR = 1 << 2, // C
  627. OPT_TARNAME = 1 << 3, // f
  628. OPT_2STDOUT = 1 << 4, // O
  629. OPT_P = 1 << 5, // p
  630. OPT_VERBOSE = 1 << 6, // v
  631. OPT_KEEP_OLD = 1 << 7, // k
  632. OPT_CREATE = USE_FEATURE_TAR_CREATE( (1<<OPTBIT_CREATE )) + 0, // c
  633. OPT_DEREFERENCE = USE_FEATURE_TAR_CREATE( (1<<OPTBIT_DEREFERENCE )) + 0, // h
  634. OPT_BZIP2 = USE_FEATURE_TAR_BZIP2( (1<<OPTBIT_BZIP2 )) + 0, // j
  635. OPT_LZMA = USE_FEATURE_TAR_LZMA( (1<<OPTBIT_LZMA )) + 0, // a
  636. OPT_INCLUDE_FROM = USE_FEATURE_TAR_FROM( (1<<OPTBIT_INCLUDE_FROM)) + 0, // T
  637. OPT_EXCLUDE_FROM = USE_FEATURE_TAR_FROM( (1<<OPTBIT_EXCLUDE_FROM)) + 0, // X
  638. OPT_GZIP = USE_FEATURE_TAR_GZIP( (1<<OPTBIT_GZIP )) + 0, // z
  639. OPT_COMPRESS = USE_FEATURE_TAR_COMPRESS((1<<OPTBIT_COMPRESS )) + 0, // Z
  640. OPT_NOPRESERVE_OWN = 1 << OPTBIT_NOPRESERVE_OWN , // no-same-owner
  641. OPT_NOPRESERVE_PERM = 1 << OPTBIT_NOPRESERVE_PERM, // no-same-permissions
  642. };
  643. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  644. static const char tar_longopts[] ALIGN1 =
  645. "list\0" No_argument "t"
  646. "extract\0" No_argument "x"
  647. "directory\0" Required_argument "C"
  648. "file\0" Required_argument "f"
  649. "to-stdout\0" No_argument "O"
  650. "same-permissions\0" No_argument "p"
  651. "verbose\0" No_argument "v"
  652. "keep-old\0" No_argument "k"
  653. # if ENABLE_FEATURE_TAR_CREATE
  654. "create\0" No_argument "c"
  655. "dereference\0" No_argument "h"
  656. # endif
  657. # if ENABLE_FEATURE_TAR_BZIP2
  658. "bzip2\0" No_argument "j"
  659. # endif
  660. # if ENABLE_FEATURE_TAR_LZMA
  661. "lzma\0" No_argument "a"
  662. # endif
  663. # if ENABLE_FEATURE_TAR_FROM
  664. "files-from\0" Required_argument "T"
  665. "exclude-from\0" Required_argument "X"
  666. # endif
  667. # if ENABLE_FEATURE_TAR_GZIP
  668. "gzip\0" No_argument "z"
  669. # endif
  670. # if ENABLE_FEATURE_TAR_COMPRESS
  671. "compress\0" No_argument "Z"
  672. # endif
  673. "no-same-owner\0" No_argument "\xfd"
  674. "no-same-permissions\0" No_argument "\xfe"
  675. /* --exclude takes next bit position in option mask, */
  676. /* therefore we have to either put it _after_ --no-same-perm */
  677. /* or add OPT[BIT]_EXCLUDE before OPT[BIT]_NOPRESERVE_OWN */
  678. # if ENABLE_FEATURE_TAR_FROM
  679. "exclude\0" Required_argument "\xff"
  680. # endif
  681. ;
  682. #endif
  683. int tar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  684. int tar_main(int argc, char **argv)
  685. {
  686. char (*get_header_ptr)(archive_handle_t *) = get_header_tar;
  687. archive_handle_t *tar_handle;
  688. char *base_dir = NULL;
  689. const char *tar_filename = "-";
  690. unsigned opt;
  691. int verboseFlag = 0;
  692. #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
  693. llist_t *excludes = NULL;
  694. #endif
  695. /* Initialise default values */
  696. tar_handle = init_handle();
  697. tar_handle->flags = ARCHIVE_CREATE_LEADING_DIRS
  698. | ARCHIVE_PRESERVE_DATE
  699. | ARCHIVE_EXTRACT_UNCONDITIONAL;
  700. /* Prepend '-' to the first argument if required */
  701. opt_complementary = "--:" // first arg is options
  702. "tt:vv:" // count -t,-v
  703. "?:" // bail out with usage instead of error return
  704. "X::T::" // cumulative lists
  705. #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
  706. "\xff::" // cumulative lists for --exclude
  707. #endif
  708. USE_FEATURE_TAR_CREATE("c:") "t:x:" // at least one of these is reqd
  709. USE_FEATURE_TAR_CREATE("c--tx:t--cx:x--ct") // mutually exclusive
  710. SKIP_FEATURE_TAR_CREATE("t--x:x--t"); // mutually exclusive
  711. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  712. applet_long_options = tar_longopts;
  713. #endif
  714. opt = getopt32(argv,
  715. "txC:f:Opvk"
  716. USE_FEATURE_TAR_CREATE( "ch" )
  717. USE_FEATURE_TAR_BZIP2( "j" )
  718. USE_FEATURE_TAR_LZMA( "a" )
  719. USE_FEATURE_TAR_FROM( "T:X:")
  720. USE_FEATURE_TAR_GZIP( "z" )
  721. USE_FEATURE_TAR_COMPRESS("Z" )
  722. , &base_dir // -C dir
  723. , &tar_filename // -f filename
  724. USE_FEATURE_TAR_FROM(, &(tar_handle->accept)) // T
  725. USE_FEATURE_TAR_FROM(, &(tar_handle->reject)) // X
  726. #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
  727. , &excludes // --exclude
  728. #endif
  729. , &verboseFlag // combined count for -t and -v
  730. , &verboseFlag // combined count for -t and -v
  731. );
  732. if (verboseFlag) tar_handle->action_header = header_verbose_list;
  733. if (verboseFlag == 1) tar_handle->action_header = header_list;
  734. if (opt & OPT_EXTRACT)
  735. tar_handle->action_data = data_extract_all;
  736. if (opt & OPT_2STDOUT)
  737. tar_handle->action_data = data_extract_to_stdout;
  738. if (opt & OPT_KEEP_OLD)
  739. tar_handle->flags &= ~ARCHIVE_EXTRACT_UNCONDITIONAL;
  740. if (opt & OPT_NOPRESERVE_OWN)
  741. tar_handle->flags |= ARCHIVE_NOPRESERVE_OWN;
  742. if (opt & OPT_NOPRESERVE_PERM)
  743. tar_handle->flags |= ARCHIVE_NOPRESERVE_PERM;
  744. if (opt & OPT_GZIP)
  745. get_header_ptr = get_header_tar_gz;
  746. if (opt & OPT_BZIP2)
  747. get_header_ptr = get_header_tar_bz2;
  748. if (opt & OPT_LZMA)
  749. get_header_ptr = get_header_tar_lzma;
  750. if (opt & OPT_COMPRESS)
  751. get_header_ptr = get_header_tar_Z;
  752. #if ENABLE_FEATURE_TAR_FROM
  753. tar_handle->reject = append_file_list_to_list(tar_handle->reject);
  754. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  755. /* Append excludes to reject */
  756. while (excludes) {
  757. llist_t *next = excludes->link;
  758. excludes->link = tar_handle->reject;
  759. tar_handle->reject = excludes;
  760. excludes = next;
  761. }
  762. #endif
  763. tar_handle->accept = append_file_list_to_list(tar_handle->accept);
  764. #endif
  765. /* Check if we are reading from stdin */
  766. if (argv[optind] && *argv[optind] == '-') {
  767. /* Default is to read from stdin, so just skip to next arg */
  768. optind++;
  769. }
  770. /* Setup an array of filenames to work with */
  771. /* TODO: This is the same as in ar, separate function ? */
  772. while (optind < argc) {
  773. /* kill trailing '/' unless the string is just "/" */
  774. char *cp = last_char_is(argv[optind], '/');
  775. if (cp > argv[optind])
  776. *cp = '\0';
  777. llist_add_to_end(&tar_handle->accept, argv[optind]);
  778. optind++;
  779. }
  780. if (tar_handle->accept || tar_handle->reject)
  781. tar_handle->filter = filter_accept_reject_list;
  782. /* Open the tar file */
  783. {
  784. FILE *tar_stream;
  785. int flags;
  786. if (opt & OPT_CREATE) {
  787. /* Make sure there is at least one file to tar up. */
  788. if (tar_handle->accept == NULL)
  789. bb_error_msg_and_die("empty archive");
  790. tar_stream = stdout;
  791. /* Mimicking GNU tar 1.15.1: */
  792. flags = O_WRONLY|O_CREAT|O_TRUNC;
  793. /* was doing unlink; open(O_WRONLY|O_CREAT|O_EXCL); why? */
  794. } else {
  795. tar_stream = stdin;
  796. flags = O_RDONLY;
  797. }
  798. if (LONE_DASH(tar_filename)) {
  799. tar_handle->src_fd = fileno(tar_stream);
  800. tar_handle->seek = seek_by_read;
  801. } else {
  802. tar_handle->src_fd = xopen(tar_filename, flags);
  803. }
  804. }
  805. if (base_dir)
  806. xchdir(base_dir);
  807. #ifdef CHECK_FOR_CHILD_EXITCODE
  808. /* We need to know whether child (gzip/bzip/etc) exits abnormally */
  809. signal(SIGCHLD, handle_SIGCHLD);
  810. #endif
  811. /* create an archive */
  812. if (opt & OPT_CREATE) {
  813. int zipMode = 0;
  814. if (ENABLE_FEATURE_TAR_GZIP && get_header_ptr == get_header_tar_gz)
  815. zipMode = 1;
  816. if (ENABLE_FEATURE_TAR_BZIP2 && get_header_ptr == get_header_tar_bz2)
  817. zipMode = 2;
  818. /* NB: writeTarFile() closes tar_handle->src_fd */
  819. return writeTarFile(tar_handle->src_fd, verboseFlag, opt & OPT_DEREFERENCE,
  820. tar_handle->accept,
  821. tar_handle->reject, zipMode);
  822. }
  823. while (get_header_ptr(tar_handle) == EXIT_SUCCESS)
  824. /* nothing */;
  825. /* Check that every file that should have been extracted was */
  826. while (tar_handle->accept) {
  827. if (!find_list_entry(tar_handle->reject, tar_handle->accept->data)
  828. && !find_list_entry(tar_handle->passed, tar_handle->accept->data)
  829. ) {
  830. bb_error_msg_and_die("%s: not found in archive",
  831. tar_handle->accept->data);
  832. }
  833. tar_handle->accept = tar_handle->accept->link;
  834. }
  835. if (ENABLE_FEATURE_CLEAN_UP /* && tar_handle->src_fd != STDIN_FILENO */)
  836. close(tar_handle->src_fd);
  837. return EXIT_SUCCESS;
  838. }