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 //#define LOOKUP_FLAG_FOLLOW_SYMLINKS 0x00000004
18 /* A lookup table that is used to translate the hash codes of dentries into the
19 * offsets and sizes of uncompressed or compressed file resources. It is
20 * implemented as a hash table. */
22 struct hlist_head *array;
30 struct ntfs_location {
32 char *stream_name_utf16;
33 u16 stream_name_utf16_num_chars;
34 struct _ntfs_volume **ntfs_vol_p;
35 bool is_reparse_point;
40 * An entry in the lookup table in the WIM file.
42 * It is used to find data streams for files in the WIM.
44 * Metadata resources and reparse point data buffers will also have lookup table
45 * entries associated with the data.
47 * The lookup_table_entry for a given dentry or alternate stream entry in the
48 * WIM is found using the SHA1 message digest field.
50 struct lookup_table_entry {
52 /* List of lookup table entries in this hash bucket */
54 struct hlist_node hash_list;
55 struct list_head list;
58 /* Location and size of the stream in the WIM, whether it is compressed
59 * or not, and whether it's a metadata resource or not. This is an
61 struct resource_entry resource_entry;
63 /* Specifies which part of the split WIM the resource is located in.
64 * This is on on-disk field.
66 * In stand-alone WIMs, this must be 1.
68 * In split WIMs, every split WIM part has its own lookup table, and in
69 * read_lookup_table() it's currently expected that the part number of
70 * each lookup table entry in a split WIM part's lookup table is the
71 * same as the part number of that split WIM part. So this makes this
72 * field redundant since we store a pointer to the corresponding
73 * WIMStruct in the lookup table entry anyway.
77 /* An enumerated type that identifies where the stream corresponding to
78 * this lookup table entry is actually located.
80 * Obviously if we open a WIM and read its lookup table, the location is
81 * set to RESOURCE_IN_WIM since all the streams will initially be
82 * located in the WIM. However, to deal with problems such as image
83 * capture and image mount, we allow the actual location of the stream
84 * to be somewhere else, such as an external file.
87 /* The lookup table entry does not correspond to a stream (this
88 * state should exist only temporarily) */
89 RESOURCE_NONEXISTENT = 0,
91 /* The stream resource is located in a WIM file. The WIMStruct
92 * for the WIM file will be pointed to by the @wim member. */
95 /* The stream resource is located in an external file. The
96 * name of the file will be provided by @file_on_disk member.
97 * In addition, if @file_on_disk_fp is not NULL, it will be an
98 * open FILE * to the file. */
99 RESOURCE_IN_FILE_ON_DISK,
101 /* The stream resource is located in an external file in the
102 * staging directory for a read-write mount. */
103 RESOURCE_IN_STAGING_FILE,
105 /* The stream resource is directly attached in an in-memory
106 * buffer pointed to by @attached_buffer. */
107 RESOURCE_IN_ATTACHED_BUFFER,
109 /* The stream resource is located in an NTFS volume. It is
110 * identified by volume, filename, data stream name, and by
111 * whether it is a reparse point or not. @ntfs_loc points to a
112 * structure containing this information. */
113 RESOURCE_IN_NTFS_VOLUME,
117 * Number of times this lookup table entry is referenced by dentries.
118 * Unfortunately, this field is not always set correctly in Microsoft's
119 * WIMs, so we have no choice but to fix it if more references to the
120 * lookup table entry are found than stated here. */
124 /* (On-disk field) SHA1 message digest of the stream referenced
125 * by this lookup table entry */
126 u8 hash[SHA1_HASH_SIZE];
128 /* First 4 or 8 bytes of the SHA1 message digest, used for
129 * inserting the entry into the hash table. Since the SHA1
130 * message digest can be considered random, we don't really need
131 * the full 20 byte hash just to insert the entry in a hash
136 /* Pointers to somewhere where the stream is actually located. See the
137 * comments for the @resource_location field above. */
141 char *staging_file_name;
144 struct ntfs_location *ntfs_loc;
148 /* @file_on_disk_fp and @attr are both used to cache file/stream
149 * handles so we don't have re-open them on every read */
151 /* Valid iff resource_location == RESOURCE_IN_FILE_ON_DISK */
152 FILE *file_on_disk_fp;
154 /* Valid iff resource_location == RESOURCE_IN_NTFS_VOLUME */
155 struct _ntfs_attr *attr;
158 /* Pointer to inode that contains the opened file descriptors to
159 * this stream (valid iff resource_location ==
160 * RESOURCE_IN_STAGING_FILE) */
161 struct inode *lte_inode;
167 /* When a WIM file is written, out_refcnt starts at 0 and is incremented
168 * whenever the file resource pointed to by this lookup table entry
169 * needs to be written. The file resource only need to be written when
170 * out_refcnt is nonzero, since otherwise it is not referenced by any
176 /* When a WIM file is written, @output_resource_entry is filled
177 * in with the resource entry for the output WIM. This will not
178 * necessarily be the same as the @resource_entry since:
179 * - The stream may have a different offset in the new WIM
180 * - The stream may have a different compressed size in the
181 * new WIM if the compression type changed
183 struct resource_entry output_resource_entry;
185 /* This field is used for the special hardlink or symlink image
186 * extraction mode. In these mode, all identical files are linked
187 * together, and @extracted_file will be set to the filename of the
188 * first extracted file containing this stream. */
189 char *extracted_file;
192 /* List of lookup table entries that correspond to streams that have
193 * been extracted to the staging directory when modifying a read-write
195 struct list_head staging_list;
197 /* Temporary field for creating a singly linked list. */
198 struct lookup_table_entry *next_lte_in_swm;
202 static inline u64 wim_resource_size(const struct lookup_table_entry *lte)
204 return lte->resource_entry.original_size;
208 wim_resource_compressed_size(const struct lookup_table_entry *lte)
210 return lte->resource_entry.size;
214 * XXX Probably should store the compression type directly in the lookup table
218 wim_resource_compression_type(const struct lookup_table_entry *lte)
220 if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
221 || lte->resource_location != RESOURCE_IN_WIM)
222 return WIM_COMPRESSION_TYPE_NONE;
223 return wimlib_get_compression_type(lte->wim);
227 extern struct lookup_table *new_lookup_table(size_t capacity);
229 extern void lookup_table_insert(struct lookup_table *table,
230 struct lookup_table_entry *lte);
232 /* Unlinks a lookup table entry from the table; does not free it. */
233 static inline void lookup_table_unlink(struct lookup_table *table,
234 struct lookup_table_entry *lte)
236 hlist_del(<e->hash_list);
237 table->num_entries--;
240 extern struct lookup_table_entry *new_lookup_table_entry();
242 extern struct lookup_table_entry *
243 clone_lookup_table_entry(const struct lookup_table_entry *lte);
245 extern int for_lookup_table_entry(struct lookup_table *table,
246 int (*visitor)(struct lookup_table_entry *, void *),
249 extern struct lookup_table_entry *
250 __lookup_resource(const struct lookup_table *table, const u8 hash[]);
252 extern int lookup_resource(WIMStruct *w, const char *path,
253 int lookup_flags, struct dentry **dentry_ret,
254 struct lookup_table_entry **lte_ret,
255 u16 *stream_idx_ret);
257 extern void lte_decrement_refcnt(struct lookup_table_entry *lte,
258 struct lookup_table *table);
260 extern void lte_decrement_num_opened_fds(struct lookup_table_entry *lte);
263 extern int lte_zero_out_refcnt(struct lookup_table_entry *entry, void *ignore);
264 extern int lte_zero_real_refcnt(struct lookup_table_entry *entry, void *ignore);
265 extern int lte_free_extracted_file(struct lookup_table_entry *lte, void *ignone);
267 extern void print_lookup_table_entry(const struct lookup_table_entry *entry);
269 extern int read_lookup_table(WIMStruct *w);
271 extern void free_lookup_table(struct lookup_table *table);
273 extern int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out);
275 extern void free_lookup_table_entry(struct lookup_table_entry *lte);
277 extern int dentry_resolve_ltes(struct dentry *dentry, void *__table);
278 extern int dentry_unresolve_ltes(struct dentry *dentry, void *ignore);
280 /* Writes the lookup table to the output file. */
281 static inline int write_lookup_table(struct lookup_table *table, FILE *out)
283 return for_lookup_table_entry(table, write_lookup_table_entry, out);
286 /* Unlinks and frees an entry from a lookup table. */
287 static inline void lookup_table_remove(struct lookup_table *table,
288 struct lookup_table_entry *lte)
290 lookup_table_unlink(table, lte);
291 free_lookup_table_entry(lte);
294 static inline struct resource_entry* wim_metadata_resource_entry(WIMStruct *w)
296 return &w->image_metadata[
297 w->current_image - 1].metadata_lte->resource_entry;
300 static inline struct lookup_table_entry *
301 inode_stream_lte_resolved(const struct inode *inode, unsigned stream_idx)
303 wimlib_assert(inode->resolved);
304 wimlib_assert(stream_idx <= inode->num_ads);
308 return inode->ads_entries[stream_idx - 1].lte;
311 static inline struct lookup_table_entry *
312 inode_stream_lte_unresolved(const struct inode *inode, unsigned stream_idx,
313 const struct lookup_table *table)
315 wimlib_assert(!inode->resolved);
316 wimlib_assert(stream_idx <= inode->num_ads);
320 return __lookup_resource(table, inode->hash);
322 return __lookup_resource(table,
324 stream_idx - 1].hash);
327 * Returns the lookup table entry for stream @stream_idx of the inode, where
328 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
329 * corresponds to an alternate data stream.
331 * This works for both resolved and un-resolved dentries.
333 static inline struct lookup_table_entry *
334 inode_stream_lte(const struct inode *inode, unsigned stream_idx,
335 const struct lookup_table *table)
338 return inode_stream_lte_resolved(inode, stream_idx);
340 return inode_stream_lte_unresolved(inode, stream_idx, table);
344 static inline const u8 *inode_stream_hash_unresolved(const struct inode *inode,
347 wimlib_assert(!inode->resolved);
348 wimlib_assert(stream_idx <= inode->num_ads);
352 return inode->ads_entries[stream_idx - 1].hash;
356 static inline const u8 *inode_stream_hash_resolved(const struct inode *inode,
359 struct lookup_table_entry *lte;
360 lte = inode_stream_lte_resolved(inode, stream_idx);
368 * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
369 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
370 * alternate data stream.
372 * This works for both resolved and un-resolved dentries.
374 static inline const u8 *inode_stream_hash(const struct inode *inode,
378 return inode_stream_hash_resolved(inode, stream_idx);
380 return inode_stream_hash_unresolved(inode, stream_idx);
383 static inline u16 inode_stream_name_len(const struct inode *inode,
386 wimlib_assert(stream_idx <= inode->num_ads);
390 return inode->ads_entries[stream_idx - 1].stream_name_len;
393 static inline struct lookup_table_entry *
394 inode_unnamed_lte_resolved(const struct inode *inode)
396 wimlib_assert(inode->resolved);
397 for (unsigned i = 0; i <= inode->num_ads; i++)
398 if (inode_stream_name_len(inode, i) == 0 &&
399 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
400 return inode_stream_lte_resolved(inode, i);
404 static inline struct lookup_table_entry *
405 inode_unnamed_lte_unresolved(const struct inode *inode,
406 const struct lookup_table *table)
408 wimlib_assert(!inode->resolved);
409 for (unsigned i = 0; i <= inode->num_ads; i++)
410 if (inode_stream_name_len(inode, i) == 0 &&
411 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
412 return inode_stream_lte_unresolved(inode, i, table);
416 extern struct lookup_table_entry *
417 inode_unnamed_lte(const struct inode *inode,
418 const struct lookup_table *table);