[SETUPAPI]
[reactos.git] / reactos / dll / win32 / setupapi / misc.c
index 0b79e43..626dc13 100644 (file)
 static const WCHAR BackSlash[] = {'\\',0};
 static const WCHAR TranslationRegKey[] = {'\\','V','e','r','F','i','l','e','I','n','f','o','\\','T','r','a','n','s','l','a','t','i','o','n',0};
 
+/* Handles and critical sections for the SetupLog API */
+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 };
+
 DWORD
 GetFunctionPointer(
     IN PWSTR InstallerName,
@@ -1964,4 +1976,143 @@ BOOL WINAPI SetupTerminateFileLog(HANDLE FileLogHandle)
     SetLastError(ERROR_SUCCESS);
 
     return TRUE;
-}
\ No newline at end of file
+}
+
+/***********************************************************************
+ *      SetupCloseLog(SETUPAPI.@)
+ */
+void WINAPI SetupCloseLog(void)
+{
+    EnterCriticalSection(&setupapi_cs);
+
+    CloseHandle(setupact);
+    setupact = INVALID_HANDLE_VALUE;
+
+    CloseHandle(setuperr);
+    setuperr = INVALID_HANDLE_VALUE;
+
+    LeaveCriticalSection(&setupapi_cs);
+}
+
+/***********************************************************************
+ *      SetupOpenLog(SETUPAPI.@)
+ */
+BOOL WINAPI SetupOpenLog(BOOL reserved)
+{
+    WCHAR path[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);
+        return TRUE;
+    }
+
+    GetWindowsDirectoryW(path, MAX_PATH);
+    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);
+
+    GetWindowsDirectoryW(path, MAX_PATH);
+    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);
+
+    return TRUE;
+}
+
+/***********************************************************************
+ *      SetupLogErrorA(SETUPAPI.@)
+ */
+BOOL WINAPI SetupLogErrorA(LPCSTR message, LogSeverity severity)
+{
+    static const char null[] = "(null)";
+    BOOL ret;
+    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);
+
+    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);
+        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);
+
+    HeapFree(GetProcessHeap(), 0, msg);
+    return ret;
+}