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 #include <ntfs-3g/volume.h>
45 #include <ntfs-3g/security.h>
50 #define WIMLIB_EXTRACT_FLAG_MULTI_IMAGE 0x80000000
52 static int extract_regular_file_linked(const struct dentry *dentry,
53 const char *output_dir,
54 const char *output_path,
56 struct lookup_table_entry *lte)
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) */
63 wimlib_assert(lte->file_on_disk);
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 "
70 output_path, lte->file_on_disk);
71 return WIMLIB_ERR_LINK;
74 int num_path_components;
75 int num_output_dir_path_components;
76 size_t file_on_disk_len;
81 wimlib_assert(extract_flags & WIMLIB_EXTRACT_FLAG_SYMLINK);
84 get_num_path_components(dentry->full_path_utf8) - 1;
85 num_output_dir_path_components =
86 get_num_path_components(output_dir);
88 if (extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
89 num_path_components++;
90 num_output_dir_path_components--;
92 file_on_disk_len = strlen(lte->file_on_disk);
94 char buf[file_on_disk_len + 3 * num_path_components + 1];
97 for (i = 0; i < num_path_components; i++) {
102 p2 = lte->file_on_disk;
105 while (num_output_dir_path_components--)
106 p2 = path_next_part(p2, NULL);
108 if (symlink(buf, output_path) != 0) {
109 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
111 buf, lte->file_on_disk);
112 return WIMLIB_ERR_LINK;
119 static int extract_regular_file_unlinked(WIMStruct *w,
120 struct dentry *dentry,
121 const char *output_path,
123 struct lookup_table_entry *lte)
125 /* Normal mode of extraction. Regular files and hard links are
126 * extracted in the way that they appear in the WIM. */
129 const struct resource_entry *res_entry;
131 const struct list_head *head = &dentry->link_group_list;
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 "
149 other->extracted_file);
150 return WIMLIB_ERR_LINK;
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;
163 /* Extract the contents of the file to @output_path. */
165 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
167 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
169 return WIMLIB_ERR_OPEN;
173 /* Empty file with no lookup table entry */
174 DEBUG("Empty file `%s'.", output_path);
180 res_entry = <e->resource_entry;
182 ret = extract_resource_to_fd(w, res_entry, out_fd,
183 res_entry->original_size);
186 ERROR("Failed to extract resource to `%s'", output_path);
190 if (extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
191 /* Mark the lookup table entry to indicate this file has been
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;
200 if (close(out_fd) != 0) {
201 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
202 ret = WIMLIB_ERR_WRITE;
208 * Extracts a regular file from the WIM archive.
210 static int extract_regular_file(WIMStruct *w,
211 struct dentry *dentry,
212 const char *output_dir,
213 const char *output_path,
216 struct lookup_table_entry *lte;
218 lte = dentry_first_lte(dentry, w->lookup_table);
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,
226 lte->file_on_disk = STRDUP(output_path);
227 if (!lte->file_on_disk)
228 return WIMLIB_ERR_NOMEM;
231 return extract_regular_file_unlinked(w, dentry, output_path,
236 static int extract_symlink(const struct dentry *dentry, const char *output_path,
240 ssize_t ret = dentry_readlink(dentry, target, sizeof(target), w);
242 ERROR("Could not read the symbolic link from dentry `%s'",
243 dentry->full_path_utf8);
244 return WIMLIB_ERR_INVALID_DENTRY;
246 ret = symlink(target, output_path);
248 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
249 output_path, target);
250 return WIMLIB_ERR_LINK;
256 * Extracts a directory from the WIM archive.
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.
262 static int extract_directory(const char *output_path)
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)
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
274 ERROR_WITH_ERRNO("Cannot create directory `%s'",
276 return WIMLIB_ERR_MKDIR;
282 struct extract_args {
285 const char *output_dir;
287 struct SECURITY_API *scapi;
292 * Extracts a file, directory, or symbolic link from the WIM archive. For use
293 * in for_dentry_in_tree().
295 static int extract_dentry(struct dentry *dentry, void *arg)
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];
304 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) {
305 wimlib_assert(dentry->full_path_utf8);
306 puts(dentry->full_path_utf8);
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';
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);
318 ret = extract_regular_file(w, dentry, args->output_dir,
319 output_path, extract_flags);
326 static int apply_dentry_timestamps(struct dentry *dentry, void *arg)
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];
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(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));
347 static int extract_single_image(WIMStruct *w, int image,
348 const char *output_dir, int extract_flags)
350 DEBUG("Extracting image %d", image);
353 ret = wimlib_select_image(w, image);
357 struct extract_args args = {
359 .extract_flags = extract_flags,
360 .output_dir = output_dir,
366 ret = for_dentry_in_tree(wim_root_dentry(w), extract_dentry, &args);
369 return for_dentry_in_tree_depth(wim_root_dentry(w),
370 apply_dentry_timestamps, &args);
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,
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];
385 const char *image_name;
387 DEBUG("Attempting to extract all images from `%s'", w->filename);
389 ret = extract_directory(output_dir);
393 memcpy(buf, output_dir, output_path_len);
394 buf[output_path_len] = '/';
395 for (image = 1; image <= w->hdr.image_count; image++) {
397 image_name = wimlib_get_image_name(w, image);
399 strcpy(buf + output_path_len + 1, image_name);
401 /* Image name is empty. Use image number instead */
402 sprintf(buf + output_path_len + 1, "%d", image);
404 ret = extract_single_image(w, image, buf, extract_flags);
409 /* Restore original output directory */
410 buf[output_path_len + 1] = '\0';
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)
420 return WIMLIB_ERR_INVALID_PARAM;
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;
426 if ((flags & WIMLIB_EXTRACT_FLAG_NTFS)) {
428 unsigned long mnt_flags;
429 int ret = ntfs_check_if_mounted(output_dir, &mnt_flags);
431 ERROR_WITH_ERRNO("NTFS-3g: Cannot determine if `%s' "
432 "is mounted", output_dir);
433 return WIMLIB_ERR_NTFS_3G;
435 if (!(mnt_flags & NTFS_MF_MOUNTED)) {
436 ERROR("NTFS-3g: Filesystem on `%s' is not mounted ",
438 return WIMLIB_ERR_NTFS_3G;
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;
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;
453 for_lookup_table_entry(w->lookup_table, zero_out_refcnts, NULL);
455 if (image == WIM_ALL_IMAGES) {
456 flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
457 return extract_all_images(w, output_dir, flags);
459 flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
460 return extract_single_image(w, image, output_dir, flags);