https://git.reactos.org/?p=reactos.git;a=commitdiff;h=3ff8adc5537f5b0555708d6f72b635744adcf3ca

commit 3ff8adc5537f5b0555708d6f72b635744adcf3ca
Author:     Whindmar Saksit <whinds...@proton.me>
AuthorDate: Sat Jan 11 19:52:07 2025 +0100
Commit:     GitHub <nore...@github.com>
CommitDate: Sat Jan 11 19:52:07 2025 +0100

    [RAPPS] Protect database update with a mutex (#7006)
---
 base/applications/rapps/gui.cpp         |  4 ++++
 base/applications/rapps/include/misc.h  | 29 +++++++++++++++++++++++++++++
 base/applications/rapps/include/rapps.h |  9 +++++++++
 base/applications/rapps/unattended.cpp  |  3 ++-
 base/applications/rapps/winmain.cpp     |  2 +-
 5 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/base/applications/rapps/gui.cpp b/base/applications/rapps/gui.cpp
index 70cedb28a21..5e8dc85a8ba 100644
--- a/base/applications/rapps/gui.cpp
+++ b/base/applications/rapps/gui.cpp
@@ -304,6 +304,7 @@ CMainWindow::CheckAvailable()
 {
     if (m_Db->GetAvailableCount() == 0)
     {
+        CUpdateDatabaseMutex lock;
         m_Db->RemoveCached();
         m_Db->UpdateAvailable();
     }
@@ -591,9 +592,12 @@ CMainWindow::OnCommand(WPARAM wParam, LPARAM lParam)
                 break;
 
             case ID_RESETDB:
+            {
+                CUpdateDatabaseMutex lock;
                 m_Db->RemoveCached();
                 UpdateApplicationsList(SelectedEnumType, bReload);
                 break;
+            }
 
             case ID_HELP:
                 MessageBoxW(L"Help not implemented yet", NULL, MB_OK);
diff --git a/base/applications/rapps/include/misc.h 
b/base/applications/rapps/include/misc.h
index 4617050855a..f2f3ea727d9 100644
--- a/base/applications/rapps/include/misc.h
+++ b/base/applications/rapps/include/misc.h
@@ -117,3 +117,32 @@ GetProgramFilesPath(CStringW &Path, BOOL PerUser, HWND 
hwnd = NULL);
 template <class T> class CLocalPtr : public CHeapPtr<T, CLocalAllocator>
 {
 };
+
+struct CScopedMutex
+{
+    HANDLE m_hMutex;
+
+    CScopedMutex(LPCWSTR Name, UINT Timeout = INFINITE, BOOL InitialOwner = 
FALSE)
+    {
+        m_hMutex = CreateMutexW(NULL, InitialOwner, Name);
+        if (m_hMutex && !InitialOwner)
+        {
+            DWORD wait = WaitForSingleObject(m_hMutex, Timeout);
+            if (wait != WAIT_OBJECT_0 && wait != WAIT_ABANDONED)
+            {
+                CloseHandle(m_hMutex);
+                m_hMutex = NULL;
+            }
+        }
+    }
+    ~CScopedMutex()
+    {
+        if (m_hMutex)
+        {
+            ReleaseMutex(m_hMutex);
+            CloseHandle(m_hMutex);
+        }
+    }
+
+    bool Acquired() const { return m_hMutex != NULL; }
+};
diff --git a/base/applications/rapps/include/rapps.h 
b/base/applications/rapps/include/rapps.h
index 49bb4691f09..c03a06d092d 100644
--- a/base/applications/rapps/include/rapps.h
+++ b/base/applications/rapps/include/rapps.h
@@ -17,4 +17,13 @@ extern LONG g_Busy;
 
 #define WM_NOTIFY_OPERATIONCOMPLETED (WM_APP + 0)
 
+#define MAINWINDOWCLASSNAME L"ROSAPPMGR2"
+#define MAINWINDOWMUTEX szWindowClass
+#define UPDATEDBMUTEX ( MAINWINDOWCLASSNAME L":UpDB" )
+
+struct CUpdateDatabaseMutex : public CScopedMutex
+{
+    CUpdateDatabaseMutex() : CScopedMutex(UPDATEDBMUTEX, 1000 * 60 * 10, 
FALSE) { };
+};
+
 #endif /* _RAPPS_H */
diff --git a/base/applications/rapps/unattended.cpp 
b/base/applications/rapps/unattended.cpp
index 12ffe79254a..4967f259de3 100644
--- a/base/applications/rapps/unattended.cpp
+++ b/base/applications/rapps/unattended.cpp
@@ -338,6 +338,7 @@ ParseCmdAndExecute(LPWSTR lpCmdLine, BOOL bIsFirstLaunch, 
int nCmdShow)
     BOOL bAppwizMode = (argc > 1 && MatchCmdOption(argv[1], CMD_KEY_APPWIZ));
     if (!bAppwizMode)
     {
+        CUpdateDatabaseMutex lock;
         if (SettingsInfo.bUpdateAtStart || bIsFirstLaunch)
             db.RemoveCached();
 
@@ -350,7 +351,7 @@ ParseCmdAndExecute(LPWSTR lpCmdLine, BOOL bIsFirstLaunch, 
int nCmdShow)
     {
         // Check whether the RAPPS MainWindow is already launched in another 
process
         CStringW szWindowText(MAKEINTRESOURCEW(bAppwizMode ? IDS_APPWIZ_TITLE 
: IDS_APPTITLE));
-        LPCWSTR pszMutex = bAppwizMode ? L"RAPPWIZ" : szWindowClass;
+        LPCWSTR pszMutex = bAppwizMode ? L"RAPPWIZ" : MAINWINDOWMUTEX;
 
         HANDLE hMutex = CreateMutexW(NULL, FALSE, pszMutex);
         if ((!hMutex) || (GetLastError() == ERROR_ALREADY_EXISTS))
diff --git a/base/applications/rapps/winmain.cpp 
b/base/applications/rapps/winmain.cpp
index 2c5c06a300f..70ce317a468 100644
--- a/base/applications/rapps/winmain.cpp
+++ b/base/applications/rapps/winmain.cpp
@@ -13,7 +13,7 @@
 #include <gdiplus.h>
 #include <conutils.h>
 
-LPCWSTR szWindowClass = L"ROSAPPMGR2";
+LPCWSTR szWindowClass = MAINWINDOWCLASSNAME;
 LONG g_Busy = 0;
 
 HWND hMainWnd;

Reply via email to