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 the WIM file that
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;
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;
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. */
115 /* Size of @i_extra buffer in bytes. If 0, there is no extra data. */
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
123 u64 i_last_access_time;
124 u64 i_last_write_time;
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: in verify_inode(), called
129 * whenever a WIM image is loaded, out-of-bounds indices are set to -1,
130 * so the extraction code does not need to do bounds checks. */
131 int32_t i_security_id;
133 /* Identity of a reparse point. See
134 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
135 * for what a reparse point is. */
138 /* Unused/unknown fields that we just read into memory so we can
139 * re-write them unchanged. */
143 /* Corresponds to not_rpfixed in `struct wim_dentry_on_disk': Set to 0
144 * if reparse point fixups have been done. Otherwise set to 1. Note:
145 * this actually may reflect the SYMBOLIC_LINK_RELATIVE flag.
149 /* Inode number; corresponds to hard_link_group_id in the `struct
150 * wim_dentry_on_disk'. */
154 /* Device number, used only during image capture, so we can
155 * identify hard linked files by the combination of inode number
156 * and device number (rather than just inode number, which could
157 * be ambigious if the captured tree spans a mountpoint). Set
161 /* Fields used only during extraction */
163 /* List of aliases of this dentry that are being
164 * extracted in the current extraction operation. This
165 * will be a (possibly nonproper) subset of the dentries
166 * in the i_dentry list. This list will be constructed
167 * regardless of whether the extraction backend supports
168 * hard links or not. */
169 struct list_head i_extraction_aliases;
172 /* In NTFS-3g extraction mode, this is set to the Master
173 * File Table (MFT) number of the NTFS file that was
174 * created for this inode. */
181 /* Used only during image mount: Table of file
182 * descriptors that have been opened to this inode.
183 * This table is freed when the last file descriptor is
185 struct wimfs_fd **i_fds;
187 /* Lower bound on the index of the next available entry
195 u16 i_num_opened_fds;
196 u16 i_num_allocated_fds;
199 /* Next alternate data stream ID to be assigned */
200 u32 i_next_stream_id;
203 /* Alternate data stream entry.
205 * We read this from disk in the read_ads_entries() function; see that function
206 * for more explanation. */
207 struct wim_ads_entry {
209 /* SHA-1 message digest of stream contents */
210 u8 hash[SHA1_HASH_SIZE];
212 /* The corresponding lookup table entry (only for resolved
214 struct wim_lookup_table_entry *lte;
217 /* Length of UTF16-encoded stream name, in bytes, not including the
218 * terminating null character; or 0 if the stream is unnamed. */
219 u16 stream_name_nbytes;
221 /* Number to identify an alternate data stream even after it's possibly
222 * been moved or renamed. */
225 /* Stream name (UTF-16LE), null-terminated, or NULL if the stream is
227 utf16lechar *stream_name;
229 /* Reserved field. We read it into memory so we can write it out
234 /* WIM alternate data stream entry (on-disk format) */
235 struct wim_ads_entry_on_disk {
236 /* Length of the entry, in bytes. This apparently includes all
237 * fixed-length fields, plus the stream name and null terminator if
238 * present, and the padding up to an 8 byte boundary. wimlib is a
239 * little less strict when reading the entries, and only requires that
240 * the number of bytes from this field is at least as large as the size
241 * of the fixed length fields and stream name without null terminator.
247 /* SHA1 message digest of the uncompressed stream; or, alternatively,
248 * can be all zeroes if the stream has zero length. */
249 u8 hash[SHA1_HASH_SIZE];
251 /* Length of the stream name, in bytes. 0 if the stream is unnamed. */
252 le16 stream_name_nbytes;
254 /* Stream name in UTF-16LE. It is @stream_name_nbytes bytes long,
255 * excluding the null terminator. There is a null terminator
256 * character if @stream_name_nbytes != 0; i.e., if this stream is named.
258 utf16lechar stream_name[];
261 #define WIM_ADS_ENTRY_DISK_SIZE 38
264 * Reparse tags documented at
265 * http://msdn.microsoft.com/en-us/library/dd541667(v=prot.10).aspx
267 #define WIM_IO_REPARSE_TAG_RESERVED_ZERO 0x00000000
268 #define WIM_IO_REPARSE_TAG_RESERVED_ONE 0x00000001
269 #define WIM_IO_REPARSE_TAG_MOUNT_POINT 0xA0000003
270 #define WIM_IO_REPARSE_TAG_HSM 0xC0000004
271 #define WIM_IO_REPARSE_TAG_HSM2 0x80000006
272 #define WIM_IO_REPARSE_TAG_DRIVER_EXTENDER 0x80000005
273 #define WIM_IO_REPARSE_TAG_SIS 0x80000007
274 #define WIM_IO_REPARSE_TAG_DFS 0x8000000A
275 #define WIM_IO_REPARSE_TAG_DFSR 0x80000012
276 #define WIM_IO_REPARSE_TAG_FILTER_MANAGER 0x8000000B
277 #define WIM_IO_REPARSE_TAG_SYMLINK 0xA000000C
279 #define FILE_ATTRIBUTE_READONLY 0x00000001
280 #define FILE_ATTRIBUTE_HIDDEN 0x00000002
281 #define FILE_ATTRIBUTE_SYSTEM 0x00000004
282 #define FILE_ATTRIBUTE_DIRECTORY 0x00000010
283 #define FILE_ATTRIBUTE_ARCHIVE 0x00000020
284 #define FILE_ATTRIBUTE_DEVICE 0x00000040
285 #define FILE_ATTRIBUTE_NORMAL 0x00000080
286 #define FILE_ATTRIBUTE_TEMPORARY 0x00000100
287 #define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
288 #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
289 #define FILE_ATTRIBUTE_COMPRESSED 0x00000800
290 #define FILE_ATTRIBUTE_OFFLINE 0x00001000
291 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
292 #define FILE_ATTRIBUTE_ENCRYPTED 0x00004000
293 #define FILE_ATTRIBUTE_VIRTUAL 0x00010000
295 extern struct wim_inode *
296 new_inode(void) _malloc_attribute;
298 extern struct wim_inode *
299 new_timeless_inode(void) _malloc_attribute;
302 put_inode(struct wim_inode *inode);
305 free_inode(struct wim_inode *inode);
307 /* Iterate through each alias of an inode. */
308 #define inode_for_each_dentry(dentry, inode) \
309 list_for_each_entry((dentry), &(inode)->i_dentry, d_alias)
311 /* Add a new alias for an inode. Does not increment i_nlink; that must be done
313 #define inode_add_dentry(dentry, inode) \
314 list_add_tail(&(dentry)->d_alias, &(inode)->i_dentry)
316 /* Return an alias of an inode. */
317 #define inode_first_dentry(inode) \
318 container_of(inode->i_dentry.next, struct wim_dentry, d_alias)
320 /* Return the full path of an alias of an inode, or NULL if it could not be
322 #define inode_first_full_path(inode) \
323 dentry_full_path(inode_first_dentry(inode))
325 extern struct wim_ads_entry *
326 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name);
328 extern struct wim_ads_entry *
329 inode_add_ads_utf16le(struct wim_inode *inode,
330 const utf16lechar *stream_name,
331 size_t stream_name_nbytes);
333 extern struct wim_ads_entry *
334 inode_add_ads(struct wim_inode *dentry, const tchar *stream_name);
336 extern struct wim_ads_entry *
337 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
338 const void *value, size_t size,
339 struct wim_lookup_table *lookup_table);
342 inode_has_named_stream(const struct wim_inode *inode);
345 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
346 struct wim_lookup_table *lookup_table);
349 inode_remove_ads(struct wim_inode *inode, struct wim_ads_entry *entry,
350 struct wim_lookup_table *lookup_table);
353 ads_entry_is_named_stream(const struct wim_ads_entry *entry)
355 return entry->stream_name_nbytes != 0;
358 /* Is the inode a directory?
359 * This doesn't count directories with reparse data.
360 * wimlib only allows inodes of this type to have children.
363 inode_is_directory(const struct wim_inode *inode)
365 return (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
366 FILE_ATTRIBUTE_REPARSE_POINT))
367 == FILE_ATTRIBUTE_DIRECTORY;
370 /* Is the inode a directory with the encrypted attribute set?
371 * This currently returns true for encrypted directories even if they have
372 * reparse data (not sure if such files can even exist). */
374 inode_is_encrypted_directory(const struct wim_inode *inode)
376 return ((inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
377 FILE_ATTRIBUTE_ENCRYPTED))
378 == (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_ENCRYPTED));
381 /* Is the inode a symbolic link?
382 * This returns true iff the inode is a reparse point that is either a "real"
383 * symbolic link or a junction point. */
385 inode_is_symlink(const struct wim_inode *inode)
387 return (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
388 && (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
389 inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
392 /* Does the inode have children?
393 * Currently (based on read_dentry_tree()), this can only return true for inodes
394 * for which inode_is_directory() returns true. (This also returns false on
395 * empty directories.) */
397 inode_has_children(const struct wim_inode *inode)
399 return inode->i_children != NULL;
403 inode_resolve_streams(struct wim_inode *inode, struct wim_lookup_table *table,
407 stream_not_found_error(const struct wim_inode *inode, const u8 *hash);
410 inode_unresolve_streams(struct wim_inode *inode);
412 static inline struct wim_lookup_table_entry *
413 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
415 wimlib_assert(inode->i_resolved);
416 wimlib_assert(stream_idx <= inode->i_num_ads);
420 return inode->i_ads_entries[stream_idx - 1].lte;
423 static inline struct wim_lookup_table_entry *
424 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
425 const struct wim_lookup_table *table)
427 wimlib_assert(!inode->i_resolved);
428 wimlib_assert(stream_idx <= inode->i_num_ads);
432 return lookup_stream(table, inode->i_hash);
434 return lookup_stream(table, inode->i_ads_entries[ stream_idx - 1].hash);
437 extern struct wim_lookup_table_entry *
438 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
439 const struct wim_lookup_table *table);
441 static inline const u8 *
442 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
444 wimlib_assert(!inode->i_resolved);
445 wimlib_assert(stream_idx <= inode->i_num_ads);
447 return inode->i_hash;
449 return inode->i_ads_entries[stream_idx - 1].hash;
453 static inline const u8 *
454 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
456 struct wim_lookup_table_entry *lte;
457 lte = inode_stream_lte_resolved(inode, stream_idx);
465 * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
466 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
467 * alternate data stream.
469 * This works for both resolved and un-resolved dentries.
471 static inline const u8 *
472 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
474 if (inode->i_resolved)
475 return inode_stream_hash_resolved(inode, stream_idx);
477 return inode_stream_hash_unresolved(inode, stream_idx);
481 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
483 wimlib_assert(stream_idx <= inode->i_num_ads);
487 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
491 inode_stream_idx_to_id(const struct wim_inode *inode, unsigned stream_idx)
496 return inode->i_ads_entries[stream_idx - 1].stream_id;
499 extern struct wim_lookup_table_entry *
500 inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret);
502 extern struct wim_lookup_table_entry *
503 inode_unnamed_lte_resolved(const struct wim_inode *inode);
505 extern struct wim_lookup_table_entry *
506 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
507 const struct wim_lookup_table *table);
509 extern struct wim_lookup_table_entry *
510 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
513 inode_unnamed_stream_hash(const struct wim_inode *inode);
516 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
517 size_t *nbytes_remaining_p);
520 verify_inode(struct wim_inode *inode, const struct wim_security_data *sd);
523 inode_ref_streams(struct wim_inode *inode);
526 inode_unref_streams(struct wim_inode *inode,
527 struct wim_lookup_table *lookup_table);
531 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list);
533 #endif /* _WIMLIB_INODE_H */