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