small fix...
[midnight-commander.git] / vfs / cpio.c
blob7503f6d1518983220a02525a61872fec07fcce92
1 /* Virtual File System: GNU Tar file system.
2 Copyright (C) 2000 The Free Software Foundation
4 Written by: 2000 Jan Hudec
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License
8 as published by the Free Software Foundation; either version 2 of
9 the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include <config.h>
21 #include <errno.h>
22 #include "utilvfs.h"
23 #include "xdirentry.h"
25 /* #include "utilvfs.h" */
27 #include "../src/dialog.h"
28 /* #include "cpio.h" */
29 #include "names.h"
31 enum {
32 STATUS_START,
33 STATUS_OK,
34 STATUS_TRAIL,
35 STATUS_FAIL,
36 STATUS_EOF
39 enum {
40 CPIO_UNKNOWN = 0, /* Not determined yet */
41 CPIO_BIN, /* Binary format */
42 CPIO_BINRE, /* Binary format, reverse endianity */
43 CPIO_OLDC, /* Old ASCII format */
44 CPIO_NEWC, /* New ASCII format */
45 CPIO_CRC /* New ASCII format + CRC */
48 struct old_cpio_header
50 unsigned short c_magic;
51 short c_dev;
52 unsigned short c_ino;
53 unsigned short c_mode;
54 unsigned short c_uid;
55 unsigned short c_gid;
56 unsigned short c_nlink;
57 short c_rdev;
58 unsigned short c_mtimes[2];
59 unsigned short c_namesize;
60 unsigned short c_filesizes[2];
63 struct new_cpio_header
65 unsigned short c_magic;
66 unsigned long c_ino;
67 unsigned long c_mode;
68 unsigned long c_uid;
69 unsigned long c_gid;
70 unsigned long c_nlink;
71 unsigned long c_mtime;
72 unsigned long c_filesize;
73 long c_dev;
74 long c_devmin;
75 long c_rdev;
76 long c_rdevmin;
77 unsigned long c_namesize;
78 unsigned long c_chksum;
81 struct defer_inode {
82 struct defer_inode *next;
83 unsigned long inumber;
84 unsigned short device;
85 vfs_s_inode *inode;
88 static int cpio_position;
90 static int cpio_find_head(vfs *me, vfs_s_super *super);
91 static int cpio_read_bin_head(vfs *me, vfs_s_super *super);
92 static int cpio_read_oldc_head(vfs *me, vfs_s_super *super);
93 static int cpio_read_crc_head(vfs *me, vfs_s_super *super);
94 static int cpio_create_entry(vfs *me, vfs_s_super *super, struct stat *stat, char *name);
95 static int cpio_read(void *fh, char *buffer, int count);
97 #define CPIO_POS(super) cpio_position
98 /* If some time reentrancy should be needed change it to */
99 /* #define CPIO_POS(super) (super)->u.cpio.fd */
101 #define CPIO_SEEK_SET(super, where) mc_lseek((super)->u.cpio.fd, CPIO_POS(super) = (where), SEEK_SET)
102 #define CPIO_SEEK_CUR(super, where) mc_lseek((super)->u.cpio.fd, CPIO_POS(super) += (where), SEEK_SET)
104 static struct defer_inode * defer_find(struct defer_inode *l, struct defer_inode *i)
106 if(!l) return NULL;
107 return l->inumber == i->inumber && l->device == i->device ? l :
108 defer_find(l->next, i);
111 static int cpio_skip_padding(vfs_s_super *super)
113 switch(super->u.cpio.type) {
114 case CPIO_BIN:
115 case CPIO_BINRE:
116 return CPIO_SEEK_CUR(super, (2 - (CPIO_POS(super) % 2)) % 2);
117 case CPIO_NEWC:
118 case CPIO_CRC:
119 return CPIO_SEEK_CUR(super, (4 - (CPIO_POS(super) % 4)) % 4);
120 case CPIO_OLDC:
121 return CPIO_POS(super);
122 default:
123 g_assert_not_reached();
124 return 42; /* & the compiler is happy :-) */
128 static void cpio_free_archive(vfs *me, vfs_s_super *super)
130 if(super->u.cpio.fd != -1)
131 mc_close(super->u.cpio.fd);
134 static int cpio_open_cpio_file(vfs *me, vfs_s_super *super, char *name)
136 int fd, type;
137 mode_t mode;
138 vfs_s_inode *root;
140 if((fd = mc_open(name, O_RDONLY)) == -1) {
141 message_2s(1, MSG_ERROR, _("Couldn't open cpio archive\n%s"), name);
142 return -1;
145 super->name = g_strdup(name);
146 super->u.cpio.fd = -1; /* for now */
147 mc_stat(name, &(super->u.cpio.stat));
148 super->u.cpio.type = CPIO_UNKNOWN;
150 type = get_compression_type(fd);
151 if (type != COMPRESSION_NONE) {
152 char *s;
154 mc_close(fd);
155 s = g_strconcat(name, decompress_extension(type), NULL);
156 if((fd = mc_open(s, O_RDONLY)) == -1) {
157 message_2s(1, MSG_ERROR, _("Couldn't open cpio archive\n%s"), s);
158 g_free(s);
159 return -1;
161 g_free(s);
164 super->u.cpio.fd = fd;
165 mode = super->u.cpio.stat.st_mode & 07777;
166 mode |= (mode & 0444) >> 2; /* set eXec where Read is */
167 mode |= S_IFDIR;
169 root = vfs_s_new_inode(me, super, &(super->u.cpio.stat));
170 root->st.st_mode = mode;
171 root->u.cpio.offset = -1;
172 root->st.st_nlink++;
173 root->st.st_dev = MEDATA->rdev++;
175 vfs_s_add_dots(me, root, NULL);
176 super->root = root;
178 CPIO_SEEK_SET(super, 0);
180 return fd;
183 static int cpio_read_head(vfs *me, vfs_s_super *super)
185 switch(cpio_find_head(me, super)) {
186 case CPIO_UNKNOWN:
187 return -1;
188 case CPIO_BIN:
189 case CPIO_BINRE:
190 return cpio_read_bin_head(me, super);
191 case CPIO_OLDC:
192 return cpio_read_oldc_head(me, super);
193 case CPIO_NEWC:
194 case CPIO_CRC:
195 return cpio_read_crc_head(me, super);
196 default:
197 g_assert_not_reached();
198 return 42; /* & the compiler is happy :-) */
202 #define MAGIC_LENGTH (6) /* How many bytes we have to read ahead */
203 #define SEEKBACK CPIO_SEEK_CUR(super, ptr - top)
204 #define RETURN(x) return(super->u.cpio.type = (x))
205 #define TYPEIS(x) ((super->u.cpio.type == CPIO_UNKNOWN) || (super->u.cpio.type == (x)))
206 static int cpio_find_head(vfs *me, vfs_s_super *super)
208 char buf[256];
209 int ptr = 0;
210 int top = 0;
211 int tmp;
213 top = mc_read(super->u.cpio.fd, buf, 256);
214 CPIO_POS(super) += top;
215 for(;;) {
216 if(ptr + MAGIC_LENGTH >= top) {
217 if(top > 128) {
218 memmove(buf, buf + top - 128, 128);
219 top = 128;
220 ptr -= top - 128;
222 if((tmp = mc_read(super->u.cpio.fd, buf, top)) == 0 || tmp == -1) {
223 message_2s(1, MSG_ERROR, _("Premature end of cpio archive\n%s"), super->name);
224 cpio_free_archive(me, super);
225 return CPIO_UNKNOWN;
227 top += tmp;
229 if(TYPEIS(CPIO_BIN) && ((*(unsigned short *)(buf + ptr)) == 070707)) {
230 SEEKBACK; RETURN(CPIO_BIN);
231 } else if(TYPEIS(CPIO_BINRE) && ((*(unsigned short *)(buf + ptr)) == GUINT16_SWAP_LE_BE_CONSTANT(070707))) {
232 SEEKBACK; RETURN(CPIO_BINRE);
233 } else if(TYPEIS(CPIO_OLDC) && (!strncmp(buf + ptr, "070707", 6))) {
234 SEEKBACK; RETURN(CPIO_OLDC);
235 } else if(TYPEIS(CPIO_NEWC) && (!strncmp(buf + ptr, "070701", 6))) {
236 SEEKBACK; RETURN(CPIO_NEWC);
237 } else if(TYPEIS(CPIO_CRC) && (!strncmp(buf + ptr, "070702", 6))) {
238 SEEKBACK; RETURN(CPIO_CRC);
240 ptr++;
243 #undef RETURN
244 #undef SEEKBACK
246 #define HEAD_LENGTH (26)
247 static int cpio_read_bin_head(vfs *me, vfs_s_super *super)
249 struct old_cpio_header buf;
250 int len;
251 char *name;
252 struct stat stat;
254 if((len = mc_read(super->u.cpio.fd, (char *)&buf, HEAD_LENGTH)) < HEAD_LENGTH)
255 return STATUS_EOF;
256 CPIO_POS(super) += len;
257 if(super->u.cpio.type == CPIO_BINRE) {
258 int i;
259 for(i = 0; i < (HEAD_LENGTH >> 1); i++)
260 ((short *)&buf)[i] = GUINT16_SWAP_LE_BE(((short *)&buf)[i]);
262 g_assert(buf.c_magic == 070707);
264 name = g_malloc(buf.c_namesize);
265 if((len = mc_read(super->u.cpio.fd, name, buf.c_namesize)) < buf.c_namesize){
266 g_free(name);
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, buf, HEAD_LENGTH)) < HEAD_LENGTH)
301 return STATUS_EOF;
302 CPIO_POS(super) += len;
303 buf[HEAD_LENGTH] = 0;
305 if(sscanf(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, _("Corrupted 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, _("Corrupted cpio header encountered in\n%s"),
360 super->name);
361 return STATUS_FAIL;
364 if((super->u.cpio.type == CPIO_NEWC && hd.c_magic != 070701) ||
365 (super->u.cpio.type == CPIO_CRC && hd.c_magic != 070702))
366 return STATUS_FAIL;
368 name = g_malloc(hd.c_namesize);
369 if((len = mc_read(super->u.cpio.fd, name, hd.c_namesize)) < hd.c_namesize){
370 g_free (name);
371 return STATUS_EOF;
373 CPIO_POS(super) += len;
374 cpio_skip_padding(super);
376 if(!strcmp("TRAILER!!!", name)) { /* We got to the last record */
377 g_free(name);
378 return STATUS_TRAIL;
381 stat.st_dev = (hd.c_dev << 8) + hd.c_devmin;
382 stat.st_ino = hd.c_ino;
383 stat.st_mode = hd.c_mode;
384 stat.st_nlink = hd.c_nlink;
385 stat.st_uid = hd.c_uid;
386 stat.st_gid = hd.c_gid;
387 stat.st_rdev = (hd.c_rdev << 8) + hd.c_rdevmin;
388 stat.st_size = hd.c_filesize;
389 stat.st_atime = stat.st_mtime = stat.st_ctime = hd.c_mtime;
391 return cpio_create_entry(me, super, &stat, name);
394 static int cpio_create_entry(vfs *me, vfs_s_super *super, struct stat *stat, char *name)
396 vfs_s_inode *inode = NULL;
397 vfs_s_inode *root = super->root;
398 vfs_s_entry *entry = NULL;
399 char *tn;
401 switch (stat->st_mode & S_IFMT) { /* For case of HP/UX archives */
402 case S_IFCHR:
403 case S_IFBLK:
404 #ifdef S_IFSOCK
405 case S_IFSOCK:
406 #endif
407 #ifdef S_IFIFO
408 case S_IFIFO:
409 #endif
410 if((stat->st_size != 0) &&
411 (stat->st_rdev == 0x0001)) {
412 stat->st_rdev = (unsigned) stat->st_size;
413 stat->st_size = 0;
415 break;
416 default:
417 break;
420 if((stat->st_nlink > 1) &&
421 (super->u.cpio.type == CPIO_NEWC ||
422 super->u.cpio.type == CPIO_CRC)) { /* For case of harlinked files */
423 struct defer_inode i, *l;
424 i.inumber = stat->st_ino;
425 i.device = stat->st_dev;
426 i.inode = NULL;
427 if((l = defer_find(super->u.cpio.defered, &i)) != NULL) {
428 inode = l->inode;
429 if(inode->st.st_size && stat->st_size && (inode->st.st_size != stat->st_size)) {
430 message_3s(1, MSG_ERROR, _("Inconsistent hardlinks of\n%s\nin cpio archive\n%s"),
431 name, super->name);
432 inode = NULL;
437 while(name[strlen(name)-1] == PATH_SEP) name[strlen(name)-1] = 0;
438 if((tn = strrchr(name, PATH_SEP))) {
439 *tn = 0;
440 root = vfs_s_find_inode(me, root, name, LINK_FOLLOW, FL_MKDIR); /* CHECKME! What function here? */
441 *tn = PATH_SEP;
442 tn++;
443 } else
444 tn = name;
446 entry = vfs_s_find_entry_tree(me, root, tn, LINK_FOLLOW, FL_NONE); /* In case entry is already there */
448 if(entry) { /* This shouldn't happen! (well, it can happen if there is a record for a
449 file and than a record for a directory it is in; cpio would die with
450 'No such file or directory' is such case) */
452 if(!S_ISDIR(entry->ino->st.st_mode)) { /* This can be considered archive inconsistency */
453 message_2s(1, MSG_ERROR, _("%s contains duplicate entries! Skipping!"), super->name);
454 } else {
455 entry->ino->st.st_mode = stat->st_mode;
456 entry->ino->st.st_uid = stat->st_uid;
457 entry->ino->st.st_gid = stat->st_gid;
458 entry->ino->st.st_atime = stat->st_atime;
459 entry->ino->st.st_mtime = stat->st_mtime;
460 entry->ino->st.st_ctime = stat->st_ctime;
463 } else { /* !entry */
465 if(!inode) {
466 inode = vfs_s_new_inode(me, super, stat);
467 if((stat->st_nlink > 0) &&
468 (super->u.cpio.type == CPIO_NEWC ||
469 super->u.cpio.type == CPIO_CRC)) { /* For case of hardlinked files */
470 struct defer_inode *i;
471 i = g_new(struct defer_inode, 1);
472 i->inumber = stat->st_ino;
473 i->device = stat->st_dev;
474 i->inode = inode;
475 i->next = super->u.cpio.defered;
476 super->u.cpio.defered = i;
480 if(stat->st_size)
481 inode->u.cpio.offset = CPIO_POS(super);
483 entry = vfs_s_new_entry(me, tn, inode);
484 vfs_s_insert_entry(me, root, entry);
486 if(S_ISDIR(stat->st_mode))
487 vfs_s_add_dots(me, inode, root);
489 if(S_ISLNK(stat->st_mode)) {
490 inode->linkname = g_malloc(stat->st_size + 1);
491 if(mc_read(super->u.cpio.fd, inode->linkname, stat->st_size) < stat->st_size) {
492 inode->linkname[0] = 0;
493 return STATUS_EOF;
495 inode->linkname[stat->st_size] = 0; /* Linkname stored without terminating \0 !!! */
496 CPIO_POS(super) += stat->st_size;
497 cpio_skip_padding(super);
498 } else {
499 CPIO_SEEK_CUR(super, stat->st_size);
502 } /* !entry */
504 g_free (name);
505 return STATUS_OK;
508 /* Need to CPIO_SEEK_CUR to skip the file at the end of add entry!!!! */
510 static int cpio_open_archive(vfs *me, vfs_s_super *super, char *name, char *op)
512 int status = STATUS_START;
514 if(cpio_open_cpio_file(me, super, name) == -1)
515 return -1;
517 for(;;) {
518 status = cpio_read_head(me, super);
520 switch(status) {
521 case STATUS_EOF:
522 message_2s(1, MSG_ERROR, _("Unexpected end of file\n%s"), name);
523 return 0;
524 case STATUS_OK:
525 continue;
526 case STATUS_TRAIL:
527 break;
529 break;
532 return 0;
535 /* Remaining functions are exactly same as for tarfs (and were in fact just copied) */
536 static void *cpio_super_check(vfs *me, char *archive_name, char *op)
538 static struct stat sb;
539 if(mc_stat(archive_name, &sb))
540 return NULL;
541 return &sb;
544 static int cpio_super_same(vfs *me, struct vfs_s_super *parc, char *archive_name, char *op, void *cookie)
546 struct stat *archive_stat = cookie; /* stat of main archive */
548 if(strcmp(parc->name, archive_name)) return 0;
550 if(vfs_uid && (!(archive_stat->st_mode & 0004)))
551 if((archive_stat->st_gid != vfs_gid) || !(archive_stat->st_mode & 0040))
552 if((archive_stat->st_uid != vfs_uid) || !(archive_stat->st_mode & 0400))
553 return 0;
554 /* Has the cached archive been changed on the disk? */
555 if(parc->u.cpio.stat.st_mtime < archive_stat->st_mtime) { /* Yes, reload! */
556 (*vfs_cpiofs_ops.free) ((vfsid) parc);
557 vfs_rmstamp (&vfs_cpiofs_ops, (vfsid) parc, 0);
558 return 2;
560 /* Hasn't been modified, give it a new timeout */
561 vfs_stamp (&vfs_cpiofs_ops, (vfsid) parc);
562 return 1;
565 static int cpio_read(void *fh, char *buffer, int count)
567 off_t begin = FH->ino->u.tar.data_offset;
568 int fd = FH_SUPER->u.tar.fd;
569 vfs *me = FH_SUPER->me;
571 if (mc_lseek (fd, begin + FH->pos, SEEK_SET) !=
572 begin + FH->pos) ERRNOR (EIO, -1);
574 count = MIN(count, FH->ino->st.st_size - FH->pos);
576 if ((count = mc_read (fd, buffer, count)) == -1) ERRNOR (errno, -1);
578 FH->pos += count;
579 return count;
582 static int cpio_ungetlocalcopy(vfs *me, char *path, char *local, int has_changed)
584 /* We do just nothing. (We are read only and do not need to free local,
585 since it will be freed when tar archive will be freed */
586 return 0;
589 static int cpio_fh_open(vfs *me, vfs_s_fh *fh, int flags, int mode)
591 if ((flags & O_ACCMODE) != O_RDONLY) ERRNOR (EROFS, -1);
592 return 0;
595 static struct vfs_s_data cpiofs_data = {
596 NULL,
599 NULL,
601 NULL, /* init inode */
602 NULL, /* free inode */
603 NULL, /* init entry */
605 cpio_super_check,
606 cpio_super_same,
607 cpio_open_archive,
608 cpio_free_archive,
610 cpio_fh_open,
611 NULL,
613 vfs_s_find_entry_tree,
614 NULL,
615 NULL
616 /* ??? */
619 vfs vfs_cpiofs_ops = {
620 NULL, /* next pointer */
621 "cpiofs",
622 0, /* flags */
623 "ucpio", /* prefix */
624 &cpiofs_data, /* vfs_s_data */
625 0, /* errno */
627 NULL,
628 NULL,
629 vfs_s_fill_names,
631 NULL,
633 vfs_s_open,
634 vfs_s_close,
635 cpio_read,
636 NULL,
638 vfs_s_opendir,
639 vfs_s_readdir,
640 vfs_s_closedir,
641 vfs_s_telldir,
642 vfs_s_seekdir,
644 vfs_s_stat,
645 vfs_s_lstat,
646 vfs_s_fstat,
648 NULL,
649 NULL,
650 NULL,
652 vfs_s_readlink,
653 NULL,
654 NULL,
655 NULL,
657 NULL,
658 vfs_s_chdir,
659 vfs_s_ferrno,
660 vfs_s_lseek,
661 NULL,
663 vfs_s_getid,
664 vfs_s_nothingisopen,
665 vfs_s_free,
667 vfs_s_getlocalcopy,
668 cpio_ungetlocalcopy,
670 NULL,
671 NULL,
672 NULL,
673 NULL
675 MMAPNULL