cpio.c 19 KB

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