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