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