VirtualBox

source: kBuild/trunk/src/lib/nt/tstkFsCache.c@ 3380

Last change on this file since 3380 was 3380, checked in by bird, 4 years ago

Cache testcase.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.9 KB
Line 
1/* $Id: tstkFsCache.c 3380 2020-06-12 00:55:31Z bird $ */
2/** @file
3 * kFsCache testcase.
4 */
5
6/*
7 * Copyright (c) 2020 knut st. osmundsen <[email protected]>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 *
27 * Alternatively, the content of this file may be used under the terms of the
28 * GPL version 2 or later, or LGPL version 2.1 or later.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <direct.h>
35#include <errno.h>
36#include <process.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include "kFsCache.h"
41
42#include <windows.h>
43
44
45/*********************************************************************************************************************************
46* Global Variables *
47*********************************************************************************************************************************/
48static unsigned g_cErrors = 0;
49
50
51/*********************************************************************************************************************************
52* Defined Constants And Macros *
53*********************************************************************************************************************************/
54#define CHECK_RETV(a_Expr) do { \
55 if (!(a_Expr)) \
56 { \
57 g_cErrors++; \
58 fprintf(stderr, "error(%u): %s\n", __LINE__, #a_Expr);\
59 return; \
60 } \
61 } while (0)
62
63#define CHECK(a_Expr) do { \
64 if (!(a_Expr)) \
65 { \
66 g_cErrors++; \
67 fprintf(stderr, "error(%u): %s\n", __LINE__, #a_Expr);\
68 } \
69 } while (0)
70
71static int myMkDir(const char *pszPath)
72{
73 if (_mkdir(pszPath) == 0)
74 return 0;
75 fprintf(stderr, "_mkdir(%s) -> errno=%d\n", pszPath, errno);
76 return -1;
77}
78
79static int myCreateFile(const char *pszPath)
80{
81 FILE *pFile = fopen(pszPath, "w");
82 if (pFile)
83 {
84 fclose(pFile);
85 return 0;
86 }
87 fprintf(stderr, "fopen(%s,w) -> errno=%d\n", pszPath, errno);
88 return -1;
89}
90
91static void test1(const char *pszWorkDir)
92{
93 char szPath[4096];
94 size_t cchWorkDir = strlen(pszWorkDir);
95 PKFSCACHE pCache;
96 KFSLOOKUPERROR enmLookupError;
97 PKFSOBJ pFsObj;
98
99 CHECK_RETV(cchWorkDir < sizeof(szPath) - 1024);
100 memcpy(szPath, pszWorkDir, cchWorkDir);
101 cchWorkDir += sprintf(&szPath[cchWorkDir], "\\tstkFsCache%u", _getpid());
102 CHECK_RETV(myMkDir(szPath) == 0);
103
104 pCache = kFsCacheCreate(KFSCACHE_F_MISSING_OBJECTS | KFSCACHE_F_MISSING_PATHS);
105 CHECK_RETV(pCache != NULL);
106
107 enmLookupError = (KFSLOOKUPERROR)-1;
108 CHECK((pFsObj = kFsCacheLookupA(pCache, szPath, &enmLookupError)) != NULL);
109
110#if 0
111 /*
112 * Accidentally left out the '\' in front of the filename, so it ended up in
113 * a temp dir with almost 1000 files and that triggered a refresh issue.
114 */
115 /* Negative lookup followed by creation of that file. */
116 enmLookupError = (KFSLOOKUPERROR)-1;
117 sprintf(&szPath[cchWorkDir], "file1.txt");
118 CHECK((pFsObj = kFsCacheLookupA(pCache, szPath, &enmLookupError)) != NULL);
119 if (pFsObj)
120 CHECK(pFsObj->bObjType == KFSOBJ_TYPE_MISSING);
121
122 CHECK(myCreateFile(szPath) == 0);
123
124 CHECK((pFsObj = kFsCacheLookupA(pCache, szPath, &enmLookupError)) != NULL);
125 if (pFsObj)
126 CHECK(pFsObj->bObjType == KFSOBJ_TYPE_MISSING);
127
128 kFsCacheInvalidateAll(pCache);
129 CHECK((pFsObj = kFsCacheLookupA(pCache, szPath, &enmLookupError)) != NULL);
130 if (pFsObj)
131 {
132 CHECK(pFsObj->bObjType == KFSOBJ_TYPE_FILE);
133 if (pFsObj->bObjType != KFSOBJ_TYPE_FILE)
134 fprintf(stderr, "bObjType=%d\n", pFsObj->bObjType);
135 }
136#endif
137
138 /*
139 * Try emulate the temp issue above. Seem to require several files.
140 */
141 szPath[cchWorkDir++] = '\\';
142 sprintf(&szPath[cchWorkDir], "longfilename1.txt");
143 CHECK(myCreateFile(szPath) == 0);
144 sprintf(&szPath[cchWorkDir], "longfilename2.txt");
145 CHECK(myCreateFile(szPath) == 0);
146#if 1
147 /* no file 3 */
148 sprintf(&szPath[cchWorkDir], "longfilename4.txt");
149 CHECK(myCreateFile(szPath) == 0);
150 sprintf(&szPath[cchWorkDir], "longfilename5.txt");
151 CHECK(myCreateFile(szPath) == 0);
152 /* no file 6 */
153 sprintf(&szPath[cchWorkDir], "longfilename7.txt");
154 CHECK(myCreateFile(szPath) == 0);
155#endif
156
157 enmLookupError = (KFSLOOKUPERROR)-1;
158 sprintf(&szPath[cchWorkDir], "longfilename3.txt");
159 CHECK((pFsObj = kFsCacheLookupA(pCache, szPath, &enmLookupError)) != NULL);
160 CHECK(pFsObj && pFsObj->bObjType == KFSOBJ_TYPE_MISSING);
161
162 enmLookupError = (KFSLOOKUPERROR)-1;
163 sprintf(&szPath[cchWorkDir], "longfilename6.txt");
164 CHECK((pFsObj = kFsCacheLookupA(pCache, szPath, &enmLookupError)) != NULL);
165 CHECK(pFsObj && pFsObj->bObjType == KFSOBJ_TYPE_MISSING);
166
167Sleep(100);
168 sprintf(&szPath[cchWorkDir], "longfilename3.txt");
169 CHECK(myCreateFile(szPath) == 0);
170 sprintf(&szPath[cchWorkDir], "longfilename6.txt");
171 CHECK(myCreateFile(szPath) == 0);
172
173 sprintf(&szPath[cchWorkDir], "longfilename3.txt");
174 CHECK((pFsObj = kFsCacheLookupA(pCache, szPath, &enmLookupError)) != NULL);
175 CHECK(pFsObj && pFsObj->bObjType == KFSOBJ_TYPE_MISSING);
176
177 sprintf(&szPath[cchWorkDir], "longfilename6.txt");
178 CHECK((pFsObj = kFsCacheLookupA(pCache, szPath, &enmLookupError)) != NULL);
179 CHECK(pFsObj && pFsObj->bObjType == KFSOBJ_TYPE_MISSING);
180
181 kFsCacheInvalidateAll(pCache);
182
183 sprintf(&szPath[cchWorkDir], "longfilename3.txt");
184 CHECK((pFsObj = kFsCacheLookupA(pCache, szPath, &enmLookupError)) != NULL);
185 if (pFsObj)
186 {
187 CHECK(pFsObj->bObjType == KFSOBJ_TYPE_FILE);
188 if (pFsObj->bObjType != KFSOBJ_TYPE_FILE)
189 fprintf(stderr, "bObjType=%d\n", pFsObj->bObjType);
190 }
191
192 sprintf(&szPath[cchWorkDir], "longfilename6.txt");
193 CHECK((pFsObj = kFsCacheLookupA(pCache, szPath, &enmLookupError)) != NULL);
194 if (pFsObj)
195 {
196 CHECK(pFsObj->bObjType == KFSOBJ_TYPE_FILE);
197 if (pFsObj->bObjType != KFSOBJ_TYPE_FILE)
198 fprintf(stderr, "bObjType=%d\n", pFsObj->bObjType);
199 }
200}
201
202static int usage(int rcExit)
203{
204 printf("usage: tstkFsCache [--workdir dir]\n"
205 "\n"
206 "Test program of the kFsCache. May leave stuff behind in the work\n"
207 "directory requiring manual cleanup.\n"
208 );
209 return rcExit;
210}
211
212int main(int argc, char **argv)
213{
214 const char *pszWorkDir = NULL;
215 int i;
216
217 /*
218 * Parse arguments.
219 */
220 for (i = 1; i < argc; i++)
221 {
222 if (argv[i][0] == '-')
223 {
224 const char *pszValue;
225 const char *psz = &argv[i][1];
226 char chOpt;
227 chOpt = *psz++;
228 if (chOpt == '-')
229 {
230 /* Convert long to short option. */
231 if (!strcmp(psz, "workdir"))
232 chOpt = 'w';
233 else if (!strcmp(psz, "help"))
234 chOpt = '?';
235 else if (!strcmp(psz, "version"))
236 chOpt = 'V';
237 else
238 return usage(2);
239 psz = "";
240 }
241
242 /*
243 * Requires value?
244 */
245 switch (chOpt)
246 {
247 case 'w':
248 if (*psz)
249 pszValue = psz;
250 else if (++i < argc)
251 pszValue = argv[i];
252 else
253 {
254 fprintf(stderr, "The '-%c' option takes a value.\n", chOpt);
255 return 2;
256 }
257 break;
258
259 default:
260 pszValue = NULL;
261 break;
262 }
263
264 switch (chOpt)
265 {
266 case 'w':
267 pszWorkDir = pszValue;
268 break;
269
270 case '?':
271 return usage(0);
272 case 'V':
273 printf("0.0.0\n");
274 return 0;
275
276 /*
277 * Invalid argument.
278 */
279 default:
280 fprintf(stderr, "syntax error: Invalid option '%s'.\n", argv[i]);
281 return 2;
282 }
283 }
284 else
285 {
286 fprintf(stderr, "syntax error: Invalid argument '%s'.\n", argv[i]);
287 return 2;
288 }
289 }
290
291 /*
292 * Resolve defaults.
293 */
294 if (!pszWorkDir)
295 {
296 pszWorkDir = getenv("TEMP");
297 if (!pszWorkDir)
298 pszWorkDir = ".";
299 }
300
301 /*
302 * Do the testing.
303 */
304 test1(pszWorkDir);
305
306 if (!g_cErrors)
307 printf("Success!\n");
308 else
309 printf("Failed - %u errors!\n", g_cErrors);
310 return g_cErrors == 0 ? 0 : 1;
311}
312
313
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