]> wimlib.net Git - wimlib/blob - src/lookup_table.h
ntfs-apply.c: Tweak NTFS inode closing some more
[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 #ifdef WITH_NTFS_3G
28 struct ntfs_location {
29         char *path_utf8;
30         char *stream_name_utf16;
31         u16 stream_name_utf16_num_chars;
32         struct _ntfs_volume **ntfs_vol_p;
33         bool is_reparse_point;
34 };
35 #endif
36
37 /* An enumerated type that identifies where the stream corresponding to this
38  * lookup table entry is actually located.
39  *
40  * If we open a WIM and read its lookup table, the location is set to
41  * RESOURCE_IN_WIM since all the streams will initially be located in the WIM.
42  * However, to deal with problems such as image capture and image mount, we
43  * allow the actual location of the stream to be somewhere else, such as an
44  * external file.
45  */
46 enum resource_location {
47         /* The lookup table entry does not correspond to a stream (this state
48          * should exist only temporarily) */
49         RESOURCE_NONEXISTENT = 0,
50
51         /* The stream resource is located in a WIM file.  The WIMStruct for the
52          * WIM file will be pointed to by the @wim member. */
53         RESOURCE_IN_WIM,
54
55         /* The stream resource is located in an external file.  The name of the
56          * file will be provided by @file_on_disk member.  In addition, if
57          * @file_on_disk_fp is not NULL, it will be an open FILE * to the file.
58          * */
59         RESOURCE_IN_FILE_ON_DISK,
60
61         /* The stream resource is located in an external file in the staging
62          * directory for a read-write mount.  */
63         RESOURCE_IN_STAGING_FILE,
64
65         /* The stream resource is directly attached in an in-memory buffer
66          * pointed to by @attached_buffer. */
67         RESOURCE_IN_ATTACHED_BUFFER,
68
69         /* The stream resource is located in an NTFS volume.  It is identified
70          * by volume, filename, data stream name, and by whether it is a reparse
71          * point or not. @ntfs_loc points to a structure containing this
72          * information. */
73         RESOURCE_IN_NTFS_VOLUME,
74 };
75
76 /*
77  * An entry in the lookup table in the WIM file.
78  *
79  * It is used to find data streams for files in the WIM.
80  *
81  * Metadata resources and reparse point data buffers will also have lookup table
82  * entries associated with the data.
83  *
84  * The lookup_table_entry for a given dentry or alternate stream entry in the
85  * WIM is found using the SHA1 message digest field.
86  */
87 struct lookup_table_entry {
88
89         /* List of lookup table entries in this hash bucket */
90         struct hlist_node hash_list;
91
92         /* Location and size of the stream in the WIM, whether it is compressed
93          * or not, and whether it's a metadata resource or not.  This is an
94          * on-disk field. */
95         struct resource_entry resource_entry;
96
97         /* Specifies which part of the split WIM the resource is located in.
98          * This is on on-disk field.
99          *
100          * In stand-alone WIMs, this must be 1.
101          *
102          * In split WIMs, every split WIM part has its own lookup table, and in
103          * read_lookup_table() it's currently expected that the part number of
104          * each lookup table entry in a split WIM part's lookup table is the
105          * same as the part number of that split WIM part.  So this makes this
106          * field redundant since we store a pointer to the corresponding
107          * WIMStruct in the lookup table entry anyway.
108          */
109         u16 part_number;
110
111         /* See enum resource_location above */
112         u16 resource_location;
113
114         /* (On-disk field)
115          * Number of times this lookup table entry is referenced by dentries.
116          * Unfortunately, this field is not always set correctly in Microsoft's
117          * WIMs, so we have no choice but to fix it if more references to the
118          * lookup table entry are found than stated here. */
119         u32 refcnt;
120
121         union {
122                 /* (On-disk field) SHA1 message digest of the stream referenced
123                  * by this lookup table entry */
124                 u8  hash[SHA1_HASH_SIZE];
125
126                 /* First 4 or 8 bytes of the SHA1 message digest, used for
127                  * inserting the entry into the hash table.  Since the SHA1
128                  * message digest can be considered random, we don't really need
129                  * the full 20 byte hash just to insert the entry in a hash
130                  * table. */
131                 size_t hash_short;
132         };
133
134         union {
135                 #ifdef WITH_FUSE
136                 u16 num_opened_fds;
137                 #endif
138
139                 /* This field is used for the special hardlink or symlink image
140                  * extraction mode.   In these mode, all identical files are linked
141                  * together, and @extracted_file will be set to the filename of the
142                  * first extracted file containing this stream.  */
143                 char *extracted_file;
144         };
145
146         /* Pointers to somewhere where the stream is actually located.  See the
147          * comments for the @resource_location field above. */
148         union {
149                 WIMStruct *wim;
150                 char *file_on_disk;
151                 char *staging_file_name;
152                 u8 *attached_buffer;
153         #ifdef WITH_NTFS_3G
154                 struct ntfs_location *ntfs_loc;
155         #endif
156         };
157         union {
158                 /* @file_on_disk_fp and @attr are both used to cache file/stream
159                  * handles so we don't have re-open them on every read */
160
161                 /* Valid iff resource_location == RESOURCE_IN_FILE_ON_DISK */
162                 FILE *file_on_disk_fp;
163         #ifdef WITH_NTFS_3G
164                 /* Valid iff resource_location == RESOURCE_IN_NTFS_VOLUME */
165                 struct _ntfs_attr *attr;
166         #endif
167
168                 /* Pointer to inode that contains the opened file descriptors to
169                  * this stream (valid iff resource_location ==
170                  * RESOURCE_IN_STAGING_FILE) */
171                 struct inode *lte_inode;
172         };
173
174         /* When a WIM file is written, out_refcnt starts at 0 and is incremented
175          * whenever the file resource pointed to by this lookup table entry
176          * needs to be written.  The file resource only need to be written when
177          * out_refcnt is nonzero, since otherwise it is not referenced by any
178          * dentries. */
179         u32 out_refcnt;
180
181         u32 real_refcnt;
182
183         union {
184                 /* When a WIM file is written, @output_resource_entry is filled
185                  * in with the resource entry for the output WIM.  This will not
186                  * necessarily be the same as the @resource_entry since: - The
187                  * stream may have a different offset in the new WIM - The
188                  * stream may have a different compressed size in the new WIM if
189                  * the compression type changed
190                  */
191                 struct resource_entry output_resource_entry;
192
193                 struct list_head msg_list;
194                 struct list_head inode_list;
195         };
196
197         /* List of lookup table entries that correspond to streams that have
198          * been extracted to the staging directory when modifying a read-write
199          * mounted WIM.
200          *
201          * This field is also used to make other lists of lookup table entries.
202          * */
203         struct list_head staging_list;
204 };
205
206 static inline u64
207 wim_resource_size(const struct lookup_table_entry *lte)
208 {
209         return lte->resource_entry.original_size;
210 }
211
212 static inline u64
213 wim_resource_chunks(const struct lookup_table_entry *lte)
214 {
215         return (wim_resource_size(lte) + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
216 }
217
218 static inline u64
219 wim_resource_compressed_size(const struct lookup_table_entry *lte)
220 {
221         return lte->resource_entry.size;
222 }
223
224 /*
225  * XXX Probably should store the compression type directly in the lookup table
226  * entry
227  */
228 static inline int
229 wim_resource_compression_type(const struct lookup_table_entry *lte)
230 {
231         if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
232             || lte->resource_location != RESOURCE_IN_WIM)
233                 return WIMLIB_COMPRESSION_TYPE_NONE;
234         return wimlib_get_compression_type(lte->wim);
235 }
236
237
238 extern struct lookup_table *
239 new_lookup_table(size_t capacity);
240
241 extern int
242 read_lookup_table(WIMStruct *w);
243
244 extern int
245 write_lookup_table(struct lookup_table *table, FILE *out,
246                    struct resource_entry *out_res_entry);
247 extern void
248 free_lookup_table(struct lookup_table *table);
249
250 extern void
251 lookup_table_insert(struct lookup_table *table, struct lookup_table_entry *lte);
252
253 /* Unlinks a lookup table entry from the table; does not free it. */
254 static inline void
255 lookup_table_unlink(struct lookup_table *table, struct lookup_table_entry *lte)
256 {
257         hlist_del(&lte->hash_list);
258         table->num_entries--;
259 }
260
261 extern struct lookup_table_entry *
262 new_lookup_table_entry();
263
264 extern struct lookup_table_entry *
265 clone_lookup_table_entry(const struct lookup_table_entry *lte);
266
267 extern void
268 print_lookup_table_entry(const struct lookup_table_entry *entry);
269
270 extern void
271 free_lookup_table_entry(struct lookup_table_entry *lte);
272
273 extern int
274 for_lookup_table_entry(struct lookup_table *table,
275                        int (*visitor)(struct lookup_table_entry *, void *),
276                        void *arg);
277
278 extern struct lookup_table_entry *
279 __lookup_resource(const struct lookup_table *table, const u8 hash[]);
280
281 extern int
282 lookup_resource(WIMStruct *w, const char *path,
283                 int lookup_flags, struct dentry **dentry_ret,
284                 struct lookup_table_entry **lte_ret, u16 *stream_idx_ret);
285
286 extern void
287 lte_decrement_refcnt(struct lookup_table_entry *lte,
288                      struct lookup_table *table);
289 #ifdef WITH_FUSE
290 extern void
291 lte_decrement_num_opened_fds(struct lookup_table_entry *lte);
292 #endif
293
294 extern int
295 lte_zero_out_refcnt(struct lookup_table_entry *entry, void *ignore);
296
297 extern int
298 lte_zero_real_refcnt(struct lookup_table_entry *entry, void *ignore);
299
300 extern int
301 lte_free_extracted_file(struct lookup_table_entry *lte, void *ignore);
302
303 extern void
304 inode_resolve_ltes(struct inode *inode, struct lookup_table *table);
305
306 extern void
307 inode_unresolve_ltes(struct inode *inode);
308
309 extern int
310 write_lookup_table_entry(struct lookup_table_entry *lte, void *__out);
311
312 static inline struct resource_entry*
313 wim_metadata_resource_entry(WIMStruct *w)
314 {
315         return &w->image_metadata[
316                         w->current_image - 1].metadata_lte->resource_entry;
317 }
318
319 static inline struct lookup_table_entry *
320 inode_stream_lte_resolved(const struct inode *inode, unsigned stream_idx)
321 {
322         wimlib_assert(inode->resolved);
323         wimlib_assert(stream_idx <= inode->num_ads);
324         if (stream_idx == 0)
325                 return inode->lte;
326         else
327                 return inode->ads_entries[stream_idx - 1].lte;
328 }
329
330 static inline struct lookup_table_entry *
331 inode_stream_lte_unresolved(const struct inode *inode, unsigned stream_idx,
332                             const struct lookup_table *table)
333 {
334         wimlib_assert(!inode->resolved);
335         wimlib_assert(stream_idx <= inode->num_ads);
336         if (!table)
337                 return NULL;
338         if (stream_idx == 0)
339                 return __lookup_resource(table, inode->hash);
340         else
341                 return __lookup_resource(table,
342                                          inode->ads_entries[
343                                                 stream_idx - 1].hash);
344 }
345
346 extern struct lookup_table_entry *
347 inode_stream_lte(const struct inode *inode, unsigned stream_idx,
348                  const struct lookup_table *table);
349
350 static inline const u8 *
351 inode_stream_hash_unresolved(const struct inode *inode, unsigned stream_idx)
352 {
353         wimlib_assert(!inode->resolved);
354         wimlib_assert(stream_idx <= inode->num_ads);
355         if (stream_idx == 0)
356                 return inode->hash;
357         else
358                 return inode->ads_entries[stream_idx - 1].hash;
359 }
360
361
362 static inline const u8 *
363 inode_stream_hash_resolved(const struct inode *inode, unsigned stream_idx)
364 {
365         struct lookup_table_entry *lte;
366         lte = inode_stream_lte_resolved(inode, stream_idx);
367         if (lte)
368                 return lte->hash;
369         else
370                 return zero_hash;
371 }
372
373 /*
374  * Returns the hash for stream @stream_idx of the inode, where stream_idx = 0
375  * means the default un-named file stream, and stream_idx >= 1 corresponds to an
376  * alternate data stream.
377  *
378  * This works for both resolved and un-resolved dentries.
379  */
380 static inline const u8 *
381 inode_stream_hash(const struct inode *inode, unsigned stream_idx)
382 {
383         if (inode->resolved)
384                 return inode_stream_hash_resolved(inode, stream_idx);
385         else
386                 return inode_stream_hash_unresolved(inode, stream_idx);
387 }
388
389 static inline u16
390 inode_stream_name_len(const struct inode *inode, unsigned stream_idx)
391 {
392         wimlib_assert(stream_idx <= inode->num_ads);
393         if (stream_idx == 0)
394                 return 0;
395         else
396                 return inode->ads_entries[stream_idx - 1].stream_name_len;
397 }
398
399 static inline struct lookup_table_entry *
400 inode_unnamed_lte_resolved(const struct inode *inode)
401 {
402         wimlib_assert(inode->resolved);
403         for (unsigned i = 0; i <= inode->num_ads; i++)
404                 if (inode_stream_name_len(inode, i) == 0 &&
405                      !is_zero_hash(inode_stream_hash_resolved(inode, i)))
406                         return inode_stream_lte_resolved(inode, i);
407         return NULL;
408 }
409
410 static inline struct lookup_table_entry *
411 inode_unnamed_lte_unresolved(const struct inode *inode,
412                              const struct lookup_table *table)
413 {
414         wimlib_assert(!inode->resolved);
415         for (unsigned i = 0; i <= inode->num_ads; i++)
416                 if (inode_stream_name_len(inode, i) == 0 &&
417                      !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
418                         return inode_stream_lte_unresolved(inode, i, table);
419         return NULL;
420 }
421
422 extern struct lookup_table_entry *
423 inode_unnamed_lte(const struct inode *inode, const struct lookup_table *table);
424
425 extern u64
426 lookup_table_total_stream_size(struct lookup_table *table);
427
428 #endif