1 | /* $Id: UIDownloaderGuestAdditions.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIDownloaderGuestAdditions class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes: */
|
---|
29 | #include <QDir>
|
---|
30 | #include <QFile>
|
---|
31 | #include <QRegularExpression>
|
---|
32 | #include <QVariant>
|
---|
33 |
|
---|
34 | /* GUI includes: */
|
---|
35 | #include "QIFileDialog.h"
|
---|
36 | #include "UIDownloaderGuestAdditions.h"
|
---|
37 | #include "UIGlobalSession.h"
|
---|
38 | #include "UIMessageCenter.h"
|
---|
39 | #include "UIModalWindowManager.h"
|
---|
40 | #include "UINetworkReply.h"
|
---|
41 | #include "UINotificationCenter.h"
|
---|
42 | #include "UIVersion.h"
|
---|
43 |
|
---|
44 | /* Other VBox includes: */
|
---|
45 | #include <iprt/sha.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | UIDownloaderGuestAdditions::UIDownloaderGuestAdditions()
|
---|
49 | {
|
---|
50 | /* Get version number and adjust it for test and trunk builds. The server only has official releases. */
|
---|
51 | const QString strVersion = UIVersion(UIVersionInfo::vboxVersionStringNormalized()).effectiveReleasedVersion().toString();
|
---|
52 |
|
---|
53 | /* Prepare source/target: */
|
---|
54 | const QString strSourceName = QString("%1_%2.iso").arg(GUI_GuestAdditionsName, strVersion);
|
---|
55 | const QString strSourcePath = QString("https://download.virtualbox.org/virtualbox/%1/").arg(strVersion);
|
---|
56 | const QString strSource = strSourcePath + strSourceName;
|
---|
57 | const QString strPathSHA256SumsFile = QString("https://www.virtualbox.org/download/hashes/%1/SHA256SUMS").arg(strVersion);
|
---|
58 | const QString strTarget = QDir(gpGlobalSession->homeFolder()).absoluteFilePath(QString("%1.tmp").arg(strSourceName));
|
---|
59 |
|
---|
60 | /* Set source/target: */
|
---|
61 | setSource(strSource);
|
---|
62 | setTarget(strTarget);
|
---|
63 | setPathSHA256SumsFile(strPathSHA256SumsFile);
|
---|
64 | }
|
---|
65 |
|
---|
66 | QString UIDownloaderGuestAdditions::description() const
|
---|
67 | {
|
---|
68 | return UIDownloader::description().arg(tr("VirtualBox Guest Additions"));
|
---|
69 | }
|
---|
70 |
|
---|
71 | bool UIDownloaderGuestAdditions::askForDownloadingConfirmation(UINetworkReply *pReply)
|
---|
72 | {
|
---|
73 | return msgCenter().confirmDownloadGuestAdditions(source().toString(), pReply->header(UINetworkReply::ContentLengthHeader).toInt());
|
---|
74 | }
|
---|
75 |
|
---|
76 | void UIDownloaderGuestAdditions::handleDownloadedObject(UINetworkReply *pReply)
|
---|
77 | {
|
---|
78 | m_receivedData = pReply->readAll();
|
---|
79 | }
|
---|
80 |
|
---|
81 | void UIDownloaderGuestAdditions::handleVerifiedObject(UINetworkReply *pReply)
|
---|
82 | {
|
---|
83 | /* Try to verify the SHA-256 checksum: */
|
---|
84 | QString strCalculatedSumm;
|
---|
85 | bool fSuccess = false;
|
---|
86 | do
|
---|
87 | {
|
---|
88 | /* Read received data into the buffer: */
|
---|
89 | const QByteArray receivedData(pReply->readAll());
|
---|
90 | /* Make sure it's not empty: */
|
---|
91 | if (receivedData.isEmpty())
|
---|
92 | break;
|
---|
93 |
|
---|
94 | /* Parse buffer contents to dictionary: */
|
---|
95 | const QStringList dictionary(QString(receivedData).split("\n", Qt::SkipEmptyParts));
|
---|
96 | /* Make sure it's not empty: */
|
---|
97 | if (dictionary.isEmpty())
|
---|
98 | break;
|
---|
99 |
|
---|
100 | /* Parse each record to tags, look for the required one: */
|
---|
101 | foreach (const QString &strRecord, dictionary)
|
---|
102 | {
|
---|
103 | const QString strFileName = strRecord.section(" *", 1);
|
---|
104 | const QString strDownloadedSumm = strRecord.section(" *", 0, 0);
|
---|
105 | if (strFileName == source().fileName())
|
---|
106 | {
|
---|
107 | /* Calc the SHA-256 on the bytes, creating a string: */
|
---|
108 | uint8_t abHash[RTSHA256_HASH_SIZE];
|
---|
109 | RTSha256(m_receivedData.constData(), m_receivedData.length(), abHash);
|
---|
110 | char szDigest[RTSHA256_DIGEST_LEN + 1];
|
---|
111 | int rc = RTSha256ToString(abHash, szDigest, sizeof(szDigest));
|
---|
112 | if (RT_FAILURE(rc))
|
---|
113 | {
|
---|
114 | AssertRC(rc);
|
---|
115 | szDigest[0] = '\0';
|
---|
116 | }
|
---|
117 | strCalculatedSumm = szDigest;
|
---|
118 | //printf("Downloaded SHA-256 summ: [%s]\n", strDownloadedSumm.toUtf8().constData());
|
---|
119 | //printf("Calculated SHA-256 summ: [%s]\n", strCalculatedSumm.toUtf8().constData());
|
---|
120 | /* Make sure checksum is valid: */
|
---|
121 | fSuccess = strDownloadedSumm == strCalculatedSumm;
|
---|
122 | break;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 | while (false);
|
---|
127 |
|
---|
128 | /* If SHA-256 checksum verification failed: */
|
---|
129 | if (!fSuccess)
|
---|
130 | {
|
---|
131 | /* Warn the user about additions-image was downloaded and saved but checksum is invalid: */
|
---|
132 | UINotificationMessage::cannotValidateGuestAdditionsSHA256Sum(source().toString(), QDir::toNativeSeparators(target()));
|
---|
133 | return;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /* Make sure temporary file exists. If we have
|
---|
137 | * reached this place, it's already written and verified. */
|
---|
138 | const QString strTempFilename = target();
|
---|
139 | if (!QFile::exists(strTempFilename))
|
---|
140 | {
|
---|
141 | /* But still we are providing a failsafe.
|
---|
142 | * Since we can try to write it again. */
|
---|
143 | QFile file(strTempFilename);
|
---|
144 | if (!file.open(QIODevice::WriteOnly))
|
---|
145 | AssertFailedReturnVoid();
|
---|
146 | file.write(m_receivedData);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /* Rename temporary file to target one. This can require a number
|
---|
150 | * of tries to let user choose the place to save file to. */
|
---|
151 | QString strNetTarget = target();
|
---|
152 | strNetTarget.remove(QRegularExpression("\\.tmp$"));
|
---|
153 | setTarget(strNetTarget);
|
---|
154 | while (true)
|
---|
155 | {
|
---|
156 | /* Make sure target file doesn't exist: */
|
---|
157 | bool fTargetFileExists = QFile::exists(target());
|
---|
158 | if (fTargetFileExists)
|
---|
159 | {
|
---|
160 | /* We should ask user about file rewriting (or exit otherwise): */
|
---|
161 | if (!msgCenter().confirmOverridingFile(QDir::toNativeSeparators(target())))
|
---|
162 | break;
|
---|
163 | /* And remove file if rewriting confirmed: */
|
---|
164 | if (QFile::remove(target()))
|
---|
165 | fTargetFileExists = false;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /* Try to rename temporary file to target one (would fail if target file still exists): */
|
---|
169 | const bool fFileRenamed = !fTargetFileExists && QFile::rename(strTempFilename, target());
|
---|
170 |
|
---|
171 | /* If file renamed: */
|
---|
172 | if (fFileRenamed)
|
---|
173 | {
|
---|
174 | /* Warn the user about additions-image downloaded and saved, propose to mount it (and/or exit in any case): */
|
---|
175 | if (msgCenter().proposeMountGuestAdditions(source().toString(), QDir::toNativeSeparators(target())))
|
---|
176 | emit sigDownloadFinished(target());
|
---|
177 | break;
|
---|
178 | }
|
---|
179 | else
|
---|
180 | {
|
---|
181 | /* Warn the user about additions-image was downloaded but was NOT saved: */
|
---|
182 | msgCenter().cannotSaveGuestAdditions(source().toString(), QDir::toNativeSeparators(target()));
|
---|
183 | /* Ask the user for another location for the additions-image file: */
|
---|
184 | const QString strTarget = QIFileDialog::getExistingDirectory(QFileInfo(target()).absolutePath(),
|
---|
185 | windowManager().mainWindowShown(),
|
---|
186 | tr("Select folder to save Guest Additions image to"), true);
|
---|
187 |
|
---|
188 | /* Check if user had really set a new target (and exit in opposite case): */
|
---|
189 | if (!strTarget.isNull())
|
---|
190 | setTarget(QDir(strTarget).absoluteFilePath(QFileInfo(target()).fileName()));
|
---|
191 | else
|
---|
192 | break;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|