1 #ifndef _WIMLIB_BLOB_TABLE_H
2 #define _WIMLIB_BLOB_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 a blob's data is located. */
12 /* The blob's data does not exist. This is a temporary state only. */
15 /* The blob's data is available in the WIM resource identified by the
16 * `struct wim_resource_descriptor' pointed to by @rdesc.
17 * @offset_in_res identifies the offset at which this particular blob
18 * begins in the uncompressed data of the resource. */
21 /* The blob's data is available as the contents of the file named by
25 /* The blob's data is available as the contents of the in-memory buffer
26 * pointed to by @attached_buffer. */
27 BLOB_IN_ATTACHED_BUFFER,
30 /* The blob's data is available as the contents of the file with name
31 * @staging_file_name relative to the open directory file descriptor
37 /* The blob's data is available as the contents of an NTFS attribute
38 * accessible through libntfs-3g. @ntfs_loc points to a structure which
39 * identifies the attribute. */
44 /* Windows only: the blob's data is available as the contents of the
45 * data stream named by @file_on_disk. @file_on_disk is an NT namespace
46 * path that may be longer than the Win32-level MAX_PATH. Furthermore,
47 * the stream may require "backup semantics" to access. */
48 BLOB_IN_WINNT_FILE_ON_DISK,
50 /* Windows only: the blob's data is available as the raw encrypted data
51 * of the external file named by @file_on_disk. @file_on_disk is a
52 * Win32 namespace path. */
57 /* A "blob extraction target" is a stream, and the inode to which that stream
58 * belongs, to which a blob needs to be extracted as part of an extraction
59 * operation. Since blobs are single-instanced, a blob may have multiple
60 * extraction targets. */
61 struct blob_extraction_target {
62 struct wim_inode *inode;
63 struct wim_inode_stream *stream;
67 * Descriptor for a "blob", which is a known length sequence of binary data.
69 * Within a WIM file, blobs are single instanced and are identified by SHA-1
72 struct blob_descriptor {
74 /* List node for a hash bucket of the blob table */
75 struct hlist_node hash_list;
78 * Uncompressed size of this blob.
80 * In most cases we are now enforcing that this is nonzero; i.e. an
81 * empty stream will have "no blob" rather than "an empty blob". The
84 * - blob descriptors with 'blob_location == BLOB_NONEXISTENT',
85 * e.g. placeholder entries for new metadata resources or for
86 * blobs required for pipable WIM extraction. In these cases the
87 * size is not meaningful information anyway.
88 * - blob descriptors with 'blob_location == BLOB_IN_STAGING_FILE'
89 * can vary their size over time, including to 0.
95 * For unhashed == 0: 'hash' is the SHA-1 message digest of the
96 * blob's data. 'hash_short' allows accessing just a prefix of
97 * the SHA-1 message digest, which is useful for getting a "hash
98 * code" for hash table lookup/insertion.
100 u8 hash[SHA1_HASH_SIZE];
103 /* For unhashed == 1: these variables make it possible to find
104 * the stream that references this blob. There can be at most
105 * one such reference, since duplicate blobs can only be joined
106 * after they have been hashed. */
108 struct wim_inode *back_inode;
111 } _packed_attribute; /* union is SHA1_HASH_SIZE bytes */
113 /* Number of times this blob is referenced by file streams in WIM
114 * images. See blob_decrement_refcnt() for information about the
115 * limitations of this field. */
119 * When a WIM file is written, this is set to the number of references
120 * (from file streams) to this blob in the output WIM file.
122 * During extraction, this is set to the number of targets to which this
123 * blob is being extracted.
125 * During image export, this is set to the number of references of this
126 * blob that originated from the source WIM.
128 * When mounting a WIM image read-write, this is set to the number of
129 * extra references to this blob preemptively taken to allow later
130 * saving the modified image as a new image and leaving the original
136 /* Number of open file descriptors to this blob during a FUSE mount of
141 /* One of the `enum blob_location' values documented above. */
142 u16 blob_location : 4;
144 /* 1 iff this blob contains "metadata" as opposed to data. */
147 /* 1 iff the SHA-1 message digest of this blob is unknown. */
150 /* Temporary fields used when writing blobs; set as documented for
151 * prepare_blob_list_for_write(). */
153 u16 will_be_in_output_wim : 1;
155 /* Set to 1 if this blob represents a metadata resource that has been
156 * changed. In such cases, the hash cannot be used to verify the data
157 * if the metadata resource is read again. (This could be avoided if we
158 * used separate fields for input/output checksum, but most blobs
159 * wouldn't need this.) */
160 u16 dont_check_metadata_hash : 1;
162 u16 may_send_done_with_file : 1;
164 /* Only used by wimlib_export_image() */
165 u16 was_exported : 1;
167 /* Specification of where this blob's data is located. Which member of
168 * this union is valid is determined by the @blob_location field. */
172 struct wim_resource_descriptor *rdesc;
175 /* Links together blobs that share the same underlying
176 * WIM resource. The head is rdesc->blob_list. */
177 struct list_head rdesc_node;
184 /* BLOB_IN_FILE_ON_DISK
185 * BLOB_IN_WINNT_FILE_ON_DISK
186 * BLOB_WIN32_ENCRYPTED */
189 struct wim_inode *file_inode;
195 /* BLOB_IN_ATTACHED_BUFFER */
196 void *attached_buffer;
199 /* BLOB_IN_STAGING_FILE */
201 char *staging_file_name;
207 /* BLOB_IN_NTFS_VOLUME */
208 struct ntfs_location *ntfs_loc;
212 /* List link for per-WIM-image list of unhashed blobs */
213 struct list_head unhashed_list;
217 /* Temporary fields */
219 /* Fields used temporarily during WIM file writing. */
222 /* List node used for blob size table. */
223 struct hlist_node hash_list_2;
225 /* Metadata for the underlying solid resource in
226 * the WIM being written (only valid if
227 * WIM_RESHDR_FLAG_SOLID set in
228 * out_reshdr.flags). */
230 u64 out_res_offset_in_wim;
231 u64 out_res_size_in_wim;
232 u64 out_res_uncompressed_size;
236 /* Links blobs being written to the WIM. */
237 struct list_head write_blobs_list;
240 /* Metadata for this blob in the WIM being
242 struct wim_reshdr out_reshdr;
245 /* Name under which this blob is being
246 * sorted; used only when sorting blobs
247 * for solid compression. */
248 utf16lechar *solid_sort_name;
249 size_t solid_sort_name_nbytes;
254 /* Used temporarily during extraction. This is an array of
255 * references to the streams being extracted that use this blob.
256 * out_refcnt tracks the number of slots filled. */
258 struct blob_extraction_target inline_blob_extraction_targets[3];
260 struct blob_extraction_target *blob_extraction_targets;
261 u32 alloc_blob_extraction_targets;
266 /* Temporary list fields. */
268 /* Links blobs for writing blob table. */
269 struct list_head blob_table_list;
271 /* Links blobs being extracted. */
272 struct list_head extraction_list;
274 /* Links blobs being exported. */
275 struct list_head export_blob_list;
277 /* Links original list of blobs in the read-write mounted image. */
278 struct list_head orig_blob_list;
282 extern struct blob_table *
283 new_blob_table(size_t capacity) _malloc_attribute;
286 free_blob_table(struct blob_table *table);
289 read_blob_table(WIMStruct *wim);
292 write_blob_table_from_blob_list(struct list_head *blob_list,
293 struct filedes *out_fd,
295 struct wim_reshdr *out_reshdr,
296 int write_resource_flags);
298 extern struct blob_descriptor *
299 new_blob_descriptor(void) _malloc_attribute;
301 extern struct blob_descriptor *
302 clone_blob_descriptor(const struct blob_descriptor *blob) _malloc_attribute;
305 blob_decrement_refcnt(struct blob_descriptor *blob, struct blob_table *table);
308 blob_subtract_refcnt(struct blob_descriptor *blob, struct blob_table *table,
313 blob_decrement_num_opened_fds(struct blob_descriptor *blob);
317 free_blob_descriptor(struct blob_descriptor *blob);
320 blob_table_insert(struct blob_table *table, struct blob_descriptor *blob);
323 blob_table_unlink(struct blob_table *table, struct blob_descriptor *blob);
325 extern struct blob_descriptor *
326 lookup_blob(const struct blob_table *table, const u8 *hash);
329 for_blob_in_table(struct blob_table *table,
330 int (*visitor)(struct blob_descriptor *, void *), void *arg);
333 for_blob_in_table_sorted_by_sequential_order(struct blob_table *table,
334 int (*visitor)(struct blob_descriptor *, void *),
337 struct wimlib_resource_entry;
340 blob_to_wimlib_resource_entry(const struct blob_descriptor *blob,
341 struct wimlib_resource_entry *wentry);
344 sort_blob_list(struct list_head *blob_list, size_t list_head_offset,
345 int (*compar)(const void *, const void*));
348 sort_blob_list_by_sequential_order(struct list_head *blob_list,
349 size_t list_head_offset);
352 cmp_blobs_by_sequential_order(const void *p1, const void *p2);
354 static inline const struct blob_extraction_target *
355 blob_extraction_targets(const struct blob_descriptor *blob)
357 if (blob->out_refcnt <= ARRAY_LEN(blob->inline_blob_extraction_targets))
358 return blob->inline_blob_extraction_targets;
360 return blob->blob_extraction_targets;
364 * Declare that the specified blob is located in the specified WIM resource at
365 * the specified offset. The caller is expected to set blob->size if required.
368 blob_set_is_located_in_wim_resource(struct blob_descriptor *blob,
369 struct wim_resource_descriptor *rdesc,
372 blob->blob_location = BLOB_IN_WIM;
374 list_add_tail(&blob->rdesc_node, &rdesc->blob_list);
375 blob->offset_in_res = offset_in_res;
379 blob_unset_is_located_in_wim_resource(struct blob_descriptor *blob)
381 list_del(&blob->rdesc_node);
382 blob->blob_location = BLOB_NONEXISTENT;
386 blob_set_is_located_in_attached_buffer(struct blob_descriptor *blob,
387 void *buffer, size_t size)
389 blob->blob_location = BLOB_IN_ATTACHED_BUFFER;
390 blob->attached_buffer = buffer;
394 extern struct blob_descriptor *
395 new_blob_from_data_buffer(const void *buffer, size_t size,
396 struct blob_table *blob_table);
398 extern struct blob_descriptor *
399 after_blob_hashed(struct blob_descriptor *blob,
400 struct blob_descriptor **back_ptr,
401 struct blob_table *blob_table);
404 hash_unhashed_blob(struct blob_descriptor *blob, struct blob_table *blob_table,
405 struct blob_descriptor **blob_ret);
407 extern struct blob_descriptor **
408 retrieve_pointer_to_unhashed_blob(struct blob_descriptor *blob);
411 prepare_unhashed_blob(struct blob_descriptor *blob,
412 struct wim_inode *back_inode, u32 stream_id,
413 struct list_head *unhashed_blobs)
418 blob->back_inode = back_inode;
419 blob->back_stream_id = stream_id;
420 list_add_tail(&blob->unhashed_list, unhashed_blobs);
423 #endif /* _WIMLIB_BLOB_TABLE_H */