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