4 * Support for extracting WIM files.
6 * This code does NOT contain any filesystem-specific features. In particular,
7 * security information (i.e. file permissions) and alternate data streams are
8 * ignored, except possibly to read an alternate data stream that contains
13 * Copyright (C) 2010 Carl Thijssen
14 * Copyright (C) 2012 Eric Biggers
16 * This file is part of wimlib, a library for working with WIM files.
18 * wimlib is free software; you can redistribute it and/or modify it under the
19 * terms of the GNU General Public License as published by the Free
20 * Software Foundation; either version 3 of the License, or (at your option)
23 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
24 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
25 * A PARTICULAR PURPOSE. See the GNU General Public License for more
28 * You should have received a copy of the GNU General Public License
29 * along with wimlib; if not, see http://www.gnu.org/licenses/.
43 #include "lookup_table.h"
44 #include "timestamp.h"
45 #include "wimlib_internal.h"
49 #define WIMLIB_EXTRACT_FLAG_MULTI_IMAGE 0x80000000
51 static int extract_regular_file_linked(const struct dentry *dentry,
52 const char *output_dir,
53 const char *output_path,
55 struct lookup_table_entry *lte)
57 /* This mode overrides the normal hard-link extraction and
58 * instead either symlinks or hardlinks *all* identical files in
59 * the WIM, even if they are in a different image (in the case
60 * of a multi-image extraction) */
61 wimlib_assert(lte->extracted_file);
63 if (extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
64 if (link(lte->extracted_file, output_path) != 0) {
65 ERROR_WITH_ERRNO("Failed to hard link "
67 output_path, lte->extracted_file);
68 return WIMLIB_ERR_LINK;
71 int num_path_components;
72 int num_output_dir_path_components;
73 size_t extracted_file_len;
78 wimlib_assert(extract_flags & WIMLIB_EXTRACT_FLAG_SYMLINK);
81 get_num_path_components(dentry->full_path_utf8) - 1;
82 num_output_dir_path_components =
83 get_num_path_components(output_dir);
85 if (extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
86 num_path_components++;
87 num_output_dir_path_components--;
89 extracted_file_len = strlen(lte->extracted_file);
91 char buf[extracted_file_len + 3 * num_path_components + 1];
94 for (i = 0; i < num_path_components; i++) {
99 p2 = lte->extracted_file;
102 while (num_output_dir_path_components--)
103 p2 = path_next_part(p2, NULL);
105 if (symlink(buf, output_path) != 0) {
106 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
108 buf, lte->extracted_file);
109 return WIMLIB_ERR_LINK;
116 static int extract_regular_file_unlinked(WIMStruct *w,
117 struct dentry *dentry,
118 const char *output_path,
120 struct lookup_table_entry *lte)
122 /* Normal mode of extraction. Regular files and hard links are
123 * extracted in the way that they appear in the WIM. */
127 struct inode *inode = dentry->inode;
129 if (!((extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
130 && (extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
131 WIMLIB_EXTRACT_FLAG_HARDLINK))))
133 /* If the dentry is one of a hard link set of at least 2
134 * dentries and one of the other dentries has already been
135 * extracted, make a hard link to the file corresponding to this
136 * already-extracted directory. Otherwise, extract the file,
137 * and set the inode->extracted_file field so that other
138 * dentries in the hard link group can link to it. */
139 if (inode->link_count > 1) {
140 if (inode->extracted_file) {
141 DEBUG("Extracting hard link `%s' => `%s'",
142 output_path, inode->extracted_file);
143 if (link(inode->extracted_file, output_path) != 0) {
144 ERROR_WITH_ERRNO("Failed to hard link "
147 inode->extracted_file);
148 return WIMLIB_ERR_LINK;
152 FREE(inode->extracted_file);
153 inode->extracted_file = STRDUP(output_path);
154 if (!inode->extracted_file) {
155 ERROR("Failed to allocate memory for filename");
156 return WIMLIB_ERR_NOMEM;
161 /* Extract the contents of the file to @output_path. */
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;
171 /* Empty file with no lookup table entry */
172 DEBUG("Empty file `%s'.", output_path);
177 ret = extract_full_wim_resource_to_fd(lte, out_fd);
179 ERROR("Failed to extract resource to `%s'", output_path);
184 if (close(out_fd) != 0) {
185 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
186 ret = WIMLIB_ERR_WRITE;
192 * Extracts a regular file from the WIM archive.
194 static int extract_regular_file(WIMStruct *w,
195 struct dentry *dentry,
196 const char *output_dir,
197 const char *output_path,
200 struct lookup_table_entry *lte;
201 const struct inode *inode = dentry->inode;
203 lte = inode_unnamed_lte(inode, w->lookup_table);
205 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
206 WIMLIB_EXTRACT_FLAG_HARDLINK)) && lte) {
207 if (++lte->out_refcnt != 1)
208 return extract_regular_file_linked(dentry, output_dir,
211 FREE(lte->extracted_file);
212 lte->extracted_file = STRDUP(output_path);
213 if (!lte->extracted_file)
214 return WIMLIB_ERR_NOMEM;
217 return extract_regular_file_unlinked(w, dentry, output_path,
222 static int extract_symlink(const struct dentry *dentry, const char *output_path,
226 ssize_t ret = inode_readlink(dentry->inode, target, sizeof(target), w);
228 ERROR("Could not read the symbolic link from dentry `%s'",
229 dentry->full_path_utf8);
230 return WIMLIB_ERR_INVALID_DENTRY;
232 ret = symlink(target, output_path);
234 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
235 output_path, target);
236 return WIMLIB_ERR_LINK;
242 * Extracts a directory from the WIM archive.
244 * @dentry: The directory entry for the directory.
245 * @output_path: The path to which the directory is to be extracted to.
246 * @return: True on success, false on failure.
248 static int extract_directory(const char *output_path, bool is_root)
252 ret = stat(output_path, &stbuf);
254 if (S_ISDIR(stbuf.st_mode)) {
256 WARNING("`%s' already exists", output_path);
259 ERROR("`%s' is not a directory", output_path);
260 return WIMLIB_ERR_MKDIR;
263 if (errno != ENOENT) {
264 ERROR_WITH_ERRNO("Failed to stat `%s'", output_path);
265 return WIMLIB_ERR_STAT;
268 /* Compute the output path directory to the directory. */
269 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP |
270 S_IROTH | S_IXOTH) != 0) {
271 ERROR_WITH_ERRNO("Cannot create directory `%s'",
273 return WIMLIB_ERR_MKDIR;
278 struct extract_args {
281 const char *output_dir;
285 * Extracts a file, directory, or symbolic link from the WIM archive. For use
286 * in for_dentry_in_tree().
288 static int extract_dentry(struct dentry *dentry, void *arg)
290 struct extract_args *args = arg;
291 WIMStruct *w = args->w;
292 int extract_flags = args->extract_flags;
293 size_t len = strlen(args->output_dir);
294 char output_path[len + dentry->full_path_utf8_len + 1];
296 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) {
297 wimlib_assert(dentry->full_path_utf8);
298 puts(dentry->full_path_utf8);
301 memcpy(output_path, args->output_dir, len);
302 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
303 output_path[len + dentry->full_path_utf8_len] = '\0';
305 if (dentry_is_symlink(dentry))
306 return extract_symlink(dentry, output_path, w);
307 else if (dentry_is_directory(dentry))
308 return extract_directory(output_path, dentry_is_root(dentry));
310 return extract_regular_file(w, dentry, args->output_dir,
311 output_path, extract_flags);
314 /* Apply timestamp to extracted file */
315 static int apply_dentry_timestamps(struct dentry *dentry, void *arg)
317 struct extract_args *args = arg;
318 size_t len = strlen(args->output_dir);
319 char output_path[len + dentry->full_path_utf8_len + 1];
321 memcpy(output_path, args->output_dir, len);
322 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
323 output_path[len + dentry->full_path_utf8_len] = '\0';
325 struct timeval tv[2];
326 wim_timestamp_to_timeval(dentry->inode->last_access_time, &tv[0]);
327 wim_timestamp_to_timeval(dentry->inode->last_write_time, &tv[1]);
328 if (lutimes(output_path, tv) != 0) {
329 WARNING("Failed to set timestamp on file `%s': %s",
330 output_path, strerror(errno));
336 static int extract_single_image(WIMStruct *w, int image,
337 const char *output_dir, int extract_flags)
339 DEBUG("Extracting image %d", image);
342 ret = wimlib_select_image(w, image);
346 struct extract_args args = {
348 .extract_flags = extract_flags,
349 .output_dir = output_dir,
352 ret = for_dentry_in_tree(wim_root_dentry(w), extract_dentry, &args);
355 return for_dentry_in_tree_depth(wim_root_dentry(w),
356 apply_dentry_timestamps, &args);
361 /* Extracts all images from the WIM to @output_dir, with the images placed in
362 * subdirectories named by their image names. */
363 static int extract_all_images(WIMStruct *w, const char *output_dir,
366 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
367 size_t output_path_len = strlen(output_dir);
368 char buf[output_path_len + 1 + image_name_max_len + 1];
371 const char *image_name;
373 DEBUG("Attempting to extract all images from `%s' to `%s'",
374 w->filename, output_dir);
376 ret = extract_directory(output_dir, true);
380 memcpy(buf, output_dir, output_path_len);
381 buf[output_path_len] = '/';
382 for (image = 1; image <= w->hdr.image_count; image++) {
384 image_name = wimlib_get_image_name(w, image);
386 strcpy(buf + output_path_len + 1, image_name);
388 /* Image name is empty. Use image number instead */
389 sprintf(buf + output_path_len + 1, "%d", image);
391 ret = extract_single_image(w, image, buf, extract_flags);
399 /* Extracts a single image or all images from a WIM file. */
400 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image,
401 const char *output_dir, int flags,
402 WIMStruct **additional_swms,
403 unsigned num_additional_swms)
405 struct lookup_table *joined_tab, *w_tab_save;
408 DEBUG("w->filename = %s, image = %d, output_dir = %s, flags = 0x%x, "
409 "num_additional_swms = %u",
410 w->filename, image, output_dir, flags, num_additional_swms);
412 if (!w || !output_dir)
413 return WIMLIB_ERR_INVALID_PARAM;
415 if ((flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
416 == (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK))
417 return WIMLIB_ERR_INVALID_PARAM;
419 ret = verify_swm_set(w, additional_swms, num_additional_swms);
423 if (num_additional_swms) {
424 ret = new_joined_lookup_table(w, additional_swms,
425 num_additional_swms, &joined_tab);
428 w_tab_save = w->lookup_table;
429 w->lookup_table = joined_tab;
433 for_lookup_table_entry(w->lookup_table, zero_out_refcnts, NULL);
435 if (image == WIM_ALL_IMAGES) {
436 flags |= WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
437 ret = extract_all_images(w, output_dir, flags);
439 flags &= ~WIMLIB_EXTRACT_FLAG_MULTI_IMAGE;
440 ret = extract_single_image(w, image, output_dir, flags);
442 if (num_additional_swms) {
443 free_lookup_table(w->lookup_table);
444 w->lookup_table = w_tab_save;