From cfe72ce09a11bfb14d3b9f942e70e3638d6f0460 Mon Sep 17 00:00:00 2001 From: Vasyl Skorych Date: Sun, 14 Jul 2024 16:24:43 +0200 Subject: [PATCH] fix: Permissions for config.ini --- DyssolInstallers/Scripts/Main.iss | 16 ++++++++-------- DyssolMainWindow/Dyssol.cpp | 24 ++++++++++++++---------- DyssolMainWindow/Dyssol.h | 1 - 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/DyssolInstallers/Scripts/Main.iss b/DyssolInstallers/Scripts/Main.iss index d40194b5..f984b7b5 100644 --- a/DyssolInstallers/Scripts/Main.iss +++ b/DyssolInstallers/Scripts/Main.iss @@ -86,9 +86,9 @@ Source: "..\Data\Licenses\*.txt" ; DestDir: "{app}\Licenses" [Dirs] Name: "{app}\Licenses"; Flags: uninsalwaysuninstall -Name: "{autoappdata}\{#MyAppName}"; Flags: uninsalwaysuninstall -Name: "{autoappdata}\{#MyAppName}\{code:DirCache}"; Flags: uninsalwaysuninstall -Name: "{autoappdata}\{#MyAppName}\{code:DirCacheDebug}"; Flags: uninsalwaysuninstall +Name: "{autoappdata}\{#MyAppName}"; Flags: uninsalwaysuninstall; Permissions: users-modify +Name: "{autoappdata}\{#MyAppName}\{code:DirCache}"; Flags: uninsalwaysuninstall; Permissions: users-modify +Name: "{autoappdata}\{#MyAppName}\{code:DirCacheDebug}"; Flags: uninsalwaysuninstall; Permissions: users-modify [Icons] Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}" @@ -113,11 +113,11 @@ Type: filesandordirs; Name: "{autoappdata}\{#MyAppName}\{code:DirCache}" Type: filesandordirs; Name: "{autoappdata}\{#MyAppName}\{code:DirCacheDebug}" [INI] -Filename: "{autoappdata}\{#MyAppName}\{code:FileConfigIni}"; Section: "General"; Key: "modelsFolders"; String: "{code:VarIniModelsFolders}"; Flags: createkeyifdoesntexist -Filename: "{autoappdata}\{#MyAppName}\{code:FileConfigIni}"; Section: "General"; Key: "modelsFoldersActivity"; String: "{code:VarIniFoldersActivity}"; Flags: createkeyifdoesntexist -Filename: "{autoappdata}\{#MyAppName}\{code:FileConfigIni}"; Section: "General"; Key: "materialsDBPath"; String: "{code:MakeRightSlashes|{app}\Materials.dmdb}"; Flags: createkeyifdoesntexist -Filename: "{autoappdata}\{#MyAppName}\{code:FileConfigIni}"; Section: "General"; Key: "cachePath"; String: "{autoappdata}\{#MyAppName}"; Flags: createkeyifdoesntexist -Filename: "{autoappdata}\{#MyAppName}\{code:FileConfigIni}"; Section: "General"; Key: "loadLast"; String: "false"; Flags: createkeyifdoesntexist +Filename: "{autoappdata}\{#MyAppName}\{code:FileConfigIni}"; Section: "General"; Key: "modelsFolders"; String: "{code:VarIniModelsFolders}"; Flags: createkeyifdoesntexist +Filename: "{autoappdata}\{#MyAppName}\{code:FileConfigIni}"; Section: "General"; Key: "modelsFoldersActivity"; String: "{code:VarIniFoldersActivity}"; Flags: createkeyifdoesntexist +Filename: "{autoappdata}\{#MyAppName}\{code:FileConfigIni}"; Section: "General"; Key: "materialsDBPath"; String: "{code:MakeRightSlashes|{app}\Materials.dmdb}"; Flags: createkeyifdoesntexist +Filename: "{autoappdata}\{#MyAppName}\{code:FileConfigIni}"; Section: "General"; Key: "cachePath"; String: "{code:MakeRightSlashes|{autoappdata}\{#MyAppName}}"; Flags: createkeyifdoesntexist +Filename: "{autoappdata}\{#MyAppName}\{code:FileConfigIni}"; Section: "General"; Key: "loadLast"; String: "false"; Flags: createkeyifdoesntexist [Code] procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep); diff --git a/DyssolMainWindow/Dyssol.cpp b/DyssolMainWindow/Dyssol.cpp index 40a2308b..1554a066 100644 --- a/DyssolMainWindow/Dyssol.cpp +++ b/DyssolMainWindow/Dyssol.cpp @@ -33,21 +33,22 @@ Dyssol::Dyssol(QWidget *parent /*= 0*/, Qt::WindowFlags flags /*= {}*/) m_Simulator.SetFlowsheet(&m_Flowsheet); // setup config file + QString settingsPath{}; const auto systemSettingsPath = QFileInfo{ QSettings{ QSettings::IniFormat, QSettings::SystemScope, StrConst::Dyssol_ApplicationName, StrConst::Dyssol_ConfigApp }.fileName() }.absolutePath(); const auto userSettingsPath = QFileInfo{ QSettings{ QSettings::IniFormat, QSettings::UserScope , StrConst::Dyssol_ApplicationName, StrConst::Dyssol_ConfigApp }.fileName() }.absolutePath(); - if (std::filesystem::exists(systemSettingsPath.toStdString())) - m_sSettingsPath = systemSettingsPath; + if (std::filesystem::exists(systemSettingsPath.toStdString()) && !FileSystem::IsWriteProtected(systemSettingsPath.toStdWString())) + settingsPath = systemSettingsPath; else - m_sSettingsPath = userSettingsPath; + settingsPath = userSettingsPath; // create directory for temporary data if it doesn't exist - if (!std::filesystem::exists(m_sSettingsPath.toStdString())) - std::filesystem::create_directories(m_sSettingsPath.toStdString()); + if (!std::filesystem::exists(settingsPath.toStdString())) + std::filesystem::create_directories(settingsPath.toStdString()); - const QString globalConfigFile = m_sSettingsPath + "/" + StrConst::Dyssol_ConfigFileName; + const QString globalConfigFile = settingsPath + "/" + StrConst::Dyssol_ConfigFileName; const QString localConfigFile = QString{ "./" } + StrConst::Dyssol_ConfigFileName; #ifdef _MSC_VER - const QString currConfigFile = QFile::exists(globalConfigFile) || (!QFile::exists(globalConfigFile) && !FileSystem::IsWriteProtected(FileSystem::FilePath(m_sSettingsPath.toStdWString()))) ? + const QString currConfigFile = QFile::exists(globalConfigFile) || (!QFile::exists(globalConfigFile) && !FileSystem::IsWriteProtected(FileSystem::FilePath(settingsPath.toStdWString()))) ? globalConfigFile : localConfigFile; #else const QString currConfigFile = QFile::exists(localConfigFile) ? localConfigFile : globalConfigFile; @@ -334,7 +335,10 @@ void Dyssol::SetupCache() // set the default cache path to config.ini if it has not been set or does not exist const QVariant cachePathVar = m_pSettings->value(StrConst::Dyssol_ConfigCachePath); if (!cachePathVar.isValid() || cachePathVar.toString().isEmpty() || !std::filesystem::exists(cachePathVar.toString().toStdString())) - m_pSettings->setValue(StrConst::Dyssol_ConfigCachePath, m_sSettingsPath); + { + const auto path = QFileInfo{ m_pSettings->fileName() }.absoluteDir().path(); + m_pSettings->setValue(StrConst::Dyssol_ConfigCachePath, path); + } // setup cache path const QString cachePath = m_pSettings->value(StrConst::Dyssol_ConfigCachePath).toString(); @@ -343,8 +347,8 @@ void Dyssol::SetupCache() // check whether the cache path is accessible if (FileSystem::IsWriteProtected(cachePath.toStdWString())) { - m_pSavingThread->Block(); - m_pLoadingThread->Block(); + if (m_pSavingThread) m_pSavingThread->Block(); + if (m_pLoadingThread) m_pLoadingThread->Block(); QMessageBox::critical(this, StrConst::Dyssol_MainWindowName, "Unable to access the selected cache path because it is write-protected:\n'" + cachePath + "'\nPlease choose another path using Tools -> Settings -> Change path...\nSaving/loading of flowsheets is blocked until that."); } diff --git a/DyssolMainWindow/Dyssol.h b/DyssolMainWindow/Dyssol.h index fee7642a..4f4cd1f3 100644 --- a/DyssolMainWindow/Dyssol.h +++ b/DyssolMainWindow/Dyssol.h @@ -61,7 +61,6 @@ class Dyssol : public QMainWindow CSimulator m_Simulator; // simulator QSettings* m_pSettings; // Config file. - QString m_sSettingsPath; // Path to store settings and temporary data, where config file and caches are stored. CMainWindowHelpHelper* m_helpHelper{}; // Fake window to provide main window with functionality to show help files. CCalculationSequenceEditor* m_pCalcSequenceEditor;