]> wimlib.net Git - wimlib/blob - src/wim.c
read_wim_header(): Check return value of lseek()
[wimlib] / src / wim.c
1 /*
2  * wim.c - High-level code dealing with WIMStructs and images.
3  */
4
5 /*
6  * Copyright (C) 2012, 2013, 2014 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.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 #include "wimlib/version.h"
42
43 #ifdef __WIN32__
44 #  include "wimlib/win32.h" /* for realpath() replacement */
45 #endif
46
47 #include <errno.h>
48 #include <fcntl.h>
49 #ifndef __WIN32__
50 #  include <langinfo.h>
51 #endif
52 #include <stdlib.h>
53 #include <unistd.h>
54
55 static int
56 wim_default_pack_compression_type(void)
57 {
58         return WIMLIB_COMPRESSION_TYPE_LZMS;
59 }
60
61 static u32
62 wim_default_pack_chunk_size(int ctype) {
63         switch (ctype) {
64         case WIMLIB_COMPRESSION_TYPE_LZMS:
65                 return 1U << 25; /* 33554432  */
66         default:
67                 return 1U << 15; /* 32768     */
68         }
69 }
70
71 static WIMStruct *
72 new_wim_struct(void)
73 {
74         WIMStruct *wim = CALLOC(1, sizeof(WIMStruct));
75         if (!wim)
76                 return NULL;
77
78         filedes_invalidate(&wim->in_fd);
79         filedes_invalidate(&wim->out_fd);
80         wim->out_pack_compression_type = wim_default_pack_compression_type();
81         wim->out_pack_chunk_size = wim_default_pack_chunk_size(
82                                         wim->out_pack_compression_type);
83         INIT_LIST_HEAD(&wim->subwims);
84         return wim;
85 }
86
87 /* Determine if the chunk size is valid for the specified compression type.  */
88 static bool
89 wim_chunk_size_valid(u32 chunk_size, int ctype)
90 {
91         u32 order;
92
93         /* Chunk size is meaningless for uncompressed WIMs --- any value is
94          * okay.  */
95         if (ctype == WIMLIB_COMPRESSION_TYPE_NONE)
96                 return true;
97
98         /* Chunk size must be power of 2.  */
99         if (chunk_size == 0)
100                 return false;
101         order = bsr32(chunk_size);
102         if (chunk_size != 1U << order)
103                 return false;
104
105         /* Order        Size
106          * =====        ====
107          * 15           32768
108          * 16           65536
109          * 17           131072
110          * 18           262144
111          * 19           524288
112          * 20           1048576
113          * 21           2097152
114          * 22           4194304
115          * 23           8388608
116          * 24           16777216
117          * 25           33554432
118          * 26           67108864
119          */
120
121         /* See the documentation for the --chunk-size option of `wimlib-imagex
122          * capture' for information about allowed chunk sizes.  */
123         switch (ctype) {
124         case WIMLIB_COMPRESSION_TYPE_LZX:
125                 return order >= 15 && order <= 21;
126         case WIMLIB_COMPRESSION_TYPE_XPRESS:
127                 return order >= 12 && order <= 16;
128         case WIMLIB_COMPRESSION_TYPE_LZMS:
129                 return order >= 15 && order <= 30;
130         }
131         return false;
132 }
133
134 /* Return the default chunk size to use for the specified compression type.
135  *
136  * See notes above in wim_chunk_size_valid().  */
137 static u32
138 wim_default_chunk_size(int ctype)
139 {
140         switch (ctype) {
141         case WIMLIB_COMPRESSION_TYPE_LZMS:
142                 return 1U << 17; /* 131072  */
143         default:
144                 return 1U << 15; /* 32768   */
145         }
146 }
147
148 /*
149  * Calls a function on images in the WIM.  If @image is WIMLIB_ALL_IMAGES,
150  * @visitor is called on the WIM once for each image, with each image selected
151  * as the current image in turn.  If @image is a certain image, @visitor is
152  * called on the WIM only once, with that image selected.
153  */
154 int
155 for_image(WIMStruct *wim, int image, int (*visitor)(WIMStruct *))
156 {
157         int ret;
158         int start;
159         int end;
160         int i;
161
162         if (image == WIMLIB_ALL_IMAGES) {
163                 start = 1;
164                 end = wim->hdr.image_count;
165         } else if (image >= 1 && image <= wim->hdr.image_count) {
166                 start = image;
167                 end = image;
168         } else {
169                 return WIMLIB_ERR_INVALID_IMAGE;
170         }
171         for (i = start; i <= end; i++) {
172                 ret = select_wim_image(wim, i);
173                 if (ret != 0)
174                         return ret;
175                 ret = visitor(wim);
176                 if (ret != 0)
177                         return ret;
178         }
179         return 0;
180 }
181
182 /* API function documented in wimlib.h  */
183 WIMLIBAPI int
184 wimlib_create_new_wim(int ctype, WIMStruct **wim_ret)
185 {
186         WIMStruct *wim;
187         struct wim_lookup_table *table;
188         int ret;
189
190         ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
191         if (ret)
192                 return ret;
193
194         wim = new_wim_struct();
195         if (!wim)
196                 return WIMLIB_ERR_NOMEM;
197
198         ret = init_wim_header(&wim->hdr, ctype, wim_default_chunk_size(ctype));
199         if (ret)
200                 goto out_free_wim;
201
202         table = new_lookup_table(9001);
203         if (!table) {
204                 ret = WIMLIB_ERR_NOMEM;
205                 goto out_free_wim;
206         }
207         wim->lookup_table = table;
208         wim->refcnts_ok = 1;
209         wim->compression_type = ctype;
210         wim->out_compression_type = ctype;
211         wim->chunk_size = wim->hdr.chunk_size;
212         wim->out_chunk_size = wim->hdr.chunk_size;
213         *wim_ret = wim;
214         return 0;
215
216 out_free_wim:
217         FREE(wim);
218         return ret;
219 }
220
221 static void
222 destroy_image_metadata(struct wim_image_metadata *imd,
223                        struct wim_lookup_table *table,
224                        bool free_metadata_lte)
225 {
226         free_dentry_tree(imd->root_dentry, table);
227         imd->root_dentry = NULL;
228         free_wim_security_data(imd->security_data);
229         imd->security_data = NULL;
230
231         if (free_metadata_lte) {
232                 free_lookup_table_entry(imd->metadata_lte);
233                 imd->metadata_lte = NULL;
234         }
235         if (!table) {
236                 struct wim_lookup_table_entry *lte, *tmp;
237                 list_for_each_entry_safe(lte, tmp, &imd->unhashed_streams, unhashed_list)
238                         free_lookup_table_entry(lte);
239         }
240         INIT_LIST_HEAD(&imd->unhashed_streams);
241         INIT_LIST_HEAD(&imd->inode_list);
242 #ifdef WITH_NTFS_3G
243         if (imd->ntfs_vol) {
244                 do_ntfs_umount(imd->ntfs_vol);
245                 imd->ntfs_vol = NULL;
246         }
247 #endif
248 }
249
250 void
251 put_image_metadata(struct wim_image_metadata *imd,
252                    struct wim_lookup_table *table)
253 {
254         if (imd && --imd->refcnt == 0) {
255                 destroy_image_metadata(imd, table, true);
256                 FREE(imd);
257         }
258 }
259
260 /* Appends the specified image metadata structure to the array of image metadata
261  * for a WIM, and increments the image count. */
262 int
263 append_image_metadata(WIMStruct *wim, struct wim_image_metadata *imd)
264 {
265         struct wim_image_metadata **imd_array;
266
267         imd_array = REALLOC(wim->image_metadata,
268                             sizeof(wim->image_metadata[0]) * (wim->hdr.image_count + 1));
269
270         if (!imd_array)
271                 return WIMLIB_ERR_NOMEM;
272         wim->image_metadata = imd_array;
273         imd_array[wim->hdr.image_count++] = imd;
274         return 0;
275 }
276
277 struct wim_image_metadata *
278 new_image_metadata(void)
279 {
280         struct wim_image_metadata *imd;
281
282         imd = CALLOC(1, sizeof(*imd));
283         if (imd) {
284                 imd->refcnt = 1;
285                 INIT_LIST_HEAD(&imd->inode_list);
286                 INIT_LIST_HEAD(&imd->unhashed_streams);
287         }
288         return imd;
289 }
290
291 static struct wim_image_metadata **
292 new_image_metadata_array(unsigned num_images)
293 {
294         struct wim_image_metadata **imd_array;
295
296         imd_array = CALLOC(num_images, sizeof(imd_array[0]));
297
298         if (!imd_array)
299                 return NULL;
300         for (unsigned i = 0; i < num_images; i++) {
301                 imd_array[i] = new_image_metadata();
302                 if (unlikely(!imd_array[i])) {
303                         for (unsigned j = 0; j < i; j++)
304                                 put_image_metadata(imd_array[j], NULL);
305                         FREE(imd_array);
306                         return NULL;
307                 }
308         }
309         return imd_array;
310 }
311
312
313 /*
314  * Load the metadata for the specified WIM image into memory and set it
315  * as the WIMStruct's currently selected image.
316  *
317  * @wim
318  *      The WIMStruct for the WIM.
319  * @image
320  *      The 1-based index of the image in the WIM to select.
321  *
322  * On success, 0 will be returned, wim->current_image will be set to
323  * @image, and wim_get_current_image_metadata() can be used to retrieve
324  * metadata information for the image.
325  *
326  * On failure, WIMLIB_ERR_INVALID_IMAGE, WIMLIB_ERR_METADATA_NOT_FOUND,
327  * or another error code will be returned.
328  */
329 int
330 select_wim_image(WIMStruct *wim, int image)
331 {
332         struct wim_image_metadata *imd;
333         int ret;
334
335         if (image == WIMLIB_NO_IMAGE)
336                 return WIMLIB_ERR_INVALID_IMAGE;
337
338         if (image == wim->current_image)
339                 return 0;
340
341         if (image < 1 || image > wim->hdr.image_count)
342                 return WIMLIB_ERR_INVALID_IMAGE;
343
344         if (!wim_has_metadata(wim))
345                 return WIMLIB_ERR_METADATA_NOT_FOUND;
346
347         /* If a valid image is currently selected, its metadata can be freed if
348          * it has not been modified.  */
349         if (wim->current_image != WIMLIB_NO_IMAGE) {
350                 imd = wim_get_current_image_metadata(wim);
351                 if (!imd->modified) {
352                         wimlib_assert(list_empty(&imd->unhashed_streams));
353                         destroy_image_metadata(imd, NULL, false);
354                 }
355         }
356         wim->current_image = image;
357         imd = wim_get_current_image_metadata(wim);
358         if (imd->root_dentry || imd->modified) {
359                 ret = 0;
360         } else {
361                 ret = read_metadata_resource(wim, imd);
362                 if (ret)
363                         wim->current_image = WIMLIB_NO_IMAGE;
364         }
365         return ret;
366 }
367
368
369 /* API function documented in wimlib.h  */
370 WIMLIBAPI const tchar *
371 wimlib_get_compression_type_string(int ctype)
372 {
373         switch (ctype) {
374         case WIMLIB_COMPRESSION_TYPE_NONE:
375                 return T("None");
376         case WIMLIB_COMPRESSION_TYPE_XPRESS:
377                 return T("XPRESS");
378         case WIMLIB_COMPRESSION_TYPE_LZX:
379                 return T("LZX");
380         case WIMLIB_COMPRESSION_TYPE_LZMS:
381                 return T("LZMS");
382         default:
383                 return T("Invalid");
384         }
385 }
386
387 /* API function documented in wimlib.h  */
388 WIMLIBAPI int
389 wimlib_resolve_image(WIMStruct *wim, const tchar *image_name_or_num)
390 {
391         tchar *p;
392         long image;
393         int i;
394
395         if (!image_name_or_num || !*image_name_or_num)
396                 return WIMLIB_NO_IMAGE;
397
398         if (!tstrcasecmp(image_name_or_num, T("all"))
399             || !tstrcasecmp(image_name_or_num, T("*")))
400                 return WIMLIB_ALL_IMAGES;
401         image = tstrtol(image_name_or_num, &p, 10);
402         if (p != image_name_or_num && *p == T('\0') && image > 0) {
403                 if (image > wim->hdr.image_count)
404                         return WIMLIB_NO_IMAGE;
405                 return image;
406         } else {
407                 for (i = 1; i <= wim->hdr.image_count; i++) {
408                         if (!tstrcmp(image_name_or_num,
409                                      wimlib_get_image_name(wim, i)))
410                                 return i;
411                 }
412                 return WIMLIB_NO_IMAGE;
413         }
414 }
415
416 /* API function documented in wimlib.h  */
417 WIMLIBAPI void
418 wimlib_print_available_images(const WIMStruct *wim, int image)
419 {
420         int first;
421         int last;
422         int i;
423         int n;
424         if (image == WIMLIB_ALL_IMAGES) {
425                 n = tprintf(T("Available Images:\n"));
426                 first = 1;
427                 last = wim->hdr.image_count;
428         } else if (image >= 1 && image <= wim->hdr.image_count) {
429                 n = tprintf(T("Information for Image %d\n"), image);
430                 first = image;
431                 last = image;
432         } else {
433                 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
434                         image);
435                 return;
436         }
437         for (i = 0; i < n - 1; i++)
438                 tputchar(T('-'));
439         tputchar(T('\n'));
440         for (i = first; i <= last; i++)
441                 print_image_info(wim->wim_info, i);
442 }
443
444 /* API function documented in wimlib.h  */
445 WIMLIBAPI int
446 wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
447 {
448         memset(info, 0, sizeof(struct wimlib_wim_info));
449         memcpy(info->guid, wim->hdr.guid, WIMLIB_GUID_LEN);
450         info->image_count = wim->hdr.image_count;
451         info->boot_index = wim->hdr.boot_idx;
452         info->wim_version = wim->hdr.wim_version;
453         info->chunk_size = wim->chunk_size;
454         info->part_number = wim->hdr.part_number;
455         info->total_parts = wim->hdr.total_parts;
456         info->compression_type = wim->compression_type;
457         info->total_bytes = wim_info_get_total_bytes(wim->wim_info);
458         info->has_integrity_table = wim_has_integrity_table(wim);
459         info->opened_from_file = (wim->filename != NULL);
460         info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
461                              (wim->hdr.total_parts != 1) ||
462                              (wim->filename && taccess(wim->filename, W_OK));
463         info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
464         info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
465         info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
466         info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
467         info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
468         info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
469         info->pipable = wim_is_pipable(wim);
470         return 0;
471 }
472
473 /* API function documented in wimlib.h  */
474 WIMLIBAPI int
475 wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
476 {
477         int ret;
478
479         if (which & ~(WIMLIB_CHANGE_READONLY_FLAG |
480                       WIMLIB_CHANGE_GUID |
481                       WIMLIB_CHANGE_BOOT_INDEX |
482                       WIMLIB_CHANGE_RPFIX_FLAG))
483                 return WIMLIB_ERR_INVALID_PARAM;
484
485         if (which & WIMLIB_CHANGE_READONLY_FLAG) {
486                 if (info->is_marked_readonly)
487                         wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
488                 else
489                         wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
490         }
491
492         if ((which & ~WIMLIB_CHANGE_READONLY_FLAG) == 0)
493                 return 0;
494
495         ret = can_modify_wim(wim);
496         if (ret)
497                 return ret;
498
499         if (which & WIMLIB_CHANGE_GUID)
500                 memcpy(wim->hdr.guid, info->guid, WIM_GUID_LEN);
501
502         if (which & WIMLIB_CHANGE_BOOT_INDEX) {
503                 if (info->boot_index > wim->hdr.image_count)
504                         return WIMLIB_ERR_INVALID_IMAGE;
505                 wim->hdr.boot_idx = info->boot_index;
506         }
507
508         if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
509                 if (info->has_rpfix)
510                         wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
511                 else
512                         wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
513         }
514         return 0;
515 }
516
517 static int
518 set_out_ctype(int ctype, u8 *out_ctype_p)
519 {
520         switch (ctype) {
521         case WIMLIB_COMPRESSION_TYPE_NONE:
522         case WIMLIB_COMPRESSION_TYPE_LZX:
523         case WIMLIB_COMPRESSION_TYPE_XPRESS:
524         case WIMLIB_COMPRESSION_TYPE_LZMS:
525                 *out_ctype_p = ctype;
526                 return 0;
527         }
528         return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
529 }
530
531 /* API function documented in wimlib.h  */
532 WIMLIBAPI int
533 wimlib_set_output_compression_type(WIMStruct *wim, int ctype)
534 {
535         int ret = set_out_ctype(ctype, &wim->out_compression_type);
536         if (ret)
537                 return ret;
538
539         /* Reset the chunk size if it's no longer valid.  */
540         if (!wim_chunk_size_valid(wim->out_chunk_size, ctype))
541                 wim->out_chunk_size = wim_default_chunk_size(ctype);
542         return 0;
543 }
544
545 /* API function documented in wimlib.h  */
546 WIMLIBAPI int
547 wimlib_set_output_pack_compression_type(WIMStruct *wim, int ctype)
548 {
549         int ret = set_out_ctype(ctype, &wim->out_pack_compression_type);
550         if (ret)
551                 return ret;
552
553         /* Reset the chunk size if it's no longer valid.  */
554         if (!wim_chunk_size_valid(wim->out_pack_chunk_size, ctype))
555                 wim->out_pack_chunk_size = wim_default_pack_chunk_size(ctype);
556         return 0;
557 }
558
559 static int
560 set_out_chunk_size(u32 chunk_size, int ctype, u32 *out_chunk_size_p)
561 {
562         if (!wim_chunk_size_valid(chunk_size, ctype))
563                 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
564
565         *out_chunk_size_p = chunk_size;
566         return 0;
567 }
568
569 /* API function documented in wimlib.h  */
570 WIMLIBAPI int
571 wimlib_set_output_chunk_size(WIMStruct *wim, uint32_t chunk_size)
572 {
573         if (chunk_size == 0) {
574                 wim->out_chunk_size =
575                         wim_default_chunk_size(wim->out_compression_type);
576                 return 0;
577         }
578
579         return set_out_chunk_size(chunk_size,
580                                   wim->out_compression_type,
581                                   &wim->out_chunk_size);
582 }
583
584 /* API function documented in wimlib.h  */
585 WIMLIBAPI int
586 wimlib_set_output_pack_chunk_size(WIMStruct *wim, uint32_t chunk_size)
587 {
588         if (chunk_size == 0) {
589                 wim->out_pack_chunk_size =
590                         wim_default_pack_chunk_size(wim->out_pack_compression_type);
591                 return 0;
592         }
593
594         return set_out_chunk_size(chunk_size,
595                                   wim->out_pack_compression_type,
596                                   &wim->out_pack_chunk_size);
597 }
598
599 WIMLIBAPI void
600 wimlib_register_progress_function(WIMStruct *wim,
601                                   wimlib_progress_func_t progfunc,
602                                   void *progctx)
603 {
604         wim->progfunc = progfunc;
605         wim->progctx = progctx;
606 }
607
608 static int
609 open_wim_file(const tchar *filename, struct filedes *fd_ret)
610 {
611         int raw_fd;
612
613         raw_fd = topen(filename, O_RDONLY | O_BINARY);
614         if (raw_fd < 0) {
615                 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
616                 return WIMLIB_ERR_OPEN;
617         }
618         filedes_init(fd_ret, raw_fd);
619         return 0;
620 }
621
622 /*
623  * Begins the reading of a WIM file; opens the file and reads its header and
624  * lookup table, and optionally checks the integrity.
625  */
626 static int
627 begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
628 {
629         int ret;
630         int xml_num_images;
631         const tchar *wimfile;
632
633         if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
634                 wimfile = NULL;
635                 filedes_init(&wim->in_fd, *(const int*)wim_filename_or_fd);
636                 wim->in_fd.is_pipe = 1;
637         } else {
638                 wimfile = wim_filename_or_fd;
639                 ret = open_wim_file(wimfile, &wim->in_fd);
640                 if (ret)
641                         return ret;
642
643                 /* The absolute path to the WIM is requested so that
644                  * wimlib_overwrite() still works even if the process changes
645                  * its working directory.  This actually happens if a WIM is
646                  * mounted read-write, since the FUSE thread changes directory
647                  * to "/", and it needs to be able to find the WIM file again.
648                  *
649                  * This will break if the full path to the WIM changes in the
650                  * intervening time...
651                  *
652                  * Warning: in Windows native builds, realpath() calls the
653                  * replacement function in win32_replacements.c.
654                  */
655                 wim->filename = realpath(wimfile, NULL);
656                 if (!wim->filename) {
657                         ERROR_WITH_ERRNO("Failed to get full path to file "
658                                          "\"%"TS"\"", wimfile);
659                         if (errno == ENOMEM)
660                                 return WIMLIB_ERR_NOMEM;
661                         else
662                                 return WIMLIB_ERR_NO_FILENAME;
663                 }
664         }
665
666         ret = read_wim_header(wim, &wim->hdr);
667         if (ret)
668                 return ret;
669
670         if (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) {
671                 WARNING("The WIM_HDR_FLAG_WRITE_IN_PROGRESS flag is set in the header of\n"
672                         "          \"%"TS"\".  It may be being changed by another process,\n"
673                         "          or a process may have crashed while writing the WIM.",
674                         wimfile);
675         }
676
677         if (open_flags & WIMLIB_OPEN_FLAG_WRITE_ACCESS) {
678                 ret = can_modify_wim(wim);
679                 if (ret)
680                         return ret;
681         }
682
683         if ((open_flags & WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT) &&
684             (wim->hdr.total_parts != 1))
685                 return WIMLIB_ERR_IS_SPLIT_WIM;
686
687         /* If the boot index is invalid, print a warning and set it to 0 */
688         if (wim->hdr.boot_idx > wim->hdr.image_count) {
689                 WARNING("Ignoring invalid boot index.");
690                 wim->hdr.boot_idx = 0;
691         }
692
693         /* Check and cache the compression type */
694         if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESSION) {
695                 if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZX) {
696                         wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
697                 } else if (wim->hdr.flags & (WIM_HDR_FLAG_COMPRESS_XPRESS |
698                                              WIM_HDR_FLAG_COMPRESS_XPRESS_2)) {
699                         wim->compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
700                 } else if (wim->hdr.flags & WIM_HDR_FLAG_COMPRESS_LZMS) {
701                         wim->compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
702                 } else {
703                         return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
704                 }
705         } else {
706                 wim->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
707         }
708         wim->out_compression_type = wim->compression_type;
709
710         /* Check and cache the chunk size.  */
711         wim->chunk_size = wim->hdr.chunk_size;
712         wim->out_chunk_size = wim->chunk_size;
713         if (!wim_chunk_size_valid(wim->chunk_size, wim->compression_type)) {
714                 ERROR("Invalid chunk size (%"PRIu32" bytes) "
715                       "for compression type %"TS"!", wim->chunk_size,
716                       wimlib_get_compression_type_string(wim->compression_type));
717                 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
718         }
719
720         if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
721                 ret = check_wim_integrity(wim);
722                 if (ret == WIM_INTEGRITY_NONEXISTENT) {
723                         WARNING("\"%"TS"\" does not contain integrity "
724                                 "information.  Skipping integrity check.",
725                                 wimfile);
726                 } else if (ret == WIM_INTEGRITY_NOT_OK) {
727                         return WIMLIB_ERR_INTEGRITY;
728                 } else if (ret != WIM_INTEGRITY_OK) {
729                         return ret;
730                 }
731         }
732
733         if (wim->hdr.image_count != 0 && wim->hdr.part_number == 1) {
734                 wim->image_metadata = new_image_metadata_array(wim->hdr.image_count);
735                 if (!wim->image_metadata)
736                         return WIMLIB_ERR_NOMEM;
737         }
738
739         if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
740                 wim->lookup_table = new_lookup_table(9001);
741                 if (!wim->lookup_table)
742                         return WIMLIB_ERR_NOMEM;
743         } else {
744
745                 ret = read_wim_xml_data(wim);
746                 if (ret)
747                         return ret;
748
749                 xml_num_images = wim_info_get_num_images(wim->wim_info);
750                 if (xml_num_images != wim->hdr.image_count) {
751                         ERROR("The WIM's header is inconsistent with its XML data.\n"
752                               "        Please submit a bug report if you believe this "
753                               "WIM file should be considered valid.");
754                         return WIMLIB_ERR_IMAGE_COUNT;
755                 }
756
757                 ret = read_wim_lookup_table(wim);
758                 if (ret)
759                         return ret;
760         }
761         return 0;
762 }
763
764 int
765 open_wim_as_WIMStruct(const void *wim_filename_or_fd, int open_flags,
766                       WIMStruct **wim_ret,
767                       wimlib_progress_func_t progfunc, void *progctx)
768 {
769         WIMStruct *wim;
770         int ret;
771
772         ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
773         if (ret)
774                 return ret;
775
776         wim = new_wim_struct();
777         if (!wim)
778                 return WIMLIB_ERR_NOMEM;
779
780         wim->progfunc = progfunc;
781         wim->progctx = progctx;
782
783         ret = begin_read(wim, wim_filename_or_fd, open_flags);
784         if (ret) {
785                 wimlib_free(wim);
786                 return ret;
787         }
788
789         *wim_ret = wim;
790         return 0;
791 }
792
793 /* API function documented in wimlib.h  */
794 WIMLIBAPI int
795 wimlib_open_wim_with_progress(const tchar *wimfile, int open_flags,
796                               WIMStruct **wim_ret,
797                               wimlib_progress_func_t progfunc, void *progctx)
798 {
799         if (open_flags & ~(WIMLIB_OPEN_FLAG_CHECK_INTEGRITY |
800                            WIMLIB_OPEN_FLAG_ERROR_IF_SPLIT |
801                            WIMLIB_OPEN_FLAG_WRITE_ACCESS))
802                 return WIMLIB_ERR_INVALID_PARAM;
803
804         if (!wimfile || !*wimfile || !wim_ret)
805                 return WIMLIB_ERR_INVALID_PARAM;
806
807         return open_wim_as_WIMStruct(wimfile, open_flags, wim_ret,
808                                      progfunc, progctx);
809 }
810
811 /* API function documented in wimlib.h  */
812 WIMLIBAPI int
813 wimlib_open_wim(const tchar *wimfile, int open_flags, WIMStruct **wim_ret)
814 {
815         return wimlib_open_wim_with_progress(wimfile, open_flags, wim_ret,
816                                              NULL, NULL);
817 }
818
819 /* Checksum all streams that are unhashed (other than the metadata streams),
820  * merging them into the lookup table as needed.  This is a no-op unless the
821  * library has previously used to add or mount an image using the same
822  * WIMStruct. */
823 int
824 wim_checksum_unhashed_streams(WIMStruct *wim)
825 {
826         int ret;
827
828         if (!wim_has_metadata(wim))
829                 return 0;
830         for (int i = 0; i < wim->hdr.image_count; i++) {
831                 struct wim_lookup_table_entry *lte, *tmp;
832                 struct wim_image_metadata *imd = wim->image_metadata[i];
833                 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
834                         struct wim_lookup_table_entry *new_lte;
835                         ret = hash_unhashed_stream(lte, wim->lookup_table, &new_lte);
836                         if (ret)
837                                 return ret;
838                         if (new_lte != lte)
839                                 free_lookup_table_entry(lte);
840                 }
841         }
842         return 0;
843 }
844
845 /*
846  * can_modify_wim - Check if a given WIM is writeable.  This is only the case if
847  * it meets the following three conditions:
848  *
849  * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
850  * 2. The WIM is not part of a spanned set.
851  * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
852  *
853  * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
854  */
855 int
856 can_modify_wim(WIMStruct *wim)
857 {
858         if (wim->filename) {
859                 if (taccess(wim->filename, W_OK)) {
860                         ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
861                         return WIMLIB_ERR_WIM_IS_READONLY;
862                 }
863         }
864         if (wim->hdr.total_parts != 1) {
865                 ERROR("Cannot modify \"%"TS"\": is part of a split WIM",
866                       wim->filename);
867                 return WIMLIB_ERR_WIM_IS_READONLY;
868         }
869         if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
870                 ERROR("Cannot modify \"%"TS"\": is marked read-only",
871                       wim->filename);
872                 return WIMLIB_ERR_WIM_IS_READONLY;
873         }
874         return 0;
875 }
876
877 /*
878  * can_delete_from_wim - Check if files or images can be deleted from a given
879  * WIM file.
880  *
881  * This theoretically should be exactly the same as can_modify_wim(), but
882  * unfortunately, due to bugs in Microsoft's software that generate incorrect
883  * reference counts for some WIM resources, we need to run expensive
884  * verifications to make sure the reference counts are correct on all WIM
885  * resources.  Otherwise we might delete a WIM resource whose reference count
886  * has fallen to 0, but is actually still referenced somewhere.
887  */
888 int
889 can_delete_from_wim(WIMStruct *wim)
890 {
891         int ret;
892
893         ret = can_modify_wim(wim);
894         if (ret)
895                 return ret;
896         if (!wim->refcnts_ok) {
897                 ret = wim_recalculate_refcnts(wim);
898                 if (ret)
899                         return ret;
900         }
901         return 0;
902 }
903
904 /* API function documented in wimlib.h  */
905 WIMLIBAPI void
906 wimlib_free(WIMStruct *wim)
907 {
908         if (!wim)
909                 return;
910
911         while (!list_empty(&wim->subwims)) {
912                 WIMStruct *subwim;
913
914                 subwim = list_entry(wim->subwims.next, WIMStruct, subwim_node);
915                 list_del(&subwim->subwim_node);
916                 wimlib_free(subwim);
917         }
918
919         if (filedes_valid(&wim->in_fd))
920                 filedes_close(&wim->in_fd);
921         if (filedes_valid(&wim->out_fd))
922                 filedes_close(&wim->out_fd);
923
924         free_lookup_table(wim->lookup_table);
925
926         wimlib_free_decompressor(wim->decompressor);
927
928         FREE(wim->filename);
929         free_wim_info(wim->wim_info);
930         if (wim->image_metadata) {
931                 for (unsigned i = 0; i < wim->hdr.image_count; i++)
932                         put_image_metadata(wim->image_metadata[i], NULL);
933                 FREE(wim->image_metadata);
934         }
935         FREE(wim);
936 }
937
938 static bool
939 test_locale_ctype_utf8(void)
940 {
941 #ifdef __WIN32__
942         return false;
943 #else
944         char *ctype = nl_langinfo(CODESET);
945
946         return (!strstr(ctype, "UTF-8") ||
947                 !strstr(ctype, "UTF8") ||
948                 !strstr(ctype, "utf8") ||
949                 !strstr(ctype, "utf-8"));
950 #endif
951 }
952
953 /* API function documented in wimlib.h  */
954 WIMLIBAPI u32
955 wimlib_get_version(void)
956 {
957         return WIMLIB_VERSION_CODE;
958 }
959
960 static bool lib_initialized = false;
961
962 /* API function documented in wimlib.h  */
963 WIMLIBAPI int
964 wimlib_global_init(int init_flags)
965 {
966         if (lib_initialized)
967                 return 0;
968
969 #ifdef ENABLE_ERROR_MESSAGES
970         if (!wimlib_error_file)
971                 wimlib_error_file = stderr;
972 #endif
973
974         if (init_flags & ~(WIMLIB_INIT_FLAG_ASSUME_UTF8 |
975                            WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES |
976                            WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES |
977                            WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES |
978                            WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE |
979                            WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE))
980                 return WIMLIB_ERR_INVALID_PARAM;
981
982         libxml_global_init();
983         if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
984                 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
985         #ifdef WITH_NTFS_3G
986                 if (!wimlib_mbs_is_utf8)
987                         libntfs3g_global_init();
988         #endif
989         }
990 #ifdef __WIN32__
991         {
992                 int ret = win32_global_init(init_flags);
993                 if (ret)
994                         return ret;
995         }
996 #endif
997         iconv_global_init();
998         init_upcase();
999         if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE)
1000                 default_ignore_case = false;
1001         else if (init_flags & WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE)
1002                 default_ignore_case = true;
1003         lib_initialized = true;
1004         return 0;
1005 }
1006
1007 /* API function documented in wimlib.h  */
1008 WIMLIBAPI void
1009 wimlib_global_cleanup(void)
1010 {
1011         if (!lib_initialized)
1012                 return;
1013         libxml_global_cleanup();
1014         iconv_global_cleanup();
1015 #ifdef __WIN32__
1016         win32_global_cleanup();
1017 #endif
1018
1019         wimlib_set_error_file(NULL);
1020         lib_initialized = false;
1021 }