]> wimlib.net Git - wimlib/blob - include/wimlib/dentry.h
edd99209fb9c70b9bbbb7b92653aed88520fc5a8
[wimlib] / include / wimlib / dentry.h
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 wim_lookup_table;
13 struct wim_lookup_table_entry;
14 struct wim_security_data;
15
16 /* Base size of a WIM dentry in the on-disk format, up to and including the file
17  * name length.  This does not include the variable-length file name, short
18  * name, alternate data stream entries, and padding to 8-byte boundaries.  */
19 #define WIM_DENTRY_DISK_SIZE 102
20
21 /*
22  * In-memory structure for a WIM directory entry (dentry).  There is a directory
23  * tree for each image in the WIM.
24  *
25  * Note that this is a directory entry and not an inode.  Since NTFS allows hard
26  * links, it's possible for a NTFS inode to correspond to multiple WIM dentries.
27  * The hard link group ID field of the on-disk WIM dentry tells us the number of
28  * the NTFS inode that the dentry corresponds to (and this gets placed in
29  * d_inode->i_ino).
30  *
31  * Unfortunately, WIM files do not have an analogue to an inode; instead certain
32  * information, such as file attributes, the security descriptor, and file
33  * streams is replicated in each hard-linked dentry, even though this
34  * information really is associated with an inode.  In-memory, we fix up this
35  * flaw by allocating a `struct wim_inode' for each dentry that contains some of
36  * this duplicated information, then combining the inodes for each hard link
37  * group together.
38  *
39  * Confusingly, it's possible for stream information to be missing from a dentry
40  * in a hard link set, in which case the stream information needs to be gotten
41  * from one of the other dentries in the hard link set.  In addition, it is
42  * possible for dentries to have inconsistent security IDs, file attributes, or
43  * file streams when they share the same hard link ID (don't even ask.  I hope
44  * that Microsoft may have fixed this problem, since I've only noticed it in the
45  * 'install.wim' for Windows 7).  For those dentries, we have to use the
46  * conflicting fields to split up the hard link groups.  (See
47  * dentry_tree_fix_inodes() in inode_fixup.c.)
48  */
49 struct wim_dentry {
50         /* Pointer to the inode for this dentry.  This will contain some
51          * information that was factored out of the on-disk WIM dentry as common
52          * to all dentries in a hard link group.  */
53         struct wim_inode *d_inode;
54
55         /* Node for the parent's balanced binary search tree of child dentries
56          * sorted by case sensitive long name (root i_children).  */
57         struct avl_tree_node d_index_node;
58
59         /* Node for the parent's balanced binary search tree of child dentries,
60          * sorted by case insensitive long name (root i_children_ci). */
61         struct avl_tree_node d_index_node_ci;
62
63         /* List of dentries in a directory that have different case sensitive
64          * long names but share the same case insensitive long name.  */
65         struct list_head d_ci_conflict_list;
66
67         /* Length of UTF-16LE encoded short filename, in bytes, not including
68          * the terminating zero wide-character. */
69         u16 short_name_nbytes;
70
71         /* Length of UTF-16LE encoded "long" file name, in bytes, not including
72          * the terminating null character. */
73         u16 file_name_nbytes;
74
75         /* Length of full path name encoded using "tchars", in bytes, not
76          * including the terminating null character. */
77         u32 full_path_nbytes;
78
79         /* During extraction extractions, this flag will be set after the
80          * "skeleton" of the dentry has been extracted.  */
81         u8 skeleton_extracted : 1;
82
83         /* When capturing from a NTFS volume using NTFS-3g, this flag is set on
84          * dentries that were created from a filename in the WIN32 or WIN32+DOS
85          * namespaces rather than the POSIX namespace.  Otherwise this will
86          * always be 0.  */
87         u8 is_win32_name : 1;
88
89         /* Temporary flag; always reset to 0 when done using.  */
90         u8 tmp_flag : 1;
91
92         /* Set to 1 if this name was extracted as a link, so no streams need to
93          * be extracted to it.  */
94         u8 was_linked : 1;
95
96         /* Used by wimlib_update_image()  */
97         u8 is_orphan : 1;
98
99         /* Temporary list field  */
100         struct list_head tmp_list;
101
102         /* Links list of dentries being extracted  */
103         struct list_head extraction_list;
104
105         /* Linked list node that places this dentry in the list of aliases for
106          * its inode (d_inode) */
107         struct list_head d_alias;
108
109         /* The parent of this directory entry. */
110         struct wim_dentry *parent;
111
112         /* 'length' and 'subdir_offset' are only used while reading and writing
113          * this dentry; see the corresponding field in
114          * `struct wim_dentry_on_disk' for explanation.  */
115         u64 length;
116         u64 subdir_offset;
117
118         /* Pointer to the UTF-16LE short filename (malloc()ed buffer), or NULL
119          * if this dentry has no short name.  */
120         utf16lechar *short_name;
121
122         /* Pointer to the UTF-16LE filename (malloc()ed buffer), or NULL if this
123          * dentry has no filename.  */
124         utf16lechar *file_name;
125
126         /* Full path to this dentry in the WIM, in platform-dependent tchars
127          * that can be printed without conversion.  By default this field will
128          * be NULL and will only be calculated on-demand by the
129          * calculate_dentry_full_path() or dentry_full_path() functions.  */
130         tchar *_full_path;
131
132         /* (Extraction only) Actual name to extract this dentry as, along with
133          * its length in tchars excluding the NULL terminator.  This usually
134          * will be the same as file_name, with the character encoding converted
135          * if needed.  But if file_name contains characters not accepted on the
136          * current platform, then this may be set slightly differently from
137          * file_name.  This will be either NULL or a malloc()ed buffer that may
138          * alias file_name.  */
139         tchar *extraction_name;
140         size_t extraction_name_nchars;
141 };
142
143 static inline bool
144 dentry_is_first_in_inode(const struct wim_dentry *dentry)
145 {
146         return inode_first_dentry(dentry->d_inode) == dentry;
147 }
148
149 extern u64
150 dentry_out_total_length(const struct wim_dentry *dentry);
151
152 extern int
153 for_dentry_in_tree(struct wim_dentry *root,
154                    int (*visitor)(struct wim_dentry*, void*),
155                    void *args);
156
157 extern int
158 for_dentry_in_tree_depth(struct wim_dentry *root,
159                          int (*visitor)(struct wim_dentry*, void*),
160                          void *args);
161
162 /* Iterate through each @child dentry of the @dir directory inode,
163  * in sorted order (by case sensitive name).  */
164 #define for_inode_child(child, dir)                                             \
165         avl_tree_for_each_in_order((child), (dir)->i_children,                  \
166                                    struct wim_dentry, d_index_node)
167
168 /* Iterate through each @child dentry of the @parent dentry,
169  * in sorted order (by case sensitive name).  */
170 #define for_dentry_child(child, parent) \
171         for_inode_child((child), (parent)->d_inode)
172
173 /* Iterate through each @child dentry of the @dir directory inode,
174  * in postorder (safe for freeing the child dentries).  */
175 #define for_inode_child_postorder(child, dir)                           \
176         avl_tree_for_each_in_postorder((child), (dir)->i_children,      \
177                                        struct wim_dentry, d_index_node)
178
179 /* Iterate through each @child dentry of the @parent dentry,
180  * in postorder (safe for freeing the child dentries).  */
181 #define for_dentry_child_postorder(child, parent) \
182         for_inode_child_postorder((child), (parent)->d_inode)
183
184 /* Get any child dentry of the @dir directory inode.  Requires
185  * inode_has_children(@dir) == true.  */
186 #define inode_any_child(dir)    \
187         avl_tree_entry((dir)->i_children, struct wim_dentry, d_index_node)
188
189 /* Get any child dentry of the @parent dentry.  Requires
190  * dentry_has_children(@parent) == true.  */
191 #define dentry_any_child(parent) \
192         inode_any_child((parent)->d_inode)
193
194 extern void
195 calculate_subdir_offsets(struct wim_dentry *root, u64 *subdir_offset_p);
196
197 extern int
198 dentry_set_name(struct wim_dentry *dentry, const tchar *new_name);
199
200 extern int
201 dentry_set_name_utf16le(struct wim_dentry *dentry, const utf16lechar *new_name);
202
203 extern struct wim_dentry *
204 get_dentry(struct WIMStruct *wim, const tchar *path,
205            CASE_SENSITIVITY_TYPE case_type);
206
207 extern struct wim_dentry *
208 get_dentry_child_with_name(const struct wim_dentry *dentry,
209                            const tchar *name,
210                            CASE_SENSITIVITY_TYPE case_type);
211
212 extern struct wim_dentry *
213 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
214                                    const utf16lechar *name,
215                                    size_t name_nbytes,
216                                    CASE_SENSITIVITY_TYPE case_type);
217
218 extern struct wim_dentry *
219 get_parent_dentry(struct WIMStruct *wim, const tchar *path,
220                   CASE_SENSITIVITY_TYPE case_type);
221
222 #ifdef WITH_FUSE
223
224 #define LOOKUP_FLAG_ADS_OK              0x00000001
225 #define LOOKUP_FLAG_DIRECTORY_OK        0x00000002
226
227 extern int
228 wim_pathname_to_stream(WIMStruct *wim,
229                        const tchar *path,
230                        int lookup_flags,
231                        struct wim_dentry **dentry_ret,
232                        struct wim_lookup_table_entry **lte_ret,
233                        u16 *stream_idx_ret);
234 #endif
235
236 extern int
237 calculate_dentry_full_path(struct wim_dentry *dentry);
238
239 extern tchar *
240 dentry_full_path(struct wim_dentry *dentry);
241
242 extern int
243 new_dentry(const tchar *name, struct wim_dentry **dentry_ret);
244
245 extern int
246 new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret);
247
248 extern int
249 new_dentry_with_timeless_inode(const tchar *name, struct wim_dentry **dentry_ret);
250
251 extern void
252 dentry_tree_clear_inode_visited(struct wim_dentry *root);
253
254 extern int
255 new_filler_directory(struct wim_dentry **dentry_ret);
256
257 extern void
258 free_dentry(struct wim_dentry *dentry);
259
260 extern void
261 put_dentry(struct wim_dentry *dentry);
262
263 extern void
264 free_dentry_tree(struct wim_dentry *root,
265                  struct wim_lookup_table *lookup_table);
266
267 extern void
268 unlink_dentry(struct wim_dentry *dentry);
269
270 extern struct wim_dentry *
271 dentry_add_child(struct wim_dentry *parent, struct wim_dentry *child);
272
273 struct update_command_journal;
274
275 extern int
276 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to,
277                 CASE_SENSITIVITY_TYPE case_type,
278                 struct update_command_journal *j);
279
280
281 extern int
282 read_dentry_tree(const u8 *buf, size_t buf_len,
283                  u64 root_offset, struct wim_dentry **root_ret);
284
285 extern u8 *
286 write_dentry_tree(struct wim_dentry *root, u8 *p);
287
288 static inline bool
289 dentry_is_root(const struct wim_dentry *dentry)
290 {
291         return dentry->parent == dentry;
292 }
293
294 static inline bool
295 dentry_is_directory(const struct wim_dentry *dentry)
296 {
297         return inode_is_directory(dentry->d_inode);
298 }
299
300 static inline bool
301 dentry_has_children(const struct wim_dentry *dentry)
302 {
303         return inode_has_children(dentry->d_inode);
304 }
305
306 static inline bool
307 dentry_has_short_name(const struct wim_dentry *dentry)
308 {
309         return dentry->short_name_nbytes != 0;
310 }
311
312 static inline bool
313 dentry_has_long_name(const struct wim_dentry *dentry)
314 {
315         return dentry->file_name_nbytes != 0;
316 }
317
318 extern int
319 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list);
320 #endif /* _WIMLIB_DENTRY_H */