]> wimlib.net Git - wimlib/blob - src/lookup_table.h
Various cleanups
[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 <sys/types.h>
6
7 /* Size of each lookup table entry in the WIM file. */
8 #define WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE 50
9
10 #define LOOKUP_FLAG_ADS_OK              0x00000001
11 #define LOOKUP_FLAG_DIRECTORY_OK        0x00000002
12 #define LOOKUP_FLAG_FOLLOW_SYMLINKS     0x00000004
13
14
15 /* A lookup table that is used to translate the hash codes of dentries into the
16  * offsets and sizes of uncompressed or compressed file resources.  It is
17  * implemented as a hash table. */
18 struct lookup_table {
19         struct lookup_table_entry **array;
20         u64 num_entries;
21         u64 capacity;
22 };
23
24 struct wimlib_fd;
25
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         /* The next struct lookup_table_entry in the hash bucket.  NULL if this is the
38          * last one. */
39         struct lookup_table_entry *next;
40
41         /* @resource_entry is read from the lookup table in the WIM
42          * file; it says where to find the file resource in the WIM
43          * file, and whether it is compressed or not. */
44         struct resource_entry resource_entry;
45
46         /* Currently ignored; set to 1 in new lookup table entries. */
47         u16 part_number;
48
49         /* If %true, this lookup table entry corresponds to a symbolic link
50          * reparse buffer.  @symlink_reparse_data_buf will give the target of
51          * the symbolic link. */
52         bool is_symlink;
53
54         /* Number of times this lookup table entry is referenced by dentries. */
55         u32 refcnt;
56
57         union {
58                 /* SHA1 hash of the file resource pointed to by this lookup
59                  * table entry */
60                 u8  hash[WIM_HASH_SIZE];
61
62                 /* First 4 or 8 bytes of the SHA1 hash, used for inserting the
63                  * entry into the hash table.  Since the SHA1 hashes can be
64                  * considered random, we don't really need the full 20 byte hash
65                  * just to insert the entry in a hash table. */
66                 size_t hash_short;
67         };
68
69         /* If @file_on_disk != NULL, the file resource indicated by this lookup
70          * table entry is not in the WIM file, but rather a file on disk; this
71          * occurs for files that are added to the WIM.  In that case,
72          * file_on_disk is the name of the file in the outside filesystem.  
73          * It will not be compressed, and its size will be given by
74          * resource_entry.size and resource_entry.original_size. */
75         union {
76                 char *file_on_disk;
77                 char *staging_file_name;
78                 void *symlink_buf;
79                 struct lookup_table_entry *next_lte_in_swm;
80         };
81
82         union {
83                 struct { /* Used for wimlib_export. */
84
85                         /* If (other_wim_fp != NULL), the file resource indicated
86                          * by this lookup table entry is in a different WIM
87                          * file, and other_wim_fp is the FILE* for it. */
88                         FILE *other_wim_fp;
89
90                         /* Compression type used in other WIM. */
91                         int   other_wim_ctype;
92                 };
93
94                 struct { /* Used for wimlib_mount */
95
96                         /* File descriptors table for this data stream */
97                         struct wimlib_fd **fds;
98                         u16 num_allocated_fds;
99                         u16 num_opened_fds;
100                 };
101         };
102
103         /* When a WIM file is written, out_refcnt starts at 0 and is incremented
104          * whenever the file resource pointed to by this lookup table entry
105          * needs to be written.  Naturally, the file resource only need to be
106          * written when out_refcnt is 0.  Incrementing it further is needed to
107          * find the correct reference count to write to the lookup table in the
108          * output file, which may be less than the regular refcnt if not all
109          * images in the WIM file are written. 
110          *
111          * output_resource_entry is the struct resource_entry for the position of the
112          * file resource when written to the output file. */
113         u32 out_refcnt;
114         struct resource_entry output_resource_entry;
115
116         /* Circular linked list of streams that share the same lookup table
117          * entry
118          * 
119          * This list of streams may include streams from different hard link
120          * sets that happen to be the same.  */
121         struct list_head lte_group_list;
122
123         /* List of lookup table entries that correspond to streams that have
124          * been extracted to the staging directory when modifying a read-write
125          * mounted WIM. */
126         struct list_head staging_list;
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 extern void lookup_table_unlink(struct lookup_table *table, 
135                                 struct lookup_table_entry *lte);
136
137 extern struct lookup_table_entry *
138 lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[]);
139
140 extern struct lookup_table_entry *
141 lte_decrement_refcnt(struct lookup_table_entry *lte,
142                      struct lookup_table *table);
143
144
145 extern struct lookup_table_entry *new_lookup_table_entry();
146
147 extern int for_lookup_table_entry(struct lookup_table *table, 
148                                   int (*visitor)(struct lookup_table_entry *, void *), 
149                                   void *arg);
150
151 extern struct lookup_table_entry *
152 __lookup_resource(const struct lookup_table *lookup_table, const u8 hash[]);
153
154 extern int lookup_resource(WIMStruct *w, const char *path,
155                            int lookup_flags, struct dentry **dentry_ret,
156                            struct lookup_table_entry **lte_ret,
157                            unsigned *stream_idx_ret);
158
159 extern int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore);
160
161 extern int print_lookup_table_entry(struct lookup_table_entry *entry, void *ignore);
162
163 extern int read_lookup_table(FILE *fp, u64 offset, u64 size, 
164                              struct lookup_table **table_ret);
165
166 extern void free_lookup_table(struct lookup_table *table);
167
168 extern int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out);
169
170 extern void free_lookup_table_entry(struct lookup_table_entry *lte);
171
172 extern int dentry_resolve_ltes(struct dentry *dentry, void *__table);
173
174 /* Writes the lookup table to the output file. */
175 static inline int write_lookup_table(struct lookup_table *table, FILE *out)
176 {
177         return for_lookup_table_entry(table, write_lookup_table_entry, out);
178 }
179
180 /* Unlinks and frees an entry from a lookup table. */
181 static inline void lookup_table_remove(struct lookup_table *table, 
182                                        struct lookup_table_entry *lte)
183 {
184         lookup_table_unlink(table, lte);
185         free_lookup_table_entry(lte);
186 }
187
188 static inline struct resource_entry* wim_metadata_resource_entry(WIMStruct *w)
189 {
190         return &w->image_metadata[
191                         w->current_image - 1].metadata_lte->resource_entry;
192 }
193
194 #endif