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