foxygit / Torlinkc Log in
commits tags

/src/config/paths.cpp · 1.83 KB

raw
#include "torlinkc/config/paths.hpp"

#include <cstdlib>
#include <filesystem>

namespace fs = std::filesystem;

namespace torlinkc::paths {

namespace {

std::string homeDir() {
  if (const char* h = std::getenv("HOME")) return h;
  return "/tmp";
}

std::string envOr(const char* name, const std::string& fallback) {
  if (const char* v = std::getenv(name); v && *v) return v;
  return fallback;
}

fs::path dataBaseDir() {
#ifdef __APPLE__
  return fs::path(homeDir()) / "Library" / "Application Support" / kAppName;
#else
  return fs::path(envOr("XDG_DATA_HOME", homeDir() + "/.local/share")) / kAppName;
#endif
}

fs::path configBaseDir() {
#ifdef __APPLE__
  return fs::path(homeDir()) / "Library" / "Preferences" / kAppName;
#else
  return fs::path(envOr("XDG_CONFIG_HOME", homeDir() + "/.config")) / kAppName;
#endif
}

fs::path dataDir() {
  if (const char* override_ = std::getenv("TORLINK_STATE_DIR"); override_ && *override_) {
    return fs::path(override_) / "data";
  }
  return dataBaseDir();
}

fs::path configDir() {
  if (const char* override_ = std::getenv("TORLINK_STATE_DIR"); override_ && *override_) {
    return fs::path(override_) / "config";
  }
  return configBaseDir();
}

}  // namespace

std::string defaultDownloadDir() {
  return (fs::path(homeDir()) / "Downloads" / "torlink").string();
}

std::string configFile() { return (configDir() / "config.json").string(); }

std::string queueFile() { return (dataDir() / "queue.json").string(); }

std::string historyFile() { return (dataDir() / "history.json").string(); }

std::string seedsFile() { return (dataDir() / "seeds.json").string(); }

std::string torrentsDir() { return (dataDir() / "torrents").string(); }

std::string bootMarkerFile() { return (dataDir() / "boot.marker").string(); }

std::string logsDir() { return (dataDir() / "logs").string(); }

}  // namespace torlinkc::paths