145ea78ada5a0cfeea3d9c2c5bef8a1450031778
[wimlib] / src / extract.c
1 /*
2  * extract.c
3  *
4  * Support for extracting WIM files.
5  *
6  * This code does NOT contain any filesystem-specific features.  In particular,
7  * security information (i.e. file permissions) and alternate data streams are
8  * ignored, except possibly to read an alternate data stream that contains
9  * symbolic link data.
10  */
11
12 /*
13  * Copyright (C) 2010 Carl Thijssen
14  * Copyright (C) 2012 Eric Biggers
15  *
16  * This file is part of wimlib, a library for working with WIM files.
17  *
18  * wimlib is free software; you can redistribute it and/or modify it under the
19  * terms of the GNU General Public License as published by the Free
20  * Software Foundation; either version 3 of the License, or (at your option)
21  * any later version.
22  *
23  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
24  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
25  * A PARTICULAR PURPOSE. See the GNU General Public License for more
26  * details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with wimlib; if not, see http://www.gnu.org/licenses/.
30  */
31
32
33 #include "config.h"
34
35 #include <dirent.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <string.h>
39 #include <sys/stat.h>
40 #include <stdlib.h>
41 #include <sys/time.h>
42
43 #ifdef HAVE_UTIME_H
44 #include <utime.h>
45 #endif
46
47 #include <unistd.h>
48
49 #include "dentry.h"
50 #include "lookup_table.h"
51 #include "timestamp.h"
52 #include "wimlib_internal.h"
53 #include "xml.h"
54
55
56 static int extract_regular_file_linked(const struct dentry *dentry,
57                                        const char *output_dir,
58                                        const char *output_path,
59                                        int extract_flags,
60                                        struct lookup_table_entry *lte)
61 {
62         /* This mode overrides the normal hard-link extraction and
63          * instead either symlinks or hardlinks *all* identical files in
64          * the WIM, even if they are in a different image (in the case
65          * of a multi-image extraction) */
66         wimlib_assert(lte->extracted_file);
67
68         if (extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
69                 if (link(lte->extracted_file, output_path) != 0) {
70                         ERROR_WITH_ERRNO("Failed to hard link "
71                                          "`%s' to `%s'",
72                                          output_path, lte->extracted_file);
73                         return WIMLIB_ERR_LINK;
74                 }
75         } else {
76                 int num_path_components;
77                 int num_output_dir_path_components;
78                 size_t extracted_file_len;
79                 char *p;
80                 const char *p2;
81                 size_t i;
82
83                 wimlib_assert(extract_flags & WIMLIB_EXTRACT_FLAG_SYMLINK);
84
85                 num_path_components =
86                         get_num_path_components(dentry->full_path_utf8) - 1;
87                 num_output_dir_path_components =
88                         get_num_path_components(output_dir);
89
90                 if (extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
91                         num_path_components++;
92                         num_output_dir_path_components--;
93                 }
94                 extracted_file_len = strlen(lte->extracted_file);
95
96                 char buf[extracted_file_len + 3 * num_path_components + 1];
97                 p = &buf[0];
98
99                 for (i = 0; i < num_path_components; i++) {
100                         *p++ = '.';
101                         *p++ = '.';
102                         *p++ = '/';
103                 }
104                 p2 = lte->extracted_file;
105                 while (*p2 == '/')
106                         p2++;
107                 while (num_output_dir_path_components--)
108                         p2 = path_next_part(p2, NULL);
109                 strcpy(p, p2);
110                 if (symlink(buf, output_path) != 0) {
111                         ERROR_WITH_ERRNO("Failed to symlink `%s' to "
112                                          "`%s'",
113                                          buf, lte->extracted_file);
114                         return WIMLIB_ERR_LINK;
115                 }
116
117         }
118         return 0;
119 }
120
121 static int extract_regular_file_unlinked(WIMStruct *w,
122                                          struct dentry *dentry,
123                                          const char *output_path,
124                                          int extract_flags,
125                                          struct lookup_table_entry *lte)
126 {
127         /* Normal mode of extraction.  Regular files and hard links are
128          * extracted in the way that they appear in the WIM. */
129
130         int out_fd;
131         int ret;
132         struct inode *inode = dentry->d_inode;
133
134         if (!((extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
135                 && (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
136                                      WIMLIB_EXTRACT_FLAG_HARDLINK))))
137         {
138                 /* If the dentry is one of a hard link set of at least 2
139                  * dentries and one of the other dentries has already been
140                  * extracted, make a hard link to the file corresponding to this
141                  * already-extracted directory.  Otherwise, extract the file,
142                  * and set the inode->extracted_file field so that other
143                  * dentries in the hard link group can link to it. */
144                 if (inode->link_count > 1) {
145                         if (inode->extracted_file) {
146                                 DEBUG("Extracting hard link `%s' => `%s'",
147                                       output_path, inode->extracted_file);
148                                 if (link(inode->extracted_file, output_path) != 0) {
149                                         ERROR_WITH_ERRNO("Failed to hard link "
150                                                          "`%s' to `%s'",
151                                                          output_path,
152                                                          inode->extracted_file);
153                                         return WIMLIB_ERR_LINK;
154                                 }
155                                 return 0;
156                         }
157                         FREE(inode->extracted_file);
158                         inode->extracted_file = STRDUP(output_path);
159                         if (!inode->extracted_file) {
160                                 ERROR("Failed to allocate memory for filename");
161                                 return WIMLIB_ERR_NOMEM;
162                         }
163                 }
164         }
165
166         /* Extract the contents of the file to @output_path. */
167
168         out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
169         if (out_fd == -1) {
170                 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
171                                  output_path);
172                 return WIMLIB_ERR_OPEN;
173         }
174
175         if (!lte) {
176                 /* Empty file with no lookup table entry */
177                 DEBUG("Empty file `%s'.", output_path);
178                 ret = 0;
179                 goto out;
180         }
181
182         ret = extract_full_wim_resource_to_fd(lte, out_fd);
183         if (ret != 0) {
184                 ERROR("Failed to extract resource to `%s'", output_path);
185                 goto out;
186         }
187
188 out:
189         if (close(out_fd) != 0) {
190                 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
191                 ret = WIMLIB_ERR_WRITE;
192         }
193         return ret;
194 }
195
196 /*
197  * Extracts a regular file from the WIM archive.
198  */
199 static int extract_regular_file(WIMStruct *w,
200                                 struct dentry *dentry,
201                                 const char *output_dir,
202                                 const char *output_path,
203                                 int extract_flags)
204 {
205         struct lookup_table_entry *lte;
206         const struct inode *inode = dentry->d_inode;
207
208         lte = inode_unnamed_lte(inode, w->lookup_table);
209
210         if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
211                               WIMLIB_EXTRACT_FLAG_HARDLINK)) && lte) {
212                 if (lte->extracted_file) {
213                         return extract_regular_file_linked(dentry, output_dir,
214                                                            output_path,
215                                                            extract_flags, lte);
216                 } else {
217                         lte->extracted_file = STRDUP(output_path);
218                         if (!lte->extracted_file)
219                                 return WIMLIB_ERR_NOMEM;
220                 }
221         }
222
223         return extract_regular_file_unlinked(w, dentry, output_path,
224                                              extract_flags, lte);
225
226 }
227
228 static int extract_symlink(const struct dentry *dentry, const char *output_path,
229                            const WIMStruct *w)
230 {
231         char target[4096];
232         ssize_t ret = inode_readlink(dentry->d_inode, target,
233                                      sizeof(target), w, 0);
234         if (ret <= 0) {
235                 ERROR("Could not read the symbolic link from dentry `%s'",
236                       dentry->full_path_utf8);
237                 return WIMLIB_ERR_INVALID_DENTRY;
238         }
239         ret = symlink(target, output_path);
240         if (ret != 0) {
241                 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
242                                  output_path, target);
243                 return WIMLIB_ERR_LINK;
244         }
245         return 0;
246 }
247
248 /*
249  * Extracts a directory from the WIM archive.
250  *
251  * @dentry:             The directory entry for the directory.
252  * @output_path:        The path to which the directory is to be extracted to.
253  * @return:             True on success, false on failure.
254  */
255 static int extract_directory(const char *output_path, bool is_root)
256 {
257         int ret;
258         struct stat stbuf;
259         ret = stat(output_path, &stbuf);
260         if (ret == 0) {
261                 if (S_ISDIR(stbuf.st_mode)) {
262                         /*if (!is_root)*/
263                                 /*WARNING("`%s' already exists", output_path);*/
264                         return 0;
265                 } else {
266                         ERROR("`%s' is not a directory", output_path);
267                         return WIMLIB_ERR_MKDIR;
268                 }
269         } else {
270                 if (errno != ENOENT) {
271                         ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
272                         return WIMLIB_ERR_STAT;
273                 }
274         }
275         /* Compute the output path directory to the directory. */
276         if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP |
277                                S_IROTH | S_IXOTH) != 0) {
278                 ERROR_WITH_ERRNO("Cannot create directory `%s'",
279                                  output_path);
280                 return WIMLIB_ERR_MKDIR;
281         }
282         return 0;
283 }
284
285 struct extract_args {
286         WIMStruct *w;
287         int extract_flags;
288         const char *output_dir;
289         unsigned num_lutimes_warnings;
290 };
291
292 /*
293  * Extracts a file, directory, or symbolic link from the WIM archive.  For use
294  * in for_dentry_in_tree().
295  */
296 static int extract_dentry(struct dentry *dentry, void *arg)
297 {
298         struct extract_args *args = arg;
299         WIMStruct *w = args->w;
300         int extract_flags = args->extract_flags;
301         size_t len = strlen(args->output_dir);
302         char output_path[len + dentry->full_path_utf8_len + 1];
303
304         if (dentry_is_directory(dentry)) {
305                 if (extract_flags & WIMLIB_EXTRACT_FLAG_SKIP_DIRS)
306                         return 0;
307         } else {
308                 if (extract_flags & WIMLIB_EXTRACT_FLAG_DIRS_ONLY)
309                         return 0;
310         }
311
312         if ((extract_flags & WIMLIB_EXTRACT_FLAG_EMPTY_ONLY)
313             && (!dentry_is_regular_file(dentry) ||
314                  inode_unnamed_lte(dentry->d_inode, w->lookup_table) != NULL))
315                 return 0;
316
317         if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) {
318                 wimlib_assert(dentry->full_path_utf8);
319                 puts(dentry->full_path_utf8);
320         }
321
322         memcpy(output_path, args->output_dir, len);
323         memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
324         output_path[len + dentry->full_path_utf8_len] = '\0';
325
326
327         if (dentry_is_symlink(dentry))
328                 return extract_symlink(dentry, output_path, w);
329         else if (dentry_is_directory(dentry))
330                 return extract_directory(output_path, dentry_is_root(dentry));
331         else
332                 return extract_regular_file(w, dentry, args->output_dir,
333                                             output_path, extract_flags);
334 }
335
336 /* Apply timestamp to extracted file */
337 static int apply_dentry_timestamps(struct dentry *dentry, void *arg)
338 {
339         struct extract_args *args = arg;
340         size_t len = strlen(args->output_dir);
341         char output_path[len + dentry->full_path_utf8_len + 1];
342         const struct inode *inode = dentry->d_inode;
343         int ret;
344
345         memcpy(output_path, args->output_dir, len);
346         memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
347         output_path[len + dentry->full_path_utf8_len] = '\0';
348
349         struct timeval tv[2];
350         wim_timestamp_to_timeval(inode->last_access_time, &tv[0]);
351         wim_timestamp_to_timeval(inode->last_write_time, &tv[1]);
352         #ifdef HAVE_LUTIMES
353         ret = lutimes(output_path, tv);
354         #else
355         ret = -1;
356         errno = ENOSYS;
357         #endif
358         if (ret != 0) {
359                 #ifdef HAVE_UTIME
360                 if (errno == ENOSYS) {
361                         struct utimbuf buf;
362                         buf.actime = wim_timestamp_to_unix(inode->last_access_time);
363                         buf.modtime = wim_timestamp_to_unix(inode->last_write_time);
364                         if (utime(output_path, &buf) == 0)
365                                 return 0;
366                 }
367                 #endif
368                 if (errno != ENOSYS || args->num_lutimes_warnings < 10) {
369                         /*WARNING("Failed to set timestamp on file `%s': %s",*/
370                                 /*output_path, strerror(errno));*/
371                         args->num_lutimes_warnings++;
372                 }
373         }
374         return 0;
375 }
376
377
378 static int dentry_add_streams_for_extraction(struct dentry *dentry,
379                                              void *wim)
380 {
381         WIMStruct *w = wim;
382         struct list_head *stream_list;
383         struct lookup_table_entry *lte;
384
385         lte = inode_unnamed_lte(dentry->d_inode, w->lookup_table);
386         if (lte) {
387                 if (++lte->out_refcnt == 1) {
388                         INIT_LIST_HEAD(&lte->dentry_list);
389                         stream_list = w->private;
390                         list_add_tail(&lte->staging_list, stream_list);
391                 }
392                 list_add_tail(&dentry->tmp_list, &lte->dentry_list);
393         }
394         return 0;
395 }
396
397 static int cmp_streams_by_wim_position(const void *p1, const void *p2)
398 {
399         const struct lookup_table_entry *lte1, *lte2;
400         lte1 = *(const struct lookup_table_entry**)p1;
401         lte2 = *(const struct lookup_table_entry**)p2;
402         if (lte1->resource_entry.offset < lte2->resource_entry.offset)
403                 return -1;
404         else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
405                 return 1;
406         else
407                 return 0;
408 }
409
410 static int sort_stream_list_by_wim_position(struct list_head *stream_list)
411 {
412         struct list_head *cur;
413         size_t num_streams;
414         struct lookup_table_entry **array;
415         size_t i;
416         size_t array_size;
417
418         DEBUG("Sorting stream list by wim position");
419
420         num_streams = 0;
421         list_for_each(cur, stream_list)
422                 num_streams++;
423         array_size = num_streams * sizeof(array[0]);
424
425         DEBUG("num_streams = %zu", num_streams);
426
427         array = MALLOC(array_size);
428         if (!array) {
429                 ERROR("Failed to allocate %zu bytes to sort stream entries",
430                       array_size);
431                 return WIMLIB_ERR_NOMEM;
432         }
433         cur = stream_list->next;
434         for (i = 0; i < num_streams; i++) {
435                 array[i] = container_of(cur, struct lookup_table_entry, staging_list);
436                 cur = cur->next;
437         }
438
439         qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
440
441         INIT_LIST_HEAD(stream_list);
442         for (i = 0; i < num_streams; i++)
443                 list_add(&array[i]->staging_list, stream_list);
444         FREE(array);
445         return 0;
446 }
447
448 static int extract_single_image(WIMStruct *w, int image,
449                                 const char *output_dir, int extract_flags)
450 {
451         int ret;
452         struct dentry *root;
453
454         DEBUG("Extracting image %d", image);
455
456         ret = select_wim_image(w, image);
457         if (ret != 0)
458                 return ret;
459
460         root = wim_root_dentry(w);
461
462         struct extract_args args = {
463                 .w                    = w,
464                 .extract_flags        = extract_flags,
465                 .output_dir           = output_dir,
466                 .num_lutimes_warnings = 0,
467         };
468
469         if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL)
470                 args.extract_flags |= WIMLIB_EXTRACT_FLAG_DIRS_ONLY;
471
472         ret = for_dentry_in_tree(root, extract_dentry, &args);
473         if (ret != 0)
474                 return ret;
475
476         if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
477                 struct list_head stream_list;
478                 INIT_LIST_HEAD(&stream_list);
479                 w->private = &stream_list;
480                 for_dentry_in_tree(root, dentry_add_streams_for_extraction, w);
481                 ret = sort_stream_list_by_wim_position(&stream_list);
482                 args.extract_flags &= ~WIMLIB_EXTRACT_FLAG_DIRS_ONLY;
483                 if (ret == 0) {
484                         args.extract_flags |= WIMLIB_EXTRACT_FLAG_SKIP_DIRS;
485                         struct lookup_table_entry *lte;
486                         struct lookup_table_entry *tmp;
487                         struct dentry *dentry;
488                         list_for_each_entry_safe(lte, tmp, &stream_list, staging_list) {
489                                 list_del(&lte->staging_list);
490                                 lte->extracted_file = NULL;
491                                 list_for_each_entry(dentry, &lte->dentry_list, tmp_list) {
492                                         ret = extract_dentry(dentry, &args);
493                                         if (ret != 0)
494                                                 return ret;
495                                 }
496                                 FREE(lte->extracted_file);
497                         }
498                         args.extract_flags |= WIMLIB_EXTRACT_FLAG_EMPTY_ONLY;
499                         ret = for_dentry_in_tree(root, extract_dentry, &args);
500                         if (ret != 0)
501                                 return ret;
502                         args.extract_flags &= ~(WIMLIB_EXTRACT_FLAG_SKIP_DIRS |
503                                                 WIMLIB_EXTRACT_FLAG_EMPTY_ONLY);
504                 } else {
505                         WARNING("Falling back to non-sequential image extraction");
506                         ret = for_dentry_in_tree(root, extract_dentry, &args);
507                         if (ret != 0)
508                                 return ret;
509                 }
510         }
511
512         return for_dentry_in_tree_depth(root, apply_dentry_timestamps, &args);
513
514 }
515
516
517 /* Extracts all images from the WIM to @output_dir, with the images placed in
518  * subdirectories named by their image names. */
519 static int extract_all_images(WIMStruct *w, const char *output_dir,
520                               int extract_flags)
521 {
522         size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
523         size_t output_path_len = strlen(output_dir);
524         char buf[output_path_len + 1 + image_name_max_len + 1];
525         int ret;
526         int image;
527         const char *image_name;
528
529         DEBUG("Attempting to extract all images from `%s' to `%s'",
530               w->filename, output_dir);
531
532         ret = extract_directory(output_dir, true);
533         if (ret != 0)
534                 return ret;
535
536         memcpy(buf, output_dir, output_path_len);
537         buf[output_path_len] = '/';
538         for (image = 1; image <= w->hdr.image_count; image++) {
539
540                 image_name = wimlib_get_image_name(w, image);
541                 if (*image_name) {
542                         strcpy(buf + output_path_len + 1, image_name);
543                 } else {
544                         /* Image name is empty. Use image number instead */
545                         sprintf(buf + output_path_len + 1, "%d", image);
546                 }
547                 ret = extract_single_image(w, image, buf, extract_flags);
548                 if (ret != 0)
549                         return ret;
550         }
551         return 0;
552 }
553
554 /* Extracts a single image or all images from a WIM file. */
555 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image,
556                                    const char *output_dir,
557                                    int extract_flags,
558                                    WIMStruct **additional_swms,
559                                    unsigned num_additional_swms)
560 {
561         struct lookup_table *joined_tab, *w_tab_save;
562         int ret;
563
564         DEBUG("w->filename = %s, image = %d, output_dir = %s, flags = 0x%x, "
565               "num_additional_swms = %u",
566               w->filename, image, output_dir, extract_flags, num_additional_swms);
567
568         if (!w || !output_dir)
569                 return WIMLIB_ERR_INVALID_PARAM;
570
571         extract_flags &= WIMLIB_EXTRACT_MASK_PUBLIC;
572
573         if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
574                         == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
575                 return WIMLIB_ERR_INVALID_PARAM;
576
577         ret = verify_swm_set(w, additional_swms, num_additional_swms);
578         if (ret != 0)
579                 return ret;
580
581         if (num_additional_swms) {
582                 ret = new_joined_lookup_table(w, additional_swms,
583                                               num_additional_swms, &joined_tab);
584                 if (ret != 0)
585                         return ret;
586                 w_tab_save = w->lookup_table;
587                 w->lookup_table = joined_tab;
588         }
589
590         for_lookup_table_entry(w->lookup_table, lte_zero_out_refcnt, NULL);
591
592         if (!(extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL)) {
593                 for_lookup_table_entry(w->lookup_table,
594                                        lte_zero_extracted_file,
595                                        NULL);
596         }
597
598         if (image == WIM_ALL_IMAGES) {
599                 extract_flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
600                 ret = extract_all_images(w, output_dir, extract_flags);
601         } else {
602                 extract_flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
603                 ret = extract_single_image(w, image, output_dir, extract_flags);
604         }
605         if (num_additional_swms) {
606                 free_lookup_table(w->lookup_table);
607                 w->lookup_table = w_tab_save;
608         }
609         if (!(extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL)) {
610                 for_lookup_table_entry(w->lookup_table,
611                                        lte_free_extracted_file,
612                                        NULL);
613         }
614         return ret;
615
616 }