]> wimlib.net Git - wimlib/blob - src/extract.c
1d229a167922d52fcd8efac250fdf67f1d7a0710
[wimlib] / src / extract.c
1 /*
2  * extract.c
3  *
4  * Support for extracting WIM files.
5  */
6
7 /*
8  * Copyright (C) 2010 Carl Thijssen
9  * Copyright (C) 2012 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
13  * wimlib is free software; you can redistribute it and/or modify it under the
14  * terms of the GNU Lesser General Public License as published by the Free
15  * Software Foundation; either version 2.1 of the License, or (at your option)
16  * any later version.
17  *
18  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with wimlib; if not, see http://www.gnu.org/licenses/.
25  */
26
27 #include "wimlib_internal.h"
28 #include "dentry.h"
29 #include "lookup_table.h"
30 #include "xml.h"
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <dirent.h>
35 #include <string.h>
36 #include <errno.h>
37
38
39 /* 
40  * Extracts a regular file from the WIM archive. 
41  *
42  * @dentry:             The directory entry for the file, which must be a
43  *                              regular file.
44  * @output_path:        The path to which the file is to be extracted.
45  * @lookup_table:       The lookup table for the WIM file.
46  * @wim_fp:             The FILE* for the WIM, opened for reading.
47  * @wim_ctype:          The type of compression used in the WIM.
48  * @link_type:          One of WIM_LINK_TYPE_*; specifies what to do with
49  *                      files that are hard-linked inside the WIM.
50  * @is_multi_image_extraction: 
51  *                      True if the image currently being extracted is just one 
52  *                      image of a multi-image extraction.  This is needed so
53  *                      that cross-image symbolic links can be created
54  *                      correctly.
55  */
56 static int extract_regular_file(WIMStruct *w, 
57                                 const struct dentry *dentry, 
58                                 const char *output_path)
59 {
60         struct lookup_table *lookup_table;
61         int link_type;
62         bool is_multi_image_extraction;
63         struct lookup_table_entry *lte;
64         int ret;
65         int out_fd;
66         const struct resource_entry *res_entry;
67
68         lookup_table = w->lookup_table;
69         link_type = w->link_type;
70         is_multi_image_extraction = w->is_multi_image_extraction;
71         lte = lookup_resource(lookup_table, dentry->hash);
72
73         /* If we already extracted the same file or a hard link copy of it, we
74          * may be able to simply create a link.  The exact action is specified
75          * by the current @link_type. */
76         if (link_type != WIM_LINK_TYPE_NONE && lte && lte->out_refcnt != 0) {
77                 wimlib_assert(lte->file_on_disk);
78
79                 if (link_type == WIM_LINK_TYPE_HARD) {
80                         if (link(lte->file_on_disk, output_path) != 0) {
81                                 ERROR_WITH_ERRNO("Failed to hard link "
82                                                  "`%s' to `%s'",
83                                                  output_path, lte->file_on_disk);
84                                 return WIMLIB_ERR_LINK;
85                         }
86                 } else {
87                         int num_path_components;
88                         int num_output_dir_path_components;
89                         size_t file_on_disk_len;
90                         char *p;
91                         const char *p2;
92                         size_t i;
93
94                         num_path_components = 
95                                 get_num_path_components(dentry->full_path_utf8) - 1;
96                         num_output_dir_path_components =
97                                 get_num_path_components(w->output_dir);
98
99                         if (is_multi_image_extraction) {
100                                 num_path_components++;
101                                 num_output_dir_path_components--;
102                         }
103                         file_on_disk_len = strlen(lte->file_on_disk);
104
105                         char buf[file_on_disk_len + 3 * num_path_components + 1];
106                         p = &buf[0];
107
108                         for (i = 0; i < num_path_components; i++) {
109                                 *p++ = '.';
110                                 *p++ = '.';
111                                 *p++ = '/';
112                         }
113                         p2 = lte->file_on_disk;
114                         while (*p2 == '/')
115                                 p2++;
116                         while (num_output_dir_path_components--)
117                                 p2 = path_next_part(p2, NULL);
118                         strcpy(p, p2);
119                         if (symlink(buf, output_path) != 0) {
120                                 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
121                                                  "`%s'",
122                                                  buf, lte->file_on_disk);
123                                 return WIMLIB_ERR_LINK;
124                         }
125
126                 }
127                 return 0;
128         } 
129
130         /* Otherwise, we must actually extract the file contents. */
131
132         out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
133         if (out_fd == -1) {
134                 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
135                                  output_path);
136                 return WIMLIB_ERR_OPEN;
137         }
138
139         /* Extract empty file, with no lookup table entry... */
140         if (!lte) {
141                 DEBUG("Empty file `%s'.", output_path);
142                 ret = 0;
143                 goto done;
144         }
145
146         res_entry = &lte->resource_entry;
147
148         ret = extract_resource_to_fd(w, res_entry, out_fd, 
149                                      res_entry->original_size);
150
151         if (ret != 0) {
152                 ERROR("Failed to extract resource to `%s'", output_path);
153                 goto done;
154         }
155
156         /* Mark the lookup table entry to indicate this file has been extracted. */
157         lte->out_refcnt++;
158         FREE(lte->file_on_disk);
159         lte->file_on_disk = STRDUP(output_path);
160         if (lte->file_on_disk)
161                 ret = 0;
162         else
163                 ret = WIMLIB_ERR_NOMEM;
164 done:
165         close(out_fd);
166         return ret;
167 }
168
169 /* 
170  * Extracts a directory from the WIM archive. 
171  *
172  * @dentry:             The directory entry for the directory.
173  * @output_path:        The path to which the directory is to be extracted to.
174  * @return:             True on success, false on failure. 
175  */
176 static int extract_directory(struct dentry *dentry, const char *output_path)
177 {
178         /* Compute the output path directory to the directory. */
179         if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) 
180         {
181                 switch (errno) {
182                 case EEXIST: /* Already existing directory is OK */
183                 case EACCES: /* We may have permissions to extract files inside
184                                  the directory, but not for the directory
185                                  itself. */
186                         return 0;
187                 default:
188                         ERROR_WITH_ERRNO("Cannot create directory `%s'",
189                                          output_path);
190                         return WIMLIB_ERR_MKDIR;
191                 }
192         }
193         return 0;
194 }
195
196
197 /* 
198  * Extracts a file or directory from the WIM archive.  For use in
199  * for_dentry_in_tree().
200  *
201  * @dentry:     The dentry to extract.
202  * @arg:        A pointer to the WIMStruct for the WIM file.
203  */
204 static int extract_regular_file_or_directory(struct dentry *dentry, void *arg)
205 {
206         WIMStruct *w = (WIMStruct*)arg;
207         size_t len = strlen(w->output_dir);
208         char output_path[len + dentry->full_path_utf8_len + 1];
209
210         if (w->verbose)
211                 puts(dentry->full_path_utf8);
212
213         memcpy(output_path, w->output_dir, len);
214         memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
215         output_path[len + dentry->full_path_utf8_len] = '\0';
216
217
218         if (dentry_is_regular_file(dentry)) {
219                 return extract_regular_file(w, dentry, output_path);
220         } else {
221                 if (dentry_is_root(dentry)) /* Root doesn't need to be extracted. */
222                         return 0;
223                 else
224                         return extract_directory(dentry, output_path);
225         }
226 }
227
228 static int extract_single_image(WIMStruct *w, int image)
229 {
230         DEBUG("Extracting image %d", image);
231
232         int ret;
233         ret = wimlib_select_image(w, image);
234         if (ret != 0)
235                 return ret;
236
237         return for_dentry_in_tree(wim_root_dentry(w),
238                                   extract_regular_file_or_directory, w);
239 }
240
241
242 /* Extracts all images from the WIM to w->output_dir, with the images placed in
243  * subdirectories named by their image names. */
244 static int extract_all_images(WIMStruct *w)
245 {
246         size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
247         size_t output_path_len = strlen(w->output_dir);
248         char buf[output_path_len + 1 + image_name_max_len + 1];
249         int ret;
250         int image;
251         const char *image_name;
252
253         DEBUG("Attempting to extract all images from `%s'", w->filename);
254
255         memcpy(buf, w->output_dir, output_path_len);
256         buf[output_path_len] = '/';
257         for (image = 1; image <= w->hdr.image_count; image++) {
258                 
259                 image_name = wimlib_get_image_name(w, image);
260                 if (*image_name) {
261                         strcpy(buf + output_path_len + 1, image_name);
262                 } else {
263                         /* Image name is empty. Use image number instead */
264                         sprintf(buf + output_path_len + 1, "%d", image);
265                 }
266                 ret = wimlib_set_output_dir(w, buf);
267                 if (ret != 0)
268                         goto done;
269                 ret = extract_single_image(w, image);
270                 if (ret != 0)
271                         goto done;
272         }
273 done:
274         /* Restore original output directory */
275         buf[output_path_len + 1] = '\0';
276         return wimlib_set_output_dir(w, buf);
277 }
278
279 /* Extracts a single image or all images from a WIM file. */
280 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image)
281 {
282         if (!w->output_dir) {
283                 ERROR("No output directory selected.");
284                 return WIMLIB_ERR_NOTDIR;
285         }
286         if (image == WIM_ALL_IMAGES) {
287                 w->is_multi_image_extraction = true;
288                 return extract_all_images(w);
289         } else {
290                 w->is_multi_image_extraction = false;
291                 return extract_single_image(w, image);
292         }
293
294 }
295
296 /* Sets and creates the directory to which files are to be extracted when
297  * extracting files from the WIM. */
298 WIMLIBAPI int wimlib_set_output_dir(WIMStruct *w, const char *dir)
299 {
300         char *p;
301         DEBUG("Setting output directory to `%s'", dir);
302
303         if (!dir) {
304                 ERROR("Must specify a directory!");
305                 return WIMLIB_ERR_INVALID_PARAM;
306         }
307         p = STRDUP(dir);
308         if (!p) {
309                 ERROR("Out of memory");
310                 return WIMLIB_ERR_NOMEM;
311         }
312
313         if (mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
314                 if (errno == EEXIST) {
315                         DEBUG("`%s' already exists", dir);
316                         goto done;
317                 }
318                 ERROR_WITH_ERRNO("Cannot create directory `%s'", dir);
319                 FREE(p);
320                 return WIMLIB_ERR_MKDIR;
321         } else {
322                 DEBUG("Created directory `%s'", dir);
323         }
324 done:
325         FREE(w->output_dir);
326         w->output_dir = p;
327         return 0;
328 }
329
330 WIMLIBAPI int wimlib_set_link_type(WIMStruct *w, int link_type)
331 {
332         switch (link_type) {
333                 case WIM_LINK_TYPE_NONE:
334                 case WIM_LINK_TYPE_HARD:
335                 case WIM_LINK_TYPE_SYMBOLIC:
336                         w->link_type = link_type;
337                         return 0;
338                 default:
339                         return WIMLIB_ERR_INVALID_PARAM;
340         }
341 }
342