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