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