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