Skip to content

Commit

Permalink
add logs for better tracing error.
Browse files Browse the repository at this point in the history
Refactoring for cleaning.
GetRawDeviceName interface little bit changed.
  • Loading branch information
BenjaminKim committed Feb 22, 2015
1 parent bda68ef commit 3919f61
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 114 deletions.
3 changes: 1 addition & 2 deletions dokani.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ SendToDevice(
ULONG OutputLength,
PULONG ReturnedLength);

LPCWSTR
GetRawDeviceName(LPCWSTR DeviceName);
BOOL GetRawDeviceName(LPCWSTR pDeviceName, LPWSTR pRawDeviceName, size_t cchRawDeviceName);

UINT __stdcall
DokanLoop(
Expand Down
44 changes: 31 additions & 13 deletions dokanx/access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,29 @@ DokanOpenRequestorToken(PDOKAN_FILE_INFO FileInfo)
ULONG eventInfoSize;

openInfo = (PDOKAN_OPEN_INFO)(INT_PTR)FileInfo->DokanContext;
if (openInfo == NULL) {
if (openInfo == NULL)
{
logw(L"OpenInfo is null");
return INVALID_HANDLE_VALUE;
}

eventContext = openInfo->EventContext;
if (eventContext == NULL) {
if (eventContext == NULL)
{
logw(L"EventContext is null");
return INVALID_HANDLE_VALUE;
}

instance = openInfo->DokanInstance;
if (instance == NULL) {
if (instance == NULL)
{
logw(L"Dokan Instance is null");
return INVALID_HANDLE_VALUE;
}

if (eventContext->MajorFunction != IRP_MJ_CREATE) {
if (eventContext->MajorFunction != IRP_MJ_CREATE)
{
logw(L"MajorFunction isn't IRP_MJ_CREATE")
return INVALID_HANDLE_VALUE;
}

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

eventInfo->SerialNumber = eventContext->SerialNumber;

WCHAR rawDeviceName[MAX_PATH];
if (GetRawDeviceName(instance->DeviceName, rawDeviceName, _countof(rawDeviceName)))
{
logw(L"Failed to get raw device name from <%s>", instance->DeviceName);
return INVALID_HANDLE_VALUE;
}

status = SendToDevice(
GetRawDeviceName(instance->DeviceName),
IOCTL_GET_ACCESS_TOKEN,
eventInfo,
eventInfoSize,
eventInfo,
eventInfoSize,
&returnedLength);
if (status) {
rawDeviceName,
IOCTL_GET_ACCESS_TOKEN,
eventInfo,
eventInfoSize,
eventInfo,
eventInfoSize,
&returnedLength);
if (status)
{
handle = eventInfo->AccessToken.Handle;
} else {
}
else
{
logw(L"IOCTL_GET_ACCESS_TOKEN failed");
}
free(eventInfo);
Expand Down
180 changes: 96 additions & 84 deletions dokanx/dokan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
#include "stdafx.h"
#include <windows.h>
#include <winioctl.h>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Expand Down Expand Up @@ -245,144 +246,150 @@ DokanMain(PDOKAN_OPTIONS DokanOptions, PDOKAN_OPERATIONS DokanOperations)

Sleep(1000);

logw(L"\nunload");
logw(L"unload");

DeleteDokanInstance(instance);

return DOKAN_SUCCESS;
}

LPCWSTR
GetRawDeviceName(LPCWSTR DeviceName)
BOOL GetRawDeviceName(LPCWSTR pDeviceName, LPWSTR pRawDeviceNameBuffer, size_t cchBuffer)
{
static WCHAR rawDeviceName[MAX_PATH];
wcscpy_s(rawDeviceName, MAX_PATH, L"\\\\.");
wcscat_s(rawDeviceName, MAX_PATH, DeviceName);
return rawDeviceName;
std::wstring raw = std::wstring(L"\\\\.") + std::wstring(pDeviceName);
HRESULT hr = StringCchCopyW(pRawDeviceNameBuffer, cchBuffer, raw.c_str());
return SUCCEEDED(hr);
}

UINT WINAPI
DokanLoop(
PVOID pDokanInstance
)
UINT WINAPI DokanLoop(PVOID pInstance)
{
PDOKAN_INSTANCE DokanInstance = (PDOKAN_INSTANCE)pDokanInstance;
HANDLE device;
char buffer[EVENT_CONTEXT_MAX_SIZE];
// ULONG count = 0;
BOOL status;
ULONG returnedLength;
DWORD result = 0;
logw(L"Start");
PDOKAN_INSTANCE pDokanInstance = (PDOKAN_INSTANCE)pInstance;
CHAR buffer[EVENT_CONTEXT_MAX_SIZE] = { 0 };
ULONG returnedLength;
DWORD result = ERROR_SUCCESS;
WCHAR szRawDeviceName[MAX_PATH];

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

device = CreateFile(
GetRawDeviceName(DokanInstance->DeviceName), // lpFileName
GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode
NULL, // lpSecurityAttributes
OPEN_EXISTING, // dwCreationDistribution
0, // dwFlagsAndAttributes
NULL // hTemplateFile
);
logw(L"RawDeviceName: <%s>", szRawDeviceName);

if (device == INVALID_HANDLE_VALUE) {
logw(L"Dokan Error: CreateFile failed %ws: %d\n",
GetRawDeviceName(DokanInstance->DeviceName), GetLastError());
result = (DWORD)-1;
HANDLE hDevice = CreateFile(
szRawDeviceName,
GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode
NULL, // lpSecurityAttributes
OPEN_EXISTING, // dwCreationDistribution
0, // dwFlagsAndAttributes
NULL // hTemplateFile
);

if (hDevice == INVALID_HANDLE_VALUE)
{
DWORD dwLastError = GetLastError();
logw(L"Dokan Error: CreateFile failed %s(%d)", szRawDeviceName, dwLastError);
result = dwLastError;
_endthreadex(result);
return result;
}

for ( ; ; ) {

status = DeviceIoControl(
device, // Handle to device
IOCTL_EVENT_WAIT, // IO Control code
NULL, // Input Buffer to driver.
0, // Length of input buffer in bytes.
buffer, // Output Buffer from driver.
sizeof(buffer), // Length of output buffer in bytes.
&returnedLength, // Bytes placed in buffer.
NULL // synchronous call
);
for ( ; ; )
{
BOOL fOk = DeviceIoControl(
hDevice, // Handle to device
IOCTL_EVENT_WAIT, // IO Control code
NULL, // Input Buffer to driver.
0, // Length of input buffer in bytes.
buffer, // Output Buffer from driver.
sizeof(buffer), // Length of output buffer in bytes.
&returnedLength, // Bytes placed in buffer.
NULL // synchronous call
);

if (!status) {
logw(L"Ioctl failed with code %d\n", GetLastError());
result = (DWORD)-1;
if (!fOk)
{
DWORD dwLastError = GetLastError();
logw(L"Ioctl failed with code (%d). rawDeviceName is <%s>", dwLastError, szRawDeviceName);
result = dwLastError;
break;
}

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

if(returnedLength > 0) {
if (returnedLength > 0)
{
PEVENT_CONTEXT context = (PEVENT_CONTEXT)buffer;
if (context->MountId != DokanInstance->MountId) {
logw(L"Dokan Error: Invalid MountId (expected:%d, acctual:%d)\n",
DokanInstance->MountId, context->MountId);
if (context->MountId != pDokanInstance->MountId)
{
logw(L"Dokan Error: Invalid MountId (expected:%d, acctual:%d)", pDokanInstance->MountId, context->MountId);
continue;
}

switch (context->MajorFunction) {
switch (context->MajorFunction)
{
case IRP_MJ_CREATE:
DispatchCreate(device, context, DokanInstance);
DispatchCreate(hDevice, context, pDokanInstance);
break;
case IRP_MJ_CLEANUP:
DispatchCleanup(device, context, DokanInstance);
DispatchCleanup(hDevice, context, pDokanInstance);
break;
case IRP_MJ_CLOSE:
DispatchClose(device, context, DokanInstance);
DispatchClose(hDevice, context, pDokanInstance);
break;
case IRP_MJ_DIRECTORY_CONTROL:
DispatchDirectoryInformation(device, context, DokanInstance);
DispatchDirectoryInformation(hDevice, context, pDokanInstance);
break;
case IRP_MJ_READ:
DispatchRead(device, context, DokanInstance);
DispatchRead(hDevice, context, pDokanInstance);
break;
case IRP_MJ_WRITE:
DispatchWrite(device, context, DokanInstance);
DispatchWrite(hDevice, context, pDokanInstance);
break;
case IRP_MJ_QUERY_INFORMATION:
DispatchQueryInformation(device, context, DokanInstance);
DispatchQueryInformation(hDevice, context, pDokanInstance);
break;
case IRP_MJ_QUERY_VOLUME_INFORMATION:
DispatchQueryVolumeInformation(device ,context, DokanInstance);
DispatchQueryVolumeInformation(hDevice ,context, pDokanInstance);
break;
case IRP_MJ_LOCK_CONTROL:
DispatchLock(device, context, DokanInstance);
DispatchLock(hDevice, context, pDokanInstance);
break;
case IRP_MJ_SET_INFORMATION:
DispatchSetInformation(device, context, DokanInstance);
DispatchSetInformation(hDevice, context, pDokanInstance);
break;
case IRP_MJ_FLUSH_BUFFERS:
DispatchFlush(device, context, DokanInstance);
DispatchFlush(hDevice, context, pDokanInstance);
break;
case IRP_MJ_QUERY_SECURITY:
DispatchQuerySecurity(device, context, DokanInstance);
DispatchQuerySecurity(hDevice, context, pDokanInstance);
break;
case IRP_MJ_SET_SECURITY:
DispatchSetSecurity(device, context, DokanInstance);
DispatchSetSecurity(hDevice, context, pDokanInstance);
break;
case IRP_MJ_SHUTDOWN:
// this cass is used before unmount not shutdown
DispatchUnmount(device, context, DokanInstance);
DispatchUnmount(hDevice, context, pDokanInstance);
break;
default:
break;
}

} else {
logw(L"ReturnedLength %d\n", returnedLength);
}
else
{
logw(L"ReturnedLength (%d)", returnedLength);
}
}

CloseHandle(device);
CloseHandle(hDevice);
_endthreadex(result);
return result;
}



VOID
SendEventInformation(
HANDLE Handle,
Expand Down Expand Up @@ -566,22 +573,27 @@ DispatchUnmount(


// ask driver to release all pending IRP to prepare for Unmount.
BOOL
SendReleaseIRP(
LPCWSTR DeviceName)
BOOL SendReleaseIRP(LPCWSTR DeviceName)
{
ULONG returnedLength;

ULONG returnedLength;
logw(L"send release");

WCHAR rawDeviceName[MAX_PATH];
if (GetRawDeviceName(DeviceName, rawDeviceName, _countof(rawDeviceName)))
{
return FALSE;
}

if (!SendToDevice(
GetRawDeviceName(DeviceName),
IOCTL_EVENT_RELEASE,
NULL,
0,
NULL,
0,
&returnedLength) ) {
rawDeviceName,
IOCTL_EVENT_RELEASE,
NULL,
0,
NULL,
0,
&returnedLength)
)
{

logw(L"Failed to unmount device:%ws\n", DeviceName);
return FALSE;
Expand Down
Loading

0 comments on commit 3919f61

Please sign in to comment.