e6b8fb2cbafcc86ee033b9d4dd5b579e0b9886d3
[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 hlist_head *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(WIMStruct *wim)
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         lte->wim          = wim;
70         INIT_LIST_HEAD(&lte->lte_group_list);
71         return lte;
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 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
85                                       void *ignore)
86 {
87         free_lookup_table_entry(entry);
88         return 0;
89 }
90
91
92 void free_lookup_table(struct lookup_table *table)
93 {
94         DEBUG("Freeing lookup table");
95         if (table) {
96                 if (table->array) {
97                         for_lookup_table_entry(table,
98                                                do_free_lookup_table_entry,
99                                                NULL);
100                         FREE(table->array);
101                 }
102                 FREE(table);
103         }
104 }
105
106 /*
107  * Inserts an entry into the lookup table.
108  *
109  * @table:      A pointer to the lookup table.
110  * @entry:      A pointer to the entry to insert.
111  */
112 void lookup_table_insert(struct lookup_table *table, 
113                          struct lookup_table_entry *lte)
114 {
115         size_t i = lte->hash_short % table->capacity;
116         hlist_add_head(&lte->hash_list, &table->array[i]);
117
118         /* XXX Make the table grow when too many entries have been inserted. */
119         table->num_entries++;
120 }
121
122
123
124 /* Decrements the reference count for the lookup table entry @lte.  If its
125  * reference count reaches 0, it is unlinked from the lookup table.  If,
126  * furthermore, the entry has no opened file descriptors associated with it, the
127  * entry is freed.  */
128 struct lookup_table_entry *
129 lte_decrement_refcnt(struct lookup_table_entry *lte, struct lookup_table *table)
130 {
131         if (lte) {
132                 wimlib_assert(lte->refcnt);
133                 if (--lte->refcnt == 0) {
134                         lookup_table_unlink(table, lte);
135                         if (lte->num_opened_fds == 0) {
136                                 free_lookup_table_entry(lte);
137                                 lte = NULL;
138                         }
139                 }
140         }
141         return lte;
142 }
143
144 /* 
145  * Calls a function on all the entries in the lookup table.  Stop early and
146  * return nonzero if any call to the function returns nonzero.
147  */
148 int for_lookup_table_entry(struct lookup_table *table, 
149                            int (*visitor)(struct lookup_table_entry *, void *),
150                            void *arg)
151 {
152         struct lookup_table_entry *lte;
153         struct hlist_node *pos, *tmp;
154         int ret;
155
156         for (size_t i = 0; i < table->capacity; i++) {
157                 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
158                                           hash_list)
159                 {
160                         ret = visitor(lte, arg);
161                         if (ret != 0)
162                                 return ret;
163                 }
164         }
165         return 0;
166 }
167
168
169 /*
170  * Reads the lookup table from a WIM file.
171  */
172 int read_lookup_table(WIMStruct *w)
173 {
174         u64    num_entries;
175         u8     buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
176         int    ret;
177         struct lookup_table *table;
178
179         DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
180               w->hdr.lookup_table_res_entry.offset,
181               w->hdr.lookup_table_res_entry.original_size);
182
183         if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0) {
184                 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
185                                  "lookup table",
186                                  w->hdr.lookup_table_res_entry.offset);
187                 return WIMLIB_ERR_READ;
188         }
189
190         num_entries = w->hdr.lookup_table_res_entry.original_size /
191                       WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
192         table = new_lookup_table(num_entries * 2 + 1);
193         if (!table)
194                 return WIMLIB_ERR_NOMEM;
195
196         while (num_entries--) {
197                 const u8 *p;
198                 struct lookup_table_entry *cur_entry;
199
200                 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
201                         if (feof(w->fp)) {
202                                 ERROR("Unexpected EOF in WIM lookup table!");
203                         } else {
204                                 ERROR_WITH_ERRNO("Error reading WIM lookup "
205                                                  "table");
206                         }
207                         ret = WIMLIB_ERR_READ;
208                         goto out;
209                 }
210                 cur_entry = new_lookup_table_entry(w);
211                 if (!cur_entry) {
212                         ret = WIMLIB_ERR_NOMEM;
213                         goto out;
214                 }
215                 cur_entry->wim = w;
216                 cur_entry->resource_location = RESOURCE_IN_WIM;
217                          
218                 p = get_resource_entry(buf, &cur_entry->resource_entry);
219                 p = get_u16(p, &cur_entry->part_number);
220                 p = get_u32(p, &cur_entry->refcnt);
221                 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
222                 lookup_table_insert(table, cur_entry);
223         }
224         DEBUG("Done reading lookup table.");
225         w->lookup_table = table;
226         return 0;
227 out:
228         free_lookup_table(table);
229         return ret;
230 }
231
232
233 /* 
234  * Writes a lookup table entry to the output file.
235  */
236 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
237 {
238         FILE *out;
239         u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
240         u8 *p;
241
242         out = __out;
243
244         /* do not write lookup table entries for empty files */
245         if (lte->output_resource_entry.original_size == 0)
246                 return 0;
247
248         /* Don't write entries that have not had file resources or metadata
249          * resources written for them. */
250         if (lte->out_refcnt == 0)
251                 return 0;
252
253         if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
254                 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
255                       ftello(out), lte->output_resource_entry.original_size);
256
257         p = put_resource_entry(buf, &lte->output_resource_entry);
258         p = put_u16(p, lte->part_number);
259         p = put_u32(p, lte->out_refcnt);
260         p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
261         if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
262                 ERROR_WITH_ERRNO("Failed to write lookup table entry");
263                 return WIMLIB_ERR_WRITE;
264         }
265         return 0;
266 }
267
268
269
270 int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore)
271 {
272         entry->out_refcnt = 0;
273         return 0;
274 }
275
276 void print_lookup_table_entry(struct lookup_table_entry *lte)
277 {
278         if (!lte) {
279                 putchar('\n');
280                 return;
281         }
282         printf("Offset            = %"PRIu64" bytes\n", 
283                lte->resource_entry.offset);
284         printf("Size              = %"PRIu64" bytes\n", 
285                (u64)lte->resource_entry.size);
286         printf("Original size     = %"PRIu64" bytes\n", 
287                lte->resource_entry.original_size);
288         printf("Part Number       = %hu\n", lte->part_number);
289         printf("Reference Count   = %u\n", lte->refcnt);
290         printf("Hash              = 0x");
291         print_hash(lte->hash);
292         putchar('\n');
293         printf("Flags             = ");
294         u8 flags = lte->resource_entry.flags;
295         if (flags & WIM_RESHDR_FLAG_COMPRESSED)
296                 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
297         if (flags & WIM_RESHDR_FLAG_FREE)
298                 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
299         if (flags & WIM_RESHDR_FLAG_METADATA)
300                 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
301         if (flags & WIM_RESHDR_FLAG_SPANNED)
302                 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
303         putchar('\n');
304         switch (lte->resource_location) {
305         case RESOURCE_IN_WIM:
306                 if (lte->wim->filename) {
307                         printf("WIM file          = `%s'\n",
308                                lte->wim->filename);
309                 }
310                 break;
311         case RESOURCE_IN_FILE_ON_DISK:
312                 printf("File on Disk      = `%s'\n", lte->file_on_disk);
313                 break;
314         case RESOURCE_IN_STAGING_FILE:
315                 printf("Staging File      = `%s'\n", lte->staging_file_name);
316                 break;
317         }
318         putchar('\n');
319 }
320
321 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
322                                        void *ignore)
323 {
324         print_lookup_table_entry(lte);
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                                do_print_lookup_table_entry,
335                                NULL);
336 }
337
338 /* 
339  * Looks up an entry in the lookup table.
340  */
341 struct lookup_table_entry *
342 __lookup_resource(const struct lookup_table *table, const u8 hash[])
343 {
344         size_t i;
345         struct lookup_table_entry *lte;
346         struct hlist_node *pos;
347
348         i = *(size_t*)hash % table->capacity;
349         hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
350                 if (hashes_equal(hash, lte->hash))
351                         return lte;
352         return NULL;
353 }
354
355 /* 
356  * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
357  * given a path name.
358  *
359  * This is only for pre-resolved dentries.
360  */
361 int lookup_resource(WIMStruct *w, const char *path,
362                     int lookup_flags,
363                     struct dentry **dentry_ret,
364                     struct lookup_table_entry **lte_ret,
365                     unsigned *stream_idx_ret)
366 {
367         struct dentry *dentry;
368         struct lookup_table_entry *lte;
369         unsigned stream_idx;
370         dentry = get_dentry(w, path);
371         if (!dentry)
372                 return -ENOENT;
373
374         wimlib_assert(dentry->resolved);
375
376         lte = dentry->lte;
377         if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
378               && dentry_is_directory(dentry))
379                 return -EISDIR;
380         stream_idx = 0;
381         if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
382                 const char *stream_name = path_stream_name(path);
383                 if (stream_name) {
384                         size_t stream_name_len = strlen(stream_name);
385                         for (u16 i = 0; i < dentry->num_ads; i++) {
386                                 if (ads_entry_has_name(&dentry->ads_entries[i],
387                                                        stream_name,
388                                                        stream_name_len))
389                                 {
390                                         stream_idx = i + 1;
391                                         lte = dentry->ads_entries[i].lte;
392                                         goto out;
393                                 }
394                         }
395                         return -ENOENT;
396                 }
397         }
398 out:
399         if (dentry_ret)
400                 *dentry_ret = dentry;
401         if (lte_ret)
402                 *lte_ret = lte;
403         if (stream_idx_ret)
404                 *stream_idx_ret = stream_idx;
405         return 0;
406 }
407
408 /* Resolve a dentry's lookup table entries 
409  *
410  * This replaces the SHA1 hash fields (which are used to lookup an entry in the
411  * lookup table) with pointers directly to the lookup table entries.  A circular
412  * linked list of streams sharing the same lookup table entry is created.
413  *
414  * This function always succeeds; unresolved lookup table entries are given a
415  * NULL pointer.
416  */
417 int dentry_resolve_ltes(struct dentry *dentry, void *__table)
418 {
419         struct lookup_table *table = __table;
420         struct lookup_table_entry *lte;
421
422         if (dentry->resolved)
423                 return 0;
424
425         /* Resolve the default file stream */
426         lte = __lookup_resource(table, dentry->hash);
427         if (lte)
428                 list_add(&dentry->lte_group_list.list, &lte->lte_group_list);
429         else
430                 INIT_LIST_HEAD(&dentry->lte_group_list.list);
431         dentry->lte = lte;
432         dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
433         dentry->resolved = true;
434
435         /* Resolve the alternate data streams */
436         if (dentry->ads_entries_status != ADS_ENTRIES_USER) {
437                 for (u16 i = 0; i < dentry->num_ads; i++) {
438                         struct ads_entry *cur_entry = &dentry->ads_entries[i];
439
440                         lte = __lookup_resource(table, cur_entry->hash);
441                         if (lte)
442                                 list_add(&cur_entry->lte_group_list.list,
443                                          &lte->lte_group_list);
444                         else
445                                 INIT_LIST_HEAD(&cur_entry->lte_group_list.list);
446                         cur_entry->lte = lte;
447                         cur_entry->lte_group_list.type = STREAM_TYPE_ADS;
448                 }
449         }
450         return 0;
451 }
452
453 struct lookup_table_entry *
454 dentry_first_lte(const struct dentry *dentry, const struct lookup_table *table)
455 {
456         if (dentry->resolved)
457                 return dentry_first_lte_resolved(dentry);
458         else
459                 return dentry_first_lte_unresolved(dentry, table);
460 }
461