cpio.c 25 KB

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