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