cache.cpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 // SPDX-FileCopyrightText: Czech Technical University in Prague
3 
4 #include <cstdlib>
5 #include <sstream>
6 #include <string>
7 #include CXX_FILESYSTEM_INCLUDE
8 
9 #include <curl/curl.h>
10 
13 #include <gnss_info/cache.h>
14 
15 namespace fs = CXX_FILESYSTEM_NAMESPACE;
16 
17 namespace gnss_info
18 {
19 
20 std::string getCacheDir()
21 {
22  std::string cacheDir;
23  const auto envDir = std::getenv("GNSS_INFO_CACHE_DIR");
24  if (envDir != nullptr)
25  {
26  cacheDir = envDir;
27  }
28  else
29  {
30 #ifdef _WIN32
31  const std::string homeDirEnv {"USERPROFILE"};
32 #else
33  const std::string homeDirEnv{"HOME"};
34 #endif
35 
36  const auto cacheEnv = std::getenv("XDG_CACHE_HOME");
37  if (cacheEnv != nullptr)
38  cacheDir = cacheEnv;
39  else
40  cacheDir = std::string(std::getenv(homeDirEnv.c_str())) + "/.cache";
41  cacheDir += "/gnss_info";
42  }
43 
44  if (!fs::is_directory(cacheDir))
45  fs::create_directories(cacheDir);
46 
47  return cacheDir;
48 }
49 
50 cras::expected<std::stringstream, std::string> download(const std::string& url,
51  const std::function<void(CURL*)>& curlOptions)
52 {
53  const auto curlCallback = +[](void *contents, size_t size, size_t nmemb, void *userp)
54  {
55  *static_cast<std::stringstream*>(userp) << std::string(static_cast<char*>(contents), size * nmemb);
56  return size * nmemb;
57  };
58 
59  std::stringstream readBuffer;
60  const auto curl = curl_easy_init();
61  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1u);
62  if (curlOptions)
63  curlOptions(curl); // Set extra config options.
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);
69 
70  if (res != CURLE_OK)
71  return cras::make_unexpected(cras::format("Error downloading %s: %s", url.c_str(), curl_easy_strerror(res)));
72 
73  return readBuffer;
74 }
75 
76 bool isCacheFileValid(const std::string& file)
77 {
78  return fs::exists(file);
79 }
80 
81 bool isCacheFileValid(const std::string& file, const ros::WallDuration& validity)
82 {
83  if (!isCacheFileValid(file))
84  return false;
85 
86 #if CXX_FILESYSTEM_IS_BOOST
87  auto fileTime = fs::last_write_time(this->data->cacheFile);
88  const auto oldestValidCache = static_cast<time_t>((ros::WallTime::now() - this->data->cacheValidity).sec);
89 #else
90  auto fileTime = fs::last_write_time(file);
91  const auto oldestValidCache = decltype(fileTime)::clock::now() - std::chrono::duration<long double>(validity.sec);
92 #endif
93  return oldestValidCache <= fileTime;
94 }
95 
96 }
file
page HOWTO subpage DoxygenGuide Documenting Your Code page DoxygenGuide Documenting Your Code todo Flesh out this document section doctips Tips for Documenting When defining make sure that the prototype is identical between the cpp and hpp file
data
data
string_utils.hpp
gnss_info::isCacheFileValid
bool isCacheFileValid(const std::string &file)
Return whether the given cache file can be used.
Definition: cache.cpp:76
ros::WallTime::now
static WallTime now()
CURL
void CURL
Definition: cache.h:13
gnss_info
Definition: cache.h:15
gnss_info::getCacheDir
std::string getCacheDir()
Return path to a directory where cache files should be stored. Also make sure the directory exists.
Definition: cache.cpp:20
cache.h
expected.hpp
gnss_info::download
cras::expected< std::stringstream, std::string > download(const std::string &url, const std::function< void(CURL *)> &curlOptions={})
Download the given URL to a stringstream.
Definition: cache.cpp:50
cras::format
inline ::std::string format(::std::string format, ::va_list args)
ros::WallDuration
DurationBase< WallDuration >::sec
int32_t sec


gnss_info
Author(s): Martin Pecka
autogenerated on Fri Nov 24 2023 03:50:35