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