Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License as published by |
| 4 | * the Free Software Foundation; either version 2 of the License, or |
| 5 | * (at your option) any later version. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU Library General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include "unarchive.h" |
| 21 | #include "libbb.h" |
| 22 | |
| 23 | struct hardlinks { |
| 24 | file_header_t *entry; |
| 25 | int inode; |
| 26 | struct hardlinks *next; |
| 27 | }; |
| 28 | |
| 29 | file_header_t *get_header_cpio(FILE *src_stream) |
| 30 | { |
| 31 | file_header_t *cpio_entry = NULL; |
| 32 | char cpio_header[110]; |
| 33 | int namesize; |
| 34 | char dummy[16]; |
| 35 | int major, minor, nlink, inode; |
| 36 | static struct hardlinks *saved_hardlinks = NULL; |
| 37 | static int pending_hardlinks = 0; |
| 38 | |
| 39 | if (pending_hardlinks) { /* Deal with any pending hardlinks */ |
| 40 | struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL; |
| 41 | while (tmp) { |
| 42 | if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */ |
| 43 | cpio_entry = tmp->entry; |
| 44 | if (oldtmp) oldtmp->next = tmp->next; /* Remove item from linked list */ |
| 45 | else saved_hardlinks = tmp->next; |
| 46 | free(tmp); |
| 47 | return (cpio_entry); |
| 48 | } |
| 49 | oldtmp = tmp; |
| 50 | tmp = tmp->next; |
| 51 | } |
| 52 | pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */ |
| 53 | } |
| 54 | |
| 55 | /* There can be padding before archive header */ |
| 56 | seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4); |
| 57 | if (fread(cpio_header, 1, 110, src_stream) == 110) { |
| 58 | archive_offset += 110; |
| 59 | if (strncmp(cpio_header, "07070", 5) != 0) { |
| 60 | error_msg("Unsupported format or invalid magic"); |
| 61 | return(NULL); |
| 62 | } |
| 63 | switch (cpio_header[5]) { |
| 64 | case '2': /* "crc" header format */ |
| 65 | /* Doesnt do the crc check yet */ |
| 66 | case '1': /* "newc" header format */ |
| 67 | cpio_entry = (file_header_t *) xcalloc(1, sizeof(file_header_t)); |
| 68 | sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c", |
| 69 | dummy, &inode, (unsigned int*)&cpio_entry->mode, |
| 70 | (unsigned int*)&cpio_entry->uid, (unsigned int*)&cpio_entry->gid, |
| 71 | &nlink, &cpio_entry->mtime, &cpio_entry->size, |
| 72 | dummy, &major, &minor, &namesize, dummy); |
| 73 | |
| 74 | cpio_entry->name = (char *) xcalloc(1, namesize); |
| 75 | fread(cpio_entry->name, 1, namesize, src_stream); /* Read in filename */ |
| 76 | archive_offset += namesize; |
| 77 | /* Skip padding before file contents */ |
| 78 | seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4); |
| 79 | if (strcmp(cpio_entry->name, "TRAILER!!!") == 0) { |
| 80 | printf("%d blocks\n", (int) (archive_offset % 512 ? (archive_offset / 512) + 1 : archive_offset / 512)); /* Always round up */ |
| 81 | if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */ |
| 82 | struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL; |
| 83 | while (tmp) { |
| 84 | error_msg("%s not created: cannot resolve hardlink", tmp->entry->name); |
| 85 | oldtmp = tmp; |
| 86 | tmp = tmp->next; |
| 87 | free (oldtmp->entry->name); |
| 88 | free (oldtmp->entry); |
| 89 | free (oldtmp); |
| 90 | } |
| 91 | saved_hardlinks = NULL; |
| 92 | pending_hardlinks = 0; |
| 93 | } |
| 94 | return(NULL); |
| 95 | } |
| 96 | |
| 97 | if (S_ISLNK(cpio_entry->mode)) { |
| 98 | cpio_entry->link_name = (char *) xcalloc(1, cpio_entry->size + 1); |
| 99 | fread(cpio_entry->link_name, 1, cpio_entry->size, src_stream); |
| 100 | archive_offset += cpio_entry->size; |
| 101 | cpio_entry->size = 0; /* Stop possiable seeks in future */ |
| 102 | } |
| 103 | if (nlink > 1 && !S_ISDIR(cpio_entry->mode)) { |
| 104 | if (cpio_entry->size == 0) { /* Put file on a linked list for later */ |
| 105 | struct hardlinks *new = xmalloc(sizeof(struct hardlinks)); |
| 106 | new->next = saved_hardlinks; |
| 107 | new->inode = inode; |
| 108 | new->entry = cpio_entry; |
| 109 | saved_hardlinks = new; |
| 110 | return(get_header_cpio(src_stream)); /* Recurse to next file */ |
| 111 | } else { /* Found the file with data in */ |
| 112 | struct hardlinks *tmp = saved_hardlinks; |
| 113 | pending_hardlinks = 1; |
| 114 | while (tmp) { |
| 115 | if (tmp->inode == inode) { |
| 116 | tmp->entry->link_name = xstrdup(cpio_entry->name); |
| 117 | nlink--; |
| 118 | } |
| 119 | tmp = tmp->next; |
| 120 | } |
| 121 | if (nlink > 1) error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?"); |
| 122 | } |
| 123 | } |
| 124 | cpio_entry->device = (major << 8) | minor; |
| 125 | break; |
| 126 | default: |
| 127 | error_msg("Unsupported format"); |
| 128 | return(NULL); |
| 129 | } |
| 130 | if (ferror(src_stream) || feof(src_stream)) { |
| 131 | perror_msg("Stream error"); |
| 132 | return(NULL); |
| 133 | } |
| 134 | } |
| 135 | return(cpio_entry); |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 136 | } |
| 137 | |