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;
38 struct ntfs_location {
40 utf16lechar *stream_name;
41 u16 stream_name_nchars;
42 struct _ntfs_volume **ntfs_vol_p;
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. */
65 /* The stream resource is located in an external file. The name of the
66 * file will be provided by @file_on_disk member. In addition, if
67 * @file_on_disk_fp is not NULL, it will be an open FILE * to the file.
69 RESOURCE_IN_FILE_ON_DISK,
71 /* The stream resource is located in an external file in the staging
72 * directory for a read-write mount. */
73 RESOURCE_IN_STAGING_FILE,
75 /* The stream resource is directly attached in an in-memory buffer
76 * pointed to by @attached_buffer. */
77 RESOURCE_IN_ATTACHED_BUFFER,
79 /* The stream resource is located in an NTFS volume. It is identified
80 * by volume, filename, data stream name, and by whether it is a reparse
81 * point or not. @ntfs_loc points to a structure containing this
83 RESOURCE_IN_NTFS_VOLUME,
85 /* Resource must be accessed using Win32 API (may be a named data
91 * An entry in the lookup table in the WIM file.
93 * It is used to find data streams for files in the WIM.
95 * Metadata resources and reparse point data buffers will also have lookup table
96 * entries associated with the data.
98 * The lookup_table_entry for a given dentry or alternate stream entry in the
99 * WIM is found using the SHA1 message digest field.
101 struct wim_lookup_table_entry {
103 /* List of lookup table entries in this hash bucket */
104 struct hlist_node hash_list;
106 /* Location and size of the stream in the WIM, whether it is compressed
107 * or not, and whether it's a metadata resource or not. This is an
109 struct resource_entry resource_entry;
111 /* Specifies which part of the split WIM the resource is located in.
112 * This is on on-disk field.
114 * In stand-alone WIMs, this must be 1.
116 * In split WIMs, every split WIM part has its own lookup table, and in
117 * read_lookup_table() it's currently expected that the part number of
118 * each lookup table entry in a split WIM part's lookup table is the
119 * same as the part number of that split WIM part. So this makes this
120 * field redundant since we store a pointer to the corresponding
121 * WIMStruct in the lookup table entry anyway.
125 /* See enum resource_location above */
126 u16 resource_location;
129 * Number of times this lookup table entry is referenced by dentries.
130 * Unfortunately, this field is not always set correctly in Microsoft's
131 * WIMs, so we have no choice but to fix it if more references to the
132 * lookup table entry are found than stated here. */
136 /* (On-disk field) SHA1 message digest of the stream referenced
137 * by this lookup table entry */
138 u8 hash[SHA1_HASH_SIZE];
140 /* First 4 or 8 bytes of the SHA1 message digest, used for
141 * inserting the entry into the hash table. Since the SHA1
142 * message digest can be considered random, we don't really need
143 * the full 20 byte hash just to insert the entry in a hash
153 /* This field is used for the special hardlink or symlink image
154 * extraction mode. In these mode, all identical files are linked
155 * together, and @extracted_file will be set to the filename of the
156 * first extracted file containing this stream. */
157 mbchar *extracted_file;
160 /* Pointers to somewhere where the stream is actually located. See the
161 * comments for the @resource_location field above. */
164 mbchar *file_on_disk;
165 mbchar *staging_file_name;
167 wchar_t *win32_file_on_disk;
171 struct ntfs_location *ntfs_loc;
175 /* @file_on_disk_fp and @attr are both used to cache file/stream
176 * handles so we don't have re-open them on every read */
179 /* Valid iff resource_location == RESOURCE_IN_FILE_ON_DISK */
180 FILE *file_on_disk_fp;
182 /* Valid iff resource_location == RESOURCE_IN_NTFS_VOLUME */
183 struct _ntfs_attr *attr;
187 HANDLE win32_file_on_disk_fp;
190 /* Pointer to inode that contains the opened file descriptors to
191 * this stream (valid iff resource_location ==
192 * RESOURCE_IN_STAGING_FILE) */
193 struct wim_inode *lte_inode;
196 /* When a WIM file is written, out_refcnt starts at 0 and is incremented
197 * whenever the file resource pointed to by this lookup table entry
198 * needs to be written. The file resource only need to be written when
199 * out_refcnt is nonzero, since otherwise it is not referenced by any
206 /* When a WIM file is written, @output_resource_entry is filled
207 * in with the resource entry for the output WIM. This will not
208 * necessarily be the same as the @resource_entry since: - The
209 * stream may have a different offset in the new WIM - The
210 * stream may have a different compressed size in the new WIM if
211 * the compression type changed
213 struct resource_entry output_resource_entry;
215 struct list_head msg_list;
216 struct list_head inode_list;
219 /* List of lookup table entries that correspond to streams that have
220 * been extracted to the staging directory when modifying a read-write
223 * This field is also used to make other lists of lookup table entries.
225 struct list_head staging_list;
229 wim_resource_size(const struct wim_lookup_table_entry *lte)
231 return lte->resource_entry.original_size;
235 wim_resource_chunks(const struct wim_lookup_table_entry *lte)
237 return (wim_resource_size(lte) + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
241 wim_resource_compressed_size(const struct wim_lookup_table_entry *lte)
243 return lte->resource_entry.size;
247 * XXX Probably should store the compression type directly in the lookup table
251 wim_resource_compression_type(const struct wim_lookup_table_entry *lte)
253 if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
254 || lte->resource_location != RESOURCE_IN_WIM)
255 return WIMLIB_COMPRESSION_TYPE_NONE;
256 return wimlib_get_compression_type(lte->wim);
260 extern struct wim_lookup_table *
261 new_lookup_table(size_t capacity);
264 read_lookup_table(WIMStruct *w);
267 write_lookup_table(struct wim_lookup_table *table, FILE *out,
268 struct resource_entry *out_res_entry);
270 free_lookup_table(struct wim_lookup_table *table);
273 lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte);
275 /* Unlinks a lookup table entry from the table; does not free it. */
277 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
279 hlist_del(<e->hash_list);
280 table->num_entries--;
283 extern struct wim_lookup_table_entry *
284 new_lookup_table_entry();
286 extern struct wim_lookup_table_entry *
287 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte);
290 print_lookup_table_entry(const struct wim_lookup_table_entry *entry,
294 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
297 for_lookup_table_entry(struct wim_lookup_table *table,
298 int (*visitor)(struct wim_lookup_table_entry *, void *),
301 extern struct wim_lookup_table_entry *
302 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
305 lookup_resource(WIMStruct *w, const mbchar *path,
306 int lookup_flags, struct wim_dentry **dentry_ret,
307 struct wim_lookup_table_entry **lte_ret, u16 *stream_idx_ret);
310 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
311 struct wim_lookup_table *table);
314 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
318 lte_zero_out_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
321 lte_zero_real_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
324 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
327 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table);
330 inode_unresolve_ltes(struct wim_inode *inode);
333 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *__out);
335 static inline struct resource_entry*
336 wim_metadata_resource_entry(WIMStruct *w)
338 return &w->image_metadata[
339 w->current_image - 1].metadata_lte->resource_entry;
342 static inline struct wim_lookup_table_entry *
343 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
345 wimlib_assert(inode->i_resolved);
346 wimlib_assert(stream_idx <= inode->i_num_ads);
350 return inode->i_ads_entries[stream_idx - 1].lte;
353 static inline struct wim_lookup_table_entry *
354 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
355 const struct wim_lookup_table *table)
357 wimlib_assert(!inode->i_resolved);
358 wimlib_assert(stream_idx <= inode->i_num_ads);
362 return __lookup_resource(table, inode->i_hash);
364 return __lookup_resource(table,
365 inode->i_ads_entries[
366 stream_idx - 1].hash);
369 extern struct wim_lookup_table_entry *
370 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
371 const struct wim_lookup_table *table);
373 static inline const u8 *
374 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
376 wimlib_assert(!inode->i_resolved);
377 wimlib_assert(stream_idx <= inode->i_num_ads);
379 return inode->i_hash;
381 return inode->i_ads_entries[stream_idx - 1].hash;
385 static inline const u8 *
386 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
388 struct wim_lookup_table_entry *lte;
389 lte = inode_stream_lte_resolved(inode, stream_idx);
397 * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
398 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
399 * alternate data stream.
401 * This works for both resolved and un-resolved dentries.
403 static inline const u8 *
404 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
406 if (inode->i_resolved)
407 return inode_stream_hash_resolved(inode, stream_idx);
409 return inode_stream_hash_unresolved(inode, stream_idx);
413 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
415 wimlib_assert(stream_idx <= inode->i_num_ads);
419 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
422 static inline struct wim_lookup_table_entry *
423 inode_unnamed_lte_resolved(const struct wim_inode *inode)
425 wimlib_assert(inode->i_resolved);
426 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
427 if (inode_stream_name_nbytes(inode, i) == 0 &&
428 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
430 return inode_stream_lte_resolved(inode, i);
436 static inline struct wim_lookup_table_entry *
437 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
438 const struct wim_lookup_table *table)
440 wimlib_assert(!inode->i_resolved);
441 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
442 if (inode_stream_name_nbytes(inode, i) == 0 &&
443 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
445 return inode_stream_lte_unresolved(inode, i, table);
451 extern struct wim_lookup_table_entry *
452 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
455 lookup_table_total_stream_size(struct wim_lookup_table *table);