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