2177cb1bca25075945257373dcdcf3c516f2940a
[wimlib] / src / lookup_table.c
1 /*
2  * lookup_table.c
3  *
4  * Lookup table, implemented as a hash table, that maps dentries to file
5  * resources.
6  */
7
8 /*
9  * Copyright (C) 2012 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
13  * wimlib is free software; you can redistribute it and/or modify it under the
14  * terms of the GNU Lesser General Public License as published by the Free
15  * Software Foundation; either version 2.1 of the License, or (at your option)
16  * any later version.
17  *
18  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with wimlib; if not, see http://www.gnu.org/licenses/.
25  */
26
27 #include "wimlib_internal.h"
28 #include "lookup_table.h"
29 #include "io.h"
30
31 struct lookup_table *new_lookup_table(size_t capacity)
32 {
33         struct lookup_table *table;
34         struct lookup_table_entry **array;
35
36         table = MALLOC(sizeof(struct lookup_table));
37         if (!table)
38                 goto err;
39         array = CALLOC(capacity, sizeof(array[0]));
40         if (!array) {
41                 FREE(table);
42                 goto err;
43         }
44         table->num_entries = 0;
45         table->capacity = capacity;
46         table->array = array;
47         return table;
48 err:
49         ERROR("Failed to allocate memory for lookup table with capacity %zu",
50               capacity);
51         return NULL;
52 }
53
54 struct lookup_table_entry *new_lookup_table_entry()
55 {
56         struct lookup_table_entry *lte;
57         
58         lte = CALLOC(1, sizeof(struct lookup_table_entry));
59         if (!lte) {
60                 ERROR("Out of memory (tried to allocate %zu bytes for "
61                       "lookup table entry)",
62                       sizeof(struct lookup_table_entry));
63                 return NULL;
64         }
65
66         lte->part_number  = 1;
67         lte->refcnt       = 1;
68         return lte;
69 }
70
71
72
73 /*
74  * Inserts an entry into the lookup table.
75  *
76  * @lookup_table:       A pointer to the lookup table.
77  * @entry:              A pointer to the entry to insert.
78  */
79 void lookup_table_insert(struct lookup_table *table, 
80                          struct lookup_table_entry *lte)
81 {
82         size_t pos;
83         pos = lte->hash_short % table->capacity;
84         lte->next = table->array[pos];
85         table->array[pos] = lte;
86         /* XXX Make the table grow when too many entries have been inserted. */
87         table->num_entries++;
88 }
89
90
91 /* Unlinks a lookup table entry from the table; does not free it. */
92 void lookup_table_unlink(struct lookup_table *table, 
93                          struct lookup_table_entry *lte)
94 {
95         size_t pos;
96         struct lookup_table_entry *prev, *cur_entry, *next;
97
98         pos = lte->hash_short % table->capacity;
99         prev = NULL;
100         cur_entry = table->array[pos];
101
102         while (cur_entry) {
103                 next = cur_entry->next;
104                 if (cur_entry == lte) {
105                         if (prev)
106                                 prev->next = next;
107                         else
108                                 table->array[pos] = next;
109                         table->num_entries--;
110                         return;
111                 }
112                 prev = cur_entry;
113                 cur_entry = next;
114         }
115 }
116
117
118 /* Decrement the reference count for the dentry having hash value @hash in the
119  * lookup table.  The lookup table entry is unlinked and freed if there are no
120  * references to in remaining.  */
121 bool lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[])
122 {
123         size_t pos = *(size_t*)hash % table->capacity;
124         struct lookup_table_entry *prev = NULL;
125         struct lookup_table_entry *entry = table->array[pos];
126         struct lookup_table_entry *next;
127         while (entry) {
128                 next = entry->next;
129                 if (memcmp(hash, entry->hash, WIM_HASH_SIZE) == 0) {
130                         wimlib_assert(entry->refcnt != 0);
131                         if (--entry->refcnt == 0) {
132                                 if (entry->num_opened_fds == 0)
133                                         free_lookup_table_entry(entry);
134                                 if (prev)
135                                         prev->next = next;
136                                 else
137                                         table->array[pos] = next;
138                                 return true;
139                         }
140                 }
141                 prev = entry;
142                 entry = next;
143         }
144         return false;
145 }
146
147
148 /* 
149  * Calls a function on all the entries in the lookup table.  Stop early and
150  * return nonzero if any call to the function returns nonzero.
151  */
152 int for_lookup_table_entry(struct lookup_table *table, 
153                            int (*visitor)(struct lookup_table_entry *, void *),
154                            void *arg)
155 {
156         struct lookup_table_entry *entry, *next;
157         size_t i;
158         int ret;
159
160         for (i = 0; i < table->capacity; i++) {
161                 entry = table->array[i];
162                 while (entry) {
163                         next = entry->next;
164                         ret = visitor(entry, arg);
165                         if (ret != 0)
166                                 return ret;
167                         entry = next;
168                 }
169         }
170         return 0;
171 }
172
173
174 /*
175  * Reads the lookup table from a WIM file.
176  *
177  * @fp:                 The FILE* for the WIM file.
178  * @offset:             The offset of the lookup table resource.
179  * @size:               The size of the lookup table resource.
180  * @lookup_table_ret:   A pointer to a struct lookup_table structure into which the
181  *                              lookup table will be returned.
182  * @return:             True on success, false on failure.
183  */
184 int read_lookup_table(FILE *fp, u64 offset, u64 size, 
185                       struct lookup_table **table_ret)
186 {
187         size_t num_entries;
188         u8     buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
189         int    ret;
190         struct lookup_table *table;
191         const u8 *p;
192         struct lookup_table_entry *cur_entry;
193
194         DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
195               offset, size);
196
197         if (fseeko(fp, offset, SEEK_SET) != 0) {
198                 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
199                                  "lookup table", offset);
200                 return WIMLIB_ERR_READ;
201         }
202
203         num_entries = size / WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
204         table = new_lookup_table(num_entries * 2 + 1);
205         if (!table)
206                 return WIMLIB_ERR_NOMEM;
207
208         while (num_entries--) {
209                 if (fread(buf, 1, sizeof(buf), fp) != sizeof(buf)) {
210                         if (feof(fp)) {
211                                 ERROR("Unexpected EOF in WIM lookup table!");
212                         } else {
213                                 ERROR_WITH_ERRNO("Error reading WIM lookup "
214                                                  "table");
215                         }
216                         ret = WIMLIB_ERR_READ;
217                         goto out;
218                 }
219                 cur_entry = new_lookup_table_entry();
220                 if (!cur_entry) {
221                         ret = WIMLIB_ERR_NOMEM;
222                         goto out;
223                 }
224                          
225                 p = get_resource_entry(buf, &cur_entry->resource_entry);
226                 p = get_u16(p, &cur_entry->part_number);
227                 p = get_u32(p, &cur_entry->refcnt);
228                 p = get_bytes(p, WIM_HASH_SIZE, cur_entry->hash);
229                 lookup_table_insert(table, cur_entry);
230         }
231         DEBUG("Done reading lookup table.");
232         *table_ret = table;
233         return 0;
234 out:
235         free_lookup_table(table);
236         return ret;
237 }
238
239
240 /* 
241  * Writes a lookup table entry to the output file.
242  */
243 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
244 {
245         FILE *out;
246         u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
247         u8 *p;
248
249         out = __out;
250
251         /* do not write lookup table entries for empty files */
252         if (lte->output_resource_entry.original_size == 0)
253                 return 0;
254
255         /* Don't write entries that have not had file resources or metadata
256          * resources written for them. */
257         if (lte->out_refcnt == 0)
258                 return 0;
259
260         if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
261                 DEBUG("Writing metadata entry at %lu", ftello(out));
262
263         p = put_resource_entry(buf, &lte->output_resource_entry);
264         p = put_u16(p, lte->part_number);
265         p = put_u32(p, lte->out_refcnt);
266         p = put_bytes(p, WIM_HASH_SIZE, lte->hash);
267         if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
268                 ERROR_WITH_ERRNO("Failed to write lookup table entry");
269                 return WIMLIB_ERR_WRITE;
270         }
271         return 0;
272 }
273
274 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
275                                       void *ignore)
276 {
277         free_lookup_table_entry(entry);
278         return 0;
279 }
280
281 void free_lookup_table(struct lookup_table *table)
282 {
283         if (!table)
284                 return;
285         if (table->array) {
286                 for_lookup_table_entry(table, do_free_lookup_table_entry, NULL);
287                 FREE(table->array);
288         }
289         FREE(table);
290 }
291
292 int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore)
293 {
294         entry->out_refcnt = 0;
295         return 0;
296 }
297
298 int print_lookup_table_entry(struct lookup_table_entry *entry, void *ignore)
299 {
300         printf("Offset            = %"PRIu64" bytes\n", 
301                entry->resource_entry.offset);
302         printf("Size              = %"PRIu64" bytes\n", 
303                (u64)entry->resource_entry.size);
304         printf("Original size     = %"PRIu64" bytes\n", 
305                entry->resource_entry.original_size);
306         printf("Part Number       = %hu\n", entry->part_number);
307         printf("Reference Count   = %u\n", entry->refcnt);
308         printf("Hash              = ");
309         print_hash(entry->hash);
310         putchar('\n');
311         printf("Flags             = ");
312         u8 flags = entry->resource_entry.flags;
313         if (flags & WIM_RESHDR_FLAG_COMPRESSED)
314                 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
315         if (flags & WIM_RESHDR_FLAG_FREE)
316                 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
317         if (flags & WIM_RESHDR_FLAG_METADATA)
318                 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
319         if (flags & WIM_RESHDR_FLAG_SPANNED)
320                 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
321         putchar('\n');
322         if (entry->file_on_disk)
323                 printf("File on Disk      = `%s'\n", entry->file_on_disk);
324         putchar('\n');
325         return 0;
326 }
327
328 /*
329  * Prints the lookup table of a WIM file. 
330  */
331 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
332 {
333         for_lookup_table_entry(w->lookup_table, 
334                                print_lookup_table_entry, NULL);
335 }
336
337 /* 
338  * Looks up an entry in the lookup table.
339  */
340 struct lookup_table_entry *
341 __lookup_resource(const struct lookup_table *lookup_table, const u8 hash[])
342 {
343         size_t pos;
344         struct lookup_table_entry *lte;
345
346         pos = *(size_t*)hash % lookup_table->capacity;
347         lte = lookup_table->array[pos];
348         while (lte) {
349                 if (memcmp(hash, lte->hash, WIM_HASH_SIZE) == 0)
350                         return lte;
351                 lte = lte->next;
352         }
353         return NULL;
354 }
355
356 int lookup_resource(WIMStruct *w, const char *path,
357                     int lookup_flags,
358                     struct dentry **dentry_ret,
359                     struct lookup_table_entry **lte_ret,
360                     u8 **hash_ret)
361 {
362         struct dentry *dentry = get_dentry(w, path);
363         struct lookup_table_entry *lte;
364         u8 *hash;
365         if (!dentry)
366                 return -ENOENT;
367         if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
368               && dentry_is_directory(dentry))
369                 return -EISDIR;
370         if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
371                 const char *stream_name = path_stream_name(path);
372                 if (stream_name) {
373                         for (u16 i = 0; i < dentry->num_ads; i++) {
374                                 if (strcmp(stream_name, dentry->ads_entries[i].stream_name) == 0) {
375                                         hash = dentry->ads_entries[i].hash;
376                                         goto do_lookup;
377                                 }
378                         }
379                         return -ENOENT;
380                 }
381         }
382         hash = dentry->hash;
383 do_lookup:
384         lte = __lookup_resource(w->lookup_table, hash);
385         if (dentry_ret)
386                 *dentry_ret = dentry;
387         if (lte_ret)
388                 *lte_ret = lte;
389         if (hash_ret)
390                 *hash_ret = hash;
391         return 0;
392 }