tar.c 32 KB

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