]> wimlib.net Git - wimlib/blob - include/wimlib/lookup_table.h
Workaround for WOF incompatibility
[wimlib] / include / wimlib / lookup_table.h
1 #ifndef _WIMLIB_LOOKUP_TABLE_H
2 #define _WIMLIB_LOOKUP_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 the stream corresponding to this
10  * lookup table entry is actually located.
11  *
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
16  * file.  */
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,
21
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
27          * streams.  */
28         RESOURCE_IN_WIM,
29
30         /* The stream is located in the external file named by @file_on_disk.
31          */
32         RESOURCE_IN_FILE_ON_DISK,
33
34         /* The stream is directly attached in the in-memory buffer pointed to by
35          * @attached_buffer.  */
36         RESOURCE_IN_ATTACHED_BUFFER,
37
38 #ifdef WITH_FUSE
39         /* The stream is located in the external file named by
40          * @staging_file_name, located in the staging directory for a read-write
41          * mount.  */
42         RESOURCE_IN_STAGING_FILE,
43 #endif
44
45 #ifdef WITH_NTFS_3G
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.
49          * */
50         RESOURCE_IN_NTFS_VOLUME,
51 #endif
52
53 #ifdef __WIN32__
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,
58
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,
63 #endif
64 };
65
66 struct stream_owner {
67         struct wim_inode *inode;
68         const utf16lechar *stream_name;
69 };
70
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.
73  *
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 {
80
81         /* List node for a hash bucket of the lookup table.  */
82         struct hlist_node hash_list;
83
84         /* Uncompressed size of this stream.  */
85         u64 size;
86
87         /* Stream flags (WIM_RESHDR_FLAG_*).  */
88         u32 flags : 8;
89
90         /* One of the `enum resource_location' values documented above.  */
91         u32 resource_location : 4;
92
93         /* 1 if this stream has not had a SHA1 message digest calculated for it
94          * yet.  */
95         u32 unhashed : 1;
96
97         /* Temoorary fields used when writing streams; set as documented for
98          * prepare_stream_list_for_write().  */
99         u32 unique_size : 1;
100         u32 will_be_in_output_wim : 1;
101
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
106          * need this.)  */
107         u32 dont_check_metadata_hash : 1;
108
109         u32 may_send_done_with_file : 1;
110
111         union {
112                 /* (On-disk field) SHA1 message digest of the stream referenced
113                  * by this lookup table entry.  */
114                 u8  hash[SHA1_HASH_SIZE];
115
116                 /* First 4 or 8 bytes of the SHA1 message digest, used for
117                  * inserting the entry into the hash table.  Since the SHA1
118                  * message digest can be considered random, we don't really need
119                  * the full 20 byte hash just to insert the entry in a hash
120                  * table.  */
121                 size_t hash_short;
122
123                 /* Unhashed entries only (unhashed == 1): these variables make
124                  * it possible to find the pointer to this 'struct
125                  * wim_lookup_table_entry' contained in either 'struct
126                  * wim_ads_entry' or 'struct wim_inode'.  There can be at most 1
127                  * such pointer, as we can only join duplicate streams after
128                  * they have been hashed.  */
129                 struct {
130                         struct wim_inode *back_inode;
131                         u32 back_stream_id;
132                 };
133         };
134
135         /* Number of times this lookup table entry is referenced by dentries in
136          * the WIM.  When a WIM's lookup table is read, this field is
137          * initialized from a corresponding entry; while it should be correct,
138          * in general it may not be.  wim_recalculate_refcnts() recalculates the
139          * reference counts for all streams and is run before doing any
140          * deletions.  */
141         u32 refcnt;
142
143         /* When a WIM file is written, this is set to the number of references
144          * (by dentries) to this stream in the output WIM file.
145          *
146          * During extraction, this is the number of slots in stream_owners (or
147          * inline_stream_owners) that have been filled.
148          *
149          * During image export, this is set to the number of references of this
150          * stream that originated from the source WIM.
151          *
152          * When mounting a WIM image read-write, this is set to the number of
153          * extra references to this stream preemptively taken to allow later
154          * saving the modified image as a new image and leaving the original
155          * image alone.  */
156         u32 out_refcnt;
157
158 #ifdef WITH_FUSE
159         /* Number of open file descriptors to this stream during a FUSE mount of
160          * the containing image.  */
161         u16 num_opened_fds;
162 #endif
163
164         /* Specification of where this stream is actually located.  Which member
165          * is valid is determined by the @resource_location field.  */
166         union {
167                 struct {
168                         struct wim_resource_spec *rspec;
169                         u64 offset_in_res;
170                 };
171                 struct {
172                         tchar *file_on_disk;
173                         struct wim_inode *file_inode;
174                 };
175                 void *attached_buffer;
176         #ifdef WITH_FUSE
177                 struct {
178                         char *staging_file_name;
179                         int staging_dir_fd;
180                 };
181         #endif
182         #ifdef WITH_NTFS_3G
183                 struct ntfs_location *ntfs_loc;
184         #endif
185         };
186
187         /* Links together streams that share the same underlying WIM resource.
188          * The head is the `stream_list' member of `struct wim_resource_spec'.
189          */
190         struct list_head rspec_node;
191
192         /* Temporary fields  */
193         union {
194                 /* Fields used temporarily during WIM file writing.  */
195                 struct {
196                         union {
197                                 /* List node used for stream size table.  */
198                                 struct hlist_node hash_list_2;
199
200                                 /* Metadata for the underlying packed resource
201                                  * in the WIM being written (only valid if
202                                  * WIM_RESHDR_FLAG_PACKED_STREAMS set in
203                                  * out_reshdr.flags).  */
204                                 struct {
205                                         u64 out_res_offset_in_wim;
206                                         u64 out_res_size_in_wim;
207                                         u64 out_res_uncompressed_size;
208                                 };
209                         };
210
211                         /* Links streams being written to the WIM.  */
212                         struct list_head write_streams_list;
213
214                         /* Metadata for this stream in the WIM being written.
215                          */
216                         struct wim_reshdr out_reshdr;
217                 };
218
219                 /* Used temporarily during extraction.  This is an array of
220                  * pointers to the inodes being extracted that use this stream.
221                  */
222                 union {
223                         /* Inodes to extract that reference this stream.
224                          * out_refcnt tracks the number of slots filled.  */
225                         struct stream_owner inline_stream_owners[3];
226                         struct {
227                                 struct stream_owner *stream_owners;
228                                 u32 alloc_stream_owners;
229                         };
230                 };
231
232                 /* Actual reference count to this stream (only used while
233                  * verifying an image).  */
234                 u32 real_refcnt;
235         };
236
237         /* Temporary list fields.  */
238         union {
239                 /* Links streams for writing lookup table.  */
240                 struct list_head lookup_table_list;
241
242                 /* Links streams being extracted.  */
243                 struct list_head extraction_list;
244
245                 /* Links streams being exported.  */
246                 struct list_head export_stream_list;
247
248                 /* Links original list of streams in the read-write mounted image.  */
249                 struct list_head orig_stream_list;
250         };
251
252         /* Links streams that are still unhashed after being been added to a
253          * WIM.  */
254         struct list_head unhashed_list;
255 };
256
257 /* Functions to allocate and free lookup tables  */
258
259 extern struct wim_lookup_table *
260 new_lookup_table(size_t capacity) _malloc_attribute;
261
262 extern void
263 free_lookup_table(struct wim_lookup_table *table);
264
265 /* Functions to read or write the lookup table from/to a WIM file  */
266
267 extern int
268 read_wim_lookup_table(WIMStruct *wim);
269
270 extern int
271 write_wim_lookup_table_from_stream_list(struct list_head *stream_list,
272                                         struct filedes *out_fd,
273                                         u16 part_number,
274                                         struct wim_reshdr *out_reshdr,
275                                         int write_resource_flags);
276
277 /* Functions to create, clone, print, and free lookup table entries  */
278
279 extern struct wim_lookup_table_entry *
280 new_lookup_table_entry(void) _malloc_attribute;
281
282 extern struct wim_lookup_table_entry *
283 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte)
284                         _malloc_attribute;
285
286 extern void
287 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
288                      struct wim_lookup_table *table);
289 #ifdef WITH_FUSE
290 extern void
291 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
292 #endif
293
294 extern void
295 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
296
297 /* Functions to insert and delete entries from a lookup table  */
298
299 extern void
300 lookup_table_insert(struct wim_lookup_table *table,
301                 struct wim_lookup_table_entry *lte);
302
303 extern void
304 lookup_table_unlink(struct wim_lookup_table *table,
305                     struct wim_lookup_table_entry *lte);
306
307 /* Function to lookup a stream by SHA1 message digest  */
308 extern struct wim_lookup_table_entry *
309 lookup_stream(const struct wim_lookup_table *table, const u8 hash[]);
310
311 /* Functions to iterate through the entries of a lookup table  */
312
313 extern int
314 for_lookup_table_entry(struct wim_lookup_table *table,
315                        int (*visitor)(struct wim_lookup_table_entry *, void *),
316                        void *arg);
317
318 extern int
319 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
320                                   int (*visitor)(struct wim_lookup_table_entry *,
321                                                  void *),
322                                   void *arg);
323
324
325
326 /* Function to get a resource entry in stable format  */
327
328 struct wimlib_resource_entry;
329
330 extern void
331 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
332                              struct wimlib_resource_entry *wentry);
333
334 /* Functions to sort a list of lookup table entries  */
335 extern int
336 sort_stream_list(struct list_head *stream_list,
337                  size_t list_head_offset,
338                  int (*compar)(const void *, const void*));
339
340 extern int
341 sort_stream_list_by_sequential_order(struct list_head *stream_list,
342                                      size_t list_head_offset);
343
344 /* Utility functions  */
345
346 extern int
347 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *ignore);
348
349 extern int
350 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *ignore);
351
352 static inline bool
353 lte_is_partial(const struct wim_lookup_table_entry * lte)
354 {
355         return lte->resource_location == RESOURCE_IN_WIM &&
356                lte->size != lte->rspec->uncompressed_size;
357 }
358
359 static inline const struct stream_owner *
360 stream_owners(struct wim_lookup_table_entry *stream)
361 {
362         if (stream->out_refcnt <= ARRAY_LEN(stream->inline_stream_owners))
363                 return stream->inline_stream_owners;
364         else
365                 return stream->stream_owners;
366 }
367
368 static inline void
369 lte_bind_wim_resource_spec(struct wim_lookup_table_entry *lte,
370                            struct wim_resource_spec *rspec)
371 {
372         lte->resource_location = RESOURCE_IN_WIM;
373         lte->rspec = rspec;
374         list_add_tail(&lte->rspec_node, &rspec->stream_list);
375 }
376
377 static inline void
378 lte_unbind_wim_resource_spec(struct wim_lookup_table_entry *lte)
379 {
380         list_del(&lte->rspec_node);
381         lte->resource_location = RESOURCE_NONEXISTENT;
382 }
383
384 extern void
385 lte_put_resource(struct wim_lookup_table_entry *lte);
386
387 extern struct wim_lookup_table_entry *
388 new_stream_from_data_buffer(const void *buffer, size_t size,
389                             struct wim_lookup_table *lookup_table);
390
391 static inline void
392 add_unhashed_stream(struct wim_lookup_table_entry *lte,
393                     struct wim_inode *back_inode,
394                     u32 back_stream_id,
395                     struct list_head *unhashed_streams)
396 {
397         lte->unhashed = 1;
398         lte->back_inode = back_inode;
399         lte->back_stream_id = back_stream_id;
400         list_add_tail(&lte->unhashed_list, unhashed_streams);
401 }
402
403 extern int
404 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
405                      struct wim_lookup_table *lookup_table,
406                      struct wim_lookup_table_entry **lte_ret);
407
408 extern struct wim_lookup_table_entry **
409 retrieve_lte_pointer(struct wim_lookup_table_entry *lte);
410
411 #endif /* _WIMLIB_LOOKUP_TABLE_H */