Changed interface of mc_stat() and mc_lstat() functions
[midnight-commander.git] / src / vfs / cpio / cpio.c
blob43541218040c81113a18c87307b1721d68558985
1 /*
2 Virtual File System: GNU Tar file system.
4 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2011
5 The Free Software Foundation, Inc.
7 Written by:
8 Jan Hudec, 2000
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /** \file
27 * \brief Source: Virtual File System: GNU Tar file system.
28 * \author Jan Hudec
29 * \date 2000
32 #include <config.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
40 #include "lib/global.h"
41 #include "lib/unixcompat.h"
42 #include "lib/util.h"
43 #include "lib/widget.h" /* message() */
45 #include "lib/vfs/vfs.h"
46 #include "lib/vfs/utilvfs.h"
47 #include "lib/vfs/xdirentry.h"
48 #include "lib/vfs/gc.h" /* vfs_rmstamp */
50 #include "cpio.h"
52 /*** global variables ****************************************************************************/
54 /*** file scope macro definitions ****************************************************************/
56 #define CPIO_POS(super) cpio_position
57 /* If some time reentrancy should be needed change it to */
58 /* #define CPIO_POS(super) (super)->u.arch.fd */
60 #define CPIO_SEEK_SET(super, where) \
61 mc_lseek (((cpio_super_data_t *)(super)->data)->fd, \
62 CPIO_POS(super) = (where), SEEK_SET)
63 #define CPIO_SEEK_CUR(super, where) \
64 mc_lseek (((cpio_super_data_t *)(super)->data)->fd, \
65 CPIO_POS(super) += (where), SEEK_SET)
67 #define MAGIC_LENGTH (6) /* How many bytes we have to read ahead */
68 #define SEEKBACK CPIO_SEEK_CUR(super, ptr - top)
69 #define RETURN(x) return (((cpio_super_data_t *)super->data)->type = (x))
70 #define TYPEIS(x) \
71 ((((cpio_super_data_t *)super->data)->type == CPIO_UNKNOWN) || \
72 (((cpio_super_data_t *)super->data)->type == (x)))
74 #define HEAD_LENGTH (26)
76 /*** file scope type declarations ****************************************************************/
78 enum
80 STATUS_START,
81 STATUS_OK,
82 STATUS_TRAIL,
83 STATUS_FAIL,
84 STATUS_EOF
87 enum
89 CPIO_UNKNOWN = 0, /* Not determined yet */
90 CPIO_BIN, /* Binary format */
91 CPIO_BINRE, /* Binary format, reverse endianity */
92 CPIO_OLDC, /* Old ASCII format */
93 CPIO_NEWC, /* New ASCII format */
94 CPIO_CRC /* New ASCII format + CRC */
97 struct old_cpio_header
99 unsigned short c_magic;
100 short c_dev;
101 unsigned short c_ino;
102 unsigned short c_mode;
103 unsigned short c_uid;
104 unsigned short c_gid;
105 unsigned short c_nlink;
106 short c_rdev;
107 unsigned short c_mtimes[2];
108 unsigned short c_namesize;
109 unsigned short c_filesizes[2];
112 struct new_cpio_header
114 unsigned short c_magic;
115 unsigned long c_ino;
116 unsigned long c_mode;
117 unsigned long c_uid;
118 unsigned long c_gid;
119 unsigned long c_nlink;
120 unsigned long c_mtime;
121 unsigned long c_filesize;
122 long c_dev;
123 long c_devmin;
124 long c_rdev;
125 long c_rdevmin;
126 unsigned long c_namesize;
127 unsigned long c_chksum;
130 typedef struct
132 unsigned long inumber;
133 unsigned short device;
134 struct vfs_s_inode *inode;
135 } defer_inode;
137 typedef struct
139 int fd;
140 struct stat st;
141 int type; /* Type of the archive */
142 GSList *deferred; /* List of inodes for which another entries may appear */
143 } cpio_super_data_t;
145 /*** file scope variables ************************************************************************/
147 static struct vfs_class vfs_cpiofs_ops;
149 /* FIXME: should be off_t instead of int. */
150 static int cpio_position;
152 /*** file scope functions ************************************************************************/
153 /* --------------------------------------------------------------------------------------------- */
155 static int cpio_find_head (struct vfs_class *me, struct vfs_s_super *super);
156 static ssize_t cpio_read_bin_head (struct vfs_class *me, struct vfs_s_super *super);
157 static ssize_t cpio_read_oldc_head (struct vfs_class *me, struct vfs_s_super *super);
158 static ssize_t cpio_read_crc_head (struct vfs_class *me, struct vfs_s_super *super);
159 static ssize_t cpio_read (void *fh, char *buffer, size_t count);
161 /* --------------------------------------------------------------------------------------------- */
163 static int
164 cpio_defer_find (const void *a, const void *b)
166 const defer_inode *a1 = (const defer_inode *) a;
167 const defer_inode *b1 = (const defer_inode *) b;
169 return (a1->inumber == b1->inumber && a1->device == b1->device) ? 0 : 1;
172 /* --------------------------------------------------------------------------------------------- */
174 static int
175 cpio_skip_padding (struct vfs_s_super *super)
177 switch (((cpio_super_data_t *) super->data)->type)
179 case CPIO_BIN:
180 case CPIO_BINRE:
181 return CPIO_SEEK_CUR (super, (2 - (CPIO_POS (super) % 2)) % 2);
182 case CPIO_NEWC:
183 case CPIO_CRC:
184 return CPIO_SEEK_CUR (super, (4 - (CPIO_POS (super) % 4)) % 4);
185 case CPIO_OLDC:
186 return CPIO_POS (super);
187 default:
188 g_assert_not_reached ();
189 return 42; /* & the compiler is happy :-) */
193 /* --------------------------------------------------------------------------------------------- */
195 static void
196 cpio_free_archive (struct vfs_class *me, struct vfs_s_super *super)
198 cpio_super_data_t *arch = (cpio_super_data_t *) super->data;
200 (void) me;
202 if (super->data == NULL)
203 return;
205 if (arch->fd != -1)
206 mc_close (arch->fd);
207 arch->fd = -1;
208 g_slist_foreach (arch->deferred, (GFunc) g_free, NULL);
209 g_slist_free (arch->deferred);
210 arch->deferred = NULL;
211 g_free (super->data);
212 super->data = NULL;
215 /* --------------------------------------------------------------------------------------------- */
217 static int
218 cpio_open_cpio_file (struct vfs_class *me, struct vfs_s_super *super, const vfs_path_t * vpath)
220 int fd, type;
221 cpio_super_data_t *arch;
222 mode_t mode;
223 struct vfs_s_inode *root;
224 char *name = vfs_path_to_str (vpath);
226 fd = mc_open (name, O_RDONLY);
227 if (fd == -1)
229 message (D_ERROR, MSG_ERROR, _("Cannot open cpio archive\n%s"), name);
230 g_free (name);
231 return -1;
234 super->name = g_strdup (name);
235 super->data = g_new (cpio_super_data_t, 1);
236 arch = (cpio_super_data_t *) super->data;
237 arch->fd = -1; /* for now */
238 mc_stat (vpath, &arch->st);
239 arch->type = CPIO_UNKNOWN;
240 arch->deferred = NULL;
242 type = get_compression_type (fd, name);
243 if (type != COMPRESSION_NONE)
245 char *s;
247 mc_close (fd);
248 s = g_strconcat (name, decompress_extension (type), (char *) NULL);
249 fd = mc_open (s, O_RDONLY);
250 if (fd == -1)
252 message (D_ERROR, MSG_ERROR, _("Cannot open cpio archive\n%s"), s);
253 g_free (s);
254 g_free (name);
255 return -1;
257 g_free (s);
260 arch->fd = fd;
261 mode = arch->st.st_mode & 07777;
262 mode |= (mode & 0444) >> 2; /* set eXec where Read is */
263 mode |= S_IFDIR;
265 root = vfs_s_new_inode (me, super, &arch->st);
266 root->st.st_mode = mode;
267 root->data_offset = -1;
268 root->st.st_nlink++;
269 root->st.st_dev = MEDATA->rdev++;
271 super->root = root;
273 CPIO_SEEK_SET (super, 0);
274 g_free (name);
276 return fd;
279 /* --------------------------------------------------------------------------------------------- */
281 static ssize_t
282 cpio_read_head (struct vfs_class *me, struct vfs_s_super *super)
284 switch (cpio_find_head (me, super))
286 case CPIO_UNKNOWN:
287 return -1;
288 case CPIO_BIN:
289 case CPIO_BINRE:
290 return cpio_read_bin_head (me, super);
291 case CPIO_OLDC:
292 return cpio_read_oldc_head (me, super);
293 case CPIO_NEWC:
294 case CPIO_CRC:
295 return cpio_read_crc_head (me, super);
296 default:
297 g_assert_not_reached ();
298 return 42; /* & the compiler is happy :-) */
302 /* --------------------------------------------------------------------------------------------- */
304 static int
305 cpio_find_head (struct vfs_class *me, struct vfs_s_super *super)
307 cpio_super_data_t *arch = (cpio_super_data_t *) super->data;
308 char buf[BUF_SMALL * 2];
309 int ptr = 0;
310 ssize_t top;
311 ssize_t tmp;
313 top = mc_read (arch->fd, buf, sizeof (buf));
314 if (top > 0)
315 CPIO_POS (super) += top;
317 while (TRUE)
319 if (ptr + MAGIC_LENGTH >= top)
321 if (top > (ssize_t) (sizeof (buf) / 2))
323 memmove (buf, buf + top - sizeof (buf) / 2, sizeof (buf) / 2);
324 ptr -= top - sizeof (buf) / 2;
325 top = sizeof (buf) / 2;
327 tmp = mc_read (arch->fd, buf, top);
328 if (tmp == 0 || tmp == -1)
330 message (D_ERROR, MSG_ERROR, _("Premature end of cpio archive\n%s"), super->name);
331 cpio_free_archive (me, super);
332 return CPIO_UNKNOWN;
334 top += tmp;
336 if (TYPEIS (CPIO_BIN) && ((*(unsigned short *) (buf + ptr)) == 070707))
338 SEEKBACK;
339 RETURN (CPIO_BIN);
341 else if (TYPEIS (CPIO_BINRE)
342 && ((*(unsigned short *) (buf + ptr)) == GUINT16_SWAP_LE_BE_CONSTANT (070707)))
344 SEEKBACK;
345 RETURN (CPIO_BINRE);
347 else if (TYPEIS (CPIO_OLDC) && (strncmp (buf + ptr, "070707", 6) == 0))
349 SEEKBACK;
350 RETURN (CPIO_OLDC);
352 else if (TYPEIS (CPIO_NEWC) && (strncmp (buf + ptr, "070701", 6) == 0))
354 SEEKBACK;
355 RETURN (CPIO_NEWC);
357 else if (TYPEIS (CPIO_CRC) && (strncmp (buf + ptr, "070702", 6) == 0))
359 SEEKBACK;
360 RETURN (CPIO_CRC);
362 ptr++;
366 /* --------------------------------------------------------------------------------------------- */
368 static int
369 cpio_create_entry (struct vfs_class *me, struct vfs_s_super *super, struct stat *st, char *name)
371 cpio_super_data_t *arch = (cpio_super_data_t *) super->data;
372 struct vfs_s_inode *inode = NULL;
373 struct vfs_s_inode *root = super->root;
374 struct vfs_s_entry *entry = NULL;
375 char *tn;
377 switch (st->st_mode & S_IFMT)
378 { /* For case of HP/UX archives */
379 case S_IFCHR:
380 case S_IFBLK:
381 #ifdef S_IFSOCK
382 case S_IFSOCK:
383 #endif
384 #ifdef S_IFIFO
385 case S_IFIFO:
386 #endif
387 #ifdef S_IFNAM
388 case S_IFNAM:
389 #endif
390 if ((st->st_size != 0) && (st->st_rdev == 0x0001))
392 /* FIXME: representation of major/minor differs between */
393 /* different operating systems. */
394 st->st_rdev = (unsigned) st->st_size;
395 st->st_size = 0;
397 break;
398 default:
399 break;
402 if ((st->st_nlink > 1) && ((arch->type == CPIO_NEWC) || (arch->type == CPIO_CRC)))
403 { /* For case of hardlinked files */
404 defer_inode i = { st->st_ino, st->st_dev, NULL };
405 GSList *l;
407 l = g_slist_find_custom (arch->deferred, &i, cpio_defer_find);
408 if (l != NULL)
410 inode = ((defer_inode *) l->data)->inode;
411 if (inode->st.st_size != 0 && st->st_size != 0 && (inode->st.st_size != st->st_size))
413 message (D_ERROR, MSG_ERROR,
414 _("Inconsistent hardlinks of\n%s\nin cpio archive\n%s"),
415 name, super->name);
416 inode = NULL;
418 else if (inode->st.st_size == 0)
419 inode->st.st_size = st->st_size;
423 /* remove trailing slashes */
424 for (tn = name + strlen (name) - 1; tn >= name && *tn == PATH_SEP; tn--)
425 *tn = '\0';
427 tn = strrchr (name, PATH_SEP);
428 if (tn == NULL)
429 tn = name;
430 else if (tn == name + 1)
432 /* started with "./" -- directory in the root of archive */
433 tn++;
435 else
437 *tn = '\0';
438 root = vfs_s_find_inode (me, super, name, LINK_FOLLOW, FL_MKDIR);
439 *tn = PATH_SEP;
440 tn++;
443 entry = MEDATA->find_entry (me, root, tn, LINK_FOLLOW, FL_NONE); /* In case entry is already there */
445 if (entry != NULL)
447 /* This shouldn't happen! (well, it can happen if there is a record for a
448 file and than a record for a directory it is in; cpio would die with
449 'No such file or directory' is such case) */
451 if (!S_ISDIR (entry->ino->st.st_mode))
453 /* This can be considered archive inconsistency */
454 message (D_ERROR, MSG_ERROR,
455 _("%s contains duplicate entries! Skipping!"), super->name);
457 else
459 entry->ino->st.st_mode = st->st_mode;
460 entry->ino->st.st_uid = st->st_uid;
461 entry->ino->st.st_gid = st->st_gid;
462 entry->ino->st.st_atime = st->st_atime;
463 entry->ino->st.st_mtime = st->st_mtime;
464 entry->ino->st.st_ctime = st->st_ctime;
467 g_free (name);
469 else
470 { /* !entry */
471 if (inode == NULL)
473 inode = vfs_s_new_inode (me, super, st);
474 if ((st->st_nlink > 0) && ((arch->type == CPIO_NEWC) || (arch->type == CPIO_CRC)))
476 /* For case of hardlinked files */
477 defer_inode *i;
479 i = g_new (defer_inode, 1);
480 i->inumber = st->st_ino;
481 i->device = st->st_dev;
482 i->inode = inode;
484 arch->deferred = g_slist_prepend (arch->deferred, i);
488 if (st->st_size != 0)
489 inode->data_offset = CPIO_POS (super);
491 entry = vfs_s_new_entry (me, tn, inode);
492 vfs_s_insert_entry (me, root, entry);
494 g_free (name);
496 if (!S_ISLNK (st->st_mode))
497 CPIO_SEEK_CUR (super, st->st_size);
498 else
500 inode->linkname = g_malloc (st->st_size + 1);
502 if (mc_read (arch->fd, inode->linkname, st->st_size) < st->st_size)
504 inode->linkname[0] = '\0';
505 return STATUS_EOF;
508 inode->linkname[st->st_size] = '\0'; /* Linkname stored without terminating \0 !!! */
509 CPIO_POS (super) += st->st_size;
510 cpio_skip_padding (super);
512 } /* !entry */
514 return STATUS_OK;
517 /* --------------------------------------------------------------------------------------------- */
519 static ssize_t
520 cpio_read_bin_head (struct vfs_class *me, struct vfs_s_super *super)
522 union
524 struct old_cpio_header buf;
525 short shorts[HEAD_LENGTH >> 1];
526 } u;
528 cpio_super_data_t *arch = (cpio_super_data_t *) super->data;
529 ssize_t len;
530 char *name;
531 struct stat st;
533 len = mc_read (arch->fd, (char *) &u.buf, HEAD_LENGTH);
534 if (len < HEAD_LENGTH)
535 return STATUS_EOF;
536 CPIO_POS (super) += len;
537 if (arch->type == CPIO_BINRE)
539 int i;
540 for (i = 0; i < (HEAD_LENGTH >> 1); i++)
541 u.shorts[i] = GUINT16_SWAP_LE_BE_CONSTANT (u.shorts[i]);
544 if (u.buf.c_magic != 070707 || u.buf.c_namesize == 0 || u.buf.c_namesize > MC_MAXPATHLEN)
546 message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), super->name);
547 return STATUS_FAIL;
549 name = g_malloc (u.buf.c_namesize);
550 len = mc_read (arch->fd, name, u.buf.c_namesize);
551 if (len < u.buf.c_namesize)
553 g_free (name);
554 return STATUS_EOF;
556 name[u.buf.c_namesize - 1] = '\0';
557 CPIO_POS (super) += len;
558 cpio_skip_padding (super);
560 if (!strcmp ("TRAILER!!!", name))
561 { /* We got to the last record */
562 g_free (name);
563 return STATUS_TRAIL;
566 st.st_dev = u.buf.c_dev;
567 st.st_ino = u.buf.c_ino;
568 st.st_mode = u.buf.c_mode;
569 st.st_nlink = u.buf.c_nlink;
570 st.st_uid = u.buf.c_uid;
571 st.st_gid = u.buf.c_gid;
572 st.st_rdev = u.buf.c_rdev;
573 st.st_size = (u.buf.c_filesizes[0] << 16) | u.buf.c_filesizes[1];
574 st.st_atime = st.st_mtime = st.st_ctime = (u.buf.c_mtimes[0] << 16) | u.buf.c_mtimes[1];
576 return cpio_create_entry (me, super, &st, name);
579 /* --------------------------------------------------------------------------------------------- */
581 #undef HEAD_LENGTH
582 #define HEAD_LENGTH (76)
584 static ssize_t
585 cpio_read_oldc_head (struct vfs_class *me, struct vfs_s_super *super)
587 cpio_super_data_t *arch = (cpio_super_data_t *) super->data;
588 struct new_cpio_header hd;
589 union
591 struct stat st;
592 char buf[HEAD_LENGTH + 1];
593 } u;
594 ssize_t len;
595 char *name;
597 if (mc_read (arch->fd, u.buf, HEAD_LENGTH) != HEAD_LENGTH)
598 return STATUS_EOF;
599 CPIO_POS (super) += HEAD_LENGTH;
600 u.buf[HEAD_LENGTH] = 0;
602 if (sscanf (u.buf, "070707%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6lo%11lo",
603 (unsigned long *) &hd.c_dev, &hd.c_ino, &hd.c_mode, &hd.c_uid, &hd.c_gid,
604 &hd.c_nlink, (unsigned long *) &hd.c_rdev, &hd.c_mtime,
605 &hd.c_namesize, &hd.c_filesize) < 10)
607 message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), super->name);
608 return STATUS_FAIL;
611 if (hd.c_namesize == 0 || hd.c_namesize > MC_MAXPATHLEN)
613 message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), super->name);
614 return STATUS_FAIL;
616 name = g_malloc (hd.c_namesize);
617 len = mc_read (arch->fd, name, hd.c_namesize);
618 if ((len == -1) || ((unsigned long) len < hd.c_namesize))
620 g_free (name);
621 return STATUS_EOF;
623 name[hd.c_namesize - 1] = '\0';
624 CPIO_POS (super) += len;
625 cpio_skip_padding (super);
627 if (!strcmp ("TRAILER!!!", name))
628 { /* We got to the last record */
629 g_free (name);
630 return STATUS_TRAIL;
633 u.st.st_dev = hd.c_dev;
634 u.st.st_ino = hd.c_ino;
635 u.st.st_mode = hd.c_mode;
636 u.st.st_nlink = hd.c_nlink;
637 u.st.st_uid = hd.c_uid;
638 u.st.st_gid = hd.c_gid;
639 u.st.st_rdev = hd.c_rdev;
640 u.st.st_size = hd.c_filesize;
641 u.st.st_atime = u.st.st_mtime = u.st.st_ctime = hd.c_mtime;
643 return cpio_create_entry (me, super, &u.st, name);
646 /* --------------------------------------------------------------------------------------------- */
648 #undef HEAD_LENGTH
649 #define HEAD_LENGTH (110)
651 static ssize_t
652 cpio_read_crc_head (struct vfs_class *me, struct vfs_s_super *super)
654 cpio_super_data_t *arch = (cpio_super_data_t *) super->data;
655 struct new_cpio_header hd;
656 union
658 struct stat st;
659 char buf[HEAD_LENGTH + 1];
660 } u;
661 ssize_t len;
662 char *name;
664 if (mc_read (arch->fd, u.buf, HEAD_LENGTH) != HEAD_LENGTH)
665 return STATUS_EOF;
667 CPIO_POS (super) += HEAD_LENGTH;
668 u.buf[HEAD_LENGTH] = '\0';
670 if (sscanf (u.buf, "%6ho%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx",
671 &hd.c_magic, &hd.c_ino, &hd.c_mode, &hd.c_uid, &hd.c_gid,
672 &hd.c_nlink, &hd.c_mtime, &hd.c_filesize,
673 (unsigned long *) &hd.c_dev, (unsigned long *) &hd.c_devmin,
674 (unsigned long *) &hd.c_rdev, (unsigned long *) &hd.c_rdevmin,
675 &hd.c_namesize, &hd.c_chksum) < 14)
677 message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), super->name);
678 return STATUS_FAIL;
681 if ((arch->type == CPIO_NEWC && hd.c_magic != 070701) ||
682 (arch->type == CPIO_CRC && hd.c_magic != 070702))
683 return STATUS_FAIL;
685 if (hd.c_namesize == 0 || hd.c_namesize > MC_MAXPATHLEN)
687 message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), super->name);
688 return STATUS_FAIL;
691 name = g_malloc (hd.c_namesize);
692 len = mc_read (arch->fd, name, hd.c_namesize);
694 if ((len == -1) || ((unsigned long) len < hd.c_namesize))
696 g_free (name);
697 return STATUS_EOF;
699 name[hd.c_namesize - 1] = '\0';
700 CPIO_POS (super) += len;
701 cpio_skip_padding (super);
703 if (strcmp ("TRAILER!!!", name) == 0)
704 { /* We got to the last record */
705 g_free (name);
706 return STATUS_TRAIL;
709 u.st.st_dev = makedev (hd.c_dev, hd.c_devmin);
710 u.st.st_ino = hd.c_ino;
711 u.st.st_mode = hd.c_mode;
712 u.st.st_nlink = hd.c_nlink;
713 u.st.st_uid = hd.c_uid;
714 u.st.st_gid = hd.c_gid;
715 u.st.st_rdev = makedev (hd.c_rdev, hd.c_rdevmin);
716 u.st.st_size = hd.c_filesize;
717 u.st.st_atime = u.st.st_mtime = u.st.st_ctime = hd.c_mtime;
719 return cpio_create_entry (me, super, &u.st, name);
722 /* --------------------------------------------------------------------------------------------- */
723 /** Need to CPIO_SEEK_CUR to skip the file at the end of add entry!!!! */
725 static int
726 cpio_open_archive (struct vfs_s_super *super, const vfs_path_t * vpath,
727 const vfs_path_element_t * vpath_element)
729 int status = STATUS_START;
731 (void) vpath_element;
733 if (cpio_open_cpio_file (vpath_element->class, super, vpath) == -1)
734 return -1;
736 while (TRUE)
738 status = cpio_read_head (vpath_element->class, super);
740 switch (status)
742 case STATUS_EOF:
744 char *archive_name;
746 archive_name = vfs_path_to_str (vpath);
747 message (D_ERROR, MSG_ERROR, _("Unexpected end of file\n%s"), archive_name);
748 g_free (archive_name);
749 return 0;
751 case STATUS_OK:
752 continue;
753 case STATUS_TRAIL:
754 break;
756 break;
759 return 0;
762 /* --------------------------------------------------------------------------------------------- */
763 /** Remaining functions are exactly same as for tarfs (and were in fact just copied) */
765 static void *
766 cpio_super_check (const vfs_path_t * vpath)
768 static struct stat sb;
769 int stat_result;
771 stat_result = mc_stat (vpath, &sb);
772 return (stat_result == 0 ? &sb : NULL);
775 /* --------------------------------------------------------------------------------------------- */
777 static int
778 cpio_super_same (const vfs_path_element_t * vpath_element, struct vfs_s_super *parc,
779 const vfs_path_t * vpath, void *cookie)
781 struct stat *archive_stat = cookie; /* stat of main archive */
782 char *archive_name = vfs_path_to_str (vpath);
784 (void) vpath_element;
786 if (strcmp (parc->name, archive_name))
788 g_free (archive_name);
789 return 0;
791 g_free (archive_name);
793 /* Has the cached archive been changed on the disk? */
794 if (((cpio_super_data_t *) parc->data)->st.st_mtime < archive_stat->st_mtime)
796 /* Yes, reload! */
797 (*vfs_cpiofs_ops.free) ((vfsid) parc);
798 vfs_rmstamp (&vfs_cpiofs_ops, (vfsid) parc);
799 return 2;
801 /* Hasn't been modified, give it a new timeout */
802 vfs_stamp (&vfs_cpiofs_ops, (vfsid) parc);
803 return 1;
806 /* --------------------------------------------------------------------------------------------- */
808 static ssize_t
809 cpio_read (void *fh, char *buffer, size_t count)
811 off_t begin = FH->ino->data_offset;
812 int fd = ((cpio_super_data_t *) FH_SUPER->data)->fd;
813 struct vfs_class *me = FH_SUPER->me;
814 ssize_t res;
816 if (mc_lseek (fd, begin + FH->pos, SEEK_SET) != begin + FH->pos)
817 ERRNOR (EIO, -1);
819 count = MIN (count, (size_t) (FH->ino->st.st_size - FH->pos));
821 res = mc_read (fd, buffer, count);
822 if (res == -1)
823 ERRNOR (errno, -1);
825 FH->pos += res;
826 return res;
829 /* --------------------------------------------------------------------------------------------- */
831 static int
832 cpio_fh_open (struct vfs_class *me, vfs_file_handler_t * fh, int flags, mode_t mode)
834 (void) mode;
836 fh->data = NULL;
838 if ((flags & O_ACCMODE) != O_RDONLY)
839 ERRNOR (EROFS, -1);
840 return 0;
843 /* --------------------------------------------------------------------------------------------- */
844 /*** public functions ****************************************************************************/
845 /* --------------------------------------------------------------------------------------------- */
847 void
848 init_cpiofs (void)
850 static struct vfs_s_subclass cpio_subclass;
852 cpio_subclass.flags = VFS_S_READONLY; /* FIXME: cpiofs used own temp files */
853 cpio_subclass.archive_check = cpio_super_check;
854 cpio_subclass.archive_same = cpio_super_same;
855 cpio_subclass.open_archive = cpio_open_archive;
856 cpio_subclass.free_archive = cpio_free_archive;
857 cpio_subclass.fh_open = cpio_fh_open;
859 vfs_s_init_class (&vfs_cpiofs_ops, &cpio_subclass);
860 vfs_cpiofs_ops.name = "cpiofs";
861 vfs_cpiofs_ops.prefix = "ucpio";
862 vfs_cpiofs_ops.read = cpio_read;
863 vfs_cpiofs_ops.setctl = NULL;
864 vfs_register_class (&vfs_cpiofs_ops);
867 /* --------------------------------------------------------------------------------------------- */