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 located in a 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. The attribute is identified by
39 * volume, path to an inode, attribute name, and attribute type.
40 * @ntfs_loc points to a structure containing this information. */
45 /* Windows only: the blob's data is available as the contents of the
46 * data stream named by @file_on_disk. @file_on_disk is an NT namespace
47 * path that may be longer than the Win32-level MAX_PATH. Furthermore,
48 * the stream may require "backup semantics" to access. */
49 BLOB_IN_WINNT_FILE_ON_DISK,
51 /* Windows only: the blob's data is available as the raw encrypted data
52 * of the external file named by @file_on_disk. @file_on_disk is a
53 * Win32 namespace path. */
58 /* A "blob target" is a stream, and the inode to which that stream belongs, to
59 * which a blob needs to be extracted as part of an extraction operation. Since
60 * blobs are single-instanced, a blob may have multiple 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;
77 /* Uncompressed size of this blob */
82 * For unhashed == 0: 'hash' is the SHA-1 message digest of the
83 * blob's data. 'hash_short' allows accessing just a prefix of
84 * the SHA-1 message digest, which is useful for getting a "hash
85 * code" for hash table lookup/insertion.
87 u8 hash[SHA1_HASH_SIZE];
90 /* For unhashed == 1: these variables make it possible to find
91 * the stream that references this blob. There can be at most
92 * one such reference, since duplicate blobs can only be joined
93 * after they have been hashed. */
95 struct wim_inode *back_inode;
100 /* Number of times this blob is referenced by file streams in WIM
101 * images. See blob_decrement_refcnt() for information about the
102 * limitations of this field. */
106 * When a WIM file is written, this is set to the number of references
107 * (from file streams) to this blob in the output WIM file.
109 * During extraction, this is set to the number of targets to which this
110 * blob is being extracted.
112 * During image export, this is set to the number of references of this
113 * blob that originated from the source WIM.
115 * When mounting a WIM image read-write, this is set to the number of
116 * extra references to this blob preemptively taken to allow later
117 * saving the modified image as a new image and leaving the original
123 /* Number of open file descriptors to this blob during a FUSE mount of
124 * the containing image. */
128 /* One of the `enum blob_location' values documented above. */
129 u16 blob_location : 4;
131 /* 1 iff this blob contains "metadata" as opposed to data. */
134 /* 1 iff the SHA-1 message digest of this blob is unknown. */
137 /* Temporary fields used when writing blobs; set as documented for
138 * prepare_blob_list_for_write(). */
140 u16 will_be_in_output_wim : 1;
142 /* Set to 1 if this blob represents a metadata resource that has been
143 * changed. In such cases, the hash cannot be used to verify the data
144 * if the metadata resource is read again. (This could be avoided if we
145 * used separate fields for input/output checksum, but most blobs
146 * wouldn't need this.) */
147 u16 dont_check_metadata_hash : 1;
149 u16 may_send_done_with_file : 1;
151 /* Only used by wimlib_export_image() */
152 u16 was_exported : 1;
154 /* Specification of where this blob's data is located. Which member of
155 * this union is valid is determined by the @blob_location field. */
159 struct wim_resource_descriptor *rdesc;
162 /* Links together blobs that share the same underlying
163 * WIM resource. The head is rdesc->blob_list. */
164 struct list_head rdesc_node;
171 /* BLOB_IN_FILE_ON_DISK
172 * BLOB_IN_WINNT_FILE_ON_DISK
173 * BLOB_WIN32_ENCRYPTED */
176 struct wim_inode *file_inode;
179 /* BLOB_IN_ATTACHED_BUFFER */
180 void *attached_buffer;
183 /* BLOB_IN_STAGING_FILE */
185 char *staging_file_name;
191 /* BLOB_IN_NTFS_VOLUME */
192 struct ntfs_location *ntfs_loc;
196 /* List link for per-WIM-image list of unhashed blobs */
197 struct list_head unhashed_list;
201 /* Temporary fields */
203 /* Fields used temporarily during WIM file writing. */
206 /* List node used for blob size table. */
207 struct hlist_node hash_list_2;
209 /* Metadata for the underlying solid resource in
210 * the WIM being written (only valid if
211 * WIM_RESHDR_FLAG_SOLID set in
212 * out_reshdr.flags). */
214 u64 out_res_offset_in_wim;
215 u64 out_res_size_in_wim;
216 u64 out_res_uncompressed_size;
220 /* Links blobs being written to the WIM. */
221 struct list_head write_blobs_list;
224 /* Metadata for this blob in the WIM being
226 struct wim_reshdr out_reshdr;
229 /* Name under which this blob is being
230 * sorted; used only when sorting blobs
231 * for solid compression. */
232 utf16lechar *solid_sort_name;
233 size_t solid_sort_name_nbytes;
238 /* Used temporarily during extraction. This is an array of
239 * references to the streams being extracted that use this blob.
240 * out_refcnt tracks the number of slots filled. */
242 struct blob_extraction_target inline_blob_extraction_targets[3];
244 struct blob_extraction_target *blob_extraction_targets;
245 u32 alloc_blob_extraction_targets;
250 /* Temporary list fields. */
252 /* Links blobs for writing blob table. */
253 struct list_head blob_table_list;
255 /* Links blobs being extracted. */
256 struct list_head extraction_list;
258 /* Links blobs being exported. */
259 struct list_head export_blob_list;
261 /* Links original list of blobs in the read-write mounted image. */
262 struct list_head orig_blob_list;
266 extern struct blob_table *
267 new_blob_table(size_t capacity) _malloc_attribute;
270 free_blob_table(struct blob_table *table);
273 read_blob_table(WIMStruct *wim);
276 write_blob_table_from_blob_list(struct list_head *blob_list,
277 struct filedes *out_fd,
279 struct wim_reshdr *out_reshdr,
280 int write_resource_flags);
282 extern struct blob_descriptor *
283 new_blob_descriptor(void) _malloc_attribute;
285 extern struct blob_descriptor *
286 clone_blob_descriptor(const struct blob_descriptor *blob) _malloc_attribute;
289 blob_decrement_refcnt(struct blob_descriptor *blob, struct blob_table *table);
292 blob_subtract_refcnt(struct blob_descriptor *blob, struct blob_table *table,
297 blob_decrement_num_opened_fds(struct blob_descriptor *blob);
301 free_blob_descriptor(struct blob_descriptor *blob);
304 blob_table_insert(struct blob_table *table, struct blob_descriptor *blob);
307 blob_table_unlink(struct blob_table *table, struct blob_descriptor *blob);
309 extern struct blob_descriptor *
310 lookup_blob(const struct blob_table *table, const u8 *hash);
313 for_blob_in_table(struct blob_table *table,
314 int (*visitor)(struct blob_descriptor *, void *), void *arg);
317 for_blob_in_table_sorted_by_sequential_order(struct blob_table *table,
318 int (*visitor)(struct blob_descriptor *, void *),
321 struct wimlib_resource_entry;
324 blob_to_wimlib_resource_entry(const struct blob_descriptor *blob,
325 struct wimlib_resource_entry *wentry);
328 sort_blob_list(struct list_head *blob_list,
329 size_t list_head_offset,
330 int (*compar)(const void *, const void*));
333 sort_blob_list_by_sequential_order(struct list_head *blob_list,
334 size_t list_head_offset);
337 cmp_blobs_by_sequential_order(const void *p1, const void *p2);
339 static inline const struct blob_extraction_target *
340 blob_extraction_targets(struct blob_descriptor *blob)
342 if (blob->out_refcnt <= ARRAY_LEN(blob->inline_blob_extraction_targets))
343 return blob->inline_blob_extraction_targets;
345 return blob->blob_extraction_targets;
349 blob_set_is_located_in_wim_resource(struct blob_descriptor *blob,
350 struct wim_resource_descriptor *rdesc)
352 blob->blob_location = BLOB_IN_WIM;
354 list_add_tail(&blob->rdesc_node, &rdesc->blob_list);
358 blob_unset_is_located_in_wim_resource(struct blob_descriptor *blob)
360 list_del(&blob->rdesc_node);
361 blob->blob_location = BLOB_NONEXISTENT;
364 extern struct blob_descriptor *
365 new_blob_from_data_buffer(const void *buffer, size_t size,
366 struct blob_table *blob_table);
368 extern struct blob_descriptor *
369 after_blob_hashed(struct blob_descriptor *blob,
370 struct blob_descriptor **back_ptr,
371 struct blob_table *blob_table);
374 hash_unhashed_blob(struct blob_descriptor *blob,
375 struct blob_table *blob_table,
376 struct blob_descriptor **blob_ret);
378 extern struct blob_descriptor **
379 retrieve_pointer_to_unhashed_blob(struct blob_descriptor *blob);
382 prepare_unhashed_blob(struct blob_descriptor *blob,
383 struct wim_inode *back_inode, u32 stream_id,
384 struct list_head *unhashed_blobs)
389 blob->back_inode = back_inode;
390 blob->back_stream_id = stream_id;
391 list_add_tail(&blob->unhashed_list, unhashed_blobs);
394 #endif /* _WIMLIB_BLOB_TABLE_H */