]> wimlib.net Git - wimlib/blob - src/wim.c
Use WARNING() for warnings instead of ERROR().
[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 #if 0
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 static 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 #if 0
224                 destroy_security_data(&imd->security_data);
225 #endif
226                 imd->root_dentry = NULL;
227         }
228
229         w->current_image = image;
230
231         if (wim_root_dentry(w))
232                 return 0;
233         else
234                 return read_metadata_resource(w->fp, 
235                                 wim_metadata_resource_entry(w),
236                                 wimlib_get_compression_type(w), 
237                                 /*wim_security_data(w), */
238                                 wim_root_dentry_p(w));
239 }
240
241
242 /* Returns the compression type of the WIM file. */
243 WIMLIBAPI int wimlib_get_compression_type(const WIMStruct *w)
244 {
245         return wim_hdr_flags_compression_type(w->hdr.flags);
246 }
247
248 WIMLIBAPI const char *wimlib_get_compression_type_string(int ctype)
249 {
250         switch (ctype) {
251                 case WIM_COMPRESSION_TYPE_NONE:
252                         return "None";
253                 case WIM_COMPRESSION_TYPE_LZX:
254                         return "LZX";
255                 case WIM_COMPRESSION_TYPE_XPRESS:
256                         return "XPRESS";
257                 default:
258                         return "Invalid";
259         }
260 }
261
262 /*
263  * Returns the number of an image in the WIM file, given a string that is either
264  * the number of the image, or the name of the image.  The images are numbered
265  * starting at 1.
266  */
267 WIMLIBAPI int wimlib_resolve_image(WIMStruct *w, const char *image_name_or_num)
268 {
269         char *p;
270         int image;
271         int i;
272
273         if (!image_name_or_num)
274                 return WIM_NO_IMAGE;
275
276         if (strcmp(image_name_or_num, "all") == 0)
277                 return WIM_ALL_IMAGES;
278         image = strtoul(image_name_or_num, &p, 10);
279         if (*p == '\0') {
280                 if (image < 1 || image > w->hdr.image_count)
281                         return WIM_NO_IMAGE;
282                 return image;
283         } else {
284                 for (i = 1; i <= w->hdr.image_count; i++) {
285                         if (strcmp(image_name_or_num, 
286                                         wimlib_get_image_name(w, i)) == 0)
287                                 return i;
288                 }
289                 return WIM_NO_IMAGE;
290         }
291 }
292
293
294 /* Prints some basic information about a WIM file. */
295 WIMLIBAPI void wimlib_print_wim_information(const WIMStruct *w)
296 {
297         const struct wim_header *hdr;
298         
299         hdr = &w->hdr;
300         puts("WIM Information:");
301         puts("----------------");
302         printf("Path:           %s\n", w->filename);
303         fputs ("GUID:           0x", stdout);
304         print_byte_field(hdr->guid, WIM_GID_LEN);
305         putchar('\n');
306         printf("Image Count:    %d\n", hdr->image_count);
307         printf("Compression:    %s\n", wimlib_get_compression_type_string(
308                                                 wimlib_get_compression_type(w)));
309         printf("Part Number:    %d/%d\n", 1, 1);
310         printf("Boot Index:     %d\n", hdr->boot_idx);
311         printf("Size:           %"PRIu64" bytes\n", 
312                                 wim_info_get_total_bytes(w->wim_info));
313         printf("Integrity Info: %s\n", (w->hdr.integrity.size != 0) ? "yes" : "no");
314         putchar('\n');
315 }
316
317 WIMLIBAPI bool wimlib_has_integrity_table(const WIMStruct *w)
318 {
319         return w->hdr.integrity.size != 0;
320 }
321
322
323 WIMLIBAPI void wimlib_print_available_images(const WIMStruct *w, int image)
324 {
325         if (image == WIM_ALL_IMAGES) {
326                 puts("Available Images:");
327                 puts("-----------------");
328         } else {
329                 int n;
330                 int i;
331
332                 n = printf("Information for Image %d\n", image);
333                 for (i = 0; i < n - 1; i++)
334                         putchar('-');
335                 putchar('\n');
336
337         }
338         print_image_info(w->wim_info, image);
339 }
340
341
342 /* Prints the metadata for the specified image, which may be WIM_ALL_IMAGES, but
343  * not WIM_NO_IMAGE. */
344 WIMLIBAPI int wimlib_print_metadata(WIMStruct *w, int image)
345 {
346         return for_image(w, image, print_metadata);
347 }
348
349 WIMLIBAPI int wimlib_print_files(WIMStruct *w, int image)
350 {
351         return for_image(w, image, print_files);
352 }
353
354 /* Sets the index of the bootable image. */
355 WIMLIBAPI int wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
356 {
357         if (boot_idx < 0 || boot_idx > w->hdr.image_count)
358                 return WIMLIB_ERR_INVALID_IMAGE;
359         w->hdr.boot_idx = boot_idx;
360         if (boot_idx == 0) {
361                 memset(&w->hdr.boot_metadata_res_entry, 0, 
362                        sizeof(struct resource_entry));
363         } else {
364                 memcpy(&w->hdr.boot_metadata_res_entry,
365                        &w->image_metadata[boot_idx - 1].lookup_table_entry->
366                                                         resource_entry, 
367                        sizeof(struct resource_entry));
368         }
369         return 0;
370 }
371
372 WIMLIBAPI int wimlib_get_boot_idx(const WIMStruct *w)
373 {
374         return w->hdr.boot_idx;
375 }
376
377 /* 
378  * Begins the reading of a WIM file; opens the file and reads its header and
379  * lookup table, and optionally checks the integrity.
380  */
381 static int wim_begin_read(WIMStruct *w, const char *in_wim_path, int flags)
382 {
383         int ret;
384         uint xml_num_images;
385         int integrity_status;
386
387         DEBUG("Reading the WIM file `%s'\n", in_wim_path);
388
389         w->filename = STRDUP(in_wim_path);
390         if (!w->filename) {
391                 ERROR("Failed to allocate memory for WIM filename.\n");
392                 return WIMLIB_ERR_NOMEM;
393         }
394
395         w->fp = fopen(in_wim_path, "rb");
396
397         if (!w->fp) {
398                 ERROR("Failed to open the file \"%s\" for reading: %m\n",
399                                 in_wim_path);
400                 ret = WIMLIB_ERR_OPEN;
401                 goto done;
402         }
403
404         ret = read_header(w->fp, &w->hdr);
405         if (ret != 0)
406                 goto done;
407
408         DEBUG("Wim file contains %u images\n", w->hdr.image_count);
409
410         /* If the boot index is invalid, print a warning and set it to 0 */
411         if (w->hdr.boot_idx > w->hdr.image_count) {
412                 WARNING("In `%s', image %u is marked as bootable,\n"
413                         "\tbut there are only %u images!\n",
414                          in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
415                 w->hdr.boot_idx = 0;
416         }
417
418         if (wimlib_get_compression_type(w) == WIM_COMPRESSION_TYPE_INVALID) {
419                 ERROR("Invalid compression type (WIM header flags "
420                                 "= %x)\n", w->hdr.flags);
421                 ret = WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
422                 goto done;
423         }
424
425
426         if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
427                 ret = check_wim_integrity(w, 
428                                           flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS, 
429                                           &integrity_status);
430                 if (ret != 0) {
431                         ERROR("Error in check_wim_integrity()\n");
432                         goto done;
433                 }
434                 if (integrity_status == WIM_INTEGRITY_NONEXISTENT) {
435                         WARNING("No integrity information; skipping "
436                                         "integrity check.\n");
437                 } else if (integrity_status == WIM_INTEGRITY_NOT_OK) {
438                         ERROR("WIM is not intact! (Failed integrity check)\n");
439                         ret = WIMLIB_ERR_INTEGRITY;
440                         goto done;
441                 }
442         }
443
444         if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
445                 ERROR("Didn't expect a compressed lookup table!\n");
446                 ERROR("Ask the author to implement support for this.\n");
447                 ret = WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
448                 goto done;
449         }
450
451         ret = read_lookup_table(w->fp, w->hdr.lookup_table_res_entry.offset,
452                                 w->hdr.lookup_table_res_entry.size, 
453                                 &w->lookup_table);
454         
455         if (ret != 0)
456                 goto done;
457
458         w->image_metadata = CALLOC(w->hdr.image_count, 
459                                    sizeof(struct image_metadata));
460
461         if (!w->image_metadata) {
462                 ERROR("Failed to allocate memory for %u metadata structures\n",
463                                 w->hdr.image_count);
464                 goto done;
465         }
466         w->current_image = 0;
467
468         DEBUG("Looking for metadata resources in the lookup table.\n");
469
470         /* Find the images in the WIM by searching the lookup table. */
471         ret = for_lookup_table_entry(w->lookup_table, 
472                                      append_metadata_resource_entry, w);
473
474         if (ret != 0)
475                 goto done;
476
477         /* Make sure all the expected images were found.  (We already have
478          * returned false if *extra* images were found) */
479         if (w->current_image != w->hdr.image_count) {
480                 ERROR("Only found %u images in WIM, but expected %u!\n",
481                                 w->current_image, w->hdr.image_count);
482                 ret = WIMLIB_ERR_IMAGE_COUNT;
483                 goto done;
484         }
485
486         w->current_image = WIM_NO_IMAGE;
487
488         /* Sort images by the position of their metadata resources.  I'm
489          * assuming that is what determines the other of the images in the WIM
490          * file, rather than their order in the lookup table, which may be
491          * random because of hashing. */
492         qsort(w->image_metadata, w->hdr.image_count, 
493               sizeof(struct image_metadata), sort_image_metadata_by_position);
494
495         /* Read the XML data. */
496         ret = read_xml_data(w->fp, &w->hdr.xml_res_entry, 
497                             &w->xml_data, &w->wim_info);
498
499         if (ret != 0) {
500                 ERROR("Missing or invalid XML data\n");
501                 goto done;
502         }
503
504         xml_num_images = wim_info_get_num_images(w->wim_info);
505         if (xml_num_images != w->hdr.image_count) {
506                 ERROR("In the file `%s', there are %u <IMAGE> elements "
507                                 "in the XML data,\n", in_wim_path, 
508                                                         xml_num_images);
509                 ERROR("but %u images in the WIM!  There must be "
510                                 "exactly one <IMAGE> element per image.\n",
511                                 w->hdr.image_count);
512                 ret = WIMLIB_ERR_IMAGE_COUNT;
513                 goto done;
514         }
515
516         DEBUG("Done beginning read of WIM file.\n");
517         ret = 0;
518 done:
519         return ret;
520 }
521
522
523 /*
524  * Opens a WIM file and creates a WIMStruct for it.
525  */
526 WIMLIBAPI int wimlib_open_wim(const char *wim_file, int flags, 
527                               WIMStruct **w_ret)
528 {
529         WIMStruct *w;
530         int ret;
531
532         DEBUG("wim_file = `%s', flags = %d\n", wim_file, flags);
533         w = new_wim_struct();
534         if (!w)
535                 return WIMLIB_ERR_NOMEM;
536
537         ret = wim_begin_read(w, wim_file, flags);
538         if (ret != 0) {
539                 ERROR("Could not begin reading the WIM file `%s'\n", wim_file);
540                 FREE(w);
541                 return ret;
542         }
543         *w_ret = w;
544         return 0;
545 }
546
547 /**
548  * Frees internal memory allocated by WIMLIB for a WIM file, and closes it if it
549  * is still open.
550  */
551 void wimlib_destroy(WIMStruct *w)
552 {
553
554 }
555
556 /* Frees the memory for the WIMStruct, including all internal memory; also
557  * closes all files associated with the WIMStruct.  */
558 WIMLIBAPI void wimlib_free(WIMStruct *w)
559 {
560         uint i;
561
562         if (!w)
563                 return;
564         if (w->fp)
565                 fclose(w->fp);
566         if (w->out_fp)
567                 fclose(w->out_fp);
568
569         free_lookup_table(w->lookup_table);
570
571         FREE(w->filename);
572         FREE(w->output_dir);
573         FREE(w->xml_data);
574         free_wim_info(w->wim_info);
575         if (w->image_metadata) {
576                 for (i = 0; i < w->hdr.image_count; i++)
577                         free_dentry_tree(w->image_metadata[i].root_dentry, 
578                                          NULL, false);
579                 FREE(w->image_metadata);
580         }
581         FREE(w);
582 }
583