Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +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 <unistd.h> |
Eric Andersen | 4f807a8 | 2004-07-26 09:11:12 +0000 | [diff] [blame] | 21 | #include <sys/sysmacros.h> /* major() and minor() */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 22 | #include "unarchive.h" |
| 23 | #include "libbb.h" |
| 24 | |
| 25 | typedef struct hardlinks_s { |
| 26 | file_header_t *entry; |
| 27 | int inode; |
| 28 | struct hardlinks_s *next; |
| 29 | } hardlinks_t; |
| 30 | |
| 31 | extern char get_header_cpio(archive_handle_t *archive_handle) |
| 32 | { |
| 33 | static hardlinks_t *saved_hardlinks = NULL; |
| 34 | static unsigned short pending_hardlinks = 0; |
| 35 | file_header_t *file_header = archive_handle->file_header; |
| 36 | char cpio_header[110]; |
| 37 | int namesize; |
| 38 | char dummy[16]; |
| 39 | int major, minor, nlink, inode; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 40 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 41 | if (pending_hardlinks) { /* Deal with any pending hardlinks */ |
| 42 | hardlinks_t *tmp; |
| 43 | hardlinks_t *oldtmp; |
| 44 | |
| 45 | tmp = saved_hardlinks; |
| 46 | oldtmp = NULL; |
| 47 | |
| 48 | while (tmp) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | bb_error_msg_and_die("need to fix this\n"); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 50 | if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */ |
| 51 | file_header = tmp->entry; |
| 52 | if (oldtmp) { |
| 53 | oldtmp->next = tmp->next; /* Remove item from linked list */ |
| 54 | } else { |
| 55 | saved_hardlinks = tmp->next; |
| 56 | } |
| 57 | free(tmp); |
| 58 | continue; |
| 59 | } |
| 60 | oldtmp = tmp; |
| 61 | tmp = tmp->next; |
| 62 | } |
| 63 | pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */ |
| 64 | } |
| 65 | |
| 66 | /* There can be padding before archive header */ |
| 67 | data_align(archive_handle, 4); |
| 68 | |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 69 | if (archive_xread_all_eof(archive_handle, (unsigned char*)cpio_header, 110) == 0) { |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 70 | return(EXIT_FAILURE); |
| 71 | } |
| 72 | archive_handle->offset += 110; |
| 73 | |
Glenn L McGrath | 4cee66d | 2003-08-28 19:12:23 +0000 | [diff] [blame] | 74 | if ((strncmp(&cpio_header[0], "07070", 5) != 0) || ((cpio_header[5] != '1') && (cpio_header[5] != '2'))) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | bb_error_msg_and_die("Unsupported cpio format, use newc or crc"); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Eric Andersen | f4b273c | 2002-12-11 21:45:08 +0000 | [diff] [blame] | 78 | { |
| 79 | unsigned long tmpsize; |
| 80 | sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c", |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 81 | dummy, &inode, (unsigned int*)&file_header->mode, |
Eric Andersen | f4b273c | 2002-12-11 21:45:08 +0000 | [diff] [blame] | 82 | (unsigned int*)&file_header->uid, (unsigned int*)&file_header->gid, |
| 83 | &nlink, &file_header->mtime, &tmpsize, |
| 84 | dummy, &major, &minor, &namesize, dummy); |
| 85 | file_header->size = tmpsize; |
| 86 | } |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 87 | |
| 88 | file_header->name = (char *) xmalloc(namesize + 1); |
| 89 | archive_xread_all(archive_handle, file_header->name, namesize); /* Read in filename */ |
| 90 | file_header->name[namesize] = '\0'; |
| 91 | archive_handle->offset += namesize; |
| 92 | |
| 93 | /* Update offset amount and skip padding before file contents */ |
| 94 | data_align(archive_handle, 4); |
| 95 | |
| 96 | if (strcmp(file_header->name, "TRAILER!!!") == 0) { |
| 97 | printf("%d blocks\n", (int) (archive_handle->offset % 512 ? (archive_handle->offset / 512) + 1 : archive_handle->offset / 512)); /* Always round up */ |
| 98 | if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */ |
| 99 | hardlinks_t *tmp = saved_hardlinks; |
| 100 | hardlinks_t *oldtmp = NULL; |
| 101 | while (tmp) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | bb_error_msg("%s not created: cannot resolve hardlink", tmp->entry->name); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 103 | oldtmp = tmp; |
| 104 | tmp = tmp->next; |
| 105 | free (oldtmp->entry->name); |
| 106 | free (oldtmp->entry); |
| 107 | free (oldtmp); |
| 108 | } |
| 109 | saved_hardlinks = NULL; |
| 110 | pending_hardlinks = 0; |
| 111 | } |
| 112 | return(EXIT_FAILURE); |
| 113 | } |
| 114 | |
| 115 | if (S_ISLNK(file_header->mode)) { |
| 116 | file_header->link_name = (char *) xmalloc(file_header->size + 1); |
| 117 | archive_xread_all(archive_handle, file_header->link_name, file_header->size); |
| 118 | file_header->link_name[file_header->size] = '\0'; |
| 119 | archive_handle->offset += file_header->size; |
| 120 | file_header->size = 0; /* Stop possible seeks in future */ |
Glenn L McGrath | faa3546 | 2004-04-29 09:24:19 +0000 | [diff] [blame] | 121 | } else { |
| 122 | file_header->link_name = NULL; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 123 | } |
| 124 | if (nlink > 1 && !S_ISDIR(file_header->mode)) { |
| 125 | if (file_header->size == 0) { /* Put file on a linked list for later */ |
| 126 | hardlinks_t *new = xmalloc(sizeof(hardlinks_t)); |
| 127 | new->next = saved_hardlinks; |
| 128 | new->inode = inode; |
| 129 | new->entry = file_header; |
| 130 | saved_hardlinks = new; |
| 131 | return(EXIT_SUCCESS); // Skip this one |
| 132 | } else { /* Found the file with data in */ |
| 133 | hardlinks_t *tmp = saved_hardlinks; |
| 134 | pending_hardlinks = 1; |
| 135 | while (tmp) { |
| 136 | if (tmp->inode == inode) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 137 | tmp->entry->link_name = bb_xstrdup(file_header->name); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 138 | nlink--; |
| 139 | } |
| 140 | tmp = tmp->next; |
| 141 | } |
| 142 | if (nlink > 1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 143 | bb_error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?"); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
Eric Andersen | 4f807a8 | 2004-07-26 09:11:12 +0000 | [diff] [blame] | 147 | file_header->device = makedev(major, minor); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 148 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 149 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 150 | archive_handle->action_data(archive_handle); |
Glenn L McGrath | 4cee66d | 2003-08-28 19:12:23 +0000 | [diff] [blame] | 151 | archive_handle->action_header(archive_handle->file_header); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 152 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 153 | data_skip(archive_handle); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 154 | } |
Glenn L McGrath | 4cee66d | 2003-08-28 19:12:23 +0000 | [diff] [blame] | 155 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 156 | archive_handle->offset += file_header->size; |
Glenn L McGrath | faa3546 | 2004-04-29 09:24:19 +0000 | [diff] [blame] | 157 | |
| 158 | free(file_header->link_name); |
| 159 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 160 | return (EXIT_SUCCESS); |
| 161 | } |