VirtualBox

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

Last change on this file since 25776 was 25708, checked in by vboxsync, 15 years ago

VBoxServiceUtils: Extended logging for VBoxServiceWritePropF().

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.1 KB
Line 
1/* $Id: VBoxServiceUtils.cpp 25708 2010-01-11 10:14:35Z vboxsync $ */
2/** @file
3 * VBoxServiceUtils - Some utility functions.
4 */
5
6/*
7 * Copyright (C) 2009 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#endif
29
30#include <iprt/assert.h>
31#include <iprt/mem.h>
32#include <iprt/string.h>
33
34#include <VBox/VBoxGuestLib.h>
35#include "VBoxServiceInternal.h"
36
37
38#ifdef VBOX_WITH_GUEST_PROPS
39/**
40 * Reads a guest property.
41 *
42 * @returns VBox status code, fully bitched.
43 *
44 * @param u32ClientId The HGCM client ID for the guest property session.
45 * @param pszPropName The property name.
46 * @param ppszValue Where to return the value. This is always set
47 * to NULL. Free it using RTStrFree().
48 * @param ppszFlags Where to return the value flags. Free it
49 * using RTStrFree(). Optional.
50 * @param puTimestamp Where to return the timestamp. This is only set
51 * on success. Optional.
52 */
53int VBoxServiceReadProp(uint32_t u32ClientId, const char *pszPropName, char **ppszValue, char **ppszFlags, uint64_t *puTimestamp)
54{
55 size_t cbBuf = _1K;
56 void *pvBuf = NULL;
57 int rc;
58
59 *ppszValue = NULL;
60
61 for (unsigned cTries = 0; cTries < 10; cTries++)
62 {
63 /*
64 * (Re-)Allocate the buffer and try read the property.
65 */
66 RTMemFree(pvBuf);
67 pvBuf = RTMemAlloc(cbBuf);
68 if (!pvBuf)
69 {
70 VBoxServiceError("Guest Property: Failed to allocate %zu bytes\n", cbBuf);
71 rc = VERR_NO_MEMORY;
72 break;
73 }
74 char *pszValue;
75 char *pszFlags;
76 uint64_t uTimestamp;
77 rc = VbglR3GuestPropRead(u32ClientId, pszPropName,
78 pvBuf, cbBuf,
79 &pszValue, &uTimestamp, &pszFlags, NULL);
80 if (RT_FAILURE(rc))
81 {
82 if (rc == VERR_BUFFER_OVERFLOW)
83 {
84 /* try again with a bigger buffer. */
85 cbBuf *= 2;
86 continue;
87 }
88 if (rc == VERR_NOT_FOUND)
89 VBoxServiceVerbose(2, "Guest Property: %s not found\n", pszPropName);
90 else
91 VBoxServiceError("Guest Property: Failed to query \"%s\": %Rrc\n", pszPropName, rc);
92 break;
93 }
94
95 VBoxServiceVerbose(2, "Guest Property: Read \"%s\" = \"%s\", timestamp %RU64n\n",
96 pszPropName, pszValue, uTimestamp);
97 *ppszValue = RTStrDup(pszValue);
98 if (!*ppszValue)
99 {
100 VBoxServiceError("Guest Property: RTStrDup failed for \"%s\"\n", pszValue);
101 rc = VERR_NO_MEMORY;
102 break;
103 }
104
105 if (puTimestamp)
106 *puTimestamp = uTimestamp;
107 if (ppszFlags)
108 *ppszFlags = RTStrDup(pszFlags);
109 break; /* done */
110 }
111
112 RTMemFree(pvBuf);
113 return rc;
114}
115
116
117/**
118 * Reads a guest property as a 32-bit value.
119 *
120 * @returns VBox status code, fully bitched.
121 *
122 * @param u32ClientId The HGCM client ID for the guest property session.
123 * @param pszPropName The property name.
124 * @param pu32 Where to store the 32-bit value.
125 *
126 */
127int VBoxServiceReadPropUInt32(uint32_t u32ClientId, const char *pszPropName, uint32_t *pu32, uint32_t u32Min, uint32_t u32Max)
128{
129 char *pszValue;
130 int rc = VBoxServiceReadProp(u32ClientId, pszPropName, &pszValue,
131 NULL /* ppszFlags */, NULL /* puTimestamp */);
132 if (RT_SUCCESS(rc))
133 {
134 AssertPtr(pu32);
135 char *pszNext;
136 rc = RTStrToUInt32Ex(pszValue, &pszNext, 0, pu32);
137 if ( RT_SUCCESS(rc)
138 && (*pu32 < u32Min || *pu32 > u32Max))
139 {
140 rc = VBoxServiceError("The guest property value %s = %RU32 is out of range [%RU32..%RU32].\n",
141 pszPropName, *pu32, u32Min, u32Max);
142 }
143 RTStrFree(pszValue);
144 }
145 return rc;
146}
147
148
149/**
150 * Wrapper around VbglR3GuestPropWriteValue that does value formatting and
151 * logging.
152 *
153 * @returns VBox status code. Errors will be logged.
154 *
155 * @param u32ClientId The HGCM client ID for the guest property session.
156 * @param pszName The property name.
157 * @param pszValueFormat The property format string. If this is NULL then
158 * the property will be deleted (if possible).
159 * @param ... Format arguments.
160 */
161int VBoxServiceWritePropF(uint32_t u32ClientId, const char *pszName, const char *pszValueFormat, ...)
162{
163 AssertPtr(pszName);
164 int rc;
165 if (pszValueFormat != NULL)
166 {
167 va_list va;
168 va_start(va, pszValueFormat);
169
170 char *pszValue = RTStrAPrintf2V(pszValueFormat, va);
171 AssertPtr(pszValue);
172 VBoxServiceVerbose(3, "Writing guest property \"%s\" = \"%s\"\n", pszName, pszValue);
173 RTStrFree(pszValue);
174
175 rc = VbglR3GuestPropWriteValueV(u32ClientId, pszName, pszValueFormat, va);
176 va_end(va);
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#endif /* VBOX_WITH_GUEST_PROPS */
190
191
192#ifdef RT_OS_WINDOWS
193/** @todo return an iprt status code instead of BOOL */
194BOOL VBoxServiceGetFileString(const char* pszFileName,
195 char* pszBlock,
196 char* pszString,
197 PUINT puiSize)
198{
199 DWORD dwHandle, dwLen = 0;
200 UINT uiDataLen = 0;
201 char* lpData = NULL;
202 UINT uiValueLen = 0;
203 LPTSTR lpValue = NULL;
204 BOOL bRet = FALSE;
205
206 Assert(pszFileName);
207 Assert(pszBlock);
208 Assert(pszString);
209 Assert(puiSize > 0);
210
211 /* The VS_FIXEDFILEINFO structure contains version information about a file.
212 This information is language and code page independent. */
213 VS_FIXEDFILEINFO *pFileInfo = NULL;
214 dwLen = GetFileVersionInfoSize(pszFileName, &dwHandle);
215
216 if (!dwLen)
217 {
218 /* Don't print this to release log -- this confuses people if a file
219 * isn't present because it's optional / was not installed intentionally. */
220 VBoxServiceVerbose(3, "No file information found! File = %s, Error: %Rrc\n",
221 pszFileName, RTErrConvertFromWin32(GetLastError()));
222 return FALSE;
223 }
224
225 lpData = (LPTSTR) RTMemTmpAlloc(dwLen);
226 if (!lpData)
227 {
228 VBoxServiceError("Could not allocate temp buffer for file string lookup!\n");
229 return FALSE;
230 }
231
232 if (GetFileVersionInfo(pszFileName, dwHandle, dwLen, lpData))
233 {
234 if((bRet = VerQueryValue(lpData, pszBlock, (LPVOID*)&lpValue, (PUINT)&uiValueLen)))
235 {
236 UINT uiSize = uiValueLen * sizeof(char);
237
238 if(uiSize > *puiSize)
239 uiSize = *puiSize;
240
241 ZeroMemory(pszString, *puiSize);
242 memcpy(pszString, lpValue, uiSize);
243 }
244 else VBoxServiceVerbose(3, "No file string value for \"%s\" in file \"%s\" available!\n", pszBlock, pszFileName);
245 }
246 else VBoxServiceVerbose(3, "No file version table for file \"%s\" available!\n", pszFileName);
247
248 RTMemFree(lpData);
249 return bRet;
250}
251
252
253/** @todo return an iprt status code instead of BOOL */
254BOOL VBoxServiceGetFileVersion(const char* pszFileName,
255 DWORD* pdwMajor,
256 DWORD* pdwMinor,
257 DWORD* pdwBuildNumber,
258 DWORD* pdwRevisionNumber)
259{
260 DWORD dwHandle, dwLen = 0;
261 UINT BufLen = 0;
262 LPTSTR lpData = NULL;
263 BOOL bRet = FALSE;
264
265 Assert(pszFileName);
266 Assert(pdwMajor);
267 Assert(pdwMinor);
268 Assert(pdwBuildNumber);
269 Assert(pdwRevisionNumber);
270
271 /* The VS_FIXEDFILEINFO structure contains version information about a file.
272 This information is language and code page independent. */
273 VS_FIXEDFILEINFO *pFileInfo = NULL;
274 dwLen = GetFileVersionInfoSize(pszFileName, &dwHandle);
275
276 /* Try own fields defined in block "\\StringFileInfo\\040904b0\\FileVersion". */
277 char szValue[_MAX_PATH] = {0};
278 char *pszValue = szValue;
279 UINT uiSize = _MAX_PATH;
280 int r = 0;
281
282 bRet = VBoxServiceGetFileString(pszFileName, "\\StringFileInfo\\040904b0\\FileVersion", szValue, &uiSize);
283 if (bRet)
284 {
285 sscanf(pszValue, "%ld.%ld.%ld.%ld", pdwMajor, pdwMinor, pdwBuildNumber, pdwRevisionNumber);
286 }
287 else if (dwLen > 0)
288 {
289 /* Try regular fields - this maybe is not file provided by VBox! */
290 lpData = (LPTSTR) RTMemTmpAlloc(dwLen);
291 if (!lpData)
292 {
293 VBoxServiceError("Could not allocate temp buffer for file version string!\n");
294 return FALSE;
295 }
296
297 if (GetFileVersionInfo(pszFileName, dwHandle, dwLen, lpData))
298 {
299 if((bRet = VerQueryValue(lpData, "\\", (LPVOID*)&pFileInfo, (PUINT)&BufLen)))
300 {
301 *pdwMajor = HIWORD(pFileInfo->dwFileVersionMS);
302 *pdwMinor = LOWORD(pFileInfo->dwFileVersionMS);
303 *pdwBuildNumber = HIWORD(pFileInfo->dwFileVersionLS);
304 *pdwRevisionNumber = LOWORD(pFileInfo->dwFileVersionLS);
305 bRet = TRUE;
306 }
307 else VBoxServiceVerbose(3, "No file version value for file \"%s\" available!\n", pszFileName);
308 }
309 else VBoxServiceVerbose(3, "No file version struct for file \"%s\" available!\n", pszFileName);
310
311 RTMemFree(lpData);
312 }
313 return bRet;
314}
315
316
317BOOL VBoxServiceGetFileVersionString(const char* pszPath, const char* pszFileName, char* pszVersion, UINT uiSize)
318{
319 BOOL bRet = FALSE;
320 char szFullPath[_MAX_PATH] = {0};
321 char szValue[_MAX_PATH] = {0};
322 int r = 0;
323
324 RTStrPrintf(szFullPath, 4096, "%s\\%s", pszPath, pszFileName);
325
326 DWORD dwMajor, dwMinor, dwBuild, dwRev;
327
328 bRet = VBoxServiceGetFileVersion(szFullPath, &dwMajor, &dwMinor, &dwBuild, &dwRev);
329 if (bRet)
330 RTStrPrintf(pszVersion, uiSize, "%ld.%ld.%ldr%ld", dwMajor, dwMinor, dwBuild, dwRev);
331 else
332 RTStrPrintf(pszVersion, uiSize, "-");
333
334 return bRet;
335}
336#endif /* !RT_OS_WINDOWS */
337
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