7 #include CXX_FILESYSTEM_INCLUDE
15 namespace fs = CXX_FILESYSTEM_NAMESPACE;
23 const auto envDir = std::getenv(
"GNSS_INFO_CACHE_DIR");
24 if (envDir !=
nullptr)
31 const std::string homeDirEnv {
"USERPROFILE"};
33 const std::string homeDirEnv{
"HOME"};
36 const auto cacheEnv = std::getenv(
"XDG_CACHE_HOME");
37 if (cacheEnv !=
nullptr)
40 cacheDir = std::string(std::getenv(homeDirEnv.c_str())) +
"/.cache";
41 cacheDir +=
"/gnss_info";
44 if (!fs::is_directory(cacheDir))
45 fs::create_directories(cacheDir);
50 cras::expected<std::stringstream, std::string>
download(
const std::string& url,
51 const std::function<
void(
CURL*)>& curlOptions)
53 const auto curlCallback = +[](
void *contents,
size_t size,
size_t nmemb,
void *userp)
55 *
static_cast<std::stringstream*
>(userp) << std::string(
static_cast<char*
>(contents), size * nmemb);
59 std::stringstream readBuffer;
60 const auto curl = curl_easy_init();
61 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1u);
64 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
65 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallback);
66 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
67 const auto res = curl_easy_perform(curl);
68 curl_easy_cleanup(curl);
71 return cras::make_unexpected(
cras::format(
"Error downloading %s: %s", url.c_str(), curl_easy_strerror(res)));
78 return fs::exists(
file);
86 #if CXX_FILESYSTEM_IS_BOOST
87 auto fileTime = fs::last_write_time(this->
data->cacheFile);
90 auto fileTime = fs::last_write_time(
file);
91 const auto oldestValidCache = decltype(fileTime)::clock::now() - std::chrono::duration<long double>(validity.
sec);
93 return oldestValidCache <= fileTime;