VirtualBox

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

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

Comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.5 KB
Line 
1/* $Id: VBoxServicePropCache.cpp 36179 2011-03-07 09:53:04Z 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.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#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 and TRANSIENT_RESET flags.
108 */
109 rc = VbglR3GuestPropWrite(u32ClientId, pszName, pszValue, "TRANSIENT,TRANSIENT_RESET");
110 if (rc == VERR_PARSE_ERROR)
111 {
112 /* Host does not support the "TRANSIENT_RESET" flag, so only
113 * use the "TRANSIENT" flag -- better than nothing :-). */
114 rc = VbglR3GuestPropWrite(u32ClientId, pszName, pszValue, "TRANSIENT");
115 }
116 }
117 else
118 rc = VbglR3GuestPropWriteValue(u32ClientId, pszName, pszValue);
119 RTStrFree(pszValue);
120 }
121 else
122 rc = VERR_NO_MEMORY;
123 va_end(va);
124 }
125 else
126 {
127 rc = VbglR3GuestPropWriteValue(u32ClientId, pszName, NULL);
128 }
129 return rc;
130}
131
132
133/**
134 * Creates a property cache.
135 *
136 * @returns IPRT status code.
137 * @param pCache Pointer to the cache.
138 * @param uClientId The HGCM handle of to the guest property service.
139 */
140int VBoxServicePropCacheCreate(PVBOXSERVICEVEPROPCACHE pCache, uint32_t uClientId)
141{
142 AssertPtr(pCache);
143 /** @todo Prevent init the cache twice!
144 * r=bird: Use a magic. */
145 RTListInit(&pCache->NodeHead);
146 pCache->uClientID = uClientId;
147 return RTCritSectInit(&pCache->CritSect);
148}
149
150
151/**
152 * Updates a cache entry without submitting any changes to the host.
153 * This is handy for defining default values/flags.
154 *
155 * @returns VBox status code.
156 *
157 * @param pCache The property cache.
158 * @param pszName The property name.
159 * @param fFlags The property flags to set.
160 * @param pszValueReset The property reset value.
161 */
162int VBoxServicePropCacheUpdateEntry(PVBOXSERVICEVEPROPCACHE pCache,
163 const char *pszName, uint32_t fFlags, const char *pszValueReset)
164{
165 AssertPtr(pCache);
166 AssertPtr(pszName);
167 PVBOXSERVICEVEPROPCACHEENTRY pNode = vboxServicePropCacheFindInternal(pCache, pszName, 0);
168 if (pNode == NULL)
169 pNode = vboxServicePropCacheInsertEntryInternal(pCache, pszName);
170
171 int rc;
172 if (pNode != NULL)
173 {
174 rc = RTCritSectEnter(&pCache->CritSect);
175 if (RT_SUCCESS(rc))
176 {
177 pNode->fFlags = fFlags;
178 if (pszValueReset)
179 {
180 if (pNode->pszValueReset)
181 RTStrFree(pNode->pszValueReset);
182 pNode->pszValueReset = RTStrDup(pszValueReset);
183 }
184 rc = RTCritSectLeave(&pCache->CritSect);
185 }
186 }
187 else
188 rc = VERR_NO_MEMORY;
189 return rc;
190}
191
192
193/**
194 * Updates the local guest property cache and writes it to HGCM if outdated.
195 *
196 * @returns VBox status code.
197 *
198 * @param pCache The property cache.
199 * @param pszName The property name.
200 * @param pszValueFormat The property format string. If this is NULL then
201 * the property will be deleted (if possible).
202 * @param ... Format arguments.
203 */
204int VBoxServicePropCacheUpdate(PVBOXSERVICEVEPROPCACHE pCache, const char *pszName, const char *pszValueFormat, ...)
205{
206 AssertPtr(pCache);
207 Assert(pCache->uClientID);
208 AssertPtr(pszName);
209
210 /*
211 * Format the value first.
212 */
213 char *pszValue = NULL;
214 if (pszValueFormat)
215 {
216 va_list va;
217 va_start(va, pszValueFormat);
218 RTStrAPrintfV(&pszValue, pszValueFormat, va);
219 va_end(va);
220 if (!pszValue)
221 return VERR_NO_STR_MEMORY;
222 }
223
224 PVBOXSERVICEVEPROPCACHEENTRY pNode = vboxServicePropCacheFindInternal(pCache, pszName, 0);
225
226 /* Lock the cache. */
227 int rc = RTCritSectEnter(&pCache->CritSect);
228 if (RT_SUCCESS(rc))
229 {
230 if (pNode == NULL)
231 pNode = vboxServicePropCacheInsertEntryInternal(pCache, pszName);
232
233 AssertPtr(pNode);
234 if (pszValue) /* Do we have a value to check for? */
235 {
236 bool fUpdate = false;
237 /* Always update this property, no matter what? */
238 if (pNode->fFlags & VBOXSERVICEPROPCACHEFLAG_ALWAYS_UPDATE)
239 fUpdate = true;
240 /* Did the value change so we have to update? */
241 else if (pNode->pszValue && strcmp(pNode->pszValue, pszValue) != 0)
242 fUpdate = true;
243 /* No value stored at the moment but we have a value now? */
244 else if (pNode->pszValue == NULL)
245 fUpdate = true;
246
247 if (fUpdate)
248 {
249 /* Write the update. */
250 rc = vboxServicePropCacheWritePropF(pCache->uClientID, pNode->pszName, pNode->fFlags, pszValue);
251 RTStrFree(pNode->pszValue);
252 pNode->pszValue = RTStrDup(pszValue);
253 }
254 else
255 rc = VINF_NO_CHANGE; /* No update needed. */
256 }
257 else
258 {
259 /* No value specified. Deletion (or no action required). */
260 if (pNode->pszValue) /* Did we have a value before? Then the value needs to be deleted. */
261 {
262 /* Delete property (but do not remove from cache) if not deleted yet. */
263 RTStrFree(pNode->pszValue);
264 pNode->pszValue = NULL;
265 rc = vboxServicePropCacheWritePropF(pCache->uClientID, pNode->pszName,
266 0, /* Flags */ NULL /* Value */);
267 }
268 else
269 rc = VINF_NO_CHANGE; /* No update needed. */
270 }
271
272 /* Release cache. */
273 RTCritSectLeave(&pCache->CritSect);
274 }
275
276 /* Delete temp stuff. */
277 RTStrFree(pszValue);
278 return rc;
279}
280
281
282/**
283 * Updates all cache values which are matching the specified path.
284 *
285 * @returns VBox status code.
286 *
287 * @param pCache The property cache.
288 * @param pszValue The value to set. A NULL will delete the value.
289 * @param fFlags Flags to set.
290 * @param pszPathFormat The path format string. May not be null and has
291 * to be an absolute path.
292 * @param ... Format arguments.
293 */
294int VBoxServicePropCacheUpdateByPath(PVBOXSERVICEVEPROPCACHE pCache, const char *pszValue, uint32_t fFlags, const char *pszPathFormat, ...)
295{
296 AssertPtr(pCache);
297 AssertPtr(pszPathFormat);
298 int rc = VERR_NOT_FOUND;
299 PVBOXSERVICEVEPROPCACHEENTRY pNodeIt = NULL;
300 if (RT_SUCCESS(RTCritSectEnter(&pCache->CritSect)))
301 {
302 /*
303 * Format the value first.
304 */
305 char *pszPath = NULL;
306 va_list va;
307 va_start(va, pszPathFormat);
308 RTStrAPrintfV(&pszPath, pszPathFormat, va);
309 va_end(va);
310 if (!pszPath)
311 {
312 rc = VERR_NO_STR_MEMORY;
313 }
314 else
315 {
316 /* Iterate through all nodes and compare their paths. */
317 RTListForEach(&pCache->NodeHead, pNodeIt, VBOXSERVICEVEPROPCACHEENTRY, NodeSucc)
318 {
319 if (RTStrStr(pNodeIt->pszName, pszPath) == pNodeIt->pszName)
320 {
321 /** @todo Use some internal function to update the node directly, this is slow atm. */
322 rc = VBoxServicePropCacheUpdate(pCache, pNodeIt->pszName, pszValue);
323 }
324 if (RT_FAILURE(rc))
325 break;
326 }
327 RTStrFree(pszPath);
328 }
329 RTCritSectLeave(&pCache->CritSect);
330 }
331 return rc;
332}
333
334
335/**
336 * Flushes the cache by writing every item regardless of its state.
337 *
338 * @param pCache The property cache.
339 */
340int VBoxServicePropCacheFlush(PVBOXSERVICEVEPROPCACHE pCache)
341{
342 AssertPtr(pCache);
343 int rc = VINF_SUCCESS;
344 PVBOXSERVICEVEPROPCACHEENTRY pNodeIt = NULL;
345 if (RT_SUCCESS(RTCritSectEnter(&pCache->CritSect)))
346 {
347 RTListForEach(&pCache->NodeHead, pNodeIt, VBOXSERVICEVEPROPCACHEENTRY, NodeSucc)
348 {
349 rc = vboxServicePropCacheWritePropF(pCache->uClientID, pNodeIt->pszName,
350 pNodeIt->fFlags, pNodeIt->pszValue);
351 if (RT_FAILURE(rc))
352 break;
353 }
354 RTCritSectLeave(&pCache->CritSect);
355 }
356 return rc;
357}
358
359
360/**
361 * Reset all temporary properties and destroy the cache.
362 *
363 * @param pCache The property cache.
364 */
365void VBoxServicePropCacheDestroy(PVBOXSERVICEVEPROPCACHE pCache)
366{
367 AssertPtr(pCache);
368 Assert(pCache->uClientID);
369
370 /* Lock the cache. */
371 int rc = RTCritSectEnter(&pCache->CritSect);
372 if (RT_SUCCESS(rc))
373 {
374 PVBOXSERVICEVEPROPCACHEENTRY pNode = RTListGetFirst(&pCache->NodeHead, VBOXSERVICEVEPROPCACHEENTRY, NodeSucc);
375 while (pNode)
376 {
377 PVBOXSERVICEVEPROPCACHEENTRY pNext = RTListNodeIsLast(&pCache->NodeHead, &pNode->NodeSucc)
378 ? NULL :
379 RTListNodeGetNext(&pNode->NodeSucc,
380 VBOXSERVICEVEPROPCACHEENTRY, NodeSucc);
381 RTListNodeRemove(&pNode->NodeSucc);
382
383 /*
384 * When destroying the cache and we have a temporary value, remove the
385 * (eventually) set TRANSIENT_RESET flag from it so that it doesn't get deleted
386 * by the host side in order to put the actual reset value in it.
387 */
388 if (pNode->fFlags & VBOXSERVICEPROPCACHEFLAG_TEMPORARY)
389 vboxServicePropCacheWritePropF(pCache->uClientID, pNode->pszName, 0 /* Flags, clear all */, pNode->pszValueReset);
390
391 AssertPtr(pNode->pszName);
392 RTStrFree(pNode->pszName);
393 RTStrFree(pNode->pszValue);
394 RTStrFree(pNode->pszValueReset);
395 pNode->fFlags = 0;
396
397 RTMemFree(pNode);
398
399 pNode = pNext;
400 }
401 RTCritSectLeave(&pCache->CritSect);
402 }
403
404 /* Destroy critical section. */
405 RTCritSectDelete(&pCache->CritSect);
406}
407
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