]> wimlib.net Git - wimlib/blob - src/wim.c
f9a6851c0c4a4a18dbb3c2d2ab6e9ab7e2df9c9a
[wimlib] / src / wim.c
1 /*
2  * wim.c
3  *
4  *
5  * Copyright (C) 2010 Carl Thijssen
6  * Copyright (C) 2012 Eric Biggers
7  *
8  * wimlib - Library for working with WIM files 
9  *
10  * This library is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU Lesser General Public License as published by the Free
12  * Software Foundation; either version 2.1 of the License, or (at your option) any
13  * later version.
14  *
15  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
17  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License along
20  * with this library; if not, write to the Free Software Foundation, Inc., 59
21  * Temple Place, Suite 330, Boston, MA 02111-1307 USA 
22  */
23
24 #include "wimlib_internal.h"
25 #include "io.h"
26 #include "lookup_table.h"
27 #include "xml.h"
28 #include <stdlib.h>
29
30 static int print_metadata(WIMStruct *w)
31 {
32 #ifdef ENABLE_SECURITY_DATA
33         print_security_data(wim_security_data(w));
34 #endif
35         return for_dentry_in_tree(wim_root_dentry(w), print_dentry, 
36                                   w->lookup_table);
37 }
38
39
40 static int print_files(WIMStruct *w)
41 {
42         return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path, 
43                                   NULL);
44 }
45
46 WIMStruct *new_wim_struct()
47 {
48         WIMStruct *w;
49         
50         w = CALLOC(1, sizeof(WIMStruct));
51         if (!w)
52                 return NULL;
53         w->link_type      = WIM_LINK_TYPE_HARD;
54         w->current_image  = WIM_NO_IMAGE;
55         return w;
56 }
57
58 /* 
59  * Calls a function on images in the WIM.  If @image is WIM_ALL_IMAGES, @visitor
60  * is called on the WIM once for each image, with each image selected as the
61  * current image in turn.  If @image is a certain image, @visitor is called on
62  * the WIM only once, with that image selected.
63  */
64 int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
65 {
66         int ret;
67         int i;
68         int image_count;
69
70         DEBUG("for_image(w = %p, image = %d, visitor = %p)\n", 
71                                 w, image, visitor);
72
73         if (image == WIM_ALL_IMAGES) {
74                 image_count = w->hdr.image_count;
75                 for (i = 1; i <= image_count; i++) {
76                         ret = wimlib_select_image(w, i);
77                         if (ret != 0)
78                                 return ret;
79                         ret = visitor(w);
80                         if (ret != 0)
81                                 return ret;
82                 }
83         } else {
84                 ret = wimlib_select_image(w, image);
85                 if (ret != 0)
86                         return ret;
87                 ret = visitor(w);
88         }
89         return ret;
90 }
91
92 static int sort_image_metadata_by_position(const void *p1, const void *p2)
93 {
94         struct image_metadata *bmd1 = (struct image_metadata*)p1;
95         struct image_metadata *bmd2 = (struct image_metadata*)p2;
96         u64 offset1 = bmd1->lookup_table_entry->resource_entry.offset;
97         u64 offset2 = bmd2->lookup_table_entry->resource_entry.offset;
98         if (offset1 < offset2)
99                 return -1;
100         else if (offset1 > offset2)
101                 return 1;
102         else
103                 return 0;
104 }
105
106 /* 
107  * If @lte points to a metadata resource, append it to the list of metadata
108  * resources in the WIMStruct.
109  */
110 static int append_metadata_resource_entry(struct lookup_table_entry *lte, 
111                                           void *wim_p)
112 {
113         WIMStruct *w = wim_p;
114
115         if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
116                 if (w->current_image == w->hdr.image_count) {
117                         ERROR("Expected only %u images, but found more!\n",
118                                         w->hdr.image_count);
119                         return WIMLIB_ERR_IMAGE_COUNT;
120                 } else {
121                         DEBUG("Found metadata resource for image %u at "
122                                 "offset %"PRIu64"\n", w->current_image + 1, 
123                                 lte->resource_entry.offset);
124                         w->image_metadata[
125                                 w->current_image++].lookup_table_entry = lte;
126                 }
127         }
128
129         /* Do nothing if not a metadata resource. */
130         return 0;
131 }
132
133 /* Returns the compression type given in the flags of a WIM header. */
134 int wim_hdr_flags_compression_type(int wim_hdr_flags)
135 {
136         if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESSION) {
137                 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_LZX)
138                         return WIM_COMPRESSION_TYPE_LZX;
139                 else if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_XPRESS)
140                         return WIM_COMPRESSION_TYPE_XPRESS;
141                 else
142                         return WIM_COMPRESSION_TYPE_INVALID;
143         } else {
144                 return WIM_COMPRESSION_TYPE_NONE;
145         }
146 }
147
148 int wim_resource_compression_type(const WIMStruct *w, 
149                                   const struct resource_entry *entry)
150 {
151         int wim_ctype = wimlib_get_compression_type(w);
152         return resource_compression_type(wim_ctype, entry->flags);
153 }
154
155 WIMLIBAPI void wimlib_set_verbose(WIMStruct *w, bool verbose)
156 {
157         w->verbose = verbose;
158 }
159
160 /*
161  * Creates a WIMStruct for a new WIM file.
162  */
163 WIMLIBAPI int wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
164 {
165         WIMStruct *w;
166         struct lookup_table *table;
167         int ret;
168
169         DEBUG("Creating new WIM with %s compression\n", 
170                         wimlib_get_compression_type_string(ctype));
171
172         /* Allocate the WIMStruct. */
173         w = new_wim_struct();
174         if (!w)
175                 return WIMLIB_ERR_NOMEM;
176
177         ret = init_header(&w->hdr, ctype);
178         if (ret != 0)
179                 goto err;
180
181         table = new_lookup_table(9001);
182         if (!table) {
183                 ret = WIMLIB_ERR_NOMEM;
184                 goto err;
185         }
186         w->lookup_table = table;
187         *w_ret = w;
188         return 0;
189 err:
190         FREE(w);
191         return ret;
192 }
193
194 WIMLIBAPI int wimlib_get_num_images(const WIMStruct *w)
195 {
196         return w->hdr.image_count;
197 }
198
199 int wimlib_select_image(WIMStruct *w, int image)
200 {
201         DEBUG("Selecting image %u\n", image);
202
203         if (image == w->current_image)
204                 return 0;
205
206         if (image > w->hdr.image_count || image < 1) {
207                 ERROR("Cannot select image %u: There are only %u images!\n",
208                                 image, w->hdr.image_count);
209                 return WIMLIB_ERR_INVALID_IMAGE;
210         }
211
212
213         /* If a valid image is selected, it can be freed if it is not modified.
214          * */
215         if (w->current_image != WIM_NO_IMAGE && 
216                                 !wim_current_image_is_modified(w)) {
217
218                 struct image_metadata *imd;
219
220                 DEBUG("Freeing image %u\n", w->current_image);
221                 imd = wim_get_current_image_metadata(w);
222                 free_dentry_tree(imd->root_dentry, NULL, false);
223                 imd->root_dentry = NULL;
224 #ifdef ENABLE_SECURITY_DATA
225                 free_security_data(imd->security_data);
226                 imd->security_data = NULL;
227 #endif
228         }
229
230         w->current_image = image;
231
232         if (wim_root_dentry(w)) {
233                 return 0;
234         } else {
235                 #ifdef ENABLE_DEBUG
236                 DEBUG("Reading metadata resource specified by the following "
237                                 "lookup table entry:\n");
238                 print_lookup_table_entry(wim_metadata_lookup_table_entry(w), NULL);
239                 #endif
240                 return read_metadata_resource(w->fp, 
241                                 wim_metadata_resource_entry(w),
242                                 wimlib_get_compression_type(w), 
243                                 wim_get_current_image_metadata(w));
244         }
245 }
246
247
248 /* Returns the compression type of the WIM file. */
249 WIMLIBAPI int wimlib_get_compression_type(const WIMStruct *w)
250 {
251         return wim_hdr_flags_compression_type(w->hdr.flags);
252 }
253
254 WIMLIBAPI const char *wimlib_get_compression_type_string(int ctype)
255 {
256         switch (ctype) {
257                 case WIM_COMPRESSION_TYPE_NONE:
258                         return "None";
259                 case WIM_COMPRESSION_TYPE_LZX:
260                         return "LZX";
261                 case WIM_COMPRESSION_TYPE_XPRESS:
262                         return "XPRESS";
263                 default:
264                         return "Invalid";
265         }
266 }
267
268 /*
269  * Returns the number of an image in the WIM file, given a string that is either
270  * the number of the image, or the name of the image.  The images are numbered
271  * starting at 1.
272  */
273 WIMLIBAPI int wimlib_resolve_image(WIMStruct *w, const char *image_name_or_num)
274 {
275         char *p;
276         int image;
277         int i;
278
279         if (!image_name_or_num)
280                 return WIM_NO_IMAGE;
281
282         if (strcmp(image_name_or_num, "all") == 0)
283                 return WIM_ALL_IMAGES;
284         image = strtoul(image_name_or_num, &p, 10);
285         if (*p == '\0') {
286                 if (image < 1 || image > w->hdr.image_count)
287                         return WIM_NO_IMAGE;
288                 return image;
289         } else {
290                 for (i = 1; i <= w->hdr.image_count; i++) {
291                         if (strcmp(image_name_or_num, 
292                                         wimlib_get_image_name(w, i)) == 0)
293                                 return i;
294                 }
295                 return WIM_NO_IMAGE;
296         }
297 }
298
299
300 /* Prints some basic information about a WIM file. */
301 WIMLIBAPI void wimlib_print_wim_information(const WIMStruct *w)
302 {
303         const struct wim_header *hdr;
304         
305         hdr = &w->hdr;
306         puts("WIM Information:");
307         puts("----------------");
308         printf("Path:           %s\n", w->filename);
309         fputs ("GUID:           0x", stdout);
310         print_byte_field(hdr->guid, WIM_GID_LEN);
311         putchar('\n');
312         printf("Image Count:    %d\n", hdr->image_count);
313         printf("Compression:    %s\n", wimlib_get_compression_type_string(
314                                                 wimlib_get_compression_type(w)));
315         printf("Part Number:    %d/%d\n", hdr->part_number, hdr->total_parts);
316         printf("Boot Index:     %d\n", hdr->boot_idx);
317         printf("Size:           %"PRIu64" bytes\n", 
318                                 wim_info_get_total_bytes(w->wim_info));
319         printf("Integrity Info: %s\n", (w->hdr.integrity.size != 0) ? "yes" : "no");
320         putchar('\n');
321 }
322
323 WIMLIBAPI bool wimlib_has_integrity_table(const WIMStruct *w)
324 {
325         return w->hdr.integrity.size != 0;
326 }
327
328
329 WIMLIBAPI void wimlib_print_available_images(const WIMStruct *w, int image)
330 {
331         if (image == WIM_ALL_IMAGES) {
332                 puts("Available Images:");
333                 puts("-----------------");
334         } else {
335                 int n;
336                 int i;
337
338                 n = printf("Information for Image %d\n", image);
339                 for (i = 0; i < n - 1; i++)
340                         putchar('-');
341                 putchar('\n');
342
343         }
344         print_image_info(w->wim_info, image);
345 }
346
347
348 /* Prints the metadata for the specified image, which may be WIM_ALL_IMAGES, but
349  * not WIM_NO_IMAGE. */
350 WIMLIBAPI int wimlib_print_metadata(WIMStruct *w, int image)
351 {
352         return for_image(w, image, print_metadata);
353 }
354
355 WIMLIBAPI int wimlib_print_files(WIMStruct *w, int image)
356 {
357         return for_image(w, image, print_files);
358 }
359
360 /* Sets the index of the bootable image. */
361 WIMLIBAPI int wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
362 {
363         if (boot_idx < 0 || boot_idx > w->hdr.image_count)
364                 return WIMLIB_ERR_INVALID_IMAGE;
365         w->hdr.boot_idx = boot_idx;
366         if (boot_idx == 0) {
367                 memset(&w->hdr.boot_metadata_res_entry, 0, 
368                        sizeof(struct resource_entry));
369         } else {
370                 memcpy(&w->hdr.boot_metadata_res_entry,
371                        &w->image_metadata[boot_idx - 1].lookup_table_entry->
372                                                         resource_entry, 
373                        sizeof(struct resource_entry));
374         }
375         return 0;
376 }
377
378 WIMLIBAPI int wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
379 {
380         if (total_parts_ret)
381                 *total_parts_ret = w->hdr.total_parts;
382         return w->hdr.part_number;
383 }
384                                     
385
386 WIMLIBAPI int wimlib_get_boot_idx(const WIMStruct *w)
387 {
388         return w->hdr.boot_idx;
389 }
390
391 /* 
392  * Begins the reading of a WIM file; opens the file and reads its header and
393  * lookup table, and optionally checks the integrity.
394  */
395 static int begin_read(WIMStruct *w, const char *in_wim_path, int flags)
396 {
397         int ret;
398         uint xml_num_images;
399         int integrity_status;
400
401         DEBUG("Reading the WIM file `%s'\n", in_wim_path);
402
403         w->filename = STRDUP(in_wim_path);
404         if (!w->filename) {
405                 ERROR("Failed to allocate memory for WIM filename.\n");
406                 return WIMLIB_ERR_NOMEM;
407         }
408
409         w->fp = fopen(in_wim_path, "rb");
410
411         if (!w->fp) {
412                 ERROR("Failed to open the file \"%s\" for reading: %m\n",
413                                 in_wim_path);
414                 ret = WIMLIB_ERR_OPEN;
415                 goto done;
416         }
417
418         ret = read_header(w->fp, &w->hdr, flags & WIMLIB_OPEN_FLAG_SPLIT_OK);
419         if (ret != 0)
420                 goto done;
421
422         DEBUG("Wim file contains %u images\n", w->hdr.image_count);
423
424         /* If the boot index is invalid, print a warning and set it to 0 */
425         if (w->hdr.boot_idx > w->hdr.image_count) {
426                 WARNING("In `%s', image %u is marked as bootable,\n"
427                         "\tbut there are only %u images!\n",
428                          in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
429                 w->hdr.boot_idx = 0;
430         }
431
432         if (wimlib_get_compression_type(w) == WIM_COMPRESSION_TYPE_INVALID) {
433                 ERROR("Invalid compression type (WIM header flags "
434                                 "= %x)\n", w->hdr.flags);
435                 ret = WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
436                 goto done;
437         }
438
439
440         if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
441                 ret = check_wim_integrity(w, 
442                                           flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS, 
443                                           &integrity_status);
444                 if (ret != 0) {
445                         ERROR("Error in check_wim_integrity()\n");
446                         goto done;
447                 }
448                 if (integrity_status == WIM_INTEGRITY_NONEXISTENT) {
449                         WARNING("No integrity information for `%s'; skipping "
450                                         "integrity check.\n", w->filename);
451                 } else if (integrity_status == WIM_INTEGRITY_NOT_OK) {
452                         ERROR("WIM is not intact! (Failed integrity check)\n");
453                         ret = WIMLIB_ERR_INTEGRITY;
454                         goto done;
455                 }
456         }
457
458         if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
459                 ERROR("Didn't expect a compressed lookup table!\n");
460                 ERROR("Ask the author to implement support for this.\n");
461                 ret = WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
462                 goto done;
463         }
464
465         ret = read_lookup_table(w->fp, w->hdr.lookup_table_res_entry.offset,
466                                 w->hdr.lookup_table_res_entry.size, 
467                                 &w->lookup_table);
468         
469         if (ret != 0)
470                 goto done;
471
472         w->image_metadata = CALLOC(w->hdr.image_count, 
473                                    sizeof(struct image_metadata));
474
475         if (!w->image_metadata) {
476                 ERROR("Failed to allocate memory for %u metadata structures\n",
477                                 w->hdr.image_count);
478                 goto done;
479         }
480         w->current_image = 0;
481
482         DEBUG("Looking for metadata resources in the lookup table.\n");
483
484         /* Find the images in the WIM by searching the lookup table. */
485         ret = for_lookup_table_entry(w->lookup_table, 
486                                      append_metadata_resource_entry, w);
487
488         if (ret != 0 && w->hdr.part_number == 1)
489                 goto done;
490
491         /* Make sure all the expected images were found.  (We already have
492          * returned false if *extra* images were found) */
493         if (w->current_image != w->hdr.image_count && w->hdr.part_number == 1) {
494                 ERROR("Only found %u images in WIM, but expected %u!\n",
495                                 w->current_image, w->hdr.image_count);
496                 ret = WIMLIB_ERR_IMAGE_COUNT;
497                 goto done;
498         }
499
500
501         /* Sort images by the position of their metadata resources.  I'm
502          * assuming that is what determines the other of the images in the WIM
503          * file, rather than their order in the lookup table, which may be
504          * random because of hashing. */
505         qsort(w->image_metadata, w->current_image,
506               sizeof(struct image_metadata), sort_image_metadata_by_position);
507
508         w->current_image = WIM_NO_IMAGE;
509
510         /* Read the XML data. */
511         ret = read_xml_data(w->fp, &w->hdr.xml_res_entry, 
512                             &w->xml_data, &w->wim_info);
513
514         if (ret != 0) {
515                 ERROR("Missing or invalid XML data\n");
516                 goto done;
517         }
518
519         xml_num_images = wim_info_get_num_images(w->wim_info);
520         if (xml_num_images != w->hdr.image_count) {
521                 ERROR("In the file `%s', there are %u <IMAGE> elements "
522                                 "in the XML data,\n", in_wim_path, 
523                                                         xml_num_images);
524                 ERROR("but %u images in the WIM!  There must be "
525                                 "exactly one <IMAGE> element per image.\n",
526                                 w->hdr.image_count);
527                 ret = WIMLIB_ERR_IMAGE_COUNT;
528                 goto done;
529         }
530
531         DEBUG("Done beginning read of WIM file.\n");
532         ret = 0;
533 done:
534         return ret;
535 }
536
537
538 /*
539  * Opens a WIM file and creates a WIMStruct for it.
540  */
541 WIMLIBAPI int wimlib_open_wim(const char *wim_file, int flags, 
542                               WIMStruct **w_ret)
543 {
544         WIMStruct *w;
545         int ret;
546
547         DEBUG("wim_file = `%s', flags = %d\n", wim_file, flags);
548         w = new_wim_struct();
549         if (!w)
550                 return WIMLIB_ERR_NOMEM;
551
552         ret = begin_read(w, wim_file, flags);
553         if (ret != 0) {
554                 ERROR("Could not begin reading the WIM file `%s'\n", wim_file);
555                 FREE(w);
556                 return ret;
557         }
558         *w_ret = w;
559         return 0;
560 }
561
562 /* Frees the memory for the WIMStruct, including all internal memory; also
563  * closes all files associated with the WIMStruct.  */
564 WIMLIBAPI void wimlib_free(WIMStruct *w)
565 {
566         uint i;
567
568         if (!w)
569                 return;
570         if (w->fp)
571                 fclose(w->fp);
572         if (w->out_fp)
573                 fclose(w->out_fp);
574
575         free_lookup_table(w->lookup_table);
576
577         FREE(w->filename);
578         FREE(w->output_dir);
579         FREE(w->xml_data);
580         free_wim_info(w->wim_info);
581         if (w->image_metadata) {
582                 for (i = 0; i < w->hdr.image_count; i++) {
583                         free_dentry_tree(w->image_metadata[i].root_dentry, 
584                                          NULL, false);
585                         #ifdef ENABLE_SECURITY_DATA
586                         free_security_data(w->image_metadata[i].security_data);
587                         #endif
588                 }
589                 FREE(w->image_metadata);
590         }
591         FREE(w);
592 }
593