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