2011-08-19 19:06:09 +00:00
|
|
|
#include "linuxpath.hpp"
|
|
|
|
|
2016-08-06 18:00:27 +00:00
|
|
|
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
|
2011-08-19 19:06:09 +00:00
|
|
|
|
|
|
|
#include <cstdlib>
|
2011-08-20 08:11:34 +00:00
|
|
|
#include <cstring>
|
2011-08-19 19:06:09 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <unistd.h>
|
2012-01-17 22:19:17 +00:00
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2011-08-19 19:06:09 +00:00
|
|
|
|
2015-12-07 20:58:30 +00:00
|
|
|
#include <components/misc/stringops.hpp>
|
|
|
|
|
2013-12-26 00:24:43 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
boost::filesystem::path getUserHome()
|
|
|
|
{
|
|
|
|
const char* dir = getenv("HOME");
|
|
|
|
if (dir == NULL)
|
|
|
|
{
|
|
|
|
struct passwd* pwd = getpwuid(getuid());
|
|
|
|
if (pwd != NULL)
|
|
|
|
{
|
|
|
|
dir = pwd->pw_dir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dir == NULL)
|
|
|
|
return boost::filesystem::path();
|
|
|
|
else
|
|
|
|
return boost::filesystem::path(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::filesystem::path getEnv(const std::string& envVariable, const boost::filesystem::path& fallback)
|
|
|
|
{
|
|
|
|
const char* result = getenv(envVariable.c_str());
|
|
|
|
if (!result)
|
|
|
|
return fallback;
|
|
|
|
boost::filesystem::path dir(result);
|
|
|
|
if (dir.empty())
|
|
|
|
return fallback;
|
|
|
|
else
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-19 19:06:09 +00:00
|
|
|
/**
|
|
|
|
* \namespace Files
|
|
|
|
*/
|
|
|
|
namespace Files
|
|
|
|
{
|
|
|
|
|
2012-09-08 16:47:31 +00:00
|
|
|
LinuxPath::LinuxPath(const std::string& application_name)
|
|
|
|
: mName(application_name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-25 23:28:19 +00:00
|
|
|
boost::filesystem::path LinuxPath::getUserConfigPath() const
|
2011-08-19 19:06:09 +00:00
|
|
|
{
|
2013-12-26 00:24:43 +00:00
|
|
|
return getEnv("XDG_CONFIG_HOME", getUserHome() / ".config") / mName;
|
|
|
|
}
|
2011-08-19 19:06:09 +00:00
|
|
|
|
2013-12-26 00:24:43 +00:00
|
|
|
boost::filesystem::path LinuxPath::getUserDataPath() const
|
|
|
|
{
|
|
|
|
return getEnv("XDG_DATA_HOME", getUserHome() / ".local/share") / mName;
|
2011-08-19 19:06:09 +00:00
|
|
|
}
|
|
|
|
|
2012-09-02 17:40:26 +00:00
|
|
|
boost::filesystem::path LinuxPath::getCachePath() const
|
|
|
|
{
|
2013-12-26 00:24:43 +00:00
|
|
|
return getEnv("XDG_CACHE_HOME", getUserHome() / ".cache") / mName;
|
2012-09-02 17:40:26 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 23:28:19 +00:00
|
|
|
boost::filesystem::path LinuxPath::getGlobalConfigPath() const
|
2011-08-19 19:06:09 +00:00
|
|
|
{
|
2014-09-19 18:23:39 +00:00
|
|
|
boost::filesystem::path globalPath(GLOBAL_CONFIG_PATH);
|
2012-09-08 16:47:31 +00:00
|
|
|
return globalPath / mName;
|
2011-08-19 19:06:09 +00:00
|
|
|
}
|
|
|
|
|
2012-01-21 00:14:35 +00:00
|
|
|
boost::filesystem::path LinuxPath::getLocalPath() const
|
2011-08-19 19:06:09 +00:00
|
|
|
{
|
|
|
|
return boost::filesystem::path("./");
|
|
|
|
}
|
|
|
|
|
2012-01-21 16:58:49 +00:00
|
|
|
boost::filesystem::path LinuxPath::getGlobalDataPath() const
|
|
|
|
{
|
2014-09-19 18:23:39 +00:00
|
|
|
boost::filesystem::path globalDataPath(GLOBAL_DATA_PATH);
|
2012-09-08 16:47:31 +00:00
|
|
|
return globalDataPath / mName;
|
2012-01-21 16:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
boost::filesystem::path LinuxPath::getInstallPath() const
|
|
|
|
{
|
2012-01-25 22:55:43 +00:00
|
|
|
boost::filesystem::path installPath;
|
|
|
|
|
2013-12-26 00:24:43 +00:00
|
|
|
boost::filesystem::path homePath = getUserHome();
|
2012-01-25 22:55:43 +00:00
|
|
|
|
2013-12-26 00:24:43 +00:00
|
|
|
if (!homePath.empty())
|
2012-01-25 22:55:43 +00:00
|
|
|
{
|
|
|
|
boost::filesystem::path wineDefaultRegistry(homePath);
|
|
|
|
wineDefaultRegistry /= ".wine/system.reg";
|
|
|
|
|
|
|
|
if (boost::filesystem::is_regular_file(wineDefaultRegistry))
|
2012-01-17 18:18:17 +00:00
|
|
|
{
|
2012-01-25 22:55:43 +00:00
|
|
|
boost::filesystem::ifstream file(wineDefaultRegistry);
|
|
|
|
bool isRegEntry = false;
|
|
|
|
std::string line;
|
|
|
|
std::string mwpath;
|
|
|
|
|
2012-02-23 22:01:42 +00:00
|
|
|
while (std::getline(file, line))
|
2012-01-17 18:18:17 +00:00
|
|
|
{
|
2012-01-25 22:55:43 +00:00
|
|
|
if (line[0] == '[') // we found an entry
|
|
|
|
{
|
2012-02-23 22:01:42 +00:00
|
|
|
if (isRegEntry)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
isRegEntry = (line.find("Softworks\\\\Morrowind]") != std::string::npos);
|
2012-01-25 22:55:43 +00:00
|
|
|
}
|
|
|
|
else if (isRegEntry)
|
2012-01-17 18:18:17 +00:00
|
|
|
{
|
2012-01-25 22:55:43 +00:00
|
|
|
if (line[0] == '"') // empty line means new registry key
|
2012-01-17 18:18:17 +00:00
|
|
|
{
|
2012-01-25 22:55:43 +00:00
|
|
|
std::string key = line.substr(1, line.find('"', 1) - 1);
|
|
|
|
if (strcasecmp(key.c_str(), "Installed Path") == 0)
|
|
|
|
{
|
|
|
|
std::string::size_type valuePos = line.find('=') + 2;
|
|
|
|
mwpath = line.substr(valuePos, line.rfind('"') - valuePos);
|
|
|
|
|
|
|
|
std::string::size_type pos = mwpath.find("\\");
|
|
|
|
while (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
mwpath.replace(pos, 2, "/");
|
|
|
|
pos = mwpath.find("\\", pos + 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-01-17 18:18:17 +00:00
|
|
|
}
|
2012-01-25 22:55:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mwpath.empty())
|
|
|
|
{
|
2012-02-19 22:39:37 +00:00
|
|
|
// Change drive letter to lowercase, so we could use
|
|
|
|
// ~/.wine/dosdevices symlinks
|
2015-12-07 20:58:30 +00:00
|
|
|
mwpath[0] = Misc::StringUtils::toLower(mwpath[0]);
|
2012-01-25 22:55:43 +00:00
|
|
|
installPath /= homePath;
|
|
|
|
installPath /= ".wine/dosdevices/";
|
|
|
|
installPath /= mwpath;
|
|
|
|
|
|
|
|
if (!boost::filesystem::is_directory(installPath))
|
|
|
|
{
|
|
|
|
installPath.clear();
|
|
|
|
}
|
2012-01-17 18:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-25 22:55:43 +00:00
|
|
|
|
|
|
|
return installPath;
|
2012-01-21 16:58:49 +00:00
|
|
|
}
|
|
|
|
|
2011-08-19 19:06:09 +00:00
|
|
|
} /* namespace Files */
|
|
|
|
|
2016-08-06 18:00:27 +00:00
|
|
|
#endif /* defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) */
|