cpio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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 int cpio_create_entry(struct vfs_class *me, struct vfs_s_super *super, struct stat *, char *name);
  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. int top;
  201. int 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. #define HEAD_LENGTH (26)
  236. static ssize_t cpio_read_bin_head(struct vfs_class *me, struct vfs_s_super *super)
  237. {
  238. union {
  239. struct old_cpio_header buf;
  240. short shorts[HEAD_LENGTH >> 1];
  241. } u;
  242. int len;
  243. char *name;
  244. struct stat st;
  245. if((len = mc_read(super->u.arch.fd, (char *)&u.buf, HEAD_LENGTH)) < HEAD_LENGTH)
  246. return STATUS_EOF;
  247. CPIO_POS(super) += len;
  248. if(super->u.arch.type == CPIO_BINRE) {
  249. int i;
  250. for(i = 0; i < (HEAD_LENGTH >> 1); i++)
  251. u.shorts[i] = GUINT16_SWAP_LE_BE_CONSTANT(u.shorts[i]);
  252. }
  253. g_assert(u.buf.c_magic == 070707);
  254. if (u.buf.c_namesize == 0 || u.buf.c_namesize > MC_MAXPATHLEN) {
  255. message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"),
  256. super->name);
  257. return STATUS_FAIL;
  258. }
  259. name = g_malloc(u.buf.c_namesize);
  260. if((len = mc_read(super->u.arch.fd, name, u.buf.c_namesize)) < u.buf.c_namesize) {
  261. g_free(name);
  262. return STATUS_EOF;
  263. }
  264. name[u.buf.c_namesize - 1] = '\0';
  265. CPIO_POS(super) += len;
  266. cpio_skip_padding(super);
  267. if(!strcmp("TRAILER!!!", name)) { /* We got to the last record */
  268. g_free(name);
  269. return STATUS_TRAIL;
  270. }
  271. st.st_dev = u.buf.c_dev;
  272. st.st_ino = u.buf.c_ino;
  273. st.st_mode = u.buf.c_mode;
  274. st.st_nlink = u.buf.c_nlink;
  275. st.st_uid = u.buf.c_uid;
  276. st.st_gid = u.buf.c_gid;
  277. st.st_rdev = u.buf.c_rdev;
  278. st.st_size = (u.buf.c_filesizes[0] << 16) | u.buf.c_filesizes[1];
  279. st.st_atime = st.st_mtime = st.st_ctime = (u.buf.c_mtimes[0] << 16) | u.buf.c_mtimes[1];
  280. return cpio_create_entry(me, super, &st, name);
  281. }
  282. #undef HEAD_LENGTH
  283. #define HEAD_LENGTH (76)
  284. static ssize_t cpio_read_oldc_head(struct vfs_class *me, struct vfs_s_super *super)
  285. {
  286. struct new_cpio_header hd;
  287. union {
  288. struct stat st;
  289. char buf[HEAD_LENGTH + 1];
  290. } u;
  291. int len;
  292. char *name;
  293. if (mc_read (super->u.arch.fd, u.buf, HEAD_LENGTH) != HEAD_LENGTH)
  294. return STATUS_EOF;
  295. CPIO_POS (super) += HEAD_LENGTH;
  296. u.buf[HEAD_LENGTH] = 0;
  297. if (sscanf (u.buf, "070707%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6lo%11lo",
  298. (unsigned long *)&hd.c_dev, &hd.c_ino, &hd.c_mode, &hd.c_uid, &hd.c_gid,
  299. &hd.c_nlink, (unsigned long *)&hd.c_rdev, &hd.c_mtime,
  300. &hd.c_namesize, &hd.c_filesize) < 10) {
  301. message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"),
  302. super->name);
  303. return STATUS_FAIL;
  304. }
  305. if (hd.c_namesize == 0 || hd.c_namesize > MC_MAXPATHLEN) {
  306. message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"),
  307. super->name);
  308. return STATUS_FAIL;
  309. }
  310. name = g_malloc(hd.c_namesize);
  311. if((len = mc_read(super->u.arch.fd, name, hd.c_namesize)) == -1 ||
  312. (unsigned long) len < hd.c_namesize) {
  313. g_free (name);
  314. return STATUS_EOF;
  315. }
  316. name[hd.c_namesize - 1] = '\0';
  317. CPIO_POS(super) += len;
  318. cpio_skip_padding(super);
  319. if(!strcmp("TRAILER!!!", name)) { /* We got to the last record */
  320. g_free(name);
  321. return STATUS_TRAIL;
  322. }
  323. u.st.st_dev = hd.c_dev;
  324. u.st.st_ino = hd.c_ino;
  325. u.st.st_mode = hd.c_mode;
  326. u.st.st_nlink = hd.c_nlink;
  327. u.st.st_uid = hd.c_uid;
  328. u.st.st_gid = hd.c_gid;
  329. u.st.st_rdev = hd.c_rdev;
  330. u.st.st_size = hd.c_filesize;
  331. u.st.st_atime = u.st.st_mtime = u.st.st_ctime = hd.c_mtime;
  332. return cpio_create_entry (me, super, &u.st, name);
  333. }
  334. #undef HEAD_LENGTH
  335. #define HEAD_LENGTH (110)
  336. static ssize_t cpio_read_crc_head(struct vfs_class *me, struct vfs_s_super *super)
  337. {
  338. struct new_cpio_header hd;
  339. union {
  340. struct stat st;
  341. char buf[HEAD_LENGTH + 1];
  342. } u;
  343. int len;
  344. char *name;
  345. if (mc_read (super->u.arch.fd, u.buf, HEAD_LENGTH) != HEAD_LENGTH)
  346. return STATUS_EOF;
  347. CPIO_POS (super) += HEAD_LENGTH;
  348. u.buf[HEAD_LENGTH] = 0;
  349. if (sscanf (u.buf, "%6ho%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx",
  350. &hd.c_magic, &hd.c_ino, &hd.c_mode, &hd.c_uid, &hd.c_gid,
  351. &hd.c_nlink, &hd.c_mtime, &hd.c_filesize,
  352. (unsigned long *)&hd.c_dev, (unsigned long *)&hd.c_devmin, (unsigned long *)&hd.c_rdev, (unsigned long *)&hd.c_rdevmin,
  353. &hd.c_namesize, &hd.c_chksum) < 14) {
  354. message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"),
  355. super->name);
  356. return STATUS_FAIL;
  357. }
  358. if((super->u.arch.type == CPIO_NEWC && hd.c_magic != 070701) ||
  359. (super->u.arch.type == CPIO_CRC && hd.c_magic != 070702))
  360. return STATUS_FAIL;
  361. if (hd.c_namesize == 0 || hd.c_namesize > MC_MAXPATHLEN) {
  362. message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"),
  363. super->name);
  364. return STATUS_FAIL;
  365. }
  366. name = g_malloc(hd.c_namesize);
  367. if((len = mc_read (super->u.arch.fd, name, hd.c_namesize)) == -1 ||
  368. (unsigned long) len < hd.c_namesize) {
  369. g_free (name);
  370. return STATUS_EOF;
  371. }
  372. name[hd.c_namesize - 1] = '\0';
  373. CPIO_POS(super) += len;
  374. cpio_skip_padding(super);
  375. if(!strcmp("TRAILER!!!", name)) { /* We got to the last record */
  376. g_free(name);
  377. return STATUS_TRAIL;
  378. }
  379. u.st.st_dev = makedev (hd.c_dev, hd.c_devmin);
  380. u.st.st_ino = hd.c_ino;
  381. u.st.st_mode = hd.c_mode;
  382. u.st.st_nlink = hd.c_nlink;
  383. u.st.st_uid = hd.c_uid;
  384. u.st.st_gid = hd.c_gid;
  385. u.st.st_rdev = makedev (hd.c_rdev, hd.c_rdevmin);
  386. u.st.st_size = hd.c_filesize;
  387. u.st.st_atime = u.st.st_mtime = u.st.st_ctime = hd.c_mtime;
  388. return cpio_create_entry (me, super, &u.st, name);
  389. }
  390. static int
  391. cpio_create_entry (struct vfs_class *me, struct vfs_s_super *super,
  392. struct stat *st, char *name)
  393. {
  394. struct vfs_s_inode *inode = NULL;
  395. struct vfs_s_inode *root = super->root;
  396. struct vfs_s_entry *entry = NULL;
  397. char *tn;
  398. switch (st->st_mode & S_IFMT) { /* For case of HP/UX archives */
  399. case S_IFCHR:
  400. case S_IFBLK:
  401. #ifdef S_IFSOCK
  402. case S_IFSOCK:
  403. #endif
  404. #ifdef S_IFIFO
  405. case S_IFIFO:
  406. #endif
  407. #ifdef S_IFNAM
  408. case S_IFNAM:
  409. #endif
  410. if ((st->st_size != 0) && (st->st_rdev == 0x0001)) {
  411. /* FIXME: representation of major/minor differs between */
  412. /* different operating systems. */
  413. st->st_rdev = (unsigned) st->st_size;
  414. st->st_size = 0;
  415. }
  416. break;
  417. default:
  418. break;
  419. }
  420. if ((st->st_nlink > 1) && (super->u.arch.type == CPIO_NEWC || super->u.arch.type == CPIO_CRC)) { /* For case of hardlinked files */
  421. struct defer_inode i, *l;
  422. i.inumber = st->st_ino;
  423. i.device = st->st_dev;
  424. i.inode = NULL;
  425. if ((l = cpio_defer_find (super->u.arch.deferred, &i)) != NULL) {
  426. inode = l->inode;
  427. if (inode->st.st_size && st->st_size
  428. && (inode->st.st_size != st->st_size)) {
  429. message (D_ERROR, MSG_ERROR,
  430. _
  431. ("Inconsistent hardlinks of\n%s\nin cpio archive\n%s"),
  432. name, super->name);
  433. inode = NULL;
  434. } else if (!inode->st.st_size)
  435. inode->st.st_size = st->st_size;
  436. }
  437. }
  438. for (tn = name + strlen (name) - 1; tn >= name && *tn == PATH_SEP; tn--)
  439. *tn = 0;
  440. if ((tn = strrchr (name, PATH_SEP))) {
  441. *tn = 0;
  442. root = vfs_s_find_inode (me, super, name, LINK_FOLLOW, FL_MKDIR);
  443. *tn = PATH_SEP;
  444. tn++;
  445. } else
  446. tn = name;
  447. entry = MEDATA->find_entry (me, root, tn, LINK_FOLLOW, FL_NONE); /* In case entry is already there */
  448. if (entry) { /* This shouldn't happen! (well, it can happen if there is a record for a
  449. file and than a record for a directory it is in; cpio would die with
  450. 'No such file or directory' is such case) */
  451. if (!S_ISDIR (entry->ino->st.st_mode)) { /* This can be considered archive inconsistency */
  452. message (D_ERROR, MSG_ERROR,
  453. _("%s contains duplicate entries! Skipping!"),
  454. super->name);
  455. } else {
  456. entry->ino->st.st_mode = st->st_mode;
  457. entry->ino->st.st_uid = st->st_uid;
  458. entry->ino->st.st_gid = st->st_gid;
  459. entry->ino->st.st_atime = st->st_atime;
  460. entry->ino->st.st_mtime = st->st_mtime;
  461. entry->ino->st.st_ctime = st->st_ctime;
  462. }
  463. } else { /* !entry */
  464. if (!inode) {
  465. inode = vfs_s_new_inode (me, super, st);
  466. if ((st->st_nlink > 0) && (super->u.arch.type == CPIO_NEWC || super->u.arch.type == CPIO_CRC)) { /* For case of hardlinked files */
  467. struct defer_inode *i;
  468. i = g_new (struct defer_inode, 1);
  469. i->inumber = st->st_ino;
  470. i->device = st->st_dev;
  471. i->inode = inode;
  472. i->next = super->u.arch.deferred;
  473. super->u.arch.deferred = i;
  474. }
  475. }
  476. if (st->st_size)
  477. inode->data_offset = CPIO_POS (super);
  478. entry = vfs_s_new_entry (me, tn, inode);
  479. vfs_s_insert_entry (me, root, entry);
  480. if (S_ISLNK (st->st_mode)) {
  481. inode->linkname = g_malloc (st->st_size + 1);
  482. if (mc_read (super->u.arch.fd, inode->linkname, st->st_size)
  483. < st->st_size) {
  484. inode->linkname[0] = 0;
  485. g_free (name);
  486. return STATUS_EOF;
  487. }
  488. inode->linkname[st->st_size] = 0; /* Linkname stored without terminating \0 !!! */
  489. CPIO_POS (super) += st->st_size;
  490. cpio_skip_padding (super);
  491. } else {
  492. CPIO_SEEK_CUR (super, st->st_size);
  493. }
  494. } /* !entry */
  495. g_free (name);
  496. return STATUS_OK;
  497. }
  498. /* Need to CPIO_SEEK_CUR to skip the file at the end of add entry!!!! */
  499. static int
  500. cpio_open_archive (struct vfs_class *me, struct vfs_s_super *super,
  501. const char *name, char *op)
  502. {
  503. int status = STATUS_START;
  504. (void) op;
  505. if (cpio_open_cpio_file (me, super, name) == -1)
  506. return -1;
  507. for (;;) {
  508. status = cpio_read_head (me, super);
  509. switch (status) {
  510. case STATUS_EOF:
  511. message (D_ERROR, MSG_ERROR, _("Unexpected end of file\n%s"), name);
  512. return 0;
  513. case STATUS_OK:
  514. continue;
  515. case STATUS_TRAIL:
  516. break;
  517. }
  518. break;
  519. }
  520. return 0;
  521. }
  522. /* Remaining functions are exactly same as for tarfs (and were in fact just copied) */
  523. static void *
  524. cpio_super_check (struct vfs_class *me, const char *archive_name, char *op)
  525. {
  526. static struct stat sb;
  527. (void) me;
  528. (void) op;
  529. if (mc_stat (archive_name, &sb))
  530. return NULL;
  531. return &sb;
  532. }
  533. static int
  534. cpio_super_same (struct vfs_class *me, struct vfs_s_super *parc,
  535. const char *archive_name, char *op, void *cookie)
  536. {
  537. struct stat *archive_stat = cookie; /* stat of main archive */
  538. (void) me;
  539. (void) op;
  540. if (strcmp (parc->name, archive_name))
  541. return 0;
  542. /* Has the cached archive been changed on the disk? */
  543. if (parc->u.arch.st.st_mtime < archive_stat->st_mtime) {
  544. /* Yes, reload! */
  545. (*vfs_cpiofs_ops.free) ((vfsid) parc);
  546. vfs_rmstamp (&vfs_cpiofs_ops, (vfsid) parc);
  547. return 2;
  548. }
  549. /* Hasn't been modified, give it a new timeout */
  550. vfs_stamp (&vfs_cpiofs_ops, (vfsid) parc);
  551. return 1;
  552. }
  553. static ssize_t cpio_read(void *fh, char *buffer, int count)
  554. {
  555. off_t begin = FH->ino->data_offset;
  556. int fd = FH_SUPER->u.arch.fd;
  557. struct vfs_class *me = FH_SUPER->me;
  558. if (mc_lseek (fd, begin + FH->pos, SEEK_SET) !=
  559. begin + FH->pos) ERRNOR (EIO, -1);
  560. count = MIN(count, FH->ino->st.st_size - FH->pos);
  561. if ((count = mc_read (fd, buffer, count)) == -1) ERRNOR (errno, -1);
  562. FH->pos += count;
  563. return count;
  564. }
  565. static int cpio_fh_open(struct vfs_class *me, struct vfs_s_fh *fh, int flags, int mode)
  566. {
  567. (void) fh;
  568. (void) mode;
  569. if ((flags & O_ACCMODE) != O_RDONLY) ERRNOR (EROFS, -1);
  570. return 0;
  571. }
  572. void
  573. init_cpiofs (void)
  574. {
  575. static struct vfs_s_subclass cpio_subclass;
  576. cpio_subclass.flags = VFS_S_READONLY;
  577. cpio_subclass.archive_check = cpio_super_check;
  578. cpio_subclass.archive_same = cpio_super_same;
  579. cpio_subclass.open_archive = cpio_open_archive;
  580. cpio_subclass.free_archive = cpio_free_archive;
  581. cpio_subclass.fh_open = cpio_fh_open;
  582. vfs_s_init_class (&vfs_cpiofs_ops, &cpio_subclass);
  583. vfs_cpiofs_ops.name = "cpiofs";
  584. vfs_cpiofs_ops.prefix = "ucpio";
  585. vfs_cpiofs_ops.read = cpio_read;
  586. vfs_cpiofs_ops.setctl = NULL;
  587. vfs_register_class (&vfs_cpiofs_ops);
  588. }