]> wimlib.net Git - wimlib/blobdiff - src/dentry.c
Alignment between dentry and ADS entries...
[wimlib] / src / dentry.c
index 4dd26ee0352ab8f71194ed2c46975fdf6e10aa2b..56a06041cb92c2e180583d22588da30b1a2cd764 100644 (file)
 #include <unistd.h>
 #include <sys/stat.h>
 
+/* Real length of a dentry, including the alternate data stream entries, which
+ * are not included in the dentry->length field... */
+u64 dentry_total_length(const struct dentry *dentry)
+{
+       u64 length = (dentry->length + 7) & ~7;
+       for (u16 i = 0 ; i < dentry->num_ads; i++)
+               length += ads_entry_length(&dentry->ads_entries[i]);
+       return length;
+}
 
 /* Transfers file attributes from a `stat' buffer to a struct dentry. */
 void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
 {
        if (S_ISDIR(stbuf->st_mode))
-               dentry->attributes = WIM_FILE_ATTRIBUTE_DIRECTORY;
+               dentry->attributes = FILE_ATTRIBUTE_DIRECTORY;
        else
-               dentry->attributes = WIM_FILE_ATTRIBUTE_NORMAL;
+               dentry->attributes = FILE_ATTRIBUTE_NORMAL;
 }
 
 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
@@ -84,9 +93,9 @@ void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf,
 void dentry_update_all_timestamps(struct dentry *dentry)
 {
        u64 now = get_timestamp();
-       dentry->creation_time       = now;
-       dentry->last_access_time    = now;
-       dentry->last_write_time     = now;
+       dentry->creation_time    = now;
+       dentry->last_access_time = now;
+       dentry->last_write_time  = now;
 }
 
 /* 
@@ -148,49 +157,48 @@ int for_dentry_in_tree_depth(struct dentry *root,
  */
 int calculate_dentry_full_path(struct dentry *dentry, void *ignore)
 {
-       int parent_len;
-       int len;
-       char *parent_full_path;
        char *full_path;
-
-       FREE(dentry->full_path_utf8);
-
+       u32 full_path_len;
        if (dentry_is_root(dentry)) {
-               dentry->full_path_utf8 = MALLOC(2);
-               if (!dentry->full_path_utf8) {
-                       ERROR("Out of memory!\n");
-                       return WIMLIB_ERR_NOMEM;
-               }
-
-               dentry->full_path_utf8[0] = '/';
-               dentry->full_path_utf8[1] = '\0';
-               dentry->full_path_utf8_len = 1;
-               return 0;
-       }
-
-       if (dentry_is_root(dentry->parent)) {
-               parent_len = 0;
-               parent_full_path = "";
+               full_path = MALLOC(2);
+               if (!full_path)
+                       goto oom;
+               full_path[0] = '/';
+               full_path[1] = '\0';
+               full_path_len = 1;
        } else {
-               parent_len = dentry->parent->full_path_utf8_len;
-               parent_full_path = dentry->parent->full_path_utf8;
-       }
+               char *parent_full_path;
+               u32 parent_full_path_len;
+               const struct dentry *parent = dentry->parent;
 
-       len = parent_len + 1 + dentry->file_name_utf8_len;
-       full_path = MALLOC(len + 1);
-       if (!full_path) {
-               ERROR("Out of memory!\n");
-               return WIMLIB_ERR_NOMEM;
-       }
+               if (dentry_is_root(parent)) {
+                       parent_full_path = "";
+                       parent_full_path_len = 0;
+               } else {
+                       parent_full_path = parent->full_path_utf8;
+                       parent_full_path_len = parent->full_path_utf8_len;
+               }
 
-       memcpy(full_path, parent_full_path, parent_len);
-       full_path[parent_len] = '/';
-       memcpy(full_path + parent_len + 1, dentry->file_name_utf8, 
-                               dentry->file_name_utf8_len);
-       full_path[len] = '\0';
+               full_path_len = parent_full_path_len + 1 +
+                               dentry->file_name_utf8_len;
+               full_path = MALLOC(full_path_len + 1);
+               if (!full_path)
+                       goto oom;
+
+               memcpy(full_path, parent_full_path, parent_full_path_len);
+               full_path[parent_full_path_len] = '/';
+               memcpy(full_path + parent_full_path_len + 1,
+                      dentry->file_name_utf8,
+                      dentry->file_name_utf8_len);
+               full_path[full_path_len] = '\0';
+       }
+       FREE(dentry->full_path_utf8);
        dentry->full_path_utf8 = full_path;
-       dentry->full_path_utf8_len = len;
+       dentry->full_path_utf8_len = full_path_len;
        return 0;
+oom:
+       ERROR("Out of memory while calculating dentry full path");
+       return WIMLIB_ERR_NOMEM;
 }
 
 /* 
@@ -211,7 +219,7 @@ void calculate_subdir_offsets(struct dentry *dentry, u64 *subdir_offset_p)
                /* Advance the subdir offset by the amount of space the children
                 * of this dentry take up. */
                do {
-                       *subdir_offset_p += child->length;
+                       *subdir_offset_p += dentry_total_length(child);
                        child = child->next;
                } while (child != dentry->children);
 
@@ -310,19 +318,45 @@ int print_dentry_full_path(struct dentry *dentry, void *ignore)
        return 0;
 }
 
+struct file_attr_flag {
+       u32 flag;
+       const char *name;
+};
+struct file_attr_flag file_attr_flags[] = {
+       {FILE_ATTRIBUTE_READONLY,               "READONLY"},
+       {FILE_ATTRIBUTE_HIDDEN,         "HIDDEN"},
+       {FILE_ATTRIBUTE_SYSTEM,         "SYSTEM"},
+       {FILE_ATTRIBUTE_DIRECTORY,              "DIRECTORY"},
+       {FILE_ATTRIBUTE_ARCHIVE,                "ARCHIVE"},
+       {FILE_ATTRIBUTE_DEVICE,         "DEVICE"},
+       {FILE_ATTRIBUTE_NORMAL,         "NORMAL"},
+       {FILE_ATTRIBUTE_TEMPORARY,              "TEMPORARY"},
+       {FILE_ATTRIBUTE_SPARSE_FILE,    "SPARSE_FILE"},
+       {FILE_ATTRIBUTE_REPARSE_POINT,  "REPARSE_POINT"},
+       {FILE_ATTRIBUTE_COMPRESSED,             "COMPRESSED"},
+       {FILE_ATTRIBUTE_OFFLINE,                "OFFLINE"},
+       {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
+       {FILE_ATTRIBUTE_ENCRYPTED,              "ENCRYPTED"},
+       {FILE_ATTRIBUTE_VIRTUAL,                "VIRTUAL"},
+};
+
 /* Prints a directory entry.  @lookup_table is a pointer to the lookup table, or
  * NULL if the resource entry for the dentry is not to be printed. */
 int print_dentry(struct dentry *dentry, void *lookup_table)
 {
        struct lookup_table_entry *lte;
+       unsigned i;
+
        printf("[DENTRY]\n");
        printf("Length            = %"PRIu64"\n", dentry->length);
        printf("Attributes        = 0x%x\n", dentry->attributes);
-#ifdef ENABLE_SECURITY_DATA
+       for (i = 0; i < ARRAY_LEN(file_attr_flags); i++)
+               if (file_attr_flags[i].flag & dentry->attributes)
+                       printf("    FILE_ATTRIBUTE_%s is set\n",
+                               file_attr_flags[i].name);
        printf("Security ID       = %d\n", dentry->security_id);
-#endif
        printf("Subdir offset     = %"PRIu64"\n", dentry->subdir_offset);
-       /*printf("Unused1           = %"PRIu64"\n", dentry->unused1);*/
+       /*printf("Unused1           = 0x%"PRIu64"\n", dentry->unused1);*/
        /*printf("Unused2           = %"PRIu64"\n", dentry->unused2);*/
        printf("Creation Time     = %"PRIu64"\n", dentry->creation_time);
        printf("Last Access Time  = %"PRIu64"\n", dentry->last_access_time);
@@ -331,9 +365,9 @@ int print_dentry(struct dentry *dentry, void *lookup_table)
        printf("Hash              = "); 
        print_hash(dentry->hash); 
        putchar('\n');
-       /*printf("Reparse Tag       = %u\n", dentry->reparse_tag);*/
-       printf("Hard Link Group   = %"PRIu64"\n", dentry->hard_link);
-       /*printf("Number of Streams = %hu\n", dentry->streams);*/
+       printf("Reparse Tag       = 0x%"PRIx32"\n", dentry->reparse_tag);
+       printf("Hard Link Group   = 0x%"PRIx64"\n", dentry->hard_link);
+       printf("Number of Alternate Data Streams = %hu\n", dentry->num_ads);
        printf("Filename          = \"");
        print_string(dentry->file_name, dentry->file_name_len);
        puts("\"");
@@ -354,6 +388,17 @@ int print_dentry(struct dentry *dentry, void *lookup_table)
        } else {
                putchar('\n');
        }
+       for (u16 i = 0; i < dentry->num_ads; i++) {
+               printf("[Alternate Stream Entry %u]\n", i);
+               printf("Name = \"%s\"\n", dentry->ads_entries[i].stream_name_utf8);
+               printf("Name Length (UTF-16) = %u\n",
+                               dentry->ads_entries[i].stream_name_len);
+               lte = lookup_resource(lookup_table, dentry->ads_entries[i].hash);
+               if (lte)
+                       print_lookup_table_entry(lte, NULL);
+               else
+                       putchar('\n');
+       }
        return 0;
 }
 
@@ -361,9 +406,7 @@ static inline void dentry_common_init(struct dentry *dentry)
 {
        memset(dentry, 0, sizeof(struct dentry));
        dentry->refcnt = 1;
-#ifdef ENABLE_SECURITY_DATA
        dentry->security_id = -1;
-#endif
 }
 
 /* 
@@ -406,7 +449,7 @@ void free_dentry(struct dentry *dentry)
 /* Arguments for do_free_dentry(). */
 struct free_dentry_args {
        struct lookup_table *lookup_table;
-       bool decrement_refcnt;
+       bool lt_decrement_refcnt;
 };
 
 /* 
@@ -417,7 +460,7 @@ static int do_free_dentry(struct dentry *dentry, void *__args)
 {
        struct free_dentry_args *args = (struct free_dentry_args*)__args;
 
-       if (args->decrement_refcnt && !dentry_is_directory(dentry)) {
+       if (args->lt_decrement_refcnt && !dentry_is_directory(dentry)) {
                lookup_table_decrement_refcnt(args->lookup_table, 
                                              dentry->hash);
        }
@@ -437,14 +480,14 @@ static int do_free_dentry(struct dentry *dentry, void *__args)
  *                     reference counts in the lookup table decremented.
  */
 void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table, 
-                     bool decrement_refcnt)
+                     bool lt_decrement_refcnt)
 {
        if (!root || !root->parent)
                return;
 
        struct free_dentry_args args;
        args.lookup_table        = lookup_table;
-       args.decrement_refcnt    = decrement_refcnt;
+       args.lt_decrement_refcnt = lt_decrement_refcnt;
        for_dentry_in_tree_depth(root, do_free_dentry, &args);
 }
 
@@ -500,7 +543,7 @@ static inline void recalculate_dentry_size(struct dentry *dentry)
        dentry->length = WIM_DENTRY_DISK_SIZE + dentry->file_name_len + 
                         2 + dentry->short_name_len;
        /* Must be multiple of 8. */
-       dentry->length += (8 - dentry->length % 8) % 8;
+       dentry->length = (dentry->length + 7) & ~7;
 }
 
 /* Changes the name of a dentry to @new_name.  Only changes the file_name and
@@ -585,11 +628,91 @@ void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *tab
        for_dentry_in_tree(root, calculate_dentry_statistics, &stats);
 }
 
+static int read_ads_entries(const u8 *p, struct dentry *dentry,
+                           u64 remaining_size)
+{
+       u16 num_ads = dentry->num_ads;
+       struct ads_entry *ads_entries = CALLOC(num_ads, sizeof(struct ads_entry));
+       int ret;
+       if (!ads_entries) {
+               ERROR("Could not allocate memory for %"PRIu16" "
+                     "alternate data stream entries", num_ads);
+               return WIMLIB_ERR_NOMEM;
+       }
+       DEBUG2("Reading %"PRIu16" alternate data streams "
+              "(remaining size = %"PRIu64")", num_ads, remaining_size);
+
+       for (u16 i = 0; i < num_ads; i++) {
+               struct ads_entry *cur_entry = &ads_entries[i];
+               u64 length;
+               size_t utf8_len;
+               const char *p_save = p;
+               /* Read the base stream entry, excluding the stream name. */
+               if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
+                       ERROR("Stream entries go past end of metadata resource");
+                       ERROR("(remaining_size = %"PRIu64")", remaining_size);
+                       ret = WIMLIB_ERR_INVALID_DENTRY;
+                       goto out_free_ads_entries;
+               }
+               remaining_size -= WIM_ADS_ENTRY_DISK_SIZE;
+
+               p = get_u64(p, &length); /* ADS entry length */
+
+               DEBUG2("ADS length = %"PRIu64, length);
+
+               p += 8; /* Unused */
+               p = get_bytes(p, WIM_HASH_SIZE, (u8*)cur_entry->hash);
+               p = get_u16(p, &cur_entry->stream_name_len);
+
+               DEBUG2("Stream name length = %u", cur_entry->stream_name_len);
+
+               cur_entry->stream_name = NULL;
+               cur_entry->stream_name_utf8 = NULL;
+
+               if (remaining_size < cur_entry->stream_name_len + 2) {
+                       ERROR("Stream entries go past end of metadata resource");
+                       ERROR("(remaining_size = %"PRIu64" bytes, stream_name_len "
+                             "= %"PRIu16" bytes", remaining_size,
+                             cur_entry->stream_name_len);
+                       ret = WIMLIB_ERR_INVALID_DENTRY;
+                       goto out_free_ads_entries;
+               }
+               remaining_size -= cur_entry->stream_name_len + 2;
+
+               cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
+               if (!cur_entry->stream_name) {
+                       ret = WIMLIB_ERR_NOMEM;
+                       goto out_free_ads_entries;
+               }
+               get_bytes(p, cur_entry->stream_name_len,
+                         (u8*)cur_entry->stream_name);
+               cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
+                                                           cur_entry->stream_name_len,
+                                                           &utf8_len);
+               cur_entry->stream_name_len_utf8 = utf8_len;
+
+               if (!cur_entry->stream_name_utf8) {
+                       ret = WIMLIB_ERR_NOMEM;
+                       goto out_free_ads_entries;
+               }
+               p = p_save + ads_entry_length(cur_entry);
+       }
+       dentry->ads_entries = ads_entries;
+       return 0;
+out_free_ads_entries:
+       for (u16 i = 0; i < num_ads; i++) {
+               FREE(ads_entries[i].stream_name);
+               FREE(ads_entries[i].stream_name_utf8);
+       }
+       FREE(ads_entries);
+       return ret;
+}
+
 /* 
  * Reads a directory entry from the metadata resource.
  */
 int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len, 
-                                       u64 offset, struct dentry *dentry)
+               u64 offset, struct dentry *dentry)
 {
        const u8 *p;
        u64 calculated_size;
@@ -599,14 +722,15 @@ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
        u16 short_name_len;
        u16 file_name_len;
        size_t file_name_utf8_len;
+       int ret;
 
        dentry_common_init(dentry);
 
        /*Make sure the dentry really fits into the metadata resource.*/
        if (offset + 8 > metadata_resource_len) {
                ERROR("Directory entry starting at %"PRIu64" ends past the "
-                       "end of the metadata resource (size %"PRIu64")!\n",
-                       offset, metadata_resource_len);
+                     "end of the metadata resource (size %"PRIu64")",
+                     offset, metadata_resource_len);
                return WIMLIB_ERR_INVALID_DENTRY;
        }
 
@@ -625,9 +749,9 @@ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
 
        if (offset + dentry->length >= metadata_resource_len) {
                ERROR("Directory entry at offset %"PRIu64" and with size "
-                               "%"PRIu64" ends past the end of the metadata resource "
-                               "(size %"PRIu64")!\n", offset, dentry->length,
-                               metadata_resource_len);
+                     "%"PRIu64" ends past the end of the metadata resource "
+                     "(size %"PRIu64")",
+                     offset, dentry->length, metadata_resource_len);
                return WIMLIB_ERR_INVALID_DENTRY;
        }
 
@@ -635,21 +759,19 @@ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
         * Note: The root directory entry has no name, and its length does not
         * include the short name length field.  */
        if (dentry->length < WIM_DENTRY_DISK_SIZE) {
-               ERROR("Directory entry has invalid length of "
-                               "%"PRIu64" bytes\n", dentry->length);
+               ERROR("Directory entry has invalid length of %"PRIu64" bytes",
+                     dentry->length);
                return WIMLIB_ERR_INVALID_DENTRY;
        }
 
        p = get_u32(p, &dentry->attributes);
-#ifdef ENABLE_SECURITY_DATA
        p = get_u32(p, (u32*)&dentry->security_id);
-#else
-       p += sizeof(u32);
-#endif
        p = get_u64(p, &dentry->subdir_offset);
 
        /* 2 unused fields */
        p += 2 * sizeof(u64);
+       /*p = get_u64(p, &dentry->unused1);*/
+       /*p = get_u64(p, &dentry->unused2);*/
 
        p = get_u64(p, &dentry->creation_time);
        p = get_u64(p, &dentry->last_access_time);
@@ -657,15 +779,31 @@ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
 
        p = get_bytes(p, WIM_HASH_SIZE, dentry->hash);
        
-       /* Currently ignoring reparse_tag. */
-       p += sizeof(u32);
+       /*
+        * I don't know what's going on here.  It seems like M$ screwed up the
+        * reparse points, then put the fields in the same place and didn't
+        * document it.  The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
+        * have something to do with this, but it's not documented.
+        */
+       if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
+               /* ??? */
+               u32 u1, u2;
+               p = get_u32(p, &u1);
+               /*p += 4;*/
+               p = get_u32(p, &dentry->reparse_tag);
+               p = get_u32(p, &u2);
+               /*p += 4;*/
+               dentry->hard_link = (u64)(u1) | ((u64)(u2) << 32);
+       } else {
+               p = get_u32(p, &dentry->reparse_tag);
+               p = get_u64(p, &dentry->hard_link);
+       }
 
-       /* The reparse_reserved field does not actually exist. */
+       /* By the way, the reparse_reserved field does not actually exist (at
+        * least when the file is not a reparse point) */
 
-       p = get_u64(p, &dentry->hard_link);
        
-       /* Currently ignoring streams. */
-       p += sizeof(u16);
+       p = get_u16(p, &dentry->num_ads);
 
        p = get_u16(p, &short_name_len);
        p = get_u16(p, &file_name_len);
@@ -675,18 +813,18 @@ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
 
        if (dentry->length < calculated_size) {
                ERROR("Unexpected end of directory entry! (Expected "
-                               "%"PRIu64" bytes, got %"PRIu64" bytes. "
-                               "short_name_len = %hu, file_name_len = %hu)\n", 
-                               calculated_size, dentry->length,
-                               short_name_len, file_name_len);
+                     "%"PRIu64" bytes, got %"PRIu64" bytes. "
+                     "short_name_len = %hu, file_name_len = %hu)", 
+                     calculated_size, dentry->length,
+                     short_name_len, file_name_len);
                return WIMLIB_ERR_INVALID_DENTRY;
        }
 
        /* Read the filename. */
        file_name = MALLOC(file_name_len);
        if (!file_name) {
-               ERROR("Failed to allocate %hu bytes for dentry file name!\n",
-                               file_name_len);
+               ERROR("Failed to allocate %hu bytes for dentry file name",
+                     file_name_len);
                return WIMLIB_ERR_NOMEM;
        }
        p = get_bytes(p, file_name_len, file_name);
@@ -696,25 +834,74 @@ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
                                       &file_name_utf8_len);
 
        if (!file_name_utf8) {
-               ERROR("Failed to allocate memory to convert UTF16 "
-                               "filename (%hu bytes) to UTF8\n",
-                               file_name_len);
-               goto err_nomem2;
+               ERROR("Failed to allocate memory to convert UTF-16 "
+                     "filename (%hu bytes) to UTF-8", file_name_len);
+               ret = WIMLIB_ERR_NOMEM;
+               goto out_free_file_name;
        }
 
        /* Undocumented padding between file name and short name.  This probably
-        * is supposed to be a terminating NULL character. */
+        * is supposed to be a terminating null character. */
        p += 2;
 
        /* Read the short filename. */
        short_name = MALLOC(short_name_len);
        if (!short_name) {
-               ERROR("Failed to allocate %hu bytes for short filename\n",
-                               short_name_len);
-               goto err_nomem1;
+               ERROR("Failed to allocate %hu bytes for short filename",
+                     short_name_len);
+               ret = WIMLIB_ERR_NOMEM;
+               goto out_free_file_name_utf8;
        }
 
-       get_bytes(p, short_name_len, short_name);
+       p = get_bytes(p, short_name_len, short_name);
+
+       /* Some directory entries inexplicibly have a little over 70 bytes of
+        * extra data.  The exact amount of data seems to be 72 bytes, but it is
+        * aligned on the next 8-byte boundary.  Here's an example of the
+        * aligned data:
+        *
+        * 01000000400000006c786bbac58ede11b0bb00261870892ab6adb76fe63a3
+        * e468fca86530d2effa16c786bbac58ede11b0bb00261870892a0000000000
+        * 0000000000000000000000
+        *
+        * Here's one interpretation of how the data is laid out.
+        *
+        * struct unknown {
+        *      u32 field1; (always 0x00000001)
+        *      u32 field2; (always 0x40000000)
+        *      u16 field3;
+        *      u32 field4;
+        *      u32 field5;
+        *      u32 field6;
+        *      u8  data[48]; (???)
+        *      u64 reserved1; (always 0)
+        *      u64 reserved2; (always 0)
+        * };*/
+#if 0
+       if (dentry->length - calculated_size >= WIM_ADS_ENTRY_DISK_SIZE) {
+               printf("%s: %lu / %lu (", file_name_utf8, 
+                               calculated_size, dentry->length);
+               print_string(p + WIM_ADS_ENTRY_DISK_SIZE, dentry->length - calculated_size - WIM_ADS_ENTRY_DISK_SIZE);
+               puts(")");
+               print_byte_field(p, dentry->length - calculated_size);
+               putchar('\n');
+       }
+#endif
+
+       if (dentry->num_ads != 0) {
+               calculated_size = (calculated_size + 7) & ~7;
+               if (calculated_size > metadata_resource_len - offset) {
+                       ERROR("Not enough space in metadata resource for "
+                             "alternate stream entries");
+                       ret = WIMLIB_ERR_INVALID_DENTRY;
+                       goto out_free_short_name;
+               }
+               ret = read_ads_entries(&metadata_resource[offset + calculated_size],
+                                      dentry,
+                                      metadata_resource_len - offset - calculated_size);
+               if (ret != 0)
+                       goto out_free_short_name;
+       }
 
        dentry->file_name          = file_name;
        dentry->file_name_utf8     = file_name_utf8;
@@ -723,11 +910,13 @@ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
        dentry->file_name_utf8_len = file_name_utf8_len;
        dentry->short_name_len     = short_name_len;
        return 0;
-err_nomem1:
-       FREE(dentry->file_name_utf8);
-err_nomem2:
-       FREE(dentry->file_name);
-       return WIMLIB_ERR_NOMEM;
+out_free_short_name:
+       FREE(short_name);
+out_free_file_name_utf8:
+       FREE(file_name_utf8);
+out_free_file_name:
+       FREE(file_name);
+       return ret;
 }
 
 /* 
@@ -735,7 +924,8 @@ err_nomem2:
  *
  * @dentry:  The dentry structure.
  * @p:       The memory location to write the data to.
- * @return:  True on success, false on failure.
+ * @return:  Pointer to the byte after the last byte we wrote as part of the
+ *             dentry.
  */
 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
 {
@@ -743,30 +933,31 @@ static u8 *write_dentry(const struct dentry *dentry, u8 *p)
        memset(p, 0, dentry->length);
        p = put_u64(p, dentry->length);
        p = put_u32(p, dentry->attributes);
-#ifdef ENABLE_SECURITY_DATA
        p = put_u32(p, dentry->security_id);
-#else
-       p = put_u32(p, (u32)(-1));
-#endif
        p = put_u64(p, dentry->subdir_offset);
        p = put_u64(p, 0); /* unused1 */
        p = put_u64(p, 0); /* unused2 */
        p = put_u64(p, dentry->creation_time);
        p = put_u64(p, dentry->last_access_time);
        p = put_u64(p, dentry->last_write_time);
-       if (!is_empty_file_hash(dentry->hash))
-               memcpy(p, dentry->hash, WIM_HASH_SIZE);
-       else
-               DEBUG("zero hash for %s\n", dentry->file_name_utf8);
+       memcpy(p, dentry->hash, WIM_HASH_SIZE);
        p += WIM_HASH_SIZE;
-       p = put_u32(p, 0); /* reparse_tag */
+       p = put_u32(p, dentry->reparse_tag);
        p = put_u64(p, dentry->hard_link);
-       p = put_u16(p, 0); /*streams */
+       p = put_u16(p, dentry->num_ads); /*streams */
        p = put_u16(p, dentry->short_name_len);
        p = put_u16(p, dentry->file_name_len);
        p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
        p = put_u16(p, 0); /* filename padding, 2 bytes. */
        p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
+       for (u16 i = 0; i < dentry->num_ads; i++) {
+               p = put_u64(p, ads_entry_length(&dentry->ads_entries[i]));
+               p = put_u64(p, 0); /* Unused */
+               p = put_bytes(p, WIM_HASH_SIZE, dentry->ads_entries[i].hash);
+               p = put_u16(p, dentry->ads_entries[i].stream_name_len);
+               p = put_bytes(p, dentry->ads_entries[i].stream_name_len,
+                                (u8*)dentry->ads_entries[i].stream_name);
+       }
        return orig_p + dentry->length;
 }
 
@@ -814,13 +1005,14 @@ u8 *write_dentry_tree(const struct dentry *tree, u8 *p)
  * metadata resource and into the dentry tree.
  *
  * @metadata_resource: An array that contains the uncompressed metadata
- *                             resource for the WIM file.
+ *                     resource for the WIM file.
  * @metadata_resource_len:     The length of @metadata_resource.
- * @dentry:    A pointer to a struct dentry that is the root of the directory tree
- *             and has already been read from the metadata resource.  It does not 
- *             need to be the real root, because this procedure is called 
- *             recursively.
- * @return:    True on success, false on failure. 
+ * @dentry:    A pointer to a struct dentry that is the root of the directory
+ *             tree and has already been read from the metadata resource.  It
+ *             does not need to be the real root because this procedure is
+ *             called recursively.
+ *
+ * @return:    Zero on success, nonzero on failure.
  */
 int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
                     struct dentry *dentry)
@@ -856,8 +1048,8 @@ int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
                 * link it to the parent and previous child. */
                child = MALLOC(sizeof(struct dentry));
                if (!child) {
-                       ERROR("Failed to allocate %zu bytes for new dentry!\n",
-                                       sizeof(struct dentry));
+                       ERROR("Failed to allocate %zu bytes for new dentry",
+                             sizeof(struct dentry));
                        ret = WIMLIB_ERR_NOMEM;
                        break;
                }
@@ -883,7 +1075,7 @@ int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
                }
 
                /* Advance to the offset of the next child. */
-               cur_offset += child->length;
+               cur_offset += dentry_total_length(child);
        }
 
        /* Link last child to first one, and set parent's