]> wimlib.net Git - wimlib/blob - src/extract.c
Rewrite to use inodes (IN PROGRESS)
[wimlib] / src / extract.c
1 /*
2  * extract.c
3  *
4  * Support for extracting WIM files.
5  *
6  * This code does NOT contain any filesystem-specific features.  In particular,
7  * security information (i.e. file permissions) and alternate data streams are
8  * ignored, except possibly to read an alternate data stream that contains
9  * symbolic link data.
10  */
11
12 /*
13  * Copyright (C) 2010 Carl Thijssen
14  * Copyright (C) 2012 Eric Biggers
15  *
16  * This file is part of wimlib, a library for working with WIM files.
17  *
18  * wimlib is free software; you can redistribute it and/or modify it under the
19  * terms of the GNU General Public License as published by the Free
20  * Software Foundation; either version 3 of the License, or (at your option)
21  * any later version.
22  *
23  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
24  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
25  * A PARTICULAR PURPOSE. See the GNU General Public License for more
26  * details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with wimlib; if not, see http://www.gnu.org/licenses/.
30  */
31
32
33 #include <dirent.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <string.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include <unistd.h>
40
41 #include "config.h"
42 #include "dentry.h"
43 #include "lookup_table.h"
44 #include "timestamp.h"
45 #include "wimlib_internal.h"
46 #include "xml.h"
47
48 /* Internal */
49 #define WIMLIB_EXTRACT_FLAG_MULTI_IMAGE 0x80000000
50
51 static int extract_regular_file_linked(const struct dentry *dentry, 
52                                        const char *output_dir,
53                                        const char *output_path,
54                                        int extract_flags,
55                                        struct lookup_table_entry *lte)
56 {
57         /* This mode overrides the normal hard-link extraction and
58          * instead either symlinks or hardlinks *all* identical files in
59          * the WIM, even if they are in a different image (in the case
60          * of a multi-image extraction) */
61         wimlib_assert(lte->extracted_file);
62
63         if (extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
64                 if (link(lte->extracted_file, output_path) != 0) {
65                         ERROR_WITH_ERRNO("Failed to hard link "
66                                          "`%s' to `%s'",
67                                          output_path, lte->extracted_file);
68                         return WIMLIB_ERR_LINK;
69                 }
70         } else {
71                 int num_path_components;
72                 int num_output_dir_path_components;
73                 size_t extracted_file_len;
74                 char *p;
75                 const char *p2;
76                 size_t i;
77
78                 wimlib_assert(extract_flags & WIMLIB_EXTRACT_FLAG_SYMLINK);
79
80                 num_path_components = 
81                         get_num_path_components(dentry->full_path_utf8) - 1;
82                 num_output_dir_path_components =
83                         get_num_path_components(output_dir);
84
85                 if (extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
86                         num_path_components++;
87                         num_output_dir_path_components--;
88                 }
89                 extracted_file_len = strlen(lte->extracted_file);
90
91                 char buf[extracted_file_len + 3 * num_path_components + 1];
92                 p = &buf[0];
93
94                 for (i = 0; i < num_path_components; i++) {
95                         *p++ = '.';
96                         *p++ = '.';
97                         *p++ = '/';
98                 }
99                 p2 = lte->extracted_file;
100                 while (*p2 == '/')
101                         p2++;
102                 while (num_output_dir_path_components--)
103                         p2 = path_next_part(p2, NULL);
104                 strcpy(p, p2);
105                 if (symlink(buf, output_path) != 0) {
106                         ERROR_WITH_ERRNO("Failed to symlink `%s' to "
107                                          "`%s'",
108                                          buf, lte->extracted_file);
109                         return WIMLIB_ERR_LINK;
110                 }
111
112         }
113         return 0;
114 }
115
116 static int extract_regular_file_unlinked(WIMStruct *w,
117                                          struct dentry *dentry, 
118                                          const char *output_path,
119                                          int extract_flags,
120                                          struct lookup_table_entry *lte)
121 {
122         /* Normal mode of extraction.  Regular files and hard links are
123          * extracted in the way that they appear in the WIM. */
124
125         int out_fd;
126         int ret;
127         struct inode *inode = dentry->inode;
128
129         if (!(extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)) {
130                 /* This dentry is one of a hard link set of at least 2 dentries.
131                  * If one of the other dentries has already been extracted, make
132                  * a hard link to the file corresponding to this
133                  * already-extracted directory.  Otherwise, extract the
134                  * file, and set the dentry->extracted_file field so that other
135                  * dentries in the hard link group can link to it. */
136                 struct dentry *other;
137                 if (inode->extracted_file) {
138                         DEBUG("Extracting hard link `%s' => `%s'",
139                               output_path, inode->extracted_file);
140                         if (link(inode->extracted_file, output_path) != 0) {
141                                 ERROR_WITH_ERRNO("Failed to hard link "
142                                                  "`%s' to `%s'",
143                                                  output_path,
144                                                  inode->extracted_file);
145                                 return WIMLIB_ERR_LINK;
146                         }
147                         return 0;
148                 }
149                 if (inode->link_count > 1) {
150                         FREE(inode->extracted_file);
151                         inode->extracted_file = STRDUP(output_path);
152                         if (!inode->extracted_file) {
153                                 ERROR("Failed to allocate memory for filename");
154                                 return WIMLIB_ERR_NOMEM;
155                         }
156                 }
157         }
158
159         /* Extract the contents of the file to @output_path. */
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         if (!lte) {
169                 /* Empty file with no lookup table entry */
170                 DEBUG("Empty file `%s'.", output_path);
171                 ret = 0;
172                 goto done;
173         }
174
175         ret = extract_full_wim_resource_to_fd(lte, out_fd);
176         if (ret != 0) {
177                 ERROR("Failed to extract resource to `%s'", output_path);
178                 goto done;
179         }
180
181 done:
182         if (close(out_fd) != 0) {
183                 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
184                 ret = WIMLIB_ERR_WRITE;
185         }
186         return ret;
187 }
188
189 /* 
190  * Extracts a regular file from the WIM archive. 
191  */
192 static int extract_regular_file(WIMStruct *w, 
193                                 struct dentry *dentry, 
194                                 const char *output_dir,
195                                 const char *output_path,
196                                 int extract_flags)
197 {
198         struct lookup_table_entry *lte;
199         const struct inode *inode = dentry->inode;
200
201         lte = inode_unnamed_lte(inode, w->lookup_table);
202
203         if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
204                               WIMLIB_EXTRACT_FLAG_HARDLINK)) && lte) {
205                 if (++lte->out_refcnt != 1)
206                         return extract_regular_file_linked(dentry, output_dir,
207                                                            output_path,
208                                                            extract_flags, lte);
209                 lte->extracted_file = STRDUP(output_path);
210                 if (!lte->extracted_file)
211                         return WIMLIB_ERR_NOMEM;
212         }
213
214         return extract_regular_file_unlinked(w, dentry, output_path,
215                                              extract_flags, lte);
216
217 }
218
219 static int extract_symlink(const struct dentry *dentry, const char *output_path,
220                            const WIMStruct *w)
221 {
222         char target[4096];
223         ssize_t ret = inode_readlink(dentry->inode, target, sizeof(target), w);
224         if (ret <= 0) {
225                 ERROR("Could not read the symbolic link from dentry `%s'",
226                       dentry->full_path_utf8);
227                 return WIMLIB_ERR_INVALID_DENTRY;
228         }
229         ret = symlink(target, output_path);
230         if (ret != 0) {
231                 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
232                                  output_path, target);
233                 return WIMLIB_ERR_LINK;
234         }
235         return 0;
236 }
237
238 /* 
239  * Extracts a directory from the WIM archive. 
240  *
241  * @dentry:             The directory entry for the directory.
242  * @output_path:        The path to which the directory is to be extracted to.
243  * @return:             True on success, false on failure. 
244  */
245 static int extract_directory(const char *output_path, bool is_root)
246 {
247         int ret;
248         struct stat stbuf;
249         ret = stat(output_path, &stbuf);
250         if (ret == 0) {
251                 if (S_ISDIR(stbuf.st_mode)) {
252                         if (!is_root)
253                                 WARNING("`%s' already exists", output_path);
254                         return 0;
255                 } else {
256                         ERROR("`%s' is not a directory", output_path);
257                         return WIMLIB_ERR_MKDIR;
258                 }
259         } else {
260                 if (errno != ENOENT) {
261                         ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
262                         return WIMLIB_ERR_STAT;
263                 }
264         }
265         /* Compute the output path directory to the directory. */
266         if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP |
267                                S_IROTH | S_IXOTH) != 0) {
268                 ERROR_WITH_ERRNO("Cannot create directory `%s'",
269                                  output_path);
270                 return WIMLIB_ERR_MKDIR;
271         }
272         return 0;
273 }
274
275 struct extract_args {
276         WIMStruct *w;
277         int extract_flags;
278         const char *output_dir;
279 };
280
281 /* 
282  * Extracts a file, directory, or symbolic link from the WIM archive.  For use
283  * in for_dentry_in_tree().
284  */
285 static int extract_dentry(struct dentry *dentry, void *arg)
286 {
287         struct extract_args *args = arg;
288         WIMStruct *w = args->w;
289         int extract_flags = args->extract_flags;
290         size_t len = strlen(args->output_dir);
291         char output_path[len + dentry->full_path_utf8_len + 1];
292
293         if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) {
294                 wimlib_assert(dentry->full_path_utf8);
295                 puts(dentry->full_path_utf8);
296         }
297
298         memcpy(output_path, args->output_dir, len);
299         memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
300         output_path[len + dentry->full_path_utf8_len] = '\0';
301
302         if (dentry_is_symlink(dentry))
303                 return extract_symlink(dentry, output_path, w);
304         else if (dentry_is_directory(dentry))
305                 return extract_directory(output_path, dentry_is_root(dentry));
306         else
307                 return extract_regular_file(w, dentry, args->output_dir,
308                                             output_path, extract_flags);
309 }
310
311 /* Apply timestamp to extracted file */
312 static int apply_dentry_timestamps(struct dentry *dentry, void *arg)
313 {
314         struct extract_args *args = arg;
315         size_t len = strlen(args->output_dir);
316         char output_path[len + dentry->full_path_utf8_len + 1];
317
318         memcpy(output_path, args->output_dir, len);
319         memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
320         output_path[len + dentry->full_path_utf8_len] = '\0';
321
322         struct timeval tv[2];
323         wim_timestamp_to_timeval(dentry->inode->last_access_time, &tv[0]);
324         wim_timestamp_to_timeval(dentry->inode->last_write_time, &tv[1]);
325         if (lutimes(output_path, tv) != 0) {
326                 WARNING("Failed to set timestamp on file `%s': %s",
327                         output_path, strerror(errno));
328         }
329         return 0;
330 }
331
332
333 static int extract_single_image(WIMStruct *w, int image,
334                                 const char *output_dir, int extract_flags)
335 {
336         DEBUG("Extracting image %d", image);
337
338         int ret;
339         ret = wimlib_select_image(w, image);
340         if (ret != 0)
341                 return ret;
342
343         struct extract_args args = {
344                 .w = w,
345                 .extract_flags = extract_flags,
346                 .output_dir = output_dir,
347         };
348
349         ret = for_dentry_in_tree(wim_root_dentry(w), extract_dentry, &args);
350         if (ret != 0)
351                 return ret;
352         return for_dentry_in_tree_depth(wim_root_dentry(w),
353                                         apply_dentry_timestamps, &args);
354
355 }
356
357
358 /* Extracts all images from the WIM to @output_dir, with the images placed in
359  * subdirectories named by their image names. */
360 static int extract_all_images(WIMStruct *w, const char *output_dir,
361                               int extract_flags)
362 {
363         size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
364         size_t output_path_len = strlen(output_dir);
365         char buf[output_path_len + 1 + image_name_max_len + 1];
366         int ret;
367         int image;
368         const char *image_name;
369
370         DEBUG("Attempting to extract all images from `%s' to `%s'",
371               w->filename, output_dir);
372
373         ret = extract_directory(output_dir, true);
374         if (ret != 0)
375                 return ret;
376
377         memcpy(buf, output_dir, output_path_len);
378         buf[output_path_len] = '/';
379         for (image = 1; image <= w->hdr.image_count; image++) {
380                 
381                 image_name = wimlib_get_image_name(w, image);
382                 if (*image_name) {
383                         strcpy(buf + output_path_len + 1, image_name);
384                 } else {
385                         /* Image name is empty. Use image number instead */
386                         sprintf(buf + output_path_len + 1, "%d", image);
387                 }
388                 ret = extract_single_image(w, image, buf, extract_flags);
389                 if (ret != 0)
390                         return ret;
391         }
392         return 0;
393 }
394
395
396 /* Extracts a single image or all images from a WIM file. */
397 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image,
398                                    const char *output_dir, int flags,
399                                    WIMStruct **additional_swms,
400                                    unsigned num_additional_swms)
401 {
402         struct lookup_table *joined_tab, *w_tab_save;
403         int ret;
404
405         DEBUG("w->filename = %s, image = %d, output_dir = %s, flags = 0x%x, "
406               "num_additional_swms = %u",
407               w->filename, image, output_dir, flags, num_additional_swms);
408
409         if (!w || !output_dir)
410                 return WIMLIB_ERR_INVALID_PARAM;
411
412         if ((flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
413                         == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
414                 return WIMLIB_ERR_INVALID_PARAM;
415
416         ret = verify_swm_set(w, additional_swms, num_additional_swms);
417         if (ret != 0)
418                 return ret;
419
420         if (num_additional_swms) {
421                 ret = new_joined_lookup_table(w, additional_swms,
422                                               num_additional_swms, &joined_tab);
423                 if (ret != 0)
424                         return ret;
425                 w_tab_save = w->lookup_table;
426                 w->lookup_table = joined_tab;
427         }
428
429
430         for_lookup_table_entry(w->lookup_table, zero_out_refcnts, NULL);
431
432         if (image == WIM_ALL_IMAGES) {
433                 flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
434                 ret = extract_all_images(w, output_dir, flags);
435         } else {
436                 flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
437                 ret = extract_single_image(w, image, output_dir, flags);
438         }
439         if (num_additional_swms) {
440                 free_lookup_table(w->lookup_table);
441                 w->lookup_table = w_tab_save;
442         }
443         return ret;
444
445 }