1 #ifndef _WIMLIB_INODE_H
2 #define _WIMLIB_INODE_H
4 #include "wimlib/assert.h"
5 #include "wimlib/list.h"
6 #include "wimlib/lookup_table.h"
7 #include "wimlib/sha1.h"
13 struct wim_security_data;
14 struct wim_lookup_table;
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.
27 /* If i_resolved == 0:
28 * SHA1 message digest of the contents of the unnamed-data stream
32 * Pointer to the lookup table entry for the unnamed data stream
33 * of this inode, or NULL.
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).
40 u8 i_hash[SHA1_HASH_SIZE];
41 struct wim_lookup_table_entry *i_lte;
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
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;
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;
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;
65 /* Field to place this inode into a list. */
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;
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;
79 /* Number of dentries that are aliases for this inode. */
82 /* Number of alternate data streams (ADS) associated with this inode */
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. */
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
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;
106 u8 i_can_externally_back : 1;
108 /* Pointer to a malloc()ed array of i_num_ads alternate data stream
109 * entries for this inode. */
110 struct wim_ads_entry *i_ads_entries;
112 /* If not NULL, a pointer to the extra data that was read from the
113 * dentry. This should be a series of tagged items, each of which
114 * represents a bit of extra metadata, such as the file's object ID.
115 * See tagged_items.c for more information. */
118 /* Size of @i_extra buffer in bytes. If 0, there is no extra data. */
121 /* Creation time, last access time, and last write time for this inode, in
122 * 100-nanosecond intervals since 12:00 a.m UTC January 1, 1601. They
123 * should correspond to the times gotten by calling GetFileTime() on
126 u64 i_last_access_time;
127 u64 i_last_write_time;
129 /* Corresponds to 'security_id' in `struct wim_dentry_on_disk': The
130 * index of this inode's security descriptor in the WIM image's table of
131 * security descriptors, or -1. Note: when a WIM image is loaded,
132 * wimlib sets out-of-bounds indices and values less than -1 in this
133 * field to -1. So the extraction code need not do an upper bound check
134 * after checking for -1 (or equivalently < 0). */
135 int32_t i_security_id;
137 /* Identity of a reparse point. See
138 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
139 * for what a reparse point is. */
142 /* Unused/unknown fields that we just read into memory so we can
143 * re-write them unchanged. */
147 /* Corresponds to not_rpfixed in `struct wim_dentry_on_disk': Set to 0
148 * if reparse point fixups have been done. Otherwise set to 1. Note:
149 * this actually may reflect the SYMBOLIC_LINK_RELATIVE flag.
153 /* Inode number; corresponds to hard_link_group_id in the `struct
154 * wim_dentry_on_disk'. */
158 /* Device number, used only during image capture, so we can
159 * identify hard linked files by the combination of inode number
160 * and device number (rather than just inode number, which could
161 * be ambigious if the captured tree spans a mountpoint). Set
165 /* Fields used only during extraction */
167 /* List of aliases of this dentry that are being
168 * extracted in the current extraction operation. This
169 * will be a (possibly nonproper) subset of the dentries
170 * in the i_dentry list. This list will be constructed
171 * regardless of whether the extraction backend supports
172 * hard links or not. */
173 struct list_head i_extraction_aliases;
176 /* In NTFS-3g extraction mode, this is set to the Master
177 * File Table (MFT) number of the NTFS file that was
178 * created for this inode. */
183 /* Used during WIM writing with
184 * WIMLIB_WRITE_FLAG_SEND_DONE_WITH_FILE_MESSAGES: the number
185 * of data streams this inode has that have not yet been fully
187 u32 num_remaining_streams;
191 /* Used only during image mount: Table of file
192 * descriptors that have been opened to this inode.
193 * This table is freed when the last file descriptor is
195 struct wimfs_fd **i_fds;
197 /* Lower bound on the index of the next available entry
205 u16 i_num_opened_fds;
206 u16 i_num_allocated_fds;
209 /* Next alternate data stream ID to be assigned */
210 u32 i_next_stream_id;
213 /* Alternate data stream entry.
215 * We read this from disk in the read_ads_entries() function; see that function
216 * for more explanation. */
217 struct wim_ads_entry {
219 /* SHA-1 message digest of stream contents */
220 u8 hash[SHA1_HASH_SIZE];
222 /* The corresponding lookup table entry (only for resolved
224 struct wim_lookup_table_entry *lte;
227 /* Length of UTF16-encoded stream name, in bytes, not including the
228 * terminating null character; or 0 if the stream is unnamed. */
229 u16 stream_name_nbytes;
231 /* Number to identify an alternate data stream even after it's possibly
232 * been moved or renamed. */
235 /* Stream name (UTF-16LE), null-terminated, or NULL if the stream is
237 utf16lechar *stream_name;
239 /* Reserved field. We read it into memory so we can write it out
244 /* WIM alternate data stream entry (on-disk format) */
245 struct wim_ads_entry_on_disk {
246 /* Length of the entry, in bytes. This apparently includes all
247 * fixed-length fields, plus the stream name and null terminator if
248 * present, and the padding up to an 8 byte boundary. wimlib is a
249 * little less strict when reading the entries, and only requires that
250 * the number of bytes from this field is at least as large as the size
251 * of the fixed length fields and stream name without null terminator.
257 /* SHA1 message digest of the uncompressed stream; or, alternatively,
258 * can be all zeroes if the stream has zero length. */
259 u8 hash[SHA1_HASH_SIZE];
261 /* Length of the stream name, in bytes. 0 if the stream is unnamed. */
262 le16 stream_name_nbytes;
264 /* Stream name in UTF-16LE. It is @stream_name_nbytes bytes long,
265 * excluding the null terminator. There is a null terminator
266 * character if @stream_name_nbytes != 0; i.e., if this stream is named.
268 utf16lechar stream_name[];
271 #define WIM_ADS_ENTRY_DISK_SIZE 38
274 * Reparse tags documented at
275 * http://msdn.microsoft.com/en-us/library/dd541667(v=prot.10).aspx
277 #define WIM_IO_REPARSE_TAG_RESERVED_ZERO 0x00000000
278 #define WIM_IO_REPARSE_TAG_RESERVED_ONE 0x00000001
279 #define WIM_IO_REPARSE_TAG_MOUNT_POINT 0xA0000003
280 #define WIM_IO_REPARSE_TAG_HSM 0xC0000004
281 #define WIM_IO_REPARSE_TAG_HSM2 0x80000006
282 #define WIM_IO_REPARSE_TAG_DRIVER_EXTENDER 0x80000005
283 #define WIM_IO_REPARSE_TAG_SIS 0x80000007
284 #define WIM_IO_REPARSE_TAG_DFS 0x8000000A
285 #define WIM_IO_REPARSE_TAG_DFSR 0x80000012
286 #define WIM_IO_REPARSE_TAG_FILTER_MANAGER 0x8000000B
287 #define WIM_IO_REPARSE_TAG_SYMLINK 0xA000000C
289 #define FILE_ATTRIBUTE_READONLY 0x00000001
290 #define FILE_ATTRIBUTE_HIDDEN 0x00000002
291 #define FILE_ATTRIBUTE_SYSTEM 0x00000004
292 #define FILE_ATTRIBUTE_DIRECTORY 0x00000010
293 #define FILE_ATTRIBUTE_ARCHIVE 0x00000020
294 #define FILE_ATTRIBUTE_DEVICE 0x00000040
295 #define FILE_ATTRIBUTE_NORMAL 0x00000080
296 #define FILE_ATTRIBUTE_TEMPORARY 0x00000100
297 #define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
298 #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
299 #define FILE_ATTRIBUTE_COMPRESSED 0x00000800
300 #define FILE_ATTRIBUTE_OFFLINE 0x00001000
301 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
302 #define FILE_ATTRIBUTE_ENCRYPTED 0x00004000
303 #define FILE_ATTRIBUTE_VIRTUAL 0x00010000
305 extern struct wim_inode *
306 new_inode(void) _malloc_attribute;
308 extern struct wim_inode *
309 new_timeless_inode(void) _malloc_attribute;
312 put_inode(struct wim_inode *inode);
315 free_inode(struct wim_inode *inode);
317 /* Iterate through each alias of an inode. */
318 #define inode_for_each_dentry(dentry, inode) \
319 list_for_each_entry((dentry), &(inode)->i_dentry, d_alias)
321 /* Add a new alias for an inode. Does not increment i_nlink; that must be done
323 #define inode_add_dentry(dentry, inode) \
324 list_add_tail(&(dentry)->d_alias, &(inode)->i_dentry)
326 /* Return an alias of an inode. */
327 #define inode_first_dentry(inode) \
328 container_of(inode->i_dentry.next, struct wim_dentry, d_alias)
330 /* Return the full path of an alias of an inode, or NULL if it could not be
332 #define inode_first_full_path(inode) \
333 dentry_full_path(inode_first_dentry(inode))
335 extern struct wim_ads_entry *
336 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name);
338 extern struct wim_ads_entry *
339 inode_add_ads_utf16le(struct wim_inode *inode,
340 const utf16lechar *stream_name,
341 size_t stream_name_nbytes);
343 extern struct wim_ads_entry *
344 inode_add_ads(struct wim_inode *dentry, const tchar *stream_name);
346 extern struct wim_ads_entry *
347 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
348 const void *value, size_t size,
349 struct wim_lookup_table *lookup_table);
352 inode_has_named_stream(const struct wim_inode *inode);
355 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
356 struct wim_lookup_table *lookup_table);
359 inode_remove_ads(struct wim_inode *inode, struct wim_ads_entry *entry,
360 struct wim_lookup_table *lookup_table);
363 * Does the specified alternate data stream entry correspond to a named stream?
365 * See inode_needs_dummy_stream() for explanation of why an alternate data
366 * stream entry might, in fact, not be named...
369 ads_entry_is_named_stream(const struct wim_ads_entry *entry)
371 return entry->stream_name_nbytes != 0;
374 /* Is the inode a directory?
375 * This doesn't count directories with reparse data.
376 * wimlib only allows inodes of this type to have children.
379 inode_is_directory(const struct wim_inode *inode)
381 return (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
382 FILE_ATTRIBUTE_REPARSE_POINT))
383 == FILE_ATTRIBUTE_DIRECTORY;
386 /* Is the inode a directory with the encrypted attribute set?
387 * This currently returns true for encrypted directories even if they have
388 * reparse data (not sure if such files can even exist). */
390 inode_is_encrypted_directory(const struct wim_inode *inode)
392 return ((inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
393 FILE_ATTRIBUTE_ENCRYPTED))
394 == (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_ENCRYPTED));
397 /* Is the inode a symbolic link?
398 * This returns true iff the inode is a reparse point that is either a "real"
399 * symbolic link or a junction point. */
401 inode_is_symlink(const struct wim_inode *inode)
403 return (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
404 && (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
405 inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
408 /* Does the inode have children?
409 * Currently (based on read_dentry_tree()), this can only return true for inodes
410 * for which inode_is_directory() returns true. (This also returns false on
411 * empty directories.) */
413 inode_has_children(const struct wim_inode *inode)
415 return inode->i_children != NULL;
419 inode_resolve_streams(struct wim_inode *inode, struct wim_lookup_table *table,
423 stream_not_found_error(const struct wim_inode *inode, const u8 *hash);
426 inode_unresolve_streams(struct wim_inode *inode);
428 static inline struct wim_lookup_table_entry *
429 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
431 wimlib_assert(inode->i_resolved);
432 wimlib_assert(stream_idx <= inode->i_num_ads);
436 return inode->i_ads_entries[stream_idx - 1].lte;
439 static inline struct wim_lookup_table_entry *
440 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
441 const struct wim_lookup_table *table)
443 wimlib_assert(!inode->i_resolved);
444 wimlib_assert(stream_idx <= inode->i_num_ads);
448 return lookup_stream(table, inode->i_hash);
450 return lookup_stream(table, inode->i_ads_entries[ stream_idx - 1].hash);
453 extern struct wim_lookup_table_entry *
454 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
455 const struct wim_lookup_table *table);
457 static inline const u8 *
458 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
460 wimlib_assert(!inode->i_resolved);
461 wimlib_assert(stream_idx <= inode->i_num_ads);
463 return inode->i_hash;
465 return inode->i_ads_entries[stream_idx - 1].hash;
469 static inline const u8 *
470 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
472 struct wim_lookup_table_entry *lte;
473 lte = inode_stream_lte_resolved(inode, stream_idx);
481 * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
482 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
483 * alternate data stream.
485 * This works for both resolved and un-resolved dentries.
487 static inline const u8 *
488 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
490 if (inode->i_resolved)
491 return inode_stream_hash_resolved(inode, stream_idx);
493 return inode_stream_hash_unresolved(inode, stream_idx);
497 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
499 wimlib_assert(stream_idx <= inode->i_num_ads);
503 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
507 inode_stream_idx_to_id(const struct wim_inode *inode, unsigned stream_idx)
512 return inode->i_ads_entries[stream_idx - 1].stream_id;
515 extern struct wim_lookup_table_entry *
516 inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret);
518 extern struct wim_lookup_table_entry *
519 inode_unnamed_lte_resolved(const struct wim_inode *inode);
521 extern struct wim_lookup_table_entry *
522 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
523 const struct wim_lookup_table *table);
525 extern struct wim_lookup_table_entry *
526 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
529 inode_unnamed_stream_hash(const struct wim_inode *inode);
532 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
533 size_t *nbytes_remaining_p);
536 check_inode(struct wim_inode *inode, const struct wim_security_data *sd);
539 inode_ref_streams(struct wim_inode *inode);
542 inode_unref_streams(struct wim_inode *inode,
543 struct wim_lookup_table *lookup_table);
547 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list);
549 #endif /* _WIMLIB_INODE_H */