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