comparison installer.cpp @ 86:230e7433030d

Backup existing files, if found
author Chris Cannam
date Thu, 27 Feb 2020 11:27:02 +0000
parents bdd4317e43de
children 5ac455fba0ed
comparison
equal deleted inserted replaced
85:bdd4317e43de 86:230e7433030d
51 #include <QPainter> 51 #include <QPainter>
52 #include <QFontMetrics> 52 #include <QFontMetrics>
53 #include <QSpacerItem> 53 #include <QSpacerItem>
54 #include <QProgressDialog> 54 #include <QProgressDialog>
55 #include <QThread> 55 #include <QThread>
56 #include <QDateTime>
56 57
57 #include <vamp-hostsdk/PluginHostAdapter.h> 58 #include <vamp-hostsdk/PluginHostAdapter.h>
58 59
59 #include <dataquay/BasicStore.h> 60 #include <dataquay/BasicStore.h>
60 #include <dataquay/RDFException.h> 61 #include <dataquay/RDFException.h>
551 SVCERR << " - relative status: " << relativeStatusLabel(status) << endl; 552 SVCERR << " - relative status: " << relativeStatusLabel(status) << endl;
552 553
553 return status; 554 return status;
554 } 555 }
555 556
557 bool
558 backup(QString filePath, QString backupDir)
559 {
560 QFileInfo file(filePath);
561
562 if (!file.exists()) {
563 return true;
564 }
565
566 if (!QDir(backupDir).exists()) {
567 QDir().mkpath(backupDir);
568 }
569
570 QString backup = backupDir + "/" + file.fileName() + ".bak";
571 SVCERR << "Note: existing file " << filePath
572 << " found, backing up to " << backup << endl;
573 if (!QFile(filePath).rename(backup)) {
574 SVCERR << "Failed to move " << filePath.toStdString()
575 << " to backup " << backup.toStdString() << endl;
576 return false;
577 }
578
579 return true;
580 }
581
556 QString 582 QString
557 installLibrary(LibraryInfo info, QString targetDir) 583 installLibrary(LibraryInfo info, QString targetDir)
558 { 584 {
559 QString library = info.fileName; 585 QString library = info.fileName;
560 QString source = ":out"; 586 QString source = ":out";
561 QFile f(source + "/" + library); 587 QFile f(source + "/" + library);
562 QString destination = targetDir + "/" + library; 588 QString destination = targetDir + "/" + library;
563 589 QString backupDir = targetDir + "/" +
564 if (QFileInfo(destination).exists()) { 590 QString("saved-%1").arg(QDateTime::currentDateTime().toString
565 auto installed = getLibraryPluginVersions(destination); 591 ("yyyyMMdd-hhmmss"));
566 } else { 592
567 SVCERR << "Note: library " << library 593 if (!QDir(targetDir).exists()) {
568 << " is not yet installed, not comparing versions" << endl; 594 QDir().mkpath(targetDir);
569 } 595 }
570 596
571 //!!! if destination exists, move it aside 597 if (!backup(destination, backupDir)) {
598 return QObject::tr("Failed to move aside existing library");
599 }
572 600
573 SVCERR << "Copying " << library.toStdString() << " to " 601 SVCERR << "Copying " << library.toStdString() << " to "
574 << destination.toStdString() << "..." << endl; 602 << destination.toStdString() << "..." << endl;
575 if (!f.copy(destination)) { 603 if (!f.copy(destination)) {
576 SVCERR << "Failed to copy " << library.toStdString() 604 SVCERR << "Failed to copy " << library.toStdString()
591 QDir dir(source); 619 QDir dir(source);
592 auto entries = dir.entryList({ base + "*" }); 620 auto entries = dir.entryList({ base + "*" });
593 for (auto e: entries) { 621 for (auto e: entries) {
594 if (e == library) continue; 622 if (e == library) continue;
595 QString destination = targetDir + "/" + e; 623 QString destination = targetDir + "/" + e;
624 if (!backup(destination, backupDir)) {
625 continue;
626 }
596 SVCERR << "Copying " << e.toStdString() << " to " 627 SVCERR << "Copying " << e.toStdString() << " to "
597 << destination.toStdString() << "..." << endl; 628 << destination.toStdString() << "..." << endl;
598 if (!QFile(source + "/" + e).copy(destination)) { 629 if (!QFile(source + "/" + e).copy(destination)) {
599 SVCERR << "Failed to copy " << e.toStdString() 630 SVCERR << "Failed to copy " << e.toStdString()
600 << " to target " << destination.toStdString() 631 << " to target " << destination.toStdString()
933 964
934 if (toInstall.empty()) { // Cancelled, or nothing selected 965 if (toInstall.empty()) { // Cancelled, or nothing selected
935 return 0; 966 return 0;
936 } 967 }
937 968
938 if (!QDir(target).exists()) {
939 QDir().mkpath(target);
940 }
941
942 QProgressDialog progress(QObject::tr("Installing..."), 969 QProgressDialog progress(QObject::tr("Installing..."),
943 QObject::tr("Stop"), 0, toInstall.size() + 1); 970 QObject::tr("Stop"), 0, toInstall.size() + 1);
944 progress.setMinimumDuration(0); 971 progress.setMinimumDuration(0);
945 972
946 int pval = 0; 973 int pval = 0;