]> wimlib.net Git - wimlib/blob - include/wimlib/inode.h
04ec9682ad5886da5a55532401ba614e0e393b69
[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         /* Set if the DOS name of an inode has already been extracted.  */
103         u8 i_dos_name_extracted : 1;
104
105         /* 1 iff all ADS entries of this inode are named or if this inode
106          * has no ADS entries  */
107         u8 i_canonical_streams : 1;
108
109         /* Pointer to a malloc()ed array of i_num_ads alternate data stream
110          * entries for this inode.  */
111         struct wim_ads_entry *i_ads_entries;
112
113         /* Creation time, last access time, and last write time for this inode, in
114          * 100-nanosecond intervals since 12:00 a.m UTC January 1, 1601.  They
115          * should correspond to the times gotten by calling GetFileTime() on
116          * Windows. */
117         u64 i_creation_time;
118         u64 i_last_access_time;
119         u64 i_last_write_time;
120
121         /* Corresponds to 'security_id' in `struct wim_dentry_on_disk':  The
122          * index of this inode's security descriptor in the WIM image's table of
123          * security descriptors, or -1.  Note: in verify_inode(), called
124          * whenever a WIM image is loaded, out-of-bounds indices are set to -1,
125          * so the extraction code does not need to do bounds checks.  */
126         int32_t i_security_id;
127
128         /* Identity of a reparse point.  See
129          * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365503(v=vs.85).aspx
130          * for what a reparse point is. */
131         u32 i_reparse_tag;
132
133         /* Unused/unknown fields that we just read into memory so we can
134          * re-write them unchanged.  */
135         u32 i_rp_unknown_1;
136         u16 i_rp_unknown_2;
137
138         /* Corresponds to not_rpfixed in `struct wim_dentry_on_disk':  Set to 0
139          * if reparse point fixups have been done.  Otherwise set to 1.  Note:
140          * this actually may reflect the SYMBOLIC_LINK_RELATIVE flag.
141          */
142         u16 i_not_rpfixed;
143
144         /* Inode number; corresponds to hard_link_group_id in the `struct
145          * wim_dentry_on_disk'.  */
146         u64 i_ino;
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                 struct {
157
158                         /* Used only during image extraction: pointer to the first path
159                          * (malloc()ed buffer) at which this inode has been extracted.
160                          * Freed and set to NULL after the extraction is done (either
161                          * success or failure).  */
162                         tchar *i_extracted_file;
163
164                         /** Used only during image extraction: "cookie" that
165                          * identifies this extracted file (inode), for example
166                          * an inode number.  Only used if supported by the
167                          * extraction mode.  */
168                         u64 extract_cookie;
169                 };
170
171 #ifdef WITH_FUSE
172                 /* Used only during image mount:  Table of file descriptors that
173                  * have been opened to this inode.  The table is automatically
174                  * freed when the last file descriptor is closed.  */
175                 struct wimfs_fd **i_fds;
176 #endif
177         };
178
179 #ifdef WITH_FUSE
180         u16 i_num_opened_fds;
181         u16 i_num_allocated_fds;
182 #endif
183
184         /* Next alternate data stream ID to be assigned */
185         u32 i_next_stream_id;
186 };
187
188 /* Alternate data stream entry.
189  *
190  * We read this from disk in the read_ads_entries() function; see that function
191  * for more explanation. */
192 struct wim_ads_entry {
193         union {
194                 /* SHA-1 message digest of stream contents */
195                 u8 hash[SHA1_HASH_SIZE];
196
197                 /* The corresponding lookup table entry (only for resolved
198                  * streams) */
199                 struct wim_lookup_table_entry *lte;
200         };
201
202         /* Length of UTF16-encoded stream name, in bytes, not including the
203          * terminating null character; or 0 if the stream is unnamed. */
204         u16 stream_name_nbytes;
205
206         /* Number to identify an alternate data stream even after it's possibly
207          * been moved or renamed. */
208         u32 stream_id;
209
210         /* Stream name (UTF-16LE), null-terminated, or NULL if the stream is
211          * unnamed.  */
212         utf16lechar *stream_name;
213
214         /* Reserved field.  We read it into memory so we can write it out
215          * unchanged. */
216         u64 reserved;
217 };
218
219 /* WIM alternate data stream entry (on-disk format) */
220 struct wim_ads_entry_on_disk {
221         /*  Length of the entry, in bytes.  This apparently includes all
222          *  fixed-length fields, plus the stream name and null terminator if
223          *  present, and the padding up to an 8 byte boundary.  wimlib is a
224          *  little less strict when reading the entries, and only requires that
225          *  the number of bytes from this field is at least as large as the size
226          *  of the fixed length fields and stream name without null terminator.
227          *  */
228         le64  length;
229
230         le64  reserved;
231
232         /* SHA1 message digest of the uncompressed stream; or, alternatively,
233          * can be all zeroes if the stream has zero length. */
234         u8 hash[SHA1_HASH_SIZE];
235
236         /* Length of the stream name, in bytes.  0 if the stream is unnamed.  */
237         le16 stream_name_nbytes;
238
239         /* Stream name in UTF-16LE.  It is @stream_name_nbytes bytes long,
240          * excluding the the null terminator.  There is a null terminator
241          * character if @stream_name_nbytes != 0; i.e., if this stream is named.
242          * */
243         utf16lechar stream_name[];
244 } _packed_attribute;
245
246 #define WIM_ADS_ENTRY_DISK_SIZE 38
247
248 /*
249  * Reparse tags documented at
250  * http://msdn.microsoft.com/en-us/library/dd541667(v=prot.10).aspx
251  */
252 #define WIM_IO_REPARSE_TAG_RESERVED_ZERO        0x00000000
253 #define WIM_IO_REPARSE_TAG_RESERVED_ONE         0x00000001
254 #define WIM_IO_REPARSE_TAG_MOUNT_POINT          0xA0000003
255 #define WIM_IO_REPARSE_TAG_HSM                  0xC0000004
256 #define WIM_IO_REPARSE_TAG_HSM2                 0x80000006
257 #define WIM_IO_REPARSE_TAG_DRIVER_EXTENDER      0x80000005
258 #define WIM_IO_REPARSE_TAG_SIS                  0x80000007
259 #define WIM_IO_REPARSE_TAG_DFS                  0x8000000A
260 #define WIM_IO_REPARSE_TAG_DFSR                 0x80000012
261 #define WIM_IO_REPARSE_TAG_FILTER_MANAGER       0x8000000B
262 #define WIM_IO_REPARSE_TAG_SYMLINK              0xA000000C
263
264 #define FILE_ATTRIBUTE_READONLY            0x00000001
265 #define FILE_ATTRIBUTE_HIDDEN              0x00000002
266 #define FILE_ATTRIBUTE_SYSTEM              0x00000004
267 #define FILE_ATTRIBUTE_DIRECTORY           0x00000010
268 #define FILE_ATTRIBUTE_ARCHIVE             0x00000020
269 #define FILE_ATTRIBUTE_DEVICE              0x00000040
270 #define FILE_ATTRIBUTE_NORMAL              0x00000080
271 #define FILE_ATTRIBUTE_TEMPORARY           0x00000100
272 #define FILE_ATTRIBUTE_SPARSE_FILE         0x00000200
273 #define FILE_ATTRIBUTE_REPARSE_POINT       0x00000400
274 #define FILE_ATTRIBUTE_COMPRESSED          0x00000800
275 #define FILE_ATTRIBUTE_OFFLINE             0x00001000
276 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
277 #define FILE_ATTRIBUTE_ENCRYPTED           0x00004000
278 #define FILE_ATTRIBUTE_VIRTUAL             0x00010000
279
280 extern struct wim_inode *
281 new_inode(void) _malloc_attribute;
282
283 extern struct wim_inode *
284 new_timeless_inode(void) _malloc_attribute;
285
286 extern void
287 put_inode(struct wim_inode *inode);
288
289 extern void
290 free_inode(struct wim_inode *inode);
291
292 /* Iterate through each alias of an inode.  */
293 #define inode_for_each_dentry(dentry, inode) \
294                 list_for_each_entry((dentry), &(inode)->i_dentry, d_alias)
295
296 /* Add a new alias for an inode.  Does not increment i_nlink; that must be done
297  * separately.  */
298 #define inode_add_dentry(dentry, inode) \
299                 list_add_tail(&(dentry)->d_alias, &(inode)->i_dentry)
300
301 /* Return an alias of an inode.  */
302 #define inode_first_dentry(inode) \
303                 container_of(inode->i_dentry.next, struct wim_dentry, d_alias)
304
305 /* Return the full path of an alias of an inode, or NULL if it could not be
306  * determined.  */
307 #define inode_first_full_path(inode) \
308                 dentry_full_path(inode_first_dentry(inode))
309
310 extern struct wim_ads_entry *
311 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
312                     u16 *idx_ret);
313
314 extern struct wim_ads_entry *
315 inode_add_ads_utf16le(struct wim_inode *inode,
316                       const utf16lechar *stream_name,
317                       size_t stream_name_nbytes);
318
319 extern struct wim_ads_entry *
320 inode_add_ads(struct wim_inode *dentry, const tchar *stream_name);
321
322 extern int
323 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
324                         const void *value, size_t size,
325                         struct wim_lookup_table *lookup_table);
326
327 extern bool
328 inode_has_named_stream(const struct wim_inode *inode);
329
330 extern int
331 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
332                          struct wim_lookup_table *lookup_table);
333
334 extern void
335 inode_remove_ads(struct wim_inode *inode, u16 idx,
336                  struct wim_lookup_table *lookup_table);
337
338 static inline bool
339 ads_entry_is_unix_data(const struct wim_ads_entry *entry)
340 {
341         return (entry->stream_name_nbytes ==
342                         WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES) &&
343                 !memcmp(entry->stream_name, WIMLIB_UNIX_DATA_TAG_UTF16LE,
344                         WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES);
345 }
346
347 static inline bool
348 ads_entry_is_named_stream(const struct wim_ads_entry *entry)
349 {
350         return entry->stream_name_nbytes != 0 && !ads_entry_is_unix_data(entry);
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  */