#include "wimlib/lookup_table.h"
#include "wimlib/metadata.h"
#include "wimlib/resource.h"
+#include "wimlib/security.h"
#include "wimlib/sha1.h"
#include "wimlib/timestamp.h"
le64 unused_1;
le64 unused_2;
- /* The following three time fields should correspond to those gotten by
- * calling GetFileTime() on Windows. */
- /* Creation time, in 100-nanosecond intervals since January 1, 1601. */
+ /* Creation time, last access time, and last write time, in
+ * 100-nanosecond intervals since 12:00 a.m UTC January 1, 1601. They
+ * should correspond to the times gotten by calling GetFileTime() on
+ * Windows. */
le64 creation_time;
-
- /* Last access time, in 100-nanosecond intervals since January 1, 1601. */
le64 last_access_time;
-
- /* Last write time, in 100-nanosecond intervals since January 1, 1601. */
le64 last_write_time;
/* Vaguely, the SHA-1 message digest ("hash") of the file's contents.
* set to 0. */
le16 file_name_nbytes;
- /* Follewed by variable length file name, in UTF16-LE, if
+ /* Followed by variable length file name, in UTF16-LE, if
* file_name_nbytes != 0. Includes null terminator. */
/*utf16lechar file_name[];*/
return cur_dentry;
}
-/* Returns the dentry corresponding to the @path, or NULL if there is no such
- * dentry. */
+/*
+ * Returns the dentry in the currently selected WIM image named by @path
+ * starting from the root of the WIM image, or NULL if there is no such dentry.
+ *
+ * On Windows, the search is done case-insensitively.
+ */
struct wim_dentry *
get_dentry(WIMStruct *wim, const tchar *path)
{
inode->i_next_stream_id = 1;
inode->i_not_rpfixed = 1;
INIT_LIST_HEAD(&inode->i_list);
- #ifdef WITH_FUSE
- if (pthread_mutex_init(&inode->i_mutex, NULL) != 0) {
- ERROR_WITH_ERRNO("Error initializing mutex");
- FREE(inode);
- return NULL;
- }
- #endif
INIT_LIST_HEAD(&inode->i_dentry);
}
return inode;
return 0;
}
+static int
+dentry_clear_inode_visited(struct wim_dentry *dentry, void *_ignore)
+{
+ dentry->d_inode->i_visited = 0;
+ return 0;
+}
+
+void
+dentry_tree_clear_inode_visited(struct wim_dentry *root)
+{
+ for_dentry_in_tree(root, dentry_clear_inode_visited, NULL);
+}
+
static int
init_ads_entry(struct wim_ads_entry *ads_entry, const void *name,
size_t name_nbytes, bool is_utf16le)
destroy_ads_entry(&inode->i_ads_entries[i]);
FREE(inode->i_ads_entries);
}
- #ifdef WITH_FUSE
- wimlib_assert(inode->i_num_opened_fds == 0);
- FREE(inode->i_fds);
- pthread_mutex_destroy(&inode->i_mutex);
- #endif
/* HACK: This may instead delete the inode from i_list, but the
* hlist_del() behaves the same as list_del(). */
if (!hlist_unhashed(&inode->i_hlist))
hlist_del(&inode->i_hlist);
- FREE(inode->i_extracted_file);
FREE(inode);
}
}
/*
* Unlinks and frees a dentry tree.
*
- * @root: The root of the tree.
- * @lookup_table: The lookup table for dentries. If non-NULL, the
- * reference counts in the lookup table for the lookup
- * table entries corresponding to the dentries will be
- * decremented.
+ * @root:
+ * The root of the tree.
+ *
+ * @lookup_table:
+ * The lookup table for dentries. If non-NULL, the reference counts in the
+ * lookup table for the lookup table entries corresponding to the dentries
+ * will be decremented.
*/
void
free_dentry_tree(struct wim_dentry *root, struct wim_lookup_table *lookup_table)
struct wim_lookup_table_entry *lte, *existing_lte;
sha1_buffer(buffer, size, hash);
- existing_lte = __lookup_resource(lookup_table, hash);
+ existing_lte = lookup_resource(lookup_table, hash);
if (existing_lte) {
wimlib_assert(wim_resource_size(existing_lte) == size);
lte = existing_lte;
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->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,
/*
* Reads the alternate data stream entries of a WIM dentry.
*
- * @p: Pointer to buffer that starts with the first alternate stream entry.
+ * @p:
+ * Pointer to buffer that starts with the first alternate stream entry.
*
- * @inode: Inode to load the alternate data streams into.
- * @inode->i_num_ads must have been set to the number of
- * alternate data streams that are expected.
+ * @inode:
+ * Inode to load the alternate data streams into. @inode->i_num_ads must
+ * have been set to the number of alternate data streams that are expected.
*
- * @remaining_size: Number of bytes of data remaining in the buffer pointed
- * to by @p.
+ * @remaining_size:
+ * Number of bytes of data remaining in the buffer pointed to by @p.
*
+ * On success, inode->i_ads_entries is set to an array of `struct
+ * wim_ads_entry's of length inode->i_num_ads. On failure, @inode is not
+ * modified.
*
- * Return 0 on success or nonzero on failure. On success, inode->i_ads_entries
- * is set to an array of `struct wim_ads_entry's of length inode->i_num_ads. On
- * failure, @inode is not modified.
+ * Return values:
+ * WIMLIB_ERR_SUCCESS (0)
+ * WIMLIB_ERR_INVALID_METADATA_RESOURCE
+ * WIMLIB_ERR_NOMEM
*/
static int
read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
goto out_free_ads_entries;
out_invalid:
ERROR("An alternate data stream entry is invalid");
- ret = WIMLIB_ERR_INVALID_DENTRY;
+ ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
out_free_ads_entries:
if (ads_entries) {
for (u16 i = 0; i < num_ads; i++)
* @metadata_resource_len:
* Length of the metadata resource buffer, in bytes.
*
- * @offset: Offset of the dentry within the metadata resource.
+ * @offset: Offset of the dentry within the metadata resource.
*
* @dentry: A `struct wim_dentry' that will be filled in by this function.
*
* this was a special "end of directory" dentry and not a real dentry. If
* nonzero, this was a real dentry.
*
- * Possible errors include:
- * WIMLIB_ERR_NOMEM
- * WIMLIB_ERR_INVALID_DENTRY
+ * Return values:
+ * WIMLIB_ERR_SUCCESS (0)
+ * WIMLIB_ERR_INVALID_METADATA_RESOURCE
+ * WIMLIB_ERR_NOMEM
*/
int
read_dentry(const u8 * restrict metadata_resource, u64 metadata_resource_len,
ERROR("Directory entry starting at %"PRIu64" ends past the "
"end of the metadata resource (size %"PRIu64")",
offset, metadata_resource_len);
- return WIMLIB_ERR_INVALID_DENTRY;
+ return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
}
dentry->length = le64_to_cpu(disk_dentry->length);
"%"PRIu64" ends past the end of the metadata resource "
"(size %"PRIu64")",
offset, dentry->length, metadata_resource_len);
- return WIMLIB_ERR_INVALID_DENTRY;
+ return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
}
/* Make sure the dentry length is at least as large as the number of
if (dentry->length < sizeof(struct wim_dentry_on_disk)) {
ERROR("Directory entry has invalid length of %"PRIu64" bytes",
dentry->length);
- return WIMLIB_ERR_INVALID_DENTRY;
+ return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
}
/* Allocate a `struct wim_inode' for this `struct wim_dentry'. */
if ((short_name_nbytes & 1) | (file_name_nbytes & 1))
{
ERROR("Dentry name is not valid UTF-16LE (odd number of bytes)!");
- ret = WIMLIB_ERR_INVALID_DENTRY;
+ ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
goto out_free_inode;
}
ERROR("Unexpected end of directory entry! (Expected "
"at least %"PRIu64" bytes, got %"PRIu64" bytes.)",
calculated_size, dentry->length);
- ret = WIMLIB_ERR_INVALID_DENTRY;
+ ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
goto out_free_inode;
}
* be included in the dentry->length field for some reason.
*/
if (inode->i_num_ads != 0) {
- ret = WIMLIB_ERR_INVALID_DENTRY;
+ ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
if (offset + dentry->length > metadata_resource_len ||
(ret = read_ads_entries(&metadata_resource[offset + dentry->length],
inode,
/* Reads the children of a dentry, and all their children, ..., etc. from the
* metadata resource and into the dentry tree.
*
- * @metadata_resource: An array that contains the uncompressed metadata
- * resource for the WIM file.
+ * @metadata_resource:
+ * An array that contains the uncompressed metadata resource for the WIM
+ * file.
*
- * @metadata_resource_len: The length of the uncompressed metadata resource, in
- * bytes.
+ * @metadata_resource_len:
+ * The length of the uncompressed metadata resource, in bytes.
*
- * @dentry: A pointer to a `struct wim_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.
+ * @dentry:
+ * A pointer to a `struct wim_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.
*
- * Returns zero on success; nonzero on failure.
+ * Return values:
+ * WIMLIB_ERR_SUCCESS (0)
+ * WIMLIB_ERR_INVALID_METADATA_RESOURCE
+ * WIMLIB_ERR_NOMEM
*/
int
-read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len,
- struct wim_dentry *dentry)
+read_dentry_tree(const u8 * restrict metadata_resource,
+ u64 metadata_resource_len,
+ struct wim_dentry * restrict dentry)
{
u64 cur_offset = dentry->subdir_offset;
struct wim_dentry *child;
"of \"%"TS"\" coincide with children of \"%"TS"\"",
dentry_full_path(dentry),
dentry_full_path(parent));
- return WIMLIB_ERR_INVALID_DENTRY;
+ return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
}
}
wimlib_assert(((uintptr_t)p & 7) == 0); /* 8 byte aligned */
orig_p = p;
- inode = dentry->d_inode;
+ inode = dentry->d_inode;
disk_dentry = (struct wim_dentry_on_disk*)p;
disk_dentry->attributes = cpu_to_le32(inode->i_attributes);
* Returns pointer to the byte after the last byte we wrote.
*/
u8 *
-write_dentry_tree(const struct wim_dentry *root, u8 *p)
+write_dentry_tree(const struct wim_dentry * restrict root, u8 * restrict p)
{
DEBUG("Writing dentry tree.");
wimlib_assert(dentry_is_root(root));
/* Recursively write the rest of the dentry tree. */
return write_dentry_tree_recursive(root, p);
}
+
+
+static int
+init_wimlib_dentry(struct wimlib_dir_entry *wdentry,
+ struct wim_dentry *dentry,
+ const WIMStruct *wim)
+{
+ int ret;
+ size_t dummy;
+ const struct wim_inode *inode = dentry->d_inode;
+ struct wim_lookup_table_entry *lte;
+
+#if TCHAR_IS_UTF16LE
+ wdentry->filename = dentry->file_name;
+ wdentry->dos_name = dentry->short_name;
+#else
+ if (dentry_has_long_name(dentry)) {
+ ret = utf16le_to_tstr(dentry->file_name,
+ dentry->file_name_nbytes,
+ (tchar**)&wdentry->filename,
+ &dummy);
+ if (ret)
+ return ret;
+ }
+ if (dentry_has_short_name(dentry)) {
+ ret = utf16le_to_tstr(dentry->short_name,
+ dentry->short_name_nbytes,
+ (tchar**)&wdentry->dos_name,
+ &dummy);
+ if (ret)
+ return ret;
+ }
+#endif
+ ret = calculate_dentry_full_path(dentry);
+ if (ret)
+ return ret;
+ wdentry->full_path = dentry->_full_path;
+
+ for (struct wim_dentry *d = dentry; !dentry_is_root(d); d = d->parent)
+ wdentry->depth++;
+
+ if (inode->i_security_id >= 0) {
+ const struct wim_security_data *sd = wim_const_security_data(wim);
+ wdentry->security_descriptor = sd->descriptors[inode->i_security_id];
+ wdentry->security_descriptor_size = sd->sizes[inode->i_security_id];
+ }
+ wdentry->reparse_tag = inode->i_reparse_tag;
+ wdentry->num_links = inode->i_nlink;
+ wdentry->attributes = inode->i_attributes;
+ wdentry->hard_link_group_id = inode->i_ino;
+ wdentry->creation_time = wim_timestamp_to_timespec(inode->i_creation_time);
+ wdentry->last_write_time = wim_timestamp_to_timespec(inode->i_last_write_time);
+ wdentry->last_access_time = wim_timestamp_to_timespec(inode->i_last_access_time);
+
+ lte = inode_unnamed_lte(inode, wim->lookup_table);
+ if (lte)
+ lte_to_wimlib_resource_entry(lte, &wdentry->streams[0].resource);
+
+ for (unsigned i = 0; i < inode->i_num_ads; i++) {
+ if (inode->i_ads_entries[i].stream_name == NULL)
+ 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);
+ }
+ #if TCHAR_IS_UTF16LE
+ wdentry->streams[wdentry->num_named_streams].stream_name =
+ inode->i_ads_entries[i].stream_name;
+ #else
+ size_t dummy;
+
+ ret = utf16le_to_tstr(inode->i_ads_entries[i].stream_name,
+ inode->i_ads_entries[i].stream_name_nbytes,
+ (tchar**)&wdentry->streams[
+ wdentry->num_named_streams].stream_name,
+ &dummy);
+ if (ret)
+ return ret;
+ #endif
+ }
+ return 0;
+}
+
+static void
+free_wimlib_dentry(struct wimlib_dir_entry *wdentry)
+{
+#if !TCHAR_IS_UTF16LE
+ FREE((tchar*)wdentry->filename);
+ FREE((tchar*)wdentry->dos_name);
+ for (unsigned i = 1; i <= wdentry->num_named_streams; i++)
+ FREE((tchar*)wdentry->streams[i].stream_name);
+#endif
+ FREE(wdentry);
+}
+
+struct iterate_dir_tree_ctx {
+ WIMStruct *wim;
+ int flags;
+ wimlib_iterate_dir_tree_callback_t cb;
+ void *user_ctx;
+};
+
+static int
+do_iterate_dir_tree(WIMStruct *wim,
+ struct wim_dentry *dentry, int flags,
+ wimlib_iterate_dir_tree_callback_t cb,
+ void *user_ctx);
+
+static int
+call_do_iterate_dir_tree(struct wim_dentry *dentry, void *_ctx)
+{
+ struct iterate_dir_tree_ctx *ctx = _ctx;
+ return do_iterate_dir_tree(ctx->wim, dentry, ctx->flags,
+ ctx->cb, ctx->user_ctx);
+}
+
+static int
+do_iterate_dir_tree(WIMStruct *wim,
+ struct wim_dentry *dentry, int flags,
+ wimlib_iterate_dir_tree_callback_t cb,
+ void *user_ctx)
+{
+ struct wimlib_dir_entry *wdentry;
+ int ret = WIMLIB_ERR_NOMEM;
+
+
+ wdentry = CALLOC(1, sizeof(struct wimlib_dir_entry) +
+ (1 + dentry->d_inode->i_num_ads) *
+ sizeof(struct wimlib_stream_entry));
+ if (!wdentry)
+ goto out;
+
+ ret = init_wimlib_dentry(wdentry, dentry, wim);
+ if (ret)
+ goto out_free_wimlib_dentry;
+
+ if (!(flags & WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN)) {
+ ret = (*cb)(wdentry, user_ctx);
+ if (ret)
+ goto out_free_wimlib_dentry;
+ }
+
+ if (flags & (WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE |
+ WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN))
+ {
+ struct iterate_dir_tree_ctx ctx = {
+ .wim = wim,
+ .flags = flags &= ~WIMLIB_ITERATE_DIR_TREE_FLAG_CHILDREN,
+ .cb = cb,
+ .user_ctx = user_ctx,
+ };
+ ret = for_dentry_child(dentry, call_do_iterate_dir_tree, &ctx);
+ }
+out_free_wimlib_dentry:
+ free_wimlib_dentry(wdentry);
+out:
+ return ret;
+}
+
+struct image_iterate_dir_tree_ctx {
+ const tchar *path;
+ int flags;
+ wimlib_iterate_dir_tree_callback_t cb;
+ void *user_ctx;
+};
+
+
+static int
+image_do_iterate_dir_tree(WIMStruct *wim)
+{
+ struct image_iterate_dir_tree_ctx *ctx = wim->private;
+ struct wim_dentry *dentry;
+
+ dentry = get_dentry(wim, ctx->path);
+ if (!dentry)
+ return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
+ return do_iterate_dir_tree(wim, dentry, ctx->flags, ctx->cb, ctx->user_ctx);
+}
+
+/* API function documented in wimlib.h */
+WIMLIBAPI int
+wimlib_iterate_dir_tree(WIMStruct *wim, int image, const tchar *path,
+ int flags,
+ wimlib_iterate_dir_tree_callback_t cb, void *user_ctx)
+{
+ struct image_iterate_dir_tree_ctx ctx = {
+ .path = path,
+ .flags = flags,
+ .cb = cb,
+ .user_ctx = user_ctx,
+ };
+ wim->private = &ctx;
+ return for_image(wim, image, image_do_iterate_dir_tree);
+}
+
+/* Returns %true iff the metadata of @inode and @template_inode are reasonably
+ * consistent with them being the same, unmodified file. */
+static bool
+inode_metadata_consistent(const struct wim_inode *inode,
+ const struct wim_inode *template_inode,
+ const struct wim_lookup_table *template_lookup_table)
+{
+ /* Must have exact same creation time and last write time. */
+ if (inode->i_creation_time != template_inode->i_creation_time ||
+ inode->i_last_write_time != template_inode->i_last_write_time)
+ return false;
+
+ /* Last access time may have stayed the same or increased, but certainly
+ * shouldn't have decreased. */
+ if (inode->i_last_access_time < template_inode->i_last_access_time)
+ return false;
+
+ /* Must have same number of alternate data stream entries. */
+ if (inode->i_num_ads != template_inode->i_num_ads)
+ return false;
+
+ /* If the stream entries for the inode are for some reason not resolved,
+ * then the hashes are already available and the point of this function
+ * is defeated. */
+ if (!inode->i_resolved)
+ return false;
+
+ /* Iterate through each stream and do some more checks. */
+ for (unsigned i = 0; i <= inode->i_num_ads; i++) {
+ const struct wim_lookup_table_entry *lte, *template_lte;
+
+ lte = inode_stream_lte_resolved(inode, i);
+ template_lte = inode_stream_lte(template_inode, i,
+ template_lookup_table);
+
+ /* Compare stream sizes. */
+ if (lte && template_lte) {
+ if (wim_resource_size(lte) != wim_resource_size(template_lte))
+ return false;
+
+ /* If hash happens to be available, compare with template. */
+ if (!lte->unhashed && !template_lte->unhashed &&
+ !hashes_equal(lte->hash, template_lte->hash))
+ return false;
+
+ } else if (lte && wim_resource_size(lte)) {
+ return false;
+ } else if (template_lte && wim_resource_size(template_lte)) {
+ return false;
+ }
+ }
+
+ /* All right, barring a full checksum and given that the inodes share a
+ * path and the user isn't trying to trick us, these inodes most likely
+ * refer to the same file. */
+ return true;
+}
+
+/**
+ * Given an inode @inode that has been determined to be "the same" as another
+ * inode @template_inode in either the same WIM or another WIM, retrieve some
+ * 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's) but do not
+ * necessarily have checksum information filled in.
+ */
+static int
+inode_copy_checksums(struct wim_inode *inode,
+ struct wim_inode *template_inode,
+ WIMStruct *wim,
+ WIMStruct *template_wim)
+{
+ for (unsigned i = 0; i <= inode->i_num_ads; i++) {
+ struct wim_lookup_table_entry *lte, *template_lte;
+ struct wim_lookup_table_entry *replace_lte;
+
+ lte = inode_stream_lte_resolved(inode, i);
+ template_lte = inode_stream_lte(template_inode, i,
+ template_wim->lookup_table);
+
+ /* 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 ||
+ !lte->unhashed || template_lte->unhashed)
+ continue;
+
+ wimlib_assert(lte->refcnt == inode->i_nlink);
+
+ /* If the WIM of the template image is the same as the WIM of
+ * the new image, then @template_lte can be used directly.
+ *
+ * Otherwise, look for a stream with the same hash in the WIM of
+ * the new image. If found, use it; otherwise re-use the entry
+ * being discarded, filling in the hash. */
+
+ if (wim == template_wim)
+ replace_lte = template_lte;
+ else
+ replace_lte = lookup_resource(wim->lookup_table,
+ template_lte->hash);
+
+ list_del(<e->unhashed_list);
+ if (replace_lte) {
+ free_lookup_table_entry(lte);
+ } else {
+ copy_hash(lte->hash, template_lte->hash);
+ lte->unhashed = 0;
+ lookup_table_insert(wim->lookup_table, lte);
+ lte->refcnt = 0;
+ replace_lte = lte;
+ }
+
+ if (i == 0)
+ inode->i_lte = replace_lte;
+ else
+ inode->i_ads_entries[i - 1].lte = replace_lte;
+
+ replace_lte->refcnt += inode->i_nlink;
+ }
+ return 0;
+}
+
+struct reference_template_args {
+ WIMStruct *wim;
+ WIMStruct *template_wim;
+};
+
+static int
+dentry_reference_template(struct wim_dentry *dentry, void *_args)
+{
+ int ret;
+ struct wim_dentry *template_dentry;
+ struct wim_inode *inode, *template_inode;
+ struct reference_template_args *args = _args;
+ WIMStruct *wim = args->wim;
+ WIMStruct *template_wim = args->template_wim;
+
+ if (dentry->d_inode->i_visited)
+ return 0;
+
+ ret = calculate_dentry_full_path(dentry);
+ if (ret)
+ return ret;
+
+ template_dentry = get_dentry(template_wim, dentry->_full_path);
+ if (!template_dentry) {
+ DEBUG("\"%"TS"\": newly added file", dentry->_full_path);
+ return 0;
+ }
+
+ inode = dentry->d_inode;
+ template_inode = template_dentry->d_inode;
+
+ if (inode_metadata_consistent(inode, template_inode,
+ template_wim->lookup_table)) {
+ /*DEBUG("\"%"TS"\": No change detected", dentry->_full_path);*/
+ ret = inode_copy_checksums(inode, template_inode,
+ wim, template_wim);
+ inode->i_visited = 1;
+ } else {
+ DEBUG("\"%"TS"\": change detected!", dentry->_full_path);
+ ret = 0;
+ }
+ return ret;
+}
+
+/* API function documented in wimlib.h */
+WIMLIBAPI int
+wimlib_reference_template_image(WIMStruct *wim, int new_image,
+ WIMStruct *template_wim, int template_image,
+ int flags, wimlib_progress_func_t progress_func)
+{
+ int ret;
+ struct wim_image_metadata *new_imd;
+
+ if (wim == NULL || template_wim == NULL)
+ return WIMLIB_ERR_INVALID_PARAM;
+
+ if (wim == template_wim && new_image == template_image)
+ return WIMLIB_ERR_INVALID_PARAM;
+
+ if (new_image < 1 || new_image > wim->hdr.image_count)
+ return WIMLIB_ERR_INVALID_IMAGE;
+
+ if (!wim_has_metadata(wim))
+ return WIMLIB_ERR_METADATA_NOT_FOUND;
+
+ new_imd = wim->image_metadata[new_image - 1];
+ if (!new_imd->modified)
+ return WIMLIB_ERR_INVALID_PARAM;
+
+ ret = select_wim_image(template_wim, template_image);
+ if (ret)
+ return ret;
+
+ struct reference_template_args args = {
+ .wim = wim,
+ .template_wim = template_wim,
+ };
+
+ ret = for_dentry_in_tree(new_imd->root_dentry,
+ dentry_reference_template, &args);
+ dentry_tree_clear_inode_visited(new_imd->root_dentry);
+ return ret;
+}