Browse Source

Added buffer for error messages in the CURLRAII class

lintest
Michael Uleysky 8 months ago
parent
commit
8670511319
  1. 7
      include/copcat.h
  2. 9
      include/curlfuncs.h
  3. 9
      sources/COPERNICUS.cpp
  4. 3
      sources/COPERNICUS.h
  5. 4
      src/copcat.cpp
  6. 6
      src/mirrorfuncs.cpp

7
include/copcat.h

@ -14,10 +14,6 @@ class CopernicusCatalog
std::unique_ptr<GenericCache> cache;
CURLRAII chandle;
Json::Value catalog;
char curlerr[CURL_ERROR_SIZE];
// Download JSON from url
RetVal<Json::Value> GetJSON(const MString& url) const;
// Download catalog
Error GetCatalog();
@ -28,6 +24,9 @@ class CopernicusCatalog
public:
CopernicusCatalog();
// Download JSON from url
RetVal<Json::Value> GetJSON(const MString& url) const;
// List of products
RetVal<std::vector<MString>> ProductList() const;

9
include/curlfuncs.h

@ -14,9 +14,16 @@ class CURLRAIIDT
class CURLRAII: public std::unique_ptr<CURL, CURLRAIIDT>
{
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

9
sources/COPERNICUS.cpp

@ -36,6 +36,7 @@ RetVal<std::vector<struct FileInfo>> 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<std::vector<struct FileInfo>> 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");
}
}

3
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<std::vector<struct FileInfo>> ReadRemoteFileList(const MString& url) const;

4
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<Json::Value> 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);
}

6
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()};
}
{

Loading…
Cancel
Save