[feat] add portable build storage mode, refs #80

This commit is contained in:
dijunkun
2026-05-20 23:53:13 +08:00
parent a3aedcb624
commit 515d517a99
5 changed files with 196 additions and 3 deletions
+96 -1
View File
@@ -1,12 +1,98 @@
#include "path_manager.h"
#include <cstdint>
#include <cstdlib>
#include <vector>
#ifndef CROSSDESK_PORTABLE
#define CROSSDESK_PORTABLE 0
#endif
#if CROSSDESK_PORTABLE
#if defined(__APPLE__)
#include <mach-o/dyld.h>
#elif !defined(_WIN32)
#include <limits.h>
#include <unistd.h>
#endif
#endif
namespace {
#if CROSSDESK_PORTABLE
std::filesystem::path GetExecutableDirectory() {
#ifdef _WIN32
std::vector<wchar_t> buffer(MAX_PATH);
while (true) {
DWORD length =
GetModuleFileNameW(nullptr, buffer.data(),
static_cast<DWORD>(buffer.size()));
if (length == 0) {
return {};
}
if (length < buffer.size()) {
return std::filesystem::path(buffer.data(), buffer.data() + length)
.parent_path();
}
if (buffer.size() >= 32768) {
return {};
}
buffer.resize(buffer.size() * 2);
}
#elif defined(__APPLE__)
uint32_t size = 0;
_NSGetExecutablePath(nullptr, &size);
std::vector<char> buffer(size + 1);
if (_NSGetExecutablePath(buffer.data(), &size) != 0) {
return {};
}
std::error_code ec;
std::filesystem::path executable =
std::filesystem::weakly_canonical(buffer.data(), ec);
if (ec) {
executable = buffer.data();
}
return executable.parent_path();
#else
std::vector<char> buffer(PATH_MAX);
while (true) {
ssize_t length = readlink("/proc/self/exe", buffer.data(),
buffer.size() - 1);
if (length <= 0) {
return {};
}
if (static_cast<size_t>(length) < buffer.size() - 1) {
buffer[static_cast<size_t>(length)] = '\0';
return std::filesystem::path(buffer.data()).parent_path();
}
buffer.resize(buffer.size() * 2);
}
#endif
}
std::filesystem::path GetPortableRootPath() {
std::filesystem::path executable_dir = GetExecutableDirectory();
if (!executable_dir.empty()) {
return executable_dir;
}
std::error_code ec;
std::filesystem::path current = std::filesystem::current_path(ec);
return ec ? std::filesystem::path(".") : current;
}
#endif
} // namespace
namespace crossdesk {
PathManager::PathManager(const std::string& app_name) : app_name_(app_name) {}
std::filesystem::path PathManager::GetConfigPath() {
#if CROSSDESK_PORTABLE
return GetPortableRootPath() / "data";
#else
#ifdef _WIN32
return GetKnownFolder(FOLDERID_RoamingAppData) / app_name_;
#elif __APPLE__
@@ -14,9 +100,13 @@ std::filesystem::path PathManager::GetConfigPath() {
#else
return GetEnvOrDefault("XDG_CONFIG_HOME", GetHome() + "/.config") / app_name_;
#endif
#endif
}
std::filesystem::path PathManager::GetCachePath() {
#if CROSSDESK_PORTABLE
return GetPortableRootPath() / "data";
#else
#ifdef _WIN32
#ifdef CROSSDESK_DEBUG
return "cache";
@@ -28,9 +118,13 @@ std::filesystem::path PathManager::GetCachePath() {
#else
return GetEnvOrDefault("XDG_CACHE_HOME", GetHome() + "/.cache") / app_name_;
#endif
#endif
}
std::filesystem::path PathManager::GetLogPath() {
#if CROSSDESK_PORTABLE
return GetPortableRootPath() / "logs";
#else
#ifdef _WIN32
return GetKnownFolder(FOLDERID_LocalAppData) / app_name_ / "logs";
#elif __APPLE__
@@ -38,6 +132,7 @@ std::filesystem::path PathManager::GetLogPath() {
#else
return GetCachePath() / "logs";
#endif
#endif
}
bool PathManager::CreateDirectories(const std::filesystem::path& p) {
@@ -93,4 +188,4 @@ std::filesystem::path PathManager::GetEnvOrDefault(const char* env_var,
return std::filesystem::path(def);
}
} // namespace crossdesk
} // namespace crossdesk