97aad090d3d6420051dfc044265cb279c4f1360f
[wimlib] / src / wim.c
1 /*
2  * wim.c - High-level code dealing with WIMStructs and images.
3  */
4
5 /*
6  * Copyright (C) 2012, 2013, 2014 Eric Biggers
7  *
8  * This file is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the Free
10  * Software Foundation; either version 3 of the License, or (at your option) any
11  * later version.
12  *
13  * This file is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this file; if not, see http://www.gnu.org/licenses/.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #ifndef __WIN32__
29 #  include <langinfo.h>
30 #endif
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #include "wimlib.h"
35 #include "wimlib/assert.h"
36 #include "wimlib/blob_table.h"
37 #include "wimlib/bitops.h"
38 #include "wimlib/dentry.h"
39 #include "wimlib/encoding.h"
40 #include "wimlib/file_io.h"
41 #include "wimlib/integrity.h"
42 #include "wimlib/metadata.h"
43 #ifdef WITH_NTFS_3G
44 #  include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
45 #endif
46 #include "wimlib/security.h"
47 #include "wimlib/wim.h"
48 #include "wimlib/xml.h"
49 #ifdef __WIN32__
50 #  include "wimlib/win32.h" /* for realpath() replacement */
51 #endif
52
53 static int
54 wim_default_solid_compression_type(void)
55 {
56         return WIMLIB_COMPRESSION_TYPE_LZMS;
57 }
58
59 static u32
60 wim_default_solid_chunk_size(int ctype) {
61         switch (ctype) {
62         case WIMLIB_COMPRESSION_TYPE_LZMS:
63                 return (u32)1 << 26; /* 67108864  */
64         default:
65                 return (u32)1 << 15; /* 32768     */
66         }
67 }
68
69 static WIMStruct *
70 new_wim_struct(void)
71 {
72         WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
73         if (!wim)
74                 return NULL;
75
76         filedes_invalidate(&wim->in_fd);
77         filedes_invalidate(&wim->out_fd);
78         wim->out_solid_compression_type = wim_default_solid_compression_type();
79         wim->out_solid_chunk_size = wim_default_solid_chunk_size(
80                                         wim->out_solid_compression_type);
81         INIT_LIST_HEAD(&wim->subwims);
82         return wim;
83 }
84
85 /* Determine if the chunk size is valid for the specified compression type.  */
86 static bool
87 wim_chunk_size_valid(u32 chunk_size, int ctype)
88 {
89         u32 order;
90
91         /* Chunk size is meaningless for uncompressed WIMs --- any value is
92          * okay.  */
93         if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
94                 return true;
95
96         /* Chunk size must be power of 2.  */
97         if (chunk_size == 0)
98                 return false;
99         order = fls32(chunk_size);
100         if (chunk_size != 1U << order)
101                 return false;
102
103         /* Order        Size
104          * =====        ====
105          * 15           32768
106          * 16           65536
107          * 17           131072
108          * 18           262144
109          * 19           524288
110          * 20           1048576
111          * 21           2097152
112          * 22           4194304
113          * 23           8388608
114          * 24           16777216
115          * 25           33554432
116          * 26           67108864
117          */
118
119         /* See the documentation for the --chunk-size option of `wimlib-imagex
120          * capture' for information about allowed chunk sizes.  */
121         switch (ctype) {
122         case WIMLIB_COMPRESSION_TYPE_LZX:
123                 return order >= 15 && order <= 21;
124         case WIMLIB_COMPRESSION_TYPE_XPRESS:
125                 return order >= 12 && order <= 16;
126         case WIMLIB_COMPRESSION_TYPE_LZMS:
127                 return order >= 15 && order <= 30;
128         }
129         return false;
130 }
131
132 /* Return the default chunk size to use for the specified compression type.
133  *
134  * See notes above in wim_chunk_size_valid().  */
135 static u32
136 wim_default_chunk_size(int ctype)
137 {
138         switch (ctype) {
139         case WIMLIB_COMPRESSION_TYPE_LZMS:
140                 return 1U << 17; /* 131072  */
141         default:
142                 return 1U << 15; /* 32768   */
143         }
144 }
145
146 static int
147 is_blob_in_solid_resource(struct blob_descriptor *blob, void *_ignore)
148 {
149         return blob->blob_location == BLOB_IN_WIM &&
150                 (blob->rdesc->flags & WIM_RESHDR_FLAG_SOLID);
151 }
152
153 bool
154 wim_has_solid_resources(const WIMStruct *wim)
155 {
156         return for_blob_in_table(wim->blob_table, is_blob_in_solid_resource, NULL);
157 }
158
159 /*
160  * Calls a function on images in the WIM.  If @image is WIMLIB_ALL_IMAGES,
161  * @visitor is called on the WIM once for each image, with each image selected
162  * as the current image in turn.  If @image is a certain image, @visitor is
163  * called on the WIM only once, with that image selected.
164  */
165 int
166 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
167 {
168         int ret;
169         int start;
170         int end;
171         int i;
172
173         if (image == WIMLIB_ALL_IMAGES) {
174                 start = 1;
175                 end = wim->hdr.image_count;
176         } else if (image >= 1 && image <= wim->hdr.image_count) {
177                 start = image;
178                 end = image;
179         } else {
180                 return WIMLIB_ERR_INVALID_IMAGE;
181         }
182         for (i = start; i <= end; i++) {
183                 ret = select_wim_image(wim, i);
184                 if (ret != 0)
185                         return ret;
186                 ret = visitor(wim);
187                 if (ret != 0)
188                         return ret;
189         }
190         return 0;
191 }
192
193 /* API function documented in wimlib.h  */
194 WIMLIBAPI int
195 wimlib_create_new_wim(int ctype, WIMStruct **wim_ret)
196 {
197         WIMStruct *wim;
198         int ret;
199
200         ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
201         if (ret)
202                 return ret;
203
204         wim = new_wim_struct();
205         if (!wim)
206                 return WIMLIB_ERR_NOMEM;
207
208         ret = init_wim_header(&wim->hdr, ctype, wim_default_chunk_size(ctype));
209         if (ret)
210                 goto out_free_wim;
211
212         wim->blob_table = new_blob_table(9001);
213         if (!wim->blob_table) {
214                 ret = WIMLIB_ERR_NOMEM;
215                 goto out_free_wim;
216         }
217         wim->compression_type = ctype;
218         wim->out_compression_type = ctype;
219         wim->chunk_size = wim->hdr.chunk_size;
220         wim->out_chunk_size = wim->hdr.chunk_size;
221         *wim_ret = wim;
222         return 0;
223
224 out_free_wim:
225         FREE(wim);
226         return ret;
227 }
228
229 static void
230 destroy_image_metadata(struct wim_image_metadata *imd,
231                        struct blob_table *table,
232                        bool free_metadata_blob_descriptor)
233 {
234         free_dentry_tree(imd->root_dentry, table);
235         imd->root_dentry = NULL;
236         free_wim_security_data(imd->security_data);
237         imd->security_data = NULL;
238
239         if (free_metadata_blob_descriptor) {
240                 free_blob_descriptor(imd->metadata_blob);
241                 imd->metadata_blob = NULL;
242         }
243         if (!table) {
244                 struct blob_descriptor *blob, *tmp;
245                 list_for_each_entry_safe(blob, tmp, &imd->unhashed_blobs, unhashed_list)
246                         free_blob_descriptor(blob);
247         }
248         INIT_LIST_HEAD(&imd->unhashed_blobs);
249         INIT_LIST_HEAD(&imd->inode_list);
250 #ifdef WITH_NTFS_3G
251         if (imd->ntfs_vol) {
252                 do_ntfs_umount(imd->ntfs_vol);
253                 imd->ntfs_vol = NULL;
254         }
255 #endif
256 }
257
258 void
259 put_image_metadata(struct wim_image_metadata *imd, struct blob_table *table)
260 {
261         if (imd && --imd->refcnt == 0) {
262                 destroy_image_metadata(imd, table, true);
263                 FREE(imd);
264         }
265 }
266
267 /* Appends the specified image metadata structure to the array of image metadata
268  * for a WIM, and increments the image count. */
269 int
270 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
271 {
272         struct wim_image_metadata **imd_array;
273
274         imd_array = REALLOC(wim->image_metadata,
275                             sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
276
277         if (!imd_array)
278                 return WIMLIB_ERR_NOMEM;
279         wim->image_metadata = imd_array;
280         imd_array[wim->hdr.image_count++] = imd;
281         return 0;
282 }
283
284 struct wim_image_metadata *
285 new_image_metadata(void)
286 {
287         struct wim_image_metadata *imd;
288
289         imd = CALLOC(1, sizeof(*imd));
290         if (imd) {
291                 imd->refcnt = 1;
292                 INIT_LIST_HEAD(&imd->inode_list);
293                 INIT_LIST_HEAD(&imd->unhashed_blobs);
294         }
295         return imd;
296 }
297
298 static struct wim_image_metadata **
299 new_image_metadata_array(unsigned num_images)
300 {
301         struct wim_image_metadata **imd_array;
302
303         imd_array = CALLOC(num_images, sizeof(imd_array[0]));
304
305         if (!imd_array)
306                 return NULL;
307         for (unsigned i = 0; i < num_images; i++) {
308                 imd_array[i] = new_image_metadata();
309                 if (unlikely(!imd_array[i])) {
310                         for (unsigned j = 0; j < i; j++)
311                                 put_image_metadata(imd_array[j], NULL);
312                         FREE(imd_array);
313                         return NULL;
314                 }
315         }
316         return imd_array;
317 }
318
319
320 /*
321  * Load the metadata for the specified WIM image into memory and set it
322  * as the WIMStruct's currently selected image.
323  *
324  * @wim
325  *      The WIMStruct for the WIM.
326  * @image
327  *      The 1-based index of the image in the WIM to select.
328  *
329  * On success, 0 will be returned, wim->current_image will be set to
330  * @image, and wim_get_current_image_metadata() can be used to retrieve
331  * metadata information for the image.
332  *
333  * On failure, WIMLIB_ERR_INVALID_IMAGE, WIMLIB_ERR_METADATA_NOT_FOUND,
334  * or another error code will be returned.
335  */
336 int
337 select_wim_image(WIMStruct *wim, int image)
338 {
339         struct wim_image_metadata *imd;
340         int ret;
341
342         if (image == WIMLIB_NO_IMAGE)
343                 return WIMLIB_ERR_INVALID_IMAGE;
344
345         if (image == wim->current_image)
346                 return 0;
347
348         if (image < 1 || image > wim->hdr.image_count)
349                 return WIMLIB_ERR_INVALID_IMAGE;
350
351         if (!wim_has_metadata(wim))
352                 return WIMLIB_ERR_METADATA_NOT_FOUND;
353
354         /* If a valid image is currently selected, its metadata can be freed if
355          * it has not been modified.  */
356         deselect_current_wim_image(wim);
357         wim->current_image = image;
358         imd = wim_get_current_image_metadata(wim);
359         if (imd->root_dentry || imd->modified) {
360                 ret = 0;
361         } else {
362                 ret = read_metadata_resource(imd);
363                 if (ret)
364                         wim->current_image = WIMLIB_NO_IMAGE;
365         }
366         return ret;
367 }
368
369 void
370 deselect_current_wim_image(WIMStruct *wim)
371 {
372         struct wim_image_metadata *imd;
373         if (wim->current_image == WIMLIB_NO_IMAGE)
374                 return;
375         imd = wim_get_current_image_metadata(wim);
376         if (!imd->modified) {
377                 wimlib_assert(list_empty(&imd->unhashed_blobs));
378                 destroy_image_metadata(imd, NULL, false);
379         }
380         wim->current_image = WIMLIB_NO_IMAGE;
381 }
382
383 /* API function documented in wimlib.h  */
384 WIMLIBAPI const tchar *
385 wimlib_get_compression_type_string(int ctype)
386 {
387         switch (ctype) {
388         case WIMLIB_COMPRESSION_TYPE_NONE:
389                 return T("None");
390         case WIMLIB_COMPRESSION_TYPE_XPRESS:
391                 return T("XPRESS");
392         case WIMLIB_COMPRESSION_TYPE_LZX:
393                 return T("LZX");
394         case WIMLIB_COMPRESSION_TYPE_LZMS:
395                 return T("LZMS");
396         default:
397                 return T("Invalid");
398         }
399 }
400
401 /* API function documented in wimlib.h  */
402 WIMLIBAPI int
403 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
404 {
405         tchar *p;
406         long image;
407         int i;
408
409         if (!image_name_or_num || !*image_name_or_num)
410                 return WIMLIB_NO_IMAGE;
411
412         if (!tstrcasecmp(image_name_or_num, T("all"))
413             || !tstrcasecmp(image_name_or_num, T("*")))
414                 return WIMLIB_ALL_IMAGES;
415         image = tstrtol(image_name_or_num, &p, 10);
416         if (p != image_name_or_num && *p == T('\0') && image > 0) {
417                 if (image > wim->hdr.image_count)
418                         return WIMLIB_NO_IMAGE;
419                 return image;
420         } else {
421                 for (i = 1; i <= wim->hdr.image_count; i++) {
422                         if (!tstrcmp(image_name_or_num,
423                                      wimlib_get_image_name(wim, i)))
424                                 return i;
425                 }
426                 return WIMLIB_NO_IMAGE;
427         }
428 }
429
430 /* API function documented in wimlib.h  */
431 WIMLIBAPI void
432 wimlib_print_available_images(const WIMStruct *wim, int image)
433 {
434         int first;
435         int last;
436         int i;
437         int n;
438         if (image == WIMLIB_ALL_IMAGES) {
439                 n = tprintf(T("Available Images:\n"));
440                 first = 1;
441                 last = wim->hdr.image_count;
442         } else if (image >= 1 && image <= wim->hdr.image_count) {
443                 n = tprintf(T("Information for Image %d\n"), image);
444                 first = image;
445                 last = image;
446         } else {
447                 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
448                         image);
449                 return;
450         }
451         for (i = 0; i < n - 1; i++)
452                 tputchar(T('-'));
453         tputchar(T('\n'));
454         for (i = first; i <= last; i++)
455                 print_image_info(wim->wim_info, i);
456 }
457
458 /* API function documented in wimlib.h  */
459 WIMLIBAPI int
460 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
461 {
462         memset(info, 0, sizeof(struct wimlib_wim_info));
463         memcpy(info->guid, wim->hdr.guid, WIMLIB_GUID_LEN);
464         info->image_count = wim->hdr.image_count;
465         info->boot_index = wim->hdr.boot_idx;
466         info->wim_version = wim->hdr.wim_version;
467         info->chunk_size = wim->chunk_size;
468         info->part_number = wim->hdr.part_number;
469         info->total_parts = wim->hdr.total_parts;
470         info->compression_type = wim->compression_type;
471         info->total_bytes = wim_info_get_total_bytes(wim->wim_info);
472         info->has_integrity_table = wim_has_integrity_table(wim);
473         info->opened_from_file = (wim->filename != NULL);
474         info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
475                              (wim->hdr.total_parts != 1) ||
476                              (wim->filename && taccess(wim->filename, W_OK));
477         info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
478         info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
479         info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
480         info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
481         info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
482         info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
483         info->pipable = wim_is_pipable(wim);
484         return 0;
485 }
486
487 /* API function documented in wimlib.h  */
488 WIMLIBAPI int
489 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
490 {
491         if (which & ~(WIMLIB_CHANGE_READONLY_FLAG |
492                       WIMLIB_CHANGE_GUID |
493                       WIMLIB_CHANGE_BOOT_INDEX |
494                       WIMLIB_CHANGE_RPFIX_FLAG))
495                 return WIMLIB_ERR_INVALID_PARAM;
496
497         if ((which & WIMLIB_CHANGE_BOOT_INDEX) &&
498             info->boot_index > wim->hdr.image_count)
499                 return WIMLIB_ERR_INVALID_IMAGE;
500
501         if (which & WIMLIB_CHANGE_READONLY_FLAG) {
502                 if (info->is_marked_readonly)
503                         wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
504                 else
505                         wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
506         }
507
508         if (which & WIMLIB_CHANGE_GUID)
509                 memcpy(wim->hdr.guid, info->guid, WIM_GUID_LEN);
510
511         if (which & WIMLIB_CHANGE_BOOT_INDEX)
512                 wim->hdr.boot_idx = info->boot_index;
513
514         if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
515                 if (info->has_rpfix)
516                         wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
517                 else
518                         wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
519         }
520         return 0;
521 }
522
523 static int
524 set_out_ctype(int ctype, u8 *out_ctype_p)
525 {
526         switch (ctype) {
527         case WIMLIB_COMPRESSION_TYPE_NONE:
528         case WIMLIB_COMPRESSION_TYPE_LZX:
529         case WIMLIB_COMPRESSION_TYPE_XPRESS:
530         case WIMLIB_COMPRESSION_TYPE_LZMS:
531                 *out_ctype_p = ctype;
532                 return 0;
533         }
534         return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
535 }
536
537 /* API function documented in wimlib.h  */
538 WIMLIBAPI int
539 wimlib_set_output_compression_type(WIMStruct *wim, int ctype)
540 {
541         int ret = set_out_ctype(ctype, &wim->out_compression_type);
542         if (ret)
543                 return ret;
544
545         /* Reset the chunk size if it's no longer valid.  */
546         if (!wim_chunk_size_valid(wim->out_chunk_size, ctype))
547                 wim->out_chunk_size = wim_default_chunk_size(ctype);
548         return 0;
549 }
550
551 /* API function documented in wimlib.h  */
552 WIMLIBAPI int
553 wimlib_set_output_pack_compression_type(WIMStruct *wim, int ctype)
554 {
555         int ret = set_out_ctype(ctype, &wim->out_solid_compression_type);
556         if (ret)
557                 return ret;
558
559         /* Reset the chunk size if it's no longer valid.  */
560         if (!wim_chunk_size_valid(wim->out_solid_chunk_size, ctype))
561                 wim->out_solid_chunk_size = wim_default_solid_chunk_size(ctype);
562         return 0;
563 }
564
565 static int
566 set_out_chunk_size(u32 chunk_size, int ctype, u32 *out_chunk_size_p)
567 {
568         if (!wim_chunk_size_valid(chunk_size, ctype))
569                 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
570
571         *out_chunk_size_p = chunk_size;
572         return 0;
573 }
574
575 /* API function documented in wimlib.h  */
576 WIMLIBAPI int
577 wimlib_set_output_chunk_size(WIMStruct *wim, uint32_t chunk_size)
578 {
579         if (chunk_size == 0) {
580                 wim->out_chunk_size =
581                         wim_default_chunk_size(wim->out_compression_type);
582                 return 0;
583         }
584
585         return set_out_chunk_size(chunk_size,
586                                   wim->out_compression_type,
587                                   &wim->out_chunk_size);
588 }
589
590 /* API function documented in wimlib.h  */
591 WIMLIBAPI int
592 wimlib_set_output_pack_chunk_size(WIMStruct *wim, uint32_t chunk_size)
593 {
594         if (chunk_size == 0) {
595                 wim->out_solid_chunk_size =
596                         wim_default_solid_chunk_size(wim->out_solid_compression_type);
597                 return 0;
598         }
599
600         return set_out_chunk_size(chunk_size,
601                                   wim->out_solid_compression_type,
602                                   &wim->out_solid_chunk_size);
603 }
604
605 WIMLIBAPI void
606 wimlib_register_progress_function(WIMStruct *wim,
607                                   wimlib_progress_func_t progfunc,
608                                   void *progctx)
609 {
610         wim->progfunc = progfunc;
611         wim->progctx = progctx;
612 }
613
614 static int
615 open_wim_file(const tchar *filename, struct filedes *fd_ret)
616 {
617         int raw_fd;
618
619         raw_fd = topen(filename, O_RDONLY | O_BINARY);
620         if (raw_fd < 0) {
621                 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
622                 return WIMLIB_ERR_OPEN;
623         }
624         filedes_init(fd_ret, raw_fd);
625         return 0;
626 }
627
628 /*
629  * Begins the reading of a WIM file; opens the file and reads its header and
630  * blob table, and optionally checks the integrity.
631  */
632 static int
633 begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
634 {
635         int ret;
636         int xml_num_images;
637         const tchar *wimfile;
638
639         if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
640                 wimfile = NULL;
641                 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
642                 wim->in_fd.is_pipe = 1;
643         } else {
644                 wimfile = wim_filename_or_fd;
645                 ret = open_wim_file(wimfile, &wim->in_fd);
646                 if (ret)
647                         return ret;
648
649                 /* The absolute path to the WIM is requested so that
650                  * wimlib_overwrite() still works even if the process changes
651                  * its working directory.  This actually happens if a WIM is
652                  * mounted read-write, since the FUSE thread changes directory
653                  * to "/", and it needs to be able to find the WIM file again.
654                  *
655                  * This will break if the full path to the WIM changes in the
656                  * intervening time...
657                  *
658                  * Warning: in Windows native builds, realpath() calls the
659                  * replacement function in win32_replacements.c.
660                  */
661                 wim->filename = realpath(wimfile, NULL);
662                 if (!wim->filename) {
663                         ERROR_WITH_ERRNO("Failed to get full path to file "
664                                          "\"%"TS"\"", wimfile);
665                         if (errno == ENOMEM)
666                                 return WIMLIB_ERR_NOMEM;
667                         else
668                                 return WIMLIB_ERR_NO_FILENAME;
669                 }
670         }
671
672         ret = read_wim_header(wim, &wim->hdr);
673         if (ret)
674                 return ret;
675
676         if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
677                 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS flag is set in the header of\n"
678                         "          \"%"TS"\".  It may be being changed by another process,\n"
679                         "          or a process may have crashed while writing the WIM.",
680                         wimfile);
681         }
682
683         if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
684                 ret = can_modify_wim(wim);
685                 if (ret)
686                         return ret;
687         }
688
689         if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
690             (wim->hdr.total_parts != 1))
691                 return WIMLIB_ERR_IS_SPLIT_WIM;
692
693         /* If the boot index is invalid, print a warning and set it to 0 */
694         if (wim->hdr.boot_idx > wim->hdr.image_count) {
695                 WARNING("Ignoring invalid boot index.");
696                 wim->hdr.boot_idx = 0;
697         }
698
699         /* Check and cache the compression type */
700         if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
701                 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
702                         wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
703                 } else if (wim->hdr.flags & (WIM_HDR_FLAG_COMPRESS_XPRESS |
704                                              WIM_HDR_FLAG_COMPRESS_XPRESS_2)) {
705                         wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
706                 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZMS) {
707                         wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
708                 } else {
709                         return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
710                 }
711         } else {
712                 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
713         }
714         wim->out_compression_type = wim->compression_type;
715
716         /* Check and cache the chunk size.  */
717         wim->chunk_size = wim->hdr.chunk_size;
718         wim->out_chunk_size = wim->chunk_size;
719         if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
720                 ERROR("Invalid chunk size (%"PRIu32" bytes) "
721                       "for compression type %"TS"!", wim->chunk_size,
722                       wimlib_get_compression_type_string(wim->compression_type));
723                 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
724         }
725
726         if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
727                 ret = check_wim_integrity(wim);
728                 if (ret == WIM_INTEGRITY_NONEXISTENT) {
729                         WARNING("\"%"TS"\" does not contain integrity "
730                                 "information.  Skipping integrity check.",
731                                 wimfile);
732                 } else if (ret == WIM_INTEGRITY_NOT_OK) {
733                         return WIMLIB_ERR_INTEGRITY;
734                 } else if (ret != WIM_INTEGRITY_OK) {
735                         return ret;
736                 }
737         }
738
739         if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
740                 wim->image_metadata = new_image_metadata_array(wim->hdr.image_count);
741                 if (!wim->image_metadata)
742                         return WIMLIB_ERR_NOMEM;
743         }
744
745         if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
746                 wim->blob_table = new_blob_table(9001);
747                 if (!wim->blob_table)
748                         return WIMLIB_ERR_NOMEM;
749         } else {
750
751                 ret = read_wim_xml_data(wim);
752                 if (ret)
753                         return ret;
754
755                 xml_num_images = wim_info_get_num_images(wim->wim_info);
756                 if (xml_num_images != wim->hdr.image_count) {
757                         ERROR("The WIM's header is inconsistent with its XML data.\n"
758                               "        Please submit a bug report if you believe this "
759                               "WIM file should be considered valid.");
760                         return WIMLIB_ERR_IMAGE_COUNT;
761                 }
762
763                 ret = read_blob_table(wim);
764                 if (ret)
765                         return ret;
766         }
767         return 0;
768 }
769
770 int
771 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
772                       WIMStruct **wim_ret,
773                       wimlib_progress_func_t progfunc, void *progctx)
774 {
775         WIMStruct *wim;
776         int ret;
777
778         ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
779         if (ret)
780                 return ret;
781
782         wim = new_wim_struct();
783         if (!wim)
784                 return WIMLIB_ERR_NOMEM;
785
786         wim->progfunc = progfunc;
787         wim->progctx = progctx;
788
789         ret = begin_read(wim, wim_filename_or_fd, open_flags);
790         if (ret) {
791                 wimlib_free(wim);
792                 return ret;
793         }
794
795         *wim_ret = wim;
796         return 0;
797 }
798
799 /* API function documented in wimlib.h  */
800 WIMLIBAPI int
801 wimlib_open_wim_with_progress(const tchar *wimfile, int open_flags,
802                               WIMStruct **wim_ret,
803                               wimlib_progress_func_t progfunc, void *progctx)
804 {
805         if (open_flags & ~(WIMLIB_OPEN_FLAG_CHECK_INTEGRITY |
806                            WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT |
807                            WIMLIB_OPEN_FLAG_WRITE_ACCESS))
808                 return WIMLIB_ERR_INVALID_PARAM;
809
810         if (!wimfile || !*wimfile || !wim_ret)
811                 return WIMLIB_ERR_INVALID_PARAM;
812
813         return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
814                                      progfunc, progctx);
815 }
816
817 /* API function documented in wimlib.h  */
818 WIMLIBAPI int
819 wimlib_open_wim(const tchar *wimfile, int open_flags, WIMStruct **wim_ret)
820 {
821         return wimlib_open_wim_with_progress(wimfile, open_flags, wim_ret,
822                                              NULL, NULL);
823 }
824
825 /* Checksum all blobs that are unhashed (other than the metadata blobs), merging
826  * them into the blob table as needed.  This is a no-op unless files have been
827  * added to an image in the same WIMStruct.  */
828 int
829 wim_checksum_unhashed_blobs(WIMStruct *wim)
830 {
831         int ret;
832
833         if (!wim_has_metadata(wim))
834                 return 0;
835         for (int i = 0; i < wim->hdr.image_count; i++) {
836                 struct blob_descriptor *blob, *tmp;
837                 struct wim_image_metadata *imd = wim->image_metadata[i];
838                 image_for_each_unhashed_blob_safe(blob, tmp, imd) {
839                         struct blob_descriptor *new_blob;
840                         ret = hash_unhashed_blob(blob, wim->blob_table, &new_blob);
841                         if (ret)
842                                 return ret;
843                         if (new_blob != blob)
844                                 free_blob_descriptor(blob);
845                 }
846         }
847         return 0;
848 }
849
850 /*
851  * can_modify_wim - Check if a given WIM is writeable.  This is only the case if
852  * it meets the following three conditions:
853  *
854  * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
855  * 2. The WIM is not part of a spanned set.
856  * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
857  *
858  * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
859  */
860 int
861 can_modify_wim(WIMStruct *wim)
862 {
863         if (wim->filename) {
864                 if (taccess(wim->filename, W_OK)) {
865                         ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
866                         return WIMLIB_ERR_WIM_IS_READONLY;
867                 }
868         }
869         if (wim->hdr.total_parts != 1) {
870                 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
871                       wim->filename);
872                 return WIMLIB_ERR_WIM_IS_READONLY;
873         }
874         if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
875                 ERROR("Cannot modify \"%"TS"\": is marked read-only",
876                       wim->filename);
877                 return WIMLIB_ERR_WIM_IS_READONLY;
878         }
879         return 0;
880 }
881
882 /* API function documented in wimlib.h  */
883 WIMLIBAPI void
884 wimlib_free(WIMStruct *wim)
885 {
886         if (!wim)
887                 return;
888
889         while (!list_empty(&wim->subwims)) {
890                 WIMStruct *subwim;
891
892                 subwim = list_entry(wim->subwims.next, WIMStruct, subwim_node);
893                 list_del(&subwim->subwim_node);
894                 wimlib_free(subwim);
895         }
896
897         if (filedes_valid(&wim->in_fd))
898                 filedes_close(&wim->in_fd);
899         if (filedes_valid(&wim->out_fd))
900                 filedes_close(&wim->out_fd);
901
902         free_blob_table(wim->blob_table);
903
904         wimlib_free_decompressor(wim->decompressor);
905
906         FREE(wim->filename);
907         free_wim_info(wim->wim_info);
908         if (wim->image_metadata) {
909                 for (unsigned i = 0; i < wim->hdr.image_count; i++)
910                         put_image_metadata(wim->image_metadata[i], NULL);
911                 FREE(wim->image_metadata);
912         }
913         FREE(wim);
914 }
915
916 static bool
917 test_locale_ctype_utf8(void)
918 {
919 #ifdef __WIN32__
920         return false;
921 #else
922         char *ctype = nl_langinfo(CODESET);
923
924         return (!strstr(ctype, "UTF-8") ||
925                 !strstr(ctype, "UTF8") ||
926                 !strstr(ctype, "utf8") ||
927                 !strstr(ctype, "utf-8"));
928 #endif
929 }
930
931 /* API function documented in wimlib.h  */
932 WIMLIBAPI u32
933 wimlib_get_version(void)
934 {
935         return (WIMLIB_MAJOR_VERSION << 20) |
936                (WIMLIB_MINOR_VERSION << 10) |
937                 WIMLIB_PATCH_VERSION;
938 }
939
940 static bool lib_initialized = false;
941
942 /* API function documented in wimlib.h  */
943 WIMLIBAPI int
944 wimlib_global_init(int init_flags)
945 {
946         if (lib_initialized)
947                 return 0;
948
949 #ifdef ENABLE_ERROR_MESSAGES
950         if (!wimlib_error_file)
951                 wimlib_error_file = stderr;
952 #endif
953
954         if (init_flags & ~(WIMLIB_INIT_FLAG_ASSUME_UTF8 |
955                            WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES |
956                            WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES |
957                            WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES |
958                            WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
959                            WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
960                 return WIMLIB_ERR_INVALID_PARAM;
961
962         libxml_global_init();
963         if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
964                 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
965         #ifdef WITH_NTFS_3G
966                 if (!wimlib_mbs_is_utf8)
967                         libntfs3g_global_init();
968         #endif
969         }
970 #ifdef __WIN32__
971         {
972                 int ret = win32_global_init(init_flags);
973                 if (ret)
974                         return ret;
975         }
976 #endif
977         iconv_global_init();
978         init_upcase();
979         if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE)
980                 default_ignore_case = false;
981         else if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE)
982                 default_ignore_case = true;
983         lib_initialized = true;
984         return 0;
985 }
986
987 /* API function documented in wimlib.h  */
988 WIMLIBAPI void
989 wimlib_global_cleanup(void)
990 {
991         if (!lib_initialized)
992                 return;
993         libxml_global_cleanup();
994         iconv_global_cleanup();
995 #ifdef __WIN32__
996         win32_global_cleanup();
997 #endif
998
999         wimlib_set_error_file(NULL);
1000         lib_initialized = false;
1001 }