]> wimlib.net Git - wimlib/blob - include/wimlib/inode.h
inode/blob cleanups
[wimlib] / include / wimlib / inode.h
1 #ifndef _WIMLIB_INODE_H
2 #define _WIMLIB_INODE_H
3
4 #include "wimlib/assert.h"
5 #include "wimlib/list.h"
6 #include "wimlib/sha1.h"
7 #include "wimlib/types.h"
8
9 struct avl_tree_node;
10 struct blob_descriptor;
11 struct blob_table;
12 struct wim_dentry;
13 struct wim_security_data;
14 struct wimfs_fd;
15
16 /* Valid values for the 'stream_type' field of a 'struct wim_inode_stream'  */
17 enum wim_inode_stream_type {
18
19         /* Data stream, may be unnamed (usual case) or named  */
20         STREAM_TYPE_DATA,
21
22         /* Reparse point stream.  This is the same as the data of the on-disk
23          * reparse point attribute, except that the first 8 bytes of the on-disk
24          * attribute are omitted.  The omitted bytes contain the reparse tag
25          * (which is instead stored in the on-disk WIM dentry), the reparse data
26          * size (which is redundant with the stream size), and a reserved field
27          * that is always zero.  */
28         STREAM_TYPE_REPARSE_POINT,
29
30         /* Encrypted data in the "EFSRPC raw data format" specified by [MS-EFSR]
31          * section 2.2.3.  This contains metadata for the Encrypting File System
32          * as well as the encrypted data of all the file's data streams.  */
33         STREAM_TYPE_EFSRPC_RAW_DATA,
34
35         /* Stream type could not be determined  */
36         STREAM_TYPE_UNKNOWN,
37 };
38
39 extern const utf16lechar NO_STREAM_NAME[1];
40
41 /*
42  * 'struct wim_inode_stream' describes a "stream", which associates a blob of
43  * data with an inode.  Each stream has a type and optionally a name.
44  *
45  * The most frequently seen kind of stream is the "unnamed data stream"
46  * (stream_type == STREAM_TYPE_DATA && stream_name == NO_STREAM_NAME), which is
47  * the "default file contents".  Many inodes just have an unnamed data stream
48  * and no other streams.  However, files on NTFS filesystems may have
49  * additional, "named" data streams, and this is supported by the WIM format.
50  *
51  * A "reparse point" is an inode with reparse data set.  The reparse data is
52  * stored in a stream of type STREAM_TYPE_REPARSE_POINT.  There should be only
53  * one such stream, and it should be unnamed.  However, it is possible for an
54  * inode to have both a reparse point stream and an unnamed data stream, and
55  * even named data streams as well.
56  */
57 struct wim_inode_stream {
58
59         /* The name of the stream or NO_STREAM_NAME.  */
60         utf16lechar *stream_name;
61
62         /*
63          * If 'stream_resolved' = 0, then 'stream_hash' is the SHA-1 message
64          * digest of the uncompressed data of this stream, or all zeroes if this
65          * stream is empty.
66          *
67          * If 'stream_resolved' = 1, then 'stream_blob' is a pointer directly to
68          * a descriptor for this stream's blob, or NULL if this stream is empty.
69          */
70         union {
71                 u8 _stream_hash[SHA1_HASH_SIZE];
72                 struct blob_descriptor *_stream_blob;
73         };
74
75         /* 'stream_resolved' determines whether 'stream_hash' or 'stream_blob'
76          * is valid as described above.  */
77         u32 stream_resolved : 1;
78
79         /* A unique identifier for this stream within the context of its inode.
80          * This stays constant even if the streams array is reallocated.  */
81         u32 stream_id : 28;
82
83         /* The type of this stream as one of the STREAM_TYPE_* values  */
84         u32 stream_type : 3;
85 };
86
87 /*
88  * WIM inode - a "file" in an image which may be accessible via multiple paths
89  *
90  * Note: in WIM files there is no true on-disk analogue of an inode; there are
91  * only directory entries, and some fields are duplicated among all links to a
92  * file.  However, wimlib uses inode structures internally to simplify handling
93  * of hard links.
94  */
95 struct wim_inode {
96
97         /*
98          * The collection of streams for this inode.  'i_streams' points to
99          * either 'i_embedded_streams' or an allocated array.
100          */
101         struct wim_inode_stream *i_streams;
102         struct wim_inode_stream i_embedded_streams[1];
103         unsigned i_num_streams;
104
105         /* Windows file attribute flags (FILE_ATTRIBUTE_*).  */
106         u32 i_attributes;
107
108         /* Root of a balanced binary search tree storing the child directory
109          * entries of this inode, if any.  Keyed by wim_dentry->file_name, case
110          * sensitively.  If this inode is not a directory or if it has no
111          * children then this will be an empty tree (NULL).  */
112         struct avl_tree_node *i_children;
113
114         /* Root of a balanced binary search tree storing the child directory
115          * entries of this inode, if any.  Keyed by wim_dentry->file_name, case
116          * insensitively.  If this inode is not a directory or if it has no
117          * children then this will be an empty tree (NULL).  */
118         struct avl_tree_node *i_children_ci;
119
120         /* List of dentries that are aliases for this inode.  There will be
121          * i_nlink dentries in this list.  */
122         struct list_head i_dentry;
123
124         /* Field to place this inode into a list. */
125         union {
126                 /* Hash list node- used in inode_fixup.c when the inodes are
127                  * placed into a hash table keyed by inode number and optionally
128                  * device number, in order to detect dentries that are aliases
129                  * for the same inode. */
130                 struct hlist_node i_hlist;
131
132                 /* Normal list node- used to connect all the inodes of a WIM
133                  * image into a single linked list referenced from the `struct
134                  * wim_image_metadata' for that image. */
135                 struct list_head i_list;
136         };
137
138         /* Number of dentries that are aliases for this inode.  */
139         u32 i_nlink;
140
141         /* Flag used to mark this inode as visited; this is used when visiting
142          * all the inodes in a dentry tree exactly once.  It will be 0 by
143          * default and must be cleared following the tree traversal, even in
144          * error paths.  */
145         u8 i_visited : 1;
146
147         /* Cached value  */
148         u8 i_can_externally_back : 1;
149
150         /* If not NULL, a pointer to the extra data that was read from the
151          * dentry.  This should be a series of tagged items, each of which
152          * represents a bit of extra metadata, such as the file's object ID.
153          * See tagged_items.c for more information.  */
154         void *i_extra;
155
156         /* Size of @i_extra buffer in bytes.  If 0, there is no extra data.  */
157         size_t i_extra_size;
158
159         /* Creation time, last access time, and last write time for this inode,
160          * in 100-nanosecond intervals since 12:00 a.m UTC January 1, 1601.
161          * They should correspond to the times gotten by calling GetFileTime()
162          * on Windows. */
163         u64 i_creation_time;
164         u64 i_last_access_time;
165         u64 i_last_write_time;
166
167         /* Corresponds to 'security_id' in `struct wim_dentry_on_disk':  The
168          * index of this inode's security descriptor in the WIM image's table of
169          * security descriptors, or -1.  Note: when a WIM image is loaded,
170          * wimlib sets out-of-bounds indices and values less than -1 in this
171          * field to -1.  So the extraction code need not do an upper bound check
172          * after checking for -1 (or equivalently < 0).  */
173         int32_t i_security_id;
174
175         /* Identity of a reparse point.  See
176          * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
177          * for what a reparse point is. */
178         u32 i_reparse_tag;
179
180         /* Unused/unknown fields that we just read into memory so we can
181          * re-write them unchanged.  */
182         u32 i_rp_unknown_1;
183         u16 i_rp_unknown_2;
184
185         /* Corresponds to not_rpfixed in `struct wim_dentry_on_disk':  Set to 0
186          * if reparse point fixups have been done.  Otherwise set to 1.  Note:
187          * this actually may reflect the SYMBOLIC_LINK_RELATIVE flag.
188          */
189         u16 i_not_rpfixed;
190
191         /* Inode number; corresponds to hard_link_group_id in the `struct
192          * wim_dentry_on_disk'.  */
193         u64 i_ino;
194
195         union {
196                 /* Device number, used only during image capture, so we can
197                  * identify hard linked files by the combination of inode number
198                  * and device number (rather than just inode number, which could
199                  * be ambigious if the captured tree spans a mountpoint).  Set
200                  * to 0 otherwise.  */
201                 u64 i_devno;
202
203                 /* Fields used only during extraction  */
204                 struct {
205                         /* List of aliases of this dentry that are being
206                          * extracted in the current extraction operation.  This
207                          * will be a (possibly nonproper) subset of the dentries
208                          * in the i_dentry list.  This list will be constructed
209                          * regardless of whether the extraction backend supports
210                          * hard links or not.  */
211                         struct list_head i_extraction_aliases;
212
213                 #ifdef WITH_NTFS_3G
214                         /* In NTFS-3g extraction mode, this is set to the Master
215                          * File Table (MFT) number of the NTFS file that was
216                          * created for this inode.  */
217                         u64 i_mft_no;
218                 #endif
219                 };
220
221                 /* Used during WIM writing with
222                  * WIMLIB_WRITE_FLAG_SEND_DONE_WITH_FILE_MESSAGES:  the number
223                  * of streams this inode has that have not yet been fully read.
224                  * */
225                 u32 num_remaining_streams;
226
227 #ifdef WITH_FUSE
228                 struct {
229                         /* Used only during image mount:  Table of file
230                          * descriptors that have been opened to this inode.
231                          * This table is freed when the last file descriptor is
232                          * closed.  */
233                         struct wimfs_fd **i_fds;
234
235                         /* Lower bound on the index of the next available entry
236                          * in 'i_fds'.  */
237                         u16 i_next_fd;
238                 };
239 #endif
240         };
241
242 #ifdef WITH_FUSE
243         u16 i_num_opened_fds;
244         u16 i_num_allocated_fds;
245 #endif
246
247         /* Next stream ID to be assigned  */
248         u32 i_next_stream_id;
249 };
250
251 /*
252  * Reparse tags documented at
253  * http://msdn.microsoft.com/en-us/library/dd541667(v=prot.10).aspx
254  */
255 #define WIM_IO_REPARSE_TAG_RESERVED_ZERO        0x00000000
256 #define WIM_IO_REPARSE_TAG_RESERVED_ONE         0x00000001
257 #define WIM_IO_REPARSE_TAG_MOUNT_POINT          0xA0000003
258 #define WIM_IO_REPARSE_TAG_HSM                  0xC0000004
259 #define WIM_IO_REPARSE_TAG_HSM2                 0x80000006
260 #define WIM_IO_REPARSE_TAG_DRIVER_EXTENDER      0x80000005
261 #define WIM_IO_REPARSE_TAG_SIS                  0x80000007
262 #define WIM_IO_REPARSE_TAG_DFS                  0x8000000A
263 #define WIM_IO_REPARSE_TAG_DFSR                 0x80000012
264 #define WIM_IO_REPARSE_TAG_FILTER_MANAGER       0x8000000B
265 #define WIM_IO_REPARSE_TAG_SYMLINK              0xA000000C
266
267 #define FILE_ATTRIBUTE_READONLY            0x00000001
268 #define FILE_ATTRIBUTE_HIDDEN              0x00000002
269 #define FILE_ATTRIBUTE_SYSTEM              0x00000004
270 #define FILE_ATTRIBUTE_DIRECTORY           0x00000010
271 #define FILE_ATTRIBUTE_ARCHIVE             0x00000020
272 #define FILE_ATTRIBUTE_DEVICE              0x00000040
273 #define FILE_ATTRIBUTE_NORMAL              0x00000080
274 #define FILE_ATTRIBUTE_TEMPORARY           0x00000100
275 #define FILE_ATTRIBUTE_SPARSE_FILE         0x00000200
276 #define FILE_ATTRIBUTE_REPARSE_POINT       0x00000400
277 #define FILE_ATTRIBUTE_COMPRESSED          0x00000800
278 #define FILE_ATTRIBUTE_OFFLINE             0x00001000
279 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
280 #define FILE_ATTRIBUTE_ENCRYPTED           0x00004000
281 #define FILE_ATTRIBUTE_VIRTUAL             0x00010000
282
283 extern struct wim_inode *
284 new_inode(struct wim_dentry *dentry, bool set_timestamps);
285
286 /* Iterate through each alias of the specified inode.  */
287 #define inode_for_each_dentry(dentry, inode) \
288         list_for_each_entry((dentry), &(inode)->i_dentry, d_alias)
289
290 /* Return an alias of the specified inode.  */
291 #define inode_first_dentry(inode) \
292         container_of(inode->i_dentry.next, struct wim_dentry, d_alias)
293
294 /* Return the full path of an alias of the specified inode, or NULL if a full
295  * path could not be determined.  */
296 #define inode_first_full_path(inode) \
297         dentry_full_path(inode_first_dentry(inode))
298
299 extern void
300 d_associate(struct wim_dentry *dentry, struct wim_inode *inode);
301
302 extern void
303 d_disassociate(struct wim_dentry *dentry);
304
305 #ifdef WITH_FUSE
306 extern void
307 inode_dec_num_opened_fds(struct wim_inode *inode);
308 #endif
309
310 /* Is the inode a directory?
311  * This doesn't count directories with reparse data.
312  * wimlib only allows inodes of this type to have children.
313  */
314 static inline bool
315 inode_is_directory(const struct wim_inode *inode)
316 {
317         return (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
318                                        FILE_ATTRIBUTE_REPARSE_POINT))
319                         == FILE_ATTRIBUTE_DIRECTORY;
320 }
321
322 /* Is the inode a symbolic link?
323  * This returns true iff the inode is a reparse point that is either a "real"
324  * symbolic link or a junction point.  */
325 static inline bool
326 inode_is_symlink(const struct wim_inode *inode)
327 {
328         return (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
329                 && (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
330                     inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
331 }
332
333 /* Does the inode have children?  Currently (based on read_dentry_tree() as well
334  * as the various build-dentry-tree implementations), this can only return true
335  * for inodes for which inode_is_directory() returns true.  */
336 static inline bool
337 inode_has_children(const struct wim_inode *inode)
338 {
339         return inode->i_children != NULL;
340 }
341
342 extern struct wim_inode_stream *
343 inode_get_stream(const struct wim_inode *inode, int stream_type,
344                  const utf16lechar *stream_name);
345
346 extern struct wim_inode_stream *
347 inode_get_unnamed_stream(const struct wim_inode *inode, int stream_type);
348
349 extern struct wim_inode_stream *
350 inode_add_stream(struct wim_inode *inode, int stream_type,
351                  const utf16lechar *stream_name, struct blob_descriptor *blob);
352
353 extern void
354 inode_replace_stream_blob(struct wim_inode *inode,
355                           struct wim_inode_stream *strm,
356                           struct blob_descriptor *new_blob,
357                           struct blob_table *blob_table);
358
359 extern bool
360 inode_replace_stream_data(struct wim_inode *inode,
361                           struct wim_inode_stream *strm,
362                           const void *data, size_t size,
363                           struct blob_table *blob_table);
364
365 extern bool
366 inode_add_stream_with_data(struct wim_inode *inode, int stream_type,
367                            const utf16lechar *stream_name,
368                            const void *data, size_t size,
369                            struct blob_table *blob_table);
370
371 extern void
372 inode_remove_stream(struct wim_inode *inode, struct wim_inode_stream *strm,
373                     struct blob_table *blob_table);
374
375 static inline struct blob_descriptor *
376 stream_blob_resolved(const struct wim_inode_stream *strm)
377 {
378         wimlib_assert(strm->stream_resolved);
379         return strm->_stream_blob;
380 }
381
382 static inline bool
383 stream_is_named(const struct wim_inode_stream *strm)
384 {
385         return strm->stream_name != NO_STREAM_NAME;
386 }
387
388 static inline bool
389 stream_is_unnamed_data_stream(const struct wim_inode_stream *strm)
390 {
391         return strm->stream_type == STREAM_TYPE_DATA && !stream_is_named(strm);
392 }
393
394 static inline bool
395 stream_is_named_data_stream(const struct wim_inode_stream *strm)
396 {
397         return strm->stream_type == STREAM_TYPE_DATA && stream_is_named(strm);
398 }
399
400 extern bool
401 inode_has_named_data_stream(const struct wim_inode *inode);
402
403 extern int
404 inode_resolve_streams(struct wim_inode *inode,
405                       struct blob_table *table, bool force);
406
407 extern void
408 inode_unresolve_streams(struct wim_inode *inode);
409
410 extern int
411 blob_not_found_error(const struct wim_inode *inode, const u8 *hash);
412
413 extern struct blob_descriptor *
414 stream_blob(const struct wim_inode_stream *strm, const struct blob_table *table);
415
416 extern struct blob_descriptor *
417 inode_get_blob_for_unnamed_data_stream(const struct wim_inode *inode,
418                                        const struct blob_table *blob_table);
419
420 extern struct blob_descriptor *
421 inode_get_blob_for_unnamed_data_stream_resolved(const struct wim_inode *inode);
422
423 extern const u8 *
424 stream_hash(const struct wim_inode_stream *strm);
425
426 extern const u8 *
427 inode_get_hash_of_unnamed_data_stream(const struct wim_inode *inode);
428
429 extern void
430 inode_ref_blobs(struct wim_inode *inode);
431
432 extern void
433 inode_unref_blobs(struct wim_inode *inode, struct blob_table *blob_table);
434
435 /* inode_fixup.c  */
436 extern int
437 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list);
438
439 #endif /* _WIMLIB_INODE_H  */