]> wimlib.net Git - wimlib/blob - src/dentry.h
Misc fixes
[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 on disk, in bytes.  Typical size is around
118          * 104 to 120 bytes.
119          *
120          * It is possible for the length field to be 0.  This situation, which
121          * is undocumented, indicates the end of a list of sibling nodes in a
122          * directory.  It also means the real length is 8, because the dentry
123          * included only the length field, but that takes up 8 bytes.
124          *
125          * The length here includes the base directory entry on disk as well as
126          * the long and short filenames.  It does NOT include any alternate
127          * stream entries that may follow the directory entry, even though the
128          * size of those needs to be considered.
129          */
130         u64 length;
131
132         /* The file attributes associated with this file. */
133         u32 attributes;
134
135         /* The index of the security descriptor in the WIM image's table of
136          * security descriptors that contains this file's security information.
137          * If -1, no security information exists for this file.  */
138         int32_t security_id;
139
140         /* The offset, from the start of the WIM metadata resource for this
141          * image, of this directory entry's child files.  0 if the directory
142          * entry has no children (as in the case of regular files or reparse
143          * points). */
144         u64 subdir_offset;
145
146         /* Timestamps for the dentry.  The timestamps are the number of
147          * 100-nanosecond intervals that have elapsed since 12:00 A.M., January
148          * 1st, 1601, UTC. */
149         u64 creation_time;
150         u64 last_access_time;
151         u64 last_write_time;
152
153         /* true if the dentry's lookup table entry has been resolved (i.e. the
154          * @lte field is valid, but the @hash field is not valid) */
155         bool resolved;
156
157         /* A hash of the file's contents, or a pointer to the lookup table entry
158          * for this dentry if the lookup table entries have been resolved.
159          *
160          * More specifically, this is for the un-named default file stream, as
161          * opposed to the alternate (named) file streams, which may have their
162          * own lookup table entries.  */
163         union {
164                 u8 hash[SHA1_HASH_SIZE];
165                 struct lookup_table_entry *lte;
166         };
167
168         /* Identity of a reparse point.  See
169          * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
170          * for what a reparse point is. */
171         u32 reparse_tag;
172
173         /* Although M$'s documentation does not tell you this, it seems that the
174          * reparse_reserved field does not actually exist.  So the hard_link
175          * field directly follows the reparse_tag on disk.  EXCEPT when the
176          * dentry is actually a reparse point... well, just take a look at the
177          * read_dentry() function. */
178         //u32 reparse_reserved;
179
180         /* Number of alternate data streams associated with this file. */
181         u16 num_ads;
182
183         /* Length of short filename, in bytes, not including the terminating
184          * zero wide-character. */
185         u16 short_name_len;
186
187         /* Length of file name, in bytes, not including the terminating zero
188          * wide-character. */
189         u16 file_name_len;
190
191         /* Length of the filename converted into UTF-8, in bytes, not including
192          * the terminating zero byte. */
193         u16 file_name_utf8_len;
194
195         /* Pointer to the short filename */
196         char *short_name;
197
198         /* Pointer to the filename. */
199         char *file_name;
200
201         /* Pointer to the filename converted to UTF-8. */
202         char *file_name_utf8;
203
204         /* Full path to this dentry. */
205         char *full_path_utf8;
206         u32   full_path_utf8_len;
207
208         /* Alternate stream entries for this dentry. */
209         struct ads_entry *ads_entries;
210
211         union {
212                 /* Number of references to the dentry tree itself, as in multiple
213                  * WIMStructs */
214                 u32 refcnt;
215
216                 /* Number of times this dentry has been opened (only for
217                  * directories!) */
218                 u32 num_times_opened;
219         };
220
221         /* If the file is part of a hard link set, all the directory entries in
222          * the set will share the same value for this field. 
223          *
224          * Unfortunately, in some WIMs it is NOT the case that all dentries that
225          * share this field are actually in the same hard link set, although the
226          * WIMs that wimlib writes maintain this restriction. */
227         u64 hard_link;
228
229         enum {
230                 /* This dentry is the owner of its ads_entries, although it may
231                  * be in a hard link set */
232                 ADS_ENTRIES_DEFAULT = 0,
233
234                 /* This dentry is the owner of the ads_entries in the hard link
235                  * set */
236                 ADS_ENTRIES_OWNER,
237
238                 /* This dentry shares its ads_entries with a dentry in the hard
239                  * link set that has ADS_ENTRIES_OWNER set. */
240                 ADS_ENTRIES_USER
241         } ads_entries_status;
242
243
244         /* List of dentries in the hard link set */
245         struct list_head link_group_list;
246
247         /* List of dentries sharing the same lookup table entry */
248         struct stream_list_head lte_group_list;
249
250         /* Path to extracted file on disk (used during extraction only) */
251         char *extracted_file;
252 };
253
254
255 /* Return the number of dentries in the hard link group */
256 static inline size_t dentry_link_group_size(const struct dentry *dentry)
257 {
258         const struct list_head *cur = &dentry->link_group_list;
259         size_t size = 0;
260         wimlib_assert(cur != NULL);
261         do {
262                 size++;
263                 cur = cur->next;
264         } while (cur != &dentry->link_group_list);
265         return size;
266 }
267
268 extern struct ads_entry *dentry_get_ads_entry(struct dentry *dentry,
269                                               const char *stream_name);
270
271 extern struct ads_entry *dentry_add_ads(struct dentry *dentry,
272                                         const char *stream_name);
273
274 extern void dentry_remove_ads(struct dentry *dentry, struct ads_entry *entry);
275
276 extern const char *path_stream_name(const char *path);
277
278 extern u64 dentry_total_length(const struct dentry *dentry);
279
280 extern void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry);
281
282 extern int for_dentry_in_tree(struct dentry *root, 
283                               int (*visitor)(struct dentry*, void*), 
284                               void *args);
285
286 extern int for_dentry_in_tree_depth(struct dentry *root, 
287                                     int (*visitor)(struct dentry*, void*), 
288                                     void *args);
289
290 extern int calculate_dentry_full_path(struct dentry *dentry, void *ignore);
291 extern void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p);
292 extern int get_names(char **name_utf16_ret, char **name_utf8_ret,
293                      u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
294                      const char *name);
295 extern int change_dentry_name(struct dentry *dentry, const char *new_name);
296 extern int change_ads_name(struct ads_entry *entry, const char *new_name);
297
298 extern void unlink_dentry(struct dentry *dentry);
299 extern void link_dentry(struct dentry *dentry, struct dentry *parent);
300
301 extern int print_dentry(struct dentry *dentry, void *lookup_table);
302 extern int print_dentry_full_path(struct dentry *entry, void *ignore);
303
304 extern struct dentry *get_dentry(WIMStruct *w, const char *path);
305 extern struct dentry *get_parent_dentry(WIMStruct *w, const char *path);
306 extern struct dentry *get_dentry_child_with_name(const struct dentry *dentry, 
307                                                         const char *name);
308 extern void dentry_update_all_timestamps(struct dentry *dentry);
309 extern void init_dentry(struct dentry *dentry, const char *name);
310 extern struct dentry *new_dentry(const char *name);
311
312 extern void dentry_free_ads_entries(struct dentry *dentry);
313 extern void free_dentry(struct dentry *dentry);
314 extern void put_dentry(struct dentry *dentry);
315 extern struct dentry *clone_dentry(struct dentry *old);
316 extern void free_dentry_tree(struct dentry *root,
317                              struct lookup_table *lookup_table);
318 extern int increment_dentry_refcnt(struct dentry *dentry, void *ignore);
319 extern int decrement_dentry_refcnt(struct dentry *dentry, void *ignore);
320
321 extern void calculate_dir_tree_statistics(struct dentry *root, 
322                                           struct lookup_table *table, 
323                                           u64 *dir_count_ret, 
324                                           u64 *file_count_ret, 
325                                           u64 *total_bytes_ret, 
326                                           u64 *hard_link_bytes_ret);
327
328 extern int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len, 
329                        u64 offset, struct dentry *dentry);
330
331 extern int read_dentry_tree(const u8 metadata_resource[], 
332                             u64 metadata_resource_len, struct dentry *dentry);
333
334 extern u8 *write_dentry_tree(const struct dentry *tree, u8 *p);
335
336
337 /* Inline utility functions for dentries */
338
339 static inline bool dentry_is_root(const struct dentry *dentry)
340 {
341         return dentry->parent == dentry;
342 }
343
344 static inline bool dentry_is_first_sibling(const struct dentry *dentry)
345 {
346         return dentry_is_root(dentry) || dentry->parent->children == dentry;
347 }
348
349 static inline bool dentry_is_only_child(const struct dentry *dentry)
350 {
351         return dentry->next == dentry;
352 }
353
354 static inline bool dentry_is_directory(const struct dentry *dentry)
355 {
356         return (dentry->attributes & FILE_ATTRIBUTE_DIRECTORY)
357                 && !(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
358 }
359
360 /* For our purposes, we consider "real" symlinks and "junction points" to both
361  * be symlinks. */
362 static inline bool dentry_is_symlink(const struct dentry *dentry)
363 {
364         return (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT)
365                 && ((dentry->reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK) ||
366                      dentry->reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
367 }
368
369 static inline bool dentry_is_regular_file(const struct dentry *dentry)
370 {
371         return !dentry_is_directory(dentry) && !dentry_is_symlink(dentry);
372 }
373
374 static inline bool dentry_is_empty_directory(const struct dentry *dentry)
375 {
376         return dentry_is_directory(dentry) && dentry->children == NULL;
377 }
378
379 #endif