tar.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /* Virtual File System: GNU Tar file system.
  2. Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  3. 2006, 2007 Free Software Foundation, Inc.
  4. Written by: 1995 Jakub Jelinek
  5. Rewritten by: 1998 Pavel Machek
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License
  8. as published by the Free Software Foundation; either version 2 of
  9. the License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  17. /* Namespace: init_tarfs */
  18. #include <config.h>
  19. #include <sys/types.h>
  20. #include <errno.h>
  21. #include <ctype.h>
  22. #ifdef hpux
  23. /* major() and minor() macros (among other things) defined here for hpux */
  24. #include <sys/mknod.h>
  25. #endif
  26. #include "../src/global.h"
  27. #include "../src/tty.h" /* enable/disable interrupt key */
  28. #include "../src/wtools.h" /* message() */
  29. #include "../src/main.h" /* print_vfs_message */
  30. #include "utilvfs.h"
  31. #include "gc.h" /* vfs_rmstamp */
  32. #include "xdirentry.h"
  33. static struct vfs_class vfs_tarfs_ops;
  34. enum {
  35. TAR_UNKNOWN = 0,
  36. TAR_V7,
  37. TAR_USTAR,
  38. TAR_POSIX,
  39. TAR_GNU
  40. };
  41. /*
  42. * Header block on tape.
  43. *
  44. * I'm going to use traditional DP naming conventions here.
  45. * A "block" is a big chunk of stuff that we do I/O on.
  46. * A "record" is a piece of info that we care about.
  47. * Typically many "record"s fit into a "block".
  48. */
  49. #define RECORDSIZE 512
  50. #define NAMSIZ 100
  51. #define PREFIX_SIZE 155
  52. #define TUNMLEN 32
  53. #define TGNMLEN 32
  54. #define SPARSE_EXT_HDR 21
  55. #define SPARSE_IN_HDR 4
  56. struct sparse {
  57. char offset[12];
  58. char numbytes[12];
  59. };
  60. struct sp_array {
  61. int offset;
  62. int numbytes;
  63. };
  64. union record {
  65. char charptr[RECORDSIZE];
  66. struct header {
  67. char arch_name[NAMSIZ];
  68. char mode[8];
  69. char uid[8];
  70. char gid[8];
  71. char size[12];
  72. char mtime[12];
  73. char chksum[8];
  74. char linkflag;
  75. char arch_linkname[NAMSIZ];
  76. char magic[8];
  77. char uname[TUNMLEN];
  78. char gname[TGNMLEN];
  79. char devmajor[8];
  80. char devminor[8];
  81. /* The following bytes of the tar header record were originally unused.
  82. Archives following the ustar specification use almost all of those
  83. bytes to support pathnames of 256 characters in length.
  84. GNU tar archives use the "unused" space to support incremental
  85. archives and sparse files. */
  86. union unused {
  87. char prefix[PREFIX_SIZE];
  88. /* GNU extensions to the ustar (POSIX.1-1988) archive format. */
  89. struct oldgnu {
  90. char atime[12];
  91. char ctime[12];
  92. char offset[12];
  93. char longnames[4];
  94. char pad;
  95. struct sparse sp[SPARSE_IN_HDR];
  96. char isextended;
  97. char realsize[12]; /* true size of the sparse file */
  98. } oldgnu;
  99. } unused;
  100. } header;
  101. struct extended_header {
  102. struct sparse sp[21];
  103. char isextended;
  104. } ext_hdr;
  105. };
  106. /* The checksum field is filled with this while the checksum is computed. */
  107. #define CHKBLANKS " " /* 8 blanks, no null */
  108. /* The magic field is filled with this if uname and gname are valid. */
  109. #define TMAGIC "ustar" /* ustar and a null */
  110. #define OLDGNU_MAGIC "ustar " /* 7 chars and a null */
  111. /* The linkflag defines the type of file */
  112. #define LF_OLDNORMAL '\0' /* Normal disk file, Unix compat */
  113. #define LF_NORMAL '0' /* Normal disk file */
  114. #define LF_LINK '1' /* Link to previously dumped file */
  115. #define LF_SYMLINK '2' /* Symbolic link */
  116. #define LF_CHR '3' /* Character special file */
  117. #define LF_BLK '4' /* Block special file */
  118. #define LF_DIR '5' /* Directory */
  119. #define LF_FIFO '6' /* FIFO special file */
  120. #define LF_CONTIG '7' /* Contiguous file */
  121. #define LF_EXTHDR 'x' /* pax Extended Header */
  122. #define LF_GLOBAL_EXTHDR 'g' /* pax Global Extended Header */
  123. /* Further link types may be defined later. */
  124. /* Note that the standards committee allows only capital A through
  125. capital Z for user-defined expansion. This means that defining something
  126. as, say '8' is a *bad* idea. */
  127. #define LF_DUMPDIR 'D' /* This is a dir entry that contains
  128. the names of files that were in
  129. the dir at the time the dump
  130. was made */
  131. #define LF_LONGLINK 'K' /* Identifies the NEXT file on the tape
  132. as having a long linkname */
  133. #define LF_LONGNAME 'L' /* Identifies the NEXT file on the tape
  134. as having a long name. */
  135. #define LF_MULTIVOL 'M' /* This is the continuation
  136. of a file that began on another
  137. volume */
  138. #define LF_NAMES 'N' /* For storing filenames that didn't
  139. fit in 100 characters */
  140. #define LF_SPARSE 'S' /* This is for sparse files */
  141. #define LF_VOLHDR 'V' /* This file is a tape/volume header */
  142. /* Ignore it on extraction */
  143. /*
  144. * Exit codes from the "tar" program
  145. */
  146. #define EX_SUCCESS 0 /* success! */
  147. #define EX_ARGSBAD 1 /* invalid args */
  148. #define EX_BADFILE 2 /* invalid filename */
  149. #define EX_BADARCH 3 /* bad archive */
  150. #define EX_SYSTEM 4 /* system gave unexpected error */
  151. #define EX_BADVOL 5 /* Special error code means
  152. Tape volume doesn't match the one
  153. specified on the command line */
  154. #define isodigit(c) ( ((c) >= '0') && ((c) <= '7') )
  155. /*
  156. * Quick and dirty octal conversion.
  157. *
  158. * Result is -1 if the field is invalid (all blank, or nonoctal).
  159. */
  160. static long tar_from_oct (int digs, char *where)
  161. {
  162. register long value;
  163. while (isspace ((unsigned char) *where)) { /* Skip spaces */
  164. where++;
  165. if (--digs <= 0)
  166. return -1; /* All blank field */
  167. }
  168. value = 0;
  169. while (digs > 0 && isodigit (*where)) { /* Scan till nonoctal */
  170. value = (value << 3) | (*where++ - '0');
  171. --digs;
  172. }
  173. if (digs > 0 && *where && !isspace ((unsigned char) *where))
  174. return -1; /* Ended on non-space/nul */
  175. return value;
  176. }
  177. static void tar_free_archive (struct vfs_class *me, struct vfs_s_super *archive)
  178. {
  179. (void) me;
  180. if (archive->u.arch.fd != -1)
  181. mc_close(archive->u.arch.fd);
  182. }
  183. /* As we open one archive at a time, it is safe to have this static */
  184. static int current_tar_position = 0;
  185. /* Returns fd of the open tar file */
  186. static int
  187. tar_open_archive_int (struct vfs_class *me, const char *name,
  188. struct vfs_s_super *archive)
  189. {
  190. int result, type;
  191. mode_t mode;
  192. struct vfs_s_inode *root;
  193. result = mc_open (name, O_RDONLY);
  194. if (result == -1) {
  195. message (1, MSG_ERROR, _("Cannot open tar archive\n%s"), name);
  196. ERRNOR (ENOENT, -1);
  197. }
  198. archive->name = g_strdup (name);
  199. mc_stat (name, &(archive->u.arch.st));
  200. archive->u.arch.fd = -1;
  201. archive->u.arch.type = TAR_UNKNOWN;
  202. /* Find out the method to handle this tar file */
  203. type = get_compression_type (result);
  204. mc_lseek (result, 0, SEEK_SET);
  205. if (type != COMPRESSION_NONE) {
  206. char *s;
  207. mc_close (result);
  208. s = g_strconcat (archive->name, decompress_extension (type), (char *) NULL);
  209. result = mc_open (s, O_RDONLY);
  210. if (result == -1)
  211. message (1, MSG_ERROR, _("Cannot open tar archive\n%s"), s);
  212. g_free (s);
  213. if (result == -1)
  214. ERRNOR (ENOENT, -1);
  215. }
  216. archive->u.arch.fd = result;
  217. mode = archive->u.arch.st.st_mode & 07777;
  218. if (mode & 0400)
  219. mode |= 0100;
  220. if (mode & 0040)
  221. mode |= 0010;
  222. if (mode & 0004)
  223. mode |= 0001;
  224. mode |= S_IFDIR;
  225. root = vfs_s_new_inode (me, archive, &archive->u.arch.st);
  226. root->st.st_mode = mode;
  227. root->data_offset = -1;
  228. root->st.st_nlink++;
  229. root->st.st_dev = MEDATA->rdev++;
  230. archive->root = root;
  231. return result;
  232. }
  233. static union record rec_buf;
  234. static union record *
  235. tar_get_next_record (struct vfs_s_super *archive, int tard)
  236. {
  237. int n;
  238. (void) archive;
  239. n = mc_read (tard, rec_buf.charptr, RECORDSIZE);
  240. if (n != RECORDSIZE)
  241. return NULL; /* An error has occurred */
  242. current_tar_position += RECORDSIZE;
  243. return &rec_buf;
  244. }
  245. static void tar_skip_n_records (struct vfs_s_super *archive, int tard, int n)
  246. {
  247. (void) archive;
  248. mc_lseek (tard, n * RECORDSIZE, SEEK_CUR);
  249. current_tar_position += n * RECORDSIZE;
  250. }
  251. static void
  252. tar_fill_stat (struct vfs_s_super *archive, struct stat *st, union record *header,
  253. size_t h_size)
  254. {
  255. st->st_mode = tar_from_oct (8, header->header.mode);
  256. /* Adjust st->st_mode because there are tar-files with
  257. * linkflag==LF_SYMLINK and S_ISLNK(mod)==0. I don't
  258. * know about the other modes but I think I cause no new
  259. * problem when I adjust them, too. -- Norbert.
  260. */
  261. if (header->header.linkflag == LF_DIR) {
  262. st->st_mode |= S_IFDIR;
  263. } else if (header->header.linkflag == LF_SYMLINK) {
  264. st->st_mode |= S_IFLNK;
  265. } else if (header->header.linkflag == LF_CHR) {
  266. st->st_mode |= S_IFCHR;
  267. } else if (header->header.linkflag == LF_BLK) {
  268. st->st_mode |= S_IFBLK;
  269. } else if (header->header.linkflag == LF_FIFO) {
  270. st->st_mode |= S_IFIFO;
  271. } else
  272. st->st_mode |= S_IFREG;
  273. st->st_rdev = 0;
  274. switch (archive->u.arch.type) {
  275. case TAR_USTAR:
  276. case TAR_POSIX:
  277. case TAR_GNU:
  278. st->st_uid =
  279. *header->header.uname ? vfs_finduid (header->header.
  280. uname) : tar_from_oct (8,
  281. header->
  282. header.
  283. uid);
  284. st->st_gid =
  285. *header->header.gname ? vfs_findgid (header->header.
  286. gname) : tar_from_oct (8,
  287. header->
  288. header.
  289. gid);
  290. switch (header->header.linkflag) {
  291. case LF_BLK:
  292. case LF_CHR:
  293. st->st_rdev =
  294. (tar_from_oct (8, header->header.devmajor) << 8) |
  295. tar_from_oct (8, header->header.devminor);
  296. }
  297. default:
  298. st->st_uid = tar_from_oct (8, header->header.uid);
  299. st->st_gid = tar_from_oct (8, header->header.gid);
  300. }
  301. st->st_size = h_size;
  302. st->st_mtime = tar_from_oct (1 + 12, header->header.mtime);
  303. st->st_atime = 0;
  304. st->st_ctime = 0;
  305. if (archive->u.arch.type == TAR_GNU) {
  306. st->st_atime = tar_from_oct (1 + 12,
  307. header->header.unused.oldgnu.atime);
  308. st->st_ctime = tar_from_oct (1 + 12,
  309. header->header.unused.oldgnu.ctime);
  310. }
  311. }
  312. typedef enum {
  313. STATUS_BADCHECKSUM,
  314. STATUS_SUCCESS,
  315. STATUS_EOFMARK,
  316. STATUS_EOF
  317. } ReadStatus;
  318. /*
  319. * Return 1 for success, 0 if the checksum is bad, EOF on eof,
  320. * 2 for a record full of zeros (EOF marker).
  321. *
  322. */
  323. static ReadStatus
  324. tar_read_header (struct vfs_class *me, struct vfs_s_super *archive,
  325. int tard, size_t *h_size)
  326. {
  327. register int i;
  328. register long sum, signed_sum, recsum;
  329. register char *p;
  330. register union record *header;
  331. static char *next_long_name = NULL, *next_long_link = NULL;
  332. recurse:
  333. header = tar_get_next_record (archive, tard);
  334. if (NULL == header)
  335. return STATUS_EOF;
  336. recsum = tar_from_oct (8, header->header.chksum);
  337. sum = 0;
  338. signed_sum = 0;
  339. p = header->charptr;
  340. for (i = sizeof (*header); --i >= 0;) {
  341. /*
  342. * We can't use unsigned char here because of old compilers,
  343. * e.g. V7.
  344. */
  345. signed_sum += *p;
  346. sum += 0xFF & *p++;
  347. }
  348. /* Adjust checksum to count the "chksum" field as blanks. */
  349. for (i = sizeof (header->header.chksum); --i >= 0;) {
  350. sum -= 0xFF & header->header.chksum[i];
  351. signed_sum -= (char) header->header.chksum[i];
  352. }
  353. sum += ' ' * sizeof header->header.chksum;
  354. signed_sum += ' ' * sizeof header->header.chksum;
  355. /*
  356. * This is a zeroed record...whole record is 0's except
  357. * for the 8 blanks we faked for the checksum field.
  358. */
  359. if (sum == 8 * ' ')
  360. return STATUS_EOFMARK;
  361. if (sum != recsum && signed_sum != recsum)
  362. return STATUS_BADCHECKSUM;
  363. /*
  364. * Try to determine the archive format.
  365. */
  366. if (archive->u.arch.type == TAR_UNKNOWN) {
  367. if (!strcmp (header->header.magic, TMAGIC)) {
  368. if (header->header.linkflag == LF_GLOBAL_EXTHDR)
  369. archive->u.arch.type = TAR_POSIX;
  370. else
  371. archive->u.arch.type = TAR_USTAR;
  372. } else if (!strcmp (header->header.magic, OLDGNU_MAGIC)) {
  373. archive->u.arch.type = TAR_GNU;
  374. }
  375. }
  376. /*
  377. * linkflag on BSDI tar (pax) always '\000'
  378. */
  379. if (header->header.linkflag == '\000') {
  380. if (header->header.arch_name[NAMSIZ - 1] != '\0')
  381. i = NAMSIZ;
  382. else
  383. i = strlen (header->header.arch_name);
  384. if (i && header->header.arch_name[i - 1] == '/')
  385. header->header.linkflag = LF_DIR;
  386. }
  387. /*
  388. * Good record. Decode file size and return.
  389. */
  390. if (header->header.linkflag == LF_LINK
  391. || header->header.linkflag == LF_DIR)
  392. *h_size = 0; /* Links 0 size on tape */
  393. else
  394. *h_size = tar_from_oct (1 + 12, header->header.size);
  395. /*
  396. * Skip over directory snapshot info records that
  397. * are stored in incremental tar archives.
  398. */
  399. if (header->header.linkflag == LF_DUMPDIR) {
  400. if (archive->u.arch.type == TAR_UNKNOWN)
  401. archive->u.arch.type = TAR_GNU;
  402. return STATUS_SUCCESS;
  403. }
  404. /*
  405. * Skip over pax extended header and global extended
  406. * header records.
  407. */
  408. if (header->header.linkflag == LF_EXTHDR ||
  409. header->header.linkflag == LF_GLOBAL_EXTHDR) {
  410. if (archive->u.arch.type == TAR_UNKNOWN)
  411. archive->u.arch.type = TAR_POSIX;
  412. return STATUS_SUCCESS;
  413. }
  414. if (header->header.linkflag == LF_LONGNAME
  415. || header->header.linkflag == LF_LONGLINK) {
  416. char **longp;
  417. char *bp, *data;
  418. int size, written;
  419. if (archive->u.arch.type == TAR_UNKNOWN)
  420. archive->u.arch.type = TAR_GNU;
  421. if (*h_size > MC_MAXPATHLEN) {
  422. message (1, MSG_ERROR, _("Inconsistent tar archive"));
  423. return STATUS_BADCHECKSUM;
  424. }
  425. longp = ((header->header.linkflag == LF_LONGNAME)
  426. ? &next_long_name : &next_long_link);
  427. g_free (*longp);
  428. bp = *longp = g_malloc (*h_size + 1);
  429. for (size = *h_size; size > 0; size -= written) {
  430. data = tar_get_next_record (archive, tard)->charptr;
  431. if (data == NULL) {
  432. g_free (*longp);
  433. *longp = NULL;
  434. message (1, MSG_ERROR,
  435. _("Unexpected EOF on archive file"));
  436. return STATUS_BADCHECKSUM;
  437. }
  438. written = RECORDSIZE;
  439. if (written > size)
  440. written = size;
  441. memcpy (bp, data, written);
  442. bp += written;
  443. }
  444. if (bp - *longp == MC_MAXPATHLEN && bp[-1] != '\0') {
  445. g_free (*longp);
  446. *longp = NULL;
  447. message (1, MSG_ERROR, _("Inconsistent tar archive"));
  448. return STATUS_BADCHECKSUM;
  449. }
  450. *bp = 0;
  451. goto recurse;
  452. } else {
  453. struct stat st;
  454. struct vfs_s_entry *entry;
  455. struct vfs_s_inode *inode, *parent;
  456. long data_position;
  457. char *q;
  458. int len;
  459. char *current_file_name, *current_link_name;
  460. current_link_name =
  461. (next_long_link ? next_long_link :
  462. g_strndup (header->header.arch_linkname, NAMSIZ));
  463. len = strlen (current_link_name);
  464. if (len > 1 && current_link_name[len - 1] == '/')
  465. current_link_name[len - 1] = 0;
  466. current_file_name = NULL;
  467. switch (archive->u.arch.type) {
  468. case TAR_USTAR:
  469. case TAR_POSIX:
  470. /* The ustar archive format supports pathnames of upto 256
  471. * characters in length. This is achieved by concatenating
  472. * the contents of the `prefix' and `arch_name' fields like
  473. * this:
  474. *
  475. * prefix + path_separator + arch_name
  476. *
  477. * If the `prefix' field contains an empty string i.e. its
  478. * first characters is '\0' the prefix field is ignored.
  479. */
  480. if (header->header.unused.prefix[0] != '\0') {
  481. char *temp_name, *temp_prefix;
  482. temp_name = g_strndup (header->header.arch_name, NAMSIZ);
  483. temp_prefix = g_strndup (header->header.unused.prefix,
  484. PREFIX_SIZE);
  485. current_file_name = g_strconcat (temp_prefix, PATH_SEP_STR,
  486. temp_name, (char *) NULL);
  487. g_free (temp_name);
  488. g_free (temp_prefix);
  489. }
  490. break;
  491. case TAR_GNU:
  492. if (next_long_name != NULL)
  493. current_file_name = next_long_name;
  494. break;
  495. default:
  496. break;
  497. }
  498. if (current_file_name == NULL)
  499. current_file_name = g_strndup (header->header.arch_name, NAMSIZ);
  500. canonicalize_pathname (current_file_name);
  501. len = strlen (current_file_name);
  502. data_position = current_tar_position;
  503. p = strrchr (current_file_name, '/');
  504. if (p == NULL) {
  505. p = current_file_name;
  506. q = current_file_name + len; /* "" */
  507. } else {
  508. *(p++) = 0;
  509. q = current_file_name;
  510. }
  511. parent =
  512. vfs_s_find_inode (me, archive, q, LINK_NO_FOLLOW, FL_MKDIR);
  513. if (parent == NULL) {
  514. message (1, MSG_ERROR, _("Inconsistent tar archive"));
  515. return STATUS_BADCHECKSUM;
  516. }
  517. if (header->header.linkflag == LF_LINK) {
  518. inode =
  519. vfs_s_find_inode (me, archive, current_link_name,
  520. LINK_NO_FOLLOW, 0);
  521. if (inode == NULL) {
  522. message (1, MSG_ERROR, _("Inconsistent tar archive"));
  523. } else {
  524. entry = vfs_s_new_entry (me, p, inode);
  525. vfs_s_insert_entry (me, parent, entry);
  526. g_free (current_link_name);
  527. goto done;
  528. }
  529. }
  530. tar_fill_stat (archive, &st, header, *h_size);
  531. inode = vfs_s_new_inode (me, archive, &st);
  532. inode->data_offset = data_position;
  533. if (*current_link_name) {
  534. inode->linkname = current_link_name;
  535. } else if (current_link_name != next_long_link) {
  536. g_free (current_link_name);
  537. }
  538. entry = vfs_s_new_entry (me, p, inode);
  539. vfs_s_insert_entry (me, parent, entry);
  540. g_free (current_file_name);
  541. done:
  542. next_long_link = next_long_name = NULL;
  543. if (archive->u.arch.type == TAR_GNU &&
  544. header->header.unused.oldgnu.isextended) {
  545. while (tar_get_next_record (archive, tard)->ext_hdr.
  546. isextended);
  547. inode->data_offset = current_tar_position;
  548. }
  549. return STATUS_SUCCESS;
  550. }
  551. }
  552. /*
  553. * Main loop for reading an archive.
  554. * Returns 0 on success, -1 on error.
  555. */
  556. static int
  557. tar_open_archive (struct vfs_class *me, struct vfs_s_super *archive,
  558. const char *name, char *op)
  559. {
  560. /* Initial status at start of archive */
  561. ReadStatus status = STATUS_EOFMARK;
  562. ReadStatus prev_status;
  563. int tard;
  564. (void) op;
  565. current_tar_position = 0;
  566. /* Open for reading */
  567. if ((tard = tar_open_archive_int (me, name, archive)) == -1)
  568. return -1;
  569. for (;;) {
  570. size_t h_size;
  571. prev_status = status;
  572. status = tar_read_header (me, archive, tard, &h_size);
  573. switch (status) {
  574. case STATUS_SUCCESS:
  575. tar_skip_n_records (archive, tard,
  576. (h_size + RECORDSIZE -
  577. 1) / RECORDSIZE);
  578. continue;
  579. /*
  580. * Invalid header:
  581. *
  582. * If the previous header was good, tell them
  583. * that we are skipping bad ones.
  584. */
  585. case STATUS_BADCHECKSUM:
  586. switch (prev_status) {
  587. /* Error on first record */
  588. case STATUS_EOFMARK:
  589. message (1, MSG_ERROR,
  590. _
  591. ("Hmm,...\n%s\ndoesn't look like a tar archive."),
  592. name);
  593. /* FALL THRU */
  594. /* Error after header rec */
  595. case STATUS_SUCCESS:
  596. /* Error after error */
  597. case STATUS_BADCHECKSUM:
  598. return -1;
  599. case STATUS_EOF:
  600. return 0;
  601. }
  602. /* Record of zeroes */
  603. case STATUS_EOFMARK:
  604. status = prev_status; /* If error after 0's */
  605. /* FALL THRU */
  606. case STATUS_EOF: /* End of archive */
  607. break;
  608. }
  609. break;
  610. };
  611. return 0;
  612. }
  613. static void *
  614. tar_super_check (struct vfs_class *me, const char *archive_name, char *op)
  615. {
  616. static struct stat stat_buf;
  617. (void) me;
  618. (void) op;
  619. if (mc_stat (archive_name, &stat_buf))
  620. return NULL;
  621. return &stat_buf;
  622. }
  623. static int
  624. tar_super_same (struct vfs_class *me, struct vfs_s_super *parc,
  625. const char *archive_name, char *op, void *cookie)
  626. {
  627. struct stat *archive_stat = cookie; /* stat of main archive */
  628. (void) me;
  629. (void) op;
  630. if (strcmp (parc->name, archive_name))
  631. return 0;
  632. /* Has the cached archive been changed on the disk? */
  633. if (parc->u.arch.st.st_mtime < archive_stat->st_mtime) {
  634. /* Yes, reload! */
  635. (*vfs_tarfs_ops.free) ((vfsid) parc);
  636. vfs_rmstamp (&vfs_tarfs_ops, (vfsid) parc);
  637. return 2;
  638. }
  639. /* Hasn't been modified, give it a new timeout */
  640. vfs_stamp (&vfs_tarfs_ops, (vfsid) parc);
  641. return 1;
  642. }
  643. static int tar_read (void *fh, char *buffer, int count)
  644. {
  645. off_t begin = FH->ino->data_offset;
  646. int fd = FH_SUPER->u.arch.fd;
  647. struct vfs_class *me = FH_SUPER->me;
  648. if (mc_lseek (fd, begin + FH->pos, SEEK_SET) !=
  649. begin + FH->pos) ERRNOR (EIO, -1);
  650. count = MIN(count, FH->ino->st.st_size - FH->pos);
  651. if ((count = mc_read (fd, buffer, count)) == -1) ERRNOR (errno, -1);
  652. FH->pos += count;
  653. return count;
  654. }
  655. static int tar_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, int mode)
  656. {
  657. (void) fh;
  658. (void) mode;
  659. if ((flags & O_ACCMODE) != O_RDONLY) ERRNOR (EROFS, -1);
  660. return 0;
  661. }
  662. void
  663. init_tarfs (void)
  664. {
  665. static struct vfs_s_subclass tarfs_subclass;
  666. tarfs_subclass.flags = VFS_S_READONLY;
  667. tarfs_subclass.archive_check = tar_super_check;
  668. tarfs_subclass.archive_same = tar_super_same;
  669. tarfs_subclass.open_archive = tar_open_archive;
  670. tarfs_subclass.free_archive = tar_free_archive;
  671. tarfs_subclass.fh_open = tar_fh_open;
  672. vfs_s_init_class (&vfs_tarfs_ops, &tarfs_subclass);
  673. vfs_tarfs_ops.name = "tarfs";
  674. vfs_tarfs_ops.prefix = "utar";
  675. vfs_tarfs_ops.read = tar_read;
  676. vfs_tarfs_ops.setctl = NULL;
  677. vfs_register_class (&vfs_tarfs_ops);
  678. }