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