]> wimlib.net Git - wimlib/blob - src/wim.c
6859d5c7d50fd9d9fe607da5e53540225e4ba19d
[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);
204                         imd->root_dentry = NULL;
205                         imd->security_data = NULL;
206                         INIT_HLIST_HEAD(&imd->inode_list);
207                 }
208         }
209         w->current_image = image;
210         imd = &w->image_metadata[image - 1];
211         if (imd->root_dentry) {
212                 ret = 0;
213         } else {
214                 #ifdef ENABLE_DEBUG
215                 DEBUG("Reading metadata resource specified by the following "
216                       "lookup table entry:");
217                 print_lookup_table_entry(imd->metadata_lte, stderr);
218                 #endif
219                 ret = read_metadata_resource(w, imd);
220                 if (ret)
221                         w->current_image = WIMLIB_NO_IMAGE;
222         }
223         return ret;
224 }
225
226
227 /* Returns the compression type of the WIM file. */
228 WIMLIBAPI int
229 wimlib_get_compression_type(const WIMStruct *w)
230 {
231         return wim_hdr_flags_compression_type(w->hdr.flags);
232 }
233
234 WIMLIBAPI const tchar *
235 wimlib_get_compression_type_string(int ctype)
236 {
237         switch (ctype) {
238                 case WIMLIB_COMPRESSION_TYPE_NONE:
239                         return T("None");
240                 case WIMLIB_COMPRESSION_TYPE_LZX:
241                         return T("LZX");
242                 case WIMLIB_COMPRESSION_TYPE_XPRESS:
243                         return T("XPRESS");
244                 default:
245                         return T("Invalid");
246         }
247 }
248
249 /*
250  * Returns the number of an image in the WIM file, given a string that is either
251  * the number of the image, or the name of the image.  The images are numbered
252  * starting at 1.
253  */
254 WIMLIBAPI int
255 wimlib_resolve_image(WIMStruct *w, const tchar *image_name_or_num)
256 {
257         tchar *p;
258         long image;
259         int i;
260
261         if (!image_name_or_num || !*image_name_or_num)
262                 return WIMLIB_NO_IMAGE;
263
264         if (!tstrcasecmp(image_name_or_num, T("all"))
265             || !tstrcasecmp(image_name_or_num, T("*")))
266                 return WIMLIB_ALL_IMAGES;
267         image = tstrtol(image_name_or_num, &p, 10);
268         if (p != image_name_or_num && *p == T('\0') && image > 0) {
269                 if (image > w->hdr.image_count)
270                         return WIMLIB_NO_IMAGE;
271                 return image;
272         } else {
273                 for (i = 1; i <= w->hdr.image_count; i++) {
274                         if (!tstrcmp(image_name_or_num,
275                                      wimlib_get_image_name(w, i)))
276                                 return i;
277                 }
278                 return WIMLIB_NO_IMAGE;
279         }
280 }
281
282 /* Prints some basic information about a WIM file. */
283 WIMLIBAPI void
284 wimlib_print_wim_information(const WIMStruct *w)
285 {
286         const struct wim_header *hdr;
287
288         hdr = &w->hdr;
289         tputs(T("WIM Information:"));
290         tputs(T("----------------"));
291         tprintf(T("Path:           %"TS"\n"), w->filename);
292         tfputs(T("GUID:           0x"), stdout);
293         print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
294         tputchar(T('\n'));
295         tprintf(T("Image Count:    %d\n"), hdr->image_count);
296         tprintf(T("Compression:    %"TS"\n"),
297                 wimlib_get_compression_type_string(wimlib_get_compression_type(w)));
298         tprintf(T("Part Number:    %d/%d\n"), hdr->part_number, hdr->total_parts);
299         tprintf(T("Boot Index:     %d\n"), hdr->boot_idx);
300         tprintf(T("Size:           %"PRIu64" bytes\n"),
301                 wim_info_get_total_bytes(w->wim_info));
302         tprintf(T("Integrity Info: %"TS"\n"),
303                 (w->hdr.integrity.offset != 0) ? T("yes") : T("no"));
304         tputchar(T('\n'));
305 }
306
307 WIMLIBAPI bool
308 wimlib_has_integrity_table(const WIMStruct *w)
309 {
310         return w->hdr.integrity.size != 0;
311 }
312
313 WIMLIBAPI void
314 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 == WIMLIB_ALL_IMAGES) {
321                 n = tprintf(T("Available Images:\n"));
322                 first = 1;
323                 last = w->hdr.image_count;
324         } else if (image >= 1 && image <= w->hdr.image_count) {
325                 n = tprintf(T("Information for Image %d\n"), image);
326                 first = image;
327                 last = image;
328         } else {
329                 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
330                         image);
331                 return;
332         }
333         for (i = 0; i < n - 1; i++)
334                 tputchar(T('-'));
335         tputchar(T('\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 WIMLIB_ALL_IMAGES, but
342  * not WIMLIB_NO_IMAGE. */
343 WIMLIBAPI int
344 wimlib_print_metadata(WIMStruct *w, int image)
345 {
346         if (w->hdr.part_number != 1) {
347                 ERROR("Cannot show the metadata from part %hu of a %hu-part split WIM!",
348                        w->hdr.part_number, w->hdr.total_parts);
349                 ERROR("Select the first part of the split WIM to see the metadata.");
350                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
351         }
352         return for_image(w, image, image_print_metadata);
353 }
354
355 WIMLIBAPI int
356 wimlib_print_files(WIMStruct *w, int image)
357 {
358         if (w->hdr.part_number != 1) {
359                 ERROR("Cannot list the files from part %hu of a %hu-part split WIM!",
360                        w->hdr.part_number, w->hdr.total_parts);
361                 ERROR("Select the first part of the split WIM if you'd like to list the files.");
362                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
363         }
364         return for_image(w, image, image_print_files);
365 }
366
367 /* Sets the index of the bootable image. */
368 WIMLIBAPI int
369 wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
370 {
371         if (w->hdr.total_parts != 1) {
372                 ERROR("Cannot modify the boot index of a split WIM!");
373                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
374         }
375         if (boot_idx < 0 || boot_idx > w->hdr.image_count)
376                 return WIMLIB_ERR_INVALID_IMAGE;
377         w->hdr.boot_idx = boot_idx;
378
379         if (boot_idx == 0) {
380                 memset(&w->hdr.boot_metadata_res_entry, 0,
381                        sizeof(struct resource_entry));
382         } else {
383                 memcpy(&w->hdr.boot_metadata_res_entry,
384                        &w->image_metadata[
385                           boot_idx - 1].metadata_lte->resource_entry,
386                        sizeof(struct resource_entry));
387         }
388
389         return 0;
390 }
391
392 WIMLIBAPI int
393 wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
394 {
395         if (total_parts_ret)
396                 *total_parts_ret = w->hdr.total_parts;
397         return w->hdr.part_number;
398 }
399
400
401 WIMLIBAPI int
402 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
412 begin_read(WIMStruct *w, const tchar *in_wim_path, int open_flags,
413            wimlib_progress_func_t progress_func)
414 {
415         int ret;
416         int xml_num_images;
417
418         DEBUG("Reading the WIM file `%"TS"'", in_wim_path);
419
420         w->fp = tfopen(in_wim_path, T("rb"));
421         if (!w->fp) {
422                 ERROR_WITH_ERRNO("Failed to open `%"TS"' for reading",
423                                  in_wim_path);
424                 return WIMLIB_ERR_OPEN;
425         }
426
427         /* The absolute path to the WIM is requested so that wimlib_overwrite()
428          * still works even if the process changes its working directory.  This
429          * actually happens if a WIM is mounted read-write, since the FUSE
430          * thread changes directory to "/", and it needs to be able to find the
431          * WIM file again.
432          *
433          * This will break if the full path to the WIM changes in the
434          * intervening time...
435          *
436          * Warning: in Windows native builds, realpath() calls the replacement
437          * function in win32.c.
438          */
439         w->filename = realpath(in_wim_path, NULL);
440         if (!w->filename) {
441                 ERROR_WITH_ERRNO("Failed to resolve WIM filename");
442                 if (errno == ENOMEM)
443                         return WIMLIB_ERR_NOMEM;
444                 else
445                         return WIMLIB_ERR_OPEN;
446         }
447
448         ret = read_header(w->fp, &w->hdr, open_flags);
449         if (ret)
450                 return ret;
451
452         DEBUG("According to header, WIM contains %u images", w->hdr.image_count);
453
454         /* If the boot index is invalid, print a warning and set it to 0 */
455         if (w->hdr.boot_idx > w->hdr.image_count) {
456                 WARNING("In `%"TS"', image %u is marked as bootable, "
457                         "but there are only %u images in the WIM",
458                         in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
459                 w->hdr.boot_idx = 0;
460         }
461
462         if (wimlib_get_compression_type(w) == WIMLIB_COMPRESSION_TYPE_INVALID) {
463                 ERROR("Invalid compression type (WIM header flags = 0x%x)",
464                       w->hdr.flags);
465                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
466         }
467
468         if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
469                 ret = check_wim_integrity(w, progress_func);
470                 if (ret == WIM_INTEGRITY_NONEXISTENT) {
471                         WARNING("No integrity information for `%"TS"'; skipping "
472                                 "integrity check.", in_wim_path);
473                 } else if (ret == WIM_INTEGRITY_NOT_OK) {
474                         ERROR("WIM is not intact! (Failed integrity check)");
475                         return WIMLIB_ERR_INTEGRITY;
476                 } else if (ret != WIM_INTEGRITY_OK) {
477                         return ret;
478                 }
479         }
480
481         if (w->hdr.image_count != 0 && w->hdr.part_number == 1) {
482                 w->image_metadata = CALLOC(w->hdr.image_count,
483                                            sizeof(struct wim_image_metadata));
484
485                 if (!w->image_metadata) {
486                         ERROR("Failed to allocate memory for %u image metadata structures",
487                               w->hdr.image_count);
488                         return WIMLIB_ERR_NOMEM;
489                 }
490         }
491
492         ret = read_lookup_table(w);
493         if (ret)
494                 return ret;
495
496         ret = read_xml_data(w->fp, &w->hdr.xml_res_entry,
497                             &w->xml_data, &w->wim_info);
498         if (ret)
499                 return ret;
500
501         xml_num_images = wim_info_get_num_images(w->wim_info);
502         if (xml_num_images != w->hdr.image_count) {
503                 ERROR("In the file `%"TS"', there are %u <IMAGE> elements "
504                       "in the XML data,", in_wim_path, xml_num_images);
505                 ERROR("but %u images in the WIM!  There must be exactly one "
506                       "<IMAGE> element per image.", w->hdr.image_count);
507                 return WIMLIB_ERR_IMAGE_COUNT;
508         }
509
510         DEBUG("Done beginning read of WIM file `%"TS"'.", in_wim_path);
511         return 0;
512 }
513
514 /*
515  * Opens a WIM file and creates a WIMStruct for it.
516  */
517 WIMLIBAPI int
518 wimlib_open_wim(const tchar *wim_file, int open_flags,
519                 WIMStruct **w_ret,
520                 wimlib_progress_func_t progress_func)
521 {
522         WIMStruct *w;
523         int ret;
524
525         if (!wim_file || !w_ret)
526                 return WIMLIB_ERR_INVALID_PARAM;
527
528         w = new_wim_struct();
529         if (!w)
530                 return WIMLIB_ERR_NOMEM;
531
532         ret = begin_read(w, wim_file, open_flags, progress_func);
533         if (ret == 0)
534                 *w_ret = w;
535         else
536                 wimlib_free(w);
537         return ret;
538 }
539
540 void
541 destroy_image_metadata(struct wim_image_metadata *imd,
542                        struct wim_lookup_table *table)
543 {
544         free_dentry_tree(imd->root_dentry, table);
545         free_security_data(imd->security_data);
546         if (table)
547                 free_lookup_table_entry(imd->metadata_lte);
548 }
549
550 /* Frees the memory for the WIMStruct, including all internal memory; also
551  * closes all files associated with the WIMStruct.  */
552 WIMLIBAPI void
553 wimlib_free(WIMStruct *w)
554 {
555         DEBUG("Freeing WIMStruct");
556
557         if (!w)
558                 return;
559         if (w->fp)
560                 fclose(w->fp);
561         if (w->out_fp)
562                 fclose(w->out_fp);
563
564 #ifdef WITH_FUSE
565         if (w->fp_tab) {
566                 for (size_t i = 0; i < w->num_allocated_fps; i++)
567                         if (w->fp_tab[i])
568                                 fclose(w->fp_tab[i]);
569                 FREE(w->fp_tab);
570         }
571         pthread_mutex_destroy(&w->fp_tab_mutex);
572 #endif
573
574         free_lookup_table(w->lookup_table);
575
576         FREE(w->filename);
577         FREE(w->xml_data);
578         free_wim_info(w->wim_info);
579         if (w->image_metadata) {
580                 for (unsigned i = 0; i < w->hdr.image_count; i++) {
581                         destroy_image_metadata(&w->image_metadata[i], NULL);
582                         free_lookup_table_entry(w->image_metadata[i].metadata_lte);
583                 }
584                 FREE(w->image_metadata);
585         }
586 #ifdef WITH_NTFS_3G
587         if (w->ntfs_vol) {
588                 DEBUG("Unmounting NTFS volume");
589                 ntfs_umount(w->ntfs_vol, FALSE);
590         }
591 #endif
592         FREE(w);
593         DEBUG("Freed WIMStruct");
594 }
595
596 static bool
597 test_locale_ctype_utf8()
598 {
599 #ifdef __WIN32__
600         return false;
601 #else
602         char *ctype = nl_langinfo(CODESET);
603
604         return (!strstr(ctype, "UTF-8") ||
605                 !strstr(ctype, "UTF8") ||
606                 !strstr(ctype, "utf8") ||
607                 !strstr(ctype, "utf-8"));
608 #endif
609 }
610
611 /* Get global memory allocations out of the way,
612  * single-threaded programs like 'imagex'. */
613 WIMLIBAPI int
614 wimlib_global_init()
615 {
616         libxml_global_init();
617 #ifdef WITH_NTFS_3G
618         libntfs3g_global_init();
619 #endif
620         wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
621 #ifdef __WIN32__
622         win32_global_init();
623 #endif
624         return 0;
625 }
626
627 /* Free global memory allocations.  Not strictly necessary if the process using
628  * wimlib is just about to exit (as is the case for 'imagex'). */
629 WIMLIBAPI void
630 wimlib_global_cleanup()
631 {
632         libxml_global_cleanup();
633         iconv_global_cleanup();
634 #ifdef __WIN32__
635         win32_global_cleanup();
636 #endif
637 }