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.
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.
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.
26 #include "unarchive.h"
29 /* Extract the data postioned at src_stream to either filesystem, stdout or
30 * buffer depending on the value of 'function' which is defined in libbb.h
32 * prefix doesnt have to be just a directory, it may prefix the filename as well.
34 * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath
35 * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have
36 * 'dpkg.' as their prefix
38 * For this reason if prefix does point to a dir then it must end with a
39 * trailing '/' or else the last dir will be assumed to be the file prefix
41 char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry,
42 const int function, const char *prefix)
44 FILE *dst_stream = NULL;
45 char *full_name = NULL;
49 /* prefix doesnt have to be a proper path it may prepend
50 * the filename as well */
52 /* strip leading '/' in filename to extract as prefix may not be dir */
53 /* Cant use concat_path_file here as prefix might not be a directory */
54 char *path = file_entry->name;
55 if (strncmp("./", path, 2) == 0) {
57 if (strlen(path) == 0) {
61 bb_asprintf(&full_name, "%s%s", prefix, path);
63 full_name = file_entry->name;
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;
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;
80 else if (function & extract_all_to_fs) {
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 */
90 if ((function & extract_quiet) != extract_quiet) {
91 error_msg("%s not created: newer or same age file exists", file_entry->name);
93 seek_sub_file(src_stream, file_entry->size);
97 if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */
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");
108 switch(file_entry->mode & S_IFMT) {
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);
118 if ((dst_stream = wfopen(full_name, "w")) == NULL) {
119 seek_sub_file(src_stream, file_entry->size);
122 archive_offset += file_entry->size;
123 if (file_entry->extract_func) file_entry->extract_func(src_stream, dst_stream);
124 else copy_file_chunk(src_stream, dst_stream, file_entry->size);
130 if (mkdir(full_name, file_entry->mode) < 0) {
131 if ((function & extract_quiet) != extract_quiet) {
132 perror_msg("extract_archive: %s", full_name);
138 if (symlink(file_entry->link_name, full_name) < 0) {
139 if ((function & extract_quiet) != extract_quiet) {
140 perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name);
149 if (mknod(full_name, file_entry->mode, file_entry->device) == -1) {
150 if ((function & extract_quiet) != extract_quiet) {
151 perror_msg("Cannot create node %s", file_entry->name);
158 /* Changing a symlink's properties normally changes the properties of the
159 * file pointed to, so dont try and change the date or mode, lchown does
160 * does the right thing, but isnt available in older versions of libc */
161 if (S_ISLNK(file_entry->mode)) {
162 #if (__GLIBC__ > 2) && (__GLIBC_MINOR__ > 1)
163 lchown(full_name, file_entry->uid, file_entry->gid);
166 if (function & extract_preserve_date) {
167 t.actime = file_entry->mtime;
168 t.modtime = file_entry->mtime;
169 utime(full_name, &t);
171 chmod(full_name, file_entry->mode);
172 chown(full_name, file_entry->uid, file_entry->gid);
175 /* If we arent extracting data we have to skip it,
176 * if data size is 0 then then just do it anyway
177 * (saves testing for it) */
178 seek_sub_file(src_stream, file_entry->size);
181 /* extract_list and extract_verbose_list can be used in conjunction
182 * with one of the above four extraction functions, so do this seperately */
183 if (function & extract_verbose_list) {
184 fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode),
185 file_entry->uid, file_entry->gid,
186 (int) file_entry->size, time_string(file_entry->mtime));
188 if ((function & extract_list) || (function & extract_verbose_list)){
189 /* fputs doesnt add a trailing \n, so use fprintf */
190 fprintf(out_stream, "%s\n", full_name);
193 if (prefix != NULL) {
197 return(NULL); /* Maybe we should say if failed */
200 char *unarchive(FILE *src_stream, FILE *out_stream, file_header_t *(*get_headers)(FILE *),
201 const int extract_function, const char *prefix, char **include_name, char **exclude_name)
203 file_header_t *file_entry;
204 int extract_flag = TRUE;
207 #ifdef CONFIG_FEATURE_UNARCHIVE_TAPE
208 int pid, tape_pipe[2];
210 if (pipe(tape_pipe) != 0) error_msg_and_die("Can't create pipe\n");
211 if ((pid = fork()) == -1) error_msg_and_die("Fork failed\n");
212 if (pid==0) { /* child process */
213 close(tape_pipe[0]); /* We don't wan't to read from the pipe */
214 copyfd(fileno(src_stream), tape_pipe[1]);
215 close(tape_pipe[1]); /* Send EOF */
219 close(tape_pipe[1]); /* Don't want to write down the pipe */
221 src_stream = fdopen(tape_pipe[0], "r");
224 while ((file_entry = get_headers(src_stream)) != NULL) {
226 if (include_name != NULL) {
227 extract_flag = FALSE;
228 for(i = 0; include_name[i] != 0; i++) {
229 if (fnmatch(include_name[i], file_entry->name, FNM_LEADING_DIR) == 0) {
238 /* If the file entry is in the exclude list dont extract it */
239 if (exclude_name != NULL) {
240 for(i = 0; exclude_name[i] != 0; i++) {
241 if (fnmatch(exclude_name[i], file_entry->name, FNM_LEADING_DIR) == 0) {
242 extract_flag = FALSE;
249 buffer = extract_archive(src_stream, out_stream, file_entry, extract_function, prefix);
251 /* seek past the data entry */
252 seek_sub_file(src_stream, file_entry->size);
254 free(file_entry->name);
255 free(file_entry->link_name);
258 #ifdef CONFIG_FEATURE_UNARCHIVE_TAPE
260 waitpid(pid, NULL, 0);