* 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. */
static u64
-_dentry_correct_length_unaligned(u16 file_name_nbytes, u16 short_name_nbytes)
+dentry_correct_length_unaligned(u16 file_name_nbytes, u16 short_name_nbytes)
{
u64 length = sizeof(struct wim_dentry_on_disk);
if (file_name_nbytes)
* ignored; also, this excludes any alternate data stream entries that may
* follow the dentry. */
static u64
-dentry_correct_length_unaligned(const struct wim_dentry *dentry)
+dentry_correct_length_aligned(const struct wim_dentry *dentry)
{
- return _dentry_correct_length_unaligned(dentry->file_name_nbytes,
- dentry->short_name_nbytes);
+ u64 len;
+
+ len = dentry_correct_length_unaligned(dentry->file_name_nbytes,
+ dentry->short_name_nbytes);
+ return (len + 7) & ~7;
}
/* Duplicates a string of system-dependent encoding into a UTF-16LE string and
#if TCHAR_IS_UTF16LE
name_utf16le_nbytes = tstrlen(name) * sizeof(utf16lechar);
name_utf16le = MALLOC(name_utf16le_nbytes + sizeof(utf16lechar));
- if (!name_utf16le)
+ if (name_utf16le == NULL)
return WIMLIB_ERR_NOMEM;
memcpy(name_utf16le, name, name_utf16le_nbytes + sizeof(utf16lechar));
ret = 0;
return (len + 7) & ~7;
}
+/*
+ * Determine whether to include a "dummy" stream when writing a WIM dentry:
+ *
+ * Some versions of Microsoft's WIM software (the boot driver(s) in WinPE 3.0,
+ * for example) contain a bug where they assume the first alternate data stream
+ * (ADS) entry of a dentry with a nonzero ADS count specifies the unnamed
+ * stream, even if it has a name and the unnamed stream is already specified in
+ * the hash field of the dentry itself.
+ *
+ * wimlib has to work around this behavior by carefully emulating the behavior
+ * of (most versions of) ImageX/WIMGAPI, which move the unnamed stream reference
+ * into the alternate stream entries whenever there are named data streams, even
+ * though there is already a field in the dentry itself for the unnamed stream
+ * reference, which then goes to waste.
+ */
+static inline bool inode_needs_dummy_stream(const struct wim_inode *inode)
+{
+ return (inode->i_num_ads > 0 &&
+ inode->i_num_ads < 0xffff && /* overflow check */
+ inode->i_canonical_streams); /* assume the dentry is okay if it
+ already had an unnamed ADS entry
+ when it was read in */
+}
-static u64
-_dentry_total_length(const struct wim_dentry *dentry, u64 length)
+/* Calculate the total number of bytes that will be consumed when a WIM dentry
+ * is written. This includes base dentry and name fields as well as all
+ * alternate data stream entries and alignment bytes. */
+u64
+dentry_out_total_length(const struct wim_dentry *dentry)
{
+ u64 length = dentry_correct_length_aligned(dentry);
const struct wim_inode *inode = dentry->d_inode;
+
+ if (inode_needs_dummy_stream(inode))
+ length += ads_entry_total_length(&(struct wim_ads_entry){});
+
for (u16 i = 0; i < inode->i_num_ads; i++)
length += ads_entry_total_length(&inode->i_ads_entries[i]);
- return (length + 7) & ~7;
-}
-/* Calculate the aligned *total* length of an on-disk WIM dentry. This includes
- * all alternate data streams. */
-u64
-dentry_correct_total_length(const struct wim_dentry *dentry)
-{
- return _dentry_total_length(dentry,
- dentry_correct_length_unaligned(dentry));
+ return length;
}
-/* Like dentry_correct_total_length(), but use the existing dentry->length field
- * instead of calculating its "correct" value. */
+/* Calculate the aligned, total length of a dentry, including all alternate data
+ * stream entries. Uses dentry->length. */
static u64
-dentry_total_length(const struct wim_dentry *dentry)
+dentry_in_total_length(const struct wim_dentry *dentry)
{
- return _dentry_total_length(dentry, dentry->length);
+ u64 length = dentry->length;
+ const struct wim_inode *inode = dentry->d_inode;
+ for (u16 i = 0; i < inode->i_num_ads; i++)
+ length += ads_entry_total_length(&inode->i_ads_entries[i]);
+ return (length + 7) & ~7;
}
int
{
int ret;
- if (!root)
+ if (root == NULL)
return 0;
ret = (*visitor)(root, arg);
if (ret)
{
int ret;
- if (!root)
+ if (root == NULL)
return 0;
ret = for_dentry_tree_in_rbtree_depth(root->d_inode->i_children.rb_node,
visitor, arg);
if (dentry_is_root(dentry)) {
static const tchar _root_path[] = {WIM_PATH_SEPARATOR, T('\0')};
full_path = TSTRDUP(_root_path);
- if (!full_path)
+ if (full_path == NULL)
return WIMLIB_ERR_NOMEM;
full_path_nbytes = 1 * sizeof(tchar);
} else {
parent_full_path = T("");
parent_full_path_nbytes = 0;
} else {
- if (!parent->_full_path) {
+ if (parent->_full_path == NULL) {
ret = calculate_dentry_full_path(parent);
if (ret)
return ret;
full_path_nbytes = parent_full_path_nbytes + sizeof(tchar) +
filename_nbytes;
full_path = MALLOC(full_path_nbytes + sizeof(tchar));
- if (!full_path)
+ if (full_path == NULL)
return WIMLIB_ERR_NOMEM;
memcpy(full_path, parent_full_path, parent_full_path_nbytes);
full_path[parent_full_path_nbytes / sizeof(tchar)] = WIM_PATH_SEPARATOR;
static int
increment_subdir_offset(struct wim_dentry *dentry, void *subdir_offset_p)
{
- *(u64*)subdir_offset_p += dentry_correct_total_length(dentry);
+ *(u64*)subdir_offset_p += dentry_out_total_length(dentry);
return 0;
}
const utf16lechar *p, *pp;
cur_dentry = parent_dentry = wim_root_dentry(wim);
- if (!cur_dentry) {
+ if (cur_dentry == NULL) {
errno = ENOENT;
return NULL;
}
inode->i_nlink = 1;
inode->i_next_stream_id = 1;
inode->i_not_rpfixed = 1;
+ inode->i_canonical_streams = 1;
INIT_LIST_HEAD(&inode->i_list);
INIT_LIST_HEAD(&inode->i_dentry);
}
int ret;
dentry = MALLOC(sizeof(struct wim_dentry));
- if (!dentry)
+ if (dentry == NULL)
return WIMLIB_ERR_NOMEM;
dentry_common_init(dentry);
dentry->d_inode = new_timeless_inode();
else
dentry->d_inode = new_inode();
- if (!dentry->d_inode) {
+ if (dentry->d_inode == NULL) {
free_dentry(dentry);
return WIMLIB_ERR_NOMEM;
}
if (is_utf16le) {
utf16lechar *p = MALLOC(name_nbytes + sizeof(utf16lechar));
- if (!p)
+ if (p == NULL)
return WIMLIB_ERR_NOMEM;
memcpy(p, name, name_nbytes);
p[name_nbytes / 2] = cpu_to_le16(0);
/*
* Returns the alternate data stream entry belonging to @inode that has the
- * stream name @stream_name.
+ * stream name @stream_name, or NULL if the inode has no alternate data stream
+ * with that name.
+ *
+ * If @p stream_name is the empty string, NULL is returned --- that is, this
+ * function will not return "unnamed" alternate data stream entries.
*/
struct wim_ads_entry *
inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
u16 i;
struct wim_ads_entry *result;
+ if (stream_name[0] == T('\0'))
+ return NULL;
+
#if TCHAR_IS_UTF16LE
const utf16lechar *stream_name_utf16le;
struct wim_ads_entry *ads_entries;
struct wim_ads_entry *new_entry;
+ wimlib_assert(stream_name_nbytes != 0);
+
if (inode->i_num_ads >= 0xfffe) {
ERROR("Too many alternate data streams in one inode!");
return NULL;
num_ads = inode->i_num_ads + 1;
ads_entries = REALLOC(inode->i_ads_entries,
num_ads * sizeof(inode->i_ads_entries[0]));
- if (!ads_entries) {
+ if (ads_entries == NULL) {
ERROR("Failed to allocate memory for new alternate data stream");
return NULL;
}
}
/*
- * Add an alternate stream entry to a WIM inode and return a pointer to it, or
- * NULL if memory could not be allocated.
+ * Add an alternate stream entry to a WIM inode. On success, returns a pointer
+ * to the new entry; on failure, returns NULL.
+ *
+ * @stream_name must be a nonempty string.
*/
struct wim_ads_entry *
inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
sha1_buffer(buffer, size, hash);
existing_lte = lookup_resource(lookup_table, hash);
if (existing_lte) {
- wimlib_assert(wim_resource_size(existing_lte) == size);
+ wimlib_assert(existing_lte->size == size);
lte = existing_lte;
lte->refcnt++;
} else {
void *buffer_copy;
lte = new_lookup_table_entry();
- if (!lte)
+ if (lte == NULL)
return NULL;
buffer_copy = memdup(buffer, size);
- if (!buffer_copy) {
+ if (buffer_copy == NULL) {
free_lookup_table_entry(lte);
return NULL;
}
- lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
- lte->attached_buffer = buffer_copy;
- lte->resource_entry.original_size = size;
+ lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
+ lte->attached_buffer = buffer_copy;
+ lte->size = size;
copy_hash(lte->hash, hash);
lookup_table_insert(lookup_table, lte);
}
wimlib_assert(inode->i_resolved);
new_ads_entry = inode_add_ads(inode, name);
- if (!new_ads_entry)
+ if (new_ads_entry == NULL)
return WIMLIB_ERR_NOMEM;
new_ads_entry->lte = add_stream_from_data_buffer(value, size,
lookup_table);
- if (!new_ads_entry->lte) {
+ if (new_ads_entry->lte == NULL) {
inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
lookup_table);
return WIMLIB_ERR_NOMEM;
struct wim_lookup_table *lookup_table)
{
inode->i_lte = add_stream_from_data_buffer(data, len, lookup_table);
- if (!inode->i_lte)
+ if (inode->i_lte == NULL)
return WIMLIB_ERR_NOMEM;
inode->i_resolved = 1;
return 0;
ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
WIMLIB_UNIX_DATA_TAG, NULL);
- if (!ads_entry)
+ if (ads_entry == NULL)
return NO_UNIX_DATA;
if (stream_idx_ret)
*stream_idx_ret = ads_entry - inode->i_ads_entries;
lte = ads_entry->lte;
- if (!lte)
+ if (lte == NULL)
return NO_UNIX_DATA;
- size = wim_resource_size(lte);
+ size = lte->size;
if (size != sizeof(struct wimlib_unix_data))
return BAD_UNIX_DATA;
- ret = read_full_resource_into_buf(lte, unix_data);
+ ret = read_full_stream_into_buf(lte, unix_data);
if (ret)
return ret;
* data stream entries. */
num_ads = inode->i_num_ads;
ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
- if (!ads_entries)
+ if (ads_entries == NULL)
goto out_of_memory;
/* Read the entries into our newly allocated buffer. */
goto out_invalid;
cur_entry->stream_name = MALLOC(cur_entry->stream_name_nbytes + 2);
- if (!cur_entry->stream_name)
+ if (cur_entry->stream_name == NULL)
goto out_of_memory;
memcpy(cur_entry->stream_name,
disk_entry->stream_name,
cur_entry->stream_name_nbytes);
cur_entry->stream_name[cur_entry->stream_name_nbytes / 2] = cpu_to_le16(0);
+ } else {
+ /* Mark inode as having weird stream entries. */
+ inode->i_canonical_streams = 0;
}
/* It's expected that the size of every ADS entry is a multiple
/* Allocate a `struct wim_inode' for this `struct wim_dentry'. */
inode = new_timeless_inode();
- if (!inode)
+ if (inode == NULL)
return WIMLIB_ERR_NOMEM;
/* Read more fields; some into the dentry, and some into the inode. */
* The calculated length here is unaligned to allow for the possibility
* that the dentry->length names an unaligned length, although this
* would be unexpected. */
- calculated_size = _dentry_correct_length_unaligned(file_name_nbytes,
- short_name_nbytes);
+ calculated_size = dentry_correct_length_unaligned(file_name_nbytes,
+ short_name_nbytes);
if (dentry->length < calculated_size) {
ERROR("Unexpected end of directory entry! (Expected "
* is no null terminator following it. */
if (file_name_nbytes) {
file_name = MALLOC(file_name_nbytes + 2);
- if (!file_name) {
+ if (file_name == NULL) {
ERROR("Failed to allocate %d bytes for dentry file name",
file_name_nbytes + 2);
ret = WIMLIB_ERR_NOMEM;
* filename, there is no null terminator following it. */
if (short_name_nbytes) {
short_name = MALLOC(short_name_nbytes + 2);
- if (!short_name) {
+ if (short_name == NULL) {
ERROR("Failed to allocate %d bytes for dentry short name",
short_name_nbytes + 2);
ret = WIMLIB_ERR_NOMEM;
/* Not end of directory. Allocate this child permanently and
* link it to the parent and previous child. */
child = memdup(&cur_child, sizeof(struct wim_dentry));
- if (!child) {
+ if (child == NULL) {
ERROR("Failed to allocate new dentry!");
ret = WIMLIB_ERR_NOMEM;
break;
* 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);
+ cur_offset += dentry_in_total_length(child);
if (unlikely(!dentry_has_long_name(child))) {
WARNING("Ignoring unnamed dentry in "
return ret;
}
+/*
+ * Writes a WIM alternate data stream (ADS) entry to an output buffer.
+ *
+ * @ads_entry: The ADS entry structure.
+ * @hash: The hash field to use (instead of the one in the ADS entry).
+ * @p: The memory location to write the data to.
+ *
+ * Returns a pointer to the byte after the last byte written.
+ */
+static u8 *
+write_ads_entry(const struct wim_ads_entry *ads_entry,
+ const u8 *hash, u8 * restrict p)
+{
+ struct wim_ads_entry_on_disk *disk_ads_entry =
+ (struct wim_ads_entry_on_disk*)p;
+ u8 *orig_p = p;
+
+ disk_ads_entry->reserved = cpu_to_le64(ads_entry->reserved);
+ copy_hash(disk_ads_entry->hash, hash);
+ disk_ads_entry->stream_name_nbytes = cpu_to_le16(ads_entry->stream_name_nbytes);
+ p += sizeof(struct wim_ads_entry_on_disk);
+ if (ads_entry->stream_name_nbytes) {
+ p = mempcpy(p, ads_entry->stream_name,
+ ads_entry->stream_name_nbytes + 2);
+ }
+ /* Align to 8-byte boundary */
+ while ((uintptr_t)p & 7)
+ *p++ = 0;
+ disk_ads_entry->length = cpu_to_le64(p - orig_p);
+ return p;
+}
+
/*
* Writes a WIM dentry to an output buffer.
*
struct wim_dentry_on_disk *disk_dentry;
const u8 *orig_p;
const u8 *hash;
+ bool use_dummy_stream;
+ u16 num_ads;
wimlib_assert(((uintptr_t)p & 7) == 0); /* 8 byte aligned */
orig_p = p;
inode = dentry->d_inode;
+ use_dummy_stream = inode_needs_dummy_stream(inode);
disk_dentry = (struct wim_dentry_on_disk*)p;
disk_dentry->attributes = cpu_to_le32(inode->i_attributes);
disk_dentry->creation_time = cpu_to_le64(inode->i_creation_time);
disk_dentry->last_access_time = cpu_to_le64(inode->i_last_access_time);
disk_dentry->last_write_time = cpu_to_le64(inode->i_last_write_time);
- hash = inode_stream_hash(inode, 0);
+ if (use_dummy_stream)
+ hash = zero_hash;
+ else
+ hash = inode_stream_hash(inode, 0);
copy_hash(disk_dentry->unnamed_stream_hash, hash);
if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
disk_dentry->reparse.rp_unknown_1 = cpu_to_le32(inode->i_rp_unknown_1);
disk_dentry->nonreparse.hard_link_group_id =
cpu_to_le64((inode->i_nlink == 1) ? 0 : inode->i_ino);
}
- disk_dentry->num_alternate_data_streams = cpu_to_le16(inode->i_num_ads);
+ num_ads = inode->i_num_ads;
+ if (use_dummy_stream)
+ num_ads++;
+ disk_dentry->num_alternate_data_streams = cpu_to_le16(num_ads);
disk_dentry->short_name_nbytes = cpu_to_le16(dentry->short_name_nbytes);
disk_dentry->file_name_nbytes = cpu_to_le16(dentry->file_name_nbytes);
p += sizeof(struct wim_dentry_on_disk);
* have been renamed, thus changing its needed length. */
disk_dentry->length = cpu_to_le64(p - orig_p);
+ if (use_dummy_stream) {
+ hash = inode_unnamed_stream_hash(inode);
+ p = write_ads_entry(&(struct wim_ads_entry){}, hash, p);
+ }
+
/* Write the alternate data streams entries, if any. */
for (u16 i = 0; i < inode->i_num_ads; i++) {
- const struct wim_ads_entry *ads_entry =
- &inode->i_ads_entries[i];
- struct wim_ads_entry_on_disk *disk_ads_entry =
- (struct wim_ads_entry_on_disk*)p;
- orig_p = p;
-
- disk_ads_entry->reserved = cpu_to_le64(ads_entry->reserved);
-
hash = inode_stream_hash(inode, i + 1);
- copy_hash(disk_ads_entry->hash, hash);
- disk_ads_entry->stream_name_nbytes = cpu_to_le16(ads_entry->stream_name_nbytes);
- p += sizeof(struct wim_ads_entry_on_disk);
- if (ads_entry->stream_name_nbytes) {
- p = mempcpy(p, ads_entry->stream_name,
- ads_entry->stream_name_nbytes + 2);
- }
- /* Align to 8-byte boundary */
- while ((uintptr_t)p & 7)
- *p++ = 0;
- disk_ads_entry->length = cpu_to_le64(p - orig_p);
+ p = write_ads_entry(&inode->i_ads_entries[i], hash, p);
}
+
return p;
}
static int
init_wimlib_dentry(struct wimlib_dir_entry *wdentry,
struct wim_dentry *dentry,
- const WIMStruct *wim)
+ const WIMStruct *wim,
+ int flags)
{
int ret;
size_t dummy;
const struct wim_inode *inode = dentry->d_inode;
struct wim_lookup_table_entry *lte;
+ const u8 *hash;
#if TCHAR_IS_UTF16LE
wdentry->filename = dentry->file_name;
wdentry->last_access_time = wim_timestamp_to_timespec(inode->i_last_access_time);
lte = inode_unnamed_lte(inode, wim->lookup_table);
- if (lte)
+ if (lte) {
lte_to_wimlib_resource_entry(lte, &wdentry->streams[0].resource);
+ } else if (!is_zero_hash(hash = inode_unnamed_stream_hash(inode))) {
+ if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
+ return resource_not_found_error(inode, hash);
+ copy_hash(wdentry->streams[0].resource.sha1_hash, hash);
+ wdentry->streams[0].resource.is_missing = 1;
+ }
for (unsigned i = 0; i < inode->i_num_ads; i++) {
- if (inode->i_ads_entries[i].stream_name == NULL)
+ if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
continue;
lte = inode_stream_lte(inode, i + 1, wim->lookup_table);
wdentry->num_named_streams++;
if (lte) {
lte_to_wimlib_resource_entry(lte, &wdentry->streams[
wdentry->num_named_streams].resource);
+ } else if (!is_zero_hash(hash = inode_stream_hash(inode, i + 1))) {
+ if (flags & WIMLIB_ITERATE_DIR_TREE_FLAG_RESOURCES_NEEDED)
+ return resource_not_found_error(inode, hash);
+ copy_hash(wdentry->streams[
+ wdentry->num_named_streams].resource.sha1_hash, hash);
+ wdentry->streams[
+ wdentry->num_named_streams].resource.is_missing = 1;
}
#if TCHAR_IS_UTF16LE
wdentry->streams[wdentry->num_named_streams].stream_name =
wdentry = CALLOC(1, sizeof(struct wimlib_dir_entry) +
(1 + dentry->d_inode->i_num_ads) *
sizeof(struct wimlib_stream_entry));
- if (!wdentry)
+ if (wdentry == NULL)
goto out;
- ret = init_wimlib_dentry(wdentry, dentry, wim);
+ ret = init_wimlib_dentry(wdentry, dentry, wim, flags);
if (ret)
goto out_free_wimlib_dentry;
struct wim_dentry *dentry;
dentry = get_dentry(wim, ctx->path);
- if (!dentry)
+ if (dentry == NULL)
return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
return do_iterate_dir_tree(wim, dentry, ctx->flags, ctx->cb, ctx->user_ctx);
}
/* Compare stream sizes. */
if (lte && template_lte) {
- if (wim_resource_size(lte) != wim_resource_size(template_lte))
+ if (lte->size != template_lte->size)
return false;
/* If hash happens to be available, compare with template. */
!hashes_equal(lte->hash, template_lte->hash))
return false;
- } else if (lte && wim_resource_size(lte)) {
+ } else if (lte && lte->size) {
return false;
- } else if (template_lte && wim_resource_size(template_lte)) {
+ } else if (template_lte && template_lte->size) {
return false;
}
-
}
/* All right, barring a full checksum and given that the inodes share a
* useful stream information (e.g. checksums) from @template_inode.
*
* This assumes that the streams for @inode have been resolved (to point
- * directly to the appropriate `struct wim_lookup_table_entry') but do not
+ * directly to the appropriate `struct wim_lookup_table_entry's) but do not
* necessarily have checksum information filled in.
*/
static int
/* Only take action if both entries exist, the entry for @inode
* has no checksum calculated, but the entry for @template_inode
* does. */
- if (!lte || !template_lte ||
+ if (lte == NULL || template_lte == NULL ||
!lte->unhashed || template_lte->unhashed)
continue;
return ret;
template_dentry = get_dentry(template_wim, dentry->_full_path);
- if (!template_dentry) {
+ if (template_dentry == NULL) {
DEBUG("\"%"TS"\": newly added file", dentry->_full_path);
return 0;
}