ReactOS  0.4.14-dev-845-g8381e29
mountmgr_local.cpp
Go to the documentation of this file.
1 #include "shellext.h"
2 #ifndef __REACTOS__
3 #include "mountmgr.h"
4 #else
5 #include "mountmgr_local.h"
6 #endif
7 #include <mountmgr.h>
8 
9 using namespace std;
10 
16 
18  InitializeObjectAttributes(&attr, &us, 0, nullptr, nullptr);
19 
22 
23  if (!NT_SUCCESS(Status))
24  throw ntstatus_error(Status);
25 }
26 
28  NtClose(h);
29 }
30 
31 void mountmgr::create_point(const wstring_view& symlink, const wstring_view& device) const {
34 
35  vector<uint8_t> buf(sizeof(MOUNTMGR_CREATE_POINT_INPUT) + ((symlink.length() + device.length()) * sizeof(WCHAR)));
36  auto mcpi = reinterpret_cast<MOUNTMGR_CREATE_POINT_INPUT*>(buf.data());
37 
38  mcpi->SymbolicLinkNameOffset = sizeof(MOUNTMGR_CREATE_POINT_INPUT);
39  mcpi->SymbolicLinkNameLength = (USHORT)(symlink.length() * sizeof(WCHAR));
40  mcpi->DeviceNameOffset = (USHORT)(mcpi->SymbolicLinkNameOffset + mcpi->SymbolicLinkNameLength);
41  mcpi->DeviceNameLength = (USHORT)(device.length() * sizeof(WCHAR));
42 
43  memcpy((uint8_t*)mcpi + mcpi->SymbolicLinkNameOffset, symlink.data(), symlink.length() * sizeof(WCHAR));
44  memcpy((uint8_t*)mcpi + mcpi->DeviceNameOffset, device.data(), device.length() * sizeof(WCHAR));
45 
46  Status = NtDeviceIoControlFile(h, nullptr, nullptr, nullptr, &iosb, IOCTL_MOUNTMGR_CREATE_POINT,
47  buf.data(), (ULONG)buf.size(), nullptr, 0);
48 
49  if (!NT_SUCCESS(Status))
50  throw ntstatus_error(Status);
51 }
52 
53 void mountmgr::delete_points(const wstring_view& symlink, const wstring_view& unique_id, const wstring_view& device_name) const {
56 
57  vector<uint8_t> buf(sizeof(MOUNTMGR_MOUNT_POINT) + ((symlink.length() + unique_id.length() + device_name.length()) * sizeof(WCHAR)));
58  auto mmp = reinterpret_cast<MOUNTMGR_MOUNT_POINT*>(buf.data());
59 
60  memset(mmp, 0, sizeof(MOUNTMGR_MOUNT_POINT));
61 
62  if (symlink.length() > 0) {
63  mmp->SymbolicLinkNameOffset = sizeof(MOUNTMGR_MOUNT_POINT);
64  mmp->SymbolicLinkNameLength = (USHORT)(symlink.length() * sizeof(WCHAR));
65  memcpy((uint8_t*)mmp + mmp->SymbolicLinkNameOffset, symlink.data(), symlink.length() * sizeof(WCHAR));
66  }
67 
68  if (unique_id.length() > 0) {
69  if (mmp->SymbolicLinkNameLength == 0)
70  mmp->UniqueIdOffset = sizeof(MOUNTMGR_MOUNT_POINT);
71  else
72  mmp->UniqueIdOffset = mmp->SymbolicLinkNameOffset + mmp->SymbolicLinkNameLength;
73 
74  mmp->UniqueIdLength = (USHORT)(unique_id.length() * sizeof(WCHAR));
75  memcpy((uint8_t*)mmp + mmp->UniqueIdOffset, unique_id.data(), unique_id.length() * sizeof(WCHAR));
76  }
77 
78  if (device_name.length() > 0) {
79  if (mmp->SymbolicLinkNameLength == 0 && mmp->UniqueIdOffset == 0)
80  mmp->DeviceNameOffset = sizeof(MOUNTMGR_MOUNT_POINT);
81  else if (mmp->SymbolicLinkNameLength != 0)
82  mmp->DeviceNameOffset = mmp->SymbolicLinkNameOffset + mmp->SymbolicLinkNameLength;
83  else
84  mmp->DeviceNameOffset = mmp->UniqueIdOffset + mmp->UniqueIdLength;
85 
86  mmp->DeviceNameLength = (USHORT)(device_name.length() * sizeof(WCHAR));
87  memcpy((uint8_t*)mmp + mmp->DeviceNameOffset, device_name.data(), device_name.length() * sizeof(WCHAR));
88  }
89 
90  vector<uint8_t> buf2(sizeof(MOUNTMGR_MOUNT_POINTS));
91  auto mmps = reinterpret_cast<MOUNTMGR_MOUNT_POINTS*>(buf2.data());
92 
93  Status = NtDeviceIoControlFile(h, nullptr, nullptr, nullptr, &iosb, IOCTL_MOUNTMGR_DELETE_POINTS,
94  buf.data(), (ULONG)buf.size(), buf2.data(), (ULONG)buf2.size());
95 
97  buf2.resize(mmps->Size);
98 
99  Status = NtDeviceIoControlFile(h, nullptr, nullptr, nullptr, &iosb, IOCTL_MOUNTMGR_DELETE_POINTS,
100  buf.data(), (ULONG)buf.size(), buf2.data(), (ULONG)buf2.size());
101  }
102 
103  if (!NT_SUCCESS(Status))
104  throw ntstatus_error(Status);
105 }
106 
107 vector<mountmgr_point> mountmgr::query_points(const wstring_view& symlink, const wstring_view& unique_id, const wstring_view& device_name) const {
110  vector<mountmgr_point> v;
111 
112  vector<uint8_t> buf(sizeof(MOUNTMGR_MOUNT_POINT) + ((symlink.length() + unique_id.length() + device_name.length()) * sizeof(WCHAR)));
113  auto mmp = reinterpret_cast<MOUNTMGR_MOUNT_POINT*>(buf.data());
114 
115  memset(mmp, 0, sizeof(MOUNTMGR_MOUNT_POINT));
116 
117  if (symlink.length() > 0) {
118  mmp->SymbolicLinkNameOffset = sizeof(MOUNTMGR_MOUNT_POINT);
119  mmp->SymbolicLinkNameLength = (USHORT)(symlink.length() * sizeof(WCHAR));
120  memcpy((uint8_t*)mmp + mmp->SymbolicLinkNameOffset, symlink.data(), symlink.length() * sizeof(WCHAR));
121  }
122 
123  if (unique_id.length() > 0) {
124  if (mmp->SymbolicLinkNameLength == 0)
125  mmp->UniqueIdOffset = sizeof(MOUNTMGR_MOUNT_POINT);
126  else
127  mmp->UniqueIdOffset = mmp->SymbolicLinkNameOffset + mmp->SymbolicLinkNameLength;
128 
129  mmp->UniqueIdLength = (USHORT)(unique_id.length() * sizeof(WCHAR));
130  memcpy((uint8_t*)mmp + mmp->UniqueIdOffset, unique_id.data(), unique_id.length() * sizeof(WCHAR));
131  }
132 
133  if (device_name.length() > 0) {
134  if (mmp->SymbolicLinkNameLength == 0 && mmp->UniqueIdOffset == 0)
135  mmp->DeviceNameOffset = sizeof(MOUNTMGR_MOUNT_POINT);
136  else if (mmp->SymbolicLinkNameLength != 0)
137  mmp->DeviceNameOffset = mmp->SymbolicLinkNameOffset + mmp->SymbolicLinkNameLength;
138  else
139  mmp->DeviceNameOffset = mmp->UniqueIdOffset + mmp->UniqueIdLength;
140 
141  mmp->DeviceNameLength = (USHORT)(device_name.length() * sizeof(WCHAR));
142  memcpy((uint8_t*)mmp + mmp->DeviceNameOffset, device_name.data(), device_name.length() * sizeof(WCHAR));
143  }
144 
145  vector<uint8_t> buf2(sizeof(MOUNTMGR_MOUNT_POINTS));
146  auto mmps = reinterpret_cast<MOUNTMGR_MOUNT_POINTS*>(buf2.data());
147 
148  Status = NtDeviceIoControlFile(h, nullptr, nullptr, nullptr, &iosb, IOCTL_MOUNTMGR_QUERY_POINTS,
149  buf.data(), (ULONG)buf.size(), buf2.data(), (ULONG)buf2.size());
150 
152  throw ntstatus_error(Status);
153 
154  buf2.resize(mmps->Size);
155  mmps = reinterpret_cast<MOUNTMGR_MOUNT_POINTS*>(buf2.data());
156 
157  Status = NtDeviceIoControlFile(h, nullptr, nullptr, nullptr, &iosb, IOCTL_MOUNTMGR_QUERY_POINTS,
158  buf.data(), (ULONG)buf.size(), buf2.data(), (ULONG)buf2.size());
159 
160  if (!NT_SUCCESS(Status))
161  throw ntstatus_error(Status);
162 
163  for (ULONG i = 0; i < mmps->NumberOfMountPoints; i++) {
164  wstring_view mpsl, mpdn;
165  string_view mpuid;
166 
167  if (mmps->MountPoints[i].SymbolicLinkNameLength)
168  mpsl = wstring_view((WCHAR*)((uint8_t*)mmps + mmps->MountPoints[i].SymbolicLinkNameOffset), mmps->MountPoints[i].SymbolicLinkNameLength / sizeof(WCHAR));
169 
170  if (mmps->MountPoints[i].UniqueIdLength)
171  mpuid = string_view((char*)((uint8_t*)mmps + mmps->MountPoints[i].UniqueIdOffset), mmps->MountPoints[i].UniqueIdLength);
172 
173  if (mmps->MountPoints[i].DeviceNameLength)
174  mpdn = wstring_view((WCHAR*)((uint8_t*)mmps + mmps->MountPoints[i].DeviceNameOffset), mmps->MountPoints[i].DeviceNameLength / sizeof(WCHAR));
175 
176 #ifndef __REACTOS__
177  v.emplace_back(mpsl, mpuid, mpdn);
178 #else
179  v.push_back(mountmgr_point(mpsl, mpuid, mpdn));
180 #endif
181  }
182 
183  return v;
184 }
#define FILE_GENERIC_READ
Definition: nt_native.h:653
static PIO_STATUS_BLOCK iosb
Definition: file.c:98
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
static const BYTE us[]
Definition: encode.c:689
struct _MOUNTMGR_MOUNT_POINT MOUNTMGR_MOUNT_POINT
LONG NTSTATUS
Definition: precomp.h:26
static const WCHAR device_name[]
Definition: btrfs.c:61
#define IOCTL_MOUNTMGR_CREATE_POINT
Definition: imports.h:118
NTSYSAPI NTSTATUS NTAPI NtDeviceIoControlFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG DeviceIoControlCode, IN PVOID InBuffer OPTIONAL, IN ULONG InBufferLength, OUT PVOID OutBuffer OPTIONAL, IN ULONG OutBufferLength)
Definition: features.h:417
#define MOUNTMGR_DEVICE_NAME
Definition: imports.h:76
#define FILE_SHARE_READ
Definition: compat.h:125
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:7723
#define IOCTL_MOUNTMGR_QUERY_POINTS
Definition: mountmgr.h:30
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define FILE_SYNCHRONOUS_IO_ALERT
Definition: from_kernel.h:30
Definition: devices.h:37
std::vector< mountmgr_point > query_points(const std::wstring_view &symlink=L"", const std::wstring_view &unique_id=L"", const std::wstring_view &device_name=L"") const
void create_point(const std::wstring_view &symlink, const std::wstring_view &device) const
void delete_points(const std::wstring_view &symlink, const std::wstring_view &unique_id=L"", const std::wstring_view &device_name=L"") const
NTSYSAPI NTSTATUS NTAPI NtOpenFile(OUT PHANDLE phFile, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG ShareMode, IN ULONG OpenMode)
Definition: file.c:3951
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
Definition: cookie.c:201
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
__u8 attr
Definition: mkdosfs.c:359
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
BYTE uint8_t
Definition: msvideo1.c:66
Status
Definition: gdiplustypes.h:24
#define FILE_GENERIC_WRITE
Definition: nt_native.h:660
struct _MOUNTMGR_CREATE_POINT_INPUT MOUNTMGR_CREATE_POINT_INPUT
const GLdouble * v
Definition: gl.h:2040
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
unsigned short USHORT
Definition: pedump.c:61
unsigned int ULONG
Definition: retypes.h:1
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define memset(x, y, z)
Definition: compat.h:39
#define IOCTL_MOUNTMGR_DELETE_POINTS
Definition: imports.h:124