cpio.c 19 KB

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