1 #ifndef _WIMLIB_DENTRY_H
2 #define _WIMLIB_DENTRY_H
12 typedef struct WIMStruct WIMStruct;
14 /* Size of the struct dentry up to and including the file_name_len. */
15 #define WIM_DENTRY_DISK_SIZE 102
17 #define WIM_ADS_ENTRY_DISK_SIZE 38
20 #define WIM_HASH_SIZE 20
24 * Reparse tags documented at
25 * http://msdn.microsoft.com/en-us/library/dd541667(v=prot.10).aspx
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
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
55 struct lookup_table_entry;
57 /* Alternate data stream entry */
60 /* SHA-1 message digest of stream contents */
61 u8 hash[WIM_HASH_SIZE];
63 /* The corresponding lookup table entry (only for resolved
65 struct lookup_table_entry *lte;
68 /* Length of stream name (UTF-16) */
71 /* Length of stream name (UTF-8) */
72 u16 stream_name_utf8_len;
74 /* Stream name (UTF-16) */
77 /* Stream name (UTF-8) */
78 char *stream_name_utf8;
80 /* Doubly linked list of streams that share the same lookup table entry */
81 struct stream_list_head lte_group_list;
84 static inline u64 ads_entry_length(const struct ads_entry *entry)
86 u64 len = WIM_ADS_ENTRY_DISK_SIZE + entry->stream_name_len + 2;
87 return (len + 7) & ~7;
90 static inline void destroy_ads_entry(struct ads_entry *entry)
92 FREE(entry->stream_name);
93 FREE(entry->stream_name_utf8);
94 memset(entry, 0, sizeof(entry));
97 static inline bool ads_entry_has_name(const struct ads_entry *entry,
98 const char *name, size_t name_len)
100 if (entry->stream_name_utf8_len != name_len)
102 return memcmp(entry->stream_name_utf8, name, name_len) == 0;
106 /* In-memory structure for a directory entry. There is a directory tree for
107 * each image in the WIM. */
109 /* The parent of this directory entry. */
110 struct dentry *parent;
112 /* Linked list of sibling directory entries. */
117 /* Pointer to a child of this directory entry. */
118 struct dentry *children;
120 /* Size of directory entry, in bytes. Typical size is around 104 to 120
122 /* It is possible for the length field to be 0. This situation, which
123 * is undocumented, indicates the end of a list of sibling nodes in a
124 * directory. It also means the real length is 8, because the dentry
125 * included only the length field, but that takes up 8 bytes. */
128 /* The file attributes associated with this file. */
131 /* The index of the node in the security table that contains this file's
132 * security information. If -1, no security information exists for this
136 /* The offset, from the start of the metadata section, of this directory
137 * entry's child files. 0 if the directory entry has no children. */
140 /* Timestamps for the entry. The timestamps are the number of
141 * 100-nanosecond intervals that have elapsed since 12:00 A.M., January
144 u64 last_access_time;
147 /* true if the dentry's lookup table entry has been resolved (i.e. the
148 * @lte field is invalid, but the @hash field is not valid) */
151 /* A hash of the file's contents, or a pointer to the lookup table entry
152 * for this dentry if the lookup table entries have been resolved.
154 * More specifically, this is for the un-named default file stream, as
155 * opposed to the alternate file streams, which may have their own
156 * lookup table entries. */
158 u8 hash[WIM_HASH_SIZE];
159 struct lookup_table_entry *lte;
162 /* Identity of a reparse point. See
163 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
164 * for what a reparse point is. */
167 /* Although M$'s documentation does not tell you this, it seems that the
168 * reparse_reserved field does not actually exist. So the hard_link
169 * field directly follows the reparse_tag on disk. EXCEPT when the
170 * dentry is actually a reparse point... well, just take a look at the
171 * read_dentry() function. */
172 //u32 reparse_reserved;
174 /* Number of alternate data streams associated with this file. */
177 /* Length of short filename, in bytes, not including the terminating
178 * zero wide-character. */
181 /* Length of file name, in bytes, not including the terminating zero
185 /* Length of the filename converted into UTF-8, in bytes, not including
186 * the terminating zero byte. */
187 u16 file_name_utf8_len;
189 /* Pointer to the short filename */
192 /* Pointer to the filename. */
195 /* Pointer to the filename converted to UTF-8. */
196 char *file_name_utf8;
198 /* Full path to this dentry. */
199 char *full_path_utf8;
200 u32 full_path_utf8_len;
202 /* Alternate stream entries for this dentry. */
203 struct ads_entry *ads_entries;
206 /* Number of references to the dentry tree itself, as in multiple
210 /* Number of times this dentry has been opened (only for
212 u32 num_times_opened;
215 /* If the file is part of a hard link set, all the directory entries in
216 * the set will share the same value for this field. */
220 /* This dentry is the owner of its ads_entries, although it may
221 * be in a hard link set */
222 ADS_ENTRIES_DEFAULT = 0,
224 /* This dentry is the owner of the ads_entries in the hard link
228 /* This dentry shares its ads_entries with a dentry in the hard
229 * link set that has ADS_ENTRIES_OWNER set. */
231 } ads_entries_status;
234 /* List of dentries in the hard link set */
235 struct list_head link_group_list;
237 /* List of dentries sharing the same lookup table entry */
238 struct stream_list_head lte_group_list;
240 /* Path to extracted file on disk (used during extraction only) */
241 char *extracted_file;
244 /* Return hash of the "unnamed" (default) data stream. */
245 static inline const u8 *dentry_hash(const struct dentry *dentry)
247 wimlib_assert(!dentry->resolved);
248 /* If there are alternate data streams, the dentry hash field is zeroed
249 * out, and we need to find the hash in the un-named data stream (should
250 * be the first one, but check them in order just in case, and fall back
251 * to the dentry hash field if we can't find an unnamed data stream). */
252 for (u16 i = 0; i < dentry->num_ads; i++)
253 if (dentry->ads_entries[i].stream_name_len == 0)
254 return dentry->ads_entries[i].hash;
258 /* Return lte for the "unnamed" (default) data stream. Only for resolved
260 static inline struct lookup_table_entry *
261 dentry_lte(const struct dentry *dentry)
263 wimlib_assert(dentry->resolved);
264 for (u16 i = 0; i < dentry->num_ads; i++)
265 if (dentry->ads_entries[i].stream_name_len == 0)
266 return dentry->ads_entries[i].lte;
270 /* Return the number of dentries in the hard link group */
271 static inline size_t dentry_link_group_size(const struct dentry *dentry)
273 const struct list_head *cur = &dentry->link_group_list;
275 wimlib_assert(cur != NULL);
279 } while (cur != &dentry->link_group_list);
283 extern struct ads_entry *dentry_get_ads_entry(struct dentry *dentry,
284 const char *stream_name);
286 extern struct ads_entry *dentry_add_ads(struct dentry *dentry,
287 const char *stream_name);
289 extern void dentry_remove_ads(struct dentry *dentry, struct ads_entry *entry);
291 extern const char *path_stream_name(const char *path);
293 extern u64 dentry_total_length(const struct dentry *dentry);
295 extern void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry);
297 extern int for_dentry_in_tree(struct dentry *root,
298 int (*visitor)(struct dentry*, void*),
301 extern int for_dentry_in_tree_depth(struct dentry *root,
302 int (*visitor)(struct dentry*, void*),
305 extern int calculate_dentry_full_path(struct dentry *dentry, void *ignore);
306 extern void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p);
307 extern int get_names(char **name_utf16_ret, char **name_utf8_ret,
308 u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
310 extern int change_dentry_name(struct dentry *dentry, const char *new_name);
311 extern int change_ads_name(struct ads_entry *entry, const char *new_name);
313 extern void unlink_dentry(struct dentry *dentry);
314 extern void link_dentry(struct dentry *dentry, struct dentry *parent);
316 extern int print_dentry(struct dentry *dentry, void *lookup_table);
317 extern int print_dentry_full_path(struct dentry *entry, void *ignore);
319 extern struct dentry *get_dentry(WIMStruct *w, const char *path);
320 extern struct dentry *get_parent_dentry(WIMStruct *w, const char *path);
321 extern struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
323 extern void dentry_update_all_timestamps(struct dentry *dentry);
324 extern void init_dentry(struct dentry *dentry, const char *name);
325 extern struct dentry *new_dentry(const char *name);
327 extern void dentry_free_ads_entries(struct dentry *dentry);
328 extern void free_dentry(struct dentry *dentry);
329 extern void put_dentry(struct dentry *dentry);
330 extern struct dentry *clone_dentry(struct dentry *old);
331 extern void free_dentry_tree(struct dentry *root,
332 struct lookup_table *lookup_table);
333 extern int increment_dentry_refcnt(struct dentry *dentry, void *ignore);
334 extern int decrement_dentry_refcnt(struct dentry *dentry, void *ignore);
336 extern void calculate_dir_tree_statistics(struct dentry *root,
337 struct lookup_table *table,
340 u64 *total_bytes_ret,
341 u64 *hard_link_bytes_ret);
343 extern int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
344 u64 offset, struct dentry *dentry);
346 extern int read_dentry_tree(const u8 metadata_resource[],
347 u64 metadata_resource_len, struct dentry *dentry);
349 extern u8 *write_dentry_tree(const struct dentry *tree, u8 *p);
352 /* Inline utility functions for dentries */
354 static inline bool dentry_is_root(const struct dentry *dentry)
356 return dentry->parent == dentry;
359 static inline bool dentry_is_first_sibling(const struct dentry *dentry)
361 return dentry_is_root(dentry) || dentry->parent->children == dentry;
364 static inline bool dentry_is_only_child(const struct dentry *dentry)
366 return dentry->next == dentry;
369 static inline bool dentry_is_directory(const struct dentry *dentry)
371 return (dentry->attributes & FILE_ATTRIBUTE_DIRECTORY)
372 && !(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
375 /* For our purposes, we consider "real" symlinks and "junction points" to both
377 static inline bool dentry_is_symlink(const struct dentry *dentry)
379 return (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT)
380 && ((dentry->reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK) ||
381 dentry->reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
384 static inline bool dentry_is_regular_file(const struct dentry *dentry)
386 return !dentry_is_directory(dentry) && !dentry_is_symlink(dentry);
389 static inline bool dentry_is_empty_directory(const struct dentry *dentry)
391 return dentry_is_directory(dentry) && dentry->children == NULL;