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_p;
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.
70 RESOURCE_IN_FILE_ON_DISK,
72 /* The stream resource is directly attached in an in-memory buffer
73 * pointed to by @attached_buffer. */
74 RESOURCE_IN_ATTACHED_BUFFER,
77 /* The stream resource is located in an external file in the staging
78 * directory for a read-write mount. */
79 RESOURCE_IN_STAGING_FILE,
83 /* The stream resource is located in an NTFS volume. It is identified
84 * by volume, filename, data stream name, and by whether it is a reparse
85 * point or not. @ntfs_loc points to a structure containing this
87 RESOURCE_IN_NTFS_VOLUME,
91 /* Resource must be accessed using Win32 API (may be a named data
95 /* Windows only: the file is on disk in the file named @file_on_disk,
96 * but the file is encrypted and must be read using special functions.
98 RESOURCE_WIN32_ENCRYPTED,
104 * An entry in the lookup table in the WIM file.
106 * It is used to find data streams for files in the WIM.
108 * Metadata resources and reparse point data buffers will also have lookup table
109 * entries associated with the data.
111 * The lookup_table_entry for a given dentry or alternate stream entry in the
112 * WIM is found using the SHA1 message digest field.
114 struct wim_lookup_table_entry {
116 /* List of lookup table entries in this hash bucket */
117 struct hlist_node hash_list;
119 /* Location and size of the stream in the WIM, whether it is compressed
120 * or not, and whether it's a metadata resource or not. This is an
122 struct resource_entry resource_entry;
124 /* Specifies which part of the split WIM the resource is located in.
125 * This is on on-disk field.
127 * In stand-alone WIMs, this must be 1.
129 * In split WIMs, every split WIM part has its own lookup table, and in
130 * read_lookup_table() it's currently expected that the part number of
131 * each lookup table entry in a split WIM part's lookup table is the
132 * same as the part number of that split WIM part. So this makes this
133 * field redundant since we store a pointer to the corresponding
134 * WIMStruct in the lookup table entry anyway.
138 /* See enum resource_location above */
139 u16 resource_location : 5;
144 * Number of times this lookup table entry is referenced by dentries.
145 * Unfortunately, this field is not always set correctly in Microsoft's
146 * WIMs, so we have no choice but to fix it if more references to the
147 * lookup table entry are found than stated here. */
151 /* (On-disk field) SHA1 message digest of the stream referenced
152 * by this lookup table entry */
153 u8 hash[SHA1_HASH_SIZE];
155 /* First 4 or 8 bytes of the SHA1 message digest, used for
156 * inserting the entry into the hash table. Since the SHA1
157 * message digest can be considered random, we don't really need
158 * the full 20 byte hash just to insert the entry in a hash
162 /* Unhashed entries only (unhashed == 1): these variables make
163 * it possible to find the to the pointer to this 'struct
164 * wim_lookup_table_entry' contained in a 'struct wim_ads_entry'
165 * or 'struct wim_inode'. */
167 struct wim_inode *back_inode;
172 /* When a WIM file is written, out_refcnt starts at 0 and is incremented
173 * whenever the file resource pointed to by this lookup table entry
174 * needs to be written. The file resource only need to be written when
175 * out_refcnt is nonzero, since otherwise it is not referenced by any
179 /* Pointers to somewhere where the stream is actually located. See the
180 * comments for the @resource_location field above. */
182 void *resource_loc_private;
185 void *attached_buffer;
187 tchar *staging_file_name;
190 struct ntfs_location *ntfs_loc;
201 /* This field is used for the special hardlink or symlink image
202 * extraction mode. In these mode, all identical files are linked
203 * together, and @extracted_file will be set to the filename of the
204 * first extracted file containing this stream. */
205 tchar *extracted_file;
209 /* When a WIM file is written, @output_resource_entry is filled
210 * in with the resource entry for the output WIM. This will not
211 * necessarily be the same as the @resource_entry since: - The
212 * stream may have a different offset in the new WIM - The
213 * stream may have a different compressed size in the new WIM if
214 * the compression type changed
216 struct resource_entry output_resource_entry;
218 struct list_head msg_list;
219 struct list_head inode_list;
222 struct hlist_node hash_list_2;
224 struct list_head write_streams_list;
228 /* Pointer to inode that contains the opened file descriptors to
229 * this stream (valid when resource_location ==
230 * RESOURCE_IN_STAGING_FILE) */
231 struct wim_inode *lte_inode;
235 /* Temporary list fields */
237 struct list_head unhashed_list;
238 struct list_head swm_stream_list;
239 struct list_head extraction_list;
240 struct list_head export_stream_list;
245 wim_resource_size(const struct wim_lookup_table_entry *lte)
247 return lte->resource_entry.original_size;
251 wim_resource_chunks(const struct wim_lookup_table_entry *lte)
253 return (wim_resource_size(lte) + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
257 wim_resource_compressed_size(const struct wim_lookup_table_entry *lte)
259 return lte->resource_entry.size;
263 * XXX Probably should store the compression type directly in the lookup table
267 wim_resource_compression_type(const struct wim_lookup_table_entry *lte)
269 if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
270 || lte->resource_location != RESOURCE_IN_WIM)
271 return WIMLIB_COMPRESSION_TYPE_NONE;
272 return wimlib_get_compression_type(lte->wim);
276 lte_filename_valid(const struct wim_lookup_table_entry *lte)
278 return lte->resource_location == RESOURCE_IN_FILE_ON_DISK
280 || lte->resource_location == RESOURCE_WIN32
281 || lte->resource_location == RESOURCE_WIN32_ENCRYPTED
284 || lte->resource_location == RESOURCE_IN_STAGING_FILE
289 extern struct wim_lookup_table *
290 new_lookup_table(size_t capacity);
293 read_lookup_table(WIMStruct *w);
296 write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry);
299 free_lookup_table(struct wim_lookup_table *table);
302 lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte);
304 /* Unlinks a lookup table entry from the table; does not free it. */
306 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
308 wimlib_assert(!lte->unhashed);
309 hlist_del(<e->hash_list);
310 wimlib_assert(table->num_entries != 0);
311 table->num_entries--;
314 extern struct wim_lookup_table_entry *
315 new_lookup_table_entry();
317 extern struct wim_lookup_table_entry *
318 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte);
321 print_lookup_table_entry(const struct wim_lookup_table_entry *entry,
325 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
328 for_lookup_table_entry(struct wim_lookup_table *table,
329 int (*visitor)(struct wim_lookup_table_entry *, void *),
333 cmp_streams_by_wim_position(const void *p1, const void *p2);
336 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
337 int (*visitor)(struct wim_lookup_table_entry *,
341 extern struct wim_lookup_table_entry *
342 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
345 lookup_resource(WIMStruct *w, const tchar *path,
346 int lookup_flags, struct wim_dentry **dentry_ret,
347 struct wim_lookup_table_entry **lte_ret, u16 *stream_idx_ret);
350 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
351 struct wim_lookup_table *table);
354 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
358 lte_zero_out_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
361 lte_zero_real_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
364 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
367 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table);
370 inode_unresolve_ltes(struct wim_inode *inode);
373 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *__out);
375 static inline struct wim_lookup_table_entry *
376 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
378 wimlib_assert(inode->i_resolved);
379 wimlib_assert(stream_idx <= inode->i_num_ads);
383 return inode->i_ads_entries[stream_idx - 1].lte;
386 static inline struct wim_lookup_table_entry *
387 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
388 const struct wim_lookup_table *table)
390 wimlib_assert(!inode->i_resolved);
391 wimlib_assert(stream_idx <= inode->i_num_ads);
395 return __lookup_resource(table, inode->i_hash);
397 return __lookup_resource(table,
398 inode->i_ads_entries[
399 stream_idx - 1].hash);
402 extern struct wim_lookup_table_entry *
403 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
404 const struct wim_lookup_table *table);
406 static inline const u8 *
407 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
409 wimlib_assert(!inode->i_resolved);
410 wimlib_assert(stream_idx <= inode->i_num_ads);
412 return inode->i_hash;
414 return inode->i_ads_entries[stream_idx - 1].hash;
418 static inline const u8 *
419 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
421 struct wim_lookup_table_entry *lte;
422 lte = inode_stream_lte_resolved(inode, stream_idx);
430 * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
431 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
432 * alternate data stream.
434 * This works for both resolved and un-resolved dentries.
436 static inline const u8 *
437 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
439 if (inode->i_resolved)
440 return inode_stream_hash_resolved(inode, stream_idx);
442 return inode_stream_hash_unresolved(inode, stream_idx);
446 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
448 wimlib_assert(stream_idx <= inode->i_num_ads);
452 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
455 static inline struct wim_lookup_table_entry *
456 inode_unnamed_lte_resolved(const struct wim_inode *inode)
458 wimlib_assert(inode->i_resolved);
459 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
460 if (inode_stream_name_nbytes(inode, i) == 0 &&
461 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
463 return inode_stream_lte_resolved(inode, i);
469 static inline struct wim_lookup_table_entry *
470 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
471 const struct wim_lookup_table *table)
473 wimlib_assert(!inode->i_resolved);
474 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
475 if (inode_stream_name_nbytes(inode, i) == 0 &&
476 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
478 return inode_stream_lte_unresolved(inode, i, table);
484 extern struct wim_lookup_table_entry *
485 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
488 lookup_table_total_stream_size(struct wim_lookup_table *table);
492 lookup_table_insert_unhashed(struct wim_lookup_table *table,
493 struct wim_lookup_table_entry *lte,
494 struct wim_inode *back_inode,
498 lte->back_inode = back_inode;
499 lte->back_stream_id = back_stream_id;
500 list_add_tail(<e->unhashed_list, table->unhashed_streams);
504 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
505 struct wim_lookup_table *lookup_table,
506 struct wim_lookup_table_entry **lte_ret);
508 extern struct wim_lookup_table_entry **
509 retrieve_lte_pointer(struct wim_lookup_table_entry *lte);