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