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