]> wimlib.net Git - wimlib/blob - src/lookup_table.h
Encodings update (IN PROGRESS)
[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 };
36
37 #ifdef WITH_NTFS_3G
38 struct ntfs_location {
39         tchar *path;
40         utf16lechar *stream_name;
41         u16 stream_name_nchars;
42         struct _ntfs_volume **ntfs_vol_p;
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         /* The stream resource is located in an external file.  The name of the
66          * file will be provided by @file_on_disk member.  In addition, if
67          * @file_on_disk_fp is not NULL, it will be an open FILE * to the file.
68          * */
69         RESOURCE_IN_FILE_ON_DISK,
70
71         /* The stream resource is located in an external file in the staging
72          * directory for a read-write mount.  */
73         RESOURCE_IN_STAGING_FILE,
74
75         /* The stream resource is directly attached in an in-memory buffer
76          * pointed to by @attached_buffer. */
77         RESOURCE_IN_ATTACHED_BUFFER,
78
79         /* The stream resource is located in an NTFS volume.  It is identified
80          * by volume, filename, data stream name, and by whether it is a reparse
81          * point or not. @ntfs_loc points to a structure containing this
82          * information. */
83         RESOURCE_IN_NTFS_VOLUME,
84
85         /* Resource must be accessed using Win32 API (may be a named data
86          * stream) */
87         RESOURCE_WIN32,
88 };
89
90 /*
91  * An entry in the lookup table in the WIM file.
92  *
93  * It is used to find data streams for files in the WIM.
94  *
95  * Metadata resources and reparse point data buffers will also have lookup table
96  * entries associated with the data.
97  *
98  * The lookup_table_entry for a given dentry or alternate stream entry in the
99  * WIM is found using the SHA1 message digest field.
100  */
101 struct wim_lookup_table_entry {
102
103         /* List of lookup table entries in this hash bucket */
104         struct hlist_node hash_list;
105
106         /* Location and size of the stream in the WIM, whether it is compressed
107          * or not, and whether it's a metadata resource or not.  This is an
108          * on-disk field. */
109         struct resource_entry resource_entry;
110
111         /* Specifies which part of the split WIM the resource is located in.
112          * This is on on-disk field.
113          *
114          * In stand-alone WIMs, this must be 1.
115          *
116          * In split WIMs, every split WIM part has its own lookup table, and in
117          * read_lookup_table() it's currently expected that the part number of
118          * each lookup table entry in a split WIM part's lookup table is the
119          * same as the part number of that split WIM part.  So this makes this
120          * field redundant since we store a pointer to the corresponding
121          * WIMStruct in the lookup table entry anyway.
122          */
123         u16 part_number;
124
125         /* See enum resource_location above */
126         u16 resource_location;
127
128         /* (On-disk field)
129          * Number of times this lookup table entry is referenced by dentries.
130          * Unfortunately, this field is not always set correctly in Microsoft's
131          * WIMs, so we have no choice but to fix it if more references to the
132          * lookup table entry are found than stated here. */
133         u32 refcnt;
134
135         union {
136                 /* (On-disk field) SHA1 message digest of the stream referenced
137                  * by this lookup table entry */
138                 u8  hash[SHA1_HASH_SIZE];
139
140                 /* First 4 or 8 bytes of the SHA1 message digest, used for
141                  * inserting the entry into the hash table.  Since the SHA1
142                  * message digest can be considered random, we don't really need
143                  * the full 20 byte hash just to insert the entry in a hash
144                  * table. */
145                 size_t hash_short;
146         };
147
148         union {
149                 #ifdef WITH_FUSE
150                 u16 num_opened_fds;
151                 #endif
152
153                 /* This field is used for the special hardlink or symlink image
154                  * extraction mode.   In these mode, all identical files are linked
155                  * together, and @extracted_file will be set to the filename of the
156                  * first extracted file containing this stream.  */
157                 tchar *extracted_file;
158         };
159
160         /* Pointers to somewhere where the stream is actually located.  See the
161          * comments for the @resource_location field above. */
162         union {
163                 WIMStruct *wim;
164                 tchar *file_on_disk;
165                 tchar *staging_file_name;
166                 u8 *attached_buffer;
167         #ifdef WITH_NTFS_3G
168                 struct ntfs_location *ntfs_loc;
169         #endif
170         };
171         union {
172                 /* @file_on_disk_fp and @attr are both used to cache file/stream
173                  * handles so we don't have re-open them on every read */
174
175
176                 /* Valid iff resource_location == RESOURCE_IN_FILE_ON_DISK */
177                 FILE *file_on_disk_fp;
178         #ifdef WITH_NTFS_3G
179                 /* Valid iff resource_location == RESOURCE_IN_NTFS_VOLUME */
180                 struct _ntfs_attr *attr;
181         #endif
182
183         #ifdef __WIN32__
184                 HANDLE win32_file_on_disk_fp;
185         #endif
186
187                 /* Pointer to inode that contains the opened file descriptors to
188                  * this stream (valid iff resource_location ==
189                  * RESOURCE_IN_STAGING_FILE) */
190                 struct wim_inode *lte_inode;
191         };
192
193         /* When a WIM file is written, out_refcnt starts at 0 and is incremented
194          * whenever the file resource pointed to by this lookup table entry
195          * needs to be written.  The file resource only need to be written when
196          * out_refcnt is nonzero, since otherwise it is not referenced by any
197          * dentries. */
198         u32 out_refcnt;
199
200         u32 real_refcnt;
201
202         union {
203                 /* When a WIM file is written, @output_resource_entry is filled
204                  * in with the resource entry for the output WIM.  This will not
205                  * necessarily be the same as the @resource_entry since: - The
206                  * stream may have a different offset in the new WIM - The
207                  * stream may have a different compressed size in the new WIM if
208                  * the compression type changed
209                  */
210                 struct resource_entry output_resource_entry;
211
212                 struct list_head msg_list;
213                 struct list_head inode_list;
214         };
215
216         /* List of lookup table entries that correspond to streams that have
217          * been extracted to the staging directory when modifying a read-write
218          * mounted WIM.
219          *
220          * This field is also used to make other lists of lookup table entries.
221          * */
222         struct list_head staging_list;
223 };
224
225 static inline u64
226 wim_resource_size(const struct wim_lookup_table_entry *lte)
227 {
228         return lte->resource_entry.original_size;
229 }
230
231 static inline u64
232 wim_resource_chunks(const struct wim_lookup_table_entry *lte)
233 {
234         return (wim_resource_size(lte) + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
235 }
236
237 static inline u64
238 wim_resource_compressed_size(const struct wim_lookup_table_entry *lte)
239 {
240         return lte->resource_entry.size;
241 }
242
243 /*
244  * XXX Probably should store the compression type directly in the lookup table
245  * entry
246  */
247 static inline int
248 wim_resource_compression_type(const struct wim_lookup_table_entry *lte)
249 {
250         if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
251             || lte->resource_location != RESOURCE_IN_WIM)
252                 return WIMLIB_COMPRESSION_TYPE_NONE;
253         return wimlib_get_compression_type(lte->wim);
254 }
255
256
257 extern struct wim_lookup_table *
258 new_lookup_table(size_t capacity);
259
260 extern int
261 read_lookup_table(WIMStruct *w);
262
263 extern int
264 write_lookup_table(struct wim_lookup_table *table, FILE *out,
265                    struct resource_entry *out_res_entry);
266 extern void
267 free_lookup_table(struct wim_lookup_table *table);
268
269 extern void
270 lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte);
271
272 /* Unlinks a lookup table entry from the table; does not free it. */
273 static inline void
274 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
275 {
276         hlist_del(&lte->hash_list);
277         table->num_entries--;
278 }
279
280 extern struct wim_lookup_table_entry *
281 new_lookup_table_entry();
282
283 extern struct wim_lookup_table_entry *
284 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte);
285
286 extern void
287 print_lookup_table_entry(const struct wim_lookup_table_entry *entry,
288                          FILE *out);
289
290 extern void
291 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
292
293 extern int
294 for_lookup_table_entry(struct wim_lookup_table *table,
295                        int (*visitor)(struct wim_lookup_table_entry *, void *),
296                        void *arg);
297
298 extern struct wim_lookup_table_entry *
299 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
300
301 extern int
302 lookup_resource(WIMStruct *w, const tchar *path,
303                 int lookup_flags, struct wim_dentry **dentry_ret,
304                 struct wim_lookup_table_entry **lte_ret, u16 *stream_idx_ret);
305
306 extern void
307 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
308                      struct wim_lookup_table *table);
309 #ifdef WITH_FUSE
310 extern void
311 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
312 #endif
313
314 extern int
315 lte_zero_out_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
316
317 extern int
318 lte_zero_real_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
319
320 extern int
321 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
322
323 extern void
324 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table);
325
326 extern void
327 inode_unresolve_ltes(struct wim_inode *inode);
328
329 extern int
330 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *__out);
331
332 static inline struct resource_entry*
333 wim_metadata_resource_entry(WIMStruct *w)
334 {
335         return &w->image_metadata[
336                         w->current_image - 1].metadata_lte->resource_entry;
337 }
338
339 static inline struct wim_lookup_table_entry *
340 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
341 {
342         wimlib_assert(inode->i_resolved);
343         wimlib_assert(stream_idx <= inode->i_num_ads);
344         if (stream_idx == 0)
345                 return inode->i_lte;
346         else
347                 return inode->i_ads_entries[stream_idx - 1].lte;
348 }
349
350 static inline struct wim_lookup_table_entry *
351 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
352                             const struct wim_lookup_table *table)
353 {
354         wimlib_assert(!inode->i_resolved);
355         wimlib_assert(stream_idx <= inode->i_num_ads);
356         if (!table)
357                 return NULL;
358         if (stream_idx == 0)
359                 return __lookup_resource(table, inode->i_hash);
360         else
361                 return __lookup_resource(table,
362                                          inode->i_ads_entries[
363                                                 stream_idx - 1].hash);
364 }
365
366 extern struct wim_lookup_table_entry *
367 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
368                  const struct wim_lookup_table *table);
369
370 static inline const u8 *
371 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
372 {
373         wimlib_assert(!inode->i_resolved);
374         wimlib_assert(stream_idx <= inode->i_num_ads);
375         if (stream_idx == 0)
376                 return inode->i_hash;
377         else
378                 return inode->i_ads_entries[stream_idx - 1].hash;
379 }
380
381
382 static inline const u8 *
383 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
384 {
385         struct wim_lookup_table_entry *lte;
386         lte = inode_stream_lte_resolved(inode, stream_idx);
387         if (lte)
388                 return lte->hash;
389         else
390                 return zero_hash;
391 }
392
393 /*
394  * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
395  * means the default un-named file stream, and stream_idx >= 1 corresponds to an
396  * alternate data stream.
397  *
398  * This works for both resolved and un-resolved dentries.
399  */
400 static inline const u8 *
401 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
402 {
403         if (inode->i_resolved)
404                 return inode_stream_hash_resolved(inode, stream_idx);
405         else
406                 return inode_stream_hash_unresolved(inode, stream_idx);
407 }
408
409 static inline u16
410 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
411 {
412         wimlib_assert(stream_idx <= inode->i_num_ads);
413         if (stream_idx == 0)
414                 return 0;
415         else
416                 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
417 }
418
419 static inline struct wim_lookup_table_entry *
420 inode_unnamed_lte_resolved(const struct wim_inode *inode)
421 {
422         wimlib_assert(inode->i_resolved);
423         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
424                 if (inode_stream_name_nbytes(inode, i) == 0 &&
425                     !is_zero_hash(inode_stream_hash_resolved(inode, i)))
426                 {
427                         return inode_stream_lte_resolved(inode, i);
428                 }
429         }
430         return NULL;
431 }
432
433 static inline struct wim_lookup_table_entry *
434 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
435                              const struct wim_lookup_table *table)
436 {
437         wimlib_assert(!inode->i_resolved);
438         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
439                 if (inode_stream_name_nbytes(inode, i) == 0 &&
440                     !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
441                 {
442                         return inode_stream_lte_unresolved(inode, i, table);
443                 }
444         }
445         return NULL;
446 }
447
448 extern struct wim_lookup_table_entry *
449 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
450
451 extern u64
452 lookup_table_total_stream_size(struct wim_lookup_table *table);
453
454 #endif