VirtualBox

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

Last change on this file since 85704 was 79112, checked in by vboxsync, 6 years ago

os2/VBoxSF: More EA fun, now for CMD.EXE opening files for copying. Ignore empty EAOPs and non-creation/replace opens.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 88.1 KB
Line 
1/** $Id: VBoxSF.cpp 79112 2019-06-13 03:33:29Z vboxsync $ */
2/** @file
3 * VBoxSF - OS/2 Shared Folders, the FS and FSD level IFS EPs
4 */
5
6/*
7 * Copyright (c) 2007 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/assert.h>
40#include <iprt/ctype.h>
41#include <iprt/err.h>
42#include <iprt/mem.h>
43#include <iprt/path.h>
44
45#include <iprt/asm.h>
46#include <iprt/asm-amd64-x86.h>
47
48
49/*********************************************************************************************************************************
50* Defined Constants And Macros *
51*********************************************************************************************************************************/
52/** Max folder name length, including terminator.
53 * Easier to deal with stack buffers if we put a reasonable limit on the. */
54#define VBOXSFOS2_MAX_FOLDER_NAME 64
55
56
57/*********************************************************************************************************************************
58* Global Variables *
59*********************************************************************************************************************************/
60/** VMMDEV_HVF_XXX (set during init). */
61uint32_t g_fHostFeatures = 0;
62/** The shared mutex protecting folders list, drives and the connection. */
63MutexLock_t g_MtxFolders;
64/** The shared folder service client structure. */
65VBGLSFCLIENT g_SfClient;
66/** Set if g_SfClient is valid, clear if not. */
67bool g_fIsConnectedToService = false;
68/** List of active folder (PVBOXSFFOLDER). */
69RTLISTANCHOR g_FolderHead;
70/** This is incremented everytime g_FolderHead is modified. */
71uint32_t volatile g_uFolderRevision;
72/** Folders mapped on drive letters. Pointers include a reference. */
73PVBOXSFFOLDER g_apDriveFolders[26];
74
75
76
77/**
78 * Generic IPRT -> OS/2 status code converter.
79 *
80 * @returns OS/2 status code.
81 * @param vrc IPRT/VBox status code.
82 * @param rcDefault The OS/2 status code to return when there
83 * is no translation.
84 */
85APIRET vboxSfOs2ConvertStatusToOs2(int vrc, APIRET rcDefault)
86{
87 switch (vrc)
88 {
89 default: return rcDefault;
90
91 case VERR_FILE_NOT_FOUND: return ERROR_FILE_NOT_FOUND;
92 case VERR_PATH_NOT_FOUND: return ERROR_PATH_NOT_FOUND;
93 case VERR_SHARING_VIOLATION: return ERROR_SHARING_VIOLATION;
94 case VERR_ACCESS_DENIED: return ERROR_ACCESS_DENIED;
95 case VERR_ALREADY_EXISTS: return ERROR_ACCESS_DENIED;
96 case VERR_WRITE_PROTECT: return ERROR_WRITE_PROTECT;
97 case VERR_IS_A_DIRECTORY: return ERROR_DIRECTORY;
98 case VERR_DISK_FULL: return ERROR_DISK_FULL;
99 case VINF_SUCCESS: return NO_ERROR;
100 }
101}
102
103
104/**
105 * Gets the delta for the local timezone, in minutes.
106 *
107 * We need to do this once for each API call rather than over and over again for
108 * each date/time conversion, so as not to create an update race.
109 *
110 * @returns Delta in minutes. Current thinking is that positive means timezone
111 * is west of UTC, while negative is east of it.
112 */
113int16_t vboxSfOs2GetLocalTimeDelta(void)
114{
115 GINFOSEG volatile *pGis = (GINFOSEG volatile *)&KernSISData;
116 if (pGis)
117 {
118 uint16_t cDelta = pGis->timezone;
119 if (cDelta != 0 && cDelta != 0xffff)
120 return (int16_t)cDelta;
121 }
122 return 0;
123}
124
125
126/**
127 * Helper for converting from IPRT timespec format to OS/2 DATE/TIME.
128 *
129 * @param pDosDate The output DOS date.
130 * @param pDosTime The output DOS time.
131 * @param SrcTimeSpec The IPRT input timestamp.
132 * @param cMinLocalTimeDelta The timezone delta in minutes.
133 */
134void vboxSfOs2DateTimeFromTimeSpec(FDATE *pDosDate, FTIME *pDosTime, RTTIMESPEC SrcTimeSpec, int16_t cMinLocalTimeDelta)
135{
136 if (cMinLocalTimeDelta != 0)
137 RTTimeSpecAddSeconds(&SrcTimeSpec, -cMinLocalTimeDelta * 60);
138
139 RTTIME Time;
140 if ( RTTimeSpecGetNano(&SrcTimeSpec) >= RTTIME_OFFSET_DOS_TIME
141 && RTTimeExplode(&Time, &SrcTimeSpec))
142 {
143 pDosDate->year = Time.i32Year - 1980;
144 pDosDate->month = Time.u8Month;
145 pDosDate->day = Time.u8MonthDay;
146 pDosTime->hours = Time.u8Hour;
147 pDosTime->minutes = Time.u8Minute;
148 pDosTime->twosecs = Time.u8Second / 2;
149 }
150 else
151 {
152 pDosDate->year = 0;
153 pDosDate->month = 1;
154 pDosDate->day = 1;
155 pDosTime->hours = 0;
156 pDosTime->minutes = 0;
157 pDosTime->twosecs = 0;
158 }
159}
160
161
162/**
163 * Helper for converting from OS/2 DATE/TIME to IPRT timespec format.
164 *
165 * @returns pDstTimeSpec on success, NULL if invalid input.
166 * @param DosDate The input DOS date.
167 * @param DosTime The input DOS time.
168 * @param cMinLocalTimeDelta The timezone delta in minutes.
169 * @param pDstTimeSpec The IPRT output timestamp.
170 */
171PRTTIMESPEC vboxSfOs2DateTimeToTimeSpec(FDATE DosDate, FTIME DosTime, int16_t cMinLocalTimeDelta, PRTTIMESPEC pDstTimeSpec)
172{
173 RTTIME Time;
174 Time.i32Year = DosDate.year + 1980;
175 Time.u8Month = DosDate.month;
176 Time.u8WeekDay = UINT8_MAX;
177 Time.u16YearDay = 0;
178 Time.u8MonthDay = DosDate.day;
179 Time.u8Hour = DosTime.hours;
180 Time.u8Minute = DosTime.minutes;
181 Time.u8Second = DosTime.twosecs * 2;
182 Time.u32Nanosecond = 0;
183 Time.fFlags = RTTIME_FLAGS_TYPE_LOCAL;
184 Time.offUTC = -cMinLocalTimeDelta;
185 if (RTTimeLocalNormalize(&Time))
186 return RTTimeImplode(pDstTimeSpec, &Time);
187 return NULL;
188}
189
190
191/*********************************************************************************************************************************
192* Shared Folder String Buffer Management *
193*********************************************************************************************************************************/
194
195/**
196 * Allocates a SHFLSTRING buffer (UTF-16).
197 *
198 * @returns Pointer to a SHFLSTRING buffer, NULL if out of memory.
199 * @param cwcLength The desired string buffer length in UTF-16 units
200 * (excluding terminator).
201 */
202PSHFLSTRING vboxSfOs2StrAlloc(size_t cwcLength)
203{
204 AssertReturn(cwcLength <= 0x1000, NULL);
205 uint16_t cb = (uint16_t)cwcLength + 1;
206 cb *= sizeof(RTUTF16);
207
208 PSHFLSTRING pStr = (PSHFLSTRING)VbglR0PhysHeapAlloc(SHFLSTRING_HEADER_SIZE + cb);
209 if (pStr)
210 {
211 pStr->u16Size = cb;
212 pStr->u16Length = 0;
213 pStr->String.utf16[0] = '\0';
214 return pStr;
215 }
216 return NULL;
217}
218
219
220/**
221 * Duplicates a shared folders string buffer (UTF-16).
222 *
223 * @returns Pointer to a SHFLSTRING buffer containing the copy.
224 * NULL if out of memory or the string is too long.
225 * @param pSrc The string to clone.
226 */
227PSHFLSTRING vboxSfOs2StrDup(PCSHFLSTRING pSrc)
228{
229 PSHFLSTRING pDst = (PSHFLSTRING)VbglR0PhysHeapAlloc(SHFLSTRING_HEADER_SIZE + pSrc->u16Length + sizeof(RTUTF16));
230 if (pDst)
231 {
232 pDst->u16Size = pSrc->u16Length + (uint16_t)sizeof(RTUTF16);
233 pDst->u16Length = pSrc->u16Length;
234 memcpy(&pDst->String, &pSrc->String, pSrc->u16Length);
235 pDst->String.utf16[pSrc->u16Length / sizeof(RTUTF16)] = '\0';
236 return pDst;
237 }
238 return NULL;
239}
240
241
242/**
243 * Frees a SHLFSTRING buffer.
244 *
245 * @param pStr The buffer to free.
246 */
247void vboxSfOs2StrFree(PSHFLSTRING pStr)
248{
249 if (pStr)
250 VbglR0PhysHeapFree(pStr);
251}
252
253
254
255/*********************************************************************************************************************************
256* Folders, Paths and Service Connection. *
257*********************************************************************************************************************************/
258
259/**
260 * Ensures that we're connected to the host service.
261 *
262 * @returns VBox status code.
263 * @remarks Caller owns g_MtxFolder exclusively!
264 */
265static int vboxSfOs2EnsureConnected(void)
266{
267 if (g_fIsConnectedToService)
268 return VINF_SUCCESS;
269
270 int rc = VbglR0SfConnect(&g_SfClient);
271 if (RT_SUCCESS(rc))
272 g_fIsConnectedToService = true;
273 else
274 LogRel(("VbglR0SfConnect failed: %Rrc\n", rc));
275 return rc;
276}
277
278
279/**
280 * Destroys a folder when the reference count has reached zero.
281 *
282 * @param pFolder The folder to destroy.
283 */
284static void vboxSfOs2DestroyFolder(PVBOXSFFOLDER pFolder)
285{
286 /* Note! We won't get there while the folder is on the list. */
287 LogRel(("vboxSfOs2ReleaseFolder: Destroying %p [%s]\n", pFolder, pFolder->szName));
288 VbglR0SfHostReqUnmapFolderSimple(pFolder->idHostRoot);
289 RT_ZERO(pFolder);
290 RTMemFree(pFolder);
291}
292
293
294/**
295 * Releases a reference to a folder.
296 *
297 * @param pFolder The folder to release.
298 */
299void vboxSfOs2ReleaseFolder(PVBOXSFFOLDER pFolder)
300{
301 if (pFolder)
302 {
303 uint32_t cRefs = ASMAtomicDecU32(&pFolder->cRefs);
304 AssertMsg(cRefs < _64K, ("%#x\n", cRefs));
305 if (!cRefs)
306 vboxSfOs2DestroyFolder(pFolder);
307 }
308}
309
310
311/**
312 * Retain a reference to a folder.
313 *
314 * @param pFolder The folder to release.
315 */
316void vboxSfOs2RetainFolder(PVBOXSFFOLDER pFolder)
317{
318 uint32_t cRefs = ASMAtomicIncU32(&pFolder->cRefs);
319 AssertMsg(cRefs < _64K, ("%#x\n", cRefs));
320}
321
322
323/**
324 * Locates and retains a folder structure.
325 *
326 * @returns Folder matching the name, NULL of not found.
327 * @remarks Caller owns g_MtxFolder.
328 */
329static PVBOXSFFOLDER vboxSfOs2FindAndRetainFolder(const char *pachName, size_t cchName)
330{
331 PVBOXSFFOLDER pCur;
332 RTListForEach(&g_FolderHead, pCur, VBOXSFFOLDER, ListEntry)
333 {
334 if ( pCur->cchName == cchName
335 && RTStrNICmpAscii(pCur->szName, pachName, cchName) == 0)
336 {
337 uint32_t cRefs = ASMAtomicIncU32(&pCur->cRefs);
338 AssertMsg(cRefs < _64K, ("%#x\n", cRefs));
339 return pCur;
340 }
341 }
342 return NULL;
343}
344
345
346/**
347 * Maps a folder, linking it into the list of folders.
348 *
349 * One reference is retained for the caller, which must pass it on or release
350 * it. The list also have a reference to it.
351 *
352 * @returns VBox status code.
353 * @param pName The name of the folder to map - ASCII (not UTF-16!).
354 * Must be large enough to hold UTF-16 expansion of the
355 * string, will do so upon return.
356 * @param pszTag Folder tag (for the VBoxService automounter). Optional.
357 * @param ppFolder Where to return the folder structure on success.
358 *
359 * @remarks Caller owns g_MtxFolder exclusively!
360 */
361static int vboxSfOs2MapFolder(PSHFLSTRING pName, const char *pszTag, PVBOXSFFOLDER *ppFolder)
362{
363 int rc;
364 size_t const cbTag = pszTag ? strlen(pszTag) + 1 : NULL;
365 PVBOXSFFOLDER pNew = (PVBOXSFFOLDER)RTMemAlloc(RT_UOFFSETOF_DYN(VBOXSFFOLDER, szName[pName->u16Length + 1 + cbTag]));
366 if (pNew != NULL)
367 {
368 pNew->u32Magic = VBOXSFFOLDER_MAGIC;
369 pNew->cRefs = 2; /* (List reference + the returned reference.) */
370 pNew->cOpenFiles = 0;
371 pNew->cOpenSearches = 0;
372 pNew->cDrives = 0;
373 pNew->idHostRoot = SHFL_ROOT_NIL;
374 pNew->hVpb = 0;
375 pNew->cbNameAndTag = pName->u16Length + (uint16_t)cbTag;
376 pNew->cchName = (uint8_t)pName->u16Length;
377 pNew->cchName = (uint8_t)pName->u16Length;
378 memcpy(pNew->szName, pName->String.utf8, pName->u16Length);
379 pNew->szName[pName->u16Length] = '\0';
380 if (cbTag)
381 memcpy(&pNew->szName[pName->u16Length + 1], pszTag, cbTag);
382
383 /* Expand the folder name to UTF-16. */
384 uint8_t off = pNew->cchName;
385 uint8_t volatile const *pbSrc = &pName->String.utf8[0];
386 RTUTF16 volatile *pwcDst = &pName->String.utf16[0];
387 do
388 pwcDst[off] = pbSrc[off];
389 while (off-- > 0);
390 pName->u16Length *= sizeof(RTUTF16);
391 Assert(pName->u16Length + sizeof(RTUTF16) <= pName->u16Size);
392
393 /* Try do the mapping.*/
394 VBOXSFMAPFOLDERWITHBUFREQ *pReq = (VBOXSFMAPFOLDERWITHBUFREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
395 if (pReq)
396 {
397 rc = VbglR0SfHostReqMapFolderWithBuf(pReq, pName, RTPATH_DELIMITER, false /*fCaseSensitive*/);
398 if (RT_SUCCESS(rc))
399 {
400 pNew->idHostRoot = pReq->Parms.id32Root.u.value32;
401
402 RTListAppend(&g_FolderHead, &pNew->ListEntry);
403 ASMAtomicIncU32(&g_uFolderRevision);
404 LogRel(("vboxSfOs2MapFolder: %p - %s\n", pNew, pNew->szName));
405
406 *ppFolder = pNew;
407 pNew = NULL;
408 }
409 else
410 LogRel(("vboxSfOs2MapFolder: VbglR0SfHostReqMapFolderWithBuf(,%s,) -> %Rrc\n", pNew->szName, rc));
411 VbglR0PhysHeapFree(pReq);
412 }
413 else
414 LogRel(("vboxSfOs2MapFolder: Out of physical heap :-(\n"));
415 RTMemFree(pNew);
416 }
417 else
418 {
419 LogRel(("vboxSfOs2MapFolder: Out of memory :-(\n"));
420 rc = VERR_NO_MEMORY;
421 }
422 return rc;
423}
424
425
426/**
427 * Worker for vboxSfOs2UncPrefixLength.
428 */
429DECLINLINE(size_t) vboxSfOs2CountLeadingSlashes(const char *pszPath)
430{
431 size_t cchSlashes = 0;
432 char ch;
433 while ((ch = *pszPath) == '\\' || ch == '/')
434 cchSlashes++, pszPath++;
435 return cchSlashes;
436}
437
438
439/**
440 * Checks for a VBox UNC prefix (server + slashes) and determins its length when
441 * found.
442 *
443 * @returns Length of VBoxSF UNC prefix, 0 if not VBoxSF UNC prefix.
444 * @param pszPath The possible UNC path.
445 */
446DECLINLINE(size_t) vboxSfOs2UncPrefixLength(const char *pszPath)
447{
448 char ch;
449 if ( ((ch = pszPath[0]) == '\\' || ch == '/')
450 && ((ch = pszPath[1]) == '\\' || ch == '/')
451 && ((ch = pszPath[2]) == 'V' || ch == 'v')
452 && ((ch = pszPath[3]) == 'B' || ch == 'b')
453 && ((ch = pszPath[4]) == 'O' || ch == 'o')
454 && ((ch = pszPath[5]) == 'X' || ch == 'x')
455 && ((ch = pszPath[6]) == 'S' || ch == 's')
456 )
457 {
458 /* \\VBoxSf\ */
459 if ( ((ch = pszPath[7]) == 'F' || ch == 'f')
460 && ((ch = pszPath[8]) == '\\' || ch == '/') )
461 return vboxSfOs2CountLeadingSlashes(&pszPath[9]) + 9;
462
463 /* \\VBoxSvr\ */
464 if ( ((ch = pszPath[7]) == 'V' || ch == 'v')
465 && ((ch = pszPath[8]) == 'R' || ch == 'r')
466 && ((ch = pszPath[9]) == '\\' || ch == '/') )
467 return vboxSfOs2CountLeadingSlashes(&pszPath[10]) + 10;
468
469 /* \\VBoxSrv\ */
470 if ( ((ch = pszPath[7]) == 'R' || ch == 'r')
471 && ((ch = pszPath[8]) == 'V' || ch == 'v')
472 && ((ch = pszPath[9]) == '\\' || ch == '/') )
473 return vboxSfOs2CountLeadingSlashes(&pszPath[10]) + 10;
474 }
475
476 return 0;
477}
478
479
480/**
481 * Converts a path to UTF-16 and puts it in a VBGL friendly buffer.
482 *
483 * @returns OS/2 status code
484 * @param pszFolderPath The path to convert.
485 * @param ppStr Where to return the pointer to the buffer. Free
486 * using vboxSfOs2FreePath.
487 */
488APIRET vboxSfOs2ConvertPath(const char *pszFolderPath, PSHFLSTRING *ppStr)
489{
490 /*
491 * Skip unnecessary leading slashes.
492 */
493 char ch = *pszFolderPath;
494 if (ch == '\\' || ch == '/')
495 while ((ch = pszFolderPath[1]) == '\\' || ch == '/')
496 pszFolderPath++;
497
498 /*
499 * Since the KEE unicode conversion routines does not seem to know of
500 * surrogate pairs, we will get a very good output size estimate by
501 * using strlen() on the input.
502 */
503 size_t cchSrc = strlen(pszFolderPath);
504 PSHFLSTRING pDst = vboxSfOs2StrAlloc(cchSrc + 4 /*fudge*/);
505 if (pDst)
506 {
507 APIRET rc = SafeKernStrToUcs(NULL, &pDst->String.utf16[0], (char *)pszFolderPath, cchSrc + 4, cchSrc);
508 if (rc == NO_ERROR)
509 {
510 pDst->u16Length = (uint16_t)RTUtf16Len(pDst->String.utf16) * (uint16_t)sizeof(RTUTF16);
511 Assert(pDst->u16Length < pDst->u16Size);
512 pDst->u16Size = pDst->u16Length + (uint16_t)sizeof(RTUTF16); /* (limit how much is copied to the host) */
513 *ppStr = pDst;
514 return NO_ERROR;
515 }
516 VbglR0PhysHeapFree(pDst);
517
518 /*
519 * This shouldn't happen, but just in case we try again with twice
520 * the buffer size.
521 */
522 if (rc == 0x20412 /*ULS_BUFFERFULL*/)
523 {
524 pDst = vboxSfOs2StrAlloc((cchSrc + 16) * 2);
525 if (pDst)
526 {
527 rc = SafeKernStrToUcs(NULL, pDst->String.utf16, (char *)pszFolderPath, (cchSrc + 16) * 2, cchSrc);
528 if (rc == NO_ERROR)
529 {
530 pDst->u16Length = (uint16_t)RTUtf16Len(pDst->String.utf16) * (uint16_t)sizeof(RTUTF16);
531 Assert(pDst->u16Length < pDst->u16Size);
532 pDst->u16Size = pDst->u16Length + (uint16_t)sizeof(RTUTF16);
533 *ppStr = pDst;
534 return NO_ERROR;
535 }
536 VbglR0PhysHeapFree(pDst);
537 LogRel(("vboxSfOs2ConvertPath: SafeKernStrToUcs returns %#x for %.*Rhxs\n", rc, cchSrc, pszFolderPath));
538 }
539 }
540 else
541 LogRel(("vboxSfOs2ConvertPath: SafeKernStrToUcs returns %#x for %.*Rhxs\n", rc, cchSrc, pszFolderPath));
542 }
543
544 LogRel(("vboxSfOs2ConvertPath: Out of memory - cchSrc=%#x\n", cchSrc));
545 *ppStr = NULL;
546 return ERROR_NOT_ENOUGH_MEMORY;
547}
548
549
550/**
551 * Converts a path to UTF-16 and puts it in a VBGL friendly buffer within a
552 * larger buffer.
553 *
554 * @returns OS/2 status code
555 * @param pszFolderPath The path to convert.
556 * @param offStrInBuf The offset of the SHLFSTRING in the return buffer.
557 * This first part of the buffer is zeroed.
558 * @param ppvBuf Where to return the pointer to the buffer. Free
559 * using vboxSfOs2FreePath.
560 */
561APIRET vboxSfOs2ConvertPathEx(const char *pszFolderPath, uint32_t offStrInBuf, void **ppvBuf)
562{
563 /*
564 * Skip unnecessary leading slashes.
565 */
566 char ch = *pszFolderPath;
567 if (ch == '\\' || ch == '/')
568 while ((ch = pszFolderPath[1]) == '\\' || ch == '/')
569 pszFolderPath++;
570
571 /*
572 * Since the KEE unicode conversion routines does not seem to know of
573 * surrogate pairs, we will get a very good output size estimate by
574 * using strlen() on the input.
575 */
576 size_t cchSrc = strlen(pszFolderPath);
577 void *pvBuf = VbglR0PhysHeapAlloc(offStrInBuf + SHFLSTRING_HEADER_SIZE + (cchSrc + 4) * sizeof(RTUTF16));
578 if (pvBuf)
579 {
580 RT_BZERO(pvBuf, offStrInBuf);
581 PSHFLSTRING pDst = (PSHFLSTRING)((uint8_t *)pvBuf + offStrInBuf);
582
583 APIRET rc = SafeKernStrToUcs(NULL, &pDst->String.utf16[0], (char *)pszFolderPath, cchSrc + 4, cchSrc);
584 if (rc == NO_ERROR)
585 {
586 pDst->u16Length = (uint16_t)RTUtf16Len(pDst->String.utf16) * (uint16_t)sizeof(RTUTF16);
587 Assert(pDst->u16Length < (cchSrc + 4) * sizeof(RTUTF16));
588 pDst->u16Size = pDst->u16Length + (uint16_t)sizeof(RTUTF16); /* (limit how much is copied to the host) */
589 *ppvBuf = pvBuf;
590 return NO_ERROR;
591 }
592 VbglR0PhysHeapFree(pvBuf);
593
594 /*
595 * This shouldn't happen, but just in case we try again with twice
596 * the buffer size.
597 */
598 if (rc == 0x20412 /*ULS_BUFFERFULL*/)
599 {
600 pvBuf = VbglR0PhysHeapAlloc(offStrInBuf + SHFLSTRING_HEADER_SIZE + (cchSrc + 16) * sizeof(RTUTF16) * 2);
601 if (pvBuf)
602 {
603 RT_BZERO(pvBuf, offStrInBuf);
604 pDst = (PSHFLSTRING)((uint8_t *)pvBuf + offStrInBuf);
605
606 rc = SafeKernStrToUcs(NULL, pDst->String.utf16, (char *)pszFolderPath, (cchSrc + 16) * 2, cchSrc);
607 if (rc == NO_ERROR)
608 {
609 pDst->u16Length = (uint16_t)RTUtf16Len(pDst->String.utf16) * (uint16_t)sizeof(RTUTF16);
610 Assert(pDst->u16Length < (cchSrc + 16) * 2 * sizeof(RTUTF16));
611 pDst->u16Size = pDst->u16Length + (uint16_t)sizeof(RTUTF16);
612 *ppvBuf = pvBuf;
613 return NO_ERROR;
614 }
615 VbglR0PhysHeapFree(pDst);
616 LogRel(("vboxSfOs2ConvertPath: SafeKernStrToUcs returns %#x for %.*Rhxs\n", rc, cchSrc, pszFolderPath));
617 }
618 }
619 else
620 LogRel(("vboxSfOs2ConvertPath: SafeKernStrToUcs returns %#x for %.*Rhxs\n", rc, cchSrc, pszFolderPath));
621 }
622
623 LogRel(("vboxSfOs2ConvertPath: Out of memory - cchSrc=%#x offStrInBuf=%#x\n", cchSrc, offStrInBuf));
624 *ppvBuf = NULL;
625 return ERROR_NOT_ENOUGH_MEMORY;
626}
627
628
629/**
630 * Counterpart to vboxSfOs2ResolvePath.
631 *
632 * @param pStrPath The path to free.
633 * @param pFolder The folder to release.
634 */
635void vboxSfOs2ReleasePathAndFolder(PSHFLSTRING pStrPath, PVBOXSFFOLDER pFolder)
636{
637 if (pStrPath)
638 VbglR0PhysHeapFree(pStrPath);
639 if (pFolder)
640 {
641 uint32_t cRefs = ASMAtomicDecU32(&pFolder->cRefs);
642 Assert(cRefs < _64K);
643 if (!cRefs)
644 vboxSfOs2DestroyFolder(pFolder);
645 }
646}
647
648
649/**
650 * Worker for vboxSfOs2ResolvePath() for dynamically mapping folders for UNC
651 * paths.
652 *
653 * @returns OS/2 status code.
654 * @param pachFolderName The folder to map. Not necessarily zero terminated
655 * at the end of the folder name!
656 * @param cchFolderName The length of the folder name.
657 * @param uRevBefore The previous folder list revision.
658 * @param ppFolder Where to return the pointer to the retained folder.
659 */
660DECL_NO_INLINE(static, int) vboxSfOs2AttachUncAndRetain(const char *pachFolderName, size_t cchFolderName,
661 uint32_t uRevBefore, PVBOXSFFOLDER *ppFolder)
662{
663 KernRequestExclusiveMutex(&g_MtxFolders);
664
665 /*
666 * Check if someone raced us to it.
667 */
668 if (uRevBefore != g_uFolderRevision)
669 {
670 PVBOXSFFOLDER pFolder = vboxSfOs2FindAndRetainFolder(pachFolderName, cchFolderName);
671 if (pFolder)
672 {
673 KernReleaseExclusiveMutex(&g_MtxFolders);
674 *ppFolder = pFolder;
675 return NO_ERROR;
676 }
677 }
678
679 int rc = vboxSfOs2EnsureConnected();
680 if (RT_SUCCESS(rc))
681 {
682 /*
683 * Copy the name into the buffer format that Vbgl desires.
684 */
685 PSHFLSTRING pStrName = vboxSfOs2StrAlloc(cchFolderName);
686 if (pStrName)
687 {
688 memcpy(pStrName->String.ach, pachFolderName, cchFolderName);
689 pStrName->String.ach[cchFolderName] = '\0';
690 pStrName->u16Length = (uint16_t)cchFolderName;
691
692 /*
693 * Do the attaching.
694 */
695 rc = vboxSfOs2MapFolder(pStrName, NULL, ppFolder);
696 vboxSfOs2StrFree(pStrName);
697 if (RT_SUCCESS(rc))
698 {
699 KernReleaseExclusiveMutex(&g_MtxFolders);
700 LogRel(("vboxSfOs2AttachUncAndRetain: Successfully attached '%s' (as UNC).\n", (*ppFolder)->szName));
701 return NO_ERROR;
702 }
703
704 if (rc == VERR_NO_MEMORY)
705 rc = ERROR_NOT_ENOUGH_MEMORY;
706 else
707 rc = ERROR_PATH_NOT_FOUND;
708 }
709 else
710 rc = ERROR_NOT_ENOUGH_MEMORY;
711 }
712 else
713 rc = ERROR_PATH_NOT_FOUND;
714
715 KernReleaseExclusiveMutex(&g_MtxFolders);
716 return rc;
717}
718
719
720/**
721 * Resolves the given path to a folder structure and folder relative string.
722 *
723 * @returns OS/2 status code.
724 * @param pszPath The path to resolve.
725 * @param pCdFsd The IFS dependent CWD structure if present.
726 * @param offCurDirEnd The offset into @a pszPath of the CWD. -1 if not
727 * CWD relative path.
728 * @param ppFolder Where to return the referenced pointer to the folder
729 * structure. Call vboxSfOs2ReleaseFolder() when done.
730 * @param ppStrFolderPath Where to return a buffer holding the folder relative
731 * path component. Free using vboxSfOs2FreePath().
732 */
733APIRET vboxSfOs2ResolvePath(const char *pszPath, PVBOXSFCD pCdFsd, LONG offCurDirEnd,
734 PVBOXSFFOLDER *ppFolder, PSHFLSTRING *ppStrFolderPath)
735{
736 APIRET rc;
737
738 /*
739 * UNC path? Reject the prefix to be on the safe side.
740 */
741 char ch = pszPath[0];
742 if (ch == '\\' || ch == '/')
743 {
744 size_t cchPrefix = vboxSfOs2UncPrefixLength(pszPath);
745 if (cchPrefix > 0)
746 {
747 /* Find the length of the folder name (share). */
748 const char *pszFolderName = &pszPath[cchPrefix];
749 size_t cchFolderName = 0;
750 while ((ch = pszFolderName[cchFolderName]) != '\0' && ch != '\\' && ch != '/')
751 {
752 if ((uint8_t)ch >= 0x20 && (uint8_t)ch <= 0x7f && ch != ':')
753 cchFolderName++;
754 else
755 {
756 LogRel(("vboxSfOs2ResolvePath: Invalid share name (@%u): %.*Rhxs\n",
757 cchPrefix + cchFolderName, strlen(pszPath), pszPath));
758 return ERROR_INVALID_NAME;
759 }
760 }
761 if (cchFolderName >= VBOXSFOS2_MAX_FOLDER_NAME)
762 {
763 LogRel(("vboxSfOs2ResolvePath: Folder name is too long: %u, max %u (%s)\n",
764 cchFolderName, VBOXSFOS2_MAX_FOLDER_NAME, pszPath));
765 return ERROR_FILENAME_EXCED_RANGE;
766 }
767
768 /*
769 * Look for the share.
770 */
771 KernRequestSharedMutex(&g_MtxFolders);
772 PVBOXSFFOLDER pFolder = *ppFolder = vboxSfOs2FindAndRetainFolder(pszFolderName, cchFolderName);
773 if (pFolder)
774 {
775 vboxSfOs2RetainFolder(pFolder);
776 KernReleaseSharedMutex(&g_MtxFolders);
777 }
778 else
779 {
780 uint32_t const uRevBefore = g_uFolderRevision;
781 KernReleaseSharedMutex(&g_MtxFolders);
782 rc = vboxSfOs2AttachUncAndRetain(pszFolderName, cchFolderName, uRevBefore, ppFolder);
783 if (rc == NO_ERROR)
784 pFolder = *ppFolder;
785 else
786 return rc;
787 }
788
789 /*
790 * Convert the path and put it in a Vbgl compatible buffer..
791 */
792 rc = vboxSfOs2ConvertPath(&pszFolderName[cchFolderName], ppStrFolderPath);
793 if (rc == NO_ERROR)
794 return rc;
795
796 vboxSfOs2ReleaseFolder(pFolder);
797 *ppFolder = NULL;
798 return rc;
799 }
800
801 LogRel(("vboxSfOs2ResolvePath: Unexpected path: %s\n", pszPath));
802 return ERROR_PATH_NOT_FOUND;
803 }
804
805 /*
806 * Drive letter?
807 */
808 ch &= ~0x20; /* upper case */
809 if ( ch >= 'A'
810 && ch <= 'Z'
811 && pszPath[1] == ':')
812 {
813 unsigned iDrive = ch - 'A';
814 ch = pszPath[2];
815 if (ch == '\\' || ch == '/')
816 {
817 KernRequestSharedMutex(&g_MtxFolders);
818 PVBOXSFFOLDER pFolder = *ppFolder = g_apDriveFolders[iDrive];
819 if (pFolder)
820 {
821 vboxSfOs2RetainFolder(pFolder);
822 KernReleaseSharedMutex(&g_MtxFolders);
823
824 /*
825 * Convert the path and put it in a Vbgl compatible buffer..
826 */
827 rc = vboxSfOs2ConvertPath(&pszPath[3], ppStrFolderPath);
828 if (rc == NO_ERROR)
829 return rc;
830
831 vboxSfOs2ReleaseFolder(pFolder);
832 *ppFolder = NULL;
833 return rc;
834 }
835 KernReleaseSharedMutex(&g_MtxFolders);
836 LogRel(("vboxSfOs2ResolvePath: No folder mapped on '%s'. Detach race?\n", pszPath));
837 return ERROR_PATH_NOT_FOUND;
838 }
839 LogRel(("vboxSfOs2ResolvePath: No root slash: '%s'\n", pszPath));
840 return ERROR_PATH_NOT_FOUND;
841 }
842 LogRel(("vboxSfOs2ResolvePath: Unexpected path: %s\n", pszPath));
843 RT_NOREF_PV(pCdFsd); RT_NOREF_PV(offCurDirEnd);
844 return ERROR_PATH_NOT_FOUND;
845}
846
847
848/**
849 * Resolves the given path to a folder structure and folder relative string,
850 * the latter placed within a larger request buffer.
851 *
852 * @returns OS/2 status code.
853 * @param pszPath The path to resolve.
854 * @param pCdFsd The IFS dependent CWD structure if present.
855 * @param offCurDirEnd The offset into @a pszPath of the CWD. -1 if not
856 * CWD relative path.
857 * @param offStrInBuf The offset of the SHLFSTRING in the return buffer.
858 * This first part of the buffer is zeroed.
859 * @param ppFolder Where to return the referenced pointer to the folder
860 * structure. Call vboxSfOs2ReleaseFolder() when done.
861 * @param ppvBuf Where to return the Pointer to the buffer. Free
862 * using VbglR0PhysHeapFree.
863 */
864APIRET vboxSfOs2ResolvePathEx(const char *pszPath, PVBOXSFCD pCdFsd, LONG offCurDirEnd, uint32_t offStrInBuf,
865 PVBOXSFFOLDER *ppFolder, void **ppvBuf)
866{
867 APIRET rc;
868
869 /*
870 * UNC path? Reject the prefix to be on the safe side.
871 */
872 char ch = pszPath[0];
873 if (ch == '\\' || ch == '/')
874 {
875 size_t cchPrefix = vboxSfOs2UncPrefixLength(pszPath);
876 if (cchPrefix > 0)
877 {
878 /* Find the length of the folder name (share). */
879 const char *pszFolderName = &pszPath[cchPrefix];
880 size_t cchFolderName = 0;
881 while ((ch = pszFolderName[cchFolderName]) != '\0' && ch != '\\' && ch != '/')
882 {
883 if ((uint8_t)ch >= 0x20 && (uint8_t)ch <= 0x7f && ch != ':')
884 cchFolderName++;
885 else
886 {
887 LogRel(("vboxSfOs2ResolvePath: Invalid share name (@%u): %.*Rhxs\n",
888 cchPrefix + cchFolderName, strlen(pszPath), pszPath));
889 return ERROR_INVALID_NAME;
890 }
891 }
892 if (cchFolderName >= VBOXSFOS2_MAX_FOLDER_NAME)
893 {
894 LogRel(("vboxSfOs2ResolvePath: Folder name is too long: %u, max %u (%s)\n",
895 cchFolderName, VBOXSFOS2_MAX_FOLDER_NAME, pszPath));
896 return ERROR_FILENAME_EXCED_RANGE;
897 }
898
899 /*
900 * Look for the share.
901 */
902 KernRequestSharedMutex(&g_MtxFolders);
903 PVBOXSFFOLDER pFolder = *ppFolder = vboxSfOs2FindAndRetainFolder(pszFolderName, cchFolderName);
904 if (pFolder)
905 {
906 vboxSfOs2RetainFolder(pFolder);
907 KernReleaseSharedMutex(&g_MtxFolders);
908 }
909 else
910 {
911 uint32_t const uRevBefore = g_uFolderRevision;
912 KernReleaseSharedMutex(&g_MtxFolders);
913 rc = vboxSfOs2AttachUncAndRetain(pszFolderName, cchFolderName, uRevBefore, ppFolder);
914 if (rc == NO_ERROR)
915 pFolder = *ppFolder;
916 else
917 return rc;
918 }
919
920 /*
921 * Convert the path and put it in a Vbgl compatible buffer..
922 */
923 rc = vboxSfOs2ConvertPathEx(&pszFolderName[cchFolderName], offStrInBuf, ppvBuf);
924 if (rc == NO_ERROR)
925 return rc;
926
927 vboxSfOs2ReleaseFolder(pFolder);
928 *ppFolder = NULL;
929 return rc;
930 }
931
932 LogRel(("vboxSfOs2ResolvePath: Unexpected path: %s\n", pszPath));
933 return ERROR_PATH_NOT_FOUND;
934 }
935
936 /*
937 * Drive letter?
938 */
939 ch &= ~0x20; /* upper case */
940 if ( ch >= 'A'
941 && ch <= 'Z'
942 && pszPath[1] == ':')
943 {
944 unsigned iDrive = ch - 'A';
945 ch = pszPath[2];
946 if (ch == '\\' || ch == '/')
947 {
948 KernRequestSharedMutex(&g_MtxFolders);
949 PVBOXSFFOLDER pFolder = *ppFolder = g_apDriveFolders[iDrive];
950 if (pFolder)
951 {
952 vboxSfOs2RetainFolder(pFolder);
953 KernReleaseSharedMutex(&g_MtxFolders);
954
955 /*
956 * Convert the path and put it in a Vbgl compatible buffer..
957 */
958 rc = vboxSfOs2ConvertPathEx(&pszPath[3], offStrInBuf, ppvBuf);
959 if (rc == NO_ERROR)
960 return rc;
961
962 vboxSfOs2ReleaseFolder(pFolder);
963 *ppFolder = NULL;
964 return rc;
965 }
966 KernReleaseSharedMutex(&g_MtxFolders);
967 LogRel(("vboxSfOs2ResolvePath: No folder mapped on '%s'. Detach race?\n", pszPath));
968 return ERROR_PATH_NOT_FOUND;
969 }
970 LogRel(("vboxSfOs2ResolvePath: No root slash: '%s'\n", pszPath));
971 return ERROR_PATH_NOT_FOUND;
972 }
973 LogRel(("vboxSfOs2ResolvePath: Unexpected path: %s\n", pszPath));
974 RT_NOREF_PV(pCdFsd); RT_NOREF_PV(offCurDirEnd);
975 return ERROR_PATH_NOT_FOUND;
976}
977
978
979DECLASM(void)
980FS32_EXIT(ULONG uid, ULONG pid, ULONG pdb)
981{
982 LogFlow(("FS32_EXIT: uid=%u pid=%u pdb=%#x\n", uid, pid, pdb));
983 NOREF(uid); NOREF(pid); NOREF(pdb);
984}
985
986
987DECLASM(APIRET)
988FS32_SHUTDOWN(ULONG uType, ULONG uReserved)
989{
990 LogFlow(("FS32_SHUTDOWN: type=%u uReserved=%u\n", uType, uReserved));
991 NOREF(uType); NOREF(uReserved);
992 return NO_ERROR;
993}
994
995
996/**
997 * FS32_ATTACH worker: FS_ATTACH
998 */
999static APIRET vboxSfOs2Attach(PCSZ pszDev, PVBOXSFVP pVpFsd, PVBOXSFCD pCdFsd, PBYTE pszParam, PUSHORT pcbParam,
1000 PSHFLSTRING *ppCleanup)
1001{
1002 /*
1003 * Check out the parameters, copying the pszParam into a suitable string buffer.
1004 */
1005 if (pszDev == NULL || !*pszDev || !RT_C_IS_ALPHA(pszDev[0]) || pszDev[1] != ':' || pszDev[2] != '\0')
1006 {
1007 LogRel(("vboxSfOs2Attach: Invalid pszDev value:%p:{%s}\n", pszDev, pszDev));
1008 return ERROR_INVALID_PARAMETER;
1009 }
1010 unsigned const iDrive = (pszDev[0] & ~0x20) - 'A';
1011
1012 if (pszParam == NULL || pcbParam == NULL)
1013 {
1014 LogRel(("vboxSfOs2Attach: NULL parameter buffer or buffer length\n"));
1015 return ERROR_INVALID_PARAMETER;
1016 }
1017
1018 PSHFLSTRING pStrName = *ppCleanup = vboxSfOs2StrAlloc(VBOXSFOS2_MAX_FOLDER_NAME - 1);
1019 pStrName->u16Length = *pcbParam;
1020 if (pStrName->u16Length < 1 || pStrName->u16Length > VBOXSFOS2_MAX_FOLDER_NAME)
1021 {
1022 LogRel(("vboxSfOs2Attach: Parameter buffer length is out of bounds: %u (min: 1, max " RT_XSTR(VBOXSFOS2_MAX_FOLDER_NAME) ")\n",
1023 pStrName->u16Length));
1024 return ERROR_INVALID_PARAMETER;
1025 }
1026
1027 int rc = KernCopyIn(pStrName->String.utf8, pszParam, pStrName->u16Length);
1028 if (rc != NO_ERROR)
1029 return rc;
1030
1031 pStrName->u16Length -= 1;
1032 if (pStrName->String.utf8[pStrName->u16Length] != '\0')
1033 {
1034 LogRel(("vboxSfOs2Attach: Parameter not null terminated\n"));
1035 return ERROR_INVALID_PARAMETER;
1036 }
1037
1038 /* Make sure it's only ascii and contains not weird stuff.
1039 Note! There could be a 2nd tag string, so identify that one. */
1040 const char *pszTag = NULL;
1041 unsigned off = pStrName->u16Length;
1042 while (off-- > 0)
1043 {
1044 char const ch = pStrName->String.utf8[off];
1045 if (ch < 0x20 || ch >= 0x7f || ch == ':' || ch == '\\' || ch == '/')
1046 {
1047 if (ch == '\0' && !pszTag && off + 1 < pStrName->u16Length && off > 0)
1048 {
1049 pszTag = &pStrName->String.ach[off + 1];
1050 pStrName->u16Length = (uint16_t)off;
1051 }
1052 else
1053 {
1054 LogRel(("vboxSfOs2Attach: Malformed folder name: %.*Rhxs (off %#x)\n", pStrName->u16Length, pStrName->String.utf8, off));
1055 return ERROR_INVALID_PARAMETER;
1056 }
1057 }
1058 }
1059
1060 /* Is there a tag following the name? */
1061
1062 if (!pVpFsd)
1063 {
1064 LogRel(("vboxSfOs2Attach: pVpFsd is NULL\n"));
1065 return ERROR_INVALID_PARAMETER;
1066 }
1067
1068 /*
1069 * Look for the folder to see if we're already using it. Map it if needed.
1070 */
1071 KernRequestExclusiveMutex(&g_MtxFolders);
1072 if (g_apDriveFolders[iDrive] == NULL)
1073 {
1074
1075 PVBOXSFFOLDER pFolder = vboxSfOs2FindAndRetainFolder(pStrName->String.ach, pStrName->u16Length);
1076 if (!pFolder)
1077 {
1078 rc = vboxSfOs2EnsureConnected();
1079 if (RT_SUCCESS(rc))
1080 rc = vboxSfOs2MapFolder(pStrName, pszTag, &pFolder);
1081 }
1082 if (pFolder && RT_SUCCESS(rc))
1083 {
1084 pFolder->cDrives += 1;
1085 g_apDriveFolders[iDrive] = pFolder;
1086
1087 pVpFsd->u32Magic = VBOXSFVP_MAGIC;
1088 pVpFsd->pFolder = pFolder;
1089
1090 KernReleaseExclusiveMutex(&g_MtxFolders);
1091
1092 LogRel(("vboxSfOs2Attach: Successfully attached '%s' to '%s'.\n", pFolder->szName, pszDev));
1093 return NO_ERROR;
1094 }
1095
1096 KernReleaseExclusiveMutex(&g_MtxFolders);
1097 return ERROR_FILE_NOT_FOUND;
1098 }
1099 KernReleaseExclusiveMutex(&g_MtxFolders);
1100
1101 LogRel(("vboxSfOs2Attach: Already got a folder on '%s'!\n", pszDev));
1102 RT_NOREF(pCdFsd);
1103 return ERROR_BUSY_DRIVE;
1104}
1105
1106
1107/**
1108 * FS32_ATTACH worker: FS_DETACH
1109 */
1110static APIRET vboxSfOs2Detach(PCSZ pszDev, PVBOXSFVP pVpFsd, PVBOXSFCD pCdFsd, PBYTE pszParam, PUSHORT pcbParam)
1111{
1112 /*
1113 * Validate the volume data and assocated folder.
1114 */
1115 AssertPtrReturn(pVpFsd, ERROR_SYS_INTERNAL);
1116 AssertReturn(pVpFsd->u32Magic == VBOXSFVP_MAGIC, ERROR_SYS_INTERNAL);
1117 PVBOXSFFOLDER pFolder = pVpFsd->pFolder;
1118 AssertPtrReturn(pFolder, ERROR_SYS_INTERNAL);
1119 AssertReturn(pFolder->u32Magic == VBOXSFFOLDER_MAGIC, ERROR_SYS_INTERNAL);
1120
1121 uint8_t idxDrive = UINT8_MAX;
1122 if ( pszDev
1123 && RT_C_IS_ALPHA(*pszDev))
1124 idxDrive = (*pszDev & ~0x20) - 'A';
1125
1126 /*
1127 * Can we detach it?
1128 */
1129 APIRET rc;
1130 KernRequestExclusiveMutex(&g_MtxFolders);
1131 if ( pFolder->cOpenFiles == 0
1132 && pFolder->cOpenSearches == 0)
1133 {
1134 /*
1135 * Check that we've got the right folder/drive combo.
1136 */
1137 if ( idxDrive < RT_ELEMENTS(g_apDriveFolders)
1138 && g_apDriveFolders[idxDrive] == pFolder)
1139 {
1140 g_apDriveFolders[idxDrive] = NULL;
1141 uint8_t cDrives = --pFolder->cDrives;
1142 AssertMsg(cDrives < 30, ("%#x\n", cDrives));
1143
1144 uint32_t cRefs = ASMAtomicDecU32(&pFolder->cRefs);
1145 AssertMsg(cRefs < _32K, ("%#x\n", cRefs));
1146 if (cRefs)
1147 {
1148 /* If there are now zero drives, unlink it from the list and release
1149 the list reference. This should almost always drop end up with us
1150 destroying the folder.*/
1151 if (cDrives == 0)
1152 {
1153 RTListNodeRemove(&pFolder->ListEntry);
1154 cRefs = ASMAtomicDecU32(&pFolder->cRefs);
1155 AssertMsg(cRefs < _32K, ("%#x\n", cRefs));
1156 if (!cRefs)
1157 vboxSfOs2DestroyFolder(pFolder);
1158 }
1159 }
1160 else
1161 {
1162 LogRel(("vboxSfOs2Detach: cRefs=0?!?\n"));
1163 vboxSfOs2DestroyFolder(pFolder);
1164 }
1165 rc = NO_ERROR;
1166 }
1167 else
1168 {
1169 LogRel(("vboxSfOs2Detach: g_apDriveFolders[%#x]=%p pFolder=%p\n",
1170 idxDrive, idxDrive < RT_ELEMENTS(g_apDriveFolders) ? g_apDriveFolders[idxDrive] : NULL, pFolder));
1171 rc = ERROR_NOT_SUPPORTED;
1172 }
1173 }
1174 else
1175 rc = ERROR_BUSY_DRIVE;
1176 KernReleaseExclusiveMutex(&g_MtxFolders);
1177
1178 RT_NOREF(pszDev, pVpFsd, pCdFsd, pszParam, pcbParam);
1179 return rc;
1180}
1181
1182
1183/**
1184 * FS32_ATTACH worker: FSA_ATTACH_INFO
1185 */
1186static APIRET vboxSfOs2QueryAttachInfo(PCSZ pszDev, PVBOXSFVP pVpFsd, PVBOXSFCD pCdFsd, PBYTE pbData, PUSHORT pcbParam)
1187{
1188 /*
1189 * Userland calls the kernel with a FSQBUFFER buffer, the kernel
1190 * fills in the first part of us and hands us &FSQBUFFER::cbFSAData
1191 * to do the rest. We could return the share name here, for instance.
1192 */
1193 APIRET rc;
1194 USHORT cbParam = *pcbParam;
1195 if ( pszDev == NULL
1196 || (pszDev[0] != '\\' && pszDev[0] != '/'))
1197 {
1198 /* Validate the volume data and assocated folder. */
1199 AssertPtrReturn(pVpFsd, ERROR_SYS_INTERNAL);
1200 AssertReturn(pVpFsd->u32Magic == VBOXSFVP_MAGIC, ERROR_SYS_INTERNAL);
1201 PVBOXSFFOLDER pFolder = pVpFsd->pFolder;
1202 AssertPtrReturn(pFolder, ERROR_SYS_INTERNAL);
1203 AssertReturn(pFolder->u32Magic == VBOXSFFOLDER_MAGIC, ERROR_SYS_INTERNAL);
1204
1205 /* Try copy out the data. */
1206 if (cbParam >= sizeof(USHORT) + pFolder->cbNameAndTag)
1207 {
1208 *pcbParam = (uint16_t)sizeof(USHORT) + pFolder->cbNameAndTag;
1209 cbParam = pFolder->cchName + 1;
1210 rc = KernCopyOut(pbData, &cbParam, sizeof(cbParam));
1211 if (rc == NO_ERROR)
1212 rc = KernCopyOut(pbData + sizeof(USHORT), pFolder->szName, pFolder->cbNameAndTag);
1213 }
1214 else
1215 rc = ERROR_BUFFER_OVERFLOW;
1216 }
1217 else
1218 {
1219 /* Looks like a device query, so return zero bytes. */
1220 if (cbParam >= sizeof(USHORT))
1221 {
1222 *pcbParam = sizeof(USHORT);
1223 cbParam = 0;
1224 rc = KernCopyOut(pbData, &cbParam, sizeof(cbParam));
1225 }
1226 else
1227 rc = ERROR_BUFFER_OVERFLOW;
1228 }
1229
1230 RT_NOREF(pCdFsd);
1231 return rc;
1232}
1233
1234
1235DECLASM(APIRET)
1236FS32_ATTACH(ULONG fFlags, PCSZ pszDev, PVBOXSFVP pVpFsd, PVBOXSFCD pCdFsd, PBYTE pszParam, PUSHORT pcbParam)
1237{
1238 LogFlow(("FS32_ATTACH: fFlags=%#x pszDev=%p:{%s} pVpFsd=%p pCdFsd=%p pszParam=%p pcbParam=%p\n",
1239 fFlags, pszDev, pszDev, pVpFsd, pCdFsd, pszParam, pcbParam));
1240 APIRET rc;
1241 if (pVpFsd)
1242 {
1243 PSHFLSTRING pCleanup = NULL;
1244
1245 if (fFlags == FS_ATTACH)
1246 rc = vboxSfOs2Attach(pszDev, pVpFsd, pCdFsd, pszParam, pcbParam, &pCleanup);
1247 else if (fFlags == FSA_DETACH)
1248 rc = vboxSfOs2Detach(pszDev, pVpFsd, pCdFsd, pszParam, pcbParam);
1249 else if (fFlags == FSA_ATTACH_INFO)
1250 rc = vboxSfOs2QueryAttachInfo(pszDev, pVpFsd, pCdFsd, pszParam, pcbParam);
1251 else
1252 {
1253 LogRel(("FS32_ATTACH: Unsupported fFlags value: %#x\n", fFlags));
1254 rc = ERROR_NOT_SUPPORTED;
1255 }
1256
1257 if (pCleanup)
1258 vboxSfOs2StrFree(pCleanup);
1259 }
1260 else
1261 rc = ERROR_NOT_SUPPORTED; /* We don't support device attaching. */
1262 LogFlow(("FS32_ATTACH: returns %u\n", rc));
1263 return rc;
1264}
1265
1266
1267DECLASM(APIRET)
1268FS32_VERIFYUNCNAME(ULONG uType, PCSZ pszName)
1269{
1270 LogFlow(("FS32_VERIFYUNCNAME: uType=%#x pszName=%p:{%s}\n", uType, pszName, pszName));
1271 RT_NOREF(uType); /* pass 1 or pass 2 doesn't matter to us, we've only got one 'server'. */
1272
1273 if (vboxSfOs2UncPrefixLength(pszName) > 0)
1274 return NO_ERROR;
1275 return ERROR_NOT_SUPPORTED;
1276}
1277
1278
1279DECLASM(APIRET)
1280FS32_FLUSHBUF(USHORT hVPB, ULONG fFlags)
1281{
1282 NOREF(hVPB); NOREF(fFlags);
1283 return NO_ERROR;
1284}
1285
1286
1287DECLASM(APIRET)
1288FS32_FSINFO(ULONG fFlags, USHORT hVpb, PBYTE pbData, ULONG cbData, ULONG uLevel)
1289{
1290 LogFlow(("FS32_FSINFO: fFlags=%#x hVpb=%#x pbData=%p cbData=%#x uLevel=%p\n", fFlags, hVpb, pbData, cbData, uLevel));
1291
1292 /*
1293 * Resolve hVpb and do parameter validation.
1294 */
1295 PVPFSI pVpFsi = NULL;
1296 PVBOXSFVP pVpFsd = Fsh32GetVolParams(hVpb, &pVpFsi);
1297 Log(("FS32_FSINFO: hVpb=%#x -> pVpFsd=%p pVpFsi=%p\n", hVpb, pVpFsd, pVpFsi));
1298
1299 AssertPtrReturn(pVpFsd, ERROR_SYS_INTERNAL);
1300 AssertReturn(pVpFsd->u32Magic == VBOXSFVP_MAGIC, ERROR_SYS_INTERNAL);
1301 PVBOXSFFOLDER pFolder = pVpFsd->pFolder; /** @todo need to retain it behind locks. */
1302 AssertPtrReturn(pFolder, ERROR_SYS_INTERNAL);
1303 AssertReturn(pFolder->u32Magic == VBOXSFFOLDER_MAGIC, ERROR_SYS_INTERNAL);
1304
1305 APIRET rc;
1306
1307 /*
1308 * Queries.
1309 */
1310 if (fFlags == INFO_RETREIVE)
1311 {
1312 /* Check that buffer/level matches up. */
1313 switch (uLevel)
1314 {
1315 case FSIL_ALLOC:
1316 if (cbData >= sizeof(FSALLOCATE))
1317 break;
1318 LogFlow(("FS32_FSINOF: cbData=%u < sizeof(FSALLOCATE) -> ERROR_BUFFER_OVERFLOW\n", cbData));
1319 return ERROR_BUFFER_OVERFLOW;
1320
1321 case FSIL_VOLSER:
1322 if (cbData >= sizeof(FSINFO))
1323 break;
1324 LogFlow(("FS32_FSINOF: cbData=%u < sizeof(FSINFO) -> ERROR_BUFFER_OVERFLOW\n", cbData));
1325 return ERROR_BUFFER_OVERFLOW;
1326
1327 default:
1328 LogRel(("FS32_FSINFO: Unsupported info level %u!\n", uLevel));
1329 return ERROR_INVALID_LEVEL;
1330 }
1331
1332 /* Work buffer union to keep it to a single allocation and no stack. */
1333 union FsInfoBufs
1334 {
1335 struct
1336 {
1337 VBOXSFCREATEREQ Req;
1338 uint8_t PathStringSpace[4 * sizeof(RTUTF16)];
1339 } Open;
1340 struct
1341 {
1342 VBOXSFVOLINFOREQ Req;
1343 union
1344 {
1345 FSALLOCATE Alloc;
1346 FSINFO FsInfo;
1347 };
1348 } Info;
1349 VBOXSFCLOSEREQ Close;
1350 } *pu = (union FsInfoBufs *)VbglR0PhysHeapAlloc(sizeof(*pu));
1351 if (!pu)
1352 return ERROR_NOT_ENOUGH_MEMORY;
1353
1354 /*
1355 * To get the info we need to open the root of the folder.
1356 */
1357 RT_ZERO(pu->Open.Req);
1358 pu->Open.Req.CreateParms.CreateFlags = SHFL_CF_DIRECTORY | SHFL_CF_ACT_FAIL_IF_NEW | SHFL_CF_ACT_OPEN_IF_EXISTS
1359 | SHFL_CF_ACCESS_READ | SHFL_CF_ACCESS_ATTR_READ | SHFL_CF_ACCESS_DENYNONE;
1360 pu->Open.Req.StrPath.u16Size = 3 * sizeof(RTUTF16);
1361 pu->Open.Req.StrPath.u16Length = 2 * sizeof(RTUTF16);
1362 pu->Open.Req.StrPath.String.utf16[0] = '\\';
1363 pu->Open.Req.StrPath.String.utf16[1] = '.';
1364 pu->Open.Req.StrPath.String.utf16[2] = '\0';
1365
1366 int vrc = VbglR0SfHostReqCreate(pFolder->idHostRoot, &pu->Open.Req);
1367 LogFlow(("FS32_FSINFO: VbglR0SfHostReqCreate -> %Rrc Result=%d Handle=%#RX64\n",
1368 vrc, pu->Open.Req.CreateParms.Result, pu->Open.Req.CreateParms.Handle));
1369 if ( RT_SUCCESS(vrc)
1370 && pu->Open.Req.CreateParms.Handle != SHFL_HANDLE_NIL)
1371 {
1372 SHFLHANDLE volatile hHandle = pu->Open.Req.CreateParms.Handle;
1373
1374 RT_ZERO(pu->Info.Req);
1375 vrc = VbglR0SfHostReqQueryVolInfo(pFolder->idHostRoot, &pu->Info.Req, hHandle);
1376 if (RT_SUCCESS(vrc))
1377 {
1378 /*
1379 * Construct and copy out the requested info.
1380 */
1381 if (uLevel == FSIL_ALLOC)
1382 {
1383 pu->Info.Alloc.idFileSystem = 0; /* unknown */
1384 uint32_t const cbSector = RT_MAX(pu->Info.Req.VolInfo.ulBytesPerSector, 1);
1385 pu->Info.Alloc.cSectorUnit = pu->Info.Req.VolInfo.ulBytesPerAllocationUnit / cbSector;
1386 pu->Info.Alloc.cUnit = (uint32_t)(pu->Info.Req.VolInfo.ullTotalAllocationBytes / cbSector);
1387 pu->Info.Alloc.cUnitAvail = (uint32_t)(pu->Info.Req.VolInfo.ullAvailableAllocationBytes / cbSector);
1388 pu->Info.Alloc.cbSector = (uint16_t)pu->Info.Req.VolInfo.ulBytesPerSector;
1389 rc = KernCopyOut(pbData, &pu->Info.Alloc, sizeof(pu->Info.Alloc));
1390 }
1391 else
1392 {
1393 RT_ZERO(pu->Info.FsInfo);
1394 pu->Info.FsInfo.vol.cch = (uint8_t)RT_MIN(pFolder->cchName, sizeof(pu->Info.FsInfo.vol.szVolLabel) - 1);
1395 memcpy(pu->Info.FsInfo.vol.szVolLabel, pFolder->szName, pu->Info.FsInfo.vol.cch);
1396 *(uint32_t *)&pu->Info.FsInfo.fdateCreation = pu->Info.Req.VolInfo.ulSerial;
1397 rc = KernCopyOut(pbData, &pu->Info.FsInfo, sizeof(pu->Info.FsInfo));
1398 }
1399 }
1400 else
1401 {
1402 LogRel(("FS32_FSINFO: VbglR0SfHostReqQueryVolInfo failed: %Rrc\n", vrc));
1403 rc = ERROR_GEN_FAILURE;
1404 }
1405
1406 vrc = VbglR0SfHostReqClose(pFolder->idHostRoot, &pu->Close, hHandle);
1407 AssertRC(vrc);
1408 }
1409 else
1410 rc = ERROR_GEN_FAILURE;
1411
1412 VbglR0PhysHeapFree(pu);
1413 }
1414 /*
1415 * We don't allow setting anything.
1416 */
1417 else if (fFlags == INFO_SET)
1418 {
1419 LogRel(("FS32_FSINFO: Attempting to set volume info (uLevel=%u, cbData=%#x) -> ERROR_ACCESS_DENIED\n", uLevel, cbData));
1420 rc = ERROR_ACCESS_DENIED;
1421 }
1422 else
1423 {
1424 LogRel(("FS32_FSINFO: Unknown flags: %#x\n", fFlags));
1425 rc = ERROR_SYS_INTERNAL;
1426 }
1427
1428 LogFlow(("FS32_FSINFO: returns %#x\n", rc));
1429 return rc;
1430}
1431
1432
1433DECLASM(APIRET)
1434FS32_FSCTL(union argdat *pArgData, ULONG iArgType, ULONG uFunction,
1435 PVOID pvParm, USHORT cbParm, PUSHORT pcbParmIO,
1436 PVOID pvData, USHORT cbData, PUSHORT pcbDataIO)
1437{
1438 LogFlow(("FS32_FSCTL: pArgData=%p iArgType=%#x uFunction=%#x pvParam=%p cbParam=%#x pcbParmIO=%p pvData=%p cbData=%#x pcbDataIO=%p\n",
1439 pArgData, iArgType, uFunction, pvParm, cbParm, pcbParmIO, pvData, cbData, pcbDataIO));
1440 NOREF(pArgData); NOREF(iArgType); NOREF(uFunction); NOREF(pvParm); NOREF(cbParm); NOREF(pcbParmIO);
1441 NOREF(pvData); NOREF(cbData); NOREF(pcbDataIO);
1442 return ERROR_NOT_SUPPORTED;
1443}
1444
1445
1446DECLASM(APIRET)
1447FS32_PROCESSNAME(PSZ pszName)
1448{
1449 LogFlow(("FS32_PROCESSNAME: '%s'\n", pszName));
1450 NOREF(pszName);
1451 return NO_ERROR;
1452}
1453
1454
1455DECLASM(APIRET)
1456FS32_CHDIR(ULONG fFlags, PCDFSI pCdFsi, PVBOXSFCD pCdFsd, PCSZ pszDir, LONG offCurDirEnd)
1457{
1458 LogFlow(("FS32_CHDIR: fFlags=%#x pCdFsi=%p:{%#x,%s} pCdFsd=%p pszDir=%p:{%s} offCurDirEnd=%d\n",
1459 fFlags, pCdFsi, pCdFsi ? pCdFsi->cdi_hVPB : 0xffff, pCdFsi ? pCdFsi->cdi_curdir : "", pCdFsd, pszDir, pszDir, offCurDirEnd));
1460
1461 /*
1462 * We do not keep any information about open directory, just verify
1463 * them before they are CD'ed into and when asked to revalidate them.
1464 * If there were any path walking benefits, we could consider opening the
1465 * directory and keeping it open, but there isn't, so we don't do that.
1466 */
1467 APIRET rc = NO_ERROR;
1468 if ( fFlags == CD_EXPLICIT
1469 || fFlags == CD_VERIFY)
1470 {
1471 if (fFlags == CD_VERIFY)
1472 pszDir = pCdFsi->cdi_curdir;
1473
1474 PVBOXSFFOLDER pFolder;
1475 VBOXSFCREATEREQ *pReq;
1476 rc = vboxSfOs2ResolvePathEx(pszDir, pCdFsd, offCurDirEnd, RT_UOFFSETOF(VBOXSFCREATEREQ, StrPath),
1477 &pFolder, (void **)&pReq);
1478 if (rc == NO_ERROR)
1479 {
1480 pReq->CreateParms.CreateFlags = SHFL_CF_LOOKUP;
1481
1482 int vrc = VbglR0SfHostReqCreate(pFolder->idHostRoot, pReq);
1483 LogFlow(("FS32_CHDIR: VbglR0SfHostReqCreate -> %Rrc Result=%d fMode=%#x\n",
1484 vrc, pReq->CreateParms.Result, pReq->CreateParms.Info.Attr.fMode));
1485 if (RT_SUCCESS(vrc))
1486 {
1487 switch (pReq->CreateParms.Result)
1488 {
1489 case SHFL_FILE_EXISTS:
1490 if (RTFS_IS_DIRECTORY(pReq->CreateParms.Info.Attr.fMode))
1491 rc = NO_ERROR;
1492 else
1493 rc = ERROR_ACCESS_DENIED;
1494 break;
1495
1496 case SHFL_PATH_NOT_FOUND:
1497 rc = ERROR_PATH_NOT_FOUND;
1498 break;
1499
1500 default:
1501 case SHFL_FILE_NOT_FOUND:
1502 rc = ERROR_FILE_NOT_FOUND;
1503 break;
1504 }
1505 }
1506 else
1507 rc = vboxSfOs2ConvertStatusToOs2(vrc, ERROR_PATH_NOT_FOUND);
1508 }
1509
1510 VbglR0PhysHeapFree(pReq);
1511 vboxSfOs2ReleaseFolder(pFolder);
1512 }
1513 else if (fFlags == CD_FREE)
1514 {
1515 /* nothing to do here. */
1516 }
1517 else
1518 {
1519 LogRel(("FS32_CHDIR: Unexpected fFlags value: %#x\n", fFlags));
1520 rc = ERROR_NOT_SUPPORTED;
1521 }
1522
1523 LogFlow(("FS32_CHDIR: returns %u\n", rc));
1524 return rc;
1525}
1526
1527
1528DECLASM(APIRET)
1529FS32_MKDIR(PCDFSI pCdFsi, PVBOXSFCD pCdFsd, PCSZ pszDir, LONG offCurDirEnd, PEAOP pEaOp, ULONG fFlags)
1530{
1531 LogFlow(("FS32_MKDIR: pCdFsi=%p pCdFsd=%p pszDir=%p:{%s} pEAOp=%p fFlags=%#x\n", pCdFsi, pCdFsd, pszDir, pszDir, offCurDirEnd, pEaOp, fFlags));
1532 RT_NOREF(fFlags);
1533
1534 /*
1535 * We don't do EAs.
1536 */
1537 APIRET rc;
1538 if (pEaOp == NULL)
1539 rc = NO_ERROR;
1540 else
1541 rc = vboxSfOs2CheckEaOpForCreation(pEaOp);
1542 if (rc == NO_ERROR)
1543 {
1544 /*
1545 * Resolve the path.
1546 */
1547 PVBOXSFFOLDER pFolder;
1548 VBOXSFCREATEREQ *pReq;
1549 rc = vboxSfOs2ResolvePathEx(pszDir, pCdFsd, offCurDirEnd, RT_UOFFSETOF(VBOXSFCREATEREQ, StrPath),
1550 &pFolder, (void **)&pReq);
1551 if (rc == NO_ERROR)
1552 {
1553 /*
1554 * The silly interface for creating directories amounts an open call that
1555 * fails if it exists and we get a file handle back that needs closing. Sigh.
1556 */
1557 pReq->CreateParms.CreateFlags = SHFL_CF_DIRECTORY | SHFL_CF_ACT_CREATE_IF_NEW | SHFL_CF_ACT_FAIL_IF_EXISTS
1558 | SHFL_CF_ACCESS_READ | SHFL_CF_ACCESS_DENYNONE;
1559
1560 int vrc = VbglR0SfHostReqCreate(pFolder->idHostRoot, pReq);
1561 LogFlow(("FS32_MKDIR: VbglR0SfHostReqCreate -> %Rrc Result=%d fMode=%#x\n",
1562 vrc, pReq->CreateParms.Result, pReq->CreateParms.Info.Attr.fMode));
1563 if (RT_SUCCESS(vrc))
1564 {
1565 switch (pReq->CreateParms.Result)
1566 {
1567 case SHFL_FILE_CREATED:
1568 if (pReq->CreateParms.Handle != SHFL_HANDLE_NIL)
1569 {
1570 AssertCompile(RTASSERT_OFFSET_OF(VBOXSFCREATEREQ, CreateParms.Handle) > sizeof(VBOXSFCLOSEREQ)); /* no aliasing issues */
1571 vrc = VbglR0SfHostReqClose(pFolder->idHostRoot, (VBOXSFCLOSEREQ *)pReq, pReq->CreateParms.Handle);
1572 AssertRC(vrc);
1573 }
1574 rc = NO_ERROR;
1575 break;
1576
1577 case SHFL_FILE_EXISTS:
1578 rc = ERROR_ACCESS_DENIED;
1579 break;
1580
1581 case SHFL_PATH_NOT_FOUND:
1582 rc = ERROR_PATH_NOT_FOUND;
1583 break;
1584
1585 default:
1586 case SHFL_FILE_NOT_FOUND:
1587 rc = ERROR_FILE_NOT_FOUND;
1588 break;
1589 }
1590 }
1591 else if (vrc == VERR_ALREADY_EXISTS)
1592 rc = ERROR_ACCESS_DENIED;
1593 else
1594 rc = vboxSfOs2ConvertStatusToOs2(vrc, ERROR_FILE_NOT_FOUND);
1595
1596 VbglR0PhysHeapFree(pReq);
1597 vboxSfOs2ReleaseFolder(pFolder);
1598 }
1599 }
1600 else
1601 Log(("FS32_MKDIR: EA trouble %p: %u%s\n", pEaOp, rc, rc == ERROR_EAS_NOT_SUPPORTED ? " (ERROR_EAS_NOT_SUPPORTED)" : ""));
1602
1603 RT_NOREF_PV(pCdFsi);
1604 LogFlow(("FS32_MMDIR: returns %u\n", rc));
1605 return rc;
1606}
1607
1608
1609DECLASM(APIRET)
1610FS32_RMDIR(PCDFSI pCdFsi, PVBOXSFCD pCdFsd, PCSZ pszDir, LONG offCurDirEnd)
1611{
1612 LogFlow(("FS32_RMDIR: pCdFsi=%p pCdFsd=%p pszDir=%p:{%s} offCurDirEnd=%d\n", pCdFsi, pCdFsd, pszDir, pszDir, offCurDirEnd));
1613
1614 /*
1615 * Resolve the path.
1616 */
1617 PVBOXSFFOLDER pFolder;
1618 VBOXSFREMOVEREQ *pReq;
1619 APIRET rc = vboxSfOs2ResolvePathEx(pszDir, pCdFsd, offCurDirEnd, RT_UOFFSETOF(VBOXSFREMOVEREQ, StrPath),
1620 &pFolder, (void **)&pReq);
1621 if (rc == NO_ERROR)
1622 {
1623 int vrc = VbglR0SfHostReqRemove(pFolder->idHostRoot, pReq, SHFL_REMOVE_DIR);
1624 LogFlow(("FS32_RMDIR: VbglR0SfHostReqRemove -> %Rrc\n", vrc));
1625 if (RT_SUCCESS(vrc))
1626 rc = NO_ERROR;
1627 else
1628 rc = vboxSfOs2ConvertStatusToOs2(vrc, ERROR_ACCESS_DENIED);
1629
1630 VbglR0PhysHeapFree(pReq);
1631 vboxSfOs2ReleaseFolder(pFolder);
1632 }
1633
1634 RT_NOREF_PV(pCdFsi);
1635 LogFlow(("FS32_RMDIR: returns %u\n", rc));
1636 return rc;
1637}
1638
1639
1640DECLASM(APIRET)
1641FS32_COPY(ULONG fFlags, PCDFSI pCdFsi, PVBOXSFCD pCdFsd, PCSZ pszSrc, LONG offSrcCurDirEnd,
1642 PCSZ pszDst, LONG offDstCurDirEnd, ULONG uNameType)
1643{
1644 LogFlow(("FS32_COPY: fFlags=%#x pCdFsi=%p pCdFsd=%p pszSrc=%p:{%s} offSrcCurDirEnd=%d pszDst=%p:{%s} offDstCurDirEnd=%d uNameType=%#x\n",
1645 fFlags, pCdFsi, pCdFsd, pszSrc, pszSrc, offSrcCurDirEnd, pszDst, pszDst, offDstCurDirEnd, uNameType));
1646 NOREF(fFlags); NOREF(pCdFsi); NOREF(pCdFsd); NOREF(pszSrc); NOREF(offSrcCurDirEnd);
1647 NOREF(pszDst); NOREF(offDstCurDirEnd); NOREF(uNameType);
1648
1649 /* Let DOSCALL1.DLL do the work for us till we get a host side function for doing this. */
1650 return ERROR_CANNOT_COPY;
1651}
1652
1653
1654DECLASM(APIRET)
1655FS32_MOVE(PCDFSI pCdFsi, PVBOXSFCD pCdFsd, PCSZ pszSrc, LONG offSrcCurDirEnd, PCSZ pszDst, LONG offDstCurDirEnd, ULONG uNameType)
1656{
1657 LogFlow(("FS32_MOVE: pCdFsi=%p pCdFsd=%p pszSrc=%p:{%s} offSrcCurDirEnd=%d pszDst=%p:{%s} offDstcurDirEnd=%d uNameType=%#x\n",
1658 pCdFsi, pCdFsd, pszSrc, pszSrc, offSrcCurDirEnd, pszDst, pszDst, offDstCurDirEnd, uNameType));
1659
1660 /*
1661 * Resolve the source and destination paths and check that they
1662 * refer to the same folder.
1663 */
1664 PVBOXSFFOLDER pSrcFolder;
1665 PSHFLSTRING pSrcFolderPath;
1666 APIRET rc = vboxSfOs2ResolvePath(pszSrc, pCdFsd, offSrcCurDirEnd, &pSrcFolder, &pSrcFolderPath);
1667 if (rc == NO_ERROR)
1668 {
1669 PVBOXSFFOLDER pDstFolder;
1670 VBOXSFRENAMEWITHSRCBUFREQ *pReq;
1671 rc = vboxSfOs2ResolvePathEx(pszDst, pCdFsd, offDstCurDirEnd, RT_UOFFSETOF(VBOXSFRENAMEWITHSRCBUFREQ, StrDstPath),
1672 &pDstFolder, (void **)&pReq);
1673 if (rc == NO_ERROR)
1674 {
1675 if (pSrcFolder == pDstFolder)
1676 {
1677 /*
1678 * Do the renaming.
1679 * Note! Requires 6.0.0beta2+ or 5.2.24+ host for renaming files.
1680 */
1681 int vrc = VbglR0SfHostReqRenameWithSrcBuf(pSrcFolder->idHostRoot, pReq, pSrcFolderPath,
1682 SHFL_RENAME_FILE | SHFL_RENAME_DIR);
1683 if (RT_SUCCESS(vrc))
1684 rc = NO_ERROR;
1685 else
1686 {
1687 Log(("FS32_MOVE: VbglR0SfHostReqRenameWithSrcBuf failed: %Rrc\n", rc));
1688 rc = vboxSfOs2ConvertStatusToOs2(vrc, ERROR_ACCESS_DENIED);
1689 }
1690 }
1691 else
1692 {
1693 Log(("FS32_MOVE: source folder '%s' != destiation folder '%s'\n", pszSrc, pszDst));
1694 rc = ERROR_NOT_SAME_DEVICE;
1695 }
1696 VbglR0PhysHeapFree(pReq);
1697 vboxSfOs2ReleaseFolder(pDstFolder);
1698 }
1699 vboxSfOs2ReleasePathAndFolder(pSrcFolderPath, pSrcFolder);
1700 }
1701
1702 RT_NOREF_PV(pCdFsi); RT_NOREF_PV(uNameType);
1703 return rc;
1704}
1705
1706
1707DECLASM(APIRET)
1708FS32_DELETE(PCDFSI pCdFsi, PVBOXSFCD pCdFsd, PCSZ pszFile, LONG offCurDirEnd)
1709{
1710 LogFlow(("FS32_DELETE: pCdFsi=%p pCdFsd=%p pszFile=%p:{%s} offCurDirEnd=%d\n", pCdFsi, pCdFsd, pszFile, pszFile, offCurDirEnd));
1711
1712 /*
1713 * Resolve the path.
1714 */
1715 PVBOXSFFOLDER pFolder;
1716 VBOXSFREMOVEREQ *pReq;
1717 APIRET rc = vboxSfOs2ResolvePathEx(pszFile, pCdFsd, offCurDirEnd, RT_UOFFSETOF(VBOXSFREMOVEREQ, StrPath),
1718 &pFolder, (void **)&pReq);
1719 if (rc == NO_ERROR)
1720 {
1721 int vrc = VbglR0SfHostReqRemove(pFolder->idHostRoot, pReq, SHFL_REMOVE_FILE);
1722 LogFlow(("FS32_DELETE: VbglR0SfHostReqRemove -> %Rrc\n", vrc));
1723 if (RT_SUCCESS(vrc))
1724 rc = NO_ERROR;
1725 else
1726 rc = vboxSfOs2ConvertStatusToOs2(vrc, ERROR_ACCESS_DENIED);
1727
1728 VbglR0PhysHeapFree(pReq);
1729 vboxSfOs2ReleaseFolder(pFolder);
1730 }
1731
1732 RT_NOREF_PV(pCdFsi);
1733 LogFlow(("FS32_DELETE: returns %u\n", rc));
1734 return rc;
1735}
1736
1737
1738
1739/**
1740 * Worker for FS32_PATHINFO that handles file stat setting.
1741 *
1742 * @returns OS/2 status code
1743 * @param pFolder The folder.
1744 * @param hHostFile The host file handle.
1745 * @param fAttribs The attributes to set.
1746 * @param pTimestamps Pointer to the timestamps. NULL if none should be
1747 * modified.
1748 * @param pObjInfoBuf Buffer to use when setting the attributes (host
1749 * will return current info upon successful
1750 * return). This must life on the phys heap.
1751 * @param offObjInfoInAlloc Offset of pObjInfoBuf in the phys heap
1752 * allocation where it lives.
1753 */
1754APIRET vboxSfOs2SetInfoCommonWorker(PVBOXSFFOLDER pFolder, SHFLHANDLE hHostFile, ULONG fAttribs,
1755 PFILESTATUS pTimestamps, PSHFLFSOBJINFO pObjInfoBuf, uint32_t offObjInfoInAlloc)
1756{
1757 /*
1758 * Validate the data a little and convert it to host speak.
1759 * When the date part is zero, the timestamp should not be updated.
1760 */
1761 RT_ZERO(*pObjInfoBuf);
1762 uint16_t cDelta = vboxSfOs2GetLocalTimeDelta();
1763
1764 /** @todo should we validate attributes? */
1765 pObjInfoBuf->Attr.fMode = (fAttribs << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_OS2;
1766 if (pObjInfoBuf->Attr.fMode == 0)
1767 pObjInfoBuf->Attr.fMode |= RTFS_DOS_NT_NORMAL;
1768
1769 if (pTimestamps)
1770 {
1771 if ( *(uint16_t *)&pTimestamps->fdateCreation != 0
1772 && !vboxSfOs2DateTimeToTimeSpec(pTimestamps->fdateCreation, pTimestamps->ftimeCreation, cDelta, &pObjInfoBuf->BirthTime))
1773 {
1774 LogRel(("vboxSfOs2SetInfoCommonWorker: Bad creation timestamp: %u-%u-%u %u:%u:%u\n",
1775 pTimestamps->fdateCreation.year + 1980, pTimestamps->fdateCreation.month, pTimestamps->fdateCreation.day,
1776 pTimestamps->ftimeCreation.hours, pTimestamps->ftimeCreation.minutes, pTimestamps->ftimeCreation.twosecs * 2));
1777 return ERROR_INVALID_PARAMETER;
1778 }
1779 if ( *(uint16_t *)&pTimestamps->fdateLastAccess != 0
1780 && !vboxSfOs2DateTimeToTimeSpec(pTimestamps->fdateLastAccess, pTimestamps->ftimeLastAccess, cDelta, &pObjInfoBuf->AccessTime))
1781 {
1782 LogRel(("vboxSfOs2SetInfoCommonWorker: Bad last access timestamp: %u-%u-%u %u:%u:%u\n",
1783 pTimestamps->fdateLastAccess.year + 1980, pTimestamps->fdateLastAccess.month, pTimestamps->fdateLastAccess.day,
1784 pTimestamps->ftimeLastAccess.hours, pTimestamps->ftimeLastAccess.minutes, pTimestamps->ftimeLastAccess.twosecs * 2));
1785 return ERROR_INVALID_PARAMETER;
1786 }
1787 if ( *(uint16_t *)&pTimestamps->fdateLastWrite != 0
1788 && !vboxSfOs2DateTimeToTimeSpec(pTimestamps->fdateLastWrite, pTimestamps->ftimeLastWrite, cDelta, &pObjInfoBuf->ModificationTime))
1789 {
1790 LogRel(("vboxSfOs2SetInfoCommonWorker: Bad last access timestamp: %u-%u-%u %u:%u:%u\n",
1791 pTimestamps->fdateLastWrite.year + 1980, pTimestamps->fdateLastWrite.month, pTimestamps->fdateLastWrite.day,
1792 pTimestamps->ftimeLastWrite.hours, pTimestamps->ftimeLastWrite.minutes, pTimestamps->ftimeLastWrite.twosecs * 2));
1793 return ERROR_INVALID_PARAMETER;
1794 }
1795 }
1796
1797 /*
1798 * Call the host to do the updating.
1799 */
1800 VBOXSFOBJINFOWITHBUFREQ *pReq = (VBOXSFOBJINFOWITHBUFREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
1801 if (pReq)
1802 {
1803 int vrc = VbglR0SfHostReqSetObjInfoWithBuf(pFolder->idHostRoot, pReq, hHostFile, pObjInfoBuf, offObjInfoInAlloc);
1804 LogFlow(("vboxSfOs2SetFileInfo: VbglR0SfHostReqSetObjInfoWithBuf -> %Rrc\n", vrc));
1805
1806 VbglR0PhysHeapFree(pReq);
1807 if (RT_SUCCESS(vrc))
1808 return NO_ERROR;
1809 return vboxSfOs2ConvertStatusToOs2(vrc, ERROR_ACCESS_DENIED);
1810 }
1811 return ERROR_NOT_ENOUGH_MEMORY;
1812}
1813
1814
1815/**
1816 * Worker for FS32_FILEATTRIBUTE and FS32_PATHINFO that handles setting stuff.
1817 *
1818 * @returns OS/2 status code.
1819 * @param pFolder The folder.
1820 * @param pReq Open/create request buffer with path.
1821 * @param fAttribs New file attributes.
1822 * @param pTimestamps New timestamps. May be NULL.
1823 */
1824static APIRET vboxSfOs2SetPathInfoWorker(PVBOXSFFOLDER pFolder, VBOXSFCREATEREQ *pReq, ULONG fAttribs, PFILESTATUS pTimestamps)
1825
1826{
1827 /*
1828 * In order to do anything we need to open the object.
1829 */
1830 APIRET rc;
1831 pReq->CreateParms.CreateFlags = SHFL_CF_ACT_OPEN_IF_EXISTS | SHFL_CF_ACT_FAIL_IF_NEW
1832 | SHFL_CF_ACCESS_ATTR_READWRITE | SHFL_CF_ACCESS_DENYNONE | SHFL_CF_ACCESS_NONE;
1833
1834 int vrc = VbglR0SfHostReqCreate(pFolder->idHostRoot, pReq);
1835 LogFlow(("vboxSfOs2SetPathInfoWorker: VbglR0SfHostReqCreate -> %Rrc Result=%d Handle=%#RX64 fMode=%#x\n",
1836 vrc, pReq->CreateParms.Result, pReq->CreateParms.Handle, pReq->CreateParms.Info.Attr.fMode));
1837 if ( vrc == VERR_IS_A_DIRECTORY
1838 || ( RT_SUCCESS(vrc)
1839 && pReq->CreateParms.Handle == SHFL_HANDLE_NIL
1840 && RTFS_IS_DIRECTORY(pReq->CreateParms.Info.Attr.fMode)))
1841 {
1842 RT_ZERO(pReq->CreateParms);
1843 pReq->CreateParms.CreateFlags = SHFL_CF_DIRECTORY | SHFL_CF_ACT_OPEN_IF_EXISTS | SHFL_CF_ACT_FAIL_IF_NEW
1844 | SHFL_CF_ACCESS_ATTR_READWRITE | SHFL_CF_ACCESS_DENYNONE | SHFL_CF_ACCESS_NONE;
1845 vrc = VbglR0SfHostReqCreate(pFolder->idHostRoot, pReq);
1846 LogFlow(("vboxSfOs2SetPathInfoWorker: VbglR0SfHostReqCreate#2 -> %Rrc Result=%d Handle=%#RX64 fMode=%#x\n",
1847 vrc, pReq->CreateParms.Result, pReq->CreateParms.Handle, pReq->CreateParms.Info.Attr.fMode));
1848 }
1849 if (RT_SUCCESS(vrc))
1850 {
1851 switch (pReq->CreateParms.Result)
1852 {
1853 case SHFL_FILE_EXISTS:
1854 if (pReq->CreateParms.Handle != SHFL_HANDLE_NIL)
1855 {
1856 /*
1857 * Join up with FS32_FILEINFO to do the actual setting.
1858 */
1859 rc = vboxSfOs2SetInfoCommonWorker(pFolder, pReq->CreateParms.Handle, fAttribs, pTimestamps,
1860 &pReq->CreateParms.Info, RT_UOFFSETOF(VBOXSFCREATEREQ, CreateParms.Info));
1861
1862 AssertCompile(RTASSERT_OFFSET_OF(VBOXSFCREATEREQ, CreateParms.Handle) > sizeof(VBOXSFCLOSEREQ)); /* no aliasing issues */
1863 vrc = VbglR0SfHostReqClose(pFolder->idHostRoot, (VBOXSFCLOSEREQ *)pReq, pReq->CreateParms.Handle);
1864 AssertRC(vrc);
1865 }
1866 else
1867 {
1868 LogRel(("vboxSfOs2SetPathInfoWorker: No handle! fMode=%#x\n", pReq->CreateParms.Info.Attr.fMode));
1869 rc = ERROR_SYS_INTERNAL;
1870 }
1871 break;
1872
1873 case SHFL_PATH_NOT_FOUND:
1874 rc = ERROR_PATH_NOT_FOUND;
1875 break;
1876
1877 default:
1878 case SHFL_FILE_NOT_FOUND:
1879 rc = ERROR_FILE_NOT_FOUND;
1880 break;
1881 }
1882 }
1883 else
1884 rc = vboxSfOs2ConvertStatusToOs2(vrc, ERROR_FILE_NOT_FOUND);
1885 return rc;
1886}
1887
1888
1889DECLASM(APIRET)
1890FS32_FILEATTRIBUTE(ULONG fFlags, PCDFSI pCdFsi, PVBOXSFCD pCdFsd, PCSZ pszName, LONG offCurDirEnd, PUSHORT pfAttr)
1891{
1892 LogFlow(("FS32_FILEATTRIBUTE: fFlags=%#x pCdFsi=%p:{%#x,%s} pCdFsd=%p pszName=%p:{%s} offCurDirEnd=%d pfAttr=%p={%#x}\n",
1893 fFlags, pCdFsi, pCdFsi->cdi_hVPB, pCdFsi->cdi_curdir, pCdFsd, pszName, pszName, offCurDirEnd, pfAttr, *pfAttr));
1894 RT_NOREF(pCdFsi, offCurDirEnd);
1895
1896 APIRET rc;
1897 if ( fFlags == FA_RETRIEVE
1898 || fFlags == FA_SET)
1899 {
1900 /* Both setting and querying needs to make a create request. */
1901 PVBOXSFFOLDER pFolder;
1902 VBOXSFCREATEREQ *pReq;
1903 rc = vboxSfOs2ResolvePathEx(pszName, pCdFsd, offCurDirEnd, RT_UOFFSETOF(VBOXSFCREATEREQ, StrPath),
1904 &pFolder, (void **)&pReq);
1905 if (rc == NO_ERROR)
1906 {
1907 if (fFlags == FA_RETRIEVE)
1908 {
1909 /*
1910 * Query it.
1911 */
1912 pReq->CreateParms.CreateFlags = SHFL_CF_LOOKUP;
1913
1914 int vrc = VbglR0SfHostReqCreate(pFolder->idHostRoot, pReq);
1915 LogFlow(("FS32_FILEATTRIBUTE: VbglR0SfHostReqCreate -> %Rrc Result=%d fMode=%#x\n",
1916 vrc, pReq->CreateParms.Result, pReq->CreateParms.Info.Attr.fMode));
1917 if (RT_SUCCESS(vrc))
1918 {
1919 switch (pReq->CreateParms.Result)
1920 {
1921 case SHFL_FILE_EXISTS:
1922 *pfAttr = (uint16_t)((pReq->CreateParms.Info.Attr.fMode & RTFS_DOS_MASK_OS2) >> RTFS_DOS_SHIFT);
1923 rc = NO_ERROR;
1924 break;
1925
1926 case SHFL_PATH_NOT_FOUND:
1927 rc = ERROR_PATH_NOT_FOUND;
1928 break;
1929
1930 default:
1931 case SHFL_FILE_NOT_FOUND:
1932 rc = ERROR_FILE_NOT_FOUND;
1933 break;
1934 }
1935 }
1936 else
1937 rc = vboxSfOs2ConvertStatusToOs2(vrc, ERROR_FILE_NOT_FOUND);
1938 }
1939 else
1940 {
1941 /*
1942 * Set the info. Join paths with FS32_PATHINFO.
1943 */
1944 rc = vboxSfOs2SetPathInfoWorker(pFolder, pReq, *pfAttr, NULL);
1945 }
1946 VbglR0PhysHeapFree(pReq);
1947 vboxSfOs2ReleaseFolder(pFolder);
1948 }
1949 }
1950 else
1951 {
1952 LogRel(("FS32_FILEATTRIBUTE: Unknwon flag value: %#x\n", fFlags));
1953 rc = ERROR_NOT_SUPPORTED;
1954 }
1955 LogFlow(("FS32_FILEATTRIBUTE: returns %u\n", rc));
1956 return rc;
1957}
1958
1959
1960/**
1961 * Creates an empty full EA list given a GEALIST and info level.
1962 *
1963 * @returns OS/2 status code.
1964 * @param pEaOp Kernel copy of the EA request with flattened pointers.
1965 * @param uLevel The info level being queried.
1966 * @param pcbWritten Where to return the length of the resulting list. Optional.
1967 * @param poffError User buffer address of EAOP.oError for reporting GEALIST issues.
1968 */
1969APIRET vboxSfOs2MakeEmptyEaListEx(PEAOP pEaOp, ULONG uLevel, uint32_t *pcbWritten, ULONG *poffError)
1970{
1971 ULONG cbDstList;
1972 APIRET rc;
1973
1974 /*
1975 * Levels 8 and 5 are simple.
1976 */
1977 if ( pEaOp->fpGEAList == NULL
1978 || uLevel == FI_LVL_EAS_FULL_8
1979 || uLevel == FI_LVL_EAS_FULL_5)
1980 {
1981 Log2(("vboxSfOs2MakeEmptyEaList: #1\n"));
1982 cbDstList = RT_UOFFSET_AFTER(FEALIST, cbList);
1983 rc = NO_ERROR;
1984 }
1985 /*
1986 * For levels 3 and 4 we have to do work when a request list is present.
1987 */
1988 else
1989 {
1990 ULONG cbGetEasLeft = 0;
1991 rc = KernCopyIn(&cbGetEasLeft, &pEaOp->fpGEAList->cbList, sizeof(pEaOp->fpGEAList->cbList));
1992 ULONG cbFullEasLeft = 0;
1993 if (rc == NO_ERROR)
1994 rc = KernCopyIn(&cbFullEasLeft, &pEaOp->fpFEAList->cbList, sizeof(cbFullEasLeft));
1995 if ( rc == NO_ERROR
1996 && cbGetEasLeft >= sizeof(pEaOp->fpGEAList->cbList)
1997 && cbFullEasLeft >= sizeof(pEaOp->fpFEAList->cbList))
1998 {
1999 cbGetEasLeft -= sizeof(pEaOp->fpGEAList->cbList);
2000 cbFullEasLeft -= sizeof(pEaOp->fpFEAList->cbList);
2001
2002 char *pszNameBuf = (char *)RTMemAlloc(256 + 1);
2003 if (!pszNameBuf)
2004 return ERROR_NOT_ENOUGH_MEMORY;
2005 /* Start of no-return zone. */
2006
2007 uint8_t const *pbSrc = (uint8_t const *)&pEaOp->fpGEAList->list[0]; /* user buffer! */
2008 uint8_t *pbDst = (uint8_t *)&pEaOp->fpFEAList->list[0]; /* user buffer! */
2009 Log2(("vboxSfOs2MakeEmptyEaList: %p LB %#x -> %p LB %#x...\n", pbSrc, cbGetEasLeft, pbDst, cbFullEasLeft));
2010 while (cbGetEasLeft > 0)
2011 {
2012 /*
2013 * pbSrc: GEA: BYTE cbName; char szName[];
2014 */
2015 /* Get name length. */
2016 uint8_t cbName = 0;
2017 rc = KernCopyIn(&cbName, pbSrc, sizeof(cbName));
2018 Log3(("vboxSfOs2MakeEmptyEaList: cbName=%#x rc=%u\n", cbName, rc));
2019 if (rc != NO_ERROR)
2020 break;
2021 pbSrc++;
2022 cbGetEasLeft--;
2023 if (cbName + 1U > cbGetEasLeft)
2024 {
2025 cbDstList = pbSrc - 1 - (uint8_t *)pEaOp->fpGEAList;
2026 rc = KernCopyOut(poffError, &cbDstList, sizeof(pEaOp->oError));
2027 if (rc == NO_ERROR)
2028 rc = ERROR_EA_LIST_INCONSISTENT;
2029 Log(("vboxSfOs2MakeEmptyEaList: ERROR_EA_LIST_INCONSISTENT\n"));
2030 break;
2031 }
2032
2033 /* Copy in name. */
2034 rc = KernCopyIn(pszNameBuf, pbSrc, cbName + 1);
2035 if (rc != NO_ERROR)
2036 break;
2037 Log3(("vboxSfOs2MakeEmptyEaList: szName: %.*Rhxs\n", cbName + 1, pszNameBuf));
2038 if ((char *)memchr(pszNameBuf, '\0', cbName) != &pszNameBuf[cbName])
2039 {
2040 cbDstList = pbSrc - 1 - (uint8_t *)pEaOp->fpGEAList;
2041 rc = KernCopyOut(poffError, &cbDstList, sizeof(pEaOp->oError));
2042 if (rc == NO_ERROR)
2043 rc = ERROR_INVALID_EA_NAME;
2044 Log(("vboxSfOs2MakeEmptyEaList: ERROR_INVALID_EA_NAME\n"));
2045 break;
2046 }
2047
2048 /* Skip input. */
2049 cbGetEasLeft -= cbName + 1;
2050 pbSrc += cbName + 1;
2051
2052 /*
2053 * Construct and emit output.
2054 * Note! We should technically skip duplicates here, but who cares...
2055 */
2056 if (cbName > 0)
2057 {
2058 FEA Result;
2059 if (sizeof(Result) + cbName + 1 > cbFullEasLeft)
2060 {
2061 Log(("vboxSfOs2MakeEmptyEaList: ERROR_BUFFER_OVERFLOW (%#x vs %#x)\n", sizeof(Result) + cbName + 1, cbFullEasLeft));
2062 rc = ERROR_BUFFER_OVERFLOW;
2063 break;
2064 }
2065 cbFullEasLeft -= sizeof(Result) + cbName + 1;
2066
2067 Result.fEA = 0;
2068 Result.cbName = cbName;
2069 Result.cbValue = 0;
2070 rc = KernCopyOut(pbDst, &Result, sizeof(Result));
2071 if (rc != NO_ERROR)
2072 break;
2073 pbDst += sizeof(Result);
2074
2075 rc = KernCopyOut(pbDst, pszNameBuf, cbName + 1);
2076 if (rc != NO_ERROR)
2077 break;
2078 pbDst += cbName + 1;
2079 }
2080 } /* (while more GEAs) */
2081
2082 /* End of no-return zone. */
2083 RTMemFree(pszNameBuf);
2084
2085 cbDstList = (uintptr_t)pbDst - (uintptr_t)pEaOp->fpFEAList;
2086 }
2087 else
2088 {
2089 if (rc == NO_ERROR)
2090 rc = ERROR_BUFFER_OVERFLOW;
2091 cbDstList = 0; /* oh, shut up. */
2092 }
2093
2094 }
2095
2096 /* Set the list length. */
2097 if (rc == NO_ERROR)
2098 rc = KernCopyOut(&pEaOp->fpFEAList->cbList, &cbDstList, sizeof(pEaOp->fpFEAList->cbList));
2099
2100 if (pcbWritten)
2101 *pcbWritten = cbDstList;
2102
2103 Log(("vboxSfOs2MakeEmptyEaList: return %u (cbDstList=%#x)\n", rc, cbDstList));
2104 return rc;
2105}
2106
2107
2108
2109/**
2110 * Creates an empty full EA list given a GEALIST and info level.
2111 *
2112 * @returns OS/2 status code.
2113 * @param pEaOp The EA request. User buffer.
2114 * @param uLevel The info level being queried.
2115 */
2116DECL_NO_INLINE(RT_NOTHING, APIRET)
2117vboxSfOs2MakeEmptyEaList(PEAOP pEaOp, ULONG uLevel)
2118{
2119 /*
2120 * Copy the user request into memory, do pointer conversion, and
2121 * join extended function version.
2122 */
2123 EAOP EaOp = { NULL, NULL, 0 };
2124 APIRET rc = KernCopyIn(&EaOp, pEaOp, sizeof(EaOp));
2125 if (rc == NO_ERROR)
2126 {
2127 Log2(("vboxSfOs2MakeEmptyEaList: #0: %p %p %#x\n", EaOp.fpGEAList, EaOp.fpFEAList, EaOp.oError));
2128 EaOp.fpFEAList = (PFEALIST)KernSelToFlat((uintptr_t)EaOp.fpFEAList);
2129 if ( uLevel != FI_LVL_EAS_FULL
2130 && uLevel != FI_LVL_EAS_FULL_5
2131 && uLevel != FI_LVL_EAS_FULL_8)
2132 EaOp.fpGEAList = (PGEALIST)KernSelToFlat((uintptr_t)EaOp.fpGEAList);
2133 else
2134 EaOp.fpGEAList = NULL;
2135 Log2(("vboxSfOs2MakeEmptyEaList: #0b: %p %p\n", EaOp.fpGEAList, EaOp.fpFEAList));
2136
2137 rc = vboxSfOs2MakeEmptyEaListEx(&EaOp, uLevel, NULL, &pEaOp->oError);
2138 }
2139 return rc;
2140}
2141
2142
2143/**
2144 * Corrects the case of the given path.
2145 *
2146 * @returns OS/2 status code
2147 * @param pFolder The folder.
2148 * @param pReq Open/create request buffer with folder path.
2149 * @param pszPath The original path for figuring the drive letter or
2150 * UNC part of the path.
2151 * @param pbData Where to return the data (user address).
2152 * @param cbData The maximum amount of data we can return.
2153 */
2154static APIRET vboxSfOs2QueryCorrectCase(PVBOXSFFOLDER pFolder, VBOXSFCREATEREQ *pReq, const char *pszPath,
2155 PBYTE pbData, ULONG cbData)
2156{
2157 RT_NOREF(pFolder, pReq);
2158 APIRET rc;
2159 size_t cchPath = RTStrNLen(pszPath, CCHMAXPATH + 1);
2160 if (cchPath <= CCHMAXPATH)
2161 {
2162 if (cbData > cchPath)
2163 {
2164 /** @todo implement this properly on the host side! */
2165 rc = KernCopyOut(pbData, pszPath, cbData + 1);
2166 LogFlow(("vboxSfOs2QueryCorrectCase: returns %u\n", rc));
2167 }
2168 else
2169 {
2170 LogFlow(("vboxSfOs2QueryCorrectCase: returns %u (ERROR_INSUFFICIENT_BUFFER) - cchPath=%#x cbData=%#x\n",
2171 ERROR_INSUFFICIENT_BUFFER, cchPath, cbData));
2172 rc = ERROR_INSUFFICIENT_BUFFER;
2173 }
2174 }
2175 else
2176 {
2177 LogFlow(("vboxSfOs2QueryCorrectCase: returns %u (ERROR_FILENAME_EXCED_RANGE)\n", ERROR_FILENAME_EXCED_RANGE));
2178 rc = ERROR_FILENAME_EXCED_RANGE;
2179 }
2180 return rc;
2181}
2182
2183
2184/**
2185 * Copy out file status info.
2186 *
2187 * @returns OS/2 status code.
2188 * @param pbDst User address to put the status info at.
2189 * @param cbDst The size of the structure to produce.
2190 * @param uLevel The info level of the structure to produce.
2191 * @param pSrc The shared folder FS object info source structure.
2192 * @note Careful with stack, thus no-inlining.
2193 */
2194DECL_NO_INLINE(RT_NOTHING, APIRET)
2195vboxSfOs2FileStatusFromObjInfo(PBYTE pbDst, ULONG cbDst, ULONG uLevel, SHFLFSOBJINFO const *pSrc)
2196{
2197 union
2198 {
2199 FILESTATUS Fst;
2200 FILESTATUS2 Fst2;
2201 FILESTATUS3L Fst3L;
2202 FILESTATUS4L Fst4L;
2203 } uTmp;
2204
2205 int16_t cMinLocalTimeDelta = vboxSfOs2GetLocalTimeDelta();
2206 vboxSfOs2DateTimeFromTimeSpec(&uTmp.Fst.fdateCreation, &uTmp.Fst.ftimeCreation, pSrc->BirthTime, cMinLocalTimeDelta);
2207 vboxSfOs2DateTimeFromTimeSpec(&uTmp.Fst.fdateLastAccess, &uTmp.Fst.ftimeLastAccess, pSrc->AccessTime, cMinLocalTimeDelta);
2208 vboxSfOs2DateTimeFromTimeSpec(&uTmp.Fst.fdateLastWrite, &uTmp.Fst.ftimeLastWrite, pSrc->ModificationTime, cMinLocalTimeDelta);
2209 if (uLevel < FI_LVL_STANDARD_64)
2210 {
2211 uTmp.Fst.cbFile = (uint32_t)RT_MIN(pSrc->cbObject, UINT32_MAX);
2212 uTmp.Fst.cbFileAlloc = (uint32_t)RT_MIN(pSrc->cbAllocated, UINT32_MAX);
2213 uTmp.Fst.attrFile = (uint16_t)((pSrc->Attr.fMode & RTFS_DOS_MASK_OS2) >> RTFS_DOS_SHIFT);
2214 if (uLevel == FI_LVL_STANDARD_EASIZE)
2215 uTmp.Fst2.cbList = 0;
2216 }
2217 else
2218 {
2219 uTmp.Fst3L.cbFile = pSrc->cbObject;
2220 uTmp.Fst3L.cbFileAlloc = pSrc->cbAllocated;
2221 uTmp.Fst3L.attrFile = (pSrc->Attr.fMode & RTFS_DOS_MASK_OS2) >> RTFS_DOS_SHIFT;
2222 uTmp.Fst4L.cbList = 0;
2223 }
2224
2225 return KernCopyOut(pbDst, &uTmp, cbDst);
2226}
2227
2228
2229
2230/**
2231 * Worker for FS32_PATHINFO that handles file stat queries.
2232 *
2233 * @returns OS/2 status code
2234 * @param pFolder The folder.
2235 * @param pReq Open/create request buffer with folder path.
2236 * @param uLevel The information level.
2237 * @param pbData Where to return the data (user address).
2238 * @param cbData The amount of data to produce.
2239 */
2240static APIRET vboxSfOs2QueryPathInfo(PVBOXSFFOLDER pFolder, VBOXSFCREATEREQ *pReq, ULONG uLevel, PBYTE pbData, ULONG cbData)
2241{
2242 APIRET rc;
2243 pReq->CreateParms.CreateFlags = SHFL_CF_LOOKUP;
2244
2245 int vrc = VbglR0SfHostReqCreate(pFolder->idHostRoot, pReq);
2246 LogFlow(("FS32_PATHINFO: VbglR0SfHostReqCreate -> %Rrc Result=%d fMode=%#x\n",
2247 vrc, pReq->CreateParms.Result, pReq->CreateParms.Info.Attr.fMode));
2248 if (RT_SUCCESS(vrc))
2249 {
2250 switch (pReq->CreateParms.Result)
2251 {
2252 case SHFL_FILE_EXISTS:
2253 switch (uLevel)
2254 {
2255 /*
2256 * Produce the desired file stat data.
2257 */
2258 case FI_LVL_STANDARD:
2259 case FI_LVL_STANDARD_EASIZE:
2260 case FI_LVL_STANDARD_64:
2261 case FI_LVL_STANDARD_EASIZE_64:
2262 rc = vboxSfOs2FileStatusFromObjInfo(pbData, cbData, uLevel, &pReq->CreateParms.Info);
2263 break;
2264
2265 /*
2266 * We don't do EAs and we "just" need to return no-EAs.
2267 * However, that's not as easy as you might think.
2268 */
2269 case FI_LVL_EAS_FROM_LIST:
2270 case FI_LVL_EAS_FULL:
2271 case FI_LVL_EAS_FULL_5:
2272 case FI_LVL_EAS_FULL_8:
2273 rc = vboxSfOs2MakeEmptyEaList((PEAOP)pbData, uLevel);
2274 break;
2275
2276 default:
2277 AssertFailed();
2278 rc = ERROR_GEN_FAILURE;
2279 break;
2280 }
2281 break;
2282
2283 case SHFL_PATH_NOT_FOUND:
2284 rc = ERROR_PATH_NOT_FOUND;
2285 break;
2286
2287 default:
2288 case SHFL_FILE_NOT_FOUND:
2289 rc = ERROR_FILE_NOT_FOUND;
2290 break;
2291 }
2292 }
2293 else
2294 rc = vboxSfOs2ConvertStatusToOs2(vrc, ERROR_FILE_NOT_FOUND);
2295 return rc;
2296}
2297
2298
2299DECLASM(APIRET)
2300FS32_PATHINFO(USHORT fFlags, PCDFSI pCdFsi, PVBOXSFCD pCdFsd, PCSZ pszPath, LONG offCurDirEnd,
2301 ULONG uLevel, PBYTE pbData, ULONG cbData)
2302{
2303 LogFlow(("FS32_PATHINFO: fFlags=%#x pCdFsi=%p:{%#x,%s} pCdFsd=%p pszPath=%p:{%s} offCurDirEnd=%d uLevel=%u pbData=%p cbData=%#x\n",
2304 fFlags, pCdFsi, pCdFsi->cdi_hVPB, pCdFsi->cdi_curdir, pCdFsd, pszPath, pszPath, offCurDirEnd, uLevel, pbData, cbData));
2305
2306 /*
2307 * Check the level.
2308 *
2309 * Note! You would think this is FIL_STANDARD, FIL_QUERYEASIZE,
2310 * FIL_QUERYEASFROMLISTL and such. However, there are several levels
2311 * (4/14, 6/16, 7/17, 8/18) that are not defined in os2.h and then
2312 * there and FIL_QUERYFULLNAME that is used very between the kernel
2313 * and the FSD so the kernel can implement DosEnumAttributes.
2314 *
2315 * Note! DOSCALL1.DLL has code for converting FILESTATUS to FILESTATUS3
2316 * and FILESTATUS2 to FILESTATUS4 as needed. We don't need to do this.
2317 * It also has weird code for doubling the FILESTATUS2.cbList value
2318 * for no apparent reason.
2319 */
2320 ULONG cbMinData;
2321 switch (uLevel)
2322 {
2323 case FI_LVL_STANDARD:
2324 cbMinData = sizeof(FILESTATUS);
2325 AssertCompileSize(FILESTATUS, 0x16);
2326 break;
2327 case FI_LVL_STANDARD_64:
2328 cbMinData = sizeof(FILESTATUS3L);
2329 AssertCompileSize(FILESTATUS3L, 0x20); /* cbFile and cbFileAlloc are misaligned. */
2330 break;
2331 case FI_LVL_STANDARD_EASIZE:
2332 cbMinData = sizeof(FILESTATUS2);
2333 AssertCompileSize(FILESTATUS2, 0x1a);
2334 break;
2335 case FI_LVL_STANDARD_EASIZE_64:
2336 cbMinData = sizeof(FILESTATUS4L);
2337 AssertCompileSize(FILESTATUS4L, 0x24); /* cbFile and cbFileAlloc are misaligned. */
2338 break;
2339 case FI_LVL_EAS_FROM_LIST:
2340 case FI_LVL_EAS_FULL:
2341 case FI_LVL_EAS_FULL_5:
2342 case FI_LVL_EAS_FULL_8:
2343 cbMinData = sizeof(EAOP);
2344 break;
2345 case FI_LVL_VERIFY_PATH:
2346 case FI_LVL_CASE_CORRECT_PATH:
2347 cbMinData = 1;
2348 break;
2349 default:
2350 LogRel(("FS32_PATHINFO: Unsupported info level %u!\n", uLevel));
2351 return ERROR_INVALID_LEVEL;
2352 }
2353 if (cbData < cbMinData || pbData == NULL)
2354 {
2355 Log(("FS32_PATHINFO: ERROR_BUFFER_OVERFLOW (cbMinData=%#x, cbData=%#x, pszPath=%s)\n", cbMinData, cbData, pszPath));
2356 return ERROR_BUFFER_OVERFLOW;
2357 }
2358
2359 /*
2360 * Resolve the path to a folder and folder path.
2361 */
2362 PVBOXSFFOLDER pFolder;
2363 VBOXSFCREATEREQ *pReq;
2364 int rc = vboxSfOs2ResolvePathEx(pszPath, pCdFsd, offCurDirEnd, RT_UOFFSETOF(VBOXSFCREATEREQ, StrPath),
2365 &pFolder, (void **)&pReq);
2366 if (rc == NO_ERROR)
2367 {
2368 /*
2369 * Query information.
2370 */
2371 if (fFlags == PI_RETRIEVE)
2372 {
2373 if ( uLevel != FI_LVL_VERIFY_PATH
2374 && uLevel != FI_LVL_CASE_CORRECT_PATH)
2375 rc = vboxSfOs2QueryPathInfo(pFolder, pReq, uLevel, pbData, cbMinData);
2376 else if (uLevel == FI_LVL_VERIFY_PATH)
2377 rc = NO_ERROR; /* vboxSfOs2ResolvePath should've taken care of this already */
2378 else
2379 rc = vboxSfOs2QueryCorrectCase(pFolder, pReq, pszPath, pbData, cbData);
2380 }
2381 /*
2382 * Update information.
2383 */
2384 else if ( fFlags == PI_SET
2385 || fFlags == (PI_SET | PI_WRITE_THRU))
2386 {
2387 if ( uLevel == FI_LVL_STANDARD
2388 || uLevel == FI_LVL_STANDARD_64)
2389 {
2390 /* Read in the data and join paths with FS32_FILEATTRIBUTE: */
2391 PFILESTATUS pDataCopy = (PFILESTATUS)VbglR0PhysHeapAlloc(cbMinData);
2392 if (pDataCopy)
2393 {
2394 rc = KernCopyIn(pDataCopy, pbData, cbMinData);
2395 if (rc == NO_ERROR)
2396 rc = vboxSfOs2SetPathInfoWorker(pFolder, pReq,
2397 uLevel == FI_LVL_STANDARD
2398 ? (ULONG)pDataCopy->attrFile
2399 : ((PFILESTATUS3L)pDataCopy)->attrFile,
2400 (PFILESTATUS)pDataCopy);
2401 VbglR0PhysHeapFree(pDataCopy);
2402 }
2403 else
2404 rc = ERROR_NOT_ENOUGH_MEMORY;
2405 }
2406 else if (uLevel == FI_LVL_STANDARD_EASIZE)
2407 rc = ERROR_EAS_NOT_SUPPORTED;
2408 else
2409 rc = ERROR_INVALID_LEVEL;
2410 }
2411 else
2412 {
2413 LogRel(("FS32_PATHINFO: Unknown flags value: %#x (path: %s)\n", fFlags, pszPath));
2414 rc = ERROR_INVALID_PARAMETER;
2415 }
2416 VbglR0PhysHeapFree(pReq);
2417 vboxSfOs2ReleaseFolder(pFolder);
2418 }
2419 RT_NOREF_PV(pCdFsi);
2420 return rc;
2421}
2422
2423
2424DECLASM(APIRET)
2425FS32_MOUNT(USHORT fFlags, PVPFSI pvpfsi, PVBOXSFVP pVpFsd, USHORT hVPB, PCSZ pszBoot)
2426{
2427 NOREF(fFlags); NOREF(pvpfsi); NOREF(pVpFsd); NOREF(hVPB); NOREF(pszBoot);
2428 return ERROR_NOT_SUPPORTED;
2429}
2430
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