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