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