*** empty log message ***
[midnight-commander.git] / vfs / cpio.c
blobd72283eba1866558802c5402df2e788c76d1de0b
1 /* Virtual File System: GNU Tar file system.
2 Copyright (C) 2000 The Free Software Foundation
4 Written by: 2000 Jan Hudec
6 $Id$
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License
10 as published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include "xdirentry.h"
24 #include "utilvfs.h"
25 #include <glib.h>
26 #include <errno.h>
28 /* #include "utilvfs.h" */
30 #include "../src/dialog.h"
31 /* #include "cpio.h" */
32 #include "names.h"
34 enum {
35 STATUS_START,
36 STATUS_OK,
37 STATUS_TRAIL,
38 STATUS_FAIL,
39 STATUS_EOF
42 enum {
43 CPIO_UNKNOWN = 0, /* Not determined yet */
44 CPIO_BIN, /* Binary format */
45 CPIO_BINRE, /* Binary format, reverse endianity */
46 CPIO_OLDC, /* Old ASCII format */
47 CPIO_NEWC, /* New ASCII format */
48 CPIO_CRC /* New ASCII format + CRC */
51 struct old_cpio_header
53 unsigned short c_magic;
54 short c_dev;
55 unsigned short c_ino;
56 unsigned short c_mode;
57 unsigned short c_uid;
58 unsigned short c_gid;
59 unsigned short c_nlink;
60 short c_rdev;
61 unsigned short c_mtimes[2];
62 unsigned short c_namesize;
63 unsigned short c_filesizes[2];
66 struct new_cpio_header
68 unsigned short c_magic;
69 unsigned long c_ino;
70 unsigned long c_mode;
71 unsigned long c_uid;
72 unsigned long c_gid;
73 unsigned long c_nlink;
74 unsigned long c_mtime;
75 unsigned long c_filesize;
76 long c_dev;
77 long c_devmin;
78 long c_rdev;
79 long c_rdevmin;
80 unsigned long c_namesize;
81 unsigned long c_chksum;
84 struct defer_inode {
85 struct defer_inode *next;
86 unsigned long inumber;
87 unsigned short device;
88 vfs_s_inode *inode;
91 static int cpio_position;
93 static int cpio_find_head(vfs *me, vfs_s_super *super);
94 static int cpio_read_bin_head(vfs *me, vfs_s_super *super);
95 static int cpio_read_oldc_head(vfs *me, vfs_s_super *super);
96 static int cpio_read_crc_head(vfs *me, vfs_s_super *super);
97 static int cpio_create_entry(vfs *me, vfs_s_super *super, struct stat *stat, char *name);
98 static int cpio_read(void *fh, char *buffer, int count);
100 #define CPIO_POS(super) cpio_position
101 /* If some time reentrancy should be needed change it to */
102 /* #define CPIO_POS(super) (super)->u.cpio.fd */
104 #define CPIO_SEEK_SET(super, where) mc_lseek((super)->u.cpio.fd, CPIO_POS(super) = (where), SEEK_SET)
105 #define CPIO_SEEK_CUR(super, where) mc_lseek((super)->u.cpio.fd, CPIO_POS(super) += (where), SEEK_SET)
107 static struct defer_inode * defer_find(struct defer_inode *l, struct defer_inode *i)
109 if(!l) return NULL;
110 return l->inumber == i->inumber && l->device == i->device ? l :
111 defer_find(l->next, i);
114 static int cpio_skip_padding(vfs_s_super *super)
116 switch(super->u.cpio.type) {
117 case CPIO_BIN:
118 case CPIO_BINRE:
119 return CPIO_SEEK_CUR(super, (2 - (CPIO_POS(super) % 2)) % 2);
120 case CPIO_NEWC:
121 case CPIO_CRC:
122 return CPIO_SEEK_CUR(super, (4 - (CPIO_POS(super) % 4)) % 4);
123 default:
124 g_assert_not_reached();
125 return 42; /* & the compiler is happy :-) */
129 static void cpio_free_archive(vfs *me, vfs_s_super *super)
131 if(super->u.cpio.fd != -1)
132 mc_close(super->u.cpio.fd);
135 static int cpio_open_cpio_file(vfs *me, vfs_s_super *super, char *name)
137 int fd, size, type;
138 mode_t mode;
139 vfs_s_inode *root;
141 if((fd = mc_open(name, O_RDONLY)) == -1) {
142 message_2s(1, MSG_ERROR, _("Couldn't open cpio archive\n%s"), name);
143 return -1;
146 super->name = g_strdup(name);
147 super->u.cpio.fd = -1; /* for now */
148 mc_stat(name, &(super->u.cpio.stat));
149 super->u.cpio.type = CPIO_UNKNOWN;
151 size = is_gunzipable(fd, &type);
152 if(size > 0) {
153 char *s;
155 mc_close(fd);
156 s = g_strconcat(name, decompress_extension(type), NULL);
157 if((fd = mc_open(s, O_RDONLY)) == -1) {
158 message_2s(1, MSG_ERROR, _("Couldn't open cpio archive\n%s"), s);
159 g_free(s);
160 return -1;
162 g_free(s);
165 super->u.cpio.fd = fd;
166 mode = super->u.cpio.stat.st_mode & 07777;
167 mode |= (mode & 0444) >> 2; /* set eXec where Read is */
168 mode |= S_IFDIR;
170 root = vfs_s_new_inode(me, super, &(super->u.cpio.stat));
171 root->st.st_mode = mode;
172 root->u.cpio.offset = -1;
173 root->st.st_nlink++;
174 root->st.st_dev = MEDATA->rdev++;
176 vfs_s_add_dots(me, root, NULL);
177 super->root = root;
179 CPIO_SEEK_SET(super, 0);
181 return fd;
184 static int cpio_read_head(vfs *me, vfs_s_super *super)
186 switch(cpio_find_head(me, super)) {
187 case CPIO_UNKNOWN:
188 return -1;
189 case CPIO_BIN:
190 case CPIO_BINRE:
191 return cpio_read_bin_head(me, super);
192 case CPIO_OLDC:
193 return cpio_read_oldc_head(me, super);
194 case CPIO_NEWC:
195 case CPIO_CRC:
196 return cpio_read_crc_head(me, super);
197 default:
198 g_assert_not_reached();
199 return 42; /* & the compiler is happy :-) */
203 #define MAGIC_LENGTH (6) /* How many bytes we have to read ahead */
204 #define SEEKBACK CPIO_SEEK_CUR(super, ptr - top)
205 #define RETURN(x) return(super->u.cpio.type = (x))
206 #define TYPEIS(x) ((super->u.cpio.type == CPIO_UNKNOWN) || (super->u.cpio.type == (x)))
207 static int cpio_find_head(vfs *me, vfs_s_super *super)
209 char buf[256];
210 int ptr = 0;
211 int top = 0;
212 int tmp;
214 top = mc_read(super->u.cpio.fd, buf, 256);
215 CPIO_POS(super) += top;
216 for(;;) {
217 if(ptr + MAGIC_LENGTH >= top) {
218 if(top > 128) {
219 memmove(buf, buf + top - 128, 128);
220 top = 128;
221 ptr -= top - 128;
223 if((tmp = mc_read(super->u.cpio.fd, buf, top)) == 0 || tmp == -1) {
224 message_2s(1, MSG_ERROR, _("Premature end of cpio archive\n%s"), super->name);
225 cpio_free_archive(me, super);
226 return CPIO_UNKNOWN;
228 top += tmp;
230 if(TYPEIS(CPIO_BIN) && ((*(unsigned short *)(buf + ptr)) == 070707)) {
231 SEEKBACK; RETURN(CPIO_BIN);
232 } else if(TYPEIS(CPIO_BINRE) && ((*(unsigned short *)(buf + ptr)) == GUINT16_SWAP_LE_BE_CONSTANT(070707))) {
233 SEEKBACK; RETURN(CPIO_BINRE);
234 } else if(TYPEIS(CPIO_OLDC) && (!strncmp(buf + ptr, "070707", 6))) {
235 SEEKBACK; RETURN(CPIO_OLDC);
236 } else if(TYPEIS(CPIO_NEWC) && (!strncmp(buf + ptr, "070701", 6))) {
237 SEEKBACK; RETURN(CPIO_NEWC);
238 } else if(TYPEIS(CPIO_CRC) && (!strncmp(buf + ptr, "070702", 6))) {
239 SEEKBACK; RETURN(CPIO_CRC);
241 ptr++;
244 #undef RETURN
245 #undef SEEKBACK
247 #define HEAD_LENGTH (26)
248 static int cpio_read_bin_head(vfs *me, vfs_s_super *super)
250 struct old_cpio_header buf;
251 int len;
252 char *name;
253 struct stat stat;
255 if((len = mc_read(super->u.cpio.fd, (char *)&buf, HEAD_LENGTH)) < HEAD_LENGTH)
256 return STATUS_EOF;
257 CPIO_POS(super) += len;
258 if(super->u.cpio.type == CPIO_BINRE) {
259 int i;
260 for(i = 0; i < (HEAD_LENGTH >> 1); i++)
261 ((short *)&buf)[i] = GUINT16_SWAP_LE_BE(((short *)&buf)[i]);
263 g_assert(buf.c_magic == 070707);
265 name = g_malloc(buf.c_namesize);
266 if((len = mc_read(super->u.cpio.fd, name, buf.c_namesize)) < buf.c_namesize)
267 return STATUS_EOF;
269 CPIO_POS(super) += len;
270 cpio_skip_padding(super);
272 if(!strcmp("TRAILER!!!", name)) { /* We got to the last record */
273 g_free(name);
274 return STATUS_TRAIL;
277 stat.st_dev = buf.c_dev;
278 stat.st_ino = buf.c_ino;
279 stat.st_mode = buf.c_mode;
280 stat.st_nlink = buf.c_nlink;
281 stat.st_uid = buf.c_uid;
282 stat.st_gid = buf.c_gid;
283 stat.st_rdev = buf.c_rdev;
284 stat.st_size = (buf.c_filesizes[0] << 16) | buf.c_filesizes[1];
285 stat.st_atime = stat.st_mtime = stat.st_ctime = (buf.c_mtimes[0] << 16) | buf.c_mtimes[1];
287 return cpio_create_entry(me, super, &stat, name);
289 #undef HEAD_LENGTH
291 #define HEAD_LENGTH (76)
292 static int cpio_read_oldc_head(vfs *me, vfs_s_super *super)
294 struct new_cpio_header hd;
295 struct stat stat;
296 char *buf[HEAD_LENGTH + 1];
297 int len;
298 char *name;
300 if((len = mc_read(super->u.cpio.fd, (void *)buf, HEAD_LENGTH)) < HEAD_LENGTH)
301 return STATUS_EOF;
302 CPIO_POS(super) += len;
303 buf[HEAD_LENGTH] = 0;
305 if(sscanf((void *)buf, "070707%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6lo%11lo",
306 &hd.c_dev, &hd.c_ino, &hd.c_mode, &hd.c_uid, &hd.c_gid,
307 &hd.c_nlink, &hd.c_rdev, &hd.c_mtime,
308 &hd.c_namesize, &hd.c_filesize) < 10) {
309 message_2s(1, MSG_ERROR, _("Corrupt cpio header encountered in\n%s"), super->name);
310 return STATUS_FAIL;
313 name = g_malloc(hd.c_namesize);
314 if((len = mc_read(super->u.cpio.fd, name, hd.c_namesize)) < hd.c_namesize) {
315 g_free (name);
316 return STATUS_EOF;
318 CPIO_POS(super) += len;
319 cpio_skip_padding(super);
321 if(!strcmp("TRAILER!!!", name)) { /* We got to the last record */
322 g_free(name);
323 return STATUS_TRAIL;
326 stat.st_dev = hd.c_dev;
327 stat.st_ino = hd.c_ino;
328 stat.st_mode = hd.c_mode;
329 stat.st_nlink = hd.c_nlink;
330 stat.st_uid = hd.c_uid;
331 stat.st_gid = hd.c_gid;
332 stat.st_rdev = hd.c_rdev;
333 stat.st_size = hd.c_filesize;
334 stat.st_atime = stat.st_mtime = stat.st_ctime = hd.c_mtime;
336 return cpio_create_entry(me, super, &stat, name);
338 #undef HEAD_LENGTH
340 #define HEAD_LENGTH (110)
341 static int cpio_read_crc_head(vfs *me, vfs_s_super *super)
343 struct new_cpio_header hd;
344 struct stat stat;
345 char buf[HEAD_LENGTH + 1];
346 int len;
347 char *name;
349 if((len = mc_read(super->u.cpio.fd, buf, HEAD_LENGTH)) < HEAD_LENGTH)
350 return STATUS_EOF;
351 CPIO_POS(super) += len;
352 buf[HEAD_LENGTH] = 0;
354 if(sscanf(buf, "%6ho%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx",
355 &hd.c_magic, &hd.c_ino, &hd.c_mode, &hd.c_uid, &hd.c_gid,
356 &hd.c_nlink, &hd.c_mtime, &hd.c_filesize,
357 &hd.c_dev, &hd.c_devmin, &hd.c_rdev, &hd.c_rdevmin,
358 &hd.c_namesize, &hd.c_chksum) < 14) {
359 message_2s(1, MSG_ERROR, _("Corrupt cpio header encountered in\n%s"), super->name);
360 return STATUS_FAIL;
363 if((super->u.cpio.type == CPIO_NEWC && hd.c_magic != 070701) ||
364 (super->u.cpio.type == CPIO_CRC && hd.c_magic != 070702))
365 return STATUS_FAIL;
367 name = g_malloc(hd.c_namesize);
368 if((len = mc_read(super->u.cpio.fd, name, hd.c_namesize)) < hd.c_namesize)
369 return STATUS_EOF;
371 CPIO_POS(super) += len;
372 cpio_skip_padding(super);
374 if(!strcmp("TRAILER!!!", name)) { /* We got to the last record */
375 g_free(name);
376 return STATUS_TRAIL;
379 stat.st_dev = (hd.c_dev << 8) + hd.c_devmin;
380 stat.st_ino = hd.c_ino;
381 stat.st_mode = hd.c_mode;
382 stat.st_nlink = hd.c_nlink;
383 stat.st_uid = hd.c_uid;
384 stat.st_gid = hd.c_gid;
385 stat.st_rdev = (hd.c_rdev << 8) + hd.c_rdevmin;
386 stat.st_size = hd.c_filesize;
387 stat.st_atime = stat.st_mtime = stat.st_ctime = hd.c_mtime;
389 return cpio_create_entry(me, super, &stat, name);
392 static int cpio_create_entry(vfs *me, vfs_s_super *super, struct stat *stat, char *name)
394 vfs_s_inode *inode = NULL;
395 vfs_s_inode *root = super->root;
396 vfs_s_entry *entry = NULL;
397 char *tn;
399 switch (stat->st_mode & S_IFMT) { /* For case of HP/UX archives */
400 case S_IFCHR:
401 case S_IFBLK:
402 #ifdef S_IFSOCK
403 case S_IFSOCK:
404 #endif
405 #ifdef S_IFIFO
406 case S_IFIFO:
407 #endif
408 if((stat->st_size != 0) &&
409 (stat->st_rdev == 0x0001)) {
410 stat->st_rdev = (unsigned) stat->st_size;
411 stat->st_size = 0;
413 break;
414 default:
415 break;
418 if((stat->st_nlink > 1) &&
419 (super->u.cpio.type == CPIO_NEWC ||
420 super->u.cpio.type == CPIO_CRC)) { /* For case of harlinked files */
421 struct defer_inode i, *l;
422 i.inumber = stat->st_ino;
423 i.device = stat->st_dev;
424 i.inode = NULL;
425 if((l = defer_find(super->u.cpio.defered, &i)) != NULL) {
426 inode = l->inode;
427 if(inode->st.st_size && stat->st_size && (inode->st.st_size != stat->st_size)) {
428 message_3s(1, MSG_ERROR, _("Inconsistent hardlinks of\n%s\nin cpio archive\n%s"),
429 name, super->name);
430 inode = NULL;
435 while(name[strlen(name)-1] == PATH_SEP) name[strlen(name)-1] = 0;
436 if((tn = strrchr(name, PATH_SEP))) {
437 *tn = 0;
438 root = vfs_s_find_inode(me, root, name, LINK_FOLLOW, FL_MKDIR); /* CHECKME! What function here? */
439 *tn = PATH_SEP;
440 tn++;
441 } else
442 tn = name;
444 entry = vfs_s_find_entry_tree(me, root, tn, LINK_FOLLOW, FL_NONE); /* In case entry is already there */
446 if(entry) { /* This shouldn't happen! (well, it can happen if there is a record for a
447 file and than a record for a directory it is in; cpio would die with
448 'No such file or directory' is such case) */
450 if(!S_ISDIR(entry->ino->st.st_mode)) { /* This can be considered archive inconsistency */
451 message_2s(1, MSG_ERROR, _("%s contains duplicit entries! Skiping!"), super->name);
452 } else {
453 entry->ino->st.st_mode = stat->st_mode;
454 entry->ino->st.st_uid = stat->st_uid;
455 entry->ino->st.st_gid = stat->st_gid;
456 entry->ino->st.st_atime = stat->st_atime;
457 entry->ino->st.st_mtime = stat->st_mtime;
458 entry->ino->st.st_ctime = stat->st_ctime;
461 } else { /* !entry */
463 if(!inode) {
464 inode = vfs_s_new_inode(me, super, stat);
465 if((stat->st_nlink > 0) &&
466 (super->u.cpio.type == CPIO_NEWC ||
467 super->u.cpio.type == CPIO_CRC)) { /* For case of hardlinked files */
468 struct defer_inode *i;
469 i = g_new(struct defer_inode, 1);
470 i->inumber = stat->st_ino;
471 i->device = stat->st_dev;
472 i->inode = inode;
473 i->next = super->u.cpio.defered;
474 super->u.cpio.defered = i;
478 if(stat->st_size)
479 inode->u.cpio.offset = CPIO_POS(super);
481 entry = vfs_s_new_entry(me, tn, inode);
482 vfs_s_insert_entry(me, root, entry);
484 if(S_ISDIR(stat->st_mode))
485 vfs_s_add_dots(me, inode, root);
487 if(S_ISLNK(stat->st_mode)) {
488 inode->linkname = g_malloc(stat->st_size + 1);
489 if(mc_read(super->u.cpio.fd, inode->linkname, stat->st_size) < stat->st_size) {
490 inode->linkname[0] = 0;
491 return STATUS_EOF;
493 inode->linkname[stat->st_size] = 0; /* Linkname stored without terminating \0 !!! */
494 cpio_skip_padding(super);
495 } else {
496 CPIO_SEEK_CUR(super, stat->st_size);
499 } /* !entry */
501 return STATUS_OK;
504 /* Need to CPIO_SEEK_CUR to skip the file at the end of add entry!!!! */
506 static int cpio_open_archive(vfs *me, vfs_s_super *super, char *name, char *op)
508 int status = STATUS_START;
510 if(cpio_open_cpio_file(me, super, name) == -1)
511 return -1;
513 for(;;) {
514 status = cpio_read_head(me, super);
516 switch(status) {
517 case STATUS_EOF:
518 message_2s(1, MSG_ERROR, _("Unexpected end of file\n%s"), name);
519 return 0;
520 case STATUS_OK:
521 continue;
522 case STATUS_TRAIL:
523 break;
525 break;
528 return 0;
531 /* Remaining functions are exactly same as for tarfs (and were in fact just copied) */
532 static void *cpio_super_check(vfs *me, char *archive_name, char *op)
534 static struct stat sb;
535 if(mc_stat(archive_name, &sb))
536 return NULL;
537 return &sb;
540 static int cpio_super_same(vfs *me, struct vfs_s_super *parc, char *archive_name, char *op, void *cookie)
542 struct stat *archive_stat = cookie; /* stat of main archive */
544 if(strcmp(parc->name, archive_name)) return 0;
546 if(vfs_uid && (!(archive_stat->st_mode & 0004)))
547 if((archive_stat->st_gid != vfs_gid) || !(archive_stat->st_mode & 0040))
548 if((archive_stat->st_uid != vfs_uid) || !(archive_stat->st_mode & 0400))
549 return 0;
550 /* Has the cached archive been changed on the disk? */
551 if(parc->u.cpio.stat.st_mtime < archive_stat->st_mtime) { /* Yes, reload! */
552 (*vfs_cpiofs_ops.free) ((vfsid) parc);
553 vfs_rmstamp (&vfs_cpiofs_ops, (vfsid) parc, 0);
554 return 2;
556 /* Hasn't been modified, give it a new timeout */
557 vfs_stamp (&vfs_cpiofs_ops, (vfsid) parc);
558 return 1;
561 static int cpio_read(void *fh, char *buffer, int count)
563 off_t begin = FH->ino->u.tar.data_offset;
564 int fd = FH_SUPER->u.tar.fd;
565 vfs *me = FH_SUPER->me;
567 if (mc_lseek (fd, begin + FH->pos, SEEK_SET) !=
568 begin + FH->pos) ERRNOR (EIO, -1);
570 count = MIN(count, FH->ino->st.st_size - FH->pos);
572 if ((count = mc_read (fd, buffer, count)) == -1) ERRNOR (errno, -1);
574 FH->pos += count;
575 return count;
578 static int cpio_ungetlocalcopy(vfs *me, char *path, char *local, int has_changed)
580 /* We do just nothing. (We are read only and do not need to free local,
581 since it will be freed when tar archive will be freed */
582 return 0;
585 static int cpio_fh_open(vfs *me, vfs_s_fh *fh, int flags, int mode)
587 if ((flags & O_ACCMODE) != O_RDONLY) ERRNOR (EROFS, -1);
588 return 0;
591 static struct vfs_s_data cpiofs_data = {
592 NULL,
595 NULL,
597 NULL, /* init inode */
598 NULL, /* free inode */
599 NULL, /* init entry */
601 cpio_super_check,
602 cpio_super_same,
603 cpio_open_archive,
604 cpio_free_archive,
606 cpio_fh_open,
607 NULL,
609 vfs_s_find_entry_tree,
610 NULL,
611 NULL
612 /* ??? */
615 vfs vfs_cpiofs_ops = {
616 NULL, /* next pointer */
617 "cpiofs",
618 0, /* flags */
619 "ucpio", /* prefix */
620 &cpiofs_data, /* vfs_s_data */
621 0, /* errno */
623 NULL,
624 NULL,
625 vfs_s_fill_names,
627 NULL,
629 vfs_s_open,
630 vfs_s_close,
631 cpio_read,
632 NULL,
634 vfs_s_opendir,
635 vfs_s_readdir,
636 vfs_s_closedir,
637 vfs_s_telldir,
638 vfs_s_seekdir,
640 vfs_s_stat,
641 vfs_s_lstat,
642 vfs_s_fstat,
644 NULL,
645 NULL,
646 NULL,
648 vfs_s_readlink,
649 NULL,
650 NULL,
651 NULL,
653 NULL,
654 vfs_s_chdir,
655 vfs_s_ferrno,
656 vfs_s_lseek,
657 NULL,
659 vfs_s_getid,
660 vfs_s_nothingisopen,
661 vfs_s_free,
663 vfs_s_getlocalcopy,
664 cpio_ungetlocalcopy,
666 NULL,
667 NULL,
668 NULL,
669 NULL
671 MMAPNULL