]> wimlib.net Git - wimlib/blob - include/wimlib/lookup_table.h
add_params: Embed inode table and sd_set directly
[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 (wim_resource_size(lte) + WIM_CHUNK_SIZE - 1) / 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);
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);
322
323 extern struct wim_lookup_table_entry *
324 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte);
325
326 extern void
327 print_lookup_table_entry(const struct wim_lookup_table_entry *entry,
328                          FILE *out);
329
330 extern void
331 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
332
333 extern int
334 for_lookup_table_entry(struct wim_lookup_table *table,
335                        int (*visitor)(struct wim_lookup_table_entry *, void *),
336                        void *arg);
337
338 extern int
339 cmp_streams_by_wim_position(const void *p1, const void *p2);
340
341 extern int
342 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
343                                   int (*visitor)(struct wim_lookup_table_entry *,
344                                                  void *),
345                                   void *arg);
346
347 extern struct wim_lookup_table_entry *
348 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
349
350 extern int
351 lookup_resource(WIMStruct *w, const tchar *path,
352                 int lookup_flags, struct wim_dentry **dentry_ret,
353                 struct wim_lookup_table_entry **lte_ret, u16 *stream_idx_ret);
354
355 extern void
356 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
357                      struct wim_lookup_table *table);
358 #ifdef WITH_FUSE
359 extern void
360 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
361 #endif
362
363 extern int
364 lte_zero_out_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
365
366 extern int
367 lte_zero_real_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
368
369 extern int
370 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
371
372 extern void
373 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table);
374
375 extern void
376 inode_unresolve_ltes(struct wim_inode *inode);
377
378 static inline struct wim_lookup_table_entry *
379 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
380 {
381         wimlib_assert(inode->i_resolved);
382         wimlib_assert(stream_idx <= inode->i_num_ads);
383         if (stream_idx == 0)
384                 return inode->i_lte;
385         else
386                 return inode->i_ads_entries[stream_idx - 1].lte;
387 }
388
389 static inline struct wim_lookup_table_entry *
390 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
391                             const struct wim_lookup_table *table)
392 {
393         wimlib_assert(!inode->i_resolved);
394         wimlib_assert(stream_idx <= inode->i_num_ads);
395         if (!table)
396                 return NULL;
397         if (stream_idx == 0)
398                 return __lookup_resource(table, inode->i_hash);
399         else
400                 return __lookup_resource(table,
401                                          inode->i_ads_entries[
402                                                 stream_idx - 1].hash);
403 }
404
405 extern struct wim_lookup_table_entry *
406 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
407                  const struct wim_lookup_table *table);
408
409 static inline const u8 *
410 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
411 {
412         wimlib_assert(!inode->i_resolved);
413         wimlib_assert(stream_idx <= inode->i_num_ads);
414         if (stream_idx == 0)
415                 return inode->i_hash;
416         else
417                 return inode->i_ads_entries[stream_idx - 1].hash;
418 }
419
420
421 static inline const u8 *
422 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
423 {
424         struct wim_lookup_table_entry *lte;
425         lte = inode_stream_lte_resolved(inode, stream_idx);
426         if (lte)
427                 return lte->hash;
428         else
429                 return zero_hash;
430 }
431
432 /*
433  * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
434  * means the default un-named file stream, and stream_idx >= 1 corresponds to an
435  * alternate data stream.
436  *
437  * This works for both resolved and un-resolved dentries.
438  */
439 static inline const u8 *
440 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
441 {
442         if (inode->i_resolved)
443                 return inode_stream_hash_resolved(inode, stream_idx);
444         else
445                 return inode_stream_hash_unresolved(inode, stream_idx);
446 }
447
448 static inline u16
449 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
450 {
451         wimlib_assert(stream_idx <= inode->i_num_ads);
452         if (stream_idx == 0)
453                 return 0;
454         else
455                 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
456 }
457
458 extern struct wim_lookup_table_entry *
459 inode_unnamed_lte_resolved(const struct wim_inode *inode);
460
461 extern struct wim_lookup_table_entry *
462 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
463                              const struct wim_lookup_table *table);
464
465 extern struct wim_lookup_table_entry *
466 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
467
468 extern u64
469 lookup_table_total_stream_size(struct wim_lookup_table *table);
470
471
472 static inline void
473 lookup_table_insert_unhashed(struct wim_lookup_table *table,
474                              struct wim_lookup_table_entry *lte,
475                              struct wim_inode *back_inode,
476                              u32 back_stream_id)
477 {
478         lte->unhashed = 1;
479         lte->back_inode = back_inode;
480         lte->back_stream_id = back_stream_id;
481         list_add_tail(&lte->unhashed_list, table->unhashed_streams);
482 }
483
484 extern int
485 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
486                      struct wim_lookup_table *lookup_table,
487                      struct wim_lookup_table_entry **lte_ret);
488
489 extern struct wim_lookup_table_entry **
490 retrieve_lte_pointer(struct wim_lookup_table_entry *lte);
491
492 #endif /* _WIMLIB_LOOKUP_TABLE_H */