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