From d2c103f032a397dbdabfac1bc427a82ec4674864 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Wed, 7 Nov 2018 21:37:42 -0800 Subject: [PATCH] Safe download --- src/connection.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/connection.cpp b/src/connection.cpp index 42ff779..f9af50c 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -48,7 +48,7 @@ void ConnectionLoader::loadConnection() { } else { // Errored out, show error and exit QString explanation = QString() % "Couldn't start the embedded zcashd.\n\n" % - "This is most likely because of corrupt zcash-params. Please delete your zcash-params directory and restart.\n\n" % + "Maybe the zcash-params are corrupt? Please delete your zcash-params directory and restart.\n\n" % (ezcashd ? "The process returned:\n\n" % ezcashd->errorString() : QString("")); this->showError(explanation); } @@ -115,8 +115,6 @@ void ConnectionLoader::downloadParams(std::function cb) { } void ConnectionLoader::doNextDownload(std::function cb) { - qDebug() << "Attempting download"; - auto fnSaveFileName = [&] (QUrl url) { QString path = url.path(); QString basename = QFileInfo(path).fileName(); @@ -138,15 +136,17 @@ void ConnectionLoader::doNextDownload(std::function cb) { QString filename = fnSaveFileName(url); QString paramsDir = zcashParamsDir(); - currentOutput = new QFile(QDir(paramsDir).filePath(filename)); - if (currentOutput->exists()) { + if (QFile(QDir(paramsDir).filePath(filename)).exists()) { qDebug() << filename << " already exists, skipping "; doNextDownload(cb); return; } + // The downloaded file is written to a new name, and then renamed when the operation completes. + currentOutput = new QFile(QDir(paramsDir).filePath(filename + ".part")); + if (!currentOutput->open(QIODevice::WriteOnly)) { this->showError("Couldn't download params. Please check the help site for more info."); } @@ -179,6 +179,9 @@ void ConnectionLoader::doNextDownload(std::function cb) { // Download Finished QObject::connect(currentDownload, &QNetworkReply::finished, [=] () { + // Rename file + currentOutput->rename(QDir(paramsDir).filePath(filename)); + currentOutput->close(); currentDownload->deleteLater(); currentOutput->deleteLater(); @@ -220,7 +223,10 @@ bool ConnectionLoader::startEmbeddedZcashd() { auto zcashdProgram = "zcashd.exe"; #endif - qDebug() << zcashdProgram << QFile(zcashdProgram).exists(); + if (!QFile(zcashdProgram).exists()) { + qDebug() << "Can't find zcashd"; + return false; + } ezcashd = new QProcess(main); ezcashd->setWorkingDirectory(fi.dir().absolutePath());