]> wimlib.net Git - wimlib/blob - include/wimlib/dentry.h
6597e7776e77518205759d8d457ade603902abee
[wimlib] / include / wimlib / dentry.h
1 #ifndef _WIMLIB_DENTRY_H
2 #define _WIMLIB_DENTRY_H
3
4 #include "wimlib/case.h"
5 #include "wimlib/compiler.h"
6 #include "wimlib/inode.h"
7 #include "wimlib/list.h"
8 #include "wimlib/rbtree.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 red-black tree of child dentries, sorted by
56          * case sensitive long name. */
57         struct rb_node rb_node;
58
59         /* Node for the parent's red-black tree of child dentries, sorted by
60          * case insensitive long name. */
61         struct rb_node rb_node_case_insensitive;
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 case_insensitive_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         /* For extraction operations, this flag will be set on dentries in the
80          * tree being extracted.  Otherwise this will always be 0.  */
81         u8 in_extraction_tree : 1;
82
83         /* For extraction operations, this flag will be set when a dentry in the
84          * tree being extracted is not being extracted for some reason (file
85          * type not supported by target filesystem, contains invalid characters,
86          * or not in one of the multiple sub-trees being extracted).  Otherwise
87          * this will always be 0.  */
88         u8 extraction_skipped : 1;
89
90         /* During extraction extractions, this flag will be set after the
91          * "skeleton" of the dentry has been extracted.  */
92         u8 skeleton_extracted : 1;
93
94         /* When capturing from a NTFS volume using NTFS-3g, this flag is set on
95          * dentries that were created from a filename in the WIN32 or WIN32+DOS
96          * namespaces rather than the POSIX namespace.  Otherwise this will
97          * always be 0.  */
98         u8 is_win32_name : 1;
99
100         /* When verifying the dentry tree after reading it into memory, this
101          * flag will be set on all dentries in a hard link group that have a
102          * nonempty DOS name except one.  This is because it is supposed to be
103          * illegal (on NTFS, at least) for a single inode to have multiple DOS
104          * names.  */
105         u8 dos_name_invalid : 1;
106
107         u8 tmp_flag : 1;
108
109         u8 was_hardlinked : 1;
110
111         /* Temporary list field used to make lists of dentries in a few places.
112          * */
113         struct list_head tmp_list;
114
115         /* Linked list node that places this dentry in the list of aliases for
116          * its inode (d_inode) */
117         struct list_head d_alias;
118
119         /* The parent of this directory entry. */
120         struct wim_dentry *parent;
121
122         /* 'length' and 'subdir_offset' are only used while reading and writing
123          * this dentry; see the corresponding field in
124          * `struct wim_dentry_on_disk' for explanation.  */
125         u64 length;
126         u64 subdir_offset;
127
128         /* These correspond to the two unused fields in the on-disk WIM dentry;
129          * we read them into memory so we can write them unchanged.  These
130          * fields are set to 0 on new dentries.  */
131         u64 d_unused_1;
132         u64 d_unused_2;
133
134         /* Pointer to the UTF-16LE short filename (malloc()ed buffer), or NULL
135          * if this dentry has no short name.  */
136         utf16lechar *short_name;
137
138         /* Pointer to the UTF-16LE filename (malloc()ed buffer), or NULL if this
139          * dentry has no filename.  */
140         utf16lechar *file_name;
141
142         /* Full path to this dentry in the WIM, in platform-dependent tchars
143          * that can be printed without conversion.  By default this field will
144          * be NULL and will only be calculated on-demand by the
145          * calculate_dentry_full_path() or dentry_full_path() functions.  */
146         tchar *_full_path;
147
148         /* (Extraction only) Actual name to extract this dentry as, along with
149          * its length in tchars excluding the NULL terminator.  This usually
150          * will be the same as file_name, with the character encoding converted
151          * if needed.  But if file_name contains characters not accepted on the
152          * current platform, then this may be set slightly differently from
153          * file_name.  This will be either NULL or a malloc()ed buffer that may
154          * alias file_name.  */
155         tchar *extraction_name;
156         size_t extraction_name_nchars;
157 };
158
159 #define rbnode_dentry(node) container_of(node, struct wim_dentry, rb_node)
160
161 static inline bool
162 dentry_is_first_in_inode(const struct wim_dentry *dentry)
163 {
164         return inode_first_dentry(dentry->d_inode) == dentry;
165 }
166
167 extern u64
168 dentry_out_total_length(const struct wim_dentry *dentry);
169
170 extern int
171 for_dentry_in_tree(struct wim_dentry *root,
172                    int (*visitor)(struct wim_dentry*, void*),
173                    void *args);
174
175 extern int
176 for_dentry_in_rbtree(struct rb_node *node,
177                      int (*visitor)(struct wim_dentry *, void *),
178                      void *arg);
179
180 extern int
181 for_dentry_child(const struct wim_dentry *dentry,
182                  int (*visitor)(struct wim_dentry *, void *),
183                  void *arg);
184
185 extern int
186 for_dentry_in_tree_depth(struct wim_dentry *root,
187                          int (*visitor)(struct wim_dentry*, void*),
188                          void *args);
189
190 extern void
191 calculate_subdir_offsets(struct wim_dentry *dentry, u64 *subdir_offset_p);
192
193 extern int
194 dentry_set_name(struct wim_dentry *dentry, const tchar *new_name);
195
196 extern struct wim_dentry *
197 get_dentry(struct WIMStruct *wim, const tchar *path,
198            CASE_SENSITIVITY_TYPE case_type);
199
200 extern struct wim_dentry *
201 get_dentry_child_with_name(const struct wim_dentry *dentry,
202                            const tchar *name,
203                            CASE_SENSITIVITY_TYPE case_type);
204
205 extern struct wim_dentry *
206 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
207                                    const utf16lechar *name,
208                                    size_t name_nbytes,
209                                    CASE_SENSITIVITY_TYPE case_type);
210
211 extern struct wim_dentry *
212 get_parent_dentry(struct WIMStruct *wim, const tchar *path,
213                   CASE_SENSITIVITY_TYPE case_type);
214
215 #ifdef WITH_FUSE
216
217 #define LOOKUP_FLAG_ADS_OK              0x00000001
218 #define LOOKUP_FLAG_DIRECTORY_OK        0x00000002
219
220 extern int
221 wim_pathname_to_stream(WIMStruct *wim,
222                        const tchar *path,
223                        int lookup_flags,
224                        struct wim_dentry **dentry_ret,
225                        struct wim_lookup_table_entry **lte_ret,
226                        u16 *stream_idx_ret);
227 #endif
228
229 extern int
230 print_dentry(struct wim_dentry *dentry, void *lookup_table);
231
232 extern int
233 print_dentry_full_path(struct wim_dentry *entry, void *ignore);
234
235 extern int
236 calculate_dentry_full_path(struct wim_dentry *dentry);
237
238 extern int
239 calculate_dentry_tree_full_paths(struct wim_dentry *root);
240
241 extern tchar *
242 dentry_full_path(struct wim_dentry *dentry);
243
244 extern int
245 new_dentry(const tchar *name, struct wim_dentry **dentry_ret);
246
247 extern int
248 new_dentry_with_inode(const tchar *name, struct wim_dentry **dentry_ret);
249
250 extern int
251 new_dentry_with_timeless_inode(const tchar *name, struct wim_dentry **dentry_ret);
252
253 extern void
254 dentry_tree_clear_inode_visited(struct wim_dentry *root);
255
256 extern int
257 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret);
258
259 extern void
260 free_dentry(struct wim_dentry *dentry);
261
262 extern void
263 put_dentry(struct wim_dentry *dentry);
264
265 extern void
266 free_dentry_tree(struct wim_dentry *root,
267                  struct wim_lookup_table *lookup_table);
268
269 extern void
270 unlink_dentry(struct wim_dentry *dentry);
271
272 extern struct wim_dentry *
273 dentry_add_child(struct wim_dentry * restrict parent,
274                  struct wim_dentry * restrict child);
275
276 extern int
277 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to,
278                 CASE_SENSITIVITY_TYPE case_type);
279
280
281 extern int
282 read_dentry(const u8 * restrict metadata_resource,
283             u64 metadata_resource_len, u64 offset,
284             struct wim_dentry * restrict dentry);
285
286 extern int
287 read_dentry_tree(const u8 * restrict metadata_resource,
288                  u64 metadata_resource_len,
289                  struct wim_dentry * restrict dentry);
290
291 extern u8 *
292 write_dentry_tree(const struct wim_dentry * restrict tree,
293                   u8 * restrict p);
294
295 static inline bool
296 dentry_is_root(const struct wim_dentry *dentry)
297 {
298         return dentry->parent == dentry;
299 }
300
301 static inline bool
302 dentry_is_directory(const struct wim_dentry *dentry)
303 {
304         return inode_is_directory(dentry->d_inode);
305 }
306
307 static inline bool
308 dentry_has_children(const struct wim_dentry *dentry)
309 {
310         return inode_has_children(dentry->d_inode);
311 }
312
313 static inline bool
314 dentry_has_short_name(const struct wim_dentry *dentry)
315 {
316         return dentry->short_name_nbytes != 0;
317 }
318
319 static inline bool
320 dentry_has_long_name(const struct wim_dentry *dentry)
321 {
322         return dentry->file_name_nbytes != 0;
323 }
324
325 extern int
326 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list);
327 #endif /* _WIMLIB_DENTRY_H */