From 57bb72857fab367e3f41a3a33a7a0ad7b5ee096b Mon Sep 17 00:00:00 2001 From: Kerber Date: Sun, 15 Sep 2019 03:47:38 +0300 Subject: [PATCH 1/5] Added log redirection --- dokan/dokan.c | 20 ++++++++++++++++++-- dokan/dokan.h | 11 ++++++++++- dokan/dokanc.h | 31 +++++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/dokan/dokan.c b/dokan/dokan.c index f15f1dc5c..885874198 100644 --- a/dokan/dokan.c +++ b/dokan/dokan.c @@ -84,6 +84,9 @@ volatile PDokanMalloc g_DokanMalloc = NULL; volatile PDokanFree g_DokanFree = NULL; volatile PDokanRealloc g_DokanRealloc = NULL; +volatile PDokanDbgPrint g_PDokanDbgPrint = NULL; +volatile PDokanDbgPrintW g_PDokanDbgPrintW = NULL; + volatile LONG g_DokanInitialized = 0; // TODO NEXT: @@ -2016,8 +2019,8 @@ DOKAN_API PTP_POOL DOKAN_CALLBACK DokanGetThreadPool() { return threadPool; } -void DOKANAPI DokanInit(DOKAN_MEMORY_CALLBACKS *memoryCallbacks) { - +void DOKANAPI DokanInit(DOKAN_MEMORY_CALLBACKS* memoryCallbacks, DOKAN_LOG_CALLBACKS* logCallbacks) +{ // ensure 64-bit alignment assert(offsetof(EVENT_INFORMATION, Buffer) % 8 == 0); @@ -2050,6 +2053,19 @@ void DOKANAPI DokanInit(DOKAN_MEMORY_CALLBACKS *memoryCallbacks) { #endif } + if (logCallbacks) + { +#if INTPTR_MAX == INT64_MAX + InterlockedExchange64((volatile LONG64*)&g_PDokanDbgPrint, (LONG64)logCallbacks->DbgPrint); + InterlockedExchange64((volatile LONG64*)&g_PDokanDbgPrintW, (LONG64)logCallbacks->DbgPrintW); +#elif INTPTR_MAX == INT32_MAX + InterlockedExchange((volatile LONG*)&g_PDokanDbgPrint, (LONG)logCallbacks->DbgPrint); + InterlockedExchange((volatile LONG*)&g_PDokanDbgPrintW, (LONG)logCallbacks->DbgPrintW); +#else + #error Unsupported architecture! +#endif + } + #if _MSC_VER < 1300 InitializeCriticalSection(&g_InstanceCriticalSection); InitializeCriticalSection(&g_EventBufferCriticalSection); diff --git a/dokan/dokan.h b/dokan/dokan.h index 8df50ee1c..6e4d04097 100644 --- a/dokan/dokan.h +++ b/dokan/dokan.h @@ -227,12 +227,21 @@ typedef void* (WINAPI *PDokanMalloc)(size_t size, const char *fileName, int line typedef void (WINAPI *PDokanFree)(void *userData); typedef void* (WINAPI *PDokanRealloc)(void *userData, size_t newSize, const char *fileName, int lineNumber); +typedef BOOL (WINAPI *PDokanDbgPrint)(LPCSTR logString); +typedef BOOL (WINAPI *PDokanDbgPrintW)(LPCWSTR logString); + typedef struct _DOKAN_MEMORY_CALLBACKS { PDokanMalloc Malloc; PDokanFree Free; PDokanRealloc Realloc; } DOKAN_MEMORY_CALLBACKS, *PDOKAN_MEMORY_CALLBACKS; +typedef struct _DOKAN_LOG_CALLBACKS +{ + PDokanDbgPrint DbgPrint; + PDokanDbgPrintW DbgPrintW; +} DOKAN_LOG_CALLBACKS, *PDOKAN_LOG_CALLBACKS; + #define DOKAN_EXCEPTION_NOT_INITIALIZED 0x0f0ff0ff #define DOKAN_EXCEPTION_INITIALIZATION_FAILED 0x0fbadbad #define DOKAN_EXCEPTION_SHUTDOWN_FAILED 0x0fbadf00 @@ -1102,7 +1111,7 @@ void DOKANAPI DokanEndDispatchSetFileSecurity(DOKAN_SET_FILE_SECURITY_EVENT *Eve DOKAN_API PTP_POOL DOKAN_CALLBACK DokanGetThreadPool(); // Init/shutdown -void DOKANAPI DokanInit(DOKAN_MEMORY_CALLBACKS *memoryCallbacks); +void DOKANAPI DokanInit(DOKAN_MEMORY_CALLBACKS* memoryCallbacks, DOKAN_LOG_CALLBACKS* logCallbacks); void DOKANAPI DokanShutdown(); /** @} */ diff --git a/dokan/dokanc.h b/dokan/dokanc.h index 6be4e7a09..fdbfa15e0 100644 --- a/dokan/dokanc.h +++ b/dokan/dokanc.h @@ -52,6 +52,9 @@ extern BOOL g_DebugMode; // DokanOptions->UseStdErr is ON? extern BOOL g_UseStdErr; +extern volatile PDokanDbgPrint g_PDokanDbgPrint; +extern volatile PDokanDbgPrintW g_PDokanDbgPrintW; + #ifdef _MSC_VER static VOID DokanDbgPrint(LPCSTR format, ...) { @@ -71,10 +74,18 @@ static VOID DokanDbgPrint(LPCSTR format, ...) { outputString = buffer; } - OutputDebugStringA(outputString); + BOOL allowStandardPrint = FALSE; + if (g_PDokanDbgPrint) + { + allowStandardPrint = g_PDokanDbgPrint(outputString); + } + if (allowStandardPrint) + { + OutputDebugStringA(outputString); + } - if(g_UseStdErr) { - + if(g_UseStdErr && allowStandardPrint) + { fputs(outputString, stderr); fflush(stderr); } @@ -103,10 +114,18 @@ static VOID DokanDbgPrintW(LPCWSTR format, ...) { outputString = buffer; } - OutputDebugStringW(outputString); - - if(g_UseStdErr) { + BOOL allowStandardPrint = FALSE; + if (g_PDokanDbgPrint) + { + allowStandardPrint = g_PDokanDbgPrintW(outputString); + } + if (allowStandardPrint) + { + OutputDebugStringW(outputString); + } + if(g_UseStdErr && allowStandardPrint) + { fputws(outputString, stderr); fflush(stderr); } From a08cad1a0ff4a969f97eedd57ee1b65a1c1b0c08 Mon Sep 17 00:00:00 2001 From: Kerber Date: Mon, 4 Nov 2019 19:36:02 +0300 Subject: [PATCH 2/5] Added log redirection (code cleanup) --- dokan/dokan.c | 8 +++----- dokan/dokanc.h | 23 ++++++++--------------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/dokan/dokan.c b/dokan/dokan.c index 885874198..ebe95ceb1 100644 --- a/dokan/dokan.c +++ b/dokan/dokan.c @@ -2019,8 +2019,7 @@ DOKAN_API PTP_POOL DOKAN_CALLBACK DokanGetThreadPool() { return threadPool; } -void DOKANAPI DokanInit(DOKAN_MEMORY_CALLBACKS* memoryCallbacks, DOKAN_LOG_CALLBACKS* logCallbacks) -{ +void DOKANAPI DokanInit(DOKAN_MEMORY_CALLBACKS* memoryCallbacks, DOKAN_LOG_CALLBACKS* logCallbacks) { // ensure 64-bit alignment assert(offsetof(EVENT_INFORMATION, Buffer) % 8 == 0); @@ -2053,8 +2052,7 @@ void DOKANAPI DokanInit(DOKAN_MEMORY_CALLBACKS* memoryCallbacks, DOKAN_LOG_CALLB #endif } - if (logCallbacks) - { + if (logCallbacks) { #if INTPTR_MAX == INT64_MAX InterlockedExchange64((volatile LONG64*)&g_PDokanDbgPrint, (LONG64)logCallbacks->DbgPrint); InterlockedExchange64((volatile LONG64*)&g_PDokanDbgPrintW, (LONG64)logCallbacks->DbgPrintW); @@ -2062,7 +2060,7 @@ void DOKANAPI DokanInit(DOKAN_MEMORY_CALLBACKS* memoryCallbacks, DOKAN_LOG_CALLB InterlockedExchange((volatile LONG*)&g_PDokanDbgPrint, (LONG)logCallbacks->DbgPrint); InterlockedExchange((volatile LONG*)&g_PDokanDbgPrintW, (LONG)logCallbacks->DbgPrintW); #else - #error Unsupported architecture! +#error Unsupported architecture! #endif } diff --git a/dokan/dokanc.h b/dokan/dokanc.h index fdbfa15e0..d13fb2226 100644 --- a/dokan/dokanc.h +++ b/dokan/dokanc.h @@ -59,6 +59,7 @@ extern volatile PDokanDbgPrintW g_PDokanDbgPrintW; static VOID DokanDbgPrint(LPCSTR format, ...) { + BOOL allowStandardPrint = TRUE; const char *outputString = format; // fallback char *buffer = NULL; int length; @@ -74,18 +75,14 @@ static VOID DokanDbgPrint(LPCSTR format, ...) { outputString = buffer; } - BOOL allowStandardPrint = FALSE; - if (g_PDokanDbgPrint) - { + if (g_PDokanDbgPrint) { allowStandardPrint = g_PDokanDbgPrint(outputString); } - if (allowStandardPrint) - { + if (allowStandardPrint) { OutputDebugStringA(outputString); } - if(g_UseStdErr && allowStandardPrint) - { + if(g_UseStdErr && allowStandardPrint) { fputs(outputString, stderr); fflush(stderr); } @@ -99,6 +96,7 @@ static VOID DokanDbgPrint(LPCSTR format, ...) { static VOID DokanDbgPrintW(LPCWSTR format, ...) { + BOOL allowStandardPrint = TRUE; const WCHAR *outputString = format; // fallback WCHAR *buffer = NULL; int length; @@ -114,24 +112,19 @@ static VOID DokanDbgPrintW(LPCWSTR format, ...) { outputString = buffer; } - BOOL allowStandardPrint = FALSE; - if (g_PDokanDbgPrint) - { + if (g_PDokanDbgPrint) { allowStandardPrint = g_PDokanDbgPrintW(outputString); } - if (allowStandardPrint) - { + if (allowStandardPrint) { OutputDebugStringW(outputString); } - if(g_UseStdErr && allowStandardPrint) - { + if(g_UseStdErr && allowStandardPrint) { fputws(outputString, stderr); fflush(stderr); } if(buffer) { - _freea(buffer); } From 25deb04d19a73c148ea009ddf4c5590ad667cb77 Mon Sep 17 00:00:00 2001 From: Kerber Date: Mon, 4 Nov 2019 19:58:20 +0300 Subject: [PATCH 3/5] Fixed second parameter to DokanInit --- dokan_control/dokanctl.c | 2 +- samples/dokan_mirror/mirror.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dokan_control/dokanctl.c b/dokan_control/dokanctl.c index 7b9244dc4..544d5d3f3 100644 --- a/dokan_control/dokanctl.c +++ b/dokan_control/dokanctl.c @@ -114,7 +114,7 @@ int __cdecl wmain(int argc, PWCHAR argv[]) { PVOID wow64OldValue; BOOL isAdmin; - DokanInit(NULL); + DokanInit(NULL, NULL); isAdmin = IsUserAnAdmin(); DokanUseStdErr(TRUE); // Set dokan library debug output diff --git a/samples/dokan_mirror/mirror.c b/samples/dokan_mirror/mirror.c index ea583a27b..01f2d2789 100644 --- a/samples/dokan_mirror/mirror.c +++ b/samples/dokan_mirror/mirror.c @@ -2820,7 +2820,7 @@ int __cdecl wmain(ULONG argc, PWCHAR argv[]) { memoryCallbacks.Free = MirrorFree; memoryCallbacks.Realloc = MirrorRealloc; - DokanInit(&memoryCallbacks); + DokanInit(&memoryCallbacks, NULL); #else From fbdda9ff6ef5e92cf29ba12f18215490bc2f506c Mon Sep 17 00:00:00 2001 From: Kerber Date: Mon, 4 Nov 2019 20:31:10 +0300 Subject: [PATCH 4/5] Fixed second parameter to DokanInit in Mirror sample and FUSE --- dokan_fuse/src/dokanfuse.cpp | 4 ++-- samples/dokan_mirror/mirror.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dokan_fuse/src/dokanfuse.cpp b/dokan_fuse/src/dokanfuse.cpp index 39fe97d05..21b15aaa3 100644 --- a/dokan_fuse/src/dokanfuse.cpp +++ b/dokan_fuse/src/dokanfuse.cpp @@ -634,7 +634,7 @@ bool fuse_chan::init() { // check version typedef ULONG(__stdcall * DokanVersionType)(); - typedef void(__stdcall *DokanInitType)(DOKAN_MEMORY_CALLBACKS *memoryCallbacks); + typedef void(__stdcall *DokanInitType)(DOKAN_MEMORY_CALLBACKS *memoryCallbacks, DOKAN_LOG_CALLBACKS *logCallbacks); DokanVersionType ResolvedDokanVersion; DokanInitType ResolvedInit; @@ -647,7 +647,7 @@ bool fuse_chan::init() { return false; } - ResolvedInit(NULL); + ResolvedInit(NULL, NULL); ResolvedDokanVersion = (DokanVersionType)GetProcAddress(dokanDll, "DokanVersion"); diff --git a/samples/dokan_mirror/mirror.c b/samples/dokan_mirror/mirror.c index 01f2d2789..9c2b846a6 100644 --- a/samples/dokan_mirror/mirror.c +++ b/samples/dokan_mirror/mirror.c @@ -2824,7 +2824,7 @@ int __cdecl wmain(ULONG argc, PWCHAR argv[]) { #else - DokanInit(NULL); + DokanInit(NULL, NULL); #endif From 051c8368f94037ffd0da70db0a436a7cfaaa0f70 Mon Sep 17 00:00:00 2001 From: Kerber Date: Tue, 5 Nov 2019 00:15:11 +0300 Subject: [PATCH 5/5] User provided logging function fully suppresses standard library behavior --- dokan/dokan.h | 4 ++-- dokan/dokanc.h | 30 ++++++++++++------------------ 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/dokan/dokan.h b/dokan/dokan.h index 6e4d04097..7697f1eba 100644 --- a/dokan/dokan.h +++ b/dokan/dokan.h @@ -227,8 +227,8 @@ typedef void* (WINAPI *PDokanMalloc)(size_t size, const char *fileName, int line typedef void (WINAPI *PDokanFree)(void *userData); typedef void* (WINAPI *PDokanRealloc)(void *userData, size_t newSize, const char *fileName, int lineNumber); -typedef BOOL (WINAPI *PDokanDbgPrint)(LPCSTR logString); -typedef BOOL (WINAPI *PDokanDbgPrintW)(LPCWSTR logString); +typedef void (WINAPI *PDokanDbgPrint)(LPCSTR logString); +typedef void (WINAPI *PDokanDbgPrintW)(LPCWSTR logString); typedef struct _DOKAN_MEMORY_CALLBACKS { PDokanMalloc Malloc; diff --git a/dokan/dokanc.h b/dokan/dokanc.h index d13fb2226..cab264c49 100644 --- a/dokan/dokanc.h +++ b/dokan/dokanc.h @@ -59,7 +59,6 @@ extern volatile PDokanDbgPrintW g_PDokanDbgPrintW; static VOID DokanDbgPrint(LPCSTR format, ...) { - BOOL allowStandardPrint = TRUE; const char *outputString = format; // fallback char *buffer = NULL; int length; @@ -76,17 +75,15 @@ static VOID DokanDbgPrint(LPCSTR format, ...) { } if (g_PDokanDbgPrint) { - allowStandardPrint = g_PDokanDbgPrint(outputString); - } - if (allowStandardPrint) { + g_PDokanDbgPrint(outputString); + } else { OutputDebugStringA(outputString); + if (g_UseStdErr) { + fputs(outputString, stderr); + fflush(stderr); + } } - if(g_UseStdErr && allowStandardPrint) { - fputs(outputString, stderr); - fflush(stderr); - } - if(buffer) { _freea(buffer); } @@ -96,7 +93,6 @@ static VOID DokanDbgPrint(LPCSTR format, ...) { static VOID DokanDbgPrintW(LPCWSTR format, ...) { - BOOL allowStandardPrint = TRUE; const WCHAR *outputString = format; // fallback WCHAR *buffer = NULL; int length; @@ -113,15 +109,13 @@ static VOID DokanDbgPrintW(LPCWSTR format, ...) { } if (g_PDokanDbgPrint) { - allowStandardPrint = g_PDokanDbgPrintW(outputString); - } - if (allowStandardPrint) { + g_PDokanDbgPrintW(outputString); + } else { OutputDebugStringW(outputString); - } - - if(g_UseStdErr && allowStandardPrint) { - fputws(outputString, stderr); - fflush(stderr); + if (g_UseStdErr) { + fputws(outputString, stderr); + fflush(stderr); + } } if(buffer) {