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. */
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,
71 /* The stream resource is directly attached in an in-memory buffer
72 * pointed to by @attached_buffer. */
73 RESOURCE_IN_ATTACHED_BUFFER,
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,
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
86 RESOURCE_IN_NTFS_VOLUME,
90 /* Resource must be accessed using Win32 API (may be a named data
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.
97 RESOURCE_WIN32_ENCRYPTED,
103 * An entry in the lookup table in the WIM file.
105 * It is used to find data streams for files in the WIM.
107 * Metadata resources and reparse point data buffers will also have lookup table
108 * entries associated with the data.
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.
113 struct wim_lookup_table_entry {
115 /* List of lookup table entries in this hash bucket */
116 struct hlist_node hash_list;
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
121 struct resource_entry resource_entry;
123 /* Specifies which part of the split WIM the resource is located in.
124 * This is on on-disk field.
126 * In stand-alone WIMs, this must be 1.
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.
137 /* One of the `enum resource_location' values documented above. */
138 u16 resource_location : 5;
140 /* 1 if this stream is a unique size (only set while writing streams). */
143 /* 1 if this stream has not had a SHA1 message digest calculated for it
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;
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. */
165 /* (On-disk field) SHA1 message digest of the stream referenced
166 * by this lookup table entry */
167 u8 hash[SHA1_HASH_SIZE];
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
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. */
183 struct wim_inode *back_inode;
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. */
194 /* Pointers to somewhere where the stream is actually located. See the
195 * comments for the @resource_location field above. */
199 void *attached_buffer;
201 tchar *staging_file_name;
204 struct ntfs_location *ntfs_loc;
208 /* Actual reference count to this stream (only used while
209 * verifying an image). */
214 /* Number of times this stream has been opened (used only during
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;
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
234 struct resource_entry output_resource_entry;
237 struct list_head msg_list;
238 struct list_head being_compressed_list;
240 struct list_head lte_dentry_list;
243 struct hlist_node hash_list_2;
245 struct list_head write_streams_list;
249 /* Temporary list fields */
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;
260 wim_resource_size(const struct wim_lookup_table_entry *lte)
262 return lte->resource_entry.original_size;
266 wim_resource_chunks(const struct wim_lookup_table_entry *lte)
268 return DIV_ROUND_UP(wim_resource_size(lte), WIM_CHUNK_SIZE);
272 wim_resource_compressed_size(const struct wim_lookup_table_entry *lte)
274 return lte->resource_entry.size;
278 wim_resource_compression_type(const struct wim_lookup_table_entry *lte)
280 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_NONE != 0);
281 return lte->compression_type;
285 lte_filename_valid(const struct wim_lookup_table_entry *lte)
289 || lte->resource_location == RESOURCE_WIN32
290 || lte->resource_location == RESOURCE_WIN32_ENCRYPTED
292 || lte->resource_location == RESOURCE_IN_FILE_ON_DISK
295 || lte->resource_location == RESOURCE_IN_STAGING_FILE
300 extern struct wim_lookup_table *
301 new_lookup_table(size_t capacity) _malloc_attribute;
304 read_lookup_table(WIMStruct *w);
307 write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry);
310 write_lookup_table_from_stream_list(struct list_head *stream_list,
312 struct resource_entry *out_res_entry);
315 free_lookup_table(struct wim_lookup_table *table);
318 lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte);
320 /* Unlinks a lookup table entry from the table; does not free it. */
322 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
324 wimlib_assert(!lte->unhashed);
325 hlist_del(<e->hash_list);
326 wimlib_assert(table->num_entries != 0);
327 table->num_entries--;
330 extern struct wim_lookup_table_entry *
331 new_lookup_table_entry(void) _malloc_attribute;
333 extern struct wim_lookup_table_entry *
334 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte)
338 print_lookup_table_entry(const struct wim_lookup_table_entry *entry,
342 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
345 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
346 struct wimlib_resource_entry *wentry);
349 for_lookup_table_entry(struct wim_lookup_table *table,
350 int (*visitor)(struct wim_lookup_table_entry *, void *),
354 cmp_streams_by_wim_position(const void *p1, const void *p2);
357 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
358 int (*visitor)(struct wim_lookup_table_entry *,
362 extern struct wim_lookup_table_entry *
363 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
366 lookup_resource(WIMStruct *w, const tchar *path,
367 int lookup_flags, struct wim_dentry **dentry_ret,
368 struct wim_lookup_table_entry **lte_ret, u16 *stream_idx_ret);
371 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
372 struct wim_lookup_table *table);
375 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
379 lte_zero_out_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
382 lte_zero_real_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
385 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
388 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table);
391 inode_unresolve_ltes(struct wim_inode *inode);
393 static inline struct wim_lookup_table_entry *
394 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
396 wimlib_assert(inode->i_resolved);
397 wimlib_assert(stream_idx <= inode->i_num_ads);
401 return inode->i_ads_entries[stream_idx - 1].lte;
404 static inline struct wim_lookup_table_entry *
405 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
406 const struct wim_lookup_table *table)
408 wimlib_assert(!inode->i_resolved);
409 wimlib_assert(stream_idx <= inode->i_num_ads);
413 return __lookup_resource(table, inode->i_hash);
415 return __lookup_resource(table,
416 inode->i_ads_entries[
417 stream_idx - 1].hash);
420 extern struct wim_lookup_table_entry *
421 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
422 const struct wim_lookup_table *table);
424 static inline const u8 *
425 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
427 wimlib_assert(!inode->i_resolved);
428 wimlib_assert(stream_idx <= inode->i_num_ads);
430 return inode->i_hash;
432 return inode->i_ads_entries[stream_idx - 1].hash;
436 static inline const u8 *
437 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
439 struct wim_lookup_table_entry *lte;
440 lte = inode_stream_lte_resolved(inode, stream_idx);
448 * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
449 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
450 * alternate data stream.
452 * This works for both resolved and un-resolved dentries.
454 static inline const u8 *
455 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
457 if (inode->i_resolved)
458 return inode_stream_hash_resolved(inode, stream_idx);
460 return inode_stream_hash_unresolved(inode, stream_idx);
464 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
466 wimlib_assert(stream_idx <= inode->i_num_ads);
470 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
473 extern struct wim_lookup_table_entry *
474 inode_unnamed_lte_resolved(const struct wim_inode *inode);
476 extern struct wim_lookup_table_entry *
477 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
478 const struct wim_lookup_table *table);
480 extern struct wim_lookup_table_entry *
481 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
484 lookup_table_total_stream_size(struct wim_lookup_table *table);
488 lookup_table_insert_unhashed(struct wim_lookup_table *table,
489 struct wim_lookup_table_entry *lte,
490 struct wim_inode *back_inode,
494 lte->back_inode = back_inode;
495 lte->back_stream_id = back_stream_id;
496 list_add_tail(<e->unhashed_list, table->unhashed_streams);
500 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
501 struct wim_lookup_table *lookup_table,
502 struct wim_lookup_table_entry **lte_ret);
504 extern struct wim_lookup_table_entry **
505 retrieve_lte_pointer(struct wim_lookup_table_entry *lte);
507 #endif /* _WIMLIB_LOOKUP_TABLE_H */