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