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