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
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;
34 struct ntfs_location {
36 char *stream_name_utf16;
37 u16 stream_name_utf16_num_chars;
38 struct _ntfs_volume **ntfs_vol_p;
39 bool is_reparse_point;
43 /* An enumerated type that identifies where the stream corresponding to this
44 * lookup table entry is actually located.
46 * If we open a WIM and read its lookup table, the location is set to
47 * RESOURCE_IN_WIM since all the streams will initially be located in the WIM.
48 * However, to deal with problems such as image capture and image mount, we
49 * allow the actual location of the stream to be somewhere else, such as an
52 enum resource_location {
53 /* The lookup table entry does not correspond to a stream (this state
54 * should exist only temporarily) */
55 RESOURCE_NONEXISTENT = 0,
57 /* The stream resource is located in a WIM file. The WIMStruct for the
58 * WIM file will be pointed to by the @wim member. */
61 /* The stream resource is located in an external file. The name of the
62 * file will be provided by @file_on_disk member. In addition, if
63 * @file_on_disk_fp is not NULL, it will be an open FILE * to the file.
65 RESOURCE_IN_FILE_ON_DISK,
67 /* The stream resource is located in an external file in the staging
68 * directory for a read-write mount. */
69 RESOURCE_IN_STAGING_FILE,
71 /* The stream resource is directly attached in an in-memory buffer
72 * pointed to by @attached_buffer. */
73 RESOURCE_IN_ATTACHED_BUFFER,
75 /* The stream resource is located in an NTFS volume. It is identified
76 * by volume, filename, data stream name, and by whether it is a reparse
77 * point or not. @ntfs_loc points to a structure containing this
79 RESOURCE_IN_NTFS_VOLUME,
83 * An entry in the lookup table in the WIM file.
85 * It is used to find data streams for files in the WIM.
87 * Metadata resources and reparse point data buffers will also have lookup table
88 * entries associated with the data.
90 * The lookup_table_entry for a given dentry or alternate stream entry in the
91 * WIM is found using the SHA1 message digest field.
93 struct wim_lookup_table_entry {
95 /* List of lookup table entries in this hash bucket */
96 struct hlist_node hash_list;
98 /* Location and size of the stream in the WIM, whether it is compressed
99 * or not, and whether it's a metadata resource or not. This is an
101 struct resource_entry resource_entry;
103 /* Specifies which part of the split WIM the resource is located in.
104 * This is on on-disk field.
106 * In stand-alone WIMs, this must be 1.
108 * In split WIMs, every split WIM part has its own lookup table, and in
109 * read_lookup_table() it's currently expected that the part number of
110 * each lookup table entry in a split WIM part's lookup table is the
111 * same as the part number of that split WIM part. So this makes this
112 * field redundant since we store a pointer to the corresponding
113 * WIMStruct in the lookup table entry anyway.
117 /* See enum resource_location above */
118 u16 resource_location;
121 * Number of times this lookup table entry is referenced by dentries.
122 * Unfortunately, this field is not always set correctly in Microsoft's
123 * WIMs, so we have no choice but to fix it if more references to the
124 * lookup table entry are found than stated here. */
128 /* (On-disk field) SHA1 message digest of the stream referenced
129 * by this lookup table entry */
130 u8 hash[SHA1_HASH_SIZE];
132 /* First 4 or 8 bytes of the SHA1 message digest, used for
133 * inserting the entry into the hash table. Since the SHA1
134 * message digest can be considered random, we don't really need
135 * the full 20 byte hash just to insert the entry in a hash
145 /* This field is used for the special hardlink or symlink image
146 * extraction mode. In these mode, all identical files are linked
147 * together, and @extracted_file will be set to the filename of the
148 * first extracted file containing this stream. */
149 char *extracted_file;
152 /* Pointers to somewhere where the stream is actually located. See the
153 * comments for the @resource_location field above. */
157 char *staging_file_name;
160 struct ntfs_location *ntfs_loc;
164 /* @file_on_disk_fp and @attr are both used to cache file/stream
165 * handles so we don't have re-open them on every read */
167 /* Valid iff resource_location == RESOURCE_IN_FILE_ON_DISK */
168 FILE *file_on_disk_fp;
170 /* Valid iff resource_location == RESOURCE_IN_NTFS_VOLUME */
171 struct _ntfs_attr *attr;
174 /* Pointer to inode that contains the opened file descriptors to
175 * this stream (valid iff resource_location ==
176 * RESOURCE_IN_STAGING_FILE) */
177 struct wim_inode *lte_inode;
180 /* When a WIM file is written, out_refcnt starts at 0 and is incremented
181 * whenever the file resource pointed to by this lookup table entry
182 * needs to be written. The file resource only need to be written when
183 * out_refcnt is nonzero, since otherwise it is not referenced by any
190 /* When a WIM file is written, @output_resource_entry is filled
191 * in with the resource entry for the output WIM. This will not
192 * necessarily be the same as the @resource_entry since: - The
193 * stream may have a different offset in the new WIM - The
194 * stream may have a different compressed size in the new WIM if
195 * the compression type changed
197 struct resource_entry output_resource_entry;
199 struct list_head msg_list;
200 struct list_head inode_list;
203 /* List of lookup table entries that correspond to streams that have
204 * been extracted to the staging directory when modifying a read-write
207 * This field is also used to make other lists of lookup table entries.
209 struct list_head staging_list;
213 wim_resource_size(const struct wim_lookup_table_entry *lte)
215 return lte->resource_entry.original_size;
219 wim_resource_chunks(const struct wim_lookup_table_entry *lte)
221 return (wim_resource_size(lte) + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
225 wim_resource_compressed_size(const struct wim_lookup_table_entry *lte)
227 return lte->resource_entry.size;
231 * XXX Probably should store the compression type directly in the lookup table
235 wim_resource_compression_type(const struct wim_lookup_table_entry *lte)
237 if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
238 || lte->resource_location != RESOURCE_IN_WIM)
239 return WIMLIB_COMPRESSION_TYPE_NONE;
240 return wimlib_get_compression_type(lte->wim);
244 extern struct wim_lookup_table *
245 new_lookup_table(size_t capacity);
248 read_lookup_table(WIMStruct *w);
251 write_lookup_table(struct wim_lookup_table *table, FILE *out,
252 struct resource_entry *out_res_entry);
254 free_lookup_table(struct wim_lookup_table *table);
257 lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte);
259 /* Unlinks a lookup table entry from the table; does not free it. */
261 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
263 hlist_del(<e->hash_list);
264 table->num_entries--;
267 extern struct wim_lookup_table_entry *
268 new_lookup_table_entry();
270 extern struct wim_lookup_table_entry *
271 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte);
274 print_lookup_table_entry(const struct wim_lookup_table_entry *entry,
278 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
281 for_lookup_table_entry(struct wim_lookup_table *table,
282 int (*visitor)(struct wim_lookup_table_entry *, void *),
285 extern struct wim_lookup_table_entry *
286 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
289 lookup_resource(WIMStruct *w, const char *path,
290 int lookup_flags, struct wim_dentry **dentry_ret,
291 struct wim_lookup_table_entry **lte_ret, u16 *stream_idx_ret);
294 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
295 struct wim_lookup_table *table);
298 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
302 lte_zero_out_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
305 lte_zero_real_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
308 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
311 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table);
314 inode_unresolve_ltes(struct wim_inode *inode);
317 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *__out);
319 static inline struct resource_entry*
320 wim_metadata_resource_entry(WIMStruct *w)
322 return &w->image_metadata[
323 w->current_image - 1].metadata_lte->resource_entry;
326 static inline struct wim_lookup_table_entry *
327 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
329 wimlib_assert(inode->i_resolved);
330 wimlib_assert(stream_idx <= inode->i_num_ads);
334 return inode->i_ads_entries[stream_idx - 1].lte;
337 static inline struct wim_lookup_table_entry *
338 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
339 const struct wim_lookup_table *table)
341 wimlib_assert(!inode->i_resolved);
342 wimlib_assert(stream_idx <= inode->i_num_ads);
346 return __lookup_resource(table, inode->i_hash);
348 return __lookup_resource(table,
349 inode->i_ads_entries[
350 stream_idx - 1].hash);
353 extern struct wim_lookup_table_entry *
354 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
355 const struct wim_lookup_table *table);
357 static inline const u8 *
358 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
360 wimlib_assert(!inode->i_resolved);
361 wimlib_assert(stream_idx <= inode->i_num_ads);
363 return inode->i_hash;
365 return inode->i_ads_entries[stream_idx - 1].hash;
369 static inline const u8 *
370 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
372 struct wim_lookup_table_entry *lte;
373 lte = inode_stream_lte_resolved(inode, stream_idx);
381 * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
382 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
383 * alternate data stream.
385 * This works for both resolved and un-resolved dentries.
387 static inline const u8 *
388 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
390 if (inode->i_resolved)
391 return inode_stream_hash_resolved(inode, stream_idx);
393 return inode_stream_hash_unresolved(inode, stream_idx);
397 inode_stream_name_len(const struct wim_inode *inode, unsigned stream_idx)
399 wimlib_assert(stream_idx <= inode->i_num_ads);
403 return inode->i_ads_entries[stream_idx - 1].stream_name_len;
406 static inline struct wim_lookup_table_entry *
407 inode_unnamed_lte_resolved(const struct wim_inode *inode)
409 wimlib_assert(inode->i_resolved);
410 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
411 if (inode_stream_name_len(inode, i) == 0 &&
412 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
414 return inode_stream_lte_resolved(inode, i);
420 static inline struct wim_lookup_table_entry *
421 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
422 const struct wim_lookup_table *table)
424 wimlib_assert(!inode->i_resolved);
425 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
426 if (inode_stream_name_len(inode, i) == 0 &&
427 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
429 return inode_stream_lte_unresolved(inode, i, table);
435 extern struct wim_lookup_table_entry *
436 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
439 lookup_table_total_stream_size(struct wim_lookup_table *table);