VirtualBox

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

Last change on this file since 37826 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

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