tar2qfile.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /*
  2. /* $OpenBSD: tar.h,v 1.7 2003/06/02 23:32:09 millert Exp $ */
  3. /* $NetBSD: tar.h,v 1.3 1995/03/21 09:07:51 cgd Exp $ */
  4. /*-
  5. * Copyright (c) 1992 Keith Muller.
  6. * Copyright (c) 1992, 1993
  7. * The Regents of the University of California. All rights reserved.
  8. *
  9. * This code is derived from software contributed to Berkeley by
  10. * Keith Muller of the University of California, San Diego.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * @(#)tar.h 8.2 (Berkeley) 4/18/94
  37. */
  38. #define _GNU_SOURCE /* For O_NOFOLLOW. */
  39. #include <errno.h>
  40. #include <ioall.h>
  41. #include <fcntl.h>
  42. #include <sys/time.h>
  43. #include <sys/stat.h>
  44. #include <stdlib.h>
  45. #include <unistd.h>
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <qfile-agent.c>
  49. /***************************************************
  50. * Most routines extracted from the PAX project (tar.c...) *
  51. ***************************************************/
  52. /*
  53. * BSD PAX global data structures and constants.
  54. */
  55. #define MAXBLK 64512 /* MAX blocksize supported (posix SPEC) */
  56. /* WARNING: increasing MAXBLK past 32256 */
  57. /* will violate posix spec. */
  58. #define MAXBLK_POSIX 32256 /* MAX blocksize supported as per POSIX */
  59. #define BLKMULT 512 /* blocksize must be even mult of 512 bytes */
  60. /* Don't even think of changing this */
  61. #define DEVBLK 8192 /* default read blksize for devices */
  62. #define FILEBLK 10240 /* default read blksize for files */
  63. #define PAXPATHLEN 3072 /* maximium path length for pax. MUST be */
  64. /*
  65. * defines and data structures common to all tar formats
  66. */
  67. #define CHK_LEN 8 /* length of checksum field */
  68. #define TNMSZ 100 /* size of name field */
  69. #define NULLCNT 2 /* number of null blocks in trailer */
  70. #define CHK_OFFSET 148 /* start of chksum field */
  71. #define BLNKSUM 256L /* sum of checksum field using ' ' */
  72. /*
  73. * General Defines
  74. */
  75. #define HEX 16
  76. #define OCT 8
  77. #define _PAX_ 1
  78. #define _TFILE_BASE "paxXXXXXXXXXX"
  79. /*
  80. * General Macros
  81. */
  82. #ifndef MIN
  83. #define MIN(a,b) (((a)<(b))?(a):(b))
  84. #endif
  85. #ifndef MAX
  86. #define MAX(a,b) (((a)>(b))?(a):(b))
  87. #endif
  88. #define MAJOR(x) major(x)
  89. #define MINOR(x) minor(x)
  90. #define TODEV(x, y) makedev((x), (y))
  91. /*
  92. * Values used in typeflag field in all tar formats
  93. * (only REGTYPE, LNKTYPE and SYMTYPE are used in old bsd tar headers)
  94. */
  95. #define REGTYPE '0' /* Regular File */
  96. #define AREGTYPE '\0' /* Regular File */
  97. #define LNKTYPE '1' /* Link */
  98. #define SYMTYPE '2' /* Symlink */
  99. #define CHRTYPE '3' /* Character Special File */
  100. #define BLKTYPE '4' /* Block Special File */
  101. #define DIRTYPE '5' /* Directory */
  102. #define FIFOTYPE '6' /* FIFO */
  103. #define CONTTYPE '7' /* high perf file */
  104. /*
  105. * GNU tar compatibility;
  106. */
  107. #define LONGLINKTYPE 'K' /* Long Symlink */
  108. #define LONGNAMETYPE 'L' /* Long File */
  109. #define EXTHEADERTYPE 'x' /* Extended header */
  110. /*
  111. * Pad with a bit mask, much faster than doing a mod but only works on powers
  112. * of 2. Macro below is for block of 512 bytes.
  113. */
  114. #define TAR_PAD(x) ((512 - ((x) & 511)) & 511)
  115. /*
  116. * Data Interchange Format - Extended tar header format - POSIX 1003.1-1990
  117. */
  118. #define TPFSZ 155
  119. #define TMAGIC "ustar" /* ustar and a null */
  120. #define TMAGLEN 6
  121. #define TVERSION "00" /* 00 and no null */
  122. #define TVERSLEN 2
  123. typedef struct {
  124. char name[TNMSZ]; /* name of entry */
  125. char mode[8]; /* mode */
  126. char uid[8]; /* uid */
  127. char gid[8]; /* gid */
  128. char size[12]; /* size */
  129. char mtime[12]; /* modification time */
  130. char chksum[CHK_LEN]; /* checksum */
  131. char typeflag; /* type of file. */
  132. char linkname[TNMSZ]; /* linked to name */
  133. char magic[TMAGLEN]; /* magic cookie */
  134. char version[TVERSLEN]; /* version */
  135. char uname[32]; /* ascii owner name */
  136. char gname[32]; /* ascii group name */
  137. char devmajor[8]; /* major device number */
  138. char devminor[8]; /* minor device number */
  139. char prefix[TPFSZ]; /* linked to name */
  140. } HD_USTAR;
  141. /*
  142. * Routines for manipulating headers, trailers:
  143. * asc_ul()
  144. * tar_trail()
  145. * tar_chksm()
  146. * ustar_id()
  147. */
  148. static unsigned long tar_chksm (char *, int);
  149. char *gnu_hack_string; /* GNU ././@LongLink hackery */
  150. char untrusted_namebuf[MAX_PATH_LENGTH];
  151. /*
  152. * asc_ul()
  153. * convert hex/octal character string into a u_long. We do not have to
  154. * check for overflow! (the headers in all supported formats are not large
  155. * enough to create an overflow).
  156. * NOTE: strings passed to us are NOT TERMINATED.
  157. * Return:
  158. * unsigned long value
  159. */
  160. u_long
  161. asc_ul (char *str, int len, int base)
  162. {
  163. char *stop;
  164. u_long tval = 0;
  165. stop = str + len;
  166. /*
  167. * skip over leading blanks and zeros
  168. */
  169. while ((str < stop) && ((*str == ' ') || (*str == '0')))
  170. ++str;
  171. /*
  172. * for each valid digit, shift running value (tval) over to next digit
  173. * and add next digit
  174. */
  175. if (base == HEX)
  176. {
  177. while (str < stop)
  178. {
  179. if ((*str >= '0') && (*str <= '9'))
  180. tval = (tval << 4) + (*str++ - '0');
  181. else if ((*str >= 'A') && (*str <= 'F'))
  182. tval = (tval << 4) + 10 + (*str++ - 'A');
  183. else if ((*str >= 'a') && (*str <= 'f'))
  184. tval = (tval << 4) + 10 + (*str++ - 'a');
  185. else
  186. break;
  187. }
  188. }
  189. else
  190. {
  191. while ((str < stop) && (*str >= '0') && (*str <= '7'))
  192. tval = (tval << 3) + (*str++ - '0');
  193. }
  194. return (tval);
  195. }
  196. /*
  197. * tar_trail()
  198. * Called to determine if a header block is a valid trailer. We are passed
  199. * the block, the in_sync flag (which tells us we are in resync mode;
  200. * looking for a valid header), and cnt (which starts at zero) which is
  201. * used to count the number of empty blocks we have seen so far.
  202. * Return:
  203. * 0 if a valid trailer, -1 if not a valid trailer, or 1 if the block
  204. * could never contain a header.
  205. */
  206. int
  207. tar_trail (char *buf,
  208. int in_resync, int *cnt)
  209. {
  210. register int i;
  211. /*
  212. * look for all zero, trailer is two consecutive blocks of zero
  213. */
  214. for (i = 0; i < BLKMULT; ++i)
  215. {
  216. if (buf[i] != '\0')
  217. break;
  218. }
  219. /*
  220. * if not all zero it is not a trailer, but MIGHT be a header.
  221. */
  222. if (i != BLKMULT)
  223. return (-1);
  224. /*
  225. * When given a zero block, we must be careful!
  226. * If we are not in resync mode, check for the trailer. Have to watch
  227. * out that we do not mis-identify file data as the trailer, so we do
  228. * NOT try to id a trailer during resync mode. During resync mode we
  229. * might as well throw this block out since a valid header can NEVER be
  230. * a block of all 0 (we must have a valid file name).
  231. */
  232. if (!in_resync && (++*cnt >= NULLCNT))
  233. return (0);
  234. return (1);
  235. }
  236. /*
  237. * tar_chksm()
  238. * calculate the checksum for a tar block counting the checksum field as
  239. * all blanks (BLNKSUM is that value pre-calculated, the sum of 8 blanks).
  240. * NOTE: we use len to short circuit summing 0's on write since we ALWAYS
  241. * pad headers with 0.
  242. * Return:
  243. * unsigned long checksum
  244. */
  245. static unsigned long
  246. tar_chksm (char *blk, int len)
  247. {
  248. char *stop;
  249. char *pt;
  250. unsigned int chksm = BLNKSUM; /* initial value is checksum field sum */
  251. /*
  252. * add the part of the block before the checksum field
  253. */
  254. pt = blk;
  255. stop = blk + CHK_OFFSET;
  256. while (pt < stop)
  257. chksm += (*pt++ & 0xff);
  258. /*
  259. * move past the checksum field and keep going, spec counts the
  260. * checksum field as the sum of 8 blanks (which is pre-computed as
  261. * BLNKSUM).
  262. * ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding
  263. * starts, no point in summing zero's)
  264. */
  265. pt += CHK_LEN;
  266. stop = blk + len;
  267. while (pt < stop)
  268. chksm += (*pt++ & 0xff);
  269. return chksm;
  270. }
  271. /*
  272. * ustar_id()
  273. * determine if a block given to us is a valid ustar header. We have to
  274. * be on the lookout for those pesky blocks of all zero's
  275. * Return:
  276. * 0 if a ustar header, -1 otherwise
  277. */
  278. int
  279. ustar_id (char *blk, size_t size)
  280. {
  281. HD_USTAR *hd;
  282. if (size < BLKMULT)
  283. return (-1);
  284. hd = (HD_USTAR *) blk;
  285. /*
  286. * check for block of zero's first, a simple and fast test then check
  287. * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
  288. * programs are fouled up and create archives missing the \0. Last we
  289. * check the checksum. If ok we have to assume it is a valid header.
  290. */
  291. if (hd->name[0] == '\0')
  292. return (-1);
  293. if (strncmp (hd->magic, TMAGIC, TMAGLEN - 1) != 0)
  294. return (-1);
  295. if (asc_ul (hd->chksum, sizeof (hd->chksum), OCT) !=
  296. tar_chksm (blk, BLKMULT))
  297. return (-1);
  298. return (0);
  299. }
  300. /*
  301. * Routines for reading tar files
  302. */
  303. /*
  304. struct file_header {
  305. unsigned int namelen;
  306. unsigned int mode;
  307. unsigned long long filelen;
  308. unsigned int atime;
  309. unsigned int atime_nsec;
  310. unsigned int mtime;
  311. unsigned int mtime_nsec;
  312. };
  313. */
  314. /*
  315. 0000cc00 76 6d 2d 74 65 6d 70 6c 61 74 65 73 2f 61 72 63 |vm-templates/arc|
  316. 0000cc10 68 6c 69 6e 75 78 2d 78 36 34 2d 73 70 65 63 2d |hlinux-x64-spec-|
  317. 0000cc20 32 30 31 33 2e 30 30 30 00 00 00 00 00 00 00 00 |2013.000........|
  318. 0000cc30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
  319. *
  320. 0000cc60 00 00 00 00 30 30 30 30 36 36 34 00 30 30 30 31 |....0000664.0001|
  321. 0000cc70 37 35 30 00 30 30 30 31 37 35 31 00 30 37 35 30 |750.0001751.0750|
  322. 0000cc80 32 32 30 30 30 34 30 00 31 32 32 32 31 35 32 37 |2200040.12221527|
  323. 0000cc90 31 34 34 00 30 31 37 33 30 33 00 20 30 00 00 00 |144.017303. 0...|
  324. 0000cca0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
  325. *
  326. 0000cd00 00 75 73 74 61 72 00 30 30 6f 6d 65 00 00 00 00 |.ustar.00ome....|
  327. 0000cd10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
  328. 0000cd20 00 00 00 00 00 00 00 00 00 6f 6d 65 00 00 00 00 |.........ome....|
  329. 0000cd30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
  330. 0000cd40 00 00 00 00 00 00 00 00 00 30 30 30 30 30 30 30 |.........0000000|
  331. 0000cd50 00 30 30 30 30 30 30 30 00 00 00 00 00 00 00 00 |.0000000........|
  332. 0000cd60 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
  333. *
  334. // Source: http://www.mkssoftware.com/docs/man4/pax.4.asp
  335. struct file_header { // PAX header is similar as file_header and can be completely ignored
  336. unsigned char[100] name;
  337. unsigned char[8] mode;
  338. unsigned char[8] uid; // unused
  339. unsigned char[8] gid; // unused
  340. unsigned char[12] size; // 0 if file is a link
  341. unsigned char[12] mtime;
  342. unsigned char[8] chksum;
  343. unsigned char[1] typeflag;
  344. unsigned char[100] linkname;
  345. unsigned char[6] magic; //ustar
  346. unsigned char[2] version; // 00
  347. unsigned char[32] uname; // unused
  348. unsigned char[32] gname; // unused
  349. unsigned char[8] devmajor; // unused ?
  350. unsigned char[8] devminor; // unused ?
  351. unsigned char[155] prefix; // only used for files > 100 characters. could be unused ?
  352. };
  353. enum {
  354. TYPE_REGULAR, //0
  355. TYPE_ARCHIVE_LINK, //1
  356. TYPE_SYMLINK, //2
  357. TYPE_CHARACTER_DEVICE, //3
  358. TYPE_BLOCK_DEVICE, //4
  359. TYPE_DIRECTORY, //5
  360. TYPE_FIFO, //6
  361. // Other types:
  362. TYPE_EXTENDED_USAGE, //xxxxx
  363. // A-Z are available for custom usage
  364. };
  365. // Extended attribute:
  366. // length keyword=value
  367. // atime, charset, comment, gname, linkpath, mtime, path, size, uname
  368. */
  369. enum {
  370. NEED_NOTHING,
  371. NEED_SKIP,
  372. NEED_READ,
  373. NEED_SYNC_TRAIL
  374. };
  375. /*
  376. * ustar_rd()
  377. * extract the values out of block already determined to be a ustar header.
  378. * store the values in the ARCHD parameter.
  379. * Return:
  380. * 0
  381. */
  382. int
  383. ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * sb)
  384. {
  385. register HD_USTAR *hd;
  386. register char *dest;
  387. register int cnt = 0;
  388. int ret;
  389. /* DISABLED: unused
  390. dev_t devmajor;
  391. dev_t devminor;
  392. */
  393. /*
  394. * we only get proper sized buffers
  395. */
  396. fprintf(stderr,"Checking if valid header\n");
  397. if (ustar_id (buf, BLKMULT) < 0)
  398. return (-1);
  399. fprintf(stderr,"Valid header!\n");
  400. /* DISABLED: Internal to PAX
  401. arcn->org_name = arcn->name;
  402. arcn->sb.st_nlink = 1;
  403. arcn->pat = NULL;
  404. arcn->nlen = 0;
  405. */
  406. untrusted_hdr->namelen = 0;
  407. hd = (HD_USTAR *) buf;
  408. /*
  409. * see if the filename is split into two parts. if, so joint the parts.
  410. * we copy the prefix first and add a / between the prefix and name.
  411. */
  412. dest = untrusted_namebuf;
  413. if (*(hd->prefix) != '\0')
  414. {
  415. cnt = strlen(strncpy (dest, hd->prefix,
  416. MIN(sizeof (untrusted_namebuf) - 1,TPFSZ+1)));
  417. dest += cnt;
  418. *dest++ = '/';
  419. cnt++;
  420. }
  421. if (gnu_hack_string)
  422. {
  423. untrusted_hdr->namelen = cnt + strlen(strncpy (dest, gnu_hack_string,
  424. MIN(TNMSZ+1, sizeof (untrusted_namebuf) - cnt)));
  425. free(gnu_hack_string);
  426. gnu_hack_string = NULL;
  427. } else
  428. untrusted_hdr->namelen = cnt + strlen(strncpy (dest, hd->name,
  429. MIN(TNMSZ+1, sizeof (untrusted_namebuf) - cnt)));
  430. fprintf(stderr,"Retrieved name len: %d\n",untrusted_hdr->namelen);
  431. fprintf(stderr,"Retrieved name: %s\n",untrusted_namebuf);
  432. /*
  433. * follow the spec to the letter. we should only have mode bits, strip
  434. * off all other crud we may be passed.
  435. */
  436. sb->st_mode = (mode_t) (asc_ul (hd->mode, sizeof (hd->mode), OCT) &
  437. 0xfff);
  438. untrusted_hdr->mode = sb->st_mode;
  439. #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
  440. sb->st_size = (off_t) asc_uqd (hd->size, sizeof (hd->size), OCT);
  441. #else
  442. sb->st_size = (off_t) asc_ul (hd->size, sizeof (hd->size), OCT);
  443. #endif
  444. untrusted_hdr->filelen = sb->st_size;
  445. untrusted_hdr->atime = (time_t) asc_ul (hd->mtime, sizeof (hd->mtime), OCT);
  446. untrusted_hdr->mtime = untrusted_hdr->atime;
  447. untrusted_hdr->atime_nsec = untrusted_hdr->mtime_nsec = 0;
  448. sb->st_mtime = (time_t) asc_ul (hd->mtime, sizeof (hd->mtime), OCT);
  449. sb->st_ctime = sb->st_atime = sb->st_mtime;
  450. /*
  451. * If we can find the ascii names for gname and uname in the password
  452. * and group files we will use the uid's and gid they bind. Otherwise
  453. * we use the uid and gid values stored in the header. (This is what
  454. * the posix spec wants).
  455. */
  456. /* DISABLED: unused
  457. hd->gname[sizeof (hd->gname) - 1] = '\0';
  458. if (gid_name (hd->gname, &(arcn->sb.st_gid)) < 0)
  459. arcn->sb.st_gid = (gid_t) asc_ul (hd->gid, sizeof (hd->gid), OCT);
  460. hd->uname[sizeof (hd->uname) - 1] = '\0';
  461. if (uid_name (hd->uname, &(arcn->sb.st_uid)) < 0)
  462. arcn->sb.st_uid = (uid_t) asc_ul (hd->uid, sizeof (hd->uid), OCT);
  463. */
  464. /*
  465. * set the defaults, these may be changed depending on the file type
  466. */
  467. /* Disabled: pax specific
  468. arcn->ln_name[0] = '\0';
  469. arcn->ln_nlen = 0;
  470. arcn->pad = 0;
  471. arcn->skip = 0;
  472. arcn->sb.st_rdev = (dev_t) 0;
  473. */
  474. /*
  475. * set the mode and PAX type according to the typeflag in the header
  476. */
  477. switch (hd->typeflag)
  478. {
  479. case FIFOTYPE:
  480. fprintf(stderr,"File is FIFOTYPE\n");
  481. /* DISABLED: unused
  482. arcn->type = PAX_FIF;
  483. arcn->sb.st_mode |= S_IFIFO;
  484. */
  485. break;
  486. case DIRTYPE:
  487. fprintf(stderr,"File is DIRTYPE\n");
  488. /* DISABLED: unused
  489. arcn->type = PAX_DIR;
  490. arcn->sb.st_mode |= S_IFDIR;
  491. arcn->sb.st_nlink = 2;
  492. */
  493. /*
  494. * Some programs that create ustar archives append a '/'
  495. * to the pathname for directories. This clearly violates
  496. * ustar specs, but we will silently strip it off anyway.
  497. */
  498. /*
  499. if (arcn->name[arcn->nlen - 1] == '/')
  500. arcn->name[--arcn->nlen] = '\0';
  501. */
  502. break;
  503. case BLKTYPE:
  504. fprintf(stderr,"File is BLKTYPE\n");
  505. break;
  506. case CHRTYPE:
  507. fprintf(stderr,"File is CHRTYPE\n");
  508. /*
  509. * this type requires the rdev field to be set.
  510. */
  511. if (hd->typeflag == BLKTYPE)
  512. {
  513. /*
  514. arcn->type = PAX_BLK;
  515. arcn->sb.st_mode |= S_IFBLK;
  516. */
  517. }
  518. else
  519. {
  520. /*
  521. arcn->type = PAX_CHR;
  522. arcn->sb.st_mode |= S_IFCHR;
  523. */
  524. }
  525. /* DISABLED: unused
  526. devmajor = (dev_t) asc_ul (hd->devmajor, sizeof (hd->devmajor), OCT);
  527. devminor = (dev_t) asc_ul (hd->devminor, sizeof (hd->devminor), OCT);
  528. */
  529. // arcn->sb.st_rdev = TODEV (devmajor, devminor);
  530. break;
  531. case SYMTYPE:
  532. fprintf(stderr,"File is SYMTYPE\n");
  533. break;
  534. case LNKTYPE:
  535. fprintf(stderr,"File is LNKTYPE\n");
  536. if (hd->typeflag == SYMTYPE)
  537. {
  538. // arcn->type = PAX_SLK;
  539. // arcn->sb.st_mode |= S_IFLNK;
  540. }
  541. else
  542. {
  543. // arcn->type = PAX_HLK;
  544. /*
  545. * so printing looks better
  546. */
  547. // arcn->sb.st_mode |= S_IFREG;
  548. // arcn->sb.st_nlink = 2;
  549. }
  550. /*
  551. * copy the link name
  552. */
  553. // arcn->ln_nlen = strlcpy (arcn->ln_name, hd->linkname,
  554. // MIN(TNMSZ+1,sizeof (arcn->ln_name)));
  555. break;
  556. case LONGLINKTYPE:
  557. fprintf(stderr,"File is LONGLINKTYPE\n");
  558. break;
  559. case LONGNAMETYPE:
  560. fprintf(stderr,"File is LONGNAMETYPE\n");
  561. /*
  562. * GNU long link/file; we tag these here and let the
  563. * pax internals deal with it -- too ugly otherwise.
  564. */
  565. // arcn->type =
  566. // hd->typeflag == LONGLINKTYPE ? PAX_GLL : PAX_GLF;
  567. // arcn->pad = TAR_PAD(arcn->sb.st_size);
  568. // arcn->skip = arcn->sb.st_size;
  569. // arcn->ln_name[0] = '\0';
  570. // arcn->ln_nlen = 0;
  571. break;
  572. case CONTTYPE:
  573. fprintf(stderr,"File is CONTTYPE\n");
  574. break;
  575. case AREGTYPE:
  576. fprintf(stderr,"File is AREGTYPE\n");
  577. break;
  578. case REGTYPE:
  579. fprintf(stderr,"File is REGTYPE of size %d\n",sb->st_size);
  580. write_headers(untrusted_hdr, untrusted_namebuf);
  581. ret = copy_file(1, fd, untrusted_hdr->filelen, &crc32_sum);
  582. if (ret != COPY_FILE_OK) {
  583. if (ret != COPY_FILE_WRITE_ERROR)
  584. gui_fatal("Copying file %s: %s", untrusted_namebuf,
  585. copy_file_status_to_str(ret));
  586. else {
  587. set_block(0);
  588. wait_for_result();
  589. exit(1);
  590. }
  591. }
  592. // Extract extra padding
  593. fprintf(stderr,"Need to remove pad:%d %d\n",sb->st_size,BLKMULT-(sb->st_size%BLKMULT));
  594. ret = read(fd, buf, BLKMULT-(sb->st_size%BLKMULT));
  595. fprintf(stderr,"Removed %d bytes of padding\n",ret);
  596. // Resync trailing headers
  597. return NEED_SYNC_TRAIL;
  598. break;
  599. case EXTHEADERTYPE:
  600. fprintf(stderr,"Extended HEADER encountered\n");
  601. return NEED_SKIP;
  602. break;
  603. default:
  604. fprintf(stderr,"Default type detected:%c\n",hd->typeflag);
  605. return NEED_SKIP;
  606. /*
  607. * these types have file data that follows. Set the skip and
  608. * pad fields.
  609. */
  610. // arcn->type = PAX_REG;
  611. // arcn->pad = TAR_PAD (arcn->sb.st_size);
  612. // arcn->skip = arcn->sb.st_size;
  613. // arcn->sb.st_mode |= S_IFREG;
  614. break;
  615. }
  616. return NEED_SKIP;
  617. }
  618. int tar_file_processor(int fd)
  619. {
  620. int ret;
  621. int i;
  622. int current;
  623. struct file_header hdr;
  624. struct stat sb; /* stat buffer see stat(2) */
  625. char buf[BLKMULT+1];
  626. size_t size;
  627. i=0;
  628. current = NEED_READ;
  629. size_t to_skip = 0;
  630. int sync_count = 0;
  631. while (size = read(fd, &buf, BLKMULT)) {
  632. fprintf(stderr,"Read %d bytes\n",size);
  633. ret = 0;
  634. if (current==NEED_SYNC_TRAIL) {
  635. ret = tar_trail (buf, 1, &sync_count);
  636. fprintf(stderr,"Synchronizing trail: %d %d\n",ret,sync_count);
  637. if (ret != 1) {
  638. current = NEED_READ;
  639. }
  640. }
  641. if (current==NEED_READ) {
  642. current = ustar_rd(fd, &hdr, &buf, &sb);
  643. fprintf(stderr,"Return %d\n",ret);
  644. }
  645. if (current==NEED_SKIP) {
  646. fprintf(stderr,"Need to skip %d bytes\n",sb.st_size);
  647. to_skip = sb.st_size;
  648. while (to_skip > 0) {
  649. to_skip -= read(fd, &buf, MIN(to_skip,BLKMULT));
  650. }
  651. // Extract extra padding
  652. fprintf(stderr,"Need to remove pad:%d %d %d\n",to_skip,sb.st_size,BLKMULT-(sb.st_size%BLKMULT));
  653. ret = read(fd, &buf, BLKMULT-(sb.st_size%BLKMULT));
  654. fprintf(stderr,"Removed %d bytes of padding\n",ret);
  655. current = NEED_READ;
  656. }
  657. i++;
  658. //if (i >= 10)
  659. // exit(0);
  660. }
  661. }
  662. int main(int argc, char **argv)
  663. {
  664. int i;
  665. char *entry;
  666. char *cwd;
  667. char *sep;
  668. int fd;
  669. //signal(SIGPIPE, SIG_IGN);
  670. // this will allow checking for possible feedback packet in the middle of transfer
  671. //set_nonblock(0);
  672. crc32_sum = 0;
  673. cwd = getcwd(NULL, 0);
  674. for (i = 1; i < argc; i++) {
  675. if (strcmp(argv[i], "--ignore-symlinks")==0) {
  676. ignore_symlinks = 1;
  677. continue;
  678. } else {
  679. // Parse tar file
  680. entry = argv[i];
  681. fprintf(stderr,"Parsing file %s\n",entry);
  682. fd = open(entry, O_RDONLY);
  683. if (fd < 0)
  684. fprintf(stderr,"Error opening file %s\n",entry);
  685. tar_file_processor(fd);
  686. }
  687. }
  688. if (i <= 1) {
  689. // No argument specified. Use STDIN
  690. fprintf(stderr,"Using STDIN");
  691. tar_file_processor(fileno(stdin));
  692. }
  693. return 0;
  694. }