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