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