]> wimlib.net Git - wimlib/blob - include/wimlib/inode.h
4b9567757aa1ba2f8fd78ac0d1a25c4c9867678e
[wimlib] / include / wimlib / inode.h
1 #ifndef _WIMLIB_INODE_H
2 #define _WIMLIB_INODE_H
3
4 #include "wimlib/list.h"
5 #include "wimlib/sha1.h"
6 #include "wimlib/types.h"
7
8 struct avl_tree_node;
9 struct wim_ads_entry;
10 struct wim_dentry;
11 struct wim_lookup_table;
12 struct wim_lookup_table_entry;
13 struct wim_security_data;
14 struct wimfs_fd;
15
16 /*
17  * WIM inode.
18  *
19  * As mentioned in the comment above `struct wim_dentry', in WIM files there
20  * is no on-disk analogue of a real inode, as most of these fields are
21  * duplicated in the dentries.  Instead, a `struct wim_inode' is something we
22  * create ourselves to simplify the handling of hard links.
23  */
24 struct wim_inode {
25         /* If i_resolved == 0:
26          *      SHA1 message digest of the contents of the unnamed-data stream
27          *      of this inode.
28          *
29          * If i_resolved == 1:
30          *      Pointer to the lookup table entry for the unnamed data stream
31          *      of this inode, or NULL.
32          *
33          * i_hash corresponds to the 'unnamed_stream_hash' field of the `struct
34          * wim_dentry_on_disk' and the additional caveats documented about that
35          * field apply here (for example, the quirks regarding all-zero hashes).
36          */
37         union {
38                 u8 i_hash[SHA1_HASH_SIZE];
39                 struct wim_lookup_table_entry *i_lte;
40         };
41
42         /* Corresponds to the 'attributes' field of `struct wim_dentry_on_disk';
43          * bitwise OR of the FILE_ATTRIBUTE_* flags that give the attributes of
44          * this inode. */
45         u32 i_attributes;
46
47         /* Root of a balanced binary search tree storing the child directory
48          * entries of this inode, if any.  Keyed by wim_dentry->file_name, case
49          * sensitively.  If this inode is not a directory or if it has no
50          * children then this will be an empty tree (NULL).  */
51         struct avl_tree_node *i_children;
52
53         /* Root of a balanced binary search tree storing the child directory
54          * entries of this inode, if any.  Keyed by wim_dentry->file_name, case
55          * insensitively.  If this inode is not a directory or if it has no
56          * children then this will be an empty tree (NULL).  */
57         struct avl_tree_node *i_children_ci;
58
59         /* List of dentries that are aliases for this inode.  There will be
60          * i_nlink dentries in this list.  */
61         struct list_head i_dentry;
62
63         /* Field to place this inode into a list. */
64         union {
65                 /* Hash list node- used in inode_fixup.c when the inodes are
66                  * placed into a hash table keyed by inode number and optionally
67                  * device number, in order to detect dentries that are aliases
68                  * for the same inode. */
69                 struct hlist_node i_hlist;
70
71                 /* Normal list node- used to connect all the inodes of a WIM
72                  * image into a single linked list referenced from the `struct
73                  * wim_image_metadata' for that image. */
74                 struct list_head i_list;
75         };
76
77         /* Number of dentries that are aliases for this inode.  */
78         u32 i_nlink;
79
80         /* Number of alternate data streams (ADS) associated with this inode */
81         u16 i_num_ads;
82
83         /* Flag that indicates whether this inode's streams have been
84          * "resolved".  By default, the inode starts as "unresolved", meaning
85          * that the i_hash field, along with the hash field of any associated
86          * wim_ads_entry's, are valid and should be used as keys in the WIM
87          * lookup table to find the associated `struct wim_lookup_table_entry'.
88          * But if the inode has been resolved, then each of these fields is
89          * replaced with a pointer directly to the appropriate `struct
90          * wim_lookup_table_entry', or NULL if the stream is empty.  */
91         u8 i_resolved : 1;
92
93         /* Flag used to mark this inode as visited; this is used when visiting
94          * all the inodes in a dentry tree exactly once.  It will be 0 by
95          * default and must be cleared following the tree traversal, even in
96          * error paths.  */
97         u8 i_visited : 1;
98
99         /* 1 iff all ADS entries of this inode are named or if this inode
100          * has no ADS entries  */
101         u8 i_canonical_streams : 1;
102
103         /* Cached value  */
104         u8 i_can_externally_back : 1;
105
106         /* Pointer to a malloc()ed array of i_num_ads alternate data stream
107          * entries for this inode.  */
108         struct wim_ads_entry *i_ads_entries;
109
110         /* If not NULL, a pointer to the extra data that was read from the
111          * dentry.  This should be a series of tagged items, each of which
112          * represents a bit of extra metadata, such as the file's object ID.
113          * See tagged_items.c for more information.  */
114         void *i_extra;
115
116         /* Size of @i_extra buffer in bytes.  If 0, there is no extra data.  */
117         size_t i_extra_size;
118
119         /* Creation time, last access time, and last write time for this inode,
120          * in 100-nanosecond intervals since 12:00 a.m UTC January 1, 1601.
121          * They should correspond to the times gotten by calling GetFileTime()
122          * on Windows. */
123         u64 i_creation_time;
124         u64 i_last_access_time;
125         u64 i_last_write_time;
126
127         /* Corresponds to 'security_id' in `struct wim_dentry_on_disk':  The
128          * index of this inode's security descriptor in the WIM image's table of
129          * security descriptors, or -1.  Note: when a WIM image is loaded,
130          * wimlib sets out-of-bounds indices and values less than -1 in this
131          * field to -1.  So the extraction code need not do an upper bound check
132          * after checking for -1 (or equivalently < 0).  */
133         int32_t i_security_id;
134
135         /* Identity of a reparse point.  See
136          * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
137          * for what a reparse point is. */
138         u32 i_reparse_tag;
139
140         /* Unused/unknown fields that we just read into memory so we can
141          * re-write them unchanged.  */
142         u32 i_rp_unknown_1;
143         u16 i_rp_unknown_2;
144
145         /* Corresponds to not_rpfixed in `struct wim_dentry_on_disk':  Set to 0
146          * if reparse point fixups have been done.  Otherwise set to 1.  Note:
147          * this actually may reflect the SYMBOLIC_LINK_RELATIVE flag.
148          */
149         u16 i_not_rpfixed;
150
151         /* Inode number; corresponds to hard_link_group_id in the `struct
152          * wim_dentry_on_disk'.  */
153         u64 i_ino;
154
155         union {
156                 /* Device number, used only during image capture, so we can
157                  * identify hard linked files by the combination of inode number
158                  * and device number (rather than just inode number, which could
159                  * be ambigious if the captured tree spans a mountpoint).  Set
160                  * to 0 otherwise.  */
161                 u64 i_devno;
162
163                 /* Fields used only during extraction  */
164                 struct {
165                         /* List of aliases of this dentry that are being
166                          * extracted in the current extraction operation.  This
167                          * will be a (possibly nonproper) subset of the dentries
168                          * in the i_dentry list.  This list will be constructed
169                          * regardless of whether the extraction backend supports
170                          * hard links or not.  */
171                         struct list_head i_extraction_aliases;
172
173                 #ifdef WITH_NTFS_3G
174                         /* In NTFS-3g extraction mode, this is set to the Master
175                          * File Table (MFT) number of the NTFS file that was
176                          * created for this inode.  */
177                         u64 i_mft_no;
178                 #endif
179                 };
180
181                 /* Used during WIM writing with
182                  * WIMLIB_WRITE_FLAG_SEND_DONE_WITH_FILE_MESSAGES:  the number
183                  * of data streams this inode has that have not yet been fully
184                  * read.  */
185                 u32 num_remaining_streams;
186
187 #ifdef WITH_FUSE
188                 struct {
189                         /* Used only during image mount:  Table of file
190                          * descriptors that have been opened to this inode.
191                          * This table is freed when the last file descriptor is
192                          * closed.  */
193                         struct wimfs_fd **i_fds;
194
195                         /* Lower bound on the index of the next available entry
196                          * in 'i_fds'.  */
197                         u16 i_next_fd;
198                 };
199 #endif
200         };
201
202 #ifdef WITH_FUSE
203         u16 i_num_opened_fds;
204         u16 i_num_allocated_fds;
205 #endif
206
207         /* Next alternate data stream ID to be assigned */
208         u32 i_next_stream_id;
209 };
210
211 /* Alternate data stream entry.
212  *
213  * We read this from disk in the read_ads_entries() function; see that function
214  * for more explanation. */
215 struct wim_ads_entry {
216         union {
217                 /* SHA-1 message digest of stream contents */
218                 u8 hash[SHA1_HASH_SIZE];
219
220                 /* The corresponding lookup table entry (only for resolved
221                  * streams) */
222                 struct wim_lookup_table_entry *lte;
223         };
224
225         /* Length of UTF16-encoded stream name, in bytes, not including the
226          * terminating null character; or 0 if the stream is unnamed. */
227         u16 stream_name_nbytes;
228
229         /* Number to identify an alternate data stream even after it's possibly
230          * been moved or renamed. */
231         u32 stream_id;
232
233         /* Stream name (UTF-16LE), null-terminated, or NULL if the stream is
234          * unnamed.  */
235         utf16lechar *stream_name;
236
237         /* Reserved field.  We read it into memory so we can write it out
238          * unchanged. */
239         u64 reserved;
240 };
241
242 /* WIM alternate data stream entry (on-disk format) */
243 struct wim_ads_entry_on_disk {
244         /* Length of the entry, in bytes.  This includes all fixed-length
245          * fields, plus the stream name and null terminator if present, and the
246          * padding up to an 8 byte boundary.  wimlib is a little less strict
247          * when reading the entries, and only requires that the number of bytes
248          * from this field is at least as large as the size of the fixed length
249          * fields and stream name without null terminator.  */
250         le64 length;
251
252         le64 reserved;
253
254         /* SHA1 message digest of the uncompressed stream; or, alternatively,
255          * can be all zeroes if the stream has zero length.  */
256         u8 hash[SHA1_HASH_SIZE];
257
258         /* Length of the stream name, in bytes.  0 if the stream is unnamed.  */
259         le16 stream_name_nbytes;
260
261         /* Stream name in UTF-16LE.  It is @stream_name_nbytes bytes long,
262          * excluding the null terminator.  There is a null terminator character
263          * if @stream_name_nbytes != 0; i.e., if this stream is named.  */
264         utf16lechar stream_name[];
265 } _packed_attribute;
266
267 #define WIM_ADS_ENTRY_DISK_SIZE 38
268
269 /*
270  * Reparse tags documented at
271  * http://msdn.microsoft.com/en-us/library/dd541667(v=prot.10).aspx
272  */
273 #define WIM_IO_REPARSE_TAG_RESERVED_ZERO        0x00000000
274 #define WIM_IO_REPARSE_TAG_RESERVED_ONE         0x00000001
275 #define WIM_IO_REPARSE_TAG_MOUNT_POINT          0xA0000003
276 #define WIM_IO_REPARSE_TAG_HSM                  0xC0000004
277 #define WIM_IO_REPARSE_TAG_HSM2                 0x80000006
278 #define WIM_IO_REPARSE_TAG_DRIVER_EXTENDER      0x80000005
279 #define WIM_IO_REPARSE_TAG_SIS                  0x80000007
280 #define WIM_IO_REPARSE_TAG_DFS                  0x8000000A
281 #define WIM_IO_REPARSE_TAG_DFSR                 0x80000012
282 #define WIM_IO_REPARSE_TAG_FILTER_MANAGER       0x8000000B
283 #define WIM_IO_REPARSE_TAG_SYMLINK              0xA000000C
284
285 #define FILE_ATTRIBUTE_READONLY            0x00000001
286 #define FILE_ATTRIBUTE_HIDDEN              0x00000002
287 #define FILE_ATTRIBUTE_SYSTEM              0x00000004
288 #define FILE_ATTRIBUTE_DIRECTORY           0x00000010
289 #define FILE_ATTRIBUTE_ARCHIVE             0x00000020
290 #define FILE_ATTRIBUTE_DEVICE              0x00000040
291 #define FILE_ATTRIBUTE_NORMAL              0x00000080
292 #define FILE_ATTRIBUTE_TEMPORARY           0x00000100
293 #define FILE_ATTRIBUTE_SPARSE_FILE         0x00000200
294 #define FILE_ATTRIBUTE_REPARSE_POINT       0x00000400
295 #define FILE_ATTRIBUTE_COMPRESSED          0x00000800
296 #define FILE_ATTRIBUTE_OFFLINE             0x00001000
297 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
298 #define FILE_ATTRIBUTE_ENCRYPTED           0x00004000
299 #define FILE_ATTRIBUTE_VIRTUAL             0x00010000
300
301 extern struct wim_inode *
302 new_inode(void) _malloc_attribute;
303
304 extern struct wim_inode *
305 new_timeless_inode(void) _malloc_attribute;
306
307 extern void
308 put_inode(struct wim_inode *inode);
309
310 extern void
311 free_inode(struct wim_inode *inode);
312
313 /* Iterate through each alias of the specified inode.  */
314 #define inode_for_each_dentry(dentry, inode) \
315         list_for_each_entry((dentry), &(inode)->i_dentry, d_alias)
316
317 /* Add a new alias for the specified inode.  Does not increment i_nlink; that
318  * must be done separately if needed.  */
319 #define inode_add_dentry(dentry, inode) \
320         list_add_tail(&(dentry)->d_alias, &(inode)->i_dentry)
321
322 /* Return an alias of the specified inode.  */
323 #define inode_first_dentry(inode) \
324         container_of(inode->i_dentry.next, struct wim_dentry, d_alias)
325
326 /* Return the full path of an alias of the specified inode, or NULL if a full
327  * path could not be determined.  */
328 #define inode_first_full_path(inode) \
329         dentry_full_path(inode_first_dentry(inode))
330
331 /* Is the inode a directory?
332  * This doesn't count directories with reparse data.
333  * wimlib only allows inodes of this type to have children.
334  */
335 static inline bool
336 inode_is_directory(const struct wim_inode *inode)
337 {
338         return (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
339                                        FILE_ATTRIBUTE_REPARSE_POINT))
340                         == FILE_ATTRIBUTE_DIRECTORY;
341 }
342
343 /* Is the inode a directory with the encrypted attribute set?
344  * This returns true for encrypted directories even if they have reparse data
345  * (I'm not sure if such files can even exist!).  */
346 static inline bool
347 inode_is_encrypted_directory(const struct wim_inode *inode)
348 {
349         return ((inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
350                                         FILE_ATTRIBUTE_ENCRYPTED))
351                 == (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_ENCRYPTED));
352 }
353
354 /* Is the inode a symbolic link?
355  * This returns true iff the inode is a reparse point that is either a "real"
356  * symbolic link or a junction point.  */
357 static inline bool
358 inode_is_symlink(const struct wim_inode *inode)
359 {
360         return (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
361                 && (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
362                     inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
363 }
364
365 /* Does the inode have children?
366  * Currently (based on read_dentry_tree()), this can only return true for inodes
367  * for which inode_is_directory() returns true.  (This also returns false on
368  * empty directories.)  */
369 static inline bool
370 inode_has_children(const struct wim_inode *inode)
371 {
372         return inode->i_children != NULL;
373 }
374
375 extern struct wim_ads_entry *
376 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name);
377
378 extern struct wim_ads_entry *
379 inode_add_ads_utf16le(struct wim_inode *inode, const utf16lechar *stream_name,
380                       size_t stream_name_nbytes);
381
382 extern struct wim_ads_entry *
383 inode_add_ads(struct wim_inode *dentry, const tchar *stream_name);
384
385 extern struct wim_ads_entry *
386 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
387                         const void *value, size_t size,
388                         struct wim_lookup_table *lookup_table);
389
390 extern void
391 inode_remove_ads(struct wim_inode *inode, struct wim_ads_entry *entry,
392                  struct wim_lookup_table *lookup_table);
393
394 extern bool
395 inode_has_named_stream(const struct wim_inode *inode);
396
397 extern int
398 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
399                          struct wim_lookup_table *lookup_table);
400
401 extern int
402 inode_resolve_streams(struct wim_inode *inode, struct wim_lookup_table *table,
403                       bool force);
404
405 extern void
406 inode_unresolve_streams(struct wim_inode *inode);
407
408 extern int
409 stream_not_found_error(const struct wim_inode *inode, const u8 *hash);
410
411 static inline struct wim_lookup_table_entry *
412 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
413 {
414         if (stream_idx == 0)
415                 return inode->i_lte;
416         return inode->i_ads_entries[stream_idx - 1].lte;
417 }
418
419 extern struct wim_lookup_table_entry *
420 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
421                  const struct wim_lookup_table *table);
422
423 extern struct wim_lookup_table_entry *
424 inode_unnamed_stream_resolved(const struct wim_inode *inode,
425                               unsigned *stream_idx_ret);
426
427 static inline struct wim_lookup_table_entry *
428 inode_unnamed_lte_resolved(const struct wim_inode *inode)
429 {
430         unsigned stream_idx;
431         return inode_unnamed_stream_resolved(inode, &stream_idx);
432 }
433
434 extern struct wim_lookup_table_entry *
435 inode_unnamed_lte(const struct wim_inode *inode,
436                   const struct wim_lookup_table *table);
437
438 extern const u8 *
439 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx);
440
441 extern const u8 *
442 inode_unnamed_stream_hash(const struct wim_inode *inode);
443
444 static inline unsigned
445 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
446 {
447         if (stream_idx == 0)
448                 return 0;
449         return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
450 }
451
452 static inline u32
453 inode_stream_idx_to_id(const struct wim_inode *inode, unsigned stream_idx)
454 {
455         if (stream_idx == 0)
456                 return 0;
457         return inode->i_ads_entries[stream_idx - 1].stream_id;
458 }
459
460 extern void
461 inode_ref_streams(struct wim_inode *inode);
462
463 extern void
464 inode_unref_streams(struct wim_inode *inode,
465                     struct wim_lookup_table *lookup_table);
466
467 extern int
468 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
469                  size_t *nbytes_remaining_p);
470
471 extern void
472 check_inode(struct wim_inode *inode, const struct wim_security_data *sd);
473
474 /* inode_fixup.c  */
475 extern int
476 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list);
477
478 #endif /* _WIMLIB_INODE_H  */