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"
8 #include "wimlib/unix_data.h"
14 struct wim_security_data;
15 struct wim_lookup_table;
22 * As mentioned in the comment above `struct wim_dentry', in the WIM file that
23 * is no on-disk analogue of a real inode, as most of these fields are
24 * duplicated in the dentries. Instead, a `struct wim_inode' is something we
25 * create ourselves to simplify the handling of hard links.
28 /* If i_resolved == 0:
29 * SHA1 message digest of the contents of the unnamed-data stream
33 * Pointer to the lookup table entry for the unnamed data stream
34 * of this inode, or NULL.
36 * i_hash corresponds to the 'unnamed_stream_hash' field of the `struct
37 * wim_dentry_on_disk' and the additional caveats documented about that
38 * field apply here (for example, the quirks regarding all-zero hashes).
41 u8 i_hash[SHA1_HASH_SIZE];
42 struct wim_lookup_table_entry *i_lte;
45 /* Corresponds to the 'attributes' field of `struct wim_dentry_on_disk';
46 * bitwise OR of the FILE_ATTRIBUTE_* flags that give the attributes of
50 /* Root of a balanced binary search tree storing the child directory
51 * entries of this inode, if any. Keyed by wim_dentry->file_name, case
52 * sensitively. If this inode is not a directory or if it has no
53 * children then this will be an empty tree (NULL). */
54 struct avl_tree_node *i_children;
56 /* Root of a balanced binary search tree storing the child directory
57 * entries of this inode, if any. Keyed by wim_dentry->file_name, case
58 * insensitively. If this inode is not a directory or if it has no
59 * children then this will be an empty tree (NULL). */
60 struct avl_tree_node *i_children_ci;
62 /* List of dentries that are aliases for this inode. There will be
63 * i_nlink dentries in this list. */
64 struct list_head i_dentry;
66 /* Field to place this inode into a list. */
68 /* Hash list node- used in inode_fixup.c when the inodes are
69 * placed into a hash table keyed by inode number and optionally
70 * device number, in order to detect dentries that are aliases
71 * for the same inode. */
72 struct hlist_node i_hlist;
74 /* Normal list node- used to connect all the inodes of a WIM image
75 * into a single linked list referenced from the
76 * `struct wim_image_metadata' for that image. */
77 struct list_head i_list;
80 /* Number of dentries that are aliases for this inode. */
83 /* Number of alternate data streams (ADS) associated with this inode */
86 /* Flag that indicates whether this inode's streams have been
87 * "resolved". By default, the inode starts as "unresolved", meaning
88 * that the i_hash field, along with the hash field of any associated
89 * wim_ads_entry's, are valid and should be used as keys in the WIM
90 * lookup table to find the associated `struct wim_lookup_table_entry'.
91 * But if the inode has been resolved, then each of these fields is
92 * replaced with a pointer directly to the appropriate `struct
93 * wim_lookup_table_entry', or NULL if the stream is empty. */
96 /* Flag used to mark this inode as visited; this is used when visiting
97 * all the inodes in a dentry tree exactly once. It will be 0 by
98 * default and must be cleared following the tree traversal, even in
102 /* 1 iff all ADS entries of this inode are named or if this inode
103 * has no ADS entries */
104 u8 i_canonical_streams : 1;
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;
110 /* Creation time, last access time, and last write time for this inode, in
111 * 100-nanosecond intervals since 12:00 a.m UTC January 1, 1601. They
112 * should correspond to the times gotten by calling GetFileTime() on
115 u64 i_last_access_time;
116 u64 i_last_write_time;
118 /* Corresponds to 'security_id' in `struct wim_dentry_on_disk': The
119 * index of this inode's security descriptor in the WIM image's table of
120 * security descriptors, or -1. Note: in verify_inode(), called
121 * whenever a WIM image is loaded, out-of-bounds indices are set to -1,
122 * so the extraction code does not need to do bounds checks. */
123 int32_t i_security_id;
125 /* Identity of a reparse point. See
126 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
127 * for what a reparse point is. */
130 /* Unused/unknown fields that we just read into memory so we can
131 * re-write them unchanged. */
135 /* Corresponds to not_rpfixed in `struct wim_dentry_on_disk': Set to 0
136 * if reparse point fixups have been done. Otherwise set to 1. Note:
137 * this actually may reflect the SYMBOLIC_LINK_RELATIVE flag.
141 /* Inode number; corresponds to hard_link_group_id in the `struct
142 * wim_dentry_on_disk'. */
145 /* UNIX data (wimlib extension) */
146 struct wimlib_unix_data i_unix_data;
149 /* Device number, used only during image capture, so we can
150 * identify hard linked files by the combination of inode number
151 * and device number (rather than just inode number, which could
152 * be ambigious if the captured tree spans a mountpoint). Set
156 /* Fields used only during extraction */
158 /* List of aliases of this dentry that are being
159 * extracted in the current extraction operation. This
160 * will be a (possibly nonproper) subset of the dentries
161 * in the i_dentry list. This list will be constructed
162 * regardless of whether the extraction backend supports
163 * hard links or not. */
164 struct list_head i_extraction_aliases;
167 /* In NTFS-3g extraction mode, this is set to the Master
168 * File Table (MFT) number of the NTFS file that was
169 * created for this inode. */
175 /* Used only during image mount: Table of file descriptors that
176 * have been opened to this inode. The table is automatically
177 * freed when the last file descriptor is closed. */
178 struct wimfs_fd **i_fds;
183 u16 i_num_opened_fds;
184 u16 i_num_allocated_fds;
187 /* Next alternate data stream ID to be assigned */
188 u32 i_next_stream_id;
191 /* Alternate data stream entry.
193 * We read this from disk in the read_ads_entries() function; see that function
194 * for more explanation. */
195 struct wim_ads_entry {
197 /* SHA-1 message digest of stream contents */
198 u8 hash[SHA1_HASH_SIZE];
200 /* The corresponding lookup table entry (only for resolved
202 struct wim_lookup_table_entry *lte;
205 /* Length of UTF16-encoded stream name, in bytes, not including the
206 * terminating null character; or 0 if the stream is unnamed. */
207 u16 stream_name_nbytes;
209 /* Number to identify an alternate data stream even after it's possibly
210 * been moved or renamed. */
213 /* Stream name (UTF-16LE), null-terminated, or NULL if the stream is
215 utf16lechar *stream_name;
217 /* Reserved field. We read it into memory so we can write it out
222 /* WIM alternate data stream entry (on-disk format) */
223 struct wim_ads_entry_on_disk {
224 /* Length of the entry, in bytes. This apparently includes all
225 * fixed-length fields, plus the stream name and null terminator if
226 * present, and the padding up to an 8 byte boundary. wimlib is a
227 * little less strict when reading the entries, and only requires that
228 * the number of bytes from this field is at least as large as the size
229 * of the fixed length fields and stream name without null terminator.
235 /* SHA1 message digest of the uncompressed stream; or, alternatively,
236 * can be all zeroes if the stream has zero length. */
237 u8 hash[SHA1_HASH_SIZE];
239 /* Length of the stream name, in bytes. 0 if the stream is unnamed. */
240 le16 stream_name_nbytes;
242 /* Stream name in UTF-16LE. It is @stream_name_nbytes bytes long,
243 * excluding the null terminator. There is a null terminator
244 * character if @stream_name_nbytes != 0; i.e., if this stream is named.
246 utf16lechar stream_name[];
249 #define WIM_ADS_ENTRY_DISK_SIZE 38
252 * Reparse tags documented at
253 * http://msdn.microsoft.com/en-us/library/dd541667(v=prot.10).aspx
255 #define WIM_IO_REPARSE_TAG_RESERVED_ZERO 0x00000000
256 #define WIM_IO_REPARSE_TAG_RESERVED_ONE 0x00000001
257 #define WIM_IO_REPARSE_TAG_MOUNT_POINT 0xA0000003
258 #define WIM_IO_REPARSE_TAG_HSM 0xC0000004
259 #define WIM_IO_REPARSE_TAG_HSM2 0x80000006
260 #define WIM_IO_REPARSE_TAG_DRIVER_EXTENDER 0x80000005
261 #define WIM_IO_REPARSE_TAG_SIS 0x80000007
262 #define WIM_IO_REPARSE_TAG_DFS 0x8000000A
263 #define WIM_IO_REPARSE_TAG_DFSR 0x80000012
264 #define WIM_IO_REPARSE_TAG_FILTER_MANAGER 0x8000000B
265 #define WIM_IO_REPARSE_TAG_SYMLINK 0xA000000C
267 #define FILE_ATTRIBUTE_READONLY 0x00000001
268 #define FILE_ATTRIBUTE_HIDDEN 0x00000002
269 #define FILE_ATTRIBUTE_SYSTEM 0x00000004
270 #define FILE_ATTRIBUTE_DIRECTORY 0x00000010
271 #define FILE_ATTRIBUTE_ARCHIVE 0x00000020
272 #define FILE_ATTRIBUTE_DEVICE 0x00000040
273 #define FILE_ATTRIBUTE_NORMAL 0x00000080
274 #define FILE_ATTRIBUTE_TEMPORARY 0x00000100
275 #define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
276 #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
277 #define FILE_ATTRIBUTE_COMPRESSED 0x00000800
278 #define FILE_ATTRIBUTE_OFFLINE 0x00001000
279 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
280 #define FILE_ATTRIBUTE_ENCRYPTED 0x00004000
281 #define FILE_ATTRIBUTE_VIRTUAL 0x00010000
283 extern struct wim_inode *
284 new_inode(void) _malloc_attribute;
286 extern struct wim_inode *
287 new_timeless_inode(void) _malloc_attribute;
290 put_inode(struct wim_inode *inode);
293 free_inode(struct wim_inode *inode);
295 /* Iterate through each alias of an inode. */
296 #define inode_for_each_dentry(dentry, inode) \
297 list_for_each_entry((dentry), &(inode)->i_dentry, d_alias)
299 /* Add a new alias for an inode. Does not increment i_nlink; that must be done
301 #define inode_add_dentry(dentry, inode) \
302 list_add_tail(&(dentry)->d_alias, &(inode)->i_dentry)
304 /* Return an alias of an inode. */
305 #define inode_first_dentry(inode) \
306 container_of(inode->i_dentry.next, struct wim_dentry, d_alias)
308 /* Return the full path of an alias of an inode, or NULL if it could not be
310 #define inode_first_full_path(inode) \
311 dentry_full_path(inode_first_dentry(inode))
313 extern struct wim_ads_entry *
314 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
317 extern struct wim_ads_entry *
318 inode_add_ads_utf16le(struct wim_inode *inode,
319 const utf16lechar *stream_name,
320 size_t stream_name_nbytes);
322 extern struct wim_ads_entry *
323 inode_add_ads(struct wim_inode *dentry, const tchar *stream_name);
326 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
327 const void *value, size_t size,
328 struct wim_lookup_table *lookup_table);
331 inode_has_named_stream(const struct wim_inode *inode);
334 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
335 struct wim_lookup_table *lookup_table);
338 inode_remove_ads(struct wim_inode *inode, u16 idx,
339 struct wim_lookup_table *lookup_table);
342 ads_entry_is_named_stream(const struct wim_ads_entry *entry)
344 return entry->stream_name_nbytes != 0;
348 inode_has_unix_data(const struct wim_inode *inode)
350 return inode->i_unix_data.mode != 0;
353 /* Is the inode a directory?
354 * This doesn't count directories with reparse data.
355 * wimlib only allows inodes of this type to have children.
358 inode_is_directory(const struct wim_inode *inode)
360 return (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
361 FILE_ATTRIBUTE_REPARSE_POINT))
362 == FILE_ATTRIBUTE_DIRECTORY;
365 /* Is the inode a directory with the encrypted attribute set?
366 * This currently returns true for encrypted directories even if they have
367 * reparse data (not sure if such files can even exist). */
369 inode_is_encrypted_directory(const struct wim_inode *inode)
371 return ((inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
372 FILE_ATTRIBUTE_ENCRYPTED))
373 == (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_ENCRYPTED));
376 /* Is the inode a symbolic link?
377 * This returns true iff the inode is a reparse point that is either a "real"
378 * symbolic link or a junction point. */
380 inode_is_symlink(const struct wim_inode *inode)
382 return (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
383 && (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
384 inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
387 /* Does the inode have children?
388 * Currently (based on read_dentry_tree()), this can only return true for inodes
389 * for which inode_is_directory() returns true. (This also returns false on
390 * empty directories.) */
392 inode_has_children(const struct wim_inode *inode)
394 return inode->i_children != NULL;
398 inode_resolve_streams(struct wim_inode *inode, struct wim_lookup_table *table,
402 stream_not_found_error(const struct wim_inode *inode, const u8 *hash);
405 inode_unresolve_streams(struct wim_inode *inode);
407 static inline struct wim_lookup_table_entry *
408 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
410 wimlib_assert(inode->i_resolved);
411 wimlib_assert(stream_idx <= inode->i_num_ads);
415 return inode->i_ads_entries[stream_idx - 1].lte;
418 static inline struct wim_lookup_table_entry *
419 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
420 const struct wim_lookup_table *table)
422 wimlib_assert(!inode->i_resolved);
423 wimlib_assert(stream_idx <= inode->i_num_ads);
427 return lookup_stream(table, inode->i_hash);
429 return lookup_stream(table, inode->i_ads_entries[ stream_idx - 1].hash);
432 extern struct wim_lookup_table_entry *
433 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
434 const struct wim_lookup_table *table);
436 static inline const u8 *
437 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
439 wimlib_assert(!inode->i_resolved);
440 wimlib_assert(stream_idx <= inode->i_num_ads);
442 return inode->i_hash;
444 return inode->i_ads_entries[stream_idx - 1].hash;
448 static inline const u8 *
449 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
451 struct wim_lookup_table_entry *lte;
452 lte = inode_stream_lte_resolved(inode, stream_idx);
460 * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
461 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
462 * alternate data stream.
464 * This works for both resolved and un-resolved dentries.
466 static inline const u8 *
467 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
469 if (inode->i_resolved)
470 return inode_stream_hash_resolved(inode, stream_idx);
472 return inode_stream_hash_unresolved(inode, stream_idx);
476 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
478 wimlib_assert(stream_idx <= inode->i_num_ads);
482 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
485 extern struct wim_lookup_table_entry *
486 inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret);
488 extern struct wim_lookup_table_entry *
489 inode_unnamed_lte_resolved(const struct wim_inode *inode);
491 extern struct wim_lookup_table_entry *
492 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
493 const struct wim_lookup_table *table);
495 extern struct wim_lookup_table_entry *
496 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
499 inode_unnamed_stream_hash(const struct wim_inode *inode);
502 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
503 size_t nbytes_remaining);
506 verify_inode(struct wim_inode *inode, const struct wim_security_data *sd);
509 inode_ref_streams(struct wim_inode *inode);
512 inode_unref_streams(struct wim_inode *inode,
513 struct wim_lookup_table *lookup_table);
517 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list);
519 #endif /* _WIMLIB_INODE_H */