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