Files
OpenRCT2-Web/src/openrct2/platform/Platform.Linux.cpp
T

167 lines
5.1 KiB
C++
Raw Normal View History

2017-12-01 00:54:03 +00:00
/*****************************************************************************
2018-06-15 14:07:34 +02:00
* Copyright (c) 2014-2018 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2017-12-01 00:54:03 +00:00
2018-01-29 15:56:34 +01:00
#if defined(__FreeBSD__) || defined(__OpenBSD__) || (defined(__linux__) && !defined(__ANDROID__))
2017-12-01 00:54:03 +00:00
2018-07-21 16:17:06 +02:00
# include <limits.h>
# include <pwd.h>
# if defined(__FreeBSD__)
# include <stddef.h>
# include <sys/sysctl.h>
# include <sys/types.h>
# endif // __FreeBSD__
# if defined(__linux__)
// for PATH_MAX
2018-07-21 16:17:06 +02:00
# include <linux/limits.h>
# endif // __linux__
# include "../OpenRCT2.h"
# include "../core/Path.hpp"
# include "../core/Util.hpp"
# include "Platform2.h"
# include "platform.h"
2017-12-01 00:54:03 +00:00
namespace Platform
{
std::string GetFolderPath(SPECIAL_FOLDER folder)
{
switch (folder)
{
2018-06-22 23:04:38 +02:00
case SPECIAL_FOLDER::USER_CACHE:
case SPECIAL_FOLDER::USER_CONFIG:
case SPECIAL_FOLDER::USER_DATA:
2017-12-01 00:54:03 +00:00
{
2017-12-03 19:22:35 +00:00
auto path = GetEnvironmentPath("XDG_CONFIG_HOME");
if (path.empty())
{
auto home = GetFolderPath(SPECIAL_FOLDER::USER_HOME);
path = Path::Combine(home, ".config");
}
return path;
2017-12-01 00:54:03 +00:00
}
2018-06-22 23:04:38 +02:00
case SPECIAL_FOLDER::USER_HOME:
return GetHomePath();
default:
return std::string();
2017-12-01 00:54:03 +00:00
}
}
2018-01-21 02:13:32 +00:00
std::string GetDocsPath()
{
2018-06-22 23:04:38 +02:00
static const utf8* searchLocations[] = {
2018-01-21 02:13:32 +00:00
"./doc",
"/usr/share/doc/openrct2",
};
for (auto searchLocation : searchLocations)
{
log_verbose("Looking for OpenRCT2 doc path at %s", searchLocation);
if (platform_directory_exists(searchLocation))
{
return searchLocation;
}
}
return std::string();
}
2017-12-01 23:48:46 +00:00
static std::string FindInstallPath()
{
2018-06-22 23:04:38 +02:00
static const char* SearchLocations[] = {
2017-12-01 23:48:46 +00:00
"../share/openrct2",
2018-07-21 16:17:06 +02:00
# ifdef ORCT2_RESOURCE_DIR
2017-12-01 23:48:46 +00:00
// defined in CMakeLists.txt
ORCT2_RESOURCE_DIR,
2018-07-21 16:17:06 +02:00
# endif // ORCT2_RESOURCE_DIR
2017-12-01 23:48:46 +00:00
"/usr/local/share/openrct2",
"/var/lib/openrct2",
"/usr/share/openrct2",
};
for (auto searchLocation : SearchLocations)
{
log_verbose("Looking for OpenRCT2 data in %s", searchLocation);
if (Path::DirectoryExists(searchLocation))
{
return searchLocation;
}
}
return std::string();
}
static std::string GetCurrentWorkingDirectory()
{
char cwdPath[PATH_MAX];
if (getcwd(cwdPath, sizeof(cwdPath)) != nullptr)
{
return cwdPath;
}
return std::string();
}
std::string GetInstallPath()
{
// 1. Try command line argument
auto path = std::string(gCustomOpenrctDataPath);
if (!path.empty())
{
path = Path::GetAbsolute(path);
}
else
{
// 2. Try ${exeDir}/data
auto exePath = Platform::GetCurrentExecutablePath();
auto exeDirectory = Path::GetDirectory(exePath);
path = Path::Combine(exeDirectory, "data");
if (!Path::DirectoryExists(path))
{
// 3. Try standard system app directories
path = FindInstallPath();
if (path.empty())
{
// 4. Fallback to ${cwd}/data
path = GetCurrentWorkingDirectory();
if (!path.empty())
{
path = Path::Combine(path, "data");
}
else
{
return "/";
}
}
}
}
return path;
}
std::string GetCurrentExecutablePath()
{
char exePath[PATH_MAX] = { 0 };
2018-07-21 16:17:06 +02:00
# ifdef __linux__
2017-12-01 23:48:46 +00:00
auto bytesRead = readlink("/proc/self/exe", exePath, sizeof(exePath));
if (bytesRead == -1)
{
log_fatal("failed to read /proc/self/exe");
}
2018-07-21 16:17:06 +02:00
# elif defined(__FreeBSD__)
const int32_t mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
2017-12-01 23:48:46 +00:00
auto exeLen = sizeof(exePath);
if (sysctl(mib, 4, exePath, &exeLen, nullptr, 0) == -1)
{
log_fatal("failed to get process path");
}
2018-07-21 16:17:06 +02:00
# elif defined(__OpenBSD__)
2017-12-01 23:48:46 +00:00
// There is no way to get the path name of a running executable.
// If you are not using the port or package, you may have to change this line!
strlcpy(exePath, "/usr/local/bin/", sizeof(exePath));
2018-07-21 16:17:06 +02:00
# else
# error "Platform does not support full path exe retrieval"
# endif
2017-12-01 23:48:46 +00:00
return exePath;
}
2018-06-22 23:04:38 +02:00
} // namespace Platform
2017-12-01 00:54:03 +00:00
#endif