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