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