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