VirtualBox

source: vbox/trunk/src/VBox/Additions/os2/VBoxSF/VBoxSFFind.cpp@ 93071

Last change on this file since 93071 was 93071, checked in by vboxsync, 3 years ago

os2/VBoxSF: Fixed off by one check in vboxSfOs2MakeEmptyEaListEx. Fixed problem with missing FEAList length when called from vboxSfOs2ReadDirEntries. ticketref:19453

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 35.2 KB
Line 
1/** $Id: VBoxSFFind.cpp 93071 2021-12-24 00:12:04Z vboxsync $ */
2/** @file
3 * VBoxSF - OS/2 Shared Folders, Find File IFS EPs.
4 */
5
6/*
7 * Copyright (c) 2007-2018 knut st. osmundsen <bird-src-spam@anduin.net>
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31
32/*********************************************************************************************************************************
33* Header Files *
34*********************************************************************************************************************************/
35#define LOG_GROUP LOG_GROUP_DEFAULT
36#include "VBoxSFInternal.h"
37
38#include <VBox/log.h>
39#include <iprt/asm.h>
40#include <iprt/assert.h>
41#include <iprt/mem.h>
42#include <iprt/path.h>
43#include <iprt/err.h>
44
45
46
47/**
48 * Checks if the given name is 8-dot-3 compatible.
49 *
50 * @returns true if compatible, false if not.
51 * @param pwszName The name to inspect (UTF-16).
52 * @param cwcName The length of the name.
53 * @param pszTmp Buffer for test conversions.
54 * @param cbTmp The size of the buffer.
55 */
56static bool vboxSfOs2IsUtf16Name8dot3(PRTUTF16 pwszName, size_t cwcName, char *pszTmp, size_t cbTmp)
57{
58 /* Reject names that must be too long. */
59 if (cwcName > 8 + 1 + 3)
60 return false;
61
62 /* First char cannot be a dot, nor can it be an empty string. */
63 if (*pwszName == '.' || !*pwszName)
64 return false;
65
66 /*
67 * To basic checks on code point level before doing full conversion.
68 */
69 for (unsigned off = 0; ; off++)
70 {
71 RTUTF16 wc = pwszName[off];
72 if (wc == '.')
73 {
74 unsigned const offMax = off + 3;
75 for (++off;; off++)
76 {
77 wc = pwszName[off];
78 if (!wc)
79 break;
80 if (wc == '.')
81 return false;
82 if (off > offMax)
83 return false;
84 }
85 break;
86 }
87 if (!wc)
88 break;
89 if (off >= 8)
90 return false;
91 }
92
93 /*
94 * Convert to the native code page.
95 */
96 APIRET rc = SafeKernStrFromUcs(NULL, pszTmp, pwszName, cbTmp, cwcName);
97 if (rc != NO_ERROR)
98 {
99 LogRel(("vboxSfOs2IsUtf8Name8dot3: SafeKernStrFromUcs failed: %d\n", rc));
100 return false;
101 }
102
103 /*
104 * Redo the check.
105 * Note! This could be bogus if a DBCS leadin sequence collides with '.'.
106 */
107 for (unsigned cch = 0; ; cch++)
108 {
109 char ch = *pszTmp++;
110 if (ch == '.')
111 break;
112 if (ch == '\0')
113 return true;
114 if (cch >= 8)
115 return false;
116 }
117 for (unsigned cch = 0; ; cch++)
118 {
119 char ch = *pszTmp++;
120 if (ch == '\0')
121 return true;
122 if (ch == '.')
123 return false;
124 if (cch >= 3)
125 return false;
126 }
127}
128
129
130/**
131 * @returns Updated pbDst on success, NULL on failure.
132 */
133static uint8_t *vboxSfOs2CopyUtf16Name(uint8_t *pbDst, PRTUTF16 pwszSrc, size_t cwcSrc)
134{
135 char *pszDst = (char *)pbDst + 1;
136 APIRET rc = SafeKernStrFromUcs(NULL, pszDst, pwszSrc, CCHMAXPATHCOMP, cwcSrc);
137 if (rc == NO_ERROR)
138 {
139 size_t cchDst = strlen(pszDst);
140 *pbDst++ = (uint8_t)cchDst;
141 pbDst += cchDst;
142 *pbDst++ = '\0';
143 return pbDst;
144 }
145 LogRel(("vboxSfOs2CopyUtf8Name: SafeKernStrFromUcs failed: %d\n", rc));
146 return NULL;
147}
148
149
150/**
151 * @returns Updated pbDst on success, NULL on failure.
152 */
153static uint8_t *vboxSfOs2CopyUtf16NameAndUpperCase(uint8_t *pbDst, PRTUTF16 pwszSrc, size_t cwcSrc)
154{
155 char *pszDst = (char *)(pbDst + 1);
156 APIRET rc = SafeKernStrFromUcs(NULL, pszDst, RTUtf16ToUpper(pwszSrc), CCHMAXPATHCOMP, cwcSrc);
157 if (rc == NO_ERROR)
158 {
159 size_t cchDst = strlen(pszDst);
160 *pbDst++ = (uint8_t)cchDst;
161 pbDst += cchDst;
162 *pbDst++ = '\0';
163 return pbDst;
164 }
165 LogRel(("vboxSfOs2CopyUtf16NameAndUpperCase: SafeKernStrFromUcs failed: %#x\n", rc));
166 return NULL;
167}
168
169
170
171/**
172 * Worker for FS32_FINDFIRST, FS32_FINDNEXT and FS32_FINDFROMNAME.
173 *
174 * @returns OS/2 status code.
175 * @param pFolder The folder we're working on.
176 * @param pFsFsd The search handle data.
177 * @param pDataBuf The search data buffer (some handle data there too).
178 * @param uLevel The info level to return.
179 * @param fFlags Position flag.
180 * @param pbData The output buffer.
181 * @param cbData The size of the output buffer.
182 * @param cMaxMatches The maximum number of matches to return.
183 * @param pcMatches Where to set the number of returned matches.
184 */
185static APIRET vboxSfOs2ReadDirEntries(PVBOXSFFOLDER pFolder, PVBOXSFFS pFsFsd, PVBOXSFFSBUF pDataBuf, ULONG uLevel, ULONG fFlags,
186 PBYTE pbData, ULONG cbData, USHORT cMaxMatches, PUSHORT pcMatches)
187{
188 APIRET rc = NO_ERROR;
189
190 /*
191 * If we're doing EAs, the buffer starts with an EAOP structure.
192 */
193 EAOP EaOp;
194 PEAOP pEaOpUser = NULL; /* Shut up gcc */
195 switch (uLevel)
196 {
197 case FI_LVL_EAS_FROM_LIST:
198 case FI_LVL_EAS_FROM_LIST_64:
199 case FI_LVL_EAS_FULL:
200 case FI_LVL_EAS_FULL_5:
201 case FI_LVL_EAS_FULL_8:
202 if (cbData >= sizeof(EaOp))
203 {
204 rc = KernCopyIn(&EaOp, pbData, sizeof(EaOp));
205 if (rc == NO_ERROR)
206 {
207 EaOp.fpGEAList = (PGEALIST)KernSelToFlat((uintptr_t)EaOp.fpGEAList);
208 EaOp.fpFEAList = NULL;
209
210 pEaOpUser = (PEAOP)pbData;
211 pbData += sizeof(*pEaOpUser);
212 cbData -= sizeof(*pEaOpUser);
213 break;
214 }
215 }
216 else
217 rc = ERROR_BUFFER_OVERFLOW;
218 Log(("vboxSfOs2ReadDirEntries: Failed to read EAOP: %u\n", rc));
219 return rc;
220 }
221
222 /*
223 * Do the reading.
224 */
225 USHORT cMatches;
226 for (cMatches = 0; cMatches < cMaxMatches;)
227 {
228 /*
229 * Do we need to fetch more directory entries?
230 */
231 PSHFLDIRINFO pEntry = pDataBuf->pEntry;
232 if ( pDataBuf->cEntriesLeft == 0
233 || pEntry == NULL /* paranoia */)
234 {
235 pDataBuf->pEntry = pEntry = pDataBuf->pBuf;
236 int vrc = VbglR0SfHostReqListDir(pFolder->idHostRoot, &pDataBuf->Req, pFsFsd->hHostDir, pDataBuf->pFilter,
237 /*cMaxMatches == 1 ? SHFL_LIST_RETURN_ONE :*/ 0, pDataBuf->pBuf, pDataBuf->cbBuf);
238 if (RT_SUCCESS(vrc))
239 {
240 pDataBuf->cEntriesLeft = pDataBuf->Req.Parms.c32Entries.u.value32;
241 pDataBuf->cbValid = pDataBuf->Req.Parms.cb32Buffer.u.value32;
242 //Log(("%.*Rhxd\n", pDataBuf->cbValid, pEntry));
243 AssertReturn(pDataBuf->cbValid >= RT_UOFFSETOF(SHFLDIRINFO, name.String), ERROR_SYS_INTERNAL);
244 AssertReturn(pDataBuf->cbValid >= RT_UOFFSETOF(SHFLDIRINFO, name.String) + pEntry->name.u16Size, ERROR_SYS_INTERNAL);
245 Log4(("vboxSfOs2ReadDirEntries: VbglR0SfHostReqListDir returned %#x matches in %#x bytes\n", pDataBuf->cEntriesLeft, pDataBuf->cbValid));
246 }
247 else
248 {
249 if (vrc == VERR_NO_MORE_FILES)
250 Log4(("vboxSfOs2ReadDirEntries: VbglR0SfHostReqListDir returned VERR_NO_MORE_FILES (%d,%d)\n",
251 pDataBuf->Req.Parms.c32Entries.u.value32, pDataBuf->Req.Parms.cb32Buffer.u.value32));
252 else
253 Log(("vboxSfOs2ReadDirEntries: VbglR0SfHostReqListDir failed %Rrc (%d,%d)\n", vrc,
254 pDataBuf->Req.Parms.c32Entries.u.value32, pDataBuf->Req.Parms.cb32Buffer.u.value32));
255 pDataBuf->pEntry = NULL;
256 pDataBuf->cEntriesLeft = 0;
257 pDataBuf->cbValid = 0;
258 if (cMatches == 0)
259 {
260 if (vrc == VERR_NO_MORE_FILES)
261 rc = ERROR_NO_MORE_FILES;
262 else
263 rc = vboxSfOs2ConvertStatusToOs2(vrc, ERROR_GEN_FAILURE);
264 }
265 break;
266 }
267 }
268
269 /*
270 * Do matching and stuff the return buffer.
271 */
272 if ( !((pEntry->Info.Attr.fMode >> RTFS_DOS_SHIFT) & pDataBuf->fExcludedAttribs)
273 && ((pEntry->Info.Attr.fMode >> RTFS_DOS_SHIFT) & pDataBuf->fMustHaveAttribs) == pDataBuf->fMustHaveAttribs
274 && ( pDataBuf->fLongFilenames
275 || pEntry->cucShortName
276 || vboxSfOs2IsUtf16Name8dot3(pEntry->name.String.utf16, pEntry->name.u16Length / sizeof(RTUTF16),
277 (char *)pDataBuf->abStaging, sizeof(pDataBuf->abStaging))))
278 {
279 /*
280 * We stages all but FEAs (level 3, 4, 13 and 14).
281 */
282 PBYTE const pbUserBufStart = pbData; /* In case we need to skip a bad name. */
283 uint8_t *pbToCopy = pDataBuf->abStaging;
284 uint8_t *pbDst = pbToCopy;
285
286 /* Position (originally used for FS32_FINDFROMNAME 'position', but since reused
287 for FILEFINDBUF3::oNextEntryOffset and FILEFINDBUF4::oNextEntryOffset): */
288 if (fFlags & FF_GETPOS)
289 {
290 *(uint32_t *)pbDst = pFsFsd->offLastFile + 1;
291 pbDst += sizeof(uint32_t);
292 }
293
294 /* Dates: Creation, Access, Write */
295 vboxSfOs2DateTimeFromTimeSpec((FDATE *)pbDst, (FTIME *)(pbDst + 2), pEntry->Info.BirthTime, pDataBuf->cMinLocalTimeDelta);
296 pbDst += sizeof(FDATE) + sizeof(FTIME);
297 vboxSfOs2DateTimeFromTimeSpec((FDATE *)pbDst, (FTIME *)(pbDst + 2), pEntry->Info.AccessTime, pDataBuf->cMinLocalTimeDelta);
298 pbDst += sizeof(FDATE) + sizeof(FTIME);
299 vboxSfOs2DateTimeFromTimeSpec((FDATE *)pbDst, (FTIME *)(pbDst + 2), pEntry->Info.ModificationTime, pDataBuf->cMinLocalTimeDelta);
300 pbDst += sizeof(FDATE) + sizeof(FTIME);
301
302 /* File size, allocation size, attributes: */
303 if (uLevel >= FI_LVL_STANDARD_64)
304 {
305 *(uint64_t *)pbDst = pEntry->Info.cbObject;
306 pbDst += sizeof(uint64_t);
307 *(uint64_t *)pbDst = pEntry->Info.cbAllocated;
308 pbDst += sizeof(uint64_t);
309 *(uint32_t *)pbDst = (pEntry->Info.Attr.fMode & RTFS_DOS_MASK_OS2) >> RTFS_DOS_SHIFT;
310 pbDst += sizeof(uint32_t);
311 }
312 else
313 {
314 *(uint32_t *)pbDst = (uint32_t)RT_MIN(pEntry->Info.cbObject, _2G - 1);
315 pbDst += sizeof(uint32_t);
316 *(uint32_t *)pbDst = (uint32_t)RT_MIN(pEntry->Info.cbAllocated, _2G - 1);
317 pbDst += sizeof(uint32_t);
318 *(uint16_t *)pbDst = (uint16_t)((pEntry->Info.Attr.fMode & RTFS_DOS_MASK_OS2) >> RTFS_DOS_SHIFT);
319 pbDst += sizeof(uint16_t); /* (Curious: Who is expanding this to 32-bits for 32-bit callers? */
320 }
321
322 /* Extra EA related fields: */
323 if ( uLevel == FI_LVL_STANDARD
324 || uLevel == FI_LVL_STANDARD_64)
325 { /* nothing */ }
326 else if ( uLevel == FI_LVL_STANDARD_EASIZE
327 || uLevel == FI_LVL_STANDARD_EASIZE_64)
328 {
329 /* EA size: */
330 *(uint32_t *)pbDst = 0;
331 pbDst += sizeof(uint32_t);
332 }
333 else
334 {
335 /* Empty FEALIST - flush pending data first: */
336 uint32_t cbToCopy = pbDst - pbToCopy;
337 if (cbToCopy < cbData)
338 {
339 rc = KernCopyOut(pbData, pbToCopy, cbToCopy);
340 if (rc == NO_ERROR)
341 {
342 pbData += cbToCopy;
343 cbData -= cbToCopy;
344 pbDst = pbToCopy;
345
346 /* Output empty EA list. We don't try anticipate filename output length here,
347 instead we'll just handle that when we come to it below. */
348 /** @todo If this overflows, JFS will return ERROR_EAS_DIDNT_FIT and just the
349 * EA size here (i.e. as if FI_LVL_STANDARD_EASIZE or _64 was requested).
350 * I think, however, that ERROR_EAS_DIDNT_FIT should only be considered if
351 * this is the first entry we're returning and we'll have to stop after it. */
352 uint32_t cbWritten = 0;
353 EaOp.fpFEAList = (PFEALIST)pbData;
354 rc = vboxSfOs2MakeEmptyEaListEx(&EaOp, uLevel, cbData, &cbWritten, &pEaOpUser->oError);
355 if (rc == NO_ERROR)
356 {
357 cbData -= cbWritten;
358 pbData += cbWritten;
359 }
360 }
361 }
362 else
363 rc = ERROR_BUFFER_OVERFLOW;
364 if (rc != NO_ERROR)
365 break;
366 }
367
368 /* The length prefixed filename. */
369 if (pDataBuf->fLongFilenames)
370 pbDst = vboxSfOs2CopyUtf16Name(pbDst, pEntry->name.String.utf16, pEntry->name.u16Length / sizeof(RTUTF16));
371 else if (pEntry->cucShortName == 0)
372 pbDst = vboxSfOs2CopyUtf16NameAndUpperCase(pbDst, pEntry->name.String.utf16, pEntry->name.u16Length / sizeof(RTUTF16));
373 else
374 pbDst = vboxSfOs2CopyUtf16NameAndUpperCase(pbDst, pEntry->uszShortName, pEntry->cucShortName);
375 if (pbDst)
376 {
377 /*
378 * Copy out the staged data.
379 */
380 uint32_t cbToCopy = pbDst - pbToCopy;
381 if (cbToCopy <= cbData)
382 {
383 rc = KernCopyOut(pbData, pbToCopy, cbToCopy);
384 if (rc == NO_ERROR)
385 {
386 Log4(("vboxSfOs2ReadDirEntries: match #%u LB %#x: '%s'\n", cMatches, cbToCopy, pEntry->name.String.utf8));
387 Log4(("%.*Rhxd\n", cbToCopy, pbToCopy));
388
389 pbData += cbToCopy;
390 cbData -= cbToCopy;
391 pbDst = pbToCopy;
392
393 cMatches++;
394 pFsFsd->offLastFile++;
395 }
396 else
397 break;
398 }
399 else
400 {
401 rc = ERROR_BUFFER_OVERFLOW;
402 break;
403 }
404 }
405 else
406 {
407 /* Name conversion issue, just skip the entry. */
408 Log3(("vboxSfOs2ReadDirEntries: Skipping '%s' due to name conversion issue.\n", pEntry->name.String.utf8));
409 cbData -= pbUserBufStart - pbData;
410 pbData = pbUserBufStart;
411 }
412 }
413 else
414 Log3(("vboxSfOs2ReadDirEntries: fMode=%#x filter out by %#x/%#x; '%s'\n",
415 pEntry->Info.Attr.fMode, pDataBuf->fMustHaveAttribs, pDataBuf->fExcludedAttribs, pEntry->name.String.utf8));
416
417 /*
418 * Advance to the next directory entry from the host.
419 */
420 if (pDataBuf->cEntriesLeft-- > 1)
421 {
422 pDataBuf->pEntry = pEntry = (PSHFLDIRINFO)&pEntry->name.String.utf8[pEntry->name.u16Size];
423 uintptr_t offEntry = (uintptr_t)pEntry - (uintptr_t)pDataBuf->pBuf;
424 AssertMsgReturn(offEntry + RT_UOFFSETOF(SHFLDIRINFO, name.String) <= pDataBuf->cbValid,
425 ("offEntry=%#x cbValid=%#x\n", offEntry, pDataBuf->cbValid), ERROR_SYS_INTERNAL);
426 //Log(("next entry: %p / %#x: u16Size=%#x => size: %#x\n", pEntry, offEntry, RT_UOFFSETOF(SHFLDIRINFO, name.String) + pEntry->name.u16Size));
427 AssertMsgReturn(offEntry + RT_UOFFSETOF(SHFLDIRINFO, name.String) + pEntry->name.u16Size <= pDataBuf->cbValid,
428 ("offEntry=%#x + offName=%#x + cbName=%#x => %#x; cbValid=%#x\n",
429 offEntry, RT_UOFFSETOF(SHFLDIRINFO, name.String), pEntry->name.u16Size,
430 offEntry + RT_UOFFSETOF(SHFLDIRINFO, name.String) + pEntry->name.u16Size, pDataBuf->cbValid),
431 ERROR_SYS_INTERNAL);
432 //Log(("%.*Rhxd\n", RT_UOFFSETOF(SHFLDIRINFO, name.String) + pEntry->name.u16Size, pEntry));
433 }
434 else
435 pDataBuf->pEntry = pEntry = NULL;
436 }
437
438 *pcMatches = cMatches;
439
440 /* Ignore buffer overflows if we've got matches to return. */
441 if (rc == ERROR_BUFFER_OVERFLOW && cMatches > 0)
442 rc = NO_ERROR;
443 return rc;
444}
445
446
447DECLASM(APIRET)
448FS32_FINDFIRST(PCDFSI pCdFsi, PVBOXSFCD pCdFsd, PCSZ pszPath, LONG offCurDirEnd, ULONG fAttribs,
449 PFSFSI pFsFsi, PVBOXSFFS pFsFsd, PBYTE pbData, ULONG cbData, PUSHORT pcMatches, ULONG uLevel, ULONG fFlags)
450{
451 LogFlow(("FS32_FINDFIRST: pCdFsi=%p pCdFsd=%p pszPath=%p:{%s} offCurDirEnd=%d fAttribs=%#x pFsFsi=%p pFsFsd=%p pbData=%p cbData=%#x pcMatches=%p:{%#x} uLevel=%#x fFlags=%#x\n",
452 pCdFsi, pCdFsd, pszPath, pszPath, offCurDirEnd, fAttribs, pFsFsi, pFsFsd, pbData, cbData, pcMatches, *pcMatches, uLevel, fFlags));
453 USHORT const cMaxMatches = *pcMatches;
454 *pcMatches = 0;
455
456 /*
457 * Input validation.
458 */
459 switch (uLevel)
460 {
461 case FI_LVL_STANDARD:
462 case FI_LVL_STANDARD_64:
463 case FI_LVL_STANDARD_EASIZE:
464 case FI_LVL_STANDARD_EASIZE_64:
465 break;
466
467 case FI_LVL_EAS_FROM_LIST:
468 case FI_LVL_EAS_FROM_LIST_64:
469 if (cbData < sizeof(EAOP))
470 {
471 Log(("FS32_FINDFIRST: Buffer smaller than EAOP: %#x\n", cbData));
472 return ERROR_BUFFER_OVERFLOW;
473 }
474 break;
475
476 default:
477 LogRel(("FS32_FINDFIRST: Unsupported info level %u!\n", uLevel));
478 return ERROR_INVALID_LEVEL;
479 }
480
481 /*
482 * Resolve path to a folder and folder relative path.
483 */
484 RT_NOREF(pCdFsi);
485 PVBOXSFFOLDER pFolder;
486 VBOXSFCREATEREQ *pReq;
487 APIRET rc = vboxSfOs2ResolvePathEx(pszPath, pCdFsd, offCurDirEnd, RT_UOFFSETOF(VBOXSFCREATEREQ, StrPath),
488 &pFolder, (void **)&pReq);
489 LogFlow(("FS32_FINDFIRST: vboxSfOs2ResolvePathEx: -> %u pReq=%p\n", rc, pReq));
490 if (rc == NO_ERROR)
491 {
492 PSHFLSTRING pStrFolderPath = &pReq->StrPath;
493
494 /*
495 * Look for a wildcard filter at the end of the path, saving it all for
496 * later in NT filter speak if present.
497 */
498 PSHFLSTRING pFilter = NULL;
499 PRTUTF16 pwszFilter = RTPathFilenameUtf16(pStrFolderPath->String.utf16);
500 if ( pwszFilter
501 && ( RTUtf16Chr(pwszFilter, '*') != NULL
502 || RTUtf16Chr(pwszFilter, '?') != NULL))
503 {
504 if (RTUtf16CmpAscii(pwszFilter, "*.*") == 0)
505 {
506 /* All files, no filtering needed. Just drop the filter expression from the directory path. */
507 *pwszFilter = '\0';
508 pStrFolderPath->u16Length = (uint16_t)((uint8_t *)pwszFilter - &pStrFolderPath->String.utf8[0]);
509 }
510 else
511 {
512 /* Duplicate the whole path. */
513 pFilter = vboxSfOs2StrDup(pStrFolderPath);
514 if (pFilter)
515 {
516 /* Drop filter from directory path. */
517 *pwszFilter = '\0';
518 pStrFolderPath->u16Length = (uint16_t)((uint8_t *)pwszFilter - &pStrFolderPath->String.utf8[0]);
519
520 /* Convert filter part of the copy to NT speak. */
521 pwszFilter = (PRTUTF16)&pFilter->String.utf8[(uint8_t *)pwszFilter - &pStrFolderPath->String.utf8[0]];
522 for (;;)
523 {
524 RTUTF16 wc = *pwszFilter;
525 if (wc == '?')
526 *pwszFilter = '>'; /* The DOS question mark: Matches one char, but dots and end-of-name eats them. */
527 else if (wc == '.')
528 {
529 RTUTF16 wc2 = pwszFilter[1];
530 if (wc2 == '*' || wc2 == '?')
531 *pwszFilter = '"'; /* The DOS dot: Matches a dot or end-of-name. */
532 }
533 else if (wc == '*')
534 {
535 if (pwszFilter[1] == '.')
536 *pwszFilter = '<'; /* The DOS star: Matches zero or more chars except the DOS dot.*/
537 }
538 else if (wc == '\0')
539 break;
540 pwszFilter++;
541 }
542 }
543 else
544 rc = ERROR_NOT_ENOUGH_MEMORY;
545 }
546 }
547 /*
548 * When no wildcard is specified, we're supposed to return a single entry
549 * with the name in the final component. Exception is the root, where we
550 * always list the whole thing.
551 *
552 * Not sure if we'll ever see a trailing slash here (pszFilter == NULL),
553 * but if we do we should accept it only for the root.
554 */
555 else if (pwszFilter)
556 {
557 /* Copy the whole path for filtering. */
558 pFilter = vboxSfOs2StrDup(pStrFolderPath);
559 if (pFilter)
560 {
561 /* Strip the filename off the one we're opening. */
562 pStrFolderPath->u16Length = (uint16_t)((uintptr_t)pwszFilter - (uintptr_t)pStrFolderPath->String.utf16);
563 pStrFolderPath->u16Size = pStrFolderPath->u16Length + (uint16_t)sizeof(RTUTF16);
564 pStrFolderPath->String.utf16[pStrFolderPath->u16Length / sizeof(RTUTF16)] = '\0';
565 }
566 else
567 rc = ERROR_NOT_ENOUGH_MEMORY;
568 }
569 else if (!pwszFilter && pStrFolderPath->u16Length > 1)
570 {
571 LogFlow(("FS32_FINDFIRST: Trailing slash (%ls)\n", pStrFolderPath->String.utf16));
572 rc = ERROR_PATH_NOT_FOUND;
573 }
574 else
575 LogFlow(("FS32_FINDFIRST: Root dir (%ls)\n", pStrFolderPath->String.utf16));
576
577 /*
578 * Allocate data/request buffer and another buffer for receiving entries in.
579 */
580 if (rc == NO_ERROR)
581 {
582 PVBOXSFFSBUF pDataBuf = (PVBOXSFFSBUF)VbglR0PhysHeapAlloc(sizeof(*pDataBuf));
583 if (pDataBuf)
584 {
585#define MIN_BUF_SIZE ( RT_ALIGN_32(sizeof(SHFLDIRINFO) + CCHMAXPATHCOMP * sizeof(RTUTF16) + 64 /*fudge*/ + ALLOC_HDR_SIZE, 64) \
586 - ALLOC_HDR_SIZE)
587 RT_ZERO(*pDataBuf);
588 pDataBuf->cbBuf = cMaxMatches == 1 ? MIN_BUF_SIZE : _16K - ALLOC_HDR_SIZE;
589 pDataBuf->pBuf = (PSHFLDIRINFO)VbglR0PhysHeapAlloc(pDataBuf->cbBuf);
590 if (pDataBuf->pBuf)
591 { /* likely */ }
592 else
593 {
594 pDataBuf->pBuf = (PSHFLDIRINFO)VbglR0PhysHeapAlloc(MIN_BUF_SIZE);
595 if (pDataBuf->pBuf)
596 pDataBuf->cbBuf = MIN_BUF_SIZE;
597 else
598 rc = ERROR_NOT_ENOUGH_MEMORY;
599 }
600 }
601 if (rc == NO_ERROR)
602 {
603 /*
604 * Now, try open the directory for reading.
605 */
606 pReq->CreateParms.CreateFlags = SHFL_CF_DIRECTORY | SHFL_CF_ACT_FAIL_IF_NEW | SHFL_CF_ACT_OPEN_IF_EXISTS
607 | SHFL_CF_ACCESS_READ | SHFL_CF_ACCESS_ATTR_READ | SHFL_CF_ACCESS_DENYNONE;
608
609 int vrc = VbglR0SfHostReqCreate(pFolder->idHostRoot, pReq);
610 LogFlow(("FS32_FINDFIRST: VbglR0SfHostReqCreate(%ls) -> %Rrc Result=%d fMode=%#x hHandle=%#RX64\n",
611 pStrFolderPath->String.utf16, vrc, pReq->CreateParms.Result, pReq->CreateParms.Info.Attr.fMode,
612 pReq->CreateParms.Handle));
613 if (RT_SUCCESS(vrc))
614 {
615 switch (pReq->CreateParms.Result)
616 {
617 case SHFL_FILE_EXISTS:
618 if (pReq->CreateParms.Handle != SHFL_HANDLE_NIL)
619 {
620 /*
621 * Initialize the structures.
622 */
623 pFsFsd->hHostDir = pReq->CreateParms.Handle;
624 pFsFsd->u32Magic = VBOXSFFS_MAGIC;
625 pFsFsd->pFolder = pFolder;
626 pFsFsd->pBuf = pDataBuf;
627 pFsFsd->offLastFile = 0;
628 pDataBuf->u32Magic = VBOXSFFSBUF_MAGIC;
629 pDataBuf->cbValid = 0;
630 pDataBuf->cEntriesLeft = 0;
631 pDataBuf->pEntry = NULL;
632 pDataBuf->pFilter = pFilter;
633 pDataBuf->fMustHaveAttribs = (uint8_t)( (fAttribs >> 8)
634 & (FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY | FILE_ARCHIVED));
635 pDataBuf->fExcludedAttribs = (uint8_t)(~fAttribs & (FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY));
636 pDataBuf->fLongFilenames = RT_BOOL(fAttribs & FF_ATTR_LONG_FILENAME);
637 LogFlow(("FS32_FINDFIRST: fMustHaveAttribs=%#x fExcludedAttribs=%#x fLongFilenames=%d (fAttribs=%#x)\n",
638 pDataBuf->fMustHaveAttribs, pDataBuf->fExcludedAttribs, pDataBuf->fLongFilenames, fAttribs));
639 pDataBuf->cMinLocalTimeDelta = vboxSfOs2GetLocalTimeDelta();
640
641 rc = vboxSfOs2ReadDirEntries(pFolder, pFsFsd, pDataBuf, uLevel, fFlags,
642 pbData, cbData, cMaxMatches ? cMaxMatches : UINT16_MAX, pcMatches);
643 if (rc == NO_ERROR)
644 {
645 uint32_t cRefs = ASMAtomicIncU32(&pFolder->cOpenSearches);
646 Assert(cRefs < _4K); RT_NOREF(cRefs);
647
648 /* We keep these on success: */
649 if (pFilter == pStrFolderPath)
650 pStrFolderPath = NULL;
651 pFilter = NULL;
652 pDataBuf = NULL;
653 pFolder = NULL;
654 }
655 else
656 {
657 AssertCompile(sizeof(VBOXSFCLOSEREQ) < sizeof(*pReq));
658 vrc = VbglR0SfHostReqClose(pFolder->idHostRoot, (VBOXSFCLOSEREQ *)pReq, pFsFsd->hHostDir);
659 AssertRC(vrc);
660 pFsFsd->u32Magic = ~VBOXSFFS_MAGIC;
661 pDataBuf->u32Magic = ~VBOXSFFSBUF_MAGIC;
662 pFsFsd->pFolder = NULL;
663 pFsFsd->hHostDir = NULL;
664 }
665 }
666 else
667 {
668 LogFlow(("FS32_FINDFIRST: VbglR0SfHostReqCreate returns NIL handle for '%ls'\n",
669 pStrFolderPath->String.utf16));
670 rc = ERROR_PATH_NOT_FOUND;
671 }
672 break;
673
674 case SHFL_PATH_NOT_FOUND:
675 rc = ERROR_PATH_NOT_FOUND;
676 break;
677
678 default:
679 case SHFL_FILE_NOT_FOUND:
680 rc = ERROR_FILE_NOT_FOUND;
681 break;
682 }
683 }
684 else
685 rc = vboxSfOs2ConvertStatusToOs2(vrc, ERROR_GEN_FAILURE);
686 }
687
688 if (pDataBuf)
689 {
690 VbglR0PhysHeapFree(pDataBuf->pBuf);
691 pDataBuf->pBuf = NULL;
692 VbglR0PhysHeapFree(pDataBuf);
693 }
694 }
695 vboxSfOs2StrFree(pFilter);
696 VbglR0PhysHeapFree(pReq);
697 vboxSfOs2ReleaseFolder(pFolder);
698 }
699
700 RT_NOREF_PV(pFsFsi);
701 LogFlow(("FS32_FINDFIRST: returns %u\n", rc));
702 return rc;
703}
704
705
706DECLASM(APIRET)
707FS32_FINDFROMNAME(PFSFSI pFsFsi, PVBOXSFFS pFsFsd, PBYTE pbData, ULONG cbData, PUSHORT pcMatches,
708 ULONG uLevel, ULONG uPosition, PCSZ pszName, ULONG fFlags)
709{
710 LogFlow(("FS32_FINDFROMNAME: pFsFsi=%p pFsFsd=%p pbData=%p cbData=%#x pcMatches=%p:{%#x} uLevel=%#x uPosition=%#x pszName=%p:{%s} fFlags=%#x\n",
711 pFsFsi, pFsFsd, pbData, cbData, pcMatches, *pcMatches, uLevel, uPosition, pszName, pszName, fFlags));
712
713 /*
714 * Input validation.
715 */
716 USHORT const cMaxMatches = *pcMatches;
717 *pcMatches = 0;
718 AssertReturn(pFsFsd->u32Magic == VBOXSFFS_MAGIC, ERROR_SYS_INTERNAL);
719 PVBOXSFFOLDER pFolder = pFsFsd->pFolder;
720 AssertReturn(pFolder != NULL, ERROR_SYS_INTERNAL);
721 Assert(pFolder->u32Magic == VBOXSFFOLDER_MAGIC);
722 Assert(pFolder->cOpenSearches > 0);
723 PVBOXSFFSBUF pDataBuf = pFsFsd->pBuf;
724 AssertReturn(pDataBuf, ERROR_SYS_INTERNAL);
725 Assert(pDataBuf->u32Magic == VBOXSFFSBUF_MAGIC);
726
727 switch (uLevel)
728 {
729 case FI_LVL_STANDARD:
730 case FI_LVL_STANDARD_64:
731 case FI_LVL_STANDARD_EASIZE:
732 case FI_LVL_STANDARD_EASIZE_64:
733 case FI_LVL_EAS_FROM_LIST:
734 case FI_LVL_EAS_FROM_LIST_64:
735 break;
736
737 default:
738 LogRel(("FS32_FINDFROMNAME: Unsupported info level %u!\n", uLevel));
739 return ERROR_INVALID_LEVEL;
740 }
741
742 /*
743 * Check if we're just continuing. This is usually the case.
744 */
745 APIRET rc;
746 if (uPosition == pFsFsd->offLastFile)
747 rc = vboxSfOs2ReadDirEntries(pFolder, pFsFsd, pDataBuf, uLevel, fFlags, pbData, cbData,
748 cMaxMatches ? cMaxMatches : UINT16_MAX, pcMatches);
749 else
750 {
751 Log(("TODO: uPosition differs: %#x, expected %#x (%s)\n", uPosition, pFsFsd->offLastFile, pszName));
752 rc = vboxSfOs2ReadDirEntries(pFolder, pFsFsd, pDataBuf, uLevel, fFlags, pbData, cbData,
753 cMaxMatches ? cMaxMatches : UINT16_MAX, pcMatches);
754 }
755
756 RT_NOREF(pFsFsi, pszName);
757 LogFlow(("FS32_FINDFROMNAME: returns %u (*pcMatches=%#x)\n", rc, *pcMatches));
758 return rc;
759}
760
761
762DECLASM(APIRET)
763FS32_FINDNEXT(PFSFSI pFsFsi, PVBOXSFFS pFsFsd, PBYTE pbData, ULONG cbData, PUSHORT pcMatches, ULONG uLevel, ULONG fFlags)
764{
765 LogFlow(("FS32_FINDNEXT: pFsFsi=%p pFsFsd=%p pbData=%p cbData=%#x pcMatches=%p:{%#x} uLevel=%#x fFlags=%#x\n",
766 pFsFsi, pFsFsd, pbData, cbData, pcMatches, *pcMatches, uLevel, fFlags));
767
768 /*
769 * Input validation.
770 */
771 USHORT const cMaxMatches = *pcMatches;
772 *pcMatches = 0;
773 AssertReturn(pFsFsd->u32Magic == VBOXSFFS_MAGIC, ERROR_SYS_INTERNAL);
774 PVBOXSFFOLDER pFolder = pFsFsd->pFolder;
775 AssertReturn(pFolder != NULL, ERROR_SYS_INTERNAL);
776 Assert(pFolder->u32Magic == VBOXSFFOLDER_MAGIC);
777 Assert(pFolder->cOpenSearches > 0);
778 PVBOXSFFSBUF pDataBuf = pFsFsd->pBuf;
779 AssertReturn(pDataBuf, ERROR_SYS_INTERNAL);
780 Assert(pDataBuf->u32Magic == VBOXSFFSBUF_MAGIC);
781
782 switch (uLevel)
783 {
784 case FI_LVL_STANDARD:
785 case FI_LVL_STANDARD_64:
786 case FI_LVL_STANDARD_EASIZE:
787 case FI_LVL_STANDARD_EASIZE_64:
788 case FI_LVL_EAS_FROM_LIST:
789 case FI_LVL_EAS_FROM_LIST_64:
790 break;
791
792 default:
793 LogRel(("FS32_FINDNEXT: Unsupported info level %u!\n", uLevel));
794 return ERROR_INVALID_LEVEL;
795 }
796
797 /*
798 * Read more.
799 */
800 APIRET rc = vboxSfOs2ReadDirEntries(pFolder, pFsFsd, pDataBuf, uLevel, fFlags, pbData, cbData,
801 cMaxMatches ? cMaxMatches : UINT16_MAX, pcMatches);
802
803 NOREF(pFsFsi);
804 LogFlow(("FS32_FINDNEXT: returns %u (*pcMatches=%#x)\n", rc, *pcMatches));
805 return rc;
806}
807
808
809DECLASM(APIRET)
810FS32_FINDCLOSE(PFSFSI pFsFsi, PVBOXSFFS pFsFsd)
811{
812 /*
813 * Input validation.
814 */
815 AssertReturn(pFsFsd->u32Magic == VBOXSFFS_MAGIC, ERROR_SYS_INTERNAL);
816 PVBOXSFFOLDER pFolder = pFsFsd->pFolder;
817 AssertReturn(pFolder != NULL, ERROR_SYS_INTERNAL);
818 Assert(pFolder->u32Magic == VBOXSFFOLDER_MAGIC);
819 Assert(pFolder->cOpenSearches > 0);
820 PVBOXSFFSBUF pDataBuf = pFsFsd->pBuf;
821 AssertReturn(pDataBuf, ERROR_SYS_INTERNAL);
822 Assert(pDataBuf->u32Magic == VBOXSFFSBUF_MAGIC);
823
824 /*
825 * Close it.
826 */
827 if (pFsFsd->hHostDir != SHFL_HANDLE_NIL)
828 {
829 int vrc = VbglR0SfHostReqCloseSimple(pFolder->idHostRoot, pFsFsd->hHostDir);
830 AssertRC(vrc);
831 }
832
833 pFsFsd->u32Magic = ~VBOXSFFS_MAGIC;
834 pFsFsd->hHostDir = SHFL_HANDLE_NIL;
835 pFsFsd->pFolder = NULL;
836 pFsFsd->pBuf = NULL;
837 vboxSfOs2StrFree(pDataBuf->pFilter);
838 pDataBuf->pFilter = NULL;
839 pDataBuf->u32Magic = ~VBOXSFFSBUF_MAGIC;
840 pDataBuf->cbBuf = 0;
841
842 VbglR0PhysHeapFree(pDataBuf->pBuf);
843 pDataBuf->pBuf = NULL;
844 VbglR0PhysHeapFree(pDataBuf);
845
846 uint32_t cRefs = ASMAtomicDecU32(&pFolder->cOpenSearches);
847 Assert(cRefs < _4K); RT_NOREF(cRefs);
848 vboxSfOs2ReleaseFolder(pFolder);
849
850 RT_NOREF(pFsFsi);
851 LogFlow(("FS32_FINDCLOSE: returns NO_ERROR\n"));
852 return NO_ERROR;
853}
854
855
856
857
858
859DECLASM(APIRET)
860FS32_FINDNOTIFYFIRST(PCDFSI pCdFsi, PVBOXSFCD pCdFsd, PCSZ pszPath, LONG offCurDirEnd, ULONG fAttribs,
861 PUSHORT phHandle, PBYTE pbData, ULONG cbData, PUSHORT pcMatches,
862 ULONG uLevel, ULONG fFlags)
863{
864 RT_NOREF(pCdFsi, pCdFsd, pszPath, offCurDirEnd, fAttribs, phHandle, pbData, cbData, pcMatches, uLevel, fFlags);
865 return ERROR_NOT_SUPPORTED;
866}
867
868
869DECLASM(APIRET)
870FS32_FINDNOTIFYNEXT(ULONG hHandle, PBYTE pbData, ULONG cbData, PUSHORT pcMatchs, ULONG uLevel, ULONG cMsTimeout)
871{
872 RT_NOREF(hHandle, pbData, cbData, pcMatchs, uLevel, cMsTimeout);
873 return ERROR_NOT_SUPPORTED;
874}
875
876
877DECLASM(APIRET)
878FS32_FINDNOTIFYCLOSE(ULONG hHandle)
879{
880 NOREF(hHandle);
881 return ERROR_NOT_SUPPORTED;
882}
883
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