1 #ifndef _WIMLIB_DENTRY_H
2 #define _WIMLIB_DENTRY_H
3
4 #include "wimlib/avl_tree.h"
5 #include "wimlib/case.h"
6 #include "wimlib/compiler.h"
7 #include "wimlib/inode.h"
8 #include "wimlib/list.h"
9 #include "wimlib/types.h"
10
11 struct wim_inode;
12 struct blob_table;
13
14 /* Base size of a WIM dentry in the on-disk format, up to and including the file
15 * name length. This does not include the variable-length file name, short
16 * name, extra stream entries, and padding to 8-byte boundaries. */
17 #define WIM_DENTRY_DISK_SIZE 102
18
19 /*
20 * In-memory structure for a WIM directory entry (dentry). There is a directory
21 * tree for each image in the WIM.
22 *
23 * Note that this is a directory entry and not an inode. Since NTFS allows hard
24 * links, it's possible for an NTFS inode to correspond to multiple WIM
25 * dentries. The hard link group ID field of the on-disk WIM dentry tells us
26 * the number of the NTFS inode that the dentry corresponds to (and this gets
27 * placed in d_inode->i_ino).
28 *
29 * Unfortunately, WIM files do not have an analogue to an inode; instead certain
30 * information, such as file attributes, the security descriptor, and streams is
31 * replicated in each hard-linked dentry, even though this information really is
32 * associated with an inode. In-memory, we fix up this flaw by allocating a
33 * `struct wim_inode' for each dentry that contains some of this duplicated
34 * information, then combining the inodes for each hard link group together.
35 * (See dentry_tree_fix_inodes().)
36 */
37 struct wim_dentry {
38 /* Pointer to the inode for this dentry. This will contain some
39 * information that was factored out of the on-disk WIM dentry as common
40 * to all dentries in a hard link group. */
41 struct wim_inode *d_inode;
42
43 /* Node for the parent's balanced binary search tree of child dentries
44 * keyed by filename (root i_children). */
45 struct avl_tree_node d_index_node;
46
47 /* The parent of this directory entry. (The root is its own parent.) */
48 struct wim_dentry *d_parent;
49
50 /* Linked list node that places this dentry in the list of aliases for
51 * its inode (d_inode) */
52 struct hlist_node d_alias_node;
53
54 /* Pointer to the UTF-16LE filename (malloc()ed buffer), or NULL if this
55 * dentry has no filename. */
56 utf16lechar *d_name;
57
58 /* Pointer to the UTF-16LE short filename (malloc()ed buffer), or NULL
59 * if this dentry has no short name. */
60 utf16lechar *d_short_name;
61
62 /* Length of 'd_name' in bytes, excluding the terminating null */
63 u16 d_name_nbytes;
64
65 /* Length of 'd_short_name' in bytes, excluding the terminating null */
66 u16 d_short_name_nbytes;
67
68 /* (Extraction only) Length of 'd_extraction_name' in _characters_,
69 * excluding the terminating null */
70 u16 d_extraction_name_nchars;
71
72 /* When capturing from an NTFS volume using NTFS-3G, this flag is set on
73 * dentries that were created from a filename in the WIN32 or WIN32+DOS
74 * namespaces rather than the POSIX namespace. Otherwise this will
75 * always be 0. */
76 u16 d_is_win32_name : 1;
77
78 /* Temporary flag; always reset to 0 when done using. */
79 u16 d_tmp_flag : 1;
80
81 /* Used by wimlib_update_image() */
82 u16 d_is_orphan : 1;
83
84 union {
85 /* The subdir offset is only used while reading and writing this
86 * dentry. See the corresponding field in `struct
87 * wim_dentry_on_disk' for explanation. */
88 u64 d_subdir_offset;
89
90 /* Temporary list field */
91 struct list_head d_tmp_list;
92 };
93
94 /* Full path to this dentry in the WIM, in platform-dependent tchars
95 * that can be printed without conversion. By default this field will
96 * be NULL and will only be calculated on-demand by the
97 * calculate_dentry_full_path() or dentry_full_path() functions. */
98 tchar *d_full_path;
99
100 /* (Extraction only) Actual name to extract this dentry as. This may be
101 * either in 'tchars' or in 'utf16lechars', depending on what encoding
102 * the extraction backend needs. This may alias 'd_name'. If it
103 * doesn't, it is an allocated buffer which must be freed. */
104 void *d_extraction_name;
105
106 /* (Extraction only) Linked list node that connects all dentries being
107 * extracted as part of the current extraction operation. */
108 struct list_head d_extraction_list_node;
109
110 /* (Extraction only) Pointer to the next alias of this dentry's inode
111 * that needs to be extracted as part of the current extraction
112 * operation, or NULL if this is the last alias. */
113 struct wim_dentry *d_next_extraction_alias;
114
115 #ifdef ENABLE_TEST_SUPPORT
116 struct wim_dentry *d_corresponding;
117 #endif
118 };
119
120 static inline bool
will_extract_dentry(const struct wim_dentry * dentry)121 will_extract_dentry(const struct wim_dentry *dentry)
122 {
123 return dentry->d_extraction_list_node.next != NULL;
124 }
125
126 extern size_t
127 dentry_out_total_length(const struct wim_dentry *dentry);
128
129 extern int
130 for_dentry_in_tree(struct wim_dentry *root,
131 int (*visitor)(struct wim_dentry *, void *), void *args);
132
133 /* Iterate through each @child dentry of the @dir directory inode in
134 * collation order. */
135 #define for_inode_child(child, dir) \
136 avl_tree_for_each_in_order((child), (dir)->i_children, \
137 struct wim_dentry, d_index_node)
138
139 /* Iterate through each @child dentry of the @parent dentry in
140 * collation order. */
141 #define for_dentry_child(child, parent) \
142 for_inode_child((child), (parent)->d_inode)
143
144 /* Iterate through each @child dentry of the @dir directory inode in
145 * postorder (safe for freeing the child dentries). */
146 #define for_inode_child_postorder(child, dir) \
147 avl_tree_for_each_in_postorder((child), (dir)->i_children, \
148 struct wim_dentry, d_index_node)
149
150 /* Iterate through each @child dentry of the @parent dentry in
151 * postorder (safe for freeing the child dentries). */
152 #define for_dentry_child_postorder(child, parent) \
153 for_inode_child_postorder((child), (parent)->d_inode)
154
155 /* Get any child dentry of the @dir directory inode. Requires
156 * inode_has_children(@dir) == true. */
157 #define inode_any_child(dir) \
158 avl_tree_entry((dir)->i_children, struct wim_dentry, d_index_node)
159
160 /* Get any child dentry of the @parent dentry. Requires
161 * dentry_has_children(@parent) == true. */
162 #define dentry_any_child(parent) \
163 inode_any_child((parent)->d_inode)
164
165 extern struct wim_dentry *
166 dentry_get_first_ci_match(struct wim_dentry *dentry);
167
168 extern struct wim_dentry *
169 dentry_get_next_ci_match(struct wim_dentry *dentry,
170 struct wim_dentry *ci_match);
171
172 /* Iterate through all other dentries which have the same case insensitive name
173 * as the one given. */
174 #define dentry_for_each_ci_match(ci_match, dentry) \
175 for ((ci_match) = dentry_get_first_ci_match((dentry)); \
176 (ci_match); \
177 (ci_match) = dentry_get_next_ci_match((dentry), (ci_match)))
178
179 extern void
180 calculate_subdir_offsets(struct wim_dentry *root, u64 *subdir_offset_p);
181
182 extern int
183 dentry_set_name(struct wim_dentry *dentry, const tchar *name);
184
185 extern int
186 dentry_set_name_utf16le(struct wim_dentry *dentry, const utf16lechar *name,
187 size_t name_nbytes);
188
189 extern struct wim_dentry *
190 get_dentry(WIMStruct *wim, const tchar *path, CASE_SENSITIVITY_TYPE case_type);
191
192 extern struct wim_dentry *
193 get_dentry_child_with_name(const struct wim_dentry *dentry, const tchar *name,
194 CASE_SENSITIVITY_TYPE case_type);
195
196 extern struct wim_dentry *
197 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
198 const utf16lechar *name,
199 size_t name_nbytes,
200 CASE_SENSITIVITY_TYPE case_type);
201
202 extern struct wim_dentry *
203 get_parent_dentry(WIMStruct *wim, const tchar *path,
204 CASE_SENSITIVITY_TYPE case_type);
205
206 extern int
207 calculate_dentry_full_path(struct wim_dentry *dentry);
208
209 extern tchar *
210 dentry_full_path(struct wim_dentry *dentry);
211
212 extern int
213 new_dentry_with_new_inode(const tchar *name, bool set_timestamps,
214 struct wim_dentry **dentry_ret);
215
216 extern int
217 new_dentry_with_existing_inode(const tchar *name, struct wim_inode *inode,
218 struct wim_dentry **dentry_ret);
219
220 extern int
221 new_filler_directory(struct wim_dentry **dentry_ret);
222
223 extern void
224 free_dentry(struct wim_dentry *dentry);
225
226 extern void
227 free_dentry_tree(struct wim_dentry *root, struct blob_table *blob_table);
228
229 extern void
230 unlink_dentry(struct wim_dentry *dentry);
231
232 extern struct wim_dentry *
233 dentry_add_child(struct wim_dentry *parent, struct wim_dentry *child);
234
235 struct update_command_journal;
236
237 extern int
238 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to,
239 CASE_SENSITIVITY_TYPE case_type,
240 struct update_command_journal *j);
241
242
243 extern int
244 read_dentry_tree(const u8 *buf, size_t buf_len,
245 u64 root_offset, struct wim_dentry **root_ret);
246
247 extern u8 *
248 write_dentry_tree(struct wim_dentry *root, u8 *p);
249
250 static inline bool
dentry_is_root(const struct wim_dentry * dentry)251 dentry_is_root(const struct wim_dentry *dentry)
252 {
253 return dentry->d_parent == dentry;
254 }
255
256 static inline bool
dentry_is_directory(const struct wim_dentry * dentry)257 dentry_is_directory(const struct wim_dentry *dentry)
258 {
259 return inode_is_directory(dentry->d_inode);
260 }
261
262 static inline bool
dentry_has_children(const struct wim_dentry * dentry)263 dentry_has_children(const struct wim_dentry *dentry)
264 {
265 return inode_has_children(dentry->d_inode);
266 }
267
268 static inline bool
dentry_has_long_name(const struct wim_dentry * dentry)269 dentry_has_long_name(const struct wim_dentry *dentry)
270 {
271 return dentry->d_name_nbytes != 0;
272 }
273
274 static inline bool
dentry_has_short_name(const struct wim_dentry * dentry)275 dentry_has_short_name(const struct wim_dentry *dentry)
276 {
277 return dentry->d_short_name_nbytes != 0;
278 }
279
280 #endif /* _WIMLIB_DENTRY_H */
281