4 * Support for extracting WIM files.
8 * Copyright (C) 2010 Carl Thijssen
9 * Copyright (C) 2012 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
38 #include "lookup_table.h"
39 #include "timestamp.h"
40 #include "wimlib_internal.h"
44 #define WIMLIB_EXTRACT_FLAG_MULTI_IMAGE 0x80000000
46 static int extract_regular_file_linked(const struct dentry *dentry,
47 const char *output_dir,
48 const char *output_path,
50 struct lookup_table_entry *lte)
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) */
57 wimlib_assert(lte->file_on_disk);
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 "
64 output_path, lte->file_on_disk);
65 return WIMLIB_ERR_LINK;
68 int num_path_components;
69 int num_output_dir_path_components;
70 size_t file_on_disk_len;
75 wimlib_assert(extract_flags & WIMLIB_EXTRACT_FLAG_SYMLINK);
78 get_num_path_components(dentry->full_path_utf8) - 1;
79 num_output_dir_path_components =
80 get_num_path_components(output_dir);
82 if (extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
83 num_path_components++;
84 num_output_dir_path_components--;
86 file_on_disk_len = strlen(lte->file_on_disk);
88 char buf[file_on_disk_len + 3 * num_path_components + 1];
91 for (i = 0; i < num_path_components; i++) {
96 p2 = lte->file_on_disk;
99 while (num_output_dir_path_components--)
100 p2 = path_next_part(p2, NULL);
102 if (symlink(buf, output_path) != 0) {
103 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
105 buf, lte->file_on_disk);
106 return WIMLIB_ERR_LINK;
113 static int extract_regular_file_unlinked(WIMStruct *w,
114 struct dentry *dentry,
115 const char *output_path,
117 struct lookup_table_entry *lte)
119 /* Normal mode of extraction. Regular files and hard links are
120 * extracted in the way that they appear in the WIM. */
123 const struct resource_entry *res_entry;
125 const struct list_head *head = &dentry->link_group_list;
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 "
143 other->extracted_file);
144 return WIMLIB_ERR_LINK;
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;
157 /* Extract the contents of the file to @output_path. */
159 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
161 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
163 return WIMLIB_ERR_OPEN;
167 /* Empty file with no lookup table entry */
168 DEBUG("Empty file `%s'.", output_path);
174 res_entry = <e->resource_entry;
176 ret = extract_resource_to_fd(w, res_entry, out_fd,
177 res_entry->original_size);
180 ERROR("Failed to extract resource to `%s'", output_path);
184 if (extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
185 /* Mark the lookup table entry to indicate this file has been
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;
194 if (close(out_fd) != 0) {
195 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
196 ret = WIMLIB_ERR_WRITE;
202 * Extracts a regular file from the WIM archive.
204 static int extract_regular_file(WIMStruct *w,
205 struct dentry *dentry,
206 const char *output_dir,
207 const char *output_path,
210 struct lookup_table_entry *lte;
212 lte = dentry_first_lte(dentry, w->lookup_table);
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,
220 lte->file_on_disk = STRDUP(output_path);
221 if (!lte->file_on_disk)
222 return WIMLIB_ERR_NOMEM;
225 return extract_regular_file_unlinked(w, dentry, output_path,
230 static int extract_symlink(const struct dentry *dentry, const char *output_path,
234 ssize_t ret = dentry_readlink(dentry, target, sizeof(target), w);
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;
291 struct SECURITY_API *scapi;
296 * Extracts a file, directory, or symbolic link from the WIM archive. For use
297 * in for_dentry_in_tree().
299 static int extract_dentry(struct dentry *dentry, void *arg)
301 struct extract_args *args = arg;
302 WIMStruct *w = args->w;
303 int extract_flags = args->extract_flags;
304 size_t len = strlen(args->output_dir);
305 char output_path[len + dentry->full_path_utf8_len + 1];
308 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) {
309 wimlib_assert(dentry->full_path_utf8);
310 puts(dentry->full_path_utf8);
313 memcpy(output_path, args->output_dir, len);
314 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
315 output_path[len + dentry->full_path_utf8_len] = '\0';
317 if (dentry_is_symlink(dentry))
318 ret = extract_symlink(dentry, output_path, w);
319 else if (dentry_is_directory(dentry))
320 ret = extract_directory(output_path, dentry_is_root(dentry));
322 ret = extract_regular_file(w, dentry, args->output_dir,
323 output_path, extract_flags);
330 static int apply_dentry_timestamps(struct dentry *dentry, void *arg)
332 struct extract_args *args = arg;
333 size_t len = strlen(args->output_dir);
334 char output_path[len + dentry->full_path_utf8_len + 1];
336 memcpy(output_path, args->output_dir, len);
337 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
338 output_path[len + dentry->full_path_utf8_len] = '\0';
340 struct timeval tv[2];
341 wim_timestamp_to_timeval(dentry->last_access_time, &tv[0]);
342 wim_timestamp_to_timeval(dentry->last_write_time, &tv[1]);
343 if (lutimes(output_path, tv) != 0) {
344 WARNING("Failed to set timestamp on file `%s': %s",
345 output_path, strerror(errno));
351 static int extract_single_image(WIMStruct *w, int image,
352 const char *output_dir, int extract_flags)
354 DEBUG("Extracting image %d", image);
357 ret = wimlib_select_image(w, image);
361 struct extract_args args = {
363 .extract_flags = extract_flags,
364 .output_dir = output_dir,
370 ret = for_dentry_in_tree(wim_root_dentry(w), extract_dentry, &args);
373 return for_dentry_in_tree_depth(wim_root_dentry(w),
374 apply_dentry_timestamps, &args);
379 /* Extracts all images from the WIM to @output_dir, with the images placed in
380 * subdirectories named by their image names. */
381 static int extract_all_images(WIMStruct *w, const char *output_dir,
384 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
385 size_t output_path_len = strlen(output_dir);
386 char buf[output_path_len + 1 + image_name_max_len + 1];
389 const char *image_name;
391 DEBUG("Attempting to extract all images from `%s'", w->filename);
393 ret = extract_directory(output_dir, true);
397 memcpy(buf, output_dir, output_path_len);
398 buf[output_path_len] = '/';
399 for (image = 1; image <= w->hdr.image_count; image++) {
401 image_name = wimlib_get_image_name(w, image);
403 strcpy(buf + output_path_len + 1, image_name);
405 /* Image name is empty. Use image number instead */
406 sprintf(buf + output_path_len + 1, "%d", image);
408 ret = extract_single_image(w, image, buf, extract_flags);
413 /* Restore original output directory */
414 buf[output_path_len + 1] = '\0';
418 /* Extracts a single image or all images from a WIM file. */
419 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image,
420 const char *output_dir, int flags)
423 return WIMLIB_ERR_INVALID_PARAM;
425 if ((flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
426 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
427 return WIMLIB_ERR_INVALID_PARAM;
429 for_lookup_table_entry(w->lookup_table, zero_out_refcnts, NULL);
431 if (image == WIM_ALL_IMAGES) {
432 flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
433 return extract_all_images(w, output_dir, flags);
435 flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
436 return extract_single_image(w, image, output_dir, flags);