]> wimlib.net Git - wimlib/blob - src/extract.c
Clean up file headers
[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("Failed to hard link `%s' to `%s': %m\n",
82                                                 output_path, lte->file_on_disk);
83                                 return WIMLIB_ERR_LINK;
84                         }
85                 } else {
86                         int num_path_components;
87                         int num_output_dir_path_components;
88                         size_t file_on_disk_len;
89                         char *p;
90                         const char *p2;
91                         size_t i;
92
93                         num_path_components = 
94                                 get_num_path_components(dentry->full_path_utf8) - 1;
95                         num_output_dir_path_components =
96                                 get_num_path_components(w->output_dir);
97
98                         if (is_multi_image_extraction) {
99                                 num_path_components++;
100                                 num_output_dir_path_components--;
101                         }
102                         file_on_disk_len = strlen(lte->file_on_disk);
103
104                         char buf[file_on_disk_len + 3 * num_path_components + 1];
105                         p = &buf[0];
106
107                         for (i = 0; i < num_path_components; i++) {
108                                 *p++ = '.';
109                                 *p++ = '.';
110                                 *p++ = '/';
111                         }
112                         p2 = lte->file_on_disk;
113                         while (*p2 == '/')
114                                 p2++;
115                         while (num_output_dir_path_components--)
116                                 p2 = path_next_part(p2, NULL);
117                         strcpy(p, p2);
118                         if (symlink(buf, output_path) != 0) {
119                                 ERROR("Failed to symlink `%s' to `%s': %m\n",
120                                                 buf, lte->file_on_disk);
121                                 return WIMLIB_ERR_LINK;
122                         }
123
124                 }
125                 return 0;
126         } 
127
128         /* Otherwise, we must actually extract the file contents. */
129
130         out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
131         if (out_fd == -1) {
132                 ERROR("Failed to open the file `%s' for writing: "
133                                 "%m\n", output_path);
134                 return WIMLIB_ERR_OPEN;
135         }
136
137         /* Extract empty file, with no lookup table entry... */
138         if (!lte) {
139                 DEBUG("Empty file `%s'\n", output_path);
140                 ret = 0;
141                 goto done;
142         }
143
144         res_entry = &lte->resource_entry;
145
146         ret = extract_resource_to_fd(w, res_entry, out_fd, 
147                                      res_entry->original_size);
148
149         if (ret != 0) {
150                 ERROR("Failed to extract resource to `%s'!\n", output_path);
151                 goto done;
152         }
153
154         /* Mark the lookup table entry to indicate this file has been extracted. */
155         lte->out_refcnt++;
156         FREE(lte->file_on_disk);
157         lte->file_on_disk = STRDUP(output_path);
158         if (lte->file_on_disk)
159                 ret = 0;
160         else
161                 ret = WIMLIB_ERR_NOMEM;
162 done:
163         close(out_fd);
164         return ret;
165 }
166
167 /* 
168  * Extracts a directory from the WIM archive. 
169  *
170  * @dentry:             The directory entry for the directory.
171  * @output_path:        The path to which the directory is to be extracted to.
172  * @return:             True on success, false on failure. 
173  */
174 static int extract_directory(struct dentry *dentry, const char *output_path)
175 {
176         /* Compute the output path directory to the directory. */
177         if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) 
178         {
179                 switch (errno) {
180                 case EEXIST: /* Already existing directory is OK */
181                 case EACCES: /* We may have permissions to extract files inside
182                                  the directory, but not for the directory
183                                  itself. */
184                         return 0;
185                 default:
186                         ERROR("Cannot create directory `%s': %m\n",
187                                         output_path);
188                         return WIMLIB_ERR_MKDIR;
189                 }
190         }
191         return 0;
192 }
193
194
195 /* 
196  * Extracts a file or directory from the WIM archive.  For use in
197  * for_dentry_in_tree().
198  *
199  * @dentry:     The dentry to extract.
200  * @arg:        A pointer to the WIMStruct for the WIM file.
201  */
202 static int extract_regular_file_or_directory(struct dentry *dentry, void *arg)
203 {
204         WIMStruct *w = (WIMStruct*)arg;
205         size_t len = strlen(w->output_dir);
206         char output_path[len + dentry->full_path_utf8_len + 1];
207
208         if (w->verbose)
209                 puts(dentry->full_path_utf8);
210
211         memcpy(output_path, w->output_dir, len);
212         memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
213         output_path[len + dentry->full_path_utf8_len] = '\0';
214
215
216         if (dentry_is_regular_file(dentry)) {
217                 return extract_regular_file(w, dentry, output_path);
218         } else {
219                 if (dentry_is_root(dentry)) /* Root doesn't need to be extracted. */
220                         return 0;
221                 else
222                         return extract_directory(dentry, output_path);
223         }
224 }
225
226 static int extract_single_image(WIMStruct *w, int image)
227 {
228         DEBUG("Extracting image %d\n", image);
229
230         int ret;
231         ret = wimlib_select_image(w, image);
232         if (ret != 0)
233                 return ret;
234
235         return for_dentry_in_tree(wim_root_dentry(w),
236                                   extract_regular_file_or_directory, w);
237 }
238
239
240 /* Extracts all images from the WIM to w->output_dir, with the images placed in
241  * subdirectories named by their image names. */
242 static int extract_all_images(WIMStruct *w)
243 {
244         size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
245         size_t output_path_len = strlen(w->output_dir);
246         char buf[output_path_len + 1 + image_name_max_len + 1];
247         int ret;
248         int image;
249         const char *image_name;
250
251         DEBUG("Attempting to extract all images from `%s'\n", w->filename);
252
253         memcpy(buf, w->output_dir, output_path_len);
254         buf[output_path_len] = '/';
255         for (image = 1; image <= w->hdr.image_count; image++) {
256                 buf[output_path_len + 1] = '\0';
257                 
258                 image_name = wimlib_get_image_name(w, image);
259                 if (*image_name) {
260                         strncat(buf + output_path_len + 1, image_name, 
261                                 image_name_max_len);
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         ret = 0;
274 done:
275         buf[output_path_len + 1] = '\0';
276         wimlib_set_output_dir(w, buf);
277         return ret;
278 }
279
280 /* Extracts a single image or all images from a WIM file. */
281 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image)
282 {
283         if (!w->output_dir) {
284                 ERROR("No output directory selected.\n");
285                 return WIMLIB_ERR_NOTDIR;
286         }
287         if (image == WIM_ALL_IMAGES) {
288                 w->is_multi_image_extraction = true;
289                 return extract_all_images(w);
290         } else {
291                 w->is_multi_image_extraction = false;
292                 return extract_single_image(w, image);
293         }
294
295 }
296
297 /* Set the output directory for WIM extraction.  The directory is created using
298  * mkdir().  Fails if directory cannot be created or already exists. */
299 WIMLIBAPI int wimlib_set_output_dir(WIMStruct *w, const char *dir)
300 {
301         char *p;
302         DEBUG("Setting output directory to `%s'\n", dir);
303
304         if (!dir) {
305                 ERROR("Must specify a directory!\n");
306                 return WIMLIB_ERR_INVALID_PARAM;
307         }
308         p = STRDUP(dir);
309         if (!p) {
310                 ERROR("Out of memory!\n");
311                 return WIMLIB_ERR_NOMEM;
312         }
313
314         if (mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
315                 if (errno == EEXIST) {
316                         DEBUG("`%s' already exists\n", dir);
317                         goto done;
318                 }
319                 ERROR("Cannot create directory `%s': %m\n", dir);
320                 FREE(p);
321                 return WIMLIB_ERR_MKDIR;
322         } else {
323                 DEBUG("Created directory `%s'\n", dir);
324         }
325 done:
326         FREE(w->output_dir);
327         w->output_dir = p;
328         return 0;
329 }
330
331 WIMLIBAPI int wimlib_set_link_type(WIMStruct *w, int link_type)
332 {
333         switch (link_type) {
334                 case WIM_LINK_TYPE_NONE:
335                 case WIM_LINK_TYPE_HARD:
336                 case WIM_LINK_TYPE_SYMBOLIC:
337                         w->link_type = link_type;
338                         return 0;
339                 default:
340                         return WIMLIB_ERR_INVALID_PARAM;
341         }
342 }
343