Safe download

This commit is contained in:
Aditya Kulkarni
2018-11-07 21:37:42 -08:00
parent cfeec8be9e
commit d2c103f032

View File

@@ -48,7 +48,7 @@ void ConnectionLoader::loadConnection() {
} else { } else {
// Errored out, show error and exit // Errored out, show error and exit
QString explanation = QString() % "Couldn't start the embedded zcashd.\n\n" % 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("")); (ezcashd ? "The process returned:\n\n" % ezcashd->errorString() : QString(""));
this->showError(explanation); this->showError(explanation);
} }
@@ -115,8 +115,6 @@ void ConnectionLoader::downloadParams(std::function<void(void)> cb) {
} }
void ConnectionLoader::doNextDownload(std::function<void(void)> cb) { void ConnectionLoader::doNextDownload(std::function<void(void)> cb) {
qDebug() << "Attempting download";
auto fnSaveFileName = [&] (QUrl url) { auto fnSaveFileName = [&] (QUrl url) {
QString path = url.path(); QString path = url.path();
QString basename = QFileInfo(path).fileName(); QString basename = QFileInfo(path).fileName();
@@ -138,15 +136,17 @@ void ConnectionLoader::doNextDownload(std::function<void(void)> cb) {
QString filename = fnSaveFileName(url); QString filename = fnSaveFileName(url);
QString paramsDir = zcashParamsDir(); 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 "; qDebug() << filename << " already exists, skipping ";
doNextDownload(cb); doNextDownload(cb);
return; 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)) { if (!currentOutput->open(QIODevice::WriteOnly)) {
this->showError("Couldn't download params. Please check the help site for more info."); this->showError("Couldn't download params. Please check the help site for more info.");
} }
@@ -179,6 +179,9 @@ void ConnectionLoader::doNextDownload(std::function<void(void)> cb) {
// Download Finished // Download Finished
QObject::connect(currentDownload, &QNetworkReply::finished, [=] () { QObject::connect(currentDownload, &QNetworkReply::finished, [=] () {
// Rename file
currentOutput->rename(QDir(paramsDir).filePath(filename));
currentOutput->close(); currentOutput->close();
currentDownload->deleteLater(); currentDownload->deleteLater();
currentOutput->deleteLater(); currentOutput->deleteLater();
@@ -220,7 +223,10 @@ bool ConnectionLoader::startEmbeddedZcashd() {
auto zcashdProgram = "zcashd.exe"; auto zcashdProgram = "zcashd.exe";
#endif #endif
qDebug() << zcashdProgram << QFile(zcashdProgram).exists(); if (!QFile(zcashdProgram).exists()) {
qDebug() << "Can't find zcashd";
return false;
}
ezcashd = new QProcess(main); ezcashd = new QProcess(main);
ezcashd->setWorkingDirectory(fi.dir().absolutePath()); ezcashd->setWorkingDirectory(fi.dir().absolutePath());