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