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 /* Alternate data stream entry */
57 /* SHA-1 message digest of stream contents */
58 u8 hash[WIM_HASH_SIZE];
60 /* Length of stream name (UTF-16) */
63 /* Length of stream name (UTF-8) */
64 u16 stream_name_utf8_len;
66 /* Stream name (UTF-16) */
69 /* Stream name (UTF-8) */
70 char *stream_name_utf8;
73 static inline u64 ads_entry_length(const struct ads_entry *entry)
75 u64 len = WIM_ADS_ENTRY_DISK_SIZE + entry->stream_name_len + 2;
76 return (len + 7) & ~7;
79 static inline void destroy_ads_entry(struct ads_entry *entry)
81 FREE(entry->stream_name);
82 FREE(entry->stream_name_utf8);
83 memset(entry, 0, sizeof(entry));
87 /* In-memory structure for a directory entry. There is a directory tree for
88 * each image in the WIM. */
90 /* The parent of this directory entry. */
91 struct dentry *parent;
93 /* Linked list of sibling directory entries. */
98 /* Pointer to a child of this directory entry. */
99 struct dentry *children;
101 /* Size of directory entry, in bytes. Typical size is around 104 to 120
103 /* It is possible for the length field to be 0. This situation, which
104 * is undocumented, indicates the end of a list of sibling nodes in a
105 * directory. It also means the real length is 8, because the dentry
106 * included only the length field, but that takes up 8 bytes. */
110 /* The file attributes associated with this file. */
113 /* The index of the node in the security table that contains this file's
114 * security information. If -1, no security information exists for this
118 /* The offset, from the start of the metadata section, of this directory
119 * entry's child files. 0 if the directory entry has no children. */
122 /* Reserved for future disuse. Currently ignoring these fields. */
126 /* Timestamps for the entry. The timestamps are the number of
127 * 100-nanosecond intervals that have elapsed since 12:00 A.M., January
130 u64 last_access_time;
133 /* A hash of the file's contents. */
134 u8 hash[WIM_HASH_SIZE];
136 /* Identity of a reparse point. See
137 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
138 * for what a reparse point is. */
141 /* Although M$'s documentation does not tell you this, it seems that the
142 * reparse_reserved field does not actually exist. So the hard_link
143 * field directly follows the reparse_tag on disk. EXCEPT when the
144 * dentry is actually a reparse point... well, just take a look at the
145 * read_dentry() function. */
146 //u32 reparse_reserved;
148 /* If the reparse_reserved field existed, there would be a 4-byte gap
149 * here to align hard_link on an 8-byte field. However,
150 * reparse_reserved does not actually exist, so there is no gap here. */
152 /* If the file is part of a hard link set, all the directory entries in
153 * the set will share the same value for this field. */
156 /* Number of alternate data streams associated with this file. */
159 /* Length of short filename, in bytes, not including the terminating
160 * zero wide-character. */
163 /* Length of file name, in bytes, not including the terminating zero
167 /* Length of the filename converted into UTF-8, in bytes, not including
168 * the terminating zero byte. */
169 u16 file_name_utf8_len;
171 /* Pointer to the short filename */
174 /* Pointer to the filename. */
177 /* Pointer to the filename converted to UTF-8. */
178 char *file_name_utf8;
180 /* Full path to this dentry. */
181 char *full_path_utf8;
182 u32 full_path_utf8_len;
184 /* Alternate stream entries for this dentry. */
185 struct ads_entry *ads_entries;
187 /* Number of references to the dentry tree itself, as in multiple
191 /* List of dentries in the hard link set */
192 struct list_head link_group_list;
195 /* Return hash of the "unnamed" (default) data stream. */
196 static inline const u8 *dentry_hash(const struct dentry *dentry)
198 /* If there are alternate data streams, the dentry hash field is zeroed
199 * out, and we need to find the hash in the un-named data stream (should
200 * be the first one, but check them in order just in case, and fall back
201 * to the dentry hash field if we can't find an unnamed data stream). */
202 for (u16 i = 0; i < dentry->num_ads; i++)
203 if (dentry->ads_entries[i].stream_name_len == 0)
204 return dentry->ads_entries[i].hash;
209 extern struct ads_entry *dentry_get_ads_entry(struct dentry *dentry,
210 const char *stream_name);
212 extern struct ads_entry *dentry_add_ads(struct dentry *dentry,
213 const char *stream_name);
215 extern void dentry_remove_ads(struct dentry *dentry, struct ads_entry *entry);
217 extern const char *path_stream_name(const char *path);
219 extern u64 dentry_total_length(const struct dentry *dentry);
221 extern void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry);
223 extern void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf,
224 const struct lookup_table *table);
226 extern int for_dentry_in_tree(struct dentry *root,
227 int (*visitor)(struct dentry*, void*),
230 extern int for_dentry_in_tree_depth(struct dentry *root,
231 int (*visitor)(struct dentry*, void*),
234 extern int calculate_dentry_full_path(struct dentry *dentry, void *ignore);
235 extern void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p);
236 extern int change_dentry_name(struct dentry *dentry, const char *new_name);
237 extern int change_ads_name(struct ads_entry *entry, const char *new_name);
239 extern void unlink_dentry(struct dentry *dentry);
240 extern void link_dentry(struct dentry *dentry, struct dentry *parent);
242 extern int print_dentry(struct dentry *dentry, void *lookup_table);
243 extern int print_dentry_full_path(struct dentry *entry, void *ignore);
245 extern struct dentry *get_dentry(WIMStruct *w, const char *path);
246 extern struct dentry *get_parent_dentry(WIMStruct *w, const char *path);
247 extern struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
249 extern void dentry_update_all_timestamps(struct dentry *dentry);
250 extern void init_dentry(struct dentry *dentry, const char *name);
251 extern struct dentry *new_dentry(const char *name);
253 extern void free_dentry(struct dentry *dentry);
254 extern void free_dentry_tree(struct dentry *root,
255 struct lookup_table *lookup_table,
256 bool lt_decrement_refcnt);
257 extern int increment_dentry_refcnt(struct dentry *dentry, void *ignore);
258 extern int decrement_dentry_refcnt(struct dentry *dentry, void *ignore);
260 extern void calculate_dir_tree_statistics(struct dentry *root,
261 struct lookup_table *table,
264 u64 *total_bytes_ret,
265 u64 *hard_link_bytes_ret);
267 extern int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
268 u64 offset, struct dentry *dentry);
270 extern int read_dentry_tree(const u8 metadata_resource[],
271 u64 metadata_resource_len, struct dentry *dentry);
273 extern u8 *write_dentry_tree(const struct dentry *tree, u8 *p);
275 extern int dentry_set_symlink_buf(struct dentry *dentry,
276 const u8 symlink_buf_hash[]);
278 /* Inline utility functions for WIMDentries */
281 static inline bool dentry_is_root(const struct dentry *dentry)
283 return dentry->parent == dentry;
286 static inline bool dentry_is_first_sibling(const struct dentry *dentry)
288 return dentry_is_root(dentry) || dentry->parent->children == dentry;
291 static inline bool dentry_is_only_child(const struct dentry *dentry)
293 return dentry->next == dentry;
296 static inline bool dentry_is_directory(const struct dentry *dentry)
298 return (dentry->attributes & FILE_ATTRIBUTE_DIRECTORY)
299 && !(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
302 /* For our purposes, we consider "real" symlinks and "junction points" to both
304 static inline bool dentry_is_symlink(const struct dentry *dentry)
306 return (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT)
307 && ((dentry->reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK) ||
308 dentry->reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
311 static inline bool dentry_is_regular_file(const struct dentry *dentry)
313 return !dentry_is_directory(dentry) && !dentry_is_symlink(dentry);
316 static inline bool dentry_is_empty_directory(const struct dentry *dentry)
318 return dentry_is_directory(dentry) && dentry->children == NULL;