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