]> wimlib.net Git - wimlib/blob - src/lookup_table.h
91684c4319ab3fcaaacfc945e9cca2916ac44516
[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
11 /* A lookup table that is used to translate the hash codes of dentries into the
12  * offsets and sizes of uncompressed or compressed file resources.  It is
13  * implemented as a hash table. */
14 struct lookup_table {
15         struct lookup_table_entry **array;
16         u64 num_entries;
17         u64 capacity;
18 };
19
20 /* An entry in the lookup table in the WIM file. */
21 struct lookup_table_entry {
22
23         /* The next struct lookup_table_entry in the hash bucket.  NULL if this is the
24          * last one. */
25         struct lookup_table_entry *next;
26
27         /* @resource_entry is read from the lookup table in the WIM
28          * file; it says where to find the file resource in the WIM
29          * file, and whether it is compressed or not. */
30         struct resource_entry resource_entry;
31
32         /* Currently ignored; set to 1 in new lookup table entries. */
33         u16 part_number;
34
35         /* Number of times this lookup table entry is referenced by dentries. */
36         u32 refcnt;
37
38         union {
39                 /* SHA1 hash of the file resource pointed to by this lookup
40                  * table entry */
41                 u8  hash[WIM_HASH_SIZE];
42
43                 /* First 4 or 8 bytes of the SHA1 hash, used for inserting the
44                  * entry into the hash table.  Since the SHA1 hashes can be
45                  * considered random, we don't really need the full 20 byte hash
46                  * just to insert the entry in a hash table. */
47                 size_t hash_short;
48         };
49
50         /* If @file_on_disk != NULL, the file resource indicated by this lookup
51          * table entry is not in the WIM file, but rather a file on disk; this
52          * occurs for files that are added to the WIM.  In that case,
53          * file_on_disk is the name of the file in the outside filesystem.  
54          * It will not be compressed, and its size will be given by
55          * resource_entry.size and resource_entry.original_size. */
56         union {
57                 char *file_on_disk;
58                 char *staging_file_name;
59                 struct lookup_table_entry *next_lte_in_swm;
60         };
61
62         union {
63                 struct { /* Used for wimlib_export. */
64
65                         /* If (other_wim_fp != NULL), the file resource indicated
66                          * by this lookup table entry is in a different WIM
67                          * file, and other_wim_fp is the FILE* for it. */
68                         FILE *other_wim_fp;
69
70                         /* Compression type used in other WIM. */
71                         int   other_wim_ctype;
72                 };
73
74                 struct { /* Used for read-write mounts. */
75
76
77                         /* Offset of the stream file_on_disk_fd. */
78                         off_t staging_offset;
79
80
81                         /* If file_on_disk_fd, if it is not -1, is the file
82                          * descriptor, opened for reading, for file_on_disk. */
83                         int staging_fd;
84
85                         /* Number of times the file has been opened.
86                          * file_on_disk_fd can be closed when num_times_opened
87                          * is decremented to 0.  */
88                         int staging_num_times_opened;
89                 };
90         };
91
92         /* When a WIM file is written, out_refcnt starts at 0 and is incremented
93          * whenever the file resource pointed to by this lookup table entry
94          * needs to be written.  Naturally, the file resource only need to be
95          * written when out_refcnt is 0.  Incrementing it further is needed to
96          * find the correct reference count to write to the lookup table in the
97          * output file, which may be less than the regular refcnt if not all
98          * images in the WIM file are written. 
99          *
100          * output_resource_entry is the struct resource_entry for the position of the
101          * file resource when written to the output file. */
102         u32 out_refcnt;
103         struct resource_entry output_resource_entry;
104 };
105
106 extern struct lookup_table *new_lookup_table(size_t capacity);
107
108 extern void lookup_table_insert(struct lookup_table *table, 
109                                 struct lookup_table_entry *lte);
110
111 extern void lookup_table_unlink(struct lookup_table *table, 
112                                 struct lookup_table_entry *lte);
113
114 extern void lookup_table_decrement_refcnt(struct lookup_table* table, 
115                                           const u8 hash[]);
116
117
118 extern struct lookup_table_entry *new_lookup_table_entry();
119
120 extern int for_lookup_table_entry(struct lookup_table *table, 
121                                   int (*visitor)(struct lookup_table_entry *, void *), 
122                                   void *arg);
123
124 extern struct lookup_table_entry *lookup_resource(const struct lookup_table *table,
125                                             const u8 hash[]);
126
127 static inline struct lookup_table_entry *
128 wim_lookup_resource(const WIMStruct *w, const struct dentry *dentry)
129 {
130         return lookup_resource(w->lookup_table, dentry->hash);
131 }
132
133 extern int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore);
134
135 extern int print_lookup_table_entry(struct lookup_table_entry *entry, void *ignore);
136
137 extern int read_lookup_table(FILE *fp, u64 offset, u64 size, 
138                              struct lookup_table **table_ret);
139
140 extern void free_lookup_table(struct lookup_table *table);
141
142 extern int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out);
143
144 static inline void free_lookup_table_entry(struct lookup_table_entry *lte)
145 {
146         if (lte) {
147                 FREE(lte->file_on_disk);
148                 FREE(lte);
149         }
150 }
151
152 /* Writes the lookup table to the output file. */
153 static inline int write_lookup_table(struct lookup_table *table, FILE *out)
154 {
155         return for_lookup_table_entry(table, write_lookup_table_entry, out);
156 }
157
158 /* Unlinks and frees an entry from a lookup table. */
159 static inline void lookup_table_remove(struct lookup_table *table, 
160                                        struct lookup_table_entry *lte)
161 {
162         lookup_table_unlink(table, lte);
163         free_lookup_table_entry(lte);
164 }
165
166 static inline struct resource_entry* wim_metadata_resource_entry(WIMStruct *w)
167 {
168         return &w->image_metadata[w->current_image - 1].
169                         lookup_table_entry->resource_entry;
170 }
171
172 #endif