]> wimlib.net Git - wimlib/blob - src/lookup_table.c
link_group_set_stream_hash(), staging_list
[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         return lte;
70 }
71
72
73 void free_lookup_table_entry(struct lookup_table_entry *lte)
74 {
75         if (lte) {
76                 if (lte->staging_list.next)
77                         list_del(&lte->staging_list);
78                 FREE(lte->file_on_disk);
79                 FREE(lte);
80         }
81 }
82
83 /*
84  * Inserts an entry into the lookup table.
85  *
86  * @lookup_table:       A pointer to the lookup table.
87  * @entry:              A pointer to the entry to insert.
88  */
89 void lookup_table_insert(struct lookup_table *table, 
90                          struct lookup_table_entry *lte)
91 {
92         size_t pos;
93         pos = lte->hash_short % table->capacity;
94         lte->next = table->array[pos];
95         table->array[pos] = lte;
96         /* XXX Make the table grow when too many entries have been inserted. */
97         table->num_entries++;
98 }
99
100
101 /* Unlinks a lookup table entry from the table; does not free it. */
102 void lookup_table_unlink(struct lookup_table *table, 
103                          struct lookup_table_entry *lte)
104 {
105         size_t pos;
106         struct lookup_table_entry *prev, *cur_entry, *next;
107
108         pos = lte->hash_short % table->capacity;
109         prev = NULL;
110         cur_entry = table->array[pos];
111
112         while (cur_entry) {
113                 next = cur_entry->next;
114                 if (cur_entry == lte) {
115                         if (prev)
116                                 prev->next = next;
117                         else
118                                 table->array[pos] = next;
119                         table->num_entries--;
120                         return;
121                 }
122                 prev = cur_entry;
123                 cur_entry = next;
124         }
125 }
126
127
128 /* Decrement the reference count for the dentry having hash value @hash in the
129  * lookup table.  The lookup table entry is unlinked and freed if there are no
130  * references to in remaining.  */
131 struct lookup_table_entry *
132 lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[])
133 {
134         size_t pos = *(size_t*)hash % table->capacity;
135         struct lookup_table_entry *prev = NULL;
136         struct lookup_table_entry *entry = table->array[pos];
137         struct lookup_table_entry *next;
138         while (entry) {
139                 next = entry->next;
140                 if (memcmp(hash, entry->hash, WIM_HASH_SIZE) == 0) {
141                         wimlib_assert(entry->refcnt != 0);
142                         if (--entry->refcnt == 0) {
143                                 if (entry->num_opened_fds == 0) {
144                                         free_lookup_table_entry(entry);
145                                         entry = NULL;
146                                 }
147                                 if (prev)
148                                         prev->next = next;
149                                 else
150                                         table->array[pos] = next;
151                                 break;
152                         }
153                 }
154                 prev = entry;
155                 entry = next;
156         }
157         return entry;
158 }
159
160
161 /* 
162  * Calls a function on all the entries in the lookup table.  Stop early and
163  * return nonzero if any call to the function returns nonzero.
164  */
165 int for_lookup_table_entry(struct lookup_table *table, 
166                            int (*visitor)(struct lookup_table_entry *, void *),
167                            void *arg)
168 {
169         struct lookup_table_entry *entry, *next;
170         size_t i;
171         int ret;
172
173         for (i = 0; i < table->capacity; i++) {
174                 entry = table->array[i];
175                 while (entry) {
176                         next = entry->next;
177                         ret = visitor(entry, arg);
178                         if (ret != 0)
179                                 return ret;
180                         entry = next;
181                 }
182         }
183         return 0;
184 }
185
186
187 /*
188  * Reads the lookup table from a WIM file.
189  *
190  * @fp:                 The FILE* for the WIM file.
191  * @offset:             The offset of the lookup table resource.
192  * @size:               The size of the lookup table resource.
193  * @lookup_table_ret:   A pointer to a struct lookup_table structure into which the
194  *                              lookup table will be returned.
195  * @return:             True on success, false on failure.
196  */
197 int read_lookup_table(FILE *fp, u64 offset, u64 size, 
198                       struct lookup_table **table_ret)
199 {
200         size_t num_entries;
201         u8     buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
202         int    ret;
203         struct lookup_table *table;
204         const u8 *p;
205         struct lookup_table_entry *cur_entry;
206
207         DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
208               offset, size);
209
210         if (fseeko(fp, offset, SEEK_SET) != 0) {
211                 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
212                                  "lookup table", offset);
213                 return WIMLIB_ERR_READ;
214         }
215
216         num_entries = size / WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
217         table = new_lookup_table(num_entries * 2 + 1);
218         if (!table)
219                 return WIMLIB_ERR_NOMEM;
220
221         while (num_entries--) {
222                 if (fread(buf, 1, sizeof(buf), fp) != sizeof(buf)) {
223                         if (feof(fp)) {
224                                 ERROR("Unexpected EOF in WIM lookup table!");
225                         } else {
226                                 ERROR_WITH_ERRNO("Error reading WIM lookup "
227                                                  "table");
228                         }
229                         ret = WIMLIB_ERR_READ;
230                         goto out;
231                 }
232                 cur_entry = new_lookup_table_entry();
233                 if (!cur_entry) {
234                         ret = WIMLIB_ERR_NOMEM;
235                         goto out;
236                 }
237                          
238                 p = get_resource_entry(buf, &cur_entry->resource_entry);
239                 p = get_u16(p, &cur_entry->part_number);
240                 p = get_u32(p, &cur_entry->refcnt);
241                 p = get_bytes(p, WIM_HASH_SIZE, cur_entry->hash);
242                 lookup_table_insert(table, cur_entry);
243         }
244         DEBUG("Done reading lookup table.");
245         *table_ret = table;
246         return 0;
247 out:
248         free_lookup_table(table);
249         return ret;
250 }
251
252
253 /* 
254  * Writes a lookup table entry to the output file.
255  */
256 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
257 {
258         FILE *out;
259         u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
260         u8 *p;
261
262         out = __out;
263
264         /* do not write lookup table entries for empty files */
265         if (lte->output_resource_entry.original_size == 0)
266                 return 0;
267
268         /* Don't write entries that have not had file resources or metadata
269          * resources written for them. */
270         if (lte->out_refcnt == 0)
271                 return 0;
272
273         if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
274                 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
275                       ftello(out), lte->output_resource_entry.original_size);
276
277         p = put_resource_entry(buf, &lte->output_resource_entry);
278         p = put_u16(p, lte->part_number);
279         p = put_u32(p, lte->out_refcnt);
280         p = put_bytes(p, WIM_HASH_SIZE, lte->hash);
281         if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
282                 ERROR_WITH_ERRNO("Failed to write lookup table entry");
283                 return WIMLIB_ERR_WRITE;
284         }
285         return 0;
286 }
287
288 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
289                                       void *ignore)
290 {
291         free_lookup_table_entry(entry);
292         return 0;
293 }
294
295 void free_lookup_table(struct lookup_table *table)
296 {
297         if (!table)
298                 return;
299         if (table->array) {
300                 for_lookup_table_entry(table, do_free_lookup_table_entry, NULL);
301                 FREE(table->array);
302         }
303         FREE(table);
304 }
305
306 int zero_out_refcnts(struct lookup_table_entry *entry, void *ignore)
307 {
308         entry->out_refcnt = 0;
309         return 0;
310 }
311
312 int print_lookup_table_entry(struct lookup_table_entry *entry, void *ignore)
313 {
314         printf("Offset            = %"PRIu64" bytes\n", 
315                entry->resource_entry.offset);
316         printf("Size              = %"PRIu64" bytes\n", 
317                (u64)entry->resource_entry.size);
318         printf("Original size     = %"PRIu64" bytes\n", 
319                entry->resource_entry.original_size);
320         printf("Part Number       = %hu\n", entry->part_number);
321         printf("Reference Count   = %u\n", entry->refcnt);
322         printf("Hash              = ");
323         print_hash(entry->hash);
324         putchar('\n');
325         printf("Flags             = ");
326         u8 flags = entry->resource_entry.flags;
327         if (flags & WIM_RESHDR_FLAG_COMPRESSED)
328                 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
329         if (flags & WIM_RESHDR_FLAG_FREE)
330                 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
331         if (flags & WIM_RESHDR_FLAG_METADATA)
332                 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
333         if (flags & WIM_RESHDR_FLAG_SPANNED)
334                 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
335         putchar('\n');
336         if (entry->file_on_disk)
337                 printf("File on Disk      = `%s'\n", entry->file_on_disk);
338         putchar('\n');
339         return 0;
340 }
341
342 /*
343  * Prints the lookup table of a WIM file. 
344  */
345 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
346 {
347         for_lookup_table_entry(w->lookup_table, 
348                                print_lookup_table_entry, NULL);
349 }
350
351 /* 
352  * Looks up an entry in the lookup table.
353  */
354 struct lookup_table_entry *
355 __lookup_resource(const struct lookup_table *lookup_table, const u8 hash[])
356 {
357         size_t pos;
358         struct lookup_table_entry *lte;
359
360         pos = *(size_t*)hash % lookup_table->capacity;
361         lte = lookup_table->array[pos];
362         while (lte) {
363                 if (memcmp(hash, lte->hash, WIM_HASH_SIZE) == 0)
364                         return lte;
365                 lte = lte->next;
366         }
367         return NULL;
368 }
369
370 int lookup_resource(WIMStruct *w, const char *path,
371                     int lookup_flags,
372                     struct dentry **dentry_ret,
373                     struct lookup_table_entry **lte_ret,
374                     unsigned *stream_idx_ret)
375 {
376         struct dentry *dentry = get_dentry(w, path);
377         struct lookup_table_entry *lte;
378         unsigned stream_idx = 0;
379         const u8 *hash = dentry->hash;
380         if (!dentry)
381                 return -ENOENT;
382         if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
383               && dentry_is_directory(dentry))
384                 return -EISDIR;
385         if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
386                 const char *stream_name = path_stream_name(path);
387                 if (stream_name) {
388                         size_t stream_name_len = strlen(stream_name);
389                         for (u16 i = 0; i < dentry->num_ads; i++) {
390                                 if (ads_entry_has_name(&dentry->ads_entries[i],
391                                                        stream_name,
392                                                        stream_name_len))
393                                 {
394                                         stream_idx = i + 1;
395                                         hash = dentry->ads_entries[i].hash;
396                                         goto do_lookup;
397                                 }
398                         }
399                         return -ENOENT;
400                 }
401         }
402 do_lookup:
403         lte = __lookup_resource(w->lookup_table, hash);
404         if (dentry_ret)
405                 *dentry_ret = dentry;
406         if (lte_ret)
407                 *lte_ret = lte;
408         if (stream_idx_ret)
409                 *stream_idx_ret = stream_idx;
410         return 0;
411 }