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