blob: 199123ec4fb5a50f5406b1222556ac651e1ffa15 [file] [log] [blame]
Glenn L McGratheb1c9402001-06-20 07:48:00 +00001/*
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
30typedef struct file_headers_s {
31 char *name;
32 char *link_name;
33 off_t size;
34 uid_t uid;
35 gid_t gid;
36 mode_t mode;
37 time_t mtime;
38 dev_t device;
39} file_header_t;
40
41off_t archive_offset;
42
43void seek_sub_file(FILE *src_stream, const int count)
44{
45 int i;
46 /* Try to fseek as faster */
47 archive_offset += count;
48 if (fseek(src_stream, count, SEEK_CUR) != 0 && errno == ESPIPE) {
49 for (i = 0; i < count; i++) {
50 fgetc(src_stream);
51 }
52 }
53 return;
54}
55
56
57/* Extract the data postioned at src_stream to either filesystem, stdout or
58 * buffer depending on the value of 'function' which is defined in libbb.h
59 *
60 * prefix doesnt have to be just a directory, it may prefix the filename as well.
61 *
62 * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath
63 * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have
64 * 'dpkg.' as their prefix
65 *
66 * For this reason if prefix does point to a dir then it must end with a
67 * trailing '/' or else the last dir will be assumed to be the file prefix
68 */
69char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry,
70 const int function, const char *prefix)
71{
72 FILE *dst_stream = NULL;
73 char *full_name = NULL;
74 char *buffer = NULL;
75 struct utimbuf t;
76
77 /* prefix doesnt have to be a proper path it may prepend
78 * the filename as well */
79 if (prefix != NULL) {
80 /* strip leading '/' in filename to extract as prefix may not be dir */
81 /* Cant use concat_path_file here as prefix might not be a directory */
82 char *path = file_entry->name;
83 if (*path == '/') {
84 path++;
85 }
86 full_name = xmalloc(strlen(prefix) + strlen(path) + 1);
87 strcpy(full_name, prefix);
88 strcat(full_name, path);
89 } else {
90 full_name = file_entry->name;
91 }
92
93 if (function & extract_to_stdout) {
94 if (S_ISREG(file_entry->mode)) {
95 copy_file_chunk(src_stream, out_stream, file_entry->size);
96 archive_offset += file_entry->size;
97 }
98 }
99 else if (function & extract_one_to_buffer) {
100 if (S_ISREG(file_entry->mode)) {
101 buffer = (char *) xmalloc(file_entry->size + 1);
102 fread(buffer, 1, file_entry->size, src_stream);
103 archive_offset += file_entry->size;
104 return(buffer);
105 }
106 }
107 else if (function & extract_all_to_fs) {
108#if 0
109 struct stat oldfile;
110 if ( (S_ISLNK(file_entry->mode) ? lstat (full_name, &oldfile) : stat (full_name, &oldfile)) == 0) { /* The file already exists */
111 if (function & extract_unconditional || oldfile.st_mtime < file_entry->mtime) {
112 if (!S_ISDIR(oldfile.st_mode)) {
113 unlink(full_name); /* Directories might not be empty etc */
114 }
115 } else {
116 error_msg("%s not created: newer or same age file exists", file_entry->name);
117 if (S_ISREG(file_entry->mode)) {
118 seek_sub_file(src_stream, file_entry->size);
119 }
120 return (NULL);
121 }
122 }
123#endif
124 switch(file_entry->mode & S_IFMT) {
125 case S_IFREG:
126 if (file_entry->link_name) { /* Found a cpio hard link */
127 if (link(file_entry->link_name, full_name) != 0) {
128 perror_msg("Cannot link from %s to '%s'",
129 file_entry->name, file_entry->link_name);
130 }
131 } else {
132 if ((dst_stream = wfopen(full_name, "w")) == NULL) {
133 seek_sub_file(src_stream, file_entry->size);
134 return NULL;
135 }
136 archive_offset += file_entry->size;
137 copy_file_chunk(src_stream, dst_stream, file_entry->size);
138 fclose(dst_stream);
139 }
140 break;
141 case S_IFDIR:
Tim Riker83709762001-06-22 00:27:11 +0000142 /* Use make_directory instead of mkdir in case prefix path hasn't been created */
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000143 if (function & extract_create_dirs) {
Tim Rikerfbbd3602001-06-22 00:14:20 +0000144 if (make_directory(full_name, file_entry->mode, FILEUTILS_RECUR) < 0) {
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000145 return NULL;
146 }
147 }
148 break;
149 case S_IFLNK:
150 if (symlink(file_entry->link_name, full_name) < 0) {
151 perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name);
152 return NULL;
153 }
154 break;
155 case S_IFSOCK:
156 case S_IFBLK:
157 case S_IFCHR:
158 case S_IFIFO:
159 if (mknod(full_name, file_entry->mode, file_entry->device) == -1) {
160 perror_msg("Cannot create node %s", file_entry->name);
161 return NULL;
162 }
163 break;
164 }
Glenn L McGrath76ce7542001-06-22 02:55:16 +0000165
166 /* Changing a symlink's properties normally changes the properties of the
167 * file pointed to, so dont try and change the date or mode, lchown does
168 * does the right thing, but isnt available in older versions of libc */
169 if (S_ISLNK(file_entry->mode)) {
170#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
171 lchown(full_name, file_entry->uid, file_entry->gid);
172#endif
173 } else {
174 if (function & extract_preserve_date) {
175 t.actime = file_entry->mtime;
176 t.modtime = file_entry->mtime;
177 utime(full_name, &t);
178 }
179 chmod(full_name, file_entry->mode);
180 chown(full_name, file_entry->uid, file_entry->gid);
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000181 }
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000182 } else {
183 /* If we arent extracting data we have to skip it,
184 * if data size is 0 then then just do it anyway
185 * (saves testing for it) */
186 seek_sub_file(src_stream, file_entry->size);
187 }
188
189 /* extract_list and extract_verbose_list can be used in conjunction
190 * with one of the above four extraction functions, so do this seperately */
191 if (function & extract_verbose_list) {
192 fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode),
193 file_entry->uid, file_entry->gid,
194 (int) file_entry->size, time_string(file_entry->mtime));
195 }
196 if ((function & extract_list) || (function & extract_verbose_list)){
197 /* fputs doesnt add a trailing \n, so use fprintf */
198 fprintf(out_stream, "%s\n", file_entry->name);
199 }
200
201 free(full_name);
202
203 return(NULL); /* Maybe we should say if failed */
204}
205
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000206char *unarchive(FILE *src_stream, void *(*get_headers)(FILE *),
207 const int extract_function, const char *prefix, char **extract_names)
208{
209 file_header_t *file_entry;
210 int found;
211 int i;
212 char *buffer = NULL;
213
214 archive_offset = 0;
215 while ((file_entry = (file_header_t *) get_headers(src_stream)) != NULL) {
216 found = FALSE;
217 if (extract_names[0] != NULL) {
218 for(i = 0; extract_names[i] != 0; i++) {
219 if (strcmp(extract_names[i], file_entry->name) == 0) {
220 found = TRUE;
221 }
222 }
223 if (!found) {
224 /* seek past the data entry */
225 if (!S_ISLNK(file_entry->mode) && file_entry->link_name && file_entry->size == 0) {
226 error_msg("You should extract %s as other files are hardlinked to it", file_entry->name);
227 }
228 seek_sub_file(src_stream, file_entry->size);
229 continue;
230 }
231 }
232 buffer = extract_archive(src_stream, stdout, file_entry, extract_function, prefix);
233 }
234 return(buffer);
235}
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000236
237#if defined BB_AR || defined BB_DPKG_DEB || defined BB_DPKG
238void *get_header_ar(FILE *src_stream)
239{
240 file_header_t *typed;
241 union {
242 char raw[60];
243 struct {
244 char name[16];
245 char date[12];
246 char uid[6];
247 char gid[6];
248 char mode[8];
249 char size[10];
250 char magic[2];
251 } formated;
252 } ar;
253 static char *ar_long_names;
254
255 if (fread(ar.raw, 1, 60, src_stream) != 60) {
256 return(NULL);
257 }
258 archive_offset += 60;
259 /* align the headers based on the header magic */
260 if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
261 /* some version of ar, have an extra '\n' after each data entry,
262 * this puts the next header out by 1 */
263 if (ar.formated.magic[1] != '`') {
264 error_msg("Invalid magic");
265 return(NULL);
266 }
267 /* read the next char out of what would be the data section,
268 * if its a '\n' then it is a valid header offset by 1*/
269 archive_offset++;
270 if (fgetc(src_stream) != '\n') {
271 error_msg("Invalid magic");
272 return(NULL);
273 }
274 /* fix up the header, we started reading 1 byte too early */
275 /* raw_header[60] wont be '\n' as it should, but it doesnt matter */
276 memmove(ar.raw, &ar.raw[1], 59);
277 }
278
279 typed = (file_header_t *) xcalloc(1, sizeof(file_header_t));
280
281 typed->size = (size_t) atoi(ar.formated.size);
282 /* long filenames have '/' as the first character */
283 if (ar.formated.name[0] == '/') {
284 if (ar.formated.name[1] == '/') {
285 /* If the second char is a '/' then this entries data section
286 * stores long filename for multiple entries, they are stored
287 * in static variable long_names for use in future entries */
288 ar_long_names = (char *) xrealloc(ar_long_names, typed->size);
289 fread(ar_long_names, 1, typed->size, src_stream);
290 archive_offset += typed->size;
291 /* This ar entries data section only contained filenames for other records
292 * they are stored in the static ar_long_names for future reference */
293 return(NULL);
294 } else {
295 /* The number after the '/' indicates the offset in the ar data section
296 (saved in variable long_name) that conatains the real filename */
297 if (!ar_long_names) {
298 error_msg("Cannot resolve long file name");
299 return (NULL);
300 }
301 typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1]));
302 }
303 } else {
304 /* short filenames */
305 typed->name = xcalloc(1, 16);
306 strncpy(typed->name, ar.formated.name, 16);
307 }
308 typed->name[strcspn(typed->name, " /")]='\0';
309
310 /* convert the rest of the now valid char header to its typed struct */
311 parse_mode(ar.formated.mode, &typed->mode);
312 typed->mtime = atoi(ar.formated.date);
313 typed->uid = atoi(ar.formated.uid);
314 typed->gid = atoi(ar.formated.gid);
315
316 return(typed);
317}
318#endif
319
320#if defined BB_CPIO
321void *get_header_cpio(FILE *src_stream)
322{
323 file_header_t *cpio_entry = NULL;
324 char cpio_header[110];
325 char dummy[14];
326 int namesize;
327 int major, minor, nlink;
328
329 /* There can be padding before archive header */
330 seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
331 if (fread(cpio_header, 1, 110, src_stream) == 110) {
332 archive_offset += 110;
333 if (strncmp(cpio_header, "07070", 5) != 0) {
334 error_msg("Unsupported format or invalid magic");
335 return(NULL);
336 }
337 switch (cpio_header[5]) {
338 case '2': /* "crc" header format */
339 /* Doesnt do the crc check yet */
340 case '1': /* "newc" header format */
341 cpio_entry = (file_header_t *) xcalloc(1, sizeof(file_header_t));
342 sscanf(cpio_header, "%14c%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
343 dummy, &cpio_entry->mode, &cpio_entry->uid, &cpio_entry->gid,
344 &nlink, &cpio_entry->mtime, &cpio_entry->size,
345 dummy, &major, &minor, &namesize, dummy);
346
347 cpio_entry->name = (char *) xcalloc(1, namesize);
348 fread(cpio_entry->name, 1, namesize, src_stream); /* Read in filename */
349 archive_offset += namesize;
350 /* Skip padding before file contents */
351 seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
352 if (strcmp(cpio_entry->name, "TRAILER!!!") == 0) {
353 printf("%d blocks\n", (int) (archive_offset % 512 ? (archive_offset / 512) + 1 : archive_offset / 512)); /* Always round up */
354 return(NULL);
355 }
356
357 if (S_ISLNK(cpio_entry->mode)) {
358 cpio_entry->link_name = (char *) xcalloc(1, cpio_entry->size + 1);
359 fread(cpio_entry->link_name, 1, cpio_entry->size, src_stream);
360 archive_offset += cpio_entry->size;
361 }
362 if (nlink > 1 && !S_ISDIR(cpio_entry->mode) && cpio_entry->size == 0) {
363 error_msg("%s not extracted: Cannot handle hard links yet", cpio_entry->name);
364 return(get_header_cpio(src_stream)); /* Recurse to next file */
365 }
366 cpio_entry->device = (major << 8) | minor;
367 break;
368 default:
369 error_msg("Unsupported format");
370 return(NULL);
371 }
372 if (ferror(src_stream) || feof(src_stream)) {
373 perror_msg("Stream error");
374 return(NULL);
375 }
376 }
377 return(cpio_entry);
378}
379#endif
380
381#if defined BB_UNTAR || defined BB_DPKG_DEB || defined BB_DPKG
382void *get_header_tar(FILE *tar_stream)
383{
384 union {
385 unsigned char raw[512];
386 struct {
387 char name[100]; /* 0-99 */
388 char mode[8]; /* 100-107 */
389 char uid[8]; /* 108-115 */
390 char gid[8]; /* 116-123 */
391 char size[12]; /* 124-135 */
392 char mtime[12]; /* 136-147 */
393 char chksum[8]; /* 148-155 */
394 char typeflag; /* 156-156 */
395 char linkname[100]; /* 157-256 */
396 char magic[6]; /* 257-262 */
397 char version[2]; /* 263-264 */
398 char uname[32]; /* 265-296 */
399 char gname[32]; /* 297-328 */
400 char devmajor[8]; /* 329-336 */
401 char devminor[8]; /* 337-344 */
402 char prefix[155]; /* 345-499 */
403 char padding[12]; /* 500-512 */
404 } formated;
405 } tar;
406 file_header_t *tar_entry = NULL;
407 long i;
408 long sum = 0;
409
410 if (archive_offset % 512 != 0) {
411 seek_sub_file(tar_stream, 512 - (archive_offset % 512));
412 }
413
414 if (fread(tar.raw, 1, 512, tar_stream) != 512) {
415 error_msg("Couldnt read header");
416 return(NULL);
417 }
418 archive_offset += 512;
419
420 /* Check header has valid magic, unfortunately some tar files
421 * have empty (0'ed) tar entries at the end, which will
422 * cause this to fail, so fail silently for now
423 */
424 if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
425 return(NULL);
426 }
427
428 /* Do checksum on headers */
429 for (i = 0; i < 148 ; i++) {
430 sum += tar.raw[i];
431 }
432 sum += ' ' * 8;
433 for (i = 156; i < 512 ; i++) {
434 sum += tar.raw[i];
435 }
436 if (sum != strtol(tar.formated.chksum, NULL, 8)) {
437 error_msg("Invalid tar header checksum");
438 return(NULL);
439 }
440
441 /* convert to type'ed variables */
442 tar_entry = xcalloc(1, sizeof(file_header_t));
443 tar_entry->name = xstrdup(tar.formated.name);
444
445 parse_mode(tar.formated.mode, &tar_entry->mode);
446 tar_entry->uid = strtol(tar.formated.uid, NULL, 8);
447 tar_entry->gid = strtol(tar.formated.gid, NULL, 8);
448 tar_entry->size = strtol(tar.formated.size, NULL, 8);
449 tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8);
450 tar_entry->link_name = strlen(tar.formated.linkname) ? xstrdup(tar.formated.linkname) : NULL;
451 tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) +
452 strtol(tar.formated.devminor, NULL, 8);
453
454 return(tar_entry);
455}
456#endif
457
458#if defined BB_DPKG || defined BB_DPKG_DEB
459char *deb_extract(const char *package_filename, FILE *out_stream, const int extract_function,
460 const char *prefix, const char *filename)
461{
462 FILE *deb_stream;
463 FILE *uncompressed_stream = NULL;
464 file_header_t *ar_header = NULL;
465 char *output_buffer = NULL;
466 char *ared_file = NULL;
467 char ar_magic[8];
468 char **file_list;
469 int gunzip_pid;
470
471 file_list = malloc(sizeof(char *));
472 file_list[0] = xstrdup(filename);
473 file_list[1] = NULL;
474
475 if (extract_function & extract_control_tar_gz) {
476 ared_file = xstrdup("control.tar.gz");
477 }
478 else if (extract_function & extract_data_tar_gz) {
479 ared_file = xstrdup("data.tar.gz");
480 }
481
482 /* open the debian package to be worked on */
483 deb_stream = wfopen(package_filename, "r");
484
485 /* check ar magic */
486 fread(ar_magic, 1, 8, deb_stream);
487 if (strncmp(ar_magic,"!<arch>",7) != 0) {
488 error_msg_and_die("invalid magic");
489 }
490 archive_offset = 8;
491
492 while ((ar_header = get_header_ar(deb_stream)) != NULL) {
493 if (strcmp(ared_file, ar_header->name) == 0) {
494 /* open a stream of decompressed data */
495 uncompressed_stream = gz_open(deb_stream, &gunzip_pid);
496 archive_offset = 0;
497 output_buffer = unarchive(uncompressed_stream, get_header_tar, extract_function, prefix, file_list);
498 }
499 seek_sub_file(deb_stream, ar_header->size);
500 }
501 gz_close(gunzip_pid);
502 fclose(deb_stream);
503 fclose(uncompressed_stream);
504 free(ared_file);
505 return(output_buffer);
506}
507#endif