From 8670511319bd4f36f7a93c5a6dfbcc2ddb454427 Mon Sep 17 00:00:00 2001 From: Michael Uleysky Date: Tue, 19 Mar 2024 16:08:03 +1000 Subject: [PATCH] Added buffer for error messages in the CURLRAII class --- include/copcat.h | 7 +++---- include/curlfuncs.h | 9 ++++++++- sources/COPERNICUS.cpp | 9 +++++---- sources/COPERNICUS.h | 3 --- src/copcat.cpp | 4 +--- src/mirrorfuncs.cpp | 6 ++---- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/include/copcat.h b/include/copcat.h index db75bcd..ab6d793 100644 --- a/include/copcat.h +++ b/include/copcat.h @@ -14,10 +14,6 @@ class CopernicusCatalog std::unique_ptr cache; CURLRAII chandle; Json::Value catalog; - char curlerr[CURL_ERROR_SIZE]; - - // Download JSON from url - RetVal GetJSON(const MString& url) const; // Download catalog Error GetCatalog(); @@ -28,6 +24,9 @@ class CopernicusCatalog public: CopernicusCatalog(); + // Download JSON from url + RetVal GetJSON(const MString& url) const; + // List of products RetVal> ProductList() const; diff --git a/include/curlfuncs.h b/include/curlfuncs.h index b0f2e63..8c9378d 100644 --- a/include/curlfuncs.h +++ b/include/curlfuncs.h @@ -14,9 +14,16 @@ class CURLRAIIDT class CURLRAII: public std::unique_ptr { + char err[CURL_ERROR_SIZE]; + public: - CURLRAII() { reset(curl_easy_init()); } + CURLRAII() + { + reset(curl_easy_init()); + curl_easy_setopt(*this, CURLOPT_ERRORBUFFER, err); + } operator CURL*() const { return get(); } + const char* Err() const { return err; } }; // Curl writeback function, write to MString diff --git a/sources/COPERNICUS.cpp b/sources/COPERNICUS.cpp index 7ea1e96..82f921b 100644 --- a/sources/COPERNICUS.cpp +++ b/sources/COPERNICUS.cpp @@ -36,6 +36,7 @@ RetVal> COPERNICUSData::ReadRemoteFileList(const MS MString cont; bool next = true; + CURLRAII chandle; while(next) { MString url = bucket + "?list-type=2&prefix=" + prefix; @@ -43,7 +44,7 @@ RetVal> COPERNICUSData::ReadRemoteFileList(const MS cont = ""; auto [data, res] = GetUrl(chandle, url); - if(res != CURLE_OK) return {pref, MString("Can't download ") + url + ": " + curlerr}; + if(res != CURLE_OK) return {pref, MString("Can't download ") + url + ": " + chandle.Err()}; xmlDocPtr doc = xmlReadMemory(data.Buf(), data.Len(), "data.xml", nullptr, 0); if(doc == nullptr) return {pref, MString("Can't download ") + url + ": XML parse error"}; @@ -128,7 +129,7 @@ Error COPERNICUSData::Mirror(const CLArgs& args) const dsets = dlist.Value(); } - CURLRAII dhandle; + CURLRAII chandle; for(const auto& dset: dsets) { michlib::message("Mirroring " + dset); @@ -180,7 +181,7 @@ Error COPERNICUSData::Mirror(const CLArgs& args) const for(size_t i = 0; i < down.size(); i++) { size_t ri = down[i]; - auto err = DownloadFile(dhandle, rfiles[ri], locroot); + auto err = DownloadFile(chandle, rfiles[ri], locroot); if(!err) return err.Add(pref, "Can't download file"); } @@ -195,7 +196,7 @@ Error COPERNICUSData::Mirror(const CLArgs& args) const { size_t ri = upd[i].first; size_t li = upd[i].second; - auto err = UpdateFile(dhandle, rfiles[ri], lfiles[li], locroot); + auto err = UpdateFile(chandle, rfiles[ri], lfiles[li], locroot); if(!err) return err.Add(pref, "Can't update file"); } } diff --git a/sources/COPERNICUS.h b/sources/COPERNICUS.h index ad74053..8c75779 100644 --- a/sources/COPERNICUS.h +++ b/sources/COPERNICUS.h @@ -8,9 +8,6 @@ using michlib::MString; class COPERNICUSData { - CURLRAII chandle; - char curlerr[CURL_ERROR_SIZE]; - // Get remote file list from url RetVal> ReadRemoteFileList(const MString& url) const; diff --git a/src/copcat.cpp b/src/copcat.cpp index 351b7dd..42c83b3 100644 --- a/src/copcat.cpp +++ b/src/copcat.cpp @@ -16,8 +16,6 @@ CopernicusCatalog::CopernicusCatalog() cache.reset(new FakeCache); } - curl_easy_setopt(chandle, CURLOPT_ERRORBUFFER, curlerr); - GetCatalog(); } @@ -158,7 +156,7 @@ RetVal CopernicusCatalog::GetJSON(const MString& url) const { michlib::message(url + " not found in cache, downloading"); auto [out, res] = GetUrl(chandle, url); - if(res != CURLE_OK) return Error(pref, MString("can't download JSON: ") + curlerr); + if(res != CURLE_OK) return Error(pref, MString("can't download JSON: ") + chandle.Err()); cache->Put(url, out, 3600); content = std::move(out); } diff --git a/src/mirrorfuncs.cpp b/src/mirrorfuncs.cpp index 040ba5f..72c5f2b 100644 --- a/src/mirrorfuncs.cpp +++ b/src/mirrorfuncs.cpp @@ -83,9 +83,7 @@ Error DownloadFile(const CURLRAII& chandle, const struct FileInfo& rinfo, const fd.Reset(creat((root + "/" + rinfo.name).Buf(), 0644)); if(!fd) return {pref, "Can't create file " + root + "/" + rinfo.name}; - char errbuf[CURL_ERROR_SIZE]; - int cfd = fd.Get(); - curl_easy_setopt(chandle, CURLOPT_ERRORBUFFER, errbuf); + int cfd = fd.Get(); curl_easy_setopt(chandle, CURLOPT_WRITEFUNCTION, Write2File); curl_easy_setopt(chandle, CURLOPT_WRITEDATA, &cfd); curl_easy_setopt(chandle, CURLOPT_URL, rinfo.url.Buf()); @@ -93,7 +91,7 @@ Error DownloadFile(const CURLRAII& chandle, const struct FileInfo& rinfo, const if(res != CURLE_OK) { unlink((root + "/" + rinfo.name).Buf()); - return {pref, MString("Can't download file: ") + errbuf}; + return {pref, MString("Can't download file: ") + chandle.Err()}; } {