1 #ifndef _WIMLIB_DENTRY_H
2 #define _WIMLIB_DENTRY_H
13 typedef struct WIMStruct WIMStruct;
15 /* Size of the struct dentry up to and including the file_name_len. */
16 #define WIM_DENTRY_DISK_SIZE 102
18 #define WIM_ADS_ENTRY_DISK_SIZE 38
21 * Reparse tags documented at
22 * http://msdn.microsoft.com/en-us/library/dd541667(v=prot.10).aspx
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
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
52 struct lookup_table_entry;
54 /* Alternate data stream entry */
57 /* SHA-1 message digest of stream contents */
58 u8 hash[SHA1_HASH_SIZE];
60 /* The corresponding lookup table entry (only for resolved
62 struct lookup_table_entry *lte;
65 /* Length of stream name (UTF-16) */
68 /* Length of stream name (UTF-8) */
69 u16 stream_name_utf8_len;
71 /* Stream name (UTF-16) */
74 /* Stream name (UTF-8) */
75 char *stream_name_utf8;
77 /* Doubly linked list of streams that share the same lookup table entry */
78 struct stream_list_head lte_group_list;
81 static inline u64 ads_entry_length(const struct ads_entry *entry)
83 u64 len = WIM_ADS_ENTRY_DISK_SIZE + entry->stream_name_len + 2;
84 return (len + 7) & ~7;
87 static inline void destroy_ads_entry(struct ads_entry *entry)
89 FREE(entry->stream_name);
90 FREE(entry->stream_name_utf8);
91 memset(entry, 0, sizeof(entry));
94 static inline bool ads_entry_has_name(const struct ads_entry *entry,
95 const char *name, size_t name_len)
97 if (entry->stream_name_utf8_len != name_len)
99 return memcmp(entry->stream_name_utf8, name, name_len) == 0;
103 /* In-memory structure for a WIM directory entry. There is a directory tree for
104 * each image in the WIM. */
106 /* The parent of this directory entry. */
107 struct dentry *parent;
109 /* Linked list of sibling directory entries. */
114 /* Pointer to a child of this directory entry. */
115 struct dentry *children;
117 /* Size of directory entry on disk, in bytes. Typical size is around
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.
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.
132 /* The file attributes associated with this file. */
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. */
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
146 /* Timestamps for the dentry. The timestamps are the number of
147 * 100-nanosecond intervals that have elapsed since 12:00 A.M., January
150 u64 last_access_time;
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) */
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.
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. */
164 u8 hash[SHA1_HASH_SIZE];
165 struct lookup_table_entry *lte;
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. */
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;
180 /* Number of alternate data streams associated with this file. */
183 /* Length of short filename, in bytes, not including the terminating
184 * zero wide-character. */
187 /* Length of file name, in bytes, not including the terminating zero
191 /* Length of the filename converted into UTF-8, in bytes, not including
192 * the terminating zero byte. */
193 u16 file_name_utf8_len;
195 /* Pointer to the short filename */
198 /* Pointer to the filename. */
201 /* Pointer to the filename converted to UTF-8. */
202 char *file_name_utf8;
204 /* Full path to this dentry. */
205 char *full_path_utf8;
206 u32 full_path_utf8_len;
208 /* Alternate stream entries for this dentry. */
209 struct ads_entry *ads_entries;
212 /* Number of references to the dentry tree itself, as in multiple
216 /* Number of times this dentry has been opened (only for
218 u32 num_times_opened;
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.
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. */
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,
234 /* This dentry is the owner of the ads_entries in the hard link
238 /* This dentry shares its ads_entries with a dentry in the hard
239 * link set that has ADS_ENTRIES_OWNER set. */
241 } ads_entries_status;
244 /* List of dentries in the hard link set */
245 struct list_head link_group_list;
247 /* List of dentries sharing the same lookup table entry */
248 struct stream_list_head lte_group_list;
250 /* Path to extracted file on disk (used during extraction only) */
251 char *extracted_file;
255 /* Return the number of dentries in the hard link group */
256 static inline size_t dentry_link_group_size(const struct dentry *dentry)
258 const struct list_head *cur = &dentry->link_group_list;
260 wimlib_assert(cur != NULL);
264 } while (cur != &dentry->link_group_list);
268 extern struct ads_entry *dentry_get_ads_entry(struct dentry *dentry,
269 const char *stream_name);
271 extern struct ads_entry *dentry_add_ads(struct dentry *dentry,
272 const char *stream_name);
274 extern void dentry_remove_ads(struct dentry *dentry, struct ads_entry *entry);
276 extern const char *path_stream_name(const char *path);
278 extern u64 dentry_total_length(const struct dentry *dentry);
280 extern void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry);
282 extern int for_dentry_in_tree(struct dentry *root,
283 int (*visitor)(struct dentry*, void*),
286 extern int for_dentry_in_tree_depth(struct dentry *root,
287 int (*visitor)(struct dentry*, void*),
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,
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);
298 extern void unlink_dentry(struct dentry *dentry);
299 extern void link_dentry(struct dentry *dentry, struct dentry *parent);
301 extern int print_dentry(struct dentry *dentry, void *lookup_table);
302 extern int print_dentry_full_path(struct dentry *entry, void *ignore);
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,
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);
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);
321 extern void calculate_dir_tree_statistics(struct dentry *root,
322 struct lookup_table *table,
325 u64 *total_bytes_ret,
326 u64 *hard_link_bytes_ret);
328 extern int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
329 u64 offset, struct dentry *dentry);
331 extern int read_dentry_tree(const u8 metadata_resource[],
332 u64 metadata_resource_len, struct dentry *dentry);
334 extern u8 *write_dentry_tree(const struct dentry *tree, u8 *p);
337 /* Inline utility functions for dentries */
339 static inline bool dentry_is_root(const struct dentry *dentry)
341 return dentry->parent == dentry;
344 static inline bool dentry_is_first_sibling(const struct dentry *dentry)
346 return dentry_is_root(dentry) || dentry->parent->children == dentry;
349 static inline bool dentry_is_only_child(const struct dentry *dentry)
351 return dentry->next == dentry;
354 static inline bool dentry_is_directory(const struct dentry *dentry)
356 return (dentry->attributes & FILE_ATTRIBUTE_DIRECTORY)
357 && !(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
360 /* For our purposes, we consider "real" symlinks and "junction points" to both
362 static inline bool dentry_is_symlink(const struct dentry *dentry)
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);
369 static inline bool dentry_is_regular_file(const struct dentry *dentry)
371 return !dentry_is_directory(dentry) && !dentry_is_symlink(dentry);
374 static inline bool dentry_is_empty_directory(const struct dentry *dentry)
376 return dentry_is_directory(dentry) && dentry->children == NULL;