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 static int extract_regular_file_linked(const struct dentry *dentry,
47 const char *output_dir,
48 const char *output_path,
50 const 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) */
56 wimlib_assert(lte->file_on_disk);
58 if (extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
59 if (link(lte->file_on_disk, output_path) != 0) {
60 ERROR_WITH_ERRNO("Failed to hard link "
62 output_path, lte->file_on_disk);
63 return WIMLIB_ERR_LINK;
66 int num_path_components;
67 int num_output_dir_path_components;
68 size_t file_on_disk_len;
74 get_num_path_components(dentry->full_path_utf8) - 1;
75 num_output_dir_path_components =
76 get_num_path_components(output_dir);
78 if (extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
79 num_path_components++;
80 num_output_dir_path_components--;
82 file_on_disk_len = strlen(lte->file_on_disk);
84 char buf[file_on_disk_len + 3 * num_path_components + 1];
87 for (i = 0; i < num_path_components; i++) {
92 p2 = lte->file_on_disk;
95 while (num_output_dir_path_components--)
96 p2 = path_next_part(p2, NULL);
98 if (symlink(buf, output_path) != 0) {
99 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
101 buf, lte->file_on_disk);
102 return WIMLIB_ERR_LINK;
109 static int extract_regular_file_unlinked(WIMStruct *w,
110 const struct dentry *dentry,
111 const char *output_path,
113 struct lookup_table_entry *lte)
116 const struct resource_entry *res_entry;
118 /* Otherwise, we must actually extract the file contents. */
120 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
122 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
124 return WIMLIB_ERR_OPEN;
127 /* Extract empty file, with no lookup table entry... */
129 DEBUG("Empty file `%s'.", output_path);
134 res_entry = <e->resource_entry;
136 ret = extract_resource_to_fd(w, res_entry, out_fd,
137 res_entry->original_size);
140 ERROR("Failed to extract resource to `%s'", output_path);
144 /* Mark the lookup table entry to indicate this file has been extracted. */
146 FREE(lte->file_on_disk);
147 lte->file_on_disk = STRDUP(output_path);
148 if (!lte->file_on_disk)
149 ret = WIMLIB_ERR_NOMEM;
156 * Extracts a regular file from the WIM archive.
158 static int extract_regular_file(WIMStruct *w,
159 const struct dentry *dentry,
160 const char *output_dir,
161 const char *output_path,
164 struct lookup_table_entry *lte;
166 lte = __lookup_resource(w->lookup_table, dentry_hash(dentry));
168 /* If we already extracted the same file or a hard link copy of it, we
169 * may be able to simply create a link. The exact action is specified
170 * by the current @link_type. */
171 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) &&
172 lte && lte->out_refcnt != 0)
173 return extract_regular_file_linked(dentry, output_dir,
174 output_path, extract_flags,
177 return extract_regular_file_unlinked(w, dentry, output_path,
182 static int extract_symlink(const struct dentry *dentry, const char *output_path,
186 ssize_t ret = dentry_readlink(dentry, target, sizeof(target), w);
188 ERROR("Could not read the symbolic link from dentry `%s'",
189 dentry->full_path_utf8);
190 return WIMLIB_ERR_INVALID_DENTRY;
192 ret = symlink(target, output_path);
194 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
195 output_path, target);
196 return WIMLIB_ERR_LINK;
202 * Extracts a directory from the WIM archive.
204 * @dentry: The directory entry for the directory.
205 * @output_path: The path to which the directory is to be extracted to.
206 * @return: True on success, false on failure.
208 static int extract_directory(struct dentry *dentry, const char *output_path)
210 /* Compute the output path directory to the directory. */
211 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0)
214 case EEXIST: /* Already existing directory is OK */
215 case EACCES: /* We may have permissions to extract files inside
216 the directory, but not for the directory
220 ERROR_WITH_ERRNO("Cannot create directory `%s'",
222 return WIMLIB_ERR_MKDIR;
228 struct extract_args {
231 const char *output_dir;
233 struct SECURITY_API *scapi;
238 * Extracts a file or directory from the WIM archive. For use in
239 * for_dentry_in_tree().
241 * @dentry: The dentry to extract.
242 * @arg: A pointer to the WIMStruct for the WIM file.
244 static int extract_dentry(struct dentry *dentry, void *arg)
246 struct extract_args *args = arg;
247 WIMStruct *w = args->w;
248 int extract_flags = args->extract_flags;
249 size_t len = strlen(args->output_dir);
250 char output_path[len + dentry->full_path_utf8_len + 1];
253 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
254 puts(dentry->full_path_utf8);
256 memcpy(output_path, args->output_dir, len);
257 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
258 output_path[len + dentry->full_path_utf8_len] = '\0';
260 if (dentry_is_symlink(dentry)) {
261 ret = extract_symlink(dentry, output_path, w);
262 } else if (dentry_is_directory(dentry)) {
263 ret = extract_directory(dentry, output_path);
265 ret = extract_regular_file(w, dentry, args->output_dir,
266 output_path, extract_flags);
272 static int extract_single_image(WIMStruct *w, int image,
273 const char *output_dir, int extract_flags)
275 DEBUG("Extracting image %d", image);
278 ret = wimlib_select_image(w, image);
282 struct extract_args args = {
284 .extract_flags = extract_flags,
285 .output_dir = output_dir,
291 return for_dentry_in_tree(wim_root_dentry(w), extract_dentry, &args);
295 /* Extracts all images from the WIM to @output_dir, with the images placed in
296 * subdirectories named by their image names. */
297 static int extract_all_images(WIMStruct *w, const char *output_dir,
300 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
301 size_t output_path_len = strlen(output_dir);
302 char buf[output_path_len + 1 + image_name_max_len + 1];
305 const char *image_name;
307 DEBUG("Attempting to extract all images from `%s'", w->filename);
309 memcpy(buf, output_dir, output_path_len);
310 buf[output_path_len] = '/';
311 for (image = 1; image <= w->hdr.image_count; image++) {
313 image_name = wimlib_get_image_name(w, image);
315 strcpy(buf + output_path_len + 1, image_name);
317 /* Image name is empty. Use image number instead */
318 sprintf(buf + output_path_len + 1, "%d", image);
320 ret = extract_single_image(w, image, buf, extract_flags);
325 /* Restore original output directory */
326 buf[output_path_len + 1] = '\0';
330 /* Extracts a single image or all images from a WIM file. */
331 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image,
332 const char *output_dir, int flags)
336 return WIMLIB_ERR_INVALID_PARAM;
337 if ((flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
338 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
339 return WIMLIB_ERR_INVALID_PARAM;
341 if (image == WIM_ALL_IMAGES)
342 flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
344 flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
346 if ((flags & WIMLIB_EXTRACT_FLAG_NTFS)) {
348 unsigned long mnt_flags;
349 ret = ntfs_check_if_mounted(output_dir, &mnt_flags);
351 ERROR_WITH_ERRNO("NTFS-3g: Cannot determine if `%s' "
352 "is mounted", output_dir);
353 return WIMLIB_ERR_NTFS_3G;
355 if (!(mnt_flags & NTFS_MF_MOUNTED)) {
356 ERROR("NTFS-3g: Filesystem on `%s' is not mounted ",
358 return WIMLIB_ERR_NTFS_3G;
360 if (mnt_flags & NTFS_MF_READONLY) {
361 ERROR("NTFS-3g: Filesystem on `%s' is mounted "
362 "read-only", output_dir);
363 return WIMLIB_ERR_NTFS_3G;
366 ERROR("wimlib was compiled without support for NTFS-3g, so");
367 ERROR("we cannot extract a WIM image while preserving NTFS-");
368 ERROR("specific information");
369 return WIMLIB_ERR_UNSUPPORTED;
372 if (image == WIM_ALL_IMAGES)
373 ret = extract_all_images(w, output_dir, flags);
375 ret = extract_single_image(w, image, output_dir, flags);