]> wimlib.net Git - wimlib/blob - src/extract.c
wimlib_mount(): Disallow mounting newly added image
[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, sizeof(target), w);
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 (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) {
305                 wimlib_assert(dentry->full_path_utf8);
306                 puts(dentry->full_path_utf8);
307         }
308
309         memcpy(output_path, args->output_dir, len);
310         memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
311         output_path[len + dentry->full_path_utf8_len] = '\0';
312
313         if (dentry_is_symlink(dentry))
314                 return extract_symlink(dentry, output_path, w);
315         else if (dentry_is_directory(dentry))
316                 return extract_directory(output_path, dentry_is_root(dentry));
317         else
318                 return extract_regular_file(w, dentry, args->output_dir,
319                                             output_path, extract_flags);
320 }
321
322 /* Apply timestamp to extracted file */
323 static int apply_dentry_timestamps(struct dentry *dentry, void *arg)
324 {
325         struct extract_args *args = arg;
326         size_t len = strlen(args->output_dir);
327         char output_path[len + dentry->full_path_utf8_len + 1];
328         const struct inode *inode = dentry->d_inode;
329         int ret;
330
331         memcpy(output_path, args->output_dir, len);
332         memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
333         output_path[len + dentry->full_path_utf8_len] = '\0';
334
335         struct timeval tv[2];
336         wim_timestamp_to_timeval(inode->last_access_time, &tv[0]);
337         wim_timestamp_to_timeval(inode->last_write_time, &tv[1]);
338         #ifdef HAVE_LUTIMES
339         ret = lutimes(output_path, tv);
340         #else
341         ret = -1;
342         errno = ENOSYS;
343         #endif
344         if (ret != 0) {
345                 #ifdef HAVE_UTIME
346                 if (errno == ENOSYS) {
347                         struct utimbuf buf;
348                         buf.actime = wim_timestamp_to_unix(inode->last_access_time);
349                         buf.modtime = wim_timestamp_to_unix(inode->last_write_time);
350                         if (utime(output_path, &buf) == 0)
351                                 return 0;
352                 }
353                 #endif
354                 if (errno != ENOSYS || args->num_lutimes_warnings < 10) {
355                         WARNING("Failed to set timestamp on file `%s': %s",
356                                 output_path, strerror(errno));
357                         args->num_lutimes_warnings++;
358                 }
359         }
360         return 0;
361 }
362
363
364 static int extract_single_image(WIMStruct *w, int image,
365                                 const char *output_dir, int extract_flags)
366 {
367         DEBUG("Extracting image %d", image);
368
369         int ret;
370         ret = select_wim_image(w, image);
371         if (ret != 0)
372                 return ret;
373
374         struct extract_args args = {
375                 .w                    = w,
376                 .extract_flags        = extract_flags,
377                 .output_dir           = output_dir,
378                 .num_lutimes_warnings = 0,
379         };
380
381         ret = for_dentry_in_tree(wim_root_dentry(w), extract_dentry, &args);
382         if (ret != 0)
383                 return ret;
384         return for_dentry_in_tree_depth(wim_root_dentry(w),
385                                         apply_dentry_timestamps, &args);
386
387 }
388
389
390 /* Extracts all images from the WIM to @output_dir, with the images placed in
391  * subdirectories named by their image names. */
392 static int extract_all_images(WIMStruct *w, const char *output_dir,
393                               int extract_flags)
394 {
395         size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
396         size_t output_path_len = strlen(output_dir);
397         char buf[output_path_len + 1 + image_name_max_len + 1];
398         int ret;
399         int image;
400         const char *image_name;
401
402         DEBUG("Attempting to extract all images from `%s' to `%s'",
403               w->filename, output_dir);
404
405         ret = extract_directory(output_dir, true);
406         if (ret != 0)
407                 return ret;
408
409         memcpy(buf, output_dir, output_path_len);
410         buf[output_path_len] = '/';
411         for (image = 1; image <= w->hdr.image_count; image++) {
412
413                 image_name = wimlib_get_image_name(w, image);
414                 if (*image_name) {
415                         strcpy(buf + output_path_len + 1, image_name);
416                 } else {
417                         /* Image name is empty. Use image number instead */
418                         sprintf(buf + output_path_len + 1, "%d", image);
419                 }
420                 ret = extract_single_image(w, image, buf, extract_flags);
421                 if (ret != 0)
422                         return ret;
423         }
424         return 0;
425 }
426
427
428 /* Extracts a single image or all images from a WIM file. */
429 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image,
430                                    const char *output_dir, int flags,
431                                    WIMStruct **additional_swms,
432                                    unsigned num_additional_swms)
433 {
434         struct lookup_table *joined_tab, *w_tab_save;
435         int ret;
436
437         DEBUG("w->filename = %s, image = %d, output_dir = %s, flags = 0x%x, "
438               "num_additional_swms = %u",
439               w->filename, image, output_dir, flags, num_additional_swms);
440
441         if (!w || !output_dir)
442                 return WIMLIB_ERR_INVALID_PARAM;
443
444         if ((flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
445                         == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
446                 return WIMLIB_ERR_INVALID_PARAM;
447
448         ret = verify_swm_set(w, additional_swms, num_additional_swms);
449         if (ret != 0)
450                 return ret;
451
452         if (num_additional_swms) {
453                 ret = new_joined_lookup_table(w, additional_swms,
454                                               num_additional_swms, &joined_tab);
455                 if (ret != 0)
456                         return ret;
457                 w_tab_save = w->lookup_table;
458                 w->lookup_table = joined_tab;
459         }
460
461         for_lookup_table_entry(w->lookup_table, lte_free_extracted_file, NULL);
462
463         if (image == WIM_ALL_IMAGES) {
464                 flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
465                 ret = extract_all_images(w, output_dir, flags);
466         } else {
467                 flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
468                 ret = extract_single_image(w, image, output_dir, flags);
469         }
470         if (num_additional_swms) {
471                 free_lookup_table(w->lookup_table);
472                 w->lookup_table = w_tab_save;
473         }
474         return ret;
475
476 }