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