]> wimlib.net Git - wimlib/blob - src/lookup_table.h
Extract NTFS junction points correctly
[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 #define LOOKUP_FLAG_FOLLOW_SYMLINKS     0x00000004
14
15
16 /* A lookup table that is used to translate the hash codes of dentries into the
17  * offsets and sizes of uncompressed or compressed file resources.  It is
18  * implemented as a hash table. */
19 struct lookup_table {
20         struct hlist_head *array;
21         u64 num_entries;
22         u64 capacity;
23 };
24
25 struct wimlib_fd;
26
27 /* 
28  * An entry in the lookup table in the WIM file. 
29  *
30  * It is used to find data streams for files in the WIM. 
31  *
32  * The lookup_table_entry for a given dentry in the WIM is found using the SHA1
33  * message digest field. 
34  */
35 struct lookup_table_entry {
36
37         /* List of lookup table entries in this hash bucket */
38         struct hlist_node hash_list;
39
40         /* @resource_entry is read from the lookup table in the WIM
41          * file; it says where to find the file resource in the WIM
42          * file, and whether it is compressed or not. */
43         struct resource_entry resource_entry;
44
45         /* Currently ignored; set to 1 in new lookup table entries. */
46         u16 part_number;
47
48         /* If %true, this lookup table entry corresponds to a symbolic link
49          * reparse buffer.  @symlink_reparse_data_buf will give the target of
50          * the symbolic link. */
51         enum {
52                 RESOURCE_NONEXISTENT = 0,
53                 RESOURCE_IN_WIM,
54                 RESOURCE_IN_FILE_ON_DISK,
55                 RESOURCE_IN_STAGING_FILE,
56                 RESOURCE_IN_ATTACHED_BUFFER,
57         } resource_location;
58
59         /* Number of times this lookup table entry is referenced by dentries. */
60         u32 refcnt;
61
62         union {
63                 /* SHA1 hash of the file resource pointed to by this lookup
64                  * table entry */
65                 u8  hash[SHA1_HASH_SIZE];
66
67                 /* First 4 or 8 bytes of the SHA1 hash, used for inserting the
68                  * entry into the hash table.  Since the SHA1 hashes can be
69                  * considered random, we don't really need the full 20 byte hash
70                  * just to insert the entry in a hash table. */
71                 size_t hash_short;
72         };
73
74         /* If @file_on_disk != NULL, the file resource indicated by this lookup
75          * table entry is not in the WIM file, but rather a file on disk; this
76          * occurs for files that are added to the WIM.  In that case,
77          * file_on_disk is the name of the file in the outside filesystem.  
78          * It will not be compressed, and its size will be given by
79          * resource_entry.size and resource_entry.original_size. */
80         union {
81                 WIMStruct *wim;
82                 char *file_on_disk;
83                 char *staging_file_name;
84                 u8 *attached_buffer;
85         };
86         union {
87                 struct lookup_table_entry *next_lte_in_swm;
88                 FILE *file_on_disk_fp;
89         };
90 #ifdef WITH_FUSE
91         /* File descriptors table for this data stream */
92         u16 num_opened_fds;
93         u16 num_allocated_fds;
94         struct wimlib_fd **fds;
95 #endif
96
97         /* When a WIM file is written, out_refcnt starts at 0 and is incremented
98          * whenever the file resource pointed to by this lookup table entry
99          * needs to be written.  Naturally, the file resource only need to be
100          * written when out_refcnt is 0.  Incrementing it further is needed to
101          * find the correct reference count to write to the lookup table in the
102          * output file, which may be less than the regular refcnt if not all
103          * images in the WIM file are written. 
104          *
105          * output_resource_entry is the struct resource_entry for the position of the
106          * file resource when written to the output file. */
107         u32 out_refcnt;
108         union {
109                 struct resource_entry output_resource_entry;
110                 char *extracted_file;
111         };
112
113         /* Circular linked list of streams that share the same lookup table
114          * entry
115          * 
116          * This list of streams may include streams from different hard link
117          * sets that happen to be the same.  */
118         struct list_head lte_group_list;
119
120         /* List of lookup table entries that correspond to streams that have
121          * been extracted to the staging directory when modifying a read-write
122          * mounted WIM. */
123         struct list_head staging_list;
124 };
125
126 static inline u64 wim_resource_size(const struct lookup_table_entry *lte)
127 {
128         return lte->resource_entry.original_size;
129 }
130
131 static inline u64
132 wim_resource_compressed_size(const struct lookup_table_entry *lte)
133 {
134         return lte->resource_entry.size;
135 }
136
137 /*
138  * XXX Probably should store the compression type directly in the lookup table
139  * entry
140  */
141 static inline int
142 wim_resource_compression_type(const struct lookup_table_entry *lte)
143 {
144         if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
145             || lte->resource_location != RESOURCE_IN_WIM)
146                 return WIM_COMPRESSION_TYPE_NONE;
147         return wimlib_get_compression_type(lte->wim);
148 }
149
150
151 extern struct lookup_table *new_lookup_table(size_t capacity);
152
153 extern void lookup_table_insert(struct lookup_table *table, 
154                                 struct lookup_table_entry *lte);
155
156 /* Unlinks a lookup table entry from the table; does not free it. */
157 static inline void lookup_table_unlink(struct lookup_table *table, 
158                                        struct lookup_table_entry *lte)
159 {
160         hlist_del(&lte->hash_list);
161         table->num_entries--;
162 }
163
164
165 extern struct lookup_table_entry *
166 lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[]);
167
168 extern struct lookup_table_entry *
169 lte_decrement_refcnt(struct lookup_table_entry *lte,
170                      struct lookup_table *table);
171
172
173 extern struct lookup_table_entry *new_lookup_table_entry();
174
175 extern int for_lookup_table_entry(struct lookup_table *table, 
176                                   int (*visitor)(struct lookup_table_entry *, void *), 
177                                   void *arg);
178
179 extern struct lookup_table_entry *
180 __lookup_resource(const struct lookup_table *table, const u8 hash[]);
181
182 extern int lookup_resource(WIMStruct *w, const char *path,
183                            int lookup_flags, struct dentry **dentry_ret,
184                            struct lookup_table_entry **lte_ret,
185                            unsigned *stream_idx_ret);
186
187 extern int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore);
188
189 extern void print_lookup_table_entry(const struct lookup_table_entry *entry);
190
191 extern int read_lookup_table(WIMStruct *w);
192
193 extern void free_lookup_table(struct lookup_table *table);
194
195 extern int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out);
196
197 extern void free_lookup_table_entry(struct lookup_table_entry *lte);
198
199 extern int dentry_resolve_ltes(struct dentry *dentry, void *__table);
200
201 /* Writes the lookup table to the output file. */
202 static inline int write_lookup_table(struct lookup_table *table, FILE *out)
203 {
204         return for_lookup_table_entry(table, write_lookup_table_entry, out);
205 }
206
207 /* Unlinks and frees an entry from a lookup table. */
208 static inline void lookup_table_remove(struct lookup_table *table, 
209                                        struct lookup_table_entry *lte)
210 {
211         lookup_table_unlink(table, lte);
212         free_lookup_table_entry(lte);
213 }
214
215 static inline struct resource_entry* wim_metadata_resource_entry(WIMStruct *w)
216 {
217         return &w->image_metadata[
218                         w->current_image - 1].metadata_lte->resource_entry;
219 }
220
221 static inline struct lookup_table_entry *
222 dentry_stream_lte_resolved(const struct dentry *dentry, unsigned stream_idx)
223 {
224         wimlib_assert(dentry->resolved);
225         wimlib_assert(stream_idx <= dentry->num_ads);
226         if (stream_idx == 0)
227                 return dentry->lte;
228         else
229                 return dentry->ads_entries[stream_idx - 1].lte;
230 }
231
232 static inline struct lookup_table_entry *
233 dentry_stream_lte_unresolved(const struct dentry *dentry, unsigned stream_idx,
234                              const struct lookup_table *table)
235 {
236         wimlib_assert(!dentry->resolved);
237         wimlib_assert(stream_idx <= dentry->num_ads);
238         if (!table)
239                 return NULL;
240         if (stream_idx == 0)
241                 return __lookup_resource(table, dentry->hash);
242         else
243                 return __lookup_resource(table,
244                                          dentry->ads_entries[
245                                                 stream_idx - 1].hash);
246 }
247 /* 
248  * Returns the lookup table entry for stream @stream_idx of the dentry, where
249  * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
250  * corresponds to an alternate data stream.
251  *
252  * This works for both resolved and un-resolved dentries.
253  */
254 static inline struct lookup_table_entry *
255 dentry_stream_lte(const struct dentry *dentry, unsigned stream_idx,
256                   const struct lookup_table *table)
257 {
258         if (dentry->resolved)
259                 return dentry_stream_lte_resolved(dentry, stream_idx);
260         else
261                 return dentry_stream_lte_unresolved(dentry, stream_idx, table);
262 }
263
264
265 static inline const u8 *dentry_stream_hash_unresolved(const struct dentry *dentry,
266                                                       unsigned stream_idx)
267 {
268         wimlib_assert(!dentry->resolved);
269         wimlib_assert(stream_idx <= dentry->num_ads);
270         if (stream_idx == 0)
271                 return dentry->hash;
272         else
273                 return dentry->ads_entries[stream_idx - 1].hash;
274 }
275
276 static inline const u8 *dentry_stream_hash_resolved(const struct dentry *dentry,
277                                                     unsigned stream_idx)
278 {
279         struct lookup_table_entry *lte;
280         lte = dentry_stream_lte_resolved(dentry, stream_idx);
281         if (lte)
282                 return lte->hash;
283         else
284                 return NULL;
285 }
286
287 /* 
288  * Returns the hash for stream @stream_idx of the dentry, where stream_idx = 0
289  * means the default un-named file stream, and stream_idx >= 1 corresponds to an
290  * alternate data stream.
291  *
292  * This works for both resolved and un-resolved dentries.
293  */
294 static inline const u8 *dentry_stream_hash(const struct dentry *dentry,
295                                            unsigned stream_idx)
296 {
297         if (dentry->resolved)
298                 return dentry_stream_hash_resolved(dentry, stream_idx);
299         else
300                 return dentry_stream_hash_unresolved(dentry, stream_idx);
301 }
302
303 static inline struct lookup_table_entry *
304 dentry_first_lte_resolved(const struct dentry *dentry)
305 {
306         struct lookup_table_entry *lte;
307         wimlib_assert(dentry->resolved);
308
309         for (unsigned i = 0; i <= dentry->num_ads; i++) {
310                 lte = dentry_stream_lte_resolved(dentry, i);
311                 if (lte)
312                         return lte;
313         }
314         return NULL;
315 }
316
317 static inline struct lookup_table_entry *
318 dentry_first_lte_unresolved(const struct dentry *dentry,
319                             const struct lookup_table *table)
320 {
321         struct lookup_table_entry *lte;
322         wimlib_assert(!dentry->resolved);
323
324         for (unsigned i = 0; i <= dentry->num_ads; i++) {
325                 lte = dentry_stream_lte_unresolved(dentry, i, table);
326                 if (lte)
327                         return lte;
328         }
329         return NULL;
330 }
331
332 extern struct lookup_table_entry *
333 dentry_first_lte(const struct dentry *dentry, const struct lookup_table *table);
334
335 #endif