VirtualBox

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

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

Additions: operator precedence

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1/* $Id: VBoxServicePropCache.cpp 28981 2010-05-03 14:46:58Z 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#ifdef VBOX_WITH_GUEST_PROPS
33
34/** @todo Docs */
35int VBoxServicePropCacheInit(PVBOXSERVICEVEPROPCACHE pCache, uint32_t u32ClientId)
36{
37 AssertPtr(pCache);
38 RTListInit(&pCache->Node);
39 pCache->uClientID = u32ClientId;
40 return VINF_SUCCESS;
41}
42
43
44/** @todo Docs */
45PVBOXSERVICEVEPROPCACHEENTRY VBoxServicePropCacheFind(PVBOXSERVICEVEPROPCACHE pCache, const char *pszName, uint32_t uFlags)
46{
47 AssertPtr(pCache);
48 AssertPtr(pszName);
49 /* This is a O(n) lookup, maybe improve this later to O(1) using a map. */
50 PVBOXSERVICEVEPROPCACHEENTRY pNode;
51 RTListForEach(&pCache->Node, pNode, VBOXSERVICEVEPROPCACHEENTRY, Node)
52 {
53 if (strcmp(pNode->pszName, pszName) == 0)
54 return pNode;
55 }
56 return NULL;
57}
58
59
60/** @todo Docs */
61int VBoxServicePropCacheUpdateEntry(PVBOXSERVICEVEPROPCACHE pCache,
62 const char *pszName, uint32_t u32Flags, const char *pszValueReset)
63{
64 AssertPtr(pCache);
65 AssertPtr(pszName);
66 PVBOXSERVICEVEPROPCACHEENTRY pNode = VBoxServicePropCacheFind(pCache, pszName, 0);
67 if (pNode != NULL)
68 {
69 pNode->uFlags = u32Flags;
70 if (pszValueReset)
71 {
72 if (pszValueReset)
73 RTStrFree(pNode->pszValueReset);
74 pNode->pszValueReset = RTStrDup(pszValueReset);
75 }
76 return VINF_SUCCESS;
77 }
78 return VERR_NOT_FOUND;
79}
80
81
82/**
83 * Updates the local guest property cache and writes it to HGCM if outdated.
84 *
85 * @returns VBox status code. Errors will be logged.
86 *
87 * @param u32ClientId The HGCM client ID for the guest property session.
88 * @param pszName The property name.
89 * @param pszValueFormat The property format string. If this is NULL then
90 * the property will be deleted (if possible).
91 * @param ... Format arguments.
92 */
93int VBoxServicePropCacheUpdate(PVBOXSERVICEVEPROPCACHE pCache, const char *pszName, const char *pszValueFormat, ...)
94{
95 char *pszValue = NULL;
96 int rc;
97 if (pszValueFormat)
98 {
99 va_list va;
100 va_start(va, pszValueFormat);
101 RTStrAPrintfV(&pszValue, pszValueFormat, va);
102 va_end(va);
103 }
104 rc = VBoxServicePropCacheUpdateEx(pCache, pszName, 0 /* Not used */, NULL /* Not used */, pszValue);
105 if (pszValue)
106 RTStrFree(pszValue);
107 return rc;
108}
109
110
111/**
112 * Updates the local guest property cache and writes it to HGCM if outdated.
113 *
114 * @returns VBox status code. Errors will be logged.
115 *
116 * @param u32ClientId The HGCM client ID for the guest property session.
117 * @param pszName The property name.
118 * @param pszValueFormat The property format string. If this is NULL then
119 * the property will be deleted (if possible).
120 * @param ... Format arguments.
121 */
122int VBoxServicePropCacheUpdateEx(PVBOXSERVICEVEPROPCACHE pCache, const char *pszName, uint32_t u32Flags, const char *pszValueReset, const char *pszValueFormat, ...)
123{
124 AssertPtr(pCache);
125 Assert(pCache->uClientID);
126 AssertPtr(pszName);
127
128 char *pszValue = NULL;
129 if (pszValueFormat)
130 {
131 va_list va;
132 va_start(va, pszValueFormat);
133 RTStrAPrintfV(&pszValue, pszValueFormat, va);
134 va_end(va);
135 }
136
137 PVBOXSERVICEVEPROPCACHEENTRY pNode = VBoxServicePropCacheFind(pCache, pszName, 0);
138 if (pNode == NULL)
139 {
140 pNode = (PVBOXSERVICEVEPROPCACHEENTRY)RTMemAlloc(sizeof(VBOXSERVICEVEPROPCACHEENTRY));
141 AssertPtrReturn(pNode, VERR_NO_MEMORY);
142
143 pNode->pszName = RTStrDup(pszName);
144 pNode->pszValue = NULL;
145 pNode->uFlags = 0;
146 pNode->pszValueReset = NULL;
147
148 /*rc =*/ RTListAppend(&pCache->Node, &pNode->Node);
149 }
150
151 int rc;
152 AssertPtr(pNode);
153 if (pszValue) /* Do we have a value to check for? */
154 {
155 bool fUpdate = false;
156 /* Always update this property, no matter what? */
157 if (pNode->uFlags & VBOXSERVICEPROPCACHEFLAG_ALWAYS_UPDATE)
158 fUpdate = true;
159 /* Did the value change so we have to update? */
160 else if (pNode->pszValue && strcmp(pNode->pszValue, pszValue) != 0)
161 fUpdate = true;
162 /* No value stored at the moment but we have a value now? */
163 else if (pNode->pszValue == NULL)
164 fUpdate = true;
165
166 if (fUpdate)
167 {
168 /* Write the update. */
169 rc = VBoxServiceWritePropF(pCache->uClientID, pNode->pszName, pszValue);
170 if (pNode->pszValue)
171 RTStrFree(pNode->pszValue);
172 pNode->pszValue = RTStrDup(pszValue);
173 }
174 else
175 rc = VINF_ALREADY_INITIALIZED; /* No update needed. */
176 }
177 else
178 {
179 /* No value specified. Deletion (or no action required). */
180 if (pNode->pszValue) /* Did we have a value before? Then the value needs to be deleted. */
181 {
182 /* Delete property (but do not remove from cache) if not deleted yet. */
183 rc = VBoxServiceWritePropF(pCache->uClientID, pNode->pszName, NULL);
184 RTStrFree(pNode->pszValue);
185 pNode->pszValue = NULL;
186 }
187 else
188 rc = VINF_ALREADY_INITIALIZED; /* No update needed. */
189 }
190
191 /* Update rest of the fields. */
192 if (pszValueReset)
193 {
194 if (pNode->pszValueReset)
195 RTStrFree(pNode->pszValueReset);
196 pNode->pszValueReset = RTStrDup(pszValueReset);
197 }
198 if (u32Flags)
199 pNode->uFlags = u32Flags;
200
201 /* Delete temp stuff. */
202 if (pszValue)
203 RTStrFree(pszValue);
204 return rc;
205}
206
207
208/** @todo Docs */
209void VBoxServicePropCacheDestroy(PVBOXSERVICEVEPROPCACHE pCache)
210{
211 AssertPtr(pCache);
212 Assert(pCache->uClientID);
213 PVBOXSERVICEVEPROPCACHEENTRY pNode;
214 RTListForEach(&pCache->Node, pNode, VBOXSERVICEVEPROPCACHEENTRY, Node)
215 {
216 if ((pNode->uFlags & VBOXSERVICEPROPCACHEFLAG_TEMPORARY) == 0)
217 {
218 if (pNode->pszValueReset) /* Write reset value? */
219 {
220 /* rc = */VBoxServiceWritePropF(pCache->uClientID, pNode->pszName, pNode->pszValueReset);
221 }
222 else /* Delete value. */
223 /* rc = */VBoxServiceWritePropF(pCache->uClientID, pNode->pszName, NULL);
224 }
225
226 AssertPtr(pNode->pszName);
227 RTStrFree(pNode->pszName);
228 if (pNode->pszValue)
229 RTStrFree(pNode->pszValue);
230 if (pNode->pszValueReset)
231 RTStrFree(pNode->pszValueReset);
232 pNode->uFlags = 0;
233 }
234
235 pNode = RTListNodeGetFirst(&pCache->Node, VBOXSERVICEVEPROPCACHEENTRY, Node);
236 while (pNode)
237 {
238 PVBOXSERVICEVEPROPCACHEENTRY pNext = RTListNodeGetNext(&pNode->Node, VBOXSERVICEVEPROPCACHEENTRY, Node);
239 RTListNodeRemove(&pNode->Node);
240 if (pNext && RTListNodeIsLast(&pCache->Node, &pNext->Node))
241 break;
242 pNode = pNext;
243 }
244}
245#endif /* VBOX_WITH_GUEST_PROPS */
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