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