]> wimlib.net Git - wimlib/blob - include/wimlib/dentry.h
Use macros to iterate through extraction aliases
[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 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          * sorted by case sensitive long name (root i_children).  */
45         struct avl_tree_node d_index_node;
46
47         /* Node for the parent's balanced binary search tree of child dentries,
48          * sorted by case insensitive long name (root i_children_ci). */
49         struct avl_tree_node d_index_node_ci;
50
51         /* List of dentries in a directory that have different case sensitive
52          * long names but share the same case insensitive long name.  */
53         struct list_head d_ci_conflict_list;
54
55         /* The parent of this directory entry. (The root is its own parent.)  */
56         struct wim_dentry *d_parent;
57
58         /* Linked list node that places this dentry in the list of aliases for
59          * its inode (d_inode) */
60         struct hlist_node d_alias;
61
62         /* Pointer to the UTF-16LE short filename (malloc()ed buffer), or NULL
63          * if this dentry has no short name.  */
64         utf16lechar *short_name;
65
66         /* Pointer to the UTF-16LE filename (malloc()ed buffer), or NULL if this
67          * dentry has no filename.  */
68         utf16lechar *file_name;
69
70         /* Length of UTF-16LE encoded short filename, in bytes, not including
71          * the terminating zero wide-character. */
72         u16 short_name_nbytes;
73
74         /* Length of UTF-16LE encoded "long" file name, in bytes, not including
75          * the terminating null character. */
76         u16 file_name_nbytes;
77
78         /* When capturing from an NTFS volume using NTFS-3g, this flag is set on
79          * dentries that were created from a filename in the WIN32 or WIN32+DOS
80          * namespaces rather than the POSIX namespace.  Otherwise this will
81          * always be 0.  */
82         u8 is_win32_name : 1;
83
84         /* Temporary flag; always reset to 0 when done using.  */
85         u8 tmp_flag : 1;
86
87         /* Used by wimlib_update_image()  */
88         u8 is_orphan : 1;
89
90         union {
91                 /* 'subdir_offset' is only used while reading and writing this
92                  * dentry.  See the corresponding field in `struct
93                  * wim_dentry_on_disk' for explanation.  */
94                 u64 subdir_offset;
95
96                 /* Temporary list field  */
97                 struct list_head tmp_list;
98         };
99
100         /* Full path to this dentry in the WIM, in platform-dependent tchars
101          * that can be printed without conversion.  By default this field will
102          * be NULL and will only be calculated on-demand by the
103          * calculate_dentry_full_path() or dentry_full_path() functions.  */
104         tchar *_full_path;
105
106         /* (Extraction only) Actual name to extract this dentry as.  This may be
107          * either in 'tchars' or in 'utf16lechars', depending on what encoding
108          * the extraction backend needs.  This may alias 'file_name'.  If it
109          * doesn't, it is an allocated buffer which must be freed.  */
110         void *d_extraction_name;
111
112         /* (Extraction only) Number of characters in d_extraction_name.  */
113         size_t d_extraction_name_nchars;
114
115         /* (Extraction only) Linked list node that connects all dentries being
116          * extracted as part of the current extraction operation.  */
117         struct list_head d_extraction_list_node;
118
119         /* (Extraction only) Linked list node that connects all dentries being
120          * extracted as aliases of the same inode as part of the current
121          * extraction operation.  */
122         struct list_head d_extraction_alias_node;
123 };
124
125 static inline bool
126 will_extract_dentry(const struct wim_dentry *dentry)
127 {
128         return dentry->d_extraction_list_node.next != NULL;
129 }
130
131 extern size_t
132 dentry_out_total_length(const struct wim_dentry *dentry);
133
134 extern int
135 for_dentry_in_tree(struct wim_dentry *root,
136                    int (*visitor)(struct wim_dentry*, void*),
137                    void *args);
138
139 /* Iterate through each @child dentry of the @dir directory inode,
140  * in sorted order by case sensitive name.  */
141 #define for_inode_child(child, dir)                                     \
142         avl_tree_for_each_in_order((child), (dir)->i_children,          \
143                                    struct wim_dentry, d_index_node)
144
145 /* Iterate through each @child dentry of the @parent dentry,
146  * in sorted order by case sensitive name.  */
147 #define for_dentry_child(child, parent) \
148         for_inode_child((child), (parent)->d_inode)
149
150 /* Iterate through each @child dentry of the @dir directory inode,
151  * in sorted order by case insensitive name.  */
152 #define for_inode_child_case_insensitive(child, dir)                    \
153         avl_tree_for_each_in_order((child), (dir)->i_children_ci,       \
154                                    struct wim_dentry, d_index_node_ci)
155
156 /* Iterate through each @child dentry of the @parent dentry,
157  * in sorted order by case insensitive name.  */
158 #define for_dentry_child_case_insensitive(child, parent) \
159         for_inode_child_case_insensitive((child), (parent)->d_inode)
160
161 /* Iterate through each @child dentry of the @dir directory inode,
162  * in postorder (safe for freeing the child dentries).  */
163 #define for_inode_child_postorder(child, dir)                           \
164         avl_tree_for_each_in_postorder((child), (dir)->i_children,      \
165                                        struct wim_dentry, d_index_node)
166
167 /* Iterate through each @child dentry of the @parent dentry,
168  * in postorder (safe for freeing the child dentries).  */
169 #define for_dentry_child_postorder(child, parent) \
170         for_inode_child_postorder((child), (parent)->d_inode)
171
172 /* Get any child dentry of the @dir directory inode.  Requires
173  * inode_has_children(@dir) == true.  */
174 #define inode_any_child(dir)    \
175         avl_tree_entry((dir)->i_children, struct wim_dentry, d_index_node)
176
177 /* Get any child dentry of the @parent dentry.  Requires
178  * dentry_has_children(@parent) == true.  */
179 #define dentry_any_child(parent) \
180         inode_any_child((parent)->d_inode)
181
182 extern void
183 calculate_subdir_offsets(struct wim_dentry *root, u64 *subdir_offset_p);
184
185 extern int
186 dentry_set_name(struct wim_dentry *dentry, const tchar *new_name);
187
188 extern int
189 dentry_set_name_utf16le(struct wim_dentry *dentry, const utf16lechar *new_name,
190                         size_t new_name_nbytes);
191
192 extern struct wim_dentry *
193 get_dentry(struct WIMStruct *wim, const tchar *path,
194            CASE_SENSITIVITY_TYPE case_type);
195
196 extern struct wim_dentry *
197 get_dentry_child_with_name(const struct wim_dentry *dentry,
198                            const tchar *name,
199                            CASE_SENSITIVITY_TYPE case_type);
200
201 extern struct wim_dentry *
202 get_dentry_child_with_utf16le_name(const struct wim_dentry *dentry,
203                                    const utf16lechar *name,
204                                    size_t name_nbytes,
205                                    CASE_SENSITIVITY_TYPE case_type);
206
207 extern struct wim_dentry *
208 get_parent_dentry(struct WIMStruct *wim, const tchar *path,
209                   CASE_SENSITIVITY_TYPE case_type);
210
211 extern int
212 calculate_dentry_full_path(struct wim_dentry *dentry);
213
214 extern tchar *
215 dentry_full_path(struct wim_dentry *dentry);
216
217 extern int
218 new_dentry_with_new_inode(const tchar *name, bool set_timestamps,
219                           struct wim_dentry **dentry_ret);
220
221 extern int
222 new_dentry_with_existing_inode(const tchar *name, struct wim_inode *inode,
223                                struct wim_dentry **dentry_ret);
224
225 extern void
226 dentry_tree_clear_inode_visited(struct wim_dentry *root);
227
228 extern int
229 new_filler_directory(struct wim_dentry **dentry_ret);
230
231 extern void
232 free_dentry(struct wim_dentry *dentry);
233
234 extern void
235 free_dentry_tree(struct wim_dentry *root,
236                  struct blob_table *blob_table);
237
238 extern void
239 unlink_dentry(struct wim_dentry *dentry);
240
241 extern struct wim_dentry *
242 dentry_add_child(struct wim_dentry *parent, struct wim_dentry *child);
243
244 struct update_command_journal;
245
246 extern int
247 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to,
248                 CASE_SENSITIVITY_TYPE case_type,
249                 struct update_command_journal *j);
250
251
252 extern int
253 read_dentry_tree(const u8 *buf, size_t buf_len,
254                  u64 root_offset, struct wim_dentry **root_ret);
255
256 extern u8 *
257 write_dentry_tree(struct wim_dentry *root, u8 *p);
258
259 static inline bool
260 dentry_is_root(const struct wim_dentry *dentry)
261 {
262         return dentry->d_parent == dentry;
263 }
264
265 static inline bool
266 dentry_is_directory(const struct wim_dentry *dentry)
267 {
268         return inode_is_directory(dentry->d_inode);
269 }
270
271 static inline bool
272 dentry_has_children(const struct wim_dentry *dentry)
273 {
274         return inode_has_children(dentry->d_inode);
275 }
276
277 static inline bool
278 dentry_has_short_name(const struct wim_dentry *dentry)
279 {
280         return dentry->short_name_nbytes != 0;
281 }
282
283 static inline bool
284 dentry_has_long_name(const struct wim_dentry *dentry)
285 {
286         return dentry->file_name_nbytes != 0;
287 }
288 #endif /* _WIMLIB_DENTRY_H */