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