cpio.c 19 KB

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