4 * Support for extracting WIM files.
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
13 * Copyright (C) 2010 Carl Thijssen
14 * Copyright (C) 2012 Eric Biggers
16 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
49 #include "lookup_table.h"
50 #include "timestamp.h"
51 #include "wimlib_internal.h"
55 #define WIMLIB_EXTRACT_FLAG_MULTI_IMAGE 0x80000000
57 static int extract_regular_file_linked(const struct dentry *dentry,
58 const char *output_dir,
59 const char *output_path,
61 struct lookup_table_entry *lte)
63 /* This mode overrides the normal hard-link extraction and
64 * instead either symlinks or hardlinks *all* identical files in
65 * the WIM, even if they are in a different image (in the case
66 * of a multi-image extraction) */
67 wimlib_assert(lte->extracted_file);
69 if (extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
70 if (link(lte->extracted_file, output_path) != 0) {
71 ERROR_WITH_ERRNO("Failed to hard link "
73 output_path, lte->extracted_file);
74 return WIMLIB_ERR_LINK;
77 int num_path_components;
78 int num_output_dir_path_components;
79 size_t extracted_file_len;
84 wimlib_assert(extract_flags & WIMLIB_EXTRACT_FLAG_SYMLINK);
87 get_num_path_components(dentry->full_path_utf8) - 1;
88 num_output_dir_path_components =
89 get_num_path_components(output_dir);
91 if (extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
92 num_path_components++;
93 num_output_dir_path_components--;
95 extracted_file_len = strlen(lte->extracted_file);
97 char buf[extracted_file_len + 3 * num_path_components + 1];
100 for (i = 0; i < num_path_components; i++) {
105 p2 = lte->extracted_file;
108 while (num_output_dir_path_components--)
109 p2 = path_next_part(p2, NULL);
111 if (symlink(buf, output_path) != 0) {
112 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
114 buf, lte->extracted_file);
115 return WIMLIB_ERR_LINK;
122 static int extract_regular_file_unlinked(WIMStruct *w,
123 struct dentry *dentry,
124 const char *output_path,
126 struct lookup_table_entry *lte)
128 /* Normal mode of extraction. Regular files and hard links are
129 * extracted in the way that they appear in the WIM. */
133 struct inode *inode = dentry->d_inode;
135 if (!((extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
136 && (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
137 WIMLIB_EXTRACT_FLAG_HARDLINK))))
139 /* If the dentry is one of a hard link set of at least 2
140 * dentries and one of the other dentries has already been
141 * extracted, make a hard link to the file corresponding to this
142 * already-extracted directory. Otherwise, extract the file,
143 * and set the inode->extracted_file field so that other
144 * dentries in the hard link group can link to it. */
145 if (inode->link_count > 1) {
146 if (inode->extracted_file) {
147 DEBUG("Extracting hard link `%s' => `%s'",
148 output_path, inode->extracted_file);
149 if (link(inode->extracted_file, output_path) != 0) {
150 ERROR_WITH_ERRNO("Failed to hard link "
153 inode->extracted_file);
154 return WIMLIB_ERR_LINK;
158 FREE(inode->extracted_file);
159 inode->extracted_file = STRDUP(output_path);
160 if (!inode->extracted_file) {
161 ERROR("Failed to allocate memory for filename");
162 return WIMLIB_ERR_NOMEM;
167 /* Extract the contents of the file to @output_path. */
169 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
171 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
173 return WIMLIB_ERR_OPEN;
177 /* Empty file with no lookup table entry */
178 DEBUG("Empty file `%s'.", output_path);
183 ret = extract_full_wim_resource_to_fd(lte, out_fd);
185 ERROR("Failed to extract resource to `%s'", output_path);
190 if (close(out_fd) != 0) {
191 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
192 ret = WIMLIB_ERR_WRITE;
198 * Extracts a regular file from the WIM archive.
200 static int extract_regular_file(WIMStruct *w,
201 struct dentry *dentry,
202 const char *output_dir,
203 const char *output_path,
206 struct lookup_table_entry *lte;
207 const struct inode *inode = dentry->d_inode;
209 lte = inode_unnamed_lte(inode, w->lookup_table);
211 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
212 WIMLIB_EXTRACT_FLAG_HARDLINK)) && lte) {
213 if (lte->extracted_file) {
214 return extract_regular_file_linked(dentry, output_dir,
218 lte->extracted_file = STRDUP(output_path);
219 if (!lte->extracted_file)
220 return WIMLIB_ERR_NOMEM;
224 return extract_regular_file_unlinked(w, dentry, output_path,
229 static int extract_symlink(const struct dentry *dentry, const char *output_path,
233 ssize_t ret = inode_readlink(dentry->d_inode, target,
234 sizeof(target), w, 0);
236 ERROR("Could not read the symbolic link from dentry `%s'",
237 dentry->full_path_utf8);
238 return WIMLIB_ERR_INVALID_DENTRY;
240 ret = symlink(target, output_path);
242 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
243 output_path, target);
244 return WIMLIB_ERR_LINK;
250 * Extracts a directory from the WIM archive.
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.
256 static int extract_directory(const char *output_path, bool is_root)
260 ret = stat(output_path, &stbuf);
262 if (S_ISDIR(stbuf.st_mode)) {
264 WARNING("`%s' already exists", output_path);
267 ERROR("`%s' is not a directory", output_path);
268 return WIMLIB_ERR_MKDIR;
271 if (errno != ENOENT) {
272 ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
273 return WIMLIB_ERR_STAT;
276 /* Compute the output path directory to the directory. */
277 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP |
278 S_IROTH | S_IXOTH) != 0) {
279 ERROR_WITH_ERRNO("Cannot create directory `%s'",
281 return WIMLIB_ERR_MKDIR;
286 struct extract_args {
289 const char *output_dir;
290 unsigned num_lutimes_warnings;
294 * Extracts a file, directory, or symbolic link from the WIM archive. For use
295 * in for_dentry_in_tree().
297 static int extract_dentry(struct dentry *dentry, void *arg)
299 struct extract_args *args = arg;
300 WIMStruct *w = args->w;
301 int extract_flags = args->extract_flags;
302 size_t len = strlen(args->output_dir);
303 char output_path[len + dentry->full_path_utf8_len + 1];
305 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) {
306 wimlib_assert(dentry->full_path_utf8);
307 puts(dentry->full_path_utf8);
310 memcpy(output_path, args->output_dir, len);
311 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
312 output_path[len + dentry->full_path_utf8_len] = '\0';
314 if (dentry_is_symlink(dentry))
315 return extract_symlink(dentry, output_path, w);
316 else if (dentry_is_directory(dentry))
317 return extract_directory(output_path, dentry_is_root(dentry));
319 return extract_regular_file(w, dentry, args->output_dir,
320 output_path, extract_flags);
323 /* Apply timestamp to extracted file */
324 static int apply_dentry_timestamps(struct dentry *dentry, void *arg)
326 struct extract_args *args = arg;
327 size_t len = strlen(args->output_dir);
328 char output_path[len + dentry->full_path_utf8_len + 1];
329 const struct inode *inode = dentry->d_inode;
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';
336 struct timeval tv[2];
337 wim_timestamp_to_timeval(inode->last_access_time, &tv[0]);
338 wim_timestamp_to_timeval(inode->last_write_time, &tv[1]);
340 ret = lutimes(output_path, tv);
347 if (errno == ENOSYS) {
349 buf.actime = wim_timestamp_to_unix(inode->last_access_time);
350 buf.modtime = wim_timestamp_to_unix(inode->last_write_time);
351 if (utime(output_path, &buf) == 0)
355 if (errno != ENOSYS || args->num_lutimes_warnings < 10) {
356 WARNING("Failed to set timestamp on file `%s': %s",
357 output_path, strerror(errno));
358 args->num_lutimes_warnings++;
365 static int extract_single_image(WIMStruct *w, int image,
366 const char *output_dir, int extract_flags)
368 DEBUG("Extracting image %d", image);
371 ret = select_wim_image(w, image);
375 struct extract_args args = {
377 .extract_flags = extract_flags,
378 .output_dir = output_dir,
379 .num_lutimes_warnings = 0,
382 ret = for_dentry_in_tree(wim_root_dentry(w), extract_dentry, &args);
385 return for_dentry_in_tree_depth(wim_root_dentry(w),
386 apply_dentry_timestamps, &args);
391 /* Extracts all images from the WIM to @output_dir, with the images placed in
392 * subdirectories named by their image names. */
393 static int extract_all_images(WIMStruct *w, const char *output_dir,
396 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
397 size_t output_path_len = strlen(output_dir);
398 char buf[output_path_len + 1 + image_name_max_len + 1];
401 const char *image_name;
403 DEBUG("Attempting to extract all images from `%s' to `%s'",
404 w->filename, output_dir);
406 ret = extract_directory(output_dir, true);
410 memcpy(buf, output_dir, output_path_len);
411 buf[output_path_len] = '/';
412 for (image = 1; image <= w->hdr.image_count; image++) {
414 image_name = wimlib_get_image_name(w, image);
416 strcpy(buf + output_path_len + 1, image_name);
418 /* Image name is empty. Use image number instead */
419 sprintf(buf + output_path_len + 1, "%d", image);
421 ret = extract_single_image(w, image, buf, extract_flags);
429 /* Extracts a single image or all images from a WIM file. */
430 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image,
431 const char *output_dir, int flags,
432 WIMStruct **additional_swms,
433 unsigned num_additional_swms)
435 struct lookup_table *joined_tab, *w_tab_save;
438 DEBUG("w->filename = %s, image = %d, output_dir = %s, flags = 0x%x, "
439 "num_additional_swms = %u",
440 w->filename, image, output_dir, flags, num_additional_swms);
442 if (!w || !output_dir)
443 return WIMLIB_ERR_INVALID_PARAM;
445 if ((flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
446 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
447 return WIMLIB_ERR_INVALID_PARAM;
449 ret = verify_swm_set(w, additional_swms, num_additional_swms);
453 if (num_additional_swms) {
454 ret = new_joined_lookup_table(w, additional_swms,
455 num_additional_swms, &joined_tab);
458 w_tab_save = w->lookup_table;
459 w->lookup_table = joined_tab;
462 for_lookup_table_entry(w->lookup_table, lte_zero_extracted_file, NULL);
464 if (image == WIM_ALL_IMAGES) {
465 flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
466 ret = extract_all_images(w, output_dir, flags);
468 flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
469 ret = extract_single_image(w, image, output_dir, flags);
471 if (num_additional_swms) {
472 free_lookup_table(w->lookup_table);
473 w->lookup_table = w_tab_save;
475 for_lookup_table_entry(w->lookup_table, lte_free_extracted_file, NULL);