cpio.c 17 KB

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