]> wimlib.net Git - wimlib/blob - src/wim.c
a62be39e05709c8f8a66f30d3461b1ac8e39700c
[wimlib] / src / wim.c
1 /*
2  * wim.c - Stuff that doesn't fit into any other file
3  */
4
5 /*
6  * Copyright (C) 2012, 2013 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib/error.h"
29 #include "wimlib/dentry.h"
30 #include "wimlib/encoding.h"
31 #include "wimlib/file_io.h"
32 #include "wimlib/integrity.h"
33 #include "wimlib/lookup_table.h"
34 #include "wimlib/metadata.h"
35 #ifdef WITH_NTFS_3G
36 #  include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
37 #endif
38 #include "wimlib/security.h"
39 #include "wimlib/wim.h"
40 #include "wimlib/xml.h"
41
42 #ifdef __WIN32__
43 #  include "wimlib/win32.h" /* for realpath() replacement */
44 #endif
45
46 #include <errno.h>
47 #include <fcntl.h>
48 #ifndef __WIN32__
49 #  include <langinfo.h>
50 #endif
51 #include <limits.h>
52 #include <stdarg.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55
56 static int
57 image_print_metadata(WIMStruct *wim)
58 {
59         DEBUG("Printing metadata for image %d", wim->current_image);
60         print_wim_security_data(wim_security_data(wim));
61         return for_dentry_in_tree(wim_root_dentry(wim), print_dentry,
62                                   wim->lookup_table);
63 }
64
65
66 static WIMStruct *
67 new_wim_struct(void)
68 {
69         WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
70         if (wim) {
71                 wim->in_fd.fd = -1;
72                 wim->out_fd.fd = -1;
73                 INIT_LIST_HEAD(&wim->subwims);
74         }
75         return wim;
76 }
77
78 /* Determine if the chunk size is valid for the specified compression type.  */
79 static bool
80 wim_chunk_size_valid(u32 chunk_size, int ctype)
81 {
82         u32 order;
83
84         /* Chunk size is meaningless for uncompressed WIMs --- any value is
85          * okay.  */
86         if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
87                 return true;
88
89         /* Chunk size must be power of 2.  */
90         if (chunk_size == 0)
91                 return false;
92         order = bsr32(chunk_size);
93         if (chunk_size != 1U << order)
94                 return false;
95
96         /* Order        Size
97          * =====        ====
98          * 15           32768
99          * 16           65536
100          * 17           131072
101          * 18           262144
102          * 19           524288
103          * 20           1048576
104          * 21           2097152
105          * 22           4194304
106          * 23           8388608
107          * 24           16777216
108          * 25           33554432
109          * 26           67108864
110          */
111         switch (ctype) {
112         case WIMLIB_COMPRESSION_TYPE_LZX:
113                 /* For LZX compression, the chunk size corresponds to the LZX
114                  * window size, which according the LZX specification can be any
115                  * power of 2 between 2^15 and 2^21, inclusively.  All these are
116                  * supported by wimlib; however, unfortunately only 2^15 is
117                  * supported by WIMGAPI[1] so this value is used by default.
118                  *
119                  * [1] WIMGAPI (Windows 7) attempts to decompress LZX chunk
120                  * sizes > 2^15 but seems to have bug(s) that cause it to fail
121                  * or crash.  (I tried several tweaks to the LZX data but none
122                  * resulted in successful decompression.)  WIMGAPI (Windows 8)
123                  * appears to refuse to open WIMs with chunk size > 2^15
124                  * entirely.  */
125                 return order >= 15 && order <= 21;
126
127         case WIMLIB_COMPRESSION_TYPE_XPRESS:
128                 /* WIMGAPI (Windows 7) didn't seem to support XPRESS chunk size
129                  * below 32768 bytes, but larger power-of-two sizes appear to be
130                  * supported.  67108864 was the largest size that worked.
131                  * (Note, however, that the offsets of XPRESS matches are still
132                  * limited to 65535 bytes even when a much larger chunk size is
133                  * used!)
134                  *
135                  * WIMGAPI (Windows 8) seemed to have removed the support for
136                  * larger XPRESS chunk sizes and will refuse to open such WIMs.
137                  *
138                  * 2^15 = 32768 is the default value used for compatibility, but
139                  * wimlib can actually use up to 2^26.  */
140                 return order >= 15 && order <= 26;
141
142         case WIMLIB_COMPRESSION_TYPE_LZMS:
143                 /* TODO */
144                 return 131072;
145         }
146         return false;
147 }
148
149 /* Return the default chunk size to use for the specified compression type.
150  *
151  * See notes above in wim_chunk_size_valid().  */
152 static u32
153 wim_default_chunk_size(int ctype)
154 {
155         return 32768;
156 }
157
158 /*
159  * Calls a function on images in the WIM.  If @image is WIMLIB_ALL_IMAGES, @visitor
160  * is called on the WIM once for each image, with each image selected as the
161  * current image in turn.  If @image is a certain image, @visitor is called on
162  * the WIM only once, with that image selected.
163  */
164 int
165 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
166 {
167         int ret;
168         int start;
169         int end;
170         int i;
171
172         if (image == WIMLIB_ALL_IMAGES) {
173                 start = 1;
174                 end = wim->hdr.image_count;
175         } else if (image >= 1 && image <= wim->hdr.image_count) {
176                 start = image;
177                 end = image;
178         } else {
179                 return WIMLIB_ERR_INVALID_IMAGE;
180         }
181         for (i = start; i <= end; i++) {
182                 ret = select_wim_image(wim, i);
183                 if (ret != 0)
184                         return ret;
185                 ret = visitor(wim);
186                 if (ret != 0)
187                         return ret;
188         }
189         return 0;
190 }
191
192 /* API function documented in wimlib.h  */
193 WIMLIBAPI int
194 wimlib_create_new_wim(int ctype, WIMStruct **wim_ret)
195 {
196         WIMStruct *wim;
197         struct wim_lookup_table *table;
198         int ret;
199
200         wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
201
202         DEBUG("Creating new WIM with %"TS" compression.",
203               wimlib_get_compression_type_string(ctype));
204
205         /* Allocate the WIMStruct. */
206         wim = new_wim_struct();
207         if (wim == NULL)
208                 return WIMLIB_ERR_NOMEM;
209
210         ret = init_wim_header(&wim->hdr, ctype, wim_default_chunk_size(ctype));
211         if (ret)
212                 goto out_free;
213
214         table = new_lookup_table(9001);
215         if (table == NULL) {
216                 ret = WIMLIB_ERR_NOMEM;
217                 goto out_free;
218         }
219         wim->lookup_table = table;
220         wim->refcnts_ok = 1;
221         wim->compression_type = ctype;
222         wim->out_compression_type = ctype;
223         wim->chunk_size = wim->hdr.chunk_size;
224         wim->out_chunk_size = wim->hdr.chunk_size;
225         *wim_ret = wim;
226         return 0;
227 out_free:
228         FREE(wim);
229         return ret;
230 }
231
232 int
233 select_wim_image(WIMStruct *wim, int image)
234 {
235         struct wim_image_metadata *imd;
236         int ret;
237
238         DEBUG("Selecting image %d", image);
239
240         if (image == WIMLIB_NO_IMAGE) {
241                 ERROR("Invalid image: %d", WIMLIB_NO_IMAGE);
242                 return WIMLIB_ERR_INVALID_IMAGE;
243         }
244
245         if (image == wim->current_image)
246                 return 0;
247
248         if (image < 1 || image > wim->hdr.image_count) {
249                 ERROR("Cannot select image %d: There are only %u images",
250                       image, wim->hdr.image_count);
251                 return WIMLIB_ERR_INVALID_IMAGE;
252         }
253
254         if (!wim_has_metadata(wim)) {
255                 ERROR("\"%"TS"\" does not contain metadata resources!", wim->filename);
256                 if (wim->hdr.part_number != 1)
257                         ERROR("Specify the first part of the split WIM instead.");
258                 return WIMLIB_ERR_METADATA_NOT_FOUND;
259         }
260
261         /* If a valid image is currently selected, it can be freed if it is not
262          * modified.  */
263         if (wim->current_image != WIMLIB_NO_IMAGE) {
264                 imd = wim_get_current_image_metadata(wim);
265                 if (!imd->modified) {
266                         wimlib_assert(list_empty(&imd->unhashed_streams));
267                         DEBUG("Freeing image %u", wim->current_image);
268                         destroy_image_metadata(imd, NULL, false);
269                 }
270         }
271         wim->current_image = image;
272         imd = wim_get_current_image_metadata(wim);
273         if (imd->root_dentry || imd->modified) {
274                 ret = 0;
275         } else {
276                 #ifdef ENABLE_DEBUG
277                 DEBUG("Reading metadata resource specified by the following "
278                       "lookup table entry:");
279                 print_lookup_table_entry(imd->metadata_lte, stderr);
280                 #endif
281                 ret = read_metadata_resource(wim, imd);
282                 if (ret)
283                         wim->current_image = WIMLIB_NO_IMAGE;
284         }
285         return ret;
286 }
287
288
289 /* API function documented in wimlib.h  */
290 WIMLIBAPI const tchar *
291 wimlib_get_compression_type_string(int ctype)
292 {
293         switch (ctype) {
294                 case WIMLIB_COMPRESSION_TYPE_NONE:
295                         return T("None");
296                 case WIMLIB_COMPRESSION_TYPE_LZX:
297                         return T("LZX");
298                 case WIMLIB_COMPRESSION_TYPE_XPRESS:
299                         return T("XPRESS");
300                 default:
301                         return T("Invalid");
302         }
303 }
304
305 /* API function documented in wimlib.h  */
306 WIMLIBAPI int
307 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
308 {
309         tchar *p;
310         long image;
311         int i;
312
313         if (!image_name_or_num || !*image_name_or_num)
314                 return WIMLIB_NO_IMAGE;
315
316         if (!tstrcasecmp(image_name_or_num, T("all"))
317             || !tstrcasecmp(image_name_or_num, T("*")))
318                 return WIMLIB_ALL_IMAGES;
319         image = tstrtol(image_name_or_num, &p, 10);
320         if (p != image_name_or_num && *p == T('\0') && image > 0) {
321                 if (image > wim->hdr.image_count)
322                         return WIMLIB_NO_IMAGE;
323                 return image;
324         } else {
325                 for (i = 1; i <= wim->hdr.image_count; i++) {
326                         if (!tstrcmp(image_name_or_num,
327                                      wimlib_get_image_name(wim, i)))
328                                 return i;
329                 }
330                 return WIMLIB_NO_IMAGE;
331         }
332 }
333
334 /* API function documented in wimlib.h  */
335 WIMLIBAPI void
336 wimlib_print_available_images(const WIMStruct *wim, int image)
337 {
338         int first;
339         int last;
340         int i;
341         int n;
342         if (image == WIMLIB_ALL_IMAGES) {
343                 n = tprintf(T("Available Images:\n"));
344                 first = 1;
345                 last = wim->hdr.image_count;
346         } else if (image >= 1 && image <= wim->hdr.image_count) {
347                 n = tprintf(T("Information for Image %d\n"), image);
348                 first = image;
349                 last = image;
350         } else {
351                 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
352                         image);
353                 return;
354         }
355         for (i = 0; i < n - 1; i++)
356                 tputchar(T('-'));
357         tputchar(T('\n'));
358         for (i = first; i <= last; i++)
359                 print_image_info(wim->wim_info, i);
360 }
361
362
363 /* API function documented in wimlib.h  */
364 WIMLIBAPI int
365 wimlib_print_metadata(WIMStruct *wim, int image)
366 {
367         return for_image(wim, image, image_print_metadata);
368 }
369
370 /* API function documented in wimlib.h  */
371 WIMLIBAPI int
372 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
373 {
374         memset(info, 0, sizeof(struct wimlib_wim_info));
375         memcpy(info->guid, wim->hdr.guid, WIMLIB_GUID_LEN);
376         info->image_count = wim->hdr.image_count;
377         info->boot_index = wim->hdr.boot_idx;
378         info->wim_version = wim->hdr.wim_version;
379         info->chunk_size = wim->hdr.chunk_size;
380         info->part_number = wim->hdr.part_number;
381         info->total_parts = wim->hdr.total_parts;
382         info->compression_type = wim->compression_type;
383         info->total_bytes = wim_info_get_total_bytes(wim->wim_info);
384         info->has_integrity_table = wim_has_integrity_table(wim);
385         info->opened_from_file = (wim->filename != NULL);
386         info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
387                              (wim->hdr.total_parts != 1) ||
388                              (wim->filename && taccess(wim->filename, W_OK));
389         info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
390         info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
391         info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
392         info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
393         info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
394         info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
395         info->pipable = wim_is_pipable(wim);
396         return 0;
397 }
398
399 /* API function documented in wimlib.h  */
400 WIMLIBAPI int
401 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
402 {
403         int ret;
404
405         if (which & WIMLIB_CHANGE_READONLY_FLAG) {
406                 if (info->is_marked_readonly)
407                         wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
408                 else
409                         wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
410         }
411
412         if ((which & ~WIMLIB_CHANGE_READONLY_FLAG) == 0)
413                 return 0;
414
415         ret = can_modify_wim(wim);
416         if (ret)
417                 return ret;
418
419         if (which & WIMLIB_CHANGE_GUID) {
420                 memcpy(wim->hdr.guid, info->guid, WIM_GID_LEN);
421                 wim->guid_set_explicitly = 1;
422         }
423
424         if (which & WIMLIB_CHANGE_BOOT_INDEX) {
425                 if (info->boot_index > wim->hdr.image_count) {
426                         ERROR("%u is not 0 or a valid image in the WIM to mark as bootable",
427                               info->boot_index);
428                         return WIMLIB_ERR_INVALID_IMAGE;
429                 }
430                 wim->hdr.boot_idx = info->boot_index;
431         }
432
433         if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
434                 if (info->has_rpfix)
435                         wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
436                 else
437                         wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
438         }
439         return 0;
440 }
441
442 /* API function documented in wimlib.h  */
443 WIMLIBAPI int
444 wimlib_set_output_compression_type(WIMStruct *wim, int ctype)
445 {
446         switch (ctype) {
447         case WIMLIB_COMPRESSION_TYPE_INVALID:
448                 break;
449         case WIMLIB_COMPRESSION_TYPE_NONE:
450         case WIMLIB_COMPRESSION_TYPE_LZX:
451         case WIMLIB_COMPRESSION_TYPE_XPRESS:
452                 wim->out_compression_type = ctype;
453
454                 /* Reset the chunk size if it's no longer valid.  */
455                 if (!wim_chunk_size_valid(wim->out_chunk_size,
456                                           wim->out_compression_type))
457                         wim->out_chunk_size = wim_default_chunk_size(wim->out_compression_type);
458                 return 0;
459         }
460         return WIMLIB_ERR_INVALID_PARAM;
461 }
462
463 /* API function documented in wimlib.h  */
464 WIMLIBAPI int
465 wimlib_set_output_chunk_size(WIMStruct *wim, uint32_t chunk_size)
466 {
467         if (!wim_chunk_size_valid(chunk_size, wim->out_compression_type)) {
468                 ERROR("Invalid chunk size (%"PRIu32" bytes) "
469                       "for compression type %"TS"!",
470                       chunk_size,
471                       wimlib_get_compression_type_string(wim->out_compression_type));
472                 switch (wim->out_compression_type) {
473                 case WIMLIB_COMPRESSION_TYPE_XPRESS:
474                         ERROR("Valid chunk sizes for XPRESS are "
475                               "32768, 65536, 131072, ..., 67108864.");
476                         break;
477                 case WIMLIB_COMPRESSION_TYPE_LZX:
478                         ERROR("Valid chunk sizes for LZX are "
479                               "32768, 65536, 131072, ..., 2097152.");
480                         break;
481                 }
482                 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
483         }
484         if (chunk_size != 32768) {
485                 WARNING  ("Changing the compression chunk size to any value other than\n"
486                 "          the default of 32768 bytes eliminates compatibility with\n"
487                 "          Microsoft's software!");
488         }
489         wim->out_chunk_size = chunk_size;
490         return 0;
491 }
492
493 static int
494 do_open_wim(const tchar *filename, struct filedes *fd_ret)
495 {
496         int raw_fd;
497
498         raw_fd = topen(filename, O_RDONLY | O_BINARY);
499         if (raw_fd < 0) {
500                 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
501                 return WIMLIB_ERR_OPEN;
502         }
503         filedes_init(fd_ret, raw_fd);
504         return 0;
505 }
506
507 int
508 reopen_wim(WIMStruct *wim)
509 {
510         wimlib_assert(!filedes_valid(&wim->in_fd));
511         return do_open_wim(wim->filename, &wim->in_fd);
512 }
513
514 int
515 close_wim(WIMStruct *wim)
516 {
517         if (filedes_valid(&wim->in_fd)) {
518                 filedes_close(&wim->in_fd);
519                 filedes_invalidate(&wim->in_fd);
520         }
521         return 0;
522 }
523
524 /*
525  * Begins the reading of a WIM file; opens the file and reads its header and
526  * lookup table, and optionally checks the integrity.
527  */
528 static int
529 begin_read(WIMStruct *wim, const void *wim_filename_or_fd,
530            int open_flags, wimlib_progress_func_t progress_func)
531 {
532         int ret;
533         int xml_num_images;
534         const tchar *wimfile;
535
536         if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
537                 wimfile = NULL;
538                 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
539                 wim->in_fd.is_pipe = 1;
540         } else {
541                 wimfile = wim_filename_or_fd;
542                 ret = do_open_wim(wimfile, &wim->in_fd);
543                 if (ret)
544                         return ret;
545
546                 /* The absolute path to the WIM is requested so that
547                  * wimlib_overwrite() still works even if the process changes
548                  * its working directory.  This actually happens if a WIM is
549                  * mounted read-write, since the FUSE thread changes directory
550                  * to "/", and it needs to be able to find the WIM file again.
551                  *
552                  * This will break if the full path to the WIM changes in the
553                  * intervening time...
554                  *
555                  * Warning: in Windows native builds, realpath() calls the
556                  * replacement function in win32_replacements.c.
557                  */
558                 wim->filename = realpath(wimfile, NULL);
559                 if (wim->filename == NULL) {
560                         ERROR_WITH_ERRNO("Failed to resolve WIM filename");
561                         if (errno == ENOMEM)
562                                 return WIMLIB_ERR_NOMEM;
563                         else
564                                 return WIMLIB_ERR_OPEN;
565                 }
566         }
567
568         ret = read_wim_header(wim, &wim->hdr);
569         if (ret)
570                 return ret;
571
572         if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
573                 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS flag is set in the header of\n"
574                         "          \"%"TS"\".  It may be being changed by another process,\n"
575                         "          or a process may have crashed while writing the WIM.",
576                         wimfile);
577         }
578
579         if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
580                 ret = can_modify_wim(wim);
581                 if (ret)
582                         return ret;
583         }
584
585         if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
586             (wim->hdr.total_parts != 1))
587                 return WIMLIB_ERR_IS_SPLIT_WIM;
588
589         DEBUG("According to header, WIM contains %u images", wim->hdr.image_count);
590
591         /* If the boot index is invalid, print a warning and set it to 0 */
592         if (wim->hdr.boot_idx > wim->hdr.image_count) {
593                 WARNING("In `%"TS"', image %u is marked as bootable, "
594                         "but there are only %u images in the WIM",
595                         wimfile, wim->hdr.boot_idx, wim->hdr.image_count);
596                 wim->hdr.boot_idx = 0;
597         }
598
599         /* Check and cache the compression type */
600         if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
601                 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
602                         wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
603                 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_XPRESS) {
604                         wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
605         #if 0
606                 /* TODO */
607                 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZMS) {
608                         wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
609         #endif
610                 } else {
611                         ERROR("The compression flag is set on \"%"TS"\", but "
612                               "a flag for a recognized format is not",
613                               wimfile);
614                         return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
615                 }
616         } else {
617                 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
618         }
619         wim->out_compression_type = wim->compression_type;
620
621         /* Check and cache the chunk size.  */
622         wim->chunk_size = wim->out_chunk_size = wim->hdr.chunk_size;
623         if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
624                 ERROR("Invalid chunk size (%"PRIu32" bytes) "
625                       "for compression type %"TS"!",
626                       wim->chunk_size,
627                       wimlib_get_compression_type_string(wim->compression_type));
628                 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
629         }
630
631         if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
632                 ret = check_wim_integrity(wim, progress_func);
633                 if (ret == WIM_INTEGRITY_NONEXISTENT) {
634                         WARNING("No integrity information for `%"TS"'; skipping "
635                                 "integrity check.", wimfile);
636                 } else if (ret == WIM_INTEGRITY_NOT_OK) {
637                         ERROR("WIM is not intact! (Failed integrity check)");
638                         return WIMLIB_ERR_INTEGRITY;
639                 } else if (ret != WIM_INTEGRITY_OK) {
640                         return ret;
641                 }
642         }
643
644         if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
645                 wim->image_metadata = new_image_metadata_array(wim->hdr.image_count);
646                 if (wim->image_metadata == NULL)
647                         return WIMLIB_ERR_NOMEM;
648         }
649
650         if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
651                 wim->lookup_table = new_lookup_table(9001);
652                 if (wim->lookup_table == NULL)
653                         return WIMLIB_ERR_NOMEM;
654         } else {
655                 ret = read_wim_lookup_table(wim);
656                 if (ret)
657                         return ret;
658
659                 ret = read_wim_xml_data(wim);
660                 if (ret)
661                         return ret;
662
663                 xml_num_images = wim_info_get_num_images(wim->wim_info);
664                 if (xml_num_images != wim->hdr.image_count) {
665                         ERROR("In the file `%"TS"', there are %u <IMAGE> elements "
666                               "in the XML data,", wimfile, xml_num_images);
667                         ERROR("but %u images in the WIM!  There must be exactly one "
668                               "<IMAGE> element per image.", wim->hdr.image_count);
669                         return WIMLIB_ERR_IMAGE_COUNT;
670                 }
671                 DEBUG("Done beginning read of WIM file `%"TS"'.", wimfile);
672         }
673         return 0;
674 }
675
676 int
677 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
678                       WIMStruct **wim_ret, wimlib_progress_func_t progress_func)
679 {
680         WIMStruct *wim;
681         int ret;
682
683         if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE)
684                 DEBUG("Opening pipable WIM from file descriptor %d.", *(const int*)wim_filename_or_fd);
685         else
686                 DEBUG("Opening WIM file \"%"TS"\"", (const tchar*)wim_filename_or_fd);
687
688         wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
689
690         if (wim_ret == NULL)
691                 return WIMLIB_ERR_INVALID_PARAM;
692
693         wim = new_wim_struct();
694         if (wim == NULL)
695                 return WIMLIB_ERR_NOMEM;
696
697         ret = begin_read(wim, wim_filename_or_fd, open_flags, progress_func);
698         if (ret) {
699                 wimlib_free(wim);
700                 return ret;
701         }
702
703         DEBUG("Successfully opened WIM and created WIMStruct.");
704         *wim_ret = wim;
705         return 0;
706 }
707
708 /* API function documented in wimlib.h  */
709 WIMLIBAPI int
710 wimlib_open_wim(const tchar *wimfile, int open_flags,
711                 WIMStruct **wim_ret, wimlib_progress_func_t progress_func)
712 {
713         open_flags &= WIMLIB_OPEN_MASK_PUBLIC;
714         return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
715                                      progress_func);
716 }
717
718 void
719 destroy_image_metadata(struct wim_image_metadata *imd,
720                        struct wim_lookup_table *table,
721                        bool free_metadata_lte)
722 {
723         free_dentry_tree(imd->root_dentry, table);
724         imd->root_dentry = NULL;
725         free_wim_security_data(imd->security_data);
726         imd->security_data = NULL;
727
728         if (free_metadata_lte) {
729                 free_lookup_table_entry(imd->metadata_lte);
730                 imd->metadata_lte = NULL;
731         }
732         if (table == NULL) {
733                 struct wim_lookup_table_entry *lte, *tmp;
734                 list_for_each_entry_safe(lte, tmp, &imd->unhashed_streams, unhashed_list)
735                         free_lookup_table_entry(lte);
736         }
737         INIT_LIST_HEAD(&imd->unhashed_streams);
738         INIT_LIST_HEAD(&imd->inode_list);
739 #ifdef WITH_NTFS_3G
740         if (imd->ntfs_vol) {
741                 do_ntfs_umount(imd->ntfs_vol);
742                 imd->ntfs_vol = NULL;
743         }
744 #endif
745 }
746
747 void
748 put_image_metadata(struct wim_image_metadata *imd,
749                    struct wim_lookup_table *table)
750 {
751         if (imd && --imd->refcnt == 0) {
752                 destroy_image_metadata(imd, table, true);
753                 FREE(imd);
754         }
755 }
756
757 /* Appends the specified image metadata structure to the array of image metadata
758  * for a WIM, and increments the image count. */
759 int
760 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
761 {
762         struct wim_image_metadata **imd_array;
763
764         DEBUG("Reallocating image metadata array for image_count = %u",
765               wim->hdr.image_count + 1);
766         imd_array = REALLOC(wim->image_metadata,
767                             sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
768
769         if (imd_array == NULL)
770                 return WIMLIB_ERR_NOMEM;
771         wim->image_metadata = imd_array;
772         imd_array[wim->hdr.image_count++] = imd;
773         return 0;
774 }
775
776
777 struct wim_image_metadata *
778 new_image_metadata(void)
779 {
780         struct wim_image_metadata *imd;
781
782         imd = CALLOC(1, sizeof(*imd));
783         if (imd) {
784                 imd->refcnt = 1;
785                 INIT_LIST_HEAD(&imd->inode_list);
786                 INIT_LIST_HEAD(&imd->unhashed_streams);
787                 DEBUG("Created new image metadata (refcnt=1)");
788         } else {
789                 ERROR_WITH_ERRNO("Failed to allocate new image metadata structure");
790         }
791         return imd;
792 }
793
794 struct wim_image_metadata **
795 new_image_metadata_array(unsigned num_images)
796 {
797         struct wim_image_metadata **imd_array;
798
799         DEBUG("Creating new image metadata array for %u images",
800               num_images);
801
802         imd_array = CALLOC(num_images, sizeof(imd_array[0]));
803
804         if (imd_array == NULL) {
805                 ERROR("Failed to allocate memory for %u image metadata structures",
806                       num_images);
807                 return NULL;
808         }
809         for (unsigned i = 0; i < num_images; i++) {
810                 imd_array[i] = new_image_metadata();
811                 if (imd_array[i] == NULL) {
812                         for (unsigned j = 0; j < i; j++)
813                                 put_image_metadata(imd_array[j], NULL);
814                         FREE(imd_array);
815                         return NULL;
816                 }
817         }
818         return imd_array;
819 }
820
821 /* Checksum all streams that are unhashed (other than the metadata streams),
822  * merging them into the lookup table as needed.  This is a no-op unless the
823  * library has previously used to add or mount an image using the same
824  * WIMStruct. */
825 int
826 wim_checksum_unhashed_streams(WIMStruct *wim)
827 {
828         int ret;
829
830         if (!wim_has_metadata(wim))
831                 return 0;
832         for (int i = 0; i < wim->hdr.image_count; i++) {
833                 struct wim_lookup_table_entry *lte, *tmp;
834                 struct wim_image_metadata *imd = wim->image_metadata[i];
835                 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
836                         ret = hash_unhashed_stream(lte, wim->lookup_table, NULL);
837                         if (ret)
838                                 return ret;
839                 }
840         }
841         return 0;
842 }
843
844 /*
845  * can_modify_wim - Check if a given WIM is writeable.  This is only the case if
846  * it meets the following three conditions:
847  *
848  * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
849  * 2. The WIM is not part of a spanned set.
850  * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
851  *
852  * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
853  */
854 int
855 can_modify_wim(WIMStruct *wim)
856 {
857         if (wim->filename) {
858                 if (taccess(wim->filename, W_OK)) {
859                         ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
860                         return WIMLIB_ERR_WIM_IS_READONLY;
861                 }
862         }
863         if (wim->hdr.total_parts != 1) {
864                 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
865                       wim->filename);
866                 return WIMLIB_ERR_WIM_IS_READONLY;
867         }
868         if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
869                 ERROR("Cannot modify \"%"TS"\": is marked read-only",
870                       wim->filename);
871                 return WIMLIB_ERR_WIM_IS_READONLY;
872         }
873         return 0;
874 }
875
876 /*
877  * can_delete_from_wim - Check if files or images can be deleted from a given
878  * WIM file.
879  *
880  * This theoretically should be exactly the same as can_modify_wim(), but
881  * unfortunately, due to bugs in Microsoft's software that generate incorrect
882  * reference counts for some WIM resources, we need to run expensive
883  * verifications to make sure the reference counts are correct on all WIM
884  * resources.  Otherwise we might delete a WIM resource whose reference count
885  * has fallen to 0, but is actually still referenced somewhere.
886  */
887 int
888 can_delete_from_wim(WIMStruct *wim)
889 {
890         int ret;
891
892         ret = can_modify_wim(wim);
893         if (ret)
894                 return ret;
895         if (!wim->refcnts_ok) {
896                 ret = wim_recalculate_refcnts(wim);
897                 if (ret)
898                         return ret;
899         }
900         return 0;
901 }
902
903 /* API function documented in wimlib.h  */
904 WIMLIBAPI void
905 wimlib_free(WIMStruct *wim)
906 {
907         if (wim == NULL)
908                 return;
909
910         DEBUG("Freeing WIMStruct (filename=\"%"TS"\", image_count=%u)",
911               wim->filename, wim->hdr.image_count);
912
913         while (!list_empty(&wim->subwims)) {
914                 WIMStruct *subwim;
915
916                 subwim = list_entry(wim->subwims.next, WIMStruct, subwim_node);
917                 list_del(&subwim->subwim_node);
918                 DEBUG("Freeing subwim.");
919                 wimlib_free(subwim);
920         }
921
922         if (filedes_valid(&wim->in_fd))
923                 filedes_close(&wim->in_fd);
924         if (filedes_valid(&wim->out_fd))
925                 filedes_close(&wim->out_fd);
926
927         wimlib_lzx_free_context(wim->lzx_context);
928
929         free_lookup_table(wim->lookup_table);
930
931         FREE(wim->filename);
932         free_wim_info(wim->wim_info);
933         if (wim->image_metadata) {
934                 for (unsigned i = 0; i < wim->hdr.image_count; i++)
935                         put_image_metadata(wim->image_metadata[i], NULL);
936                 FREE(wim->image_metadata);
937         }
938         FREE(wim);
939 }
940
941 static bool
942 test_locale_ctype_utf8(void)
943 {
944 #ifdef __WIN32__
945         return false;
946 #else
947         char *ctype = nl_langinfo(CODESET);
948
949         return (!strstr(ctype, "UTF-8") ||
950                 !strstr(ctype, "UTF8") ||
951                 !strstr(ctype, "utf8") ||
952                 !strstr(ctype, "utf-8"));
953 #endif
954 }
955
956 /* API function documented in wimlib.h  */
957 WIMLIBAPI int
958 wimlib_global_init(int init_flags)
959 {
960         static bool already_inited = false;
961         int ret;
962
963         if (already_inited)
964                 return 0;
965         libxml_global_init();
966         if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
967                 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
968         #ifdef WITH_NTFS_3G
969                 if (!wimlib_mbs_is_utf8)
970                         libntfs3g_global_init();
971         #endif
972         }
973 #ifdef __WIN32__
974         ret = win32_global_init(init_flags);
975         if (ret)
976                 return ret;
977 #else
978         ret = 0;
979 #endif
980         already_inited = true;
981         return ret;
982 }
983
984 /* API function documented in wimlib.h  */
985 WIMLIBAPI void
986 wimlib_global_cleanup(void)
987 {
988         libxml_global_cleanup();
989         iconv_global_cleanup();
990 #ifdef __WIN32__
991         win32_global_cleanup();
992 #endif
993 }