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
15 //#define LOOKUP_FLAG_FOLLOW_SYMLINKS 0x00000004
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. */
22 struct hlist_head *array;
29 typedef struct _ntfs_attr ntfs_attr;
30 typedef struct _ntfs_volume ntfs_volume;
31 struct ntfs_location {
38 * An entry in the lookup table in the WIM file.
40 * It is used to find data streams for files in the WIM.
42 * The lookup_table_entry for a given dentry in the WIM is found using the SHA1
43 * message digest field.
45 struct lookup_table_entry {
47 /* List of lookup table entries in this hash bucket */
48 struct hlist_node hash_list;
50 /* @resource_entry is read from the lookup table in the WIM
51 * file; it says where to find the file resource in the WIM
52 * file, and whether it is compressed or not. */
53 struct resource_entry resource_entry;
55 /* Currently ignored; set to 1 in new lookup table entries. */
58 /* If %true, this lookup table entry corresponds to a symbolic link
59 * reparse buffer. @symlink_reparse_data_buf will give the target of
60 * the symbolic link. */
62 RESOURCE_NONEXISTENT = 0,
64 RESOURCE_IN_FILE_ON_DISK,
65 RESOURCE_IN_STAGING_FILE,
66 RESOURCE_IN_ATTACHED_BUFFER,
67 RESOURCE_IN_NTFS_VOLUME,
70 /* Number of times this lookup table entry is referenced by dentries. */
74 /* SHA1 hash of the file resource pointed to by this lookup
76 u8 hash[SHA1_HASH_SIZE];
78 /* First 4 or 8 bytes of the SHA1 hash, used for inserting the
79 * entry into the hash table. Since the SHA1 hashes can be
80 * considered random, we don't really need the full 20 byte hash
81 * just to insert the entry in a hash table. */
85 /* If @file_on_disk != NULL, the file resource indicated by this lookup
86 * table entry is not in the WIM file, but rather a file on disk; this
87 * occurs for files that are added to the WIM. In that case,
88 * file_on_disk is the name of the file in the outside filesystem.
89 * It will not be compressed, and its size will be given by
90 * resource_entry.size and resource_entry.original_size. */
94 char *staging_file_name;
96 struct ntfs_location *ntfs_location;
99 struct lookup_table_entry *next_lte_in_swm;
100 FILE *file_on_disk_fp;
104 /* File descriptors table for this data stream */
106 u16 num_allocated_fds;
107 struct wimlib_fd **fds;
110 /* When a WIM file is written, out_refcnt starts at 0 and is incremented
111 * whenever the file resource pointed to by this lookup table entry
112 * needs to be written. Naturally, the file resource only need to be
113 * written when out_refcnt is 0. Incrementing it further is needed to
114 * find the correct reference count to write to the lookup table in the
115 * output file, which may be less than the regular refcnt if not all
116 * images in the WIM file are written.
118 * output_resource_entry is the struct resource_entry for the position of the
119 * file resource when written to the output file. */
122 struct resource_entry output_resource_entry;
123 char *extracted_file;
126 /* Circular linked list of streams that share the same lookup table
129 * This list of streams may include streams from different hard link
130 * sets that happen to be the same. */
131 struct list_head lte_group_list;
133 /* List of lookup table entries that correspond to streams that have
134 * been extracted to the staging directory when modifying a read-write
136 struct list_head staging_list;
139 static inline u64 wim_resource_size(const struct lookup_table_entry *lte)
141 return lte->resource_entry.original_size;
145 wim_resource_compressed_size(const struct lookup_table_entry *lte)
147 return lte->resource_entry.size;
151 * XXX Probably should store the compression type directly in the lookup table
155 wim_resource_compression_type(const struct lookup_table_entry *lte)
157 if (!(lte->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
158 || lte->resource_location != RESOURCE_IN_WIM)
159 return WIM_COMPRESSION_TYPE_NONE;
160 return wimlib_get_compression_type(lte->wim);
164 extern struct lookup_table *new_lookup_table(size_t capacity);
166 extern void lookup_table_insert(struct lookup_table *table,
167 struct lookup_table_entry *lte);
169 /* Unlinks a lookup table entry from the table; does not free it. */
170 static inline void lookup_table_unlink(struct lookup_table *table,
171 struct lookup_table_entry *lte)
173 hlist_del(<e->hash_list);
174 table->num_entries--;
178 extern struct lookup_table_entry *
179 lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[]);
181 extern struct lookup_table_entry *
182 lte_decrement_refcnt(struct lookup_table_entry *lte,
183 struct lookup_table *table);
186 extern struct lookup_table_entry *new_lookup_table_entry();
188 extern int for_lookup_table_entry(struct lookup_table *table,
189 int (*visitor)(struct lookup_table_entry *, void *),
192 extern struct lookup_table_entry *
193 __lookup_resource(const struct lookup_table *table, const u8 hash[]);
195 extern int lookup_resource(WIMStruct *w, const char *path,
196 int lookup_flags, struct dentry **dentry_ret,
197 struct lookup_table_entry **lte_ret,
198 unsigned *stream_idx_ret);
200 extern int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore);
202 extern void print_lookup_table_entry(const struct lookup_table_entry *entry);
204 extern int read_lookup_table(WIMStruct *w);
206 extern void free_lookup_table(struct lookup_table *table);
208 extern int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out);
210 extern void free_lookup_table_entry(struct lookup_table_entry *lte);
212 extern int dentry_resolve_ltes(struct dentry *dentry, void *__table);
214 /* Writes the lookup table to the output file. */
215 static inline int write_lookup_table(struct lookup_table *table, FILE *out)
217 return for_lookup_table_entry(table, write_lookup_table_entry, out);
220 /* Unlinks and frees an entry from a lookup table. */
221 static inline void lookup_table_remove(struct lookup_table *table,
222 struct lookup_table_entry *lte)
224 lookup_table_unlink(table, lte);
225 free_lookup_table_entry(lte);
228 static inline struct resource_entry* wim_metadata_resource_entry(WIMStruct *w)
230 return &w->image_metadata[
231 w->current_image - 1].metadata_lte->resource_entry;
234 static inline struct lookup_table_entry *
235 dentry_stream_lte_resolved(const struct dentry *dentry, unsigned stream_idx)
237 wimlib_assert(dentry->resolved);
238 wimlib_assert(stream_idx <= dentry->num_ads);
242 return dentry->ads_entries[stream_idx - 1].lte;
245 static inline struct lookup_table_entry *
246 dentry_stream_lte_unresolved(const struct dentry *dentry, unsigned stream_idx,
247 const struct lookup_table *table)
249 wimlib_assert(!dentry->resolved);
250 wimlib_assert(stream_idx <= dentry->num_ads);
254 return __lookup_resource(table, dentry->hash);
256 return __lookup_resource(table,
258 stream_idx - 1].hash);
261 * Returns the lookup table entry for stream @stream_idx of the dentry, where
262 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
263 * corresponds to an alternate data stream.
265 * This works for both resolved and un-resolved dentries.
267 static inline struct lookup_table_entry *
268 dentry_stream_lte(const struct dentry *dentry, unsigned stream_idx,
269 const struct lookup_table *table)
271 if (dentry->resolved)
272 return dentry_stream_lte_resolved(dentry, stream_idx);
274 return dentry_stream_lte_unresolved(dentry, stream_idx, table);
278 static inline const u8 *dentry_stream_hash_unresolved(const struct dentry *dentry,
281 wimlib_assert(!dentry->resolved);
282 wimlib_assert(stream_idx <= dentry->num_ads);
286 return dentry->ads_entries[stream_idx - 1].hash;
289 static inline const u8 *dentry_stream_hash_resolved(const struct dentry *dentry,
292 struct lookup_table_entry *lte;
293 lte = dentry_stream_lte_resolved(dentry, stream_idx);
301 * Returns the hash for stream @stream_idx of the dentry, where stream_idx = 0
302 * means the default un-named file stream, and stream_idx >= 1 corresponds to an
303 * alternate data stream.
305 * This works for both resolved and un-resolved dentries.
307 static inline const u8 *dentry_stream_hash(const struct dentry *dentry,
310 if (dentry->resolved)
311 return dentry_stream_hash_resolved(dentry, stream_idx);
313 return dentry_stream_hash_unresolved(dentry, stream_idx);
316 static inline struct lookup_table_entry *
317 dentry_first_lte_resolved(const struct dentry *dentry)
319 struct lookup_table_entry *lte;
320 wimlib_assert(dentry->resolved);
322 for (unsigned i = 0; i <= dentry->num_ads; i++) {
323 lte = dentry_stream_lte_resolved(dentry, i);
330 static inline struct lookup_table_entry *
331 dentry_first_lte_unresolved(const struct dentry *dentry,
332 const struct lookup_table *table)
334 struct lookup_table_entry *lte;
335 wimlib_assert(!dentry->resolved);
337 for (unsigned i = 0; i <= dentry->num_ads; i++) {
338 lte = dentry_stream_lte_unresolved(dentry, i, table);
345 extern struct lookup_table_entry *
346 dentry_first_lte(const struct dentry *dentry, const struct lookup_table *table);