]> wimlib.net Git - wimlib/blob - include/wimlib/lookup_table.h
Cleanup and add more comments
[wimlib] / include / wimlib / lookup_table.h
1 #ifndef _WIMLIB_LOOKUP_TABLE_H
2 #define _WIMLIB_LOOKUP_TABLE_H
3
4 #include "wimlib/assert.h"
5 #include "wimlib/dentry.h"
6 #include "wimlib/list.h"
7 #include "wimlib/sha1.h"
8 #include "wimlib/types.h"
9 #include "wimlib/wim.h"
10
11 #define LOOKUP_FLAG_ADS_OK              0x00000001
12 #define LOOKUP_FLAG_DIRECTORY_OK        0x00000002
13
14
15 /* The lookup table of a WIM file maps SHA1 message digests to streams of data.
16  * Here, the in-memory structure is implemented as a hash table.
17  *
18  * Given a SHA1 message digest, the mapped-to stream is specified by an offset
19  * in the WIM, an uncompressed and compressed size, and resource flags (see
20  * 'struct resource_entry').  But, we associate additional information, such as
21  * a reference count, with each stream, so the actual mapping is from SHA1
22  * message digests to 'struct wim_lookup_table_entry's, each of which contains
23  * an embedded 'struct resource_entry'.
24  *
25  * Note: Everything will break horribly if there is a SHA1 collision.
26  */
27 struct wim_lookup_table {
28         struct hlist_head *array;
29         u64 num_entries;
30         u64 capacity;
31         struct list_head *unhashed_streams;
32 };
33
34 #ifdef WITH_NTFS_3G
35
36 struct _ntfs_volume;
37
38 struct ntfs_location {
39         tchar *path;
40         utf16lechar *stream_name;
41         u16 stream_name_nchars;
42         struct _ntfs_volume *ntfs_vol;
43         bool is_reparse_point;
44 };
45 #endif
46
47 /* An enumerated type that identifies where the stream corresponding to this
48  * lookup table entry is actually located.
49  *
50  * If we open a WIM and read its lookup table, the location is set to
51  * RESOURCE_IN_WIM since all the streams will initially be located in the WIM.
52  * However, to handle situations such as image capture and image mount, we allow
53  * the actual location of the stream to be somewhere else, such as an external
54  * file.
55  */
56 enum resource_location {
57         /* The lookup table entry does not yet correspond to a stream; this is a
58          * temporary state only.  */
59         RESOURCE_NONEXISTENT = 0,
60
61         /* The stream is located in a resource in a WIM file identified by the
62          * `struct wim_resource_spec' pointed to by @rspec.  @offset_in_res
63          * identifies the offset at which this particular stream begins in the
64          * uncompressed data of the resource; this is normally 0, but in general
65          * a WIM resource may contain multiple streams.  */
66         RESOURCE_IN_WIM,
67
68         /* The stream is located in the external file named by @file_on_disk.
69          * On Windows, @file_on_disk may actually specify a named data stream.
70          */
71         RESOURCE_IN_FILE_ON_DISK,
72
73         /* The stream is directly attached in the in-memory buffer pointed to by
74          * @attached_buffer.  */
75         RESOURCE_IN_ATTACHED_BUFFER,
76
77 #ifdef WITH_FUSE
78         /* The stream is located in the external file named by
79          * @staging_file_name, located in the staging directory for a read-write
80          * mount.  */
81         RESOURCE_IN_STAGING_FILE,
82 #endif
83
84 #ifdef WITH_NTFS_3G
85         /* The stream is located in an NTFS volume.  It is identified by volume,
86          * filename, data stream name, and by whether it is a reparse point or
87          * not.  @ntfs_loc points to a structure containing this information.
88          * */
89         RESOURCE_IN_NTFS_VOLUME,
90 #endif
91
92 #ifdef __WIN32__
93         /* Windows only: the stream is located in the external file named by
94          * @file_on_disk, but the file is encrypted and must be read using the
95          * appropriate Windows API.  */
96         RESOURCE_WIN32_ENCRYPTED,
97 #endif
98
99 };
100
101 /*
102  * An entry in the lookup table in the WIM file.
103  *
104  * It is used to find data streams for files in the WIM.
105  *
106  * Metadata resources and reparse point data buffers will also have lookup table
107  * entries associated with the data.
108  *
109  * The lookup_table_entry for a given dentry or alternate stream entry in the
110  * WIM is found using the SHA1 message digest field.
111  */
112 struct wim_lookup_table_entry {
113
114         /* List of lookup table entries in this hash bucket */
115         struct hlist_node hash_list;
116
117         /* Uncompressed size of the stream.  */
118         u64 size;
119
120         /* Stream flags (WIM_RESHDR_FLAG_*).  */
121         u16 flags : 8;
122
123         /* One of the `enum resource_location' values documented above. */
124         u16 resource_location : 5;
125
126         /* 1 if this stream is a unique size (only set while writing streams). */
127         u16 unique_size : 1;
128
129         /* 1 if this stream has not had a SHA1 message digest calculated for it
130          * yet */
131         u16 unhashed : 1;
132
133         u16 deferred : 1;
134
135         u16 no_progress : 1;
136
137         /* Set to 1 when a metadata entry has its checksum changed; in such
138          * cases the hash is no longer valid to verify the data if the metadata
139          * resource is read again.  */
140         u16 dont_check_metadata_hash : 1;
141
142         /* Only used during WIM write.  Normal value is 0 (resource not
143          * filtered).  */
144         u16 filtered : 2;
145 #define FILTERED_SAME_WIM       0x1  /* Resource already in same WIM  */
146 #define FILTERED_EXTERNAL_WIM   0x2  /* Resource already in external WIM  */
147
148         /* (On-disk field)
149          * Number of times this lookup table entry is referenced by dentries.
150          * Unfortunately, this field is not always set correctly in Microsoft's
151          * WIMs, so we have no choice but to fix it if more references to the
152          * lookup table entry are found than stated here.  */
153         u32 refcnt;
154
155         union {
156                 /* (On-disk field) SHA1 message digest of the stream referenced
157                  * by this lookup table entry.  */
158                 u8  hash[SHA1_HASH_SIZE];
159
160                 /* First 4 or 8 bytes of the SHA1 message digest, used for
161                  * inserting the entry into the hash table.  Since the SHA1
162                  * message digest can be considered random, we don't really need
163                  * the full 20 byte hash just to insert the entry in a hash
164                  * table.  */
165                 size_t hash_short;
166
167                 /* Unhashed entries only (unhashed == 1): these variables make
168                  * it possible to find the pointer to this 'struct
169                  * wim_lookup_table_entry' contained in either 'struct
170                  * wim_ads_entry' or 'struct wim_inode'.  There can be at most 1
171                  * such pointer, as we can only join duplicate streams after
172                  * they have been hashed.  */
173                 struct {
174                         struct wim_inode *back_inode;
175                         u32 back_stream_id;
176                 };
177         };
178
179         /* When a WIM file is written, out_refcnt starts at 0 and is incremented
180          * whenever the stream pointed to by this lookup table entry needs to be
181          * written.  The stream only need to be written when out_refcnt is
182          * nonzero, since otherwise it is not referenced by any dentries. */
183         u32 out_refcnt;
184
185         /* Pointers to somewhere where the stream is actually located.  See the
186          * comments for the @resource_location field above. */
187         union {
188                 struct {
189                         struct wim_resource_spec *rspec;
190                         u64 offset_in_res;
191                 };
192                 tchar *file_on_disk;
193                 void *attached_buffer;
194         #ifdef WITH_FUSE
195                 tchar *staging_file_name;
196         #endif
197         #ifdef WITH_NTFS_3G
198                 struct ntfs_location *ntfs_loc;
199         #endif
200         };
201
202         /* Actual reference count to this stream (only used while
203          * verifying an image). */
204         u32 real_refcnt;
205
206         union {
207         #ifdef WITH_FUSE
208                 /* Number of times this stream has been opened (used only during
209                  * mounting) */
210                 u16 num_opened_fds;
211         #endif
212
213                 /* This field is used for the special hardlink or symlink image
214                  * extraction mode.   In these mode, all identical files are linked
215                  * together, and @extracted_file will be set to the filename of the
216                  * first extracted file containing this stream.  */
217                 tchar *extracted_file;
218         };
219
220         /* Temporary fields  */
221         union {
222                 /* Used temporarily during WIM file writing  */
223                 struct {
224                         struct hlist_node hash_list_2;
225
226                         /* Links streams being written to the WIM.  */
227                         struct list_head write_streams_list;
228                 };
229
230                 /* Used temporarily during WIM file writing (after above)  */
231                 struct {
232                         struct list_head msg_list;
233                         struct list_head being_compressed_list;
234                 };
235
236                 /* When a WIM file is written, @output_reshdr is filled in with
237                  * the resource header for the output WIM.  */
238                 struct wim_reshdr out_reshdr;
239
240                 /* Used temporarily during extraction  */
241                 union {
242                         /* out_refcnt tracks number of slots filled */
243                         struct wim_dentry *inline_lte_dentries[4];
244                         struct {
245                                 struct wim_dentry **lte_dentries;
246                                 unsigned long alloc_lte_dentries;
247                         };
248                 };
249         };
250
251         /* Temporary list fields */
252         union {
253                 /* Links streams when writing lookup table.  */
254                 struct list_head lookup_table_list;
255
256                 /* Links streams being extracted.  */
257                 struct list_head extraction_list;
258
259                 /* Links streams being exported.  */
260                 struct list_head export_stream_list;
261         };
262
263         /* Links streams that are still unhashed after being been added
264          * to a WIM.  */
265         struct list_head unhashed_list;
266
267         struct list_head wim_resource_list;
268 };
269
270 static inline bool
271 lte_is_partial(const struct wim_lookup_table_entry * lte)
272 {
273         return lte->resource_location == RESOURCE_IN_WIM &&
274                lte->size != lte->rspec->uncompressed_size;
275 }
276
277 static inline bool
278 lte_filename_valid(const struct wim_lookup_table_entry *lte)
279 {
280         return     lte->resource_location == RESOURCE_IN_FILE_ON_DISK
281         #ifdef __WIN32__
282                 || lte->resource_location == RESOURCE_WIN32_ENCRYPTED
283         #endif
284         #ifdef WITH_FUSE
285                 || lte->resource_location == RESOURCE_IN_STAGING_FILE
286         #endif
287                 ;
288 }
289
290 extern struct wim_lookup_table *
291 new_lookup_table(size_t capacity) _malloc_attribute;
292
293 extern int
294 read_wim_lookup_table(WIMStruct *wim);
295
296 extern int
297 write_wim_lookup_table(WIMStruct *wim, int image, int write_flags,
298                        struct wim_reshdr *out_reshdr,
299                        struct list_head *stream_list_override);
300
301 extern void
302 free_lookup_table(struct wim_lookup_table *table);
303
304 extern void
305 lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte);
306
307 /* Unlinks a lookup table entry from the table; does not free it. */
308 static inline void
309 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
310 {
311         wimlib_assert(!lte->unhashed);
312         hlist_del(&lte->hash_list);
313         wimlib_assert(table->num_entries != 0);
314         table->num_entries--;
315 }
316
317 extern struct wim_lookup_table_entry *
318 new_lookup_table_entry(void) _malloc_attribute;
319
320 extern struct wim_lookup_table_entry *
321 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte)
322                         _malloc_attribute;
323
324 extern void
325 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out);
326
327 extern void
328 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
329
330 extern void
331 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
332                              struct wimlib_resource_entry *wentry);
333
334 extern int
335 for_lookup_table_entry(struct wim_lookup_table *table,
336                        int (*visitor)(struct wim_lookup_table_entry *, void *),
337                        void *arg);
338
339 extern int
340 sort_stream_list_by_sequential_order(struct list_head *stream_list,
341                                      size_t list_head_offset);
342
343 extern int
344 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
345                                   int (*visitor)(struct wim_lookup_table_entry *,
346                                                  void *),
347                                   void *arg);
348
349 extern struct wim_lookup_table_entry *
350 lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
351
352 extern int
353 wim_pathname_to_stream(WIMStruct *wim, const tchar *path,
354                        int lookup_flags,
355                        struct wim_dentry **dentry_ret,
356                        struct wim_lookup_table_entry **lte_ret,
357                        u16 *stream_idx_ret);
358
359 extern void
360 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
361                      struct wim_lookup_table *table);
362 #ifdef WITH_FUSE
363 extern void
364 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
365 #endif
366
367 extern int
368 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *ignore);
369
370 extern int
371 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *ignore);
372
373 extern int
374 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
375
376 static inline void
377 lte_bind_wim_resource_spec(struct wim_lookup_table_entry *lte,
378                            struct wim_resource_spec *rspec)
379 {
380         lte->resource_location = RESOURCE_IN_WIM;
381         lte->rspec = rspec;
382         list_add_tail(&lte->wim_resource_list, &rspec->stream_list);
383 }
384
385 static inline void
386 lte_unbind_wim_resource_spec(struct wim_lookup_table_entry *lte)
387 {
388         list_del(&lte->wim_resource_list);
389         lte->rspec = NULL;
390         lte->resource_location = RESOURCE_NONEXISTENT;
391 }
392
393 extern int
394 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table,
395                    bool force);
396
397 extern int
398 resource_not_found_error(const struct wim_inode *inode, const u8 *hash);
399
400 extern void
401 inode_unresolve_ltes(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)
421                 return NULL;
422         if (stream_idx == 0)
423                 return lookup_resource(table, inode->i_hash);
424         else
425                 return lookup_resource(table,
426                                          inode->i_ads_entries[
427                                                 stream_idx - 1].hash);
428 }
429
430 extern struct wim_lookup_table_entry *
431 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
432                  const struct wim_lookup_table *table);
433
434 static inline const u8 *
435 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
436 {
437         wimlib_assert(!inode->i_resolved);
438         wimlib_assert(stream_idx <= inode->i_num_ads);
439         if (stream_idx == 0)
440                 return inode->i_hash;
441         else
442                 return inode->i_ads_entries[stream_idx - 1].hash;
443 }
444
445
446 static inline const u8 *
447 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
448 {
449         struct wim_lookup_table_entry *lte;
450         lte = inode_stream_lte_resolved(inode, stream_idx);
451         if (lte)
452                 return lte->hash;
453         else
454                 return zero_hash;
455 }
456
457 /*
458  * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
459  * means the default un-named file stream, and stream_idx >= 1 corresponds to an
460  * alternate data stream.
461  *
462  * This works for both resolved and un-resolved dentries.
463  */
464 static inline const u8 *
465 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
466 {
467         if (inode->i_resolved)
468                 return inode_stream_hash_resolved(inode, stream_idx);
469         else
470                 return inode_stream_hash_unresolved(inode, stream_idx);
471 }
472
473 static inline u16
474 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
475 {
476         wimlib_assert(stream_idx <= inode->i_num_ads);
477         if (stream_idx == 0)
478                 return 0;
479         else
480                 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
481 }
482
483 extern struct wim_lookup_table_entry *
484 inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret);
485
486 extern struct wim_lookup_table_entry *
487 inode_unnamed_lte_resolved(const struct wim_inode *inode);
488
489 extern struct wim_lookup_table_entry *
490 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
491                              const struct wim_lookup_table *table);
492
493 extern struct wim_lookup_table_entry *
494 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
495
496 extern const u8 *
497 inode_unnamed_stream_hash(const struct wim_inode *inode);
498
499 static inline void
500 lookup_table_insert_unhashed(struct wim_lookup_table *table,
501                              struct wim_lookup_table_entry *lte,
502                              struct wim_inode *back_inode,
503                              u32 back_stream_id)
504 {
505         lte->unhashed = 1;
506         lte->back_inode = back_inode;
507         lte->back_stream_id = back_stream_id;
508         list_add_tail(&lte->unhashed_list, table->unhashed_streams);
509 }
510
511 extern int
512 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
513                      struct wim_lookup_table *lookup_table,
514                      struct wim_lookup_table_entry **lte_ret);
515
516 extern struct wim_lookup_table_entry **
517 retrieve_lte_pointer(struct wim_lookup_table_entry *lte);
518
519 #endif /* _WIMLIB_LOOKUP_TABLE_H */