blob: 4a42eaf7deb44ae75d2af17c908a9d2322bc4fdd [file] [log] [blame]
Glenn L McGrath95ebf612001-10-25 14:18:08 +00001/*
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}