VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceUtils.cpp@ 28243

Last change on this file since 28243 was 26140, checked in by vboxsync, 15 years ago

VBoxServiceUtils.cpp: dwords are unsigned.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1/* $Id: VBoxServiceUtils.cpp 26140 2010-02-02 10:59:34Z vboxsync $ */
2/** @file
3 * VBoxServiceUtils - Some utility functions.
4 */
5
6/*
7 * Copyright (C) 2009-2010 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#ifdef RT_OS_WINDOWS
27# include <Windows.h>
28# include <iprt/param.h>
29# include <iprt/path.h>
30#endif
31#include <iprt/assert.h>
32#include <iprt/mem.h>
33#include <iprt/string.h>
34
35#include <VBox/VBoxGuestLib.h>
36#include "VBoxServiceInternal.h"
37
38#ifdef VBOX_WITH_GUEST_PROPS
39
40/**
41 * Reads a guest property.
42 *
43 * @returns VBox status code, fully bitched.
44 *
45 * @param u32ClientId The HGCM client ID for the guest property session.
46 * @param pszPropName The property name.
47 * @param ppszValue Where to return the value. This is always set
48 * to NULL. Free it using RTStrFree().
49 * @param ppszFlags Where to return the value flags. Free it
50 * using RTStrFree(). Optional.
51 * @param puTimestamp Where to return the timestamp. This is only set
52 * on success. Optional.
53 */
54int VBoxServiceReadProp(uint32_t u32ClientId, const char *pszPropName, char **ppszValue, char **ppszFlags, uint64_t *puTimestamp)
55{
56 uint32_t cbBuf = _1K;
57 void *pvBuf = NULL;
58 int rc;
59
60 *ppszValue = NULL;
61
62 for (unsigned cTries = 0; cTries < 10; cTries++)
63 {
64 /*
65 * (Re-)Allocate the buffer and try read the property.
66 */
67 RTMemFree(pvBuf);
68 pvBuf = RTMemAlloc(cbBuf);
69 if (!pvBuf)
70 {
71 VBoxServiceError("Guest Property: Failed to allocate %zu bytes\n", cbBuf);
72 rc = VERR_NO_MEMORY;
73 break;
74 }
75 char *pszValue;
76 char *pszFlags;
77 uint64_t uTimestamp;
78 rc = VbglR3GuestPropRead(u32ClientId, pszPropName,
79 pvBuf, cbBuf,
80 &pszValue, &uTimestamp, &pszFlags, NULL);
81 if (RT_FAILURE(rc))
82 {
83 if (rc == VERR_BUFFER_OVERFLOW)
84 {
85 /* try again with a bigger buffer. */
86 cbBuf *= 2;
87 continue;
88 }
89 if (rc == VERR_NOT_FOUND)
90 VBoxServiceVerbose(2, "Guest Property: %s not found\n", pszPropName);
91 else
92 VBoxServiceError("Guest Property: Failed to query \"%s\": %Rrc\n", pszPropName, rc);
93 break;
94 }
95
96 VBoxServiceVerbose(2, "Guest Property: Read \"%s\" = \"%s\", timestamp %RU64n\n",
97 pszPropName, pszValue, uTimestamp);
98 *ppszValue = RTStrDup(pszValue);
99 if (!*ppszValue)
100 {
101 VBoxServiceError("Guest Property: RTStrDup failed for \"%s\"\n", pszValue);
102 rc = VERR_NO_MEMORY;
103 break;
104 }
105
106 if (puTimestamp)
107 *puTimestamp = uTimestamp;
108 if (ppszFlags)
109 *ppszFlags = RTStrDup(pszFlags);
110 break; /* done */
111 }
112
113 RTMemFree(pvBuf);
114 return rc;
115}
116
117
118/**
119 * Reads a guest property as a 32-bit value.
120 *
121 * @returns VBox status code, fully bitched.
122 *
123 * @param u32ClientId The HGCM client ID for the guest property session.
124 * @param pszPropName The property name.
125 * @param pu32 Where to store the 32-bit value.
126 *
127 */
128int VBoxServiceReadPropUInt32(uint32_t u32ClientId, const char *pszPropName, uint32_t *pu32, uint32_t u32Min, uint32_t u32Max)
129{
130 char *pszValue;
131 int rc = VBoxServiceReadProp(u32ClientId, pszPropName, &pszValue,
132 NULL /* ppszFlags */, NULL /* puTimestamp */);
133 if (RT_SUCCESS(rc))
134 {
135 AssertPtr(pu32);
136 char *pszNext;
137 rc = RTStrToUInt32Ex(pszValue, &pszNext, 0, pu32);
138 if ( RT_SUCCESS(rc)
139 && (*pu32 < u32Min || *pu32 > u32Max))
140 {
141 rc = VBoxServiceError("The guest property value %s = %RU32 is out of range [%RU32..%RU32].\n",
142 pszPropName, *pu32, u32Min, u32Max);
143 }
144 RTStrFree(pszValue);
145 }
146 return rc;
147}
148
149
150/**
151 * Wrapper around VbglR3GuestPropWriteValue that does value formatting and
152 * logging.
153 *
154 * @returns VBox status code. Errors will be logged.
155 *
156 * @param u32ClientId The HGCM client ID for the guest property session.
157 * @param pszName The property name.
158 * @param pszValueFormat The property format string. If this is NULL then
159 * the property will be deleted (if possible).
160 * @param ... Format arguments.
161 */
162int VBoxServiceWritePropF(uint32_t u32ClientId, const char *pszName, const char *pszValueFormat, ...)
163{
164 AssertPtr(pszName);
165 int rc;
166 if (pszValueFormat != NULL)
167 {
168 va_list va;
169 va_start(va, pszValueFormat);
170 VBoxServiceVerbose(3, "Writing guest property \"%s\" = \"%N\"\n", pszName, pszValueFormat, &va);
171 va_end(va);
172
173 va_start(va, pszValueFormat);
174 rc = VbglR3GuestPropWriteValueV(u32ClientId, pszName, pszValueFormat, va);
175 va_end(va);
176
177 if (RT_FAILURE(rc))
178 VBoxServiceError("Error writing guest property \"%s\" (rc=%Rrc)\n", pszName, rc);
179 }
180 else
181 {
182 VBoxServiceVerbose(3, "Deleting guest property \"%s\"\n", pszName);
183 rc = VbglR3GuestPropWriteValue(u32ClientId, pszName, NULL);
184 if (RT_FAILURE(rc))
185 VBoxServiceError("Error deleting guest property \"%s\" (rc=%Rrc)\n", pszName, rc);
186 }
187 return rc;
188}
189
190#endif /* VBOX_WITH_GUEST_PROPS */
191#ifdef RT_OS_WINDOWS
192
193/**
194 * Helper for VBoxServiceGetFileVersion and attemps to read and parse
195 * FileVersion.
196 *
197 * @returns Success indicator.
198 */
199static bool VBoxServiceGetFileVersionOwn(LPSTR pVerData,
200 PDWORD pdwMajor,
201 PDWORD pdwMinor,
202 PDWORD pdwBuildNumber,
203 PDWORD pdwRevisionNumber)
204{
205 UINT cchStrValue = 0;
206 LPTSTR pStrValue = NULL;
207 if (!VerQueryValueA(pVerData, "\\StringFileInfo\\040904b0\\FileVersion", (LPVOID *)&pStrValue, &cchStrValue))
208 return false;
209
210 /** @todo r=bird: get rid of this. Avoid sscanf like the plague! */
211 if (sscanf(pStrValue, "%ld.%ld.%ld.%ld", pdwMajor, pdwMinor, pdwBuildNumber, pdwRevisionNumber) != 4)
212 return false;
213
214 return true;
215}
216
217
218/**
219 * Worker for VBoxServiceGetFileVersionString.
220 *
221 * @returns VBox status code.
222 * @param pszFilename ASCII & ANSI & UTF-8 compliant name.
223 */
224static int VBoxServiceGetFileVersion(const char *pszFilename,
225 PDWORD pdwMajor,
226 PDWORD pdwMinor,
227 PDWORD pdwBuildNumber,
228 PDWORD pdwRevisionNumber)
229{
230 int rc;
231
232 *pdwMajor = *pdwMinor = *pdwBuildNumber = *pdwRevisionNumber = 0;
233
234 /*
235 * Get the file version info.
236 */
237 DWORD dwHandleIgnored;
238 DWORD cbVerData = GetFileVersionInfoSizeA(pszFilename, &dwHandleIgnored);
239 if (cbVerData)
240 {
241 LPTSTR pVerData = (LPTSTR)RTMemTmpAllocZ(cbVerData);
242 if (pVerData)
243 {
244 if (GetFileVersionInfoA(pszFilename, dwHandleIgnored, cbVerData, pVerData))
245 {
246 /*
247 * Try query and parse the FileVersion string our selves first
248 * since this will give us the correct revision number when
249 * it goes beyond the range of an uint16_t / WORD.
250 */
251 if (VBoxServiceGetFileVersionOwn(pVerData, pdwMajor, pdwMinor, pdwBuildNumber, pdwRevisionNumber))
252 rc = VINF_SUCCESS;
253 else
254 {
255 /* Fall back on VS_FIXEDFILEINFO */
256 UINT cbFileInfoIgnored = 0;
257 VS_FIXEDFILEINFO *pFileInfo = NULL;
258 if (VerQueryValue(pVerData, "\\", (LPVOID *)&pFileInfo, &cbFileInfoIgnored))
259 {
260 *pdwMajor = HIWORD(pFileInfo->dwFileVersionMS);
261 *pdwMinor = LOWORD(pFileInfo->dwFileVersionMS);
262 *pdwBuildNumber = HIWORD(pFileInfo->dwFileVersionLS);
263 *pdwRevisionNumber = LOWORD(pFileInfo->dwFileVersionLS);
264 rc = VINF_SUCCESS;
265 }
266 else
267 {
268 rc = RTErrConvertFromWin32(GetLastError());
269 VBoxServiceVerbose(3, "No file version value for file \"%s\" available! (%d / rc=%Rrc)\n",
270 pszFilename, GetLastError(), rc);
271 }
272 }
273 }
274 else
275 {
276 rc = RTErrConvertFromWin32(GetLastError());
277 VBoxServiceVerbose(0, "GetFileVersionInfo(%s) -> %u / %Rrc\n", pszFilename, GetLastError(), rc);
278 }
279
280 RTMemTmpFree(pVerData);
281 }
282 else
283 {
284 VBoxServiceVerbose(0, "Failed to allocate %u byte for file version info for '%s'\n", cbVerData, pszFilename);
285 rc = VERR_NO_TMP_MEMORY;
286 }
287 }
288 else
289 {
290 rc = RTErrConvertFromWin32(GetLastError());
291 VBoxServiceVerbose(3, "GetFileVersionInfoSize(%s) -> %u / %Rrc\n", pszFilename, GetLastError(), rc);
292 }
293 return rc;
294}
295
296
297/**
298 * Gets a re-formatted version string from the VS_FIXEDFILEINFO table.
299 *
300 * @returns VBox status code. The output buffer is always valid and the status
301 * code can safely be ignored.
302 *
303 * @param pszPath The base path.
304 * @param pszFilaname The filename.
305 * @param pszVersion Where to return the version string.
306 * @param cbVersion The size of the version string buffer. This MUST be
307 * at least 2 bytes!
308 */
309int VBoxServiceGetFileVersionString(const char *pszPath, const char *pszFilename,
310 char *pszVersion, size_t cbVersion)
311{
312 /*
313 * We will ALWAYS return with a valid output buffer.
314 */
315 AssertReturn(cbVersion >= 2, VERR_BUFFER_OVERFLOW);
316 pszVersion[0] = '-';
317 pszVersion[1] = '\0';
318
319 /*
320 * Create the path and query the bits.
321 */
322 char szFullPath[RTPATH_MAX];
323 int rc = RTPathJoin(szFullPath, sizeof(szFullPath), pszPath, pszFilename);
324 if (RT_SUCCESS(rc))
325 {
326 DWORD dwMajor, dwMinor, dwBuild, dwRev;
327 rc = VBoxServiceGetFileVersion(szFullPath, &dwMajor, &dwMinor, &dwBuild, &dwRev);
328 if (RT_SUCCESS(rc))
329 RTStrPrintf(pszVersion, cbVersion, "%u.%u.%ur%u", dwMajor, dwMinor, dwBuild, dwRev);
330 }
331 return rc;
332}
333
334#endif /* RT_OS_WINDOWS */
335
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