cpio.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /* Virtual File System: GNU Tar file system.
  2. Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007
  3. Free Software Foundation, Inc.
  4. Written by: 2000 Jan Hudec
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License
  7. as published by the Free Software Foundation; either version 2 of
  8. the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  16. /** \file
  17. * \brief Source: Virtual File System: GNU Tar file system.
  18. * \author Jan Hudec
  19. * \date 2000
  20. */
  21. #include <config.h>
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #include "lib/global.h"
  25. #include "lib/unixcompat.h"
  26. #include "lib/util.h"
  27. #include "lib/widget.h" /* message() */
  28. #include "vfs-impl.h"
  29. #include "utilvfs.h"
  30. #include "xdirentry.h"
  31. #include "gc.h" /* vfs_rmstamp */
  32. /*** global variables ****************************************************************************/
  33. /*** file scope macro definitions ****************************************************************/
  34. #define CPIO_POS(super) cpio_position
  35. /* If some time reentrancy should be needed change it to */
  36. /* #define CPIO_POS(super) (super)->u.arch.fd */
  37. #define CPIO_SEEK_SET(super, where) mc_lseek((super)->u.arch.fd, CPIO_POS(super) = (where), SEEK_SET)
  38. #define CPIO_SEEK_CUR(super, where) mc_lseek((super)->u.arch.fd, CPIO_POS(super) += (where), SEEK_SET)
  39. #define MAGIC_LENGTH (6) /* How many bytes we have to read ahead */
  40. #define SEEKBACK CPIO_SEEK_CUR(super, ptr - top)
  41. #define RETURN(x) return(super->u.arch.type = (x))
  42. #define TYPEIS(x) ((super->u.arch.type == CPIO_UNKNOWN) || (super->u.arch.type == (x)))
  43. #define HEAD_LENGTH (26)
  44. /*** file scope type declarations ****************************************************************/
  45. enum
  46. {
  47. STATUS_START,
  48. STATUS_OK,
  49. STATUS_TRAIL,
  50. STATUS_FAIL,
  51. STATUS_EOF
  52. };
  53. enum
  54. {
  55. CPIO_UNKNOWN = 0, /* Not determined yet */
  56. CPIO_BIN, /* Binary format */
  57. CPIO_BINRE, /* Binary format, reverse endianity */
  58. CPIO_OLDC, /* Old ASCII format */
  59. CPIO_NEWC, /* New ASCII format */
  60. CPIO_CRC /* New ASCII format + CRC */
  61. };
  62. struct old_cpio_header
  63. {
  64. unsigned short c_magic;
  65. short c_dev;
  66. unsigned short c_ino;
  67. unsigned short c_mode;
  68. unsigned short c_uid;
  69. unsigned short c_gid;
  70. unsigned short c_nlink;
  71. short c_rdev;
  72. unsigned short c_mtimes[2];
  73. unsigned short c_namesize;
  74. unsigned short c_filesizes[2];
  75. };
  76. struct new_cpio_header
  77. {
  78. unsigned short c_magic;
  79. unsigned long c_ino;
  80. unsigned long c_mode;
  81. unsigned long c_uid;
  82. unsigned long c_gid;
  83. unsigned long c_nlink;
  84. unsigned long c_mtime;
  85. unsigned long c_filesize;
  86. long c_dev;
  87. long c_devmin;
  88. long c_rdev;
  89. long c_rdevmin;
  90. unsigned long c_namesize;
  91. unsigned long c_chksum;
  92. };
  93. struct defer_inode
  94. {
  95. struct defer_inode *next;
  96. unsigned long inumber;
  97. unsigned short device;
  98. struct vfs_s_inode *inode;
  99. };
  100. /*** file scope variables ************************************************************************/
  101. static struct vfs_class vfs_cpiofs_ops;
  102. /* FIXME: should be off_t instead of int. */
  103. static int cpio_position;
  104. /*** file scope functions ************************************************************************/
  105. /* --------------------------------------------------------------------------------------------- */
  106. static int cpio_find_head (struct vfs_class *me, struct vfs_s_super *super);
  107. static ssize_t cpio_read_bin_head (struct vfs_class *me, struct vfs_s_super *super);
  108. static ssize_t cpio_read_oldc_head (struct vfs_class *me, struct vfs_s_super *super);
  109. static ssize_t cpio_read_crc_head (struct vfs_class *me, struct vfs_s_super *super);
  110. static ssize_t cpio_read (void *fh, char *buffer, size_t count);
  111. /* --------------------------------------------------------------------------------------------- */
  112. static struct defer_inode *
  113. cpio_defer_find (struct defer_inode *l, struct defer_inode *i)
  114. {
  115. while (l && (l->inumber != i->inumber || l->device != i->device))
  116. l = l->next;
  117. return l;
  118. }
  119. /* --------------------------------------------------------------------------------------------- */
  120. static int
  121. cpio_skip_padding (struct vfs_s_super *super)
  122. {
  123. switch (super->u.arch.type)
  124. {
  125. case CPIO_BIN:
  126. case CPIO_BINRE:
  127. return CPIO_SEEK_CUR (super, (2 - (CPIO_POS (super) % 2)) % 2);
  128. case CPIO_NEWC:
  129. case CPIO_CRC:
  130. return CPIO_SEEK_CUR (super, (4 - (CPIO_POS (super) % 4)) % 4);
  131. case CPIO_OLDC:
  132. return CPIO_POS (super);
  133. default:
  134. g_assert_not_reached ();
  135. return 42; /* & the compiler is happy :-) */
  136. }
  137. }
  138. /* --------------------------------------------------------------------------------------------- */
  139. static void
  140. cpio_free_archive (struct vfs_class *me, struct vfs_s_super *super)
  141. {
  142. struct defer_inode *l, *lnext;
  143. (void) me;
  144. if (super->u.arch.fd != -1)
  145. mc_close (super->u.arch.fd);
  146. super->u.arch.fd = -1;
  147. for (l = super->u.arch.deferred; l; l = lnext)
  148. {
  149. lnext = l->next;
  150. g_free (l);
  151. }
  152. super->u.arch.deferred = NULL;
  153. }
  154. /* --------------------------------------------------------------------------------------------- */
  155. static int
  156. cpio_open_cpio_file (struct vfs_class *me, struct vfs_s_super *super, const char *name)
  157. {
  158. int fd, type;
  159. mode_t mode;
  160. struct vfs_s_inode *root;
  161. fd = mc_open (name, O_RDONLY);
  162. if (fd == -1)
  163. {
  164. message (D_ERROR, MSG_ERROR, _("Cannot open cpio archive\n%s"), name);
  165. return -1;
  166. }
  167. super->name = g_strdup (name);
  168. super->u.arch.fd = -1; /* for now */
  169. mc_stat (name, &(super->u.arch.st));
  170. super->u.arch.type = CPIO_UNKNOWN;
  171. type = get_compression_type (fd, name);
  172. if (type != COMPRESSION_NONE)
  173. {
  174. char *s;
  175. mc_close (fd);
  176. s = g_strconcat (name, decompress_extension (type), (char *) NULL);
  177. fd = mc_open (s, O_RDONLY);
  178. if (fd == -1)
  179. {
  180. message (D_ERROR, MSG_ERROR, _("Cannot open cpio archive\n%s"), s);
  181. g_free (s);
  182. return -1;
  183. }
  184. g_free (s);
  185. }
  186. super->u.arch.fd = fd;
  187. mode = super->u.arch.st.st_mode & 07777;
  188. mode |= (mode & 0444) >> 2; /* set eXec where Read is */
  189. mode |= S_IFDIR;
  190. root = vfs_s_new_inode (me, super, &(super->u.arch.st));
  191. root->st.st_mode = mode;
  192. root->data_offset = -1;
  193. root->st.st_nlink++;
  194. root->st.st_dev = MEDATA->rdev++;
  195. super->root = root;
  196. CPIO_SEEK_SET (super, 0);
  197. return fd;
  198. }
  199. /* --------------------------------------------------------------------------------------------- */
  200. static ssize_t
  201. cpio_read_head (struct vfs_class *me, struct vfs_s_super *super)
  202. {
  203. switch (cpio_find_head (me, super))
  204. {
  205. case CPIO_UNKNOWN:
  206. return -1;
  207. case CPIO_BIN:
  208. case CPIO_BINRE:
  209. return cpio_read_bin_head (me, super);
  210. case CPIO_OLDC:
  211. return cpio_read_oldc_head (me, super);
  212. case CPIO_NEWC:
  213. case CPIO_CRC:
  214. return cpio_read_crc_head (me, super);
  215. default:
  216. g_assert_not_reached ();
  217. return 42; /* & the compiler is happy :-) */
  218. }
  219. }
  220. /* --------------------------------------------------------------------------------------------- */
  221. static int
  222. cpio_find_head (struct vfs_class *me, struct vfs_s_super *super)
  223. {
  224. char buf[256];
  225. int ptr = 0;
  226. ssize_t top;
  227. ssize_t tmp;
  228. top = mc_read (super->u.arch.fd, buf, 256);
  229. if (top > 0)
  230. CPIO_POS (super) += top;
  231. for (;;)
  232. {
  233. if (ptr + MAGIC_LENGTH >= top)
  234. {
  235. if (top > 128)
  236. {
  237. memmove (buf, buf + top - 128, 128);
  238. ptr -= top - 128;
  239. top = 128;
  240. }
  241. tmp = mc_read (super->u.arch.fd, buf, top);
  242. if (tmp == 0 || tmp == -1)
  243. {
  244. message (D_ERROR, MSG_ERROR, _("Premature end of cpio archive\n%s"), super->name);
  245. cpio_free_archive (me, super);
  246. return CPIO_UNKNOWN;
  247. }
  248. top += tmp;
  249. }
  250. if (TYPEIS (CPIO_BIN) && ((*(unsigned short *) (buf + ptr)) == 070707))
  251. {
  252. SEEKBACK;
  253. RETURN (CPIO_BIN);
  254. }
  255. else if (TYPEIS (CPIO_BINRE)
  256. && ((*(unsigned short *) (buf + ptr)) == GUINT16_SWAP_LE_BE_CONSTANT (070707)))
  257. {
  258. SEEKBACK;
  259. RETURN (CPIO_BINRE);
  260. }
  261. else if (TYPEIS (CPIO_OLDC) && (!strncmp (buf + ptr, "070707", 6)))
  262. {
  263. SEEKBACK;
  264. RETURN (CPIO_OLDC);
  265. }
  266. else if (TYPEIS (CPIO_NEWC) && (!strncmp (buf + ptr, "070701", 6)))
  267. {
  268. SEEKBACK;
  269. RETURN (CPIO_NEWC);
  270. }
  271. else if (TYPEIS (CPIO_CRC) && (!strncmp (buf + ptr, "070702", 6)))
  272. {
  273. SEEKBACK;
  274. RETURN (CPIO_CRC);
  275. };
  276. ptr++;
  277. }
  278. }
  279. /* --------------------------------------------------------------------------------------------- */
  280. static int
  281. cpio_create_entry (struct vfs_class *me, struct vfs_s_super *super, struct stat *st, char *name)
  282. {
  283. struct vfs_s_inode *inode = NULL;
  284. struct vfs_s_inode *root = super->root;
  285. struct vfs_s_entry *entry = NULL;
  286. char *tn;
  287. switch (st->st_mode & S_IFMT)
  288. { /* For case of HP/UX archives */
  289. case S_IFCHR:
  290. case S_IFBLK:
  291. #ifdef S_IFSOCK
  292. case S_IFSOCK:
  293. #endif
  294. #ifdef S_IFIFO
  295. case S_IFIFO:
  296. #endif
  297. #ifdef S_IFNAM
  298. case S_IFNAM:
  299. #endif
  300. if ((st->st_size != 0) && (st->st_rdev == 0x0001))
  301. {
  302. /* FIXME: representation of major/minor differs between */
  303. /* different operating systems. */
  304. st->st_rdev = (unsigned) st->st_size;
  305. st->st_size = 0;
  306. }
  307. break;
  308. default:
  309. break;
  310. }
  311. if ((st->st_nlink > 1)
  312. && ((super->u.arch.type == CPIO_NEWC) || (super->u.arch.type == CPIO_CRC)))
  313. { /* For case of hardlinked files */
  314. struct defer_inode i, *l;
  315. i.inumber = st->st_ino;
  316. i.device = st->st_dev;
  317. i.inode = NULL;
  318. l = cpio_defer_find (super->u.arch.deferred, &i);
  319. if (l != NULL)
  320. {
  321. inode = l->inode;
  322. if (inode->st.st_size != 0 && st->st_size != 0 && (inode->st.st_size != st->st_size))
  323. {
  324. message (D_ERROR, MSG_ERROR,
  325. _("Inconsistent hardlinks of\n%s\nin cpio archive\n%s"),
  326. name, super->name);
  327. inode = NULL;
  328. }
  329. else if (inode->st.st_size == 0)
  330. inode->st.st_size = st->st_size;
  331. }
  332. }
  333. /* remove trailing slashes */
  334. for (tn = name + strlen (name) - 1; tn >= name && *tn == PATH_SEP; tn--)
  335. *tn = '\0';
  336. tn = strrchr (name, PATH_SEP);
  337. if (tn == NULL)
  338. tn = name;
  339. else if (tn == name + 1)
  340. {
  341. /* started with "./" -- directory in the root of archive */
  342. tn++;
  343. }
  344. else
  345. {
  346. *tn = '\0';
  347. root = vfs_s_find_inode (me, super, name, LINK_FOLLOW, FL_MKDIR);
  348. *tn = PATH_SEP;
  349. tn++;
  350. }
  351. entry = MEDATA->find_entry (me, root, tn, LINK_FOLLOW, FL_NONE); /* In case entry is already there */
  352. if (entry != NULL)
  353. {
  354. /* This shouldn't happen! (well, it can happen if there is a record for a
  355. file and than a record for a directory it is in; cpio would die with
  356. 'No such file or directory' is such case) */
  357. if (!S_ISDIR (entry->ino->st.st_mode))
  358. {
  359. /* This can be considered archive inconsistency */
  360. message (D_ERROR, MSG_ERROR,
  361. _("%s contains duplicate entries! Skipping!"), super->name);
  362. }
  363. else
  364. {
  365. entry->ino->st.st_mode = st->st_mode;
  366. entry->ino->st.st_uid = st->st_uid;
  367. entry->ino->st.st_gid = st->st_gid;
  368. entry->ino->st.st_atime = st->st_atime;
  369. entry->ino->st.st_mtime = st->st_mtime;
  370. entry->ino->st.st_ctime = st->st_ctime;
  371. }
  372. g_free (name);
  373. }
  374. else
  375. { /* !entry */
  376. if (inode == NULL)
  377. {
  378. inode = vfs_s_new_inode (me, super, st);
  379. if ((st->st_nlink > 0)
  380. && ((super->u.arch.type == CPIO_NEWC) || (super->u.arch.type == CPIO_CRC)))
  381. {
  382. /* For case of hardlinked files */
  383. struct defer_inode *i;
  384. i = g_new (struct defer_inode, 1);
  385. i->inumber = st->st_ino;
  386. i->device = st->st_dev;
  387. i->inode = inode;
  388. i->next = super->u.arch.deferred;
  389. super->u.arch.deferred = i;
  390. }
  391. }
  392. if (st->st_size != 0)
  393. inode->data_offset = CPIO_POS (super);
  394. entry = vfs_s_new_entry (me, tn, inode);
  395. vfs_s_insert_entry (me, root, entry);
  396. g_free (name);
  397. if (!S_ISLNK (st->st_mode))
  398. CPIO_SEEK_CUR (super, st->st_size);
  399. else
  400. {
  401. inode->linkname = g_malloc (st->st_size + 1);
  402. if (mc_read (super->u.arch.fd, inode->linkname, st->st_size) < st->st_size)
  403. {
  404. inode->linkname[0] = '\0';
  405. return STATUS_EOF;
  406. }
  407. inode->linkname[st->st_size] = '\0'; /* Linkname stored without terminating \0 !!! */
  408. CPIO_POS (super) += st->st_size;
  409. cpio_skip_padding (super);
  410. }
  411. } /* !entry */
  412. return STATUS_OK;
  413. }
  414. /* --------------------------------------------------------------------------------------------- */
  415. static ssize_t
  416. cpio_read_bin_head (struct vfs_class *me, struct vfs_s_super *super)
  417. {
  418. union
  419. {
  420. struct old_cpio_header buf;
  421. short shorts[HEAD_LENGTH >> 1];
  422. } u;
  423. ssize_t len;
  424. char *name;
  425. struct stat st;
  426. len = mc_read (super->u.arch.fd, (char *) &u.buf, HEAD_LENGTH);
  427. if (len < HEAD_LENGTH)
  428. return STATUS_EOF;
  429. CPIO_POS (super) += len;
  430. if (super->u.arch.type == CPIO_BINRE)
  431. {
  432. int i;
  433. for (i = 0; i < (HEAD_LENGTH >> 1); i++)
  434. u.shorts[i] = GUINT16_SWAP_LE_BE_CONSTANT (u.shorts[i]);
  435. }
  436. if (u.buf.c_magic != 070707 || u.buf.c_namesize == 0 || u.buf.c_namesize > MC_MAXPATHLEN)
  437. {
  438. message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), super->name);
  439. return STATUS_FAIL;
  440. }
  441. name = g_malloc (u.buf.c_namesize);
  442. len = mc_read (super->u.arch.fd, name, u.buf.c_namesize);
  443. if (len < u.buf.c_namesize)
  444. {
  445. g_free (name);
  446. return STATUS_EOF;
  447. }
  448. name[u.buf.c_namesize - 1] = '\0';
  449. CPIO_POS (super) += len;
  450. cpio_skip_padding (super);
  451. if (!strcmp ("TRAILER!!!", name))
  452. { /* We got to the last record */
  453. g_free (name);
  454. return STATUS_TRAIL;
  455. }
  456. st.st_dev = u.buf.c_dev;
  457. st.st_ino = u.buf.c_ino;
  458. st.st_mode = u.buf.c_mode;
  459. st.st_nlink = u.buf.c_nlink;
  460. st.st_uid = u.buf.c_uid;
  461. st.st_gid = u.buf.c_gid;
  462. st.st_rdev = u.buf.c_rdev;
  463. st.st_size = (u.buf.c_filesizes[0] << 16) | u.buf.c_filesizes[1];
  464. st.st_atime = st.st_mtime = st.st_ctime = (u.buf.c_mtimes[0] << 16) | u.buf.c_mtimes[1];
  465. return cpio_create_entry (me, super, &st, name);
  466. }
  467. /* --------------------------------------------------------------------------------------------- */
  468. #undef HEAD_LENGTH
  469. #define HEAD_LENGTH (76)
  470. static ssize_t
  471. cpio_read_oldc_head (struct vfs_class *me, struct vfs_s_super *super)
  472. {
  473. struct new_cpio_header hd;
  474. union
  475. {
  476. struct stat st;
  477. char buf[HEAD_LENGTH + 1];
  478. } u;
  479. ssize_t len;
  480. char *name;
  481. if (mc_read (super->u.arch.fd, u.buf, HEAD_LENGTH) != HEAD_LENGTH)
  482. return STATUS_EOF;
  483. CPIO_POS (super) += HEAD_LENGTH;
  484. u.buf[HEAD_LENGTH] = 0;
  485. if (sscanf (u.buf, "070707%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6lo%11lo",
  486. (unsigned long *) &hd.c_dev, &hd.c_ino, &hd.c_mode, &hd.c_uid, &hd.c_gid,
  487. &hd.c_nlink, (unsigned long *) &hd.c_rdev, &hd.c_mtime,
  488. &hd.c_namesize, &hd.c_filesize) < 10)
  489. {
  490. message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), super->name);
  491. return STATUS_FAIL;
  492. }
  493. if (hd.c_namesize == 0 || hd.c_namesize > MC_MAXPATHLEN)
  494. {
  495. message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), super->name);
  496. return STATUS_FAIL;
  497. }
  498. name = g_malloc (hd.c_namesize);
  499. len = mc_read (super->u.arch.fd, name, hd.c_namesize);
  500. if ((len == -1) || ((unsigned long) len < hd.c_namesize))
  501. {
  502. g_free (name);
  503. return STATUS_EOF;
  504. }
  505. name[hd.c_namesize - 1] = '\0';
  506. CPIO_POS (super) += len;
  507. cpio_skip_padding (super);
  508. if (!strcmp ("TRAILER!!!", name))
  509. { /* We got to the last record */
  510. g_free (name);
  511. return STATUS_TRAIL;
  512. }
  513. u.st.st_dev = hd.c_dev;
  514. u.st.st_ino = hd.c_ino;
  515. u.st.st_mode = hd.c_mode;
  516. u.st.st_nlink = hd.c_nlink;
  517. u.st.st_uid = hd.c_uid;
  518. u.st.st_gid = hd.c_gid;
  519. u.st.st_rdev = hd.c_rdev;
  520. u.st.st_size = hd.c_filesize;
  521. u.st.st_atime = u.st.st_mtime = u.st.st_ctime = hd.c_mtime;
  522. return cpio_create_entry (me, super, &u.st, name);
  523. }
  524. /* --------------------------------------------------------------------------------------------- */
  525. #undef HEAD_LENGTH
  526. #define HEAD_LENGTH (110)
  527. static ssize_t
  528. cpio_read_crc_head (struct vfs_class *me, struct vfs_s_super *super)
  529. {
  530. struct new_cpio_header hd;
  531. union
  532. {
  533. struct stat st;
  534. char buf[HEAD_LENGTH + 1];
  535. } u;
  536. ssize_t len;
  537. char *name;
  538. if (mc_read (super->u.arch.fd, u.buf, HEAD_LENGTH) != HEAD_LENGTH)
  539. return STATUS_EOF;
  540. CPIO_POS (super) += HEAD_LENGTH;
  541. u.buf[HEAD_LENGTH] = '\0';
  542. if (sscanf (u.buf, "%6ho%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx",
  543. &hd.c_magic, &hd.c_ino, &hd.c_mode, &hd.c_uid, &hd.c_gid,
  544. &hd.c_nlink, &hd.c_mtime, &hd.c_filesize,
  545. (unsigned long *) &hd.c_dev, (unsigned long *) &hd.c_devmin,
  546. (unsigned long *) &hd.c_rdev, (unsigned long *) &hd.c_rdevmin,
  547. &hd.c_namesize, &hd.c_chksum) < 14)
  548. {
  549. message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), super->name);
  550. return STATUS_FAIL;
  551. }
  552. if ((super->u.arch.type == CPIO_NEWC && hd.c_magic != 070701) ||
  553. (super->u.arch.type == CPIO_CRC && hd.c_magic != 070702))
  554. return STATUS_FAIL;
  555. if (hd.c_namesize == 0 || hd.c_namesize > MC_MAXPATHLEN)
  556. {
  557. message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), super->name);
  558. return STATUS_FAIL;
  559. }
  560. name = g_malloc (hd.c_namesize);
  561. len = mc_read (super->u.arch.fd, name, hd.c_namesize);
  562. if ((len == -1) || ((unsigned long) len < hd.c_namesize))
  563. {
  564. g_free (name);
  565. return STATUS_EOF;
  566. }
  567. name[hd.c_namesize - 1] = '\0';
  568. CPIO_POS (super) += len;
  569. cpio_skip_padding (super);
  570. if (strcmp ("TRAILER!!!", name) == 0)
  571. { /* We got to the last record */
  572. g_free (name);
  573. return STATUS_TRAIL;
  574. }
  575. u.st.st_dev = makedev (hd.c_dev, hd.c_devmin);
  576. u.st.st_ino = hd.c_ino;
  577. u.st.st_mode = hd.c_mode;
  578. u.st.st_nlink = hd.c_nlink;
  579. u.st.st_uid = hd.c_uid;
  580. u.st.st_gid = hd.c_gid;
  581. u.st.st_rdev = makedev (hd.c_rdev, hd.c_rdevmin);
  582. u.st.st_size = hd.c_filesize;
  583. u.st.st_atime = u.st.st_mtime = u.st.st_ctime = hd.c_mtime;
  584. return cpio_create_entry (me, super, &u.st, name);
  585. }
  586. /* --------------------------------------------------------------------------------------------- */
  587. /** Need to CPIO_SEEK_CUR to skip the file at the end of add entry!!!! */
  588. static int
  589. cpio_open_archive (struct vfs_class *me, struct vfs_s_super *super, const char *name, char *op)
  590. {
  591. int status = STATUS_START;
  592. (void) op;
  593. if (cpio_open_cpio_file (me, super, name) == -1)
  594. return -1;
  595. for (;;)
  596. {
  597. status = cpio_read_head (me, super);
  598. switch (status)
  599. {
  600. case STATUS_EOF:
  601. message (D_ERROR, MSG_ERROR, _("Unexpected end of file\n%s"), name);
  602. return 0;
  603. case STATUS_OK:
  604. continue;
  605. case STATUS_TRAIL:
  606. break;
  607. }
  608. break;
  609. }
  610. return 0;
  611. }
  612. /* --------------------------------------------------------------------------------------------- */
  613. /** Remaining functions are exactly same as for tarfs (and were in fact just copied) */
  614. static void *
  615. cpio_super_check (struct vfs_class *me, const char *archive_name, char *op)
  616. {
  617. static struct stat sb;
  618. (void) me;
  619. (void) op;
  620. if (mc_stat (archive_name, &sb))
  621. return NULL;
  622. return &sb;
  623. }
  624. /* --------------------------------------------------------------------------------------------- */
  625. static int
  626. cpio_super_same (struct vfs_class *me, struct vfs_s_super *parc,
  627. const char *archive_name, char *op, void *cookie)
  628. {
  629. struct stat *archive_stat = cookie; /* stat of main archive */
  630. (void) me;
  631. (void) op;
  632. if (strcmp (parc->name, archive_name))
  633. return 0;
  634. /* Has the cached archive been changed on the disk? */
  635. if (parc->u.arch.st.st_mtime < archive_stat->st_mtime)
  636. {
  637. /* Yes, reload! */
  638. (*vfs_cpiofs_ops.free) ((vfsid) parc);
  639. vfs_rmstamp (&vfs_cpiofs_ops, (vfsid) parc);
  640. return 2;
  641. }
  642. /* Hasn't been modified, give it a new timeout */
  643. vfs_stamp (&vfs_cpiofs_ops, (vfsid) parc);
  644. return 1;
  645. }
  646. /* --------------------------------------------------------------------------------------------- */
  647. static ssize_t
  648. cpio_read (void *fh, char *buffer, size_t count)
  649. {
  650. off_t begin = FH->ino->data_offset;
  651. int fd = FH_SUPER->u.arch.fd;
  652. struct vfs_class *me = FH_SUPER->me;
  653. ssize_t res;
  654. if (mc_lseek (fd, begin + FH->pos, SEEK_SET) != begin + FH->pos)
  655. ERRNOR (EIO, -1);
  656. count = MIN (count, (size_t) (FH->ino->st.st_size - FH->pos));
  657. res = mc_read (fd, buffer, count);
  658. if (res == -1)
  659. ERRNOR (errno, -1);
  660. FH->pos += res;
  661. return res;
  662. }
  663. /* --------------------------------------------------------------------------------------------- */
  664. static int
  665. cpio_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, mode_t mode)
  666. {
  667. (void) fh;
  668. (void) mode;
  669. if ((flags & O_ACCMODE) != O_RDONLY)
  670. ERRNOR (EROFS, -1);
  671. return 0;
  672. }
  673. /* --------------------------------------------------------------------------------------------- */
  674. /*** public functions ****************************************************************************/
  675. /* --------------------------------------------------------------------------------------------- */
  676. void
  677. init_cpiofs (void)
  678. {
  679. static struct vfs_s_subclass cpio_subclass;
  680. cpio_subclass.flags = VFS_S_READONLY;
  681. cpio_subclass.archive_check = cpio_super_check;
  682. cpio_subclass.archive_same = cpio_super_same;
  683. cpio_subclass.open_archive = cpio_open_archive;
  684. cpio_subclass.free_archive = cpio_free_archive;
  685. cpio_subclass.fh_open = cpio_fh_open;
  686. vfs_s_init_class (&vfs_cpiofs_ops, &cpio_subclass);
  687. vfs_cpiofs_ops.name = "cpiofs";
  688. vfs_cpiofs_ops.prefix = "ucpio";
  689. vfs_cpiofs_ops.read = cpio_read;
  690. vfs_cpiofs_ops.setctl = NULL;
  691. vfs_register_class (&vfs_cpiofs_ops);
  692. }
  693. /* --------------------------------------------------------------------------------------------- */