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