]> wimlib.net Git - wimlib/blob - src/lookup_table.h
Fix glob()
[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         mbchar *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                 mbchar *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                 mbchar *file_on_disk;
165                 mbchar *staging_file_name;
166         #ifdef __WIN32__
167                 wchar_t *win32_file_on_disk;
168         #endif
169                 u8 *attached_buffer;
170         #ifdef WITH_NTFS_3G
171                 struct ntfs_location *ntfs_loc;
172         #endif
173         };
174         union {
175                 /* @file_on_disk_fp and @attr are both used to cache file/stream
176                  * handles so we don't have re-open them on every read */
177
178
179                 /* Valid iff resource_location == RESOURCE_IN_FILE_ON_DISK */
180                 FILE *file_on_disk_fp;
181         #ifdef WITH_NTFS_3G
182                 /* Valid iff resource_location == RESOURCE_IN_NTFS_VOLUME */
183                 struct _ntfs_attr *attr;
184         #endif
185
186         #ifdef __WIN32__
187                 HANDLE win32_file_on_disk_fp;
188         #endif
189
190                 /* Pointer to inode that contains the opened file descriptors to
191                  * this stream (valid iff resource_location ==
192                  * RESOURCE_IN_STAGING_FILE) */
193                 struct wim_inode *lte_inode;
194         };
195
196         /* When a WIM file is written, out_refcnt starts at 0 and is incremented
197          * whenever the file resource pointed to by this lookup table entry
198          * needs to be written.  The file resource only need to be written when
199          * out_refcnt is nonzero, since otherwise it is not referenced by any
200          * dentries. */
201         u32 out_refcnt;
202
203         u32 real_refcnt;
204
205         union {
206                 /* When a WIM file is written, @output_resource_entry is filled
207                  * in with the resource entry for the output WIM.  This will not
208                  * necessarily be the same as the @resource_entry since: - The
209                  * stream may have a different offset in the new WIM - The
210                  * stream may have a different compressed size in the new WIM if
211                  * the compression type changed
212                  */
213                 struct resource_entry output_resource_entry;
214
215                 struct list_head msg_list;
216                 struct list_head inode_list;
217         };
218
219         /* List of lookup table entries that correspond to streams that have
220          * been extracted to the staging directory when modifying a read-write
221          * mounted WIM.
222          *
223          * This field is also used to make other lists of lookup table entries.
224          * */
225         struct list_head staging_list;
226 };
227
228 static inline u64
229 wim_resource_size(const struct wim_lookup_table_entry *lte)
230 {
231         return lte->resource_entry.original_size;
232 }
233
234 static inline u64
235 wim_resource_chunks(const struct wim_lookup_table_entry *lte)
236 {
237         return (wim_resource_size(lte) + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
238 }
239
240 static inline u64
241 wim_resource_compressed_size(const struct wim_lookup_table_entry *lte)
242 {
243         return lte->resource_entry.size;
244 }
245
246 /*
247  * XXX Probably should store the compression type directly in the lookup table
248  * entry
249  */
250 static inline int
251 wim_resource_compression_type(const struct wim_lookup_table_entry *lte)
252 {
253         if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
254             || lte->resource_location != RESOURCE_IN_WIM)
255                 return WIMLIB_COMPRESSION_TYPE_NONE;
256         return wimlib_get_compression_type(lte->wim);
257 }
258
259
260 extern struct wim_lookup_table *
261 new_lookup_table(size_t capacity);
262
263 extern int
264 read_lookup_table(WIMStruct *w);
265
266 extern int
267 write_lookup_table(struct wim_lookup_table *table, FILE *out,
268                    struct resource_entry *out_res_entry);
269 extern void
270 free_lookup_table(struct wim_lookup_table *table);
271
272 extern void
273 lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte);
274
275 /* Unlinks a lookup table entry from the table; does not free it. */
276 static inline void
277 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
278 {
279         hlist_del(&lte->hash_list);
280         table->num_entries--;
281 }
282
283 extern struct wim_lookup_table_entry *
284 new_lookup_table_entry();
285
286 extern struct wim_lookup_table_entry *
287 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte);
288
289 extern void
290 print_lookup_table_entry(const struct wim_lookup_table_entry *entry,
291                          FILE *out);
292
293 extern void
294 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
295
296 extern int
297 for_lookup_table_entry(struct wim_lookup_table *table,
298                        int (*visitor)(struct wim_lookup_table_entry *, void *),
299                        void *arg);
300
301 extern struct wim_lookup_table_entry *
302 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
303
304 extern int
305 lookup_resource(WIMStruct *w, const mbchar *path,
306                 int lookup_flags, struct wim_dentry **dentry_ret,
307                 struct wim_lookup_table_entry **lte_ret, u16 *stream_idx_ret);
308
309 extern void
310 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
311                      struct wim_lookup_table *table);
312 #ifdef WITH_FUSE
313 extern void
314 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
315 #endif
316
317 extern int
318 lte_zero_out_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
319
320 extern int
321 lte_zero_real_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
322
323 extern int
324 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
325
326 extern void
327 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table);
328
329 extern void
330 inode_unresolve_ltes(struct wim_inode *inode);
331
332 extern int
333 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *__out);
334
335 static inline struct resource_entry*
336 wim_metadata_resource_entry(WIMStruct *w)
337 {
338         return &w->image_metadata[
339                         w->current_image - 1].metadata_lte->resource_entry;
340 }
341
342 static inline struct wim_lookup_table_entry *
343 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
344 {
345         wimlib_assert(inode->i_resolved);
346         wimlib_assert(stream_idx <= inode->i_num_ads);
347         if (stream_idx == 0)
348                 return inode->i_lte;
349         else
350                 return inode->i_ads_entries[stream_idx - 1].lte;
351 }
352
353 static inline struct wim_lookup_table_entry *
354 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
355                             const struct wim_lookup_table *table)
356 {
357         wimlib_assert(!inode->i_resolved);
358         wimlib_assert(stream_idx <= inode->i_num_ads);
359         if (!table)
360                 return NULL;
361         if (stream_idx == 0)
362                 return __lookup_resource(table, inode->i_hash);
363         else
364                 return __lookup_resource(table,
365                                          inode->i_ads_entries[
366                                                 stream_idx - 1].hash);
367 }
368
369 extern struct wim_lookup_table_entry *
370 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
371                  const struct wim_lookup_table *table);
372
373 static inline const u8 *
374 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
375 {
376         wimlib_assert(!inode->i_resolved);
377         wimlib_assert(stream_idx <= inode->i_num_ads);
378         if (stream_idx == 0)
379                 return inode->i_hash;
380         else
381                 return inode->i_ads_entries[stream_idx - 1].hash;
382 }
383
384
385 static inline const u8 *
386 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
387 {
388         struct wim_lookup_table_entry *lte;
389         lte = inode_stream_lte_resolved(inode, stream_idx);
390         if (lte)
391                 return lte->hash;
392         else
393                 return zero_hash;
394 }
395
396 /*
397  * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
398  * means the default un-named file stream, and stream_idx >= 1 corresponds to an
399  * alternate data stream.
400  *
401  * This works for both resolved and un-resolved dentries.
402  */
403 static inline const u8 *
404 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
405 {
406         if (inode->i_resolved)
407                 return inode_stream_hash_resolved(inode, stream_idx);
408         else
409                 return inode_stream_hash_unresolved(inode, stream_idx);
410 }
411
412 static inline u16
413 inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
414 {
415         wimlib_assert(stream_idx <= inode->i_num_ads);
416         if (stream_idx == 0)
417                 return 0;
418         else
419                 return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
420 }
421
422 static inline struct wim_lookup_table_entry *
423 inode_unnamed_lte_resolved(const struct wim_inode *inode)
424 {
425         wimlib_assert(inode->i_resolved);
426         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
427                 if (inode_stream_name_nbytes(inode, i) == 0 &&
428                     !is_zero_hash(inode_stream_hash_resolved(inode, i)))
429                 {
430                         return inode_stream_lte_resolved(inode, i);
431                 }
432         }
433         return NULL;
434 }
435
436 static inline struct wim_lookup_table_entry *
437 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
438                              const struct wim_lookup_table *table)
439 {
440         wimlib_assert(!inode->i_resolved);
441         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
442                 if (inode_stream_name_nbytes(inode, i) == 0 &&
443                     !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
444                 {
445                         return inode_stream_lte_unresolved(inode, i, table);
446                 }
447         }
448         return NULL;
449 }
450
451 extern struct wim_lookup_table_entry *
452 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
453
454 extern u64
455 lookup_table_total_stream_size(struct wim_lookup_table *table);
456
457 #endif