summaryrefslogtreecommitdiff
path: root/plugins/PluginUpdater/src/Utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/PluginUpdater/src/Utils.cpp')
-rw-r--r--plugins/PluginUpdater/src/Utils.cpp87
1 files changed, 47 insertions, 40 deletions
diff --git a/plugins/PluginUpdater/src/Utils.cpp b/plugins/PluginUpdater/src/Utils.cpp
index f76e47237e..9d05d695b1 100644
--- a/plugins/PluginUpdater/src/Utils.cpp
+++ b/plugins/PluginUpdater/src/Utils.cpp
@@ -49,12 +49,15 @@ bool ParseHashes(const wchar_t *pwszUrl, ptrW &baseUrl, SERVLIST &arHashes)
pFileUrl.CRCsum = 0;
HNETLIBCONN nlc = nullptr;
- bool ret = DownloadFile(&pFileUrl, nlc);
+ int ret = DownloadFile(&pFileUrl, nlc);
Netlib_CloseHandle(nlc);
- if (!ret) {
- Netlib_LogfW(g_hNetlibUser, L"Downloading list of available updates from %s failed", baseUrl.get());
- ShowPopup(TranslateT("Plugin Updater"), TranslateT("An error occurred while checking for new updates."), POPUP_TYPE_ERROR);
+ if (ret != ERROR_SUCCESS) {
+ Netlib_LogfW(g_hNetlibUser, L"Downloading list of available updates from %s failed with error %d", baseUrl.get(), ret);
+ if (ret == 404)
+ ShowPopup(TranslateT("Plugin Updater"), TranslateT("Updates are temporarily disabled, try again later."), POPUP_TYPE_INFO);
+ else
+ ShowPopup(TranslateT("Plugin Updater"), TranslateT("An error occurred while checking for new updates."), POPUP_TYPE_ERROR);
Skin_PlaySound("updatefailed");
return false;
}
@@ -122,7 +125,7 @@ bool ParseHashes(const wchar_t *pwszUrl, ptrW &baseUrl, SERVLIST &arHashes)
/////////////////////////////////////////////////////////////////////////////////////////
// Single file HTTP transaction
-bool DownloadFile(FILEURL *pFileURL, HNETLIBCONN &nlc)
+int DownloadFile(FILEURL *pFileURL, HNETLIBCONN &nlc)
{
char szMirVer[100];
Miranda_GetVersionText(szMirVer, _countof(szMirVer));
@@ -162,49 +165,53 @@ bool DownloadFile(FILEURL *pFileURL, HNETLIBCONN &nlc)
for (int i = 0; i < MAX_RETRIES; i++) {
Netlib_LogfW(g_hNetlibUser, L"Downloading file %s to %s (attempt %d)", pFileURL->wszDownloadURL, pFileURL->wszDiskPath, i + 1);
NLHR_PTR pReply(Netlib_HttpTransaction(g_hNetlibUser, &nlhr));
- if (pReply) {
- nlc = pReply->nlc;
- if ((200 == pReply->resultCode) && (pReply->dataLength > 0)) {
- // Check CRC sum
- if (pFileURL->CRCsum) {
- int crc = crc32(0, (unsigned char *)pReply->pData, pReply->dataLength);
- if (crc != pFileURL->CRCsum) {
- // crc check failed, try again
- Netlib_LogfW(g_hNetlibUser, L"crc check failed for file %s", pFileURL->wszDiskPath);
- continue;
- }
- }
+ if (pReply == nullptr) {
+ Netlib_LogfW(g_hNetlibUser, L"Downloading file %s failed, host is propably temporary down.", pFileURL->wszDownloadURL);
+ nlc = nullptr;
+ continue;
+ }
- DWORD dwBytes;
- HANDLE hFile = CreateFile(pFileURL->wszDiskPath, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
- if (hFile != INVALID_HANDLE_VALUE) {
- // write the downloaded file directly
- WriteFile(hFile, pReply->pData, (DWORD)pReply->dataLength, &dwBytes, nullptr);
- CloseHandle(hFile);
- }
- else {
- // try to write it via PU stub
- TFileName wszTempFile;
- mir_snwprintf(wszTempFile, L"%s\\pulocal.tmp", g_wszTempPath);
- hFile = CreateFile(wszTempFile, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
- if (hFile != INVALID_HANDLE_VALUE) {
- WriteFile(hFile, pReply->pData, (DWORD)pReply->dataLength, &dwBytes, nullptr);
- CloseHandle(hFile);
- PU::SafeMoveFile(wszTempFile, pFileURL->wszDiskPath);
- }
- }
- return true;
- }
+ nlc = pReply->nlc;
+ if (pReply->resultCode != 200 || pReply->dataLength <= 0) {
Netlib_LogfW(g_hNetlibUser, L"Downloading file %s failed with error %d", pFileURL->wszDownloadURL, pReply->resultCode);
+ return pReply->resultCode;
+ }
+
+ // Check CRC sum
+ if (pFileURL->CRCsum) {
+ int crc = crc32(0, (unsigned char *)pReply->pData, pReply->dataLength);
+ if (crc != pFileURL->CRCsum) {
+ // crc check failed, try again
+ Netlib_LogfW(g_hNetlibUser, L"crc check failed for file %s", pFileURL->wszDiskPath);
+ continue;
+ }
+ }
+
+ // everything is ok, write file down
+ DWORD dwBytes;
+ HANDLE hFile = CreateFile(pFileURL->wszDiskPath, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
+ if (hFile != INVALID_HANDLE_VALUE) {
+ // write the downloaded file directly
+ WriteFile(hFile, pReply->pData, (DWORD)pReply->dataLength, &dwBytes, nullptr);
+ CloseHandle(hFile);
}
else {
- Netlib_LogfW(g_hNetlibUser, L"Downloading file %s failed, host is propably temporary down.", pFileURL->wszDownloadURL);
- nlc = nullptr;
+ // try to write it via PU stub
+ TFileName wszTempFile;
+ mir_snwprintf(wszTempFile, L"%s\\pulocal.tmp", g_wszTempPath);
+ hFile = CreateFile(wszTempFile, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
+ if (hFile != INVALID_HANDLE_VALUE) {
+ WriteFile(hFile, pReply->pData, (DWORD)pReply->dataLength, &dwBytes, nullptr);
+ CloseHandle(hFile);
+ PU::SafeMoveFile(wszTempFile, pFileURL->wszDiskPath);
+ }
}
+ return ERROR_SUCCESS;
}
+ // no more retries, return previous error code
Netlib_LogfW(g_hNetlibUser, L"Downloading file %s failed, giving up", pFileURL->wszDownloadURL);
- return false;
+ return 500;
}
/////////////////////////////////////////////////////////////////////////////////////////