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