Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vcpkg] Use XDG/LOCALAPPDATA for default binary caching path #12091

Merged
merged 5 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion toolsrc/include/vcpkg/base/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ namespace vcpkg::System
{
Optional<std::string> get_environment_variable(ZStringView varname) noexcept;

ExpectedS<std::string> get_home_dir() noexcept;
const ExpectedS<fs::path>& get_home_dir() noexcept;

const ExpectedS<fs::path>& get_platform_cache_home() noexcept;

const ExpectedS<fs::path>& get_appdata_local() noexcept;
const ExpectedS<fs::path>& get_xdg_config_home() noexcept;
const ExpectedS<fs::path>& get_xdg_cache_home() noexcept;
const ExpectedS<fs::path>& get_xdg_data_home() noexcept;

Optional<std::string> get_registry_string(void* base_hkey, StringView subkey, StringView valuename);

Expand Down
106 changes: 99 additions & 7 deletions toolsrc/src/vcpkg/base/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ namespace vcpkg
return supported_architectures;
}

const ExpectedS<fs::path>& System::get_platform_cache_home() noexcept
{
#ifdef _WIN32
return System::get_appdata_local();
#else
return System::get_xdg_cache_home();
#endif
}

Optional<std::string> System::get_environment_variable(ZStringView varname) noexcept
{
#if defined(_WIN32)
Expand All @@ -105,17 +114,100 @@ namespace vcpkg
#endif // defined(_WIN32)
}

ExpectedS<std::string> System::get_home_dir() noexcept
const ExpectedS<fs::path>& System::get_home_dir() noexcept
{
static ExpectedS<fs::path> s_home = []() -> ExpectedS<fs::path> {
#ifdef _WIN32
auto maybe_home = System::get_environment_variable("USERPROFILE");
if (!maybe_home.has_value() || maybe_home.get()->empty())
return {"unable to read %USERPROFILE%", ExpectedRightTag{}};
#define HOMEVAR "%USERPROFILE%"
auto maybe_home = System::get_environment_variable("USERPROFILE");
if (!maybe_home.has_value() || maybe_home.get()->empty())
return {"unable to read " HOMEVAR, ExpectedRightTag{}};
#else
auto maybe_home = System::get_environment_variable("HOME");
if (!maybe_home.has_value() || maybe_home.get()->empty()) return {"unable to read $HOME", ExpectedRightTag{}};
#define HOMEVAR "$HOME"
auto maybe_home = System::get_environment_variable("HOME");
if (!maybe_home.has_value() || maybe_home.get()->empty())
return {"unable to read " HOMEVAR, ExpectedRightTag{}};
#endif
return {std::move(*maybe_home.get()), ExpectedLeftTag{}};

auto p = fs::u8path(*maybe_home.get());
if (!p.is_absolute()) return {HOMEVAR " was not an absolute path", ExpectedRightTag{}};

return {std::move(p), ExpectedLeftTag{}};
}();
return s_home;
#undef HOMEVAR
}

const ExpectedS<fs::path>& System::get_appdata_local() noexcept
{
static ExpectedS<fs::path> s_home = []() -> ExpectedS<fs::path> {
auto maybe_home = System::get_environment_variable("LOCALAPPDATA");
if (!maybe_home.has_value() || maybe_home.get()->empty())
return {"unable to read %LOCALAPPDATA%", ExpectedRightTag{}};

auto p = fs::u8path(*maybe_home.get());
if (!p.is_absolute()) return {"%LOCALAPPDATA% was not an absolute path", ExpectedRightTag{}};

return {std::move(p), ExpectedLeftTag{}};
}();
return s_home;
}

const ExpectedS<fs::path>& get_xdg_config_home() noexcept
{
static ExpectedS<fs::path> s_home = [] {
auto maybe_home = System::get_environment_variable("XDG_CONFIG_HOME");
if (auto p = maybe_home.get())
{
return ExpectedS<fs::path>(fs::u8path(*p));
}
else
{
return System::get_home_dir().map([](fs::path home) {
home.append(".config");
return home;
});
}
}();
return s_home;
}

const ExpectedS<fs::path>& get_xdg_cache_home() noexcept
{
static ExpectedS<fs::path> s_home = [] {
auto maybe_home = System::get_environment_variable("XDG_CACHE_HOME");
if (auto p = maybe_home.get())
{
return ExpectedS<fs::path>(fs::u8path(*p));
}
else
{
return System::get_home_dir().map([](fs::path home) {
home.append(".cache");
return home;
});
}
}();
return s_home;
}

const ExpectedS<fs::path>& get_xdg_data_home() noexcept
{
static ExpectedS<fs::path> s_home = [] {
auto maybe_home = System::get_environment_variable("XDG_DATA_HOME");
if (auto p = maybe_home.get())
{
return ExpectedS<fs::path>(fs::u8path(*p));
}
else
{
return System::get_home_dir().map([](fs::path home) {
home.append(".local").append(".share");
return home;
});
}
}();
return s_home;
}

#if defined(_WIN32)
Expand Down
5 changes: 3 additions & 2 deletions toolsrc/src/vcpkg/binarycaching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,11 @@ ExpectedS<std::unique_ptr<IBinaryProvider>> vcpkg::create_binary_provider_from_c
return add_error("unexpected arguments: binary config 'default' does not take more than 1 argument",
segments[0].first);

auto maybe_home = System::get_home_dir();
auto&& maybe_home = System::get_platform_cache_home();
if (!maybe_home.has_value()) return add_error(maybe_home.error(), segments[0].first);

auto p = fs::u8path(maybe_home.value_or_exit(VCPKG_LINE_INFO)) / fs::u8path(".vcpkg/archives");
auto p = *maybe_home.get();
p.append("vcpkg").append("archives");
if (!p.is_absolute())
return add_error("default path was not absolute: " + p.u8string(), segments[0].first);
if (segments.size() == 2)
Expand Down