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