1 #ifndef _WIMLIB_DENTRY_H
2 #define _WIMLIB_DENTRY_H
8 /* Size of the struct dentry up to and including the file_name_len. */
9 #define WIM_DENTRY_DISK_SIZE 102
11 #define WIM_ADS_ENTRY_DISK_SIZE 38
15 * Reparse tags documented at
16 * http://msdn.microsoft.com/en-us/library/dd541667(v=prot.10).aspx
18 * IO_REPARSE_TAG_SYMLINK is the only one we really care about.
20 #define IO_REPARSE_TAG_RESERVED_ZERO 0x00000000
21 #define IO_REPARSE_TAG_RESERVED_ONE 0x00000001
22 #define IO_REPARSE_TAG_MOUNT_POINT 0xA0000003
23 #define IO_REPARSE_TAG_HSM 0xC0000004
24 #define IO_REPARSE_TAG_HSM2 0x80000006
25 #define IO_REPARSE_TAG_DRIVER_EXTENDER 0x80000005
26 #define IO_REPARSE_TAG_SIS 0x80000007
27 #define IO_REPARSE_TAG_DFS 0x8000000A
28 #define IO_REPARSE_TAG_DFSR 0x80000012
29 #define IO_REPARSE_TAG_FILTER_MANAGER 0x8000000B
30 #define IO_REPARSE_TAG_SYMLINK 0xA000000C
31 #endif /* !WITH_NTFS_3G */
33 #define FILE_ATTRIBUTE_READONLY 0x00000001
34 #define FILE_ATTRIBUTE_HIDDEN 0x00000002
35 #define FILE_ATTRIBUTE_SYSTEM 0x00000004
36 #define FILE_ATTRIBUTE_DIRECTORY 0x00000010
37 #define FILE_ATTRIBUTE_ARCHIVE 0x00000020
38 #define FILE_ATTRIBUTE_DEVICE 0x00000040
39 #define FILE_ATTRIBUTE_NORMAL 0x00000080
40 #define FILE_ATTRIBUTE_TEMPORARY 0x00000100
41 #define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
42 #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
43 #define FILE_ATTRIBUTE_COMPRESSED 0x00000800
44 #define FILE_ATTRIBUTE_OFFLINE 0x00001000
45 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
46 #define FILE_ATTRIBUTE_ENCRYPTED 0x00004000
47 #define FILE_ATTRIBUTE_VIRTUAL 0x00010000
49 /* Alternate data stream entry */
51 /* SHA-1 message digest of stream contents */
52 u8 hash[WIM_HASH_SIZE];
54 /* Length of stream name (UTF-16) */
57 /* Length of stream name (UTF-8) */
58 u16 stream_name_len_utf8;
60 /* Stream name (UTF-16) */
63 /* Stream name (UTF-8) */
64 char *stream_name_utf8;
67 static inline u64 ads_entry_length(const struct ads_entry *entry)
69 u64 len = WIM_ADS_ENTRY_DISK_SIZE + entry->stream_name_len + 2;
70 return (len + 7) & ~7;
73 /* In-memory structure for a directory entry. There is a directory tree for
74 * each image in the WIM. */
76 /* The parent of this directory entry. */
77 struct dentry *parent;
79 /* Linked list of sibling directory entries. */
84 /* Pointer to a child of this directory entry. */
85 struct dentry *children;
87 /* Size of directory entry, in bytes. Typical size is around 104 to 120
89 /* It is possible for the length field to be 0. This situation, which
90 * is undocumented, indicates the end of a list of sibling nodes in a
91 * directory. It also means the real length is 8, because the dentry
92 * included only the length field, but that takes up 8 bytes. */
96 /* The file attributes associated with this file. */
99 /* The index of the node in the security table that contains this file's
100 * security information. If -1, no security information exists for this
104 /* The offset, from the start of the metadata section, of this directory
105 * entry's child files. 0 if the directory entry has no children. */
108 /* Reserved for future disuse. Currently ignoring these fields. */
112 /* Timestamps for the entry. The timestamps are the number of
113 * 100-nanosecond intervals that have elapsed since 12:00 A.M., January
116 u64 last_access_time;
119 /* A hash of the file's contents. */
120 u8 hash[WIM_HASH_SIZE];
122 /* Identity of a reparse point. See
123 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
124 * for what a reparse point is. */
127 /* Although M$'s documentation does not tell you this, it seems that the
128 * reparse_reserved field does not actually exist. So the hard_link
129 * field directly follows the reparse_tag on disk. EXCEPT when the
130 * dentry is actually a reparse point... well, just take a look at the
131 * read_dentry() function. */
132 //u32 reparse_reserved;
134 /* If the reparse_reserved field existed, there would be a 4-byte gap
135 * here to align hard_link on an 8-byte field. However,
136 * reparse_reserved does not actually exist, so there is no gap here. */
138 /* If the file is part of a hard link set, all the directory entries in
139 * the set will share the same value for this field. */
142 /* Number of alternate data streams associated with this file. */
145 /* Length of short filename, in bytes, not including the terminating
146 * zero wide-character. */
149 /* Length of file name, in bytes, not including the terminating zero
153 /* Length of the filename converted into UTF-8, in bytes, not including
154 * the terminating zero byte. */
155 u16 file_name_utf8_len;
157 /* Pointer to the short filename */
160 /* Pointer to the filename. */
163 /* Pointer to the filename converted to UTF-8. */
164 char *file_name_utf8;
166 /* Full path to this dentry. */
167 char *full_path_utf8;
168 u32 full_path_utf8_len;
170 /* Alternate stream entries for this dentry. */
171 struct ads_entry *ads_entries;
173 /* Number of references to the dentry tree itself, as in multiple
178 /* Return hash of the "unnamed" (default) data stream. */
179 static inline const u8 *dentry_hash(const struct dentry *dentry)
181 /* If there are alternate data streams, the dentry hash field is zeroed
182 * out, and we need to find the hash in the un-named data stream (should
183 * be the first one, but check them in order just in case, and fall back
184 * to the dentry hash field if we can't find an unnamed data stream). */
185 for (u16 i = 0; i < dentry->num_ads; i++)
186 if (dentry->ads_entries[i].stream_name_len == 0)
187 return dentry->ads_entries[i].hash;
191 extern u64 dentry_total_length(const struct dentry *dentry);
193 extern void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry);
195 extern void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf,
196 const struct lookup_table *table);
198 extern int for_dentry_in_tree(struct dentry *root,
199 int (*visitor)(struct dentry*, void*),
202 extern int for_dentry_in_tree_depth(struct dentry *root,
203 int (*visitor)(struct dentry*, void*),
206 extern int calculate_dentry_full_path(struct dentry *dentry, void *ignore);
207 extern void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p);
208 extern int change_dentry_name(struct dentry *dentry, const char *new_name);
210 extern void unlink_dentry(struct dentry *dentry);
211 extern void link_dentry(struct dentry *dentry, struct dentry *parent);
213 extern int print_dentry(struct dentry *dentry, void *lookup_table);
214 extern int print_dentry_full_path(struct dentry *entry, void *ignore);
216 extern struct dentry *get_dentry(WIMStruct *w, const char *path);
217 extern struct dentry *get_parent_dentry(WIMStruct *w, const char *path);
218 extern struct dentry *get_dentry_child_with_name(const struct dentry *dentry,
220 extern void dentry_update_all_timestamps(struct dentry *dentry);
221 extern void init_dentry(struct dentry *dentry, const char *name);
222 extern struct dentry *new_dentry(const char *name);
224 extern void free_dentry(struct dentry *dentry);
225 extern void free_dentry_tree(struct dentry *root,
226 struct lookup_table *lookup_table,
227 bool lt_decrement_refcnt);
228 extern int increment_dentry_refcnt(struct dentry *dentry, void *ignore);
229 extern int decrement_dentry_refcnt(struct dentry *dentry, void *ignore);
231 extern void calculate_dir_tree_statistics(struct dentry *root,
232 struct lookup_table *table,
235 u64 *total_bytes_ret,
236 u64 *hard_link_bytes_ret);
238 extern int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
239 u64 offset, struct dentry *dentry);
241 extern int read_dentry_tree(const u8 metadata_resource[],
242 u64 metadata_resource_len, struct dentry *dentry);
244 extern u8 *write_dentry_tree(const struct dentry *tree, u8 *p);
246 /* Inline utility functions for WIMDentries */
249 * Returns true if @dentry has the UTF-8 file name @name that has length
252 static inline bool dentry_has_name(const struct dentry *dentry, const char *name,
255 if (dentry->file_name_utf8_len != name_len)
257 return memcmp(dentry->file_name_utf8, name, name_len) == 0;
260 static inline bool dentry_is_root(const struct dentry *dentry)
262 return dentry->parent == dentry;
265 static inline bool dentry_is_first_sibling(const struct dentry *dentry)
267 return dentry_is_root(dentry) || dentry->parent->children == dentry;
270 static inline bool dentry_is_only_child(const struct dentry *dentry)
272 return dentry->next == dentry;
275 static inline bool dentry_is_directory(const struct dentry *dentry)
277 return (dentry->attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
280 static inline bool dentry_is_regular_file(const struct dentry *dentry)
282 return !dentry_is_directory(dentry);
285 static inline bool dentry_is_empty_directory(const struct dentry *dentry)
287 return dentry_is_directory(dentry) && dentry->children == NULL;