]> wimlib.net Git - wimlib/blob - src/lookup_table.h
Remove duplicate ntfs typedefs
[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 struct ntfs_location {
31         char *path_utf8;
32         char *stream_name_utf16;
33         u16 stream_name_utf16_num_chars;
34         struct _ntfs_volume **ntfs_vol_p;
35         bool is_reparse_point;
36 };
37 #endif
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         #ifdef WITH_NTFS_3G
99                 struct ntfs_location *ntfs_loc;
100         #endif
101         };
102         union {
103                 struct lookup_table_entry *next_lte_in_swm;
104                 FILE *file_on_disk_fp;
105         #ifdef WITH_NTFS_3G
106                 struct _ntfs_attr *attr;
107         #endif
108         };
109 #ifdef WITH_FUSE
110         /* File descriptors table for this data stream */
111         u16 num_opened_fds;
112         u16 num_allocated_fds;
113         struct wimlib_fd **fds;
114 #endif
115
116         /* When a WIM file is written, out_refcnt starts at 0 and is incremented
117          * whenever the file resource pointed to by this lookup table entry
118          * needs to be written.  Naturally, the file resource only need to be
119          * written when out_refcnt is 0.  Incrementing it further is needed to
120          * find the correct reference count to write to the lookup table in the
121          * output file, which may be less than the regular refcnt if not all
122          * images in the WIM file are written. 
123          *
124          * output_resource_entry is the struct resource_entry for the position of the
125          * file resource when written to the output file. */
126         u32 out_refcnt;
127         union {
128                 struct resource_entry output_resource_entry;
129                 char *extracted_file;
130         };
131
132         /* Circular linked list of streams that share the same lookup table
133          * entry
134          * 
135          * This list of streams may include streams from different hard link
136          * sets that happen to be the same.  */
137         struct list_head lte_group_list;
138
139         /* List of lookup table entries that correspond to streams that have
140          * been extracted to the staging directory when modifying a read-write
141          * mounted WIM. */
142         struct list_head staging_list;
143 };
144
145 static inline u64 wim_resource_size(const struct lookup_table_entry *lte)
146 {
147         return lte->resource_entry.original_size;
148 }
149
150 static inline u64
151 wim_resource_compressed_size(const struct lookup_table_entry *lte)
152 {
153         return lte->resource_entry.size;
154 }
155
156 /*
157  * XXX Probably should store the compression type directly in the lookup table
158  * entry
159  */
160 static inline int
161 wim_resource_compression_type(const struct lookup_table_entry *lte)
162 {
163         if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
164             || lte->resource_location != RESOURCE_IN_WIM)
165                 return WIM_COMPRESSION_TYPE_NONE;
166         return wimlib_get_compression_type(lte->wim);
167 }
168
169
170 extern struct lookup_table *new_lookup_table(size_t capacity);
171
172 extern void lookup_table_insert(struct lookup_table *table, 
173                                 struct lookup_table_entry *lte);
174
175 /* Unlinks a lookup table entry from the table; does not free it. */
176 static inline void lookup_table_unlink(struct lookup_table *table, 
177                                        struct lookup_table_entry *lte)
178 {
179         hlist_del(&lte->hash_list);
180         table->num_entries--;
181 }
182
183
184 extern struct lookup_table_entry *
185 lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[]);
186
187 extern struct lookup_table_entry *
188 lte_decrement_refcnt(struct lookup_table_entry *lte,
189                      struct lookup_table *table);
190
191
192 extern struct lookup_table_entry *new_lookup_table_entry();
193
194 extern int for_lookup_table_entry(struct lookup_table *table, 
195                                   int (*visitor)(struct lookup_table_entry *, void *), 
196                                   void *arg);
197
198 extern struct lookup_table_entry *
199 __lookup_resource(const struct lookup_table *table, const u8 hash[]);
200
201 extern int lookup_resource(WIMStruct *w, const char *path,
202                            int lookup_flags, struct dentry **dentry_ret,
203                            struct lookup_table_entry **lte_ret,
204                            unsigned *stream_idx_ret);
205
206 extern int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore);
207
208 extern void print_lookup_table_entry(const struct lookup_table_entry *entry);
209
210 extern int read_lookup_table(WIMStruct *w);
211
212 extern void free_lookup_table(struct lookup_table *table);
213
214 extern int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out);
215
216 extern void free_lookup_table_entry(struct lookup_table_entry *lte);
217
218 extern int dentry_resolve_ltes(struct dentry *dentry, void *__table);
219
220 /* Writes the lookup table to the output file. */
221 static inline int write_lookup_table(struct lookup_table *table, FILE *out)
222 {
223         return for_lookup_table_entry(table, write_lookup_table_entry, out);
224 }
225
226 /* Unlinks and frees an entry from a lookup table. */
227 static inline void lookup_table_remove(struct lookup_table *table, 
228                                        struct lookup_table_entry *lte)
229 {
230         lookup_table_unlink(table, lte);
231         free_lookup_table_entry(lte);
232 }
233
234 static inline struct resource_entry* wim_metadata_resource_entry(WIMStruct *w)
235 {
236         return &w->image_metadata[
237                         w->current_image - 1].metadata_lte->resource_entry;
238 }
239
240 static inline struct lookup_table_entry *
241 dentry_stream_lte_resolved(const struct dentry *dentry, unsigned stream_idx)
242 {
243         wimlib_assert(dentry->resolved);
244         wimlib_assert(stream_idx <= dentry->num_ads);
245         if (stream_idx == 0)
246                 return dentry->lte;
247         else
248                 return dentry->ads_entries[stream_idx - 1].lte;
249 }
250
251 static inline struct lookup_table_entry *
252 dentry_stream_lte_unresolved(const struct dentry *dentry, unsigned stream_idx,
253                              const struct lookup_table *table)
254 {
255         wimlib_assert(!dentry->resolved);
256         wimlib_assert(stream_idx <= dentry->num_ads);
257         if (!table)
258                 return NULL;
259         if (stream_idx == 0)
260                 return __lookup_resource(table, dentry->hash);
261         else
262                 return __lookup_resource(table,
263                                          dentry->ads_entries[
264                                                 stream_idx - 1].hash);
265 }
266 /* 
267  * Returns the lookup table entry for stream @stream_idx of the dentry, where
268  * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
269  * corresponds to an alternate data stream.
270  *
271  * This works for both resolved and un-resolved dentries.
272  */
273 static inline struct lookup_table_entry *
274 dentry_stream_lte(const struct dentry *dentry, unsigned stream_idx,
275                   const struct lookup_table *table)
276 {
277         if (dentry->resolved)
278                 return dentry_stream_lte_resolved(dentry, stream_idx);
279         else
280                 return dentry_stream_lte_unresolved(dentry, stream_idx, table);
281 }
282
283
284 static inline const u8 *dentry_stream_hash_unresolved(const struct dentry *dentry,
285                                                       unsigned stream_idx)
286 {
287         wimlib_assert(!dentry->resolved);
288         wimlib_assert(stream_idx <= dentry->num_ads);
289         if (stream_idx == 0)
290                 return dentry->hash;
291         else
292                 return dentry->ads_entries[stream_idx - 1].hash;
293 }
294
295 static inline u16 dentry_stream_name_len(const struct dentry *dentry,
296                                          unsigned stream_idx)
297 {
298         wimlib_assert(stream_idx <= dentry->num_ads);
299         if (stream_idx == 0)
300                 return 0;
301         else
302                 return dentry->ads_entries[stream_idx - 1].stream_name_len;
303 }
304
305 static inline const u8 *dentry_stream_hash_resolved(const struct dentry *dentry,
306                                                     unsigned stream_idx)
307 {
308         struct lookup_table_entry *lte;
309         lte = dentry_stream_lte_resolved(dentry, stream_idx);
310         if (lte)
311                 return lte->hash;
312         else
313                 return NULL;
314 }
315
316 /* 
317  * Returns the hash for stream @stream_idx of the dentry, where stream_idx = 0
318  * means the default un-named file stream, and stream_idx >= 1 corresponds to an
319  * alternate data stream.
320  *
321  * This works for both resolved and un-resolved dentries.
322  */
323 static inline const u8 *dentry_stream_hash(const struct dentry *dentry,
324                                            unsigned stream_idx)
325 {
326         if (dentry->resolved)
327                 return dentry_stream_hash_resolved(dentry, stream_idx);
328         else
329                 return dentry_stream_hash_unresolved(dentry, stream_idx);
330 }
331
332 static inline struct lookup_table_entry *
333 dentry_unnamed_lte_resolved(const struct dentry *dentry)
334 {
335         wimlib_assert(dentry->resolved);
336         for (unsigned i = 0; i <= dentry->num_ads; i++)
337                 if (dentry_stream_name_len(dentry, i) == 0 &&
338                      !is_zero_hash(dentry_stream_hash_resolved(dentry, i)))
339                         return dentry_stream_lte_resolved(dentry, i);
340         return NULL;
341 }
342
343 static inline struct lookup_table_entry *
344 dentry_unnamed_lte_unresolved(const struct dentry *dentry,
345                               const struct lookup_table *table)
346 {
347         wimlib_assert(!dentry->resolved);
348         for (unsigned i = 0; i <= dentry->num_ads; i++)
349                 if (dentry_stream_name_len(dentry, i) == 0 &&
350                      !is_zero_hash(dentry_stream_hash_unresolved(dentry, i)))
351                         return dentry_stream_lte_unresolved(dentry, i, table);
352         return NULL;
353 }
354
355 extern struct lookup_table_entry *
356 dentry_unnamed_lte(const struct dentry *dentry,
357                    const struct lookup_table *table);
358
359 #endif