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