Revert "VFS: make VFS-specific super class as derived one from vfs_s_super."
[midnight-commander.git] / src / vfs / cpio / cpio.c
blobeb3f299f4fc8a13ab49b0fd27cbd9112682e03c4
1 /*
2 Virtual File System: GNU Tar file system.
4 Copyright (C) 2000-2018
5 Free Software Foundation, Inc.
7 Written by:
8 Jan Hudec, 2000
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/>.
27 /** \file
28 * \brief Source: Virtual File System: GNU Tar file system.
29 * \author Jan Hudec
30 * \date 2000
33 #include <config.h>
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
39 #include "lib/global.h"
40 #include "lib/unixcompat.h"
41 #include "lib/util.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 */
49 #include "cpio.h"
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))
69 #define TYPEIS(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 ****************************************************************/
77 enum
79 STATUS_START,
80 STATUS_OK,
81 STATUS_TRAIL,
82 STATUS_FAIL,
83 STATUS_EOF
86 enum
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;
99 short c_dev;
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;
105 short c_rdev;
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;
114 unsigned long c_ino;
115 unsigned long c_mode;
116 unsigned long c_uid;
117 unsigned long c_gid;
118 unsigned long c_nlink;
119 unsigned long c_mtime;
120 unsigned long c_filesize;
121 long c_dev;
122 long c_devmin;
123 long c_rdev;
124 long c_rdevmin;
125 unsigned long c_namesize;
126 unsigned long c_chksum;
129 typedef struct
131 unsigned long inumber;
132 dev_t device;
133 struct vfs_s_inode *inode;
134 } defer_inode;
136 typedef struct
138 int fd;
139 struct stat st;
140 int type; /* Type of the archive */
141 GSList *deferred; /* List of inodes for which another entries may appear */
142 } cpio_super_data_t;
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 /* --------------------------------------------------------------------------------------------- */
162 static int
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 /* --------------------------------------------------------------------------------------------- */
173 static ssize_t
174 cpio_skip_padding (struct vfs_s_super *super)
176 switch (((cpio_super_data_t *) super->data)->type)
178 case CPIO_BIN:
179 case CPIO_BINRE:
180 return CPIO_SEEK_CUR (super, (2 - (CPIO_POS (super) % 2)) % 2);
181 case CPIO_NEWC:
182 case CPIO_CRC:
183 return CPIO_SEEK_CUR (super, (4 - (CPIO_POS (super) % 4)) % 4);
184 case CPIO_OLDC:
185 return CPIO_POS (super);
186 default:
187 g_assert_not_reached ();
188 return 42; /* & the compiler is happy :-) */
192 /* --------------------------------------------------------------------------------------------- */
194 static void
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;
199 (void) me;
201 if (super->data == NULL)
202 return;
204 if (arch->fd != -1)
205 mc_close (arch->fd);
206 arch->fd = -1;
207 g_slist_free_full (arch->deferred, g_free);
208 arch->deferred = NULL;
209 MC_PTR_FREE (super->data);
212 /* --------------------------------------------------------------------------------------------- */
214 static int
215 cpio_open_cpio_file (struct vfs_class *me, struct vfs_s_super *super, const vfs_path_t * vpath)
217 int fd, type;
218 cpio_super_data_t *arch;
219 mode_t mode;
220 struct vfs_s_inode *root;
222 fd = mc_open (vpath, O_RDONLY);
223 if (fd == -1)
225 message (D_ERROR, MSG_ERROR, _("Cannot open cpio archive\n%s"), vfs_path_as_str (vpath));
226 return -1;
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);
240 else
242 char *s;
243 vfs_path_t *tmp_vpath;
245 mc_close (fd);
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);
250 if (fd == -1)
252 message (D_ERROR, MSG_ERROR, _("Cannot open cpio archive\n%s"), s);
253 g_free (s);
254 MC_PTR_FREE (super->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);
275 return fd;
278 /* --------------------------------------------------------------------------------------------- */
280 static ssize_t
281 cpio_read_head (struct vfs_class *me, struct vfs_s_super *super)
283 switch (cpio_find_head (me, super))
285 case CPIO_UNKNOWN:
286 return -1;
287 case CPIO_BIN:
288 case CPIO_BINRE:
289 return cpio_read_bin_head (me, super);
290 case CPIO_OLDC:
291 return cpio_read_oldc_head (me, super);
292 case CPIO_NEWC:
293 case CPIO_CRC:
294 return cpio_read_crc_head (me, super);
295 default:
296 g_assert_not_reached ();
297 return 42; /* & the compiler is happy :-) */
301 /* --------------------------------------------------------------------------------------------- */
303 static ssize_t
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];
308 ssize_t ptr = 0;
309 ssize_t top;
310 ssize_t tmp;
312 top = mc_read (arch->fd, buf, sizeof (buf));
313 if (top > 0)
314 CPIO_POS (super) += top;
316 while (TRUE)
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);
331 return CPIO_UNKNOWN;
333 top += tmp;
335 if (TYPEIS (CPIO_BIN) && ((*(unsigned short *) (buf + ptr)) == 070707))
337 SEEKBACK;
338 RETURN (CPIO_BIN);
340 else if (TYPEIS (CPIO_BINRE)
341 && ((*(unsigned short *) (buf + ptr)) == GUINT16_SWAP_LE_BE_CONSTANT (070707)))
343 SEEKBACK;
344 RETURN (CPIO_BINRE);
346 else if (TYPEIS (CPIO_OLDC) && (strncmp (buf + ptr, "070707", 6) == 0))
348 SEEKBACK;
349 RETURN (CPIO_OLDC);
351 else if (TYPEIS (CPIO_NEWC) && (strncmp (buf + ptr, "070701", 6) == 0))
353 SEEKBACK;
354 RETURN (CPIO_NEWC);
356 else if (TYPEIS (CPIO_CRC) && (strncmp (buf + ptr, "070702", 6) == 0))
358 SEEKBACK;
359 RETURN (CPIO_CRC);
361 ptr++;
365 /* --------------------------------------------------------------------------------------------- */
367 static int
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;
374 char *tn;
376 switch (st->st_mode & S_IFMT)
377 { /* For case of HP/UX archives */
378 case S_IFCHR:
379 case S_IFBLK:
380 #ifdef S_IFSOCK
381 /* cppcheck-suppress syntaxError */
382 case S_IFSOCK:
383 #endif
384 #ifdef S_IFIFO
385 /* cppcheck-suppress syntaxError */
386 case S_IFIFO:
387 #endif
388 #ifdef S_IFNAM
389 /* cppcheck-suppress syntaxError */
390 case S_IFNAM:
391 #endif
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;
398 st->st_size = 0;
400 #endif
401 break;
402 default:
403 break;
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 };
409 GSList *l;
411 l = g_slist_find_custom (arch->deferred, &i, cpio_defer_find);
412 if (l != NULL)
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"),
419 name, super->name);
420 inode = NULL;
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--)
429 *tn = '\0';
431 tn = strrchr (name, PATH_SEP);
432 if (tn == NULL)
433 tn = name;
434 else if (tn == name + 1)
436 /* started with "./" -- directory in the root of archive */
437 tn++;
439 else
441 *tn = '\0';
442 root = vfs_s_find_inode (me, super, name, LINK_FOLLOW, FL_MKDIR);
443 *tn = PATH_SEP;
444 tn++;
447 entry = MEDATA->find_entry (me, root, tn, LINK_FOLLOW, FL_NONE); /* In case entry is already there */
449 if (entry != NULL)
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);
461 else
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;
470 #else
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;
474 #endif
477 g_free (name);
479 else
480 { /* !entry */
481 /* root == NULL can be in the following case:
482 * a/b/c -> d
483 * where 'a/b' is the stale link and therefore root of 'c' cannot be found in the archive
485 if (root != NULL)
487 if (inode == NULL)
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 */
493 defer_inode *i;
495 i = g_new (defer_inode, 1);
496 i->inumber = st->st_ino;
497 i->device = st->st_dev;
498 i->inode = inode;
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);
511 g_free (name);
513 if (!S_ISLNK (st->st_mode))
514 CPIO_SEEK_CUR (super, st->st_size);
515 else
517 if (inode != NULL)
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';
526 return STATUS_EOF;
529 inode->linkname[st->st_size] = '\0'; /* Linkname stored without terminating \0 !!! */
532 CPIO_POS (super) += st->st_size;
533 cpio_skip_padding (super);
535 } /* !entry */
537 return STATUS_OK;
540 /* --------------------------------------------------------------------------------------------- */
542 static ssize_t
543 cpio_read_bin_head (struct vfs_class *me, struct vfs_s_super *super)
545 union
547 struct old_cpio_header buf;
548 short shorts[HEAD_LENGTH >> 1];
549 } u;
551 cpio_super_data_t *arch = (cpio_super_data_t *) super->data;
552 ssize_t len;
553 char *name;
554 struct stat st;
556 len = mc_read (arch->fd, (char *) &u.buf, HEAD_LENGTH);
557 if (len < HEAD_LENGTH)
558 return STATUS_EOF;
559 CPIO_POS (super) += len;
560 if (arch->type == CPIO_BINRE)
562 int i;
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);
570 return STATUS_FAIL;
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)
576 g_free (name);
577 return STATUS_EOF;
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 */
585 g_free (name);
586 return STATUS_TRAIL;
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;
597 #endif
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;
601 #endif
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 /* --------------------------------------------------------------------------------------------- */
609 #undef HEAD_LENGTH
610 #define HEAD_LENGTH (76)
612 static ssize_t
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;
617 union
619 struct stat st;
620 char buf[HEAD_LENGTH + 1];
621 } u;
622 ssize_t len;
623 char *name;
625 if (mc_read (arch->fd, u.buf, HEAD_LENGTH) != HEAD_LENGTH)
626 return STATUS_EOF;
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);
636 return STATUS_FAIL;
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);
642 return STATUS_FAIL;
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))
648 g_free (name);
649 return STATUS_EOF;
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 */
657 g_free (name);
658 return STATUS_TRAIL;
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;
669 #endif
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;
673 #endif
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 /* --------------------------------------------------------------------------------------------- */
681 #undef HEAD_LENGTH
682 #define HEAD_LENGTH (110)
684 static ssize_t
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;
689 union
691 struct stat st;
692 char buf[HEAD_LENGTH + 1];
693 } u;
694 ssize_t len;
695 char *name;
697 if (mc_read (arch->fd, u.buf, HEAD_LENGTH) != HEAD_LENGTH)
698 return STATUS_EOF;
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);
711 return STATUS_FAIL;
714 if ((arch->type == CPIO_NEWC && hd.c_magic != 070701) ||
715 (arch->type == CPIO_CRC && hd.c_magic != 070702))
716 return STATUS_FAIL;
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);
721 return STATUS_FAIL;
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))
729 g_free (name);
730 return STATUS_EOF;
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 */
738 g_free (name);
739 return STATUS_TRAIL;
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);
750 #endif
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;
754 #endif
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!!!! */
763 static int
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)
770 return -1;
772 while (TRUE)
774 ssize_t status;
776 status = cpio_read_head (vpath_element->class, super);
777 if (status < 0)
778 return (-1);
780 switch (status)
782 case STATUS_EOF:
784 message (D_ERROR, MSG_ERROR, _("Unexpected end of file\n%s"),
785 vfs_path_as_str (vpath));
786 return 0;
788 case STATUS_OK:
789 continue;
790 case STATUS_TRAIL:
791 break;
792 default:
793 break;
795 break;
798 return 0;
801 /* --------------------------------------------------------------------------------------------- */
802 /** Remaining functions are exactly same as for tarfs (and were in fact just copied) */
804 static void *
805 cpio_super_check (const vfs_path_t * vpath)
807 static struct stat sb;
808 int stat_result;
810 stat_result = mc_stat (vpath, &sb);
811 return (stat_result == 0 ? &sb : NULL);
814 /* --------------------------------------------------------------------------------------------- */
816 static int
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)))
825 return 0;
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)
831 /* Yes, reload! */
832 vfs_cpiofs_ops->free ((vfsid) parc);
833 vfs_rmstamp (vfs_cpiofs_ops, (vfsid) parc);
834 return 2;
836 /* Hasn't been modified, give it a new timeout */
837 vfs_stamp (vfs_cpiofs_ops, (vfsid) parc);
838 return 1;
841 /* --------------------------------------------------------------------------------------------- */
843 static ssize_t
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;
849 ssize_t res;
851 if (mc_lseek (fd, begin + FH->pos, SEEK_SET) != begin + FH->pos)
852 ERRNOR (EIO, -1);
854 count = MIN (count, (size_t) (FH->ino->st.st_size - FH->pos));
856 res = mc_read (fd, buffer, count);
857 if (res == -1)
858 ERRNOR (errno, -1);
860 FH->pos += res;
861 return res;
864 /* --------------------------------------------------------------------------------------------- */
866 static int
867 cpio_fh_open (struct vfs_class *me, vfs_file_handler_t * fh, int flags, mode_t mode)
869 (void) mode;
871 fh->data = NULL;
873 if ((flags & O_ACCMODE) != O_RDONLY)
874 ERRNOR (EROFS, -1);
875 return 0;
878 /* --------------------------------------------------------------------------------------------- */
879 /*** public functions ****************************************************************************/
880 /* --------------------------------------------------------------------------------------------- */
882 void
883 init_cpiofs (void)
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 /* --------------------------------------------------------------------------------------------- */