Fixes
[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 #include <errno.h>
31
32 struct lookup_table *new_lookup_table(size_t capacity)
33 {
34         struct lookup_table *table;
35         struct lookup_table_entry **array;
36
37         table = MALLOC(sizeof(struct lookup_table));
38         if (!table)
39                 goto err;
40         array = CALLOC(capacity, sizeof(array[0]));
41         if (!array) {
42                 FREE(table);
43                 goto err;
44         }
45         table->num_entries = 0;
46         table->capacity = capacity;
47         table->array = array;
48         return table;
49 err:
50         ERROR("Failed to allocate memory for lookup table with capacity %zu",
51               capacity);
52         return NULL;
53 }
54
55 struct lookup_table_entry *new_lookup_table_entry()
56 {
57         struct lookup_table_entry *lte;
58         
59         lte = CALLOC(1, sizeof(struct lookup_table_entry));
60         if (!lte) {
61                 ERROR("Out of memory (tried to allocate %zu bytes for "
62                       "lookup table entry)",
63                       sizeof(struct lookup_table_entry));
64                 return NULL;
65         }
66
67         lte->part_number  = 1;
68         lte->refcnt       = 1;
69         INIT_LIST_HEAD(&lte->lte_group_list);
70         return lte;
71 }
72
73
74 void free_lookup_table_entry(struct lookup_table_entry *lte)
75 {
76         if (lte) {
77                 if (lte->staging_list.next)
78                         list_del(&lte->staging_list);
79                 FREE(lte->file_on_disk);
80                 FREE(lte);
81         }
82 }
83
84 /*
85  * Inserts an entry into the lookup table.
86  *
87  * @lookup_table:       A pointer to the lookup table.
88  * @entry:              A pointer to the entry to insert.
89  */
90 void lookup_table_insert(struct lookup_table *table, 
91                          struct lookup_table_entry *lte)
92 {
93         size_t pos;
94         pos = lte->hash_short % table->capacity;
95         lte->next = table->array[pos];
96         table->array[pos] = lte;
97         /* XXX Make the table grow when too many entries have been inserted. */
98         table->num_entries++;
99 }
100
101
102 /* Unlinks a lookup table entry from the table; does not free it. */
103 void lookup_table_unlink(struct lookup_table *table, 
104                          struct lookup_table_entry *lte)
105 {
106         size_t pos;
107         struct lookup_table_entry *prev, *cur_entry, *next;
108
109         pos = lte->hash_short % table->capacity;
110         prev = NULL;
111         cur_entry = table->array[pos];
112
113         while (cur_entry) {
114                 next = cur_entry->next;
115                 if (cur_entry == lte) {
116                         if (prev)
117                                 prev->next = next;
118                         else
119                                 table->array[pos] = next;
120                         table->num_entries--;
121                         return;
122                 }
123                 prev = cur_entry;
124                 cur_entry = next;
125         }
126 }
127
128
129 /* Decrement the reference count for the dentry having hash value @hash in the
130  * lookup table.  The lookup table entry is unlinked and freed if there are no
131  * references to in remaining.  */
132 struct lookup_table_entry *
133 lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[])
134 {
135         size_t pos = *(size_t*)hash % table->capacity;
136         struct lookup_table_entry *prev = NULL;
137         struct lookup_table_entry *entry = table->array[pos];
138         struct lookup_table_entry *next;
139         while (entry) {
140                 next = entry->next;
141                 if (memcmp(hash, entry->hash, WIM_HASH_SIZE) == 0) {
142                         wimlib_assert(entry->refcnt != 0);
143                         if (--entry->refcnt == 0) {
144                                 if (entry->num_opened_fds == 0) {
145                                         free_lookup_table_entry(entry);
146                                         entry = NULL;
147                                 }
148                                 if (prev)
149                                         prev->next = next;
150                                 else
151                                         table->array[pos] = next;
152                                 break;
153                         }
154                 }
155                 prev = entry;
156                 entry = next;
157         }
158         return entry;
159 }
160
161 /* Like lookup_table_decrement_refcnt(), but for when we already know the lookup
162  * table entry. */
163 struct lookup_table_entry *
164 lte_decrement_refcnt(struct lookup_table_entry *lte, struct lookup_table *table)
165 {
166         if (lte) {
167                 wimlib_assert(lte->refcnt);
168                 if (--lte->refcnt == 0) {
169                         lookup_table_unlink(table, lte);
170                         if (lte->num_opened_fds == 0) {
171                                 free_lookup_table_entry(lte);
172                                 lte = NULL;
173                         }
174                 }
175         }
176         return lte;
177 }
178
179 /* 
180  * Calls a function on all the entries in the lookup table.  Stop early and
181  * return nonzero if any call to the function returns nonzero.
182  */
183 int for_lookup_table_entry(struct lookup_table *table, 
184                            int (*visitor)(struct lookup_table_entry *, void *),
185                            void *arg)
186 {
187         struct lookup_table_entry *entry, *next;
188         size_t i;
189         int ret;
190
191         for (i = 0; i < table->capacity; i++) {
192                 entry = table->array[i];
193                 while (entry) {
194                         next = entry->next;
195                         ret = visitor(entry, arg);
196                         if (ret != 0)
197                                 return ret;
198                         entry = next;
199                 }
200         }
201         return 0;
202 }
203
204
205 /*
206  * Reads the lookup table from a WIM file.
207  *
208  * @fp:                 The FILE* for the WIM file.
209  * @offset:             The offset of the lookup table resource.
210  * @size:               The size of the lookup table resource.
211  * @lookup_table_ret:   A pointer to a struct lookup_table structure into which the
212  *                              lookup table will be returned.
213  * @return:             True on success, false on failure.
214  */
215 int read_lookup_table(FILE *fp, u64 offset, u64 size, 
216                       struct lookup_table **table_ret)
217 {
218         size_t num_entries;
219         u8     buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
220         int    ret;
221         struct lookup_table *table;
222         const u8 *p;
223         struct lookup_table_entry *cur_entry;
224
225         DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
226               offset, size);
227
228         if (fseeko(fp, offset, SEEK_SET) != 0) {
229                 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
230                                  "lookup table", offset);
231                 return WIMLIB_ERR_READ;
232         }
233
234         num_entries = size / WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
235         table = new_lookup_table(num_entries * 2 + 1);
236         if (!table)
237                 return WIMLIB_ERR_NOMEM;
238
239         while (num_entries--) {
240                 if (fread(buf, 1, sizeof(buf), fp) != sizeof(buf)) {
241                         if (feof(fp)) {
242                                 ERROR("Unexpected EOF in WIM lookup table!");
243                         } else {
244                                 ERROR_WITH_ERRNO("Error reading WIM lookup "
245                                                  "table");
246                         }
247                         ret = WIMLIB_ERR_READ;
248                         goto out;
249                 }
250                 cur_entry = new_lookup_table_entry();
251                 if (!cur_entry) {
252                         ret = WIMLIB_ERR_NOMEM;
253                         goto out;
254                 }
255                          
256                 p = get_resource_entry(buf, &cur_entry->resource_entry);
257                 p = get_u16(p, &cur_entry->part_number);
258                 p = get_u32(p, &cur_entry->refcnt);
259                 p = get_bytes(p, WIM_HASH_SIZE, cur_entry->hash);
260                 lookup_table_insert(table, cur_entry);
261         }
262         DEBUG("Done reading lookup table.");
263         *table_ret = table;
264         return 0;
265 out:
266         free_lookup_table(table);
267         return ret;
268 }
269
270
271 /* 
272  * Writes a lookup table entry to the output file.
273  */
274 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
275 {
276         FILE *out;
277         u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
278         u8 *p;
279
280         out = __out;
281
282         /* do not write lookup table entries for empty files */
283         if (lte->output_resource_entry.original_size == 0)
284                 return 0;
285
286         /* Don't write entries that have not had file resources or metadata
287          * resources written for them. */
288         if (lte->out_refcnt == 0)
289                 return 0;
290
291         if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
292                 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
293                       ftello(out), lte->output_resource_entry.original_size);
294
295         p = put_resource_entry(buf, &lte->output_resource_entry);
296         p = put_u16(p, lte->part_number);
297         p = put_u32(p, lte->out_refcnt);
298         p = put_bytes(p, WIM_HASH_SIZE, lte->hash);
299         if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
300                 ERROR_WITH_ERRNO("Failed to write lookup table entry");
301                 return WIMLIB_ERR_WRITE;
302         }
303         return 0;
304 }
305
306 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
307                                       void *ignore)
308 {
309         free_lookup_table_entry(entry);
310         return 0;
311 }
312
313 void free_lookup_table(struct lookup_table *table)
314 {
315         if (!table)
316                 return;
317         if (table->array) {
318                 for_lookup_table_entry(table, do_free_lookup_table_entry, NULL);
319                 FREE(table->array);
320         }
321         FREE(table);
322 }
323
324 int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore)
325 {
326         entry->out_refcnt = 0;
327         return 0;
328 }
329
330 int print_lookup_table_entry(struct lookup_table_entry *entry, void *ignore)
331 {
332         printf("Offset            = %"PRIu64" bytes\n", 
333                entry->resource_entry.offset);
334         printf("Size              = %"PRIu64" bytes\n", 
335                (u64)entry->resource_entry.size);
336         printf("Original size     = %"PRIu64" bytes\n", 
337                entry->resource_entry.original_size);
338         printf("Part Number       = %hu\n", entry->part_number);
339         printf("Reference Count   = %u\n", entry->refcnt);
340         printf("Hash              = ");
341         print_hash(entry->hash);
342         putchar('\n');
343         printf("Flags             = ");
344         u8 flags = entry->resource_entry.flags;
345         if (flags & WIM_RESHDR_FLAG_COMPRESSED)
346                 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
347         if (flags & WIM_RESHDR_FLAG_FREE)
348                 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
349         if (flags & WIM_RESHDR_FLAG_METADATA)
350                 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
351         if (flags & WIM_RESHDR_FLAG_SPANNED)
352                 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
353         putchar('\n');
354         if (entry->file_on_disk)
355                 printf("File on Disk      = `%s'\n", entry->file_on_disk);
356         putchar('\n');
357         return 0;
358 }
359
360 /*
361  * Prints the lookup table of a WIM file. 
362  */
363 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
364 {
365         for_lookup_table_entry(w->lookup_table, 
366                                print_lookup_table_entry, NULL);
367 }
368
369 /* 
370  * Looks up an entry in the lookup table.
371  */
372 struct lookup_table_entry *
373 __lookup_resource(const struct lookup_table *lookup_table, const u8 hash[])
374 {
375         size_t pos;
376         struct lookup_table_entry *lte;
377
378         pos = *(size_t*)hash % lookup_table->capacity;
379         lte = lookup_table->array[pos];
380         while (lte) {
381                 if (memcmp(hash, lte->hash, WIM_HASH_SIZE) == 0)
382                         return lte;
383                 lte = lte->next;
384         }
385         return NULL;
386 }
387
388 /* 
389  * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
390  * given a path name.
391  *
392  * This is only for pre-resolved dentries.
393  */
394 int lookup_resource(WIMStruct *w, const char *path,
395                     int lookup_flags,
396                     struct dentry **dentry_ret,
397                     struct lookup_table_entry **lte_ret,
398                     unsigned *stream_idx_ret)
399 {
400         struct dentry *dentry;
401         struct lookup_table_entry *lte;
402         unsigned stream_idx;
403         dentry = get_dentry(w, path);
404         if (!dentry)
405                 return -ENOENT;
406
407         wimlib_assert(dentry->resolved);
408
409         lte = dentry->lte;
410         if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
411               && dentry_is_directory(dentry))
412                 return -EISDIR;
413         stream_idx = 0;
414         if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
415                 const char *stream_name = path_stream_name(path);
416                 if (stream_name) {
417                         size_t stream_name_len = strlen(stream_name);
418                         for (u16 i = 0; i < dentry->num_ads; i++) {
419                                 if (ads_entry_has_name(&dentry->ads_entries[i],
420                                                        stream_name,
421                                                        stream_name_len))
422                                 {
423                                         stream_idx = i + 1;
424                                         lte = dentry->ads_entries[i].lte;
425                                         goto out;
426                                 }
427                         }
428                         return -ENOENT;
429                 }
430         }
431 out:
432         if (dentry_ret)
433                 *dentry_ret = dentry;
434         if (lte_ret)
435                 *lte_ret = lte;
436         if (stream_idx_ret)
437                 *stream_idx_ret = stream_idx;
438         return 0;
439 }
440
441 /* Resolve a dentry's lookup table entries 
442  *
443  * This replaces the SHA1 hash fields (which are used to lookup an entry in the
444  * lookup table) with pointers directly to the lookup table entries.  A circular
445  * linked list of streams sharing the same lookup table entry is created.
446  *
447  * This function always succeeds; unresolved lookup table entries are given a
448  * NULL pointer.
449  */
450 int dentry_resolve_ltes(struct dentry *dentry, void *__table)
451 {
452         struct lookup_table *table = __table;
453         struct lookup_table_entry *lte;
454
455         wimlib_assert(!dentry->resolved);
456
457         /* Resolve the default file stream */
458         lte = __lookup_resource(table, dentry->hash);
459         if (lte)
460                 list_add(&dentry->lte_group_list.list, &lte->lte_group_list);
461         else
462                 INIT_LIST_HEAD(&dentry->lte_group_list.list);
463         dentry->lte = lte;
464         dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
465         dentry->resolved = true;
466
467         /* Resolve the alternate data streams */
468         if (dentry->ads_entries_status != ADS_ENTRIES_USER) {
469                 for (u16 i = 0; i < dentry->num_ads; i++) {
470                         struct ads_entry *cur_entry = &dentry->ads_entries[i];
471
472                         lte = __lookup_resource(table, cur_entry->hash);
473                         if (lte)
474                                 list_add(&cur_entry->lte_group_list.list,
475                                          &lte->lte_group_list);
476                         else
477                                 INIT_LIST_HEAD(&cur_entry->lte_group_list.list);
478                         cur_entry->lte = lte;
479                         cur_entry->lte_group_list.type = STREAM_TYPE_ADS;
480                 }
481         }
482         return 0;
483 }