]> wimlib.net Git - wimlib/blob - include/wimlib/dentry.h
dfb5a95be76b5301f6ea31c41b235b44bc9be28b
[wimlib] / include / wimlib / dentry.h
1 #ifndef _WIMLIB_DENTRY_H
2 #define _WIMLIB_DENTRY_H
3
4 #include "wimlib/compiler.h"
5 #include "wimlib/list.h"
6 #include "wimlib/rbtree.h"
7 #include "wimlib/sha1.h"
8 #include "wimlib/types.h"
9
10 #include <string.h>
11 #include <sys/types.h> /* uid_t, gid_t */
12
13 #ifdef WITH_FUSE
14 #  include <pthread.h>
15 #endif
16
17 struct wim_lookup_table;
18 struct wim_lookup_table_entry;
19 struct wimfs_fd;
20 struct wim_inode;
21 struct wim_security_data;
22
23 /* Size of the struct wim_dentry up to and including the file_name_len. */
24 #define WIM_DENTRY_DISK_SIZE    102
25
26 /*
27  * Reparse tags documented at
28  * http://msdn.microsoft.com/en-us/library/dd541667(v=prot.10).aspx
29  */
30 #define WIM_IO_REPARSE_TAG_RESERVED_ZERO        0x00000000
31 #define WIM_IO_REPARSE_TAG_RESERVED_ONE         0x00000001
32 #define WIM_IO_REPARSE_TAG_MOUNT_POINT          0xA0000003
33 #define WIM_IO_REPARSE_TAG_HSM                  0xC0000004
34 #define WIM_IO_REPARSE_TAG_HSM2                 0x80000006
35 #define WIM_IO_REPARSE_TAG_DRIVER_EXTENDER      0x80000005
36 #define WIM_IO_REPARSE_TAG_SIS                  0x80000007
37 #define WIM_IO_REPARSE_TAG_DFS                  0x8000000A
38 #define WIM_IO_REPARSE_TAG_DFSR                 0x80000012
39 #define WIM_IO_REPARSE_TAG_FILTER_MANAGER       0x8000000B
40 #define WIM_IO_REPARSE_TAG_SYMLINK              0xA000000C
41
42 #define FILE_ATTRIBUTE_READONLY            0x00000001
43 #define FILE_ATTRIBUTE_HIDDEN              0x00000002
44 #define FILE_ATTRIBUTE_SYSTEM              0x00000004
45 #define FILE_ATTRIBUTE_DIRECTORY           0x00000010
46 #define FILE_ATTRIBUTE_ARCHIVE             0x00000020
47 #define FILE_ATTRIBUTE_DEVICE              0x00000040
48 #define FILE_ATTRIBUTE_NORMAL              0x00000080
49 #define FILE_ATTRIBUTE_TEMPORARY           0x00000100
50 #define FILE_ATTRIBUTE_SPARSE_FILE         0x00000200
51 #define FILE_ATTRIBUTE_REPARSE_POINT       0x00000400
52 #define FILE_ATTRIBUTE_COMPRESSED          0x00000800
53 #define FILE_ATTRIBUTE_OFFLINE             0x00001000
54 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
55 #define FILE_ATTRIBUTE_ENCRYPTED           0x00004000
56 #define FILE_ATTRIBUTE_VIRTUAL             0x00010000
57
58
59 /* Alternate data stream entry.
60  *
61  * We read this from disk in the read_ads_entries() function; see that function
62  * for more explanation. */
63 struct wim_ads_entry {
64         union {
65                 /* SHA-1 message digest of stream contents */
66                 u8 hash[SHA1_HASH_SIZE];
67
68                 /* The corresponding lookup table entry (only for resolved
69                  * streams) */
70                 struct wim_lookup_table_entry *lte;
71         };
72
73         /* Length of UTF16-encoded stream name, in bytes, not including the
74          * terminating null character; or 0 if the stream is unnamed. */
75         u16 stream_name_nbytes;
76
77         /* Number to identify an alternate data stream even after it's possibly
78          * been moved or renamed. */
79         u32 stream_id;
80
81         /* Stream name (UTF-16LE), null-terminated, or NULL if the stream is
82          * unnamed.  */
83         utf16lechar *stream_name;
84
85         /* Reserved field.  We read it into memory so we can write it out
86          * unchanged. */
87         u64 reserved;
88 };
89
90
91 static inline bool
92 ads_entries_have_same_name(const struct wim_ads_entry *entry_1,
93                            const struct wim_ads_entry *entry_2)
94 {
95         return entry_1->stream_name_nbytes == entry_2->stream_name_nbytes &&
96                memcmp(entry_1->stream_name, entry_2->stream_name,
97                       entry_1->stream_name_nbytes) == 0;
98 }
99
100 /*
101  * In-memory structure for a WIM directory entry (dentry).  There is a directory
102  * tree for each image in the WIM.
103  *
104  * Note that this is a directory entry and not an inode.  Since NTFS allows hard
105  * links, it's possible for a NTFS inode to correspond to multiple WIM dentries.
106  * The hard link group ID field of the on-disk WIM dentry tells us the number of
107  * the NTFS inode that the dentry corresponds to (and this gets placed in
108  * d_inode->i_ino).
109  *
110  * Unfortunately, WIM files do not have an analogue to an inode; instead certain
111  * information, such as file attributes, the security descriptor, and file
112  * streams is replicated in each hard-linked dentry, even though this
113  * information really is associated with an inode.  In-memory, we fix up this
114  * flaw by allocating a `struct wim_inode' for each dentry that contains some of
115  * this duplicated information, then combining the inodes for each hard link
116  * group together.
117  *
118  * Confusingly, it's possible for stream information to be missing from a dentry
119  * in a hard link set, in which case the stream information needs to be gotten
120  * from one of the other dentries in the hard link set.  In addition, it is
121  * possible for dentries to have inconsistent security IDs, file attributes, or
122  * file streams when they share the same hard link ID (don't even ask.  I hope
123  * that Microsoft may have fixed this problem, since I've only noticed it in the
124  * 'install.wim' for Windows 7).  For those dentries, we have to use the
125  * conflicting fields to split up the hard link groups.  (See
126  * dentry_tree_fix_inodes() in hardlink.c).
127  */
128 struct wim_dentry {
129         /* The inode for this dentry */
130         struct wim_inode *d_inode;
131
132         /* Node for the parent's red-black tree of child dentries, sorted by
133          * case sensitive long name. */
134         struct rb_node rb_node;
135
136 #ifdef __WIN32__
137         /* Node for the parent's red-black tree of child dentries, sorted by
138          * case insensitive long name. */
139         struct rb_node rb_node_case_insensitive;
140
141         /* List of dentries in a directory that have different case sensitive
142          * long names but share the same case insensitive long name */
143         struct list_head case_insensitive_conflict_list;
144 #endif
145
146         /* Length of UTF-16LE encoded short filename, in bytes, not including
147          * the terminating zero wide-character. */
148         u16 short_name_nbytes;
149
150         /* Length of UTF-16LE encoded "long" file name, in bytes, not including
151          * the terminating null character. */
152         u16 file_name_nbytes;
153
154         /* Length of full path name encoded using "tchars", in bytes, not
155          * including the terminating null character. */
156         u32 full_path_nbytes;
157
158         /* Does this dentry need to be extracted? */
159         u8 needs_extraction : 1;
160
161         u8 not_extracted : 1;
162
163         /* Only used during NTFS capture */
164         u8 is_win32_name : 1;
165
166         /* Set to 1 if an inode has multiple DOS names. */
167         u8 dos_name_invalid : 1;
168
169         /* Temporary list */
170         struct list_head tmp_list;
171
172         /* List of dentries in the inode (hard link set)  */
173         struct list_head d_alias;
174
175         /* The parent of this directory entry. */
176         struct wim_dentry *parent;
177
178         /*
179          * Size of directory entry on disk, in bytes.  Typical size is around
180          * 104 to 120 bytes.
181          *
182          * It is possible for the length field to be 0.  This situation, which
183          * is undocumented, indicates the end of a list of sibling nodes in a
184          * directory.  It also means the real length is 8, because the dentry
185          * included only the length field, but that takes up 8 bytes.
186          *
187          * The length here includes the base directory entry on disk as well as
188          * the long and short filenames.  It does NOT include any alternate
189          * stream entries that may follow the directory entry, even though the
190          * size of those needs to be considered.  The length SHOULD be 8-byte
191          * aligned, although we don't require it to be.  We do require the
192          * length to be large enough to hold the file name(s) of the dentry;
193          * additionally, a warning is issued if this field is larger than the
194          * aligned size.
195          */
196         u64 length;
197
198         /* The offset, from the start of the uncompressed WIM metadata resource
199          * for this image, of this dentry's child dentries.  0 if the directory
200          * entry has no children, which is the case for regular files or reparse
201          * points. */
202         u64 subdir_offset;
203
204         u64 d_unused_1;
205         u64 d_unused_2;
206
207         /* Pointer to the UTF-16LE short filename (malloc()ed buffer) */
208         utf16lechar *short_name;
209
210         /* Pointer to the UTF-16LE filename (malloc()ed buffer). */
211         utf16lechar *file_name;
212
213         /* Full path of this dentry in the WIM */
214         tchar *_full_path;
215
216         /* Actual name to extract this dentry as. */
217         tchar *extraction_name;
218         size_t extraction_name_nchars;
219
220         /* List head for building a list of dentries that contain a certain
221          * stream. */
222         struct list_head extraction_stream_list;
223 };
224
225 #define rbnode_dentry(node) container_of(node, struct wim_dentry, rb_node)
226
227 /*
228  * WIM inode.
229  *
230  * As mentioned above, in the WIM file that is no on-disk analogue of a real
231  * inode, as most of these fields are duplicated in the dentries.
232  */
233 struct wim_inode {
234         /* Timestamps for the inode.  The timestamps are the number of
235          * 100-nanosecond intervals that have elapsed since 12:00 A.M., January
236          * 1st, 1601, UTC.  This is the same format used in NTFS inodes. */
237         u64 i_creation_time;
238         u64 i_last_access_time;
239         u64 i_last_write_time;
240
241         /* The file attributes associated with this inode.  This is a bitwise OR
242          * of the FILE_ATTRIBUTE_* flags. */
243         u32 i_attributes;
244
245         /* The index of the security descriptor in the WIM image's table of
246          * security descriptors that contains this file's security information.
247          * If -1, no security information exists for this file.  */
248         int32_t i_security_id;
249
250         /* %true iff the inode's lookup table entries has been resolved (i.e.
251          * the @lte field is valid, but the @hash field is not valid)
252          *
253          * (This is not an on-disk field.) */
254         u8 i_resolved : 1;
255
256         u8 i_visited : 1;
257
258         /* Used only in NTFS-mode extraction */
259         u8 i_dos_name_extracted : 1;
260
261         /* Set to 0 if reparse point fixups have been done.  Otherwise set to 1.
262          *
263          * Note: this actually may reflect the SYMBOLIC_LINK_RELATIVE flag.  */
264         u16 i_not_rpfixed;
265
266         /* Number of alternate data streams associated with this inode */
267         u16 i_num_ads;
268
269         /* Unused/unknown fields that we just read into memory so we can
270          * re-write them unchanged.  */
271         u32 i_rp_unknown_1;
272         u16 i_rp_unknown_2;
273
274         /* If i_resolved == 0:
275          *      SHA1 message digest of the contents of the unnamed-data stream
276          *      of this inode, or all zeroes if this inode has no unnamed data
277          *      stream, or optionally all zeroes if this inode has an empty
278          *      unnamed data stream.
279          *
280          * If i_resolved == 1:
281          *      Pointer to the lookup table entry for the unnamed data stream
282          *      of this inode, or NULL if this inode has no unnamed data stream,
283          *      or optionally all zeroes if this inode has an empty unnamed data
284          *      stream.
285          */
286         union {
287                 u8 i_hash[SHA1_HASH_SIZE];
288                 struct wim_lookup_table_entry *i_lte;
289         };
290
291         /* Identity of a reparse point.  See
292          * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
293          * for what a reparse point is. */
294         u32 i_reparse_tag;
295
296         /* Number of dentries that reference this inode */
297         u32 i_nlink;
298
299         /* Alternate data stream entries. */
300         struct wim_ads_entry *i_ads_entries;
301
302         /* Inode number */
303         u64 i_ino;
304
305         /* Device number, used only during image capture */
306         u64 i_devno;
307
308         /* List of dentries that reference this inode (there should be i_nlink
309          * of them) */
310         struct list_head i_dentry;
311
312         union {
313                 struct hlist_node i_hlist;
314                 struct list_head i_list;
315         };
316
317         tchar *i_extracted_file;
318
319         /* Root of a red-black tree storing the children of this inode (if
320          * non-empty, implies the inode is a directory, although that is also
321          * noted in the @attributes field.) */
322         struct rb_root i_children;
323
324 #ifdef __WIN32__
325         struct rb_root i_children_case_insensitive;
326 #endif
327
328         /* Next alternate data stream ID to be assigned */
329         u32 i_next_stream_id;
330
331 #ifdef WITH_FUSE
332         /* wimfs file descriptors table for the inode */
333         u16 i_num_opened_fds;
334         u16 i_num_allocated_fds;
335         struct wimfs_fd **i_fds;
336         /* This mutex protects the inode's file descriptors table during
337          * read-only mounts.  Read-write mounts are still restricted to 1
338          * thread. */
339         pthread_mutex_t i_mutex;
340 #endif
341 };
342
343 #define inode_for_each_dentry(dentry, inode) \
344                 list_for_each_entry((dentry), &(inode)->i_dentry, d_alias)
345
346 #define inode_add_dentry(dentry, inode) \
347                 list_add_tail(&(dentry)->d_alias, &(inode)->i_dentry)
348
349 #define inode_first_dentry(inode) \
350                 container_of(inode->i_dentry.next, struct wim_dentry, d_alias)
351
352 #define inode_first_full_path(inode) \
353                 dentry_full_path(inode_first_dentry(inode))
354
355 static inline bool
356 dentry_is_first_in_inode(const struct wim_dentry *dentry)
357 {
358         return inode_first_dentry(dentry->d_inode) == dentry;
359 }
360
361 extern u64
362 dentry_correct_total_length(const struct wim_dentry *dentry);
363
364 extern int
365 for_dentry_in_tree(struct wim_dentry *root,
366                    int (*visitor)(struct wim_dentry*, void*),
367                    void *args);
368
369 extern int
370 for_dentry_in_rbtree(struct rb_node *node,
371                      int (*visitor)(struct wim_dentry *, void *),
372                      void *arg);
373
374 static inline int
375 for_dentry_child(const struct wim_dentry *dentry,
376                  int (*visitor)(struct wim_dentry *, void *),
377                  void *arg)
378 {
379         return for_dentry_in_rbtree(dentry->d_inode->i_children.rb_node,
380                                     visitor,
381                                     arg);
382 }
383
384 extern int
385 for_dentry_in_tree_depth(struct wim_dentry *root,
386                          int (*visitor)(struct wim_dentry*, void*),
387                          void *args);
388
389 extern void
390 calculate_subdir_offsets(struct wim_dentry *dentry, u64 *subdir_offset_p);
391
392 extern int
393 set_dentry_name(struct wim_dentry *dentry, const tchar *new_name);
394
395 extern struct wim_dentry *
396 get_dentry(struct WIMStruct *w, const tchar *path);
397
398 extern struct wim_inode *
399 wim_pathname_to_inode(struct WIMStruct *w, const tchar *path);
400
401 extern struct wim_dentry *
402 get_dentry_child_with_name(const struct wim_dentry *dentry,
403                            const tchar *name);
404
405 extern struct wim_dentry *
406 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
407                                    const utf16lechar *name,
408                                    size_t name_nbytes);
409
410 extern struct wim_dentry *
411 get_parent_dentry(struct WIMStruct *w, const tchar *path);
412
413 extern int
414 print_dentry(struct wim_dentry *dentry, void *lookup_table);
415
416 extern int
417 print_dentry_full_path(struct wim_dentry *entry, void *ignore);
418
419 extern int
420 calculate_dentry_full_path(struct wim_dentry *dentry);
421
422 extern int
423 calculate_dentry_tree_full_paths(struct wim_dentry *root);
424
425 extern tchar *
426 dentry_full_path(struct wim_dentry *dentry);
427
428 extern struct wim_inode *
429 new_timeless_inode(void) _malloc_attribute;
430
431 extern int
432 new_dentry(const tchar *name, struct wim_dentry **dentry_ret);
433
434 extern int
435 new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret);
436
437 extern int
438 new_dentry_with_timeless_inode(const tchar *name, struct wim_dentry **dentry_ret);
439
440 extern int
441 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret);
442
443 extern void
444 free_inode(struct wim_inode *inode);
445
446 extern void
447 free_dentry(struct wim_dentry *dentry);
448
449 extern void
450 put_dentry(struct wim_dentry *dentry);
451
452 extern void
453 free_dentry_tree(struct wim_dentry *root,
454                  struct wim_lookup_table *lookup_table);
455
456 extern void
457 unlink_dentry(struct wim_dentry *dentry);
458
459 extern struct wim_dentry *
460 dentry_add_child(struct wim_dentry * restrict parent,
461                  struct wim_dentry * restrict child);
462
463 extern struct wim_ads_entry *
464 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
465                     u16 *idx_ret);
466
467 extern struct wim_ads_entry *
468 inode_add_ads_utf16le(struct wim_inode *inode,
469                       const utf16lechar *stream_name,
470                       size_t stream_name_nbytes);
471
472 extern struct wim_ads_entry *
473 inode_add_ads(struct wim_inode *dentry, const tchar *stream_name);
474
475 extern int
476 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
477                         const void *value, size_t size,
478                         struct wim_lookup_table *lookup_table);
479
480 extern int
481 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
482                          struct wim_lookup_table *lookup_table);
483
484 extern void
485 inode_remove_ads(struct wim_inode *inode, u16 idx,
486                  struct wim_lookup_table *lookup_table);
487
488
489 #define WIMLIB_UNIX_DATA_TAG "$$__wimlib_UNIX_data"
490 #define WIMLIB_UNIX_DATA_TAG_NBYTES (sizeof(WIMLIB_UNIX_DATA_TAG) - 1)
491
492 #define WIMLIB_UNIX_DATA_TAG_UTF16LE "$\0$\0_\0_\0w\0i\0m\0l\0i\0b\0_\0U\0N\0I\0X\0_\0d\0a\0t\0a\0"
493 #define WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES (sizeof(WIMLIB_UNIX_DATA_TAG_UTF16LE) - 1)
494
495 /* Format for special alternate data stream entries to store UNIX data for files
496  * and directories (see: WIMLIB_ADD_FLAG_UNIX_DATA) */
497 struct wimlib_unix_data {
498         u16 version; /* Must be 0 */
499         u16 uid;
500         u16 gid;
501         u16 mode;
502 } _packed_attribute;
503
504 #ifndef __WIN32__
505
506 #define NO_UNIX_DATA (-1)
507 #define BAD_UNIX_DATA (-2)
508 extern int
509 inode_get_unix_data(const struct wim_inode *inode,
510                     struct wimlib_unix_data *unix_data,
511                     u16 *stream_idx_ret);
512
513 #define UNIX_DATA_UID    0x1
514 #define UNIX_DATA_GID    0x2
515 #define UNIX_DATA_MODE   0x4
516 #define UNIX_DATA_ALL    (UNIX_DATA_UID | UNIX_DATA_GID | UNIX_DATA_MODE)
517 #define UNIX_DATA_CREATE 0x8
518 extern int
519 inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
520                     struct wim_lookup_table *lookup_table, int which);
521 #endif
522
523 extern int
524 read_dentry(const u8 *metadata_resource, u64 metadata_resource_len,
525             u64 offset, struct wim_dentry *dentry);
526
527 extern int
528 read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
529                  struct wim_dentry *dentry);
530
531 extern u8 *
532 write_dentry_tree(const struct wim_dentry *tree, u8 *p);
533
534 static inline bool
535 dentry_is_root(const struct wim_dentry *dentry)
536 {
537         return dentry->parent == dentry;
538 }
539
540 static inline bool
541 inode_is_directory(const struct wim_inode *inode)
542 {
543         return (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
544                 && !(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
545 }
546
547 static inline bool
548 dentry_is_directory(const struct wim_dentry *dentry)
549 {
550         return inode_is_directory(dentry->d_inode);
551 }
552
553 /* For our purposes, we consider "real" symlinks and "junction points" to both
554  * be symlinks. */
555 static inline bool
556 inode_is_symlink(const struct wim_inode *inode)
557 {
558         return (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
559                 && (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
560                     inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
561 }
562
563 static inline bool
564 inode_is_regular_file(const struct wim_inode *inode)
565 {
566         return !inode_is_directory(inode) && !inode_is_symlink(inode);
567 }
568
569 static inline bool
570 dentry_is_regular_file(const struct wim_dentry *dentry)
571 {
572         return inode_is_regular_file(dentry->d_inode);
573 }
574
575 static inline bool
576 inode_has_children(const struct wim_inode *inode)
577 {
578         return inode->i_children.rb_node != NULL;
579 }
580
581 static inline bool
582 dentry_has_children(const struct wim_dentry *dentry)
583 {
584         return inode_has_children(dentry->d_inode);
585 }
586
587 static inline bool
588 dentry_has_short_name(const struct wim_dentry *dentry)
589 {
590         return dentry->short_name_nbytes != 0;
591 }
592
593 static inline bool
594 dentry_has_long_name(const struct wim_dentry *dentry)
595 {
596         return dentry->file_name_nbytes != 0;
597 }
598
599 extern void
600 inode_ref_streams(struct wim_inode *inode);
601
602 extern int
603 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list);
604
605 int
606 verify_inode(struct wim_inode *inode, const struct wim_security_data *sd);
607
608 #endif