VirtualBox

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

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

VBoxService/PropCache: Bugfixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Id: VBoxServicePropCache.cpp 28972 2010-05-03 12:25:23Z 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 va_list va;
96 va_start(va, pszValueFormat);
97 char *pszValue;
98 int rc = RTStrAPrintfV(&pszValue, pszValueFormat, va);
99 if (RT_SUCCESS(rc))
100 {
101 rc = VBoxServicePropCacheUpdateEx(pCache, pszName, 0 /* Not used */, NULL /* Not used */, pszValue);
102 RTStrFree(pszValue);
103 }
104 va_end(va);
105 return rc;
106}
107
108
109/**
110 * Updates the local guest property cache and writes it to HGCM if outdated.
111 *
112 * @returns VBox status code. Errors will be logged.
113 *
114 * @param u32ClientId The HGCM client ID for the guest property session.
115 * @param pszName The property name.
116 * @param pszValueFormat The property format string. If this is NULL then
117 * the property will be deleted (if possible).
118 * @param ... Format arguments.
119 */
120int VBoxServicePropCacheUpdateEx(PVBOXSERVICEVEPROPCACHE pCache, const char *pszName, uint32_t u32Flags, const char *pszValueReset, const char *pszValueFormat, ...)
121{
122 AssertPtr(pCache);
123 Assert(pCache->uClientID);
124 AssertPtr(pszName);
125 AssertPtr(pszValueFormat);
126
127 int rc = VINF_SUCCESS;
128 char *pszValue = NULL;
129 if (pszValueFormat)
130 {
131 va_list va;
132 va_start(va, pszValueFormat);
133 rc = RTStrAPrintfV(&pszValue, pszValueFormat, va);
134 va_end(va);
135 }
136
137 PVBOXSERVICEVEPROPCACHEENTRY pNode = VBoxServicePropCacheFind(pCache, pszName, 0);
138 if (pNode != NULL)
139 {
140 if (pszValue)
141 {
142 bool fUpdate = false;
143 if (pNode->uFlags & VBOXSERVICEPROPCACHEFLAG_ALWAYS_UPDATE)
144 fUpdate = true;
145 else if (pNode->pszValue && strcmp(pNode->pszValue, pszValue) != 0)
146 fUpdate = true;
147
148 if (fUpdate)
149 {
150 /* Write the update. */
151 rc = VBoxServiceWritePropF(pCache->uClientID, pNode->pszName, pszValue);
152 if (pNode->pszValue)
153 RTStrFree(pNode->pszValue);
154 pNode->pszValue = RTStrDup(pszValue);
155 if (pszValueReset)
156 {
157 if (pNode->pszValueReset)
158 RTStrFree(pNode->pszValueReset);
159 pNode->pszValueReset = RTStrDup(pszValueReset);
160 }
161 if (u32Flags)
162 pNode->uFlags = u32Flags;
163 }
164 else
165 rc = VINF_ALREADY_INITIALIZED; /* No update needed. */
166 }
167 else if (pNode->pszValue)
168 {
169 /* Delete property (but do not remove from cache) if not deleted yet. */
170 VBoxServiceWritePropF(pCache->uClientID, pNode->pszName, NULL);
171 RTStrFree(pNode->pszValue);
172 pNode->pszValue = NULL;
173 }
174 }
175 else /* Node not found; add it and update HGCM service. */
176 {
177 PVBOXSERVICEVEPROPCACHEENTRY pNewNode = (PVBOXSERVICEVEPROPCACHEENTRY)RTMemAlloc(sizeof(VBOXSERVICEVEPROPCACHEENTRY));
178 pNewNode->pszName = RTStrDup(pszName);
179 if (pszValue)
180 pNewNode->pszValue = RTStrDup(pszValue);
181 else
182 pNewNode->pszValue = NULL;
183 if (pszValueReset)
184 pNewNode->pszValueReset = RTStrDup(pszValueReset);
185 else
186 pNewNode->pszValueReset = NULL;
187 pNewNode->uFlags = u32Flags;
188 /*rc =*/ RTListAppend(&pCache->Node, &pNewNode->Node);
189 if (RT_SUCCESS(rc))
190 rc = VERR_NOT_FOUND;
191 }
192
193 if (pszValue)
194 RTStrFree(pszValue);
195 return rc;
196}
197
198
199/** @todo Docs */
200void VBoxServicePropCacheDestroy(PVBOXSERVICEVEPROPCACHE pCache)
201{
202 AssertPtr(pCache);
203 Assert(pCache->uClientID);
204 PVBOXSERVICEVEPROPCACHEENTRY pNode;
205 RTListForEach(&pCache->Node, pNode, VBOXSERVICEVEPROPCACHEENTRY, Node)
206 {
207 if (pNode->uFlags & VBOXSERVICEPROPCACHEFLAG_TEMPORARY == 0)
208 {
209 if (pNode->pszValueReset) /* Write reset value? */
210 {
211 /* rc = */VBoxServiceWritePropF(pCache->uClientID, pNode->pszName, pNode->pszValueReset);
212 }
213 else /* Delete value. */
214 /* rc = */VBoxServiceWritePropF(pCache->uClientID, pNode->pszName, NULL);
215 }
216
217 AssertPtr(pNode->pszName);
218 RTStrFree(pNode->pszName);
219 if (pNode->pszValue)
220 RTStrFree(pNode->pszValue);
221 if (pNode->pszValueReset)
222 RTStrFree(pNode->pszValueReset);
223 pNode->uFlags = 0;
224 }
225
226 pNode = RTListNodeGetFirst(&pCache->Node, VBOXSERVICEVEPROPCACHEENTRY, Node);
227 while (pNode)
228 {
229 PVBOXSERVICEVEPROPCACHEENTRY pNext = RTListNodeGetNext(&pNode->Node, VBOXSERVICEVEPROPCACHEENTRY, Node);
230 RTListNodeRemove(&pNode->Node);
231 if (pNext && RTListNodeIsLast(&pCache->Node, &pNext->Node))
232 break;
233 pNode = pNext;
234 }
235}
236#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