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