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