From ffe037f9f1f00806d4cde52de2d29552fd6b80f9 Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Wed, 22 Jan 2020 07:12:07 +0400 Subject: [PATCH] Fix single instance socket path for compatibility with flatpak --- Telegram/SourceFiles/core/sandbox.cpp | 7 +----- .../platform/linux/specific_linux.cpp | 24 +++++++++++++++++++ .../platform/linux/specific_linux.h | 7 +++--- .../SourceFiles/platform/mac/specific_mac.h | 9 ++----- .../SourceFiles/platform/mac/specific_mac.mm | 8 +++++++ .../SourceFiles/platform/win/specific_win.cpp | 4 ++++ .../SourceFiles/platform/win/specific_win.h | 5 ++-- 7 files changed, 45 insertions(+), 19 deletions(-) diff --git a/Telegram/SourceFiles/core/sandbox.cpp b/Telegram/SourceFiles/core/sandbox.cpp index 762fd66d6..bd43415c7 100644 --- a/Telegram/SourceFiles/core/sandbox.cpp +++ b/Telegram/SourceFiles/core/sandbox.cpp @@ -98,12 +98,7 @@ int Sandbox::start() { const auto d = QFile::encodeName(QDir(cWorkingDir()).absolutePath()); char h[33] = { 0 }; hashMd5Hex(d.constData(), d.size(), h); -#ifndef OS_MAC_STORE - _localServerName = psServerPrefix() + h + '-' + cGUIDStr(); -#else // OS_MAC_STORE - h[4] = 0; // use only first 4 chars - _localServerName = psServerPrefix() + h; -#endif // OS_MAC_STORE + _localServerName = Platform::SingleInstanceLocalServerName(h); connect( &_localSocket, diff --git a/Telegram/SourceFiles/platform/linux/specific_linux.cpp b/Telegram/SourceFiles/platform/linux/specific_linux.cpp index b5dcc8f45..a085956ac 100644 --- a/Telegram/SourceFiles/platform/linux/specific_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/specific_linux.cpp @@ -95,6 +95,13 @@ void SetApplicationIcon(const QIcon &icon) { QApplication::setWindowIcon(icon); } +bool InSandbox() { + static const auto Sandbox = QFileInfo::exists( + QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation) + + qsl("/flatpak-info")); + return Sandbox; +} + QString CurrentExecutablePath(int argc, char *argv[]) { constexpr auto kMaxPath = 1024; char result[kMaxPath] = { 0 }; @@ -112,6 +119,23 @@ QString CurrentExecutablePath(int argc, char *argv[]) { return argc ? QFile::decodeName(argv[0]) : QString(); } +QString SingleInstanceLocalServerName(const QString &hash) { + const auto runtimeDir = QStandardPaths::writableLocation( + QStandardPaths::RuntimeLocation); + + if (InSandbox()) { + return runtimeDir + + qsl("/app/") + + QString::fromUtf8(qgetenv("FLATPAK_ID")) + + '/' + hash; + } else if (QFileInfo::exists(runtimeDir)) { + return runtimeDir + '/' + hash + '-' + cGUIDStr(); + } else { // non-systemd distros + return QStandardPaths::writableLocation(QStandardPaths::TempLocation) + + '/' + hash + '-' + cGUIDStr(); + } +} + } // namespace Platform namespace { diff --git a/Telegram/SourceFiles/platform/linux/specific_linux.h b/Telegram/SourceFiles/platform/linux/specific_linux.h index 1aa1471da..7546f758a 100644 --- a/Telegram/SourceFiles/platform/linux/specific_linux.h +++ b/Telegram/SourceFiles/platform/linux/specific_linux.h @@ -20,8 +20,12 @@ namespace Platform { inline void SetWatchingMediaKeys(bool watching) { } +bool InSandbox(); + QString CurrentExecutablePath(int argc, char *argv[]); +QString SingleInstanceLocalServerName(const QString &hash); + inline std::optional LastUserInputTime() { return std::nullopt; } @@ -31,9 +35,6 @@ inline void IgnoreApplicationActivationRightNow() { } // namespace Platform -inline QString psServerPrefix() { - return qsl("/tmp/"); -} inline void psCheckLocalSocket(const QString &serverName) { QFile address(serverName); if (address.exists()) { diff --git a/Telegram/SourceFiles/platform/mac/specific_mac.h b/Telegram/SourceFiles/platform/mac/specific_mac.h index 38878770d..0d4c4896f 100644 --- a/Telegram/SourceFiles/platform/mac/specific_mac.h +++ b/Telegram/SourceFiles/platform/mac/specific_mac.h @@ -18,6 +18,8 @@ namespace Platform { QString CurrentExecutablePath(int argc, char *argv[]); +QString SingleInstanceLocalServerName(const QString &hash); + void RemoveQuarantine(const QString &path); namespace ThirdParty { @@ -31,13 +33,6 @@ inline void finish() { } // namespace ThirdParty } // namespace Platform -inline QString psServerPrefix() { -#ifndef OS_MAC_STORE - return qsl("/tmp/"); -#else // OS_MAC_STORE - return objc_documentsPath(); -#endif // OS_MAC_STORE -} inline void psCheckLocalSocket(const QString &serverName) { QFile address(serverName); if (address.exists()) { diff --git a/Telegram/SourceFiles/platform/mac/specific_mac.mm b/Telegram/SourceFiles/platform/mac/specific_mac.mm index 6d2ecd541..c8490da50 100644 --- a/Telegram/SourceFiles/platform/mac/specific_mac.mm +++ b/Telegram/SourceFiles/platform/mac/specific_mac.mm @@ -120,6 +120,14 @@ QString CurrentExecutablePath(int argc, char *argv[]) { return NS2QString([[NSBundle mainBundle] bundlePath]); } +QString SingleInstanceLocalServerName(const QString &hash) { +#ifndef OS_MAC_STORE + return qsl("/tmp/") + hash + '-' + cGUIDStr(); +#else // OS_MAC_STORE + return objc_documentsPath() + hash.left(4); +#endif // OS_MAC_STORE +} + void RemoveQuarantine(const QString &path) { const auto kQuarantineAttribute = "com.apple.quarantine"; diff --git a/Telegram/SourceFiles/platform/win/specific_win.cpp b/Telegram/SourceFiles/platform/win/specific_win.cpp index eaa230870..6a4b35a47 100644 --- a/Telegram/SourceFiles/platform/win/specific_win.cpp +++ b/Telegram/SourceFiles/platform/win/specific_win.cpp @@ -325,6 +325,10 @@ QString CurrentExecutablePath(int argc, char *argv[]) { return QString(); } +QString SingleInstanceLocalServerName(const QString &hash) { + return qsl("Global\\") + hash + '-' + cGUIDStr(); +} + std::optional LastUserInputTime() { auto lii = LASTINPUTINFO{ 0 }; lii.cbSize = sizeof(LASTINPUTINFO); diff --git a/Telegram/SourceFiles/platform/win/specific_win.h b/Telegram/SourceFiles/platform/win/specific_win.h index 49a5836d6..330cae4b7 100644 --- a/Telegram/SourceFiles/platform/win/specific_win.h +++ b/Telegram/SourceFiles/platform/win/specific_win.h @@ -21,6 +21,8 @@ inline void SetWatchingMediaKeys(bool watching) { QString CurrentExecutablePath(int argc, char *argv[]); +QString SingleInstanceLocalServerName(const QString &hash); + inline void IgnoreApplicationActivationRightNow() { } @@ -34,9 +36,6 @@ inline void finish() { } // namespace ThirdParty } // namespace Platform -inline QString psServerPrefix() { - return qsl("Global\\"); -} inline void psCheckLocalSocket(const QString &) { }