bool AutoUpdater::downloadUpdate() { // Updates only for supported platforms if (platform.isEmpty()) return false; // Get a list of files to update QByteArray newFlistData = getUpdateFlist(); QList<UpdateFileMeta> newFlist = parseFlist(newFlistData); QList<UpdateFileMeta> diff = genUpdateDiff(newFlist); if (abortFlag) return false; qDebug() << "AutoUpdater: Need to update "<<diff.size()<<" files"; // Create an empty directory to download updates into QString updateDirStr = Settings::getInstance().getSettingsDirPath() + "/update/"; QDir updateDir(updateDirStr); if (updateDir.exists()) updateDir.removeRecursively(); QDir().mkdir(updateDirStr); updateDir = QDir(updateDirStr); if (!updateDir.exists()) { qWarning() << "AutoUpdater::downloadUpdate: Can't create update directory, aborting..."; return false; } // Write the new flist for the updater QFile newFlistFile(updateDirStr+"flist"); if (!newFlistFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) { qWarning() << "AutoUpdater::downloadUpdate: Can't save new flist file, aborting..."; return false; } newFlistFile.write(newFlistData); newFlistFile.close(); // Download and write each new file for (UpdateFileMeta fileMeta : diff) { if (abortFlag) return false; qDebug() << "AutoUpdater: Downloading '"+fileMeta.installpath+"' ..."; // Create subdirs if necessary QString fileDirStr{QFileInfo(updateDirStr+fileMeta.installpath).absolutePath()}; if (!QDir(fileDirStr).exists()) QDir().mkpath(fileDirStr); // Download UpdateFile file = getUpdateFile(fileMeta); if (abortFlag) return false; if (file.data.isNull()) { qWarning() << "AutoUpdater::downloadUpdate: Error downloading a file, aborting..."; return false; } // Check signature if (crypto_sign_verify_detached(file.metadata.sig, (unsigned char*)file.data.data(), file.data.size(), key) != 0) { qCritical() << "AutoUpdater: downloadUpdate: RECEIVED FORGED FILE, aborting..."; return false; } // Save QFile fileFile(updateDirStr+fileMeta.installpath); if (!fileFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) { qWarning() << "AutoUpdater::downloadUpdate: Can't save new update file, aborting..."; return false; } fileFile.write(file.data); fileFile.close(); } return true; }
bool AutoUpdater::downloadUpdate() { // Updates only for supported platforms if (platform.isEmpty()) return false; bool expectFalse = false; if (!isDownloadingUpdate.compare_exchange_strong(expectFalse,true)) return false; // Get a list of files to update QByteArray newFlistData = getUpdateFlist(); QList<UpdateFileMeta> newFlist = parseFlist(newFlistData); QList<UpdateFileMeta> diff = genUpdateDiff(newFlist); // Progress progressValue = 0; if (abortFlag) { isDownloadingUpdate = false; return false; } qDebug() << "Need to update" << diff.size() << "files"; // Create an empty directory to download updates into QString updateDirStr = Settings::getInstance().getSettingsDirPath() + "/update/"; QDir updateDir(updateDirStr); if (!updateDir.exists()) QDir().mkdir(updateDirStr); updateDir = QDir(updateDirStr); if (!updateDir.exists()) { qWarning() << "downloadUpdate: Can't create update directory, aborting..."; isDownloadingUpdate = false; return false; } // Write the new flist for the updater QFile newFlistFile(updateDirStr+"flist"); if (!newFlistFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) { qWarning() << "downloadUpdate: Can't save new flist file, aborting..."; isDownloadingUpdate = false; return false; } newFlistFile.write(newFlistData); newFlistFile.close(); progressValue = 1; // Download and write each new file for (UpdateFileMeta fileMeta : diff) { float initialProgress = progressValue, step = 99./diff.size(); auto stepProgressCallback = [&](int current, int total) { progressValue = initialProgress + step * (float)current/total; }; if (abortFlag) { isDownloadingUpdate = false; return false; } // Skip files we already have QFile fileFile(updateDirStr+fileMeta.installpath); if (fileFile.open(QIODevice::ReadOnly) && fileFile.size() == (qint64)fileMeta.size) { qDebug() << "Skipping already downloaded file '" + fileMeta.installpath+ "'"; fileFile.close(); progressValue = initialProgress + step; continue; } qDebug() << "Downloading '" + fileMeta.installpath + "' ..."; // Create subdirs if necessary QString fileDirStr{QFileInfo(updateDirStr+fileMeta.installpath).absolutePath()}; if (!QDir(fileDirStr).exists()) QDir().mkpath(fileDirStr); // Download UpdateFile file = getUpdateFile(fileMeta, stepProgressCallback); if (abortFlag) goto fail; if (file.data.isNull()) { qCritical() << "downloadUpdate: Error downloading a file, aborting..."; goto fail; } // Check signature if (crypto_sign_verify_detached(file.metadata.sig, (unsigned char*)file.data.data(), file.data.size(), key) != 0) { qCritical() << "downloadUpdate: RECEIVED FORGED FILE, aborting..."; goto fail; } // Save if (!fileFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) { qCritical() << "downloadUpdate: Can't save new update file, aborting..."; goto fail; } fileFile.write(file.data); fileFile.close(); progressValue = initialProgress + step; } qDebug() << "downloadUpdate: The update is ready, it'll be installed on the next restart"; isDownloadingUpdate = false; progressValue = 100; return true; fail: isDownloadingUpdate = false; progressValue = 0; setProgressVersion(""); return false; }