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