4 * Support for extracting WIM files.
6 * Copyright (C) 2010 Carl Thijssen
7 * Copyright (C) 2012 Eric Biggers
9 * wimlib - Library for working with WIM files
11 * This library is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 2.1 of the License, or (at your option) any
16 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
17 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
18 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License along
21 * with this library; if not, write to the Free Software Foundation, Inc., 59
22 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wimlib_internal.h"
27 #include "lookup_table.h"
38 * Extracts a regular file from the WIM archive.
40 * @dentry: The directory entry for the file, which must be a
42 * @output_path: The path to which the file is to be extracted.
43 * @lookup_table: The lookup table for the WIM file.
44 * @wim_fp: The FILE* for the WIM, opened for reading.
45 * @wim_ctype: The type of compression used in the WIM.
46 * @link_type: One of WIM_LINK_TYPE_*; specifies what to do with
47 * files that are hard-linked inside the WIM.
48 * @is_multi_image_extraction:
49 * True if the image currently being extracted is just one
50 * image of a multi-image extraction. This is needed so
51 * that cross-image symbolic links can be created
54 static int extract_regular_file(WIMStruct *w,
55 const struct dentry *dentry,
56 const char *output_path)
58 struct lookup_table *lookup_table;
60 bool is_multi_image_extraction;
61 struct lookup_table_entry *lte;
64 const struct resource_entry *res_entry;
66 lookup_table = w->lookup_table;
67 link_type = w->link_type;
68 is_multi_image_extraction = w->is_multi_image_extraction;
69 lte = lookup_resource(lookup_table, dentry->hash);
71 /* If we already extracted the same file or a hard link copy of it, we
72 * may be able to simply create a link. The exact action is specified
73 * by the current @link_type. */
74 if (link_type != WIM_LINK_TYPE_NONE && lte && lte->out_refcnt != 0) {
75 wimlib_assert(lte->file_on_disk);
77 if (link_type == WIM_LINK_TYPE_HARD) {
78 if (link(lte->file_on_disk, output_path) != 0) {
79 ERROR("Failed to hard link `%s' to `%s': %m\n",
80 output_path, lte->file_on_disk);
81 return WIMLIB_ERR_LINK;
84 int num_path_components;
85 int num_output_dir_path_components;
86 size_t file_on_disk_len;
92 get_num_path_components(dentry->full_path_utf8) - 1;
93 num_output_dir_path_components =
94 get_num_path_components(w->output_dir);
96 if (is_multi_image_extraction) {
97 num_path_components++;
98 num_output_dir_path_components--;
100 file_on_disk_len = strlen(lte->file_on_disk);
102 char buf[file_on_disk_len + 3 * num_path_components + 1];
105 for (i = 0; i < num_path_components; i++) {
110 p2 = lte->file_on_disk;
113 while (num_output_dir_path_components--)
114 p2 = path_next_part(p2, NULL);
116 if (symlink(buf, output_path) != 0) {
117 ERROR("Failed to symlink `%s' to `%s': %m\n",
118 buf, lte->file_on_disk);
119 return WIMLIB_ERR_LINK;
126 /* Otherwise, we must actually extract the file contents. */
128 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
130 ERROR("Failed to open the file `%s' for writing: "
131 "%m\n", output_path);
132 return WIMLIB_ERR_OPEN;
135 /* Extract empty file, with no lookup table entry... */
137 DEBUG("Empty file `%s'\n", output_path);
142 res_entry = <e->resource_entry;
144 ret = extract_resource_to_fd(w, res_entry, out_fd,
145 res_entry->original_size);
148 ERROR("Failed to extract resource to `%s'!\n", output_path);
152 /* Mark the lookup table entry to indicate this file has been extracted. */
154 FREE(lte->file_on_disk);
155 lte->file_on_disk = STRDUP(output_path);
156 if (lte->file_on_disk)
159 ret = WIMLIB_ERR_NOMEM;
166 * Extracts a directory from the WIM archive.
168 * @dentry: The directory entry for the directory.
169 * @output_path: The path to which the directory is to be extracted to.
170 * @return: True on success, false on failure.
172 static int extract_directory(struct dentry *dentry, const char *output_path)
174 /* Compute the output path directory to the directory. */
175 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0)
178 case EEXIST: /* Already existing directory is OK */
179 case EACCES: /* We may have permissions to extract files inside
180 the directory, but not for the directory
184 ERROR("Cannot create directory `%s': %m\n",
186 return WIMLIB_ERR_MKDIR;
194 * Extracts a file or directory from the WIM archive. For use in
195 * for_dentry_in_tree().
197 * @dentry: The dentry to extract.
198 * @arg: A pointer to the WIMStruct for the WIM file.
200 static int extract_regular_file_or_directory(struct dentry *dentry, void *arg)
202 WIMStruct *w = (WIMStruct*)arg;
203 size_t len = strlen(w->output_dir);
204 char output_path[len + dentry->full_path_utf8_len + 1];
207 puts(dentry->full_path_utf8);
209 memcpy(output_path, w->output_dir, len);
210 memcpy(output_path + len, dentry->full_path_utf8, dentry->full_path_utf8_len);
211 output_path[len + dentry->full_path_utf8_len] = '\0';
214 if (dentry_is_regular_file(dentry)) {
215 return extract_regular_file(w, dentry, output_path);
217 if (dentry_is_root(dentry)) /* Root doesn't need to be extracted. */
220 return extract_directory(dentry, output_path);
224 static int extract_single_image(WIMStruct *w, int image)
226 DEBUG("Extracting image %d\n", image);
229 ret = wimlib_select_image(w, image);
233 return for_dentry_in_tree(wim_root_dentry(w),
234 extract_regular_file_or_directory, w);
238 /* Extracts all images from the WIM to w->output_dir, with the images placed in
239 * subdirectories named by their image names. */
240 static int extract_all_images(WIMStruct *w)
242 size_t image_name_max_len = max(xml_get_max_image_name_len(w), 20);
243 size_t output_path_len = strlen(w->output_dir);
244 char buf[output_path_len + 1 + image_name_max_len + 1];
247 const char *image_name;
249 DEBUG("Attempting to extract all images from `%s'\n", w->filename);
251 memcpy(buf, w->output_dir, output_path_len);
252 buf[output_path_len] = '/';
253 for (image = 1; image <= w->hdr.image_count; image++) {
254 buf[output_path_len + 1] = '\0';
256 image_name = wimlib_get_image_name(w, image);
258 strncat(buf + output_path_len + 1, image_name,
261 /* Image name is empty. Use image number instead */
262 sprintf(buf + output_path_len + 1, "%d", image);
264 ret = wimlib_set_output_dir(w, buf);
267 ret = extract_single_image(w, image);
273 buf[output_path_len + 1] = '\0';
274 wimlib_set_output_dir(w, buf);
278 /* Extracts a single image or all images from a WIM file. */
279 WIMLIBAPI int wimlib_extract_image(WIMStruct *w, int image)
281 if (!w->output_dir) {
282 ERROR("No output directory selected.\n");
283 return WIMLIB_ERR_NOTDIR;
285 if (image == WIM_ALL_IMAGES) {
286 w->is_multi_image_extraction = true;
287 return extract_all_images(w);
289 w->is_multi_image_extraction = false;
290 return extract_single_image(w, image);
295 /* Set the output directory for WIM extraction. The directory is created using
296 * mkdir(). Fails if directory cannot be created or already exists. */
297 WIMLIBAPI int wimlib_set_output_dir(WIMStruct *w, const char *dir)
300 DEBUG("Setting output directory to `%s'\n", dir);
303 ERROR("Must specify a directory!\n");
304 return WIMLIB_ERR_INVALID_PARAM;
308 ERROR("Out of memory!\n");
309 return WIMLIB_ERR_NOMEM;
312 if (mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
313 if (errno == EEXIST) {
314 DEBUG("`%s' already exists\n", dir);
317 ERROR("Cannot create directory `%s': %m\n", dir);
319 return WIMLIB_ERR_MKDIR;
321 DEBUG("Created directory `%s'\n", dir);
329 WIMLIBAPI int wimlib_set_link_type(WIMStruct *w, int link_type)
332 case WIM_LINK_TYPE_NONE:
333 case WIM_LINK_TYPE_HARD:
334 case WIM_LINK_TYPE_SYMBOLIC:
335 w->link_type = link_type;
338 return WIMLIB_ERR_INVALID_PARAM;