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 /* Uncompressed size of the stream. */
120 /* Stream flags (WIM_RESHDR_FLAG_*). */
123 /* One of the `enum resource_location' values documented above. */
124 u16 resource_location : 5;
126 /* 1 if this stream is a unique size (only set while writing streams). */
129 /* 1 if this stream has not had a SHA1 message digest calculated for it
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;
142 /* Only used during WIM write. Normal value is 0 (resource not
145 #define FILTERED_SAME_WIM 0x1 /* Resource already in same WIM */
146 #define FILTERED_EXTERNAL_WIM 0x2 /* Resource already in external WIM */
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. */
156 /* (On-disk field) SHA1 message digest of the stream referenced
157 * by this lookup table entry. */
158 u8 hash[SHA1_HASH_SIZE];
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
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. */
174 struct wim_inode *back_inode;
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. */
185 /* Pointers to somewhere where the stream is actually located. See the
186 * comments for the @resource_location field above. */
188 struct wim_resource_spec *rspec;
190 void *attached_buffer;
192 tchar *staging_file_name;
195 struct ntfs_location *ntfs_loc;
199 /* Actual reference count to this stream (only used while
200 * verifying an image). */
205 /* Number of times this stream has been opened (used only during
210 /* This field is used for the special hardlink or symlink image
211 * extraction mode. In these mode, all identical files are linked
212 * together, and @extracted_file will be set to the filename of the
213 * first extracted file containing this stream. */
214 tchar *extracted_file;
217 /* Temporary fields */
219 /* Used temporarily during WIM file writing */
221 struct hlist_node hash_list_2;
223 /* Links streams being written to the WIM. */
224 struct list_head write_streams_list;
227 /* Used temporarily during WIM file writing (after above) */
229 struct list_head msg_list;
230 struct list_head being_compressed_list;
233 /* When a WIM file is written, @output_reshdr is filled in with
234 * the resource header for the output WIM. */
235 struct wim_reshdr out_reshdr;
237 /* Used temporarily during extraction */
239 /* out_refcnt tracks number of slots filled */
240 struct wim_dentry *inline_lte_dentries[4];
242 struct wim_dentry **lte_dentries;
243 unsigned long alloc_lte_dentries;
248 /* Temporary list fields */
250 /* Links streams when writing lookup table. */
251 struct list_head lookup_table_list;
253 /* Links streams being extracted. */
254 struct list_head extraction_list;
256 /* Links streams being exported. */
257 struct list_head export_stream_list;
260 /* Links streams that are still unhashed after being been added
262 struct list_head unhashed_list;
264 struct list_head wim_resource_list;
268 lte_ctype(const struct wim_lookup_table_entry *lte)
270 if (lte->resource_location == RESOURCE_IN_WIM)
271 return lte->rspec->ctype;
273 return WIMLIB_COMPRESSION_TYPE_NONE;
277 lte_cchunk_size(const struct wim_lookup_table_entry * lte)
279 if (lte->resource_location == RESOURCE_IN_WIM &&
280 lte->rspec->ctype != WIMLIB_COMPRESSION_TYPE_NONE)
281 return lte->rspec->cchunk_size;
287 lte_filename_valid(const struct wim_lookup_table_entry *lte)
289 return lte->resource_location == RESOURCE_IN_FILE_ON_DISK
291 || lte->resource_location == RESOURCE_WIN32_ENCRYPTED
294 || lte->resource_location == RESOURCE_IN_STAGING_FILE
299 extern struct wim_lookup_table *
300 new_lookup_table(size_t capacity) _malloc_attribute;
303 read_wim_lookup_table(WIMStruct *wim);
306 write_wim_lookup_table(WIMStruct *wim, int image, int write_flags,
307 struct wim_reshdr *out_reshdr,
308 struct list_head *stream_list_override);
311 free_lookup_table(struct wim_lookup_table *table);
314 lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte);
316 /* Unlinks a lookup table entry from the table; does not free it. */
318 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
320 wimlib_assert(!lte->unhashed);
321 hlist_del(<e->hash_list);
322 wimlib_assert(table->num_entries != 0);
323 table->num_entries--;
326 extern struct wim_lookup_table_entry *
327 new_lookup_table_entry(void) _malloc_attribute;
329 extern struct wim_lookup_table_entry *
330 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte)
334 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out);
337 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
340 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
341 struct wimlib_resource_entry *wentry);
344 for_lookup_table_entry(struct wim_lookup_table *table,
345 int (*visitor)(struct wim_lookup_table_entry *, void *),
349 sort_stream_list_by_sequential_order(struct list_head *stream_list,
350 size_t list_head_offset);
353 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
354 int (*visitor)(struct wim_lookup_table_entry *,
358 extern struct wim_lookup_table_entry *
359 lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
362 wim_pathname_to_stream(WIMStruct *wim, const tchar *path,
364 struct wim_dentry **dentry_ret,
365 struct wim_lookup_table_entry **lte_ret,
366 u16 *stream_idx_ret);
369 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
370 struct wim_lookup_table *table);
373 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
377 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *ignore);
380 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *ignore);
383 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
386 lte_bind_wim_resource_spec(struct wim_lookup_table_entry *lte,
387 struct wim_resource_spec *rspec)
389 lte->resource_location = RESOURCE_IN_WIM;
391 list_add(<e->wim_resource_list, &rspec->lte_list);
392 lte->flags = rspec->flags;
393 lte->size = rspec->uncompressed_size;
397 lte_unbind_wim_resource_spec(struct wim_lookup_table_entry *lte)
399 list_del(<e->wim_resource_list);
401 lte->resource_location = RESOURCE_NONEXISTENT;
405 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table,
409 resource_not_found_error(const struct wim_inode *inode, const u8 *hash);
412 inode_unresolve_ltes(struct wim_inode *inode);
414 static inline struct wim_lookup_table_entry *
415 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
417 wimlib_assert(inode->i_resolved);
418 wimlib_assert(stream_idx <= inode->i_num_ads);
422 return inode->i_ads_entries[stream_idx - 1].lte;
425 static inline struct wim_lookup_table_entry *
426 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
427 const struct wim_lookup_table *table)
429 wimlib_assert(!inode->i_resolved);
430 wimlib_assert(stream_idx <= inode->i_num_ads);
434 return lookup_resource(table, inode->i_hash);
436 return lookup_resource(table,
437 inode->i_ads_entries[
438 stream_idx - 1].hash);
441 extern struct wim_lookup_table_entry *
442 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
443 const struct wim_lookup_table *table);
445 static inline const u8 *
446 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
448 wimlib_assert(!inode->i_resolved);
449 wimlib_assert(stream_idx <= inode->i_num_ads);
451 return inode->i_hash;
453 return inode->i_ads_entries[stream_idx - 1].hash;
457 static inline const u8 *
458 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
460 struct wim_lookup_table_entry *lte;
461 lte = inode_stream_lte_resolved(inode, stream_idx);
469 * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
470 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
471 * alternate data stream.
473 * This works for both resolved and un-resolved dentries.
475 static inline const u8 *
476 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
478 if (inode->i_resolved)
479 return inode_stream_hash_resolved(inode, stream_idx);
481 return inode_stream_hash_unresolved(inode, stream_idx);
485 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
487 wimlib_assert(stream_idx <= inode->i_num_ads);
491 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
494 extern struct wim_lookup_table_entry *
495 inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret);
497 extern struct wim_lookup_table_entry *
498 inode_unnamed_lte_resolved(const struct wim_inode *inode);
500 extern struct wim_lookup_table_entry *
501 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
502 const struct wim_lookup_table *table);
504 extern struct wim_lookup_table_entry *
505 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
508 inode_unnamed_stream_hash(const struct wim_inode *inode);
511 lookup_table_insert_unhashed(struct wim_lookup_table *table,
512 struct wim_lookup_table_entry *lte,
513 struct wim_inode *back_inode,
517 lte->back_inode = back_inode;
518 lte->back_stream_id = back_stream_id;
519 list_add_tail(<e->unhashed_list, table->unhashed_streams);
523 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
524 struct wim_lookup_table *lookup_table,
525 struct wim_lookup_table_entry **lte_ret);
527 extern struct wim_lookup_table_entry **
528 retrieve_lte_pointer(struct wim_lookup_table_entry *lte);
530 #endif /* _WIMLIB_LOOKUP_TABLE_H */