]> wimlib.net Git - wimlib/blob - src/lookup_table.c
cc71c8bec4e19b6adc6a59e0a04bb6fb9d8e67a4
[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 #ifdef WITH_FUSE
33 #include <unistd.h>
34 #endif
35
36 struct lookup_table *new_lookup_table(size_t capacity)
37 {
38         struct lookup_table *table;
39         struct hlist_head *array;
40
41         table = MALLOC(sizeof(struct lookup_table));
42         if (!table)
43                 goto err;
44         array = CALLOC(capacity, sizeof(array[0]));
45         if (!array) {
46                 FREE(table);
47                 goto err;
48         }
49         table->num_entries = 0;
50         table->capacity = capacity;
51         table->array = array;
52         return table;
53 err:
54         ERROR("Failed to allocate memory for lookup table with capacity %zu",
55               capacity);
56         return NULL;
57 }
58
59 struct lookup_table_entry *new_lookup_table_entry()
60 {
61         struct lookup_table_entry *lte;
62
63         lte = CALLOC(1, sizeof(struct lookup_table_entry));
64         if (lte) {
65                 lte->part_number  = 1;
66                 lte->refcnt       = 1;
67         } else {
68                 ERROR("Out of memory (tried to allocate %zu bytes for "
69                       "lookup table entry)",
70                       sizeof(struct lookup_table_entry));
71         }
72         return lte;
73 }
74
75 struct lookup_table_entry *
76 clone_lookup_table_entry(const struct lookup_table_entry *old)
77 {
78         struct lookup_table_entry *new;
79
80         new = MALLOC(sizeof(*new));
81         if (!new)
82                 return NULL;
83
84         memcpy(new, old, sizeof(*old));
85         new->extracted_file = NULL;
86         switch (new->resource_location) {
87         case RESOURCE_IN_STAGING_FILE:
88         case RESOURCE_IN_FILE_ON_DISK:
89                 wimlib_assert((void*)&old->file_on_disk ==
90                               (void*)&old->staging_file_name);
91                 new->staging_file_name = STRDUP(old->staging_file_name);
92                 if (!new->staging_file_name)
93                         goto out_free;
94                 break;
95         case RESOURCE_IN_ATTACHED_BUFFER:
96                 new->attached_buffer = MALLOC(wim_resource_size(old));
97                 if (!new->attached_buffer)
98                         goto out_free;
99                 memcpy(new->attached_buffer, old->attached_buffer,
100                        wim_resource_size(old));
101                 break;
102 #ifdef WITH_NTFS_3G
103         case RESOURCE_IN_NTFS_VOLUME:
104                 if (old->ntfs_loc) {
105                         struct ntfs_location *loc;
106                         loc = MALLOC(sizeof(*loc));
107                         if (!loc)
108                                 goto out_free;
109                         memcpy(loc, old->ntfs_loc, sizeof(*loc));
110                         loc->path_utf8 = NULL;
111                         loc->stream_name_utf16 = NULL;
112                         new->ntfs_loc = loc;
113                         loc->path_utf8 = STRDUP(old->ntfs_loc->path_utf8);
114                         if (!loc->path_utf8)
115                                 goto out_free;
116                         loc->stream_name_utf16 = MALLOC(loc->stream_name_utf16_num_chars * 2);
117                         if (!loc->stream_name_utf16)
118                                 goto out_free;
119                         memcpy(loc->stream_name_utf16,
120                                old->ntfs_loc->stream_name_utf16,
121                                loc->stream_name_utf16_num_chars * 2);
122                 }
123                 break;
124 #endif
125         }
126         return new;
127 out_free:
128         free_lookup_table_entry(new);
129         return NULL;
130 }
131
132 void free_lookup_table_entry(struct lookup_table_entry *lte)
133 {
134         if (lte) {
135                 switch (lte->resource_location) {
136                 case RESOURCE_IN_STAGING_FILE:
137                 case RESOURCE_IN_ATTACHED_BUFFER:
138                 case RESOURCE_IN_FILE_ON_DISK:
139                         wimlib_assert(((void*)&lte->file_on_disk ==
140                                       (void*)&lte->staging_file_name)
141                                       && ((void*)&lte->file_on_disk ==
142                                       (void*)&lte->attached_buffer));
143                         FREE(lte->file_on_disk);
144                         break;
145 #ifdef WITH_NTFS_3G
146                 case RESOURCE_IN_NTFS_VOLUME:
147                         if (lte->ntfs_loc) {
148                                 FREE(lte->ntfs_loc->path_utf8);
149                                 FREE(lte->ntfs_loc->stream_name_utf16);
150                                 FREE(lte->ntfs_loc);
151                         }
152                         break;
153 #endif
154                 default:
155                         break;
156                 }
157                 FREE(lte->extracted_file);
158                 FREE(lte);
159         }
160 }
161
162 static int do_free_lookup_table_entry(struct lookup_table_entry *entry,
163                                       void *ignore)
164 {
165         free_lookup_table_entry(entry);
166         return 0;
167 }
168
169
170 void free_lookup_table(struct lookup_table *table)
171 {
172         DEBUG2("Freeing lookup table");
173         if (table) {
174                 if (table->array) {
175                         for_lookup_table_entry(table,
176                                                do_free_lookup_table_entry,
177                                                NULL);
178                         FREE(table->array);
179                 }
180                 FREE(table);
181         }
182 }
183
184 /*
185  * Inserts an entry into the lookup table.
186  *
187  * @table:      A pointer to the lookup table.
188  * @entry:      A pointer to the entry to insert.
189  */
190 void lookup_table_insert(struct lookup_table *table,
191                          struct lookup_table_entry *lte)
192 {
193         size_t i = lte->hash_short % table->capacity;
194         hlist_add_head(&lte->hash_list, &table->array[i]);
195
196         /* XXX Make the table grow when too many entries have been inserted. */
197         table->num_entries++;
198 }
199
200 static void finalize_lte(struct lookup_table_entry *lte)
201 {
202         #ifdef WITH_FUSE
203         if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
204                 unlink(lte->staging_file_name);
205                 wimlib_assert(lte->staging_list.next);
206                 wimlib_assert(lte->staging_list.prev);
207                 list_del(&lte->staging_list);
208         }
209         #endif
210         free_lookup_table_entry(lte);
211 }
212
213 /* Decrements the reference count for the lookup table entry @lte.  If its
214  * reference count reaches 0, it is unlinked from the lookup table.  If,
215  * furthermore, the entry has no opened file descriptors associated with it, the
216  * entry is freed.  */
217 void lte_decrement_refcnt(struct lookup_table_entry *lte,
218                           struct lookup_table *table)
219 {
220         wimlib_assert(lte);
221         wimlib_assert(lte->refcnt);
222         if (--lte->refcnt == 0) {
223                 lookup_table_unlink(table, lte);
224         #ifdef WITH_FUSE
225                 if (lte->num_opened_fds == 0)
226         #endif
227                         finalize_lte(lte);
228         }
229 }
230
231 #ifdef WITH_FUSE
232 void lte_decrement_num_opened_fds(struct lookup_table_entry *lte)
233 {
234         wimlib_assert(lte);
235         wimlib_assert(lte->num_opened_fds);
236         if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
237                 finalize_lte(lte);
238 }
239 #endif
240
241 /*
242  * Calls a function on all the entries in the lookup table.  Stop early and
243  * return nonzero if any call to the function returns nonzero.
244  */
245 int for_lookup_table_entry(struct lookup_table *table,
246                            int (*visitor)(struct lookup_table_entry *, void *),
247                            void *arg)
248 {
249         struct lookup_table_entry *lte;
250         struct hlist_node *pos, *tmp;
251         int ret;
252
253         for (size_t i = 0; i < table->capacity; i++) {
254                 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
255                                           hash_list)
256                 {
257                         ret = visitor(lte, arg);
258                         if (ret != 0)
259                                 return ret;
260                 }
261         }
262         return 0;
263 }
264
265
266 /*
267  * Reads the lookup table from a WIM file.
268  */
269 int read_lookup_table(WIMStruct *w)
270 {
271         u64    num_entries;
272         u8     buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
273         int    ret;
274         struct lookup_table *table;
275         struct lookup_table_entry *cur_entry = NULL, *duplicate_entry;
276
277         DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
278               w->hdr.lookup_table_res_entry.offset,
279               w->hdr.lookup_table_res_entry.original_size);
280
281         if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0)
282         {
283                 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
284                                  "lookup table",
285                                  w->hdr.lookup_table_res_entry.offset);
286                 return WIMLIB_ERR_READ;
287         }
288
289         num_entries = w->hdr.lookup_table_res_entry.original_size /
290                       WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
291         table = new_lookup_table(num_entries * 2 + 1);
292         if (!table)
293                 return WIMLIB_ERR_NOMEM;
294
295         while (num_entries--) {
296                 const u8 *p;
297
298                 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
299                         if (feof(w->fp)) {
300                                 ERROR("Unexpected EOF in WIM lookup table!");
301                         } else {
302                                 ERROR_WITH_ERRNO("Error reading WIM lookup "
303                                                  "table");
304                         }
305                         ret = WIMLIB_ERR_READ;
306                         goto out;
307                 }
308                 cur_entry = new_lookup_table_entry();
309                 if (!cur_entry) {
310                         ret = WIMLIB_ERR_NOMEM;
311                         goto out;
312                 }
313                 cur_entry->wim = w;
314                 cur_entry->resource_location = RESOURCE_IN_WIM;
315
316                 p = get_resource_entry(buf, &cur_entry->resource_entry);
317                 p = get_u16(p, &cur_entry->part_number);
318                 p = get_u32(p, &cur_entry->refcnt);
319                 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
320
321                 if (cur_entry->part_number != w->hdr.part_number) {
322                         ERROR("A lookup table entry in part %hu of the WIM "
323                               "points to part %hu",
324                               w->hdr.part_number, cur_entry->part_number);
325                         ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
326                         goto out_free_cur_entry;
327                 }
328
329                 if (is_zero_hash(cur_entry->hash)) {
330                         ERROR("The WIM lookup table contains an entry with a "
331                               "SHA1 message digest of all 0's");
332                         ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
333                         goto out_free_cur_entry;
334                 }
335
336                 /* Ordinarily, no two streams should share the same SHA1 message
337                  * digest.  However, this constraint can be broken for metadata
338                  * resources--- two identical images will have the same metadata
339                  * resource, but their lookup table entries are not shared. */
340                 duplicate_entry = __lookup_resource(table, cur_entry->hash);
341                 if (duplicate_entry
342                     && !((duplicate_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
343                           && cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA))
344                 {
345                         ERROR("The WIM lookup table contains two entries with the "
346                               "same SHA1 message digest!");
347                         ERROR("The first entry is:");
348                         print_lookup_table_entry(duplicate_entry);
349                         ERROR("The second entry is:");
350                         print_lookup_table_entry(cur_entry);
351                         ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
352                         goto out_free_cur_entry;
353                 }
354
355                 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
356                     && (cur_entry->resource_entry.size !=
357                         cur_entry->resource_entry.original_size))
358                 {
359                         ERROR("Found uncompressed resource with original size "
360                               "not the same as compressed size");
361                         ERROR("The lookup table entry for the resource is as follows:");
362                         print_lookup_table_entry(cur_entry);
363                         ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
364                         goto out_free_cur_entry;
365                 }
366                 lookup_table_insert(table, cur_entry);
367
368         }
369         DEBUG("Done reading lookup table.");
370         w->lookup_table = table;
371         return 0;
372 out_free_cur_entry:
373         FREE(cur_entry);
374 out:
375         free_lookup_table(table);
376         return ret;
377 }
378
379
380 /*
381  * Writes a lookup table entry to the output file.
382  */
383 int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
384 {
385         FILE *out;
386         u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
387         u8 *p;
388
389         out = __out;
390
391         /* Don't write entries that have not had file resources or metadata
392          * resources written for them. */
393         if (lte->out_refcnt == 0)
394                 return 0;
395
396         if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
397                 DEBUG("Writing metadata entry at %lu (orig size = %zu)",
398                       ftello(out), lte->output_resource_entry.original_size);
399
400         p = put_resource_entry(buf, &lte->output_resource_entry);
401         p = put_u16(p, lte->part_number);
402         p = put_u32(p, lte->out_refcnt);
403         p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
404         if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
405                 ERROR_WITH_ERRNO("Failed to write lookup table entry");
406                 return WIMLIB_ERR_WRITE;
407         }
408         return 0;
409 }
410
411
412 int lte_zero_real_refcnt(struct lookup_table_entry *lte, void *ignore)
413 {
414         lte->real_refcnt = 0;
415         return 0;
416 }
417
418 int lte_zero_out_refcnt(struct lookup_table_entry *lte, void *ignore)
419 {
420         lte->out_refcnt = 0;
421         return 0;
422 }
423
424 int lte_free_extracted_file(struct lookup_table_entry *lte, void *ignone)
425 {
426         FREE(lte->extracted_file);
427         lte->extracted_file = NULL;
428         return 0;
429 }
430
431 void print_lookup_table_entry(const struct lookup_table_entry *lte)
432 {
433         if (!lte) {
434                 putchar('\n');
435                 return;
436         }
437         printf("Offset            = %"PRIu64" bytes\n",
438                lte->resource_entry.offset);
439         printf("Size              = %"PRIu64" bytes\n",
440                (u64)lte->resource_entry.size);
441         printf("Original size     = %"PRIu64" bytes\n",
442                lte->resource_entry.original_size);
443         printf("Part Number       = %hu\n", lte->part_number);
444         printf("Reference Count   = %u\n", lte->refcnt);
445         printf("Hash              = 0x");
446         print_hash(lte->hash);
447         putchar('\n');
448         printf("Flags             = ");
449         u8 flags = lte->resource_entry.flags;
450         if (flags & WIM_RESHDR_FLAG_COMPRESSED)
451                 fputs("WIM_RESHDR_FLAG_COMPRESSED, ", stdout);
452         if (flags & WIM_RESHDR_FLAG_FREE)
453                 fputs("WIM_RESHDR_FLAG_FREE, ", stdout);
454         if (flags & WIM_RESHDR_FLAG_METADATA)
455                 fputs("WIM_RESHDR_FLAG_METADATA, ", stdout);
456         if (flags & WIM_RESHDR_FLAG_SPANNED)
457                 fputs("WIM_RESHDR_FLAG_SPANNED, ", stdout);
458         putchar('\n');
459         switch (lte->resource_location) {
460         case RESOURCE_IN_WIM:
461                 if (lte->wim->filename) {
462                         printf("WIM file          = `%s'\n",
463                                lte->wim->filename);
464                 }
465                 break;
466         case RESOURCE_IN_FILE_ON_DISK:
467                 printf("File on Disk      = `%s'\n", lte->file_on_disk);
468                 break;
469         case RESOURCE_IN_STAGING_FILE:
470                 printf("Staging File      = `%s'\n", lte->staging_file_name);
471                 break;
472         default:
473                 break;
474         }
475         putchar('\n');
476 }
477
478 static int do_print_lookup_table_entry(struct lookup_table_entry *lte,
479                                        void *ignore)
480 {
481         print_lookup_table_entry(lte);
482         return 0;
483 }
484
485 /*
486  * Prints the lookup table of a WIM file.
487  */
488 WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
489 {
490         for_lookup_table_entry(w->lookup_table,
491                                do_print_lookup_table_entry,
492                                NULL);
493 }
494
495 /*
496  * Looks up an entry in the lookup table.
497  */
498 struct lookup_table_entry *
499 __lookup_resource(const struct lookup_table *table, const u8 hash[])
500 {
501         size_t i;
502         struct lookup_table_entry *lte;
503         struct hlist_node *pos;
504
505         wimlib_assert(table != NULL);
506         wimlib_assert(hash != NULL);
507
508         i = *(size_t*)hash % table->capacity;
509         hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
510                 if (hashes_equal(hash, lte->hash))
511                         return lte;
512         return NULL;
513 }
514
515 #ifdef WITH_FUSE
516 /*
517  * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
518  * given a path name.
519  *
520  * This is only for pre-resolved inodes.
521  */
522 int lookup_resource(WIMStruct *w, const char *path,
523                     int lookup_flags,
524                     struct dentry **dentry_ret,
525                     struct lookup_table_entry **lte_ret,
526                     u16 *stream_idx_ret)
527 {
528         struct dentry *dentry;
529         struct lookup_table_entry *lte;
530         u16 stream_idx;
531         const char *stream_name = NULL;
532         struct inode *inode;
533         char *p = NULL;
534
535         if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
536                 stream_name = path_stream_name(path);
537                 if (stream_name) {
538                         p = (char*)stream_name - 1;
539                         *p = '\0';
540                 }
541         }
542
543         dentry = get_dentry(w, path);
544         if (p)
545                 *p = ':';
546         if (!dentry)
547                 return -ENOENT;
548
549         inode = dentry->d_inode;
550
551         wimlib_assert(inode->resolved);
552
553         if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
554               && inode_is_directory(inode))
555                 return -EISDIR;
556
557         if (stream_name) {
558                 struct ads_entry *ads_entry;
559                 u16 ads_idx;
560                 ads_entry = inode_get_ads_entry(inode, stream_name,
561                                                 &ads_idx);
562                 if (ads_entry) {
563                         stream_idx = ads_idx + 1;
564                         lte = ads_entry->lte;
565                         goto out;
566                 } else {
567                         return -ENOENT;
568                 }
569         } else {
570                 lte = inode->lte;
571                 stream_idx = 0;
572         }
573 out:
574         if (dentry_ret)
575                 *dentry_ret = dentry;
576         if (lte_ret)
577                 *lte_ret = lte;
578         if (stream_idx_ret)
579                 *stream_idx_ret = stream_idx;
580         return 0;
581 }
582 #endif
583
584 static void inode_resolve_ltes(struct inode *inode, struct lookup_table *table)
585 {
586         struct lookup_table_entry *lte;
587
588         wimlib_assert(!inode->resolved);
589
590         /* Resolve the default file stream */
591         lte = __lookup_resource(table, inode->hash);
592         inode->lte = lte;
593         inode->resolved = true;
594
595         /* Resolve the alternate data streams */
596         for (u16 i = 0; i < inode->num_ads; i++) {
597                 struct ads_entry *cur_entry = &inode->ads_entries[i];
598                 lte = __lookup_resource(table, cur_entry->hash);
599                 cur_entry->lte = lte;
600         }
601 }
602
603 static void inode_unresolve_ltes(struct inode *inode)
604 {
605         wimlib_assert(inode->resolved);
606         if (inode->lte)
607                 copy_hash(inode->hash, inode->lte->hash);
608         else
609                 zero_out_hash(inode->hash);
610
611         for (u16 i = 0; i < inode->num_ads; i++) {
612                 if (inode->ads_entries[i].lte)
613                         copy_hash(inode->ads_entries[i].hash,
614                                   inode->ads_entries[i].lte->hash);
615                 else
616                         zero_out_hash(inode->ads_entries[i].hash);
617         }
618         inode->resolved = false;
619 }
620
621 /* Resolve a dentry's lookup table entries
622  *
623  * This replaces the SHA1 hash fields (which are used to lookup an entry in the
624  * lookup table) with pointers directly to the lookup table entries.  A circular
625  * linked list of streams sharing the same lookup table entry is created.
626  *
627  * This function always succeeds; unresolved lookup table entries are given a
628  * NULL pointer.
629  */
630 int dentry_resolve_ltes(struct dentry *dentry, void *table)
631 {
632         if (!dentry->d_inode->resolved)
633                 inode_resolve_ltes(dentry->d_inode, table);
634         return 0;
635 }
636
637 int dentry_unresolve_ltes(struct dentry *dentry, void *ignore)
638 {
639         if (dentry->d_inode->resolved)
640                 inode_unresolve_ltes(dentry->d_inode);
641         return 0;
642 }
643
644 /* Return the lookup table entry for the unnamed data stream of an inode, or
645  * NULL if there is none.
646  *
647  * You'd think this would be easier than it actually is, since the unnamed data
648  * stream should be the one referenced from the inode itself.  Alas, if there
649  * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
650  * data stream in one of the alternate data streams instead of inside the WIM
651  * dentry itself.  So we need to check the alternate data streams too.
652  *
653  * Also, note that a dentry may appear to have more than one unnamed stream, but
654  * if the SHA1 message digest is all 0's then the corresponding stream does not
655  * really "count" (this is the case for the inode's own file stream when the
656  * file stream that should be there is actually in one of the alternate stream
657  * entries.).  This is despite the fact that we may need to extract such a
658  * missing entry as an empty file or empty named data stream.
659  */
660 struct lookup_table_entry *
661 inode_unnamed_lte(const struct inode *inode,
662                    const struct lookup_table *table)
663 {
664         if (inode->resolved)
665                 return inode_unnamed_lte_resolved(inode);
666         else
667                 return inode_unnamed_lte_unresolved(inode, table);
668 }
669