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