Improve current executable path computing.

Fixes #960 (hopefully).
This commit is contained in:
John Preston 2017-08-15 20:12:57 +03:00
parent f2801d4775
commit 8e433971c9
13 changed files with 406 additions and 375 deletions

View File

@ -56,6 +56,7 @@ string updaterDir;
string updaterName; string updaterName;
string workDir; string workDir;
string exeName; string exeName;
string exePath;
FILE *_logFile = 0; FILE *_logFile = 0;
void openLog() { void openLog() {
@ -85,7 +86,9 @@ void closeLog() {
} }
void writeLog(const char *format, ...) { void writeLog(const char *format, ...) {
if (!_logFile) return; if (!_logFile) {
return;
}
va_list args; va_list args;
va_start(args, format); va_start(args, format);
@ -243,7 +246,7 @@ bool update() {
string dir = dirs.front(); string dir = dirs.front();
dirs.pop_front(); dirs.pop_front();
string toDir = updaterDir; string toDir = exePath;
if (dir.size() > updDir.size() + 1) { if (dir.size() > updDir.size() + 1) {
toDir += (dir.substr(updDir.size() + 1) + '/'); toDir += (dir.substr(updDir.size() + 1) + '/');
forcedirs.push_back(toDir); forcedirs.push_back(toDir);
@ -269,13 +272,13 @@ bool update() {
dirs.push_back(fname); dirs.push_back(fname);
writeLog("Added dir '%s' in update tree..", fname.c_str()); writeLog("Added dir '%s' in update tree..", fname.c_str());
} else { } else {
string tofname = updaterDir + fname.substr(updDir.size() + 1); string tofname = exePath + fname.substr(updDir.size() + 1);
if (equal(tofname, updaterName)) { // bad update - has Updater - delete all dir if (equal(tofname, updaterName)) { // bad update - has Updater - delete all dir
writeLog("Error: bad update, has Updater! '%s' equal '%s'", tofname.c_str(), updaterName.c_str()); writeLog("Error: bad update, has Updater! '%s' equal '%s'", tofname.c_str(), updaterName.c_str());
delFolder(); delFolder();
return false; return false;
} else if (equal(tofname, updaterDir + "Telegram") && exeName != "Telegram") { } else if (equal(tofname, exePath + "Telegram") && exeName != "Telegram") {
string fullBinaryPath = updaterDir + exeName; string fullBinaryPath = exePath + exeName;
writeLog("Target binary found: '%s', changing to '%s'", tofname.c_str(), fullBinaryPath.c_str()); writeLog("Target binary found: '%s', changing to '%s'", tofname.c_str(), fullBinaryPath.c_str());
tofname = fullBinaryPath; tofname = fullBinaryPath;
} }
@ -328,6 +331,18 @@ bool update() {
return true; return true;
} }
string CurrentExecutablePath(int argc, char *argv[]) {
constexpr auto kMaxPath = 1024;
char result[kMaxPath] = { 0 };
auto count = readlink("/proc/self/exe", result, kMaxPath);
if (count > 0) {
return string(result);
}
// Fallback to the first command line argument.
return argc ? string(argv[0]) : string();
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
bool needupdate = true, autostart = false, debug = false, tosettings = false, startintray = false, testmode = false; bool needupdate = true, autostart = false, debug = false, tosettings = false, startintray = false, testmode = false;
@ -353,6 +368,8 @@ int main(int argc, char *argv[]) {
crashreport = argv[i]; crashreport = argv[i];
} else if (equal(argv[i], "-exename") && ++i < argc) { } else if (equal(argv[i], "-exename") && ++i < argc) {
exeName = argv[i]; exeName = argv[i];
} else if (equal(argv[i], "-exepath") && ++i < argc) {
exePath = argv[i];
} }
} }
if (exeName.empty() || exeName.find('/') != string::npos) { if (exeName.empty() || exeName.find('/') != string::npos) {
@ -367,12 +384,21 @@ int main(int argc, char *argv[]) {
if (needupdate) writeLog("Need to update!"); if (needupdate) writeLog("Need to update!");
if (autostart) writeLog("From autostart!"); if (autostart) writeLog("From autostart!");
updaterName = argv[0]; updaterName = CurrentExecutablePath(argc, argv);
writeLog("Updater binary name is: %s", updaterName.c_str()); writeLog("Updater binary full path is: %s", updaterName.c_str());
if (exePath.empty()) {
writeLog("Executable path is not specified :(");
} else {
writeLog("Executable path: %s", exePath.c_str());
}
if (updaterName.size() >= 7) { if (updaterName.size() >= 7) {
if (equal(updaterName.substr(updaterName.size() - 7), "Updater")) { if (equal(updaterName.substr(updaterName.size() - 7), "Updater")) {
updaterDir = updaterName.substr(0, updaterName.size() - 7); updaterDir = updaterName.substr(0, updaterName.size() - 7);
writeLog("Updater binary dir is: %s", updaterDir.c_str()); writeLog("Updater binary dir is: %s", updaterDir.c_str());
if (exePath.empty()) {
exePath = updaterDir;
writeLog("Using updater binary dir.", exePath.c_str());
}
if (needupdate) { if (needupdate) {
if (workDir.empty()) { // old app launched, update prepared in tupdates/ready (not in tupdates/temp) if (workDir.empty()) { // old app launched, update prepared in tupdates/ready (not in tupdates/temp)
writeLog("No workdir, trying to figure it out"); writeLog("No workdir, trying to figure it out");
@ -390,7 +416,7 @@ int main(int argc, char *argv[]) {
} }
} }
if (workDir.empty()) { if (workDir.empty()) {
workDir = updaterDir; workDir = exePath;
struct stat statbuf; struct stat statbuf;
writeLog("Trying to use current as workDir, getting stat() for tupdates/ready"); writeLog("Trying to use current as workDir, getting stat() for tupdates/ready");
@ -417,7 +443,7 @@ int main(int argc, char *argv[]) {
static const int MaxLen = 65536, MaxArgsCount = 128; static const int MaxLen = 65536, MaxArgsCount = 128;
char path[MaxLen] = {0}; char path[MaxLen] = {0};
string fullBinaryPath = updaterDir + exeName; string fullBinaryPath = exePath + exeName;
strcpy(path, fullBinaryPath.c_str()); strcpy(path, fullBinaryPath.c_str());
char *args[MaxArgsCount] = {0}, p_noupdate[] = "-noupdate", p_autostart[] = "-autostart", p_debug[] = "-debug", p_tosettings[] = "-tosettings", p_key[] = "-key", p_startintray[] = "-startintray", p_testmode[] = "-testmode"; char *args[MaxArgsCount] = {0}, p_noupdate[] = "-noupdate", p_autostart[] = "-autostart", p_debug[] = "-debug", p_tosettings[] = "-tosettings", p_key[] = "-key", p_startintray[] = "-startintray", p_testmode[] = "-testmode";

View File

@ -454,7 +454,7 @@ void Application::startUpdateCheck(bool forceWait) {
if (!Sandbox::started()) return; if (!Sandbox::started()) return;
_updateCheckTimer->stop(); _updateCheckTimer->stop();
if (_updateThread || _updateReply || !cAutoUpdate()) return; if (_updateThread || _updateReply || !cAutoUpdate() || cExeName().isEmpty()) return;
int32 constDelay = cBetaVersion() ? 600 : UpdateDelayConstPart, randDelay = cBetaVersion() ? 300 : UpdateDelayRandPart; int32 constDelay = cBetaVersion() ? 600 : UpdateDelayConstPart, randDelay = cBetaVersion() ? 300 : UpdateDelayRandPart;
int32 updateInSecs = cLastUpdateCheck() + constDelay + int32(rand() % randDelay) - unixtime(); int32 updateInSecs = cLastUpdateCheck() + constDelay + int32(rand() % randDelay) - unixtime();

View File

@ -486,7 +486,7 @@ UpdateChecker::~UpdateChecker() {
bool checkReadyUpdate() { bool checkReadyUpdate() {
QString readyFilePath = cWorkingDir() + qsl("tupdates/temp/ready"), readyPath = cWorkingDir() + qsl("tupdates/temp"); QString readyFilePath = cWorkingDir() + qsl("tupdates/temp/ready"), readyPath = cWorkingDir() + qsl("tupdates/temp");
if (!QFile(readyFilePath).exists()) { if (!QFile(readyFilePath).exists() || cExeName().isEmpty()) {
if (QDir(cWorkingDir() + qsl("tupdates/ready")).exists() || QDir(cWorkingDir() + qsl("tupdates/temp")).exists()) { if (QDir(cWorkingDir() + qsl("tupdates/ready")).exists() || QDir(cWorkingDir() + qsl("tupdates/temp")).exists()) {
UpdateChecker::clearAll(); UpdateChecker::clearAll();
} }

View File

@ -369,7 +369,7 @@ namespace Logs {
LOG(("Executable dir: %1, name: %2").arg(cExeDir()).arg(cExeName())); LOG(("Executable dir: %1, name: %2").arg(cExeDir()).arg(cExeName()));
LOG(("Initial working dir: %1").arg(initialWorkingDir)); LOG(("Initial working dir: %1").arg(initialWorkingDir));
LOG(("Working dir: %1").arg(cWorkingDir())); LOG(("Working dir: %1").arg(cWorkingDir()));
LOG(("Arguments: %1").arg(cArguments())); LOG(("Command line: %1").arg(cArguments()));
if (!LogsData) { if (!LogsData) {
LOG(("FATAL: Could not open '%1' for writing log!").arg(_logsFilePath(LogDataMain, qsl("_startXX")))); LOG(("FATAL: Could not open '%1' for writing log!").arg(_logsFilePath(LogDataMain, qsl("_startXX"))));
@ -391,7 +391,6 @@ namespace Logs {
for (LogsInMemoryList::const_iterator i = list.cbegin(), e = list.cend(); i != e; ++i) { for (LogsInMemoryList::const_iterator i = list.cbegin(), e = list.cend(); i != e; ++i) {
if (i->first == LogDataMain) { if (i->first == LogDataMain) {
_logsWrite(i->first, i->second); _logsWrite(i->first, i->second);
LOG(("First: %1, %2").arg(i->first).arg(i->second));
} }
} }
} }

View File

@ -40,6 +40,27 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
using namespace Platform; using namespace Platform;
using Platform::File::internal::EscapeShell; using Platform::File::internal::EscapeShell;
namespace Platform {
QString CurrentExecutablePath(int argc, char *argv[]) {
constexpr auto kMaxPath = 1024;
char result[kMaxPath] = { 0 };
auto count = readlink("/proc/self/exe", result, kMaxPath);
if (count > 0) {
auto filename = QFile::decodeName(result);
auto deletedPostfix = qstr(" (deleted)");
if (filename.endsWith(deletedPostfix) && !QFileInfo(filename).exists()) {
filename.chop(deletedPostfix.size());
}
return filename;
}
// Fallback to the first command line argument.
return argc ? QFile::decodeName(argv[0]) : QString();
}
} // namespace Platform
namespace { namespace {
class _PsEventFilter : public QAbstractNativeEventFilter { class _PsEventFilter : public QAbstractNativeEventFilter {
@ -104,7 +125,7 @@ QString demanglestr(const QString &mangled) {
QStringList addr2linestr(uint64 *addresses, int count) { QStringList addr2linestr(uint64 *addresses, int count) {
QStringList result; QStringList result;
if (!count) return result; if (!count || cExeName().isEmpty()) return result;
result.reserve(count); result.reserve(count);
QByteArray cmd = "addr2line -e " + EscapeShell(QFile::encodeName(cExeDir() + cExeName())); QByteArray cmd = "addr2line -e " + EscapeShell(QFile::encodeName(cExeDir() + cExeName()));
@ -306,34 +327,6 @@ QString psDownloadPath() {
return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + '/' + str_const_toString(AppName) + '/'; return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + '/' + str_const_toString(AppName) + '/';
} }
QString psCurrentExeDirectory(int argc, char *argv[]) {
QString first = argc ? QFile::decodeName(argv[0]) : QString();
if (!first.isEmpty()) {
QFileInfo info(first);
if (info.isSymLink()) {
info = info.symLinkTarget();
}
if (info.exists()) {
return QDir(info.absolutePath()).absolutePath() + '/';
}
}
return QString();
}
QString psCurrentExeName(int argc, char *argv[]) {
QString first = argc ? QFile::decodeName(argv[0]) : QString();
if (!first.isEmpty()) {
QFileInfo info(first);
if (info.isSymLink()) {
info = info.symLinkTarget();
}
if (info.exists()) {
return info.fileName();
}
}
return QString();
}
void psDoCleanup() { void psDoCleanup() {
try { try {
psAutoStart(false, true); psAutoStart(false, true);
@ -431,7 +424,7 @@ bool _psRunCommand(const QByteArray &command) {
void psRegisterCustomScheme() { void psRegisterCustomScheme() {
#ifndef TDESKTOP_DISABLE_REGISTER_CUSTOM_SCHEME #ifndef TDESKTOP_DISABLE_REGISTER_CUSTOM_SCHEME
auto home = getHomeDir(); auto home = getHomeDir();
if (home.isEmpty() || cBetaVersion()) return; // don't update desktop file for beta version if (home.isEmpty() || cBetaVersion() || cExeName().isEmpty()) return; // don't update desktop file for beta version
#ifndef TDESKTOP_DISABLE_DESKTOP_FILE_GENERATION #ifndef TDESKTOP_DISABLE_DESKTOP_FILE_GENERATION
DEBUG_LOG(("App Info: placing .desktop file")); DEBUG_LOG(("App Info: placing .desktop file"));
@ -533,10 +526,13 @@ void psNewVersion() {
} }
bool _execUpdater(bool update = true, const QString &crashreport = QString()) { bool _execUpdater(bool update = true, const QString &crashreport = QString()) {
if (cExeName().isEmpty()) {
return false;
}
static const int MaxLen = 65536, MaxArgsCount = 128; static const int MaxLen = 65536, MaxArgsCount = 128;
char path[MaxLen] = {0}; char path[MaxLen] = {0};
QByteArray data(QFile::encodeName(cExeDir() + (update ? "Updater" : gExeName))); QByteArray data(QFile::encodeName(cExeDir() + (update ? "Updater" : cExeName())));
memcpy(path, data.constData(), data.size()); memcpy(path, data.constData(), data.size());
char *args[MaxArgsCount] = { 0 }; char *args[MaxArgsCount] = { 0 };
@ -554,6 +550,8 @@ bool _execUpdater(bool update = true, const QString &crashreport = QString()) {
char p_crashreportbuf[MaxLen] = { 0 }; char p_crashreportbuf[MaxLen] = { 0 };
char p_exe[] = "-exename"; char p_exe[] = "-exename";
char p_exebuf[MaxLen] = { 0 }; char p_exebuf[MaxLen] = { 0 };
char p_exepath[] = "-exepath";
char p_exepathbuf[MaxLen] = { 0 };
int argIndex = 0; int argIndex = 0;
args[argIndex++] = path; args[argIndex++] = path;
if (!update) { if (!update) {
@ -592,6 +590,12 @@ bool _execUpdater(bool update = true, const QString &crashreport = QString()) {
args[argIndex++] = p_exe; args[argIndex++] = p_exe;
args[argIndex++] = p_exebuf; args[argIndex++] = p_exebuf;
} }
QByteArray exepathf = QFile::encodeName(cExeDir());
if (exepathf.size() > 0 && exepathf.size() < MaxLen) {
memcpy(p_exepathbuf, exepathf.constData(), exepathf.size());
args[argIndex++] = p_exepath;
args[argIndex++] = p_exepathbuf;
}
Logs::closeMain(); Logs::closeMain();
SignalHandlers::finish(); SignalHandlers::finish();

View File

@ -37,6 +37,8 @@ inline void DeInitOnTopPanel(QWidget *panel) {
inline void ReInitOnTopPanel(QWidget *panel) { inline void ReInitOnTopPanel(QWidget *panel) {
} }
QString CurrentExecutablePath(int argc, char *argv[]);
} // namespace Platform } // namespace Platform
inline QString psServerPrefix() { inline QString psServerPrefix() {
@ -65,8 +67,6 @@ void psActivateProcess(uint64 pid = 0);
QString psLocalServerPrefix(); QString psLocalServerPrefix();
QString psAppDataPath(); QString psAppDataPath();
QString psDownloadPath(); QString psDownloadPath();
QString psCurrentExeDirectory(int argc, char *argv[]);
QString psCurrentExeName(int argc, char *argv[]);
void psAutoStart(bool start, bool silent = false); void psAutoStart(bool start, bool silent = false);
void psSendToMenu(bool send, bool silent = false); void psSendToMenu(bool send, bool silent = false);

View File

@ -25,6 +25,8 @@ inline bool TranslucentWindowsSupported(QPoint globalPosition) {
return true; return true;
} }
QString CurrentExecutablePath(int argc, char *argv[]);
namespace ThirdParty { namespace ThirdParty {
inline void start() { inline void start() {
@ -66,8 +68,6 @@ void psActivateProcess(uint64 pid = 0);
QString psLocalServerPrefix(); QString psLocalServerPrefix();
QString psAppDataPath(); QString psAppDataPath();
QString psDownloadPath(); QString psDownloadPath();
QString psCurrentExeDirectory(int argc, char *argv[]);
QString psCurrentExeName(int argc, char *argv[]);
void psAutoStart(bool start, bool silent = false); void psAutoStart(bool start, bool silent = false);
void psSendToMenu(bool send, bool silent = false); void psSendToMenu(bool send, bool silent = false);

View File

@ -35,6 +35,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include <IOKit/IOKitLib.h> #include <IOKit/IOKitLib.h>
#include <IOKit/hidsystem/ev_keymap.h> #include <IOKit/hidsystem/ev_keymap.h>
#include <SPMediaKeyTap.h> #include <SPMediaKeyTap.h>
#include <mach-o/dyld.h>
namespace { namespace {
@ -133,7 +134,7 @@ QString escapeShell(const QString &str) {
QStringList atosstr(uint64 *addresses, int count, uint64 base) { QStringList atosstr(uint64 *addresses, int count, uint64 base) {
QStringList result; QStringList result;
if (!count) return result; if (!count || cExeName().isEmpty()) return result;
result.reserve(count); result.reserve(count);
QString cmdstr = "atos -o " + escapeShell(cExeDir() + cExeName()) + qsl("/Contents/MacOS/Telegram -l 0x%1").arg(base, 0, 16); QString cmdstr = "atos -o " + escapeShell(cExeDir() + cExeName()) + qsl("/Contents/MacOS/Telegram -l 0x%1").arg(base, 0, 16);
@ -337,28 +338,6 @@ QString psDownloadPath() {
return objc_downloadPath(); return objc_downloadPath();
} }
QString psCurrentExeDirectory(int argc, char *argv[]) {
QString first = argc ? fromUtf8Safe(argv[0]) : QString();
if (!first.isEmpty()) {
QFileInfo info(first);
if (info.exists()) {
return QDir(info.absolutePath() + qsl("/../../..")).absolutePath() + '/';
}
}
return QString();
}
QString psCurrentExeName(int argc, char *argv[]) {
QString first = argc ? fromUtf8Safe(argv[0]) : QString();
if (!first.isEmpty()) {
QFileInfo info(first);
if (info.exists()) {
return QDir(QDir(info.absolutePath() + qsl("/../..")).absolutePath()).dirName();
}
}
return QString();
}
void psDoCleanup() { void psDoCleanup() {
try { try {
psAutoStart(false, true); psAutoStart(false, true);
@ -422,6 +401,10 @@ QString SystemLanguage() {
return QString(); return QString();
} }
QString CurrentExecutablePath(int argc, char *argv[]) {
return NS2QString([[NSBundle mainBundle] bundlePath]);
}
} // namespace Platform } // namespace Platform
void psNewVersion() { void psNewVersion() {

View File

@ -463,6 +463,9 @@ bool objc_execUpdater() {
} }
void objc_execTelegram(const QString &crashreport) { void objc_execTelegram(const QString &crashreport) {
if (cExeName().isEmpty()) {
return;
}
#ifndef OS_MAC_STORE #ifndef OS_MAC_STORE
_execUpdater(NO, crashreport); _execUpdater(NO, crashreport);
#else // OS_MAC_STORE #else // OS_MAC_STORE

View File

@ -211,34 +211,6 @@ QString psDownloadPath() {
return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + '/' + str_const_toString(AppName) + '/'; return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + '/' + str_const_toString(AppName) + '/';
} }
QString psCurrentExeDirectory(int argc, char *argv[]) {
LPWSTR *args;
int argsCount;
args = CommandLineToArgvW(GetCommandLine(), &argsCount);
if (args) {
QFileInfo info(QDir::fromNativeSeparators(QString::fromWCharArray(args[0])));
if (info.isFile()) {
return info.absoluteDir().absolutePath() + '/';
}
LocalFree(args);
}
return QString();
}
QString psCurrentExeName(int argc, char *argv[]) {
LPWSTR *args;
int argsCount;
args = CommandLineToArgvW(GetCommandLine(), &argsCount);
if (args) {
QFileInfo info(QDir::fromNativeSeparators(QString::fromWCharArray(args[0])));
if (info.isFile()) {
return info.fileName();
}
LocalFree(args);
}
return QString();
}
void psDoCleanup() { void psDoCleanup() {
try { try {
psAutoStart(false, true); psAutoStart(false, true);
@ -374,6 +346,24 @@ QString SystemCountry() {
return QString(); return QString();
} }
QString CurrentExecutablePath(int argc, char *argv[]) {
WCHAR result[MAX_PATH + 1] = { 0 };
auto count = GetModuleFileName(nullptr, result, MAX_PATH + 1);
if (count < MAX_PATH + 1) {
auto info = QFileInfo(QDir::fromNativeSeparators(QString::fromWCharArray(result)));
return info.absoluteFilePath();
}
// Fallback to the first command line argument.
auto argsCount = 0;
if (auto args = CommandLineToArgvW(GetCommandLine(), &argsCount)) {
auto info = QFileInfo(QDir::fromNativeSeparators(QString::fromWCharArray(args[0])));
LocalFree(args);
return info.absoluteFilePath();
}
return QString();
}
namespace { namespace {
QString GetLangCodeById(unsigned lngId) { QString GetLangCodeById(unsigned lngId) {
@ -592,6 +582,9 @@ namespace {
} }
void RegisterCustomScheme() { void RegisterCustomScheme() {
if (cExeName().isEmpty()) {
return;
}
#ifndef TDESKTOP_DISABLE_REGISTER_CUSTOM_SCHEME #ifndef TDESKTOP_DISABLE_REGISTER_CUSTOM_SCHEME
DEBUG_LOG(("App Info: Checking custom scheme 'tg'...")); DEBUG_LOG(("App Info: Checking custom scheme 'tg'..."));
@ -647,6 +640,10 @@ void psNewVersion() {
} }
void psExecUpdater() { void psExecUpdater() {
if (cExeName().isEmpty()) {
return;
}
QString targs = qsl("-update -exename \"") + cExeName() + '"'; QString targs = qsl("-update -exename \"") + cExeName() + '"';
if (cLaunchMode() == LaunchModeAutoStart) targs += qsl(" -autostart"); if (cLaunchMode() == LaunchModeAutoStart) targs += qsl(" -autostart");
if (cDebug()) targs += qsl(" -debug"); if (cDebug()) targs += qsl(" -debug");
@ -666,6 +663,9 @@ void psExecUpdater() {
} }
void psExecTelegram(const QString &crashreport) { void psExecTelegram(const QString &crashreport) {
if (cExeName().isEmpty()) {
return;
}
QString targs = crashreport.isEmpty() ? qsl("-noupdate") : ('"' + crashreport + '"'); QString targs = crashreport.isEmpty() ? qsl("-noupdate") : ('"' + crashreport + '"');
if (crashreport.isEmpty()) { if (crashreport.isEmpty()) {
if (cRestartingToSettings()) targs += qsl(" -tosettings"); if (cRestartingToSettings()) targs += qsl(" -tosettings");
@ -687,6 +687,9 @@ void psExecTelegram(const QString &crashreport) {
} }
void _manageAppLnk(bool create, bool silent, int path_csidl, const wchar_t *args, const wchar_t *description) { void _manageAppLnk(bool create, bool silent, int path_csidl, const wchar_t *args, const wchar_t *description) {
if (cExeName().isEmpty()) {
return;
}
WCHAR startupFolder[MAX_PATH]; WCHAR startupFolder[MAX_PATH];
HRESULT hr = SHGetFolderPath(0, path_csidl, 0, SHGFP_TYPE_CURRENT, startupFolder); HRESULT hr = SHGetFolderPath(0, path_csidl, 0, SHGFP_TYPE_CURRENT, startupFolder);
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) {
@ -1113,7 +1116,7 @@ void psWriteDump() {
char ImageHlpSymbol64[sizeof(IMAGEHLP_SYMBOL64) + StackEntryMaxNameLength]; char ImageHlpSymbol64[sizeof(IMAGEHLP_SYMBOL64) + StackEntryMaxNameLength];
QString psPrepareCrashDump(const QByteArray &crashdump, QString dumpfile) { QString psPrepareCrashDump(const QByteArray &crashdump, QString dumpfile) {
if (!LoadDbgHelp(true)) { if (!LoadDbgHelp(true) || cExeName().isEmpty()) {
return qsl("ERROR: could not init dbghelp.dll!"); return qsl("ERROR: could not init dbghelp.dll!");
} }

View File

@ -43,6 +43,8 @@ inline void DeInitOnTopPanel(QWidget *panel) {
inline void ReInitOnTopPanel(QWidget *panel) { inline void ReInitOnTopPanel(QWidget *panel) {
} }
QString CurrentExecutablePath(int argc, char *argv[]);
namespace ThirdParty { namespace ThirdParty {
inline void start() { inline void start() {
@ -78,8 +80,6 @@ QString psLocalServerPrefix();
QString psAppDataPath(); QString psAppDataPath();
QString psAppDataPathOld(); QString psAppDataPathOld();
QString psDownloadPath(); QString psDownloadPath();
QString psCurrentExeDirectory(int argc, char *argv[]);
QString psCurrentExeName(int argc, char *argv[]);
void psAutoStart(bool start, bool silent = false); void psAutoStart(bool start, bool silent = false);
void psSendToMenu(bool send, bool silent = false); void psSendToMenu(bool send, bool silent = false);

View File

@ -263,7 +263,7 @@ bool validateShortcutAt(const QString &path) {
bool validateShortcut() { bool validateShortcut() {
QString path = systemShortcutPath(); QString path = systemShortcutPath();
if (path.isEmpty()) return false; if (path.isEmpty() || cExeName().isEmpty()) return false;
if (cBetaVersion()) { if (cBetaVersion()) {
path += qsl("TelegramBeta.lnk"); path += qsl("TelegramBeta.lnk");

View File

@ -163,13 +163,26 @@ void settingsParseArgs(int argc, char *argv[]) {
QStringList args; QStringList args;
for (int32 i = 0; i < argc; ++i) { for (int32 i = 0; i < argc; ++i) {
args.push_back('"' + fromUtf8Safe(argv[i]) + '"'); args.push_back(fromUtf8Safe(argv[i]));
} }
gArguments = args.join(' '); gArguments = args.join(' ');
gExeDir = psCurrentExeDirectory(argc, argv); auto path = Platform::CurrentExecutablePath(argc, argv);
gExeName = psCurrentExeName(argc, argv); LOG(("Executable path before check: %1").arg(path));
for (int32 i = 0; i < argc; ++i) { if (!path.isEmpty()) {
auto info = QFileInfo(path);
if (info.isSymLink()) {
info = info.symLinkTarget();
}
if (info.exists()) {
gExeDir = info.absoluteDir().absolutePath() + '/';
gExeName = info.fileName();
}
}
if (cExeName().isEmpty()) {
LOG(("WARNING: Could not compute executable path, some features will be disabled."));
}
for (auto i = 0; i != argc; ++i) {
if (qstr("-testmode") == argv[i]) { if (qstr("-testmode") == argv[i]) {
gTestMode = true; gTestMode = true;
} else if (qstr("-debug") == argv[i]) { } else if (qstr("-debug") == argv[i]) {