tar.c 26 KB

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