]> wimlib.net Git - wimlib/blob - include/wimlib/blob_table.h
dcf6af7ffaa2c2a44870b1e3d8eb6bb55e8caf6b
[wimlib] / include / wimlib / blob_table.h
1 #ifndef _WIMLIB_BLOB_TABLE_H
2 #define _WIMLIB_BLOB_TABLE_H
3
4 #include "wimlib/list.h"
5 #include "wimlib/resource.h"
6 #include "wimlib/sha1.h"
7 #include "wimlib/types.h"
8
9 /* An enumerated type that identifies where a blob's data is located.  */
10 enum blob_location {
11
12         /* The blob's data does not exist.  This is a temporary state only.  */
13         BLOB_NONEXISTENT = 0,
14
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.  */
19         BLOB_IN_WIM,
20
21         /* The blob's data is available as the contents of the file named by
22          * @file_on_disk.  */
23         BLOB_IN_FILE_ON_DISK,
24
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,
28
29 #ifdef WITH_FUSE
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
32          * @staging_dir_fd.  */
33         BLOB_IN_STAGING_FILE,
34 #endif
35
36 #ifdef WITH_NTFS_3G
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.  */
40         BLOB_IN_NTFS_VOLUME,
41 #endif
42
43 #ifdef __WIN32__
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,
49
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.  */
53         BLOB_WIN32_ENCRYPTED,
54 #endif
55 };
56
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;
64 };
65
66 /*
67  * Descriptor for a "blob", which is a known length sequence of binary data.
68  *
69  * Within a WIM file, blobs are single instanced and are identified by SHA-1
70  * message digest.
71  */
72 struct blob_descriptor {
73
74         /* List node for a hash bucket of the blob table  */
75         struct hlist_node hash_list;
76
77         /* Uncompressed size of this blob  */
78         u64 size;
79
80         union {
81                 /*
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.
86                  */
87                 u8 hash[SHA1_HASH_SIZE];
88                 size_t hash_short;
89
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.  */
94                 struct {
95                         struct wim_inode *back_inode;
96                         u32 back_stream_id;
97                 };
98         };
99
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.  */
103         u32 refcnt;
104
105         /*
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.
108          *
109          * During extraction, this is set to the number of targets to which this
110          * blob is being extracted.
111          *
112          * During image export, this is set to the number of references of this
113          * blob that originated from the source WIM.
114          *
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
118          * image alone.
119          */
120         u32 out_refcnt;
121
122 #ifdef WITH_FUSE
123         /* Number of open file descriptors to this blob during a FUSE mount of
124          * a WIM image.  */
125         u16 num_opened_fds;
126 #endif
127
128         /* One of the `enum blob_location' values documented above.  */
129         u16 blob_location : 4;
130
131         /* 1 iff this blob contains "metadata" as opposed to data.  */
132         u16 is_metadata : 1;
133
134         /* 1 iff the SHA-1 message digest of this blob is unknown.  */
135         u16 unhashed : 1;
136
137         /* Temporary fields used when writing blobs; set as documented for
138          * prepare_blob_list_for_write().  */
139         u16 unique_size : 1;
140         u16 will_be_in_output_wim : 1;
141
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;
148
149         u16 may_send_done_with_file : 1;
150
151         /* Only used by wimlib_export_image() */
152         u16 was_exported : 1;
153
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.  */
156         union {
157                 /* BLOB_IN_WIM  */
158                 struct {
159                         struct wim_resource_descriptor *rdesc;
160                         u64 offset_in_res;
161
162                         /* Links together blobs that share the same underlying
163                          * WIM resource.  The head is rdesc->blob_list.  */
164                         struct list_head rdesc_node;
165                 };
166
167                 struct {
168
169                         union {
170
171                                 /* BLOB_IN_FILE_ON_DISK
172                                  * BLOB_IN_WINNT_FILE_ON_DISK
173                                  * BLOB_WIN32_ENCRYPTED  */
174                                 struct {
175                                         tchar *file_on_disk;
176                                         struct wim_inode *file_inode;
177                                 #ifdef __WIN32__
178                                         u64 sort_key;
179                                 #endif
180                                 };
181
182                                 /* BLOB_IN_ATTACHED_BUFFER */
183                                 void *attached_buffer;
184
185                         #ifdef WITH_FUSE
186                                 /* BLOB_IN_STAGING_FILE  */
187                                 struct {
188                                         char *staging_file_name;
189                                         int staging_dir_fd;
190                                 };
191                         #endif
192
193                         #ifdef WITH_NTFS_3G
194                                 /* BLOB_IN_NTFS_VOLUME  */
195                                 struct ntfs_location *ntfs_loc;
196                         #endif
197                         };
198
199                         /* List link for per-WIM-image list of unhashed blobs */
200                         struct list_head unhashed_list;
201                 };
202         };
203
204         /* Temporary fields  */
205         union {
206                 /* Fields used temporarily during WIM file writing.  */
207                 struct {
208                         union {
209                                 /* List node used for blob size table.  */
210                                 struct hlist_node hash_list_2;
211
212                                 /* Metadata for the underlying solid resource in
213                                  * the WIM being written (only valid if
214                                  * WIM_RESHDR_FLAG_SOLID set in
215                                  * out_reshdr.flags).  */
216                                 struct {
217                                         u64 out_res_offset_in_wim;
218                                         u64 out_res_size_in_wim;
219                                         u64 out_res_uncompressed_size;
220                                 };
221                         };
222
223                         /* Links blobs being written to the WIM.  */
224                         struct list_head write_blobs_list;
225
226                         union {
227                                 /* Metadata for this blob in the WIM being
228                                  * written.  */
229                                 struct wim_reshdr out_reshdr;
230
231                                 struct {
232                                         /* Name under which this blob is being
233                                          * sorted; used only when sorting blobs
234                                          * for solid compression.  */
235                                         utf16lechar *solid_sort_name;
236                                         size_t solid_sort_name_nbytes;
237                                 };
238                         };
239                 };
240
241                 /* Used temporarily during extraction.  This is an array of
242                  * references to the streams being extracted that use this blob.
243                  * out_refcnt tracks the number of slots filled.  */
244                 union {
245                         struct blob_extraction_target inline_blob_extraction_targets[3];
246                         struct {
247                                 struct blob_extraction_target *blob_extraction_targets;
248                                 u32 alloc_blob_extraction_targets;
249                         };
250                 };
251         };
252
253         /* Temporary list fields.  */
254         union {
255                 /* Links blobs for writing blob table.  */
256                 struct list_head blob_table_list;
257
258                 /* Links blobs being extracted.  */
259                 struct list_head extraction_list;
260
261                 /* Links blobs being exported.  */
262                 struct list_head export_blob_list;
263
264                 /* Links original list of blobs in the read-write mounted image.  */
265                 struct list_head orig_blob_list;
266         };
267 };
268
269 extern struct blob_table *
270 new_blob_table(size_t capacity) _malloc_attribute;
271
272 extern void
273 free_blob_table(struct blob_table *table);
274
275 extern int
276 read_blob_table(WIMStruct *wim);
277
278 extern int
279 write_blob_table_from_blob_list(struct list_head *blob_list,
280                                 struct filedes *out_fd,
281                                 u16 part_number,
282                                 struct wim_reshdr *out_reshdr,
283                                 int write_resource_flags);
284
285 extern struct blob_descriptor *
286 new_blob_descriptor(void) _malloc_attribute;
287
288 extern struct blob_descriptor *
289 clone_blob_descriptor(const struct blob_descriptor *blob) _malloc_attribute;
290
291 extern void
292 blob_decrement_refcnt(struct blob_descriptor *blob, struct blob_table *table);
293
294 extern void
295 blob_subtract_refcnt(struct blob_descriptor *blob, struct blob_table *table,
296                      u32 count);
297
298 #ifdef WITH_FUSE
299 extern void
300 blob_decrement_num_opened_fds(struct blob_descriptor *blob);
301 #endif
302
303 extern void
304 free_blob_descriptor(struct blob_descriptor *blob);
305
306 extern void
307 blob_table_insert(struct blob_table *table, struct blob_descriptor *blob);
308
309 extern void
310 blob_table_unlink(struct blob_table *table, struct blob_descriptor *blob);
311
312 extern struct blob_descriptor *
313 lookup_blob(const struct blob_table *table, const u8 *hash);
314
315 extern int
316 for_blob_in_table(struct blob_table *table,
317                   int (*visitor)(struct blob_descriptor *, void *), void *arg);
318
319 extern int
320 for_blob_in_table_sorted_by_sequential_order(struct blob_table *table,
321                                              int (*visitor)(struct blob_descriptor *, void *),
322                                              void *arg);
323
324 struct wimlib_resource_entry;
325
326 extern void
327 blob_to_wimlib_resource_entry(const struct blob_descriptor *blob,
328                               struct wimlib_resource_entry *wentry);
329
330 extern int
331 sort_blob_list(struct list_head *blob_list,
332                size_t list_head_offset,
333                int (*compar)(const void *, const void*));
334
335 extern int
336 sort_blob_list_by_sequential_order(struct list_head *blob_list,
337                                    size_t list_head_offset);
338
339 extern int
340 cmp_blobs_by_sequential_order(const void *p1, const void *p2);
341
342 static inline const struct blob_extraction_target *
343 blob_extraction_targets(struct blob_descriptor *blob)
344 {
345         if (blob->out_refcnt <= ARRAY_LEN(blob->inline_blob_extraction_targets))
346                 return blob->inline_blob_extraction_targets;
347         else
348                 return blob->blob_extraction_targets;
349 }
350
351 /*
352  * Declare that the specified blob is located in the specified WIM resource at
353  * the specified offset.  The caller is expected to set blob->size if required.
354  */
355 static inline void
356 blob_set_is_located_in_wim_resource(struct blob_descriptor *blob,
357                                     struct wim_resource_descriptor *rdesc,
358                                     u64 offset_in_res)
359 {
360         blob->blob_location = BLOB_IN_WIM;
361         blob->rdesc = rdesc;
362         list_add_tail(&blob->rdesc_node, &rdesc->blob_list);
363         blob->offset_in_res = offset_in_res;
364 }
365
366 /*
367  * Declare that the specified blob is located in the specified non-solid WIM
368  * resource.  In this case, the blob data is the entire uncompressed resource.
369  */
370 static inline void
371 blob_set_is_located_in_nonsolid_wim_resource(struct blob_descriptor *blob,
372                                              struct wim_resource_descriptor *rdesc)
373 {
374         blob_set_is_located_in_wim_resource(blob, rdesc, 0);
375         blob->size = rdesc->uncompressed_size;
376 }
377
378 static inline void
379 blob_unset_is_located_in_wim_resource(struct blob_descriptor *blob)
380 {
381         list_del(&blob->rdesc_node);
382         blob->blob_location = BLOB_NONEXISTENT;
383 }
384
385 static inline void
386 blob_set_is_located_in_attached_buffer(struct blob_descriptor *blob,
387                                        void *buffer, size_t size)
388 {
389         blob->blob_location = BLOB_IN_ATTACHED_BUFFER;
390         blob->attached_buffer = buffer;
391         blob->size = size;
392 }
393
394 extern struct blob_descriptor *
395 new_blob_from_data_buffer(const void *buffer, size_t size,
396                           struct blob_table *blob_table);
397
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);
402
403 extern int
404 hash_unhashed_blob(struct blob_descriptor *blob,
405                    struct blob_table *blob_table,
406                    struct blob_descriptor **blob_ret);
407
408 extern struct blob_descriptor **
409 retrieve_pointer_to_unhashed_blob(struct blob_descriptor *blob);
410
411 static inline void
412 prepare_unhashed_blob(struct blob_descriptor *blob,
413                       struct wim_inode *back_inode, u32 stream_id,
414                       struct list_head *unhashed_blobs)
415 {
416         if (!blob)
417                 return;
418         blob->unhashed = 1;
419         blob->back_inode = back_inode;
420         blob->back_stream_id = stream_id;
421         list_add_tail(&blob->unhashed_list, unhashed_blobs);
422 }
423
424 #endif /* _WIMLIB_BLOB_TABLE_H */