1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
|
#include "updatemanager.h"
#include "services/pluginmanager.h"
#include "services/notifymanager.h"
#include "common/unused.h"
#include <QTemporaryDir>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrl>
#include <QUrlQuery>
#include <QDebug>
#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QProcess>
#include <QThread>
#include <QtConcurrent/QtConcurrent>
#ifdef Q_OS_WIN32
#include "JlCompress.h"
#include <windows.h>
#include <shellapi.h>
#endif
// Note on creating update packages:
// Packages for Linux and MacOSX should be an archive of _contents_ of SQLiteStudio directory,
// while for Windows it should be an archive of SQLiteStudio directory itself.
QString UpdateManager::staticErrorMessage;
UpdateManager::RetryFunction UpdateManager::retryFunction = nullptr;
UpdateManager::UpdateManager(QObject *parent) :
QObject(parent)
{
networkManager = new QNetworkAccessManager(this);
connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));
connect(this, SIGNAL(updatingError(QString)), NOTIFY_MANAGER, SLOT(error(QString)));
}
UpdateManager::~UpdateManager()
{
cleanup();
}
void UpdateManager::checkForUpdates()
{
getUpdatesMetadata(updatesCheckReply);
}
void UpdateManager::update()
{
if (updatesGetUrlsReply || updatesInProgress)
return;
getUpdatesMetadata(updatesGetUrlsReply);
}
QString UpdateManager::getPlatformForUpdate() const
{
#if defined(Q_OS_LINUX)
if (QSysInfo::WordSize == 64)
return "linux64";
else
return "linux32";
#elif defined(Q_OS_WIN)
return "win32";
#elif defined(Q_OS_OSX)
return "macosx";
#else
return QString();
#endif
}
QString UpdateManager::getCurrentVersions() const
{
QJsonArray versionsArray;
QJsonObject arrayEntry;
arrayEntry["component"] = "SQLiteStudio";
arrayEntry["version"] = SQLITESTUDIO->getVersionString();
versionsArray.append(arrayEntry);
for (const PluginManager::PluginDetails& details : PLUGINS->getAllPluginDetails())
{
if (details.builtIn)
continue;
arrayEntry["component"] = details.name;
arrayEntry["version"] = details.versionString;
versionsArray.append(arrayEntry);
}
QJsonObject topObj;
topObj["versions"] = versionsArray;
QJsonDocument doc(topObj);
return QString::fromLatin1(doc.toJson(QJsonDocument::Compact));
}
bool UpdateManager::isPlatformEligibleForUpdate() const
{
return !getPlatformForUpdate().isNull() && getDistributionType() != DistributionType::OS_MANAGED;
}
#if defined(Q_OS_WIN32)
bool UpdateManager::executePreFinalStepWin(const QString &tempDir, const QString &backupDir, const QString &appDir, bool reqAdmin)
{
bool res;
if (reqAdmin)
res = executeFinalStepAsRootWin(tempDir, backupDir, appDir);
else
res = executeFinalStep(tempDir, backupDir, appDir);
if (res)
{
QFileInfo path(qApp->applicationFilePath());
QProcess::startDetached(appDir + "/" + path.fileName(), {WIN_POST_FINAL_UPDATE_OPTION_NAME, tempDir});
}
return res;
}
#endif
void UpdateManager::handleAvailableUpdatesReply(QNetworkReply* reply)
{
if (reply->error() != QNetworkReply::NoError)
{
updatingFailed(tr("An error occurred while checking for updates: %1.").arg(reply->errorString()));
reply->deleteLater();
return;
}
QJsonParseError err;
QByteArray data = reply->readAll();
reply->deleteLater();
QJsonDocument doc = QJsonDocument::fromJson(data, &err);
if (err.error != QJsonParseError::NoError)
{
qWarning() << "Invalid response from update service:" << err.errorString() << "\n" << "The data was:" << QString::fromLatin1(data);
notifyWarn(tr("Could not check available updates, because server responded with invalid message format. It is safe to ignore this warning."));
return;
}
QList<UpdateEntry> updates = readMetadata(doc);
if (updates.size() > 0)
emit updatesAvailable(updates);
else
emit noUpdatesAvailable();
}
void UpdateManager::getUpdatesMetadata(QNetworkReply*& replyStoragePointer)
{
#ifndef NO_AUTO_UPDATES
if (!isPlatformEligibleForUpdate() || replyStoragePointer)
return;
QUrlQuery query;
query.addQueryItem("platform", getPlatformForUpdate());
query.addQueryItem("data", getCurrentVersions());
QUrl url(QString::fromLatin1(updateServiceUrl) + "?" + query.query(QUrl::FullyEncoded));
QNetworkRequest request(url);
replyStoragePointer = networkManager->get(request);
#endif
}
void UpdateManager::handleUpdatesMetadata(QNetworkReply* reply)
{
if (reply->error() != QNetworkReply::NoError)
{
updatingFailed(tr("An error occurred while reading updates metadata: %1.").arg(reply->errorString()));
reply->deleteLater();
return;
}
QJsonParseError err;
QByteArray data = reply->readAll();
reply->deleteLater();
QJsonDocument doc = QJsonDocument::fromJson(data, &err);
if (err.error != QJsonParseError::NoError)
{
qWarning() << "Invalid response from update service for getting metadata:" << err.errorString() << "\n" << "The data was:" << QString::fromLatin1(data);
notifyWarn(tr("Could not download updates, because server responded with invalid message format. "
"You can try again later or download and install updates manually. See <a href=\"%1\">User Manual</a> for details.").arg(manualUpdatesHelpUrl));
return;
}
tempDir = new QTemporaryDir();
if (!tempDir->isValid()) {
notifyWarn(tr("Could not create temporary directory for downloading the update. Updating aborted."));
return;
}
updatesInProgress = true;
updatesToDownload = readMetadata(doc);
totalDownloadsCount = updatesToDownload.size();
totalPercent = 0;
if (totalDownloadsCount == 0)
{
updatingFailed(tr("There was no updates to download. Updating aborted."));
return;
}
downloadUpdates();
}
QList<UpdateManager::UpdateEntry> UpdateManager::readMetadata(const QJsonDocument& doc)
{
QList<UpdateEntry> updates;
UpdateEntry entry;
QJsonObject obj = doc.object();
QJsonArray versionsArray = obj["newVersions"].toArray();
QJsonObject entryObj;
for (const QJsonValue& value : versionsArray)
{
entryObj = value.toObject();
entry.compontent = entryObj["component"].toString();
entry.version = entryObj["version"].toString();
entry.url = entryObj["url"].toString();
updates << entry;
}
return updates;
}
void UpdateManager::downloadUpdates()
{
if (updatesToDownload.size() == 0)
{
QtConcurrent::run(this, &UpdateManager::installUpdates);
return;
}
UpdateEntry entry = updatesToDownload.takeFirst();
currentJobTitle = tr("Downloading: %1").arg(entry.compontent);
emit updatingProgress(currentJobTitle, 0, totalPercent);
QStringList parts = entry.url.split("/");
if (parts.size() < 1)
{
updatingFailed(tr("Could not determinate file name from update URL: %1. Updating aborted.").arg(entry.url));
return;
}
QString path = tempDir->path() + QLatin1Char('/') + parts.last();
currentDownloadFile = new QFile(path);
if (!currentDownloadFile->open(QIODevice::WriteOnly))
{
updatingFailed(tr("Failed to open file '%1' for writting: %2. Updating aborted.").arg(path, currentDownloadFile->errorString()));
return;
}
updatesToInstall[entry.compontent] = path;
QNetworkRequest request(QUrl(entry.url));
updatesGetReply = networkManager->get(request);
connect(updatesGetReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
connect(updatesGetReply, SIGNAL(readyRead()), this, SLOT(readDownload()));
}
void UpdateManager::updatingFailed(const QString& errMsg)
{
cleanup();
updatesInProgress = false;
emit updatingError(errMsg);
}
void UpdateManager::installUpdates()
{
currentJobTitle = tr("Installing updates.");
totalPercent = (totalDownloadsCount - updatesToDownload.size()) * 100 / (totalDownloadsCount + 1);
emit updatingProgress(currentJobTitle, 0, totalPercent);
requireAdmin = doRequireAdminPrivileges();
QTemporaryDir installTempDir;
QString appDirName = QDir(getAppDirPath()).dirName();
QString targetDir = installTempDir.path() + QLatin1Char('/') + appDirName;
if (!copyRecursively(getAppDirPath(), targetDir))
{
updatingFailed(tr("Could not copy current application directory into %1 directory.").arg(installTempDir.path()));
return;
}
emit updatingProgress(currentJobTitle, 40, totalPercent);
int i = 0;
int updatesCnt = updatesToInstall.size();
for (const QString& component : updatesToInstall.keys())
{
if (!installComponent(component, targetDir))
{
cleanup();
updatesInProgress = false;
return;
}
i++;
emit updatingProgress(currentJobTitle, (30 + (50 / updatesCnt * i)), totalPercent);
}
if (!executeFinalStep(targetDir))
{
cleanup();
updatesInProgress = false;
return;
}
currentJobTitle = QString();
totalPercent = 100;
emit updatingProgress(currentJobTitle, 100, totalPercent);
cleanup();
updatesInProgress = false;
#ifdef Q_OS_WIN32
installTempDir.setAutoRemove(false);
#endif
SQLITESTUDIO->setImmediateQuit(true);
qApp->exit(0);
}
bool UpdateManager::executeFinalStep(const QString& tempDir, const QString& backupDir, const QString& appDir)
{
bool isWin = false;
#ifdef Q_OS_WIN32
isWin = true;
// Windows needs to wait for previus process to exit
QThread::sleep(3);
QDir dir(backupDir);
QString dirName = dir.dirName();
dir.cdUp();
if (!dir.mkdir(dirName))
{
staticUpdatingFailed(tr("Could not create directory %1.").arg(backupDir));
return false;
}
#endif
while (!moveDir(appDir, backupDir, isWin))
{
if (!retryFunction)
{
staticUpdatingFailed(tr("Could not rename directory %1 to %2.\nDetails: %3").arg(appDir, backupDir, staticErrorMessage));
return false;
}
if (!retryFunction(tr("Cannot not rename directory %1 to %2.\nDetails: %3").arg(appDir, backupDir, staticErrorMessage)))
return false;
}
if (!moveDir(tempDir, appDir, isWin))
{
if (!moveDir(backupDir, appDir, isWin))
{
staticUpdatingFailed(tr("Could not move directory %1 to %2 and also failed to restore original directory, "
"so the original SQLiteStudio directory is now located at: %3").arg(tempDir, appDir, backupDir));
}
else
{
staticUpdatingFailed(tr("Could not rename directory %1 to %2. Rolled back to the original SQLiteStudio version.").arg(tempDir, appDir));
}
deleteDir(backupDir);
return false;
}
deleteDir(backupDir);
return true;
}
bool UpdateManager::handleUpdateOptions(const QStringList& argList, int& returnCode)
{
if (argList.size() == 5 && argList[1] == UPDATE_OPTION_NAME)
{
bool result = UpdateManager::executeFinalStep(argList[2], argList[3], argList[4]);
if (result)
returnCode = 0;
else
returnCode = 1;
return true;
}
#ifdef Q_OS_WIN32
if (argList.size() == 6 && argList[1] == WIN_PRE_FINAL_UPDATE_OPTION_NAME)
{
bool result = UpdateManager::executePreFinalStepWin(argList[2], argList[3], argList[4], (bool)argList[5].toInt());
if (result)
returnCode = 0;
else
returnCode = -1;
return true;
}
if (argList.size() == 3 && argList[1] == WIN_POST_FINAL_UPDATE_OPTION_NAME)
{
QThread::sleep(1); // to make sure that the previous process has quit
returnCode = 0;
UpdateManager::executePostFinalStepWin(argList[2]);
return true;
}
#endif
return false;
}
QString UpdateManager::getStaticErrorMessage()
{
return staticErrorMessage;
}
bool UpdateManager::executeFinalStep(const QString& tempDir)
{
QString appDir = getAppDirPath();
// Find inexisting dir name next to app dir
QDir backupDir(getBackupDir(appDir));
#if defined(Q_OS_WIN32)
return runAnotherInstanceForUpdate(tempDir, backupDir.absolutePath(), qApp->applicationDirPath(), requireAdmin);
#else
bool res;
if (requireAdmin)
res = executeFinalStepAsRoot(tempDir, backupDir.absolutePath(), appDir);
else
res = executeFinalStep(tempDir, backupDir.absolutePath(), appDir);
if (res)
QProcess::startDetached(qApp->applicationFilePath(), QStringList());
return res;
#endif
}
bool UpdateManager::installComponent(const QString& component, const QString& tempDir)
{
if (!unpackToDir(updatesToInstall[component], tempDir))
{
updatingFailed(tr("Could not unpack component %1 into %2 directory.").arg(component, tempDir));
return false;
}
// In future here we might also delete/change some files, according to some update script.
return true;
}
void UpdateManager::cleanup()
{
safe_delete(currentDownloadFile);
safe_delete(tempDir);
updatesToDownload.clear();
updatesToInstall.clear();
requireAdmin = false;
}
bool UpdateManager::waitForProcess(QProcess& proc)
{
if (!proc.waitForFinished(-1))
{
qDebug() << "Update QProcess timed out.";
return false;
}
if (proc.exitStatus() == QProcess::CrashExit)
{
qDebug() << "Update QProcess finished by crashing.";
return false;
}
if (proc.exitCode() != 0)
{
qDebug() << "Update QProcess finished with code:" << proc.exitCode();
return false;
}
return true;
}
QString UpdateManager::readError(QProcess& proc, bool reverseOrder)
{
QString err = QString::fromLocal8Bit(reverseOrder ? proc.readAllStandardOutput() : proc.readAllStandardError());
if (err.isEmpty())
err = QString::fromLocal8Bit(reverseOrder ? proc.readAllStandardError() : proc.readAllStandardOutput());
QString errStr = proc.errorString();
if (!errStr.isEmpty())
err += "\n" + errStr;
return err;
}
void UpdateManager::staticUpdatingFailed(const QString& errMsg)
{
#if defined(Q_OS_WIN32)
staticErrorMessage = errMsg;
#else
UPDATES->handleStaticFail(errMsg);
#endif
qCritical() << errMsg;
}
bool UpdateManager::executeFinalStepAsRoot(const QString& tempDir, const QString& backupDir, const QString& appDir)
{
#if defined(Q_OS_LINUX)
return executeFinalStepAsRootLinux(tempDir, backupDir, appDir);
#elif defined(Q_OS_WIN32)
return executeFinalStepAsRootWin(tempDir, backupDir, appDir);
#elif defined(Q_OS_MACX)
return executeFinalStepAsRootMac(tempDir, backupDir, appDir);
#else
qCritical() << "Unknown update platform in UpdateManager::executeFinalStepAsRoot() for package" << packagePath;
return false;
#endif
}
#if defined(Q_OS_LINUX)
bool UpdateManager::executeFinalStepAsRootLinux(const QString& tempDir, const QString& backupDir, const QString& appDir)
{
QStringList args = {qApp->applicationFilePath(), UPDATE_OPTION_NAME, tempDir, backupDir, appDir};
QProcess proc;
LinuxPermElevator elevator = findPermElevatorForLinux();
switch (elevator)
{
case LinuxPermElevator::KDESU:
proc.setProgram("kdesu");
args.prepend("-t");
proc.setArguments(args);
break;
case LinuxPermElevator::GKSU:
proc.setProgram("gksu"); // TODO test gksu updates
proc.setArguments(args);
break;
case LinuxPermElevator::PKEXEC:
{
// We call CLI for doing final step, because pkexec runs cmd completly in root env, so there's no X server.
args[0] += "cli";
QStringList newArgs;
for (const QString& arg : args)
newArgs << wrapCmdLineArgument(arg);
QString cmd = "cd " + wrapCmdLineArgument(qApp->applicationDirPath()) +"; " + newArgs.join(" ");
proc.setProgram("pkexec");
proc.setArguments({"sh", "-c", cmd});
}
break;
case LinuxPermElevator::NONE:
updatingFailed(tr("Could not find permissions elevator application to run update as a root. Looked for: %1").arg("kdesu, gksu, pkexec"));
return false;
}
proc.start();
if (!waitForProcess(proc))
{
updatingFailed(tr("Could not execute final updating steps as root: %1").arg(readError(proc, (elevator == LinuxPermElevator::KDESU))));
return false;
}
return true;
}
#endif
#ifdef Q_OS_MACX
bool UpdateManager::executeFinalStepAsRootMac(const QString& tempDir, const QString& backupDir, const QString& appDir)
{
// Prepare script for updater
// osascript -e "do shell script \"stufftorunasroot\" with administrator privileges"
QStringList args = {wrapCmdLineArgument(qApp->applicationFilePath() + "cli"),
UPDATE_OPTION_NAME,
wrapCmdLineArgument(tempDir),
wrapCmdLineArgument(backupDir),
wrapCmdLineArgument(appDir)};
QProcess proc;
QString innerCmd = wrapCmdLineArgument(args.join(" "));
static_qstring(scriptTpl, "do shell script %1 with administrator privileges");
QString scriptCmd = scriptTpl.arg(innerCmd);
// Prepare updater temporary directory
QTemporaryDir updaterDir;
if (!updaterDir.isValid())
{
updatingFailed(tr("Could not execute final updating steps as admin: %1").arg(tr("Cannot create temporary directory for updater.")));
return false;
}
// Create updater script
QString scriptPath = updaterDir.path() + "/UpdateSQLiteStudio.scpt";
QFile updaterScript(scriptPath);
if (!updaterScript.open(QIODevice::WriteOnly))
{
updatingFailed(tr("Could not execute final updating steps as admin: %1").arg(tr("Cannot create updater script file.")));
return false;
}
updaterScript.write(scriptCmd.toLocal8Bit());
updaterScript.close();
// Compile script to updater application
QString updaterApp = updaterDir.path() + "/UpdateSQLiteStudio.app";
proc.setProgram("osacompile");
proc.setArguments({"-o", updaterApp, scriptPath});
proc.start();
if (!waitForProcess(proc))
{
updatingFailed(tr("Could not execute final updating steps as admin: %1").arg(readError(proc)));
return false;
}
// Execute updater
proc.setProgram(updaterApp + "/Contents/MacOS/applet");
proc.setArguments({});
proc.start();
if (!waitForProcess(proc))
{
updatingFailed(tr("Could not execute final updating steps as admin: %1").arg(readError(proc)));
return false;
}
// Validating update
// The updater script will not return error if the user canceled the password prompt.
// We need to check if the update was actually made and return true only then.
if (QDir(tempDir).exists())
{
// Temp dir still exists, so it was not moved by root process
updatingFailed(tr("Updating canceled."));
return false;
}
return true;
}
#endif
#ifdef Q_OS_WIN32
bool UpdateManager::executeFinalStepAsRootWin(const QString& tempDir, const QString& backupDir, const QString& appDir)
{
QString updateBin = qApp->applicationDirPath() + "/" + WIN_UPDATER_BINARY;
QString installFilePath = tempDir + "/" + WIN_INSTALL_FILE;
QFile installFile(installFilePath);
installFile.open(QIODevice::WriteOnly);
QString nl("\n");
installFile.write(UPDATE_OPTION_NAME);
installFile.write(nl.toLocal8Bit());
installFile.write(backupDir.toLocal8Bit());
installFile.write(nl.toLocal8Bit());
installFile.write(appDir.toLocal8Bit());
installFile.write(nl.toLocal8Bit());
installFile.close();
int res = (int)::ShellExecuteA(0, "runas", updateBin.toUtf8().constData(), 0, 0, SW_SHOWNORMAL);
if (res < 32)
{
staticUpdatingFailed(tr("Could not execute final updating steps as administrator."));
return false;
}
// Since I suck as a developer and I cannot implement a simple synchronous app call under Windows
// (QProcess does it somehow, but I'm too lazy to look it up and probably the solution wouldn't be compatible
// with our "privileges elevation" trick above... so after all I think we're stuck with this solution for now),
// I do the workaround here, which makes this process wait for the other process to create the "done"
// file when it's done, so this process knows when the other has ended. This way we can proceed with this
// process and we will delete some directories later on, which were required by that other process.
if (!waitForFileToDisappear(installFilePath, 10))
{
staticUpdatingFailed(tr("Could not execute final updating steps as administrator. Updater startup timed out."));
return false;
}
if (!waitForFileToAppear(appDir + QLatin1Char('/') + WIN_UPDATE_DONE_FILE, 30))
{
staticUpdatingFailed(tr("Could not execute final updating steps as administrator. Updater operation timed out."));
return false;
}
return true;
}
#endif
#if defined(Q_OS_WIN32)
bool UpdateManager::executePostFinalStepWin(const QString &tempDir)
{
QString doneFile = qApp->applicationDirPath() + QLatin1Char('/') + WIN_UPDATE_DONE_FILE;
QFile::remove(doneFile);
QDir dir(tempDir);
dir.cdUp();
if (!deleteDir(dir.absolutePath()))
staticUpdatingFailed(tr("Could not clean up temporary directory %1. You can delete it manually at any time.").arg(dir.absolutePath()));
QProcess::startDetached(qApp->applicationFilePath(), QStringList());
return true;
}
bool UpdateManager::waitForFileToDisappear(const QString &filePath, int seconds)
{
QFile file(filePath);
while (file.exists() && seconds > 0)
{
QThread::sleep(1);
seconds--;
}
return !file.exists();
}
bool UpdateManager::waitForFileToAppear(const QString &filePath, int seconds)
{
QFile file(filePath);
while (!file.exists() && seconds > 0)
{
QThread::sleep(1);
seconds--;
}
return file.exists();
}
bool UpdateManager::runAnotherInstanceForUpdate(const QString &tempDir, const QString &backupDir, const QString &appDir, bool reqAdmin)
{
bool res = QProcess::startDetached(tempDir + "/SQLiteStudio.exe", {WIN_PRE_FINAL_UPDATE_OPTION_NAME, tempDir, backupDir, appDir,
QString::number((int)reqAdmin)});
if (!res)
{
updatingFailed(tr("Could not run new version for continuing update."));
return false;
}
return true;
}
#endif
UpdateManager::LinuxPermElevator UpdateManager::findPermElevatorForLinux()
{
#if defined(Q_OS_LINUX)
QProcess proc;
proc.setProgram("which");
if (!SQLITESTUDIO->getEnv("DISPLAY").isEmpty())
{
proc.setArguments({"kdesu"});
proc.start();
if (waitForProcess(proc))
return LinuxPermElevator::KDESU;
proc.setArguments({"gksu"});
proc.start();
if (waitForProcess(proc))
return LinuxPermElevator::GKSU;
}
proc.setArguments({"pkexec"});
proc.start();
if (waitForProcess(proc))
return LinuxPermElevator::PKEXEC;
#endif
return LinuxPermElevator::NONE;
}
QString UpdateManager::wrapCmdLineArgument(const QString& arg)
{
return "\"" + escapeCmdLineArgument(arg) + "\"";
}
QString UpdateManager::escapeCmdLineArgument(const QString& arg)
{
if (!arg.contains("\\") && !arg.contains("\""))
return arg;
QString str = arg;
return str.replace("\\", "\\\\").replace("\"", "\\\"");
}
QString UpdateManager::getBackupDir(const QString &appDir)
{
static_qstring(bakDirTpl, "%1.old%2");
QDir backupDir(bakDirTpl.arg(appDir, ""));
int cnt = 1;
while (backupDir.exists())
backupDir = QDir(bakDirTpl.arg(appDir, QString::number(cnt)));
return backupDir.absolutePath();
}
bool UpdateManager::unpackToDir(const QString& packagePath, const QString& outputDir)
{
#if defined(Q_OS_LINUX)
return unpackToDirLinux(packagePath, outputDir);
#elif defined(Q_OS_WIN32)
return unpackToDirWin(packagePath, outputDir);
#elif defined(Q_OS_MACX)
return unpackToDirMac(packagePath, outputDir);
#else
qCritical() << "Unknown update platform in UpdateManager::unpackToDir() for package" << packagePath;
return false;
#endif
}
#if defined(Q_OS_LINUX)
bool UpdateManager::unpackToDirLinux(const QString &packagePath, const QString &outputDir)
{
QProcess proc;
proc.setWorkingDirectory(outputDir);
proc.setStandardOutputFile(QProcess::nullDevice());
proc.setStandardErrorFile(QProcess::nullDevice());
if (!packagePath.endsWith("tar.gz"))
{
updatingFailed(tr("Package not in tar.gz format, cannot install: %1").arg(packagePath));
return false;
}
proc.start("mv", {packagePath, outputDir});
if (!waitForProcess(proc))
{
updatingFailed(tr("Package %1 cannot be installed, because cannot move it to directory: %2").arg(packagePath, outputDir));
return false;
}
QString fileName = packagePath.split("/").last();
QString newPath = outputDir + "/" + fileName;
proc.start("tar", {"-xzf", newPath});
if (!waitForProcess(proc))
{
updatingFailed(tr("Package %1 cannot be installed, because cannot unpack it: %2").arg(packagePath, readError(proc)));
return false;
}
QProcess::execute("rm", {"-f", newPath});
return true;
}
#endif
#if defined(Q_OS_MACX)
bool UpdateManager::unpackToDirMac(const QString &packagePath, const QString &outputDir)
{
QProcess proc;
proc.setWorkingDirectory(outputDir);
proc.setStandardOutputFile(QProcess::nullDevice());
proc.setStandardErrorFile(QProcess::nullDevice());
if (!packagePath.endsWith("zip"))
{
updatingFailed(tr("Package not in zip format, cannot install: %1").arg(packagePath));
return false;
}
proc.start("unzip", {"-o", "-d", outputDir, packagePath});
if (!waitForProcess(proc))
{
updatingFailed(tr("Package %1 cannot be installed, because cannot unzip it to directory %2: %3")
.arg(packagePath, outputDir, readError(proc)));
return false;
}
return true;
}
#endif
#if defined(Q_OS_WIN32)
bool UpdateManager::unpackToDirWin(const QString& packagePath, const QString& outputDir)
{
if (JlCompress::extractDir(packagePath, outputDir + "/..").isEmpty())
{
updatingFailed(tr("Package %1 cannot be installed, because cannot unzip it to directory: %2").arg(packagePath, outputDir));
return false;
}
return true;
}
#endif
void UpdateManager::handleStaticFail(const QString& errMsg)
{
emit updatingFailed(errMsg);
}
QString UpdateManager::getAppDirPath() const
{
static QString appDir;
if (appDir.isNull())
{
appDir = qApp->applicationDirPath();
#ifdef Q_OS_MACX
QDir tmpAppDir(appDir);
tmpAppDir.cdUp();
tmpAppDir.cdUp();
appDir = tmpAppDir.absolutePath();
#endif
}
return appDir;
}
bool UpdateManager::moveDir(const QString& src, const QString& dst, bool contentsOnly)
{
// If we're doing a rename in the very same parent directory then we don't want
// the 'move between partitions' to be involved, cause any failure to rename
// is due to permissions or file lock.
QFileInfo srcFi(src);
QFileInfo dstFi(dst);
bool sameParentDir = (srcFi.dir() == dstFi.dir());
QDir dir;
if (contentsOnly)
{
QString localSrc;
QString localDst;
QDir srcDir(src);
for (const QFileInfo& entry : srcDir.entryInfoList(QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot|QDir::Hidden|QDir::System))
{
localSrc = entry.absoluteFilePath();
localDst = dst + "/" + entry.fileName();
if (!dir.rename(localSrc, localDst) && (sameParentDir || !renameBetweenPartitions(localSrc, localDst)))
{
staticUpdatingFailed(tr("Could not rename directory %1 to %2.").arg(localSrc, localDst));
return false;
}
}
}
else
{
if (!dir.rename(src, dst) && (sameParentDir || !renameBetweenPartitions(src, dst)))
{
staticUpdatingFailed(tr("Could not rename directory %1 to %2.").arg(src, dst));
return false;
}
}
return true;
}
bool UpdateManager::deleteDir(const QString& path)
{
QDir dir(path);
if (!dir.removeRecursively())
{
staticUpdatingFailed(tr("Could not delete directory %1.").arg(path));
return false;
}
return true;
}
bool UpdateManager::execCmd(const QString& cmd, const QStringList& args, QString* errorMsg)
{
QProcess proc;
proc.start(cmd, args);
QString cmdString = QString("%1 \"%2\"").arg(cmd, args.join("\\\" \\\""));
if (!waitForProcess(proc))
{
if (errorMsg)
*errorMsg = tr("Error executing update command: %1\nError message: %2").arg(cmdString).arg(readError(proc));
return false;
}
return true;
}
void UpdateManager::setRetryFunction(const RetryFunction &value)
{
retryFunction = value;
}
bool UpdateManager::doRequireAdminPrivileges()
{
QString appDirPath = getAppDirPath();
QDir appDir(appDirPath);
bool isWritable = isWritableRecursively(appDir.absolutePath());
appDir.cdUp();
QFileInfo fi(appDir.absolutePath());
isWritable &= fi.isWritable();
if (isWritable)
{
QDir backupDir(getBackupDir(appDirPath));
QString backupDirName = backupDir.dirName();
backupDir.cdUp();
if (backupDir.mkdir(backupDirName))
backupDir.rmdir(backupDirName);
else
isWritable = false;
}
return !isWritable;
}
void UpdateManager::finished(QNetworkReply* reply)
{
if (reply == updatesCheckReply)
{
updatesCheckReply = nullptr;
handleAvailableUpdatesReply(reply);
return;
}
if (reply == updatesGetUrlsReply)
{
updatesGetUrlsReply = nullptr;
handleUpdatesMetadata(reply);
return;
}
if (reply == updatesGetReply)
{
handleDownloadReply(reply);
if (reply == updatesGetReply) // if no new download is requested
updatesGetReply = nullptr;
return;
}
}
void UpdateManager::handleDownloadReply(QNetworkReply* reply)
{
if (reply->error() != QNetworkReply::NoError)
{
updatingFailed(tr("An error occurred while downloading updates: %1. Updating aborted.").arg(reply->errorString()));
reply->deleteLater();
return;
}
totalPercent = (totalDownloadsCount - updatesToDownload.size()) * 100 / (totalDownloadsCount + 1);
readDownload();
currentDownloadFile->close();
safe_delete(currentDownloadFile);
reply->deleteLater();
downloadUpdates();
}
void UpdateManager::downloadProgress(qint64 bytesReceived, qint64 totalBytes)
{
int perc;
if (totalBytes < 0)
perc = -1;
else if (totalBytes == 0)
perc = 100;
else
perc = bytesReceived * 100 / totalBytes;
emit updatingProgress(currentJobTitle, perc, totalPercent);
}
void UpdateManager::readDownload()
{
currentDownloadFile->write(updatesGetReply->readAll());
}
|