tar.c 21 KB

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