aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile6
-rw-r--r--archival/libunarchive/Makefile57
-rw-r--r--archival/libunarchive/deb_extract.c83
-rw-r--r--archival/libunarchive/get_header_ar.c106
-rw-r--r--archival/libunarchive/get_header_cpio.c136
-rw-r--r--archival/libunarchive/get_header_tar.c99
-rw-r--r--archival/libunarchive/seek_sub_file.c34
-rw-r--r--archival/libunarchive/unarchive.c241
8 files changed, 759 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index c0f7c6587..0ff3afff6 100644
--- a/Makefile
+++ b/Makefile
@@ -201,9 +201,9 @@ do-it-all: menuconfig
201endif 201endif
202 202
203 203
204SUBDIRS =applets archival console-tools editors fileutils findutils init \ 204SUBDIRS =applets archival archival/libunarchive console-tools editors \
205 miscutils modutils networking procps pwd_grp shell shellutils \ 205 fileutils findutils init miscutils modutils networking procps \
206 sysklogd textutils util-linux libbb 206 pwd_grp shell shellutils sysklogd textutils util-linux libbb
207 207
208bbsubdirs: $(patsubst %, _dir_%, $(SUBDIRS)) 208bbsubdirs: $(patsubst %, _dir_%, $(SUBDIRS))
209 209
diff --git a/archival/libunarchive/Makefile b/archival/libunarchive/Makefile
new file mode 100644
index 000000000..0c7219dcd
--- /dev/null
+++ b/archival/libunarchive/Makefile
@@ -0,0 +1,57 @@
1# Makefile for busybox
2#
3# Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13# General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18#
19
20TOPDIR :=../..
21L_TARGET := libunarchive.a
22
23obj-y := unarchive.o seek_sub_file.o
24obj-n :=
25obj- :=
26
27ifeq ($(CONFIG_DPKG),y)
28 obj-y += deb_extract.o get_header_ar.o get_header_tar.o
29endif
30
31ifeq ($(CONFIG_DPKG_DEB),y)
32 obj-y += deb_extract.o get_header_ar.o get_header_tar.o
33endif
34
35ifeq ($(CONFIG_AR),y)
36 obj-y += get_header_ar.o
37endif
38
39ifeq ($(CONFIG_CPIO),y)
40 obj-y += get_header_cpio.o
41endif
42
43ifeq ($(CONFIG_RPM2CPIO),y)
44 obj-y += get_header_cpio.o
45endif
46
47ifeq ($(CONFIG_TAR),y)
48 obj-y += get_header_tar.o
49endif
50
51
52# Hand off to toplevel Rules.mak
53include $(TOPDIR)/Rules.mak
54
55clean:
56 rm -f $(L_TARGET) *.o core
57
diff --git a/archival/libunarchive/deb_extract.c b/archival/libunarchive/deb_extract.c
new file mode 100644
index 000000000..e5b5c9685
--- /dev/null
+++ b/archival/libunarchive/deb_extract.c
@@ -0,0 +1,83 @@
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
23char *deb_extract(const char *package_filename, FILE *out_stream,
24 const int extract_function, const char *prefix, const char *filename)
25{
26 FILE *deb_stream;
27 FILE *uncompressed_stream = NULL;
28 file_header_t *ar_header = NULL;
29 char **file_list = NULL;
30 char *output_buffer = NULL;
31 char *ared_file = NULL;
32 char ar_magic[8];
33 int gunzip_pid;
34
35 if (filename != NULL) {
36 file_list = xmalloc(sizeof(char *) * 2);
37 file_list[0] = xstrdup(filename);
38 file_list[1] = NULL;
39 }
40
41 if (extract_function & extract_control_tar_gz) {
42 ared_file = xstrdup("control.tar.gz");
43 }
44 else if (extract_function & extract_data_tar_gz) {
45 ared_file = xstrdup("data.tar.gz");
46 }
47
48 /* open the debian package to be worked on */
49 deb_stream = wfopen(package_filename, "r");
50 if (deb_stream == NULL) {
51 return(NULL);
52 }
53 /* set the buffer size */
54 setvbuf(deb_stream, NULL, _IOFBF, 0x8000);
55
56 /* check ar magic */
57 fread(ar_magic, 1, 8, deb_stream);
58 if (strncmp(ar_magic,"!<arch>",7) != 0) {
59 error_msg_and_die("invalid magic");
60 }
61 archive_offset = 8;
62
63 while ((ar_header = get_header_ar(deb_stream)) != NULL) {
64 if (strcmp(ared_file, ar_header->name) == 0) {
65 /* open a stream of decompressed data */
66 uncompressed_stream = gz_open(deb_stream, &gunzip_pid);
67 archive_offset = 0;
68 output_buffer = unarchive(uncompressed_stream, out_stream, get_header_tar, extract_function, prefix, file_list, NULL);
69 }
70 seek_sub_file(deb_stream, ar_header->size);
71 free(ar_header->name);
72 free(ar_header);
73 }
74 gz_close(gunzip_pid);
75 fclose(deb_stream);
76 fclose(uncompressed_stream);
77 free(ared_file);
78 if (filename != NULL) {
79 free(file_list[0]);
80 free(file_list);
81 }
82 return(output_buffer);
83} \ No newline at end of file
diff --git a/archival/libunarchive/get_header_ar.c b/archival/libunarchive/get_header_ar.c
new file mode 100644
index 000000000..d2840e0e3
--- /dev/null
+++ b/archival/libunarchive/get_header_ar.c
@@ -0,0 +1,106 @@
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
23file_header_t *get_header_ar(FILE *src_stream)
24{
25 file_header_t *typed;
26 union {
27 char raw[60];
28 struct {
29 char name[16];
30 char date[12];
31 char uid[6];
32 char gid[6];
33 char mode[8];
34 char size[10];
35 char magic[2];
36 } formated;
37 } ar;
38 static char *ar_long_names;
39
40 if (fread(ar.raw, 1, 60, src_stream) != 60) {
41 return(NULL);
42 }
43 archive_offset += 60;
44 /* align the headers based on the header magic */
45 if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
46 /* some version of ar, have an extra '\n' after each data entry,
47 * this puts the next header out by 1 */
48 if (ar.formated.magic[1] != '`') {
49 error_msg("Invalid magic");
50 return(NULL);
51 }
52 /* read the next char out of what would be the data section,
53 * if its a '\n' then it is a valid header offset by 1*/
54 archive_offset++;
55 if (fgetc(src_stream) != '\n') {
56 error_msg("Invalid magic");
57 return(NULL);
58 }
59 /* fix up the header, we started reading 1 byte too early */
60 /* raw_header[60] wont be '\n' as it should, but it doesnt matter */
61 memmove(ar.raw, &ar.raw[1], 59);
62 }
63
64 typed = (file_header_t *) xcalloc(1, sizeof(file_header_t));
65
66 typed->size = (size_t) atoi(ar.formated.size);
67 /* long filenames have '/' as the first character */
68 if (ar.formated.name[0] == '/') {
69 if (ar.formated.name[1] == '/') {
70 /* If the second char is a '/' then this entries data section
71 * stores long filename for multiple entries, they are stored
72 * in static variable long_names for use in future entries */
73 ar_long_names = (char *) xrealloc(ar_long_names, typed->size);
74 fread(ar_long_names, 1, typed->size, src_stream);
75 archive_offset += typed->size;
76 /* This ar entries data section only contained filenames for other records
77 * they are stored in the static ar_long_names for future reference */
78 return (get_header_ar(src_stream)); /* Return next header */
79 } else if (ar.formated.name[1] == ' ') {
80 /* This is the index of symbols in the file for compilers */
81 seek_sub_file(src_stream, typed->size);
82 return (get_header_ar(src_stream)); /* Return next header */
83 } else {
84 /* The number after the '/' indicates the offset in the ar data section
85 (saved in variable long_name) that conatains the real filename */
86 if (!ar_long_names) {
87 error_msg("Cannot resolve long file name");
88 return (NULL);
89 }
90 typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1]));
91 }
92 } else {
93 /* short filenames */
94 typed->name = xcalloc(1, 16);
95 strncpy(typed->name, ar.formated.name, 16);
96 }
97 typed->name[strcspn(typed->name, " /")]='\0';
98
99 /* convert the rest of the now valid char header to its typed struct */
100 parse_mode(ar.formated.mode, &typed->mode);
101 typed->mtime = atoi(ar.formated.date);
102 typed->uid = atoi(ar.formated.uid);
103 typed->gid = atoi(ar.formated.gid);
104
105 return(typed);
106} \ No newline at end of file
diff --git a/archival/libunarchive/get_header_cpio.c b/archival/libunarchive/get_header_cpio.c
new file mode 100644
index 000000000..a2136e441
--- /dev/null
+++ b/archival/libunarchive/get_header_cpio.c
@@ -0,0 +1,136 @@
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
23struct hardlinks {
24 file_header_t *entry;
25 int inode;
26 struct hardlinks *next;
27};
28
29file_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);
136} \ No newline at end of file
diff --git a/archival/libunarchive/get_header_tar.c b/archival/libunarchive/get_header_tar.c
new file mode 100644
index 000000000..639e2dc36
--- /dev/null
+++ b/archival/libunarchive/get_header_tar.c
@@ -0,0 +1,99 @@
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
23file_header_t *get_header_tar(FILE *tar_stream)
24{
25 union {
26 unsigned char raw[512];
27 struct {
28 char name[100]; /* 0-99 */
29 char mode[8]; /* 100-107 */
30 char uid[8]; /* 108-115 */
31 char gid[8]; /* 116-123 */
32 char size[12]; /* 124-135 */
33 char mtime[12]; /* 136-147 */
34 char chksum[8]; /* 148-155 */
35 char typeflag; /* 156-156 */
36 char linkname[100]; /* 157-256 */
37 char magic[6]; /* 257-262 */
38 char version[2]; /* 263-264 */
39 char uname[32]; /* 265-296 */
40 char gname[32]; /* 297-328 */
41 char devmajor[8]; /* 329-336 */
42 char devminor[8]; /* 337-344 */
43 char prefix[155]; /* 345-499 */
44 char padding[12]; /* 500-512 */
45 } formated;
46 } tar;
47 file_header_t *tar_entry = NULL;
48 long sum = 0;
49 long i;
50
51 if (archive_offset % 512 != 0) {
52 seek_sub_file(tar_stream, 512 - (archive_offset % 512));
53 }
54
55 if (fread(tar.raw, 1, 512, tar_stream) != 512) {
56 /* Unfortunatly its common for tar files to have all sorts of
57 * trailing garbage, fail silently */
58// error_msg("Couldnt read header");
59 return(NULL);
60 }
61 archive_offset += 512;
62
63 /* Check header has valid magic, unfortunately some tar files
64 * have empty (0'ed) tar entries at the end, which will
65 * cause this to fail, so fail silently for now
66 */
67 if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
68 return(NULL);
69 }
70
71 /* Do checksum on headers */
72 for (i = 0; i < 148 ; i++) {
73 sum += tar.raw[i];
74 }
75 sum += ' ' * 8;
76 for (i = 156; i < 512 ; i++) {
77 sum += tar.raw[i];
78 }
79 if (sum != strtol(tar.formated.chksum, NULL, 8)) {
80 error_msg("Invalid tar header checksum");
81 return(NULL);
82 }
83
84 /* convert to type'ed variables */
85 tar_entry = xcalloc(1, sizeof(file_header_t));
86 tar_entry->name = xstrdup(tar.formated.name);
87
88 parse_mode(tar.formated.mode, &tar_entry->mode);
89 tar_entry->uid = strtol(tar.formated.uid, NULL, 8);
90 tar_entry->gid = strtol(tar.formated.gid, NULL, 8);
91 tar_entry->size = strtol(tar.formated.size, NULL, 8);
92 tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8);
93 tar_entry->link_name = strlen(tar.formated.linkname) ?
94 xstrdup(tar.formated.linkname) : NULL;
95 tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) +
96 strtol(tar.formated.devminor, NULL, 8);
97
98 return(tar_entry);
99} \ No newline at end of file
diff --git a/archival/libunarchive/seek_sub_file.c b/archival/libunarchive/seek_sub_file.c
new file mode 100644
index 000000000..76cc0bc0c
--- /dev/null
+++ b/archival/libunarchive/seek_sub_file.c
@@ -0,0 +1,34 @@
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 <errno.h>
19#include <stdlib.h>
20
21off_t archive_offset;
22
23void seek_sub_file(FILE *src_stream, const int count)
24{
25 /* Try to fseek as faster */
26 archive_offset += count;
27 if (fseek(src_stream, count, SEEK_CUR) != 0 && errno == ESPIPE) {
28 int i;
29 for (i = 0; i < count; i++) {
30 fgetc(src_stream);
31 }
32 }
33 return;
34} \ No newline at end of file
diff --git a/archival/libunarchive/unarchive.c b/archival/libunarchive/unarchive.c
new file mode 100644
index 000000000..4a42eaf7d
--- /dev/null
+++ b/archival/libunarchive/unarchive.c
@@ -0,0 +1,241 @@
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 <errno.h>
18#include <fnmatch.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23#include <utime.h>
24#include "unarchive.h"
25#include "libbb.h"
26
27/* Extract the data postioned at src_stream to either filesystem, stdout or
28 * buffer depending on the value of 'function' which is defined in libbb.h
29 *
30 * prefix doesnt have to be just a directory, it may prefix the filename as well.
31 *
32 * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath
33 * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have
34 * 'dpkg.' as their prefix
35 *
36 * For this reason if prefix does point to a dir then it must end with a
37 * trailing '/' or else the last dir will be assumed to be the file prefix
38 */
39char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry,
40 const int function, const char *prefix)
41{
42 FILE *dst_stream = NULL;
43 char *full_name = NULL;
44 char *buffer = NULL;
45 struct utimbuf t;
46
47 /* prefix doesnt have to be a proper path it may prepend
48 * the filename as well */
49 if (prefix != NULL) {
50 /* strip leading '/' in filename to extract as prefix may not be dir */
51 /* Cant use concat_path_file here as prefix might not be a directory */
52 char *path = file_entry->name;
53 if (strncmp("./", path, 2) == 0) {
54 path += 2;
55 if (strlen(path) == 0) {
56 return(NULL);
57 }
58 }
59 full_name = xmalloc(strlen(prefix) + strlen(path) + 1);
60 strcpy(full_name, prefix);
61 strcat(full_name, path);
62 } else {
63 full_name = file_entry->name;
64 }
65 if (function & extract_to_stdout) {
66 if (S_ISREG(file_entry->mode)) {
67 copy_file_chunk(src_stream, out_stream, file_entry->size);
68 archive_offset += file_entry->size;
69 }
70 }
71 else if (function & extract_one_to_buffer) {
72 if (S_ISREG(file_entry->mode)) {
73 buffer = (char *) xmalloc(file_entry->size + 1);
74 fread(buffer, 1, file_entry->size, src_stream);
75 buffer[file_entry->size] = '\0';
76 archive_offset += file_entry->size;
77 return(buffer);
78 }
79 }
80 else if (function & extract_all_to_fs) {
81 struct stat oldfile;
82 int stat_res;
83 stat_res = lstat (full_name, &oldfile);
84 if (stat_res == 0) { /* The file already exists */
85 if ((function & extract_unconditional) || (oldfile.st_mtime < file_entry->mtime)) {
86 if (!S_ISDIR(oldfile.st_mode)) {
87 unlink(full_name); /* Directories might not be empty etc */
88 }
89 } else {
90 if ((function & extract_quiet) != extract_quiet) {
91 error_msg("%s not created: newer or same age file exists", file_entry->name);
92 }
93 seek_sub_file(src_stream, file_entry->size);
94 return (NULL);
95 }
96 }
97 if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */
98 char *buf, *parent;
99 buf = xstrdup(full_name);
100 parent = dirname(buf);
101 if (make_directory (parent, -1, FILEUTILS_RECUR) != 0) {
102 if ((function & extract_quiet) != extract_quiet) {
103 error_msg("couldn't create leading directories");
104 }
105 }
106 free (buf);
107 }
108 switch(file_entry->mode & S_IFMT) {
109 case S_IFREG:
110 if (file_entry->link_name) { /* Found a cpio hard link */
111 if (link(file_entry->link_name, full_name) != 0) {
112 if ((function & extract_quiet) != extract_quiet) {
113 perror_msg("Cannot link from %s to '%s'",
114 file_entry->name, file_entry->link_name);
115 }
116 }
117 } else {
118 if ((dst_stream = wfopen(full_name, "w")) == NULL) {
119 seek_sub_file(src_stream, file_entry->size);
120 return NULL;
121 }
122 archive_offset += file_entry->size;
123 copy_file_chunk(src_stream, dst_stream, file_entry->size);
124 fclose(dst_stream);
125 }
126 break;
127 case S_IFDIR:
128 if (stat_res != 0) {
129 if (mkdir(full_name, file_entry->mode) < 0) {
130 if ((function & extract_quiet) != extract_quiet) {
131 perror_msg("extract_archive: %s", full_name);
132 }
133 }
134 }
135 break;
136 case S_IFLNK:
137 if (symlink(file_entry->link_name, full_name) < 0) {
138 if ((function & extract_quiet) != extract_quiet) {
139 perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name);
140 }
141 return NULL;
142 }
143 break;
144 case S_IFSOCK:
145 case S_IFBLK:
146 case S_IFCHR:
147 case S_IFIFO:
148 if (mknod(full_name, file_entry->mode, file_entry->device) == -1) {
149 if ((function & extract_quiet) != extract_quiet) {
150 perror_msg("Cannot create node %s", file_entry->name);
151 }
152 return NULL;
153 }
154 break;
155 }
156
157 /* Changing a symlink's properties normally changes the properties of the
158 * file pointed to, so dont try and change the date or mode, lchown does
159 * does the right thing, but isnt available in older versions of libc */
160 if (S_ISLNK(file_entry->mode)) {
161#if (__GLIBC__ > 2) && (__GLIBC_MINOR__ > 1)
162 lchown(full_name, file_entry->uid, file_entry->gid);
163#endif
164 } else {
165 if (function & extract_preserve_date) {
166 t.actime = file_entry->mtime;
167 t.modtime = file_entry->mtime;
168 utime(full_name, &t);
169 }
170 chmod(full_name, file_entry->mode);
171 chown(full_name, file_entry->uid, file_entry->gid);
172 }
173 } else {
174 /* If we arent extracting data we have to skip it,
175 * if data size is 0 then then just do it anyway
176 * (saves testing for it) */
177 seek_sub_file(src_stream, file_entry->size);
178 }
179
180 /* extract_list and extract_verbose_list can be used in conjunction
181 * with one of the above four extraction functions, so do this seperately */
182 if (function & extract_verbose_list) {
183 fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode),
184 file_entry->uid, file_entry->gid,
185 (int) file_entry->size, time_string(file_entry->mtime));
186 }
187 if ((function & extract_list) || (function & extract_verbose_list)){
188 /* fputs doesnt add a trailing \n, so use fprintf */
189 fprintf(out_stream, "%s\n", full_name);
190 }
191
192 free(full_name);
193
194 return(NULL); /* Maybe we should say if failed */
195}
196
197char *unarchive(FILE *src_stream, FILE *out_stream, file_header_t *(*get_headers)(FILE *),
198 const int extract_function, const char *prefix, char **include_name, char **exclude_name)
199{
200 file_header_t *file_entry;
201 int extract_flag = TRUE;
202 int i;
203 char *buffer = NULL;
204
205 archive_offset = 0;
206 while ((file_entry = get_headers(src_stream)) != NULL) {
207
208 if (include_name != NULL) {
209 extract_flag = FALSE;
210 for(i = 0; include_name[i] != 0; i++) {
211 if (fnmatch(include_name[i], file_entry->name, FNM_LEADING_DIR) == 0) {
212 extract_flag = TRUE;
213 break;
214 }
215 }
216 } else {
217 extract_flag = TRUE;
218 }
219
220 /* If the file entry is in the exclude list dont extract it */
221 if (exclude_name != NULL) {
222 for(i = 0; exclude_name[i] != 0; i++) {
223 if (fnmatch(exclude_name[i], file_entry->name, FNM_LEADING_DIR) == 0) {
224 extract_flag = FALSE;
225 break;
226 }
227 }
228 }
229
230 if (extract_flag == TRUE) {
231 buffer = extract_archive(src_stream, out_stream, file_entry, extract_function, prefix);
232 } else {
233 /* seek past the data entry */
234 seek_sub_file(src_stream, file_entry->size);
235 }
236 free(file_entry->name);
237 free(file_entry->link_name);
238 free(file_entry);
239 }
240 return(buffer);
241} \ No newline at end of file