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