]> wimlib.net Git - wimlib/blobdiff - src/lookup_table.c
Fixes
[wimlib] / src / lookup_table.c
index a24b02eb9c1725859d02938a0ee5728f51935aae..66c4670076f61dfabf8e278d91caef7d3276f427 100644 (file)
@@ -27,6 +27,7 @@
 #include "wimlib_internal.h"
 #include "lookup_table.h"
 #include "io.h"
+#include <errno.h>
 
 struct lookup_table *new_lookup_table(size_t capacity)
 {
@@ -55,7 +56,7 @@ struct lookup_table_entry *new_lookup_table_entry()
 {
        struct lookup_table_entry *lte;
        
-       lte = MALLOC(sizeof(struct lookup_table_entry));
+       lte = CALLOC(1, sizeof(struct lookup_table_entry));
        if (!lte) {
                ERROR("Out of memory (tried to allocate %zu bytes for "
                      "lookup table entry)",
@@ -63,16 +64,22 @@ struct lookup_table_entry *new_lookup_table_entry()
                return NULL;
        }
 
-       lte->next         = NULL;
-       lte->file_on_disk = NULL;
-       lte->other_wim_fp = NULL;
        lte->part_number  = 1;
        lte->refcnt       = 1;
-       lte->staging_num_times_opened = 0;
+       INIT_LIST_HEAD(&lte->lte_group_list);
        return lte;
 }
 
 
+void free_lookup_table_entry(struct lookup_table_entry *lte)
+{
+       if (lte) {
+               if (lte->staging_list.next)
+                       list_del(&lte->staging_list);
+               FREE(lte->file_on_disk);
+               FREE(lte);
+       }
+}
 
 /*
  * Inserts an entry into the lookup table.
@@ -122,7 +129,8 @@ void lookup_table_unlink(struct lookup_table *table,
 /* Decrement the reference count for the dentry having hash value @hash in the
  * lookup table.  The lookup table entry is unlinked and freed if there are no
  * references to in remaining.  */
-void lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[])
+struct lookup_table_entry *
+lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[])
 {
        size_t pos = *(size_t*)hash % table->capacity;
        struct lookup_table_entry *prev = NULL;
@@ -133,35 +141,39 @@ void lookup_table_decrement_refcnt(struct lookup_table* table, const u8 hash[])
                if (memcmp(hash, entry->hash, WIM_HASH_SIZE) == 0) {
                        wimlib_assert(entry->refcnt != 0);
                        if (--entry->refcnt == 0) {
-                               free_lookup_table_entry(entry);
+                               if (entry->num_opened_fds == 0) {
+                                       free_lookup_table_entry(entry);
+                                       entry = NULL;
+                               }
                                if (prev)
                                        prev->next = next;
                                else
                                        table->array[pos] = next;
+                               break;
                        }
                }
                prev = entry;
                entry = next;
        }
+       return entry;
 }
 
-/* 
- * Looks up an entry in the lookup table.
- */
-struct lookup_table_entry *lookup_resource(const struct lookup_table *lookup_table, 
-                                          const u8 hash[])
+/* Like lookup_table_decrement_refcnt(), but for when we already know the lookup
+ * table entry. */
+struct lookup_table_entry *
+lte_decrement_refcnt(struct lookup_table_entry *lte, struct lookup_table *table)
 {
-       size_t pos;
-       struct lookup_table_entry *lte;
-
-       pos = *(size_t*)hash % lookup_table->capacity;
-       lte = lookup_table->array[pos];
-       while (lte) {
-               if (memcmp(hash, lte->hash, WIM_HASH_SIZE) == 0)
-                       return lte;
-               lte = lte->next;
+       if (lte) {
+               wimlib_assert(lte->refcnt);
+               if (--lte->refcnt == 0) {
+                       lookup_table_unlink(table, lte);
+                       if (lte->num_opened_fds == 0) {
+                               free_lookup_table_entry(lte);
+                               lte = NULL;
+                       }
+               }
        }
-       return NULL;
+       return lte;
 }
 
 /* 
@@ -277,7 +289,8 @@ int write_lookup_table_entry(struct lookup_table_entry *lte, void *__out)
                return 0;
 
        if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
-               DEBUG("Writing metadata entry at %lu", ftello(out));
+               DEBUG("Writing metadata entry at %lu (orig size = %zu)",
+                     ftello(out), lte->output_resource_entry.original_size);
 
        p = put_resource_entry(buf, &lte->output_resource_entry);
        p = put_u16(p, lte->part_number);
@@ -352,3 +365,119 @@ WIMLIBAPI void wimlib_print_lookup_table(WIMStruct *w)
        for_lookup_table_entry(w->lookup_table, 
                               print_lookup_table_entry, NULL);
 }
+
+/* 
+ * Looks up an entry in the lookup table.
+ */
+struct lookup_table_entry *
+__lookup_resource(const struct lookup_table *lookup_table, const u8 hash[])
+{
+       size_t pos;
+       struct lookup_table_entry *lte;
+
+       pos = *(size_t*)hash % lookup_table->capacity;
+       lte = lookup_table->array[pos];
+       while (lte) {
+               if (memcmp(hash, lte->hash, WIM_HASH_SIZE) == 0)
+                       return lte;
+               lte = lte->next;
+       }
+       return NULL;
+}
+
+/* 
+ * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
+ * given a path name.
+ *
+ * This is only for pre-resolved dentries.
+ */
+int lookup_resource(WIMStruct *w, const char *path,
+                   int lookup_flags,
+                   struct dentry **dentry_ret,
+                   struct lookup_table_entry **lte_ret,
+                   unsigned *stream_idx_ret)
+{
+       struct dentry *dentry;
+       struct lookup_table_entry *lte;
+       unsigned stream_idx;
+       dentry = get_dentry(w, path);
+       if (!dentry)
+               return -ENOENT;
+
+       wimlib_assert(dentry->resolved);
+
+       lte = dentry->lte;
+       if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
+             && dentry_is_directory(dentry))
+               return -EISDIR;
+       stream_idx = 0;
+       if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
+               const char *stream_name = path_stream_name(path);
+               if (stream_name) {
+                       size_t stream_name_len = strlen(stream_name);
+                       for (u16 i = 0; i < dentry->num_ads; i++) {
+                               if (ads_entry_has_name(&dentry->ads_entries[i],
+                                                      stream_name,
+                                                      stream_name_len))
+                               {
+                                       stream_idx = i + 1;
+                                       lte = dentry->ads_entries[i].lte;
+                                       goto out;
+                               }
+                       }
+                       return -ENOENT;
+               }
+       }
+out:
+       if (dentry_ret)
+               *dentry_ret = dentry;
+       if (lte_ret)
+               *lte_ret = lte;
+       if (stream_idx_ret)
+               *stream_idx_ret = stream_idx;
+       return 0;
+}
+
+/* Resolve a dentry's lookup table entries 
+ *
+ * This replaces the SHA1 hash fields (which are used to lookup an entry in the
+ * lookup table) with pointers directly to the lookup table entries.  A circular
+ * linked list of streams sharing the same lookup table entry is created.
+ *
+ * This function always succeeds; unresolved lookup table entries are given a
+ * NULL pointer.
+ */
+int dentry_resolve_ltes(struct dentry *dentry, void *__table)
+{
+       struct lookup_table *table = __table;
+       struct lookup_table_entry *lte;
+
+       wimlib_assert(!dentry->resolved);
+
+       /* Resolve the default file stream */
+       lte = __lookup_resource(table, dentry->hash);
+       if (lte)
+               list_add(&dentry->lte_group_list.list, &lte->lte_group_list);
+       else
+               INIT_LIST_HEAD(&dentry->lte_group_list.list);
+       dentry->lte = lte;
+       dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
+       dentry->resolved = true;
+
+       /* Resolve the alternate data streams */
+       if (dentry->ads_entries_status != ADS_ENTRIES_USER) {
+               for (u16 i = 0; i < dentry->num_ads; i++) {
+                       struct ads_entry *cur_entry = &dentry->ads_entries[i];
+
+                       lte = __lookup_resource(table, cur_entry->hash);
+                       if (lte)
+                               list_add(&cur_entry->lte_group_list.list,
+                                        &lte->lte_group_list);
+                       else
+                               INIT_LIST_HEAD(&cur_entry->lte_group_list.list);
+                       cur_entry->lte = lte;
+                       cur_entry->lte_group_list.type = STREAM_TYPE_ADS;
+               }
+       }
+       return 0;
+}