Skip to content

Commit e2c3a6e

Browse files
committed
[DISKPART] Move mount manager functions into a separate file
- Move the existing function from automount.c - Implement required function for the assign and remove commands. Deleting a drive letter works, until the next reboot. Assigning a new drive letter fails.
1 parent e04d052 commit e2c3a6e

6 files changed

Lines changed: 499 additions & 205 deletions

File tree

‎base/system/diskpart/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ list(APPEND SOURCE
3232
list.c
3333
merge.c
3434
misc.c
35+
mountmgr.c
3536
offline.c
3637
online.c
3738
partlist.c

‎base/system/diskpart/assign.c‎

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ assign_main(
1616
_In_ INT argc,
1717
_In_ LPWSTR *argv)
1818
{
19-
WCHAR szMountPoint[4];
2019
PWSTR pszSuffix = NULL;
2120
WCHAR DriveLetter = UNICODE_NULL;
2221
INT i, nExclusive = 0;
@@ -105,12 +104,7 @@ assign_main(
105104
if (CurrentVolume->DriveLetter != UNICODE_NULL)
106105
{
107106
/* Remove the current drive letter */
108-
szMountPoint[0] = CurrentVolume->DriveLetter;
109-
szMountPoint[1] = L':';
110-
szMountPoint[2] = L'\\';
111-
szMountPoint[3] = UNICODE_NULL;
112-
113-
bResult = DeleteVolumeMountPointW(szMountPoint);
107+
bResult = DeleteDriveLetter(CurrentVolume->DriveLetter);
114108
if (bResult == FALSE)
115109
{
116110
ConResPuts(StdOut, IDS_ASSIGN_FAIL);
@@ -123,13 +117,8 @@ assign_main(
123117
if (DriveLetter != UNICODE_NULL)
124118
{
125119
/* Assign the new drive letter */
126-
szMountPoint[0] = DriveLetter;
127-
szMountPoint[1] = L':';
128-
szMountPoint[2] = L'\\';
129-
szMountPoint[3] = UNICODE_NULL;
130-
131-
bResult = SetVolumeMountPointW(szMountPoint,
132-
CurrentVolume->VolumeName);
120+
bResult = AssignDriveLetter(CurrentVolume->DeviceName,
121+
DriveLetter);
133122
if (bResult == FALSE)
134123
{
135124
ConResPuts(StdOut, IDS_ASSIGN_FAIL);
@@ -140,16 +129,19 @@ assign_main(
140129
}
141130
else
142131
{
143-
/* TODO: Assign the next drive letter */
144-
#if 0
145-
bResult = SetNextDriveLetter(CurrentVolume->VolumeName,
146-
&CurrentVolume->DriveLetter);
132+
bResult = AssignNextDriveLetter(CurrentVolume->DeviceName,
133+
&CurrentVolume->DriveLetter);
147134
if (bResult == FALSE)
148135
{
149136
ConResPuts(StdOut, IDS_ASSIGN_FAIL);
150137
return TRUE;
151138
}
152-
#endif
139+
140+
if (CurrentVolume->DriveLetter == UNICODE_NULL)
141+
{
142+
ConResPuts(StdOut, IDS_ASSIGN_NO_MORE_LETTER);
143+
return TRUE;
144+
}
153145
}
154146

155147
ConResPuts(StdOut, IDS_REMOVE_SUCCESS);

‎base/system/diskpart/automount.c‎

Lines changed: 40 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -12,176 +12,6 @@
1212
#include <debug.h>
1313

1414

15-
static
16-
NTSTATUS
17-
OpenMountManager(
18-
_Out_ PHANDLE MountMgrHandle,
19-
_In_ ACCESS_MASK Access)
20-
{
21-
OBJECT_ATTRIBUTES ObjectAttributes;
22-
UNICODE_STRING DeviceName;
23-
IO_STATUS_BLOCK Iosb;
24-
25-
RtlInitUnicodeString(&DeviceName, MOUNTMGR_DEVICE_NAME);
26-
27-
InitializeObjectAttributes(&ObjectAttributes,
28-
&DeviceName,
29-
0,
30-
NULL,
31-
NULL);
32-
33-
return NtOpenFile(MountMgrHandle,
34-
Access | SYNCHRONIZE,
35-
&ObjectAttributes,
36-
&Iosb,
37-
0,
38-
FILE_SYNCHRONOUS_IO_NONALERT);
39-
}
40-
41-
42-
static
43-
BOOL
44-
ShowAutomountState(VOID)
45-
{
46-
HANDLE MountMgrHandle;
47-
MOUNTMGR_QUERY_AUTO_MOUNT AutoMount;
48-
IO_STATUS_BLOCK Iosb;
49-
NTSTATUS Status;
50-
51-
DPRINT("ShowAutomountState()\n");
52-
53-
Status = OpenMountManager(&MountMgrHandle, GENERIC_READ);
54-
if (!NT_SUCCESS(Status))
55-
{
56-
DPRINT1("OpenMountManager() Status 0x%08lx\n", Status);
57-
return TRUE;
58-
}
59-
60-
Status = NtDeviceIoControlFile(MountMgrHandle,
61-
NULL,
62-
NULL,
63-
NULL,
64-
&Iosb,
65-
IOCTL_MOUNTMGR_QUERY_AUTO_MOUNT,
66-
NULL,
67-
0,
68-
&AutoMount,
69-
sizeof(AutoMount));
70-
71-
NtClose(MountMgrHandle);
72-
73-
if (!NT_SUCCESS(Status))
74-
{
75-
DPRINT1("NtDeviceIoControlFile() Status 0x%08lx\n", Status);
76-
return TRUE;
77-
}
78-
79-
if (AutoMount.CurrentState == Enabled)
80-
ConResPuts(StdOut, IDS_AUTOMOUNT_ENABLED);
81-
else
82-
ConResPuts(StdOut, IDS_AUTOMOUNT_DISABLED);
83-
ConPuts(StdOut, L"\n");
84-
85-
return TRUE;
86-
}
87-
88-
89-
static
90-
BOOL
91-
SetAutomountState(
92-
_In_ BOOL bEnable)
93-
{
94-
HANDLE MountMgrHandle;
95-
MOUNTMGR_SET_AUTO_MOUNT AutoMount;
96-
IO_STATUS_BLOCK Iosb;
97-
NTSTATUS Status;
98-
99-
DPRINT("SetAutomountState()\n");
100-
101-
Status = OpenMountManager(&MountMgrHandle, GENERIC_READ | GENERIC_WRITE);
102-
if (!NT_SUCCESS(Status))
103-
{
104-
DPRINT1("OpenMountManager() Status 0x%08lx\n", Status);
105-
return TRUE;
106-
}
107-
108-
if (bEnable)
109-
AutoMount.NewState = Enabled;
110-
else
111-
AutoMount.NewState = Disabled;
112-
113-
Status = NtDeviceIoControlFile(MountMgrHandle,
114-
NULL,
115-
NULL,
116-
NULL,
117-
&Iosb,
118-
IOCTL_MOUNTMGR_SET_AUTO_MOUNT,
119-
&AutoMount,
120-
sizeof(AutoMount),
121-
NULL,
122-
0);
123-
124-
NtClose(MountMgrHandle);
125-
126-
if (!NT_SUCCESS(Status))
127-
{
128-
DPRINT1("NtDeviceIoControlFile() Status 0x%08lx\n", Status);
129-
return TRUE;
130-
}
131-
132-
if (AutoMount.NewState == Enabled)
133-
ConResPuts(StdOut, IDS_AUTOMOUNT_ENABLED);
134-
else
135-
ConResPuts(StdOut, IDS_AUTOMOUNT_DISABLED);
136-
ConPuts(StdOut, L"\n");
137-
138-
return TRUE;
139-
}
140-
141-
142-
static
143-
BOOL
144-
ScrubAutomount(VOID)
145-
{
146-
HANDLE MountMgrHandle;
147-
IO_STATUS_BLOCK Iosb;
148-
NTSTATUS Status;
149-
150-
DPRINT("ScrubAutomount()\n");
151-
152-
Status = OpenMountManager(&MountMgrHandle, GENERIC_READ | GENERIC_WRITE);
153-
if (!NT_SUCCESS(Status))
154-
{
155-
DPRINT1("OpenMountManager() Status 0x%08lx\n", Status);
156-
return TRUE;
157-
}
158-
159-
Status = NtDeviceIoControlFile(MountMgrHandle,
160-
NULL,
161-
NULL,
162-
NULL,
163-
&Iosb,
164-
IOCTL_MOUNTMGR_SCRUB_REGISTRY,
165-
NULL,
166-
0,
167-
NULL,
168-
0);
169-
170-
NtClose(MountMgrHandle);
171-
172-
if (!NT_SUCCESS(Status))
173-
{
174-
DPRINT1("NtDeviceIoControlFile() Status 0x%08lx\n", Status);
175-
return TRUE;
176-
}
177-
178-
ConResPuts(StdOut, IDS_AUTOMOUNT_SCRUBBED);
179-
ConPuts(StdOut, L"\n");
180-
181-
return TRUE;
182-
}
183-
184-
18515
BOOL
18616
automount_main(
18717
INT argc,
@@ -192,6 +22,7 @@ automount_main(
19222
BOOL bNoErr = FALSE;
19323
#endif
19424
INT i;
25+
BOOL Result, State;
19526

19627
DPRINT("Automount()\n");
19728

@@ -247,25 +78,60 @@ automount_main(
24778
if ((bDisable == FALSE) && (bEnable == FALSE) && (bScrub == FALSE))
24879
{
24980
DPRINT("Show automount\n");
250-
return ShowAutomountState();
81+
Result = GetAutomountState(&State);
82+
if (Result == FALSE)
83+
{
84+
// ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
85+
return TRUE;
86+
}
87+
88+
if (State)
89+
ConResPuts(StdOut, IDS_AUTOMOUNT_ENABLED);
90+
else
91+
ConResPuts(StdOut, IDS_AUTOMOUNT_DISABLED);
92+
ConPuts(StdOut, L"\n");
25193
}
25294

25395
if (bDisable)
25496
{
25597
DPRINT("Disable automount\n");
256-
return SetAutomountState(FALSE);
98+
Result = SetAutomountState(FALSE);
99+
if (Result == FALSE)
100+
{
101+
// ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
102+
return TRUE;
103+
}
104+
105+
ConResPuts(StdOut, IDS_AUTOMOUNT_DISABLED);
106+
ConPuts(StdOut, L"\n");
257107
}
258108

259109
if (bEnable)
260110
{
261111
DPRINT("Enable automount\n");
262-
return SetAutomountState(TRUE);
112+
Result = SetAutomountState(TRUE);
113+
if (Result == FALSE)
114+
{
115+
// ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
116+
return TRUE;
117+
}
118+
119+
ConResPuts(StdOut, IDS_AUTOMOUNT_ENABLED);
120+
ConPuts(StdOut, L"\n");
263121
}
264122

265123
if (bScrub)
266124
{
267125
DPRINT("Scrub automount\n");
268-
return ScrubAutomount();
126+
Result = ScrubAutomount();
127+
if (Result == FALSE)
128+
{
129+
// ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
130+
return TRUE;
131+
}
132+
133+
ConResPuts(StdOut, IDS_AUTOMOUNT_SCRUBBED);
134+
ConPuts(StdOut, L"\n");
269135
}
270136

271137
return TRUE;

‎base/system/diskpart/diskpart.h‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,32 @@ PrintBusType(
503503
_In_ INT cchBufferMax,
504504
_In_ STORAGE_BUS_TYPE Bustype);
505505

506+
/* mountmgr.h */
507+
BOOL
508+
GetAutomountState(
509+
_Out_ PBOOL State);
510+
511+
BOOL
512+
SetAutomountState(
513+
_In_ BOOL bEnable);
514+
515+
BOOL
516+
ScrubAutomount(VOID);
517+
518+
BOOL
519+
AssignDriveLetter(
520+
_In_ PWSTR DeviceName,
521+
_In_ WCHAR DriveLetter);
522+
523+
BOOL
524+
AssignNextDriveLetter(
525+
_In_ PWSTR DeviceName,
526+
_Out_ PWCHAR DriveLetter);
527+
528+
BOOL
529+
DeleteDriveLetter(
530+
_In_ WCHAR DriveLetter);
531+
506532
/* offline.c */
507533
BOOL offline_main(INT argc, LPWSTR *argv);
508534

0 commit comments

Comments
 (0)