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