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