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:
Robert Knight 2011-09-16 14:35:49 +01:00
parent c848cbfe6b
commit 61c717e6d6
3 changed files with 74 additions and 16 deletions

View file

@ -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
}

View file

@ -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);
};

View file

@ -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()
{