https://git.reactos.org/?p=reactos.git;a=commitdiff;h=0f8dc6b2dfb926c971dc8796c96db05158ead940

commit 0f8dc6b2dfb926c971dc8796c96db05158ead940
Author:     Hermès Bélusca-Maïto <hermes.belusca-ma...@reactos.org>
AuthorDate: Fri Jul 12 18:39:39 2024 +0200
Commit:     Hermès Bélusca-Maïto <hermes.belusca-ma...@reactos.org>
CommitDate: Mon Aug 26 16:42:46 2024 +0200

    [SETUPLIB] Add some device utility functions; to be used later (#7258)
---
 base/setup/lib/CMakeLists.txt   |  1 +
 base/setup/lib/utils/devutils.c | 87 +++++++++++++++++++++++++++++++++++++++++
 base/setup/lib/utils/devutils.h | 24 ++++++++++++
 3 files changed, 112 insertions(+)

diff --git a/base/setup/lib/CMakeLists.txt b/base/setup/lib/CMakeLists.txt
index cc79ec83ef8..57eae6b3a6e 100644
--- a/base/setup/lib/CMakeLists.txt
+++ b/base/setup/lib/CMakeLists.txt
@@ -11,6 +11,7 @@ list(APPEND SOURCE
     spapisup/infsupp.c
     utils/arcname.c
     utils/bldrsup.c
+    utils/devutils.c
     utils/filesup.c
     utils/fsrec.c
     utils/genlist.c
diff --git a/base/setup/lib/utils/devutils.c b/base/setup/lib/utils/devutils.c
new file mode 100644
index 00000000000..f8bc8a83daf
--- /dev/null
+++ b/base/setup/lib/utils/devutils.c
@@ -0,0 +1,87 @@
+/*
+ * PROJECT:     ReactOS Setup Library
+ * LICENSE:     GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
+ * PURPOSE:     Device utility functions
+ * COPYRIGHT:   Copyright 2024 Hermès Bélusca-Maïto 
<hermes.belusca-ma...@reactos.org>
+ */
+
+#include "precomp.h"
+#include "devutils.h"
+
+/* FUNCTIONS *****************************************************************/
+
+/**
+ * @brief
+ * Open an existing device given by its NT-style path, which is assumed to be
+ * for a disk device or a partition. The open is for synchronous I/O access.
+ *
+ * @param[in]   DevicePath
+ * Supplies the NT-style path to the device to open.
+ *
+ * @param[out]  DeviceHandle
+ * If successful, receives the NT handle of the opened device.
+ * Once the handle is no longer in use, call NtClose() to close it.
+ *
+ * @param[in]   DesiredAccess
+ * An ACCESS_MASK value combination that determines the requested access
+ * to the device. Because the open is for synchronous access, SYNCHRONIZE
+ * is automatically added to the access mask.
+ *
+ * @param[in]   ShareAccess
+ * Specifies the type of share access for the device.
+ *
+ * @return  An NTSTATUS code indicating success or failure.
+ **/
+NTSTATUS
+pOpenDeviceEx(
+    _In_ PCWSTR DevicePath,
+    _Out_ PHANDLE DeviceHandle,
+    _In_ ACCESS_MASK DesiredAccess,
+    _In_ ULONG ShareAccess)
+{
+    UNICODE_STRING Name;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    IO_STATUS_BLOCK IoStatusBlock;
+
+    RtlInitUnicodeString(&Name, DevicePath);
+    InitializeObjectAttributes(&ObjectAttributes,
+                               &Name,
+                               OBJ_CASE_INSENSITIVE,
+                               NULL,
+                               NULL);
+    return NtOpenFile(DeviceHandle,
+                      DesiredAccess | SYNCHRONIZE,
+                      &ObjectAttributes,
+                      &IoStatusBlock,
+                      ShareAccess,
+                      /* FILE_NON_DIRECTORY_FILE | */
+                      FILE_SYNCHRONOUS_IO_NONALERT);
+}
+
+/**
+ * @brief
+ * Open an existing device given by its NT-style path, which is assumed to be
+ * for a disk device or a partition. The open is share read/write/delete, for
+ * synchronous I/O and read access.
+ *
+ * @param[in]   DevicePath
+ * @param[out]  DeviceHandle
+ * See the DevicePath and DeviceHandle parameters of pOpenDeviceEx().
+ *
+ * @return  An NTSTATUS code indicating success or failure.
+ *
+ * @see pOpenDeviceEx()
+ **/
+NTSTATUS
+pOpenDevice(
+    _In_ PCWSTR DevicePath,
+    _Out_ PHANDLE DeviceHandle)
+{
+    return pOpenDeviceEx(DevicePath,
+                         DeviceHandle,
+                         FILE_READ_DATA | FILE_READ_ATTRIBUTES,
+                         FILE_SHARE_VALID_FLAGS // FILE_SHARE_READ,WRITE,DELETE
+                         );
+}
+
+/* EOF */
diff --git a/base/setup/lib/utils/devutils.h b/base/setup/lib/utils/devutils.h
new file mode 100644
index 00000000000..63bbb624d66
--- /dev/null
+++ b/base/setup/lib/utils/devutils.h
@@ -0,0 +1,24 @@
+/*
+ * PROJECT:     ReactOS Setup Library
+ * LICENSE:     GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
+ * PURPOSE:     Device utility functions
+ * COPYRIGHT:   Copyright 2024 Hermès Bélusca-Maïto 
<hermes.belusca-ma...@reactos.org>
+ */
+
+#pragma once
+
+/* FUNCTIONS *****************************************************************/
+
+NTSTATUS
+pOpenDeviceEx(
+    _In_ PCWSTR DevicePath,
+    _Out_ PHANDLE DeviceHandle,
+    _In_ ACCESS_MASK DesiredAccess,
+    _In_ ULONG ShareAccess);
+
+NTSTATUS
+pOpenDevice(
+    _In_ PCWSTR DevicePath,
+    _Out_ PHANDLE DeviceHandle);
+
+/* EOF */

Reply via email to