Initial commit (current version is wimlib 0.6.2)
[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                 ERROR("WARNING:  In `%s', image %u is marked as bootable,\n",
413                                 in_wim_path, w->hdr.boot_idx);
414                 ERROR("    but there are only %u images!\n", 
415                                 w->hdr.image_count);
416                 w->hdr.boot_idx = 0;
417         }
418
419         if (wimlib_get_compression_type(w) == WIM_COMPRESSION_TYPE_INVALID) {
420                 ERROR("Invalid compression type (WIM header flags "
421                                 "= %x)\n", w->hdr.flags);
422                 ret = WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
423                 goto done;
424         }
425
426
427         if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
428                 ret = check_wim_integrity(w, 
429                                           flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS, 
430                                           &integrity_status);
431                 if (ret != 0) {
432                         ERROR("Error in check_wim_integrity()\n");
433                         goto done;
434                 }
435                 if (integrity_status == WIM_INTEGRITY_NONEXISTENT) {
436                         DEBUG("WARNING: No integrity information; skipping "
437                                         "integrity check.\n");
438                 } else if (integrity_status == WIM_INTEGRITY_NOT_OK) {
439                         ERROR("WIM is not intact! (Failed integrity check)\n");
440                         ret = WIMLIB_ERR_INTEGRITY;
441                         goto done;
442                 }
443         }
444
445         if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
446                 ERROR("Didn't expect a compressed lookup table!\n");
447                 ERROR("Ask the author to implement support for this.\n");
448                 ret = WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
449                 goto done;
450         }
451
452         ret = read_lookup_table(w->fp, w->hdr.lookup_table_res_entry.offset,
453                                 w->hdr.lookup_table_res_entry.size, 
454                                 &w->lookup_table);
455         
456         if (ret != 0)
457                 goto done;
458
459         w->image_metadata = CALLOC(w->hdr.image_count, 
460                                    sizeof(struct image_metadata));
461
462         if (!w->image_metadata) {
463                 ERROR("Failed to allocate memory for %u metadata structures\n",
464                                 w->hdr.image_count);
465                 goto done;
466         }
467         w->current_image = 0;
468
469         DEBUG("Looking for metadata resources in the lookup table.\n");
470
471         /* Find the images in the WIM by searching the lookup table. */
472         ret = for_lookup_table_entry(w->lookup_table, 
473                                      append_metadata_resource_entry, w);
474
475         if (ret != 0)
476                 goto done;
477
478         /* Make sure all the expected images were found.  (We already have
479          * returned false if *extra* images were found) */
480         if (w->current_image != w->hdr.image_count) {
481                 ERROR("Only found %u images in WIM, but expected %u!\n",
482                                 w->current_image, w->hdr.image_count);
483                 ret = WIMLIB_ERR_IMAGE_COUNT;
484                 goto done;
485         }
486
487         w->current_image = WIM_NO_IMAGE;
488
489         /* Sort images by the position of their metadata resources.  I'm
490          * assuming that is what determines the other of the images in the WIM
491          * file, rather than their order in the lookup table, which may be
492          * random because of hashing. */
493         qsort(w->image_metadata, w->hdr.image_count, 
494               sizeof(struct image_metadata), sort_image_metadata_by_position);
495
496         /* Read the XML data. */
497         ret = read_xml_data(w->fp, &w->hdr.xml_res_entry, 
498                             &w->xml_data, &w->wim_info);
499
500         if (ret != 0) {
501                 ERROR("Missing or invalid XML data\n");
502                 goto done;
503         }
504
505         xml_num_images = wim_info_get_num_images(w->wim_info);
506         if (xml_num_images != w->hdr.image_count) {
507                 ERROR("In the file `%s', there are %u <IMAGE> elements "
508                                 "in the XML data,\n", in_wim_path, 
509                                                         xml_num_images);
510                 ERROR("but %u images in the WIM!  There must be "
511                                 "exactly one <IMAGE> element per image.\n",
512                                 w->hdr.image_count);
513                 ret = WIMLIB_ERR_IMAGE_COUNT;
514                 goto done;
515         }
516
517         DEBUG("Done beginning read of WIM file.\n");
518         ret = 0;
519 done:
520         return ret;
521 }
522
523
524 /*
525  * Opens a WIM file and creates a WIMStruct for it.
526  */
527 WIMLIBAPI int wimlib_open_wim(const char *wim_file, int flags, 
528                               WIMStruct **w_ret)
529 {
530         WIMStruct *w;
531         int ret;
532
533         DEBUG("wim_file = `%s', flags = %d\n", wim_file, flags);
534         w = new_wim_struct();
535         if (!w)
536                 return WIMLIB_ERR_NOMEM;
537
538         ret = wim_begin_read(w, wim_file, flags);
539         if (ret != 0) {
540                 ERROR("Could not begin reading the WIM file `%s'\n", wim_file);
541                 FREE(w);
542                 return ret;
543         }
544         *w_ret = w;
545         return 0;
546 }
547
548 /**
549  * Frees internal memory allocated by WIMLIB for a WIM file, and closes it if it
550  * is still open.
551  */
552 void wimlib_destroy(WIMStruct *w)
553 {
554
555 }
556
557 /* Frees the memory for the WIMStruct, including all internal memory; also
558  * closes all files associated with the WIMStruct.  */
559 WIMLIBAPI void wimlib_free(WIMStruct *w)
560 {
561         uint i;
562
563         if (!w)
564                 return;
565         if (w->fp)
566                 fclose(w->fp);
567         if (w->out_fp)
568                 fclose(w->out_fp);
569
570         free_lookup_table(w->lookup_table);
571
572         FREE(w->filename);
573         FREE(w->output_dir);
574         FREE(w->xml_data);
575         free_wim_info(w->wim_info);
576         if (w->image_metadata) {
577                 for (i = 0; i < w->hdr.image_count; i++)
578                         free_dentry_tree(w->image_metadata[i].root_dentry, 
579                                          NULL, false);
580                 FREE(w->image_metadata);
581         }
582         FREE(w);
583 }
584