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