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