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>
44 #define WIMLIB_EXTRACT_FLAG_MULTI_IMAGE 0x80000000
46 /* Sets and creates the directory to which files are to be extracted when
47 * extracting files from the WIM. */
48 static int make_output_dir(const char *dir)
51 DEBUG("Setting output directory to `%s'", dir);
53 if (mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
54 if (errno == EEXIST) {
55 DEBUG("`%s' already exists", dir);
58 ERROR_WITH_ERRNO("Cannot create directory `%s'", dir);
59 return WIMLIB_ERR_MKDIR;
61 DEBUG("Created directory `%s'", dir);
66 static int extract_regular_file_linked(const struct dentry *dentry,
67 const char *output_dir,
68 const char *output_path,
70 const struct lookup_table_entry *lte)
72 /* This mode overrides the normal hard-link extraction and
73 * instead either symlinks or hardlinks *all* identical files in
74 * the WIM, even if they are in a different image (in the case
75 * of a multi-image extraction) */
76 wimlib_assert(lte->file_on_disk);
78 if (extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
79 if (link(lte->file_on_disk, output_path) != 0) {
80 ERROR_WITH_ERRNO("Failed to hard link "
82 output_path, lte->file_on_disk);
83 return WIMLIB_ERR_LINK;
86 int num_path_components;
87 int num_output_dir_path_components;
88 size_t file_on_disk_len;
94 get_num_path_components(dentry->full_path_utf8) - 1;
95 num_output_dir_path_components =
96 get_num_path_components(output_dir);
98 if (extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
99 num_path_components++;
100 num_output_dir_path_components--;
102 file_on_disk_len = strlen(lte->file_on_disk);
104 char buf[file_on_disk_len + 3 * num_path_components + 1];
107 for (i = 0; i < num_path_components; i++) {
112 p2 = lte->file_on_disk;
115 while (num_output_dir_path_components--)
116 p2 = path_next_part(p2, NULL);
118 if (symlink(buf, output_path) != 0) {
119 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
121 buf, lte->file_on_disk);
122 return WIMLIB_ERR_LINK;
129 static int extract_regular_file_unlinked(WIMStruct *w,
130 const struct dentry *dentry,
131 const char *output_path,
133 struct lookup_table_entry *lte)
136 const struct resource_entry *res_entry;
138 /* Otherwise, we must actually extract the file contents. */
140 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
142 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
144 return WIMLIB_ERR_OPEN;
147 /* Extract empty file, with no lookup table entry... */
149 DEBUG("Empty file `%s'.", output_path);
154 res_entry = <e->resource_entry;
156 ret = extract_resource_to_fd(w, res_entry, out_fd,
157 res_entry->original_size);
160 ERROR("Failed to extract resource to `%s'", output_path);
164 /* Mark the lookup table entry to indicate this file has been extracted. */
166 FREE(lte->file_on_disk);
167 lte->file_on_disk = STRDUP(output_path);
168 if (!lte->file_on_disk)
169 ret = WIMLIB_ERR_NOMEM;
176 * Extracts a regular file from the WIM archive.
178 static int extract_regular_file(WIMStruct *w,
179 const struct dentry *dentry,
180 const char *output_dir,
181 const char *output_path,
184 struct lookup_table_entry *lte;
186 lte = __lookup_resource(w->lookup_table, dentry_hash(dentry));
188 /* If we already extracted the same file or a hard link copy of it, we
189 * may be able to simply create a link. The exact action is specified
190 * by the current @link_type. */
191 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) &&
192 lte && lte->out_refcnt != 0)
193 return extract_regular_file_linked(dentry, output_dir,
194 output_path, extract_flags,
197 return extract_regular_file_unlinked(w, dentry, output_path,
202 static int extract_symlink(const struct dentry *dentry, const char *output_path,
206 ssize_t ret = dentry_readlink(dentry, target, sizeof(target), w);
208 ERROR("Could not read the symbolic link from dentry `%s'",
209 dentry->full_path_utf8);
210 return WIMLIB_ERR_INVALID_DENTRY;
212 ret = symlink(target, output_path);
214 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
215 output_path, target);
216 return WIMLIB_ERR_LINK;
222 * Extracts a directory from the WIM archive.
224 * @dentry: The directory entry for the directory.
225 * @output_path: The path to which the directory is to be extracted to.
226 * @return: True on success, false on failure.
228 static int extract_directory(struct dentry *dentry, const char *output_path)
230 /* Compute the output path directory to the directory. */
231 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0)
234 case EEXIST: /* Already existing directory is OK */
235 case EACCES: /* We may have permissions to extract files inside
236 the directory, but not for the directory
240 ERROR_WITH_ERRNO("Cannot create directory `%s'",
242 return WIMLIB_ERR_MKDIR;
248 struct extract_args {
251 const char *output_dir;
253 struct SECURITY_API *scapi;
258 * Extracts a file or directory from the WIM archive. For use in
259 * for_dentry_in_tree().
261 * @dentry: The dentry to extract.
262 * @arg: A pointer to the WIMStruct for the WIM file.
264 static int extract_dentry(struct dentry *dentry, void *arg)
266 struct extract_args *args = arg;
267 WIMStruct *w = args->w;
268 int extract_flags = args->extract_flags;
269 size_t len = strlen(args->output_dir);
270 char output_path[len + dentry->full_path_utf8_len + 1];
273 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
274 puts(dentry->full_path_utf8);
276 memcpy(output_path, args->output_dir, len);
277 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
278 output_path[len + dentry->full_path_utf8_len] = '\0';
280 if (dentry_is_symlink(dentry)) {
281 ret = extract_symlink(dentry, output_path, w);
282 } else if (dentry_is_directory(dentry)) {
283 if (!dentry_is_root(dentry)) /* Root doesn't need to be extracted. */
284 ret = extract_directory(dentry, output_path);
286 ret = extract_regular_file(w, dentry, args->output_dir,
287 output_path, extract_flags);
293 static int extract_single_image(WIMStruct *w, int image,
294 const char *output_dir, int extract_flags)
296 DEBUG("Extracting image %d", image);
299 ret = wimlib_select_image(w, image);
303 struct extract_args args = {
305 .extract_flags = extract_flags,
306 .output_dir = output_dir,
312 return for_dentry_in_tree(wim_root_dentry(w), extract_dentry, &args);
316 /* Extracts all images from the WIM to @output_dir, with the images placed in
317 * subdirectories named by their image names. */
318 static int extract_all_images(WIMStruct *w, const char *output_dir,
321 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
322 size_t output_path_len = strlen(output_dir);
323 char buf[output_path_len + 1 + image_name_max_len + 1];
326 const char *image_name;
328 DEBUG("Attempting to extract all images from `%s'", w->filename);
330 memcpy(buf, output_dir, output_path_len);
331 buf[output_path_len] = '/';
332 for (image = 1; image <= w->hdr.image_count; image++) {
334 image_name = wimlib_get_image_name(w, image);
336 strcpy(buf + output_path_len + 1, image_name);
338 /* Image name is empty. Use image number instead */
339 sprintf(buf + output_path_len + 1, "%d", image);
341 ret = make_output_dir(buf);
344 ret = extract_single_image(w, image, buf, extract_flags);
349 /* Restore original output directory */
350 buf[output_path_len + 1] = '\0';
354 /* Extracts a single image or all images from a WIM file. */
355 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image,
356 const char *output_dir, int flags)
360 return WIMLIB_ERR_INVALID_PARAM;
361 if ((flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
362 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
363 return WIMLIB_ERR_INVALID_PARAM;
365 if (image == WIM_ALL_IMAGES)
366 flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
368 flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
370 ret = make_output_dir(output_dir);
374 if ((flags & WIMLIB_EXTRACT_FLAG_NTFS)) {
376 unsigned long mnt_flags;
377 ret = ntfs_check_if_mounted(output_dir, &mnt_flags);
379 ERROR_WITH_ERRNO("NTFS-3g: Cannot determine if `%s' "
380 "is mounted", output_dir);
381 return WIMLIB_ERR_NTFS_3G;
383 if (!(mnt_flags & NTFS_MF_MOUNTED)) {
384 ERROR("NTFS-3g: Filesystem on `%s' is not mounted ",
386 return WIMLIB_ERR_NTFS_3G;
388 if (mnt_flags & NTFS_MF_READONLY) {
389 ERROR("NTFS-3g: Filesystem on `%s' is mounted "
390 "read-only", output_dir);
391 return WIMLIB_ERR_NTFS_3G;
394 ERROR("wimlib was compiled without support for NTFS-3g, so");
395 ERROR("we cannot extract a WIM image while preserving NTFS-");
396 ERROR("specific information");
397 return WIMLIB_ERR_UNSUPPORTED;
400 if (image == WIM_ALL_IMAGES)
401 ret = extract_all_images(w, output_dir, flags);
403 ret = extract_single_image(w, image, output_dir, flags);