tar.c 28 KB

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