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 tchar *extracted_file;
160 /* Pointers to somewhere where the stream is actually located. See the
161 * comments for the @resource_location field above. */
165 tchar *staging_file_name;
166 void *attached_buffer;
168 struct ntfs_location *ntfs_loc;
172 /* @file_on_disk_fp and @attr are both used to cache file/stream
173 * handles so we don't have re-open them on every read */
176 /* Valid iff resource_location == RESOURCE_IN_FILE_ON_DISK */
177 FILE *file_on_disk_fp;
179 /* Valid iff resource_location == RESOURCE_IN_NTFS_VOLUME */
180 struct _ntfs_attr *attr;
184 HANDLE win32_file_on_disk_fp;
187 /* Pointer to inode that contains the opened file descriptors to
188 * this stream (valid iff resource_location ==
189 * RESOURCE_IN_STAGING_FILE) */
190 struct wim_inode *lte_inode;
193 /* When a WIM file is written, out_refcnt starts at 0 and is incremented
194 * whenever the file resource pointed to by this lookup table entry
195 * needs to be written. The file resource only need to be written when
196 * out_refcnt is nonzero, since otherwise it is not referenced by any
203 /* When a WIM file is written, @output_resource_entry is filled
204 * in with the resource entry for the output WIM. This will not
205 * necessarily be the same as the @resource_entry since: - The
206 * stream may have a different offset in the new WIM - The
207 * stream may have a different compressed size in the new WIM if
208 * the compression type changed
210 struct resource_entry output_resource_entry;
212 struct list_head msg_list;
213 struct list_head inode_list;
216 /* List of lookup table entries that correspond to streams that have
217 * been extracted to the staging directory when modifying a read-write
220 * This field is also used to make other lists of lookup table entries.
222 struct list_head staging_list;
226 wim_resource_size(const struct wim_lookup_table_entry *lte)
228 return lte->resource_entry.original_size;
232 wim_resource_chunks(const struct wim_lookup_table_entry *lte)
234 return (wim_resource_size(lte) + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
238 wim_resource_compressed_size(const struct wim_lookup_table_entry *lte)
240 return lte->resource_entry.size;
244 * XXX Probably should store the compression type directly in the lookup table
248 wim_resource_compression_type(const struct wim_lookup_table_entry *lte)
250 if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
251 || lte->resource_location != RESOURCE_IN_WIM)
252 return WIMLIB_COMPRESSION_TYPE_NONE;
253 return wimlib_get_compression_type(lte->wim);
257 extern struct wim_lookup_table *
258 new_lookup_table(size_t capacity);
261 read_lookup_table(WIMStruct *w);
264 write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry);
267 free_lookup_table(struct wim_lookup_table *table);
270 lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte);
272 /* Unlinks a lookup table entry from the table; does not free it. */
274 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
276 hlist_del(<e->hash_list);
277 table->num_entries--;
280 extern struct wim_lookup_table_entry *
281 new_lookup_table_entry();
283 extern struct wim_lookup_table_entry *
284 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte);
287 print_lookup_table_entry(const struct wim_lookup_table_entry *entry,
291 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
294 for_lookup_table_entry(struct wim_lookup_table *table,
295 int (*visitor)(struct wim_lookup_table_entry *, void *),
298 extern struct wim_lookup_table_entry *
299 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
302 lookup_resource(WIMStruct *w, const tchar *path,
303 int lookup_flags, struct wim_dentry **dentry_ret,
304 struct wim_lookup_table_entry **lte_ret, u16 *stream_idx_ret);
307 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
308 struct wim_lookup_table *table);
311 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
315 lte_zero_out_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
318 lte_zero_real_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
321 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
324 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table);
327 inode_unresolve_ltes(struct wim_inode *inode);
330 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *__out);
332 static inline struct resource_entry*
333 wim_metadata_resource_entry(WIMStruct *w)
335 return &w->image_metadata[
336 w->current_image - 1].metadata_lte->resource_entry;
339 static inline struct wim_lookup_table_entry *
340 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
342 wimlib_assert(inode->i_resolved);
343 wimlib_assert(stream_idx <= inode->i_num_ads);
347 return inode->i_ads_entries[stream_idx - 1].lte;
350 static inline struct wim_lookup_table_entry *
351 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
352 const struct wim_lookup_table *table)
354 wimlib_assert(!inode->i_resolved);
355 wimlib_assert(stream_idx <= inode->i_num_ads);
359 return __lookup_resource(table, inode->i_hash);
361 return __lookup_resource(table,
362 inode->i_ads_entries[
363 stream_idx - 1].hash);
366 extern struct wim_lookup_table_entry *
367 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
368 const struct wim_lookup_table *table);
370 static inline const u8 *
371 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
373 wimlib_assert(!inode->i_resolved);
374 wimlib_assert(stream_idx <= inode->i_num_ads);
376 return inode->i_hash;
378 return inode->i_ads_entries[stream_idx - 1].hash;
382 static inline const u8 *
383 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
385 struct wim_lookup_table_entry *lte;
386 lte = inode_stream_lte_resolved(inode, stream_idx);
394 * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
395 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
396 * alternate data stream.
398 * This works for both resolved and un-resolved dentries.
400 static inline const u8 *
401 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
403 if (inode->i_resolved)
404 return inode_stream_hash_resolved(inode, stream_idx);
406 return inode_stream_hash_unresolved(inode, stream_idx);
410 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
412 wimlib_assert(stream_idx <= inode->i_num_ads);
416 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
419 static inline struct wim_lookup_table_entry *
420 inode_unnamed_lte_resolved(const struct wim_inode *inode)
422 wimlib_assert(inode->i_resolved);
423 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
424 if (inode_stream_name_nbytes(inode, i) == 0 &&
425 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
427 return inode_stream_lte_resolved(inode, i);
433 static inline struct wim_lookup_table_entry *
434 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
435 const struct wim_lookup_table *table)
437 wimlib_assert(!inode->i_resolved);
438 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
439 if (inode_stream_name_nbytes(inode, i) == 0 &&
440 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
442 return inode_stream_lte_unresolved(inode, i, table);
448 extern struct wim_lookup_table_entry *
449 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
452 lookup_table_total_stream_size(struct wim_lookup_table *table);