+/* Transfers file attributes and possibly a security descriptor from a WIM inode
+ * to a NTFS inode.
+ *
+ * @ni: The NTFS inode to apply the metadata to.
+ * @dir_ni: The NTFS inode for a directory containing @ni.
+ * @dentry: The WIM dentry whose inode contains the metadata to apply.
+ * @w: The WIMStruct for the WIM, through which the table of security
+ * descriptors can be accessed.
+ *
+ * Returns 0 on success, nonzero on failure.
+ */
+static int
+apply_file_attributes_and_security_data(ntfs_inode *ni,
+ ntfs_inode *dir_ni,
+ const struct dentry *dentry,
+ const WIMStruct *w)
+{
+ int ret;
+ struct SECURITY_CONTEXT ctx;
+ u32 attributes_le32;
+ const struct inode *inode;
+
+ inode = dentry->d_inode;
+
+ DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
+ dentry->full_path_utf8, inode->attributes);
+
+ attributes_le32 = cpu_to_le32(inode->attributes);
+ memset(&ctx, 0, sizeof(ctx));
+ ctx.vol = ni->vol;
+ ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ATTRIB,
+ ni, dir_ni,
+ (const char*)&attributes_le32,
+ sizeof(u32), 0);
+ if (ret != 0) {
+ ERROR("Failed to set NTFS file attributes on `%s'",
+ dentry->full_path_utf8);
+ return WIMLIB_ERR_NTFS_3G;
+ }
+ if (inode->security_id != -1) {
+ const char *desc;
+ const struct wim_security_data *sd;
+
+ sd = wim_const_security_data(w);
+ wimlib_assert(inode->security_id < sd->num_entries);
+ desc = (const char *)sd->descriptors[inode->security_id];
+ DEBUG("Applying security descriptor %d to `%s'",
+ inode->security_id, dentry->full_path_utf8);
+
+ ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ACL,
+ ni, dir_ni, desc,
+ sd->sizes[inode->security_id], 0);
+
+ if (ret != 0) {
+ ERROR_WITH_ERRNO("Failed to set security data on `%s'",
+ dentry->full_path_utf8);
+ return WIMLIB_ERR_NTFS_3G;
+ }
+ }
+ return 0;
+}
+
+/*
+ * Transfers the reparse data from a WIM inode (which must represent a reparse
+ * point) to a NTFS inode.
+ */
+static int apply_reparse_data(ntfs_inode *ni, const struct dentry *dentry,
+ union wimlib_progress_info *progress_info)
+{
+ struct lookup_table_entry *lte;
+ int ret = 0;
+
+ lte = inode_unnamed_lte_resolved(dentry->d_inode);
+
+ DEBUG("Applying reparse data to `%s'", dentry->full_path_utf8);
+
+ if (!lte) {
+ ERROR("Could not find reparse data for `%s'",
+ dentry->full_path_utf8);
+ return WIMLIB_ERR_INVALID_DENTRY;
+ }
+
+ if (wim_resource_size(lte) >= 0xffff) {
+ ERROR("Reparse data of `%s' is too long (%"PRIu64" bytes)",
+ dentry->full_path_utf8, wim_resource_size(lte));
+ return WIMLIB_ERR_INVALID_DENTRY;
+ }
+
+ u8 reparse_data_buf[8 + wim_resource_size(lte)];
+ u8 *p = reparse_data_buf;
+ p = put_u32(p, dentry->d_inode->reparse_tag); /* ReparseTag */
+ p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
+ p = put_u16(p, 0); /* Reserved */
+
+ ret = read_full_wim_resource(lte, p, 0);
+ if (ret != 0)
+ return ret;
+
+ ret = ntfs_set_ntfs_reparse_data(ni, (char*)reparse_data_buf,
+ wim_resource_size(lte) + 8, 0);
+ if (ret != 0) {
+ ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
+ dentry->full_path_utf8);
+ return WIMLIB_ERR_NTFS_3G;
+ }
+ progress_info->extract.completed_bytes += wim_resource_size(lte);
+ return 0;
+}
+
+static int do_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
+ struct apply_args *args);
+
+/*
+ * If @dentry is part of a hard link group, search for hard-linked dentries in
+ * the same directory that have a nonempty DOS (short) filename. There should
+ * be exactly 0 or 1 such dentries. If there is 1, extract that dentry first,
+ * so that the DOS name is correctly associated with the corresponding long name
+ * in the Win32 namespace, and not any of the additional names in the POSIX
+ * namespace created from hard links.
+ */
+static int preapply_dentry_with_dos_name(struct dentry *dentry,
+ ntfs_inode **dir_ni_p,
+ struct apply_args *args)
+{
+ struct dentry *other;
+ struct dentry *dentry_with_dos_name;
+
+ dentry_with_dos_name = NULL;
+ inode_for_each_dentry(other, dentry->d_inode) {
+ if (other != dentry && (dentry->parent == other->parent)
+ && other->short_name_len)
+ {
+ if (dentry_with_dos_name) {
+ ERROR("Found multiple DOS names for file `%s' "
+ "in the same directory",
+ dentry_with_dos_name->full_path_utf8);
+ return WIMLIB_ERR_INVALID_DENTRY;
+ }
+ dentry_with_dos_name = other;
+ }
+ }
+ /* If there's a dentry with a DOS name, extract it first */
+ if (dentry_with_dos_name && !dentry_with_dos_name->is_extracted) {
+ char *p;
+ const char *dir_name;
+ char orig;
+ int ret;
+ ntfs_volume *vol = (*dir_ni_p)->vol;
+
+ DEBUG("pre-applying DOS name `%s'",
+ dentry_with_dos_name->full_path_utf8);
+ ret = do_apply_dentry_ntfs(dentry_with_dos_name,
+ *dir_ni_p, args);
+ if (ret != 0)
+ return ret;
+
+ *dir_ni_p = dentry_open_parent_ni(dentry, vol);
+ if (!*dir_ni_p)
+ return WIMLIB_ERR_NTFS_3G;
+ }
+ return 0;
+}
+
+/*