]> wimlib.net Git - wimlib/blob - src/lookup_table.h
ntfs capture: Store security descriptors in rbtree
[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
276 extern void
277 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
278
279 extern int
280 for_lookup_table_entry(struct wim_lookup_table *table,
281                        int (*visitor)(struct wim_lookup_table_entry *, void *),
282                        void *arg);
283
284 extern struct wim_lookup_table_entry *
285 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
286
287 extern int
288 lookup_resource(WIMStruct *w, const char *path,
289                 int lookup_flags, struct wim_dentry **dentry_ret,
290                 struct wim_lookup_table_entry **lte_ret, u16 *stream_idx_ret);
291
292 extern void
293 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
294                      struct wim_lookup_table *table);
295 #ifdef WITH_FUSE
296 extern void
297 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte);
298 #endif
299
300 extern int
301 lte_zero_out_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
302
303 extern int
304 lte_zero_real_refcnt(struct wim_lookup_table_entry *entry, void *ignore);
305
306 extern int
307 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *ignore);
308
309 extern void
310 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table);
311
312 extern void
313 inode_unresolve_ltes(struct wim_inode *inode);
314
315 extern int
316 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *__out);
317
318 static inline struct resource_entry*
319 wim_metadata_resource_entry(WIMStruct *w)
320 {
321         return &w->image_metadata[
322                         w->current_image - 1].metadata_lte->resource_entry;
323 }
324
325 static inline struct wim_lookup_table_entry *
326 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
327 {
328         wimlib_assert(inode->i_resolved);
329         wimlib_assert(stream_idx <= inode->i_num_ads);
330         if (stream_idx == 0)
331                 return inode->i_lte;
332         else
333                 return inode->i_ads_entries[stream_idx - 1].lte;
334 }
335
336 static inline struct wim_lookup_table_entry *
337 inode_stream_lte_unresolved(const struct wim_inode *inode, unsigned stream_idx,
338                             const struct wim_lookup_table *table)
339 {
340         wimlib_assert(!inode->i_resolved);
341         wimlib_assert(stream_idx <= inode->i_num_ads);
342         if (!table)
343                 return NULL;
344         if (stream_idx == 0)
345                 return __lookup_resource(table, inode->i_hash);
346         else
347                 return __lookup_resource(table,
348                                          inode->i_ads_entries[
349                                                 stream_idx - 1].hash);
350 }
351
352 extern struct wim_lookup_table_entry *
353 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
354                  const struct wim_lookup_table *table);
355
356 static inline const u8 *
357 inode_stream_hash_unresolved(const struct wim_inode *inode, unsigned stream_idx)
358 {
359         wimlib_assert(!inode->i_resolved);
360         wimlib_assert(stream_idx <= inode->i_num_ads);
361         if (stream_idx == 0)
362                 return inode->i_hash;
363         else
364                 return inode->i_ads_entries[stream_idx - 1].hash;
365 }
366
367
368 static inline const u8 *
369 inode_stream_hash_resolved(const struct wim_inode *inode, unsigned stream_idx)
370 {
371         struct wim_lookup_table_entry *lte;
372         lte = inode_stream_lte_resolved(inode, stream_idx);
373         if (lte)
374                 return lte->hash;
375         else
376                 return zero_hash;
377 }
378
379 /*
380  * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
381  * means the default un-named file stream, and stream_idx >= 1 corresponds to an
382  * alternate data stream.
383  *
384  * This works for both resolved and un-resolved dentries.
385  */
386 static inline const u8 *
387 inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
388 {
389         if (inode->i_resolved)
390                 return inode_stream_hash_resolved(inode, stream_idx);
391         else
392                 return inode_stream_hash_unresolved(inode, stream_idx);
393 }
394
395 static inline u16
396 inode_stream_name_len(const struct wim_inode *inode, unsigned stream_idx)
397 {
398         wimlib_assert(stream_idx <= inode->i_num_ads);
399         if (stream_idx == 0)
400                 return 0;
401         else
402                 return inode->i_ads_entries[stream_idx - 1].stream_name_len;
403 }
404
405 static inline struct wim_lookup_table_entry *
406 inode_unnamed_lte_resolved(const struct wim_inode *inode)
407 {
408         wimlib_assert(inode->i_resolved);
409         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
410                 if (inode_stream_name_len(inode, i) == 0 &&
411                     !is_zero_hash(inode_stream_hash_resolved(inode, i)))
412                 {
413                         return inode_stream_lte_resolved(inode, i);
414                 }
415         }
416         return NULL;
417 }
418
419 static inline struct wim_lookup_table_entry *
420 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
421                              const struct wim_lookup_table *table)
422 {
423         wimlib_assert(!inode->i_resolved);
424         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
425                 if (inode_stream_name_len(inode, i) == 0 &&
426                     !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
427                 {
428                         return inode_stream_lte_unresolved(inode, i, table);
429                 }
430         }
431         return NULL;
432 }
433
434 extern struct wim_lookup_table_entry *
435 inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *table);
436
437 extern u64
438 lookup_table_total_stream_size(struct wim_lookup_table *table);
439
440 #endif