]> wimlib.net Git - wimlib/blob - src/lookup_table.h
ab545f2d3ea94cd38b139af34ab7fee4bdbcaaa9
[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;
140
141         /* (On-disk field)
142          * Number of times this lookup table entry is referenced by dentries.
143          * Unfortunately, this field is not always set correctly in Microsoft's
144          * WIMs, so we have no choice but to fix it if more references to the
145          * lookup table entry are found than stated here. */
146         u32 refcnt;
147
148         union {
149                 /* (On-disk field) SHA1 message digest of the stream referenced
150                  * by this lookup table entry */
151                 u8  hash[SHA1_HASH_SIZE];
152
153                 /* First 4 or 8 bytes of the SHA1 message digest, used for
154                  * inserting the entry into the hash table.  Since the SHA1
155                  * message digest can be considered random, we don't really need
156                  * the full 20 byte hash just to insert the entry in a hash
157                  * table. */
158                 size_t hash_short;
159         };
160
161         union {
162                 #ifdef WITH_FUSE
163                 u16 num_opened_fds;
164                 #endif
165
166                 /* This field is used for the special hardlink or symlink image
167                  * extraction mode.   In these mode, all identical files are linked
168                  * together, and @extracted_file will be set to the filename of the
169                  * first extracted file containing this stream.  */
170                 tchar *extracted_file;
171         };
172
173         /* Pointers to somewhere where the stream is actually located.  See the
174          * comments for the @resource_location field above. */
175         union {
176                 WIMStruct *wim;
177                 tchar *file_on_disk;
178                 tchar *staging_file_name;
179                 void *attached_buffer;
180         #ifdef WITH_NTFS_3G
181                 struct ntfs_location *ntfs_loc;
182         #endif
183         };
184
185         /* Pointer to inode that contains the opened file descriptors to
186          * this stream (valid iff resource_location ==
187          * RESOURCE_IN_STAGING_FILE) */
188         struct wim_inode *lte_inode;
189
190         /* When a WIM file is written, out_refcnt starts at 0 and is incremented
191          * whenever the file resource pointed to by this lookup table entry
192          * needs to be written.  The file resource only need to be written when
193          * out_refcnt is nonzero, since otherwise it is not referenced by any
194          * dentries. */
195         u32 out_refcnt;
196
197         u32 real_refcnt;
198
199         union {
200                 /* When a WIM file is written, @output_resource_entry is filled
201                  * in with the resource entry for the output WIM.  This will not
202                  * necessarily be the same as the @resource_entry since: - The
203                  * stream may have a different offset in the new WIM - The
204                  * stream may have a different compressed size in the new WIM if
205                  * the compression type changed
206                  */
207                 struct resource_entry output_resource_entry;
208
209                 struct list_head msg_list;
210                 struct list_head inode_list;
211         };
212
213         /* List of lookup table entries that correspond to streams that have
214          * been extracted to the staging directory when modifying a read-write
215          * mounted WIM.
216          *
217          * This field is also used to make other lists of lookup table entries.
218          * */
219         struct list_head staging_list;
220 };
221
222 static inline u64
223 wim_resource_size(const struct wim_lookup_table_entry *lte)
224 {
225         return lte->resource_entry.original_size;
226 }
227
228 static inline u64
229 wim_resource_chunks(const struct wim_lookup_table_entry *lte)
230 {
231         return (wim_resource_size(lte) + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
232 }
233
234 static inline u64
235 wim_resource_compressed_size(const struct wim_lookup_table_entry *lte)
236 {
237         return lte->resource_entry.size;
238 }
239
240 /*
241  * XXX Probably should store the compression type directly in the lookup table
242  * entry
243  */
244 static inline int
245 wim_resource_compression_type(const struct wim_lookup_table_entry *lte)
246 {
247         if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
248             || lte->resource_location != RESOURCE_IN_WIM)
249                 return WIMLIB_COMPRESSION_TYPE_NONE;
250         return wimlib_get_compression_type(lte->wim);
251 }
252
253 static inline bool
254 lte_filename_valid(const struct wim_lookup_table_entry *lte)
255 {
256         return lte->resource_location == RESOURCE_IN_FILE_ON_DISK
257         #ifdef __WIN32__
258                 || lte->resource_location == RESOURCE_WIN32
259                 || lte->resource_location == RESOURCE_WIN32_ENCRYPTED
260         #endif
261         #ifdef WITH_FUSE
262                 || lte->resource_location == RESOURCE_IN_STAGING_FILE
263         #endif
264                 ;
265 }
266
267 extern struct wim_lookup_table *
268 new_lookup_table(size_t capacity);
269
270 extern int
271 read_lookup_table(WIMStruct *w);
272
273 extern int
274 write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry);
275
276 extern void
277 free_lookup_table(struct wim_lookup_table *table);
278
279 extern void
280 lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte);
281
282 /* Unlinks a lookup table entry from the table; does not free it. */
283 static inline void
284 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
285 {
286         hlist_del(&lte->hash_list);
287         wimlib_assert(table->num_entries != 0);
288         table->num_entries--;
289 }
290
291 extern struct wim_lookup_table_entry *
292 new_lookup_table_entry();
293
294 extern struct wim_lookup_table_entry *
295 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte);
296
297 extern void
298 print_lookup_table_entry(const struct wim_lookup_table_entry *entry,
299                          FILE *out);
300
301 extern void
302 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
303
304 extern int
305 for_lookup_table_entry(struct wim_lookup_table *table,
306                        int (*visitor)(struct wim_lookup_table_entry *, void *),
307                        void *arg);
308
309 extern int
310 sort_stream_list_by_wim_position(struct list_head *stream_list);
311
312 extern int
313 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
314                                   int (*visitor)(struct wim_lookup_table_entry *,
315                                                  void *),
316                                   void *arg);
317
318 extern struct wim_lookup_table_entry *
319 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
320
321 extern int
322 lookup_resource(WIMStruct *w, const tchar *path,
323                 int lookup_flags, struct wim_dentry **dentry_ret,
324                 struct wim_lookup_table_entry **lte_ret, u16 *stream_idx_ret);
325
326 extern void
327 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
328                      struct wim_lookup_table *table);
329 #ifdef WITH_FUSE
330 extern void
331 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
332 #endif
333
334 extern int
335 lte_zero_out_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
336
337 extern int
338 lte_zero_real_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
339
340 extern int
341 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
342
343 extern void
344 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table);
345
346 extern void
347 inode_unresolve_ltes(struct wim_inode *inode);
348
349 extern int
350 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *__out);
351
352 static inline struct resource_entry*
353 wim_metadata_resource_entry(WIMStruct *w)
354 {
355         return &w->image_metadata[
356                         w->current_image - 1].metadata_lte->resource_entry;
357 }
358
359 static inline struct wim_lookup_table_entry *
360 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
361 {
362         wimlib_assert(inode->i_resolved);
363         wimlib_assert(stream_idx <= inode->i_num_ads);
364         if (stream_idx == 0)
365                 return inode->i_lte;
366         else
367                 return inode->i_ads_entries[stream_idx - 1].lte;
368 }
369
370 static inline struct wim_lookup_table_entry *
371 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
372                             const struct wim_lookup_table *table)
373 {
374         wimlib_assert(!inode->i_resolved);
375         wimlib_assert(stream_idx <= inode->i_num_ads);
376         if (!table)
377                 return NULL;
378         if (stream_idx == 0)
379                 return __lookup_resource(table, inode->i_hash);
380         else
381                 return __lookup_resource(table,
382                                          inode->i_ads_entries[
383                                                 stream_idx - 1].hash);
384 }
385
386 extern struct wim_lookup_table_entry *
387 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
388                  const struct wim_lookup_table *table);
389
390 static inline const u8 *
391 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
392 {
393         wimlib_assert(!inode->i_resolved);
394         wimlib_assert(stream_idx <= inode->i_num_ads);
395         if (stream_idx == 0)
396                 return inode->i_hash;
397         else
398                 return inode->i_ads_entries[stream_idx - 1].hash;
399 }
400
401
402 static inline const u8 *
403 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
404 {
405         struct wim_lookup_table_entry *lte;
406         lte = inode_stream_lte_resolved(inode, stream_idx);
407         if (lte)
408                 return lte->hash;
409         else
410                 return zero_hash;
411 }
412
413 /*
414  * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
415  * means the default un-named file stream, and stream_idx >= 1 corresponds to an
416  * alternate data stream.
417  *
418  * This works for both resolved and un-resolved dentries.
419  */
420 static inline const u8 *
421 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
422 {
423         if (inode->i_resolved)
424                 return inode_stream_hash_resolved(inode, stream_idx);
425         else
426                 return inode_stream_hash_unresolved(inode, stream_idx);
427 }
428
429 static inline u16
430 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
431 {
432         wimlib_assert(stream_idx <= inode->i_num_ads);
433         if (stream_idx == 0)
434                 return 0;
435         else
436                 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
437 }
438
439 static inline struct wim_lookup_table_entry *
440 inode_unnamed_lte_resolved(const struct wim_inode *inode)
441 {
442         wimlib_assert(inode->i_resolved);
443         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
444                 if (inode_stream_name_nbytes(inode, i) == 0 &&
445                     !is_zero_hash(inode_stream_hash_resolved(inode, i)))
446                 {
447                         return inode_stream_lte_resolved(inode, i);
448                 }
449         }
450         return NULL;
451 }
452
453 static inline struct wim_lookup_table_entry *
454 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
455                              const struct wim_lookup_table *table)
456 {
457         wimlib_assert(!inode->i_resolved);
458         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
459                 if (inode_stream_name_nbytes(inode, i) == 0 &&
460                     !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
461                 {
462                         return inode_stream_lte_unresolved(inode, i, table);
463                 }
464         }
465         return NULL;
466 }
467
468 extern struct wim_lookup_table_entry *
469 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
470
471 extern u64
472 lookup_table_total_stream_size(struct wim_lookup_table *table);
473
474
475 static inline void
476 lookup_table_insert_unhashed(struct wim_lookup_table *table,
477                              struct wim_lookup_table_entry *lte)
478 {
479         list_add_tail(&lte->staging_list, table->unhashed_streams);
480 }
481
482 extern void
483 lookup_table_free_unhashed_streams(struct wim_lookup_table *table);
484
485 #endif