2 Virtual File System: GNU Tar file system.
4 Copyright (C) 2000-2018
5 Free Software Foundation, Inc.
9 Slava Zanko <slavazanko@gmail.com>, 2013
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * \brief Source: Virtual File System: GNU Tar file system.
36 #include <sys/types.h>
39 #include "lib/global.h"
40 #include "lib/unixcompat.h"
42 #include "lib/widget.h" /* message() */
44 #include "lib/vfs/vfs.h"
45 #include "lib/vfs/utilvfs.h"
46 #include "lib/vfs/xdirentry.h"
47 #include "lib/vfs/gc.h" /* vfs_rmstamp */
51 /*** global variables ****************************************************************************/
53 /*** file scope macro definitions ****************************************************************/
55 #define CPIO_POS(super) cpio_position
56 /* If some time reentrancy should be needed change it to */
57 /* #define CPIO_POS(super) (super)->u.arch.fd */
59 #define CPIO_SEEK_SET(super, where) \
60 mc_lseek (((cpio_super_data_t *)(super)->data)->fd, \
61 CPIO_POS(super) = (where), SEEK_SET)
62 #define CPIO_SEEK_CUR(super, where) \
63 mc_lseek (((cpio_super_data_t *)(super)->data)->fd, \
64 CPIO_POS(super) += (where), SEEK_SET)
66 #define MAGIC_LENGTH (6) /* How many bytes we have to read ahead */
67 #define SEEKBACK CPIO_SEEK_CUR(super, ptr - top)
68 #define RETURN(x) return (((cpio_super_data_t *)super->data)->type = (x))
70 ((((cpio_super_data_t *)super->data)->type == CPIO_UNKNOWN) || \
71 (((cpio_super_data_t *)super->data)->type == (x)))
73 #define HEAD_LENGTH (26)
75 /*** file scope type declarations ****************************************************************/
88 CPIO_UNKNOWN
= 0, /* Not determined yet */
89 CPIO_BIN
, /* Binary format */
90 CPIO_BINRE
, /* Binary format, reverse endianity */
91 CPIO_OLDC
, /* Old ASCII format */
92 CPIO_NEWC
, /* New ASCII format */
93 CPIO_CRC
/* New ASCII format + CRC */
96 struct old_cpio_header
98 unsigned short c_magic
;
100 unsigned short c_ino
;
101 unsigned short c_mode
;
102 unsigned short c_uid
;
103 unsigned short c_gid
;
104 unsigned short c_nlink
;
106 unsigned short c_mtimes
[2];
107 unsigned short c_namesize
;
108 unsigned short c_filesizes
[2];
111 struct new_cpio_header
113 unsigned short c_magic
;
115 unsigned long c_mode
;
118 unsigned long c_nlink
;
119 unsigned long c_mtime
;
120 unsigned long c_filesize
;
125 unsigned long c_namesize
;
126 unsigned long c_chksum
;
131 unsigned long inumber
;
133 struct vfs_s_inode
*inode
;
140 int type
; /* Type of the archive */
141 GSList
*deferred
; /* List of inodes for which another entries may appear */
144 /*** file scope variables ************************************************************************/
146 static struct vfs_s_subclass cpio_subclass
;
147 static struct vfs_class
*vfs_cpiofs_ops
= (struct vfs_class
*) &cpio_subclass
;
149 static off_t cpio_position
;
151 /*** file scope functions ************************************************************************/
152 /* --------------------------------------------------------------------------------------------- */
154 static ssize_t
cpio_find_head (struct vfs_class
*me
, struct vfs_s_super
*super
);
155 static ssize_t
cpio_read_bin_head (struct vfs_class
*me
, struct vfs_s_super
*super
);
156 static ssize_t
cpio_read_oldc_head (struct vfs_class
*me
, struct vfs_s_super
*super
);
157 static ssize_t
cpio_read_crc_head (struct vfs_class
*me
, struct vfs_s_super
*super
);
158 static ssize_t
cpio_read (void *fh
, char *buffer
, size_t count
);
160 /* --------------------------------------------------------------------------------------------- */
163 cpio_defer_find (const void *a
, const void *b
)
165 const defer_inode
*a1
= (const defer_inode
*) a
;
166 const defer_inode
*b1
= (const defer_inode
*) b
;
168 return (a1
->inumber
== b1
->inumber
&& a1
->device
== b1
->device
) ? 0 : 1;
171 /* --------------------------------------------------------------------------------------------- */
174 cpio_skip_padding (struct vfs_s_super
*super
)
176 switch (((cpio_super_data_t
*) super
->data
)->type
)
180 return CPIO_SEEK_CUR (super
, (2 - (CPIO_POS (super
) % 2)) % 2);
183 return CPIO_SEEK_CUR (super
, (4 - (CPIO_POS (super
) % 4)) % 4);
185 return CPIO_POS (super
);
187 g_assert_not_reached ();
188 return 42; /* & the compiler is happy :-) */
192 /* --------------------------------------------------------------------------------------------- */
195 cpio_free_archive (struct vfs_class
*me
, struct vfs_s_super
*super
)
197 cpio_super_data_t
*arch
= (cpio_super_data_t
*) super
->data
;
201 if (super
->data
== NULL
)
207 g_slist_free_full (arch
->deferred
, g_free
);
208 arch
->deferred
= NULL
;
209 MC_PTR_FREE (super
->data
);
212 /* --------------------------------------------------------------------------------------------- */
215 cpio_open_cpio_file (struct vfs_class
*me
, struct vfs_s_super
*super
, const vfs_path_t
* vpath
)
218 cpio_super_data_t
*arch
;
220 struct vfs_s_inode
*root
;
222 fd
= mc_open (vpath
, O_RDONLY
);
225 message (D_ERROR
, MSG_ERROR
, _("Cannot open cpio archive\n%s"), vfs_path_as_str (vpath
));
229 super
->name
= g_strdup (vfs_path_as_str (vpath
));
230 super
->data
= g_new (cpio_super_data_t
, 1);
231 arch
= (cpio_super_data_t
*) super
->data
;
232 arch
->fd
= -1; /* for now */
233 mc_stat (vpath
, &arch
->st
);
234 arch
->type
= CPIO_UNKNOWN
;
235 arch
->deferred
= NULL
;
237 type
= get_compression_type (fd
, super
->name
);
238 if (type
== COMPRESSION_NONE
)
239 mc_lseek (fd
, 0, SEEK_SET
);
243 vfs_path_t
*tmp_vpath
;
246 s
= g_strconcat (super
->name
, decompress_extension (type
), (char *) NULL
);
247 tmp_vpath
= vfs_path_from_str_flags (s
, VPF_NO_CANON
);
248 fd
= mc_open (tmp_vpath
, O_RDONLY
);
249 vfs_path_free (tmp_vpath
);
252 message (D_ERROR
, MSG_ERROR
, _("Cannot open cpio archive\n%s"), s
);
254 MC_PTR_FREE (super
->name
);
261 mode
= arch
->st
.st_mode
& 07777;
262 mode
|= (mode
& 0444) >> 2; /* set eXec where Read is */
265 root
= vfs_s_new_inode (me
, super
, &arch
->st
);
266 root
->st
.st_mode
= mode
;
267 root
->data_offset
= -1;
269 root
->st
.st_dev
= MEDATA
->rdev
++;
273 CPIO_SEEK_SET (super
, 0);
278 /* --------------------------------------------------------------------------------------------- */
281 cpio_read_head (struct vfs_class
*me
, struct vfs_s_super
*super
)
283 switch (cpio_find_head (me
, super
))
289 return cpio_read_bin_head (me
, super
);
291 return cpio_read_oldc_head (me
, super
);
294 return cpio_read_crc_head (me
, super
);
296 g_assert_not_reached ();
297 return 42; /* & the compiler is happy :-) */
301 /* --------------------------------------------------------------------------------------------- */
304 cpio_find_head (struct vfs_class
*me
, struct vfs_s_super
*super
)
306 cpio_super_data_t
*arch
= (cpio_super_data_t
*) super
->data
;
307 char buf
[BUF_SMALL
* 2];
312 top
= mc_read (arch
->fd
, buf
, sizeof (buf
));
314 CPIO_POS (super
) += top
;
318 if (ptr
+ MAGIC_LENGTH
>= top
)
320 if (top
> (ssize_t
) (sizeof (buf
) / 2))
322 memmove (buf
, buf
+ top
- sizeof (buf
) / 2, sizeof (buf
) / 2);
323 ptr
-= top
- sizeof (buf
) / 2;
324 top
= sizeof (buf
) / 2;
326 tmp
= mc_read (arch
->fd
, buf
, top
);
327 if (tmp
== 0 || tmp
== -1)
329 message (D_ERROR
, MSG_ERROR
, _("Premature end of cpio archive\n%s"), super
->name
);
330 cpio_free_archive (me
, super
);
335 if (TYPEIS (CPIO_BIN
) && ((*(unsigned short *) (buf
+ ptr
)) == 070707))
340 else if (TYPEIS (CPIO_BINRE
)
341 && ((*(unsigned short *) (buf
+ ptr
)) == GUINT16_SWAP_LE_BE_CONSTANT (070707)))
346 else if (TYPEIS (CPIO_OLDC
) && (strncmp (buf
+ ptr
, "070707", 6) == 0))
351 else if (TYPEIS (CPIO_NEWC
) && (strncmp (buf
+ ptr
, "070701", 6) == 0))
356 else if (TYPEIS (CPIO_CRC
) && (strncmp (buf
+ ptr
, "070702", 6) == 0))
365 /* --------------------------------------------------------------------------------------------- */
368 cpio_create_entry (struct vfs_class
*me
, struct vfs_s_super
*super
, struct stat
*st
, char *name
)
370 cpio_super_data_t
*arch
= (cpio_super_data_t
*) super
->data
;
371 struct vfs_s_inode
*inode
= NULL
;
372 struct vfs_s_inode
*root
= super
->root
;
373 struct vfs_s_entry
*entry
= NULL
;
376 switch (st
->st_mode
& S_IFMT
)
377 { /* For case of HP/UX archives */
381 /* cppcheck-suppress syntaxError */
385 /* cppcheck-suppress syntaxError */
389 /* cppcheck-suppress syntaxError */
392 #ifdef HAVE_STRUCT_STAT_ST_RDEV
393 if ((st
->st_size
!= 0) && (st
->st_rdev
== 0x0001))
395 /* FIXME: representation of major/minor differs between */
396 /* different operating systems. */
397 st
->st_rdev
= (unsigned) st
->st_size
;
406 if ((st
->st_nlink
> 1) && ((arch
->type
== CPIO_NEWC
) || (arch
->type
== CPIO_CRC
)))
407 { /* For case of hardlinked files */
408 defer_inode i
= { st
->st_ino
, st
->st_dev
, NULL
};
411 l
= g_slist_find_custom (arch
->deferred
, &i
, cpio_defer_find
);
414 inode
= ((defer_inode
*) l
->data
)->inode
;
415 if (inode
->st
.st_size
!= 0 && st
->st_size
!= 0 && (inode
->st
.st_size
!= st
->st_size
))
417 message (D_ERROR
, MSG_ERROR
,
418 _("Inconsistent hardlinks of\n%s\nin cpio archive\n%s"),
422 else if (inode
->st
.st_size
== 0)
423 inode
->st
.st_size
= st
->st_size
;
427 /* remove trailing slashes */
428 for (tn
= name
+ strlen (name
) - 1; tn
>= name
&& IS_PATH_SEP (*tn
); tn
--)
431 tn
= strrchr (name
, PATH_SEP
);
434 else if (tn
== name
+ 1)
436 /* started with "./" -- directory in the root of archive */
442 root
= vfs_s_find_inode (me
, super
, name
, LINK_FOLLOW
, FL_MKDIR
);
447 entry
= MEDATA
->find_entry (me
, root
, tn
, LINK_FOLLOW
, FL_NONE
); /* In case entry is already there */
451 /* This shouldn't happen! (well, it can happen if there is a record for a
452 file and than a record for a directory it is in; cpio would die with
453 'No such file or directory' is such case) */
455 if (!S_ISDIR (entry
->ino
->st
.st_mode
))
457 /* This can be considered archive inconsistency */
458 message (D_ERROR
, MSG_ERROR
,
459 _("%s contains duplicate entries! Skipping!"), super
->name
);
463 entry
->ino
->st
.st_mode
= st
->st_mode
;
464 entry
->ino
->st
.st_uid
= st
->st_uid
;
465 entry
->ino
->st
.st_gid
= st
->st_gid
;
466 #ifdef HAVE_STRUCT_STAT_ST_MTIM
467 entry
->ino
->st
.st_atim
= st
->st_atim
;
468 entry
->ino
->st
.st_mtim
= st
->st_mtim
;
469 entry
->ino
->st
.st_ctim
= st
->st_ctim
;
471 entry
->ino
->st
.st_atime
= st
->st_atime
;
472 entry
->ino
->st
.st_mtime
= st
->st_mtime
;
473 entry
->ino
->st
.st_ctime
= st
->st_ctime
;
481 /* root == NULL can be in the following case:
483 * where 'a/b' is the stale link and therefore root of 'c' cannot be found in the archive
489 inode
= vfs_s_new_inode (me
, super
, st
);
490 if ((st
->st_nlink
> 0) && ((arch
->type
== CPIO_NEWC
) || (arch
->type
== CPIO_CRC
)))
492 /* For case of hardlinked files */
495 i
= g_new (defer_inode
, 1);
496 i
->inumber
= st
->st_ino
;
497 i
->device
= st
->st_dev
;
500 arch
->deferred
= g_slist_prepend (arch
->deferred
, i
);
504 if (st
->st_size
!= 0)
505 inode
->data_offset
= CPIO_POS (super
);
507 entry
= vfs_s_new_entry (me
, tn
, inode
);
508 vfs_s_insert_entry (me
, root
, entry
);
513 if (!S_ISLNK (st
->st_mode
))
514 CPIO_SEEK_CUR (super
, st
->st_size
);
519 /* FIXME: do we must read from arch->fd in case of inode != NULL only or in any case? */
521 inode
->linkname
= g_malloc (st
->st_size
+ 1);
523 if (mc_read (arch
->fd
, inode
->linkname
, st
->st_size
) < st
->st_size
)
525 inode
->linkname
[0] = '\0';
529 inode
->linkname
[st
->st_size
] = '\0'; /* Linkname stored without terminating \0 !!! */
532 CPIO_POS (super
) += st
->st_size
;
533 cpio_skip_padding (super
);
540 /* --------------------------------------------------------------------------------------------- */
543 cpio_read_bin_head (struct vfs_class
*me
, struct vfs_s_super
*super
)
547 struct old_cpio_header buf
;
548 short shorts
[HEAD_LENGTH
>> 1];
551 cpio_super_data_t
*arch
= (cpio_super_data_t
*) super
->data
;
556 len
= mc_read (arch
->fd
, (char *) &u
.buf
, HEAD_LENGTH
);
557 if (len
< HEAD_LENGTH
)
559 CPIO_POS (super
) += len
;
560 if (arch
->type
== CPIO_BINRE
)
563 for (i
= 0; i
< (HEAD_LENGTH
>> 1); i
++)
564 u
.shorts
[i
] = GUINT16_SWAP_LE_BE_CONSTANT (u
.shorts
[i
]);
567 if (u
.buf
.c_magic
!= 070707 || u
.buf
.c_namesize
== 0 || u
.buf
.c_namesize
> MC_MAXPATHLEN
)
569 message (D_ERROR
, MSG_ERROR
, _("Corrupted cpio header encountered in\n%s"), super
->name
);
572 name
= g_malloc (u
.buf
.c_namesize
);
573 len
= mc_read (arch
->fd
, name
, u
.buf
.c_namesize
);
574 if (len
< u
.buf
.c_namesize
)
579 name
[u
.buf
.c_namesize
- 1] = '\0';
580 CPIO_POS (super
) += len
;
581 cpio_skip_padding (super
);
583 if (!strcmp ("TRAILER!!!", name
))
584 { /* We got to the last record */
589 st
.st_dev
= u
.buf
.c_dev
;
590 st
.st_ino
= u
.buf
.c_ino
;
591 st
.st_mode
= u
.buf
.c_mode
;
592 st
.st_nlink
= u
.buf
.c_nlink
;
593 st
.st_uid
= u
.buf
.c_uid
;
594 st
.st_gid
= u
.buf
.c_gid
;
595 #ifdef HAVE_STRUCT_STAT_ST_RDEV
596 st
.st_rdev
= u
.buf
.c_rdev
;
598 st
.st_size
= (u
.buf
.c_filesizes
[0] << 16) | u
.buf
.c_filesizes
[1];
599 #ifdef HAVE_STRUCT_STAT_ST_MTIM
600 st
.st_atim
.tv_nsec
= st
.st_mtim
.tv_nsec
= st
.st_ctim
.tv_nsec
= 0;
602 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= (u
.buf
.c_mtimes
[0] << 16) | u
.buf
.c_mtimes
[1];
604 return cpio_create_entry (me
, super
, &st
, name
);
607 /* --------------------------------------------------------------------------------------------- */
610 #define HEAD_LENGTH (76)
613 cpio_read_oldc_head (struct vfs_class
*me
, struct vfs_s_super
*super
)
615 cpio_super_data_t
*arch
= (cpio_super_data_t
*) super
->data
;
616 struct new_cpio_header hd
;
620 char buf
[HEAD_LENGTH
+ 1];
625 if (mc_read (arch
->fd
, u
.buf
, HEAD_LENGTH
) != HEAD_LENGTH
)
627 CPIO_POS (super
) += HEAD_LENGTH
;
628 u
.buf
[HEAD_LENGTH
] = 0;
630 if (sscanf (u
.buf
, "070707%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6lo%11lo",
631 (unsigned long *) &hd
.c_dev
, &hd
.c_ino
, &hd
.c_mode
, &hd
.c_uid
, &hd
.c_gid
,
632 &hd
.c_nlink
, (unsigned long *) &hd
.c_rdev
, &hd
.c_mtime
,
633 &hd
.c_namesize
, &hd
.c_filesize
) < 10)
635 message (D_ERROR
, MSG_ERROR
, _("Corrupted cpio header encountered in\n%s"), super
->name
);
639 if (hd
.c_namesize
== 0 || hd
.c_namesize
> MC_MAXPATHLEN
)
641 message (D_ERROR
, MSG_ERROR
, _("Corrupted cpio header encountered in\n%s"), super
->name
);
644 name
= g_malloc (hd
.c_namesize
);
645 len
= mc_read (arch
->fd
, name
, hd
.c_namesize
);
646 if ((len
== -1) || ((unsigned long) len
< hd
.c_namesize
))
651 name
[hd
.c_namesize
- 1] = '\0';
652 CPIO_POS (super
) += len
;
653 cpio_skip_padding (super
);
655 if (!strcmp ("TRAILER!!!", name
))
656 { /* We got to the last record */
661 u
.st
.st_dev
= hd
.c_dev
;
662 u
.st
.st_ino
= hd
.c_ino
;
663 u
.st
.st_mode
= hd
.c_mode
;
664 u
.st
.st_nlink
= hd
.c_nlink
;
665 u
.st
.st_uid
= hd
.c_uid
;
666 u
.st
.st_gid
= hd
.c_gid
;
667 #ifdef HAVE_STRUCT_STAT_ST_RDEV
668 u
.st
.st_rdev
= hd
.c_rdev
;
670 u
.st
.st_size
= hd
.c_filesize
;
671 #ifdef HAVE_STRUCT_STAT_ST_MTIM
672 u
.st
.st_atim
.tv_nsec
= u
.st
.st_mtim
.tv_nsec
= u
.st
.st_ctim
.tv_nsec
= 0;
674 u
.st
.st_atime
= u
.st
.st_mtime
= u
.st
.st_ctime
= hd
.c_mtime
;
676 return cpio_create_entry (me
, super
, &u
.st
, name
);
679 /* --------------------------------------------------------------------------------------------- */
682 #define HEAD_LENGTH (110)
685 cpio_read_crc_head (struct vfs_class
*me
, struct vfs_s_super
*super
)
687 cpio_super_data_t
*arch
= (cpio_super_data_t
*) super
->data
;
688 struct new_cpio_header hd
;
692 char buf
[HEAD_LENGTH
+ 1];
697 if (mc_read (arch
->fd
, u
.buf
, HEAD_LENGTH
) != HEAD_LENGTH
)
700 CPIO_POS (super
) += HEAD_LENGTH
;
701 u
.buf
[HEAD_LENGTH
] = '\0';
703 if (sscanf (u
.buf
, "%6ho%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx",
704 &hd
.c_magic
, &hd
.c_ino
, &hd
.c_mode
, &hd
.c_uid
, &hd
.c_gid
,
705 &hd
.c_nlink
, &hd
.c_mtime
, &hd
.c_filesize
,
706 (unsigned long *) &hd
.c_dev
, (unsigned long *) &hd
.c_devmin
,
707 (unsigned long *) &hd
.c_rdev
, (unsigned long *) &hd
.c_rdevmin
,
708 &hd
.c_namesize
, &hd
.c_chksum
) < 14)
710 message (D_ERROR
, MSG_ERROR
, _("Corrupted cpio header encountered in\n%s"), super
->name
);
714 if ((arch
->type
== CPIO_NEWC
&& hd
.c_magic
!= 070701) ||
715 (arch
->type
== CPIO_CRC
&& hd
.c_magic
!= 070702))
718 if (hd
.c_namesize
== 0 || hd
.c_namesize
> MC_MAXPATHLEN
)
720 message (D_ERROR
, MSG_ERROR
, _("Corrupted cpio header encountered in\n%s"), super
->name
);
724 name
= g_malloc (hd
.c_namesize
);
725 len
= mc_read (arch
->fd
, name
, hd
.c_namesize
);
727 if ((len
== -1) || ((unsigned long) len
< hd
.c_namesize
))
732 name
[hd
.c_namesize
- 1] = '\0';
733 CPIO_POS (super
) += len
;
734 cpio_skip_padding (super
);
736 if (strcmp ("TRAILER!!!", name
) == 0)
737 { /* We got to the last record */
742 u
.st
.st_dev
= makedev (hd
.c_dev
, hd
.c_devmin
);
743 u
.st
.st_ino
= hd
.c_ino
;
744 u
.st
.st_mode
= hd
.c_mode
;
745 u
.st
.st_nlink
= hd
.c_nlink
;
746 u
.st
.st_uid
= hd
.c_uid
;
747 u
.st
.st_gid
= hd
.c_gid
;
748 #ifdef HAVE_STRUCT_STAT_ST_RDEV
749 u
.st
.st_rdev
= makedev (hd
.c_rdev
, hd
.c_rdevmin
);
751 u
.st
.st_size
= hd
.c_filesize
;
752 #ifdef HAVE_STRUCT_STAT_ST_MTIM
753 u
.st
.st_atim
.tv_nsec
= u
.st
.st_mtim
.tv_nsec
= u
.st
.st_ctim
.tv_nsec
= 0;
755 u
.st
.st_atime
= u
.st
.st_mtime
= u
.st
.st_ctime
= hd
.c_mtime
;
757 return cpio_create_entry (me
, super
, &u
.st
, name
);
760 /* --------------------------------------------------------------------------------------------- */
761 /** Need to CPIO_SEEK_CUR to skip the file at the end of add entry!!!! */
764 cpio_open_archive (struct vfs_s_super
*super
, const vfs_path_t
* vpath
,
765 const vfs_path_element_t
* vpath_element
)
767 (void) vpath_element
;
769 if (cpio_open_cpio_file (vpath_element
->class, super
, vpath
) == -1)
776 status
= cpio_read_head (vpath_element
->class, super
);
784 message (D_ERROR
, MSG_ERROR
, _("Unexpected end of file\n%s"),
785 vfs_path_as_str (vpath
));
801 /* --------------------------------------------------------------------------------------------- */
802 /** Remaining functions are exactly same as for tarfs (and were in fact just copied) */
805 cpio_super_check (const vfs_path_t
* vpath
)
807 static struct stat sb
;
810 stat_result
= mc_stat (vpath
, &sb
);
811 return (stat_result
== 0 ? &sb
: NULL
);
814 /* --------------------------------------------------------------------------------------------- */
817 cpio_super_same (const vfs_path_element_t
* vpath_element
, struct vfs_s_super
*parc
,
818 const vfs_path_t
* vpath
, void *cookie
)
820 struct stat
*archive_stat
= cookie
; /* stat of main archive */
822 (void) vpath_element
;
824 if (strcmp (parc
->name
, vfs_path_as_str (vpath
)))
827 /* Has the cached archive been changed on the disk? */
828 if (parc
->data
!= NULL
829 && ((cpio_super_data_t
*) parc
->data
)->st
.st_mtime
< archive_stat
->st_mtime
)
832 vfs_cpiofs_ops
->free ((vfsid
) parc
);
833 vfs_rmstamp (vfs_cpiofs_ops
, (vfsid
) parc
);
836 /* Hasn't been modified, give it a new timeout */
837 vfs_stamp (vfs_cpiofs_ops
, (vfsid
) parc
);
841 /* --------------------------------------------------------------------------------------------- */
844 cpio_read (void *fh
, char *buffer
, size_t count
)
846 off_t begin
= FH
->ino
->data_offset
;
847 int fd
= ((cpio_super_data_t
*) FH_SUPER
->data
)->fd
;
848 struct vfs_class
*me
= FH_SUPER
->me
;
851 if (mc_lseek (fd
, begin
+ FH
->pos
, SEEK_SET
) != begin
+ FH
->pos
)
854 count
= MIN (count
, (size_t) (FH
->ino
->st
.st_size
- FH
->pos
));
856 res
= mc_read (fd
, buffer
, count
);
864 /* --------------------------------------------------------------------------------------------- */
867 cpio_fh_open (struct vfs_class
*me
, vfs_file_handler_t
* fh
, int flags
, mode_t mode
)
873 if ((flags
& O_ACCMODE
) != O_RDONLY
)
878 /* --------------------------------------------------------------------------------------------- */
879 /*** public functions ****************************************************************************/
880 /* --------------------------------------------------------------------------------------------- */
885 cpio_subclass
.flags
= VFS_S_READONLY
; /* FIXME: cpiofs used own temp files */
886 cpio_subclass
.archive_check
= cpio_super_check
;
887 cpio_subclass
.archive_same
= cpio_super_same
;
888 cpio_subclass
.open_archive
= cpio_open_archive
;
889 cpio_subclass
.free_archive
= cpio_free_archive
;
890 cpio_subclass
.fh_open
= cpio_fh_open
;
892 vfs_s_init_class (&cpio_subclass
);
893 vfs_cpiofs_ops
->name
= "cpiofs";
894 vfs_cpiofs_ops
->prefix
= "ucpio";
895 vfs_cpiofs_ops
->read
= cpio_read
;
896 vfs_cpiofs_ops
->setctl
= NULL
;
897 vfs_register_class (vfs_cpiofs_ops
);
900 /* --------------------------------------------------------------------------------------------- */