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