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