Rewrite integrity calculation and checks
[wimlib] / src / wim.c
1 /*
2  * wim.c
3  */
4
5 /*
6  * Copyright (C) 2010 Carl Thijssen
7  * Copyright (C) 2012 Eric Biggers
8  *
9  * wimlib - Library for working with WIM files
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
13  * wimlib is free software; you can redistribute it and/or modify it under the
14  * terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 3 of the License, or (at your option)
16  * any later version.
17  *
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 General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with wimlib; if not, see http://www.gnu.org/licenses/.
25  */
26
27 #include "config.h"
28 #include <limits.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31
32 #include "dentry.h"
33 #include <unistd.h>
34 #include <fcntl.h>
35
36 #ifdef WITH_NTFS_3G
37 #include <time.h>
38 #include <ntfs-3g/volume.h>
39 #endif
40
41 #include "wimlib_internal.h"
42 #include "io.h"
43 #include "lookup_table.h"
44 #include "xml.h"
45
46
47 static int print_metadata(WIMStruct *w)
48 {
49         DEBUG("Printing metadata for image %d", w->current_image);
50         print_security_data(wim_security_data(w));
51         return for_dentry_in_tree(wim_root_dentry(w), print_dentry,
52                                   w->lookup_table);
53 }
54
55
56 static int print_files(WIMStruct *w)
57 {
58         return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
59                                   NULL);
60 }
61
62 WIMStruct *new_wim_struct()
63 {
64         WIMStruct *w = CALLOC(1, sizeof(WIMStruct));
65 #ifdef WITH_FUSE
66         if (pthread_mutex_init(&w->fp_tab_mutex, NULL) != 0) {
67                 ERROR_WITH_ERRNO("Failed to initialize mutex");
68                 FREE(w);
69                 w = NULL;
70         }
71 #endif
72         return w;
73
74 }
75
76 /*
77  * Calls a function on images in the WIM.  If @image is WIM_ALL_IMAGES, @visitor
78  * is called on the WIM once for each image, with each image selected as the
79  * current image in turn.  If @image is a certain image, @visitor is called on
80  * the WIM only once, with that image selected.
81  */
82 int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
83 {
84         int ret;
85         int start;
86         int end;
87         int i;
88
89         if (image == WIM_ALL_IMAGES) {
90                 start = 1;
91                 end = w->hdr.image_count;
92         } else if (image >= 1 && image <= w->hdr.image_count) {
93                 start = image;
94                 end = image;
95         } else {
96                 return WIMLIB_ERR_INVALID_IMAGE;
97         }
98         for (i = start; i <= end; i++) {
99                 ret = select_wim_image(w, i);
100                 if (ret != 0)
101                         return ret;
102                 ret = visitor(w);
103                 if (ret != 0)
104                         return ret;
105         }
106         return 0;
107 }
108
109 static int sort_image_metadata_by_position(const void *p1, const void *p2)
110 {
111         const struct image_metadata *imd1 = p1;
112         const struct image_metadata *imd2 = p2;
113         u64 offset1 = imd1->metadata_lte->resource_entry.offset;
114         u64 offset2 = imd2->metadata_lte->resource_entry.offset;
115         if (offset1 < offset2)
116                 return -1;
117         else if (offset1 > offset2)
118                 return 1;
119         else
120                 return 0;
121 }
122
123 /*
124  * If @lte points to a metadata resource, append it to the list of metadata
125  * resources in the WIMStruct.  Otherwise, do nothing.
126  */
127 static int append_metadata_resource_entry(struct lookup_table_entry *lte,
128                                           void *wim_p)
129 {
130         WIMStruct *w = wim_p;
131         int ret = 0;
132
133         if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
134                 if (w->current_image == w->hdr.image_count) {
135                         ERROR("Expected only %u images, but found more",
136                               w->hdr.image_count);
137                         ret = WIMLIB_ERR_IMAGE_COUNT;
138                 } else {
139                         DEBUG("Found metadata resource for image %u at "
140                               "offset %"PRIu64".",
141                               w->current_image + 1,
142                               lte->resource_entry.offset);
143                         w->image_metadata[
144                                 w->current_image++].metadata_lte = lte;
145                 }
146         }
147         return ret;
148 }
149
150 /* Returns the compression type given in the flags of a WIM header. */
151 int wim_hdr_flags_compression_type(int wim_hdr_flags)
152 {
153         if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESSION) {
154                 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_LZX)
155                         return WIM_COMPRESSION_TYPE_LZX;
156                 else if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_XPRESS)
157                         return WIM_COMPRESSION_TYPE_XPRESS;
158                 else
159                         return WIM_COMPRESSION_TYPE_INVALID;
160         } else {
161                 return WIM_COMPRESSION_TYPE_NONE;
162         }
163 }
164
165 /*
166  * Creates a WIMStruct for a new WIM file.
167  */
168 WIMLIBAPI int wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
169 {
170         WIMStruct *w;
171         struct lookup_table *table;
172         int ret;
173
174         DEBUG("Creating new WIM with %s compression.",
175               wimlib_get_compression_type_string(ctype));
176
177         /* Allocate the WIMStruct. */
178         w = new_wim_struct();
179         if (!w)
180                 return WIMLIB_ERR_NOMEM;
181
182         ret = init_header(&w->hdr, ctype);
183         if (ret != 0)
184                 goto out_free;
185
186         table = new_lookup_table(9001);
187         if (!table) {
188                 ret = WIMLIB_ERR_NOMEM;
189                 goto out_free;
190         }
191         w->lookup_table = table;
192         *w_ret = w;
193         return 0;
194 out_free:
195         FREE(w);
196         return ret;
197 }
198
199 WIMLIBAPI int wimlib_get_num_images(const WIMStruct *w)
200 {
201         return w->hdr.image_count;
202 }
203
204 int select_wim_image(WIMStruct *w, int image)
205 {
206         struct image_metadata *imd;
207
208         DEBUG("Selecting image %d", image);
209
210         if (image == w->current_image)
211                 return 0;
212
213         if (image < 1 || image > w->hdr.image_count) {
214                 ERROR("Cannot select image %d: There are only %u images",
215                       image, w->hdr.image_count);
216                 return WIMLIB_ERR_INVALID_IMAGE;
217         }
218
219         /* If a valid image is currently selected, it can be freed if it is not
220          * modified.  */
221         if (w->current_image != WIM_NO_IMAGE) {
222                 imd = wim_get_current_image_metadata(w);
223                 if (!imd->modified) {
224                         DEBUG("Freeing image %u", w->current_image);
225                         destroy_image_metadata(imd, NULL);
226                         imd->root_dentry = NULL;
227                         imd->security_data = NULL;
228                 }
229         }
230
231         w->current_image = image;
232         imd = wim_get_current_image_metadata(w);
233
234         if (imd->root_dentry) {
235                 return 0;
236         } else {
237                 #ifdef ENABLE_DEBUG
238                 DEBUG("Reading metadata resource specified by the following "
239                       "lookup table entry:");
240                 print_lookup_table_entry(imd->metadata_lte);
241                 #endif
242                 return read_metadata_resource(w, imd);
243         }
244 }
245
246
247 /* Returns the compression type of the WIM file. */
248 WIMLIBAPI int wimlib_get_compression_type(const WIMStruct *w)
249 {
250         return wim_hdr_flags_compression_type(w->hdr.flags);
251 }
252
253 WIMLIBAPI const char *wimlib_get_compression_type_string(int ctype)
254 {
255         switch (ctype) {
256                 case WIM_COMPRESSION_TYPE_NONE:
257                         return "None";
258                 case WIM_COMPRESSION_TYPE_LZX:
259                         return "LZX";
260                 case WIM_COMPRESSION_TYPE_XPRESS:
261                         return "XPRESS";
262                 default:
263                         return "Invalid";
264         }
265 }
266
267 /*
268  * Returns the number of an image in the WIM file, given a string that is either
269  * the number of the image, or the name of the image.  The images are numbered
270  * starting at 1.
271  */
272 WIMLIBAPI int wimlib_resolve_image(WIMStruct *w, const char *image_name_or_num)
273 {
274         char *p;
275         int image;
276         int i;
277
278         if (!image_name_or_num)
279                 return WIM_NO_IMAGE;
280
281         if (strcmp(image_name_or_num, "all") == 0
282             || strcmp(image_name_or_num, "*") == 0)
283                 return WIM_ALL_IMAGES;
284         image = strtol(image_name_or_num, &p, 10);
285         if (p != image_name_or_num && *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 WIMLIBAPI void wimlib_print_available_images(const WIMStruct *w, int image)
329 {
330         int first;
331         int last;
332         int i;
333         int n;
334         if (image == WIM_ALL_IMAGES) {
335                 n = printf("Available Images:\n");
336                 first = 1;
337                 last = w->hdr.image_count;
338         } else if (image >= 1 && image <= w->hdr.image_count) {
339                 n = printf("Information for Image %d\n", image);
340                 first = image;
341                 last = image;
342         } else {
343                 printf("wimlib_print_available_images(): Invalid image %d",
344                        image);
345                 return;
346         }
347         for (i = 0; i < n - 1; i++)
348                 putchar('-');
349         putchar('\n');
350         for (i = first; i <= last; i++)
351                 print_image_info(w->wim_info, i);
352 }
353
354
355 /* Prints the metadata for the specified image, which may be WIM_ALL_IMAGES, but
356  * not WIM_NO_IMAGE. */
357 WIMLIBAPI int wimlib_print_metadata(WIMStruct *w, int image)
358 {
359         if (!w)
360                 return WIMLIB_ERR_INVALID_PARAM;
361         if (w->hdr.part_number != 1) {
362                 ERROR("Cannot show the metadata from part %hu of a %hu-part split WIM!",
363                        w->hdr.part_number, w->hdr.total_parts);
364                 ERROR("Select the first part of the split WIM to see the metadata.");
365                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
366         }
367         return for_image(w, image, print_metadata);
368 }
369
370 WIMLIBAPI int wimlib_print_files(WIMStruct *w, int image)
371 {
372         if (!w)
373                 return WIMLIB_ERR_INVALID_PARAM;
374         if (w->hdr.part_number != 1) {
375                 ERROR("Cannot list the files from part %hu of a %hu-part split WIM!",
376                        w->hdr.part_number, w->hdr.total_parts);
377                 ERROR("Select the first part of the split WIM if you'd like to list the files.");
378                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
379         }
380         return for_image(w, image, print_files);
381 }
382
383 /* Sets the index of the bootable image. */
384 WIMLIBAPI int wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
385 {
386         if (!w)
387                 return WIMLIB_ERR_INVALID_PARAM;
388         if (w->hdr.total_parts != 1) {
389                 ERROR("Cannot modify the boot index of a split WIM!");
390                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
391         }
392         if (boot_idx < 0 || boot_idx > w->hdr.image_count)
393                 return WIMLIB_ERR_INVALID_IMAGE;
394         w->hdr.boot_idx = boot_idx;
395
396         if (boot_idx == 0) {
397                 memset(&w->hdr.boot_metadata_res_entry, 0,
398                        sizeof(struct resource_entry));
399         } else {
400                 memcpy(&w->hdr.boot_metadata_res_entry,
401                        &w->image_metadata[
402                           boot_idx - 1].metadata_lte->resource_entry,
403                        sizeof(struct resource_entry));
404         }
405
406         return 0;
407 }
408
409 WIMLIBAPI int wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
410 {
411         if (total_parts_ret)
412                 *total_parts_ret = w->hdr.total_parts;
413         return w->hdr.part_number;
414 }
415
416
417 WIMLIBAPI int wimlib_get_boot_idx(const WIMStruct *w)
418 {
419         return w->hdr.boot_idx;
420 }
421
422 /* Opens a WIM readable */
423 int open_wim_readable(WIMStruct *w, const char *path)
424 {
425         if (w->fp != NULL)
426                 fclose(w->fp);
427         wimlib_assert(path != NULL);
428         w->fp = fopen(path, "rb");
429         if (!w->fp) {
430                 ERROR_WITH_ERRNO("Failed to open `%s' for reading",
431                                  path);
432                 return WIMLIB_ERR_OPEN;
433         }
434         return 0;
435 }
436
437 /* Opens a WIM writable */
438 int open_wim_writable(WIMStruct *w, const char *path,
439                       bool trunc, bool readable)
440 {
441         const char *mode;
442         if (trunc)
443                 if (readable)
444                         mode = "w+b";
445                 else
446                         mode = "wb";
447         else
448                 mode = "r+b";
449
450         DEBUG("Opening `%s' read-write", path);
451         wimlib_assert(w->out_fp == NULL);
452         wimlib_assert(path != NULL);
453         w->out_fp = fopen(path, mode);
454         if (!w->out_fp) {
455                 ERROR_WITH_ERRNO("Failed to open `%s' for writing", path);
456                 return WIMLIB_ERR_OPEN;
457         }
458         return 0;
459 }
460
461 /*
462  * Begins the reading of a WIM file; opens the file and reads its header and
463  * lookup table, and optionally checks the integrity.
464  */
465 static int begin_read(WIMStruct *w, const char *in_wim_path, int open_flags)
466 {
467         int ret;
468         uint xml_num_images;
469
470         DEBUG("Reading the WIM file `%s'", in_wim_path);
471
472         ret = open_wim_readable(w, in_wim_path);
473         if (ret != 0)
474                 goto out;
475
476         w->filename = realpath(in_wim_path, NULL);
477         if (!w->filename) {
478                 ERROR_WITH_ERRNO("Failed to resolve WIM filename");
479                 ret = WIMLIB_ERR_NOMEM;
480                 goto out_close;
481         }
482
483         ret = read_header(w->fp, &w->hdr, open_flags);
484         if (ret != 0)
485                 goto out_close;
486
487         DEBUG("Wim file contains %u images", w->hdr.image_count);
488
489         /* If the boot index is invalid, print a warning and set it to 0 */
490         if (w->hdr.boot_idx > w->hdr.image_count) {
491                 WARNING("In `%s', image %u is marked as bootable, "
492                         "but there are only %u images in the WIM",
493                         in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
494                 w->hdr.boot_idx = 0;
495         }
496
497         if (wimlib_get_compression_type(w) == WIM_COMPRESSION_TYPE_INVALID) {
498                 ERROR("Invalid compression type (WIM header flags = %x)",
499                       w->hdr.flags);
500                 ret = WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
501                 goto out_close;
502         }
503
504         if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
505                 ret = check_wim_integrity(w,
506                                           (open_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS) != 0);
507                 if (ret == WIM_INTEGRITY_NONEXISTENT) {
508                         WARNING("No integrity information for `%s'; skipping "
509                                 "integrity check.", w->filename);
510                 } else if (ret == WIM_INTEGRITY_NOT_OK) {
511                         ERROR("WIM is not intact! (Failed integrity check)");
512                         ret = WIMLIB_ERR_INTEGRITY;
513                         goto out_close;
514                 } else if (ret != 0) {
515                         goto out_close;
516                 }
517         }
518
519         if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
520                 ERROR("Didn't expect a compressed lookup table!");
521                 ERROR("Ask the author to implement support for this.");
522                 ret = WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
523                 goto out_close;
524         }
525
526         ret = read_lookup_table(w);
527         if (ret != 0)
528                 goto out_close;
529
530         w->image_metadata = CALLOC(w->hdr.image_count,
531                                    sizeof(struct image_metadata));
532
533         if (!w->image_metadata) {
534                 ERROR("Failed to allocate memory for %u metadata structures",
535                       w->hdr.image_count);
536                 ret = WIMLIB_ERR_NOMEM;
537                 goto out_free_lookup_table;
538         }
539         w->current_image = 0;
540
541         DEBUG("Looking for metadata resources in the lookup table.");
542
543         /* Find the images in the WIM by searching the lookup table. */
544         ret = for_lookup_table_entry(w->lookup_table,
545                                      append_metadata_resource_entry, w);
546
547         if (ret != 0)
548                 goto out_free_image_metadata;
549
550         /* Make sure all the expected images were found.  (We already have
551          * returned false if *extra* images were found) */
552         if (w->current_image != w->hdr.image_count &&
553             w->hdr.part_number == 1)
554         {
555                 ERROR("Only found %u images in WIM, but expected %u",
556                       w->current_image, w->hdr.image_count);
557                 ret = WIMLIB_ERR_IMAGE_COUNT;
558                 goto out_free_image_metadata;
559         }
560
561         /* Sort images by the position of their metadata resources.  I'm
562          * assuming that is what determines the other of the images in the WIM
563          * file, rather than their order in the lookup table, which is random
564          * because of hashing. */
565         qsort(w->image_metadata, w->current_image,
566               sizeof(struct image_metadata), sort_image_metadata_by_position);
567
568         w->current_image = WIM_NO_IMAGE;
569
570         /* Read the XML data. */
571         ret = read_xml_data(w->fp, &w->hdr.xml_res_entry,
572                             &w->xml_data, &w->wim_info);
573
574         if (ret != 0)
575                 goto out_free_image_metadata;
576
577         xml_num_images = wim_info_get_num_images(w->wim_info);
578         if (xml_num_images != w->hdr.image_count) {
579                 ERROR("In the file `%s', there are %u <IMAGE> elements "
580                       "in the XML data,", in_wim_path, xml_num_images);
581                 ERROR("but %u images in the WIM!  There must be exactly one "
582                       "<IMAGE> element per image.", w->hdr.image_count);
583                 ret = WIMLIB_ERR_IMAGE_COUNT;
584                 goto out_free_xml_data;
585         }
586
587         DEBUG("Done beginning read of WIM file `%s'.", in_wim_path);
588         return 0;
589
590         //
591         // Everything is freed in wimlib_free() anyway, so no need to roll back
592         // changes here.
593         //
594 out_free_xml_data:
595         /*FREE(w->xml_data);*/
596         /*w->xml_data = NULL;*/
597         /*free_wim_info(w->wim_info);*/
598         /*w->wim_info = NULL;*/
599 out_free_image_metadata:
600         /*FREE(w->image_metadata);*/
601         /*w->image_metadata = NULL;*/
602         /*w->current_image = WIM_NO_IMAGE;*/
603 out_free_lookup_table:
604         /*free_lookup_table(w->lookup_table);*/
605         /*w->lookup_table = NULL;*/
606 out_close:
607         /*fclose(w->fp);*/
608         /*w->fp = NULL;*/
609 out:
610         return ret;
611 }
612
613
614 /*
615  * Opens a WIM file and creates a WIMStruct for it.
616  */
617 WIMLIBAPI int wimlib_open_wim(const char *wim_file, int open_flags,
618                               WIMStruct **w_ret)
619 {
620         WIMStruct *w;
621         int ret;
622
623         DEBUG("wim_file = `%s', open_flags = %#x", wim_file, open_flags);
624         w = new_wim_struct();
625         if (!w) {
626                 ERROR("Failed to allocate memory for WIMStruct");
627                 return WIMLIB_ERR_NOMEM;
628         }
629
630         ret = begin_read(w, wim_file, open_flags);
631         if (ret == 0) {
632                 *w_ret = w;
633         } else {
634                 DEBUG("Could not begin reading the WIM file `%s'", wim_file);
635                 wimlib_free(w);
636         }
637         return ret;
638 }
639
640 /* Frees the memory for the WIMStruct, including all internal memory; also
641  * closes all files associated with the WIMStruct.  */
642 WIMLIBAPI void wimlib_free(WIMStruct *w)
643 {
644         DEBUG2("Freeing WIMStruct");
645
646         if (!w)
647                 return;
648         if (w->fp)
649                 fclose(w->fp);
650         if (w->out_fp)
651                 fclose(w->out_fp);
652
653 #ifdef WITH_FUSE
654         if (w->fp_tab) {
655                 for (size_t i = 0; i < w->num_allocated_fps; i++)
656                         if (w->fp_tab[i])
657                                 fclose(w->fp_tab[i]);
658                 FREE(w->fp_tab);
659         }
660         pthread_mutex_destroy(&w->fp_tab_mutex);
661 #endif
662
663         free_lookup_table(w->lookup_table);
664
665         FREE(w->filename);
666         FREE(w->xml_data);
667         free_wim_info(w->wim_info);
668         if (w->image_metadata) {
669                 for (uint i = 0; i < w->hdr.image_count; i++)
670                         destroy_image_metadata(&w->image_metadata[i], NULL);
671                 FREE(w->image_metadata);
672         }
673 #ifdef WITH_NTFS_3G
674         if (w->ntfs_vol) {
675                 DEBUG("Unmounting NTFS volume");
676                 ntfs_umount(w->ntfs_vol, FALSE);
677         }
678 #endif
679         FREE(w);
680 }