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"
39 #include <ntfs-3g/volume.h>
40 #include <ntfs-3g/security.h>
43 /* Sets and creates the directory to which files are to be extracted when
44 * extracting files from the WIM. */
45 static int set_output_dir(WIMStruct *w, const char *dir)
48 DEBUG("Setting output directory to `%s'", dir);
52 ERROR("Out of memory");
53 return WIMLIB_ERR_NOMEM;
56 if (mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
57 if (errno == EEXIST) {
58 DEBUG("`%s' already exists", dir);
61 ERROR_WITH_ERRNO("Cannot create directory `%s'", dir);
63 return WIMLIB_ERR_MKDIR;
65 DEBUG("Created directory `%s'", dir);
74 * Extracts a regular file from the WIM archive.
76 * @dentry: The directory entry for the file, which must be a
78 * @output_path: The path to which the file is to be extracted.
79 * @lookup_table: The lookup table for the WIM file.
80 * @wim_fp: The FILE* for the WIM, opened for reading.
81 * @wim_ctype: The type of compression used in the WIM.
82 * @link_type: One of WIM_LINK_TYPE_*; specifies what to do with
83 * files that are hard-linked inside the WIM.
84 * @is_multi_image_extraction:
85 * True if the image currently being extracted is just one
86 * image of a multi-image extraction. This is needed so
87 * that cross-image symbolic links can be created
90 static int extract_regular_file(WIMStruct *w,
91 const struct dentry *dentry,
92 const char *output_path,
95 struct lookup_table_entry *lte;
98 const struct resource_entry *res_entry;
100 lte = lookup_resource(w->lookup_table, dentry_hash(dentry));
102 /* If we already extracted the same file or a hard link copy of it, we
103 * may be able to simply create a link. The exact action is specified
104 * by the current @link_type. */
105 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) &&
106 lte && lte->out_refcnt != 0)
108 wimlib_assert(lte->file_on_disk);
110 if (extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
111 if (link(lte->file_on_disk, output_path) != 0) {
112 ERROR_WITH_ERRNO("Failed to hard link "
114 output_path, lte->file_on_disk);
115 return WIMLIB_ERR_LINK;
118 int num_path_components;
119 int num_output_dir_path_components;
120 size_t file_on_disk_len;
125 num_path_components =
126 get_num_path_components(dentry->full_path_utf8) - 1;
127 num_output_dir_path_components =
128 get_num_path_components(w->output_dir);
130 if (w->is_multi_image_extraction) {
131 num_path_components++;
132 num_output_dir_path_components--;
134 file_on_disk_len = strlen(lte->file_on_disk);
136 char buf[file_on_disk_len + 3 * num_path_components + 1];
139 for (i = 0; i < num_path_components; i++) {
144 p2 = lte->file_on_disk;
147 while (num_output_dir_path_components--)
148 p2 = path_next_part(p2, NULL);
150 if (symlink(buf, output_path) != 0) {
151 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
153 buf, lte->file_on_disk);
154 return WIMLIB_ERR_LINK;
161 /* Otherwise, we must actually extract the file contents. */
163 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
165 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
167 return WIMLIB_ERR_OPEN;
170 /* Extract empty file, with no lookup table entry... */
172 DEBUG("Empty file `%s'.", output_path);
177 res_entry = <e->resource_entry;
179 ret = extract_resource_to_fd(w, res_entry, out_fd,
180 res_entry->original_size);
183 ERROR("Failed to extract resource to `%s'", output_path);
187 /* Mark the lookup table entry to indicate this file has been extracted. */
189 FREE(lte->file_on_disk);
190 lte->file_on_disk = STRDUP(output_path);
191 if (lte->file_on_disk)
194 ret = WIMLIB_ERR_NOMEM;
201 * Extracts a directory from the WIM archive.
203 * @dentry: The directory entry for the directory.
204 * @output_path: The path to which the directory is to be extracted to.
205 * @return: True on success, false on failure.
207 static int extract_directory(struct dentry *dentry, const char *output_path)
209 /* Compute the output path directory to the directory. */
210 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0)
213 case EEXIST: /* Already existing directory is OK */
214 case EACCES: /* We may have permissions to extract files inside
215 the directory, but not for the directory
219 ERROR_WITH_ERRNO("Cannot create directory `%s'",
221 return WIMLIB_ERR_MKDIR;
227 struct extract_args {
231 struct SECURITY_API *scapi;
236 * Extracts a file or directory from the WIM archive. For use in
237 * for_dentry_in_tree().
239 * @dentry: The dentry to extract.
240 * @arg: A pointer to the WIMStruct for the WIM file.
242 static int extract_regular_file_or_directory(struct dentry *dentry, void *arg)
244 struct extract_args *args = arg;
245 WIMStruct *w = args->w;
246 int extract_flags = args->extract_flags;
247 size_t len = strlen(w->output_dir);
248 char output_path[len + dentry->full_path_utf8_len + 1];
250 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
251 puts(dentry->full_path_utf8);
253 memcpy(output_path, w->output_dir, len);
254 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
255 output_path[len + dentry->full_path_utf8_len] = '\0';
257 if (dentry_is_regular_file(dentry)) {
258 return extract_regular_file(w, dentry, output_path, extract_flags);
260 if (dentry_is_root(dentry)) /* Root doesn't need to be extracted. */
263 return extract_directory(dentry, output_path);
268 static int extract_single_image(WIMStruct *w, int image, int extract_flags)
270 DEBUG("Extracting image %d", image);
273 ret = wimlib_select_image(w, image);
277 struct extract_args args = {
279 .extract_flags = extract_flags,
285 return for_dentry_in_tree(wim_root_dentry(w),
286 extract_regular_file_or_directory, &args);
290 /* Extracts all images from the WIM to w->output_dir, with the images placed in
291 * subdirectories named by their image names. */
292 static int extract_all_images(WIMStruct *w, int extract_flags)
294 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
295 size_t output_path_len = strlen(w->output_dir);
296 char buf[output_path_len + 1 + image_name_max_len + 1];
299 const char *image_name;
301 DEBUG("Attempting to extract all images from `%s'", w->filename);
303 memcpy(buf, w->output_dir, output_path_len);
304 buf[output_path_len] = '/';
305 for (image = 1; image <= w->hdr.image_count; image++) {
307 image_name = wimlib_get_image_name(w, image);
309 strcpy(buf + output_path_len + 1, image_name);
311 /* Image name is empty. Use image number instead */
312 sprintf(buf + output_path_len + 1, "%d", image);
314 ret = set_output_dir(w, buf);
317 ret = extract_single_image(w, image, extract_flags);
322 /* Restore original output directory */
323 buf[output_path_len + 1] = '\0';
327 /* Extracts a single image or all images from a WIM file. */
328 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image,
329 const char *output_dir, int flags)
333 return WIMLIB_ERR_INVALID_PARAM;
334 if ((flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
335 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
336 return WIMLIB_ERR_INVALID_PARAM;
338 ret = set_output_dir(w, output_dir);
342 if ((flags & WIMLIB_EXTRACT_FLAG_NTFS)) {
344 unsigned long mnt_flags;
345 ret = ntfs_check_if_mounted(output_dir, &mnt_flags);
347 ERROR_WITH_ERRNO("NTFS-3g: Cannot determine if `%s' "
348 "is mounted", output_dir);
349 return WIMLIB_ERR_NTFS_3G;
351 if (!(mnt_flags & NTFS_MF_MOUNTED)) {
352 ERROR("NTFS-3g: Filesystem on `%s' is not mounted ",
355 if (mnt_flags & NTFS_MF_READONLY) {
356 ERROR("NTFS-3g: Filesystem on `%s' is mounted "
357 "read-only", output_dir);
358 return WIMLIB_ERR_NTFS_3G;
361 ERROR("wimlib was compiled without support for NTFS-3g, so");
362 ERROR("we cannot extract a WIM image while preserving NTFS-");
363 ERROR("specific information");
364 return WIMLIB_ERR_UNSUPPORTED;
367 if (image == WIM_ALL_IMAGES) {
368 w->is_multi_image_extraction = true;
369 ret = extract_all_images(w, flags);
371 w->is_multi_image_extraction = false;
372 ret = extract_single_image(w, image, flags);