mirror of
https://github.com/etlegacy/Update-Installer.git
synced 2025-01-22 07:21:13 +00:00
Temporarily change the current directory to that of the main binary when re-launching the application.
This may be required on Windows so that the application can find libraries that it depends upon.
This commit is contained in:
parent
c848cbfe6b
commit
61c717e6d6
3 changed files with 74 additions and 16 deletions
|
@ -516,3 +516,38 @@ std::string FileUtils::makeAbsolute(const char* path, const char* basePath)
|
|||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
void FileUtils::chdir(const char* path) throw (IOException)
|
||||
{
|
||||
#ifdef PLATFORM_UNIX
|
||||
if (::chdir(path) != 0)
|
||||
{
|
||||
throw FileUtils::IOException("Unable to change directory");
|
||||
}
|
||||
#else
|
||||
if (!SetCurrentDirectory(path))
|
||||
{
|
||||
throw FileUtils::IOException("Unable to change directory");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string FileUtils::getcwd() throw (IOException)
|
||||
{
|
||||
#ifdef PLATFORM_UNIX
|
||||
char path[PATH_MAX];
|
||||
if (!::getcwd(path,PATH_MAX))
|
||||
{
|
||||
throw FileUtils::IOException("Failed to get current directory");
|
||||
}
|
||||
return std::string(path);
|
||||
#else
|
||||
char path[MAX_PATH];
|
||||
if (GetCurrentDirectory(MAX_PATH,path) != 0)
|
||||
{
|
||||
throw FileUtils::IOException("Failed to get current directory");
|
||||
}
|
||||
return toUnixPathSeparators(std::string(path));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
@ -119,5 +119,11 @@ class FileUtils
|
|||
static std::string makeAbsolute(const char* path, const char* basePath);
|
||||
|
||||
static void writeFile(const char* path, const char* data, int length) throw (IOException);
|
||||
|
||||
/** Changes the current working directory to @p path */
|
||||
static void chdir(const char* path) throw (IOException);
|
||||
|
||||
/** Returns the current working directory of the application. */
|
||||
static std::string getcwd() throw (IOException);
|
||||
};
|
||||
|
||||
|
|
|
@ -401,6 +401,8 @@ void UpdateInstaller::setObserver(UpdateObserver* observer)
|
|||
}
|
||||
|
||||
void UpdateInstaller::restartMainApp()
|
||||
{
|
||||
try
|
||||
{
|
||||
std::string command;
|
||||
std::list<std::string> args;
|
||||
|
@ -418,13 +420,28 @@ void UpdateInstaller::restartMainApp()
|
|||
if (!command.empty())
|
||||
{
|
||||
LOG(Info,"Starting main application " + command);
|
||||
|
||||
// change the current directory to that of the application binary,
|
||||
// so that on Windows the application can find shared libraries
|
||||
// that it depends on which are in the same directory
|
||||
std::string appDir = FileUtils::dirname(command.c_str());
|
||||
std::string currentDir = FileUtils::getcwd();
|
||||
FileUtils::chdir(appDir.c_str());
|
||||
|
||||
ProcessUtils::runAsync(command,args);
|
||||
|
||||
FileUtils::chdir(currentDir.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(Error,"No main binary specified in update script");
|
||||
}
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
LOG(Error,"Unable to restart main app " + std::string(ex.what()));
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateInstaller::postInstallUpdate()
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue