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