1 #ifndef _WIMLIB_LOOKUP_TABLE_H
2 #define _WIMLIB_LOOKUP_TABLE_H
4 #include "wimlib/list.h"
5 #include "wimlib/resource.h"
6 #include "wimlib/sha1.h"
7 #include "wimlib/types.h"
9 /* An enumerated type that identifies where the stream corresponding to this
10 * lookup table entry is actually located.
12 * If we open a WIM and read its lookup table, the location is set to
13 * RESOURCE_IN_WIM since all the streams will initially be located in the WIM.
14 * However, to handle situations such as image capture and image mount, we allow
15 * the actual location of the stream to be somewhere else, such as an external
17 enum resource_location {
18 /* The lookup table entry does not yet correspond to a stream; this is a
19 * temporary state only. */
20 RESOURCE_NONEXISTENT = 0,
22 /* The stream is located in a resource in a WIM file identified by the
23 * `struct wim_resource_spec' pointed to by @rspec. @offset_in_res
24 * identifies the offset at which this particular stream begins in the
25 * uncompressed data of the resource; this is normally 0, but a WIM
26 * resource can be "solid" and contain multiple streams. */
29 /* The stream is located in the external file named by @file_on_disk.
31 RESOURCE_IN_FILE_ON_DISK,
33 /* The stream is directly attached in the in-memory buffer pointed to by
34 * @attached_buffer. */
35 RESOURCE_IN_ATTACHED_BUFFER,
38 /* The stream is located in the external file named by
39 * @staging_file_name, located in the staging directory for a read-write
41 RESOURCE_IN_STAGING_FILE,
45 /* The stream is located in an NTFS volume. It is identified by volume,
46 * filename, data stream name, and by whether it is a reparse point or
47 * not. @ntfs_loc points to a structure containing this information.
49 RESOURCE_IN_NTFS_VOLUME,
53 /* Windows only: the stream is located in the external file named by
54 * @file_on_disk, which is in the Windows NT namespace and may specify a
55 * named data stream. */
56 RESOURCE_IN_WINNT_FILE_ON_DISK,
58 /* Windows only: the stream is located in the external file named by
59 * @file_on_disk, but the file is encrypted and must be read using the
60 * appropriate Windows API. */
61 RESOURCE_WIN32_ENCRYPTED,
66 struct wim_inode *inode;
67 const utf16lechar *stream_name;
70 /* Specification for a stream, which may be the contents of a file (unnamed data
71 * stream), a named data stream, reparse point data, or a WIM metadata resource.
73 * One instance of this structure is created for each entry in the WIM's lookup
74 * table, hence the name of the struct. Each of these entries contains the SHA1
75 * message digest of a stream and the location of the stream data in the WIM
76 * file (size, location, flags). The in-memory lookup table is a map from SHA1
77 * message digests to stream locations. */
78 struct wim_lookup_table_entry {
80 /* List node for a hash bucket of the lookup table. */
81 struct hlist_node hash_list;
83 /* Uncompressed size of this stream. */
86 /* Stream flags (WIM_RESHDR_FLAG_*). */
89 /* One of the `enum resource_location' values documented above. */
90 u32 resource_location : 4;
92 /* 1 if this stream has not had a SHA1 message digest calculated for it
96 /* Temoorary fields used when writing streams; set as documented for
97 * prepare_stream_list_for_write(). */
99 u32 will_be_in_output_wim : 1;
101 /* Set to 1 when a metadata entry has its checksum changed; in such
102 * cases the hash cannot be used to verify the data if the metadata
103 * resource is read again. (This could be avoided if we used separate
104 * fields for input/output checksum, but most stream entries wouldn't
106 u32 dont_check_metadata_hash : 1;
108 u32 may_send_done_with_file : 1;
110 /* Only used by wimlib_export_image() */
111 u32 was_exported : 1;
114 /* (On-disk field) SHA1 message digest of the stream referenced
115 * by this lookup table entry. */
116 u8 hash[SHA1_HASH_SIZE];
118 /* First 4 or 8 bytes of the SHA1 message digest, used for
119 * inserting the entry into the hash table. Since the SHA1
120 * message digest can be considered random, we don't really need
121 * the full 20 byte hash just to insert the entry in a hash
125 /* Unhashed entries only (unhashed == 1): these variables make
126 * it possible to find the pointer to this 'struct
127 * wim_lookup_table_entry' contained in either 'struct
128 * wim_ads_entry' or 'struct wim_inode'. There can be at most 1
129 * such pointer, as we can only join duplicate streams after
130 * they have been hashed. */
132 struct wim_inode *back_inode;
137 /* Number of times this lookup table entry is referenced by dentries in
138 * the WIM. When a WIM's lookup table is read, this field is
139 * initialized from a corresponding entry.
141 * However, see lte_decrement_refcnt() for information about the
142 * limitations of this field. */
145 /* When a WIM file is written, this is set to the number of references
146 * (by dentries) to this stream in the output WIM file.
148 * During extraction, this is the number of slots in stream_owners (or
149 * inline_stream_owners) that have been filled.
151 * During image export, this is set to the number of references of this
152 * stream that originated from the source WIM.
154 * When mounting a WIM image read-write, this is set to the number of
155 * extra references to this stream preemptively taken to allow later
156 * saving the modified image as a new image and leaving the original
161 /* Number of open file descriptors to this stream during a FUSE mount of
162 * the containing image. */
166 /* Specification of where this stream is actually located. Which member
167 * is valid is determined by the @resource_location field. */
170 struct wim_resource_spec *rspec;
175 struct wim_inode *file_inode;
177 void *attached_buffer;
180 char *staging_file_name;
185 struct ntfs_location *ntfs_loc;
189 /* Links together streams that share the same underlying WIM resource.
190 * The head is the `stream_list' member of `struct wim_resource_spec'.
192 struct list_head rspec_node;
194 /* Temporary fields */
196 /* Fields used temporarily during WIM file writing. */
199 /* List node used for stream size table. */
200 struct hlist_node hash_list_2;
202 /* Metadata for the underlying solid resource in
203 * the WIM being written (only valid if
204 * WIM_RESHDR_FLAG_SOLID set in
205 * out_reshdr.flags). */
207 u64 out_res_offset_in_wim;
208 u64 out_res_size_in_wim;
209 u64 out_res_uncompressed_size;
213 /* Links streams being written to the WIM. */
214 struct list_head write_streams_list;
217 /* Metadata for this stream in the WIM being
219 struct wim_reshdr out_reshdr;
222 /* Name under which this stream is being
223 * sorted; used only when sorting
224 * streams for solid compression. */
225 utf16lechar *solid_sort_name;
226 size_t solid_sort_name_nbytes;
231 /* Used temporarily during extraction. This is an array of
232 * pointers to the inodes being extracted that use this stream.
235 /* Inodes to extract that reference this stream.
236 * out_refcnt tracks the number of slots filled. */
237 struct stream_owner inline_stream_owners[3];
239 struct stream_owner *stream_owners;
240 u32 alloc_stream_owners;
245 /* Temporary list fields. */
247 /* Links streams for writing lookup table. */
248 struct list_head lookup_table_list;
250 /* Links streams being extracted. */
251 struct list_head extraction_list;
253 /* Links streams being exported. */
254 struct list_head export_stream_list;
256 /* Links original list of streams in the read-write mounted image. */
257 struct list_head orig_stream_list;
260 /* Links streams that are still unhashed after being been added to a
262 struct list_head unhashed_list;
265 /* Functions to allocate and free lookup tables */
267 extern struct wim_lookup_table *
268 new_lookup_table(size_t capacity) _malloc_attribute;
271 free_lookup_table(struct wim_lookup_table *table);
273 /* Functions to read or write the lookup table from/to a WIM file */
276 read_wim_lookup_table(WIMStruct *wim);
279 write_wim_lookup_table_from_stream_list(struct list_head *stream_list,
280 struct filedes *out_fd,
282 struct wim_reshdr *out_reshdr,
283 int write_resource_flags);
285 /* Functions to create, clone, print, and free lookup table entries */
287 extern struct wim_lookup_table_entry *
288 new_lookup_table_entry(void) _malloc_attribute;
290 extern struct wim_lookup_table_entry *
291 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte)
295 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
296 struct wim_lookup_table *table);
299 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
303 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
305 /* Functions to insert and delete entries from a lookup table */
308 lookup_table_insert(struct wim_lookup_table *table,
309 struct wim_lookup_table_entry *lte);
312 lookup_table_unlink(struct wim_lookup_table *table,
313 struct wim_lookup_table_entry *lte);
315 /* Function to lookup a stream by SHA1 message digest */
316 extern struct wim_lookup_table_entry *
317 lookup_stream(const struct wim_lookup_table *table, const u8 hash[]);
319 /* Functions to iterate through the entries of a lookup table */
322 for_lookup_table_entry(struct wim_lookup_table *table,
323 int (*visitor)(struct wim_lookup_table_entry *, void *),
327 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
328 int (*visitor)(struct wim_lookup_table_entry *,
334 /* Function to get a resource entry in stable format */
336 struct wimlib_resource_entry;
339 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
340 struct wimlib_resource_entry *wentry);
342 /* Functions to sort a list of lookup table entries */
344 sort_stream_list(struct list_head *stream_list,
345 size_t list_head_offset,
346 int (*compar)(const void *, const void*));
349 sort_stream_list_by_sequential_order(struct list_head *stream_list,
350 size_t list_head_offset);
353 cmp_streams_by_sequential_order(const void *p1, const void *p2);
355 /* Utility functions */
358 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *ignore);
361 lte_is_partial(const struct wim_lookup_table_entry * lte)
363 return lte->resource_location == RESOURCE_IN_WIM &&
364 lte->size != lte->rspec->uncompressed_size;
367 static inline const struct stream_owner *
368 stream_owners(struct wim_lookup_table_entry *stream)
370 if (stream->out_refcnt <= ARRAY_LEN(stream->inline_stream_owners))
371 return stream->inline_stream_owners;
373 return stream->stream_owners;
377 lte_bind_wim_resource_spec(struct wim_lookup_table_entry *lte,
378 struct wim_resource_spec *rspec)
380 lte->resource_location = RESOURCE_IN_WIM;
382 list_add_tail(<e->rspec_node, &rspec->stream_list);
386 lte_unbind_wim_resource_spec(struct wim_lookup_table_entry *lte)
388 list_del(<e->rspec_node);
389 lte->resource_location = RESOURCE_NONEXISTENT;
393 lte_put_resource(struct wim_lookup_table_entry *lte);
395 extern struct wim_lookup_table_entry *
396 new_stream_from_data_buffer(const void *buffer, size_t size,
397 struct wim_lookup_table *lookup_table);
400 add_unhashed_stream(struct wim_lookup_table_entry *lte,
401 struct wim_inode *back_inode,
403 struct list_head *unhashed_streams)
406 lte->back_inode = back_inode;
407 lte->back_stream_id = back_stream_id;
408 list_add_tail(<e->unhashed_list, unhashed_streams);
412 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
413 struct wim_lookup_table *lookup_table,
414 struct wim_lookup_table_entry **lte_ret);
416 extern struct wim_lookup_table_entry **
417 retrieve_lte_pointer(struct wim_lookup_table_entry *lte);
419 #endif /* _WIMLIB_LOOKUP_TABLE_H */