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"
40 * Extracts a regular file from the WIM archive.
42 * @dentry: The directory entry for the file, which must be a
44 * @output_path: The path to which the file is to be extracted.
45 * @lookup_table: The lookup table for the WIM file.
46 * @wim_fp: The FILE* for the WIM, opened for reading.
47 * @wim_ctype: The type of compression used in the WIM.
48 * @link_type: One of WIM_LINK_TYPE_*; specifies what to do with
49 * files that are hard-linked inside the WIM.
50 * @is_multi_image_extraction:
51 * True if the image currently being extracted is just one
52 * image of a multi-image extraction. This is needed so
53 * that cross-image symbolic links can be created
56 static int extract_regular_file(WIMStruct *w,
57 const struct dentry *dentry,
58 const char *output_path)
60 struct lookup_table *lookup_table;
62 bool is_multi_image_extraction;
63 struct lookup_table_entry *lte;
66 const struct resource_entry *res_entry;
68 lookup_table = w->lookup_table;
69 link_type = w->link_type;
70 is_multi_image_extraction = w->is_multi_image_extraction;
71 lte = lookup_resource(lookup_table, dentry->hash);
73 /* If we already extracted the same file or a hard link copy of it, we
74 * may be able to simply create a link. The exact action is specified
75 * by the current @link_type. */
76 if (link_type != WIM_LINK_TYPE_NONE && lte && lte->out_refcnt != 0) {
77 wimlib_assert(lte->file_on_disk);
79 if (link_type == WIM_LINK_TYPE_HARD) {
80 if (link(lte->file_on_disk, output_path) != 0) {
81 ERROR_WITH_ERRNO("Failed to hard link "
83 output_path, lte->file_on_disk);
84 return WIMLIB_ERR_LINK;
87 int num_path_components;
88 int num_output_dir_path_components;
89 size_t file_on_disk_len;
95 get_num_path_components(dentry->full_path_utf8) - 1;
96 num_output_dir_path_components =
97 get_num_path_components(w->output_dir);
99 if (is_multi_image_extraction) {
100 num_path_components++;
101 num_output_dir_path_components--;
103 file_on_disk_len = strlen(lte->file_on_disk);
105 char buf[file_on_disk_len + 3 * num_path_components + 1];
108 for (i = 0; i < num_path_components; i++) {
113 p2 = lte->file_on_disk;
116 while (num_output_dir_path_components--)
117 p2 = path_next_part(p2, NULL);
119 if (symlink(buf, output_path) != 0) {
120 ERROR_WITH_ERRNO("Failed to symlink `%s' to "
122 buf, lte->file_on_disk);
123 return WIMLIB_ERR_LINK;
130 /* Otherwise, we must actually extract the file contents. */
132 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
134 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
136 return WIMLIB_ERR_OPEN;
139 /* Extract empty file, with no lookup table entry... */
141 DEBUG("Empty file `%s'.", output_path);
146 res_entry = <e->resource_entry;
148 ret = extract_resource_to_fd(w, res_entry, out_fd,
149 res_entry->original_size);
152 ERROR("Failed to extract resource to `%s'", output_path);
156 /* Mark the lookup table entry to indicate this file has been extracted. */
158 FREE(lte->file_on_disk);
159 lte->file_on_disk = STRDUP(output_path);
160 if (lte->file_on_disk)
163 ret = WIMLIB_ERR_NOMEM;
170 * Extracts a directory from the WIM archive.
172 * @dentry: The directory entry for the directory.
173 * @output_path: The path to which the directory is to be extracted to.
174 * @return: True on success, false on failure.
176 static int extract_directory(struct dentry *dentry, const char *output_path)
178 /* Compute the output path directory to the directory. */
179 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0)
182 case EEXIST: /* Already existing directory is OK */
183 case EACCES: /* We may have permissions to extract files inside
184 the directory, but not for the directory
188 ERROR_WITH_ERRNO("Cannot create directory `%s'",
190 return WIMLIB_ERR_MKDIR;
198 * Extracts a file or directory from the WIM archive. For use in
199 * for_dentry_in_tree().
201 * @dentry: The dentry to extract.
202 * @arg: A pointer to the WIMStruct for the WIM file.
204 static int extract_regular_file_or_directory(struct dentry *dentry, void *arg)
206 WIMStruct *w = (WIMStruct*)arg;
207 size_t len = strlen(w->output_dir);
208 char output_path[len + dentry->full_path_utf8_len + 1];
211 puts(dentry->full_path_utf8);
213 memcpy(output_path, w->output_dir, len);
214 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
215 output_path[len + dentry->full_path_utf8_len] = '\0';
218 if (dentry_is_regular_file(dentry)) {
219 return extract_regular_file(w, dentry, output_path);
221 if (dentry_is_root(dentry)) /* Root doesn't need to be extracted. */
224 return extract_directory(dentry, output_path);
228 static int extract_single_image(WIMStruct *w, int image)
230 DEBUG("Extracting image %d", image);
233 ret = wimlib_select_image(w, image);
237 return for_dentry_in_tree(wim_root_dentry(w),
238 extract_regular_file_or_directory, w);
242 /* Extracts all images from the WIM to w->output_dir, with the images placed in
243 * subdirectories named by their image names. */
244 static int extract_all_images(WIMStruct *w)
246 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
247 size_t output_path_len = strlen(w->output_dir);
248 char buf[output_path_len + 1 + image_name_max_len + 1];
251 const char *image_name;
253 DEBUG("Attempting to extract all images from `%s'", w->filename);
255 memcpy(buf, w->output_dir, output_path_len);
256 buf[output_path_len] = '/';
257 for (image = 1; image <= w->hdr.image_count; image++) {
259 image_name = wimlib_get_image_name(w, image);
261 strcpy(buf + output_path_len + 1, image_name);
263 /* Image name is empty. Use image number instead */
264 sprintf(buf + output_path_len + 1, "%d", image);
266 ret = wimlib_set_output_dir(w, buf);
269 ret = extract_single_image(w, image);
274 /* Restore original output directory */
275 buf[output_path_len + 1] = '\0';
276 return wimlib_set_output_dir(w, buf);
279 /* Extracts a single image or all images from a WIM file. */
280 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image)
282 if (!w->output_dir) {
283 ERROR("No output directory selected.");
284 return WIMLIB_ERR_NOTDIR;
286 if (image == WIM_ALL_IMAGES) {
287 w->is_multi_image_extraction = true;
288 return extract_all_images(w);
290 w->is_multi_image_extraction = false;
291 return extract_single_image(w, image);
296 /* Set the output directory for WIM extraction. The directory is created using
297 * mkdir(). Fails if directory cannot be created or already exists. */
298 WIMLIBAPI int wimlib_set_output_dir(WIMStruct *w, const char *dir)
301 DEBUG("Setting output directory to `%s'", dir);
304 ERROR("Must specify a directory!");
305 return WIMLIB_ERR_INVALID_PARAM;
309 ERROR("Out of memory");
310 return WIMLIB_ERR_NOMEM;
313 if (mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
314 if (errno == EEXIST) {
315 DEBUG("`%s' already exists", dir);
318 ERROR_WITH_ERRNO("Cannot create directory `%s'", dir);
320 return WIMLIB_ERR_MKDIR;
322 DEBUG("Created directory `%s'", dir);
330 WIMLIBAPI int wimlib_set_link_type(WIMStruct *w, int link_type)
333 case WIM_LINK_TYPE_NONE:
334 case WIM_LINK_TYPE_HARD:
335 case WIM_LINK_TYPE_SYMBOLIC:
336 w->link_type = link_type;
339 return WIMLIB_ERR_INVALID_PARAM;