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