2a3c08c10eda14a8e58453736a8095f455620336
[wimlib] / src / wim.c
1 /*
2  * wim.c - Stuff that doesn't fit into any other file
3  */
4
5 /*
6  * Copyright (C) 2012, 2013 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib/error.h"
29 #include "wimlib/dentry.h"
30 #include "wimlib/encoding.h"
31 #include "wimlib/file_io.h"
32 #include "wimlib/integrity.h"
33 #include "wimlib/lookup_table.h"
34 #include "wimlib/metadata.h"
35 #ifdef WITH_NTFS_3G
36 #  include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
37 #endif
38 #include "wimlib/security.h"
39 #include "wimlib/wim.h"
40 #include "wimlib/xml.h"
41
42 #ifdef __WIN32__
43 #  include "wimlib/win32.h" /* for realpath() replacement */
44 #endif
45
46 #include <errno.h>
47 #include <fcntl.h>
48 #ifndef __WIN32__
49 #  include <langinfo.h>
50 #endif
51 #include <limits.h>
52 #include <stdarg.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55
56 static int
57 image_print_metadata(WIMStruct *w)
58 {
59         DEBUG("Printing metadata for image %d", w->current_image);
60         print_wim_security_data(wim_security_data(w));
61         return for_dentry_in_tree(wim_root_dentry(w), print_dentry,
62                                   w->lookup_table);
63 }
64
65
66 static int
67 image_print_files(WIMStruct *w)
68 {
69         return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
70                                   NULL);
71 }
72
73 static WIMStruct *
74 new_wim_struct(void)
75 {
76         WIMStruct *w = CALLOC(1, sizeof(WIMStruct));
77         if (w) {
78                 w->in_fd = -1;
79                 w->out_fd = -1;
80         }
81         return w;
82 }
83
84 /*
85  * Calls a function on images in the WIM.  If @image is WIMLIB_ALL_IMAGES, @visitor
86  * is called on the WIM once for each image, with each image selected as the
87  * current image in turn.  If @image is a certain image, @visitor is called on
88  * the WIM only once, with that image selected.
89  */
90 int
91 for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
92 {
93         int ret;
94         int start;
95         int end;
96         int i;
97
98         if (image == WIMLIB_ALL_IMAGES) {
99                 start = 1;
100                 end = w->hdr.image_count;
101         } else if (image >= 1 && image <= w->hdr.image_count) {
102                 start = image;
103                 end = image;
104         } else {
105                 return WIMLIB_ERR_INVALID_IMAGE;
106         }
107         for (i = start; i <= end; i++) {
108                 ret = select_wim_image(w, i);
109                 if (ret != 0)
110                         return ret;
111                 ret = visitor(w);
112                 if (ret != 0)
113                         return ret;
114         }
115         return 0;
116 }
117
118 /*
119  * Creates a WIMStruct for a new WIM file.
120  */
121 WIMLIBAPI int
122 wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
123 {
124         WIMStruct *w;
125         struct wim_lookup_table *table;
126         int ret;
127
128         DEBUG("Creating new WIM with %"TS" compression.",
129               wimlib_get_compression_type_string(ctype));
130
131         /* Allocate the WIMStruct. */
132         w = new_wim_struct();
133         if (!w)
134                 return WIMLIB_ERR_NOMEM;
135
136         ret = init_header(&w->hdr, ctype);
137         if (ret != 0)
138                 goto out_free;
139
140         table = new_lookup_table(9001);
141         if (!table) {
142                 ret = WIMLIB_ERR_NOMEM;
143                 goto out_free;
144         }
145         w->lookup_table = table;
146         w->refcnts_ok = 1;
147         w->compression_type = ctype;
148         *w_ret = w;
149         return 0;
150 out_free:
151         FREE(w);
152         return ret;
153 }
154
155 int
156 select_wim_image(WIMStruct *w, int image)
157 {
158         struct wim_image_metadata *imd;
159         int ret;
160
161         DEBUG("Selecting image %d", image);
162
163         if (image == WIMLIB_NO_IMAGE) {
164                 ERROR("Invalid image: %d", WIMLIB_NO_IMAGE);
165                 return WIMLIB_ERR_INVALID_IMAGE;
166         }
167
168         if (image == w->current_image)
169                 return 0;
170
171         if (image < 1 || image > w->hdr.image_count) {
172                 ERROR("Cannot select image %d: There are only %u images",
173                       image, w->hdr.image_count);
174                 return WIMLIB_ERR_INVALID_IMAGE;
175         }
176
177         /* If a valid image is currently selected, it can be freed if it is not
178          * modified.  */
179         if (w->current_image != WIMLIB_NO_IMAGE) {
180                 imd = wim_get_current_image_metadata(w);
181                 if (!imd->modified) {
182                         wimlib_assert(list_empty(&imd->unhashed_streams));
183                         DEBUG("Freeing image %u", w->current_image);
184                         destroy_image_metadata(imd, NULL, false);
185                 }
186         }
187         w->current_image = image;
188         imd = wim_get_current_image_metadata(w);
189         if (imd->root_dentry || imd->modified) {
190                 ret = 0;
191         } else {
192                 #ifdef ENABLE_DEBUG
193                 DEBUG("Reading metadata resource specified by the following "
194                       "lookup table entry:");
195                 print_lookup_table_entry(imd->metadata_lte, stderr);
196                 #endif
197                 ret = read_metadata_resource(w, imd);
198                 if (ret)
199                         w->current_image = WIMLIB_NO_IMAGE;
200         }
201         return ret;
202 }
203
204
205 WIMLIBAPI const tchar *
206 wimlib_get_compression_type_string(int ctype)
207 {
208         switch (ctype) {
209                 case WIMLIB_COMPRESSION_TYPE_NONE:
210                         return T("None");
211                 case WIMLIB_COMPRESSION_TYPE_LZX:
212                         return T("LZX");
213                 case WIMLIB_COMPRESSION_TYPE_XPRESS:
214                         return T("XPRESS");
215                 default:
216                         return T("Invalid");
217         }
218 }
219
220 /*
221  * Returns the number of an image in the WIM file, given a string that is either
222  * the number of the image, or the name of the image.  The images are numbered
223  * starting at 1.
224  */
225 WIMLIBAPI int
226 wimlib_resolve_image(WIMStruct *w, const tchar *image_name_or_num)
227 {
228         tchar *p;
229         long image;
230         int i;
231
232         if (!image_name_or_num || !*image_name_or_num)
233                 return WIMLIB_NO_IMAGE;
234
235         if (!tstrcasecmp(image_name_or_num, T("all"))
236             || !tstrcasecmp(image_name_or_num, T("*")))
237                 return WIMLIB_ALL_IMAGES;
238         image = tstrtol(image_name_or_num, &p, 10);
239         if (p != image_name_or_num && *p == T('\0') && image > 0) {
240                 if (image > w->hdr.image_count)
241                         return WIMLIB_NO_IMAGE;
242                 return image;
243         } else {
244                 for (i = 1; i <= w->hdr.image_count; i++) {
245                         if (!tstrcmp(image_name_or_num,
246                                      wimlib_get_image_name(w, i)))
247                                 return i;
248                 }
249                 return WIMLIB_NO_IMAGE;
250         }
251 }
252
253 /* Prints some basic information about a WIM file. */
254 WIMLIBAPI void
255 wimlib_print_wim_information(const WIMStruct *w)
256 {
257         const struct wim_header *hdr;
258
259         hdr = &w->hdr;
260         tputs(T("WIM Information:"));
261         tputs(T("----------------"));
262         tprintf(T("Path:           %"TS"\n"), w->filename);
263         tfputs(T("GUID:           0x"), stdout);
264         print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
265         tputchar(T('\n'));
266         tprintf(T("Image Count:    %d\n"), hdr->image_count);
267         tprintf(T("Compression:    %"TS"\n"),
268                 wimlib_get_compression_type_string(w->compression_type));
269         tprintf(T("Part Number:    %d/%d\n"), hdr->part_number, hdr->total_parts);
270         tprintf(T("Boot Index:     %d\n"), hdr->boot_idx);
271         tprintf(T("Size:           %"PRIu64" bytes\n"),
272                 wim_info_get_total_bytes(w->wim_info));
273         tprintf(T("Integrity Info: %"TS"\n"),
274                 (w->hdr.integrity.offset != 0) ? T("yes") : T("no"));
275         tprintf(T("Relative path junction: %"TS"\n"),
276                 (hdr->flags & WIM_HDR_FLAG_RP_FIX) ? T("yes") : T("no"));
277         tputchar(T('\n'));
278 }
279
280 WIMLIBAPI bool
281 wimlib_has_integrity_table(const WIMStruct *w)
282 {
283         return w->hdr.integrity.size != 0;
284 }
285
286 WIMLIBAPI void
287 wimlib_print_available_images(const WIMStruct *w, int image)
288 {
289         int first;
290         int last;
291         int i;
292         int n;
293         if (image == WIMLIB_ALL_IMAGES) {
294                 n = tprintf(T("Available Images:\n"));
295                 first = 1;
296                 last = w->hdr.image_count;
297         } else if (image >= 1 && image <= w->hdr.image_count) {
298                 n = tprintf(T("Information for Image %d\n"), image);
299                 first = image;
300                 last = image;
301         } else {
302                 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
303                         image);
304                 return;
305         }
306         for (i = 0; i < n - 1; i++)
307                 tputchar(T('-'));
308         tputchar(T('\n'));
309         for (i = first; i <= last; i++)
310                 print_image_info(w->wim_info, i);
311 }
312
313
314 /* Prints the metadata for the specified image, which may be WIMLIB_ALL_IMAGES, but
315  * not WIMLIB_NO_IMAGE. */
316 WIMLIBAPI int
317 wimlib_print_metadata(WIMStruct *w, int image)
318 {
319         if (w->hdr.part_number != 1) {
320                 ERROR("Cannot show the metadata from part %hu of a %hu-part split WIM!",
321                        w->hdr.part_number, w->hdr.total_parts);
322                 ERROR("Select the first part of the split WIM to see the metadata.");
323                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
324         }
325         return for_image(w, image, image_print_metadata);
326 }
327
328 WIMLIBAPI int
329 wimlib_print_files(WIMStruct *w, int image)
330 {
331         if (w->hdr.part_number != 1) {
332                 ERROR("Cannot list the files from part %hu of a %hu-part split WIM!",
333                        w->hdr.part_number, w->hdr.total_parts);
334                 ERROR("Select the first part of the split WIM if you'd like to list the files.");
335                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
336         }
337         return for_image(w, image, image_print_files);
338 }
339
340 WIMLIBAPI int
341 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
342 {
343         memset(info, 0, sizeof(struct wimlib_wim_info));
344         memcpy(info->guid, wim->hdr.guid, WIMLIB_GUID_LEN);
345         info->image_count = wim->hdr.image_count;
346         info->boot_index = wim->hdr.boot_idx;
347         info->wim_version = WIM_VERSION;
348         info->chunk_size = WIM_CHUNK_SIZE;
349         info->part_number = wim->hdr.part_number;
350         info->total_parts = wim->hdr.total_parts;
351         info->compression_type = wim->compression_type;
352         if (wim->wim_info)
353                 info->total_bytes = wim->wim_info->total_bytes;
354         else
355                 info->total_bytes = 0;
356         info->has_integrity_table = wim->hdr.integrity.offset != 0;
357         info->opened_from_file = (wim->filename != NULL);
358         info->is_readonly == (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
359                              (wim->hdr.total_parts != 1) ||
360                              (wim->filename && taccess(wim->filename, W_OK));
361         info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
362         info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
363         info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
364         info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
365         info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
366         info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
367         return 0;
368 }
369
370
371 /* Deprecated */
372 WIMLIBAPI int
373 wimlib_get_boot_idx(const WIMStruct *wim)
374 {
375         struct wimlib_wim_info info;
376
377         wimlib_get_wim_info((WIMStruct*)wim, &info);
378         return info.boot_index;
379 }
380
381 /* Deprecated */
382 WIMLIBAPI int
383 wimlib_get_compression_type(const WIMStruct *wim)
384 {
385         struct wimlib_wim_info info;
386
387         wimlib_get_wim_info((WIMStruct*)wim, &info);
388         return info.compression_type;
389 }
390
391 /* Deprecated */
392 WIMLIBAPI int
393 wimlib_get_num_images(const WIMStruct *wim)
394 {
395         struct wimlib_wim_info info;
396
397         wimlib_get_wim_info((WIMStruct*)wim, &info);
398         return info.image_count;
399 }
400
401 /* Deprecated */
402 WIMLIBAPI int
403 wimlib_get_part_number(const WIMStruct *wim, int *total_parts_ret)
404 {
405         struct wimlib_wim_info info;
406
407         wimlib_get_wim_info((WIMStruct*)wim, &info);
408         if (total_parts_ret)
409                 *total_parts_ret = info.total_parts;
410         return info.part_number;
411 }
412
413 WIMLIBAPI int
414 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
415 {
416         int ret;
417
418         if (which & WIMLIB_CHANGE_READONLY_FLAG) {
419                 if (info->is_marked_readonly)
420                         wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
421                 else
422                         wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
423         }
424
425         if ((which & ~WIMLIB_CHANGE_READONLY_FLAG) == 0)
426                 return 0;
427
428         ret = can_modify_wim(wim);
429         if (ret)
430                 return ret;
431
432         if (which & WIMLIB_CHANGE_GUID)
433                 memcpy(wim->hdr.guid, info->guid, WIM_GID_LEN);
434
435         if (which & WIMLIB_CHANGE_BOOT_INDEX) {
436                 if (info->boot_index < 0 || info->boot_index > wim->hdr.image_count)
437                 {
438                         ERROR("%u is not 0 or a valid image in the WIM to mark as bootable",
439                               info->boot_index);
440                         return WIMLIB_ERR_INVALID_IMAGE;
441                 }
442                 wim->hdr.boot_idx = info->boot_index;
443         }
444
445         if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
446                 if (info->has_rpfix)
447                         wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
448                 else
449                         wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
450         }
451         return 0;
452 }
453
454 /* Deprecated */
455 WIMLIBAPI int
456 wimlib_set_boot_idx(WIMStruct *wim, int boot_idx)
457 {
458         struct wimlib_wim_info info;
459
460         info.boot_index = boot_idx;
461         return wimlib_set_wim_info(wim, &info, WIMLIB_CHANGE_BOOT_INDEX);
462 }
463
464 static int
465 do_open_wim(const tchar *filename, int *fd_ret)
466 {
467         int fd;
468
469         fd = topen(filename, O_RDONLY | O_BINARY);
470         if (fd == -1) {
471                 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
472                 return WIMLIB_ERR_OPEN;
473         }
474         *fd_ret = fd;
475         return 0;
476 }
477
478 int
479 reopen_wim(WIMStruct *w)
480 {
481         wimlib_assert(w->in_fd == -1);
482         return do_open_wim(w->filename, &w->in_fd);
483 }
484
485 int
486 close_wim(WIMStruct *w)
487 {
488         if (w->in_fd != -1) {
489                 close(w->in_fd);
490                 w->in_fd = -1;
491         }
492         return 0;
493 }
494
495 /*
496  * Begins the reading of a WIM file; opens the file and reads its header and
497  * lookup table, and optionally checks the integrity.
498  */
499 static int
500 begin_read(WIMStruct *w, const tchar *in_wim_path, int open_flags,
501            wimlib_progress_func_t progress_func)
502 {
503         int ret;
504         int xml_num_images;
505
506         DEBUG("Reading the WIM file `%"TS"'", in_wim_path);
507
508         ret = do_open_wim(in_wim_path, &w->in_fd);
509         if (ret)
510                 return ret;
511
512         /* The absolute path to the WIM is requested so that wimlib_overwrite()
513          * still works even if the process changes its working directory.  This
514          * actually happens if a WIM is mounted read-write, since the FUSE
515          * thread changes directory to "/", and it needs to be able to find the
516          * WIM file again.
517          *
518          * This will break if the full path to the WIM changes in the
519          * intervening time...
520          *
521          * Warning: in Windows native builds, realpath() calls the replacement
522          * function in win32.c.
523          */
524         w->filename = realpath(in_wim_path, NULL);
525         if (!w->filename) {
526                 ERROR_WITH_ERRNO("Failed to resolve WIM filename");
527                 if (errno == ENOMEM)
528                         return WIMLIB_ERR_NOMEM;
529                 else
530                         return WIMLIB_ERR_OPEN;
531         }
532
533         ret = read_header(w->filename, w->in_fd, &w->hdr);
534         if (ret)
535                 return ret;
536
537         if (w->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
538                 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS is set in the header of \"%"TS"\".\n"
539                         "          It may be being changed by another process, or a process\n"
540                         "          may have crashed while writing the WIM.", in_wim_path);
541         }
542
543         if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
544                 ret = can_modify_wim(w);
545                 if (ret)
546                         return ret;
547         }
548
549         if (w->hdr.total_parts != 1 && !(open_flags & WIMLIB_OPEN_FLAG_SPLIT_OK)) {
550                 ERROR("\"%"TS"\": This WIM is part %u of a %u-part WIM",
551                       in_wim_path, w->hdr.part_number, w->hdr.total_parts);
552                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
553         }
554
555         DEBUG("According to header, WIM contains %u images", w->hdr.image_count);
556
557         /* If the boot index is invalid, print a warning and set it to 0 */
558         if (w->hdr.boot_idx > w->hdr.image_count) {
559                 WARNING("In `%"TS"', image %u is marked as bootable, "
560                         "but there are only %u images in the WIM",
561                         in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
562                 w->hdr.boot_idx = 0;
563         }
564
565         /* Check and cache the compression type */
566         if (w->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
567                 if (w->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
568                         if (w->hdr.flags & WIM_HDR_FLAG_COMPRESS_XPRESS) {
569                                 ERROR("Multiple compression flags are set in \"%"TS"\"",
570                                       in_wim_path);
571                                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
572                         }
573                         w->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
574                 } else if (w->hdr.flags & WIM_HDR_FLAG_COMPRESS_XPRESS) {
575                         w->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
576                 } else {
577                         ERROR("The compression flag is set on \"%"TS"\", but "
578                               "neither the XPRESS nor LZX flag is set",
579                               in_wim_path);
580                         return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
581                 }
582         } else {
583                 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_NONE != 0);
584         }
585
586         if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
587                 ret = check_wim_integrity(w, progress_func);
588                 if (ret == WIM_INTEGRITY_NONEXISTENT) {
589                         WARNING("No integrity information for `%"TS"'; skipping "
590                                 "integrity check.", in_wim_path);
591                 } else if (ret == WIM_INTEGRITY_NOT_OK) {
592                         ERROR("WIM is not intact! (Failed integrity check)");
593                         return WIMLIB_ERR_INTEGRITY;
594                 } else if (ret != WIM_INTEGRITY_OK) {
595                         return ret;
596                 }
597         }
598
599         if (w->hdr.image_count != 0 && w->hdr.part_number == 1) {
600                 w->image_metadata = new_image_metadata_array(w->hdr.image_count);
601                 if (!w->image_metadata)
602                         return WIMLIB_ERR_NOMEM;
603         }
604
605         ret = read_lookup_table(w);
606         if (ret)
607                 return ret;
608
609         ret = read_xml_data(w->in_fd, &w->hdr.xml_res_entry, &w->wim_info);
610         if (ret)
611                 return ret;
612
613         xml_num_images = wim_info_get_num_images(w->wim_info);
614         if (xml_num_images != w->hdr.image_count) {
615                 ERROR("In the file `%"TS"', there are %u <IMAGE> elements "
616                       "in the XML data,", in_wim_path, xml_num_images);
617                 ERROR("but %u images in the WIM!  There must be exactly one "
618                       "<IMAGE> element per image.", w->hdr.image_count);
619                 return WIMLIB_ERR_IMAGE_COUNT;
620         }
621
622         DEBUG("Done beginning read of WIM file `%"TS"'.", in_wim_path);
623         return 0;
624 }
625
626 /*
627  * Opens a WIM file and creates a WIMStruct for it.
628  */
629 WIMLIBAPI int
630 wimlib_open_wim(const tchar *wim_file, int open_flags,
631                 WIMStruct **wim_ret,
632                 wimlib_progress_func_t progress_func)
633 {
634         WIMStruct *wim;
635         int ret;
636
637         ret = WIMLIB_ERR_INVALID_PARAM;
638         if (!wim_file || !wim_ret)
639                 goto out;
640
641         ret = WIMLIB_ERR_NOMEM;
642         wim = new_wim_struct();
643         if (!wim)
644                 goto out;
645
646         ret = begin_read(wim, wim_file, open_flags, progress_func);
647         if (ret)
648                 goto out_wimlib_free;
649
650         ret = 0;
651         *wim_ret = wim;
652         goto out;
653 out_wimlib_free:
654         wimlib_free(wim);
655 out:
656         return ret;
657 }
658
659 void
660 destroy_image_metadata(struct wim_image_metadata *imd,
661                        struct wim_lookup_table *table,
662                        bool free_metadata_lte)
663 {
664         free_dentry_tree(imd->root_dentry, table);
665         imd->root_dentry = NULL;
666         free_wim_security_data(imd->security_data);
667         imd->security_data = NULL;
668
669         if (free_metadata_lte) {
670                 free_lookup_table_entry(imd->metadata_lte);
671                 imd->metadata_lte = NULL;
672         }
673         if (!table) {
674                 struct wim_lookup_table_entry *lte, *tmp;
675                 list_for_each_entry_safe(lte, tmp, &imd->unhashed_streams, unhashed_list)
676                         free_lookup_table_entry(lte);
677         }
678         INIT_LIST_HEAD(&imd->unhashed_streams);
679         INIT_LIST_HEAD(&imd->inode_list);
680 #ifdef WITH_NTFS_3G
681         if (imd->ntfs_vol) {
682                 do_ntfs_umount(imd->ntfs_vol);
683                 imd->ntfs_vol = NULL;
684         }
685 #endif
686 }
687
688 void
689 put_image_metadata(struct wim_image_metadata *imd,
690                    struct wim_lookup_table *table)
691 {
692         if (imd && --imd->refcnt == 0) {
693                 destroy_image_metadata(imd, table, true);
694                 FREE(imd);
695         }
696 }
697
698 /* Appends the specified image metadata structure to the array of image metadata
699  * for a WIM, and increments the image count. */
700 int
701 append_image_metadata(WIMStruct *w, struct wim_image_metadata *imd)
702 {
703         struct wim_image_metadata **imd_array;
704
705         DEBUG("Reallocating image metadata array for image_count = %u",
706               w->hdr.image_count + 1);
707         imd_array = REALLOC(w->image_metadata,
708                             sizeof(w->image_metadata[0]) * (w->hdr.image_count + 1));
709
710         if (!imd_array)
711                 return WIMLIB_ERR_NOMEM;
712         w->image_metadata = imd_array;
713         imd_array[w->hdr.image_count++] = imd;
714         return 0;
715 }
716
717
718 struct wim_image_metadata *
719 new_image_metadata(void)
720 {
721         struct wim_image_metadata *imd;
722
723         imd = CALLOC(1, sizeof(*imd));
724         if (imd) {
725                 imd->refcnt = 1;
726                 INIT_LIST_HEAD(&imd->inode_list);
727                 INIT_LIST_HEAD(&imd->unhashed_streams);
728                 DEBUG("Created new image metadata (refcnt=1)");
729         } else {
730                 ERROR_WITH_ERRNO("Failed to allocate new image metadata structure");
731         }
732         return imd;
733 }
734
735 struct wim_image_metadata **
736 new_image_metadata_array(unsigned num_images)
737 {
738         struct wim_image_metadata **imd_array;
739
740         DEBUG("Creating new image metadata array for %u images",
741               num_images);
742
743         imd_array = CALLOC(num_images, sizeof(imd_array[0]));
744
745         if (!imd_array) {
746                 ERROR("Failed to allocate memory for %u image metadata structures",
747                       num_images);
748                 return NULL;
749         }
750         for (unsigned i = 0; i < num_images; i++) {
751                 imd_array[i] = new_image_metadata();
752                 if (!imd_array[i]) {
753                         for (unsigned j = 0; j < i; j++)
754                                 put_image_metadata(imd_array[j], NULL);
755                         FREE(imd_array);
756                         return NULL;
757                 }
758         }
759         return imd_array;
760 }
761
762 /* Checksum all streams that are unhashed (other than the metadata streams),
763  * merging them into the lookup table as needed.  This is a no-op unless the
764  * library has previously used to add or mount an image using the same
765  * WIMStruct. */
766 int
767 wim_checksum_unhashed_streams(WIMStruct *w)
768 {
769         int ret;
770         for (int i = 0; i < w->hdr.image_count; i++) {
771                 struct wim_lookup_table_entry *lte, *tmp;
772                 struct wim_image_metadata *imd = w->image_metadata[i];
773                 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
774                         ret = hash_unhashed_stream(lte, w->lookup_table, NULL);
775                         if (ret)
776                                 return ret;
777                 }
778         }
779         return 0;
780 }
781
782 /*
783  * can_modify_wim - Check if a given WIM is writeable.  This is only the case if
784  * it meets the following three conditions:
785  *
786  * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
787  * 2. The WIM is not part of a spanned set.
788  * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
789  *
790  * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
791  */
792 int
793 can_modify_wim(WIMStruct *wim)
794 {
795         if (wim->filename) {
796                 if (taccess(wim->filename, W_OK)) {
797                         ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
798                         return WIMLIB_ERR_WIM_IS_READONLY;
799                 }
800         }
801         if (wim->hdr.total_parts != 1) {
802                 ERROR("Cannot modify \"%"TS"\": is part of a spanned set",
803                       wim->filename);
804                 return WIMLIB_ERR_WIM_IS_READONLY;
805         }
806         if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
807                 ERROR("Cannot modify \"%"TS"\": is marked read-only",
808                       wim->filename);
809                 return WIMLIB_ERR_WIM_IS_READONLY;
810         }
811         return 0;
812 }
813
814 /*
815  * can_delete_from_wim - Check if files or images can be deleted from a given
816  * WIM file.
817  *
818  * This theoretically should be exactly the same as can_modify_wim(), but
819  * unfortunately, due to bugs in Microsoft's software that generate incorrect
820  * reference counts for some WIM resources, we need to run expensive
821  * verifications to make sure the reference counts are correct on all WIM
822  * resources.  Otherwise we might delete a WIM resource whose reference count
823  * has fallen to 0, but is actually still referenced somewhere.
824  */
825 int
826 can_delete_from_wim(WIMStruct *wim)
827 {
828         int ret;
829
830         ret = can_modify_wim(wim);
831         if (ret)
832                 return ret;
833         if (!wim->refcnts_ok)
834                 wim_recalculate_refcnts(wim);
835         return 0;
836 }
837
838 /* Frees the memory for the WIMStruct, including all internal memory; also
839  * closes all files associated with the WIMStruct.  */
840 WIMLIBAPI void
841 wimlib_free(WIMStruct *w)
842 {
843         DEBUG("Freeing WIMStruct");
844
845         if (!w)
846                 return;
847         if (w->in_fd != -1)
848                 close(w->in_fd);
849         if (w->out_fd != -1)
850                 close(w->out_fd);
851
852         free_lookup_table(w->lookup_table);
853
854         FREE(w->filename);
855         free_wim_info(w->wim_info);
856         if (w->image_metadata) {
857                 for (unsigned i = 0; i < w->hdr.image_count; i++)
858                         put_image_metadata(w->image_metadata[i], NULL);
859                 FREE(w->image_metadata);
860         }
861         FREE(w);
862         DEBUG("Freed WIMStruct");
863 }
864
865 static bool
866 test_locale_ctype_utf8(void)
867 {
868 #ifdef __WIN32__
869         return false;
870 #else
871         char *ctype = nl_langinfo(CODESET);
872
873         return (!strstr(ctype, "UTF-8") ||
874                 !strstr(ctype, "UTF8") ||
875                 !strstr(ctype, "utf8") ||
876                 !strstr(ctype, "utf-8"));
877 #endif
878 }
879
880 WIMLIBAPI int
881 wimlib_global_init(int init_flags)
882 {
883         libxml_global_init();
884         if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
885                 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
886         #ifdef WITH_NTFS_3G
887                 if (!wimlib_mbs_is_utf8)
888                         libntfs3g_global_init();
889         #endif
890         }
891 #ifdef __WIN32__
892         win32_global_init();
893 #endif
894         return 0;
895 }
896
897 /* Free global memory allocations.  Not strictly necessary if the process using
898  * wimlib is just about to exit (as is the case for 'imagex'). */
899 WIMLIBAPI void
900 wimlib_global_cleanup(void)
901 {
902         libxml_global_cleanup();
903         iconv_global_cleanup();
904 #ifdef __WIN32__
905         win32_global_cleanup();
906 #endif
907 }