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