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