ReactOS 0.4.16-dev-2104-gb84fa49
mountmgr.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS DiskPart
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/diskpart/mountmgr.c
5 * PURPOSE: Manages all the partitions of the OS in an interactive way.
6 * PROGRAMMERS: Eric Kohl
7 */
8
9#include "diskpart.h"
10
11#include <mountmgr.h>
12
13#define NDEBUG
14#include <debug.h>
15
16/* FUNCTIONS ******************************************************************/
17
18static
21 _Out_ PHANDLE MountMgrHandle,
22 _In_ ACCESS_MASK Access)
23{
27
29
32 0,
33 NULL,
34 NULL);
35
36 return NtOpenFile(MountMgrHandle,
37 Access | SYNCHRONIZE,
39 &Iosb,
40 0,
42}
43
44
45BOOL
48{
49 HANDLE MountMgrHandle;
53
54 DPRINT("ShowAutomountState()\n");
55
56 Status = OpenMountManager(&MountMgrHandle, GENERIC_READ);
57 if (!NT_SUCCESS(Status))
58 {
59 DPRINT1("OpenMountManager() Status 0x%08lx\n", Status);
60 return FALSE;
61 }
62
63 Status = NtDeviceIoControlFile(MountMgrHandle,
64 NULL,
65 NULL,
66 NULL,
67 &Iosb,
69 NULL,
70 0,
71 &AutoMount,
72 sizeof(AutoMount));
73
74 NtClose(MountMgrHandle);
75
76 if (!NT_SUCCESS(Status))
77 {
78 DPRINT1("NtDeviceIoControlFile() Status 0x%08lx\n", Status);
79 return FALSE;
80 }
81
82 if (State)
83 *State = (AutoMount.CurrentState == Enabled);
84
85 return TRUE;
86}
87
88
89BOOL
92{
93 HANDLE MountMgrHandle;
97
98 DPRINT("SetAutomountState()\n");
99
101 if (!NT_SUCCESS(Status))
102 {
103 DPRINT1("OpenMountManager() Status 0x%08lx\n", Status);
104 return TRUE;
105 }
106
107 if (bEnable)
108 AutoMount.NewState = Enabled;
109 else
110 AutoMount.NewState = Disabled;
111
112 Status = NtDeviceIoControlFile(MountMgrHandle,
113 NULL,
114 NULL,
115 NULL,
116 &Iosb,
118 &AutoMount,
119 sizeof(AutoMount),
120 NULL,
121 0);
122
123 NtClose(MountMgrHandle);
124
125 if (!NT_SUCCESS(Status))
126 {
127 DPRINT1("NtDeviceIoControlFile() Status 0x%08lx\n", Status);
128 return TRUE;
129 }
130
131 if (AutoMount.NewState == Enabled)
133 else
135 ConPuts(StdOut, L"\n");
136
137 return TRUE;
138}
139
140BOOL
142{
143 HANDLE MountMgrHandle;
146
147 DPRINT("ScrubAutomount()\n");
148
150 if (!NT_SUCCESS(Status))
151 {
152 DPRINT1("OpenMountManager() Status 0x%08lx\n", Status);
153 return FALSE;
154 }
155
156 Status = NtDeviceIoControlFile(MountMgrHandle,
157 NULL,
158 NULL,
159 NULL,
160 &Iosb,
162 NULL,
163 0,
164 NULL,
165 0);
166
167 NtClose(MountMgrHandle);
168
169 if (!NT_SUCCESS(Status))
170 {
171 DPRINT1("NtDeviceIoControlFile() Status 0x%08lx\n", Status);
172 return FALSE;
173 }
174
175 return TRUE;
176}
177
178
179
180BOOL
183 _In_ WCHAR DriveLetter)
184{
186 ULONG DosDeviceNameLength, DeviceNameLength;
189 HANDLE MountMgrHandle = NULL;
192 BOOL Ret = TRUE;
193
194 DPRINT1("AssignDriveLetter(%S %c)\n", DeviceName, DriveLetter);
195
196 DeviceNameLength = wcslen(DeviceName) * sizeof(WCHAR);
197
198 swprintf(DosDeviceName, L"\\DosDevices\\%c:", DriveLetter);
199 DosDeviceNameLength = wcslen(DosDeviceName) * sizeof(WCHAR);
200
201 /* Allocate the input buffer for the MountMgr */
202 InputBufferLength = DosDeviceNameLength + DeviceNameLength + sizeof(MOUNTMGR_CREATE_POINT_INPUT);
204 if (InputBuffer == NULL)
205 {
206 DPRINT1("InputBuffer allocation failed!\n");
207 return FALSE;
208 }
209
210 /* Fill the input buffer */
211 InputBuffer->SymbolicLinkNameOffset = sizeof(MOUNTMGR_CREATE_POINT_INPUT);
212 InputBuffer->SymbolicLinkNameLength = DosDeviceNameLength;
213 InputBuffer->DeviceNameOffset = DosDeviceNameLength + sizeof(MOUNTMGR_CREATE_POINT_INPUT);
214 InputBuffer->DeviceNameLength = DeviceNameLength;
217 DosDeviceNameLength);
218 RtlCopyMemory((PVOID)((ULONG_PTR)InputBuffer + InputBuffer->DeviceNameOffset),
220 DeviceNameLength);
221
222DPRINT1("\n");
224 if (!NT_SUCCESS(Status))
225 {
226 DPRINT1("OpenMountManager() failed (Status 0x%08lx)\n", Status);
227 Ret = FALSE;
228 goto done;
229 }
230
231DPRINT1("\n");
232 Status = NtDeviceIoControlFile(MountMgrHandle,
233 NULL,
234 NULL,
235 NULL,
236 &Iosb,
240 NULL,
241 0);
242 if (!NT_SUCCESS(Status))
243 {
244 DPRINT1("NtDeviceIoControlFile() failed (Status 0x%08lx)\n", Status);
245 Ret = FALSE;
246 goto done;
247 }
248
249DPRINT1("\n");
250done:
251 if (MountMgrHandle)
252 NtClose(MountMgrHandle);
253
254 RtlFreeHeap(RtlGetProcessHeap(), 0, InputBuffer);
255
256 return Ret;
257}
258
259
260BOOL
263 _Out_ PWCHAR DriveLetter)
264{
267 ULONG DeviceNameLength, InputBufferLength;
268 HANDLE MountMgrHandle = NULL;
271 BOOL Ret = TRUE;
272
273 DPRINT1("AssignNextDriveLetter(%S %p)\n", DeviceName, DriveLetter);
274
275 DeviceNameLength = wcslen(DeviceName) * sizeof(WCHAR);
276
279 if (InputBuffer == NULL)
280 {
281 DPRINT1("InputBuffer allocation failed!\n");
282 return FALSE;
283 }
284
285 /* And fill it with the device hat needs a drive letter */
286 InputBuffer->DeviceNameLength = DeviceNameLength;
287 RtlCopyMemory(&InputBuffer->DeviceName[0],
289 DeviceNameLength);
290
291DPRINT1("\n");
293 if (!NT_SUCCESS(Status))
294 {
295 DPRINT1("OpenMountManager() failed (Status 0x%08lx)\n", Status);
296 Ret = FALSE;
297 goto done;
298 }
299
300DPRINT1("\n");
301 Status = NtDeviceIoControlFile(MountMgrHandle,
302 NULL,
303 NULL,
304 NULL,
305 &Iosb,
309 &LetterInfo,
310 sizeof(LetterInfo));
311 if (!NT_SUCCESS(Status))
312 {
313 DPRINT1("NtDeviceIoControlFile() failed (Status 0x%08lx)\n", Status);
314 Ret = FALSE;
315 goto done;
316 }
317
318DPRINT1("\n");
319done:
320 if (MountMgrHandle)
321 NtClose(MountMgrHandle);
322
323 RtlFreeHeap(RtlGetProcessHeap(), 0, InputBuffer);
324
325 if (Ret)
326 {
327 if (LetterInfo.DriveLetterWasAssigned)
328 *DriveLetter = LetterInfo.CurrentDriveLetter;
329 else
330 *DriveLetter = UNICODE_NULL;
331 }
332
333 return Ret;
334}
335
336
337BOOL
339 _In_ WCHAR DriveLetter)
340{
344 ULONG InputBufferLength, DosDeviceNameLength;
345 HANDLE MountMgrHandle = NULL;
348 BOOL Ret = TRUE;
349
350 DPRINT1("DeleteDriveLetter(%c)\n", DriveLetter);
351
352 /* Setup the device name of the letter to delete */
353 swprintf(DosDeviceName, L"\\DosDevices\\%c:", DriveLetter);
354 DosDeviceNameLength = wcslen(DosDeviceName) * sizeof(WCHAR);
355
356 /* Allocate the input buffer for MountMgr */
357 InputBufferLength = DosDeviceNameLength + sizeof(MOUNTMGR_MOUNT_POINT);
359 if (InputBuffer == NULL)
360 {
361 DPRINT1("InputBuffer allocation failed!\n");
362 return FALSE;
363 }
364
365 /* Fill it in */
366// RtlZeroMemory(InputBuffer, InputBufferLength);
367 InputBuffer->SymbolicLinkNameOffset = sizeof(MOUNTMGR_MOUNT_POINT);
368 InputBuffer->SymbolicLinkNameLength = DosDeviceNameLength;
369 RtlCopyMemory(&InputBuffer[1], DosDeviceName, DosDeviceNameLength);
370
371 /* Allocate big enough output buffer (we don't care about the output) */
373 if (OutputBuffer == NULL)
374 {
375 DPRINT1("OutputBuffer allocation failed!\n");
376 Ret = FALSE;
377 goto done;
378 }
379
380DPRINT1("\n");
382 if (!NT_SUCCESS(Status))
383 {
384 DPRINT1("OpenMountManager() failed (Status 0x%08lx)\n", Status);
385 Ret = FALSE;
386 goto done;
387 }
388
389DPRINT1("\n");
390 Status = NtDeviceIoControlFile(MountMgrHandle,
391 NULL,
392 NULL,
393 NULL,
394 &Iosb,
399 0x1000);
400 if (!NT_SUCCESS(Status))
401 {
402 DPRINT1("NtDeviceIoControlFile() failed (Status 0x%08lx)\n", Status);
403 Ret = FALSE;
404 goto done;
405 }
406
407DPRINT1("\n");
408done:
409 if (MountMgrHandle)
410 NtClose(MountMgrHandle);
411
412 if (InputBuffer)
413 RtlFreeHeap(RtlGetProcessHeap(), 0, InputBuffer);
414
415 if (OutputBuffer)
416 RtlFreeHeap(RtlGetProcessHeap(), 0, OutputBuffer);
417
418 return Ret;
419}
void ConPuts(FILE *fp, LPCWSTR psz)
Definition: fc.c:16
#define StdOut
Definition: fc.c:14
void ConResPuts(FILE *fp, UINT nID)
Definition: fc.c:27
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
BOOL GetAutomountState(_Out_ PBOOL State)
Definition: mountmgr.c:46
static NTSTATUS OpenMountManager(_Out_ PHANDLE MountMgrHandle, _In_ ACCESS_MASK Access)
Definition: mountmgr.c:20
BOOL ScrubAutomount(VOID)
Definition: mountmgr.c:141
BOOL DeleteDriveLetter(_In_ WCHAR DriveLetter)
Definition: mountmgr.c:338
BOOL SetAutomountState(_In_ BOOL bEnable)
Definition: mountmgr.c:90
BOOL AssignNextDriveLetter(_In_ PWSTR DeviceName, _Out_ PWCHAR DriveLetter)
Definition: mountmgr.c:261
BOOL AssignDriveLetter(_In_ PWSTR DeviceName, _In_ WCHAR DriveLetter)
Definition: mountmgr.c:181
#define IDS_AUTOMOUNT_DISABLED
Definition: resource.h:27
#define IDS_AUTOMOUNT_ENABLED
Definition: resource.h:26
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:616
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:634
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define GetProcessHeap()
Definition: compat.h:736
#define GENERIC_READ
Definition: compat.h:135
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
_ACRTIMP size_t __cdecl wcslen(const wchar_t *)
Definition: wcs.c:2983
#define swprintf
Definition: precomp.h:40
#define L(x)
Definition: resources.c:13
return Iosb
Definition: create.c:4403
unsigned int BOOL
Definition: ntddk_ex.h:94
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
Status
Definition: gdiplustypes.h:25
static char DosDeviceName[DEVICE_SIZE]
Definition: lsdd.c:26
BOOL * PBOOL
Definition: minwindef.h:137
#define IOCTL_MOUNTMGR_DELETE_POINTS
Definition: imports.h:122
struct _MOUNTMGR_CREATE_POINT_INPUT MOUNTMGR_CREATE_POINT_INPUT
#define MOUNTMGR_DEVICE_NAME
Definition: imports.h:74
struct _MOUNTMGR_MOUNT_POINT MOUNTMGR_MOUNT_POINT
#define IOCTL_MOUNTMGR_CREATE_POINT
Definition: imports.h:116
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
@ Enabled
Definition: mountmgr.h:179
@ Disabled
Definition: mountmgr.h:178
#define IOCTL_MOUNTMGR_SCRUB_REGISTRY
Definition: mountmgr.h:170
#define IOCTL_MOUNTMGR_QUERY_AUTO_MOUNT
Definition: mountmgr.h:172
#define IOCTL_MOUNTMGR_SET_AUTO_MOUNT
Definition: mountmgr.h:174
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
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:3953
#define SYNCHRONIZE
Definition: nt_native.h:61
ULONG ACCESS_MASK
Definition: nt_native.h:40
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
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)
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
#define GENERIC_WRITE
Definition: nt_native.h:90
#define UNICODE_NULL
PVOID *typedef PHANDLE
Definition: ntsecpkg.h:455
#define DPRINT
Definition: sndvol32.h:73
MOUNTMGR_AUTO_MOUNT_STATE CurrentState
Definition: mountmgr.h:183
MOUNTMGR_AUTO_MOUNT_STATE NewState
Definition: mountmgr.h:187
uint16_t * PWSTR
Definition: typedefs.h:56
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint16_t * PWCHAR
Definition: typedefs.h:56
uint32_t ULONG
Definition: typedefs.h:59
_Must_inspect_result_ _In_ PWDFDEVICE_INIT _In_opt_ PCUNICODE_STRING DeviceName
Definition: wdfdevice.h:3281
_In_ WDFREQUEST _In_ size_t _In_ size_t InputBufferLength
Definition: wdfio.h:322
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_opt_ PWDF_MEMORY_DESCRIPTOR OutputBuffer
Definition: wdfiotarget.h:863
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_opt_ PWDF_MEMORY_DESCRIPTOR InputBuffer
Definition: wdfiotarget.h:953
_In_ BOOL bEnable
Definition: winddi.h:3426
__wchar_t WCHAR
Definition: xmlstorage.h:180