From 9b4419f3fc108eb26edb9b1d75da0bfbbdb6f12b Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 17 Sep 2019 11:12:13 +0200 Subject: [PATCH] [ABI Break] Correctly detect the executable path in appimage. There are two executable path : - The user one (the appimage path) - The real one (in the appimage archive) When we search of `library.xml` we need the user one. But when we search of `aria2c` or `kiwix-serve` we need the real one. Fix kiwix/kiwix-desktop#256 --- include/tools/pathTools.h | 2 +- src/aria2.cpp | 2 +- src/kiwixserve.cpp | 2 +- src/tools/pathTools.cpp | 22 +++++++++------------- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/include/tools/pathTools.h b/include/tools/pathTools.h index 0e7f24bbe..01bc485d9 100644 --- a/include/tools/pathTools.h +++ b/include/tools/pathTools.h @@ -38,7 +38,7 @@ bool makeDirectory(const std::string& path); std::string makeTmpDirectory(); bool copyFile(const std::string& sourcePath, const std::string& destPath); std::string getLastPathElement(const std::string& path); -std::string getExecutablePath(); +std::string getExecutablePath(bool realPathOnly = false); std::string getCurrentDirectory(); std::string getDataDirectory(); bool writeTextFile(const std::string& path, const std::string& content); diff --git a/src/aria2.cpp b/src/aria2.cpp index c63faf76f..a6e081d7d 100644 --- a/src/aria2.cpp +++ b/src/aria2.cpp @@ -49,7 +49,7 @@ Aria2::Aria2(): m_secret = "token:"+m_secret; std::string aria2cmd = appendToDirectory( - removeLastPathElement(getExecutablePath(), true, true), + removeLastPathElement(getExecutablePath(true), true, true), ARIA2_CMD); if (fileExists(aria2cmd)) { // A local aria2c exe exists (packaged with kiwix-desktop), use it. diff --git a/src/kiwixserve.cpp b/src/kiwixserve.cpp index 1111fec17..f8e08d780 100644 --- a/src/kiwixserve.cpp +++ b/src/kiwixserve.cpp @@ -36,7 +36,7 @@ void KiwixServe::run() std::vector callCmd; std::string kiwixServeCmd = appendToDirectory( - removeLastPathElement(getExecutablePath(), true, true), + removeLastPathElement(getExecutablePath(true), true, true), KIWIXSERVE_CMD); if (fileExists(kiwixServeCmd)) { // A local kiwix-serve exe exists (packaged with kiwix-desktop), use it. diff --git a/src/tools/pathTools.cpp b/src/tools/pathTools.cpp index b72657e99..9c46fc01c 100644 --- a/src/tools/pathTools.cpp +++ b/src/tools/pathTools.cpp @@ -279,24 +279,20 @@ bool copyFile(const std::string& sourcePath, const std::string& destPath) return true; } -std::string getExecutablePath() +std::string getExecutablePath(bool realPathOnly) { char binRootPath[PATH_MAX]; - char* cAppImage = ::getenv("APPIMAGE"); - if (cAppImage) { - char* cArgv0 = ::getenv("ARGV0"); - char* cOwd = ::getenv("OWD"); - if (!cArgv0 && !cOwd) { - // Nothing to clean, goto in the same function... - // This is silly but .. ok.. - // And we should pass here, if APPIMAGE is set ARGV0 and OWD should also - // (except if there is a bug in appimage) - goto normal; + if (!realPathOnly) { + char* cAppImage = ::getenv("APPIMAGE"); + if (cAppImage) { + char* cArgv0 = ::getenv("ARGV0"); + char* cOwd = ::getenv("OWD"); + if (cArgv0 && cOwd) { + return appendToDirectory(cOwd, cArgv0); + } } - return appendToDirectory(cOwd, cArgv0); } -normal: #ifdef _WIN32 GetModuleFileName(NULL, binRootPath, PATH_MAX);