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("Failed to hard link `%s' to `%s': %m\n",
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(w->output_dir);
98 if (is_multi_image_extraction) {
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("Failed to symlink `%s' to `%s': %m\n",
120 buf, lte->file_on_disk);
121 return WIMLIB_ERR_LINK;
128 /* Otherwise, we must actually extract the file contents. */
130 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
132 ERROR("Failed to open the file `%s' for writing: "
133 "%m\n", output_path);
134 return WIMLIB_ERR_OPEN;
137 /* Extract empty file, with no lookup table entry... */
139 DEBUG("Empty file `%s'\n", output_path);
144 res_entry = <e->resource_entry;
146 ret = extract_resource_to_fd(w, res_entry, out_fd,
147 res_entry->original_size);
150 ERROR("Failed to extract resource to `%s'!\n", output_path);
154 /* Mark the lookup table entry to indicate this file has been extracted. */
156 FREE(lte->file_on_disk);
157 lte->file_on_disk = STRDUP(output_path);
158 if (lte->file_on_disk)
161 ret = WIMLIB_ERR_NOMEM;
168 * Extracts a directory from the WIM archive.
170 * @dentry: The directory entry for the directory.
171 * @output_path: The path to which the directory is to be extracted to.
172 * @return: True on success, false on failure.
174 static int extract_directory(struct dentry *dentry, const char *output_path)
176 /* Compute the output path directory to the directory. */
177 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0)
180 case EEXIST: /* Already existing directory is OK */
181 case EACCES: /* We may have permissions to extract files inside
182 the directory, but not for the directory
186 ERROR("Cannot create directory `%s': %m\n",
188 return WIMLIB_ERR_MKDIR;
196 * Extracts a file or directory from the WIM archive. For use in
197 * for_dentry_in_tree().
199 * @dentry: The dentry to extract.
200 * @arg: A pointer to the WIMStruct for the WIM file.
202 static int extract_regular_file_or_directory(struct dentry *dentry, void *arg)
204 WIMStruct *w = (WIMStruct*)arg;
205 size_t len = strlen(w->output_dir);
206 char output_path[len + dentry->full_path_utf8_len + 1];
209 puts(dentry->full_path_utf8);
211 memcpy(output_path, w->output_dir, len);
212 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
213 output_path[len + dentry->full_path_utf8_len] = '\0';
216 if (dentry_is_regular_file(dentry)) {
217 return extract_regular_file(w, dentry, output_path);
219 if (dentry_is_root(dentry)) /* Root doesn't need to be extracted. */
222 return extract_directory(dentry, output_path);
226 static int extract_single_image(WIMStruct *w, int image)
228 DEBUG("Extracting image %d\n", image);
231 ret = wimlib_select_image(w, image);
235 return for_dentry_in_tree(wim_root_dentry(w),
236 extract_regular_file_or_directory, w);
240 /* Extracts all images from the WIM to w->output_dir, with the images placed in
241 * subdirectories named by their image names. */
242 static int extract_all_images(WIMStruct *w)
244 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
245 size_t output_path_len = strlen(w->output_dir);
246 char buf[output_path_len + 1 + image_name_max_len + 1];
249 const char *image_name;
251 DEBUG("Attempting to extract all images from `%s'\n", w->filename);
253 memcpy(buf, w->output_dir, output_path_len);
254 buf[output_path_len] = '/';
255 for (image = 1; image <= w->hdr.image_count; image++) {
256 buf[output_path_len + 1] = '\0';
258 image_name = wimlib_get_image_name(w, image);
260 strncat(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);
275 buf[output_path_len + 1] = '\0';
276 wimlib_set_output_dir(w, buf);
280 /* Extracts a single image or all images from a WIM file. */
281 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image)
283 if (!w->output_dir) {
284 ERROR("No output directory selected.\n");
285 return WIMLIB_ERR_NOTDIR;
287 if (image == WIM_ALL_IMAGES) {
288 w->is_multi_image_extraction = true;
289 return extract_all_images(w);
291 w->is_multi_image_extraction = false;
292 return extract_single_image(w, image);
297 /* Set the output directory for WIM extraction. The directory is created using
298 * mkdir(). Fails if directory cannot be created or already exists. */
299 WIMLIBAPI int wimlib_set_output_dir(WIMStruct *w, const char *dir)
302 DEBUG("Setting output directory to `%s'\n", dir);
305 ERROR("Must specify a directory!\n");
306 return WIMLIB_ERR_INVALID_PARAM;
310 ERROR("Out of memory!\n");
311 return WIMLIB_ERR_NOMEM;
314 if (mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
315 if (errno == EEXIST) {
316 DEBUG("`%s' already exists\n", dir);
319 ERROR("Cannot create directory `%s': %m\n", dir);
321 return WIMLIB_ERR_MKDIR;
323 DEBUG("Created directory `%s'\n", dir);
331 WIMLIBAPI int wimlib_set_link_type(WIMStruct *w, int link_type)
334 case WIM_LINK_TYPE_NONE:
335 case WIM_LINK_TYPE_HARD:
336 case WIM_LINK_TYPE_SYMBOLIC:
337 w->link_type = link_type;
340 return WIMLIB_ERR_INVALID_PARAM;