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