1 #ifndef _WIMLIB_LOOKUP_TABLE_H
2 #define _WIMLIB_LOOKUP_TABLE_H
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"
11 #define LOOKUP_FLAG_ADS_OK 0x00000001
12 #define LOOKUP_FLAG_DIRECTORY_OK 0x00000002
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.
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'.
25 * Note: Everything will break horribly if there is a SHA1 collision.
27 struct wim_lookup_table {
28 struct hlist_head *array;
31 struct list_head *unhashed_streams;
38 struct ntfs_location {
40 utf16lechar *stream_name;
41 u16 stream_name_nchars;
42 struct _ntfs_volume *ntfs_vol;
43 bool is_reparse_point;
47 /* An enumerated type that identifies where the stream corresponding to this
48 * lookup table entry is actually located.
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
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,
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. */
67 /* The stream resource is located in an external file. The name of the
68 * file will be provided by @file_on_disk member.
70 * Note: On Windows @file_on_disk may actually specify a named data
72 RESOURCE_IN_FILE_ON_DISK,
74 /* The stream resource is directly attached in an in-memory buffer
75 * pointed to by @attached_buffer. */
76 RESOURCE_IN_ATTACHED_BUFFER,
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,
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
89 RESOURCE_IN_NTFS_VOLUME,
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.
96 RESOURCE_WIN32_ENCRYPTED,
102 * An entry in the lookup table in the WIM file.
104 * It is used to find data streams for files in the WIM.
106 * Metadata resources and reparse point data buffers will also have lookup table
107 * entries associated with the data.
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.
112 struct wim_lookup_table_entry {
114 /* List of lookup table entries in this hash bucket */
115 struct hlist_node hash_list;
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
120 struct resource_entry resource_entry;
122 /* Specifies which part of the split WIM the resource is located in.
123 * This is on on-disk field.
125 * In stand-alone WIMs, this must be 1.
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.
136 /* One of the `enum resource_location' values documented above. */
137 u16 resource_location : 5;
139 /* 1 if this stream is a unique size (only set while writing streams). */
142 /* 1 if this stream has not had a SHA1 message digest calculated for it
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;
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(). */
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;
167 * Number of times this lookup table entry is referenced by dentries.
168 * Unfortunately, this field is not always set correctly in Microsoft's
169 * WIMs, so we have no choice but to fix it if more references to the
170 * lookup table entry are found than stated here. */
174 /* (On-disk field) SHA1 message digest of the stream referenced
175 * by this lookup table entry. */
176 u8 hash[SHA1_HASH_SIZE];
178 /* First 4 or 8 bytes of the SHA1 message digest, used for
179 * inserting the entry into the hash table. Since the SHA1
180 * message digest can be considered random, we don't really need
181 * the full 20 byte hash just to insert the entry in a hash
185 /* Unhashed entries only (unhashed == 1): these variables make
186 * it possible to find the pointer to this 'struct
187 * wim_lookup_table_entry' contained in either 'struct
188 * wim_ads_entry' or 'struct wim_inode'. There can be at most 1
189 * such pointer, as we can only join duplicate streams after
190 * they have been hashed. */
192 struct wim_inode *back_inode;
197 /* When a WIM file is written, out_refcnt starts at 0 and is incremented
198 * whenever the stream pointed to by this lookup table entry needs to be
199 * written. The stream only need to be written when out_refcnt is
200 * nonzero, since otherwise it is not referenced by any dentries. */
203 /* Pointers to somewhere where the stream is actually located. See the
204 * comments for the @resource_location field above. */
208 void *attached_buffer;
210 tchar *staging_file_name;
213 struct ntfs_location *ntfs_loc;
217 /* Actual reference count to this stream (only used while
218 * verifying an image). */
223 /* Number of times this stream has been opened (used only during
228 /* This field is used for the special hardlink or symlink image
229 * extraction mode. In these mode, all identical files are linked
230 * together, and @extracted_file will be set to the filename of the
231 * first extracted file containing this stream. */
232 tchar *extracted_file;
235 /* Temporary fields */
237 /* Used temporarily during WIM file writing */
239 struct hlist_node hash_list_2;
241 /* Links streams being written to the WIM. */
242 struct list_head write_streams_list;
245 /* Used temporarily during WIM file writing (after above) */
247 struct list_head msg_list;
248 struct list_head being_compressed_list;
251 /* When a WIM file is written, @output_resource_entry is filled
252 * in with the resource entry for the output WIM. This will not
253 * necessarily be the same as the @resource_entry since:
254 * - The stream may have a different offset in the new WIM
255 * - The stream may have a different compressed size in the new
256 * WIM if the compression type changed
258 struct resource_entry output_resource_entry;
261 /* Used temporarily during extraction */
263 /* out_refcnt tracks number of slots filled */
264 struct wim_dentry *inline_lte_dentries[4];
266 struct wim_dentry **lte_dentries;
267 unsigned long alloc_lte_dentries;
272 /* Temporary list fields */
274 /* Links streams when writing lookup table. */
275 struct list_head lookup_table_list;
277 /* Links streams being extracted. */
278 struct list_head extraction_list;
280 /* Links streams being exported. */
281 struct list_head export_stream_list;
284 /* Links streams that are still unhashed after being been added
286 struct list_head unhashed_list;
290 wim_resource_size(const struct wim_lookup_table_entry *lte)
292 return lte->resource_entry.original_size;
296 wim_resource_chunks(const struct wim_lookup_table_entry *lte)
298 return DIV_ROUND_UP(wim_resource_size(lte), WIM_CHUNK_SIZE);
302 wim_resource_compressed_size(const struct wim_lookup_table_entry *lte)
304 return lte->resource_entry.size;
308 wim_resource_compression_type(const struct wim_lookup_table_entry *lte)
310 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_NONE != 0);
311 return lte->compression_type;
315 lte_filename_valid(const struct wim_lookup_table_entry *lte)
317 return lte->resource_location == RESOURCE_IN_FILE_ON_DISK
319 || lte->resource_location == RESOURCE_WIN32_ENCRYPTED
322 || lte->resource_location == RESOURCE_IN_STAGING_FILE
327 extern struct wim_lookup_table *
328 new_lookup_table(size_t capacity) _malloc_attribute;
331 read_wim_lookup_table(WIMStruct *wim);
334 write_wim_lookup_table(WIMStruct *wim, int image, int write_flags,
335 struct resource_entry *out_res_entry,
336 struct list_head *stream_list_override);
339 free_lookup_table(struct wim_lookup_table *table);
342 lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte);
344 /* Unlinks a lookup table entry from the table; does not free it. */
346 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
348 wimlib_assert(!lte->unhashed);
349 hlist_del(<e->hash_list);
350 wimlib_assert(table->num_entries != 0);
351 table->num_entries--;
354 extern struct wim_lookup_table_entry *
355 new_lookup_table_entry(void) _malloc_attribute;
357 extern struct wim_lookup_table_entry *
358 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte)
362 print_lookup_table_entry(const struct wim_lookup_table_entry *entry,
366 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
369 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
370 struct wimlib_resource_entry *wentry);
373 for_lookup_table_entry(struct wim_lookup_table *table,
374 int (*visitor)(struct wim_lookup_table_entry *, void *),
378 sort_stream_list_by_sequential_order(struct list_head *stream_list,
379 size_t list_head_offset);
382 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
383 int (*visitor)(struct wim_lookup_table_entry *,
387 extern struct wim_lookup_table_entry *
388 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
391 lookup_resource(WIMStruct *wim, const tchar *path,
392 int lookup_flags, struct wim_dentry **dentry_ret,
393 struct wim_lookup_table_entry **lte_ret, u16 *stream_idx_ret);
396 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
397 struct wim_lookup_table *table);
400 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
404 lte_zero_out_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
407 lte_zero_real_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
410 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
413 lte_init_wim(struct wim_lookup_table_entry *lte, WIMStruct *wim);
416 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table,
420 inode_unresolve_ltes(struct wim_inode *inode);
422 static inline struct wim_lookup_table_entry *
423 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
425 wimlib_assert(inode->i_resolved);
426 wimlib_assert(stream_idx <= inode->i_num_ads);
430 return inode->i_ads_entries[stream_idx - 1].lte;
433 static inline struct wim_lookup_table_entry *
434 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
435 const struct wim_lookup_table *table)
437 wimlib_assert(!inode->i_resolved);
438 wimlib_assert(stream_idx <= inode->i_num_ads);
442 return __lookup_resource(table, inode->i_hash);
444 return __lookup_resource(table,
445 inode->i_ads_entries[
446 stream_idx - 1].hash);
449 extern struct wim_lookup_table_entry *
450 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
451 const struct wim_lookup_table *table);
453 static inline const u8 *
454 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
456 wimlib_assert(!inode->i_resolved);
457 wimlib_assert(stream_idx <= inode->i_num_ads);
459 return inode->i_hash;
461 return inode->i_ads_entries[stream_idx - 1].hash;
465 static inline const u8 *
466 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
468 struct wim_lookup_table_entry *lte;
469 lte = inode_stream_lte_resolved(inode, stream_idx);
477 * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
478 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
479 * alternate data stream.
481 * This works for both resolved and un-resolved dentries.
483 static inline const u8 *
484 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
486 if (inode->i_resolved)
487 return inode_stream_hash_resolved(inode, stream_idx);
489 return inode_stream_hash_unresolved(inode, stream_idx);
493 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
495 wimlib_assert(stream_idx <= inode->i_num_ads);
499 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
502 extern struct wim_lookup_table_entry *
503 inode_unnamed_lte_resolved(const struct wim_inode *inode);
505 extern struct wim_lookup_table_entry *
506 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
507 const struct wim_lookup_table *table);
509 extern struct wim_lookup_table_entry *
510 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
513 lookup_table_total_stream_size(struct wim_lookup_table *table);
517 lookup_table_insert_unhashed(struct wim_lookup_table *table,
518 struct wim_lookup_table_entry *lte,
519 struct wim_inode *back_inode,
523 lte->back_inode = back_inode;
524 lte->back_stream_id = back_stream_id;
525 list_add_tail(<e->unhashed_list, table->unhashed_streams);
529 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
530 struct wim_lookup_table *lookup_table,
531 struct wim_lookup_table_entry **lte_ret);
533 extern struct wim_lookup_table_entry **
534 retrieve_lte_pointer(struct wim_lookup_table_entry *lte);
536 #endif /* _WIMLIB_LOOKUP_TABLE_H */