]> wimlib.net Git - wimlib/blob - include/wimlib/lookup_table.h
d1b980dafe240f4ae18c33c05443ed9f09c98b9f
[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 deal with problems such as image capture and image mount, we
53  * allow the actual location of the stream to be somewhere else, such as an
54  * external file.
55  */
56 enum resource_location {
57         /* The lookup table entry does not correspond to a stream (this state
58          * should exist only temporarily) */
59         RESOURCE_NONEXISTENT = 0,
60
61         /* The stream resource is located in a WIM file.  The WIMStruct for the
62          * WIM file will be pointed to by the @wim member.  The compression type
63          * of the resource will be cached in @compression_type, and the pipable
64          * status of the resource will be cached in @pipable.  */
65         RESOURCE_IN_WIM,
66
67         /* The stream resource is located in an external file.  The name of the
68          * file will be provided by @file_on_disk member.
69          *
70          * Note: On Windows @file_on_disk may actually specify a named data
71          * stream.  */
72         RESOURCE_IN_FILE_ON_DISK,
73
74         /* The stream resource is directly attached in an in-memory buffer
75          * pointed to by @attached_buffer.  */
76         RESOURCE_IN_ATTACHED_BUFFER,
77
78 #ifdef WITH_FUSE
79         /* The stream resource is located in an external file in the staging
80          * directory for a read-write mount.  */
81         RESOURCE_IN_STAGING_FILE,
82 #endif
83
84 #ifdef WITH_NTFS_3G
85         /* The stream resource is located in an NTFS volume.  It is identified
86          * by volume, filename, data stream name, and by whether it is a reparse
87          * point or not. @ntfs_loc points to a structure containing this
88          * information.  */
89         RESOURCE_IN_NTFS_VOLUME,
90 #endif
91
92 #ifdef __WIN32__
93         /* Windows only: the file is on disk in the file named @file_on_disk,
94          * but the file is encrypted and must be read using special functions.
95          * */
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         /* Location and size of the stream in the WIM, whether it is compressed
118          * or not, and whether it's a metadata resource or not.  This is an
119          * on-disk field. */
120         struct resource_entry resource_entry;
121
122         /* Specifies which part of the split WIM the resource is located in.
123          * This is on on-disk field.
124          *
125          * In stand-alone WIMs, this must be 1.
126          *
127          * In split WIMs, every split WIM part has its own lookup table, and in
128          * read_lookup_table() it's currently expected that the part number of
129          * each lookup table entry in a split WIM part's lookup table is the
130          * same as the part number of that split WIM part.  So this makes this
131          * field redundant since we store a pointer to the corresponding
132          * WIMStruct in the lookup table entry anyway.
133          */
134         u16 part_number;
135
136         /* One of the `enum resource_location' values documented above. */
137         u16 resource_location : 5;
138
139         /* 1 if this stream is a unique size (only set while writing streams). */
140         u16 unique_size : 1;
141
142         /* 1 if this stream has not had a SHA1 message digest calculated for it
143          * yet */
144         u16 unhashed : 1;
145
146         u16 deferred : 1;
147
148         u16 no_progress : 1;
149
150         /* If resource_location == RESOURCE_IN_WIM, this will be a cached value
151          * that specifies the compression type of this stream as one of
152          * WIMLIB_COMPRESSION_TYPE_*.  Otherwise this will be 0, which is the
153          * same as WIMLIB_COMPRESSION_TYPE_NONE.  */
154         u16 compression_type : 2;
155
156         /* If resource_location == RESOURCE_IN_WIM, this flag will be set if the
157          * WIM is pipable and therefore the stream is in a slightly different
158          * format.  See comment above write_pipable_wim().  */
159         u16 is_pipable : 1;
160
161         /* Set to 1 when a metadata entry has its checksum changed; in such
162          * cases the hash is no longer valid to verify the data if the metadata
163          * resource is read again.  */
164         u16 dont_check_metadata_hash : 1;
165
166         /* Only used during WIM write.  Normal value is 0 (resource not
167          * filtered).  */
168         u16 filtered : 2;
169 #define FILTERED_SAME_WIM       0x1  /* Resource already in same WIM  */
170 #define FILTERED_EXTERNAL_WIM   0x2  /* Resource already in external WIM  */
171
172         /* (On-disk field)
173          * Number of times this lookup table entry is referenced by dentries.
174          * Unfortunately, this field is not always set correctly in Microsoft's
175          * WIMs, so we have no choice but to fix it if more references to the
176          * lookup table entry are found than stated here.  */
177         u32 refcnt;
178
179         union {
180                 /* (On-disk field) SHA1 message digest of the stream referenced
181                  * by this lookup table entry.  */
182                 u8  hash[SHA1_HASH_SIZE];
183
184                 /* First 4 or 8 bytes of the SHA1 message digest, used for
185                  * inserting the entry into the hash table.  Since the SHA1
186                  * message digest can be considered random, we don't really need
187                  * the full 20 byte hash just to insert the entry in a hash
188                  * table.  */
189                 size_t hash_short;
190
191                 /* Unhashed entries only (unhashed == 1): these variables make
192                  * it possible to find the pointer to this 'struct
193                  * wim_lookup_table_entry' contained in either 'struct
194                  * wim_ads_entry' or 'struct wim_inode'.  There can be at most 1
195                  * such pointer, as we can only join duplicate streams after
196                  * they have been hashed.  */
197                 struct {
198                         struct wim_inode *back_inode;
199                         u32 back_stream_id;
200                 };
201         };
202
203         /* When a WIM file is written, out_refcnt starts at 0 and is incremented
204          * whenever the stream pointed to by this lookup table entry needs to be
205          * written.  The stream only need to be written when out_refcnt is
206          * nonzero, since otherwise it is not referenced by any dentries. */
207         u32 out_refcnt;
208
209         /* Pointers to somewhere where the stream is actually located.  See the
210          * comments for the @resource_location field above. */
211         union {
212                 WIMStruct *wim;
213                 tchar *file_on_disk;
214                 void *attached_buffer;
215         #ifdef WITH_FUSE
216                 tchar *staging_file_name;
217         #endif
218         #ifdef WITH_NTFS_3G
219                 struct ntfs_location *ntfs_loc;
220         #endif
221         };
222
223         /* Actual reference count to this stream (only used while
224          * verifying an image). */
225         u32 real_refcnt;
226
227         union {
228         #ifdef WITH_FUSE
229                 /* Number of times this stream has been opened (used only during
230                  * mounting) */
231                 u16 num_opened_fds;
232         #endif
233
234                 /* This field is used for the special hardlink or symlink image
235                  * extraction mode.   In these mode, all identical files are linked
236                  * together, and @extracted_file will be set to the filename of the
237                  * first extracted file containing this stream.  */
238                 tchar *extracted_file;
239         };
240
241         /* Temporary fields  */
242         union {
243                 /* Used temporarily during WIM file writing  */
244                 struct {
245                         struct hlist_node hash_list_2;
246
247                         /* Links streams being written to the WIM.  */
248                         struct list_head write_streams_list;
249                 };
250
251                 /* Used temporarily during WIM file writing (after above)  */
252                 struct {
253                         struct list_head msg_list;
254                         struct list_head being_compressed_list;
255                 };
256
257                 /* When a WIM file is written, @output_resource_entry is filled
258                  * in with the resource entry for the output WIM.  This will not
259                  * necessarily be the same as the @resource_entry since:
260                  * - The stream may have a different offset in the new WIM
261                  * - The stream may have a different compressed size in the new
262                  *   WIM if the compression type changed
263                  */
264                 struct resource_entry output_resource_entry;
265
266
267                 /* Used temporarily during extraction  */
268                 union {
269                         /* out_refcnt tracks number of slots filled */
270                         struct wim_dentry *inline_lte_dentries[4];
271                         struct {
272                                 struct wim_dentry **lte_dentries;
273                                 unsigned long alloc_lte_dentries;
274                         };
275                 };
276         };
277
278         /* Temporary list fields */
279         union {
280                 /* Links streams when writing lookup table.  */
281                 struct list_head lookup_table_list;
282
283                 /* Links streams being extracted.  */
284                 struct list_head extraction_list;
285
286                 /* Links streams being exported.  */
287                 struct list_head export_stream_list;
288         };
289
290         /* Links streams that are still unhashed after being been added
291          * to a WIM.  */
292         struct list_head unhashed_list;
293 };
294
295 static inline u64
296 wim_resource_size(const struct wim_lookup_table_entry *lte)
297 {
298         return lte->resource_entry.original_size;
299 }
300
301 static inline u32
302 wim_resource_chunk_size(const struct wim_lookup_table_entry * lte)
303 {
304         if (lte->resource_location == RESOURCE_IN_WIM &&
305             lte->compression_type != WIMLIB_COMPRESSION_TYPE_NONE)
306                 return lte->wim->chunk_size;
307         else
308                 return 32768;
309 }
310
311
312 static inline u64
313 wim_resource_chunks(const struct wim_lookup_table_entry *lte)
314 {
315         return DIV_ROUND_UP(wim_resource_size(lte), wim_resource_chunk_size(lte));
316 }
317
318 static inline int
319 wim_resource_compression_type(const struct wim_lookup_table_entry *lte)
320 {
321         return lte->compression_type;
322 }
323
324 static inline bool
325 lte_filename_valid(const struct wim_lookup_table_entry *lte)
326 {
327         return     lte->resource_location == RESOURCE_IN_FILE_ON_DISK
328         #ifdef __WIN32__
329                 || lte->resource_location == RESOURCE_WIN32_ENCRYPTED
330         #endif
331         #ifdef WITH_FUSE
332                 || lte->resource_location == RESOURCE_IN_STAGING_FILE
333         #endif
334                 ;
335 }
336
337 extern struct wim_lookup_table *
338 new_lookup_table(size_t capacity) _malloc_attribute;
339
340 extern int
341 read_wim_lookup_table(WIMStruct *wim);
342
343 extern int
344 write_wim_lookup_table(WIMStruct *wim, int image, int write_flags,
345                        struct resource_entry *out_res_entry,
346                        struct list_head *stream_list_override);
347
348 extern void
349 free_lookup_table(struct wim_lookup_table *table);
350
351 extern void
352 lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte);
353
354 /* Unlinks a lookup table entry from the table; does not free it. */
355 static inline void
356 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
357 {
358         wimlib_assert(!lte->unhashed);
359         hlist_del(&lte->hash_list);
360         wimlib_assert(table->num_entries != 0);
361         table->num_entries--;
362 }
363
364 extern struct wim_lookup_table_entry *
365 new_lookup_table_entry(void) _malloc_attribute;
366
367 extern struct wim_lookup_table_entry *
368 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte)
369                         _malloc_attribute;
370
371 extern void
372 print_lookup_table_entry(const struct wim_lookup_table_entry *entry,
373                          FILE *out);
374
375 extern void
376 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
377
378 extern void
379 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
380                              struct wimlib_resource_entry *wentry);
381
382 extern int
383 for_lookup_table_entry(struct wim_lookup_table *table,
384                        int (*visitor)(struct wim_lookup_table_entry *, void *),
385                        void *arg);
386
387 extern int
388 sort_stream_list_by_sequential_order(struct list_head *stream_list,
389                                      size_t list_head_offset);
390
391 extern int
392 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
393                                   int (*visitor)(struct wim_lookup_table_entry *,
394                                                  void *),
395                                   void *arg);
396
397 extern struct wim_lookup_table_entry *
398 lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
399
400 extern int
401 wim_pathname_to_stream(WIMStruct *wim, const tchar *path,
402                        int lookup_flags,
403                        struct wim_dentry **dentry_ret,
404                        struct wim_lookup_table_entry **lte_ret,
405                        u16 *stream_idx_ret);
406
407 extern void
408 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
409                      struct wim_lookup_table *table);
410 #ifdef WITH_FUSE
411 extern void
412 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
413 #endif
414
415 extern int
416 lte_zero_out_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
417
418 extern int
419 lte_zero_real_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
420
421 extern int
422 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
423
424 extern void
425 lte_init_wim(struct wim_lookup_table_entry *lte, WIMStruct *wim);
426
427 extern int
428 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table,
429                    bool force);
430
431 extern int
432 resource_not_found_error(const struct wim_inode *inode, const u8 *hash);
433
434 extern void
435 inode_unresolve_ltes(struct wim_inode *inode);
436
437 static inline struct wim_lookup_table_entry *
438 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
439 {
440         wimlib_assert(inode->i_resolved);
441         wimlib_assert(stream_idx <= inode->i_num_ads);
442         if (stream_idx == 0)
443                 return inode->i_lte;
444         else
445                 return inode->i_ads_entries[stream_idx - 1].lte;
446 }
447
448 static inline struct wim_lookup_table_entry *
449 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
450                             const struct wim_lookup_table *table)
451 {
452         wimlib_assert(!inode->i_resolved);
453         wimlib_assert(stream_idx <= inode->i_num_ads);
454         if (!table)
455                 return NULL;
456         if (stream_idx == 0)
457                 return lookup_resource(table, inode->i_hash);
458         else
459                 return lookup_resource(table,
460                                          inode->i_ads_entries[
461                                                 stream_idx - 1].hash);
462 }
463
464 extern struct wim_lookup_table_entry *
465 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
466                  const struct wim_lookup_table *table);
467
468 static inline const u8 *
469 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
470 {
471         wimlib_assert(!inode->i_resolved);
472         wimlib_assert(stream_idx <= inode->i_num_ads);
473         if (stream_idx == 0)
474                 return inode->i_hash;
475         else
476                 return inode->i_ads_entries[stream_idx - 1].hash;
477 }
478
479
480 static inline const u8 *
481 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
482 {
483         struct wim_lookup_table_entry *lte;
484         lte = inode_stream_lte_resolved(inode, stream_idx);
485         if (lte)
486                 return lte->hash;
487         else
488                 return zero_hash;
489 }
490
491 /*
492  * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
493  * means the default un-named file stream, and stream_idx >= 1 corresponds to an
494  * alternate data stream.
495  *
496  * This works for both resolved and un-resolved dentries.
497  */
498 static inline const u8 *
499 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
500 {
501         if (inode->i_resolved)
502                 return inode_stream_hash_resolved(inode, stream_idx);
503         else
504                 return inode_stream_hash_unresolved(inode, stream_idx);
505 }
506
507 static inline u16
508 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
509 {
510         wimlib_assert(stream_idx <= inode->i_num_ads);
511         if (stream_idx == 0)
512                 return 0;
513         else
514                 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
515 }
516
517 extern struct wim_lookup_table_entry *
518 inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret);
519
520 extern struct wim_lookup_table_entry *
521 inode_unnamed_lte_resolved(const struct wim_inode *inode);
522
523 extern struct wim_lookup_table_entry *
524 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
525                              const struct wim_lookup_table *table);
526
527 extern struct wim_lookup_table_entry *
528 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
529
530 extern const u8 *
531 inode_unnamed_stream_hash(const struct wim_inode *inode);
532
533 extern u64
534 lookup_table_total_stream_size(struct wim_lookup_table *table);
535
536
537 static inline void
538 lookup_table_insert_unhashed(struct wim_lookup_table *table,
539                              struct wim_lookup_table_entry *lte,
540                              struct wim_inode *back_inode,
541                              u32 back_stream_id)
542 {
543         lte->unhashed = 1;
544         lte->back_inode = back_inode;
545         lte->back_stream_id = back_stream_id;
546         list_add_tail(&lte->unhashed_list, table->unhashed_streams);
547 }
548
549 extern int
550 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
551                      struct wim_lookup_table *lookup_table,
552                      struct wim_lookup_table_entry **lte_ret);
553
554 extern struct wim_lookup_table_entry **
555 retrieve_lte_pointer(struct wim_lookup_table_entry *lte);
556
557 #endif /* _WIMLIB_LOOKUP_TABLE_H */