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 */
53 struct hlist_node hash_list;
55 /* Location and size of the stream in the WIM, whether it is compressed
56 * or not, and whether it's a metadata resource or not. This is an
58 struct resource_entry resource_entry;
60 /* Specifies which part of the split WIM the resource is located in.
61 * This is on on-disk field.
63 * In stand-alone WIMs, this must be 1.
65 * In split WIMs, every split WIM part has its own lookup table, and in
66 * read_lookup_table() it's currently expected that the part number of
67 * each lookup table entry in a split WIM part's lookup table is the
68 * same as the part number of that split WIM part. So this makes this
69 * field redundant since we store a pointer to the corresponding
70 * WIMStruct in the lookup table entry anyway.
74 /* An enumerated type that identifies where the stream corresponding to
75 * this lookup table entry is actually located.
77 * Obviously if we open a WIM and read its lookup table, the location is
78 * set to RESOURCE_IN_WIM since all the streams will initially be
79 * located in the WIM. However, to deal with problems such as image
80 * capture and image mount, we allow the actual location of the stream
81 * to be somewhere else, such as an external file.
84 /* The lookup table entry does not correspond to a stream (this
85 * state should exist only temporarily) */
86 RESOURCE_NONEXISTENT = 0,
88 /* The stream resource is located in a WIM file. The WIMStruct
89 * for the WIM file will be pointed to by the @wim member. */
92 /* The stream resource is located in an external file. The
93 * name of the file will be provided by @file_on_disk member.
94 * In addition, if @file_on_disk_fp is not NULL, it will be an
95 * open FILE * to the file. */
96 RESOURCE_IN_FILE_ON_DISK,
98 /* The stream resource is located in an external file in the
99 * staging directory for a read-write mount. */
100 RESOURCE_IN_STAGING_FILE,
102 /* The stream resource is directly attached in an in-memory
103 * buffer pointed to by @attached_buffer. */
104 RESOURCE_IN_ATTACHED_BUFFER,
106 /* The stream resource is located in an NTFS volume. It is
107 * identified by volume, filename, data stream name, and by
108 * whether it is a reparse point or not. @ntfs_loc points to a
109 * structure containing this information. */
110 RESOURCE_IN_NTFS_VOLUME,
114 * Number of times this lookup table entry is referenced by dentries.
115 * Unfortunately, this field is not always set correctly in Microsoft's
116 * WIMs, so we have no choice but to fix it if more references to the
117 * lookup table entry are found than stated here. */
121 /* (On-disk field) SHA1 message digest of the stream referenced
122 * by this lookup table entry */
123 u8 hash[SHA1_HASH_SIZE];
125 /* First 4 or 8 bytes of the SHA1 message digest, used for
126 * inserting the entry into the hash table. Since the SHA1
127 * message digest can be considered random, we don't really need
128 * the full 20 byte hash just to insert the entry in a hash
133 /* Pointers to somewhere where the stream is actually located. See the
134 * comments for the @resource_location field above. */
138 char *staging_file_name;
141 struct ntfs_location *ntfs_loc;
145 /* Temporary field for creating a singly linked list. Shouldn't
147 struct lookup_table_entry *next_lte_in_swm;
149 /* @file_on_disk_fp and @attr are both used to cache file/stream
150 * handles so we don't have re-open them on every read */
151 FILE *file_on_disk_fp;
153 struct _ntfs_attr *attr;
157 /* File descriptors table for this data stream. This is used if the WIM
158 * is mounted. Basically, each time a file is open()ed, a new file
159 * descriptor is added here, and each time a file is close()ed, the file
160 * descriptor is gotten rid of. If the stream is opened for writing, it
161 * will be extracted to the staging directory and there will be an
162 * actual native file descriptor associated with each "wimlib file
165 u16 num_allocated_fds;
166 struct wimlib_fd **fds;
169 /* When a WIM file is written, out_refcnt starts at 0 and is incremented
170 * whenever the file resource pointed to by this lookup table entry
171 * needs to be written. The file resource only need to be written when
172 * out_refcnt is nonzero, since otherwise it is not referenced by any
177 /* When a WIM file is written, @output_resource_entry is filled
178 * in with the resource entry for the output WIM. This will not
179 * necessarily be the same as the @resource_entry since:
180 * - The stream may have a different offset in the new WIM
181 * - The stream may have a different compressed size in the
182 * new WIM if the compression type changed
184 struct resource_entry output_resource_entry;
186 /* This field is used for the special hardlink or symlink image
187 * application mode. In these mode, all identical files are
188 * linked together, and @extracted_file will be set to the
189 * filename of the first extracted file containing this stream.
191 char *extracted_file;
194 /* Circular linked list of streams that share the same lookup table
197 * This list of streams may include streams from different hard link
198 * sets that happen to be the same. */
199 struct list_head lte_group_list;
201 /* List of lookup table entries that correspond to streams that have
202 * been extracted to the staging directory when modifying a read-write
204 struct list_head staging_list;
207 static inline u64 wim_resource_size(const struct lookup_table_entry *lte)
209 return lte->resource_entry.original_size;
213 wim_resource_compressed_size(const struct lookup_table_entry *lte)
215 return lte->resource_entry.size;
219 * XXX Probably should store the compression type directly in the lookup table
223 wim_resource_compression_type(const struct lookup_table_entry *lte)
225 if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
226 || lte->resource_location != RESOURCE_IN_WIM)
227 return WIM_COMPRESSION_TYPE_NONE;
228 return wimlib_get_compression_type(lte->wim);
232 extern struct lookup_table *new_lookup_table(size_t capacity);
234 extern void lookup_table_insert(struct lookup_table *table,
235 struct lookup_table_entry *lte);
237 /* Unlinks a lookup table entry from the table; does not free it. */
238 static inline void lookup_table_unlink(struct lookup_table *table,
239 struct lookup_table_entry *lte)
241 hlist_del(<e->hash_list);
242 table->num_entries--;
246 extern struct lookup_table_entry *
247 lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[]);
249 extern struct lookup_table_entry *
250 lte_decrement_refcnt(struct lookup_table_entry *lte,
251 struct lookup_table *table);
254 extern struct lookup_table_entry *new_lookup_table_entry();
256 extern int for_lookup_table_entry(struct lookup_table *table,
257 int (*visitor)(struct lookup_table_entry *, void *),
260 extern struct lookup_table_entry *
261 __lookup_resource(const struct lookup_table *table, const u8 hash[]);
263 extern int lookup_resource(WIMStruct *w, const char *path,
264 int lookup_flags, struct dentry **dentry_ret,
265 struct lookup_table_entry **lte_ret,
266 unsigned *stream_idx_ret);
268 extern int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore);
270 extern void print_lookup_table_entry(const struct lookup_table_entry *entry);
272 extern int read_lookup_table(WIMStruct *w);
274 extern void free_lookup_table(struct lookup_table *table);
276 extern int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out);
278 extern void free_lookup_table_entry(struct lookup_table_entry *lte);
280 extern int dentry_resolve_ltes(struct dentry *dentry, void *__table);
282 /* Writes the lookup table to the output file. */
283 static inline int write_lookup_table(struct lookup_table *table, FILE *out)
285 return for_lookup_table_entry(table, write_lookup_table_entry, out);
288 /* Unlinks and frees an entry from a lookup table. */
289 static inline void lookup_table_remove(struct lookup_table *table,
290 struct lookup_table_entry *lte)
292 lookup_table_unlink(table, lte);
293 free_lookup_table_entry(lte);
296 static inline struct resource_entry* wim_metadata_resource_entry(WIMStruct *w)
298 return &w->image_metadata[
299 w->current_image - 1].metadata_lte->resource_entry;
302 static inline struct lookup_table_entry *
303 dentry_stream_lte_resolved(const struct dentry *dentry, unsigned stream_idx)
305 wimlib_assert(dentry->resolved);
306 wimlib_assert(stream_idx <= dentry->num_ads);
310 return dentry->ads_entries[stream_idx - 1].lte;
313 static inline struct lookup_table_entry *
314 dentry_stream_lte_unresolved(const struct dentry *dentry, unsigned stream_idx,
315 const struct lookup_table *table)
317 wimlib_assert(!dentry->resolved);
318 wimlib_assert(stream_idx <= dentry->num_ads);
322 return __lookup_resource(table, dentry->hash);
324 return __lookup_resource(table,
326 stream_idx - 1].hash);
329 * Returns the lookup table entry for stream @stream_idx of the dentry, where
330 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
331 * corresponds to an alternate data stream.
333 * This works for both resolved and un-resolved dentries.
335 static inline struct lookup_table_entry *
336 dentry_stream_lte(const struct dentry *dentry, unsigned stream_idx,
337 const struct lookup_table *table)
339 if (dentry->resolved)
340 return dentry_stream_lte_resolved(dentry, stream_idx);
342 return dentry_stream_lte_unresolved(dentry, stream_idx, table);
346 static inline const u8 *dentry_stream_hash_unresolved(const struct dentry *dentry,
349 wimlib_assert(!dentry->resolved);
350 wimlib_assert(stream_idx <= dentry->num_ads);
354 return dentry->ads_entries[stream_idx - 1].hash;
357 static inline u16 dentry_stream_name_len(const struct dentry *dentry,
360 wimlib_assert(stream_idx <= dentry->num_ads);
364 return dentry->ads_entries[stream_idx - 1].stream_name_len;
367 static inline const u8 *dentry_stream_hash_resolved(const struct dentry *dentry,
370 struct lookup_table_entry *lte;
371 lte = dentry_stream_lte_resolved(dentry, stream_idx);
379 * Returns the hash for stream @stream_idx of the dentry, where stream_idx = 0
380 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
381 * alternate data stream.
383 * This works for both resolved and un-resolved dentries.
385 static inline const u8 *dentry_stream_hash(const struct dentry *dentry,
388 if (dentry->resolved)
389 return dentry_stream_hash_resolved(dentry, stream_idx);
391 return dentry_stream_hash_unresolved(dentry, stream_idx);
394 static inline struct lookup_table_entry *
395 dentry_unnamed_lte_resolved(const struct dentry *dentry)
397 wimlib_assert(dentry->resolved);
398 for (unsigned i = 0; i <= dentry->num_ads; i++)
399 if (dentry_stream_name_len(dentry, i) == 0 &&
400 !is_zero_hash(dentry_stream_hash_resolved(dentry, i)))
401 return dentry_stream_lte_resolved(dentry, i);
405 static inline struct lookup_table_entry *
406 dentry_unnamed_lte_unresolved(const struct dentry *dentry,
407 const struct lookup_table *table)
409 wimlib_assert(!dentry->resolved);
410 for (unsigned i = 0; i <= dentry->num_ads; i++)
411 if (dentry_stream_name_len(dentry, i) == 0 &&
412 !is_zero_hash(dentry_stream_hash_unresolved(dentry, i)))
413 return dentry_stream_lte_unresolved(dentry, i, table);
417 extern struct lookup_table_entry *
418 dentry_unnamed_lte(const struct dentry *dentry,
419 const struct lookup_table *table);