X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Fdentry.c;h=f986f9df12fbfe02624b70bbc66dee964565028c;hb=e9fa56fa6e181ae15282a2fcf0dbfa0b7c59cd81;hp=5082eac64d48af415a88daef375efed499f31c3b;hpb=55491147fce2bc03ffb602a3985e7fd4e32169a3;p=wimlib diff --git a/src/dentry.c b/src/dentry.c index 5082eac6..f986f9df 100644 --- a/src/dentry.c +++ b/src/dentry.c @@ -70,6 +70,8 @@ struct wim_ads_entry_on_disk { utf16lechar stream_name[]; } _packed_attribute; +#define WIM_ADS_ENTRY_DISK_SIZE 38 + /* WIM directory entry (on-disk format) */ struct wim_dentry_on_disk { le64 length; @@ -105,6 +107,8 @@ struct wim_dentry_on_disk { /*utf16lechar short_name[];*/ } _packed_attribute; +#define WIM_DENTRY_DISK_SIZE 102 + /* Calculates the unaligned length, in bytes, of an on-disk WIM dentry that has * a file name and short name that take the specified numbers of bytes. This * excludes any alternate data stream entries that may follow the dentry. */ @@ -1025,6 +1029,7 @@ dentry_add_child(struct wim_dentry * restrict parent, struct wim_dentry * restrict child) { wimlib_assert(dentry_is_directory(parent)); + wimlib_assert(parent != child); struct rb_root *root = &parent->d_inode->i_children; struct rb_node **new = &(root->rb_node); @@ -1179,12 +1184,11 @@ add_stream_from_data_buffer(const void *buffer, size_t size, lte = new_lookup_table_entry(); if (!lte) return NULL; - buffer_copy = MALLOC(size); + buffer_copy = memdup(buffer, size); if (!buffer_copy) { free_lookup_table_entry(lte); return NULL; } - memcpy(buffer_copy, buffer, size); lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER; lte->attached_buffer = buffer_copy; lte->resource_entry.original_size = size; @@ -1387,6 +1391,8 @@ read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode, struct wim_ads_entry *ads_entries; int ret; + BUILD_BUG_ON(sizeof(struct wim_ads_entry_on_disk) != WIM_ADS_ENTRY_DISK_SIZE); + /* Allocate an array for our in-memory representation of the alternate * data stream entries. */ num_ads = inode->i_num_ads; @@ -1531,6 +1537,8 @@ read_dentry(const u8 * restrict metadata_resource, u64 metadata_resource_len, const struct wim_dentry_on_disk *disk_dentry = (const struct wim_dentry_on_disk*)p; + BUILD_BUG_ON(sizeof(struct wim_dentry_on_disk) != WIM_DENTRY_DISK_SIZE); + if ((uintptr_t)p & 7) WARNING("WIM dentry is not 8-byte aligned"); @@ -1773,32 +1781,43 @@ read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len, /* Not end of directory. Allocate this child permanently and * link it to the parent and previous child. */ - child = MALLOC(sizeof(struct wim_dentry)); + child = memdup(&cur_child, sizeof(struct wim_dentry)); if (!child) { - ERROR("Failed to allocate %zu bytes for new dentry", - sizeof(struct wim_dentry)); + ERROR("Failed to allocate new dentry!"); ret = WIMLIB_ERR_NOMEM; break; } - memcpy(child, &cur_child, sizeof(struct wim_dentry)); - dentry_add_child(dentry, child); - inode_add_dentry(child, child->d_inode); - - /* If there are children of this child, call this procedure - * recursively. */ - if (child->subdir_offset != 0) { - ret = read_dentry_tree(metadata_resource, - metadata_resource_len, child); - if (ret) - break; - } /* Advance to the offset of the next child. Note: We need to * advance by the TOTAL length of the dentry, not by the length - * child->length, which although it does take into account the - * padding, it DOES NOT take into account alternate stream + * cur_child.length, which although it does take into account + * the padding, it DOES NOT take into account alternate stream * entries. */ cur_offset += dentry_total_length(child); + + if (dentry_add_child(dentry, child)) { + WARNING("Ignoring duplicate dentry \"%"WS"\"", + child->file_name); + WARNING("(In directory \"%"TS"\")", dentry_full_path(dentry)); + free_dentry(child); + } else { + inode_add_dentry(child, child->d_inode); + /* If there are children of this child, call this + * procedure recursively. */ + if (child->subdir_offset != 0) { + if (dentry_is_directory(child)) { + ret = read_dentry_tree(metadata_resource, + metadata_resource_len, + child); + if (ret) + break; + } else { + WARNING("Ignoring children of non-directory \"%"TS"\"", + dentry_full_path(child)); + } + } + + } } return ret; }