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