QList<AutoUpdater::UpdateFileMeta> AutoUpdater::genUpdateDiff(QList<UpdateFileMeta> updateFlist) { QList<UpdateFileMeta> diff; QList<UpdateFileMeta> localFlist = parseFlist(getLocalFlist()); for (UpdateFileMeta file : updateFlist) if (!localFlist.contains(file)) diff += file; return diff; }
bool AutoUpdater::isUpdateAvailable() { if (isDownloadingUpdate) return false; QString updaterPath = updaterBin.startsWith('/') ? updaterBin : qApp->applicationDirPath()+'/'+updaterBin; if (!QFile::exists(updaterPath)) return false; QByteArray updateFlist = getUpdateFlist(); QList<UpdateFileMeta> diff = genUpdateDiff(parseFlist(updateFlist)); return !diff.isEmpty(); }
bool AutoUpdater::isLocalUpdateReady() { // Updates only for supported platforms if (platform.isEmpty()) return false; // Check that there's an update dir in the first place, valid or not QString updateDirStr = Settings::getInstance().getSettingsDirPath() + "/update/"; QDir updateDir(updateDirStr); if (!updateDir.exists()) return false; // Check that we have a flist and generate a diff QFile updateFlistFile(updateDirStr+"flist"); if (!updateFlistFile.open(QIODevice::ReadOnly)) return false; QByteArray updateFlistData = updateFlistFile.readAll(); updateFlistFile.close(); QList<UpdateFileMeta> updateFlist = parseFlist(updateFlistData); QList<UpdateFileMeta> diff = genUpdateDiff(updateFlist); // If the update wasn't downloaded correctly, redownload it // We don't check signatures to not block qTox too long, the updater will do it anyway for (UpdateFileMeta fileMeta : diff) { if (!QFile::exists(updateDirStr+fileMeta.installpath)) { QtConcurrent::run(&AutoUpdater::downloadUpdate); return false; } QFile f(updateDirStr+fileMeta.installpath); if (f.size() != (int64_t)fileMeta.size) { QtConcurrent::run(&AutoUpdater::downloadUpdate); return false; } } return true; }
bool AutoUpdater::isLocalUpdateReady() { // Updates only for supported platforms if (platform.isEmpty()) return false; if (isDownloadingUpdate) return false; // Check that there's an update dir in the first place, valid or not QString updateDirStr = Settings::getInstance().getSettingsDirPath() + "/update/"; QDir updateDir(updateDirStr); if (!updateDir.exists()) return false; // Check that we have a flist and generate a diff QFile updateFlistFile(updateDirStr+"flist"); if (!updateFlistFile.open(QIODevice::ReadOnly)) return false; QByteArray updateFlistData = updateFlistFile.readAll(); updateFlistFile.close(); QList<UpdateFileMeta> updateFlist = parseFlist(updateFlistData); QList<UpdateFileMeta> diff = genUpdateDiff(updateFlist); // Check that we have every file for (UpdateFileMeta fileMeta : diff) { if (!QFile::exists(updateDirStr+fileMeta.installpath)) return false; QFile f(updateDirStr+fileMeta.installpath); if (f.size() != (int64_t)fileMeta.size) return false; } return true; }
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; }
void Widget::update() { /// 1. Find and parse the update (0-5%) // Check that the dir exists QString updateDirStr = getSettingsDirPath()+"/update/"; QDir updateDir(updateDirStr); if (!updateDir.exists()) fatalError(tr("No update found.")); // Check that we have a flist and that every file on the diff exists QFile updateFlistFile(updateDirStr+"flist"); if (!updateFlistFile.open(QIODevice::ReadOnly)) fatalError(tr("The update is incomplete.")); QByteArray updateFlistData = updateFlistFile.readAll(); updateFlistFile.close(); setProgress(1); QList<UpdateFileMeta> updateFlist = parseFlist(updateFlistData); setProgress(2); QList<UpdateFileMeta> diff = genUpdateDiff(updateFlist); setProgress(4); for (UpdateFileMeta fileMeta : diff) if (!QFile::exists(updateDirStr+fileMeta.installpath)) fatalError(tr("The update is incomplete.")); if (diff.size() == 0) fatalError(tr("The diff list is empty.")); setProgress(5); /// 2. Check the update (5-50%) float checkProgressStep = 45.0/(float)diff.size(); float checkProgress = 5; for (UpdateFileMeta fileMeta : diff) { UpdateFile file; file.metadata = fileMeta; QFile fileFile(updateDirStr+fileMeta.installpath); if (!fileFile.open(QIODevice::ReadOnly)) fatalError(tr("Update files are unreadable.")); file.data = fileFile.readAll(); fileFile.close(); if (file.data.size() != (int)fileMeta.size) fatalError(tr("Update files are corrupted.")); if (crypto_sign_verify_detached(file.metadata.sig, (unsigned char*)file.data.data(), file.data.size(), key) != 0) fatalError(tr("Update files are corrupted.")); checkProgress += checkProgressStep; setProgress(checkProgress); } setProgress(50); /// 3. Install the update (50-95%) float installProgressStep = 45.0/(float)diff.size(); float installProgress = 50; for (UpdateFileMeta fileMeta : diff) { // Backup old files if (QFile(fileMeta.installpath).exists()) { QFile(fileMeta.installpath+".bak").remove(); QFile(fileMeta.installpath).rename(fileMeta.installpath+".bak"); backups.append(fileMeta.installpath); } // Install new ones QDir().mkpath(QFileInfo(fileMeta.installpath).absolutePath()); QFile fileFile(updateDirStr+fileMeta.installpath); if (!fileFile.copy(fileMeta.installpath)) fatalError(tr("Unable to copy the update's files from ")+(updateDirStr+fileMeta.installpath)+" to "+fileMeta.installpath); installProgress += installProgressStep; setProgress(installProgress); } setProgress(95); /// 4. Delete the update and backups (95-100%) deleteUpdate(); setProgress(97); deleteBackups(); setProgress(100); /// 5. Start qTox and exit startQToxAndExit(); }
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; }