667d076b6859ecca519828f36ef79aa8e8b5dc83
[wimlib] / src / wim.c
1 /*
2  * wim.c - Stuff that doesn't fit into any other file
3  */
4
5 /*
6  * Copyright (C) 2012, 2013 Eric Biggers
7  *
8  * wimlib - Library for working with WIM files
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
12  * wimlib is free software; you can redistribute it and/or modify it under the
13  * terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 3 of the License, or (at your option)
15  * any later version.
16  *
17  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19  * A PARTICULAR PURPOSE. See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with wimlib; if not, see http://www.gnu.org/licenses/.
24  */
25
26 #include "config.h"
27
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <limits.h>
31 #include <stdarg.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34
35 #ifdef WITH_NTFS_3G
36 #  include <time.h>
37 #  include <ntfs-3g/volume.h>
38 #endif
39
40 #ifdef __WIN32__
41 #  include "win32.h"
42 #else
43 #  include <langinfo.h>
44 #endif
45
46 #include "buffer_io.h"
47 #include "dentry.h"
48 #include "lookup_table.h"
49 #include "wimlib_internal.h"
50 #include "xml.h"
51
52 static int
53 image_print_metadata(WIMStruct *w)
54 {
55         DEBUG("Printing metadata for image %d", w->current_image);
56         print_security_data(wim_security_data(w));
57         return for_dentry_in_tree(wim_root_dentry(w), print_dentry,
58                                   w->lookup_table);
59 }
60
61
62 static int
63 image_print_files(WIMStruct *w)
64 {
65         return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
66                                   NULL);
67 }
68
69 static WIMStruct *
70 new_wim_struct()
71 {
72         WIMStruct *w = CALLOC(1, sizeof(WIMStruct));
73 #ifdef WITH_FUSE
74         if (pthread_mutex_init(&w->fp_tab_mutex, NULL) != 0) {
75                 ERROR_WITH_ERRNO("Failed to initialize mutex");
76                 FREE(w);
77                 w = NULL;
78         }
79 #endif
80         return w;
81
82 }
83
84 /*
85  * Calls a function on images in the WIM.  If @image is WIMLIB_ALL_IMAGES, @visitor
86  * is called on the WIM once for each image, with each image selected as the
87  * current image in turn.  If @image is a certain image, @visitor is called on
88  * the WIM only once, with that image selected.
89  */
90 int
91 for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
92 {
93         int ret;
94         int start;
95         int end;
96         int i;
97
98         if (image == WIMLIB_ALL_IMAGES) {
99                 start = 1;
100                 end = w->hdr.image_count;
101         } else if (image >= 1 && image <= w->hdr.image_count) {
102                 start = image;
103                 end = image;
104         } else {
105                 return WIMLIB_ERR_INVALID_IMAGE;
106         }
107         for (i = start; i <= end; i++) {
108                 ret = select_wim_image(w, i);
109                 if (ret != 0)
110                         return ret;
111                 ret = visitor(w);
112                 if (ret != 0)
113                         return ret;
114         }
115         return 0;
116 }
117
118 static int
119 sort_image_metadata_by_position(const void *p1, const void *p2)
120 {
121         const struct wim_image_metadata *imd1 = p1;
122         const struct wim_image_metadata *imd2 = p2;
123         u64 offset1 = imd1->metadata_lte->resource_entry.offset;
124         u64 offset2 = imd2->metadata_lte->resource_entry.offset;
125         if (offset1 < offset2)
126                 return -1;
127         else if (offset1 > offset2)
128                 return 1;
129         else
130                 return 0;
131 }
132
133 /*
134  * If @lte points to a metadata resource, append it to the list of metadata
135  * resources in the WIMStruct.  Otherwise, do nothing.
136  */
137 static int
138 append_metadata_resource_entry(struct wim_lookup_table_entry *lte, void *wim_p)
139 {
140         WIMStruct *w = wim_p;
141         int ret = 0;
142
143         if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
144                 if (w->current_image == w->hdr.image_count) {
145                         ERROR("The WIM header says there are %u images in the WIM,\n"
146                               "        but we found more metadata resources than this",
147                               w->hdr.image_count);
148                         ret = WIMLIB_ERR_IMAGE_COUNT;
149                 } else {
150                         DEBUG("Found metadata resource for image %u at "
151                               "offset %"PRIu64".",
152                               w->current_image + 1,
153                               lte->resource_entry.offset);
154                         w->image_metadata[
155                                 w->current_image++].metadata_lte = lte;
156                 }
157         }
158         return ret;
159 }
160
161 /* Returns the compression type given in the flags of a WIM header. */
162 static int
163 wim_hdr_flags_compression_type(int wim_hdr_flags)
164 {
165         if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESSION) {
166                 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_LZX)
167                         return WIMLIB_COMPRESSION_TYPE_LZX;
168                 else if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_XPRESS)
169                         return WIMLIB_COMPRESSION_TYPE_XPRESS;
170                 else
171                         return WIMLIB_COMPRESSION_TYPE_INVALID;
172         } else {
173                 return WIMLIB_COMPRESSION_TYPE_NONE;
174         }
175 }
176
177 /*
178  * Creates a WIMStruct for a new WIM file.
179  */
180 WIMLIBAPI int
181 wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
182 {
183         WIMStruct *w;
184         struct wim_lookup_table *table;
185         int ret;
186
187         DEBUG("Creating new WIM with %s compression.",
188               wimlib_get_compression_type_string(ctype));
189
190         /* Allocate the WIMStruct. */
191         w = new_wim_struct();
192         if (!w)
193                 return WIMLIB_ERR_NOMEM;
194
195         ret = init_header(&w->hdr, ctype);
196         if (ret != 0)
197                 goto out_free;
198
199         table = new_lookup_table(9001);
200         if (!table) {
201                 ret = WIMLIB_ERR_NOMEM;
202                 goto out_free;
203         }
204         w->lookup_table = table;
205         *w_ret = w;
206         return 0;
207 out_free:
208         FREE(w);
209         return ret;
210 }
211
212 WIMLIBAPI int
213 wimlib_get_num_images(const WIMStruct *w)
214 {
215         return w->hdr.image_count;
216 }
217
218 int
219 select_wim_image(WIMStruct *w, int image)
220 {
221         struct wim_image_metadata *imd;
222         int ret;
223
224         DEBUG("Selecting image %d", image);
225
226         if (image == WIMLIB_NO_IMAGE) {
227                 ERROR("Invalid image: %d", WIMLIB_NO_IMAGE);
228                 return WIMLIB_ERR_INVALID_IMAGE;
229         }
230
231         if (image == w->current_image)
232                 return 0;
233
234         if (image < 1 || image > w->hdr.image_count) {
235                 ERROR("Cannot select image %d: There are only %u images",
236                       image, w->hdr.image_count);
237                 return WIMLIB_ERR_INVALID_IMAGE;
238         }
239
240         /* If a valid image is currently selected, it can be freed if it is not
241          * modified.  */
242         if (w->current_image != WIMLIB_NO_IMAGE) {
243                 imd = wim_get_current_image_metadata(w);
244                 if (!imd->modified) {
245                         DEBUG("Freeing image %u", w->current_image);
246                         destroy_image_metadata(imd, NULL);
247                         imd->root_dentry = NULL;
248                         imd->security_data = NULL;
249                         INIT_HLIST_HEAD(&imd->inode_list);
250                 }
251         }
252         w->current_image = image;
253         imd = &w->image_metadata[image - 1];
254         if (imd->root_dentry) {
255                 ret = 0;
256         } else {
257                 #ifdef ENABLE_DEBUG
258                 DEBUG("Reading metadata resource specified by the following "
259                       "lookup table entry:");
260                 print_lookup_table_entry(imd->metadata_lte, stdout);
261                 #endif
262                 ret = read_metadata_resource(w, imd);
263                 if (ret)
264                         w->current_image = WIMLIB_NO_IMAGE;
265         }
266         return ret;
267 }
268
269
270 /* Returns the compression type of the WIM file. */
271 WIMLIBAPI int
272 wimlib_get_compression_type(const WIMStruct *w)
273 {
274         return wim_hdr_flags_compression_type(w->hdr.flags);
275 }
276
277 WIMLIBAPI const char *
278 wimlib_get_compression_type_string(int ctype)
279 {
280         switch (ctype) {
281                 case WIMLIB_COMPRESSION_TYPE_NONE:
282                         return "None";
283                 case WIMLIB_COMPRESSION_TYPE_LZX:
284                         return "LZX";
285                 case WIMLIB_COMPRESSION_TYPE_XPRESS:
286                         return "XPRESS";
287                 default:
288                         return "Invalid";
289         }
290 }
291
292 /*
293  * Returns the number of an image in the WIM file, given a string that is either
294  * the number of the image, or the name of the image.  The images are numbered
295  * starting at 1.
296  */
297 WIMLIBAPI int
298 wimlib_resolve_image(WIMStruct *w, const utf8char *image_name_or_num)
299 {
300         char *p;
301         int image;
302         int i;
303
304         if (!image_name_or_num || !*image_name_or_num)
305                 return WIMLIB_NO_IMAGE;
306
307         if (strcmp(image_name_or_num, "all") == 0
308             || strcmp(image_name_or_num, "*") == 0)
309                 return WIMLIB_ALL_IMAGES;
310         image = strtol(image_name_or_num, &p, 10);
311         if (p != image_name_or_num && *p == '\0' && image > 0) {
312                 if (image > w->hdr.image_count)
313                         return WIMLIB_NO_IMAGE;
314                 return image;
315         } else {
316                 for (i = 1; i <= w->hdr.image_count; i++) {
317                         if (strcmp(image_name_or_num,
318                                    wimlib_get_image_name(w, i)) == 0)
319                                 return i;
320                 }
321                 return WIMLIB_NO_IMAGE;
322         }
323 }
324
325
326 /* Prints some basic information about a WIM file. */
327 WIMLIBAPI void
328 wimlib_print_wim_information(const WIMStruct *w)
329 {
330         const struct wim_header *hdr;
331
332         hdr = &w->hdr;
333         puts("WIM Information:");
334         puts("----------------");
335         printf("Path:           %s\n", w->filename);
336         fputs ("GUID:           0x", stdout);
337         print_byte_field(hdr->guid, WIM_GID_LEN);
338         putchar('\n');
339         printf("Image Count:    %d\n", hdr->image_count);
340         printf("Compression:    %s\n", wimlib_get_compression_type_string(
341                                                 wimlib_get_compression_type(w)));
342         printf("Part Number:    %d/%d\n", hdr->part_number, hdr->total_parts);
343         printf("Boot Index:     %d\n", hdr->boot_idx);
344         printf("Size:           %"PRIu64" bytes\n",
345                                 wim_info_get_total_bytes(w->wim_info));
346         printf("Integrity Info: %s\n", (w->hdr.integrity.offset != 0) ? "yes" : "no");
347         putchar('\n');
348 }
349
350 WIMLIBAPI bool
351 wimlib_has_integrity_table(const WIMStruct *w)
352 {
353         return w->hdr.integrity.size != 0;
354 }
355
356 WIMLIBAPI void
357 wimlib_print_available_images(const WIMStruct *w, int image)
358 {
359         int first;
360         int last;
361         int i;
362         int n;
363         if (image == WIMLIB_ALL_IMAGES) {
364                 n = printf("Available Images:\n");
365                 first = 1;
366                 last = w->hdr.image_count;
367         } else if (image >= 1 && image <= w->hdr.image_count) {
368                 n = printf("Information for Image %d\n", image);
369                 first = image;
370                 last = image;
371         } else {
372                 printf("wimlib_print_available_images(): Invalid image %d",
373                        image);
374                 return;
375         }
376         for (i = 0; i < n - 1; i++)
377                 putchar('-');
378         putchar('\n');
379         for (i = first; i <= last; i++)
380                 print_image_info(w->wim_info, i);
381 }
382
383
384 /* Prints the metadata for the specified image, which may be WIMLIB_ALL_IMAGES, but
385  * not WIMLIB_NO_IMAGE. */
386 WIMLIBAPI int
387 wimlib_print_metadata(WIMStruct *w, int image)
388 {
389         if (w->hdr.part_number != 1) {
390                 ERROR("Cannot show the metadata from part %hu of a %hu-part split WIM!",
391                        w->hdr.part_number, w->hdr.total_parts);
392                 ERROR("Select the first part of the split WIM to see the metadata.");
393                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
394         }
395         return for_image(w, image, image_print_metadata);
396 }
397
398 WIMLIBAPI int
399 wimlib_print_files(WIMStruct *w, int image)
400 {
401         if (w->hdr.part_number != 1) {
402                 ERROR("Cannot list the files from part %hu of a %hu-part split WIM!",
403                        w->hdr.part_number, w->hdr.total_parts);
404                 ERROR("Select the first part of the split WIM if you'd like to list the files.");
405                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
406         }
407         return for_image(w, image, image_print_files);
408 }
409
410 /* Sets the index of the bootable image. */
411 WIMLIBAPI int
412 wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
413 {
414         if (w->hdr.total_parts != 1) {
415                 ERROR("Cannot modify the boot index of a split WIM!");
416                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
417         }
418         if (boot_idx < 0 || boot_idx > w->hdr.image_count)
419                 return WIMLIB_ERR_INVALID_IMAGE;
420         w->hdr.boot_idx = boot_idx;
421
422         if (boot_idx == 0) {
423                 memset(&w->hdr.boot_metadata_res_entry, 0,
424                        sizeof(struct resource_entry));
425         } else {
426                 memcpy(&w->hdr.boot_metadata_res_entry,
427                        &w->image_metadata[
428                           boot_idx - 1].metadata_lte->resource_entry,
429                        sizeof(struct resource_entry));
430         }
431
432         return 0;
433 }
434
435 WIMLIBAPI int
436 wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
437 {
438         if (total_parts_ret)
439                 *total_parts_ret = w->hdr.total_parts;
440         return w->hdr.part_number;
441 }
442
443
444 WIMLIBAPI int
445 wimlib_get_boot_idx(const WIMStruct *w)
446 {
447         return w->hdr.boot_idx;
448 }
449
450 /*
451  * Begins the reading of a WIM file; opens the file and reads its header and
452  * lookup table, and optionally checks the integrity.
453  */
454 static int
455 begin_read(WIMStruct *w, const mbchar *in_wim_path, int open_flags,
456            wimlib_progress_func_t progress_func)
457 {
458         int ret;
459         int xml_num_images;
460
461         DEBUG("Reading the WIM file `%s'", in_wim_path);
462
463         w->fp = fopen(in_wim_path, "rb");
464         if (!w->fp) {
465                 ERROR_WITH_ERRNO("Failed to open `%s' for reading",
466                                  in_wim_path);
467                 return WIMLIB_ERR_OPEN;
468         }
469
470         /* The absolute path to the WIM is requested so that wimlib_overwrite()
471          * still works even if the process changes its working directory.  This
472          * actually happens if a WIM is mounted read-write, since the FUSE
473          * thread changes directory to "/", and it needs to be able to find the
474          * WIM file again.
475          *
476          * This will break if the full path to the WIM changes in the
477          * intervening time...
478          *
479          * Warning: in Windows native builds, realpath() calls the replacement
480          * function in win32.c.
481          */
482         w->filename = realpath(in_wim_path, NULL);
483         if (!w->filename) {
484                 ERROR_WITH_ERRNO("Failed to resolve WIM filename");
485                 if (errno == ENOMEM)
486                         return WIMLIB_ERR_NOMEM;
487                 else
488                         return WIMLIB_ERR_OPEN;
489         }
490
491         ret = read_header(w->fp, &w->hdr, open_flags);
492         if (ret != 0)
493                 return ret;
494
495         DEBUG("According to header, WIM contains %u images", w->hdr.image_count);
496
497         /* If the boot index is invalid, print a warning and set it to 0 */
498         if (w->hdr.boot_idx > w->hdr.image_count) {
499                 WARNING("In `%s', image %u is marked as bootable, "
500                         "but there are only %u images in the WIM",
501                         in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
502                 w->hdr.boot_idx = 0;
503         }
504
505         if (wimlib_get_compression_type(w) == WIMLIB_COMPRESSION_TYPE_INVALID) {
506                 ERROR("Invalid compression type (WIM header flags = 0x%x)",
507                       w->hdr.flags);
508                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
509         }
510
511         if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
512                 ret = check_wim_integrity(w, progress_func);
513                 if (ret == WIM_INTEGRITY_NONEXISTENT) {
514                         WARNING("No integrity information for `%s'; skipping "
515                                 "integrity check.", in_wim_path);
516                 } else if (ret == WIM_INTEGRITY_NOT_OK) {
517                         ERROR("WIM is not intact! (Failed integrity check)");
518                         return WIMLIB_ERR_INTEGRITY;
519                 } else if (ret != WIM_INTEGRITY_OK) {
520                         return ret;
521                 }
522         }
523
524         ret = read_lookup_table(w);
525         if (ret != 0)
526                 return ret;
527
528         if (w->hdr.image_count != 0) {
529                 w->image_metadata = CALLOC(w->hdr.image_count,
530                                            sizeof(struct wim_image_metadata));
531
532                 if (!w->image_metadata) {
533                         ERROR("Failed to allocate memory for %u image metadata structures",
534                               w->hdr.image_count);
535                         return WIMLIB_ERR_NOMEM;
536                 }
537         }
538         w->current_image = 0;
539
540         DEBUG("Looking for metadata resources in the lookup table.");
541
542         /* Find the images in the WIM by searching the lookup table. */
543         ret = for_lookup_table_entry(w->lookup_table,
544                                      append_metadata_resource_entry, w);
545
546         if (ret != 0)
547                 return ret;
548
549         /* Make sure all the expected images were found.  (We already have
550          * returned WIMLIB_ERR_IMAGE_COUNT if *extra* images were found) */
551         if (w->current_image != w->hdr.image_count &&
552             w->hdr.part_number == 1)
553         {
554                 ERROR("Only found %d images in WIM, but expected %u",
555                       w->current_image, w->hdr.image_count);
556                 return WIMLIB_ERR_IMAGE_COUNT;
557         }
558
559         /* Sort images by the position of their metadata resources.  I'm
560          * assuming that is what determines the other of the images in the WIM
561          * file, rather than their order in the lookup table, which is random
562          * because of hashing. */
563         qsort(w->image_metadata, w->current_image,
564               sizeof(struct wim_image_metadata), sort_image_metadata_by_position);
565
566         w->current_image = WIMLIB_NO_IMAGE;
567
568         /* Read the XML data. */
569         ret = read_xml_data(w->fp, &w->hdr.xml_res_entry,
570                             &w->xml_data, &w->wim_info);
571
572         if (ret != 0)
573                 return ret;
574
575         xml_num_images = wim_info_get_num_images(w->wim_info);
576         if (xml_num_images != w->hdr.image_count) {
577                 ERROR("In the file `%s', there are %u <IMAGE> elements "
578                       "in the XML data,", in_wim_path, xml_num_images);
579                 ERROR("but %u images in the WIM!  There must be exactly one "
580                       "<IMAGE> element per image.", w->hdr.image_count);
581                 return WIMLIB_ERR_IMAGE_COUNT;
582         }
583
584         DEBUG("Done beginning read of WIM file `%s'.", in_wim_path);
585         return 0;
586 }
587
588 /*
589  * Opens a WIM file and creates a WIMStruct for it.
590  */
591 WIMLIBAPI int
592 wimlib_open_wim(const mbchar *wim_file, int open_flags,
593                 WIMStruct **w_ret,
594                 wimlib_progress_func_t progress_func)
595 {
596         WIMStruct *w;
597         int ret;
598
599         if (!wim_file || !w_ret)
600                 return WIMLIB_ERR_INVALID_PARAM;
601
602         w = new_wim_struct();
603         if (!w)
604                 return WIMLIB_ERR_NOMEM;
605
606         ret = begin_read(w, wim_file, open_flags, progress_func);
607         if (ret == 0)
608                 *w_ret = w;
609         else
610                 wimlib_free(w);
611         return ret;
612 }
613
614 void
615 destroy_image_metadata(struct wim_image_metadata *imd,
616                        struct wim_lookup_table *table)
617 {
618         free_dentry_tree(imd->root_dentry, table);
619         free_security_data(imd->security_data);
620
621         /* Get rid of the lookup table entry for this image's metadata resource
622          * */
623         if (table) {
624                 lookup_table_unlink(table, imd->metadata_lte);
625                 free_lookup_table_entry(imd->metadata_lte);
626         }
627 }
628
629 /* Frees the memory for the WIMStruct, including all internal memory; also
630  * closes all files associated with the WIMStruct.  */
631 WIMLIBAPI void
632 wimlib_free(WIMStruct *w)
633 {
634         DEBUG("Freeing WIMStruct");
635
636         if (!w)
637                 return;
638         if (w->fp)
639                 fclose(w->fp);
640         if (w->out_fp)
641                 fclose(w->out_fp);
642
643 #ifdef WITH_FUSE
644         if (w->fp_tab) {
645                 for (size_t i = 0; i < w->num_allocated_fps; i++)
646                         if (w->fp_tab[i])
647                                 fclose(w->fp_tab[i]);
648                 FREE(w->fp_tab);
649         }
650         pthread_mutex_destroy(&w->fp_tab_mutex);
651 #endif
652
653         free_lookup_table(w->lookup_table);
654
655         FREE(w->filename);
656         FREE(w->xml_data);
657         free_wim_info(w->wim_info);
658         if (w->image_metadata) {
659                 for (unsigned i = 0; i < w->hdr.image_count; i++)
660                         destroy_image_metadata(&w->image_metadata[i], NULL);
661                 FREE(w->image_metadata);
662         }
663 #ifdef WITH_NTFS_3G
664         if (w->ntfs_vol) {
665                 DEBUG("Unmounting NTFS volume");
666                 ntfs_umount(w->ntfs_vol, FALSE);
667         }
668 #endif
669         FREE(w);
670         DEBUG("Freed WIMStruct");
671 }
672
673 static bool
674 test_locale_ctype_utf8()
675 {
676         char *ctype = nl_langinfo(CODESET);
677
678         return (strstr(ctype, "UTF-8") == 0 ||
679                 strstr(ctype, "UTF8") == 0 ||
680                 strstr(ctype, "utf8") == 0 ||
681                 strstr(ctype, "utf-8") == 0);
682 }
683
684 /* Get global memory allocations out of the way.  Not strictly necessary in
685  * single-threaded programs like 'imagex'. */
686 WIMLIBAPI int
687 wimlib_global_init()
688 {
689         libxml_global_init();
690 #ifdef WITH_NTFS_3G
691         libntfs3g_global_init();
692 #endif
693         wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
694         return 0;
695 }
696
697 /* Free global memory allocations.  Not strictly necessary if the process using
698  * wimlib is just about to exit (as is the case for 'imagex'). */
699 WIMLIBAPI void
700 wimlib_global_cleanup()
701 {
702         libxml_global_cleanup();
703         iconv_global_cleanup();
704 }