Make different threads use different FILE*'s
[wimlib] / src / wim.c
1 /*
2  * wim.c
3  */
4
5 /*
6  * Copyright (C) 2010 Carl Thijssen
7  * Copyright (C) 2012 Eric Biggers
8  *
9  * wimlib - Library for working with WIM files
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
13  * wimlib is free software; you can redistribute it and/or modify it under the
14  * terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 3 of the License, or (at your option)
16  * any later version.
17  *
18  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20  * A PARTICULAR PURPOSE. See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with wimlib; if not, see http://www.gnu.org/licenses/.
25  */
26
27 #include "config.h"
28 #include <limits.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31
32 #include "dentry.h"
33
34 #ifdef WITH_NTFS_3G
35 #include <ntfs-3g/volume.h>
36 #endif
37
38 #include "wimlib_internal.h"
39 #include "io.h"
40 #include "lookup_table.h"
41 #include "xml.h"
42
43
44 static int print_metadata(WIMStruct *w)
45 {
46         DEBUG("Printing metadata for image %d", w->current_image);
47         print_security_data(wim_security_data(w));
48         return for_dentry_in_tree(wim_root_dentry(w), print_dentry,
49                                   w->lookup_table);
50 }
51
52
53 static int print_files(WIMStruct *w)
54 {
55         return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
56                                   NULL);
57 }
58
59 WIMStruct *new_wim_struct()
60 {
61         WIMStruct *w = CALLOC(1, sizeof(WIMStruct));
62         if (pthread_mutex_init(&w->fp_tab_mutex, NULL) != 0) {
63                 ERROR_WITH_ERRNO("Failed to initialize mutex");
64                 FREE(w);
65                 w = NULL;
66         }
67         return w;
68
69 }
70
71 /*
72  * Calls a function on images in the WIM.  If @image is WIM_ALL_IMAGES, @visitor
73  * is called on the WIM once for each image, with each image selected as the
74  * current image in turn.  If @image is a certain image, @visitor is called on
75  * the WIM only once, with that image selected.
76  */
77 int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
78 {
79         int ret;
80         int start;
81         int end;
82         int i;
83
84         if (image == WIM_ALL_IMAGES) {
85                 start = 1;
86                 end = w->hdr.image_count;
87         } else if (image >= 1 && image <= w->hdr.image_count) {
88                 start = image;
89                 end = image;
90         } else {
91                 return WIMLIB_ERR_INVALID_IMAGE;
92         }
93         for (i = start; i <= end; i++) {
94                 ret = select_wim_image(w, i);
95                 if (ret != 0)
96                         return ret;
97                 ret = visitor(w);
98                 if (ret != 0)
99                         return ret;
100         }
101         return 0;
102 }
103
104 static int sort_image_metadata_by_position(const void *p1, const void *p2)
105 {
106         const struct image_metadata *imd1 = p1;
107         const struct image_metadata *imd2 = p2;
108         u64 offset1 = imd1->metadata_lte->resource_entry.offset;
109         u64 offset2 = imd2->metadata_lte->resource_entry.offset;
110         if (offset1 < offset2)
111                 return -1;
112         else if (offset1 > offset2)
113                 return 1;
114         else
115                 return 0;
116 }
117
118 /*
119  * If @lte points to a metadata resource, append it to the list of metadata
120  * resources in the WIMStruct.  Otherwise, do nothing.
121  */
122 static int append_metadata_resource_entry(struct lookup_table_entry *lte,
123                                           void *wim_p)
124 {
125         WIMStruct *w = wim_p;
126         int ret = 0;
127
128         if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
129                 if (w->current_image == w->hdr.image_count) {
130                         ERROR("Expected only %u images, but found more",
131                               w->hdr.image_count);
132                         ret = WIMLIB_ERR_IMAGE_COUNT;
133                 } else {
134                         DEBUG("Found metadata resource for image %u at "
135                               "offset %"PRIu64".",
136                               w->current_image + 1,
137                               lte->resource_entry.offset);
138                         w->image_metadata[
139                                 w->current_image++].metadata_lte = lte;
140                 }
141         }
142         return ret;
143 }
144
145 /* Returns the compression type given in the flags of a WIM header. */
146 int wim_hdr_flags_compression_type(int wim_hdr_flags)
147 {
148         if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESSION) {
149                 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_LZX)
150                         return WIM_COMPRESSION_TYPE_LZX;
151                 else if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_XPRESS)
152                         return WIM_COMPRESSION_TYPE_XPRESS;
153                 else
154                         return WIM_COMPRESSION_TYPE_INVALID;
155         } else {
156                 return WIM_COMPRESSION_TYPE_NONE;
157         }
158 }
159
160 /*
161  * Creates a WIMStruct for a new WIM file.
162  */
163 WIMLIBAPI int wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
164 {
165         WIMStruct *w;
166         struct lookup_table *table;
167         int ret;
168
169         DEBUG("Creating new WIM with %s compression.",
170               wimlib_get_compression_type_string(ctype));
171
172         /* Allocate the WIMStruct. */
173         w = new_wim_struct();
174         if (!w)
175                 return WIMLIB_ERR_NOMEM;
176
177         ret = init_header(&w->hdr, ctype);
178         if (ret != 0)
179                 goto out_free;
180
181         table = new_lookup_table(9001);
182         if (!table) {
183                 ret = WIMLIB_ERR_NOMEM;
184                 goto out_free;
185         }
186         w->lookup_table = table;
187         *w_ret = w;
188         return 0;
189 out_free:
190         FREE(w);
191         return ret;
192 }
193
194 WIMLIBAPI int wimlib_get_num_images(const WIMStruct *w)
195 {
196         return w->hdr.image_count;
197 }
198
199 int select_wim_image(WIMStruct *w, int image)
200 {
201         struct image_metadata *imd;
202
203         DEBUG("Selecting image %d", image);
204
205         if (image == w->current_image)
206                 return 0;
207
208         if (image < 1 || image > w->hdr.image_count) {
209                 ERROR("Cannot select image %d: There are only %u images",
210                       image, w->hdr.image_count);
211                 return WIMLIB_ERR_INVALID_IMAGE;
212         }
213
214
215         /* If a valid image is currently selected, it can be freed if it is not
216          * modified.  */
217         if (w->current_image != WIM_NO_IMAGE) {
218                 imd = wim_get_current_image_metadata(w);
219                 if (!imd->modified) {
220                         DEBUG("Freeing image %u", w->current_image);
221                         destroy_image_metadata(imd, NULL);
222                         imd->root_dentry = NULL;
223                         imd->security_data = NULL;
224                 }
225         }
226
227         w->current_image = image;
228         imd = wim_get_current_image_metadata(w);
229
230         if (imd->root_dentry) {
231                 return 0;
232         } else {
233                 #ifdef ENABLE_DEBUG
234                 DEBUG("Reading metadata resource specified by the following "
235                       "lookup table entry:");
236                 print_lookup_table_entry(imd->metadata_lte);
237                 #endif
238                 return read_metadata_resource(w, imd);
239         }
240 }
241
242
243 /* Returns the compression type of the WIM file. */
244 WIMLIBAPI int wimlib_get_compression_type(const WIMStruct *w)
245 {
246         return wim_hdr_flags_compression_type(w->hdr.flags);
247 }
248
249 WIMLIBAPI const char *wimlib_get_compression_type_string(int ctype)
250 {
251         switch (ctype) {
252                 case WIM_COMPRESSION_TYPE_NONE:
253                         return "None";
254                 case WIM_COMPRESSION_TYPE_LZX:
255                         return "LZX";
256                 case WIM_COMPRESSION_TYPE_XPRESS:
257                         return "XPRESS";
258                 default:
259                         return "Invalid";
260         }
261 }
262
263 /*
264  * Returns the number of an image in the WIM file, given a string that is either
265  * the number of the image, or the name of the image.  The images are numbered
266  * starting at 1.
267  */
268 WIMLIBAPI int wimlib_resolve_image(WIMStruct *w, const char *image_name_or_num)
269 {
270         char *p;
271         int image;
272         int i;
273
274         if (!image_name_or_num)
275                 return WIM_NO_IMAGE;
276
277         if (strcmp(image_name_or_num, "all") == 0
278             || strcmp(image_name_or_num, "*") == 0)
279                 return WIM_ALL_IMAGES;
280         image = strtol(image_name_or_num, &p, 10);
281         if (p != image_name_or_num && *p == '\0') {
282                 if (image < 1 || image > w->hdr.image_count)
283                         return WIM_NO_IMAGE;
284                 return image;
285         } else {
286                 for (i = 1; i <= w->hdr.image_count; i++) {
287                         if (strcmp(image_name_or_num,
288                                    wimlib_get_image_name(w, i)) == 0)
289                                 return i;
290                 }
291                 return WIM_NO_IMAGE;
292         }
293 }
294
295
296 /* Prints some basic information about a WIM file. */
297 WIMLIBAPI void wimlib_print_wim_information(const WIMStruct *w)
298 {
299         const struct wim_header *hdr;
300
301         hdr = &w->hdr;
302         puts("WIM Information:");
303         puts("----------------");
304         printf("Path:           %s\n", w->filename);
305         fputs ("GUID:           0x", stdout);
306         print_byte_field(hdr->guid, WIM_GID_LEN);
307         putchar('\n');
308         printf("Image Count:    %d\n", hdr->image_count);
309         printf("Compression:    %s\n", wimlib_get_compression_type_string(
310                                                 wimlib_get_compression_type(w)));
311         printf("Part Number:    %d/%d\n", hdr->part_number, hdr->total_parts);
312         printf("Boot Index:     %d\n", hdr->boot_idx);
313         printf("Size:           %"PRIu64" bytes\n",
314                                 wim_info_get_total_bytes(w->wim_info));
315         printf("Integrity Info: %s\n", (w->hdr.integrity.size != 0) ? "yes" : "no");
316         putchar('\n');
317 }
318
319 WIMLIBAPI bool wimlib_has_integrity_table(const WIMStruct *w)
320 {
321         return w->hdr.integrity.size != 0;
322 }
323
324 WIMLIBAPI void wimlib_print_available_images(const WIMStruct *w, int image)
325 {
326         int first;
327         int last;
328         int i;
329         int n;
330         if (image == WIM_ALL_IMAGES) {
331                 n = printf("Available Images:\n");
332                 first = 1;
333                 last = w->hdr.image_count;
334         } else if (image >= 1 && image <= w->hdr.image_count) {
335                 n = printf("Information for Image %d\n", image);
336                 first = image;
337                 last = image;
338         } else {
339                 printf("wimlib_print_available_images(): Invalid image %d",
340                        image);
341                 return;
342         }
343         for (i = 0; i < n - 1; i++)
344                 putchar('-');
345         putchar('\n');
346         for (i = first; i <= last; i++)
347                 print_image_info(w->wim_info, i);
348 }
349
350
351 /* Prints the metadata for the specified image, which may be WIM_ALL_IMAGES, but
352  * not WIM_NO_IMAGE. */
353 WIMLIBAPI int wimlib_print_metadata(WIMStruct *w, int image)
354 {
355         if (!w)
356                 return WIMLIB_ERR_INVALID_PARAM;
357         if (w->hdr.part_number != 1) {
358                 ERROR("Cannot show the metadata from part %hu of a %hu-part split WIM!",
359                        w->hdr.part_number, w->hdr.total_parts);
360                 ERROR("Select the first part of the split WIM to see the metadata.");
361                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
362         }
363         return for_image(w, image, print_metadata);
364 }
365
366 WIMLIBAPI int wimlib_print_files(WIMStruct *w, int image)
367 {
368         if (!w)
369                 return WIMLIB_ERR_INVALID_PARAM;
370         if (w->hdr.part_number != 1) {
371                 ERROR("Cannot list the files from part %hu of a %hu-part split WIM!",
372                        w->hdr.part_number, w->hdr.total_parts);
373                 ERROR("Select the first part of the split WIM if you'd like to list the files.");
374                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
375         }
376         return for_image(w, image, print_files);
377 }
378
379 /* Sets the index of the bootable image. */
380 WIMLIBAPI int wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
381 {
382         if (!w)
383                 return WIMLIB_ERR_INVALID_PARAM;
384         if (w->hdr.total_parts != 1) {
385                 ERROR("Cannot modify the boot index of a split WIM!");
386                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
387         }
388         if (boot_idx < 0 || boot_idx > w->hdr.image_count)
389                 return WIMLIB_ERR_INVALID_IMAGE;
390         w->hdr.boot_idx = boot_idx;
391         return 0;
392 }
393
394 WIMLIBAPI int wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
395 {
396         if (total_parts_ret)
397                 *total_parts_ret = w->hdr.total_parts;
398         return w->hdr.part_number;
399 }
400
401
402 WIMLIBAPI int wimlib_get_boot_idx(const WIMStruct *w)
403 {
404         return w->hdr.boot_idx;
405 }
406
407 /*
408  * Begins the reading of a WIM file; opens the file and reads its header and
409  * lookup table, and optionally checks the integrity.
410  */
411 static int begin_read(WIMStruct *w, const char *in_wim_path, int open_flags)
412 {
413         int ret;
414         uint xml_num_images;
415
416         DEBUG("Reading the WIM file `%s'", in_wim_path);
417
418         w->filename = realpath(in_wim_path, NULL);
419         if (!w->filename) {
420                 ERROR("Failed to allocate memory for WIM filename");
421                 return WIMLIB_ERR_NOMEM;
422         }
423
424         w->fp = fopen(in_wim_path, "rb");
425
426         if (!w->fp) {
427                 ERROR_WITH_ERRNO("Failed to open the file `%s' for reading",
428                                  in_wim_path);
429                 return WIMLIB_ERR_OPEN;
430         }
431
432         ret = read_header(w->fp, &w->hdr, open_flags);
433         if (ret != 0)
434                 return ret;
435
436         DEBUG("Wim file contains %u images", w->hdr.image_count);
437
438         /* If the boot index is invalid, print a warning and set it to 0 */
439         if (w->hdr.boot_idx > w->hdr.image_count) {
440                 WARNING("In `%s', image %u is marked as bootable, "
441                         "but there are only %u images in the WIM",
442                         in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
443                 w->hdr.boot_idx = 0;
444         }
445
446         if (wimlib_get_compression_type(w) == WIM_COMPRESSION_TYPE_INVALID) {
447                 ERROR("Invalid compression type (WIM header flags = %x)",
448                       w->hdr.flags);
449                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
450         }
451
452
453         if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
454                 int integrity_status;
455                 ret = check_wim_integrity(w,
456                                           open_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS,
457                                           &integrity_status);
458                 if (ret != 0)
459                         return ret;
460                 if (integrity_status == WIM_INTEGRITY_NONEXISTENT) {
461                         WARNING("No integrity information for `%s'; skipping "
462                                 "integrity check.", w->filename);
463                 } else if (integrity_status == WIM_INTEGRITY_NOT_OK) {
464                         ERROR("WIM is not intact! (Failed integrity check)");
465                         return WIMLIB_ERR_INTEGRITY;
466                 }
467         }
468
469         if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
470                 ERROR("Didn't expect a compressed lookup table!");
471                 ERROR("Ask the author to implement support for this.");
472                 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
473         }
474
475         ret = read_lookup_table(w);
476         if (ret != 0)
477                 return ret;
478
479         w->image_metadata = CALLOC(w->hdr.image_count,
480                                    sizeof(struct image_metadata));
481
482         if (!w->image_metadata) {
483                 ERROR("Failed to allocate memory for %u metadata structures",
484                       w->hdr.image_count);
485                 return WIMLIB_ERR_NOMEM;
486         }
487         w->current_image = 0;
488
489         DEBUG("Looking for metadata resources in the lookup table.");
490
491         /* Find the images in the WIM by searching the lookup table. */
492         ret = for_lookup_table_entry(w->lookup_table,
493                                      append_metadata_resource_entry, w);
494
495         if (ret != 0)
496                 return ret;
497
498         /* Make sure all the expected images were found.  (We already have
499          * returned false if *extra* images were found) */
500         if (w->current_image != w->hdr.image_count &&
501             w->hdr.part_number == 1)
502         {
503                 ERROR("Only found %u images in WIM, but expected %u",
504                       w->current_image, w->hdr.image_count);
505                 return WIMLIB_ERR_IMAGE_COUNT;
506         }
507
508
509         /* Sort images by the position of their metadata resources.  I'm
510          * assuming that is what determines the other of the images in the WIM
511          * file, rather than their order in the lookup table, which is random
512          * because of hashing. */
513         qsort(w->image_metadata, w->current_image,
514               sizeof(struct image_metadata), sort_image_metadata_by_position);
515
516         w->current_image = WIM_NO_IMAGE;
517
518         /* Read the XML data. */
519         ret = read_xml_data(w->fp, &w->hdr.xml_res_entry,
520                             &w->xml_data, &w->wim_info);
521
522         if (ret != 0) {
523                 ERROR("Missing or invalid XML data");
524                 return ret;
525         }
526
527         xml_num_images = wim_info_get_num_images(w->wim_info);
528         if (xml_num_images != w->hdr.image_count) {
529                 ERROR("In the file `%s', there are %u <IMAGE> elements "
530                       "in the XML data,", in_wim_path, xml_num_images);
531                 ERROR("but %u images in the WIM!  There must be exactly one "
532                       "<IMAGE> element per image.", w->hdr.image_count);
533                 return WIMLIB_ERR_IMAGE_COUNT;
534         }
535
536         DEBUG("Done beginning read of WIM file `%s'.", in_wim_path);
537         return 0;
538 }
539
540
541 /*
542  * Opens a WIM file and creates a WIMStruct for it.
543  */
544 WIMLIBAPI int wimlib_open_wim(const char *wim_file, int open_flags,
545                               WIMStruct **w_ret)
546 {
547         WIMStruct *w;
548         int ret;
549
550         DEBUG("wim_file = `%s', open_flags = %#x", wim_file, open_flags);
551         w = new_wim_struct();
552         if (!w) {
553                 ERROR("Failed to allocate memory for WIMStruct");
554                 return WIMLIB_ERR_NOMEM;
555         }
556
557         ret = begin_read(w, wim_file, open_flags);
558         if (ret == 0) {
559                 *w_ret = w;
560         } else {
561                 DEBUG("Could not begin reading the WIM file `%s'", wim_file);
562                 wimlib_free(w);
563         }
564         return ret;
565 }
566
567 /* Frees the memory for the WIMStruct, including all internal memory; also
568  * closes all files associated with the WIMStruct.  */
569 WIMLIBAPI void wimlib_free(WIMStruct *w)
570 {
571         DEBUG2("Freeing WIMStruct");
572
573         if (!w)
574                 return;
575         if (w->fp)
576                 fclose(w->fp);
577         if (w->out_fp)
578                 fclose(w->out_fp);
579
580         if (w->fp_tab) {
581                 for (size_t i = 0; i < w->num_allocated_fps; i++)
582                         if (w->fp_tab[i])
583                                 fclose(w->fp_tab[i]);
584                 FREE(w->fp_tab);
585         }
586         pthread_mutex_destroy(&w->fp_tab_mutex);
587
588         free_lookup_table(w->lookup_table);
589
590         FREE(w->filename);
591         FREE(w->xml_data);
592         free_wim_info(w->wim_info);
593         if (w->image_metadata) {
594                 for (uint i = 0; i < w->hdr.image_count; i++)
595                         destroy_image_metadata(&w->image_metadata[i], NULL);
596                 FREE(w->image_metadata);
597         }
598 #ifdef WITH_NTFS_3G
599         if (w->ntfs_vol) {
600                 DEBUG("Unmounting NTFS volume");
601                 ntfs_umount(w->ntfs_vol, FALSE);
602         }
603 #endif
604         FREE(w);
605 }
606