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