]> wimlib.net Git - wimlib/blob - include/wimlib/inode.h
Update progress functions
[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 #include "wimlib/unix_data.h"
9
10 #include <string.h>
11
12 struct wim_ads_entry;
13 struct wim_dentry;
14 struct wim_security_data;
15 struct wim_lookup_table;
16 struct wimfs_fd;
17 struct avl_tree_node;
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 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;
55
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;
61
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;
65
66         /* Field to place this inode into a list. */
67         union {
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;
73
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;
78         };
79
80         /* Number of dentries that are aliases for this inode.  */
81         u32 i_nlink;
82
83         /* Number of alternate data streams (ADS) associated with this inode */
84         u16 i_num_ads;
85
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.  */
94         u8 i_resolved : 1;
95
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
99          * error paths.  */
100         u8 i_visited : 1;
101
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;
105
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;
109
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
113          * Windows. */
114         u64 i_creation_time;
115         u64 i_last_access_time;
116         u64 i_last_write_time;
117
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;
124
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. */
128         u32 i_reparse_tag;
129
130         /* Unused/unknown fields that we just read into memory so we can
131          * re-write them unchanged.  */
132         u32 i_rp_unknown_1;
133         u16 i_rp_unknown_2;
134
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.
138          */
139         u16 i_not_rpfixed;
140
141         /* Inode number; corresponds to hard_link_group_id in the `struct
142          * wim_dentry_on_disk'.  */
143         u64 i_ino;
144
145         /* UNIX data (wimlib extension)  */
146         struct wimlib_unix_data i_unix_data;
147
148         union {
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
153                  * to 0 otherwise.  */
154                 u64 i_devno;
155
156                 /* Fields used only during extraction  */
157                 struct {
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;
165
166                 #ifdef WITH_NTFS_3G
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.  */
170                         u64 i_mft_no;
171                 #endif
172                 };
173
174 #ifdef WITH_FUSE
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;
179 #endif
180         };
181
182 #ifdef WITH_FUSE
183         u16 i_num_opened_fds;
184         u16 i_num_allocated_fds;
185 #endif
186
187         /* Next alternate data stream ID to be assigned */
188         u32 i_next_stream_id;
189 };
190
191 /* Alternate data stream entry.
192  *
193  * We read this from disk in the read_ads_entries() function; see that function
194  * for more explanation. */
195 struct wim_ads_entry {
196         union {
197                 /* SHA-1 message digest of stream contents */
198                 u8 hash[SHA1_HASH_SIZE];
199
200                 /* The corresponding lookup table entry (only for resolved
201                  * streams) */
202                 struct wim_lookup_table_entry *lte;
203         };
204
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;
208
209         /* Number to identify an alternate data stream even after it's possibly
210          * been moved or renamed. */
211         u32 stream_id;
212
213         /* Stream name (UTF-16LE), null-terminated, or NULL if the stream is
214          * unnamed.  */
215         utf16lechar *stream_name;
216
217         /* Reserved field.  We read it into memory so we can write it out
218          * unchanged. */
219         u64 reserved;
220 };
221
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.
230          *  */
231         le64  length;
232
233         le64  reserved;
234
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];
238
239         /* Length of the stream name, in bytes.  0 if the stream is unnamed.  */
240         le16 stream_name_nbytes;
241
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.
245          * */
246         utf16lechar stream_name[];
247 } _packed_attribute;
248
249 #define WIM_ADS_ENTRY_DISK_SIZE 38
250
251 /*
252  * Reparse tags documented at
253  * http://msdn.microsoft.com/en-us/library/dd541667(v=prot.10).aspx
254  */
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
266
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
282
283 extern struct wim_inode *
284 new_inode(void) _malloc_attribute;
285
286 extern struct wim_inode *
287 new_timeless_inode(void) _malloc_attribute;
288
289 extern void
290 put_inode(struct wim_inode *inode);
291
292 extern void
293 free_inode(struct wim_inode *inode);
294
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)
298
299 /* Add a new alias for an inode.  Does not increment i_nlink; that must be done
300  * separately.  */
301 #define inode_add_dentry(dentry, inode) \
302                 list_add_tail(&(dentry)->d_alias, &(inode)->i_dentry)
303
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)
307
308 /* Return the full path of an alias of an inode, or NULL if it could not be
309  * determined.  */
310 #define inode_first_full_path(inode) \
311                 dentry_full_path(inode_first_dentry(inode))
312
313 extern struct wim_ads_entry *
314 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
315                     u16 *idx_ret);
316
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);
321
322 extern struct wim_ads_entry *
323 inode_add_ads(struct wim_inode *dentry, const tchar *stream_name);
324
325 extern int
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);
329
330 extern bool
331 inode_has_named_stream(const struct wim_inode *inode);
332
333 extern int
334 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
335                          struct wim_lookup_table *lookup_table);
336
337 extern void
338 inode_remove_ads(struct wim_inode *inode, u16 idx,
339                  struct wim_lookup_table *lookup_table);
340
341 static inline bool
342 ads_entry_is_named_stream(const struct wim_ads_entry *entry)
343 {
344         return entry->stream_name_nbytes != 0;
345 }
346
347 static inline bool
348 inode_has_unix_data(const struct wim_inode *inode)
349 {
350         return inode->i_unix_data.mode != 0;
351 }
352
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.
356  */
357 static inline bool
358 inode_is_directory(const struct wim_inode *inode)
359 {
360         return (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
361                                        FILE_ATTRIBUTE_REPARSE_POINT))
362                         == FILE_ATTRIBUTE_DIRECTORY;
363 }
364
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).  */
368 static inline bool
369 inode_is_encrypted_directory(const struct wim_inode *inode)
370 {
371         return ((inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
372                                         FILE_ATTRIBUTE_ENCRYPTED))
373                 == (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_ENCRYPTED));
374 }
375
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.  */
379 static inline bool
380 inode_is_symlink(const struct wim_inode *inode)
381 {
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);
385 }
386
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.)  */
391 static inline bool
392 inode_has_children(const struct wim_inode *inode)
393 {
394         return inode->i_children != NULL;
395 }
396
397 extern int
398 inode_resolve_streams(struct wim_inode *inode, struct wim_lookup_table *table,
399                       bool force);
400
401 extern int
402 stream_not_found_error(const struct wim_inode *inode, const u8 *hash);
403
404 extern void
405 inode_unresolve_streams(struct wim_inode *inode);
406
407 static inline struct wim_lookup_table_entry *
408 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
409 {
410         wimlib_assert(inode->i_resolved);
411         wimlib_assert(stream_idx <= inode->i_num_ads);
412         if (stream_idx == 0)
413                 return inode->i_lte;
414         else
415                 return inode->i_ads_entries[stream_idx - 1].lte;
416 }
417
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)
421 {
422         wimlib_assert(!inode->i_resolved);
423         wimlib_assert(stream_idx <= inode->i_num_ads);
424         if (table == NULL)
425                 return NULL;
426         if (stream_idx == 0)
427                 return lookup_stream(table, inode->i_hash);
428         else
429                 return lookup_stream(table, inode->i_ads_entries[ stream_idx - 1].hash);
430 }
431
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);
435
436 static inline const u8 *
437 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
438 {
439         wimlib_assert(!inode->i_resolved);
440         wimlib_assert(stream_idx <= inode->i_num_ads);
441         if (stream_idx == 0)
442                 return inode->i_hash;
443         else
444                 return inode->i_ads_entries[stream_idx - 1].hash;
445 }
446
447
448 static inline const u8 *
449 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
450 {
451         struct wim_lookup_table_entry *lte;
452         lte = inode_stream_lte_resolved(inode, stream_idx);
453         if (lte)
454                 return lte->hash;
455         else
456                 return zero_hash;
457 }
458
459 /*
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.
463  *
464  * This works for both resolved and un-resolved dentries.
465  */
466 static inline const u8 *
467 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
468 {
469         if (inode->i_resolved)
470                 return inode_stream_hash_resolved(inode, stream_idx);
471         else
472                 return inode_stream_hash_unresolved(inode, stream_idx);
473 }
474
475 static inline u16
476 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
477 {
478         wimlib_assert(stream_idx <= inode->i_num_ads);
479         if (stream_idx == 0)
480                 return 0;
481         else
482                 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
483 }
484
485 extern struct wim_lookup_table_entry *
486 inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret);
487
488 extern struct wim_lookup_table_entry *
489 inode_unnamed_lte_resolved(const struct wim_inode *inode);
490
491 extern struct wim_lookup_table_entry *
492 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
493                              const struct wim_lookup_table *table);
494
495 extern struct wim_lookup_table_entry *
496 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
497
498 extern const u8 *
499 inode_unnamed_stream_hash(const struct wim_inode *inode);
500
501 extern int
502 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
503                  size_t nbytes_remaining);
504
505 extern int
506 verify_inode(struct wim_inode *inode, const struct wim_security_data *sd);
507
508 extern void
509 inode_ref_streams(struct wim_inode *inode);
510
511 extern void
512 inode_unref_streams(struct wim_inode *inode,
513                     struct wim_lookup_table *lookup_table);
514
515 /* inode_fixup.c  */
516 extern int
517 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list);
518
519 #endif /* _WIMLIB_INODE_H  */