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