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