Skip to content

Commit 3919f61

Browse files
committed
add logs for better tracing error.
Refactoring for cleaning. GetRawDeviceName interface little bit changed.
1 parent bda68ef commit 3919f61

File tree

4 files changed

+156
-114
lines changed

4 files changed

+156
-114
lines changed

dokani.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ SendToDevice(
9898
ULONG OutputLength,
9999
PULONG ReturnedLength);
100100

101-
LPCWSTR
102-
GetRawDeviceName(LPCWSTR DeviceName);
101+
BOOL GetRawDeviceName(LPCWSTR pDeviceName, LPWSTR pRawDeviceName, size_t cchRawDeviceName);
103102

104103
UINT __stdcall
105104
DokanLoop(

dokanx/access.cpp

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,29 @@ DokanOpenRequestorToken(PDOKAN_FILE_INFO FileInfo)
3535
ULONG eventInfoSize;
3636

3737
openInfo = (PDOKAN_OPEN_INFO)(INT_PTR)FileInfo->DokanContext;
38-
if (openInfo == NULL) {
38+
if (openInfo == NULL)
39+
{
40+
logw(L"OpenInfo is null");
3941
return INVALID_HANDLE_VALUE;
4042
}
4143

4244
eventContext = openInfo->EventContext;
43-
if (eventContext == NULL) {
45+
if (eventContext == NULL)
46+
{
47+
logw(L"EventContext is null");
4448
return INVALID_HANDLE_VALUE;
4549
}
4650

4751
instance = openInfo->DokanInstance;
48-
if (instance == NULL) {
52+
if (instance == NULL)
53+
{
54+
logw(L"Dokan Instance is null");
4955
return INVALID_HANDLE_VALUE;
5056
}
5157

52-
if (eventContext->MajorFunction != IRP_MJ_CREATE) {
58+
if (eventContext->MajorFunction != IRP_MJ_CREATE)
59+
{
60+
logw(L"MajorFunction isn't IRP_MJ_CREATE")
5361
return INVALID_HANDLE_VALUE;
5462
}
5563

@@ -59,17 +67,27 @@ DokanOpenRequestorToken(PDOKAN_FILE_INFO FileInfo)
5967

6068
eventInfo->SerialNumber = eventContext->SerialNumber;
6169

70+
WCHAR rawDeviceName[MAX_PATH];
71+
if (GetRawDeviceName(instance->DeviceName, rawDeviceName, _countof(rawDeviceName)))
72+
{
73+
logw(L"Failed to get raw device name from <%s>", instance->DeviceName);
74+
return INVALID_HANDLE_VALUE;
75+
}
76+
6277
status = SendToDevice(
63-
GetRawDeviceName(instance->DeviceName),
64-
IOCTL_GET_ACCESS_TOKEN,
65-
eventInfo,
66-
eventInfoSize,
67-
eventInfo,
68-
eventInfoSize,
69-
&returnedLength);
70-
if (status) {
78+
rawDeviceName,
79+
IOCTL_GET_ACCESS_TOKEN,
80+
eventInfo,
81+
eventInfoSize,
82+
eventInfo,
83+
eventInfoSize,
84+
&returnedLength);
85+
if (status)
86+
{
7187
handle = eventInfo->AccessToken.Handle;
72-
} else {
88+
}
89+
else
90+
{
7391
logw(L"IOCTL_GET_ACCESS_TOKEN failed");
7492
}
7593
free(eventInfo);

dokanx/dokan.cpp

Lines changed: 96 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
2020
#include "stdafx.h"
2121
#include <windows.h>
2222
#include <winioctl.h>
23+
#include <string>
2324
#include <stdio.h>
2425
#include <string.h>
2526
#include <stdlib.h>
@@ -245,144 +246,150 @@ DokanMain(PDOKAN_OPTIONS DokanOptions, PDOKAN_OPERATIONS DokanOperations)
245246

246247
Sleep(1000);
247248

248-
logw(L"\nunload");
249+
logw(L"unload");
249250

250251
DeleteDokanInstance(instance);
251252

252253
return DOKAN_SUCCESS;
253254
}
254255

255-
LPCWSTR
256-
GetRawDeviceName(LPCWSTR DeviceName)
256+
BOOL GetRawDeviceName(LPCWSTR pDeviceName, LPWSTR pRawDeviceNameBuffer, size_t cchBuffer)
257257
{
258-
static WCHAR rawDeviceName[MAX_PATH];
259-
wcscpy_s(rawDeviceName, MAX_PATH, L"\\\\.");
260-
wcscat_s(rawDeviceName, MAX_PATH, DeviceName);
261-
return rawDeviceName;
258+
std::wstring raw = std::wstring(L"\\\\.") + std::wstring(pDeviceName);
259+
HRESULT hr = StringCchCopyW(pRawDeviceNameBuffer, cchBuffer, raw.c_str());
260+
return SUCCEEDED(hr);
262261
}
263262

264-
UINT WINAPI
265-
DokanLoop(
266-
PVOID pDokanInstance
267-
)
263+
UINT WINAPI DokanLoop(PVOID pInstance)
268264
{
269-
PDOKAN_INSTANCE DokanInstance = (PDOKAN_INSTANCE)pDokanInstance;
270-
HANDLE device;
271-
char buffer[EVENT_CONTEXT_MAX_SIZE];
272-
// ULONG count = 0;
273-
BOOL status;
274-
ULONG returnedLength;
275-
DWORD result = 0;
265+
logw(L"Start");
266+
PDOKAN_INSTANCE pDokanInstance = (PDOKAN_INSTANCE)pInstance;
267+
CHAR buffer[EVENT_CONTEXT_MAX_SIZE] = { 0 };
268+
ULONG returnedLength;
269+
DWORD result = ERROR_SUCCESS;
270+
WCHAR szRawDeviceName[MAX_PATH];
276271

277-
RtlZeroMemory(buffer, sizeof(buffer));
272+
if (!GetRawDeviceName(pDokanInstance->DeviceName, szRawDeviceName, _countof(szRawDeviceName)))
273+
{
274+
logw(L"Failed to get raw device name from <%s>", pDokanInstance->DeviceName);
275+
result = ERROR_INVALID_PARAMETER;
276+
_endthreadex(result);
277+
return result;
278+
}
278279

279-
device = CreateFile(
280-
GetRawDeviceName(DokanInstance->DeviceName), // lpFileName
281-
GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
282-
FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode
283-
NULL, // lpSecurityAttributes
284-
OPEN_EXISTING, // dwCreationDistribution
285-
0, // dwFlagsAndAttributes
286-
NULL // hTemplateFile
287-
);
280+
logw(L"RawDeviceName: <%s>", szRawDeviceName);
288281

289-
if (device == INVALID_HANDLE_VALUE) {
290-
logw(L"Dokan Error: CreateFile failed %ws: %d\n",
291-
GetRawDeviceName(DokanInstance->DeviceName), GetLastError());
292-
result = (DWORD)-1;
282+
HANDLE hDevice = CreateFile(
283+
szRawDeviceName,
284+
GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
285+
FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode
286+
NULL, // lpSecurityAttributes
287+
OPEN_EXISTING, // dwCreationDistribution
288+
0, // dwFlagsAndAttributes
289+
NULL // hTemplateFile
290+
);
291+
292+
if (hDevice == INVALID_HANDLE_VALUE)
293+
{
294+
DWORD dwLastError = GetLastError();
295+
logw(L"Dokan Error: CreateFile failed %s(%d)", szRawDeviceName, dwLastError);
296+
result = dwLastError;
293297
_endthreadex(result);
294298
return result;
295299
}
296300

297-
for ( ; ; ) {
298-
299-
status = DeviceIoControl(
300-
device, // Handle to device
301-
IOCTL_EVENT_WAIT, // IO Control code
302-
NULL, // Input Buffer to driver.
303-
0, // Length of input buffer in bytes.
304-
buffer, // Output Buffer from driver.
305-
sizeof(buffer), // Length of output buffer in bytes.
306-
&returnedLength, // Bytes placed in buffer.
307-
NULL // synchronous call
308-
);
301+
for ( ; ; )
302+
{
303+
BOOL fOk = DeviceIoControl(
304+
hDevice, // Handle to device
305+
IOCTL_EVENT_WAIT, // IO Control code
306+
NULL, // Input Buffer to driver.
307+
0, // Length of input buffer in bytes.
308+
buffer, // Output Buffer from driver.
309+
sizeof(buffer), // Length of output buffer in bytes.
310+
&returnedLength, // Bytes placed in buffer.
311+
NULL // synchronous call
312+
);
309313

310-
if (!status) {
311-
logw(L"Ioctl failed with code %d\n", GetLastError());
312-
result = (DWORD)-1;
314+
if (!fOk)
315+
{
316+
DWORD dwLastError = GetLastError();
317+
logw(L"Ioctl failed with code (%d). rawDeviceName is <%s>", dwLastError, szRawDeviceName);
318+
result = dwLastError;
313319
break;
314320
}
315321

316322
//printf("#%d got notification %d\n", (ULONG)Param, count++);
317323

318-
if(returnedLength > 0) {
324+
if (returnedLength > 0)
325+
{
319326
PEVENT_CONTEXT context = (PEVENT_CONTEXT)buffer;
320-
if (context->MountId != DokanInstance->MountId) {
321-
logw(L"Dokan Error: Invalid MountId (expected:%d, acctual:%d)\n",
322-
DokanInstance->MountId, context->MountId);
327+
if (context->MountId != pDokanInstance->MountId)
328+
{
329+
logw(L"Dokan Error: Invalid MountId (expected:%d, acctual:%d)", pDokanInstance->MountId, context->MountId);
323330
continue;
324331
}
325332

326-
switch (context->MajorFunction) {
333+
switch (context->MajorFunction)
334+
{
327335
case IRP_MJ_CREATE:
328-
DispatchCreate(device, context, DokanInstance);
336+
DispatchCreate(hDevice, context, pDokanInstance);
329337
break;
330338
case IRP_MJ_CLEANUP:
331-
DispatchCleanup(device, context, DokanInstance);
339+
DispatchCleanup(hDevice, context, pDokanInstance);
332340
break;
333341
case IRP_MJ_CLOSE:
334-
DispatchClose(device, context, DokanInstance);
342+
DispatchClose(hDevice, context, pDokanInstance);
335343
break;
336344
case IRP_MJ_DIRECTORY_CONTROL:
337-
DispatchDirectoryInformation(device, context, DokanInstance);
345+
DispatchDirectoryInformation(hDevice, context, pDokanInstance);
338346
break;
339347
case IRP_MJ_READ:
340-
DispatchRead(device, context, DokanInstance);
348+
DispatchRead(hDevice, context, pDokanInstance);
341349
break;
342350
case IRP_MJ_WRITE:
343-
DispatchWrite(device, context, DokanInstance);
351+
DispatchWrite(hDevice, context, pDokanInstance);
344352
break;
345353
case IRP_MJ_QUERY_INFORMATION:
346-
DispatchQueryInformation(device, context, DokanInstance);
354+
DispatchQueryInformation(hDevice, context, pDokanInstance);
347355
break;
348356
case IRP_MJ_QUERY_VOLUME_INFORMATION:
349-
DispatchQueryVolumeInformation(device ,context, DokanInstance);
357+
DispatchQueryVolumeInformation(hDevice ,context, pDokanInstance);
350358
break;
351359
case IRP_MJ_LOCK_CONTROL:
352-
DispatchLock(device, context, DokanInstance);
360+
DispatchLock(hDevice, context, pDokanInstance);
353361
break;
354362
case IRP_MJ_SET_INFORMATION:
355-
DispatchSetInformation(device, context, DokanInstance);
363+
DispatchSetInformation(hDevice, context, pDokanInstance);
356364
break;
357365
case IRP_MJ_FLUSH_BUFFERS:
358-
DispatchFlush(device, context, DokanInstance);
366+
DispatchFlush(hDevice, context, pDokanInstance);
359367
break;
360368
case IRP_MJ_QUERY_SECURITY:
361-
DispatchQuerySecurity(device, context, DokanInstance);
369+
DispatchQuerySecurity(hDevice, context, pDokanInstance);
362370
break;
363371
case IRP_MJ_SET_SECURITY:
364-
DispatchSetSecurity(device, context, DokanInstance);
372+
DispatchSetSecurity(hDevice, context, pDokanInstance);
365373
break;
366374
case IRP_MJ_SHUTDOWN:
367375
// this cass is used before unmount not shutdown
368-
DispatchUnmount(device, context, DokanInstance);
376+
DispatchUnmount(hDevice, context, pDokanInstance);
369377
break;
370378
default:
371379
break;
372380
}
373-
374-
} else {
375-
logw(L"ReturnedLength %d\n", returnedLength);
381+
}
382+
else
383+
{
384+
logw(L"ReturnedLength (%d)", returnedLength);
376385
}
377386
}
378387

379-
CloseHandle(device);
388+
CloseHandle(hDevice);
380389
_endthreadex(result);
381390
return result;
382391
}
383392

384-
385-
386393
VOID
387394
SendEventInformation(
388395
HANDLE Handle,
@@ -566,22 +573,27 @@ DispatchUnmount(
566573

567574

568575
// ask driver to release all pending IRP to prepare for Unmount.
569-
BOOL
570-
SendReleaseIRP(
571-
LPCWSTR DeviceName)
576+
BOOL SendReleaseIRP(LPCWSTR DeviceName)
572577
{
573-
ULONG returnedLength;
574-
578+
ULONG returnedLength;
575579
logw(L"send release");
576580

581+
WCHAR rawDeviceName[MAX_PATH];
582+
if (GetRawDeviceName(DeviceName, rawDeviceName, _countof(rawDeviceName)))
583+
{
584+
return FALSE;
585+
}
586+
577587
if (!SendToDevice(
578-
GetRawDeviceName(DeviceName),
579-
IOCTL_EVENT_RELEASE,
580-
NULL,
581-
0,
582-
NULL,
583-
0,
584-
&returnedLength) ) {
588+
rawDeviceName,
589+
IOCTL_EVENT_RELEASE,
590+
NULL,
591+
0,
592+
NULL,
593+
0,
594+
&returnedLength)
595+
)
596+
{
585597

586598
logw(L"Failed to unmount device:%ws\n", DeviceName);
587599
return FALSE;

0 commit comments

Comments
 (0)