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