+static int
+delete_file_or_stream(struct win32_apply_ctx *ctx)
+{
+ NTSTATUS status;
+ HANDLE h;
+ FILE_DISPOSITION_INFORMATION disposition_info;
+ FILE_BASIC_INFORMATION basic_info;
+ bool retried = false;
+
+ status = do_create_file(&h,
+ DELETE,
+ NULL,
+ 0,
+ FILE_OPEN,
+ FILE_NON_DIRECTORY_FILE,
+ ctx);
+ if (unlikely(!NT_SUCCESS(status))) {
+ winnt_error(status, L"Can't open \"%ls\" for deletion",
+ current_path(ctx));
+ return WIMLIB_ERR_OPEN;
+ }
+
+retry:
+ disposition_info.DoDeleteFile = TRUE;
+ status = (*func_NtSetInformationFile)(h, &ctx->iosb,
+ &disposition_info,
+ sizeof(disposition_info),
+ FileDispositionInformation);
+ (*func_NtClose)(h);
+ if (likely(NT_SUCCESS(status)))
+ return 0;
+
+ if (status == STATUS_CANNOT_DELETE && !retried) {
+ /* Clear file attributes and try again. This is necessary for
+ * FILE_ATTRIBUTE_READONLY files. */
+ status = do_create_file(&h,
+ FILE_WRITE_ATTRIBUTES | DELETE,
+ NULL,
+ 0,
+ FILE_OPEN,
+ FILE_NON_DIRECTORY_FILE,
+ ctx);
+ if (!NT_SUCCESS(status)) {
+ winnt_error(status,
+ L"Can't open \"%ls\" to reset attributes",
+ current_path(ctx));
+ return WIMLIB_ERR_OPEN;
+ }
+ memset(&basic_info, 0, sizeof(basic_info));
+ basic_info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
+ status = (*func_NtSetInformationFile)(h, &ctx->iosb,
+ &basic_info,
+ sizeof(basic_info),
+ FileBasicInformation);
+ if (!NT_SUCCESS(status)) {
+ winnt_error(status,
+ L"Can't reset file attributes on \"%ls\"",
+ current_path(ctx));
+ (*func_NtClose)(h);
+ return WIMLIB_ERR_SET_ATTRIBUTES;
+ }
+ retried = true;
+ goto retry;
+ }
+ winnt_error(status, L"Can't delete \"%ls\"", current_path(ctx));
+ return WIMLIB_ERR_OPEN;
+}
+
+/*
+ * Create a nondirectory file or named data stream at the current path,
+ * superseding any that already exists at that path. If successful, return an
+ * open handle to the file or named data stream.
+ */
+static int
+supersede_file_or_stream(struct win32_apply_ctx *ctx, HANDLE *h_ret)
+{
+ NTSTATUS status;
+ bool retried = false;
+
+ /* FILE_ATTRIBUTE_SYSTEM is needed to ensure that
+ * FILE_ATTRIBUTE_ENCRYPTED doesn't get set before we want it to be. */
+retry:
+ status = do_create_file(h_ret,
+ GENERIC_READ | GENERIC_WRITE | DELETE,
+ NULL,
+ FILE_ATTRIBUTE_SYSTEM,
+ FILE_CREATE,
+ FILE_NON_DIRECTORY_FILE,
+ ctx);
+ if (likely(NT_SUCCESS(status)))
+ return 0;
+
+ /* STATUS_OBJECT_NAME_COLLISION means that the file or stream already
+ * exists. Delete the existing file or stream, then try again.
+ *
+ * Note: we don't use FILE_OVERWRITE_IF or FILE_SUPERSEDE because of
+ * problems with certain file attributes, especially
+ * FILE_ATTRIBUTE_ENCRYPTED. FILE_SUPERSEDE is also broken in the
+ * Windows PE ramdisk. */
+ if (status == STATUS_OBJECT_NAME_COLLISION && !retried) {
+ int ret = delete_file_or_stream(ctx);
+ if (ret)
+ return ret;
+ retried = true;
+ goto retry;
+ }
+ winnt_error(status, L"Can't create \"%ls\"", current_path(ctx));
+ return WIMLIB_ERR_OPEN;
+}
+