+/* Set the short name on the open file @h which has been created at the location
+ * indicated by @dentry.
+ *
+ * Note that this may add, change, or remove the short name.
+ *
+ * @h must be opened with DELETE access.
+ *
+ * Returns 0 or WIMLIB_ERR_SET_SHORT_NAME. The latter only happens in
+ * STRICT_SHORT_NAMES mode.
+ */
+static int
+set_short_name(HANDLE h, const struct wim_dentry *dentry,
+ struct win32_apply_ctx *ctx)
+{
+ size_t bufsize = offsetof(FILE_NAME_INFORMATION, FileName) +
+ dentry->short_name_nbytes;
+ u8 buf[bufsize] _aligned_attribute(8);
+ FILE_NAME_INFORMATION *info = (FILE_NAME_INFORMATION *)buf;
+ NTSTATUS status;
+
+ info->FileNameLength = dentry->short_name_nbytes;
+ memcpy(info->FileName, dentry->short_name, dentry->short_name_nbytes);
+
+ status = (*func_NtSetInformationFile)(h, &ctx->iosb, info, bufsize,
+ FileShortNameInformation);
+ if (NT_SUCCESS(status))
+ return 0;
+
+ /* By default, failure to set short names is not an error (since short
+ * names aren't too important anymore...). */
+ if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES))
+ return 0;
+
+ if (status == STATUS_SHORT_NAMES_NOT_ENABLED_ON_VOLUME) {
+ if (dentry->short_name_nbytes == 0)
+ return 0;
+ ERROR("Can't extract short name when short "
+ "names are not enabled on the volume!");
+ } else {
+ ERROR("Can't set short name on \"%ls\" (status=0x%08"PRIx32")",
+ current_path(ctx), (u32)status);
+ }
+ return WIMLIB_ERR_SET_SHORT_NAME;
+}
+
+/*
+ * A wrapper around NtCreateFile() to make it slightly more usable...
+ * This uses the path currently constructed in ctx->pathbuf.
+ *
+ * Also, we always specify FILE_OPEN_FOR_BACKUP_INTENT and
+ * FILE_OPEN_REPARSE_POINT.
+ */
+static NTSTATUS
+do_create_file(PHANDLE FileHandle,
+ ACCESS_MASK DesiredAccess,
+ PLARGE_INTEGER AllocationSize,
+ ULONG FileAttributes,
+ ULONG CreateDisposition,
+ ULONG CreateOptions,
+ struct win32_apply_ctx *ctx)
+{
+ return (*func_NtCreateFile)(FileHandle,
+ DesiredAccess,
+ &ctx->attr,
+ &ctx->iosb,
+ AllocationSize,
+ FileAttributes,
+ FILE_SHARE_VALID_FLAGS,
+ CreateDisposition,
+ CreateOptions |
+ FILE_OPEN_FOR_BACKUP_INTENT |
+ FILE_OPEN_REPARSE_POINT,
+ NULL,
+ 0);
+}
+
+/* Like do_create_file(), but builds the extraction path of the @dentry first.
+ */
+static NTSTATUS
+create_file(PHANDLE FileHandle,
+ ACCESS_MASK DesiredAccess,
+ PLARGE_INTEGER AllocationSize,
+ ULONG FileAttributes,
+ ULONG CreateDisposition,
+ ULONG CreateOptions,
+ const struct wim_dentry *dentry,
+ struct win32_apply_ctx *ctx)
+{
+ build_extraction_path(dentry, ctx);
+ return do_create_file(FileHandle,
+ DesiredAccess,
+ AllocationSize,
+ FileAttributes,
+ CreateDisposition,
+ CreateOptions,
+ ctx);
+}
+
+/* Create empty named data streams.
+ *
+ * Since these won't have 'struct wim_lookup_table_entry's, they won't show up
+ * in the call to extract_stream_list(). Hence the need for the special case.
+ */
+static int
+create_any_empty_ads(const struct wim_dentry *dentry,
+ struct win32_apply_ctx *ctx)
+{
+ const struct wim_inode *inode = dentry->d_inode;
+ LARGE_INTEGER allocation_size;
+ bool path_modified = false;
+ int ret = 0;
+
+ if (!ctx->common.supported_features.named_data_streams)
+ return 0;
+
+ for (u16 i = 0; i < inode->i_num_ads; i++) {
+ const struct wim_ads_entry *entry;
+ NTSTATUS status;
+ HANDLE h;
+
+ entry = &inode->i_ads_entries[i];
+
+ /* Not named? */
+ if (!entry->stream_name_nbytes)
+ continue;
+
+ /* Not empty? */
+ if (entry->lte)
+ continue;
+
+ /* Probably setting the allocation size to 0 has no effect, but
+ * we might as well try. */
+ allocation_size.QuadPart = 0;
+
+ build_extraction_path_with_ads(dentry, ctx,
+ entry->stream_name,
+ entry->stream_name_nbytes /
+ sizeof(wchar_t));
+ path_modified = true;
+ status = do_create_file(&h, FILE_WRITE_DATA, &allocation_size,
+ 0, FILE_SUPERSEDE, 0, ctx);
+ if (!NT_SUCCESS(status)) {
+ set_errno_from_nt_status(status);
+ ERROR_WITH_ERRNO("Can't create \"%ls\" "
+ "(status=0x%08"PRIx32")",
+ current_path(ctx), (u32)status);
+ ret = WIMLIB_ERR_OPEN;
+ break;
+ }
+ (*func_NtClose)(h);
+ }
+ /* Restore the path to the dentry itself */
+ if (path_modified)
+ build_extraction_path(dentry, ctx);
+ return ret;