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, in bytes. Typical size is around 104 to 120
119 /* It is possible for the length field to be 0. This situation, which
120 * is undocumented, indicates the end of a list of sibling nodes in a
121 * directory. It also means the real length is 8, because the dentry
122 * included only the length field, but that takes up 8 bytes. */
125 /* The file attributes associated with this file. */
128 /* The index of the node in the security table that contains this file's
129 * security information. If -1, no security information exists for this
133 /* The offset, from the start of the metadata section, of this directory
134 * entry's child files. 0 if the directory entry has no children. */
137 /* Timestamps for the entry. The timestamps are the number of
138 * 100-nanosecond intervals that have elapsed since 12:00 A.M., January
141 u64 last_access_time;
144 /* true if the dentry's lookup table entry has been resolved (i.e. the
145 * @lte field is invalid, but the @hash field is not valid) */
148 /* A hash of the file's contents, or a pointer to the lookup table entry
149 * for this dentry if the lookup table entries have been resolved.
151 * More specifically, this is for the un-named default file stream, as
152 * opposed to the alternate file streams, which may have their own
153 * lookup table entries. */
155 u8 hash[SHA1_HASH_SIZE];
156 struct lookup_table_entry *lte;
159 /* Identity of a reparse point. See
160 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
161 * for what a reparse point is. */
164 /* Although M$'s documentation does not tell you this, it seems that the
165 * reparse_reserved field does not actually exist. So the hard_link
166 * field directly follows the reparse_tag on disk. EXCEPT when the
167 * dentry is actually a reparse point... well, just take a look at the
168 * read_dentry() function. */
169 //u32 reparse_reserved;
171 /* Number of alternate data streams associated with this file. */
174 /* Length of short filename, in bytes, not including the terminating
175 * zero wide-character. */
178 /* Length of file name, in bytes, not including the terminating zero
182 /* Length of the filename converted into UTF-8, in bytes, not including
183 * the terminating zero byte. */
184 u16 file_name_utf8_len;
186 /* Pointer to the short filename */
189 /* Pointer to the filename. */
192 /* Pointer to the filename converted to UTF-8. */
193 char *file_name_utf8;
195 /* Full path to this dentry. */
196 char *full_path_utf8;
197 u32 full_path_utf8_len;
199 /* Alternate stream entries for this dentry. */
200 struct ads_entry *ads_entries;
203 /* Number of references to the dentry tree itself, as in multiple
207 /* Number of times this dentry has been opened (only for
209 u32 num_times_opened;
212 /* If the file is part of a hard link set, all the directory entries in
213 * the set will share the same value for this field. */
217 /* This dentry is the owner of its ads_entries, although it may
218 * be in a hard link set */
219 ADS_ENTRIES_DEFAULT = 0,
221 /* This dentry is the owner of the ads_entries in the hard link
225 /* This dentry shares its ads_entries with a dentry in the hard
226 * link set that has ADS_ENTRIES_OWNER set. */
228 } ads_entries_status;
231 /* List of dentries in the hard link set */
232 struct list_head link_group_list;
234 /* List of dentries sharing the same lookup table entry */
235 struct stream_list_head lte_group_list;
237 /* Path to extracted file on disk (used during extraction only) */
238 char *extracted_file;
242 /* Return the number of dentries in the hard link group */
243 static inline size_t dentry_link_group_size(const struct dentry *dentry)
245 const struct list_head *cur = &dentry->link_group_list;
247 wimlib_assert(cur != NULL);
251 } while (cur != &dentry->link_group_list);
255 extern struct ads_entry *dentry_get_ads_entry(struct dentry *dentry,
256 const char *stream_name);
258 extern struct ads_entry *dentry_add_ads(struct dentry *dentry,
259 const char *stream_name);
261 extern void dentry_remove_ads(struct dentry *dentry, struct ads_entry *entry);
263 extern const char *path_stream_name(const char *path);
265 extern u64 dentry_total_length(const struct dentry *dentry);
267 extern void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry);
269 extern int for_dentry_in_tree(struct dentry *root,
270 int (*visitor)(struct dentry*, void*),
273 extern int for_dentry_in_tree_depth(struct dentry *root,
274 int (*visitor)(struct dentry*, void*),
277 extern int calculate_dentry_full_path(struct dentry *dentry, void *ignore);
278 extern void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p);
279 extern int get_names(char **name_utf16_ret, char **name_utf8_ret,
280 u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
282 extern int change_dentry_name(struct dentry *dentry, const char *new_name);
283 extern int change_ads_name(struct ads_entry *entry, const char *new_name);
285 extern void unlink_dentry(struct dentry *dentry);
286 extern void link_dentry(struct dentry *dentry, struct dentry *parent);
288 extern int print_dentry(struct dentry *dentry, void *lookup_table);
289 extern int print_dentry_full_path(struct dentry *entry, void *ignore);
291 extern struct dentry *get_dentry(WIMStruct *w, const char *path);
292 extern struct dentry *get_parent_dentry(WIMStruct *w, const char *path);
293 extern struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
295 extern void dentry_update_all_timestamps(struct dentry *dentry);
296 extern void init_dentry(struct dentry *dentry, const char *name);
297 extern struct dentry *new_dentry(const char *name);
299 extern void dentry_free_ads_entries(struct dentry *dentry);
300 extern void free_dentry(struct dentry *dentry);
301 extern void put_dentry(struct dentry *dentry);
302 extern struct dentry *clone_dentry(struct dentry *old);
303 extern void free_dentry_tree(struct dentry *root,
304 struct lookup_table *lookup_table);
305 extern int increment_dentry_refcnt(struct dentry *dentry, void *ignore);
306 extern int decrement_dentry_refcnt(struct dentry *dentry, void *ignore);
308 extern void calculate_dir_tree_statistics(struct dentry *root,
309 struct lookup_table *table,
312 u64 *total_bytes_ret,
313 u64 *hard_link_bytes_ret);
315 extern int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
316 u64 offset, struct dentry *dentry);
318 extern int read_dentry_tree(const u8 metadata_resource[],
319 u64 metadata_resource_len, struct dentry *dentry);
321 extern u8 *write_dentry_tree(const struct dentry *tree, u8 *p);
324 /* Inline utility functions for dentries */
326 static inline bool dentry_is_root(const struct dentry *dentry)
328 return dentry->parent == dentry;
331 static inline bool dentry_is_first_sibling(const struct dentry *dentry)
333 return dentry_is_root(dentry) || dentry->parent->children == dentry;
336 static inline bool dentry_is_only_child(const struct dentry *dentry)
338 return dentry->next == dentry;
341 static inline bool dentry_is_directory(const struct dentry *dentry)
343 return (dentry->attributes & FILE_ATTRIBUTE_DIRECTORY)
344 && !(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
347 /* For our purposes, we consider "real" symlinks and "junction points" to both
349 static inline bool dentry_is_symlink(const struct dentry *dentry)
351 return (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT)
352 && ((dentry->reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK) ||
353 dentry->reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
356 static inline bool dentry_is_regular_file(const struct dentry *dentry)
358 return !dentry_is_directory(dentry) && !dentry_is_symlink(dentry);
361 static inline bool dentry_is_empty_directory(const struct dentry *dentry)
363 return dentry_is_directory(dentry) && dentry->children == NULL;