cpio.c 17 KB

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