tar.c 21 KB

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