fix cache writing to cwd

This commit is contained in:
ouwou 2020-10-27 02:29:02 -04:00
parent 7c31190adc
commit c9162544f6
2 changed files with 9 additions and 0 deletions

View File

@ -9,6 +9,10 @@ Cache::Cache() {
}
Cache::~Cache() {
m_canceled = true;
for (auto &future : m_futures)
if (future.valid()) future.get();
std::error_code err;
if (!std::filesystem::remove_all(m_tmp_path, err))
fprintf(stderr, "error removing tmp dir\n");
@ -45,9 +49,12 @@ void Cache::GetFileFromURL(std::string url, callback_type cb) {
} else {
m_callbacks[url].push_back(cb);
auto future = std::async(std::launch::async, [this, url]() {
if (m_canceled) return;
m_semaphore->wait();
if (m_canceled) return;
const auto &r = cpr::Get(cpr::Url { url });
m_semaphore->notify();
if (m_canceled) return;
OnResponse(r);
});
m_futures.push_back(std::move(future));

View File

@ -29,4 +29,6 @@ private:
std::unordered_map<std::string, std::vector<callback_type>> m_callbacks;
std::vector<std::future<void>> m_futures;
std::filesystem::path m_tmp_path;
bool m_canceled = false;
};