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