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