]> wimlib.net Git - wimlib/blob - include/wimlib/lookup_table.h
aad400deb2d556584cd3a2704729e13c2e00cb41
[wimlib] / include / wimlib / lookup_table.h
1 #ifndef _WIMLIB_LOOKUP_TABLE_H
2 #define _WIMLIB_LOOKUP_TABLE_H
3
4 #include "wimlib/assert.h"
5 #include "wimlib/dentry.h"
6 #include "wimlib/list.h"
7 #include "wimlib/sha1.h"
8 #include "wimlib/types.h"
9 #include "wimlib/wim.h"
10
11 #define LOOKUP_FLAG_ADS_OK              0x00000001
12 #define LOOKUP_FLAG_DIRECTORY_OK        0x00000002
13
14
15 /* The lookup table of a WIM file maps SHA1 message digests to streams of data.
16  * Here, the in-memory structure is implemented as a hash table.
17  *
18  * Given a SHA1 message digest, the mapped-to stream is specified by an offset
19  * in the WIM, an uncompressed and compressed size, and resource flags (see
20  * 'struct resource_entry').  But, we associate additional information, such as
21  * a reference count, with each stream, so the actual mapping is from SHA1
22  * message digests to 'struct wim_lookup_table_entry's, each of which contains
23  * an embedded 'struct resource_entry'.
24  *
25  * Note: Everything will break horribly if there is a SHA1 collision.
26  */
27 struct wim_lookup_table {
28         struct hlist_head *array;
29         u64 num_entries;
30         u64 capacity;
31         struct list_head *unhashed_streams;
32 };
33
34 #ifdef WITH_NTFS_3G
35
36 struct _ntfs_volume;
37
38 struct ntfs_location {
39         tchar *path;
40         utf16lechar *stream_name;
41         u16 stream_name_nchars;
42         struct _ntfs_volume *ntfs_vol;
43         bool is_reparse_point;
44 };
45 #endif
46
47 /* An enumerated type that identifies where the stream corresponding to this
48  * lookup table entry is actually located.
49  *
50  * If we open a WIM and read its lookup table, the location is set to
51  * RESOURCE_IN_WIM since all the streams will initially be located in the WIM.
52  * However, to deal with problems such as image capture and image mount, we
53  * allow the actual location of the stream to be somewhere else, such as an
54  * external file.
55  */
56 enum resource_location {
57         /* The lookup table entry does not correspond to a stream (this state
58          * should exist only temporarily) */
59         RESOURCE_NONEXISTENT = 0,
60
61         /* The stream resource is located in a WIM file.  The WIMStruct for the
62          * WIM file will be pointed to by the @wim member. */
63         RESOURCE_IN_WIM,
64
65 #ifndef __WIN32__
66         /* The stream resource is located in an external file.  The name of the
67          * file will be provided by @file_on_disk member. */
68         RESOURCE_IN_FILE_ON_DISK,
69 #endif
70
71         /* The stream resource is directly attached in an in-memory buffer
72          * pointed to by @attached_buffer. */
73         RESOURCE_IN_ATTACHED_BUFFER,
74
75 #ifdef WITH_FUSE
76         /* The stream resource is located in an external file in the staging
77          * directory for a read-write mount.  */
78         RESOURCE_IN_STAGING_FILE,
79 #endif
80
81 #ifdef WITH_NTFS_3G
82         /* The stream resource is located in an NTFS volume.  It is identified
83          * by volume, filename, data stream name, and by whether it is a reparse
84          * point or not. @ntfs_loc points to a structure containing this
85          * information. */
86         RESOURCE_IN_NTFS_VOLUME,
87 #endif
88
89 #ifdef __WIN32__
90         /* Resource must be accessed using Win32 API (may be a named data
91          * stream) */
92         RESOURCE_WIN32,
93
94         /* Windows only: the file is on disk in the file named @file_on_disk,
95          * but the file is encrypted and must be read using special functions.
96          * */
97         RESOURCE_WIN32_ENCRYPTED,
98 #endif
99
100 };
101
102 /*
103  * An entry in the lookup table in the WIM file.
104  *
105  * It is used to find data streams for files in the WIM.
106  *
107  * Metadata resources and reparse point data buffers will also have lookup table
108  * entries associated with the data.
109  *
110  * The lookup_table_entry for a given dentry or alternate stream entry in the
111  * WIM is found using the SHA1 message digest field.
112  */
113 struct wim_lookup_table_entry {
114
115         /* List of lookup table entries in this hash bucket */
116         struct hlist_node hash_list;
117
118         /* Location and size of the stream in the WIM, whether it is compressed
119          * or not, and whether it's a metadata resource or not.  This is an
120          * on-disk field. */
121         struct resource_entry resource_entry;
122
123         /* Specifies which part of the split WIM the resource is located in.
124          * This is on on-disk field.
125          *
126          * In stand-alone WIMs, this must be 1.
127          *
128          * In split WIMs, every split WIM part has its own lookup table, and in
129          * read_lookup_table() it's currently expected that the part number of
130          * each lookup table entry in a split WIM part's lookup table is the
131          * same as the part number of that split WIM part.  So this makes this
132          * field redundant since we store a pointer to the corresponding
133          * WIMStruct in the lookup table entry anyway.
134          */
135         u16 part_number;
136
137         /* One of the `enum resource_location' values documented above. */
138         u16 resource_location : 5;
139
140         /* 1 if this stream is a unique size (only set while writing streams). */
141         u8 unique_size : 1;
142
143         /* 1 if this stream has not had a SHA1 message digest calculated for it
144          * yet */
145         u8 unhashed : 1;
146
147         u8 deferred : 1;
148
149         u8 no_progress : 1;
150
151         /* (On-disk field)
152          * Number of times this lookup table entry is referenced by dentries.
153          * Unfortunately, this field is not always set correctly in Microsoft's
154          * WIMs, so we have no choice but to fix it if more references to the
155          * lookup table entry are found than stated here. */
156         u32 refcnt;
157
158         union {
159                 /* (On-disk field) SHA1 message digest of the stream referenced
160                  * by this lookup table entry */
161                 u8  hash[SHA1_HASH_SIZE];
162
163                 /* First 4 or 8 bytes of the SHA1 message digest, used for
164                  * inserting the entry into the hash table.  Since the SHA1
165                  * message digest can be considered random, we don't really need
166                  * the full 20 byte hash just to insert the entry in a hash
167                  * table. */
168                 size_t hash_short;
169
170                 /* Unhashed entries only (unhashed == 1): these variables make
171                  * it possible to find the pointer to this 'struct
172                  * wim_lookup_table_entry' contained in either 'struct
173                  * wim_ads_entry' or 'struct wim_inode'.  There can be at most 1
174                  * such pointer, as we can only join duplicate streams after
175                  * they have been hashed.  */
176                 struct {
177                         struct wim_inode *back_inode;
178                         u32 back_stream_id;
179                 };
180         };
181
182         /* When a WIM file is written, out_refcnt starts at 0 and is incremented
183          * whenever the stream pointed to by this lookup table entry needs to be
184          * written.  The stream only need to be written when out_refcnt is
185          * nonzero, since otherwise it is not referenced by any dentries. */
186         u32 out_refcnt;
187
188         /* Pointers to somewhere where the stream is actually located.  See the
189          * comments for the @resource_location field above. */
190         union {
191                 WIMStruct *wim;
192                 tchar *file_on_disk;
193                 void *attached_buffer;
194         #ifdef WITH_FUSE
195                 tchar *staging_file_name;
196         #endif
197         #ifdef WITH_NTFS_3G
198                 struct ntfs_location *ntfs_loc;
199         #endif
200         };
201
202         /* Actual reference count to this stream (only used while
203          * verifying an image). */
204         u32 real_refcnt;
205
206         union {
207         #ifdef WITH_FUSE
208                 /* Number of times this stream has been opened (used only during
209                  * mounting) */
210                 u16 num_opened_fds;
211         #endif
212
213                 /* This field is used for the special hardlink or symlink image
214                  * extraction mode.   In these mode, all identical files are linked
215                  * together, and @extracted_file will be set to the filename of the
216                  * first extracted file containing this stream.  */
217                 tchar *extracted_file;
218         };
219
220         union {
221                 /* When a WIM file is written, @output_resource_entry is filled
222                  * in with the resource entry for the output WIM.  This will not
223                  * necessarily be the same as the @resource_entry since:
224                  * - The stream may have a different offset in the new WIM
225                  * - The stream may have a different compressed size in the new
226                  *   WIM if the compression type changed
227                  */
228                 struct resource_entry output_resource_entry;
229
230                 struct {
231                         struct list_head msg_list;
232                         struct list_head being_compressed_list;
233                 };
234                 struct list_head lte_dentry_list;
235
236                 struct {
237                         struct hlist_node hash_list_2;
238
239                         struct list_head write_streams_list;
240                 };
241         };
242
243         /* Temporary list fields */
244         union {
245                 struct list_head unhashed_list;
246                 struct list_head swm_stream_list;
247                 struct list_head lookup_table_list;
248                 struct list_head extraction_list;
249                 struct list_head export_stream_list;
250         };
251 };
252
253 static inline u64
254 wim_resource_size(const struct wim_lookup_table_entry *lte)
255 {
256         return lte->resource_entry.original_size;
257 }
258
259 static inline u64
260 wim_resource_chunks(const struct wim_lookup_table_entry *lte)
261 {
262         return DIV_ROUND_UP(wim_resource_size(lte), WIM_CHUNK_SIZE);
263 }
264
265 static inline u64
266 wim_resource_compressed_size(const struct wim_lookup_table_entry *lte)
267 {
268         return lte->resource_entry.size;
269 }
270
271 extern int
272 wim_resource_compression_type(const struct wim_lookup_table_entry *lte);
273
274 static inline bool
275 lte_filename_valid(const struct wim_lookup_table_entry *lte)
276 {
277         return 0
278         #ifdef __WIN32__
279                 || lte->resource_location == RESOURCE_WIN32
280                 || lte->resource_location == RESOURCE_WIN32_ENCRYPTED
281         #else
282                 || lte->resource_location == RESOURCE_IN_FILE_ON_DISK
283         #endif
284         #ifdef WITH_FUSE
285                 || lte->resource_location == RESOURCE_IN_STAGING_FILE
286         #endif
287                 ;
288 }
289
290 extern struct wim_lookup_table *
291 new_lookup_table(size_t capacity) _malloc_attribute;
292
293 extern int
294 read_lookup_table(WIMStruct *w);
295
296 extern int
297 write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry);
298
299 extern int
300 write_lookup_table_from_stream_list(struct list_head *stream_list,
301                                     int out_fd,
302                                     struct resource_entry *out_res_entry);
303
304 extern void
305 free_lookup_table(struct wim_lookup_table *table);
306
307 extern void
308 lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte);
309
310 /* Unlinks a lookup table entry from the table; does not free it. */
311 static inline void
312 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
313 {
314         wimlib_assert(!lte->unhashed);
315         hlist_del(&lte->hash_list);
316         wimlib_assert(table->num_entries != 0);
317         table->num_entries--;
318 }
319
320 extern struct wim_lookup_table_entry *
321 new_lookup_table_entry(void) _malloc_attribute;
322
323 extern struct wim_lookup_table_entry *
324 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte)
325                         _malloc_attribute;
326
327 extern void
328 print_lookup_table_entry(const struct wim_lookup_table_entry *entry,
329                          FILE *out);
330
331 extern void
332 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
333
334 extern int
335 for_lookup_table_entry(struct wim_lookup_table *table,
336                        int (*visitor)(struct wim_lookup_table_entry *, void *),
337                        void *arg);
338
339 extern int
340 cmp_streams_by_wim_position(const void *p1, const void *p2);
341
342 extern int
343 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
344                                   int (*visitor)(struct wim_lookup_table_entry *,
345                                                  void *),
346                                   void *arg);
347
348 extern struct wim_lookup_table_entry *
349 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
350
351 extern int
352 lookup_resource(WIMStruct *w, const tchar *path,
353                 int lookup_flags, struct wim_dentry **dentry_ret,
354                 struct wim_lookup_table_entry **lte_ret, u16 *stream_idx_ret);
355
356 extern void
357 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
358                      struct wim_lookup_table *table);
359 #ifdef WITH_FUSE
360 extern void
361 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
362 #endif
363
364 extern int
365 lte_zero_out_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
366
367 extern int
368 lte_zero_real_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
369
370 extern int
371 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
372
373 extern int
374 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table);
375
376 extern void
377 inode_unresolve_ltes(struct wim_inode *inode);
378
379 static inline struct wim_lookup_table_entry *
380 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
381 {
382         wimlib_assert(inode->i_resolved);
383         wimlib_assert(stream_idx <= inode->i_num_ads);
384         if (stream_idx == 0)
385                 return inode->i_lte;
386         else
387                 return inode->i_ads_entries[stream_idx - 1].lte;
388 }
389
390 static inline struct wim_lookup_table_entry *
391 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
392                             const struct wim_lookup_table *table)
393 {
394         wimlib_assert(!inode->i_resolved);
395         wimlib_assert(stream_idx <= inode->i_num_ads);
396         if (!table)
397                 return NULL;
398         if (stream_idx == 0)
399                 return __lookup_resource(table, inode->i_hash);
400         else
401                 return __lookup_resource(table,
402                                          inode->i_ads_entries[
403                                                 stream_idx - 1].hash);
404 }
405
406 extern struct wim_lookup_table_entry *
407 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
408                  const struct wim_lookup_table *table);
409
410 static inline const u8 *
411 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
412 {
413         wimlib_assert(!inode->i_resolved);
414         wimlib_assert(stream_idx <= inode->i_num_ads);
415         if (stream_idx == 0)
416                 return inode->i_hash;
417         else
418                 return inode->i_ads_entries[stream_idx - 1].hash;
419 }
420
421
422 static inline const u8 *
423 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
424 {
425         struct wim_lookup_table_entry *lte;
426         lte = inode_stream_lte_resolved(inode, stream_idx);
427         if (lte)
428                 return lte->hash;
429         else
430                 return zero_hash;
431 }
432
433 /*
434  * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
435  * means the default un-named file stream, and stream_idx >= 1 corresponds to an
436  * alternate data stream.
437  *
438  * This works for both resolved and un-resolved dentries.
439  */
440 static inline const u8 *
441 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
442 {
443         if (inode->i_resolved)
444                 return inode_stream_hash_resolved(inode, stream_idx);
445         else
446                 return inode_stream_hash_unresolved(inode, stream_idx);
447 }
448
449 static inline u16
450 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
451 {
452         wimlib_assert(stream_idx <= inode->i_num_ads);
453         if (stream_idx == 0)
454                 return 0;
455         else
456                 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
457 }
458
459 extern struct wim_lookup_table_entry *
460 inode_unnamed_lte_resolved(const struct wim_inode *inode);
461
462 extern struct wim_lookup_table_entry *
463 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
464                              const struct wim_lookup_table *table);
465
466 extern struct wim_lookup_table_entry *
467 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
468
469 extern u64
470 lookup_table_total_stream_size(struct wim_lookup_table *table);
471
472
473 static inline void
474 lookup_table_insert_unhashed(struct wim_lookup_table *table,
475                              struct wim_lookup_table_entry *lte,
476                              struct wim_inode *back_inode,
477                              u32 back_stream_id)
478 {
479         lte->unhashed = 1;
480         lte->back_inode = back_inode;
481         lte->back_stream_id = back_stream_id;
482         list_add_tail(&lte->unhashed_list, table->unhashed_streams);
483 }
484
485 extern int
486 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
487                      struct wim_lookup_table *lookup_table,
488                      struct wim_lookup_table_entry **lte_ret);
489
490 extern struct wim_lookup_table_entry **
491 retrieve_lte_pointer(struct wim_lookup_table_entry *lte);
492
493 #endif /* _WIMLIB_LOOKUP_TABLE_H */