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