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 utf16lechar *stream_name;
37 u16 stream_name_nchars;
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,
81 /* Resource must be accessed using Win32 API (may be a named data
87 * An entry in the lookup table in the WIM file.
89 * It is used to find data streams for files in the WIM.
91 * Metadata resources and reparse point data buffers will also have lookup table
92 * entries associated with the data.
94 * The lookup_table_entry for a given dentry or alternate stream entry in the
95 * WIM is found using the SHA1 message digest field.
97 struct wim_lookup_table_entry {
99 /* List of lookup table entries in this hash bucket */
100 struct hlist_node hash_list;
102 /* Location and size of the stream in the WIM, whether it is compressed
103 * or not, and whether it's a metadata resource or not. This is an
105 struct resource_entry resource_entry;
107 /* Specifies which part of the split WIM the resource is located in.
108 * This is on on-disk field.
110 * In stand-alone WIMs, this must be 1.
112 * In split WIMs, every split WIM part has its own lookup table, and in
113 * read_lookup_table() it's currently expected that the part number of
114 * each lookup table entry in a split WIM part's lookup table is the
115 * same as the part number of that split WIM part. So this makes this
116 * field redundant since we store a pointer to the corresponding
117 * WIMStruct in the lookup table entry anyway.
121 /* See enum resource_location above */
122 u16 resource_location;
125 * Number of times this lookup table entry is referenced by dentries.
126 * Unfortunately, this field is not always set correctly in Microsoft's
127 * WIMs, so we have no choice but to fix it if more references to the
128 * lookup table entry are found than stated here. */
132 /* (On-disk field) SHA1 message digest of the stream referenced
133 * by this lookup table entry */
134 u8 hash[SHA1_HASH_SIZE];
136 /* First 4 or 8 bytes of the SHA1 message digest, used for
137 * inserting the entry into the hash table. Since the SHA1
138 * message digest can be considered random, we don't really need
139 * the full 20 byte hash just to insert the entry in a hash
149 /* This field is used for the special hardlink or symlink image
150 * extraction mode. In these mode, all identical files are linked
151 * together, and @extracted_file will be set to the filename of the
152 * first extracted file containing this stream. */
153 mbchar *extracted_file;
156 /* Pointers to somewhere where the stream is actually located. See the
157 * comments for the @resource_location field above. */
160 mbchar *file_on_disk;
161 mbchar *staging_file_name;
164 struct ntfs_location *ntfs_loc;
168 /* @file_on_disk_fp and @attr are both used to cache file/stream
169 * handles so we don't have re-open them on every read */
171 /* Valid iff resource_location == RESOURCE_IN_FILE_ON_DISK */
172 FILE *file_on_disk_fp;
174 /* Valid iff resource_location == RESOURCE_IN_NTFS_VOLUME */
175 struct _ntfs_attr *attr;
178 /* Pointer to inode that contains the opened file descriptors to
179 * this stream (valid iff resource_location ==
180 * RESOURCE_IN_STAGING_FILE) */
181 struct wim_inode *lte_inode;
184 /* When a WIM file is written, out_refcnt starts at 0 and is incremented
185 * whenever the file resource pointed to by this lookup table entry
186 * needs to be written. The file resource only need to be written when
187 * out_refcnt is nonzero, since otherwise it is not referenced by any
194 /* When a WIM file is written, @output_resource_entry is filled
195 * in with the resource entry for the output WIM. This will not
196 * necessarily be the same as the @resource_entry since: - The
197 * stream may have a different offset in the new WIM - The
198 * stream may have a different compressed size in the new WIM if
199 * the compression type changed
201 struct resource_entry output_resource_entry;
203 struct list_head msg_list;
204 struct list_head inode_list;
207 /* List of lookup table entries that correspond to streams that have
208 * been extracted to the staging directory when modifying a read-write
211 * This field is also used to make other lists of lookup table entries.
213 struct list_head staging_list;
217 wim_resource_size(const struct wim_lookup_table_entry *lte)
219 return lte->resource_entry.original_size;
223 wim_resource_chunks(const struct wim_lookup_table_entry *lte)
225 return (wim_resource_size(lte) + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
229 wim_resource_compressed_size(const struct wim_lookup_table_entry *lte)
231 return lte->resource_entry.size;
235 * XXX Probably should store the compression type directly in the lookup table
239 wim_resource_compression_type(const struct wim_lookup_table_entry *lte)
241 if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
242 || lte->resource_location != RESOURCE_IN_WIM)
243 return WIMLIB_COMPRESSION_TYPE_NONE;
244 return wimlib_get_compression_type(lte->wim);
248 extern struct wim_lookup_table *
249 new_lookup_table(size_t capacity);
252 read_lookup_table(WIMStruct *w);
255 write_lookup_table(struct wim_lookup_table *table, FILE *out,
256 struct resource_entry *out_res_entry);
258 free_lookup_table(struct wim_lookup_table *table);
261 lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte);
263 /* Unlinks a lookup table entry from the table; does not free it. */
265 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
267 hlist_del(<e->hash_list);
268 table->num_entries--;
271 extern struct wim_lookup_table_entry *
272 new_lookup_table_entry();
274 extern struct wim_lookup_table_entry *
275 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte);
278 print_lookup_table_entry(const struct wim_lookup_table_entry *entry,
282 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
285 for_lookup_table_entry(struct wim_lookup_table *table,
286 int (*visitor)(struct wim_lookup_table_entry *, void *),
289 extern struct wim_lookup_table_entry *
290 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
293 lookup_resource(WIMStruct *w, const mbchar *path,
294 int lookup_flags, struct wim_dentry **dentry_ret,
295 struct wim_lookup_table_entry **lte_ret, u16 *stream_idx_ret);
298 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
299 struct wim_lookup_table *table);
302 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
306 lte_zero_out_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
309 lte_zero_real_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
312 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
315 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table);
318 inode_unresolve_ltes(struct wim_inode *inode);
321 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *__out);
323 static inline struct resource_entry*
324 wim_metadata_resource_entry(WIMStruct *w)
326 return &w->image_metadata[
327 w->current_image - 1].metadata_lte->resource_entry;
330 static inline struct wim_lookup_table_entry *
331 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
333 wimlib_assert(inode->i_resolved);
334 wimlib_assert(stream_idx <= inode->i_num_ads);
338 return inode->i_ads_entries[stream_idx - 1].lte;
341 static inline struct wim_lookup_table_entry *
342 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
343 const struct wim_lookup_table *table)
345 wimlib_assert(!inode->i_resolved);
346 wimlib_assert(stream_idx <= inode->i_num_ads);
350 return __lookup_resource(table, inode->i_hash);
352 return __lookup_resource(table,
353 inode->i_ads_entries[
354 stream_idx - 1].hash);
357 extern struct wim_lookup_table_entry *
358 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
359 const struct wim_lookup_table *table);
361 static inline const u8 *
362 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
364 wimlib_assert(!inode->i_resolved);
365 wimlib_assert(stream_idx <= inode->i_num_ads);
367 return inode->i_hash;
369 return inode->i_ads_entries[stream_idx - 1].hash;
373 static inline const u8 *
374 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
376 struct wim_lookup_table_entry *lte;
377 lte = inode_stream_lte_resolved(inode, stream_idx);
385 * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
386 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
387 * alternate data stream.
389 * This works for both resolved and un-resolved dentries.
391 static inline const u8 *
392 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
394 if (inode->i_resolved)
395 return inode_stream_hash_resolved(inode, stream_idx);
397 return inode_stream_hash_unresolved(inode, stream_idx);
401 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
403 wimlib_assert(stream_idx <= inode->i_num_ads);
407 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
410 static inline struct wim_lookup_table_entry *
411 inode_unnamed_lte_resolved(const struct wim_inode *inode)
413 wimlib_assert(inode->i_resolved);
414 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
415 if (inode_stream_name_nbytes(inode, i) == 0 &&
416 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
418 return inode_stream_lte_resolved(inode, i);
424 static inline struct wim_lookup_table_entry *
425 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
426 const struct wim_lookup_table *table)
428 wimlib_assert(!inode->i_resolved);
429 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
430 if (inode_stream_name_nbytes(inode, i) == 0 &&
431 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
433 return inode_stream_lte_unresolved(inode, i, table);
439 extern struct wim_lookup_table_entry *
440 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
443 lookup_table_total_stream_size(struct wim_lookup_table *table);