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