VirtualBox

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

Last change on this file since 86663 was 85780, checked in by vboxsync, 4 years ago

VBoxManage/checkupdate: machine readable output. bugref:7983

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.4 KB
Line 
1/* $Id: VBoxManageUpdateCheck.cpp 85780 2020-08-14 21:29:46Z vboxsync $ */
2/** @file
3 * VBoxManage - The 'updatecheck' command.
4 */
5
6/*
7 * Copyright (C) 2020 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#ifndef VBOX_ONLY_DOCS
19
20
21/*********************************************************************************************************************************
22* Header Files *
23*********************************************************************************************************************************/
24#include <VBox/com/com.h>
25#include <VBox/com/ErrorInfo.h>
26#include <VBox/com/errorprint.h>
27#include <VBox/com/VirtualBox.h>
28#include <VBox/com/array.h>
29
30#include <iprt/buildconfig.h>
31#include <VBox/version.h>
32
33#include <VBox/log.h>
34#include <iprt/getopt.h>
35#include <iprt/stream.h>
36#include <iprt/ctype.h>
37#include <iprt/message.h>
38
39#include "VBoxManage.h"
40
41using namespace com; // SafeArray
42
43/**
44 * updatecheck getsettings
45 */
46static RTEXITCODE doVBoxUpdateGetSettings(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox)
47{
48 /*
49 * Parse options.
50 */
51 static const RTGETOPTDEF s_aOptions[] =
52 {
53 { "--machine-readable", 'm', RTGETOPT_REQ_NOTHING }
54 };
55 RTGETOPTSTATE GetState;
56 int vrc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0 /* First */, 0);
57 AssertRCReturn(vrc, RTEXITCODE_INIT);
58
59 bool fMachineReadable = false;
60
61 int c;
62 RTGETOPTUNION ValueUnion;
63 while ((c = RTGetOpt(&GetState, &ValueUnion)) != 0)
64 {
65 switch (c)
66 {
67 case 'm':
68 fMachineReadable = true;
69 break;
70
71 default:
72 return errorGetOpt(c, &ValueUnion);
73 }
74 }
75
76 /*
77 * Do the work.
78 */
79 ComPtr<ISystemProperties> pSystemProperties;
80 CHECK_ERROR2I_RET(aVirtualBox, COMGETTER(SystemProperties)(pSystemProperties.asOutParam()), RTEXITCODE_FAILURE);
81
82 BOOL fVBoxUpdateEnabled;
83 CHECK_ERROR2I_RET(pSystemProperties, COMGETTER(VBoxUpdateEnabled)(&fVBoxUpdateEnabled), RTEXITCODE_FAILURE);
84 if (fMachineReadable)
85 outputMachineReadableBool("enabled", &fVBoxUpdateEnabled);
86 else
87 RTPrintf("Enabled: %s\n", fVBoxUpdateEnabled ? "yes" : "no");
88
89 ULONG cVBoxUpdateCount;
90 CHECK_ERROR2I_RET(pSystemProperties, COMGETTER(VBoxUpdateCount)(&cVBoxUpdateCount), RTEXITCODE_FAILURE);
91 if (fMachineReadable)
92 outputMachineReadableULong("count", &cVBoxUpdateCount);
93 else
94 RTPrintf("Count: %u\n", cVBoxUpdateCount);
95
96 ULONG cDaysFrequencey;
97 CHECK_ERROR2I_RET(pSystemProperties, COMGETTER(VBoxUpdateFrequency)(&cDaysFrequencey), RTEXITCODE_FAILURE);
98 if (fMachineReadable)
99 outputMachineReadableULong("frequency", &cDaysFrequencey);
100 else if (cDaysFrequencey == 0)
101 RTPrintf("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). */
102 else if (cDaysFrequencey == 1)
103 RTPrintf("Frequency: every day\n");
104 else
105 RTPrintf("Frequency: every %u days\n", cDaysFrequencey);
106
107 VBoxUpdateTarget_T enmVBoxUpdateTarget;
108 CHECK_ERROR2I_RET(pSystemProperties, COMGETTER(VBoxUpdateTarget)(&enmVBoxUpdateTarget), RTEXITCODE_FAILURE);
109 const char *psz;
110 const char *pszMachine;
111 switch (enmVBoxUpdateTarget)
112 {
113 case VBoxUpdateTarget_Stable:
114 psz = "Stable - new minor and maintenance releases";
115 pszMachine = "stable";
116 break;
117 case VBoxUpdateTarget_AllReleases:
118 psz = "All releases - new minor, maintenance, and major releases";
119 pszMachine = "all-releases";
120 break;
121 case VBoxUpdateTarget_WithBetas:
122 psz = "With Betas - new minor, maintenance, major, and beta releases";
123 pszMachine = "with-betas";
124 break;
125 default:
126 AssertFailed();
127 psz = "Unset";
128 pszMachine = "invalid";
129 break;
130 }
131 if (fMachineReadable)
132 outputMachineReadableString("target", pszMachine);
133 else
134 RTPrintf("Target: %s\n", psz);
135
136 Bstr bstrLastCheckDate;
137 CHECK_ERROR2I_RET(pSystemProperties, COMGETTER(VBoxUpdateLastCheckDate)(bstrLastCheckDate.asOutParam()),
138 RTEXITCODE_FAILURE);
139 if (fMachineReadable)
140 outputMachineReadableString("last-check-date", &bstrLastCheckDate);
141 else if (bstrLastCheckDate.isNotEmpty())
142 RTPrintf("Last Check Date: %ls\n", bstrLastCheckDate.raw());
143
144 return RTEXITCODE_SUCCESS;
145}
146
147/**
148 * updatecheck modifysettings
149 */
150static RTEXITCODE doVBoxUpdateModifySettings(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox)
151{
152 /*
153 * Parse options.
154 */
155 static const RTGETOPTDEF s_aOptions[] =
156 {
157 { "--enable", 'e', RTGETOPT_REQ_NOTHING },
158 { "--disable", 'd', RTGETOPT_REQ_NOTHING },
159 { "--target", 't', RTGETOPT_REQ_STRING },
160 { "--frequency", 'f', RTGETOPT_REQ_UINT32 },
161 };
162
163 RTGETOPTSTATE GetState;
164 int vrc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0 /* First */, 0);
165 AssertRCReturn(vrc, RTEXITCODE_INIT);
166
167 int fEnabled = -1; /* tristate: -1 (not modified), false, true */
168 VBoxUpdateTarget_T const enmVBoxUpdateTargetNil = (VBoxUpdateTarget_T)-999;
169 VBoxUpdateTarget_T enmVBoxUpdateTarget = enmVBoxUpdateTargetNil;
170 uint32_t cDaysUpdateFrequency = 0;
171
172 int c;
173 RTGETOPTUNION ValueUnion;
174 while ((c = RTGetOpt(&GetState, &ValueUnion)) != 0)
175 {
176 switch (c)
177 {
178 case 'e':
179 fEnabled = true;
180 break;
181
182 case 'd':
183 fEnabled = false;
184 break;
185
186 case 't':
187 if (!RTStrICmp(ValueUnion.psz, "stable"))
188 enmVBoxUpdateTarget = VBoxUpdateTarget_Stable;
189 else if (!RTStrICmp(ValueUnion.psz, "withbetas"))
190 enmVBoxUpdateTarget = VBoxUpdateTarget_WithBetas;
191 else if (!RTStrICmp(ValueUnion.psz, "allreleases"))
192 enmVBoxUpdateTarget = VBoxUpdateTarget_AllReleases;
193 else
194 return errorArgument("Unknown target specified: '%s'", ValueUnion.psz);
195 break;
196
197 case 'f':
198 cDaysUpdateFrequency = ValueUnion.u32;
199 if (cDaysUpdateFrequency == 0)
200 return errorArgument("The update frequency cannot be zero");
201 break;
202
203 default:
204 return errorGetOpt(c, &ValueUnion);
205 }
206 }
207
208 if ( fEnabled == -1
209 && enmVBoxUpdateTarget != enmVBoxUpdateTargetNil
210 && cDaysUpdateFrequency == 0)
211 return errorSyntax("No change requested");
212
213 /*
214 * Make the changes.
215 */
216 ComPtr<ISystemProperties> pSystemProperties;
217 CHECK_ERROR2I_RET(aVirtualBox, COMGETTER(SystemProperties)(pSystemProperties.asOutParam()), RTEXITCODE_FAILURE);
218
219 if (enmVBoxUpdateTarget != enmVBoxUpdateTargetNil)
220 {
221 CHECK_ERROR2I_RET(pSystemProperties, COMSETTER(VBoxUpdateTarget)(enmVBoxUpdateTarget), RTEXITCODE_FAILURE);
222 }
223
224 if (fEnabled != -1)
225 {
226 CHECK_ERROR2I_RET(pSystemProperties, COMSETTER(VBoxUpdateEnabled)((BOOL)fEnabled), RTEXITCODE_FAILURE);
227 }
228
229 if (cDaysUpdateFrequency)
230 {
231 CHECK_ERROR2I_RET(pSystemProperties, COMSETTER(VBoxUpdateFrequency)(cDaysUpdateFrequency), RTEXITCODE_FAILURE);
232 }
233
234 return RTEXITCODE_SUCCESS;
235}
236
237/**
238 * updatecheck perform
239 */
240static RTEXITCODE doVBoxUpdate(int argc, char **argv, ComPtr<IVirtualBox> aVirtualBox)
241{
242 /*
243 * Parse arguments.
244 */
245 static const RTGETOPTDEF s_aOptions[] =
246 {
247 { "--machine-readable", 'm', RTGETOPT_REQ_NOTHING }
248 };
249 RTGETOPTSTATE GetState;
250 int vrc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0 /* First */, 0);
251 AssertRCReturn(vrc, RTEXITCODE_INIT);
252
253 bool fMachineReadable = false;
254
255 int c;
256 RTGETOPTUNION ValueUnion;
257 while ((c = RTGetOpt(&GetState, &ValueUnion)) != 0)
258 {
259 switch (c)
260 {
261 case 'm':
262 fMachineReadable = true;
263 break;
264
265 default:
266 return errorGetOpt(c, &ValueUnion);
267 }
268 }
269
270 /*
271 * Do the work.
272 */
273 ComPtr<IHost> pHost;
274 CHECK_ERROR2I_RET(aVirtualBox, COMGETTER(Host)(pHost.asOutParam()), RTEXITCODE_FAILURE);
275
276 ComPtr<IHostUpdate> pHostUpdate;
277 CHECK_ERROR2I_RET(pHost, COMGETTER(Update)(pHostUpdate.asOutParam()), RTEXITCODE_FAILURE);
278
279 UpdateCheckType_T updateCheckType = UpdateCheckType_VirtualBox;
280 ComPtr<IProgress> ptrProgress;
281
282 RTPrintf("Checking for a new VirtualBox version...\n");
283
284 // we don't call CHECK_ERROR2I_RET(pHostUpdate, VBoxUpdate(updateCheckType, ...); here so we can check for a specific
285 // return value indicating update checks are disabled.
286 HRESULT rc = pHostUpdate->UpdateCheck(updateCheckType, ptrProgress.asOutParam());
287 if (FAILED(rc))
288 {
289/** @todo r=bird: WTF? This makes no sense. I've commented upon this in the
290 * HostUpdateImpl.cpp code too. */
291 if (rc == E_NOTIMPL)
292 {
293 RTPrintf("VirtualBox update checking has been disabled.\n");
294 return RTEXITCODE_SUCCESS;
295 }
296
297 if (ptrProgress.isNull())
298 RTStrmPrintf(g_pStdErr, "Failed to create ptrProgress object: %Rhrc\n", rc);
299 else
300 com::GlueHandleComError(pHostUpdate, "VBoxUpdate(updateCheckType, ptrProgress.asOutParam())", rc, __FILE__, __LINE__);
301 return RTEXITCODE_FAILURE;
302 }
303
304 /* HRESULT hrc = */ showProgress(ptrProgress);
305 CHECK_PROGRESS_ERROR_RET(ptrProgress, ("Check for update failed."), RTEXITCODE_FAILURE);
306
307 BOOL fUpdateNeeded = FALSE;
308 CHECK_ERROR2I_RET(pHostUpdate, COMGETTER(UpdateResponse)(&fUpdateNeeded), RTEXITCODE_FAILURE);
309
310 if (fMachineReadable)
311 outputMachineReadableBool("update-needed", &fUpdateNeeded);
312
313 if (fUpdateNeeded)
314 {
315 Bstr bstrUpdateVersion;
316 CHECK_ERROR2I_RET(pHostUpdate, COMGETTER(UpdateVersion)(bstrUpdateVersion.asOutParam()), RTEXITCODE_FAILURE);
317 Bstr bstrUpdateURL;
318 CHECK_ERROR2I_RET(pHostUpdate, COMGETTER(UpdateURL)(bstrUpdateURL.asOutParam()), RTEXITCODE_FAILURE);
319
320 if (fMachineReadable)
321 RTPrintf("A new version of VirtualBox has been released! Version %ls is available at virtualbox.org.\n"
322 "You can download this version here: %ls\n", bstrUpdateVersion.raw(), bstrUpdateURL.raw());
323 else
324 {
325 outputMachineReadableString("update-version", &bstrUpdateVersion);
326 outputMachineReadableString("update-url", &bstrUpdateURL);
327 }
328 }
329 else if (!fMachineReadable)
330 RTPrintf("You are already running the most recent version of VirtualBox.\n");
331
332 return RTEXITCODE_SUCCESS;
333}
334
335/**
336 * Handles the 'updatecheck' command.
337 *
338 * @returns Appropriate exit code.
339 * @param a Handler argument.
340 */
341RTEXITCODE handleUpdateCheck(HandlerArg *a)
342{
343 if (a->argc < 1)
344 return errorNoSubcommand();
345 if (!strcmp(a->argv[0], "perform"))
346 {
347 setCurrentSubcommand(HELP_SCOPE_UPDATECHECK_PERFORM);
348 return doVBoxUpdate(a->argc - 1, &a->argv[1], a->virtualBox);
349 }
350 if (!strcmp(a->argv[0], "getsettings"))
351 {
352 setCurrentSubcommand(HELP_SCOPE_UPDATECHECK_GETSETTINGS);
353 return doVBoxUpdateGetSettings(a->argc - 1, &a->argv[1], a->virtualBox);
354 }
355 if (!strcmp(a->argv[0], "modifysettings"))
356 {
357 setCurrentSubcommand(HELP_SCOPE_UPDATECHECK_MODIFYSETTINGS);
358 return doVBoxUpdateModifySettings(a->argc - 1, &a->argv[1], a->virtualBox);
359 }
360 return errorUnknownSubcommand(a->argv[0]);
361}
362
363#endif /* !VBOX_ONLY_DOCS */
364/* 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