cpio.c 18 KB

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