1 #ifndef _WIMLIB_LOOKUP_TABLE_H
2 #define _WIMLIB_LOOKUP_TABLE_H
3 #include "wimlib_internal.h"
8 /* Size of each lookup table entry in the WIM file. */
9 #define WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE 50
11 #define LOOKUP_FLAG_ADS_OK 0x00000001
12 #define LOOKUP_FLAG_DIRECTORY_OK 0x00000002
19 /* The lookup table of a WIM file maps SHA1 message digests to streams of data.
20 * Here, the in-memory structure is implemented as a hash table.
22 * Given a SHA1 message digest, the mapped-to stream is specified by an offset
23 * in the WIM, an uncompressed and compressed size, and resource flags (see
24 * 'struct resource_entry'). But, we associate additional information, such as
25 * a reference count, with each stream, so the actual mapping is from SHA1
26 * message digests to 'struct wim_lookup_table_entry's, each of which contains
27 * an embedded 'struct resource_entry'.
29 * Note: Everything will break horribly if there is a SHA1 collision.
31 struct wim_lookup_table {
32 struct hlist_head *array;
35 struct list_head *unhashed_streams;
39 struct ntfs_location {
41 utf16lechar *stream_name;
42 u16 stream_name_nchars;
43 struct _ntfs_volume *ntfs_vol;
44 bool is_reparse_point;
48 /* An enumerated type that identifies where the stream corresponding to this
49 * lookup table entry is actually located.
51 * If we open a WIM and read its lookup table, the location is set to
52 * RESOURCE_IN_WIM since all the streams will initially be located in the WIM.
53 * However, to deal with problems such as image capture and image mount, we
54 * allow the actual location of the stream to be somewhere else, such as an
57 enum resource_location {
58 /* The lookup table entry does not correspond to a stream (this state
59 * should exist only temporarily) */
60 RESOURCE_NONEXISTENT = 0,
62 /* The stream resource is located in a WIM file. The WIMStruct for the
63 * 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. In addition, if
68 * @file_on_disk_fp is not NULL, it will be an open FILE * to the file.
71 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 /* Resource must be accessed using Win32 API (may be a named data
97 /* Windows only: the file is on disk in the file named @file_on_disk,
98 * but the file is encrypted and must be read using special functions.
100 RESOURCE_WIN32_ENCRYPTED,
106 * An entry in the lookup table in the WIM file.
108 * It is used to find data streams for files in the WIM.
110 * Metadata resources and reparse point data buffers will also have lookup table
111 * entries associated with the data.
113 * The lookup_table_entry for a given dentry or alternate stream entry in the
114 * WIM is found using the SHA1 message digest field.
116 struct wim_lookup_table_entry {
118 /* List of lookup table entries in this hash bucket */
119 struct hlist_node hash_list;
121 /* Location and size of the stream in the WIM, whether it is compressed
122 * or not, and whether it's a metadata resource or not. This is an
124 struct resource_entry resource_entry;
126 /* Specifies which part of the split WIM the resource is located in.
127 * This is on on-disk field.
129 * In stand-alone WIMs, this must be 1.
131 * In split WIMs, every split WIM part has its own lookup table, and in
132 * read_lookup_table() it's currently expected that the part number of
133 * each lookup table entry in a split WIM part's lookup table is the
134 * same as the part number of that split WIM part. So this makes this
135 * field redundant since we store a pointer to the corresponding
136 * WIMStruct in the lookup table entry anyway.
140 /* One of the `enum resource_location' values documented above. */
141 u16 resource_location : 5;
143 /* 1 if this stream is a unique size (only set while writing streams). */
146 /* 1 if this stream had a SHA1-message digest calculated for it yet? */
154 * Number of times this lookup table entry is referenced by dentries.
155 * Unfortunately, this field is not always set correctly in Microsoft's
156 * WIMs, so we have no choice but to fix it if more references to the
157 * lookup table entry are found than stated here. */
161 /* (On-disk field) SHA1 message digest of the stream referenced
162 * by this lookup table entry */
163 u8 hash[SHA1_HASH_SIZE];
165 /* First 4 or 8 bytes of the SHA1 message digest, used for
166 * inserting the entry into the hash table. Since the SHA1
167 * message digest can be considered random, we don't really need
168 * the full 20 byte hash just to insert the entry in a hash
172 /* Unhashed entries only (unhashed == 1): these variables make
173 * it possible to find the pointer to this 'struct
174 * wim_lookup_table_entry' contained in either 'struct
175 * wim_ads_entry' or 'struct wim_inode'. There can be at most 1
176 * such pointer, as we can only join duplicate streams after
177 * they have been hashed. */
179 struct wim_inode *back_inode;
184 /* When a WIM file is written, out_refcnt starts at 0 and is incremented
185 * whenever the stream pointed to by this lookup table entry needs to be
186 * written. The stream only need to be written when out_refcnt is
187 * nonzero, since otherwise it is not referenced by any dentries. */
190 /* Pointers to somewhere where the stream is actually located. See the
191 * comments for the @resource_location field above. */
195 void *attached_buffer;
197 tchar *staging_file_name;
200 struct ntfs_location *ntfs_loc;
204 /* Actual reference count to this stream (only used while
205 * verifying an image). */
210 /* Number of times this stream has been opened (used only during
215 /* This field is used for the special hardlink or symlink image
216 * extraction mode. In these mode, all identical files are linked
217 * together, and @extracted_file will be set to the filename of the
218 * first extracted file containing this stream. */
219 tchar *extracted_file;
223 /* When a WIM file is written, @output_resource_entry is filled
224 * in with the resource entry for the output WIM. This will not
225 * necessarily be the same as the @resource_entry since: - The
226 * stream may have a different offset in the new WIM - The
227 * stream may have a different compressed size in the new WIM if
228 * the compression type changed
230 struct resource_entry output_resource_entry;
233 struct list_head msg_list;
234 struct list_head being_compressed_list;
236 struct list_head inode_list;
239 struct hlist_node hash_list_2;
241 struct list_head write_streams_list;
245 /* Temporary list fields */
247 struct list_head unhashed_list;
248 struct list_head swm_stream_list;
249 struct list_head extraction_list;
250 struct list_head export_stream_list;
255 wim_resource_size(const struct wim_lookup_table_entry *lte)
257 return lte->resource_entry.original_size;
261 wim_resource_chunks(const struct wim_lookup_table_entry *lte)
263 return (wim_resource_size(lte) + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
267 wim_resource_compressed_size(const struct wim_lookup_table_entry *lte)
269 return lte->resource_entry.size;
273 * XXX Probably should store the compression type directly in the lookup table
277 wim_resource_compression_type(const struct wim_lookup_table_entry *lte)
279 if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
280 || lte->resource_location != RESOURCE_IN_WIM)
281 return WIMLIB_COMPRESSION_TYPE_NONE;
282 return wimlib_get_compression_type(lte->wim);
286 lte_filename_valid(const struct wim_lookup_table_entry *lte)
290 || lte->resource_location == RESOURCE_WIN32
291 || lte->resource_location == RESOURCE_WIN32_ENCRYPTED
293 || lte->resource_location == RESOURCE_IN_FILE_ON_DISK
296 || lte->resource_location == RESOURCE_IN_STAGING_FILE
301 extern struct wim_lookup_table *
302 new_lookup_table(size_t capacity);
305 read_lookup_table(WIMStruct *w);
308 write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry);
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();
329 extern struct wim_lookup_table_entry *
330 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte);
333 print_lookup_table_entry(const struct wim_lookup_table_entry *entry,
337 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
340 for_lookup_table_entry(struct wim_lookup_table *table,
341 int (*visitor)(struct wim_lookup_table_entry *, void *),
345 cmp_streams_by_wim_position(const void *p1, const void *p2);
348 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
349 int (*visitor)(struct wim_lookup_table_entry *,
353 extern struct wim_lookup_table_entry *
354 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
357 lookup_resource(WIMStruct *w, const tchar *path,
358 int lookup_flags, struct wim_dentry **dentry_ret,
359 struct wim_lookup_table_entry **lte_ret, u16 *stream_idx_ret);
362 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
363 struct wim_lookup_table *table);
366 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
370 lte_zero_out_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
373 lte_zero_real_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
376 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
379 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table);
382 inode_unresolve_ltes(struct wim_inode *inode);
385 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *__out);
387 static inline struct wim_lookup_table_entry *
388 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
390 wimlib_assert(inode->i_resolved);
391 wimlib_assert(stream_idx <= inode->i_num_ads);
395 return inode->i_ads_entries[stream_idx - 1].lte;
398 static inline struct wim_lookup_table_entry *
399 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
400 const struct wim_lookup_table *table)
402 wimlib_assert(!inode->i_resolved);
403 wimlib_assert(stream_idx <= inode->i_num_ads);
407 return __lookup_resource(table, inode->i_hash);
409 return __lookup_resource(table,
410 inode->i_ads_entries[
411 stream_idx - 1].hash);
414 extern struct wim_lookup_table_entry *
415 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
416 const struct wim_lookup_table *table);
418 static inline const u8 *
419 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
421 wimlib_assert(!inode->i_resolved);
422 wimlib_assert(stream_idx <= inode->i_num_ads);
424 return inode->i_hash;
426 return inode->i_ads_entries[stream_idx - 1].hash;
430 static inline const u8 *
431 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
433 struct wim_lookup_table_entry *lte;
434 lte = inode_stream_lte_resolved(inode, stream_idx);
442 * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
443 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
444 * alternate data stream.
446 * This works for both resolved and un-resolved dentries.
448 static inline const u8 *
449 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
451 if (inode->i_resolved)
452 return inode_stream_hash_resolved(inode, stream_idx);
454 return inode_stream_hash_unresolved(inode, stream_idx);
458 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
460 wimlib_assert(stream_idx <= inode->i_num_ads);
464 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
467 static inline struct wim_lookup_table_entry *
468 inode_unnamed_lte_resolved(const struct wim_inode *inode)
470 wimlib_assert(inode->i_resolved);
471 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
472 if (inode_stream_name_nbytes(inode, i) == 0 &&
473 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
475 return inode_stream_lte_resolved(inode, i);
481 static inline struct wim_lookup_table_entry *
482 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
483 const struct wim_lookup_table *table)
485 wimlib_assert(!inode->i_resolved);
486 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
487 if (inode_stream_name_nbytes(inode, i) == 0 &&
488 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
490 return inode_stream_lte_unresolved(inode, i, table);
496 extern struct wim_lookup_table_entry *
497 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
500 lookup_table_total_stream_size(struct wim_lookup_table *table);
504 lookup_table_insert_unhashed(struct wim_lookup_table *table,
505 struct wim_lookup_table_entry *lte,
506 struct wim_inode *back_inode,
510 lte->back_inode = back_inode;
511 lte->back_stream_id = back_stream_id;
512 list_add_tail(<e->unhashed_list, table->unhashed_streams);
516 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
517 struct wim_lookup_table *lookup_table,
518 struct wim_lookup_table_entry **lte_ret);
520 extern struct wim_lookup_table_entry **
521 retrieve_lte_pointer(struct wim_lookup_table_entry *lte);