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