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