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