]> wimlib.net Git - wimlib/blob - src/dentry.h
Alternate stream entries fixes
[wimlib] / src / dentry.h
1 #ifndef _WIMLIB_DENTRY_H
2 #define _WIMLIB_DENTRY_H
3
4 #include "util.h"
5 #include "config.h"
6 #include <string.h>
7
8 /* Size of the struct dentry up to and including the file_name_len. */
9 #define WIM_DENTRY_DISK_SIZE    102
10
11 #define WIM_ADS_ENTRY_DISK_SIZE 38
12
13 #ifndef WITH_NTFS_3G
14 /* 
15  * Reparse tags documented at 
16  * http://msdn.microsoft.com/en-us/library/dd541667(v=prot.10).aspx
17  *
18  * IO_REPARSE_TAG_SYMLINK is the only one we really care about.
19  */
20 #define IO_REPARSE_TAG_RESERVED_ZERO    0x00000000
21 #define IO_REPARSE_TAG_RESERVED_ONE     0x00000001
22 #define IO_REPARSE_TAG_MOUNT_POINT      0xA0000003
23 #define IO_REPARSE_TAG_HSM              0xC0000004
24 #define IO_REPARSE_TAG_HSM2             0x80000006
25 #define IO_REPARSE_TAG_DRIVER_EXTENDER  0x80000005
26 #define IO_REPARSE_TAG_SIS              0x80000007
27 #define IO_REPARSE_TAG_DFS              0x8000000A
28 #define IO_REPARSE_TAG_DFSR             0x80000012
29 #define IO_REPARSE_TAG_FILTER_MANAGER   0x8000000B
30 #define IO_REPARSE_TAG_SYMLINK          0xA000000C
31 #endif /* !WITH_NTFS_3G */
32
33 #define FILE_ATTRIBUTE_READONLY            0x00000001
34 #define FILE_ATTRIBUTE_HIDDEN              0x00000002
35 #define FILE_ATTRIBUTE_SYSTEM              0x00000004
36 #define FILE_ATTRIBUTE_DIRECTORY           0x00000010
37 #define FILE_ATTRIBUTE_ARCHIVE             0x00000020
38 #define FILE_ATTRIBUTE_DEVICE              0x00000040
39 #define FILE_ATTRIBUTE_NORMAL              0x00000080
40 #define FILE_ATTRIBUTE_TEMPORARY           0x00000100
41 #define FILE_ATTRIBUTE_SPARSE_FILE         0x00000200
42 #define FILE_ATTRIBUTE_REPARSE_POINT       0x00000400
43 #define FILE_ATTRIBUTE_COMPRESSED          0x00000800
44 #define FILE_ATTRIBUTE_OFFLINE             0x00001000
45 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
46 #define FILE_ATTRIBUTE_ENCRYPTED           0x00004000
47 #define FILE_ATTRIBUTE_VIRTUAL             0x00010000
48
49 /* Alternate data stream entry */
50 struct ads_entry {
51         /* SHA-1 message digest of stream contents */
52         u8 hash[WIM_HASH_SIZE];
53
54         /* Length of stream name (UTF-16) */
55         u16 stream_name_len;
56
57         /* Length of stream name (UTF-8) */
58         u16 stream_name_len_utf8;
59
60         /* Stream name (UTF-16) */
61         char *stream_name;
62
63         /* Stream name (UTF-8) */
64         char *stream_name_utf8;
65 };
66
67 static inline u64 ads_entry_length(const struct ads_entry *entry)
68 {
69         return WIM_ADS_ENTRY_DISK_SIZE + entry->stream_name_len;
70 }
71
72 /* In-memory structure for a directory entry.  There is a directory tree for
73  * each image in the WIM.  */
74 struct dentry {
75         /* The parent of this directory entry. */
76         struct dentry *parent;
77
78         /* Linked list of sibling directory entries. */
79         struct dentry *next;
80
81         struct dentry *prev;
82
83         /* Pointer to a child of this directory entry. */
84         struct dentry *children;
85
86         /* Size of directory entry, in bytes.  Typical size is around 104 to 120
87          * bytes. */
88         /* It is possible for the length field to be 0.  This situation, which
89          * is undocumented, indicates the end of a list of sibling nodes in a
90          * directory.  It also means the real length is 8, because the dentry
91          * included only the length field, but that takes up 8 bytes. */
92         u64 length;
93
94
95         /* The file attributes associated with this file. */
96         u32 attributes;
97
98         /* The index of the node in the security table that contains this file's
99          * security information.  If -1, no security information exists for this
100          * file.  */
101         int32_t security_id;
102
103         /* The offset, from the start of the metadata section, of this directory
104          * entry's child files.  0 if the directory entry has no children. */
105         u64 subdir_offset;
106
107         /* Reserved for future disuse.  Currently ignoring these fields. */
108         u64 unused1;
109         u64 unused2;
110
111         /* Timestamps for the entry.  The timestamps are the number of
112          * 100-nanosecond intervals that have elapsed since 12:00 A.M., January
113          * 1st, 1601, UTC. */
114         u64 creation_time;
115         u64 last_access_time;
116         u64 last_write_time;
117
118         /* A hash of the file's contents. */
119         u8 hash[WIM_HASH_SIZE];
120
121         /* Identity of a reparse point.  See
122          * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
123          * for what a reparse point is. */
124         u32 reparse_tag;
125
126         /* Although M$'s documentation does not tell you this, it seems that the
127          * reparse_reserved field does not actually exist.  So the hard_link
128          * field directly follows the reparse_tag on disk.  EXCEPT when the
129          * dentry is actually a reparse point... well, just take a look at the
130          * read_dentry() function. */
131         //u32 reparse_reserved;
132
133         /* If the reparse_reserved field existed, there would be a 4-byte gap
134          * here to align hard_link on an 8-byte field.  However,
135          * reparse_reserved does not actually exist, so there is no gap here. */
136
137         /* If the file is part of a hard link set, all the directory entries in
138          * the set will share the same value for this field. */
139         u64 hard_link;
140
141         /* Number of alternate data streams associated with this file. */
142         u16 num_ads;
143
144         /* Length of short filename, in bytes, not including the terminating
145          * zero wide-character. */
146         u16 short_name_len;
147
148         /* Length of file name, in bytes, not including the terminating zero
149          * wide-character. */
150         u16 file_name_len;
151
152         /* Length of the filename converted into UTF-8, in bytes, not including
153          * the terminating zero byte. */
154         u16 file_name_utf8_len;
155
156         /* Pointer to the short filename */
157         char *short_name;
158
159         /* Pointer to the filename. */
160         char *file_name;
161
162         /* Pointer to the filename converted to UTF-8. */
163         char *file_name_utf8;
164
165         /* Full path to this dentry. */
166         char *full_path_utf8;
167         u32   full_path_utf8_len;
168
169         /* Alternate stream entries for this dentry. */
170         struct ads_entry *ads_entries;
171
172         /* Number of references to the dentry tree itself, as in multiple
173          * WIMStructs */
174         int refcnt;
175 };
176
177 extern u64 dentry_total_length(const struct dentry *dentry);
178
179 extern void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry);
180
181 extern void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf, 
182                             const struct lookup_table *table);
183
184 extern int for_dentry_in_tree(struct dentry *root, 
185                               int (*visitor)(struct dentry*, void*), 
186                               void *args);
187
188 extern int for_dentry_in_tree_depth(struct dentry *root, 
189                                     int (*visitor)(struct dentry*, void*), 
190                                     void *args);
191
192 extern int calculate_dentry_full_path(struct dentry *dentry, void *ignore);
193 extern void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p);
194 extern int change_dentry_name(struct dentry *dentry, const char *new_name);
195
196 extern void unlink_dentry(struct dentry *dentry);
197 extern void link_dentry(struct dentry *dentry, struct dentry *parent);
198
199 extern int print_dentry(struct dentry *dentry, void *lookup_table);
200 extern int print_dentry_full_path(struct dentry *entry, void *ignore);
201
202 extern struct dentry *get_dentry(WIMStruct *w, const char *path);
203 extern struct dentry *get_parent_dentry(WIMStruct *w, const char *path);
204 extern struct dentry *get_dentry_child_with_name(const struct dentry *dentry, 
205                                                         const char *name);
206 extern void dentry_update_all_timestamps(struct dentry *dentry);
207 extern void init_dentry(struct dentry *dentry, const char *name);
208 extern struct dentry *new_dentry(const char *name);
209
210 extern void free_dentry(struct dentry *dentry);
211 extern void free_dentry_tree(struct dentry *root,
212                              struct lookup_table *lookup_table, 
213                              bool lt_decrement_refcnt);
214 extern int increment_dentry_refcnt(struct dentry *dentry, void *ignore);
215 extern int decrement_dentry_refcnt(struct dentry *dentry, void *ignore);
216
217 extern void calculate_dir_tree_statistics(struct dentry *root, 
218                                           struct lookup_table *table, 
219                                           u64 *dir_count_ret, 
220                                           u64 *file_count_ret, 
221                                           u64 *total_bytes_ret, 
222                                           u64 *hard_link_bytes_ret);
223
224 extern int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len, 
225                        u64 offset, struct dentry *dentry);
226
227 extern int read_dentry_tree(const u8 metadata_resource[], 
228                             u64 metadata_resource_len, struct dentry *dentry);
229
230 extern u8 *write_dentry_tree(const struct dentry *tree, u8 *p);
231
232 /* Inline utility functions for WIMDentries */
233
234 /*
235  * Returns true if @dentry has the UTF-8 file name @name that has length
236  * @name_len.
237  */
238 static inline bool dentry_has_name(const struct dentry *dentry, const char *name, 
239                                    size_t name_len)
240 {
241         if (dentry->file_name_utf8_len != name_len)
242                 return false;
243         return memcmp(dentry->file_name_utf8, name, name_len) == 0;
244 }
245
246 static inline bool dentry_is_root(const struct dentry *dentry)
247 {
248         return dentry->parent == dentry;
249 }
250
251 static inline bool dentry_is_first_sibling(const struct dentry *dentry)
252 {
253         return dentry_is_root(dentry) || dentry->parent->children == dentry;
254 }
255
256 static inline bool dentry_is_only_child(const struct dentry *dentry)
257 {
258         return dentry->next == dentry;
259 }
260
261 static inline bool dentry_is_directory(const struct dentry *dentry)
262 {
263         return (dentry->attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
264 }
265
266 static inline bool dentry_is_regular_file(const struct dentry *dentry)
267 {
268         return !dentry_is_directory(dentry);
269 }
270
271 static inline bool dentry_is_empty_directory(const struct dentry *dentry)
272 {
273         return dentry_is_directory(dentry) && dentry->children == NULL;
274 }
275
276 #endif