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