1 | /** @file
|
---|
2 | *
|
---|
3 | * Testcase for shared folder case conversion
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #include <VBox/shflsvc.h>
|
---|
26 | #include <iprt/stream.h>
|
---|
27 | #include <iprt/alloc.h>
|
---|
28 | #include <iprt/assert.h>
|
---|
29 | #include <iprt/fs.h>
|
---|
30 | #include <iprt/dir.h>
|
---|
31 | #include <iprt/file.h>
|
---|
32 | #include <iprt/path.h>
|
---|
33 | #include <iprt/string.h>
|
---|
34 | #include <iprt/uni.h>
|
---|
35 | #include <stdio.h>
|
---|
36 |
|
---|
37 | #undef Log
|
---|
38 | #define Log(a) printf a
|
---|
39 |
|
---|
40 | #define RTPathQueryInfo rtPathQueryInfo
|
---|
41 |
|
---|
42 | static int iDirList = 0;
|
---|
43 | static int iDirFile = 0;
|
---|
44 |
|
---|
45 | static char *pszDirList[] =
|
---|
46 | {
|
---|
47 | "c:",
|
---|
48 | "c:\\test dir",
|
---|
49 | "c:\\test dir\\SUBDIR",
|
---|
50 | };
|
---|
51 |
|
---|
52 | static char *pszDirListC[] =
|
---|
53 | {
|
---|
54 | ".",
|
---|
55 | "..",
|
---|
56 | "test dir"
|
---|
57 | };
|
---|
58 |
|
---|
59 | static char *pszDirListTestdir[] =
|
---|
60 | {
|
---|
61 | ".",
|
---|
62 | "..",
|
---|
63 | "SUBDIR",
|
---|
64 | "a.bat",
|
---|
65 | "aTestJe.bat",
|
---|
66 | "aTestje.bat",
|
---|
67 | "b.bat",
|
---|
68 | "c.bat",
|
---|
69 | "d.bat",
|
---|
70 | "e.bat",
|
---|
71 | "f.bat",
|
---|
72 | "g.bat",
|
---|
73 | "h.bat",
|
---|
74 | "x.bat",
|
---|
75 | "z.bat",
|
---|
76 | };
|
---|
77 |
|
---|
78 | static char *pszDirListSUBDIR[] =
|
---|
79 | {
|
---|
80 | ".",
|
---|
81 | "..",
|
---|
82 | "a.bat",
|
---|
83 | "aTestJe.bat",
|
---|
84 | "aTestje.bat",
|
---|
85 | "b.bat",
|
---|
86 | "c.bat",
|
---|
87 | "d.bat",
|
---|
88 | "e.bat",
|
---|
89 | "f.bat",
|
---|
90 | "g.bat",
|
---|
91 | "h.bat",
|
---|
92 | "x.bat",
|
---|
93 | "z.bat",
|
---|
94 | };
|
---|
95 |
|
---|
96 | int rtDirOpenFiltered(PRTDIR *ppDir, const char *pszPath, RTDIRFILTER enmFilter)
|
---|
97 | {
|
---|
98 | if (!strcmp(pszPath, "c:\\*"))
|
---|
99 | iDirList = 1;
|
---|
100 | else
|
---|
101 | if (!strcmp(pszPath, "c:\\test dir\\*"))
|
---|
102 | iDirList = 2;
|
---|
103 | else
|
---|
104 | if (!strcmp(pszPath, "c:\\test dir\\SUBDIR\\*"))
|
---|
105 | iDirList = 3;
|
---|
106 | else
|
---|
107 | AssertFailed();
|
---|
108 |
|
---|
109 | return VINF_SUCCESS;
|
---|
110 | }
|
---|
111 |
|
---|
112 | int rtDirClose(PRTDIR pDir)
|
---|
113 | {
|
---|
114 | iDirFile = 0;
|
---|
115 | return VINF_SUCCESS;
|
---|
116 | }
|
---|
117 |
|
---|
118 | int rtDirReadEx(PRTDIR pDir, PRTDIRENTRYEX pDirEntry, unsigned *pcbDirEntry, RTFSOBJATTRADD enmAdditionalAttribs)
|
---|
119 | {
|
---|
120 | switch(iDirList)
|
---|
121 | {
|
---|
122 | case 1:
|
---|
123 | if (iDirFile == RT_ELEMENTS(pszDirListC))
|
---|
124 | return VERR_NO_MORE_FILES;
|
---|
125 | pDirEntry->cbName = strlen(pszDirListC[iDirFile]);
|
---|
126 | strcpy(pDirEntry->szName, pszDirListC[iDirFile++]);
|
---|
127 | break;
|
---|
128 | case 2:
|
---|
129 | if (iDirFile == RT_ELEMENTS(pszDirListTestdir))
|
---|
130 | return VERR_NO_MORE_FILES;
|
---|
131 | pDirEntry->cbName = strlen(pszDirListTestdir[iDirFile]);
|
---|
132 | strcpy(pDirEntry->szName, pszDirListTestdir[iDirFile++]);
|
---|
133 | break;
|
---|
134 | case 3:
|
---|
135 | if (iDirFile == RT_ELEMENTS(pszDirListSUBDIR))
|
---|
136 | return VERR_NO_MORE_FILES;
|
---|
137 | pDirEntry->cbName = strlen(pszDirListSUBDIR[iDirFile]);
|
---|
138 | strcpy(pDirEntry->szName, pszDirListSUBDIR[iDirFile++]);
|
---|
139 | break;
|
---|
140 | }
|
---|
141 | return VINF_SUCCESS;
|
---|
142 | }
|
---|
143 |
|
---|
144 | int rtPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
|
---|
145 | {
|
---|
146 | int cMax;
|
---|
147 | char **ppszDirList;
|
---|
148 |
|
---|
149 | /* first try pszDirList */
|
---|
150 | for (int i=0;i<RT_ELEMENTS(pszDirList);i++)
|
---|
151 | {
|
---|
152 | if(!strcmp(pszPath, pszDirList[i]))
|
---|
153 | return VINF_SUCCESS;
|
---|
154 | }
|
---|
155 |
|
---|
156 | switch(iDirList)
|
---|
157 | {
|
---|
158 | case 1:
|
---|
159 | cMax = RT_ELEMENTS(pszDirListC);
|
---|
160 | ppszDirList = pszDirListC;
|
---|
161 | break;
|
---|
162 | case 2:
|
---|
163 | cMax = RT_ELEMENTS(pszDirListTestdir);
|
---|
164 | ppszDirList = pszDirListTestdir;
|
---|
165 | break;
|
---|
166 | case 3:
|
---|
167 | cMax = RT_ELEMENTS(pszDirListSUBDIR);
|
---|
168 | ppszDirList = pszDirListSUBDIR;
|
---|
169 | break;
|
---|
170 | default:
|
---|
171 | return VERR_FILE_NOT_FOUND;
|
---|
172 | }
|
---|
173 | for (int i=0;i<cMax;i++)
|
---|
174 | {
|
---|
175 | if(!strcmp(pszPath, ppszDirList[i]))
|
---|
176 | return VINF_SUCCESS;
|
---|
177 | }
|
---|
178 | return VERR_FILE_NOT_FOUND;
|
---|
179 | }
|
---|
180 |
|
---|
181 | static int vbsfCorrectCasing(char *pszFullPath, char *pszStartComponent)
|
---|
182 | {
|
---|
183 | PRTDIRENTRYEX pDirEntry = NULL;
|
---|
184 | uint32_t cbDirEntry, cbComponent;
|
---|
185 | int rc = VERR_FILE_NOT_FOUND;
|
---|
186 | PRTDIR hSearch;
|
---|
187 |
|
---|
188 | cbComponent = strlen(pszStartComponent);
|
---|
189 |
|
---|
190 | cbDirEntry = 4096;
|
---|
191 | pDirEntry = (PRTDIRENTRYEX)RTMemAlloc(cbDirEntry);
|
---|
192 | if (pDirEntry == 0)
|
---|
193 | {
|
---|
194 | AssertFailed();
|
---|
195 | return VERR_NO_MEMORY;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /** @todo this is quite inefficient, especially for directories with many files */
|
---|
199 | Assert(pszFullPath < pszStartComponent-1);
|
---|
200 | Assert(*(pszStartComponent-1) == RTPATH_DELIMITER);
|
---|
201 | *(pszStartComponent-1) = 0;
|
---|
202 | strcpy(pDirEntry->szName, pszFullPath);
|
---|
203 | strcat(pDirEntry->szName, "\\*");
|
---|
204 | rc = rtDirOpenFiltered (&hSearch, pDirEntry->szName, RTDIRFILTER_WINNT);
|
---|
205 | *(pszStartComponent-1) = RTPATH_DELIMITER;
|
---|
206 | if (VBOX_FAILURE(rc))
|
---|
207 | goto end;
|
---|
208 |
|
---|
209 | for(;;)
|
---|
210 | {
|
---|
211 | uint32_t cbDirEntrySize = cbDirEntry;
|
---|
212 |
|
---|
213 | rc = rtDirReadEx(hSearch, pDirEntry, &cbDirEntrySize, RTFSOBJATTRADD_NOTHING);
|
---|
214 | if (rc == VERR_NO_MORE_FILES)
|
---|
215 | break;
|
---|
216 |
|
---|
217 | if (VINF_SUCCESS != rc && rc != VWRN_NO_DIRENT_INFO)
|
---|
218 | {
|
---|
219 | AssertFailed();
|
---|
220 | if (rc != VERR_NO_TRANSLATION)
|
---|
221 | break;
|
---|
222 | else
|
---|
223 | continue;
|
---|
224 | }
|
---|
225 | if ( pDirEntry->cbName == cbComponent
|
---|
226 | && !RTStrICmp(pszStartComponent, &pDirEntry->szName[0]))
|
---|
227 | {
|
---|
228 | Log(("Found original name %s (%s)\n", &pDirEntry->szName[0], pszStartComponent));
|
---|
229 | strcpy(pszStartComponent, &pDirEntry->szName[0]);
|
---|
230 | rc = VINF_SUCCESS;
|
---|
231 | break;
|
---|
232 | }
|
---|
233 | }
|
---|
234 | Assert(VBOX_SUCCESS(rc));
|
---|
235 |
|
---|
236 | end:
|
---|
237 | if (pDirEntry)
|
---|
238 | RTMemFree(pDirEntry);
|
---|
239 |
|
---|
240 | rtDirClose(hSearch);
|
---|
241 | return rc;
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 |
|
---|
246 | int testCase(char *pszFullPath)
|
---|
247 | {
|
---|
248 | int rc;
|
---|
249 | RTFSOBJINFO info;
|
---|
250 | rc = RTPathQueryInfo(pszFullPath, &info, RTFSOBJATTRADD_NOTHING);
|
---|
251 | if (rc == VERR_FILE_NOT_FOUND || rc == VERR_PATH_NOT_FOUND)
|
---|
252 | {
|
---|
253 | uint32_t len = strlen(pszFullPath);
|
---|
254 | char *src = pszFullPath + len - 1;
|
---|
255 |
|
---|
256 | Log(("Handle case insenstive guest fs on top of host case sensitive fs for %s\n", pszFullPath));
|
---|
257 |
|
---|
258 | /* Find partial path that's valid */
|
---|
259 | while(src > pszFullPath)
|
---|
260 | {
|
---|
261 | if (*src == RTPATH_DELIMITER)
|
---|
262 | {
|
---|
263 | *src = 0;
|
---|
264 | rc = RTPathQueryInfo (pszFullPath, &info, RTFSOBJATTRADD_NOTHING);
|
---|
265 | *src = RTPATH_DELIMITER;
|
---|
266 | if (rc == VINF_SUCCESS)
|
---|
267 | {
|
---|
268 | #ifdef DEBUG
|
---|
269 | *src = 0;
|
---|
270 | Log(("Found valid partial path %s\n", pszFullPath));
|
---|
271 | *src = RTPATH_DELIMITER;
|
---|
272 | #endif
|
---|
273 | break;
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | src--;
|
---|
278 | }
|
---|
279 | Assert(*src == RTPATH_DELIMITER && VBOX_SUCCESS(rc));
|
---|
280 | if ( *src == RTPATH_DELIMITER
|
---|
281 | && VBOX_SUCCESS(rc))
|
---|
282 | {
|
---|
283 | src++;
|
---|
284 | for(;;)
|
---|
285 | {
|
---|
286 | char *end = src;
|
---|
287 | bool fEndOfString = true;
|
---|
288 |
|
---|
289 | while(*end)
|
---|
290 | {
|
---|
291 | if (*end == RTPATH_DELIMITER)
|
---|
292 | break;
|
---|
293 | end++;
|
---|
294 | }
|
---|
295 |
|
---|
296 | if (*end == RTPATH_DELIMITER)
|
---|
297 | {
|
---|
298 | fEndOfString = false;
|
---|
299 | *end = 0;
|
---|
300 | rc = RTPathQueryInfo(src, &info, RTFSOBJATTRADD_NOTHING);
|
---|
301 | Assert(rc == VINF_SUCCESS || rc == VERR_FILE_NOT_FOUND || rc == VERR_PATH_NOT_FOUND);
|
---|
302 | }
|
---|
303 | else
|
---|
304 | rc = VERR_FILE_NOT_FOUND;
|
---|
305 |
|
---|
306 | if (rc == VERR_FILE_NOT_FOUND || rc == VERR_PATH_NOT_FOUND)
|
---|
307 | {
|
---|
308 | /* path component is invalid; try to correct the casing */
|
---|
309 | rc = vbsfCorrectCasing(pszFullPath, src);
|
---|
310 | if (VBOX_FAILURE(rc))
|
---|
311 | break;
|
---|
312 | }
|
---|
313 |
|
---|
314 | if (fEndOfString)
|
---|
315 | break;
|
---|
316 |
|
---|
317 | *end = RTPATH_DELIMITER;
|
---|
318 | src = end + 1;
|
---|
319 | }
|
---|
320 | Assert(rc == VINF_SUCCESS);
|
---|
321 | }
|
---|
322 | else
|
---|
323 | rc = VERR_FILE_NOT_FOUND;
|
---|
324 |
|
---|
325 | }
|
---|
326 |
|
---|
327 | if (VBOX_SUCCESS(rc))
|
---|
328 | Log(("New valid path %s\n", pszFullPath));
|
---|
329 | return rc;
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 | int main(int argc, char **argv)
|
---|
334 | {
|
---|
335 | char szTest[128];
|
---|
336 |
|
---|
337 | strcpy(szTest, "c:\\test Dir\\z.bAt");
|
---|
338 | testCase(szTest);
|
---|
339 | strcpy(szTest, "c:\\test dir\\z.bAt");
|
---|
340 | testCase(szTest);
|
---|
341 | strcpy(szTest, "c:\\test dir\\SUBDIR\\z.bAt");
|
---|
342 | testCase(szTest);
|
---|
343 | strcpy(szTest, "c:\\test dir\\SUBDiR\\atestje.bat");
|
---|
344 | testCase(szTest);
|
---|
345 | strcpy(szTest, "c:\\TEST dir\\subDiR\\aTestje.baT");
|
---|
346 | testCase(szTest);
|
---|
347 | return 0;
|
---|
348 | }
|
---|