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/.
27 #include "wimlib_internal.h"
29 #include "lookup_table.h"
30 #include "timestamp.h"
40 #include <ntfs-3g/volume.h>
41 #include <ntfs-3g/security.h>
45 #define WIMLIB_EXTRACT_FLAG_MULTI_IMAGE 0x80000000
47 static int extract_regular_file_linked(const struct dentry *dentry,
48 const char *output_dir,
49 const char *output_path,
51 struct lookup_table_entry *lte)
53 /* This mode overrides the normal hard-link extraction and
54 * instead either symlinks or hardlinks *all* identical files in
55 * the WIM, even if they are in a different image (in the case
56 * of a multi-image extraction) */
58 wimlib_assert(lte->file_on_disk);
61 if (extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
62 if (link(lte->file_on_disk, output_path) != 0) {
63 ERROR_WITH_ERRNO("Failed to hard link "
65 output_path, lte->file_on_disk);
66 return WIMLIB_ERR_LINK;
69 int num_path_components;
70 int num_output_dir_path_components;
71 size_t file_on_disk_len;
76 wimlib_assert(extract_flags & WIMLIB_EXTRACT_FLAG_SYMLINK);
79 get_num_path_components(dentry->full_path_utf8) - 1;
80 num_output_dir_path_components =
81 get_num_path_components(output_dir);
83 if (extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
84 num_path_components++;
85 num_output_dir_path_components--;
87 file_on_disk_len = strlen(lte->file_on_disk);
89 char buf[file_on_disk_len + 3 * num_path_components + 1];
92 for (i = 0; i < num_path_components; i++) {
97 p2 = lte->file_on_disk;
100 while (num_output_dir_path_components--)
101 p2 = path_next_part(p2, NULL);
103 if (symlink(buf, output_path) != 0) {
104 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
106 buf, lte->file_on_disk);
107 return WIMLIB_ERR_LINK;
114 static int extract_regular_file_unlinked(WIMStruct *w,
115 struct dentry *dentry,
116 const char *output_path,
118 struct lookup_table_entry *lte)
120 /* Normal mode of extraction. Regular files and hard links are
121 * extracted in the way that they appear in the WIM. */
124 const struct resource_entry *res_entry;
126 const struct list_head *head = &dentry->link_group_list;
128 if (head->next != head) {
129 /* This dentry is one of a hard link set of at least 2 dentries.
130 * If one of the other dentries has already been extracted, make
131 * a hard link to the file corresponding to this
132 * already-extracted directory. Otherwise, extract the
133 * file, and set the dentry->extracted_file field so that other
134 * dentries in the hard link group can link to it. */
135 struct dentry *other;
136 list_for_each_entry(other, head, link_group_list) {
137 if (other->extracted_file) {
138 DEBUG("Extracting hard link `%s' => `%s'",
139 output_path, other->extracted_file);
140 if (link(other->extracted_file, output_path) != 0) {
141 ERROR_WITH_ERRNO("Failed to hard link "
144 other->extracted_file);
145 return WIMLIB_ERR_LINK;
150 FREE(dentry->extracted_file);
151 dentry->extracted_file = STRDUP(output_path);
152 if (!dentry->extracted_file) {
153 ERROR("Failed to allocate memory for filename");
154 return WIMLIB_ERR_NOMEM;
158 /* Extract the contents of the file to @output_path. */
160 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
162 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
164 return WIMLIB_ERR_OPEN;
168 /* Empty file with no lookup table entry */
169 DEBUG("Empty file `%s'.", output_path);
175 res_entry = <e->resource_entry;
177 ret = extract_resource_to_fd(w, res_entry, out_fd,
178 res_entry->original_size);
181 ERROR("Failed to extract resource to `%s'", output_path);
185 if (extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
186 /* Mark the lookup table entry to indicate this file has been
189 FREE(lte->file_on_disk);
190 lte->file_on_disk = STRDUP(output_path);
191 if (!lte->file_on_disk)
192 ret = WIMLIB_ERR_NOMEM;
195 if (close(out_fd) != 0) {
196 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
197 ret = WIMLIB_ERR_WRITE;
203 * Extracts a regular file from the WIM archive.
205 static int extract_regular_file(WIMStruct *w,
206 struct dentry *dentry,
207 const char *output_dir,
208 const char *output_path,
211 struct lookup_table_entry *lte;
213 lte = dentry_first_lte(dentry, w->lookup_table);
215 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
216 WIMLIB_EXTRACT_FLAG_HARDLINK)) && lte) {
217 if (lte->out_refcnt++ != 0)
218 return extract_regular_file_linked(dentry, output_dir,
221 lte->file_on_disk = STRDUP(output_path);
222 if (!lte->file_on_disk)
223 return WIMLIB_ERR_NOMEM;
226 return extract_regular_file_unlinked(w, dentry, output_path,
231 static int extract_symlink(const struct dentry *dentry, const char *output_path,
235 ssize_t ret = dentry_readlink(dentry, target, sizeof(target), w);
237 ERROR("Could not read the symbolic link from dentry `%s'",
238 dentry->full_path_utf8);
239 return WIMLIB_ERR_INVALID_DENTRY;
241 ret = symlink(target, output_path);
243 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
244 output_path, target);
245 return WIMLIB_ERR_LINK;
251 * Extracts a directory from the WIM archive.
253 * @dentry: The directory entry for the directory.
254 * @output_path: The path to which the directory is to be extracted to.
255 * @return: True on success, false on failure.
257 static int extract_directory(const char *output_path)
259 /* Compute the output path directory to the directory. */
260 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0)
263 case EEXIST: /* Already existing directory is OK */
264 case EACCES: /* We may have permissions to extract files inside
265 the directory, but not for the directory
269 ERROR_WITH_ERRNO("Cannot create directory `%s'",
271 return WIMLIB_ERR_MKDIR;
277 struct extract_args {
280 const char *output_dir;
282 struct SECURITY_API *scapi;
287 * Extracts a file, directory, or symbolic link from the WIM archive. For use
288 * in for_dentry_in_tree().
290 static int extract_dentry(struct dentry *dentry, void *arg)
292 struct extract_args *args = arg;
293 WIMStruct *w = args->w;
294 int extract_flags = args->extract_flags;
295 size_t len = strlen(args->output_dir);
296 char output_path[len + dentry->full_path_utf8_len + 1];
299 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) {
300 wimlib_assert(dentry->full_path_utf8);
301 puts(dentry->full_path_utf8);
304 memcpy(output_path, args->output_dir, len);
305 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
306 output_path[len + dentry->full_path_utf8_len] = '\0';
308 if (dentry_is_symlink(dentry))
309 ret = extract_symlink(dentry, output_path, w);
310 else if (dentry_is_directory(dentry))
311 ret = extract_directory(output_path);
313 ret = extract_regular_file(w, dentry, args->output_dir,
314 output_path, extract_flags);
318 struct timeval tv[2];
319 wim_timestamp_to_timeval(dentry->last_access_time, &tv[0]);
320 wim_timestamp_to_timeval(dentry->last_write_time, &tv[1]);
321 if (lutimes(output_path, tv) != 0) {
322 WARNING("Failed to set timestamp on file `%s': %s",
323 output_path, strerror(errno));
329 static int extract_single_image(WIMStruct *w, int image,
330 const char *output_dir, int extract_flags)
332 DEBUG("Extracting image %d", image);
335 ret = wimlib_select_image(w, image);
339 struct extract_args args = {
341 .extract_flags = extract_flags,
342 .output_dir = output_dir,
348 return for_dentry_in_tree(wim_root_dentry(w), extract_dentry, &args);
352 /* Extracts all images from the WIM to @output_dir, with the images placed in
353 * subdirectories named by their image names. */
354 static int extract_all_images(WIMStruct *w, const char *output_dir,
357 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
358 size_t output_path_len = strlen(output_dir);
359 char buf[output_path_len + 1 + image_name_max_len + 1];
362 const char *image_name;
364 DEBUG("Attempting to extract all images from `%s'", w->filename);
366 ret = extract_directory(output_dir);
370 memcpy(buf, output_dir, output_path_len);
371 buf[output_path_len] = '/';
372 for (image = 1; image <= w->hdr.image_count; image++) {
374 image_name = wimlib_get_image_name(w, image);
376 strcpy(buf + output_path_len + 1, image_name);
378 /* Image name is empty. Use image number instead */
379 sprintf(buf + output_path_len + 1, "%d", image);
381 ret = extract_single_image(w, image, buf, extract_flags);
386 /* Restore original output directory */
387 buf[output_path_len + 1] = '\0';
391 /* Extracts a single image or all images from a WIM file. */
392 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image,
393 const char *output_dir, int flags)
397 return WIMLIB_ERR_INVALID_PARAM;
399 if ((flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
400 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
401 return WIMLIB_ERR_INVALID_PARAM;
403 if ((flags & WIMLIB_EXTRACT_FLAG_NTFS)) {
405 unsigned long mnt_flags;
406 int ret = ntfs_check_if_mounted(output_dir, &mnt_flags);
408 ERROR_WITH_ERRNO("NTFS-3g: Cannot determine if `%s' "
409 "is mounted", output_dir);
410 return WIMLIB_ERR_NTFS_3G;
412 if (!(mnt_flags & NTFS_MF_MOUNTED)) {
413 ERROR("NTFS-3g: Filesystem on `%s' is not mounted ",
415 return WIMLIB_ERR_NTFS_3G;
417 if (mnt_flags & NTFS_MF_READONLY) {
418 ERROR("NTFS-3g: Filesystem on `%s' is mounted "
419 "read-only", output_dir);
420 return WIMLIB_ERR_NTFS_3G;
423 ERROR("wimlib was compiled without support for NTFS-3g, so");
424 ERROR("we cannot extract a WIM image while preserving NTFS-");
425 ERROR("specific information");
426 return WIMLIB_ERR_UNSUPPORTED;
430 for_lookup_table_entry(w->lookup_table, zero_out_refcnts, NULL);
432 if (image == WIM_ALL_IMAGES) {
433 flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
434 return extract_all_images(w, output_dir, flags);
436 flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
437 return extract_single_image(w, image, output_dir, flags);