]> wimlib.net Git - wimlib/blob - include/wimlib/inode.h
86d3feb33ed9ec9e636768f8f90e0ad6d76ff06e
[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         } _packed_attribute; /* union is SHA1_HASH_SIZE bytes */
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 hlist_head i_dentry;
123
124         /* Field to place this inode into a list.  While reading a WIM image or
125          * adding files to a WIM image this is owned by the inode table;
126          * otherwise this links the inodes for the WIM image.  */
127         struct hlist_node i_hlist;
128
129         /* Number of dentries that are aliases for this inode.  */
130         u32 i_nlink : 30;
131
132         /* Flag used to mark this inode as visited; this is used when visiting
133          * all the inodes in a dentry tree exactly once.  It will be 0 by
134          * default and must be cleared following the tree traversal, even in
135          * error paths.  */
136         u32 i_visited : 1;
137
138         /* Cached value  */
139         u32 i_can_externally_back : 1;
140
141         /* If not NULL, a pointer to the extra data that was read from the
142          * dentry.  This should be a series of tagged items, each of which
143          * represents a bit of extra metadata, such as the file's object ID.
144          * See tagged_items.c for more information.  */
145         void *i_extra;
146
147         /* Size of @i_extra buffer in bytes.  If 0, there is no extra data.  */
148         size_t i_extra_size;
149
150         /* Creation time, last access time, and last write time for this inode,
151          * in 100-nanosecond intervals since 12:00 a.m UTC January 1, 1601.
152          * They should correspond to the times gotten by calling GetFileTime()
153          * on Windows. */
154         u64 i_creation_time;
155         u64 i_last_access_time;
156         u64 i_last_write_time;
157
158         /* Corresponds to 'security_id' in `struct wim_dentry_on_disk':  The
159          * index of this inode's security descriptor in the WIM image's table of
160          * security descriptors, or -1 if this inode does not have a security
161          * descriptor.  */
162         s32 i_security_id;
163
164         /* Unknown field that we only read into memory so we can re-write it
165          * unchanged.  Probably it's actually just padding...  */
166         u32 i_unknown_0x54;
167
168         /* The following fields correspond to 'reparse_tag', 'rp_reserved', and
169          * 'rp_flags' in `struct wim_dentry_on_disk'.  They are only meaningful
170          * for reparse point files.  */
171         u32 i_reparse_tag;
172         u16 i_rp_reserved;
173         u16 i_rp_flags;
174
175         /* Inode number; corresponds to hard_link_group_id in the `struct
176          * wim_dentry_on_disk'.  */
177         u64 i_ino;
178
179         union {
180                 /* Device number, used only during image capture, so we can
181                  * identify hard linked files by the combination of inode number
182                  * and device number (rather than just inode number, which could
183                  * be ambigious if the captured tree spans a mountpoint).  Set
184                  * to 0 otherwise.  */
185                 u64 i_devno;
186
187                 /* Fields used only during extraction  */
188                 struct {
189                         /* A singly linked list of aliases of this dentry that
190                          * are being extracted in the current extraction
191                          * operation.  This list may be shorter than the inode's
192                          * full alias list.  This list will be constructed
193                          * regardless of whether the extraction backend supports
194                          * hard links or not.  */
195                         struct wim_dentry *i_first_extraction_alias;
196
197                 #ifdef WITH_NTFS_3G
198                         /* In NTFS-3g extraction mode, this is set to the Master
199                          * File Table (MFT) number of the NTFS file that was
200                          * created for this inode.  */
201                         u64 i_mft_no;
202                 #endif
203                 };
204
205                 /* Used during WIM writing with
206                  * WIMLIB_WRITE_FLAG_SEND_DONE_WITH_FILE_MESSAGES:  the number
207                  * of streams this inode has that have not yet been fully read.
208                  * */
209                 u32 i_num_remaining_streams;
210
211 #ifdef WITH_FUSE
212                 struct {
213                         /* Used only during image mount:  Table of file
214                          * descriptors that have been opened to this inode.
215                          * This table is freed when the last file descriptor is
216                          * closed.  */
217                         struct wimfs_fd **i_fds;
218
219                         /* Lower bound on the index of the next available entry
220                          * in 'i_fds'.  */
221                         u16 i_next_fd;
222                 };
223 #endif
224         };
225
226 #ifdef WITH_FUSE
227         u16 i_num_opened_fds;
228         u16 i_num_allocated_fds;
229 #endif
230
231         /* Next stream ID to be assigned  */
232         u32 i_next_stream_id;
233 };
234
235 /*
236  * The available reparse tags are documented at
237  * http://msdn.microsoft.com/en-us/library/dd541667(v=prot.10).aspx.
238  * Here we only define the ones of interest to us.
239  */
240 #define WIM_IO_REPARSE_TAG_MOUNT_POINT          0xA0000003
241 #define WIM_IO_REPARSE_TAG_SYMLINK              0xA000000C
242 #define WIM_IO_REPARSE_TAG_WOF                  0x80000017
243
244 /* Flags for the rp_flags field.  Currently the only known flag is NOT_FIXED,
245  * which indicates that the target of the absolute symbolic link or junction was
246  * not changed when it was stored.  */
247 #define WIM_RP_FLAG_NOT_FIXED              0x0001
248
249 /* Windows file attribute flags  */
250 #define FILE_ATTRIBUTE_READONLY            0x00000001
251 #define FILE_ATTRIBUTE_HIDDEN              0x00000002
252 #define FILE_ATTRIBUTE_SYSTEM              0x00000004
253 #define FILE_ATTRIBUTE_DIRECTORY           0x00000010
254 #define FILE_ATTRIBUTE_ARCHIVE             0x00000020
255 #define FILE_ATTRIBUTE_DEVICE              0x00000040
256 #define FILE_ATTRIBUTE_NORMAL              0x00000080
257 #define FILE_ATTRIBUTE_TEMPORARY           0x00000100
258 #define FILE_ATTRIBUTE_SPARSE_FILE         0x00000200
259 #define FILE_ATTRIBUTE_REPARSE_POINT       0x00000400
260 #define FILE_ATTRIBUTE_COMPRESSED          0x00000800
261 #define FILE_ATTRIBUTE_OFFLINE             0x00001000
262 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
263 #define FILE_ATTRIBUTE_ENCRYPTED           0x00004000
264 #define FILE_ATTRIBUTE_VIRTUAL             0x00010000
265
266 extern struct wim_inode *
267 new_inode(struct wim_dentry *dentry, bool set_timestamps);
268
269 /* Iterate through each alias of the specified inode.  */
270 #define inode_for_each_dentry(dentry, inode) \
271         hlist_for_each_entry((dentry), &(inode)->i_dentry, d_alias)
272
273 /* Return an alias of the specified inode.  */
274 #define inode_first_dentry(inode) \
275         hlist_entry(inode->i_dentry.first, struct wim_dentry, d_alias)
276
277 /* Return the full path of an alias of the specified inode, or NULL if a full
278  * path could not be determined.  */
279 #define inode_first_full_path(inode) \
280         dentry_full_path(inode_first_dentry(inode))
281
282 extern void
283 d_associate(struct wim_dentry *dentry, struct wim_inode *inode);
284
285 extern void
286 d_disassociate(struct wim_dentry *dentry);
287
288 #ifdef WITH_FUSE
289 extern void
290 inode_dec_num_opened_fds(struct wim_inode *inode);
291 #endif
292
293 /* Is the inode a directory?
294  * This doesn't count directories with reparse data.
295  * wimlib only allows inodes of this type to have children.
296  */
297 static inline bool
298 inode_is_directory(const struct wim_inode *inode)
299 {
300         return (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
301                                        FILE_ATTRIBUTE_REPARSE_POINT))
302                         == FILE_ATTRIBUTE_DIRECTORY;
303 }
304
305 /* Is the inode a symbolic link?
306  * This returns true iff the inode is a reparse point that is either a "real"
307  * symbolic link or a junction point.  */
308 static inline bool
309 inode_is_symlink(const struct wim_inode *inode)
310 {
311         return (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
312                 && (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
313                     inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
314 }
315
316 /* Does the inode have children?  Currently (based on read_dentry_tree() as well
317  * as the various build-dentry-tree implementations), this can only return true
318  * for inodes for which inode_is_directory() returns true.  */
319 static inline bool
320 inode_has_children(const struct wim_inode *inode)
321 {
322         return inode->i_children != NULL;
323 }
324
325 /* Does the inode have a security descriptor?  */
326 static inline bool
327 inode_has_security_descriptor(const struct wim_inode *inode)
328 {
329         return inode->i_security_id >= 0;
330 }
331
332 extern struct wim_inode_stream *
333 inode_get_stream(const struct wim_inode *inode, int stream_type,
334                  const utf16lechar *stream_name);
335
336 extern struct wim_inode_stream *
337 inode_get_unnamed_stream(const struct wim_inode *inode, int stream_type);
338
339 static inline struct wim_inode_stream *
340 inode_get_unnamed_data_stream(const struct wim_inode *inode)
341 {
342         return inode_get_unnamed_stream(inode, STREAM_TYPE_DATA);
343 }
344
345 extern struct wim_inode_stream *
346 inode_add_stream(struct wim_inode *inode, int stream_type,
347                  const utf16lechar *stream_name, struct blob_descriptor *blob);
348
349 extern void
350 inode_replace_stream_blob(struct wim_inode *inode,
351                           struct wim_inode_stream *strm,
352                           struct blob_descriptor *new_blob,
353                           struct blob_table *blob_table);
354
355 extern bool
356 inode_replace_stream_data(struct wim_inode *inode,
357                           struct wim_inode_stream *strm,
358                           const void *data, size_t size,
359                           struct blob_table *blob_table);
360
361 extern bool
362 inode_add_stream_with_data(struct wim_inode *inode, int stream_type,
363                            const utf16lechar *stream_name,
364                            const void *data, size_t size,
365                            struct blob_table *blob_table);
366
367 extern void
368 inode_remove_stream(struct wim_inode *inode, struct wim_inode_stream *strm,
369                     struct blob_table *blob_table);
370
371 static inline struct blob_descriptor *
372 stream_blob_resolved(const struct wim_inode_stream *strm)
373 {
374         wimlib_assert(strm->stream_resolved);
375         return strm->_stream_blob;
376 }
377
378 static inline bool
379 stream_is_named(const struct wim_inode_stream *strm)
380 {
381         return strm->stream_name != NO_STREAM_NAME;
382 }
383
384 static inline bool
385 stream_is_unnamed_data_stream(const struct wim_inode_stream *strm)
386 {
387         return strm->stream_type == STREAM_TYPE_DATA && !stream_is_named(strm);
388 }
389
390 static inline bool
391 stream_is_named_data_stream(const struct wim_inode_stream *strm)
392 {
393         return strm->stream_type == STREAM_TYPE_DATA && stream_is_named(strm);
394 }
395
396 extern bool
397 inode_has_named_data_stream(const struct wim_inode *inode);
398
399 extern int
400 inode_resolve_streams(struct wim_inode *inode,
401                       struct blob_table *table, bool force);
402
403 extern int
404 blob_not_found_error(const struct wim_inode *inode, const u8 *hash);
405
406 extern struct blob_descriptor *
407 stream_blob(const struct wim_inode_stream *strm, const struct blob_table *table);
408
409 extern struct blob_descriptor *
410 inode_get_blob_for_unnamed_data_stream(const struct wim_inode *inode,
411                                        const struct blob_table *blob_table);
412
413 extern struct blob_descriptor *
414 inode_get_blob_for_unnamed_data_stream_resolved(const struct wim_inode *inode);
415
416 extern const u8 *
417 stream_hash(const struct wim_inode_stream *strm);
418
419 extern const u8 *
420 inode_get_hash_of_unnamed_data_stream(const struct wim_inode *inode);
421
422 extern void
423 inode_ref_blobs(struct wim_inode *inode);
424
425 extern void
426 inode_unref_blobs(struct wim_inode *inode, struct blob_table *blob_table);
427
428 /* inode_fixup.c  */
429 extern int
430 dentry_tree_fix_inodes(struct wim_dentry *root, struct hlist_head *inode_list);
431
432 #endif /* _WIMLIB_INODE_H  */