- src = get_dentry(wim, from, case_type);
- if (!src)
- return -errno;
-
- dst = get_dentry(wim, to, case_type);
-
- if (dst) {
- /* Destination file exists */
-
- if (src == dst) /* Same file */
- return 0;
-
- if (!dentry_is_directory(src)) {
- /* Cannot rename non-directory to directory. */
- if (dentry_is_directory(dst))
- return -EISDIR;
- } else {
- /* Cannot rename directory to a non-directory or a non-empty
- * directory */
- if (!dentry_is_directory(dst))
- return -ENOTDIR;
- if (dentry_has_children(dst))
- return -ENOTEMPTY;
- }
- parent_of_dst = dst->parent;
- } else {
- /* Destination does not exist */
- parent_of_dst = get_parent_dentry(wim, to, case_type);
- if (!parent_of_dst)
- return -errno;
-
- if (!dentry_is_directory(parent_of_dst))
- return -ENOTDIR;
- }
-
- ret = set_dentry_name(src, path_basename(to));
- if (ret)
- return -ENOMEM;
- if (dst) {
- unlink_dentry(dst);
- free_dentry_tree(dst, wim->lookup_table);
- }
- unlink_dentry(src);
- dentry_add_child(parent_of_dst, src);
- if (src->_full_path)
- for_dentry_in_tree(src, free_dentry_full_path, NULL);
- return 0;
-}
-
-/*
- * Returns the alternate data stream entry belonging to @inode that has the
- * 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 *idx_ret)
-{
- if (inode->i_num_ads == 0) {
- return NULL;
- } else {
- size_t stream_name_utf16le_nbytes;
- u16 i;
- struct wim_ads_entry *result;
-
- if (stream_name[0] == T('\0'))
- return NULL;
-
- #if TCHAR_IS_UTF16LE
- const utf16lechar *stream_name_utf16le;
-
- stream_name_utf16le = stream_name;
- stream_name_utf16le_nbytes = tstrlen(stream_name) * sizeof(tchar);
- #else
- utf16lechar *stream_name_utf16le;
-
- {
- int ret = tstr_to_utf16le(stream_name,
- tstrlen(stream_name) *
- sizeof(tchar),
- &stream_name_utf16le,
- &stream_name_utf16le_nbytes);
- if (ret)
- return NULL;
- }
- #endif
- i = 0;
- result = NULL;
- do {
- if (ads_entry_has_name(&inode->i_ads_entries[i],
- stream_name_utf16le,
- stream_name_utf16le_nbytes,
- default_ignore_case))
- {
- if (idx_ret)
- *idx_ret = i;
- result = &inode->i_ads_entries[i];
- break;
- }
- } while (++i != inode->i_num_ads);
- #if !TCHAR_IS_UTF16LE
- FREE(stream_name_utf16le);
- #endif
- return result;
- }
-}
-
-static struct wim_ads_entry *
-do_inode_add_ads(struct wim_inode *inode, const void *stream_name,
- size_t stream_name_nbytes, bool is_utf16le)
-{
- u16 num_ads;
- 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 == NULL) {
- ERROR("Failed to allocate memory for new alternate data stream");
- return NULL;
- }
- inode->i_ads_entries = ads_entries;
-
- new_entry = &inode->i_ads_entries[num_ads - 1];
- if (init_ads_entry(new_entry, stream_name, stream_name_nbytes, is_utf16le))
- return NULL;
- new_entry->stream_id = inode->i_next_stream_id++;
- inode->i_num_ads = num_ads;
- return new_entry;
-}
-
-struct wim_ads_entry *
-inode_add_ads_utf16le(struct wim_inode *inode,
- const utf16lechar *stream_name,
- size_t stream_name_nbytes)
-{
- DEBUG("Add alternate data stream \"%"WS"\"", stream_name);
- return do_inode_add_ads(inode, stream_name, stream_name_nbytes, true);
-}
-
-/*
- * 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)
-{
- DEBUG("Add alternate data stream \"%"TS"\"", stream_name);
- return do_inode_add_ads(inode, stream_name,
- tstrlen(stream_name) * sizeof(tchar),
- TCHAR_IS_UTF16LE);
-}
-
-static struct wim_lookup_table_entry *
-add_stream_from_data_buffer(const void *buffer, size_t size,
- struct wim_lookup_table *lookup_table)
-{
- u8 hash[SHA1_HASH_SIZE];
- struct wim_lookup_table_entry *lte, *existing_lte;
-
- sha1_buffer(buffer, size, hash);
- existing_lte = lookup_resource(lookup_table, hash);
- if (existing_lte) {
- wimlib_assert(existing_lte->size == size);
- lte = existing_lte;
- lte->refcnt++;
- } else {
- void *buffer_copy;
- lte = new_lookup_table_entry();
- if (lte == NULL)
- return NULL;
- buffer_copy = memdup(buffer, size);
- if (buffer_copy == NULL) {
- free_lookup_table_entry(lte);
- return NULL;
- }
- 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);
- }
- return lte;
-}
-
-int
-inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
- const void *value, size_t size,
- struct wim_lookup_table *lookup_table)
-{
- struct wim_ads_entry *new_ads_entry;
-
- wimlib_assert(inode->i_resolved);
-
- new_ads_entry = inode_add_ads(inode, name);
- 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 == NULL) {
- inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
- lookup_table);
- return WIMLIB_ERR_NOMEM;
- }
- return 0;
-}
-
-bool
-inode_has_named_stream(const struct wim_inode *inode)
-{
- for (u16 i = 0; i < inode->i_num_ads; i++)
- if (ads_entry_is_named_stream(&inode->i_ads_entries[i]))
- return true;
- return false;
-}
-
-/* Set the unnamed stream of a WIM inode, given a data buffer containing the
- * stream contents. */
-int
-inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
- struct wim_lookup_table *lookup_table)
-{
- inode->i_lte = add_stream_from_data_buffer(data, len, lookup_table);
- if (inode->i_lte == NULL)
- return WIMLIB_ERR_NOMEM;
- inode->i_resolved = 1;
- return 0;
-}
-
-/* Remove an alternate data stream from a WIM inode */
-void
-inode_remove_ads(struct wim_inode *inode, u16 idx,
- struct wim_lookup_table *lookup_table)
-{
- struct wim_ads_entry *ads_entry;
- struct wim_lookup_table_entry *lte;
-
- wimlib_assert(idx < inode->i_num_ads);
- wimlib_assert(inode->i_resolved);
-
- ads_entry = &inode->i_ads_entries[idx];
-
- DEBUG("Remove alternate data stream \"%"WS"\"", ads_entry->stream_name);
-
- lte = ads_entry->lte;
- if (lte)
- lte_decrement_refcnt(lte, lookup_table);
-
- destroy_ads_entry(ads_entry);
-
- memmove(&inode->i_ads_entries[idx],
- &inode->i_ads_entries[idx + 1],
- (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
- inode->i_num_ads--;
-}
-
-bool
-inode_has_unix_data(const struct wim_inode *inode)
-{
- for (u16 i = 0; i < inode->i_num_ads; i++)
- if (ads_entry_is_unix_data(&inode->i_ads_entries[i]))
- return true;
- return false;
-}
-
-#ifndef __WIN32__
-int
-inode_get_unix_data(const struct wim_inode *inode,
- struct wimlib_unix_data *unix_data,
- u16 *stream_idx_ret)
-{
- const struct wim_ads_entry *ads_entry;
- const struct wim_lookup_table_entry *lte;
- size_t size;
- int ret;
-
- wimlib_assert(inode->i_resolved);
-
- ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
- WIMLIB_UNIX_DATA_TAG, NULL);
- 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 == NULL)
- return NO_UNIX_DATA;
-
- size = lte->size;
- if (size != sizeof(struct wimlib_unix_data))
- return BAD_UNIX_DATA;
-
- ret = read_full_stream_into_buf(lte, unix_data);
- if (ret)
- return ret;
-
- if (unix_data->version != 0)
- return BAD_UNIX_DATA;
- return 0;
-}
-
-int
-inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
- struct wim_lookup_table *lookup_table, int which)
-{
- struct wimlib_unix_data unix_data;
- int ret;
- bool have_good_unix_data = false;
- bool have_unix_data = false;
- u16 stream_idx;