tar.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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. * This is free software under the GNU General Public License.
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License as published by
  26. * the Free Software Foundation; either version 2 of the License, or
  27. * (at your option) any later version.
  28. *
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  32. * General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU General Public License
  35. * along with this program; if not, write to the Free Software
  36. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  37. *
  38. */
  39. #include <fcntl.h>
  40. #include <getopt.h>
  41. #include <search.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <unistd.h>
  45. #include <fnmatch.h>
  46. #include <string.h>
  47. #include <errno.h>
  48. #include <signal.h>
  49. #include <sys/wait.h>
  50. #include <sys/socket.h>
  51. #include <sys/sysmacros.h> /* major() and minor() */
  52. #include "unarchive.h"
  53. #include "busybox.h"
  54. #ifdef CONFIG_FEATURE_TAR_CREATE
  55. /* Tar file constants */
  56. # define TAR_MAGIC "ustar" /* ustar and a null */
  57. # define TAR_VERSION " " /* Be compatable with GNU tar format */
  58. static const int TAR_BLOCK_SIZE = 512;
  59. static const int TAR_MAGIC_LEN = 6;
  60. static const int TAR_VERSION_LEN = 2;
  61. /* POSIX tar Header Block, from POSIX 1003.1-1990 */
  62. enum { NAME_SIZE = 100 }; /* because gcc won't let me use 'static const int' */
  63. struct TarHeader { /* byte offset */
  64. char name[NAME_SIZE]; /* 0-99 */
  65. char mode[8]; /* 100-107 */
  66. char uid[8]; /* 108-115 */
  67. char gid[8]; /* 116-123 */
  68. char size[12]; /* 124-135 */
  69. char mtime[12]; /* 136-147 */
  70. char chksum[8]; /* 148-155 */
  71. char typeflag; /* 156-156 */
  72. char linkname[NAME_SIZE]; /* 157-256 */
  73. char magic[6]; /* 257-262 */
  74. char version[2]; /* 263-264 */
  75. char uname[32]; /* 265-296 */
  76. char gname[32]; /* 297-328 */
  77. char devmajor[8]; /* 329-336 */
  78. char devminor[8]; /* 337-344 */
  79. char prefix[155]; /* 345-499 */
  80. char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */
  81. };
  82. typedef struct TarHeader TarHeader;
  83. /*
  84. ** writeTarFile(), writeFileToTarball(), and writeTarHeader() are
  85. ** the only functions that deal with the HardLinkInfo structure.
  86. ** Even these functions use the xxxHardLinkInfo() functions.
  87. */
  88. typedef struct HardLinkInfo HardLinkInfo;
  89. struct HardLinkInfo {
  90. HardLinkInfo *next; /* Next entry in list */
  91. dev_t dev; /* Device number */
  92. ino_t ino; /* Inode number */
  93. short linkCount; /* (Hard) Link Count */
  94. char name[1]; /* Start of filename (must be last) */
  95. };
  96. /* Some info to be carried along when creating a new tarball */
  97. struct TarBallInfo {
  98. char *fileName; /* File name of the tarball */
  99. int tarFd; /* Open-for-write file descriptor
  100. for the tarball */
  101. struct stat statBuf; /* Stat info for the tarball, letting
  102. us know the inode and device that the
  103. tarball lives, so we can avoid trying
  104. to include the tarball into itself */
  105. int verboseFlag; /* Whether to print extra stuff or not */
  106. const llist_t *excludeList; /* List of files to not include */
  107. HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */
  108. HardLinkInfo *hlInfo; /* Hard Link Info for the current file */
  109. };
  110. typedef struct TarBallInfo TarBallInfo;
  111. /* A nice enum with all the possible tar file content types */
  112. enum TarFileType {
  113. REGTYPE = '0', /* regular file */
  114. REGTYPE0 = '\0', /* regular file (ancient bug compat) */
  115. LNKTYPE = '1', /* hard link */
  116. SYMTYPE = '2', /* symbolic link */
  117. CHRTYPE = '3', /* character special */
  118. BLKTYPE = '4', /* block special */
  119. DIRTYPE = '5', /* directory */
  120. FIFOTYPE = '6', /* FIFO special */
  121. CONTTYPE = '7', /* reserved */
  122. GNULONGLINK = 'K', /* GNU long (>100 chars) link name */
  123. GNULONGNAME = 'L', /* GNU long (>100 chars) file name */
  124. };
  125. typedef enum TarFileType TarFileType;
  126. /* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
  127. static inline void addHardLinkInfo(HardLinkInfo ** hlInfoHeadPtr,
  128. struct stat *statbuf,
  129. const char *name)
  130. {
  131. /* Note: hlInfoHeadPtr can never be NULL! */
  132. HardLinkInfo *hlInfo;
  133. hlInfo = (HardLinkInfo *) xmalloc(sizeof(HardLinkInfo) + strlen(name));
  134. hlInfo->next = *hlInfoHeadPtr;
  135. *hlInfoHeadPtr = hlInfo;
  136. hlInfo->dev = statbuf->st_dev;
  137. hlInfo->ino = statbuf->st_ino;
  138. hlInfo->linkCount = statbuf->st_nlink;
  139. strcpy(hlInfo->name, name);
  140. }
  141. static void freeHardLinkInfo(HardLinkInfo ** hlInfoHeadPtr)
  142. {
  143. HardLinkInfo *hlInfo = NULL;
  144. HardLinkInfo *hlInfoNext = NULL;
  145. if (hlInfoHeadPtr) {
  146. hlInfo = *hlInfoHeadPtr;
  147. while (hlInfo) {
  148. hlInfoNext = hlInfo->next;
  149. free(hlInfo);
  150. hlInfo = hlInfoNext;
  151. }
  152. *hlInfoHeadPtr = NULL;
  153. }
  154. return;
  155. }
  156. /* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
  157. static inline HardLinkInfo *findHardLinkInfo(HardLinkInfo * hlInfo, struct stat *statbuf)
  158. {
  159. while (hlInfo) {
  160. if ((statbuf->st_ino == hlInfo->ino) && (statbuf->st_dev == hlInfo->dev))
  161. break;
  162. hlInfo = hlInfo->next;
  163. }
  164. return (hlInfo);
  165. }
  166. /* Put an octal string into the specified buffer.
  167. * The number is zero and space padded and possibly null padded.
  168. * Returns TRUE if successful. */
  169. static int putOctal(char *cp, int len, long value)
  170. {
  171. int tempLength;
  172. char tempBuffer[32];
  173. char *tempString = tempBuffer;
  174. /* Create a string of the specified length with an initial space,
  175. * leading zeroes and the octal number, and a trailing null. */
  176. sprintf(tempString, "%0*lo", len - 1, value);
  177. /* If the string is too large, suppress the leading space. */
  178. tempLength = strlen(tempString) + 1;
  179. if (tempLength > len) {
  180. tempLength--;
  181. tempString++;
  182. }
  183. /* If the string is still too large, suppress the trailing null. */
  184. if (tempLength > len)
  185. tempLength--;
  186. /* If the string is still too large, fail. */
  187. if (tempLength > len)
  188. return FALSE;
  189. /* Copy the string to the field. */
  190. memcpy(cp, tempString, len);
  191. return TRUE;
  192. }
  193. /* Write out a tar header for the specified file/directory/whatever */
  194. static inline int writeTarHeader(struct TarBallInfo *tbInfo,
  195. const char *header_name,
  196. const char *real_name, struct stat *statbuf)
  197. {
  198. long chksum = 0;
  199. struct TarHeader header;
  200. const unsigned char *cp = (const unsigned char *) &header;
  201. ssize_t size = sizeof(struct TarHeader);
  202. memset(&header, 0, size);
  203. strncpy(header.name, header_name, sizeof(header.name));
  204. putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
  205. putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
  206. putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
  207. putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
  208. putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
  209. strncpy(header.magic, TAR_MAGIC TAR_VERSION,
  210. TAR_MAGIC_LEN + TAR_VERSION_LEN);
  211. /* Enter the user and group names (default to root if it fails) */
  212. if (my_getpwuid(header.uname, statbuf->st_uid, sizeof(header.uname)) == NULL)
  213. strcpy(header.uname, "root");
  214. if (my_getgrgid(header.gname, statbuf->st_gid, sizeof(header.gname)) == NULL)
  215. strcpy(header.gname, "root");
  216. if (tbInfo->hlInfo) {
  217. /* This is a hard link */
  218. header.typeflag = LNKTYPE;
  219. strncpy(header.linkname, tbInfo->hlInfo->name,
  220. sizeof(header.linkname));
  221. } else if (S_ISLNK(statbuf->st_mode)) {
  222. char *lpath = xreadlink(real_name);
  223. if (!lpath) /* Already printed err msg inside xreadlink() */
  224. return (FALSE);
  225. header.typeflag = SYMTYPE;
  226. strncpy(header.linkname, lpath, sizeof(header.linkname));
  227. free(lpath);
  228. } else if (S_ISDIR(statbuf->st_mode)) {
  229. header.typeflag = DIRTYPE;
  230. strncat(header.name, "/", sizeof(header.name));
  231. } else if (S_ISCHR(statbuf->st_mode)) {
  232. header.typeflag = CHRTYPE;
  233. putOctal(header.devmajor, sizeof(header.devmajor),
  234. major(statbuf->st_rdev));
  235. putOctal(header.devminor, sizeof(header.devminor),
  236. minor(statbuf->st_rdev));
  237. } else if (S_ISBLK(statbuf->st_mode)) {
  238. header.typeflag = BLKTYPE;
  239. putOctal(header.devmajor, sizeof(header.devmajor),
  240. major(statbuf->st_rdev));
  241. putOctal(header.devminor, sizeof(header.devminor),
  242. minor(statbuf->st_rdev));
  243. } else if (S_ISFIFO(statbuf->st_mode)) {
  244. header.typeflag = FIFOTYPE;
  245. } else if (S_ISREG(statbuf->st_mode)) {
  246. header.typeflag = REGTYPE;
  247. putOctal(header.size, sizeof(header.size), statbuf->st_size);
  248. } else {
  249. bb_error_msg("%s: Unknown file type", real_name);
  250. return (FALSE);
  251. }
  252. /* Calculate and store the checksum (i.e., the sum of all of the bytes of
  253. * the header). The checksum field must be filled with blanks for the
  254. * calculation. The checksum field is formatted differently from the
  255. * other fields: it has [6] digits, a null, then a space -- rather than
  256. * digits, followed by a null like the other fields... */
  257. memset(header.chksum, ' ', sizeof(header.chksum));
  258. cp = (const unsigned char *) &header;
  259. while (size-- > 0)
  260. chksum += *cp++;
  261. putOctal(header.chksum, 7, chksum);
  262. /* Now write the header out to disk */
  263. if ((size =
  264. bb_full_write(tbInfo->tarFd, (char *) &header,
  265. sizeof(struct TarHeader))) < 0) {
  266. bb_error_msg(bb_msg_io_error, real_name);
  267. return (FALSE);
  268. }
  269. /* Pad the header up to the tar block size */
  270. for (; size < TAR_BLOCK_SIZE; size++) {
  271. write(tbInfo->tarFd, "\0", 1);
  272. }
  273. /* Now do the verbose thing (or not) */
  274. if (tbInfo->verboseFlag) {
  275. FILE *vbFd = stdout;
  276. if (tbInfo->tarFd == STDOUT_FILENO) /* If the archive goes to stdout, verbose to stderr */
  277. vbFd = stderr;
  278. fprintf(vbFd, "%s\n", header.name);
  279. }
  280. return (TRUE);
  281. }
  282. # ifdef CONFIG_FEATURE_TAR_FROM
  283. static inline int exclude_file(const llist_t *excluded_files, const char *file)
  284. {
  285. while (excluded_files) {
  286. if (excluded_files->data[0] == '/') {
  287. if (fnmatch(excluded_files->data, file,
  288. FNM_PATHNAME | FNM_LEADING_DIR) == 0)
  289. return 1;
  290. } else {
  291. const char *p;
  292. for (p = file; p[0] != '\0'; p++) {
  293. if ((p == file || p[-1] == '/') && p[0] != '/' &&
  294. fnmatch(excluded_files->data, p,
  295. FNM_PATHNAME | FNM_LEADING_DIR) == 0)
  296. return 1;
  297. }
  298. }
  299. excluded_files = excluded_files->link;
  300. }
  301. return 0;
  302. }
  303. # endif
  304. static int writeFileToTarball(const char *fileName, struct stat *statbuf,
  305. void *userData)
  306. {
  307. struct TarBallInfo *tbInfo = (struct TarBallInfo *) userData;
  308. const char *header_name;
  309. /*
  310. ** Check to see if we are dealing with a hard link.
  311. ** If so -
  312. ** Treat the first occurance of a given dev/inode as a file while
  313. ** treating any additional occurances as hard links. This is done
  314. ** by adding the file information to the HardLinkInfo linked list.
  315. */
  316. tbInfo->hlInfo = NULL;
  317. if (statbuf->st_nlink > 1) {
  318. tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf);
  319. if (tbInfo->hlInfo == NULL)
  320. addHardLinkInfo(&tbInfo->hlInfoHead, statbuf, fileName);
  321. }
  322. /* It is against the rules to archive a socket */
  323. if (S_ISSOCK(statbuf->st_mode)) {
  324. bb_error_msg("%s: socket ignored", fileName);
  325. return (TRUE);
  326. }
  327. /* It is a bad idea to store the archive we are in the process of creating,
  328. * so check the device and inode to be sure that this particular file isn't
  329. * the new tarball */
  330. if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
  331. tbInfo->statBuf.st_ino == statbuf->st_ino) {
  332. bb_error_msg("%s: file is the archive; skipping", fileName);
  333. return (TRUE);
  334. }
  335. header_name = fileName;
  336. while (header_name[0] == '/') {
  337. static int alreadyWarned = FALSE;
  338. if (alreadyWarned == FALSE) {
  339. bb_error_msg("Removing leading '/' from member names");
  340. alreadyWarned = TRUE;
  341. }
  342. header_name++;
  343. }
  344. if (strlen(fileName) >= NAME_SIZE) {
  345. bb_error_msg(bb_msg_name_longer_than_foo, NAME_SIZE);
  346. return (TRUE);
  347. }
  348. if (header_name[0] == '\0')
  349. return TRUE;
  350. # ifdef CONFIG_FEATURE_TAR_FROM
  351. if (exclude_file(tbInfo->excludeList, header_name)) {
  352. return SKIP;
  353. }
  354. # endif /* CONFIG_FEATURE_TAR_FROM */
  355. if (writeTarHeader(tbInfo, header_name, fileName, statbuf) == FALSE) {
  356. return (FALSE);
  357. }
  358. /* Now, if the file is a regular file, copy it out to the tarball */
  359. if ((tbInfo->hlInfo == NULL)
  360. && (S_ISREG(statbuf->st_mode))) {
  361. int inputFileFd;
  362. ssize_t readSize = 0;
  363. /* open the file we want to archive, and make sure all is well */
  364. if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
  365. bb_perror_msg("%s: Cannot open", fileName);
  366. return (FALSE);
  367. }
  368. /* write the file to the archive */
  369. readSize = bb_copyfd_eof(inputFileFd, tbInfo->tarFd);
  370. /* Pad the file up to the tar block size */
  371. for (; (readSize % TAR_BLOCK_SIZE) != 0; readSize++) {
  372. write(tbInfo->tarFd, "\0", 1);
  373. }
  374. close(inputFileFd);
  375. }
  376. return (TRUE);
  377. }
  378. static inline int writeTarFile(const int tar_fd, const int verboseFlag,
  379. const unsigned long dereferenceFlag, const llist_t *include,
  380. const llist_t *exclude, const int gzip)
  381. {
  382. #ifdef CONFIG_FEATURE_TAR_GZIP
  383. int gzipDataPipe[2] = { -1, -1 };
  384. int gzipStatusPipe[2] = { -1, -1 };
  385. pid_t gzipPid = 0;
  386. volatile int vfork_exec_errno = 0;
  387. #endif
  388. int errorFlag = FALSE;
  389. ssize_t size;
  390. struct TarBallInfo tbInfo;
  391. tbInfo.hlInfoHead = NULL;
  392. fchmod(tar_fd, 0644);
  393. tbInfo.tarFd = tar_fd;
  394. tbInfo.verboseFlag = verboseFlag;
  395. /* Store the stat info for the tarball's file, so
  396. * can avoid including the tarball into itself.... */
  397. if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
  398. bb_perror_msg_and_die("Couldnt stat tar file");
  399. #ifdef CONFIG_FEATURE_TAR_GZIP
  400. if (gzip) {
  401. if (pipe(gzipDataPipe) < 0 || pipe(gzipStatusPipe) < 0) {
  402. bb_perror_msg_and_die("Failed to create gzip pipe");
  403. }
  404. signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */
  405. # if __GNUC__
  406. /* Avoid vfork clobbering */
  407. (void) &include;
  408. (void) &errorFlag;
  409. # endif
  410. gzipPid = vfork();
  411. if (gzipPid == 0) {
  412. dup2(gzipDataPipe[0], 0);
  413. close(gzipDataPipe[1]);
  414. if (tbInfo.tarFd != 1)
  415. dup2(tbInfo.tarFd, 1);
  416. close(gzipStatusPipe[0]);
  417. fcntl(gzipStatusPipe[1], F_SETFD, FD_CLOEXEC); /* close on exec shows sucess */
  418. execl("/bin/gzip", "gzip", "-f", 0);
  419. vfork_exec_errno = errno;
  420. close(gzipStatusPipe[1]);
  421. exit(-1);
  422. } else if (gzipPid > 0) {
  423. close(gzipDataPipe[0]);
  424. close(gzipStatusPipe[1]);
  425. while (1) {
  426. char buf;
  427. int n = bb_full_read(gzipStatusPipe[0], &buf, 1);
  428. if (n == 0 && vfork_exec_errno != 0) {
  429. errno = vfork_exec_errno;
  430. bb_perror_msg_and_die("Could not exec gzip process");
  431. } else if ((n < 0) && (errno == EAGAIN || errno == EINTR))
  432. continue; /* try it again */
  433. break;
  434. }
  435. close(gzipStatusPipe[0]);
  436. tbInfo.tarFd = gzipDataPipe[1];
  437. } else {
  438. bb_perror_msg_and_die("Failed to vfork gzip process");
  439. }
  440. }
  441. #endif
  442. tbInfo.excludeList = exclude;
  443. /* Read the directory/files and iterate over them one at a time */
  444. while (include) {
  445. if (!recursive_action(include->data, TRUE, dereferenceFlag, FALSE,
  446. writeFileToTarball, writeFileToTarball,
  447. (void *) &tbInfo)) {
  448. errorFlag = TRUE;
  449. }
  450. include = include->link;
  451. }
  452. /* Write two empty blocks to the end of the archive */
  453. for (size = 0; size < (2 * TAR_BLOCK_SIZE); size++) {
  454. write(tbInfo.tarFd, "\0", 1);
  455. }
  456. /* To be pedantically correct, we would check if the tarball
  457. * is smaller than 20 tar blocks, and pad it if it was smaller,
  458. * but that isn't necessary for GNU tar interoperability, and
  459. * so is considered a waste of space */
  460. /* Hang up the tools, close up shop, head home */
  461. close(tbInfo.tarFd);
  462. if (errorFlag)
  463. bb_error_msg("Error exit delayed from previous errors");
  464. freeHardLinkInfo(&tbInfo.hlInfoHead);
  465. #ifdef CONFIG_FEATURE_TAR_GZIP
  466. if (gzip && gzipPid) {
  467. if (waitpid(gzipPid, NULL, 0) == -1)
  468. printf("Couldnt wait ?");
  469. }
  470. #endif
  471. return !errorFlag;
  472. }
  473. #endif /* tar_create */
  474. #ifdef CONFIG_FEATURE_TAR_FROM
  475. static llist_t *append_file_list_to_list(llist_t *list)
  476. {
  477. FILE *src_stream;
  478. llist_t *cur = list;
  479. llist_t *tmp;
  480. char *line;
  481. llist_t *newlist = NULL;
  482. while(cur) {
  483. src_stream = bb_xfopen(cur->data, "r");
  484. tmp = cur;
  485. cur = cur->link;
  486. free(tmp);
  487. while((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
  488. newlist = llist_add_to(newlist, line);
  489. }
  490. fclose(src_stream);
  491. }
  492. return newlist;
  493. }
  494. #endif
  495. #ifdef CONFIG_FEATURE_TAR_COMPRESS
  496. static char get_header_tar_Z(archive_handle_t *archive_handle)
  497. {
  498. /* Cant lseek over pipe's */
  499. archive_handle->seek = seek_by_char;
  500. /* do the decompression, and cleanup */
  501. if ((bb_xread_char(archive_handle->src_fd) != 0x1f) || (bb_xread_char(archive_handle->src_fd) != 0x9d)) {
  502. bb_error_msg_and_die("Invalid magic");
  503. }
  504. archive_handle->src_fd = open_transformer(archive_handle->src_fd, uncompress);
  505. archive_handle->offset = 0;
  506. while (get_header_tar(archive_handle) == EXIT_SUCCESS);
  507. /* Can only do one file at a time */
  508. return(EXIT_FAILURE);
  509. }
  510. #endif
  511. #define CTX_TEST (1 << 0)
  512. #define CTX_EXTRACT (1 << 1)
  513. #define TAR_OPT_BASEDIR (1 << 2)
  514. #define TAR_OPT_TARNAME (1 << 3)
  515. #define TAR_OPT_2STDOUT (1 << 4)
  516. #define TAR_OPT_P (1 << 5)
  517. #define TAR_OPT_VERBOSE (1 << 6)
  518. #define TAR_OPT_KEEP_OLD (1 << 7)
  519. #ifdef CONFIG_FEATURE_TAR_CREATE
  520. # define CTX_CREATE (1 << 8)
  521. # define TAR_OPT_DEREFERNCE (1 << 9)
  522. # define TAR_OPT_STR_CREATE "ch"
  523. # define TAR_OPT_FLAG_CREATE 2
  524. #else
  525. //# define CTX_CREATE 0
  526. # define TAR_OPT_STR_CREATE ""
  527. # define TAR_OPT_FLAG_CREATE 0
  528. #endif
  529. #ifdef CONFIG_FEATURE_TAR_BZIP2
  530. # define TAR_OPT_BZIP2 (1 << (8 + TAR_OPT_FLAG_CREATE))
  531. # define TAR_OPT_STR_BZIP2 "j"
  532. # define TAR_OPT_FLAG_BZIP2 1
  533. #else
  534. # define TAR_OPT_STR_BZIP2 ""
  535. # define TAR_OPT_FLAG_BZIP2 0
  536. #endif
  537. #ifdef CONFIG_FEATURE_TAR_FROM
  538. # define TAR_OPT_INCLUDE_FROM (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2))
  539. # define TAR_OPT_EXCLUDE_FROM (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2 + 1))
  540. # define TAR_OPT_STR_FROM "T:X:"
  541. # define TAR_OPT_FLAG_FROM 2
  542. #else
  543. # define TAR_OPT_STR_FROM ""
  544. # define TAR_OPT_FLAG_FROM 0
  545. #endif
  546. #ifdef CONFIG_FEATURE_TAR_GZIP
  547. # define TAR_OPT_GZIP (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2 + TAR_OPT_FLAG_FROM))
  548. # define TAR_OPT_STR_GZIP "z"
  549. # define TAR_OPT_FLAG_GZIP 1
  550. #else
  551. # define TAR_OPT_STR_GZIP ""
  552. # define TAR_OPT_FLAG_GZIP 0
  553. #endif
  554. #ifdef CONFIG_FEATURE_TAR_COMPRESS
  555. # define TAR_OPT_UNCOMPRESS (1 << (8 + TAR_OPT_FLAG_CREATE + TAR_OPT_FLAG_BZIP2 + TAR_OPT_FLAG_FROM + TAR_OPT_FLAG_GZIP))
  556. # define TAR_OPT_STR_COMPRESS "Z"
  557. #else
  558. # define TAR_OPT_STR_COMPRESS ""
  559. #endif
  560. static const char tar_options[]="txC:f:Opvk" \
  561. TAR_OPT_STR_CREATE \
  562. TAR_OPT_STR_BZIP2 \
  563. TAR_OPT_STR_FROM \
  564. TAR_OPT_STR_GZIP \
  565. TAR_OPT_STR_COMPRESS;
  566. #ifdef CONFIG_FEATURE_TAR_LONG_OPTIONS
  567. static const struct option tar_long_options[] = {
  568. { "list", 0, NULL, 't' },
  569. { "extract", 0, NULL, 'x' },
  570. { "directory", 1, NULL, 'C' },
  571. { "file", 1, NULL, 'f' },
  572. { "to-stdout", 0, NULL, 'O' },
  573. { "same-permissions", 0, NULL, 'p' },
  574. { "verbose", 0, NULL, 'v' },
  575. { "keep-old", 0, NULL, 'k' },
  576. # ifdef CONFIG_FEATURE_TAR_CREATE
  577. { "create", 0, NULL, 'c' },
  578. { "dereference", 0, NULL, 'h' },
  579. # endif
  580. # ifdef CONFIG_FEATURE_TAR_BZIP2
  581. { "bzip2", 0, NULL, 'j' },
  582. # endif
  583. # ifdef CONFIG_FEATURE_TAR_FROM
  584. { "files-from", 1, NULL, 'T' },
  585. { "exclude-from", 1, NULL, 'X' },
  586. # endif
  587. # ifdef CONFIG_FEATURE_TAR_GZIP
  588. { "gzip", 0, NULL, 'z' },
  589. # endif
  590. # ifdef CONFIG_FEATURE_TAR_COMPRESS
  591. { "compress", 0, NULL, 'Z' },
  592. # endif
  593. { 0, 0, 0, 0 }
  594. };
  595. #endif
  596. int tar_main(int argc, char **argv)
  597. {
  598. char (*get_header_ptr)(archive_handle_t *) = get_header_tar;
  599. archive_handle_t *tar_handle;
  600. char *base_dir = NULL;
  601. const char *tar_filename = "-";
  602. unsigned long opt;
  603. unsigned long ctx_flag = 0;
  604. if (argc < 2) {
  605. bb_show_usage();
  606. }
  607. /* Prepend '-' to the first argument if required */
  608. if (argv[1][0] != '-') {
  609. char *tmp;
  610. bb_xasprintf(&tmp, "-%s", argv[1]);
  611. argv[1] = tmp;
  612. }
  613. /* Initialise default values */
  614. tar_handle = init_handle();
  615. tar_handle->flags = ARCHIVE_CREATE_LEADING_DIRS | ARCHIVE_PRESERVE_DATE | ARCHIVE_EXTRACT_UNCONDITIONAL;
  616. bb_opt_complementaly = "c~tx:t~cx:x~ct:X*:T*";
  617. #ifdef CONFIG_FEATURE_TAR_LONG_OPTIONS
  618. bb_applet_long_options = tar_long_options;
  619. #endif
  620. opt = bb_getopt_ulflags(argc, argv, tar_options,
  621. &base_dir, /* Change to dir <optarg> */
  622. &tar_filename /* archive filename */
  623. #ifdef CONFIG_FEATURE_TAR_FROM
  624. , &(tar_handle->accept),
  625. &(tar_handle->reject)
  626. #endif
  627. );
  628. /* Check one and only one context option was given */
  629. if(opt & BB_GETOPT_ERROR) {
  630. bb_show_usage();
  631. }
  632. #ifdef CONFIG_FEATURE_TAR_CREATE
  633. ctx_flag = opt & (CTX_CREATE | CTX_TEST | CTX_EXTRACT);
  634. #else
  635. ctx_flag = opt & (CTX_TEST | CTX_EXTRACT);
  636. #endif
  637. if (ctx_flag == 0) {
  638. bb_show_usage();
  639. }
  640. if(ctx_flag & CTX_TEST) {
  641. if ((tar_handle->action_header == header_list) ||
  642. (tar_handle->action_header == header_verbose_list)) {
  643. tar_handle->action_header = header_verbose_list;
  644. } else {
  645. tar_handle->action_header = header_list;
  646. }
  647. }
  648. if(ctx_flag & CTX_EXTRACT) {
  649. if (tar_handle->action_data != data_extract_to_stdout)
  650. tar_handle->action_data = data_extract_all;
  651. }
  652. if(opt & TAR_OPT_2STDOUT) {
  653. /* To stdout */
  654. tar_handle->action_data = data_extract_to_stdout;
  655. }
  656. if(opt & TAR_OPT_VERBOSE) {
  657. if ((tar_handle->action_header == header_list) ||
  658. (tar_handle->action_header == header_verbose_list))
  659. {
  660. tar_handle->action_header = header_verbose_list;
  661. } else {
  662. tar_handle->action_header = header_list;
  663. }
  664. }
  665. if (opt & TAR_OPT_KEEP_OLD) {
  666. tar_handle->flags &= ~ARCHIVE_EXTRACT_UNCONDITIONAL;
  667. }
  668. #ifdef CONFIG_FEATURE_TAR_GZIP
  669. if(opt & TAR_OPT_GZIP) {
  670. get_header_ptr = get_header_tar_gz;
  671. }
  672. #endif
  673. #ifdef CONFIG_FEATURE_TAR_BZIP2
  674. if(opt & TAR_OPT_BZIP2) {
  675. get_header_ptr = get_header_tar_bz2;
  676. }
  677. #endif
  678. #ifdef CONFIG_FEATURE_TAR_COMPRESS
  679. if(opt & TAR_OPT_UNCOMPRESS) {
  680. get_header_ptr = get_header_tar_Z;
  681. }
  682. #endif
  683. #ifdef CONFIG_FEATURE_TAR_FROM
  684. if(opt & TAR_OPT_EXCLUDE_FROM) {
  685. tar_handle->reject = append_file_list_to_list(tar_handle->reject);
  686. }
  687. if(opt & TAR_OPT_INCLUDE_FROM) {
  688. tar_handle->accept = append_file_list_to_list(tar_handle->accept);
  689. }
  690. #endif
  691. /* Check if we are reading from stdin */
  692. if ((argv[optind]) && (*argv[optind] == '-')) {
  693. /* Default is to read from stdin, so just skip to next arg */
  694. optind++;
  695. }
  696. /* Setup an array of filenames to work with */
  697. /* TODO: This is the same as in ar, separate function ? */
  698. while (optind < argc) {
  699. char *filename_ptr = last_char_is(argv[optind], '/');
  700. if (filename_ptr) {
  701. *filename_ptr = '\0';
  702. }
  703. tar_handle->accept = llist_add_to(tar_handle->accept, argv[optind]);
  704. optind++;
  705. }
  706. if ((tar_handle->accept) || (tar_handle->reject)) {
  707. tar_handle->filter = filter_accept_reject_list;
  708. }
  709. /* Open the tar file */
  710. {
  711. FILE *tar_stream;
  712. int flags;
  713. #ifdef CONFIG_FEATURE_TAR_CREATE
  714. if (opt & CTX_CREATE) {
  715. /* Make sure there is at least one file to tar up. */
  716. if (tar_handle->accept == NULL) {
  717. bb_error_msg_and_die("Cowardly refusing to create an empty archive");
  718. }
  719. tar_stream = stdout;
  720. flags = O_WRONLY | O_CREAT | O_EXCL;
  721. unlink(tar_filename);
  722. } else
  723. #endif
  724. {
  725. tar_stream = stdin;
  726. flags = O_RDONLY;
  727. }
  728. if ((tar_filename[0] == '-') && (tar_filename[1] == '\0')) {
  729. tar_handle->src_fd = fileno(tar_stream);
  730. tar_handle->seek = seek_by_char;
  731. } else {
  732. tar_handle->src_fd = bb_xopen(tar_filename, flags);
  733. }
  734. }
  735. if ((base_dir) && (chdir(base_dir))) {
  736. bb_perror_msg_and_die("Couldnt chdir to %s", base_dir);
  737. }
  738. #ifdef CONFIG_FEATURE_TAR_CREATE
  739. /* create an archive */
  740. if (opt & CTX_CREATE) {
  741. int verboseFlag = FALSE;
  742. int gzipFlag = FALSE;
  743. # ifdef CONFIG_FEATURE_TAR_GZIP
  744. if (get_header_ptr == get_header_tar_gz) {
  745. gzipFlag = TRUE;
  746. }
  747. # endif /* CONFIG_FEATURE_TAR_GZIP */
  748. # ifdef CONFIG_FEATURE_TAR_BZIP2
  749. if (get_header_ptr == get_header_tar_bz2) {
  750. bb_error_msg_and_die("Creating bzip2 compressed archives is not currently supported.");
  751. }
  752. # endif /* CONFIG_FEATURE_TAR_BZIP2 */
  753. if ((tar_handle->action_header == header_list) ||
  754. (tar_handle->action_header == header_verbose_list)) {
  755. verboseFlag = TRUE;
  756. }
  757. writeTarFile(tar_handle->src_fd, verboseFlag, opt & TAR_OPT_DEREFERNCE, tar_handle->accept,
  758. tar_handle->reject, gzipFlag);
  759. } else
  760. #endif /* CONFIG_FEATURE_TAR_CREATE */
  761. {
  762. while (get_header_ptr(tar_handle) == EXIT_SUCCESS);
  763. /* Ckeck that every file that should have been extracted was */
  764. while (tar_handle->accept) {
  765. if (find_list_entry(tar_handle->reject, tar_handle->accept->data) == NULL) {
  766. if (find_list_entry(tar_handle->passed, tar_handle->accept->data) == NULL) {
  767. bb_error_msg_and_die("%s: Not found in archive\n", tar_handle->accept->data);
  768. }
  769. }
  770. tar_handle->accept = tar_handle->accept->link;
  771. }
  772. }
  773. #ifdef CONFIG_FEATURE_CLEAN_UP
  774. if (tar_handle->src_fd != STDIN_FILENO) {
  775. close(tar_handle->src_fd);
  776. }
  777. #endif /* CONFIG_FEATURE_CLEAN_UP */
  778. return(EXIT_SUCCESS);
  779. }