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