1 #ifndef _WIMLIB_LOOKUP_TABLE_H
2 #define _WIMLIB_LOOKUP_TABLE_H
3 #include "wimlib_internal.h"
8 /* Size of each lookup table entry in the WIM file. */
9 #define WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE 50
11 #define LOOKUP_FLAG_ADS_OK 0x00000001
12 #define LOOKUP_FLAG_DIRECTORY_OK 0x00000002
13 #define LOOKUP_FLAG_FOLLOW_SYMLINKS 0x00000004
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. */
20 struct hlist_head *array;
28 * An entry in the lookup table in the WIM file.
30 * It is used to find data streams for files in the WIM.
32 * The lookup_table_entry for a given dentry in the WIM is found using the SHA1
33 * message digest field.
35 struct lookup_table_entry {
37 /* List of lookup table entries in this hash bucket */
38 struct hlist_node hash_list;
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;
45 /* Currently ignored; set to 1 in new lookup table entries. */
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. */
52 RESOURCE_NONEXISTENT = 0,
54 RESOURCE_IN_FILE_ON_DISK,
55 RESOURCE_IN_STAGING_FILE,
56 RESOURCE_IN_ATTACHED_BUFFER,
59 /* Number of times this lookup table entry is referenced by dentries. */
63 /* SHA1 hash of the file resource pointed to by this lookup
65 u8 hash[SHA1_HASH_SIZE];
67 /* First 4 or 8 bytes of the SHA1 hash, used for inserting the
68 * entry into the hash table. Since the SHA1 hashes can be
69 * considered random, we don't really need the full 20 byte hash
70 * just to insert the entry in a hash table. */
74 /* If @file_on_disk != NULL, the file resource indicated by this lookup
75 * table entry is not in the WIM file, but rather a file on disk; this
76 * occurs for files that are added to the WIM. In that case,
77 * file_on_disk is the name of the file in the outside filesystem.
78 * It will not be compressed, and its size will be given by
79 * resource_entry.size and resource_entry.original_size. */
83 char *staging_file_name;
85 struct lookup_table_entry *next_lte_in_swm;
87 FILE *file_on_disk_fp;
89 /* File descriptors table for this data stream */
91 u16 num_allocated_fds;
92 struct wimlib_fd **fds;
95 /* When a WIM file is written, out_refcnt starts at 0 and is incremented
96 * whenever the file resource pointed to by this lookup table entry
97 * needs to be written. Naturally, the file resource only need to be
98 * written when out_refcnt is 0. Incrementing it further is needed to
99 * find the correct reference count to write to the lookup table in the
100 * output file, which may be less than the regular refcnt if not all
101 * images in the WIM file are written.
103 * output_resource_entry is the struct resource_entry for the position of the
104 * file resource when written to the output file. */
106 struct resource_entry output_resource_entry;
108 /* Circular linked list of streams that share the same lookup table
111 * This list of streams may include streams from different hard link
112 * sets that happen to be the same. */
113 struct list_head lte_group_list;
115 /* List of lookup table entries that correspond to streams that have
116 * been extracted to the staging directory when modifying a read-write
118 struct list_head staging_list;
121 static inline u64 wim_resource_size(const struct lookup_table_entry *lte)
123 return lte->resource_entry.original_size;
127 wim_resource_compression_type(const struct lookup_table_entry *lte)
129 if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
131 return WIM_COMPRESSION_TYPE_NONE;
132 return wimlib_get_compression_type(lte->wim);
136 extern struct lookup_table *new_lookup_table(size_t capacity);
138 extern void lookup_table_insert(struct lookup_table *table,
139 struct lookup_table_entry *lte);
141 /* Unlinks a lookup table entry from the table; does not free it. */
142 static inline void lookup_table_unlink(struct lookup_table *table,
143 struct lookup_table_entry *lte)
145 hlist_del(<e->hash_list);
146 table->num_entries--;
150 extern struct lookup_table_entry *
151 lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[]);
153 extern struct lookup_table_entry *
154 lte_decrement_refcnt(struct lookup_table_entry *lte,
155 struct lookup_table *table);
158 extern struct lookup_table_entry *new_lookup_table_entry();
160 extern int for_lookup_table_entry(struct lookup_table *table,
161 int (*visitor)(struct lookup_table_entry *, void *),
164 extern struct lookup_table_entry *
165 __lookup_resource(const struct lookup_table *table, const u8 hash[]);
167 extern int lookup_resource(WIMStruct *w, const char *path,
168 int lookup_flags, struct dentry **dentry_ret,
169 struct lookup_table_entry **lte_ret,
170 unsigned *stream_idx_ret);
172 extern int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore);
174 extern void print_lookup_table_entry(struct lookup_table_entry *entry);
176 extern int read_lookup_table(WIMStruct *w);
178 extern void free_lookup_table(struct lookup_table *table);
180 extern int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out);
182 extern void free_lookup_table_entry(struct lookup_table_entry *lte);
184 extern int dentry_resolve_ltes(struct dentry *dentry, void *__table);
186 /* Writes the lookup table to the output file. */
187 static inline int write_lookup_table(struct lookup_table *table, FILE *out)
189 return for_lookup_table_entry(table, write_lookup_table_entry, out);
192 /* Unlinks and frees an entry from a lookup table. */
193 static inline void lookup_table_remove(struct lookup_table *table,
194 struct lookup_table_entry *lte)
196 lookup_table_unlink(table, lte);
197 free_lookup_table_entry(lte);
200 static inline struct resource_entry* wim_metadata_resource_entry(WIMStruct *w)
202 return &w->image_metadata[
203 w->current_image - 1].metadata_lte->resource_entry;
206 static inline struct lookup_table_entry *
207 dentry_stream_lte_resolved(const struct dentry *dentry, unsigned stream_idx)
209 wimlib_assert(dentry->resolved);
210 wimlib_assert(stream_idx <= dentry->num_ads);
214 return dentry->ads_entries[stream_idx - 1].lte;
217 static inline struct lookup_table_entry *
218 dentry_stream_lte_unresolved(const struct dentry *dentry, unsigned stream_idx,
219 const struct lookup_table *table)
221 wimlib_assert(!dentry->resolved);
222 wimlib_assert(stream_idx <= dentry->num_ads);
226 return __lookup_resource(table, dentry->hash);
228 return __lookup_resource(table,
230 stream_idx - 1].hash);
233 * Returns the lookup table entry for stream @stream_idx of the dentry, where
234 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
235 * corresponds to an alternate data stream.
237 * This works for both resolved and un-resolved dentries.
239 static inline struct lookup_table_entry *
240 dentry_stream_lte(const struct dentry *dentry, unsigned stream_idx,
241 const struct lookup_table *table)
243 if (dentry->resolved)
244 return dentry_stream_lte_resolved(dentry, stream_idx);
246 return dentry_stream_lte_unresolved(dentry, stream_idx, table);
250 static inline const u8 *dentry_stream_hash_unresolved(const struct dentry *dentry,
253 wimlib_assert(!dentry->resolved);
254 wimlib_assert(stream_idx <= dentry->num_ads);
258 return dentry->ads_entries[stream_idx - 1].hash;
261 static inline const u8 *dentry_stream_hash_resolved(const struct dentry *dentry,
264 struct lookup_table_entry *lte;
265 lte = dentry_stream_lte_resolved(dentry, stream_idx);
273 * Returns the hash for stream @stream_idx of the dentry, where stream_idx = 0
274 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
275 * alternate data stream.
277 * This works for both resolved and un-resolved dentries.
279 static inline const u8 *dentry_stream_hash(const struct dentry *dentry,
282 if (dentry->resolved)
283 return dentry_stream_hash_resolved(dentry, stream_idx);
285 return dentry_stream_hash_unresolved(dentry, stream_idx);
288 static inline struct lookup_table_entry *
289 dentry_first_lte_resolved(const struct dentry *dentry)
291 struct lookup_table_entry *lte;
292 wimlib_assert(dentry->resolved);
294 for (unsigned i = 0; i <= dentry->num_ads; i++) {
295 lte = dentry_stream_lte_resolved(dentry, i);
302 static inline struct lookup_table_entry *
303 dentry_first_lte_unresolved(const struct dentry *dentry,
304 const struct lookup_table *table)
306 struct lookup_table_entry *lte;
307 wimlib_assert(!dentry->resolved);
309 for (unsigned i = 0; i <= dentry->num_ads; i++) {
310 lte = dentry_stream_lte_unresolved(dentry, i, table);
317 extern struct lookup_table_entry *
318 dentry_first_lte(const struct dentry *dentry, const struct lookup_table *table);