]> wimlib.net Git - wimlib/blob - src/dentry.h
NTFS capture (IN PROGRESS)
[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 "list.h"
7 #include "sha1.h"
8 #include <string.h>
9
10
11 struct stat;
12 struct lookup_table;
13 typedef struct WIMStruct WIMStruct;
14
15 /* Size of the struct dentry up to and including the file_name_len. */
16 #define WIM_DENTRY_DISK_SIZE    102
17
18 #define WIM_ADS_ENTRY_DISK_SIZE 38
19
20 /* 
21  * Reparse tags documented at 
22  * http://msdn.microsoft.com/en-us/library/dd541667(v=prot.10).aspx
23  */
24 #define WIM_IO_REPARSE_TAG_RESERVED_ZERO        0x00000000
25 #define WIM_IO_REPARSE_TAG_RESERVED_ONE         0x00000001
26 #define WIM_IO_REPARSE_TAG_MOUNT_POINT          0xA0000003
27 #define WIM_IO_REPARSE_TAG_HSM                  0xC0000004
28 #define WIM_IO_REPARSE_TAG_HSM2                 0x80000006
29 #define WIM_IO_REPARSE_TAG_DRIVER_EXTENDER      0x80000005
30 #define WIM_IO_REPARSE_TAG_SIS                  0x80000007
31 #define WIM_IO_REPARSE_TAG_DFS                  0x8000000A
32 #define WIM_IO_REPARSE_TAG_DFSR                 0x80000012
33 #define WIM_IO_REPARSE_TAG_FILTER_MANAGER       0x8000000B
34 #define WIM_IO_REPARSE_TAG_SYMLINK              0xA000000C
35
36 #define FILE_ATTRIBUTE_READONLY            0x00000001
37 #define FILE_ATTRIBUTE_HIDDEN              0x00000002
38 #define FILE_ATTRIBUTE_SYSTEM              0x00000004
39 #define FILE_ATTRIBUTE_DIRECTORY           0x00000010
40 #define FILE_ATTRIBUTE_ARCHIVE             0x00000020
41 #define FILE_ATTRIBUTE_DEVICE              0x00000040
42 #define FILE_ATTRIBUTE_NORMAL              0x00000080
43 #define FILE_ATTRIBUTE_TEMPORARY           0x00000100
44 #define FILE_ATTRIBUTE_SPARSE_FILE         0x00000200
45 #define FILE_ATTRIBUTE_REPARSE_POINT       0x00000400
46 #define FILE_ATTRIBUTE_COMPRESSED          0x00000800
47 #define FILE_ATTRIBUTE_OFFLINE             0x00001000
48 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
49 #define FILE_ATTRIBUTE_ENCRYPTED           0x00004000
50 #define FILE_ATTRIBUTE_VIRTUAL             0x00010000
51
52 struct lookup_table_entry;
53
54 /* Alternate data stream entry */
55 struct ads_entry {
56         union {
57                 /* SHA-1 message digest of stream contents */
58                 u8 hash[SHA1_HASH_SIZE];
59
60                 /* The corresponding lookup table entry (only for resolved
61                  * streams) */
62                 struct lookup_table_entry *lte;
63         };
64
65         /* Length of stream name (UTF-16) */
66         u16 stream_name_len;
67
68         /* Length of stream name (UTF-8) */
69         u16 stream_name_utf8_len;
70
71         /* Stream name (UTF-16) */
72         char *stream_name;
73
74         /* Stream name (UTF-8) */
75         char *stream_name_utf8;
76
77         /* Doubly linked list of streams that share the same lookup table entry */
78         struct stream_list_head lte_group_list;
79 };
80
81 static inline u64 ads_entry_length(const struct ads_entry *entry)
82 {
83         u64 len = WIM_ADS_ENTRY_DISK_SIZE + entry->stream_name_len + 2;
84         return (len + 7) & ~7;
85 }
86
87 static inline void destroy_ads_entry(struct ads_entry *entry)
88 {
89         FREE(entry->stream_name);
90         FREE(entry->stream_name_utf8);
91         memset(entry, 0, sizeof(entry));
92 }
93
94 static inline bool ads_entry_has_name(const struct ads_entry *entry,
95                                       const char *name, size_t name_len)
96 {
97         if (entry->stream_name_utf8_len != name_len)
98                 return false;
99         return memcmp(entry->stream_name_utf8, name, name_len) == 0;
100 }
101
102
103 /* In-memory structure for a WIM directory entry.  There is a directory tree for
104  * each image in the WIM.  */
105 struct dentry {
106         /* The parent of this directory entry. */
107         struct dentry *parent;
108
109         /* Linked list of sibling directory entries. */
110         struct dentry *next;
111
112         struct dentry *prev;
113
114         /* Pointer to a child of this directory entry. */
115         struct dentry *children;
116
117         /* Size of directory entry, in bytes.  Typical size is around 104 to 120
118          * bytes. */
119         /* It is possible for the length field to be 0.  This situation, which
120          * is undocumented, indicates the end of a list of sibling nodes in a
121          * directory.  It also means the real length is 8, because the dentry
122          * included only the length field, but that takes up 8 bytes. */
123         u64 length;
124
125         /* The file attributes associated with this file. */
126         u32 attributes;
127
128         /* The index of the node in the security table that contains this file's
129          * security information.  If -1, no security information exists for this
130          * file.  */
131         int32_t security_id;
132
133         /* The offset, from the start of the metadata section, of this directory
134          * entry's child files.  0 if the directory entry has no children. */
135         u64 subdir_offset;
136
137         /* Timestamps for the dentry.  The timestamps are the number of
138          * 100-nanosecond intervals that have elapsed since 12:00 A.M., January
139          * 1st, 1601, UTC. */
140         u64 creation_time;
141         u64 last_access_time;
142         u64 last_write_time;
143
144         /* true if the dentry's lookup table entry has been resolved (i.e. the
145          * @lte field is invalid, but the @hash field is not valid) */
146         bool resolved;
147
148         /* A hash of the file's contents, or a pointer to the lookup table entry
149          * for this dentry if the lookup table entries have been resolved.
150          *
151          * More specifically, this is for the un-named default file stream, as
152          * opposed to the alternate file streams, which may have their own
153          * lookup table entries.  */
154         union {
155                 u8 hash[SHA1_HASH_SIZE];
156                 struct lookup_table_entry *lte;
157         };
158
159         /* Identity of a reparse point.  See
160          * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
161          * for what a reparse point is. */
162         u32 reparse_tag;
163
164         /* Although M$'s documentation does not tell you this, it seems that the
165          * reparse_reserved field does not actually exist.  So the hard_link
166          * field directly follows the reparse_tag on disk.  EXCEPT when the
167          * dentry is actually a reparse point... well, just take a look at the
168          * read_dentry() function. */
169         //u32 reparse_reserved;
170
171         /* Number of alternate data streams associated with this file. */
172         u16 num_ads;
173
174         /* Length of short filename, in bytes, not including the terminating
175          * zero wide-character. */
176         u16 short_name_len;
177
178         /* Length of file name, in bytes, not including the terminating zero
179          * wide-character. */
180         u16 file_name_len;
181
182         /* Length of the filename converted into UTF-8, in bytes, not including
183          * the terminating zero byte. */
184         u16 file_name_utf8_len;
185
186         /* Pointer to the short filename */
187         char *short_name;
188
189         /* Pointer to the filename. */
190         char *file_name;
191
192         /* Pointer to the filename converted to UTF-8. */
193         char *file_name_utf8;
194
195         /* Full path to this dentry. */
196         char *full_path_utf8;
197         u32   full_path_utf8_len;
198
199         /* Alternate stream entries for this dentry. */
200         struct ads_entry *ads_entries;
201
202         union {
203                 /* Number of references to the dentry tree itself, as in multiple
204                  * WIMStructs */
205                 u32 refcnt;
206
207                 /* Number of times this dentry has been opened (only for
208                  * directories!) */
209                 u32 num_times_opened;
210         };
211
212         /* If the file is part of a hard link set, all the directory entries in
213          * the set will share the same value for this field. */
214         u64 hard_link;
215
216         enum {
217                 /* This dentry is the owner of its ads_entries, although it may
218                  * be in a hard link set */
219                 ADS_ENTRIES_DEFAULT = 0,
220
221                 /* This dentry is the owner of the ads_entries in the hard link
222                  * set */
223                 ADS_ENTRIES_OWNER,
224
225                 /* This dentry shares its ads_entries with a dentry in the hard
226                  * link set that has ADS_ENTRIES_OWNER set. */
227                 ADS_ENTRIES_USER
228         } ads_entries_status;
229
230
231         /* List of dentries in the hard link set */
232         struct list_head link_group_list;
233
234         /* List of dentries sharing the same lookup table entry */
235         struct stream_list_head lte_group_list;
236
237         /* Path to extracted file on disk (used during extraction only) */
238         char *extracted_file;
239 };
240
241
242 /* Return the number of dentries in the hard link group */
243 static inline size_t dentry_link_group_size(const struct dentry *dentry)
244 {
245         const struct list_head *cur = &dentry->link_group_list;
246         size_t size = 0;
247         wimlib_assert(cur != NULL);
248         do {
249                 size++;
250                 cur = cur->next;
251         } while (cur != &dentry->link_group_list);
252         return size;
253 }
254
255 extern struct ads_entry *dentry_get_ads_entry(struct dentry *dentry,
256                                               const char *stream_name);
257
258 extern struct ads_entry *dentry_add_ads(struct dentry *dentry,
259                                         const char *stream_name);
260
261 extern void dentry_remove_ads(struct dentry *dentry, struct ads_entry *entry);
262
263 extern const char *path_stream_name(const char *path);
264
265 extern u64 dentry_total_length(const struct dentry *dentry);
266
267 extern void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry);
268
269 extern int for_dentry_in_tree(struct dentry *root, 
270                               int (*visitor)(struct dentry*, void*), 
271                               void *args);
272
273 extern int for_dentry_in_tree_depth(struct dentry *root, 
274                                     int (*visitor)(struct dentry*, void*), 
275                                     void *args);
276
277 extern int calculate_dentry_full_path(struct dentry *dentry, void *ignore);
278 extern void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p);
279 extern int get_names(char **name_utf16_ret, char **name_utf8_ret,
280                      u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
281                      const char *name);
282 extern int change_dentry_name(struct dentry *dentry, const char *new_name);
283 extern int change_ads_name(struct ads_entry *entry, const char *new_name);
284
285 extern void unlink_dentry(struct dentry *dentry);
286 extern void link_dentry(struct dentry *dentry, struct dentry *parent);
287
288 extern int print_dentry(struct dentry *dentry, void *lookup_table);
289 extern int print_dentry_full_path(struct dentry *entry, void *ignore);
290
291 extern struct dentry *get_dentry(WIMStruct *w, const char *path);
292 extern struct dentry *get_parent_dentry(WIMStruct *w, const char *path);
293 extern struct dentry *get_dentry_child_with_name(const struct dentry *dentry, 
294                                                         const char *name);
295 extern void dentry_update_all_timestamps(struct dentry *dentry);
296 extern void init_dentry(struct dentry *dentry, const char *name);
297 extern struct dentry *new_dentry(const char *name);
298
299 extern void dentry_free_ads_entries(struct dentry *dentry);
300 extern void free_dentry(struct dentry *dentry);
301 extern void put_dentry(struct dentry *dentry);
302 extern struct dentry *clone_dentry(struct dentry *old);
303 extern void free_dentry_tree(struct dentry *root,
304                              struct lookup_table *lookup_table);
305 extern int increment_dentry_refcnt(struct dentry *dentry, void *ignore);
306 extern int decrement_dentry_refcnt(struct dentry *dentry, void *ignore);
307
308 extern void calculate_dir_tree_statistics(struct dentry *root, 
309                                           struct lookup_table *table, 
310                                           u64 *dir_count_ret, 
311                                           u64 *file_count_ret, 
312                                           u64 *total_bytes_ret, 
313                                           u64 *hard_link_bytes_ret);
314
315 extern int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len, 
316                        u64 offset, struct dentry *dentry);
317
318 extern int read_dentry_tree(const u8 metadata_resource[], 
319                             u64 metadata_resource_len, struct dentry *dentry);
320
321 extern u8 *write_dentry_tree(const struct dentry *tree, u8 *p);
322
323
324 /* Inline utility functions for dentries */
325
326 static inline bool dentry_is_root(const struct dentry *dentry)
327 {
328         return dentry->parent == dentry;
329 }
330
331 static inline bool dentry_is_first_sibling(const struct dentry *dentry)
332 {
333         return dentry_is_root(dentry) || dentry->parent->children == dentry;
334 }
335
336 static inline bool dentry_is_only_child(const struct dentry *dentry)
337 {
338         return dentry->next == dentry;
339 }
340
341 static inline bool dentry_is_directory(const struct dentry *dentry)
342 {
343         return (dentry->attributes & FILE_ATTRIBUTE_DIRECTORY)
344                 && !(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
345 }
346
347 /* For our purposes, we consider "real" symlinks and "junction points" to both
348  * be symlinks. */
349 static inline bool dentry_is_symlink(const struct dentry *dentry)
350 {
351         return (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT)
352                 && ((dentry->reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK) ||
353                      dentry->reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
354 }
355
356 static inline bool dentry_is_regular_file(const struct dentry *dentry)
357 {
358         return !dentry_is_directory(dentry) && !dentry_is_symlink(dentry);
359 }
360
361 static inline bool dentry_is_empty_directory(const struct dentry *dentry)
362 {
363         return dentry_is_directory(dentry) && dentry->children == NULL;
364 }
365
366 #endif