VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageUpdateCheck.cpp@ 94660

Last change on this file since 94660 was 94644, checked in by vboxsync, 3 years ago

Main/Update check: Big overhaul of the API and functionality [build fix]. bugref:7983

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.7 KB
Line 
1/* $Id: VBoxManageUpdateCheck.cpp 94644 2022-04-20 09:15:47Z vboxsync $ */
2/** @file
3 * VBoxManage - The 'updatecheck' command.
4 */
5
6/*
7 * Copyright (C) 2020-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <VBox/com/com.h>
23#include <VBox/com/ErrorInfo.h>
24#include <VBox/com/errorprint.h>
25#include <VBox/com/VirtualBox.h>
26#include <VBox/com/array.h>
27
28#include <iprt/buildconfig.h>
29#include <VBox/version.h>
30
31#include <VBox/log.h>
32#include <iprt/getopt.h>
33#include <iprt/stream.h>
34#include <iprt/ctype.h>
35#include <iprt/message.h>
36
37#include "VBoxManage.h"
38
39DECLARE_TRANSLATION_CONTEXT(UpdateCheck);
40
41using namespace com; // SafeArray
42
43static RTEXITCODE doUpdateList(int argc, char **argv, ComPtr<IUpdateAgent> pUpdateAgent)
44{
45 /*
46 * Parse options.
47 */
48 static const RTGETOPTDEF s_aOptions[] =
49 {
50 { "--machine-readable", 'm', RTGETOPT_REQ_NOTHING }
51 };
52 RTGETOPTSTATE GetState;
53 int vrc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0 /* First */, 0);
54 AssertRCReturn(vrc, RTEXITCODE_INIT);
55
56 bool fMachineReadable = false;
57
58 int c;
59 RTGETOPTUNION ValueUnion;
60 while ((c = RTGetOpt(&GetState, &ValueUnion)) != 0)
61 {
62 switch (c)
63 {
64 case 'm':
65 fMachineReadable = true;
66 break;
67
68 default:
69 return errorGetOpt(c, &ValueUnion);
70 }
71 }
72
73 /*
74 * Do the work.
75 */
76 BOOL fEnabled;
77 CHECK_ERROR2I_RET(pUpdateAgent, COMGETTER(Enabled)(&fEnabled), RTEXITCODE_FAILURE);
78 if (fMachineReadable)
79 outputMachineReadableBool("enabled", &fEnabled);
80 else
81 RTPrintf(UpdateCheck::tr("Enabled: %s\n"),
82 fEnabled ? UpdateCheck::tr("yes") : UpdateCheck::tr("no"));
83
84 ULONG cCheckCount;
85 CHECK_ERROR2I_RET(pUpdateAgent, COMGETTER(CheckCount)(&cCheckCount), RTEXITCODE_FAILURE);
86 if (fMachineReadable)
87 outputMachineReadableULong("count", &cCheckCount);
88 else
89 RTPrintf(UpdateCheck::tr("Count: %u\n"), cCheckCount);
90
91 ULONG uCheckFreqSeconds;
92 CHECK_ERROR2I_RET(pUpdateAgent, COMGETTER(CheckFrequency)(&uCheckFreqSeconds), RTEXITCODE_FAILURE);
93
94 ULONG const uCheckFreqDays = uCheckFreqSeconds / RT_SEC_1DAY;
95
96 if (fMachineReadable)
97 outputMachineReadableULong("frequency", &uCheckFreqSeconds);
98 else if (uCheckFreqDays == 0)
99 RTPrintf(UpdateCheck::tr("Frequency: never\n")); /** @todo r=bird: Two inconsistencies here. HostUpdateImpl.cpp code will indicate the need for updating if no last-check-date. modifysettings cannot set it to zero (I added the error message, you just skipped setting it originally). */
100 else if (uCheckFreqDays == 1)
101 RTPrintf(UpdateCheck::tr("Frequency: every day\n"));
102 else
103 RTPrintf(UpdateCheck::tr("Frequency: every %u days\n"), uCheckFreqDays);
104
105 UpdateChannel_T enmUpdateChannel;
106 CHECK_ERROR2I_RET(pUpdateAgent, COMGETTER(Channel)(&enmUpdateChannel), RTEXITCODE_FAILURE);
107 const char *psz;
108 const char *pszMachine;
109 switch (enmUpdateChannel)
110 {
111 case UpdateChannel_Stable:
112 psz = UpdateCheck::tr("Stable - new minor and maintenance releases");
113 pszMachine = "stable";
114 break;
115 case UpdateChannel_All:
116 psz = UpdateCheck::tr("All releases - new minor, maintenance, and major releases");
117 pszMachine = "all-releases";
118 break;
119 case UpdateChannel_WithBetas:
120 psz = UpdateCheck::tr("With Betas - new minor, maintenance, major, and beta releases");
121 pszMachine = "with-betas";
122 break;
123 default:
124 AssertFailed();
125 psz = UpdateCheck::tr("Unset");
126 pszMachine = "invalid";
127 break;
128 }
129 if (fMachineReadable)
130 outputMachineReadableString("channel", pszMachine);
131 else
132 RTPrintf(UpdateCheck::tr("Channel: %s\n"), psz);
133
134 Bstr bstrLastCheckDate;
135 CHECK_ERROR2I_RET(pUpdateAgent, COMGETTER(LastCheckDate)(bstrLastCheckDate.asOutParam()),
136 RTEXITCODE_FAILURE);
137 if (fMachineReadable)
138 outputMachineReadableString("last-check-date", &bstrLastCheckDate);
139 else if (bstrLastCheckDate.isNotEmpty())
140 RTPrintf(UpdateCheck::tr("Last Check Date: %ls\n"), bstrLastCheckDate.raw());
141
142 return RTEXITCODE_SUCCESS;
143}
144
145static RTEXITCODE doUpdateModify(int argc, char **argv, ComPtr<IUpdateAgent> pUpdateAgent)
146{
147 /*
148 * Parse options.
149 */
150 static const RTGETOPTDEF s_aOptions[] =
151 {
152 { "--enable", 'e', RTGETOPT_REQ_NOTHING },
153 { "--disable", 'd', RTGETOPT_REQ_NOTHING },
154 { "--channel", 'c', RTGETOPT_REQ_STRING },
155 { "--frequency", 'f', RTGETOPT_REQ_UINT32 },
156 };
157
158 RTGETOPTSTATE GetState;
159 int vrc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0 /* First */, 0);
160 AssertRCReturn(vrc, RTEXITCODE_INIT);
161
162 int fEnabled = -1; /* tristate: -1 (not modified), false, true */
163 UpdateChannel_T const enmChannelNil = (UpdateChannel_T)-999;
164 UpdateChannel_T enmChannel = enmChannelNil;
165 uint32_t cFrequencyDays = 0;
166
167 int c;
168 RTGETOPTUNION ValueUnion;
169 while ((c = RTGetOpt(&GetState, &ValueUnion)) != 0)
170 {
171 switch (c)
172 {
173 case 'e':
174 fEnabled = true;
175 break;
176
177 case 'd':
178 fEnabled = false;
179 break;
180
181 case 'c':
182 if (!RTStrICmp(ValueUnion.psz, "stable"))
183 enmChannel = UpdateChannel_Stable;
184 else if (!RTStrICmp(ValueUnion.psz, "withbetas"))
185 enmChannel = UpdateChannel_WithBetas;
186 else if (!RTStrICmp(ValueUnion.psz, "all"))
187 enmChannel = UpdateChannel_All;
188 else
189 return errorArgument(UpdateCheck::tr("Unknown channel specified: '%s'"), ValueUnion.psz);
190 break;
191
192 case 'f':
193 cFrequencyDays = ValueUnion.u32;
194 if (cFrequencyDays == 0)
195 return errorArgument(UpdateCheck::tr("The update frequency cannot be zero"));
196 break;
197
198 default:
199 return errorGetOpt(c, &ValueUnion);
200 }
201 }
202
203 if ( fEnabled == -1
204 && enmChannel != enmChannelNil
205 && cFrequencyDays == 0)
206 return errorSyntax(UpdateCheck::tr("No change requested"));
207
208 /*
209 * Make the changes.
210 */
211 if (enmChannel != enmChannelNil)
212 {
213 CHECK_ERROR2I_RET(pUpdateAgent, COMSETTER(Channel)(enmChannel), RTEXITCODE_FAILURE);
214 }
215 if (fEnabled != -1)
216 {
217 CHECK_ERROR2I_RET(pUpdateAgent, COMSETTER(Enabled)((BOOL)fEnabled), RTEXITCODE_FAILURE);
218 }
219 if (cFrequencyDays)
220 {
221 CHECK_ERROR2I_RET(pUpdateAgent, COMSETTER(CheckFrequency)(cFrequencyDays * RT_SEC_1DAY), RTEXITCODE_FAILURE);
222 }
223
224 return RTEXITCODE_SUCCESS;
225}
226
227static RTEXITCODE doUpdateCheck(int argc, char **argv, ComPtr<IUpdateAgent> pUpdateAgent)
228{
229 /*
230 * Parse arguments.
231 */
232 static const RTGETOPTDEF s_aOptions[] =
233 {
234 { "--machine-readable", 'm', RTGETOPT_REQ_NOTHING }
235 };
236 RTGETOPTSTATE GetState;
237 int vrc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0 /* First */, 0);
238 AssertRCReturn(vrc, RTEXITCODE_INIT);
239
240 bool fMachineReadable = false;
241
242 int c;
243 RTGETOPTUNION ValueUnion;
244 while ((c = RTGetOpt(&GetState, &ValueUnion)) != 0)
245 {
246 switch (c)
247 {
248 case 'm':
249 fMachineReadable = true;
250 break;
251
252 default:
253 return errorGetOpt(c, &ValueUnion);
254 }
255 }
256
257 /*
258 * Do the work.
259 */
260 Bstr bstrName;
261 CHECK_ERROR2I_RET(pUpdateAgent, COMGETTER(Name)(bstrName.asOutParam()), RTEXITCODE_FAILURE);
262
263 if (!fMachineReadable)
264 RTPrintf(UpdateCheck::tr("Checking for a new %ls version...\n"), bstrName.raw());
265
266 /*
267 * We don't call CHECK_ERROR2I_RET(pHostUpdate, VBoxUpdate(updateCheckType, ...); here so we can check for a specific
268 * return value indicating update checks are disabled.
269 */
270 ComPtr<IProgress> pProgress;
271 HRESULT rc = pUpdateAgent->Check(pProgress.asOutParam());
272 if (FAILED(rc))
273 {
274 if (pProgress.isNull())
275 RTStrmPrintf(g_pStdErr, UpdateCheck::tr("Failed to create update progress object: %Rhrc\n"), rc);
276 else
277 com::GlueHandleComError(pUpdateAgent, "HostUpdate(UpdateChannel_Stable, pProgress.asOutParam())",
278 rc, __FILE__, __LINE__);
279 return RTEXITCODE_FAILURE;
280 }
281
282 /* HRESULT hrc = */ showProgress(pProgress, fMachineReadable ? SHOW_PROGRESS_NONE : SHOW_PROGRESS);
283 CHECK_PROGRESS_ERROR_RET(pProgress, (UpdateCheck::tr("Checking for update failed.")), RTEXITCODE_FAILURE);
284
285 UpdateState_T updateState;
286 CHECK_ERROR2I_RET(pUpdateAgent, COMGETTER(State)(&updateState), RTEXITCODE_FAILURE);
287
288 BOOL const fUpdateNeeded = updateState == UpdateState_Available;
289 if (fMachineReadable)
290 outputMachineReadableBool("update-needed", &fUpdateNeeded);
291
292 switch (updateState)
293 {
294 case UpdateState_Available:
295 {
296 Bstr bstrUpdateVersion;
297 CHECK_ERROR2I_RET(pUpdateAgent, COMGETTER(Version)(bstrUpdateVersion.asOutParam()), RTEXITCODE_FAILURE);
298 Bstr bstrUpdateURL;
299 CHECK_ERROR2I_RET(pUpdateAgent, COMGETTER(DownloadUrl)(bstrUpdateURL.asOutParam()), RTEXITCODE_FAILURE);
300
301 if (!fMachineReadable)
302 RTPrintf(UpdateCheck::tr(
303 "A new version of %ls has been released! Version %ls is available at virtualbox.org.\n"
304 "You can download this version here: %ls\n"),
305 bstrName.raw(), bstrUpdateVersion.raw(), bstrUpdateURL.raw());
306 else
307 {
308 outputMachineReadableString("update-version", &bstrUpdateVersion);
309 outputMachineReadableString("update-url", &bstrUpdateURL);
310 }
311
312 break;
313 }
314
315 case UpdateState_NotAvailable:
316 {
317 if (!fMachineReadable)
318 RTPrintf(UpdateCheck::tr("You are already running the most recent version of %ls.\n"), bstrName.raw());
319 break;
320 }
321
322 case UpdateState_Canceled:
323 break;
324
325 case UpdateState_Error:
326 RT_FALL_THROUGH();
327 default:
328 {
329 if (!fMachineReadable)
330 RTPrintf(UpdateCheck::tr("Something went wrong while checking for updates!\n"
331 "Please check network connection and try again later.\n"));
332 break;
333 }
334 }
335
336 return RTEXITCODE_SUCCESS;
337}
338
339/**
340 * Handles the 'updatecheck' command.
341 *
342 * @returns Appropriate exit code.
343 * @param a Handler argument.
344 */
345RTEXITCODE handleUpdateCheck(HandlerArg *a)
346{
347 ComPtr<IHost> pHost;
348 CHECK_ERROR2I_RET(a->virtualBox, COMGETTER(Host)(pHost.asOutParam()), RTEXITCODE_FAILURE);
349
350 ComPtr<IUpdateAgent> pUpdate;
351 CHECK_ERROR2I_RET(pHost, COMGETTER(UpdateHost)(pUpdate.asOutParam()), RTEXITCODE_FAILURE);
352 /** @todo Add other update agents here. */
353
354 if (a->argc < 1)
355 return errorNoSubcommand();
356 if (!RTStrICmp(a->argv[0], "perform"))
357 {
358 setCurrentSubcommand(HELP_SCOPE_UPDATECHECK_PERFORM);
359 return doUpdateCheck(a->argc - 1, &a->argv[1], pUpdate);
360 }
361 if (!RTStrICmp(a->argv[0], "list"))
362 {
363 setCurrentSubcommand(HELP_SCOPE_UPDATECHECK_LIST);
364 return doUpdateList(a->argc - 1, &a->argv[1], pUpdate);
365 }
366 if (!RTStrICmp(a->argv[0], "modify"))
367 {
368 setCurrentSubcommand(HELP_SCOPE_UPDATECHECK_MODIFY);
369 return doUpdateModify(a->argc - 1, &a->argv[1], pUpdate);
370 }
371 return errorUnknownSubcommand(a->argv[0]);
372}
373
374/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette