]> wimlib.net Git - wimlib/blob - src/lookup_table.c
Fix a couple random issues
[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 General Public License as published by the Free
15  * Software Foundation; either version 3 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 General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU 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()
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 void free_lookup_table_entry(struct lookup_table_entry *lte)
74 {
75         if (lte) {
76 #ifdef WITH_FUSE
77                 if (lte->staging_list.next)
78                         list_del(&lte->staging_list);
79 #endif
80                 switch (lte->resource_location) {
81                 case RESOURCE_IN_STAGING_FILE:
82                 case RESOURCE_IN_ATTACHED_BUFFER:
83                 case RESOURCE_IN_FILE_ON_DISK:
84                         wimlib_assert(((void*)&lte->file_on_disk ==
85                                       (void*)&lte->staging_file_name)
86                                       && ((void*)&lte->file_on_disk ==
87                                       (void*)&lte->attached_buffer));
88                         FREE(lte->file_on_disk);
89                         break;
90 #ifdef WITH_NTFS_3G
91                 case RESOURCE_IN_NTFS_VOLUME:
92                         if (lte->ntfs_loc) {
93                                 FREE(lte->ntfs_loc->path_utf8);
94                                 FREE(lte->ntfs_loc->stream_name_utf16);
95                                 FREE(lte->ntfs_loc);
96                         }
97                         break;
98 #endif
99                 default:
100                         break;
101                 }
102                 FREE(lte);
103         }
104 }
105
106 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
107                                       void *ignore)
108 {
109         free_lookup_table_entry(entry);
110         return 0;
111 }
112
113
114 void free_lookup_table(struct lookup_table *table)
115 {
116         DEBUG("Freeing lookup table");
117         if (table) {
118                 if (table->array) {
119                         for_lookup_table_entry(table,
120                                                do_free_lookup_table_entry,
121                                                NULL);
122                         FREE(table->array);
123                 }
124                 FREE(table);
125         }
126 }
127
128 /*
129  * Inserts an entry into the lookup table.
130  *
131  * @table:      A pointer to the lookup table.
132  * @entry:      A pointer to the entry to insert.
133  */
134 void lookup_table_insert(struct lookup_table *table, 
135                          struct lookup_table_entry *lte)
136 {
137         size_t i = lte->hash_short % table->capacity;
138         hlist_add_head(&lte->hash_list, &table->array[i]);
139
140         /* XXX Make the table grow when too many entries have been inserted. */
141         table->num_entries++;
142 }
143
144
145
146 /* Decrements the reference count for the lookup table entry @lte.  If its
147  * reference count reaches 0, it is unlinked from the lookup table.  If,
148  * furthermore, the entry has no opened file descriptors associated with it, the
149  * entry is freed.  */
150 struct lookup_table_entry *
151 lte_decrement_refcnt(struct lookup_table_entry *lte, struct lookup_table *table)
152 {
153         if (lte) {
154                 wimlib_assert(lte->refcnt);
155                 if (--lte->refcnt == 0) {
156                         lookup_table_unlink(table, lte);
157                 #ifdef WITH_FUSE
158                         if (lte->num_opened_fds == 0)
159                 #endif
160                         {
161                                 free_lookup_table_entry(lte);
162                                 lte = NULL;
163                         }
164                 }
165         }
166         return lte;
167 }
168
169 /* 
170  * Calls a function on all the entries in the lookup table.  Stop early and
171  * return nonzero if any call to the function returns nonzero.
172  */
173 int for_lookup_table_entry(struct lookup_table *table, 
174                            int (*visitor)(struct lookup_table_entry *, void *),
175                            void *arg)
176 {
177         struct lookup_table_entry *lte;
178         struct hlist_node *pos, *tmp;
179         int ret;
180
181         for (size_t i = 0; i < table->capacity; i++) {
182                 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
183                                           hash_list)
184                 {
185                         ret = visitor(lte, arg);
186                         if (ret != 0)
187                                 return ret;
188                 }
189         }
190         return 0;
191 }
192
193
194 /*
195  * Reads the lookup table from a WIM file.
196  */
197 int read_lookup_table(WIMStruct *w)
198 {
199         u64    num_entries;
200         u8     buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
201         int    ret;
202         struct lookup_table *table;
203
204         DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
205               w->hdr.lookup_table_res_entry.offset,
206               w->hdr.lookup_table_res_entry.original_size);
207
208         if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0) {
209                 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
210                                  "lookup table",
211                                  w->hdr.lookup_table_res_entry.offset);
212                 return WIMLIB_ERR_READ;
213         }
214
215         num_entries = w->hdr.lookup_table_res_entry.original_size /
216                       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                 const u8 *p;
223                 struct lookup_table_entry *cur_entry, *duplicate_entry;
224
225                 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
226                         if (feof(w->fp)) {
227                                 ERROR("Unexpected EOF in WIM lookup table!");
228                         } else {
229                                 ERROR_WITH_ERRNO("Error reading WIM lookup "
230                                                  "table");
231                         }
232                         ret = WIMLIB_ERR_READ;
233                         goto out;
234                 }
235                 cur_entry = new_lookup_table_entry();
236                 if (!cur_entry) {
237                         ret = WIMLIB_ERR_NOMEM;
238                         goto out;
239                 }
240                 cur_entry->wim = w;
241                 cur_entry->resource_location = RESOURCE_IN_WIM;
242                          
243                 p = get_resource_entry(buf, &cur_entry->resource_entry);
244                 p = get_u16(p, &cur_entry->part_number);
245                 p = get_u32(p, &cur_entry->refcnt);
246                 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
247
248                 if (is_zero_hash(cur_entry->hash)) {
249                         ERROR("The WIM lookup table contains an entry with a "
250                               "SHA1 message digest of all 0's");
251                         ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
252                         FREE(cur_entry);
253                         goto out;
254                 }
255
256                 duplicate_entry = __lookup_resource(table, cur_entry->hash);
257                 if (duplicate_entry) {
258                         ERROR("The WIM lookup table contains two entries with the "
259                               "same SHA1 message digest!");
260                         ERROR("The first entry is:");
261                         print_lookup_table_entry(duplicate_entry);
262                         ERROR("The second entry is:");
263                         print_lookup_table_entry(cur_entry);
264                         ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
265                         FREE(cur_entry);
266                         goto out;
267                 }
268                 lookup_table_insert(table, cur_entry);
269
270                 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
271                     && (cur_entry->resource_entry.size !=
272                       cur_entry->resource_entry.original_size))
273                 {
274                         ERROR("Found uncompressed resource with original size "
275                               "not the same as compressed size");
276                         ERROR("The lookup table entry for the resource is as follows:");
277                         print_lookup_table_entry(cur_entry);
278                         ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
279                         goto out;
280                 }
281         }
282         DEBUG("Done reading lookup table.");
283         w->lookup_table = table;
284         return 0;
285 out:
286         free_lookup_table(table);
287         return ret;
288 }
289
290
291 /* 
292  * Writes a lookup table entry to the output file.
293  */
294 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
295 {
296         FILE *out;
297         u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
298         u8 *p;
299
300         out = __out;
301
302         /* Don't write entries that have not had file resources or metadata
303          * resources written for them. */
304         if (lte->out_refcnt == 0)
305                 return 0;
306
307         if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
308                 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
309                       ftello(out), lte->output_resource_entry.original_size);
310
311         p = put_resource_entry(buf, &lte->output_resource_entry);
312         p = put_u16(p, lte->part_number);
313         p = put_u32(p, lte->out_refcnt);
314         p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
315         if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
316                 ERROR_WITH_ERRNO("Failed to write lookup table entry");
317                 return WIMLIB_ERR_WRITE;
318         }
319         return 0;
320 }
321
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 void print_lookup_table_entry(const struct lookup_table_entry *lte)
331 {
332         if (!lte) {
333                 putchar('\n');
334                 return;
335         }
336         printf("Offset            = %"PRIu64" bytes\n", 
337                lte->resource_entry.offset);
338         printf("Size              = %"PRIu64" bytes\n", 
339                (u64)lte->resource_entry.size);
340         printf("Original size     = %"PRIu64" bytes\n", 
341                lte->resource_entry.original_size);
342         printf("Part Number       = %hu\n", lte->part_number);
343         printf("Reference Count   = %u\n", lte->refcnt);
344         printf("Hash              = 0x");
345         print_hash(lte->hash);
346         putchar('\n');
347         printf("Flags             = ");
348         u8 flags = lte->resource_entry.flags;
349         if (flags & WIM_RESHDR_FLAG_COMPRESSED)
350                 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
351         if (flags & WIM_RESHDR_FLAG_FREE)
352                 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
353         if (flags & WIM_RESHDR_FLAG_METADATA)
354                 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
355         if (flags & WIM_RESHDR_FLAG_SPANNED)
356                 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
357         putchar('\n');
358         switch (lte->resource_location) {
359         case RESOURCE_IN_WIM:
360                 if (lte->wim->filename) {
361                         printf("WIM file          = `%s'\n",
362                                lte->wim->filename);
363                 }
364                 break;
365         case RESOURCE_IN_FILE_ON_DISK:
366                 printf("File on Disk      = `%s'\n", lte->file_on_disk);
367                 break;
368         case RESOURCE_IN_STAGING_FILE:
369                 printf("Staging File      = `%s'\n", lte->staging_file_name);
370                 break;
371         default:
372                 break;
373         }
374         putchar('\n');
375 }
376
377 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
378                                        void *ignore)
379 {
380         print_lookup_table_entry(lte);
381         return 0;
382 }
383
384 /*
385  * Prints the lookup table of a WIM file. 
386  */
387 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
388 {
389         for_lookup_table_entry(w->lookup_table, 
390                                do_print_lookup_table_entry,
391                                NULL);
392 }
393
394 /* 
395  * Looks up an entry in the lookup table.
396  */
397 struct lookup_table_entry *
398 __lookup_resource(const struct lookup_table *table, const u8 hash[])
399 {
400         size_t i;
401         struct lookup_table_entry *lte;
402         struct hlist_node *pos;
403
404         i = *(size_t*)hash % table->capacity;
405         hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
406                 if (hashes_equal(hash, lte->hash))
407                         return lte;
408         return NULL;
409 }
410
411 /* 
412  * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
413  * given a path name.
414  *
415  * This is only for pre-resolved dentries.
416  */
417 int lookup_resource(WIMStruct *w, const char *path,
418                     int lookup_flags,
419                     struct dentry **dentry_ret,
420                     struct lookup_table_entry **lte_ret,
421                     unsigned *stream_idx_ret)
422 {
423         struct dentry *dentry;
424         struct lookup_table_entry *lte;
425         unsigned stream_idx;
426         const char *stream_name = NULL;
427         char *p = NULL;
428
429         if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
430                 stream_name = path_stream_name(path);
431                 if (stream_name) {
432                         p = (char*)stream_name - 1;
433                         *p = '\0';
434                 }
435         }
436
437         dentry = get_dentry(w, path);
438         if (p)
439                 *p = ':';
440         if (!dentry)
441                 return -ENOENT;
442
443         wimlib_assert(dentry->resolved);
444
445         lte = dentry->lte;
446         if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
447               && dentry_is_directory(dentry))
448                 return -EISDIR;
449         stream_idx = 0;
450         if (stream_name) {
451                 size_t stream_name_len = strlen(stream_name);
452                 for (u16 i = 0; i < dentry->num_ads; i++) {
453                         if (ads_entry_has_name(&dentry->ads_entries[i],
454                                                stream_name,
455                                                stream_name_len))
456                         {
457                                 stream_idx = i + 1;
458                                 lte = dentry->ads_entries[i].lte;
459                                 goto out;
460                         }
461                 }
462                 return -ENOENT;
463         }
464 out:
465         if (dentry_ret)
466                 *dentry_ret = dentry;
467         if (lte_ret)
468                 *lte_ret = lte;
469         if (stream_idx_ret)
470                 *stream_idx_ret = stream_idx;
471         return 0;
472 }
473
474 /* Resolve a dentry's lookup table entries 
475  *
476  * This replaces the SHA1 hash fields (which are used to lookup an entry in the
477  * lookup table) with pointers directly to the lookup table entries.  A circular
478  * linked list of streams sharing the same lookup table entry is created.
479  *
480  * This function always succeeds; unresolved lookup table entries are given a
481  * NULL pointer.
482  */
483 int dentry_resolve_ltes(struct dentry *dentry, void *__table)
484 {
485         struct lookup_table *table = __table;
486         struct lookup_table_entry *lte;
487
488         if (dentry->resolved)
489                 return 0;
490
491         /* Resolve the default file stream */
492         lte = __lookup_resource(table, dentry->hash);
493         if (lte)
494                 list_add(&dentry->lte_group_list.list, &lte->lte_group_list);
495         else
496                 INIT_LIST_HEAD(&dentry->lte_group_list.list);
497         dentry->lte = lte;
498         dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
499         dentry->resolved = true;
500
501         /* Resolve the alternate data streams */
502         if (dentry->ads_entries_status != ADS_ENTRIES_USER) {
503                 for (u16 i = 0; i < dentry->num_ads; i++) {
504                         struct ads_entry *cur_entry = &dentry->ads_entries[i];
505
506                         lte = __lookup_resource(table, cur_entry->hash);
507                         if (lte)
508                                 list_add(&cur_entry->lte_group_list.list,
509                                          &lte->lte_group_list);
510                         else
511                                 INIT_LIST_HEAD(&cur_entry->lte_group_list.list);
512                         cur_entry->lte = lte;
513                         cur_entry->lte_group_list.type = STREAM_TYPE_ADS;
514                 }
515         }
516         return 0;
517 }
518
519 /* Return the lookup table entry for the unnamed data stream of a dentry, or
520  * NULL if there is none.
521  *
522  * You'd think this would be easier than it actually is, since the unnamed data
523  * stream should be the one referenced from the dentry itself.  Alas, if there
524  * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
525  * data stream in one of the alternate data streams instead of inside the
526  * dentry.  So we need to check the alternate data streams too.
527  *
528  * Also, note that a dentry may appear to have than one unnamed stream, but if
529  * the SHA1 message digest is all 0's then the corresponding stream does not
530  * really "count" (this is the case for the dentry's own file stream when the
531  * file stream that should be there is actually in one of the alternate stream
532  * entries.).  This is despite the fact that we may need to extract such a
533  * missing entry as an empty file or empty named data stream.
534  */
535 struct lookup_table_entry *
536 dentry_unnamed_lte(const struct dentry *dentry,
537                    const struct lookup_table *table)
538 {
539         if (dentry->resolved)
540                 return dentry_unnamed_lte_resolved(dentry);
541         else
542                 return dentry_unnamed_lte_unresolved(dentry, table);
543 }
544