]> wimlib.net Git - wimlib/blob - include/wimlib/lookup_table.h
Rewrite of write_stream_list(), and writing packed resources
[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 handle situations such as image capture and image mount, we allow
53  * the actual location of the stream to be somewhere else, such as an external
54  * file.
55  */
56 enum resource_location {
57         /* The lookup table entry does not yet correspond to a stream; this is a
58          * temporary state only.  */
59         RESOURCE_NONEXISTENT = 0,
60
61         /* The stream is located in a resource in a WIM file identified by the
62          * `struct wim_resource_spec' pointed to by @rspec.  @offset_in_res
63          * identifies the offset at which this particular stream begins in the
64          * uncompressed data of the resource; this is normally 0, but in general
65          * a WIM resource may contain multiple streams.  */
66         RESOURCE_IN_WIM,
67
68         /* The stream is located in the external file named by @file_on_disk.
69          * On Windows, @file_on_disk may actually specify a named data stream.
70          */
71         RESOURCE_IN_FILE_ON_DISK,
72
73         /* The stream is directly attached in the in-memory buffer pointed to by
74          * @attached_buffer.  */
75         RESOURCE_IN_ATTACHED_BUFFER,
76
77 #ifdef WITH_FUSE
78         /* The stream is located in the external file named by
79          * @staging_file_name, located in the staging directory for a read-write
80          * mount.  */
81         RESOURCE_IN_STAGING_FILE,
82 #endif
83
84 #ifdef WITH_NTFS_3G
85         /* The stream is located in an NTFS volume.  It is identified by volume,
86          * filename, data stream name, and by whether it is a reparse point or
87          * not.  @ntfs_loc points to a structure containing this information.
88          * */
89         RESOURCE_IN_NTFS_VOLUME,
90 #endif
91
92 #ifdef __WIN32__
93         /* Windows only: the stream is located in the external file named by
94          * @file_on_disk, but the file is encrypted and must be read using the
95          * appropriate Windows API.  */
96         RESOURCE_WIN32_ENCRYPTED,
97 #endif
98
99 };
100
101 /*
102  * An entry in the lookup table in the WIM file.
103  *
104  * It is used to find data streams for files in the WIM.
105  *
106  * Metadata resources and reparse point data buffers will also have lookup table
107  * entries associated with the data.
108  *
109  * The lookup_table_entry for a given dentry or alternate stream entry in the
110  * WIM is found using the SHA1 message digest field.
111  */
112 struct wim_lookup_table_entry {
113
114         /* List of lookup table entries in this hash bucket */
115         struct hlist_node hash_list;
116
117         /* Uncompressed size of the stream.  */
118         u64 size;
119
120         /* Stream flags (WIM_RESHDR_FLAG_*).  */
121         u32 flags : 8;
122
123         /* One of the `enum resource_location' values documented above. */
124         u32 resource_location : 5;
125
126         /* 1 if this stream has not had a SHA1 message digest calculated for it
127          * yet */
128         u32 unhashed : 1;
129
130         /* Temoorary files used for writing; set as documented for
131          * prepare_stream_list_for_write().  */
132         u32 unique_size : 1;
133         u32 will_be_in_output_wim : 1;
134
135         /* Set to 1 when a metadata entry has its checksum changed; in such
136          * cases the hash is no longer valid to verify the data if the metadata
137          * resource is read again.  */
138         u32 dont_check_metadata_hash : 1;
139
140         union {
141                 /* (On-disk field) SHA1 message digest of the stream referenced
142                  * by this lookup table entry.  */
143                 u8  hash[SHA1_HASH_SIZE];
144
145                 /* First 4 or 8 bytes of the SHA1 message digest, used for
146                  * inserting the entry into the hash table.  Since the SHA1
147                  * message digest can be considered random, we don't really need
148                  * the full 20 byte hash just to insert the entry in a hash
149                  * table.  */
150                 size_t hash_short;
151
152                 /* Unhashed entries only (unhashed == 1): these variables make
153                  * it possible to find the pointer to this 'struct
154                  * wim_lookup_table_entry' contained in either 'struct
155                  * wim_ads_entry' or 'struct wim_inode'.  There can be at most 1
156                  * such pointer, as we can only join duplicate streams after
157                  * they have been hashed.  */
158                 struct {
159                         struct wim_inode *back_inode;
160                         u32 back_stream_id;
161                 };
162         };
163
164         /* Number of times this lookup table entry is referenced by dentries in
165          * the WIM.  */
166         u32 refcnt;
167
168         /* Actual reference count to this stream (only used while verifying an
169          * image). */
170         u32 real_refcnt;
171
172         /* When a WIM file is written, out_refcnt starts at 0 and is incremented
173          * whenever the stream pointed to by this lookup table entry needs to be
174          * written.  The stream only need to be written when out_refcnt is
175          * nonzero, since otherwise it is not referenced by any dentries. */
176         u32 out_refcnt;
177
178 #ifdef WITH_FUSE
179         /* Number of times this stream has been opened (used only during
180          * mounting) */
181         u16 num_opened_fds;
182 #endif
183
184         /* Pointers to somewhere where the stream is actually located.  See the
185          * comments for the @resource_location field above. */
186         union {
187                 struct {
188                         struct wim_resource_spec *rspec;
189                         u64 offset_in_res;
190                 };
191                 tchar *file_on_disk;
192                 void *attached_buffer;
193         #ifdef WITH_FUSE
194                 tchar *staging_file_name;
195         #endif
196         #ifdef WITH_NTFS_3G
197                 struct ntfs_location *ntfs_loc;
198         #endif
199         };
200
201         /* Links together streams that share the same underlying WIM resource.
202          * The head is wim_resource_spec.stream_list.  */
203         struct list_head rspec_node;
204
205         /* This field is used for the special hardlink or symlink image
206          * extraction mode.   In these mode, all identical files are linked
207          * together, and @extracted_file will be set to the filename of the
208          * first extracted file containing this stream.  */
209         tchar *extracted_file;
210
211         /* Temporary fields  */
212         union {
213                 /* Used temporarily during WIM file writing  */
214                 struct {
215                         union {
216                                 struct hlist_node hash_list_2;
217                                 struct {
218                                         u64 out_res_offset_in_wim;
219                                         u64 out_res_size_in_wim;
220                                         u64 out_res_uncompressed_size;
221                                 };
222                         };
223                         /* Links streams being written to the WIM.  */
224                         struct list_head write_streams_list;
225
226                         struct wim_reshdr out_reshdr;
227                 };
228
229                 /* Used temporarily during extraction  */
230                 union {
231                         /* out_refcnt tracks number of slots filled */
232                         struct wim_dentry *inline_lte_dentries[8];
233                         struct {
234                                 struct wim_dentry **lte_dentries;
235                                 unsigned long alloc_lte_dentries;
236                         };
237                 };
238         };
239
240         /* Temporary list fields */
241         union {
242                 /* Links streams for writing lookup table.  */
243                 struct list_head lookup_table_list;
244
245                 /* Links streams being extracted.  */
246                 struct list_head extraction_list;
247
248                 /* Links streams being exported.  */
249                 struct list_head export_stream_list;
250         };
251
252         /* Links streams that are still unhashed after being been added to a
253          * WIM.  */
254         struct list_head unhashed_list;
255 };
256
257 static inline bool
258 lte_is_partial(const struct wim_lookup_table_entry * lte)
259 {
260         return lte->resource_location == RESOURCE_IN_WIM &&
261                lte->size != lte->rspec->uncompressed_size;
262 }
263
264 static inline bool
265 lte_filename_valid(const struct wim_lookup_table_entry *lte)
266 {
267         return     lte->resource_location == RESOURCE_IN_FILE_ON_DISK
268         #ifdef __WIN32__
269                 || lte->resource_location == RESOURCE_WIN32_ENCRYPTED
270         #endif
271         #ifdef WITH_FUSE
272                 || lte->resource_location == RESOURCE_IN_STAGING_FILE
273         #endif
274                 ;
275 }
276
277 extern struct wim_lookup_table *
278 new_lookup_table(size_t capacity) _malloc_attribute;
279
280 extern int
281 read_wim_lookup_table(WIMStruct *wim);
282
283 extern int
284 write_wim_lookup_table_from_stream_list(struct list_head *stream_list,
285                                         struct filedes *out_fd,
286                                         u16 part_number,
287                                         struct wim_reshdr *out_reshdr,
288                                         int write_resource_flags,
289                                         struct wimlib_lzx_context **comp_ctx);
290
291 extern void
292 free_lookup_table(struct wim_lookup_table *table);
293
294 extern void
295 lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte);
296
297 /* Unlinks a lookup table entry from the table; does not free it. */
298 static inline void
299 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
300 {
301         wimlib_assert(!lte->unhashed);
302         hlist_del(&lte->hash_list);
303         wimlib_assert(table->num_entries != 0);
304         table->num_entries--;
305 }
306
307 extern struct wim_lookup_table_entry *
308 new_lookup_table_entry(void) _malloc_attribute;
309
310 extern struct wim_lookup_table_entry *
311 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte)
312                         _malloc_attribute;
313
314 extern void
315 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out);
316
317 extern void
318 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
319
320 extern void
321 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
322                              struct wimlib_resource_entry *wentry);
323
324 extern int
325 for_lookup_table_entry(struct wim_lookup_table *table,
326                        int (*visitor)(struct wim_lookup_table_entry *, void *),
327                        void *arg);
328
329 extern int
330 sort_stream_list(struct list_head *stream_list,
331                  size_t list_head_offset,
332                  int (*compar)(const void *, const void*));
333
334 extern int
335 sort_stream_list_by_sequential_order(struct list_head *stream_list,
336                                      size_t list_head_offset);
337
338 extern int
339 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
340                                   int (*visitor)(struct wim_lookup_table_entry *,
341                                                  void *),
342                                   void *arg);
343
344 extern struct wim_lookup_table_entry *
345 lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
346
347 extern int
348 wim_pathname_to_stream(WIMStruct *wim, const tchar *path,
349                        int lookup_flags,
350                        struct wim_dentry **dentry_ret,
351                        struct wim_lookup_table_entry **lte_ret,
352                        u16 *stream_idx_ret);
353
354 extern void
355 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
356                      struct wim_lookup_table *table);
357 #ifdef WITH_FUSE
358 extern void
359 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
360 #endif
361
362 extern int
363 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *ignore);
364
365 extern int
366 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *ignore);
367
368 extern int
369 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
370
371 static inline void
372 lte_bind_wim_resource_spec(struct wim_lookup_table_entry *lte,
373                            struct wim_resource_spec *rspec)
374 {
375         lte->resource_location = RESOURCE_IN_WIM;
376         lte->rspec = rspec;
377         list_add_tail(&lte->rspec_node, &rspec->stream_list);
378 }
379
380 static inline void
381 lte_unbind_wim_resource_spec(struct wim_lookup_table_entry *lte)
382 {
383         list_del(&lte->rspec_node);
384         lte->rspec = NULL;
385         lte->resource_location = RESOURCE_NONEXISTENT;
386 }
387
388 extern int
389 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table,
390                    bool force);
391
392 extern int
393 resource_not_found_error(const struct wim_inode *inode, const u8 *hash);
394
395 extern void
396 inode_unresolve_ltes(struct wim_inode *inode);
397
398 static inline struct wim_lookup_table_entry *
399 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
400 {
401         wimlib_assert(inode->i_resolved);
402         wimlib_assert(stream_idx <= inode->i_num_ads);
403         if (stream_idx == 0)
404                 return inode->i_lte;
405         else
406                 return inode->i_ads_entries[stream_idx - 1].lte;
407 }
408
409 static inline struct wim_lookup_table_entry *
410 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
411                             const struct wim_lookup_table *table)
412 {
413         wimlib_assert(!inode->i_resolved);
414         wimlib_assert(stream_idx <= inode->i_num_ads);
415         if (!table)
416                 return NULL;
417         if (stream_idx == 0)
418                 return lookup_resource(table, inode->i_hash);
419         else
420                 return lookup_resource(table,
421                                          inode->i_ads_entries[
422                                                 stream_idx - 1].hash);
423 }
424
425 extern struct wim_lookup_table_entry *
426 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
427                  const struct wim_lookup_table *table);
428
429 static inline const u8 *
430 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
431 {
432         wimlib_assert(!inode->i_resolved);
433         wimlib_assert(stream_idx <= inode->i_num_ads);
434         if (stream_idx == 0)
435                 return inode->i_hash;
436         else
437                 return inode->i_ads_entries[stream_idx - 1].hash;
438 }
439
440
441 static inline const u8 *
442 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
443 {
444         struct wim_lookup_table_entry *lte;
445         lte = inode_stream_lte_resolved(inode, stream_idx);
446         if (lte)
447                 return lte->hash;
448         else
449                 return zero_hash;
450 }
451
452 /*
453  * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
454  * means the default un-named file stream, and stream_idx >= 1 corresponds to an
455  * alternate data stream.
456  *
457  * This works for both resolved and un-resolved dentries.
458  */
459 static inline const u8 *
460 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
461 {
462         if (inode->i_resolved)
463                 return inode_stream_hash_resolved(inode, stream_idx);
464         else
465                 return inode_stream_hash_unresolved(inode, stream_idx);
466 }
467
468 static inline u16
469 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
470 {
471         wimlib_assert(stream_idx <= inode->i_num_ads);
472         if (stream_idx == 0)
473                 return 0;
474         else
475                 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
476 }
477
478 extern struct wim_lookup_table_entry *
479 inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret);
480
481 extern struct wim_lookup_table_entry *
482 inode_unnamed_lte_resolved(const struct wim_inode *inode);
483
484 extern struct wim_lookup_table_entry *
485 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
486                              const struct wim_lookup_table *table);
487
488 extern struct wim_lookup_table_entry *
489 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
490
491 extern const u8 *
492 inode_unnamed_stream_hash(const struct wim_inode *inode);
493
494 static inline void
495 lookup_table_insert_unhashed(struct wim_lookup_table *table,
496                              struct wim_lookup_table_entry *lte,
497                              struct wim_inode *back_inode,
498                              u32 back_stream_id)
499 {
500         lte->unhashed = 1;
501         lte->back_inode = back_inode;
502         lte->back_stream_id = back_stream_id;
503         list_add_tail(&lte->unhashed_list, table->unhashed_streams);
504 }
505
506 extern int
507 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
508                      struct wim_lookup_table *lookup_table,
509                      struct wim_lookup_table_entry **lte_ret);
510
511 extern struct wim_lookup_table_entry **
512 retrieve_lte_pointer(struct wim_lookup_table_entry *lte);
513
514 #endif /* _WIMLIB_LOOKUP_TABLE_H */