Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000 by Glenn McGrath |
| 3 | * Copyright (C) 2001 by Laurence Anderson |
| 4 | * |
| 5 | * Based on previous work by busybox developers and others. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Library General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <errno.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <unistd.h> |
| 27 | #include <utime.h> |
| 28 | #include "libbb.h" |
| 29 | |
Eric Andersen | 30f1eaf | 2001-06-22 03:00:21 +0000 | [diff] [blame] | 30 | extern void seek_sub_file(FILE *src_stream, const int count); |
| 31 | extern char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry, |
| 32 | const int function, const char *prefix); |
| 33 | |
| 34 | |
| 35 | #ifdef L_archive_offset |
| 36 | off_t archive_offset; |
| 37 | #else |
| 38 | extern off_t archive_offset; |
| 39 | #endif |
| 40 | |
| 41 | #ifdef L_seek_sub_file |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 42 | void seek_sub_file(FILE *src_stream, const int count) |
| 43 | { |
| 44 | int i; |
| 45 | /* Try to fseek as faster */ |
| 46 | archive_offset += count; |
| 47 | if (fseek(src_stream, count, SEEK_CUR) != 0 && errno == ESPIPE) { |
| 48 | for (i = 0; i < count; i++) { |
| 49 | fgetc(src_stream); |
| 50 | } |
| 51 | } |
| 52 | return; |
| 53 | } |
Eric Andersen | 30f1eaf | 2001-06-22 03:00:21 +0000 | [diff] [blame] | 54 | #endif |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 55 | |
| 56 | |
Eric Andersen | 30f1eaf | 2001-06-22 03:00:21 +0000 | [diff] [blame] | 57 | |
| 58 | #ifdef L_extract_archive |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 59 | /* Extract the data postioned at src_stream to either filesystem, stdout or |
| 60 | * buffer depending on the value of 'function' which is defined in libbb.h |
| 61 | * |
| 62 | * prefix doesnt have to be just a directory, it may prefix the filename as well. |
| 63 | * |
| 64 | * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath |
| 65 | * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have |
| 66 | * 'dpkg.' as their prefix |
| 67 | * |
| 68 | * For this reason if prefix does point to a dir then it must end with a |
| 69 | * trailing '/' or else the last dir will be assumed to be the file prefix |
| 70 | */ |
| 71 | char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry, |
| 72 | const int function, const char *prefix) |
| 73 | { |
| 74 | FILE *dst_stream = NULL; |
| 75 | char *full_name = NULL; |
| 76 | char *buffer = NULL; |
| 77 | struct utimbuf t; |
| 78 | |
| 79 | /* prefix doesnt have to be a proper path it may prepend |
| 80 | * the filename as well */ |
| 81 | if (prefix != NULL) { |
| 82 | /* strip leading '/' in filename to extract as prefix may not be dir */ |
| 83 | /* Cant use concat_path_file here as prefix might not be a directory */ |
| 84 | char *path = file_entry->name; |
| 85 | if (*path == '/') { |
| 86 | path++; |
| 87 | } |
| 88 | full_name = xmalloc(strlen(prefix) + strlen(path) + 1); |
| 89 | strcpy(full_name, prefix); |
| 90 | strcat(full_name, path); |
| 91 | } else { |
| 92 | full_name = file_entry->name; |
| 93 | } |
| 94 | |
| 95 | if (function & extract_to_stdout) { |
| 96 | if (S_ISREG(file_entry->mode)) { |
| 97 | copy_file_chunk(src_stream, out_stream, file_entry->size); |
| 98 | archive_offset += file_entry->size; |
| 99 | } |
| 100 | } |
| 101 | else if (function & extract_one_to_buffer) { |
| 102 | if (S_ISREG(file_entry->mode)) { |
| 103 | buffer = (char *) xmalloc(file_entry->size + 1); |
| 104 | fread(buffer, 1, file_entry->size, src_stream); |
Glenn L McGrath | cfeb08a | 2001-07-13 17:59:48 +0000 | [diff] [blame] | 105 | buffer[file_entry->size] = '\0'; |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 106 | archive_offset += file_entry->size; |
| 107 | return(buffer); |
| 108 | } |
| 109 | } |
| 110 | else if (function & extract_all_to_fs) { |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 111 | struct stat oldfile; |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 112 | int stat_res; |
| 113 | stat_res = lstat (full_name, &oldfile); |
| 114 | if (stat_res == 0) { /* The file already exists */ |
| 115 | if ((function & extract_unconditional) || (oldfile.st_mtime < file_entry->mtime)) { |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 116 | if (!S_ISDIR(oldfile.st_mode)) { |
| 117 | unlink(full_name); /* Directories might not be empty etc */ |
| 118 | } |
| 119 | } else { |
Glenn L McGrath | 3e94f72 | 2001-07-12 10:24:57 +0000 | [diff] [blame] | 120 | if ((function & extract_quiet) != extract_quiet) { |
| 121 | error_msg("%s not created: newer or same age file exists", file_entry->name); |
| 122 | } |
| 123 | seek_sub_file(src_stream, file_entry->size); |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 124 | return (NULL); |
| 125 | } |
| 126 | } |
Glenn L McGrath | 2e6d3cf | 2001-06-24 12:36:54 +0000 | [diff] [blame] | 127 | if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */ |
| 128 | char *parent = dirname(full_name); |
| 129 | if (make_directory (parent, -1, FILEUTILS_RECUR) != 0) { |
Glenn L McGrath | 3e94f72 | 2001-07-12 10:24:57 +0000 | [diff] [blame] | 130 | if ((function & extract_quiet) != extract_quiet) { |
| 131 | error_msg("couldn't create leading directories"); |
| 132 | } |
Glenn L McGrath | 2e6d3cf | 2001-06-24 12:36:54 +0000 | [diff] [blame] | 133 | } |
| 134 | free (parent); |
| 135 | } |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 136 | switch(file_entry->mode & S_IFMT) { |
| 137 | case S_IFREG: |
| 138 | if (file_entry->link_name) { /* Found a cpio hard link */ |
| 139 | if (link(file_entry->link_name, full_name) != 0) { |
Glenn L McGrath | 3e94f72 | 2001-07-12 10:24:57 +0000 | [diff] [blame] | 140 | if ((function & extract_quiet) != extract_quiet) { |
| 141 | perror_msg("Cannot link from %s to '%s'", |
| 142 | file_entry->name, file_entry->link_name); |
| 143 | } |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 144 | } |
| 145 | } else { |
| 146 | if ((dst_stream = wfopen(full_name, "w")) == NULL) { |
| 147 | seek_sub_file(src_stream, file_entry->size); |
| 148 | return NULL; |
| 149 | } |
| 150 | archive_offset += file_entry->size; |
| 151 | copy_file_chunk(src_stream, dst_stream, file_entry->size); |
| 152 | fclose(dst_stream); |
| 153 | } |
| 154 | break; |
| 155 | case S_IFDIR: |
Glenn L McGrath | 2e6d3cf | 2001-06-24 12:36:54 +0000 | [diff] [blame] | 156 | if (stat_res != 0) { |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 157 | if (mkdir(full_name, file_entry->mode) < 0) { |
Glenn L McGrath | 3e94f72 | 2001-07-12 10:24:57 +0000 | [diff] [blame] | 158 | if ((function & extract_quiet) != extract_quiet) { |
| 159 | perror_msg("extract_archive: "); |
| 160 | } |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | break; |
| 164 | case S_IFLNK: |
| 165 | if (symlink(file_entry->link_name, full_name) < 0) { |
Glenn L McGrath | 3e94f72 | 2001-07-12 10:24:57 +0000 | [diff] [blame] | 166 | if ((function & extract_quiet) != extract_quiet) { |
| 167 | perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name); |
| 168 | } |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 169 | return NULL; |
| 170 | } |
| 171 | break; |
| 172 | case S_IFSOCK: |
| 173 | case S_IFBLK: |
| 174 | case S_IFCHR: |
| 175 | case S_IFIFO: |
| 176 | if (mknod(full_name, file_entry->mode, file_entry->device) == -1) { |
Glenn L McGrath | 3e94f72 | 2001-07-12 10:24:57 +0000 | [diff] [blame] | 177 | if ((function & extract_quiet) != extract_quiet) { |
| 178 | perror_msg("Cannot create node %s", file_entry->name); |
| 179 | } |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 180 | return NULL; |
| 181 | } |
| 182 | break; |
| 183 | } |
Glenn L McGrath | 76ce754 | 2001-06-22 02:55:16 +0000 | [diff] [blame] | 184 | |
| 185 | /* Changing a symlink's properties normally changes the properties of the |
| 186 | * file pointed to, so dont try and change the date or mode, lchown does |
| 187 | * does the right thing, but isnt available in older versions of libc */ |
| 188 | if (S_ISLNK(file_entry->mode)) { |
| 189 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
| 190 | lchown(full_name, file_entry->uid, file_entry->gid); |
| 191 | #endif |
| 192 | } else { |
| 193 | if (function & extract_preserve_date) { |
| 194 | t.actime = file_entry->mtime; |
| 195 | t.modtime = file_entry->mtime; |
| 196 | utime(full_name, &t); |
| 197 | } |
| 198 | chmod(full_name, file_entry->mode); |
| 199 | chown(full_name, file_entry->uid, file_entry->gid); |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 200 | } |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 201 | } else { |
| 202 | /* If we arent extracting data we have to skip it, |
| 203 | * if data size is 0 then then just do it anyway |
| 204 | * (saves testing for it) */ |
| 205 | seek_sub_file(src_stream, file_entry->size); |
| 206 | } |
| 207 | |
| 208 | /* extract_list and extract_verbose_list can be used in conjunction |
| 209 | * with one of the above four extraction functions, so do this seperately */ |
| 210 | if (function & extract_verbose_list) { |
| 211 | fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode), |
| 212 | file_entry->uid, file_entry->gid, |
| 213 | (int) file_entry->size, time_string(file_entry->mtime)); |
| 214 | } |
| 215 | if ((function & extract_list) || (function & extract_verbose_list)){ |
| 216 | /* fputs doesnt add a trailing \n, so use fprintf */ |
| 217 | fprintf(out_stream, "%s\n", file_entry->name); |
| 218 | } |
| 219 | |
| 220 | free(full_name); |
| 221 | |
| 222 | return(NULL); /* Maybe we should say if failed */ |
| 223 | } |
Eric Andersen | 30f1eaf | 2001-06-22 03:00:21 +0000 | [diff] [blame] | 224 | #endif |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 225 | |
Eric Andersen | 30f1eaf | 2001-06-22 03:00:21 +0000 | [diff] [blame] | 226 | #ifdef L_unarchive |
Glenn L McGrath | c127008 | 2001-07-11 17:32:14 +0000 | [diff] [blame] | 227 | char *unarchive(FILE *src_stream, FILE *out_stream, file_header_t *(*get_headers)(FILE *), |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 228 | const int extract_function, const char *prefix, char **extract_names) |
| 229 | { |
| 230 | file_header_t *file_entry; |
| 231 | int found; |
| 232 | int i; |
| 233 | char *buffer = NULL; |
| 234 | |
| 235 | archive_offset = 0; |
Eric Andersen | 5ef5614 | 2001-06-26 16:36:26 +0000 | [diff] [blame] | 236 | while ((file_entry = get_headers(src_stream)) != NULL) { |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 237 | found = FALSE; |
Glenn L McGrath | a868ec8 | 2001-07-14 08:49:53 +0000 | [diff] [blame] | 238 | if (extract_names == NULL) { |
| 239 | found = TRUE; |
| 240 | } else { |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 241 | for(i = 0; extract_names[i] != 0; i++) { |
| 242 | if (strcmp(extract_names[i], file_entry->name) == 0) { |
| 243 | found = TRUE; |
| 244 | } |
| 245 | } |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 246 | } |
Glenn L McGrath | a868ec8 | 2001-07-14 08:49:53 +0000 | [diff] [blame] | 247 | if (found) { |
| 248 | buffer = extract_archive(src_stream, out_stream, file_entry, extract_function, prefix); |
| 249 | } else { |
| 250 | /* seek past the data entry */ |
| 251 | seek_sub_file(src_stream, file_entry->size); |
| 252 | } |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 253 | } |
| 254 | return(buffer); |
| 255 | } |
Eric Andersen | 30f1eaf | 2001-06-22 03:00:21 +0000 | [diff] [blame] | 256 | #endif |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 257 | |
Eric Andersen | 30f1eaf | 2001-06-22 03:00:21 +0000 | [diff] [blame] | 258 | #ifdef L_get_header_ar |
Eric Andersen | 5ef5614 | 2001-06-26 16:36:26 +0000 | [diff] [blame] | 259 | file_header_t *get_header_ar(FILE *src_stream) |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 260 | { |
| 261 | file_header_t *typed; |
| 262 | union { |
| 263 | char raw[60]; |
| 264 | struct { |
| 265 | char name[16]; |
| 266 | char date[12]; |
| 267 | char uid[6]; |
| 268 | char gid[6]; |
| 269 | char mode[8]; |
| 270 | char size[10]; |
| 271 | char magic[2]; |
| 272 | } formated; |
| 273 | } ar; |
| 274 | static char *ar_long_names; |
| 275 | |
| 276 | if (fread(ar.raw, 1, 60, src_stream) != 60) { |
| 277 | return(NULL); |
| 278 | } |
| 279 | archive_offset += 60; |
| 280 | /* align the headers based on the header magic */ |
| 281 | if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) { |
| 282 | /* some version of ar, have an extra '\n' after each data entry, |
| 283 | * this puts the next header out by 1 */ |
| 284 | if (ar.formated.magic[1] != '`') { |
| 285 | error_msg("Invalid magic"); |
| 286 | return(NULL); |
| 287 | } |
| 288 | /* read the next char out of what would be the data section, |
| 289 | * if its a '\n' then it is a valid header offset by 1*/ |
| 290 | archive_offset++; |
| 291 | if (fgetc(src_stream) != '\n') { |
| 292 | error_msg("Invalid magic"); |
| 293 | return(NULL); |
| 294 | } |
| 295 | /* fix up the header, we started reading 1 byte too early */ |
| 296 | /* raw_header[60] wont be '\n' as it should, but it doesnt matter */ |
| 297 | memmove(ar.raw, &ar.raw[1], 59); |
| 298 | } |
| 299 | |
| 300 | typed = (file_header_t *) xcalloc(1, sizeof(file_header_t)); |
| 301 | |
| 302 | typed->size = (size_t) atoi(ar.formated.size); |
| 303 | /* long filenames have '/' as the first character */ |
| 304 | if (ar.formated.name[0] == '/') { |
| 305 | if (ar.formated.name[1] == '/') { |
| 306 | /* If the second char is a '/' then this entries data section |
| 307 | * stores long filename for multiple entries, they are stored |
| 308 | * in static variable long_names for use in future entries */ |
| 309 | ar_long_names = (char *) xrealloc(ar_long_names, typed->size); |
| 310 | fread(ar_long_names, 1, typed->size, src_stream); |
| 311 | archive_offset += typed->size; |
| 312 | /* This ar entries data section only contained filenames for other records |
| 313 | * they are stored in the static ar_long_names for future reference */ |
Glenn L McGrath | 2e6d3cf | 2001-06-24 12:36:54 +0000 | [diff] [blame] | 314 | return (get_header_ar(src_stream)); /* Return next header */ |
| 315 | } else if (ar.formated.name[1] == ' ') { |
| 316 | /* This is the index of symbols in the file for compilers */ |
| 317 | seek_sub_file(src_stream, typed->size); |
| 318 | return (get_header_ar(src_stream)); /* Return next header */ |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 319 | } else { |
| 320 | /* The number after the '/' indicates the offset in the ar data section |
| 321 | (saved in variable long_name) that conatains the real filename */ |
| 322 | if (!ar_long_names) { |
| 323 | error_msg("Cannot resolve long file name"); |
| 324 | return (NULL); |
| 325 | } |
| 326 | typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1])); |
| 327 | } |
| 328 | } else { |
| 329 | /* short filenames */ |
| 330 | typed->name = xcalloc(1, 16); |
| 331 | strncpy(typed->name, ar.formated.name, 16); |
| 332 | } |
| 333 | typed->name[strcspn(typed->name, " /")]='\0'; |
| 334 | |
| 335 | /* convert the rest of the now valid char header to its typed struct */ |
| 336 | parse_mode(ar.formated.mode, &typed->mode); |
| 337 | typed->mtime = atoi(ar.formated.date); |
| 338 | typed->uid = atoi(ar.formated.uid); |
| 339 | typed->gid = atoi(ar.formated.gid); |
| 340 | |
| 341 | return(typed); |
| 342 | } |
| 343 | #endif |
| 344 | |
Eric Andersen | 30f1eaf | 2001-06-22 03:00:21 +0000 | [diff] [blame] | 345 | #ifdef L_get_header_cpio |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 346 | struct hardlinks { |
| 347 | file_header_t *entry; |
| 348 | int inode; |
| 349 | struct hardlinks *next; |
| 350 | }; |
| 351 | |
Eric Andersen | 5ef5614 | 2001-06-26 16:36:26 +0000 | [diff] [blame] | 352 | file_header_t *get_header_cpio(FILE *src_stream) |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 353 | { |
| 354 | file_header_t *cpio_entry = NULL; |
| 355 | char cpio_header[110]; |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 356 | int namesize; |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 357 | char dummy[16]; |
| 358 | int major, minor, nlink, inode; |
| 359 | static struct hardlinks *saved_hardlinks = NULL; |
| 360 | static int pending_hardlinks = 0; |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 361 | |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 362 | if (pending_hardlinks) { /* Deal with any pending hardlinks */ |
| 363 | struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL; |
| 364 | while (tmp) { |
| 365 | if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */ |
| 366 | cpio_entry = tmp->entry; |
| 367 | if (oldtmp) oldtmp->next = tmp->next; /* Remove item from linked list */ |
| 368 | else saved_hardlinks = tmp->next; |
| 369 | free(tmp); |
| 370 | return (cpio_entry); |
| 371 | } |
| 372 | oldtmp = tmp; |
| 373 | tmp = tmp->next; |
| 374 | } |
| 375 | pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */ |
| 376 | } |
| 377 | |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 378 | /* There can be padding before archive header */ |
| 379 | seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4); |
| 380 | if (fread(cpio_header, 1, 110, src_stream) == 110) { |
| 381 | archive_offset += 110; |
| 382 | if (strncmp(cpio_header, "07070", 5) != 0) { |
| 383 | error_msg("Unsupported format or invalid magic"); |
| 384 | return(NULL); |
| 385 | } |
| 386 | switch (cpio_header[5]) { |
| 387 | case '2': /* "crc" header format */ |
| 388 | /* Doesnt do the crc check yet */ |
| 389 | case '1': /* "newc" header format */ |
| 390 | cpio_entry = (file_header_t *) xcalloc(1, sizeof(file_header_t)); |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 391 | sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c", |
| 392 | dummy, &inode, &cpio_entry->mode, &cpio_entry->uid, &cpio_entry->gid, |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 393 | &nlink, &cpio_entry->mtime, &cpio_entry->size, |
| 394 | dummy, &major, &minor, &namesize, dummy); |
| 395 | |
| 396 | cpio_entry->name = (char *) xcalloc(1, namesize); |
| 397 | fread(cpio_entry->name, 1, namesize, src_stream); /* Read in filename */ |
| 398 | archive_offset += namesize; |
| 399 | /* Skip padding before file contents */ |
| 400 | seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4); |
| 401 | if (strcmp(cpio_entry->name, "TRAILER!!!") == 0) { |
| 402 | printf("%d blocks\n", (int) (archive_offset % 512 ? (archive_offset / 512) + 1 : archive_offset / 512)); /* Always round up */ |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 403 | if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */ |
| 404 | struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL; |
| 405 | while (tmp) { |
| 406 | error_msg("%s not created: cannot resolve hardlink", tmp->entry->name); |
| 407 | oldtmp = tmp; |
| 408 | tmp = tmp->next; |
| 409 | free (oldtmp->entry->name); |
| 410 | free (oldtmp->entry); |
| 411 | free (oldtmp); |
| 412 | } |
| 413 | saved_hardlinks = NULL; |
| 414 | pending_hardlinks = 0; |
| 415 | } |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 416 | return(NULL); |
| 417 | } |
| 418 | |
| 419 | if (S_ISLNK(cpio_entry->mode)) { |
| 420 | cpio_entry->link_name = (char *) xcalloc(1, cpio_entry->size + 1); |
| 421 | fread(cpio_entry->link_name, 1, cpio_entry->size, src_stream); |
| 422 | archive_offset += cpio_entry->size; |
Glenn L McGrath | 2e6d3cf | 2001-06-24 12:36:54 +0000 | [diff] [blame] | 423 | cpio_entry->size = 0; /* Stop possiable seeks in future */ |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 424 | } |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 425 | if (nlink > 1 && !S_ISDIR(cpio_entry->mode)) { |
| 426 | if (cpio_entry->size == 0) { /* Put file on a linked list for later */ |
| 427 | struct hardlinks *new = xmalloc(sizeof(struct hardlinks)); |
| 428 | new->next = saved_hardlinks; |
| 429 | new->inode = inode; |
| 430 | new->entry = cpio_entry; |
| 431 | saved_hardlinks = new; |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 432 | return(get_header_cpio(src_stream)); /* Recurse to next file */ |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 433 | } else { /* Found the file with data in */ |
| 434 | struct hardlinks *tmp = saved_hardlinks; |
| 435 | pending_hardlinks = 1; |
| 436 | while (tmp) { |
| 437 | if (tmp->inode == inode) { |
| 438 | tmp->entry->link_name = xstrdup(cpio_entry->name); |
| 439 | nlink--; |
| 440 | } |
| 441 | tmp = tmp->next; |
| 442 | } |
| 443 | if (nlink > 1) error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?"); |
| 444 | } |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 445 | } |
| 446 | cpio_entry->device = (major << 8) | minor; |
| 447 | break; |
| 448 | default: |
| 449 | error_msg("Unsupported format"); |
| 450 | return(NULL); |
| 451 | } |
| 452 | if (ferror(src_stream) || feof(src_stream)) { |
| 453 | perror_msg("Stream error"); |
| 454 | return(NULL); |
| 455 | } |
| 456 | } |
| 457 | return(cpio_entry); |
| 458 | } |
| 459 | #endif |
| 460 | |
Eric Andersen | 30f1eaf | 2001-06-22 03:00:21 +0000 | [diff] [blame] | 461 | #ifdef L_get_header_tar |
Eric Andersen | 5ef5614 | 2001-06-26 16:36:26 +0000 | [diff] [blame] | 462 | file_header_t *get_header_tar(FILE *tar_stream) |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 463 | { |
| 464 | union { |
| 465 | unsigned char raw[512]; |
| 466 | struct { |
| 467 | char name[100]; /* 0-99 */ |
| 468 | char mode[8]; /* 100-107 */ |
| 469 | char uid[8]; /* 108-115 */ |
| 470 | char gid[8]; /* 116-123 */ |
| 471 | char size[12]; /* 124-135 */ |
| 472 | char mtime[12]; /* 136-147 */ |
| 473 | char chksum[8]; /* 148-155 */ |
| 474 | char typeflag; /* 156-156 */ |
| 475 | char linkname[100]; /* 157-256 */ |
| 476 | char magic[6]; /* 257-262 */ |
| 477 | char version[2]; /* 263-264 */ |
| 478 | char uname[32]; /* 265-296 */ |
| 479 | char gname[32]; /* 297-328 */ |
| 480 | char devmajor[8]; /* 329-336 */ |
| 481 | char devminor[8]; /* 337-344 */ |
| 482 | char prefix[155]; /* 345-499 */ |
| 483 | char padding[12]; /* 500-512 */ |
| 484 | } formated; |
| 485 | } tar; |
| 486 | file_header_t *tar_entry = NULL; |
| 487 | long i; |
| 488 | long sum = 0; |
| 489 | |
| 490 | if (archive_offset % 512 != 0) { |
| 491 | seek_sub_file(tar_stream, 512 - (archive_offset % 512)); |
| 492 | } |
| 493 | |
| 494 | if (fread(tar.raw, 1, 512, tar_stream) != 512) { |
Glenn L McGrath | 6fc92a5 | 2001-07-18 03:23:10 +0000 | [diff] [blame] | 495 | /* Unfortunatly its common for tar files to have all sorts of |
| 496 | * trailing garbage, fail silently */ |
| 497 | // error_msg("Couldnt read header"); |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 498 | return(NULL); |
| 499 | } |
| 500 | archive_offset += 512; |
| 501 | |
| 502 | /* Check header has valid magic, unfortunately some tar files |
| 503 | * have empty (0'ed) tar entries at the end, which will |
| 504 | * cause this to fail, so fail silently for now |
| 505 | */ |
| 506 | if (strncmp(tar.formated.magic, "ustar", 5) != 0) { |
| 507 | return(NULL); |
| 508 | } |
| 509 | |
| 510 | /* Do checksum on headers */ |
| 511 | for (i = 0; i < 148 ; i++) { |
| 512 | sum += tar.raw[i]; |
| 513 | } |
| 514 | sum += ' ' * 8; |
| 515 | for (i = 156; i < 512 ; i++) { |
| 516 | sum += tar.raw[i]; |
| 517 | } |
| 518 | if (sum != strtol(tar.formated.chksum, NULL, 8)) { |
| 519 | error_msg("Invalid tar header checksum"); |
| 520 | return(NULL); |
| 521 | } |
| 522 | |
| 523 | /* convert to type'ed variables */ |
| 524 | tar_entry = xcalloc(1, sizeof(file_header_t)); |
| 525 | tar_entry->name = xstrdup(tar.formated.name); |
| 526 | |
| 527 | parse_mode(tar.formated.mode, &tar_entry->mode); |
| 528 | tar_entry->uid = strtol(tar.formated.uid, NULL, 8); |
| 529 | tar_entry->gid = strtol(tar.formated.gid, NULL, 8); |
| 530 | tar_entry->size = strtol(tar.formated.size, NULL, 8); |
| 531 | tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8); |
Eric Andersen | 5ef5614 | 2001-06-26 16:36:26 +0000 | [diff] [blame] | 532 | tar_entry->link_name = strlen(tar.formated.linkname) ? |
| 533 | xstrdup(tar.formated.linkname) : NULL; |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 534 | tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) + |
| 535 | strtol(tar.formated.devminor, NULL, 8); |
| 536 | |
| 537 | return(tar_entry); |
| 538 | } |
| 539 | #endif |
| 540 | |
Eric Andersen | 30f1eaf | 2001-06-22 03:00:21 +0000 | [diff] [blame] | 541 | #ifdef L_deb_extract |
Eric Andersen | 5ef5614 | 2001-06-26 16:36:26 +0000 | [diff] [blame] | 542 | char *deb_extract(const char *package_filename, FILE *out_stream, |
| 543 | const int extract_function, const char *prefix, const char *filename) |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 544 | { |
| 545 | FILE *deb_stream; |
| 546 | FILE *uncompressed_stream = NULL; |
| 547 | file_header_t *ar_header = NULL; |
Glenn L McGrath | a868ec8 | 2001-07-14 08:49:53 +0000 | [diff] [blame] | 548 | char **file_list = NULL; |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 549 | char *output_buffer = NULL; |
| 550 | char *ared_file = NULL; |
| 551 | char ar_magic[8]; |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 552 | int gunzip_pid; |
| 553 | |
Glenn L McGrath | a868ec8 | 2001-07-14 08:49:53 +0000 | [diff] [blame] | 554 | if (filename != NULL) { |
| 555 | file_list = xmalloc(sizeof(char *) * 2); |
Glenn L McGrath | 248c57c | 2001-07-11 07:22:17 +0000 | [diff] [blame] | 556 | file_list[0] = xstrdup(filename); |
| 557 | file_list[1] = NULL; |
| 558 | } |
| 559 | |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 560 | if (extract_function & extract_control_tar_gz) { |
| 561 | ared_file = xstrdup("control.tar.gz"); |
| 562 | } |
| 563 | else if (extract_function & extract_data_tar_gz) { |
| 564 | ared_file = xstrdup("data.tar.gz"); |
| 565 | } |
| 566 | |
| 567 | /* open the debian package to be worked on */ |
| 568 | deb_stream = wfopen(package_filename, "r"); |
Glenn L McGrath | 3d46224 | 2001-07-13 18:16:57 +0000 | [diff] [blame] | 569 | if (deb_stream == NULL) { |
| 570 | return(NULL); |
| 571 | } |
Glenn L McGrath | b028e08 | 2001-07-13 06:43:03 +0000 | [diff] [blame] | 572 | /* set the buffer size */ |
| 573 | setvbuf(deb_stream, NULL, _IOFBF, 0x8000); |
| 574 | |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 575 | /* check ar magic */ |
| 576 | fread(ar_magic, 1, 8, deb_stream); |
| 577 | if (strncmp(ar_magic,"!<arch>",7) != 0) { |
| 578 | error_msg_and_die("invalid magic"); |
| 579 | } |
| 580 | archive_offset = 8; |
| 581 | |
| 582 | while ((ar_header = get_header_ar(deb_stream)) != NULL) { |
| 583 | if (strcmp(ared_file, ar_header->name) == 0) { |
| 584 | /* open a stream of decompressed data */ |
| 585 | uncompressed_stream = gz_open(deb_stream, &gunzip_pid); |
| 586 | archive_offset = 0; |
Glenn L McGrath | c127008 | 2001-07-11 17:32:14 +0000 | [diff] [blame] | 587 | output_buffer = unarchive(uncompressed_stream, out_stream, get_header_tar, extract_function, prefix, file_list); |
Glenn L McGrath | eb1c940 | 2001-06-20 07:48:00 +0000 | [diff] [blame] | 588 | } |
| 589 | seek_sub_file(deb_stream, ar_header->size); |
| 590 | } |
| 591 | gz_close(gunzip_pid); |
| 592 | fclose(deb_stream); |
| 593 | fclose(uncompressed_stream); |
| 594 | free(ared_file); |
| 595 | return(output_buffer); |
| 596 | } |
| 597 | #endif |