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 in general
26 * a WIM resource may be "packed" and potentially contain multiple
30 /* The stream is located in the external file named by @file_on_disk.
32 RESOURCE_IN_FILE_ON_DISK,
34 /* The stream is directly attached in the in-memory buffer pointed to by
35 * @attached_buffer. */
36 RESOURCE_IN_ATTACHED_BUFFER,
39 /* The stream is located in the external file named by
40 * @staging_file_name, located in the staging directory for a read-write
42 RESOURCE_IN_STAGING_FILE,
46 /* The stream is located in an NTFS volume. It is identified by volume,
47 * filename, data stream name, and by whether it is a reparse point or
48 * not. @ntfs_loc points to a structure containing this information.
50 RESOURCE_IN_NTFS_VOLUME,
54 /* Windows only: the stream is located in the external file named by
55 * @file_on_disk, which is in the Windows NT namespace and may specify a
56 * named data stream. */
57 RESOURCE_IN_WINNT_FILE_ON_DISK,
59 /* Windows only: the stream is located in the external file named by
60 * @file_on_disk, but the file is encrypted and must be read using the
61 * appropriate Windows API. */
62 RESOURCE_WIN32_ENCRYPTED,
67 struct wim_inode *inode;
68 const utf16lechar *stream_name;
71 /* Specification for a stream, which may be the contents of a file (unnamed data
72 * stream), a named data stream, reparse point data, or a WIM metadata resource.
74 * One instance of this structure is created for each entry in the WIM's lookup
75 * table, hence the name of the struct. Each of these entries contains the SHA1
76 * message digest of a stream and the location of the stream data in the WIM
77 * file (size, location, flags). The in-memory lookup table is a map from SHA1
78 * message digests to stream locations. */
79 struct wim_lookup_table_entry {
81 /* List node for a hash bucket of the lookup table. */
82 struct hlist_node hash_list;
84 /* Uncompressed size of this stream. */
87 /* Stream flags (WIM_RESHDR_FLAG_*). */
90 /* One of the `enum resource_location' values documented above. */
91 u32 resource_location : 4;
93 /* 1 if this stream has not had a SHA1 message digest calculated for it
97 /* Temoorary fields used when writing streams; set as documented for
98 * prepare_stream_list_for_write(). */
100 u32 will_be_in_output_wim : 1;
102 /* Set to 1 when a metadata entry has its checksum changed; in such
103 * cases the hash cannot be used to verify the data if the metadata
104 * resource is read again. (This could be avoided if we used separate
105 * fields for input/output checksum, but most stream entries wouldn't
107 u32 dont_check_metadata_hash : 1;
110 /* (On-disk field) SHA1 message digest of the stream referenced
111 * by this lookup table entry. */
112 u8 hash[SHA1_HASH_SIZE];
114 /* First 4 or 8 bytes of the SHA1 message digest, used for
115 * inserting the entry into the hash table. Since the SHA1
116 * message digest can be considered random, we don't really need
117 * the full 20 byte hash just to insert the entry in a hash
121 /* Unhashed entries only (unhashed == 1): these variables make
122 * it possible to find the pointer to this 'struct
123 * wim_lookup_table_entry' contained in either 'struct
124 * wim_ads_entry' or 'struct wim_inode'. There can be at most 1
125 * such pointer, as we can only join duplicate streams after
126 * they have been hashed. */
128 struct wim_inode *back_inode;
133 /* Number of times this lookup table entry is referenced by dentries in
134 * the WIM. When a WIM's lookup table is read, this field is
135 * initialized from a corresponding entry; while it should be correct,
136 * in general it may not be. wim_recalculate_refcnts() recalculates the
137 * reference counts for all streams and is run before doing any
141 /* When a WIM file is written, this is set to the number of references
142 * (by dentries) to this stream in the output WIM file.
144 * During extraction, this is the number of slots in stream_owners (or
145 * inline_stream_owners) that have been filled.
147 * During image export, this is set to the number of references of this
148 * stream that originated from the source WIM.
150 * When mounting a WIM image read-write, this is set to the number of
151 * extra references to this stream preemptively taken to allow later
152 * saving the modified image as a new image and leaving the original
157 /* Number of open file descriptors to this stream during a FUSE mount of
158 * the containing image. */
162 /* Specification of where this stream is actually located. Which member
163 * is valid is determined by the @resource_location field. */
166 struct wim_resource_spec *rspec;
170 void *attached_buffer;
173 char *staging_file_name;
178 struct ntfs_location *ntfs_loc;
182 /* Links together streams that share the same underlying WIM resource.
183 * The head is the `stream_list' member of `struct wim_resource_spec'.
185 struct list_head rspec_node;
187 /* Temporary fields */
189 /* Fields used temporarily during WIM file writing. */
192 /* List node used for stream size table. */
193 struct hlist_node hash_list_2;
195 /* Metadata for the underlying packed resource
196 * in the WIM being written (only valid if
197 * WIM_RESHDR_FLAG_PACKED_STREAMS set in
198 * out_reshdr.flags). */
200 u64 out_res_offset_in_wim;
201 u64 out_res_size_in_wim;
202 u64 out_res_uncompressed_size;
206 /* Links streams being written to the WIM. */
207 struct list_head write_streams_list;
209 /* Metadata for this stream in the WIM being written.
211 struct wim_reshdr out_reshdr;
214 /* Used temporarily during extraction. This is an array of
215 * pointers to the inodes being extracted that use this stream.
218 /* Inodes to extract that reference this stream.
219 * out_refcnt tracks the number of slots filled. */
220 struct stream_owner inline_stream_owners[3];
222 struct stream_owner *stream_owners;
223 u32 alloc_stream_owners;
227 /* Actual reference count to this stream (only used while
228 * verifying an image). */
232 /* Temporary list fields. */
234 /* Links streams for writing lookup table. */
235 struct list_head lookup_table_list;
237 /* Links streams being extracted. */
238 struct list_head extraction_list;
240 /* Links streams being exported. */
241 struct list_head export_stream_list;
243 /* Links original list of streams in the read-write mounted image. */
244 struct list_head orig_stream_list;
247 /* Links streams that are still unhashed after being been added to a
249 struct list_head unhashed_list;
252 /* Functions to allocate and free lookup tables */
254 extern struct wim_lookup_table *
255 new_lookup_table(size_t capacity) _malloc_attribute;
258 free_lookup_table(struct wim_lookup_table *table);
260 /* Functions to read or write the lookup table from/to a WIM file */
263 read_wim_lookup_table(WIMStruct *wim);
266 write_wim_lookup_table_from_stream_list(struct list_head *stream_list,
267 struct filedes *out_fd,
269 struct wim_reshdr *out_reshdr,
270 int write_resource_flags);
272 /* Functions to create, clone, print, and free lookup table entries */
274 extern struct wim_lookup_table_entry *
275 new_lookup_table_entry(void) _malloc_attribute;
277 extern struct wim_lookup_table_entry *
278 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte)
282 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
283 struct wim_lookup_table *table);
286 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
290 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
292 /* Functions to insert and delete entries from a lookup table */
295 lookup_table_insert(struct wim_lookup_table *table,
296 struct wim_lookup_table_entry *lte);
299 lookup_table_unlink(struct wim_lookup_table *table,
300 struct wim_lookup_table_entry *lte);
302 /* Function to lookup a stream by SHA1 message digest */
303 extern struct wim_lookup_table_entry *
304 lookup_stream(const struct wim_lookup_table *table, const u8 hash[]);
306 /* Functions to iterate through the entries of a lookup table */
309 for_lookup_table_entry(struct wim_lookup_table *table,
310 int (*visitor)(struct wim_lookup_table_entry *, void *),
314 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
315 int (*visitor)(struct wim_lookup_table_entry *,
321 /* Function to get a resource entry in stable format */
323 struct wimlib_resource_entry;
326 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
327 struct wimlib_resource_entry *wentry);
329 /* Functions to sort a list of lookup table entries */
331 sort_stream_list(struct list_head *stream_list,
332 size_t list_head_offset,
333 int (*compar)(const void *, const void*));
336 sort_stream_list_by_sequential_order(struct list_head *stream_list,
337 size_t list_head_offset);
339 /* Utility functions */
342 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *ignore);
345 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *ignore);
348 lte_is_partial(const struct wim_lookup_table_entry * lte)
350 return lte->resource_location == RESOURCE_IN_WIM &&
351 lte->size != lte->rspec->uncompressed_size;
354 static inline const struct stream_owner *
355 stream_owners(struct wim_lookup_table_entry *stream)
357 if (stream->out_refcnt <= ARRAY_LEN(stream->inline_stream_owners))
358 return stream->inline_stream_owners;
360 return stream->stream_owners;
364 lte_bind_wim_resource_spec(struct wim_lookup_table_entry *lte,
365 struct wim_resource_spec *rspec)
367 lte->resource_location = RESOURCE_IN_WIM;
369 list_add_tail(<e->rspec_node, &rspec->stream_list);
373 lte_unbind_wim_resource_spec(struct wim_lookup_table_entry *lte)
375 list_del(<e->rspec_node);
376 lte->resource_location = RESOURCE_NONEXISTENT;
380 lte_put_resource(struct wim_lookup_table_entry *lte);
382 extern struct wim_lookup_table_entry *
383 new_stream_from_data_buffer(const void *buffer, size_t size,
384 struct wim_lookup_table *lookup_table);
387 add_unhashed_stream(struct wim_lookup_table_entry *lte,
388 struct wim_inode *back_inode,
390 struct list_head *unhashed_streams)
393 lte->back_inode = back_inode;
394 lte->back_stream_id = back_stream_id;
395 list_add_tail(<e->unhashed_list, unhashed_streams);
399 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
400 struct wim_lookup_table *lookup_table,
401 struct wim_lookup_table_entry **lte_ret);
403 extern struct wim_lookup_table_entry **
404 retrieve_lte_pointer(struct wim_lookup_table_entry *lte);
406 #endif /* _WIMLIB_LOOKUP_TABLE_H */