file(DOWNLOAD): Handle write errors

This commit is contained in:
Hanjiang Yu
2025-03-23 10:58:36 +08:00
committed by Brad King
parent 1994393f7a
commit 9476245dcd
5 changed files with 25 additions and 9 deletions

View File

@@ -1703,7 +1703,9 @@ size_t cmWriteToFileCallback(void* ptr, size_t size, size_t nmemb, void* data)
cmsys::ofstream* fout = static_cast<cmsys::ofstream*>(data);
if (fout) {
char const* chPtr = static_cast<char*>(ptr);
fout->write(chPtr, realsize);
if (!fout->write(chPtr, realsize)) {
return CURL_WRITEFUNC_ERROR;
}
}
return realsize;
}
@@ -2283,6 +2285,14 @@ bool HandleDownloadCommand(std::vector<std::string> const& args,
g_curl.release();
::curl_easy_cleanup(curl);
// Explicitly close the file so we can check for write errors.
if (!file.empty()) {
fout.close();
if (!fout) {
res = CURLE_WRITE_ERROR;
}
}
if (!statusVar.empty()) {
std::string m = curl_easy_strerror(res);
if ((res == CURLE_SSL_CONNECT_ERROR ||
@@ -2306,13 +2316,6 @@ bool HandleDownloadCommand(std::vector<std::string> const& args,
status.GetMakefile().AddDefinition(logVar, chunkDebug.data());
}
// Explicitly flush/close so we can measure the md5 accurately.
//
if (!file.empty()) {
fout.flush();
fout.close();
}
// Verify MD5 sum if requested:
//
if (hash) {