d22f209bbe33351ff69ea97366b6da452432eded
[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
28 #include <dirent.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <sys/time.h>
34 #include <unistd.h>
35
36 #include "config.h"
37 #include "dentry.h"
38 #include "lookup_table.h"
39 #include "timestamp.h"
40 #include "wimlib_internal.h"
41 #include "xml.h"
42
43 /* Internal */
44 #define WIMLIB_EXTRACT_FLAG_MULTI_IMAGE 0x80000000
45
46 static int extract_regular_file_linked(const struct dentry *dentry, 
47                                        const char *output_dir,
48                                        const char *output_path,
49                                        int extract_flags,
50                                        struct lookup_table_entry *lte)
51 {
52         /* This mode overrides the normal hard-link extraction and
53          * instead either symlinks or hardlinks *all* identical files in
54          * the WIM, even if they are in a different image (in the case
55          * of a multi-image extraction) */
56         wimlib_assert(lte->extracted_file);
57
58         if (extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
59                 if (link(lte->extracted_file, output_path) != 0) {
60                         ERROR_WITH_ERRNO("Failed to hard link "
61                                          "`%s' to `%s'",
62                                          output_path, lte->extracted_file);
63                         return WIMLIB_ERR_LINK;
64                 }
65         } else {
66                 int num_path_components;
67                 int num_output_dir_path_components;
68                 size_t extracted_file_len;
69                 char *p;
70                 const char *p2;
71                 size_t i;
72
73                 wimlib_assert(extract_flags & WIMLIB_EXTRACT_FLAG_SYMLINK);
74
75                 num_path_components = 
76                         get_num_path_components(dentry->full_path_utf8) - 1;
77                 num_output_dir_path_components =
78                         get_num_path_components(output_dir);
79
80                 if (extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
81                         num_path_components++;
82                         num_output_dir_path_components--;
83                 }
84                 extracted_file_len = strlen(lte->extracted_file);
85
86                 char buf[extracted_file_len + 3 * num_path_components + 1];
87                 p = &buf[0];
88
89                 for (i = 0; i < num_path_components; i++) {
90                         *p++ = '.';
91                         *p++ = '.';
92                         *p++ = '/';
93                 }
94                 p2 = lte->extracted_file;
95                 while (*p2 == '/')
96                         p2++;
97                 while (num_output_dir_path_components--)
98                         p2 = path_next_part(p2, NULL);
99                 strcpy(p, p2);
100                 if (symlink(buf, output_path) != 0) {
101                         ERROR_WITH_ERRNO("Failed to symlink `%s' to "
102                                          "`%s'",
103                                          buf, lte->extracted_file);
104                         return WIMLIB_ERR_LINK;
105                 }
106
107         }
108         return 0;
109 }
110
111 static int extract_regular_file_unlinked(WIMStruct *w,
112                                          struct dentry *dentry, 
113                                          const char *output_path,
114                                          int extract_flags,
115                                          struct lookup_table_entry *lte)
116 {
117         /* Normal mode of extraction.  Regular files and hard links are
118          * extracted in the way that they appear in the WIM. */
119
120         int out_fd;
121         const struct resource_entry *res_entry;
122         int ret;
123         const struct list_head *head = &dentry->link_group_list;
124
125         if (head->next != head &&
126              !(extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE))
127         {
128                 /* This dentry is one of a hard link set of at least 2 dentries.
129                  * If one of the other dentries has already been extracted, make
130                  * a hard link to the file corresponding to this
131                  * already-extracted directory.  Otherwise, extract the
132                  * file, and set the dentry->extracted_file field so that other
133                  * dentries in the hard link group can link to it. */
134                 struct dentry *other;
135                 list_for_each_entry(other, head, link_group_list) {
136                         if (other->extracted_file) {
137                                 DEBUG("Extracting hard link `%s' => `%s'",
138                                       output_path, other->extracted_file);
139                                 if (link(other->extracted_file, output_path) != 0) {
140                                         ERROR_WITH_ERRNO("Failed to hard link "
141                                                          "`%s' to `%s'",
142                                                          output_path,
143                                                          other->extracted_file);
144                                         return WIMLIB_ERR_LINK;
145                                 }
146                                 return 0;
147                         }
148                 }
149                 FREE(dentry->extracted_file);
150                 dentry->extracted_file = STRDUP(output_path);
151                 if (!dentry->extracted_file) {
152                         ERROR("Failed to allocate memory for filename");
153                         return WIMLIB_ERR_NOMEM;
154                 }
155         }
156
157         /* Extract the contents of the file to @output_path. */
158
159         out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
160         if (out_fd == -1) {
161                 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
162                                  output_path);
163                 return WIMLIB_ERR_OPEN;
164         }
165
166         if (!lte) {
167                 /* Empty file with no lookup table entry */
168                 DEBUG("Empty file `%s'.", output_path);
169                 ret = 0;
170                 goto done;
171         }
172
173         ret = extract_full_wim_resource_to_fd(lte, out_fd);
174         if (ret != 0) {
175                 ERROR("Failed to extract resource to `%s'", output_path);
176                 goto done;
177         }
178
179 done:
180         if (close(out_fd) != 0) {
181                 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
182                 ret = WIMLIB_ERR_WRITE;
183         }
184         return ret;
185 }
186
187 /* 
188  * Extracts a regular file from the WIM archive. 
189  */
190 static int extract_regular_file(WIMStruct *w, 
191                                 struct dentry *dentry, 
192                                 const char *output_dir,
193                                 const char *output_path,
194                                 int extract_flags)
195 {
196         struct lookup_table_entry *lte;
197
198         lte = dentry_first_lte(dentry, w->lookup_table);
199
200         if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
201                               WIMLIB_EXTRACT_FLAG_HARDLINK)) && lte) {
202                 if (++lte->out_refcnt != 1)
203                         return extract_regular_file_linked(dentry, output_dir,
204                                                            output_path,
205                                                            extract_flags, lte);
206                 lte->extracted_file = STRDUP(output_path);
207                 if (!lte->extracted_file)
208                         return WIMLIB_ERR_NOMEM;
209         }
210
211         return extract_regular_file_unlinked(w, dentry, output_path,
212                                              extract_flags, lte);
213
214 }
215
216 static int extract_symlink(const struct dentry *dentry, const char *output_path,
217                            const WIMStruct *w)
218 {
219         char target[4096];
220         ssize_t ret = dentry_readlink(dentry, target, sizeof(target), w);
221         if (ret <= 0) {
222                 ERROR("Could not read the symbolic link from dentry `%s'",
223                       dentry->full_path_utf8);
224                 return WIMLIB_ERR_INVALID_DENTRY;
225         }
226         ret = symlink(target, output_path);
227         if (ret != 0) {
228                 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
229                                  output_path, target);
230                 return WIMLIB_ERR_LINK;
231         }
232         return 0;
233 }
234
235 /* 
236  * Extracts a directory from the WIM archive. 
237  *
238  * @dentry:             The directory entry for the directory.
239  * @output_path:        The path to which the directory is to be extracted to.
240  * @return:             True on success, false on failure. 
241  */
242 static int extract_directory(const char *output_path, bool is_root)
243 {
244         int ret;
245         struct stat stbuf;
246         ret = stat(output_path, &stbuf);
247         if (ret == 0) {
248                 if (S_ISDIR(stbuf.st_mode)) {
249                         if (!is_root)
250                                 WARNING("`%s' already exists", output_path);
251                         return 0;
252                 } else {
253                         ERROR("`%s' is not a directory", output_path);
254                         return WIMLIB_ERR_MKDIR;
255                 }
256         } else {
257                 if (errno != ENOENT) {
258                         ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
259                         return WIMLIB_ERR_STAT;
260                 }
261         }
262         /* Compute the output path directory to the directory. */
263         if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP |
264                                S_IROTH | S_IXOTH) != 0) {
265                 ERROR_WITH_ERRNO("Cannot create directory `%s'",
266                                  output_path);
267                 return WIMLIB_ERR_MKDIR;
268         }
269         return 0;
270 }
271
272 struct extract_args {
273         WIMStruct *w;
274         int extract_flags;
275         const char *output_dir;
276 #ifdef WITH_NTFS_3G
277         struct SECURITY_API *scapi;
278 #endif
279 };
280
281 /* 
282  * Extracts a file, directory, or symbolic link from the WIM archive.  For use
283  * in for_dentry_in_tree().
284  */
285 static int extract_dentry(struct dentry *dentry, void *arg)
286 {
287         struct extract_args *args = arg;
288         WIMStruct *w = args->w;
289         int extract_flags = args->extract_flags;
290         size_t len = strlen(args->output_dir);
291         char output_path[len + dentry->full_path_utf8_len + 1];
292         int ret;
293
294         if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) {
295                 wimlib_assert(dentry->full_path_utf8);
296                 puts(dentry->full_path_utf8);
297         }
298
299         memcpy(output_path, args->output_dir, len);
300         memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
301         output_path[len + dentry->full_path_utf8_len] = '\0';
302
303         if (dentry_is_symlink(dentry))
304                 ret = extract_symlink(dentry, output_path, w);
305         else if (dentry_is_directory(dentry))
306                 ret = extract_directory(output_path, dentry_is_root(dentry));
307         else
308                 ret = extract_regular_file(w, dentry, args->output_dir,
309                                             output_path, extract_flags);
310         if (ret != 0)
311                 return ret;
312
313         return 0;
314 }
315
316 /* Apply timestamp to extracted file */
317 static int apply_dentry_timestamps(struct dentry *dentry, void *arg)
318 {
319         struct extract_args *args = arg;
320         size_t len = strlen(args->output_dir);
321         char output_path[len + dentry->full_path_utf8_len + 1];
322
323         memcpy(output_path, args->output_dir, len);
324         memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
325         output_path[len + dentry->full_path_utf8_len] = '\0';
326
327         struct timeval tv[2];
328         wim_timestamp_to_timeval(dentry->last_access_time, &tv[0]);
329         wim_timestamp_to_timeval(dentry->last_write_time, &tv[1]);
330         if (lutimes(output_path, tv) != 0) {
331                 WARNING("Failed to set timestamp on file `%s': %s",
332                         output_path, strerror(errno));
333         }
334         return 0;
335 }
336
337
338 static int extract_single_image(WIMStruct *w, int image,
339                                 const char *output_dir, int extract_flags)
340 {
341         DEBUG("Extracting image %d", image);
342
343         int ret;
344         ret = wimlib_select_image(w, image);
345         if (ret != 0)
346                 return ret;
347
348         struct extract_args args = {
349                 .w = w,
350                 .extract_flags = extract_flags,
351                 .output_dir = output_dir,
352         #ifdef WITH_NTFS_3G
353                 .scapi = NULL
354         #endif
355         };
356
357         ret = for_dentry_in_tree(wim_root_dentry(w), extract_dentry, &args);
358         if (ret != 0)
359                 return ret;
360         return for_dentry_in_tree_depth(wim_root_dentry(w),
361                                         apply_dentry_timestamps, &args);
362
363 }
364
365
366 /* Extracts all images from the WIM to @output_dir, with the images placed in
367  * subdirectories named by their image names. */
368 static int extract_all_images(WIMStruct *w, const char *output_dir,
369                               int extract_flags)
370 {
371         size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
372         size_t output_path_len = strlen(output_dir);
373         char buf[output_path_len + 1 + image_name_max_len + 1];
374         int ret;
375         int image;
376         const char *image_name;
377
378         DEBUG("Attempting to extract all images from `%s'", w->filename);
379
380         ret = extract_directory(output_dir, true);
381         if (ret != 0)
382                 return ret;
383
384         memcpy(buf, output_dir, output_path_len);
385         buf[output_path_len] = '/';
386         for (image = 1; image <= w->hdr.image_count; image++) {
387                 
388                 image_name = wimlib_get_image_name(w, image);
389                 if (*image_name) {
390                         strcpy(buf + output_path_len + 1, image_name);
391                 } else {
392                         /* Image name is empty. Use image number instead */
393                         sprintf(buf + output_path_len + 1, "%d", image);
394                 }
395                 ret = extract_single_image(w, image, buf, extract_flags);
396                 if (ret != 0)
397                         goto done;
398         }
399 done:
400         /* Restore original output directory */
401         buf[output_path_len + 1] = '\0';
402         return 0;
403 }
404
405 /* Extracts a single image or all images from a WIM file. */
406 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image,
407                                    const char *output_dir, int flags)
408 {
409         if (!output_dir)
410                 return WIMLIB_ERR_INVALID_PARAM;
411
412         if ((flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
413                         == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
414                 return WIMLIB_ERR_INVALID_PARAM;
415
416         for_lookup_table_entry(w->lookup_table, zero_out_refcnts, NULL);
417
418         if (image == WIM_ALL_IMAGES) {
419                 flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
420                 return extract_all_images(w, output_dir, flags);
421         } else {
422                 flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
423                 return extract_single_image(w, image, output_dir, flags);
424         }
425
426 }