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