From: Pierre Schweitzer Subject: setupapi: Implement SetupOpenLog(), SetupLogErrorA(), SetupLogErrorW(), SetupCloseLog() (try 7) Message-Id: <550C9232.2040701@reactos.org> Date: Fri, 20 Mar 2015 22:33:38 +0100 Dear all, Please find my new iteration on that patch. It's been kindly reviewed on IRC. And properly tested against Windows for proper behavior. Cheers, -- Pierre Schweitzer System & Network Administrator Senior Kernel Developer ReactOS Deutschland e.V. From dcfcb284768af9be1b081500eb12cb0a2114cd16 Mon Sep 17 00:00:00 2001 From: Pierre Schweitzer Date: Thu, 12 Mar 2015 17:56:06 +0100 Subject: [PATCH] setupapi: Implement SetupOpenLog(), SetupLogErrorA(), SetupLogErrorW(), SetupCloseLog() --- dlls/setupapi/Makefile.in | 1 + dlls/setupapi/log.c | 183 ++++++++++++++++++++++++++++++++++++++++ dlls/setupapi/setupapi.spec | 2 +- dlls/setupapi/setupcab.c | 1 + dlls/setupapi/stubs.c | 26 ------ dlls/setupapi/tests/Makefile.in | 1 + dlls/setupapi/tests/log.c | 68 +++++++++++++++ 7 files changed, 255 insertions(+), 27 deletions(-) create mode 100644 dlls/setupapi/log.c create mode 100644 dlls/setupapi/tests/log.c diff --git a/dlls/setupapi/Makefile.in b/dlls/setupapi/Makefile.in index bf157cb..030b2d3 100644 --- a/dlls/setupapi/Makefile.in +++ b/dlls/setupapi/Makefile.in @@ -11,6 +11,7 @@ C_SRCS = \ diskspace.c \ fakedll.c \ install.c \ + log.c \ misc.c \ parser.c \ query.c \ diff --git a/dlls/setupapi/log.c b/dlls/setupapi/log.c new file mode 100644 index 0000000..53103fb --- /dev/null +++ b/dlls/setupapi/log.c @@ -0,0 +1,183 @@ +/* + * SetupAPI logs functions + * + * Copyright 2015 Pierre Schweitzer + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include "wine/debug.h" +#include "windef.h" +#include "winbase.h" +#include "winuser.h" +#include "winreg.h" +#include "setupapi.h" +#include "winnls.h" + +static HANDLE setupact = INVALID_HANDLE_VALUE; +static HANDLE setuperr = INVALID_HANDLE_VALUE; +static CRITICAL_SECTION setupapi_cs; +static CRITICAL_SECTION_DEBUG critsect_debug = +{ + 0, 0, &setupapi_cs, + { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList }, + 0, 0, { (DWORD_PTR)(__FILE__ ": setupapi_cs") } +}; +static CRITICAL_SECTION setupapi_cs = { &critsect_debug, -1, 0, 0, 0, 0 }; + +/*********************************************************************** + * SetupCloseLog(SETUPAPI.@) + */ +void WINAPI SetupCloseLog(void) +{ + EnterCriticalSection(&setupapi_cs); + + CloseHandle(setupact); + setupact = INVALID_HANDLE_VALUE; + + CloseHandle(setuperr); + setuperr = INVALID_HANDLE_VALUE; + + LeaveCriticalSection(&setupapi_cs); +} + +/*********************************************************************** + * SetupLogErrorA(SETUPAPI.@) + */ +BOOL WINAPI SetupLogErrorA(LPCSTR message, LogSeverity severity) +{ + BOOL ret; + static const CHAR null[] = {'(','n','u','l','l',')',0}; + DWORD written; + DWORD len; + + EnterCriticalSection(&setupapi_cs); + + if (setupact == INVALID_HANDLE_VALUE || setuperr == INVALID_HANDLE_VALUE) + { + SetLastError(ERROR_FILE_INVALID); + ret = FALSE; + goto done; + } + + if (message == NULL) + message = null; + + len = lstrlenA(message) * sizeof(CHAR); + + ret = WriteFile(setupact, message, len, &written, NULL); + if (!ret) + goto done; + + if (severity >= LogSevMaximum) + { + ret = FALSE; + goto done; + } + + if (severity > LogSevInformation) + ret = WriteFile(setuperr, message, len, &written, NULL); + +done: + LeaveCriticalSection(&setupapi_cs); + return ret; +} + +/*********************************************************************** + * SetupLogErrorW(SETUPAPI.@) + */ +BOOL WINAPI SetupLogErrorW(LPCWSTR message, LogSeverity severity) +{ + LPSTR msg = NULL; + DWORD len; + BOOL ret; + + if (message) + { + len = WideCharToMultiByte(CP_ACP, 0, message, -1, NULL, 0, NULL, NULL); + msg = HeapAlloc(GetProcessHeap(), 0, len * sizeof(CHAR)); + if (msg == NULL) + { + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + return FALSE; + } + WideCharToMultiByte(CP_ACP, 0, message, -1, msg, len, NULL, NULL); + } + + /* This is the normal way to proceed. The log files are ASCII files + * and W is to be converted. + */ + ret = SetupLogErrorA(msg, severity); + + if (msg) + HeapFree(GetProcessHeap(), 0, msg); + + return ret; +} + +/*********************************************************************** + * SetupOpenLog(SETUPAPI.@) + */ +BOOL WINAPI SetupOpenLog(BOOL reserved) +{ + WCHAR path[MAX_PATH]; + WCHAR win[MAX_PATH]; + + static const WCHAR setupactlog[] = {'\\','s','e','t','u','p','a','c','t','.','l','o','g',0}; + static const WCHAR setuperrlog[] = {'\\','s','e','t','u','p','e','r','r','.','l','o','g',0}; + + EnterCriticalSection(&setupapi_cs); + + if (setupact != INVALID_HANDLE_VALUE && setuperr != INVALID_HANDLE_VALUE) + { + LeaveCriticalSection(&setupapi_cs); + SetLastError(ERROR_ALREADY_INITIALIZED); + return TRUE; + } + + GetWindowsDirectoryW(win, MAX_PATH); + lstrcpyW(path, win); + lstrcatW(path, setupactlog); + + setupact = CreateFileW(path, FILE_GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, + NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (setupact == INVALID_HANDLE_VALUE) + { + LeaveCriticalSection(&setupapi_cs); + return FALSE; + } + + SetFilePointer(setupact, 0, NULL, FILE_END); + + lstrcpyW(path, win); + lstrcatW(path, setuperrlog); + + setuperr = CreateFileW(path, FILE_GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, + NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (setuperr == INVALID_HANDLE_VALUE) + { + CloseHandle(setupact); + setupact = INVALID_HANDLE_VALUE; + LeaveCriticalSection(&setupapi_cs); + return FALSE; + } + + SetFilePointer(setuperr, 0, NULL, FILE_END); + + LeaveCriticalSection(&setupapi_cs); + + SetLastError(ERROR_ALREADY_EXISTS); + return TRUE; +} diff --git a/dlls/setupapi/setupapi.spec b/dlls/setupapi/setupapi.spec index 4fcf298..c1dbed5 100644 --- a/dlls/setupapi/setupapi.spec +++ b/dlls/setupapi/setupapi.spec @@ -459,7 +459,7 @@ @ stdcall SetupInstallServicesFromInfSectionW(long wstr long) @ stdcall SetupIterateCabinetA(str long ptr ptr) @ stdcall SetupIterateCabinetW(wstr long ptr ptr) -@ stub SetupLogErrorA +@ stdcall SetupLogErrorA(str long) @ stdcall SetupLogErrorW(wstr long) @ stdcall SetupLogFileA(ptr str str str long str str str long) @ stdcall SetupLogFileW(ptr wstr wstr wstr long wstr wstr wstr long) diff --git a/dlls/setupapi/setupcab.c b/dlls/setupapi/setupcab.c index 21336d7..54c0256 100644 --- a/dlls/setupapi/setupcab.c +++ b/dlls/setupapi/setupcab.c @@ -694,6 +694,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) break; case DLL_PROCESS_DETACH: if (lpvReserved) break; + SetupCloseLog(); if (CABINET_hInstance) FreeLibrary(CABINET_hInstance); break; } diff --git a/dlls/setupapi/stubs.c b/dlls/setupapi/stubs.c index 673bbbf..a57f5fe 100644 --- a/dlls/setupapi/stubs.c +++ b/dlls/setupapi/stubs.c @@ -189,32 +189,6 @@ BOOL WINAPI RegistryDelnode(DWORD x, DWORD y) } /*********************************************************************** - * SetupCloseLog(SETUPAPI.@) - */ -void WINAPI SetupCloseLog(void) -{ - FIXME("() stub\n"); -} - -/*********************************************************************** - * SetupLogErrorW(SETUPAPI.@) - */ -BOOL WINAPI SetupLogErrorW(LPCWSTR MessageString, LogSeverity Severity) -{ - FIXME("(%s, %d) stub\n", debugstr_w(MessageString), Severity); - return TRUE; -} - -/*********************************************************************** - * SetupOpenLog(SETUPAPI.@) - */ -BOOL WINAPI SetupOpenLog(BOOL Reserved) -{ - FIXME("(%d) stub\n", Reserved); - return TRUE; -} - -/*********************************************************************** * SetupPromptReboot(SETUPAPI.@) */ INT WINAPI SetupPromptReboot( HSPFILEQ file_queue, HWND owner, BOOL scan_only ) diff --git a/dlls/setupapi/tests/Makefile.in b/dlls/setupapi/tests/Makefile.in index 30902e7..32ce41f 100644 --- a/dlls/setupapi/tests/Makefile.in +++ b/dlls/setupapi/tests/Makefile.in @@ -5,6 +5,7 @@ C_SRCS = \ devinst.c \ diskspace.c \ install.c \ + log.c \ misc.c \ parser.c \ query.c \ diff --git a/dlls/setupapi/tests/log.c b/dlls/setupapi/tests/log.c new file mode 100644 index 0000000..5de009e --- /dev/null +++ b/dlls/setupapi/tests/log.c @@ -0,0 +1,68 @@ +/* + * Unit tests for setupapi.dll log functions + * + * Copyright (C) 2015 Pierre Schweitzer + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include +#include +#include +#include "wine/test.h" + +static void test_SetupLogError(void) +{ + BOOL ret; + DWORD error; + + SetLastError(0xdeadbeef); + ret = SetupLogErrorA("Test without opening\r\n", LogSevInformation); + error = GetLastError(); + ok(!ret, "SetupLogError succeeded\n"); + ok(error == ERROR_FILE_INVALID, "got wrong error: %d\n", error); + + SetLastError(0xdeadbeef); + ret = SetupOpenLog(FALSE); + error = GetLastError(); + ok(ret, "SetupOpenLog failed\n"); + ok(error == ERROR_ALREADY_EXISTS, "got wrong error: %d\n", error); /* Even if log file didn't exist */ + + SetLastError(0xdeadbeef); + ret = SetupLogErrorA("Test with wrong log severity\r\n", LogSevMaximum); + error = GetLastError(); + ok(!ret, "SetupLogError succeeded\n"); + ok(error == 0xdeadbeef, "got wrong error: %d\n", error); + ret = SetupLogErrorA("Test without EOL", LogSevInformation); + ok(ret, "SetupLogError failed\n"); + + SetLastError(0xdeadbeef); + ret = SetupLogErrorA(NULL, LogSevInformation); + ok(ret || broken(!ret && GetLastError() == ERROR_INVALID_PARAMETER /* Win Vista+ */), + "SetupLogError failed: %08x\n", GetLastError()); + + SetLastError(0xdeadbeef); + ret = SetupOpenLog(FALSE); + error = GetLastError(); + ok(ret, "SetupOpenLog failed\n"); + ok(error == ERROR_ALREADY_INITIALIZED, "got wrong error: %d\n", error); + + SetupCloseLog(); +} + +START_TEST(log) +{ + test_SetupLogError(); +} -- 1.9.1