+static int
+apply_file_attributes_and_security_data(ntfs_inode *ni,
+ const struct dentry *dentry,
+ const WIMStruct *w)
+{
+ DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
+ dentry->full_path_utf8, dentry->attributes);
+ if (!ntfs_inode_set_attributes(ni, dentry->attributes)) {
+ ERROR("Failed to set NTFS file attributes on `%s'",
+ dentry->full_path_utf8);
+ return WIMLIB_ERR_NTFS_3G;
+ }
+
+ if (dentry->security_id != -1) {
+ const struct wim_security_data *sd;
+
+ sd = wim_const_security_data(w);
+ wimlib_assert(dentry->security_id < sd->num_entries);
+ DEBUG("Applying security descriptor %d to `%s'",
+ dentry->security_id, dentry->full_path_utf8);
+ u32 selection = OWNER_SECURITY_INFORMATION |
+ GROUP_SECURITY_INFORMATION |
+ DACL_SECURITY_INFORMATION |
+ SACL_SECURITY_INFORMATION;
+
+ if (!ntfs_inode_set_security(ni, selection,
+ (const char*)sd->descriptors[dentry->security_id]))
+ {
+ ERROR_WITH_ERRNO("Failed to set security data on `%s'",
+ dentry->full_path_utf8);
+ return WIMLIB_ERR_NTFS_3G;
+ }
+ }
+ return 0;
+}
+
+static int apply_reparse_data(ntfs_inode *ni, const struct dentry *dentry,
+ const WIMStruct *w)
+{
+ struct lookup_table_entry *lte;
+ int ret = 0;
+
+ wimlib_assert(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
+
+ lte = dentry_unnamed_lte(dentry, w->lookup_table);
+
+ 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 (%lu 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->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);
+ 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;
+ }
+ return 0;
+}
+
+static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
+ WIMStruct *w);
+
+/*
+ * 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,
+ WIMStruct *w)
+{
+ int ret;
+ struct dentry *other;
+ struct dentry *dentry_with_dos_name;
+
+ if (dentry->link_group_list.next == &dentry->link_group_list)
+ return 0;
+
+ dentry_with_dos_name = NULL;
+ list_for_each_entry(other, &dentry->link_group_list,
+ link_group_list)
+ {
+ if (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->extracted_file) {
+ char *p;
+ const char *dir_name;
+ char orig;
+ ntfs_volume *vol = (*dir_ni_p)->vol;
+
+ DEBUG("pre-applying DOS name `%s'",
+ dentry_with_dos_name->full_path_utf8);
+ ret = do_wim_apply_dentry_ntfs(dentry_with_dos_name,
+ *dir_ni_p, w);
+ if (ret != 0)
+ return ret;
+ p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
+ do {
+ p--;
+ } while (*p != '/');
+
+ orig = *p;
+ *p = '\0';
+ dir_name = dentry->full_path_utf8;
+
+ *dir_ni_p = ntfs_pathname_to_inode(vol, NULL, dir_name);
+ *p = orig;
+ if (!*dir_ni_p) {
+ ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
+ dir_name);
+ return WIMLIB_ERR_NTFS_3G;
+ }
+ }
+ return 0;
+}
+