]> wimlib.net Git - wimlib/blob - include/wimlib/dentry.h
dentry.c, dentry.h: Comment dentry structures better
[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         /* Pointer to the inode for this dentry.  This will contain some
130          * information that was factored out of the on-disk WIM dentry as common
131          * to all dentries in a hard link group.  */
132         struct wim_inode *d_inode;
133
134         /* Node for the parent's red-black tree of child dentries, sorted by
135          * case sensitive long name. */
136         struct rb_node rb_node;
137
138 #ifdef __WIN32__
139         /* Node for the parent's red-black tree of child dentries, sorted by
140          * case insensitive long name. */
141         struct rb_node rb_node_case_insensitive;
142
143         /* List of dentries in a directory that have different case sensitive
144          * long names but share the same case insensitive long name */
145         struct list_head case_insensitive_conflict_list;
146 #endif
147
148         /* Length of UTF-16LE encoded short filename, in bytes, not including
149          * the terminating zero wide-character. */
150         u16 short_name_nbytes;
151
152         /* Length of UTF-16LE encoded "long" file name, in bytes, not including
153          * the terminating null character. */
154         u16 file_name_nbytes;
155
156         /* Length of full path name encoded using "tchars", in bytes, not
157          * including the terminating null character. */
158         u32 full_path_nbytes;
159
160         /* For extraction operations, a subtree of dentries will have this flag
161          * set so we can keep track of which dentries still need to be
162          * extracted.  Otherwise this will always be 0.  */
163         u8 needs_extraction : 1;
164
165         /* For extraction operations, this flag will be set when a dentry in the
166          * tree being extracted is not being extracted for some reason (file
167          * type not supported by target filesystem or contains invalid
168          * characters).  Otherwise this will always be 0. */
169         u8 not_extracted : 1;
170
171         /* When capturing from a NTFS volume using NTFS-3g, this flag is set on
172          * dentries that were created from a filename in the WIN32 or WIN32+DOS
173          * namespaces rather than the POSIX namespace.  Otherwise this will
174          * always be 0.  */
175         u8 is_win32_name : 1;
176
177         /* When verifying the dentry tree after reading it into memory, this
178          * flag will be set on all dentries in a hard link group that have a
179          * nonempty DOS name except one.  This is because it is supposed to be
180          * illegal (on NTFS, at least) for a single inode to have multiple DOS
181          * names.  */
182         u8 dos_name_invalid : 1;
183
184         /* Temporary list field used to make lists of dentries in a few places.
185          * */
186         struct list_head tmp_list;
187
188         /* Linked list node that places this dentry in the list of aliases for
189          * its inode (d_inode) */
190         struct list_head d_alias;
191
192         /* The parent of this directory entry. */
193         struct wim_dentry *parent;
194
195         /* 'length' and 'subdir_offset' are only used while reading and writing
196          * this dentry; see the corresponding field in
197          * `struct wim_dentry_on_disk' for explanation.  */
198         u64 length;
199         u64 subdir_offset;
200
201         /* These correspond to the two unused fields in the on-disk WIM dentry;
202          * we read them into memory so we can write them unchanged.  These
203          * fields are set to 0 on new dentries.  */
204         u64 d_unused_1;
205         u64 d_unused_2;
206
207         /* Pointer to the UTF-16LE short filename (malloc()ed buffer), or NULL
208          * if this dentry has no short name.  */
209         utf16lechar *short_name;
210
211         /* Pointer to the UTF-16LE filename (malloc()ed buffer), or NULL if this
212          * dentry has no filename.  */
213         utf16lechar *file_name;
214
215         /* Full path to this dentry in the WIM, in platform-dependent "tchars"
216          * that can be printed without convension.  By default this field will
217          * be NULL and will only be calculated on-demand by the
218          * calculate_dentry_full_path() or dentry_full_path() functions.  */
219         tchar *_full_path;
220
221         /* (Extraction only) Actual name to extract this dentry as, along with
222          * its length in tchars excluding the NULL terminator.  This usually
223          * will be the same as file_name, with the character encoding converted
224          * if needed.  But if file_name contains characters not accepted on the
225          * current platform, then this may be set slightly differently from
226          * file_name.  This will be either NULL or a malloc()ed buffer that may
227          * alias file_name.  */
228         tchar *extraction_name;
229         size_t extraction_name_nchars;
230
231         /* (Extraction only) List head for building a list of dentries that
232          * contain a certain stream. */
233         struct list_head extraction_stream_list;
234 };
235
236 #define rbnode_dentry(node) container_of(node, struct wim_dentry, rb_node)
237
238 /*
239  * WIM inode.
240  *
241  * As mentioned above, in the WIM file that is no on-disk analogue of a real
242  * inode, as most of these fields are duplicated in the dentries.
243  */
244 struct wim_inode {
245         /* Timestamps for the inode.  The timestamps are the number of
246          * 100-nanosecond intervals that have elapsed since 12:00 A.M., January
247          * 1st, 1601, UTC.  This is the same format used in NTFS inodes. */
248         u64 i_creation_time;
249         u64 i_last_access_time;
250         u64 i_last_write_time;
251
252         /* The file attributes associated with this inode.  This is a bitwise OR
253          * of the FILE_ATTRIBUTE_* flags. */
254         u32 i_attributes;
255
256         /* The index of the security descriptor in the WIM image's table of
257          * security descriptors that contains this file's security information.
258          * If -1, no security information exists for this file.  */
259         int32_t i_security_id;
260
261         /* %true iff the inode's lookup table entries has been resolved (i.e.
262          * the @lte field is valid, but the @hash field is not valid)
263          *
264          * (This is not an on-disk field.) */
265         u8 i_resolved : 1;
266
267         u8 i_visited : 1;
268
269         /* Used only in NTFS-mode extraction */
270         u8 i_dos_name_extracted : 1;
271
272         /* Set to 0 if reparse point fixups have been done.  Otherwise set to 1.
273          *
274          * Note: this actually may reflect the SYMBOLIC_LINK_RELATIVE flag.  */
275         u16 i_not_rpfixed;
276
277         /* Number of alternate data streams associated with this inode */
278         u16 i_num_ads;
279
280         /* Unused/unknown fields that we just read into memory so we can
281          * re-write them unchanged.  */
282         u32 i_rp_unknown_1;
283         u16 i_rp_unknown_2;
284
285         /* If i_resolved == 0:
286          *      SHA1 message digest of the contents of the unnamed-data stream
287          *      of this inode, or all zeroes if this inode has no unnamed data
288          *      stream, or optionally all zeroes if this inode has an empty
289          *      unnamed data stream.
290          *
291          * If i_resolved == 1:
292          *      Pointer to the lookup table entry for the unnamed data stream
293          *      of this inode, or NULL if this inode has no unnamed data stream,
294          *      or optionally all zeroes if this inode has an empty unnamed data
295          *      stream.
296          */
297         union {
298                 u8 i_hash[SHA1_HASH_SIZE];
299                 struct wim_lookup_table_entry *i_lte;
300         };
301
302         /* Identity of a reparse point.  See
303          * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
304          * for what a reparse point is. */
305         u32 i_reparse_tag;
306
307         /* Number of dentries that reference this inode */
308         u32 i_nlink;
309
310         /* Alternate data stream entries. */
311         struct wim_ads_entry *i_ads_entries;
312
313         /* Inode number */
314         u64 i_ino;
315
316         /* Device number, used only during image capture */
317         u64 i_devno;
318
319         /* List of dentries that reference this inode (there should be i_nlink
320          * of them) */
321         struct list_head i_dentry;
322
323         union {
324                 struct hlist_node i_hlist;
325                 struct list_head i_list;
326         };
327
328         tchar *i_extracted_file;
329
330         /* Root of a red-black tree storing the children of this inode (if
331          * non-empty, implies the inode is a directory, although that is also
332          * noted in the @attributes field.) */
333         struct rb_root i_children;
334
335 #ifdef __WIN32__
336         struct rb_root i_children_case_insensitive;
337 #endif
338
339         /* Next alternate data stream ID to be assigned */
340         u32 i_next_stream_id;
341
342 #ifdef WITH_FUSE
343         /* wimfs file descriptors table for the inode */
344         u16 i_num_opened_fds;
345         u16 i_num_allocated_fds;
346         struct wimfs_fd **i_fds;
347         /* This mutex protects the inode's file descriptors table during
348          * read-only mounts.  Read-write mounts are still restricted to 1
349          * thread. */
350         pthread_mutex_t i_mutex;
351 #endif
352 };
353
354 #define inode_for_each_dentry(dentry, inode) \
355                 list_for_each_entry((dentry), &(inode)->i_dentry, d_alias)
356
357 #define inode_add_dentry(dentry, inode) \
358                 list_add_tail(&(dentry)->d_alias, &(inode)->i_dentry)
359
360 #define inode_first_dentry(inode) \
361                 container_of(inode->i_dentry.next, struct wim_dentry, d_alias)
362
363 #define inode_first_full_path(inode) \
364                 dentry_full_path(inode_first_dentry(inode))
365
366 static inline bool
367 dentry_is_first_in_inode(const struct wim_dentry *dentry)
368 {
369         return inode_first_dentry(dentry->d_inode) == dentry;
370 }
371
372 extern u64
373 dentry_correct_total_length(const struct wim_dentry *dentry);
374
375 extern int
376 for_dentry_in_tree(struct wim_dentry *root,
377                    int (*visitor)(struct wim_dentry*, void*),
378                    void *args);
379
380 extern int
381 for_dentry_in_rbtree(struct rb_node *node,
382                      int (*visitor)(struct wim_dentry *, void *),
383                      void *arg);
384
385 static inline int
386 for_dentry_child(const struct wim_dentry *dentry,
387                  int (*visitor)(struct wim_dentry *, void *),
388                  void *arg)
389 {
390         return for_dentry_in_rbtree(dentry->d_inode->i_children.rb_node,
391                                     visitor,
392                                     arg);
393 }
394
395 extern int
396 for_dentry_in_tree_depth(struct wim_dentry *root,
397                          int (*visitor)(struct wim_dentry*, void*),
398                          void *args);
399
400 extern void
401 calculate_subdir_offsets(struct wim_dentry *dentry, u64 *subdir_offset_p);
402
403 extern int
404 set_dentry_name(struct wim_dentry *dentry, const tchar *new_name);
405
406 extern struct wim_dentry *
407 get_dentry(struct WIMStruct *w, const tchar *path);
408
409 extern struct wim_inode *
410 wim_pathname_to_inode(struct WIMStruct *w, const tchar *path);
411
412 extern struct wim_dentry *
413 get_dentry_child_with_name(const struct wim_dentry *dentry,
414                            const tchar *name);
415
416 extern struct wim_dentry *
417 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
418                                    const utf16lechar *name,
419                                    size_t name_nbytes);
420
421 extern struct wim_dentry *
422 get_parent_dentry(struct WIMStruct *w, const tchar *path);
423
424 extern int
425 print_dentry(struct wim_dentry *dentry, void *lookup_table);
426
427 extern int
428 print_dentry_full_path(struct wim_dentry *entry, void *ignore);
429
430 extern int
431 calculate_dentry_full_path(struct wim_dentry *dentry);
432
433 extern int
434 calculate_dentry_tree_full_paths(struct wim_dentry *root);
435
436 extern tchar *
437 dentry_full_path(struct wim_dentry *dentry);
438
439 extern struct wim_inode *
440 new_timeless_inode(void) _malloc_attribute;
441
442 extern int
443 new_dentry(const tchar *name, struct wim_dentry **dentry_ret);
444
445 extern int
446 new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret);
447
448 extern int
449 new_dentry_with_timeless_inode(const tchar *name, struct wim_dentry **dentry_ret);
450
451 extern int
452 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret);
453
454 extern void
455 free_inode(struct wim_inode *inode);
456
457 extern void
458 free_dentry(struct wim_dentry *dentry);
459
460 extern void
461 put_dentry(struct wim_dentry *dentry);
462
463 extern void
464 free_dentry_tree(struct wim_dentry *root,
465                  struct wim_lookup_table *lookup_table);
466
467 extern void
468 unlink_dentry(struct wim_dentry *dentry);
469
470 extern struct wim_dentry *
471 dentry_add_child(struct wim_dentry * restrict parent,
472                  struct wim_dentry * restrict child);
473
474 extern struct wim_ads_entry *
475 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
476                     u16 *idx_ret);
477
478 extern struct wim_ads_entry *
479 inode_add_ads_utf16le(struct wim_inode *inode,
480                       const utf16lechar *stream_name,
481                       size_t stream_name_nbytes);
482
483 extern struct wim_ads_entry *
484 inode_add_ads(struct wim_inode *dentry, const tchar *stream_name);
485
486 extern int
487 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
488                         const void *value, size_t size,
489                         struct wim_lookup_table *lookup_table);
490
491 extern int
492 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
493                          struct wim_lookup_table *lookup_table);
494
495 extern void
496 inode_remove_ads(struct wim_inode *inode, u16 idx,
497                  struct wim_lookup_table *lookup_table);
498
499
500 #define WIMLIB_UNIX_DATA_TAG "$$__wimlib_UNIX_data"
501 #define WIMLIB_UNIX_DATA_TAG_NBYTES (sizeof(WIMLIB_UNIX_DATA_TAG) - 1)
502
503 #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"
504 #define WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES (sizeof(WIMLIB_UNIX_DATA_TAG_UTF16LE) - 1)
505
506 /* Format for special alternate data stream entries to store UNIX data for files
507  * and directories (see: WIMLIB_ADD_FLAG_UNIX_DATA) */
508 struct wimlib_unix_data {
509         u16 version; /* Must be 0 */
510         u16 uid;
511         u16 gid;
512         u16 mode;
513 } _packed_attribute;
514
515 #ifndef __WIN32__
516
517 #define NO_UNIX_DATA (-1)
518 #define BAD_UNIX_DATA (-2)
519 extern int
520 inode_get_unix_data(const struct wim_inode *inode,
521                     struct wimlib_unix_data *unix_data,
522                     u16 *stream_idx_ret);
523
524 #define UNIX_DATA_UID    0x1
525 #define UNIX_DATA_GID    0x2
526 #define UNIX_DATA_MODE   0x4
527 #define UNIX_DATA_ALL    (UNIX_DATA_UID | UNIX_DATA_GID | UNIX_DATA_MODE)
528 #define UNIX_DATA_CREATE 0x8
529 extern int
530 inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
531                     struct wim_lookup_table *lookup_table, int which);
532 #endif
533
534 extern int
535 read_dentry(const u8 *metadata_resource, u64 metadata_resource_len,
536             u64 offset, struct wim_dentry *dentry);
537
538 extern int
539 read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
540                  struct wim_dentry *dentry);
541
542 extern u8 *
543 write_dentry_tree(const struct wim_dentry *tree, u8 *p);
544
545 static inline bool
546 dentry_is_root(const struct wim_dentry *dentry)
547 {
548         return dentry->parent == dentry;
549 }
550
551 static inline bool
552 inode_is_directory(const struct wim_inode *inode)
553 {
554         return (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
555                 && !(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
556 }
557
558 static inline bool
559 dentry_is_directory(const struct wim_dentry *dentry)
560 {
561         return inode_is_directory(dentry->d_inode);
562 }
563
564 /* For our purposes, we consider "real" symlinks and "junction points" to both
565  * be symlinks. */
566 static inline bool
567 inode_is_symlink(const struct wim_inode *inode)
568 {
569         return (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
570                 && (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
571                     inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
572 }
573
574 static inline bool
575 inode_is_regular_file(const struct wim_inode *inode)
576 {
577         return !inode_is_directory(inode) && !inode_is_symlink(inode);
578 }
579
580 static inline bool
581 dentry_is_regular_file(const struct wim_dentry *dentry)
582 {
583         return inode_is_regular_file(dentry->d_inode);
584 }
585
586 static inline bool
587 inode_has_children(const struct wim_inode *inode)
588 {
589         return inode->i_children.rb_node != NULL;
590 }
591
592 static inline bool
593 dentry_has_children(const struct wim_dentry *dentry)
594 {
595         return inode_has_children(dentry->d_inode);
596 }
597
598 static inline bool
599 dentry_has_short_name(const struct wim_dentry *dentry)
600 {
601         return dentry->short_name_nbytes != 0;
602 }
603
604 static inline bool
605 dentry_has_long_name(const struct wim_dentry *dentry)
606 {
607         return dentry->file_name_nbytes != 0;
608 }
609
610 extern void
611 inode_ref_streams(struct wim_inode *inode);
612
613 extern int
614 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list);
615
616 int
617 verify_inode(struct wim_inode *inode, const struct wim_security_data *sd);
618
619 #endif