]> wimlib.net Git - wimlib/blob - src/wimlib_internal.h
Win32: Acquire/release appropriate privileges
[wimlib] / src / wimlib_internal.h
1 /*
2  * wimlib_internal.h
3  *
4  * Internal header for wimlib.
5  */
6
7 /*
8  * Copyright (C) 2012 Eric Biggers
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 #ifndef _WIMLIB_INTERNAL_H
27 #define _WIMLIB_INTERNAL_H
28
29 #include "config.h"
30 #include "util.h"
31 #include "list.h"
32 #include "wimlib.h"
33
34 #ifdef WITH_FUSE
35 #include <pthread.h>
36 #endif
37
38 #define WIMLIB_MAKEVERSION(major, minor, patch) \
39         ((major << 20) | (minor << 10) | patch)
40
41
42 #define WIMLIB_VERSION_CODE \
43                 WIMLIB_MAKEVERSION(WIMLIB_MAJOR_VERSION,\
44                                    WIMLIB_MINOR_VERSION,\
45                                    WIMLIB_PATCH_VERSION)
46
47 #define WIMLIB_GET_PATCH_VERSION(version) \
48         ((version >> 0) & ((1 << 10) - 1))
49 #define WIMLIB_GET_MINOR_VERSION(version) \
50         ((version >> 10) & ((1 << 10) - 1))
51 #define WIMLIB_GET_MAJOR_VERSION(version) \
52         ((version >> 20) & ((1 << 10) - 1))
53
54
55 struct stat;
56 struct wim_dentry;
57 struct wim_inode;
58
59 #define WIM_MAGIC_LEN  8
60 #define WIM_GID_LEN    16
61 #define WIM_UNUSED_LEN 60
62
63 /* Length of the WIM header on disk. */
64 #define WIM_HEADER_DISK_SIZE (148 + WIM_UNUSED_LEN)
65
66 /* Compressed resources in the WIM are divided into separated compressed chunks
67  * of this size. */
68 #define WIM_CHUNK_SIZE 32768
69
70 /* Version of the WIM file.  There is an older version, but we don't support it
71  * yet.  The differences between the versions are undocumented. */
72 #define WIM_VERSION 0x10d00
73
74 /* Metadata for a resource in a WIM file. */
75 struct resource_entry {
76         /* Size, in bytes, of the resource in the WIM file. */
77         u64 size  : 56;
78
79         /* Bitwise or of one or more of the WIM_RESHDR_FLAG_* flags. */
80         u64 flags : 8;
81
82         /* Offset, in bytes, of the resource in the WIM file. */
83         u64 offset;
84
85         /* Uncompressed size of the resource in the WIM file.  Is the same as
86          * @size if the resource is uncompressed. */
87         u64 original_size;
88 };
89
90 /* Flags for the `flags' field of the struct resource_entry structure. */
91
92 /* I haven't seen this flag used in any of the WIMs I have examined.  I assume
93  * it means that there are no references to the stream, so the space is free.
94  * However, even after deleting files from a WIM mounted with `imagex.exe
95  * /mountrw', I could not see this flag being used.  Either way, we don't
96  * actually use this flag for anything. */
97 #define WIM_RESHDR_FLAG_FREE            0x01
98
99 /* Indicates that the stream is a metadata resource for a WIM image. */
100 #define WIM_RESHDR_FLAG_METADATA        0x02
101
102 /* Indicates that the stream is compressed. */
103 #define WIM_RESHDR_FLAG_COMPRESSED      0x04
104
105 /* I haven't seen this flag used in any of the WIMs I have examined.  Perhaps it
106  * means that a stream could possibly be split among multiple split WIM parts.
107  * However, `imagex.exe /split' does not seem to create any WIMs like this.
108  * Either way, we don't actually use this flag for anything.  */
109 #define WIM_RESHDR_FLAG_SPANNED         0x08
110
111 /* Header at the very beginning of the WIM file. */
112 struct wim_header {
113         /* Identifies the file as WIM file. Must be exactly
114          * {'M', 'S', 'W', 'I', 'M', 0, 0, 0}  */
115         //u8  magic[WIM_MAGIC_LEN];
116
117         /* size of WIM header in bytes. */
118         //u32 hdr_size;
119
120         /* Version of the WIM file.  Microsoft provides no documentation about
121          * exactly what this field affects about the file format, other than the
122          * fact that more recent versions have a higher value. */
123         //u32 version;
124
125         /* Bitwise OR of one or more of the WIM_HDR_FLAG_* defined below. */
126         u32 flags;
127
128         /* The size of the pieces that the uncompressed files were split up into
129          * when they were compressed.  This should be the same as
130          * WIM_CHUNK_SIZE.  Microsoft incorrectly documents this as "the size of
131          * the compressed .wim file in bytes".*/
132         //u32 chunk_size;
133
134         /* A unique identifier for the WIM file. */
135         u8 guid[WIM_GID_LEN];
136
137         /* Part number of the WIM file in a spanned set. */
138         u16 part_number;
139
140         /* Total number of parts in a spanned set. */
141         u16 total_parts;
142
143         /* Number of images in the WIM file. */
144         u32 image_count;
145
146         /* Location, size, and flags of the lookup table of the WIM. */
147         struct resource_entry lookup_table_res_entry;
148
149         /* Location, size, and flags for the XML data of the WIM. */
150         struct resource_entry xml_res_entry;
151
152         /* Location, size, and flags for the boot metadata.  This means the
153          * metadata resource for the image specified by boot_idx below.  Should
154          * be zeroed out if boot_idx is 0. */
155         struct resource_entry boot_metadata_res_entry;
156
157         /* The index of the bootable image in the WIM file. If 0, there are no
158          * bootable images available. */
159         u32 boot_idx;
160
161         /* The location of the optional integrity table used to verify the
162          * integrity WIM.  Zeroed out if there is no integrity table.*/
163         struct resource_entry integrity;
164
165         /* Reserved for future disuse */
166         //u8 unused[WIM_UNUSED_LEN];
167 };
168
169 /* Flags for the `flags' field of the struct wim_header: */
170
171 /* Reserved for future use */
172 #define WIM_HDR_FLAG_RESERVED           0x00000001
173
174 /* Files and metadata in the WIM are compressed. */
175 #define WIM_HDR_FLAG_COMPRESSION        0x00000002
176
177 /* WIM is read-only (wimlib ignores this because it's pretty much pointless) */
178 #define WIM_HDR_FLAG_READONLY           0x00000004
179
180 /* Resource data specified by images in this WIM may be contained in a different
181  * WIM.  Or in other words, this WIM is part of a split WIM.  */
182 #define WIM_HDR_FLAG_SPANNED            0x00000008
183
184 /* The WIM contains resources only; no filesystem metadata.  wimlib ignores this
185  * flag, as it looks for resources in all the WIMs anyway. */
186 #define WIM_HDR_FLAG_RESOURCE_ONLY      0x00000010
187
188 /* The WIM contains metadata only.  wimlib ignores this flag.  Note that all the
189  * metadata resources for a split WIM should be in the first part. */
190 #define WIM_HDR_FLAG_METADATA_ONLY      0x00000020
191
192 /* Lock field to prevent multiple writers from writing the WIM concurrently.
193  * wimlib ignores this flag as it uses flock() to acquire a real lock on the
194  * file (if supported by the underlying filesystem). */
195 #define WIM_HDR_FLAG_WRITE_IN_PROGRESS  0x00000040
196
197 /* Reparse point fixup ???
198  * This has something to do with absolute targets of reparse points / symbolic
199  * links but I don't know what.  wimlib ignores this flag.  */
200 #define WIM_HDR_FLAG_RP_FIX             0x00000080
201
202 /* Unused, reserved flag for another compression type */
203 #define WIM_HDR_FLAG_COMPRESS_RESERVED  0x00010000
204
205 /* Resources within the WIM are compressed using "XPRESS" compression, which is
206  * a LZ77-based compression algorithm. */
207 #define WIM_HDR_FLAG_COMPRESS_XPRESS    0x00020000
208
209 /* Resources within the WIM are compressed using "LZX" compression.  This is also
210  * a LZ77-based algorithm. */
211 #define WIM_HDR_FLAG_COMPRESS_LZX       0x00040000
212
213 #ifdef WITH_NTFS_3G
214 struct _ntfs_volume;
215 #endif
216
217 /* Table of security descriptors for a WIM image. */
218 struct wim_security_data {
219         /* The total length of the security data, in bytes.  If there are no
220          * security descriptors, this field, when read from the on-disk metadata
221          * resource, may be either 8 (which is correct) or 0 (which is
222          * interpreted as 0). */
223         u32 total_length;
224
225         /* The number of security descriptors in the array @descriptors, below.
226          * It is really an unsigned int on-disk, but it must fit into an int
227          * because the security ID's are signed.  (Not like you would ever have
228          * more than a few hundred security descriptors anyway.) */
229         int32_t num_entries;
230
231         /* Array of sizes of the descriptors in the array @descriptors. */
232         u64 *sizes;
233
234         /* Array of descriptors. */
235         u8 **descriptors;
236
237         /* Keep track of how many WIMs reference this security data (used when
238          * exporting images between WIMs) */
239         u32 refcnt;
240 };
241
242 /* Metadata for a WIM image */
243 struct wim_image_metadata {
244
245         /* Pointer to the root dentry of the image. */
246         struct wim_dentry    *root_dentry;
247
248         /* Pointer to the security data of the image. */
249         struct wim_security_data *security_data;
250
251         /* Pointer to the lookup table entry for this image's metadata resource
252          */
253         struct wim_lookup_table_entry *metadata_lte;
254
255         /* Linked list of inodes of this image */
256         struct hlist_head inode_list;
257
258         /* 1 iff the dentry tree has been modified.  If this is the case, the
259          * memory for the dentry tree should not be freed when switching to a
260          * different WIM image. */
261         u8 modified : 1;
262
263         /* 1 iff this image has been mounted read-write */
264         u8 has_been_mounted_rw : 1;
265 };
266
267 /* The opaque structure exposed to the wimlib API. */
268 struct WIMStruct {
269
270         /* A pointer to the file indicated by @filename, opened for reading. */
271         FILE *fp;
272
273 #ifdef WITH_FUSE
274         /* Extra file pointers to be used by concurrent readers */
275         FILE **fp_tab;
276         size_t num_allocated_fps;
277         pthread_mutex_t fp_tab_mutex;
278 #endif
279
280         /* FILE pointer for the WIM file (if any) currently being written. */
281         FILE *out_fp;
282
283         /* The name of the WIM file (if any) that has been opened. */
284         char *filename;
285
286         /* The lookup table for the WIM file. */
287         struct wim_lookup_table *lookup_table;
288
289         /* Pointer to the XML data read from the WIM file. */
290         u8 *xml_data;
291
292         /* Information retrieved from the XML data, arranged in an orderly
293          * manner. */
294         struct wim_info *wim_info;
295
296         /* Array of the image metadata, one for each image in the WIM. */
297         struct wim_image_metadata *image_metadata;
298
299         /* The header of the WIM file. */
300         struct wim_header hdr;
301
302         /* Temporary fields */
303         union {
304                 bool write_metadata;
305                 void *private;
306         };
307 #ifdef WITH_NTFS_3G
308         struct _ntfs_volume *ntfs_vol;
309 #endif
310
311         /* The currently selected image, indexed starting at 1.  If not 0,
312          * subtract 1 from this to get the index of the current image in the
313          * image_metadata array. */
314         int current_image;
315
316         u8 deletion_occurred : 1;
317         u8 all_images_verified : 1;
318         u8 full_verification_in_progress : 1;
319         u8 wim_locked : 1;
320 };
321
322 /* Inline utility functions for WIMStructs. */
323
324 static inline struct wim_dentry *wim_root_dentry(WIMStruct *w)
325 {
326         return w->image_metadata[w->current_image - 1].root_dentry;
327 }
328
329 static inline struct wim_security_data *
330 wim_security_data(WIMStruct *w)
331 {
332         return w->image_metadata[w->current_image - 1].security_data;
333 }
334 static inline const struct wim_security_data *
335 wim_const_security_data(const WIMStruct *w)
336 {
337         return w->image_metadata[w->current_image - 1].security_data;
338 }
339
340 static inline struct wim_image_metadata *
341 wim_get_current_image_metadata(WIMStruct *w)
342 {
343         return &w->image_metadata[w->current_image - 1];
344 }
345
346 /* Nonzero if a struct resource_entry indicates a compressed resource. */
347 static inline int resource_is_compressed(const struct resource_entry *entry)
348 {
349         return (entry->flags & WIM_RESHDR_FLAG_COMPRESSED);
350 }
351
352 /* add_image.c */
353
354 struct pattern_list {
355         const char **pats;
356         size_t num_pats;
357         size_t num_allocated_pats;
358 };
359
360 struct capture_config {
361         struct pattern_list exclusion_list;
362         struct pattern_list exclusion_exception;
363         struct pattern_list compression_exclusion_list;
364         struct pattern_list alignment_list;
365         char *config_str;
366         char *prefix;
367         size_t prefix_len;
368 };
369 extern bool exclude_path(const char *path,
370                          const struct capture_config *config,
371                          bool exclude_prefix);
372 extern int add_new_dentry_tree(WIMStruct *dest_wim, struct wim_dentry *root,
373                                struct wim_security_data *sd);
374
375 /* extract_image.c */
376
377 /* Internal use only */
378 #define WIMLIB_EXTRACT_FLAG_MULTI_IMAGE         0x80000000
379 #define WIMLIB_EXTRACT_FLAG_NO_STREAMS          0x40000000
380 #define WIMLIB_EXTRACT_MASK_PUBLIC              0x3fffffff
381
382 /* hardlink.c */
383
384 extern u64 assign_inode_numbers(struct hlist_head *inode_list);
385
386 extern int dentry_tree_fix_inodes(struct wim_dentry *root,
387                                   struct hlist_head *inode_list);
388
389 /* header.c */
390 extern int read_header(FILE *fp, struct wim_header *hdr, int split_ok);
391 extern int write_header(const struct wim_header *hdr, FILE *out);
392 extern int init_header(struct wim_header *hdr, int ctype);
393
394 /* integrity.c */
395
396 #define WIM_INTEGRITY_OK 0
397 #define WIM_INTEGRITY_NOT_OK -1
398 #define WIM_INTEGRITY_NONEXISTENT -2
399
400 extern int write_integrity_table(FILE *out,
401                                  struct resource_entry *integrity_res_entry,
402                                  off_t new_lookup_table_end,
403                                  off_t old_lookup_table_end,
404                                  wimlib_progress_func_t progress_func);
405
406 extern int check_wim_integrity(WIMStruct *w,
407                                wimlib_progress_func_t progress_func);
408
409 /* join.c */
410
411 extern int new_joined_lookup_table(WIMStruct *w,
412                                    WIMStruct **additional_swms,
413                                    unsigned num_additional_swms,
414                                    struct wim_lookup_table **table_ret);
415
416 /* metadata_resource.c */
417
418 extern int read_metadata_resource(WIMStruct *w,
419                                   struct wim_image_metadata *image_metadata);
420 extern int write_metadata_resource(WIMStruct *w);
421
422 /* ntfs-apply.c */
423
424 struct apply_args {
425         WIMStruct *w;
426         const char *target;
427         int extract_flags;
428         unsigned num_lutimes_warnings;
429         struct list_head *stream_list;
430         union wimlib_progress_info progress;
431 #ifdef WITH_NTFS_3G
432         struct _ntfs_volume *vol;
433 #endif
434         wimlib_progress_func_t progress_func;
435         int (*apply_dentry)(struct wim_dentry *, void *);
436 };
437
438 extern int apply_dentry_ntfs(struct wim_dentry *dentry, void *arg);
439 extern int apply_dentry_timestamps_ntfs(struct wim_dentry *dentry, void *arg);
440
441 /* ntfs-capture.c */
442 extern int build_dentry_tree_ntfs(struct wim_dentry **root_p,
443                                   const char *device,
444                                   struct wim_lookup_table *lookup_table,
445                                   struct wim_security_data *sd,
446                                   const struct capture_config *config,
447                                   int add_image_flags,
448                                   wimlib_progress_func_t progress_func,
449                                   void *extra_arg);
450
451 /* resource.c */
452
453 #define WIMLIB_RESOURCE_FLAG_RAW                0x1
454 #define WIMLIB_RESOURCE_FLAG_MULTITHREADED      0x2
455 #define WIMLIB_RESOURCE_FLAG_RECOMPRESS         0x4
456
457 extern const u8 *get_resource_entry(const u8 *p, struct resource_entry *entry);
458 extern u8 *put_resource_entry(u8 *p, const struct resource_entry *entry);
459
460 extern int read_uncompressed_resource(FILE *fp, u64 offset, u64 size, u8 buf[]);
461
462 extern int read_wim_resource(const struct wim_lookup_table_entry *lte, u8 buf[],
463                              size_t size, u64 offset, int flags);
464
465 extern int read_full_wim_resource(const struct wim_lookup_table_entry *lte,
466                                   u8 buf[], int flags);
467
468 extern int write_wim_resource(struct wim_lookup_table_entry *lte,
469                               FILE *out_fp, int out_ctype,
470                               struct resource_entry *out_res_entry,
471                               int flags);
472
473
474 typedef int (*extract_chunk_func_t)(const u8 *, size_t, u64, void *);
475
476 extern int extract_wim_chunk_to_fd(const u8 *buf, size_t len,
477                                    u64 offset, void *arg);
478
479 extern int extract_wim_resource(const struct wim_lookup_table_entry *lte,
480                                 u64 size, extract_chunk_func_t extract_chunk,
481                                 void *extract_chunk_arg);
482
483 /*
484  * Extracts the first @size bytes of the WIM resource specified by @lte to the
485  * open file descriptor @fd.
486  *
487  * Returns 0 on success; nonzero on failure.
488  */
489 static inline int
490 extract_wim_resource_to_fd(const struct wim_lookup_table_entry *lte,
491                            int fd, u64 size)
492 {
493         return extract_wim_resource(lte, size,
494                                     extract_wim_chunk_to_fd, &fd);
495 }
496
497
498 extern int write_dentry_resources(struct wim_dentry *dentry, void *wim_p);
499 extern int copy_resource(struct wim_lookup_table_entry *lte, void *w);
500
501
502 /* security.c */
503 extern int read_security_data(const u8 metadata_resource[],
504                               u64 metadata_resource_len,
505                               struct wim_security_data **sd_p);
506 extern void print_security_data(const struct wim_security_data *sd);
507 extern u8 *write_security_data(const struct wim_security_data *sd, u8 *p);
508 extern void free_security_data(struct wim_security_data *sd);
509
510 /* symlink.c */
511 ssize_t inode_readlink(const struct wim_inode *inode, char *buf, size_t buf_len,
512                         const WIMStruct *w, int read_resource_flags);
513 extern int inode_set_symlink(struct wim_inode *inode,
514                              const char *target,
515                              struct wim_lookup_table *lookup_table,
516                              struct wim_lookup_table_entry **lte_ret);
517
518 /* verify.c */
519 extern int verify_dentry(struct wim_dentry *dentry, void *wim);
520 extern int wim_run_full_verifications(WIMStruct *w);
521 extern int verify_swm_set(WIMStruct *w,
522                           WIMStruct **additional_swms,
523                           unsigned num_additional_swms);
524
525 /* wim.c */
526 extern int select_wim_image(WIMStruct *w, int image);
527 extern int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *));
528 extern void destroy_image_metadata(struct wim_image_metadata *imd,
529                                    struct wim_lookup_table *lt);
530
531 /* win32.c */
532
533 #if defined(__CYGWIN__) || defined(__WIN32__)
534 extern int win32_read_file(const char *filename, void *handle, u64 offset,
535                            size_t size, u8 *buf);
536 extern void *win32_open_file_readonly(const void *path_utf16);
537 extern void win32_close_file(void *handle);
538 extern bool win32_acquire_privilege(const char *privilege);
539 extern bool win32_release_privilege(const char *privilege);
540 #ifdef ENABLE_ERROR_MESSAGES
541 extern void win32_error(u32 err);
542 #else
543 #define win32_error(err)
544 #endif
545 #endif
546
547
548 /* write.c */
549
550 /* Internal use only */
551 #define WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE       0x80000000
552 #define WIMLIB_WRITE_FLAG_REUSE_INTEGRITY_TABLE 0x40000000
553 #define WIMLIB_WRITE_FLAG_CHECKPOINT_AFTER_XML  0x20000000
554 #define WIMLIB_WRITE_MASK_PUBLIC                0x1fffffff
555
556 extern int begin_write(WIMStruct *w, const char *path, int write_flags);
557 extern void close_wim_writable(WIMStruct *w);
558
559 extern int finish_write(WIMStruct *w, int image, int write_flags,
560                         wimlib_progress_func_t progress_func);
561
562 #if defined(HAVE_SYS_FILE_H) && defined(HAVE_FLOCK)
563 extern int lock_wim(WIMStruct *w, FILE *fp);
564 #else
565 static inline int lock_wim(WIMStruct *w, FILE *fp)
566 {
567         return 0;
568 }
569 #endif
570
571 #endif /* _WIMLIB_INTERNAL_H */
572