VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServicePropCache.cpp@ 31924

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

VBoxService/VMInfo/PropCache: Really clear/set default values of temporary properties.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.3 KB
Line 
1/* $Id: VBoxServicePropCache.cpp 31924 2010-08-24 13:14:33Z vboxsync $ */
2/** @file
3 * VBoxServicePropCache - Guest property cache.
4 */
5
6/*
7 * Copyright (C) 2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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 <iprt/assert.h>
23#include <iprt/list.h>
24#include <iprt/mem.h>
25#include <iprt/string.h>
26
27#include <VBox/VBoxGuestLib.h>
28#include "VBoxServiceInternal.h"
29#include "VBoxServiceUtils.h"
30#include "VBoxServicePropCache.h"
31
32
33/** Internal functions, not for public use. */
34PVBOXSERVICEVEPROPCACHEENTRY vboxServicePropCacheFindInternal(PVBOXSERVICEVEPROPCACHE pCache, const char *pszName, uint32_t uFlags);
35PVBOXSERVICEVEPROPCACHEENTRY vboxServicePropCacheInsertEntryInternal(PVBOXSERVICEVEPROPCACHE pCache, const char *pszName);
36
37
38/** @todo Docs */
39PVBOXSERVICEVEPROPCACHEENTRY vboxServicePropCacheFindInternal(PVBOXSERVICEVEPROPCACHE pCache, const char *pszName, uint32_t uFlags)
40{
41 AssertPtr(pCache);
42 AssertPtr(pszName);
43 /** @todo This is a O(n) lookup, maybe improve this later to O(1) using a
44 * map.
45 * r=bird: Use a string space (RTstrSpace*). That is O(log n) in its current
46 * implementation (AVL tree). However, this is not important at the
47 * moment. */
48 PVBOXSERVICEVEPROPCACHEENTRY pNodeIt, pNode = NULL;
49 if (RT_SUCCESS(RTCritSectEnter(&pCache->CritSect)))
50 {
51 RTListForEach(&pCache->NodeHead, pNodeIt, VBOXSERVICEVEPROPCACHEENTRY, NodeSucc)
52 {
53 if (strcmp(pNodeIt->pszName, pszName) == 0)
54 {
55 pNode = pNodeIt;
56 break;
57 }
58 }
59 RTCritSectLeave(&pCache->CritSect);
60 }
61 return pNode;
62}
63
64
65/** @todo Docs */
66PVBOXSERVICEVEPROPCACHEENTRY vboxServicePropCacheInsertEntryInternal(PVBOXSERVICEVEPROPCACHE pCache, const char *pszName)
67{
68 AssertPtr(pszName);
69 PVBOXSERVICEVEPROPCACHEENTRY pNode = (PVBOXSERVICEVEPROPCACHEENTRY)RTMemAlloc(sizeof(VBOXSERVICEVEPROPCACHEENTRY));
70 if (pNode)
71 {
72 pNode->pszName = RTStrDup(pszName);
73 pNode->pszValue = NULL;
74 pNode->fFlags = 0;
75 pNode->pszValueReset = NULL;
76
77 int rc = RTCritSectEnter(&pCache->CritSect);
78 if (RT_SUCCESS(rc))
79 {
80 /*rc =*/ RTListAppend(&pCache->NodeHead, &pNode->NodeSucc);
81 rc = RTCritSectLeave(&pCache->CritSect);
82 }
83 }
84 return pNode;
85}
86
87
88/** @todo Docs */
89int vboxServicePropCacheWritePropF(uint32_t u32ClientId, const char *pszName, uint32_t fFlags, const char *pszValueFormat, ...)
90{
91 AssertPtr(pszName);
92 int rc;
93 if (pszValueFormat != NULL)
94 {
95 va_list va;
96 va_start(va, pszValueFormat);
97
98 char *pszValue;
99 if (RTStrAPrintfV(&pszValue, pszValueFormat, va) >= 0)
100 {
101 if (fFlags & VBOXSERVICEPROPCACHEFLAG_TEMPORARY)
102 {
103 /*
104 * Because a value can be temporary we have to make sure it also
105 * gets deleted when the property cache did not have the chance to
106 * gracefully clean it up (due to a hard VM reset etc), so set this
107 * guest property using the TRANSIENT flag.
108 */
109 rc = VbglR3GuestPropWrite(u32ClientId, pszName, pszValue, "TRANSIENT");
110 }
111 else
112 rc = VbglR3GuestPropWriteValue(u32ClientId, pszName, pszValue);
113 RTStrFree(pszValue);
114 }
115 else
116 rc = VERR_NO_MEMORY;
117 va_end(va);
118 }
119 else
120 {
121 rc = VbglR3GuestPropWriteValue(u32ClientId, pszName, NULL);
122 }
123 return rc;
124}
125
126
127/**
128 * Creates a property cache.
129 *
130 * @returns IPRT status code.
131 * @param pCache Pointer to the cache.
132 * @param uClientId The HGCM handle of to the guest property service.
133 */
134int VBoxServicePropCacheCreate(PVBOXSERVICEVEPROPCACHE pCache, uint32_t uClientId)
135{
136 AssertPtr(pCache);
137 /** @todo Prevent init the cache twice!
138 * r=bird: Use a magic. */
139 RTListInit(&pCache->NodeHead);
140 pCache->uClientID = uClientId;
141 return RTCritSectInit(&pCache->CritSect);
142}
143
144
145/**
146 * Updates a cache entry without submitting any changes to the host.
147 * This is handy for defining default values/flags.
148 *
149 * @returns VBox status code.
150 *
151 * @param pCache The property cache.
152 * @param pszName The property name.
153 * @param fFlags The property flags to set.
154 * @param pszValueReset The property reset value.
155 */
156int VBoxServicePropCacheUpdateEntry(PVBOXSERVICEVEPROPCACHE pCache,
157 const char *pszName, uint32_t fFlags, const char *pszValueReset)
158{
159 AssertPtr(pCache);
160 AssertPtr(pszName);
161 PVBOXSERVICEVEPROPCACHEENTRY pNode = vboxServicePropCacheFindInternal(pCache, pszName, 0);
162 if (pNode == NULL)
163 pNode = vboxServicePropCacheInsertEntryInternal(pCache, pszName);
164
165 int rc;
166 if (pNode != NULL)
167 {
168 rc = RTCritSectEnter(&pCache->CritSect);
169 if (RT_SUCCESS(rc))
170 {
171 pNode->fFlags = fFlags;
172 if (pszValueReset)
173 {
174 if (pNode->pszValueReset)
175 RTStrFree(pNode->pszValueReset);
176 pNode->pszValueReset = RTStrDup(pszValueReset);
177 }
178 rc = RTCritSectLeave(&pCache->CritSect);
179 }
180 }
181 else
182 rc = VERR_NO_MEMORY;
183 return rc;
184}
185
186
187/**
188 * Updates the local guest property cache and writes it to HGCM if outdated.
189 *
190 * @returns VBox status code.
191 *
192 * @param pCache The property cache.
193 * @param pszName The property name.
194 * @param pszValueFormat The property format string. If this is NULL then
195 * the property will be deleted (if possible).
196 * @param ... Format arguments.
197 */
198int VBoxServicePropCacheUpdate(PVBOXSERVICEVEPROPCACHE pCache, const char *pszName, const char *pszValueFormat, ...)
199{
200 char *pszValue = NULL;
201 int rc;
202 if (pszValueFormat)
203 {
204 va_list va;
205 va_start(va, pszValueFormat);
206 RTStrAPrintfV(&pszValue, pszValueFormat, va);
207 va_end(va);
208 }
209 rc = VBoxServicePropCacheUpdateEx(pCache, pszName, 0 /* Not used */, NULL /* Not used */, pszValue);
210 if (pszValue)
211 RTStrFree(pszValue);
212 return rc;
213}
214
215
216/**
217 * Updates the local guest property cache and writes it to HGCM if outdated.
218 *
219 * @returns VBox status code.
220 *
221 * @param pCache The property cache.
222 * @param pszName The property name.
223 * @param fFlags The property flags to set.
224 * @param pszValueReset The property reset value.
225 * @param pszValueFormat The property format string. If this is NULL then
226 * the property will be deleted (if possible).
227 * @param ... Format arguments.
228 */
229int VBoxServicePropCacheUpdateEx(PVBOXSERVICEVEPROPCACHE pCache, const char *pszName, uint32_t fFlags,
230 const char *pszValueReset, const char *pszValueFormat, ...)
231{
232 AssertPtr(pCache);
233 Assert(pCache->uClientID);
234 AssertPtr(pszName);
235
236 /*
237 * Format the value first.
238 */
239 char *pszValue = NULL;
240 if (pszValueFormat)
241 {
242 va_list va;
243 va_start(va, pszValueFormat);
244 RTStrAPrintfV(&pszValue, pszValueFormat, va);
245 va_end(va);
246 if (!pszValue)
247 return VERR_NO_STR_MEMORY;
248 }
249
250 PVBOXSERVICEVEPROPCACHEENTRY pNode = vboxServicePropCacheFindInternal(pCache, pszName, 0);
251
252 /* Lock the cache. */
253 int rc = RTCritSectEnter(&pCache->CritSect);
254 if (RT_SUCCESS(rc))
255 {
256 if (pNode == NULL)
257 pNode = vboxServicePropCacheInsertEntryInternal(pCache, pszName);
258
259 AssertPtr(pNode);
260 if (pszValue) /* Do we have a value to check for? */
261 {
262 bool fUpdate = false;
263 /* Always update this property, no matter what? */
264 if (pNode->fFlags & VBOXSERVICEPROPCACHEFLAG_ALWAYS_UPDATE)
265 fUpdate = true;
266 /* Did the value change so we have to update? */
267 else if (pNode->pszValue && strcmp(pNode->pszValue, pszValue) != 0)
268 fUpdate = true;
269 /* No value stored at the moment but we have a value now? */
270 else if (pNode->pszValue == NULL)
271 fUpdate = true;
272
273 if (fUpdate)
274 {
275 /* Write the update. */
276 rc = vboxServicePropCacheWritePropF(pCache->uClientID, pNode->pszName, fFlags, pszValue);
277 RTStrFree(pNode->pszValue);
278 pNode->pszValue = RTStrDup(pszValue);
279 }
280 else
281 rc = VINF_NO_CHANGE; /* No update needed. */
282 }
283 else
284 {
285 /* No value specified. Deletion (or no action required). */
286 if (pNode->pszValue) /* Did we have a value before? Then the value needs to be deleted. */
287 {
288 /* Delete property (but do not remove from cache) if not deleted yet. */
289 RTStrFree(pNode->pszValue);
290 pNode->pszValue = NULL;
291 rc = vboxServicePropCacheWritePropF(pCache->uClientID, pNode->pszName,
292 0, /* Flags */ NULL /* Value */);
293 }
294 else
295 rc = VINF_NO_CHANGE; /* No update needed. */
296 }
297
298 /* Update rest of the fields. */
299 if (pszValueReset)
300 {
301 if (pNode->pszValueReset)
302 RTStrFree(pNode->pszValueReset);
303 pNode->pszValueReset = RTStrDup(pszValueReset);
304 }
305 if (fFlags)
306 pNode->fFlags = fFlags;
307
308 /* Release cache. */
309 int rc2 = RTCritSectLeave(&pCache->CritSect);
310 if (RT_SUCCESS(rc))
311 rc2 = rc;
312 }
313
314 /* Delete temp stuff. */
315 RTStrFree(pszValue);
316
317 return rc;
318}
319
320
321/**
322 * Updates all cache values which are matching the specified path.
323 *
324 * @returns VBox status code.
325 *
326 * @param pCache The property cache.
327 * @param pszValue The value to set. A NULL will delete the value.
328 * @param fFlags Flags to set.
329 * @param pszPathFormat The path format string. May not be null and has
330 * to be an absolute path.
331 * @param ... Format arguments.
332 */
333int VBoxServicePropCacheUpdateByPath(PVBOXSERVICEVEPROPCACHE pCache, const char *pszValue, uint32_t fFlags, const char *pszPathFormat, ...)
334{
335 AssertPtr(pCache);
336 AssertPtr(pszPathFormat);
337 int rc = VERR_NOT_FOUND;
338 PVBOXSERVICEVEPROPCACHEENTRY pNodeIt = NULL;
339 if (RT_SUCCESS(RTCritSectEnter(&pCache->CritSect)))
340 {
341 /*
342 * Format the value first.
343 */
344 char *pszPath = NULL;
345 va_list va;
346 va_start(va, pszPathFormat);
347 RTStrAPrintfV(&pszPath, pszPathFormat, va);
348 va_end(va);
349 if (!pszPath)
350 return VERR_NO_STR_MEMORY;
351
352 /* Iterate through all nodes and compare their paths. */
353 RTListForEach(&pCache->NodeHead, pNodeIt, VBOXSERVICEVEPROPCACHEENTRY, NodeSucc)
354 {
355 if (RTStrStr(pNodeIt->pszName, pszPath) == pNodeIt->pszName)
356 {
357 /** @todo Use some internal function to update the node directly, this is slow atm. */
358 rc = VBoxServicePropCacheUpdate(pCache, pNodeIt->pszName, pszValue);
359 }
360 if (RT_FAILURE(rc))
361 break;
362 }
363 RTStrFree(pszPath);
364 RTCritSectLeave(&pCache->CritSect);
365 }
366 return rc;
367}
368
369
370/**
371 * Flushes the cache by writing every item regardless of its state.
372 *
373 * @param pCache The property cache.
374 */
375int VBoxServicePropCacheFlush(PVBOXSERVICEVEPROPCACHE pCache)
376{
377 AssertPtr(pCache);
378 int rc = VINF_SUCCESS;
379 PVBOXSERVICEVEPROPCACHEENTRY pNodeIt = NULL;
380 if (RT_SUCCESS(RTCritSectEnter(&pCache->CritSect)))
381 {
382 RTListForEach(&pCache->NodeHead, pNodeIt, VBOXSERVICEVEPROPCACHEENTRY, NodeSucc)
383 {
384 rc = vboxServicePropCacheWritePropF(pCache->uClientID, pNodeIt->pszName,
385 pNodeIt->fFlags, pNodeIt->pszValue);
386 if (RT_FAILURE(rc))
387 break;
388 }
389 RTCritSectLeave(&pCache->CritSect);
390 }
391 return rc;
392}
393
394
395/**
396 * Reset all temporary properties and destroy the cache.
397 *
398 * @param pCache The property cache.
399 */
400void VBoxServicePropCacheDestroy(PVBOXSERVICEVEPROPCACHE pCache)
401{
402 AssertPtr(pCache);
403 Assert(pCache->uClientID);
404
405 /* Lock the cache. */
406 int rc = RTCritSectEnter(&pCache->CritSect);
407 if (RT_SUCCESS(rc))
408 {
409 PVBOXSERVICEVEPROPCACHEENTRY pNode = RTListNodeGetFirst(&pCache->NodeHead, VBOXSERVICEVEPROPCACHEENTRY, NodeSucc);
410 while (pNode)
411 {
412 /*
413 * When destroying the cache and we have a temporary value, remove the
414 * (eventually) set TRANSIENT flag from it so that it doesn't get deleted
415 * by the host side in order to put the actual reset value in it.
416 */
417 if (pNode->fFlags & VBOXSERVICEPROPCACHEFLAG_TEMPORARY)
418 vboxServicePropCacheWritePropF(pCache->uClientID, pNode->pszName, 0 /* Flags, clear all */, pNode->pszValueReset);
419
420 AssertPtr(pNode->pszName);
421 RTStrFree(pNode->pszName);
422 RTStrFree(pNode->pszValue);
423 RTStrFree(pNode->pszValueReset);
424 pNode->fFlags = 0;
425
426 PVBOXSERVICEVEPROPCACHEENTRY pNext = RTListNodeGetNext(&pNode->NodeSucc, VBOXSERVICEVEPROPCACHEENTRY, NodeSucc);
427 RTListNodeRemove(&pNode->NodeSucc);
428 RTMemFree(pNode);
429
430 if (pNext && RTListNodeIsLast(&pCache->NodeHead, &pNext->NodeSucc))
431 break;
432 pNode = pNext;
433 }
434 RTCritSectLeave(&pCache->CritSect);
435 }
436
437 /* Destroy critical section. */
438 RTCritSectDelete(&pCache->CritSect);
439}
440
Note: See TracBrowser for help on using the repository browser.

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