179 lines
5.5 KiB
C++
179 lines
5.5 KiB
C++
#include "echoion/standalone/RuntimePaths.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <ranges>
|
|
#include <system_error>
|
|
|
|
namespace Echoion::Standalone
|
|
{
|
|
namespace
|
|
{
|
|
std::filesystem::path absoluteNormalized(std::filesystem::path path)
|
|
{
|
|
std::error_code error;
|
|
auto absolute = std::filesystem::absolute(path, error);
|
|
if (error) {
|
|
return path.lexically_normal();
|
|
}
|
|
return absolute.lexically_normal();
|
|
}
|
|
|
|
bool isWithinOrEqual(const std::filesystem::path& root,
|
|
const std::filesystem::path& candidate)
|
|
{
|
|
const auto relative = candidate.lexically_relative(root);
|
|
return !relative.empty() && !relative.is_absolute() &&
|
|
std::ranges::none_of(relative, [](const auto& component) {
|
|
return component == "..";
|
|
});
|
|
}
|
|
} // namespace
|
|
|
|
std::expected<std::filesystem::path, std::string> runningExecutablePath()
|
|
{
|
|
std::error_code error;
|
|
auto executable =
|
|
std::filesystem::read_symlink("/proc/self/exe", error);
|
|
if (error || executable.empty()) {
|
|
return std::unexpected("cannot resolve /proc/self/exe: " +
|
|
error.message());
|
|
}
|
|
auto canonical = std::filesystem::weakly_canonical(executable, error);
|
|
if (error) {
|
|
return absoluteNormalized(std::move(executable));
|
|
}
|
|
return canonical;
|
|
}
|
|
|
|
std::filesystem::path deriveInstallRoot(
|
|
const std::filesystem::path& executablePath,
|
|
const std::optional<std::filesystem::path>& homeOverride)
|
|
{
|
|
if (homeOverride && !homeOverride->empty()) {
|
|
return absoluteNormalized(*homeOverride);
|
|
}
|
|
|
|
const auto executableDirectory =
|
|
absoluteNormalized(executablePath).parent_path();
|
|
if (executableDirectory.filename() == "bin") {
|
|
return executableDirectory.parent_path();
|
|
}
|
|
return executableDirectory;
|
|
}
|
|
|
|
std::filesystem::path resolveCorePath(
|
|
const std::filesystem::path& installRoot,
|
|
const std::filesystem::path& executableDirectory,
|
|
const std::optional<std::filesystem::path>& coreOverride)
|
|
{
|
|
if (coreOverride && !coreOverride->empty()) {
|
|
return coreOverride->is_absolute()
|
|
? coreOverride->lexically_normal()
|
|
: (installRoot / *coreOverride).lexically_normal();
|
|
}
|
|
|
|
const std::array candidates{
|
|
installRoot / "lib" / "Echoion.ts",
|
|
executableDirectory / "Echoion.ts",
|
|
installRoot / "bin" / "Echoion.ts",
|
|
installRoot / "plugins" / "Echoion.ts",
|
|
};
|
|
for (const auto& candidate : candidates) {
|
|
std::error_code error;
|
|
if (std::filesystem::is_regular_file(candidate, error) && !error) {
|
|
return candidate.lexically_normal();
|
|
}
|
|
}
|
|
return candidates.front().lexically_normal();
|
|
}
|
|
|
|
std::filesystem::path resolveStateDirectory(
|
|
const std::optional<std::filesystem::path>& commandLineOverride,
|
|
const std::optional<std::filesystem::path>& environmentOverride,
|
|
const std::optional<std::filesystem::path>& xdgStateHome,
|
|
const std::optional<std::filesystem::path>& userHome,
|
|
const std::filesystem::path& fallback)
|
|
{
|
|
if (commandLineOverride && !commandLineOverride->empty()) {
|
|
return absoluteNormalized(*commandLineOverride);
|
|
}
|
|
if (environmentOverride && !environmentOverride->empty()) {
|
|
return absoluteNormalized(*environmentOverride);
|
|
}
|
|
if (xdgStateHome && !xdgStateHome->empty()) {
|
|
return absoluteNormalized(*xdgStateHome / "echoion");
|
|
}
|
|
if (userHome && !userHome->empty()) {
|
|
return absoluteNormalized(*userHome / ".local" / "state" /
|
|
"echoion");
|
|
}
|
|
return absoluteNormalized(fallback);
|
|
}
|
|
|
|
std::optional<std::filesystem::path> runtimeWorkingDirectory(
|
|
const std::filesystem::path& installRoot,
|
|
const std::filesystem::path& executableDirectory)
|
|
{
|
|
const auto packagedDirectory = installRoot / "share" / "echoion";
|
|
std::error_code error;
|
|
if (std::filesystem::is_regular_file(
|
|
packagedDirectory / "web" / "index.html", error) &&
|
|
!error) {
|
|
return packagedDirectory;
|
|
}
|
|
|
|
error.clear();
|
|
if (std::filesystem::is_regular_file(
|
|
executableDirectory / "web" / "index.html", error) &&
|
|
!error) {
|
|
return executableDirectory;
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::expected<std::filesystem::path, std::string>
|
|
resolveAvatarCacheDirectory(
|
|
const std::filesystem::path& configuredDirectory,
|
|
const std::filesystem::path& requestedDirectory)
|
|
{
|
|
if (configuredDirectory.empty()) {
|
|
return std::unexpected("avatar cache directory is not configured");
|
|
}
|
|
|
|
std::error_code error;
|
|
const auto configured = std::filesystem::weakly_canonical(
|
|
absoluteNormalized(configuredDirectory), error);
|
|
if (error) {
|
|
return std::unexpected("cannot resolve avatar cache directory: " +
|
|
error.message());
|
|
}
|
|
|
|
if (!requestedDirectory.empty() && !requestedDirectory.is_absolute() &&
|
|
(requestedDirectory != requestedDirectory.lexically_normal() ||
|
|
std::ranges::any_of(requestedDirectory, [](const auto& component) {
|
|
return component == "." || component == "..";
|
|
}))) {
|
|
return std::unexpected("avatar cache subdirectory must be "
|
|
"normalized and non-traversing");
|
|
}
|
|
|
|
const auto requested = requestedDirectory.empty() ? configured
|
|
: requestedDirectory.is_absolute()
|
|
? requestedDirectory
|
|
: configured / requestedDirectory;
|
|
const auto resolved = std::filesystem::weakly_canonical(
|
|
absoluteNormalized(requested), error);
|
|
if (error) {
|
|
return std::unexpected(
|
|
"cannot resolve requested avatar directory: " +
|
|
error.message());
|
|
}
|
|
if (!isWithinOrEqual(configured, resolved)) {
|
|
return std::unexpected("requested avatar directory is outside the "
|
|
"configured state cache");
|
|
}
|
|
return resolved;
|
|
}
|
|
} // namespace Echoion::Standalone
|