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