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