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