]> wimlib.net Git - wimlib/blobdiff - src/win32_common.c
win32_common: add winnt_fsctl() helper method
[wimlib] / src / win32_common.c
index 3b0d9e842978f04ed354670e31760a0e585c7a85..628f1f0bb7ebd8284b81876970c2c258f2b8580e 100644 (file)
@@ -3,7 +3,7 @@
  */
 
 /*
- * Copyright (C) 2013, 2014, 2015 Eric Biggers
+ * Copyright (C) 2013-2016 Eric Biggers
  *
  * This file is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the Free
 
 #include "wimlib/win32_common.h"
 
+#include "wimlib/assert.h"
 #include "wimlib/error.h"
 #include "wimlib/util.h"
+#include "wimlib/win32_vss.h"
 
 static bool
 win32_modify_privilege(const wchar_t *privilege, bool enable)
@@ -169,16 +171,20 @@ NTSTATUS (WINAPI *func_NtSetSecurityObject)(HANDLE Handle,
                                            SECURITY_INFORMATION SecurityInformation,
                                            PSECURITY_DESCRIPTOR SecurityDescriptor);
 
-NTSTATUS (WINAPI *func_NtFsControlFile) (HANDLE FileHandle,
-                                        HANDLE Event,
-                                        PIO_APC_ROUTINE ApcRoutine,
-                                        PVOID ApcContext,
-                                        PIO_STATUS_BLOCK IoStatusBlock,
-                                        ULONG FsControlCode,
-                                        PVOID InputBuffer,
-                                        ULONG InputBufferLength,
-                                        PVOID OutputBuffer,
-                                        ULONG OutputBufferLength);
+static NTSTATUS (WINAPI *func_NtFsControlFile) (HANDLE FileHandle,
+                                               HANDLE Event,
+                                               PIO_APC_ROUTINE ApcRoutine,
+                                               PVOID ApcContext,
+                                               PIO_STATUS_BLOCK IoStatusBlock,
+                                               ULONG FsControlCode,
+                                               PVOID InputBuffer,
+                                               ULONG InputBufferLength,
+                                               PVOID OutputBuffer,
+                                               ULONG OutputBufferLength);
+
+static NTSTATUS (WINAPI *func_NtWaitForSingleObject) (HANDLE Handle,
+                                                     BOOLEAN Alertable,
+                                                     PLARGE_INTEGER Timeout);
 
 NTSTATUS (WINAPI *func_NtClose) (HANDLE Handle);
 
@@ -232,6 +238,7 @@ struct dll_spec ntdll_spec = {
                DLL_SYM(NtSetInformationFile, true),
                DLL_SYM(NtSetSecurityObject, true),
                DLL_SYM(NtFsControlFile, true),
+               DLL_SYM(NtWaitForSingleObject, true),
                DLL_SYM(NtClose, true),
                DLL_SYM(RtlNtStatusToDosError, true),
                DLL_SYM(RtlCreateSystemVolumeInformationFolder, false),
@@ -316,6 +323,8 @@ out_drop_privs:
 void
 win32_global_cleanup(void)
 {
+       vss_global_cleanup();
+
        if (acquired_privileges)
                win32_release_capture_and_apply_privileges();
 
@@ -407,7 +416,10 @@ win32_try_to_attach_wof(const wchar_t *drive)
        if (func_FilterAttach) {
                HRESULT res;
 
-               res = (*func_FilterAttach)(L"WoF", drive, NULL, 0, NULL);
+               res = (*func_FilterAttach)(L"wof", drive, NULL, 0, NULL);
+
+               if (res != S_OK)
+                       res = (*func_FilterAttach)(L"wofadk", drive, NULL, 0, NULL);
 
                if (res == S_OK)
                        retval = true;
@@ -446,9 +458,11 @@ retry:
        if (n >= buflen)
                goto realloc;
 
-       ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
-                           NULL,
-                           is_ntstatus ? (*func_RtlNtStatusToDosError)(code) : code,
+       ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
+                               FORMAT_MESSAGE_IGNORE_INSERTS |
+                               (is_ntstatus ? FORMAT_MESSAGE_FROM_HMODULE : 0),
+                           (is_ntstatus ? ntdll_spec.handle : NULL),
+                           code,
                            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                            &buf[n],
                            buflen - n,
@@ -523,4 +537,40 @@ winnt_error(NTSTATUS status, const wchar_t *format, ...)
        va_end(va);
 }
 
+/*
+ * Synchronously execute a filesystem control method.  This is a wrapper around
+ * NtFsControlFile() that handles STATUS_PENDING.  Note that SYNCHRONIZE
+ * permission is, in general, required on the handle.
+ */
+NTSTATUS
+winnt_fsctl(HANDLE h, u32 code, const void *in, u32 in_size,
+           void *out, u32 out_size_avail, u32 *actual_out_size_ret)
+{
+       IO_STATUS_BLOCK iosb;
+       NTSTATUS status;
+
+       status = (*func_NtFsControlFile)(h, NULL, NULL, NULL, &iosb, code,
+                                        (void *)in, in_size,
+                                        out, out_size_avail);
+       if (status == STATUS_PENDING) {
+               /* Beware: this case is often encountered with remote
+                * filesystems, but rarely with local filesystems.  */
+
+               status = (*func_NtWaitForSingleObject)(h, FALSE, NULL);
+               if (NT_SUCCESS(status)) {
+                       status = iosb.Status;
+               } else {
+                       /* We shouldn't be issuing ioctls on a handle to which
+                        * we don't have SYNCHRONIZE access.  Otherwise we have
+                        * no way to wait for them to complete.  */
+                       wimlib_assert(status != STATUS_ACCESS_DENIED);
+               }
+       }
+
+       if (NT_SUCCESS(status) && actual_out_size_ret != NULL)
+               *actual_out_size_ret = (u32)iosb.Information;
+
+       return status;
+}
+
 #endif /* __WIN32__ */