VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedFolders/vbsf.cpp@ 66092

Last change on this file since 66092 was 66038, checked in by vboxsync, 8 years ago

tstSharedFolderService: Added testcase for case-insensitive create-file.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 66.0 KB
Line 
1/* $Id: vbsf.cpp 66038 2017-03-10 15:18:45Z vboxsync $ */
2/** @file
3 * Shared Folders - VBox Shared Folders.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifdef UNITTEST
19# include "testcase/tstSharedFolderService.h"
20#endif
21
22#include "vbsfpath.h"
23#include "mappings.h"
24#include "vbsf.h"
25#include "shflhandle.h"
26
27#include <iprt/alloc.h>
28#include <iprt/assert.h>
29#include <iprt/asm.h>
30#include <iprt/fs.h>
31#include <iprt/dir.h>
32#include <iprt/file.h>
33#include <iprt/path.h>
34#include <iprt/string.h>
35#include <iprt/symlink.h>
36#include <iprt/uni.h>
37#include <iprt/stream.h>
38#ifdef RT_OS_DARWIN
39# include <Carbon/Carbon.h>
40#endif
41
42#ifdef UNITTEST
43# include "teststubs.h"
44#endif
45
46#define SHFL_RT_LINK(pClient) ((pClient)->fu32Flags & SHFL_CF_SYMLINKS ? RTPATH_F_ON_LINK : RTPATH_F_FOLLOW_LINK)
47
48/**
49 * @todo find a better solution for supporting the execute bit for non-windows
50 * guests on windows host. Search for "0111" to find all the relevant places.
51 */
52
53void vbsfStripLastComponent(char *pszFullPath, uint32_t cbFullPathRoot)
54{
55 RTUNICP cp;
56
57 /* Do not strip root. */
58 char *s = pszFullPath + cbFullPathRoot;
59 char *delimSecondLast = NULL;
60 char *delimLast = NULL;
61
62 LogFlowFunc(("%s -> %s\n", pszFullPath, s));
63
64 for (;;)
65 {
66 cp = RTStrGetCp(s);
67
68 if (cp == RTUNICP_INVALID || cp == 0)
69 {
70 break;
71 }
72
73 if (cp == RTPATH_DELIMITER)
74 {
75 if (delimLast != NULL)
76 {
77 delimSecondLast = delimLast;
78 }
79
80 delimLast = s;
81 }
82
83 s = RTStrNextCp(s);
84 }
85
86 if (cp == 0)
87 {
88 if (delimLast + 1 == s)
89 {
90 if (delimSecondLast)
91 {
92 *delimSecondLast = 0;
93 }
94 else if (delimLast)
95 {
96 *delimLast = 0;
97 }
98 }
99 else
100 {
101 if (delimLast)
102 {
103 *delimLast = 0;
104 }
105 }
106 }
107
108 LogFlowFunc(("%s, %s, %s\n", pszFullPath, delimLast, delimSecondLast));
109}
110
111static int vbsfBuildFullPath(SHFLCLIENTDATA *pClient, SHFLROOT root, PSHFLSTRING pPath,
112 uint32_t cbPath, char **ppszFullPath, uint32_t *pcbFullPathRoot,
113 bool fWildCard = false, bool fPreserveLastComponent = false)
114{
115 char *pszHostPath = NULL;
116 uint32_t fu32PathFlags = 0;
117 uint32_t fu32Options = VBSF_O_PATH_CHECK_ROOT_ESCAPE
118 | (fWildCard? VBSF_O_PATH_WILDCARD: 0)
119 | (fPreserveLastComponent? VBSF_O_PATH_PRESERVE_LAST_COMPONENT: 0);
120
121 int rc = vbsfPathGuestToHost(pClient, root, pPath, cbPath,
122 &pszHostPath, pcbFullPathRoot, fu32Options, &fu32PathFlags);
123 if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
124 {
125 LogRel2(("SharedFolders: GuestToHost 0x%RX32 [%.*s]->[%s] %Rrc\n", fu32PathFlags, pPath->u16Length, &pPath->String.utf8[0], pszHostPath, rc));
126 }
127 else
128 {
129 LogRel2(("SharedFolders: GuestToHost 0x%RX32 [%.*ls]->[%s] %Rrc\n", fu32PathFlags, pPath->u16Length / 2, &pPath->String.ucs2[0], pszHostPath, rc));
130 }
131
132 if (RT_SUCCESS(rc))
133 {
134 if (ppszFullPath)
135 *ppszFullPath = pszHostPath;
136 }
137 return rc;
138}
139
140static void vbsfFreeFullPath(char *pszFullPath)
141{
142 vbsfFreeHostPath(pszFullPath);
143}
144
145typedef enum VBSFCHECKACCESS
146{
147 VBSF_CHECK_ACCESS_READ = 0,
148 VBSF_CHECK_ACCESS_WRITE = 1
149} VBSFCHECKACCESS;
150
151/**
152 * Check if the handle data is valid and the operation is allowed on the shared folder.
153 *
154 * @returns IPRT status code
155 * @param pClient Data structure describing the client accessing the shared folder
156 * @param root The index of the shared folder in the table of mappings.
157 * @param pHandle Information about the file or directory object.
158 * @param enmCheckAccess Whether the operation needs read only or write access.
159 */
160static int vbsfCheckHandleAccess(SHFLCLIENTDATA *pClient, SHFLROOT root,
161 SHFLFILEHANDLE *pHandle, VBSFCHECKACCESS enmCheckAccess)
162{
163 /* Handle from the same 'root' index? */
164 if (RT_LIKELY(RT_VALID_PTR(pHandle) && root == pHandle->root))
165 { /* likely */ }
166 else
167 return VERR_INVALID_HANDLE;
168
169 /* Check if the guest is still allowed to access this share.
170 * vbsfMappingsQueryWritable returns error if the shared folder has been removed from the VM settings.
171 */
172 bool fWritable;
173 int rc = vbsfMappingsQueryWritable(pClient, root, &fWritable);
174 if (RT_SUCCESS(rc))
175 { /* likely */ }
176 else
177 return VERR_ACCESS_DENIED;
178
179 if (enmCheckAccess == VBSF_CHECK_ACCESS_WRITE)
180 {
181 /* Operation requires write access. Check if the shared folder is writable too. */
182 if (RT_LIKELY(fWritable))
183 { /* likely */ }
184 else
185 return VERR_WRITE_PROTECT;
186 }
187
188 return VINF_SUCCESS;
189}
190
191/**
192 * Convert shared folder create flags (see include/iprt/shflsvc.h) into iprt create flags.
193 *
194 * @returns iprt status code
195 * @param fWritable whether the shared folder is writable
196 * @param fShflFlags shared folder create flags
197 * @param fMode file attributes
198 * @param handleInitial initial handle
199 * @retval pfOpen iprt create flags
200 */
201static int vbsfConvertFileOpenFlags(bool fWritable, unsigned fShflFlags, RTFMODE fMode, SHFLHANDLE handleInitial, uint32_t *pfOpen)
202{
203 uint32_t fOpen = 0;
204 int rc = VINF_SUCCESS;
205
206 if ( (fMode & RTFS_DOS_MASK) != 0
207 && (fMode & RTFS_UNIX_MASK) == 0)
208 {
209 /* A DOS/Windows guest, make RTFS_UNIX_* from RTFS_DOS_*.
210 * @todo this is based on rtFsModeNormalize/rtFsModeFromDos.
211 * May be better to use RTFsModeNormalize here.
212 */
213 fMode |= RTFS_UNIX_IRUSR | RTFS_UNIX_IRGRP | RTFS_UNIX_IROTH;
214 /* x for directories. */
215 if (fMode & RTFS_DOS_DIRECTORY)
216 fMode |= RTFS_TYPE_DIRECTORY | RTFS_UNIX_IXUSR | RTFS_UNIX_IXGRP | RTFS_UNIX_IXOTH;
217 /* writable? */
218 if (!(fMode & RTFS_DOS_READONLY))
219 fMode |= RTFS_UNIX_IWUSR | RTFS_UNIX_IWGRP | RTFS_UNIX_IWOTH;
220
221 /* Set the requested mode using only allowed bits. */
222 fOpen |= ((fMode & RTFS_UNIX_MASK) << RTFILE_O_CREATE_MODE_SHIFT) & RTFILE_O_CREATE_MODE_MASK;
223 }
224 else
225 {
226 /* Old linux and solaris additions did not initialize the Info.Attr.fMode field
227 * and it contained random bits from stack. Detect this using the handle field value
228 * passed from the guest: old additions set it (incorrectly) to 0, new additions
229 * set it to SHFL_HANDLE_NIL(~0).
230 */
231 if (handleInitial == 0)
232 {
233 /* Old additions. Do nothing, use default mode. */
234 }
235 else
236 {
237 /* New additions or Windows additions. Set the requested mode using only allowed bits.
238 * Note: Windows guest set RTFS_UNIX_MASK bits to 0, which means a default mode
239 * will be set in fOpen.
240 */
241 fOpen |= ((fMode & RTFS_UNIX_MASK) << RTFILE_O_CREATE_MODE_SHIFT) & RTFILE_O_CREATE_MODE_MASK;
242 }
243 }
244
245 switch (BIT_FLAG(fShflFlags, SHFL_CF_ACCESS_MASK_RW))
246 {
247 default:
248 case SHFL_CF_ACCESS_NONE:
249 {
250#ifdef RT_OS_WINDOWS
251 if (BIT_FLAG(fShflFlags, SHFL_CF_ACCESS_MASK_ATTR) != SHFL_CF_ACCESS_ATTR_NONE)
252 fOpen |= RTFILE_O_ATTR_ONLY;
253 else
254#endif
255 fOpen |= RTFILE_O_READ;
256 Log(("FLAG: SHFL_CF_ACCESS_NONE\n"));
257 break;
258 }
259
260 case SHFL_CF_ACCESS_READ:
261 {
262 fOpen |= RTFILE_O_READ;
263 Log(("FLAG: SHFL_CF_ACCESS_READ\n"));
264 break;
265 }
266
267 case SHFL_CF_ACCESS_WRITE:
268 {
269 fOpen |= RTFILE_O_WRITE;
270 Log(("FLAG: SHFL_CF_ACCESS_WRITE\n"));
271 break;
272 }
273
274 case SHFL_CF_ACCESS_READWRITE:
275 {
276 fOpen |= RTFILE_O_READWRITE;
277 Log(("FLAG: SHFL_CF_ACCESS_READWRITE\n"));
278 break;
279 }
280 }
281
282 if (fShflFlags & SHFL_CF_ACCESS_APPEND)
283 {
284 fOpen |= RTFILE_O_APPEND;
285 }
286
287 switch (BIT_FLAG(fShflFlags, SHFL_CF_ACCESS_MASK_ATTR))
288 {
289 default:
290 case SHFL_CF_ACCESS_ATTR_NONE:
291 {
292 fOpen |= RTFILE_O_ACCESS_ATTR_DEFAULT;
293 Log(("FLAG: SHFL_CF_ACCESS_ATTR_NONE\n"));
294 break;
295 }
296
297 case SHFL_CF_ACCESS_ATTR_READ:
298 {
299 fOpen |= RTFILE_O_ACCESS_ATTR_READ;
300 Log(("FLAG: SHFL_CF_ACCESS_ATTR_READ\n"));
301 break;
302 }
303
304 case SHFL_CF_ACCESS_ATTR_WRITE:
305 {
306 fOpen |= RTFILE_O_ACCESS_ATTR_WRITE;
307 Log(("FLAG: SHFL_CF_ACCESS_ATTR_WRITE\n"));
308 break;
309 }
310
311 case SHFL_CF_ACCESS_ATTR_READWRITE:
312 {
313 fOpen |= RTFILE_O_ACCESS_ATTR_READWRITE;
314 Log(("FLAG: SHFL_CF_ACCESS_ATTR_READWRITE\n"));
315 break;
316 }
317 }
318
319 /* Sharing mask */
320 switch (BIT_FLAG(fShflFlags, SHFL_CF_ACCESS_MASK_DENY))
321 {
322 default:
323 case SHFL_CF_ACCESS_DENYNONE:
324 fOpen |= RTFILE_O_DENY_NONE;
325 Log(("FLAG: SHFL_CF_ACCESS_DENYNONE\n"));
326 break;
327
328 case SHFL_CF_ACCESS_DENYREAD:
329 fOpen |= RTFILE_O_DENY_READ;
330 Log(("FLAG: SHFL_CF_ACCESS_DENYREAD\n"));
331 break;
332
333 case SHFL_CF_ACCESS_DENYWRITE:
334 fOpen |= RTFILE_O_DENY_WRITE;
335 Log(("FLAG: SHFL_CF_ACCESS_DENYWRITE\n"));
336 break;
337
338 case SHFL_CF_ACCESS_DENYALL:
339 fOpen |= RTFILE_O_DENY_ALL;
340 Log(("FLAG: SHFL_CF_ACCESS_DENYALL\n"));
341 break;
342 }
343
344 /* Open/Create action mask */
345 switch (BIT_FLAG(fShflFlags, SHFL_CF_ACT_MASK_IF_EXISTS))
346 {
347 case SHFL_CF_ACT_OPEN_IF_EXISTS:
348 if (SHFL_CF_ACT_CREATE_IF_NEW == BIT_FLAG(fShflFlags, SHFL_CF_ACT_MASK_IF_NEW))
349 {
350 fOpen |= RTFILE_O_OPEN_CREATE;
351 Log(("FLAGS: SHFL_CF_ACT_OPEN_IF_EXISTS and SHFL_CF_ACT_CREATE_IF_NEW\n"));
352 }
353 else if (SHFL_CF_ACT_FAIL_IF_NEW == BIT_FLAG(fShflFlags, SHFL_CF_ACT_MASK_IF_NEW))
354 {
355 fOpen |= RTFILE_O_OPEN;
356 Log(("FLAGS: SHFL_CF_ACT_OPEN_IF_EXISTS and SHFL_CF_ACT_FAIL_IF_NEW\n"));
357 }
358 else
359 {
360 Log(("FLAGS: invalid open/create action combination\n"));
361 rc = VERR_INVALID_PARAMETER;
362 }
363 break;
364 case SHFL_CF_ACT_FAIL_IF_EXISTS:
365 if (SHFL_CF_ACT_CREATE_IF_NEW == BIT_FLAG(fShflFlags, SHFL_CF_ACT_MASK_IF_NEW))
366 {
367 fOpen |= RTFILE_O_CREATE;
368 Log(("FLAGS: SHFL_CF_ACT_FAIL_IF_EXISTS and SHFL_CF_ACT_CREATE_IF_NEW\n"));
369 }
370 else
371 {
372 Log(("FLAGS: invalid open/create action combination\n"));
373 rc = VERR_INVALID_PARAMETER;
374 }
375 break;
376 case SHFL_CF_ACT_REPLACE_IF_EXISTS:
377 if (SHFL_CF_ACT_CREATE_IF_NEW == BIT_FLAG(fShflFlags, SHFL_CF_ACT_MASK_IF_NEW))
378 {
379 fOpen |= RTFILE_O_CREATE_REPLACE;
380 Log(("FLAGS: SHFL_CF_ACT_REPLACE_IF_EXISTS and SHFL_CF_ACT_CREATE_IF_NEW\n"));
381 }
382 else if (SHFL_CF_ACT_FAIL_IF_NEW == BIT_FLAG(fShflFlags, SHFL_CF_ACT_MASK_IF_NEW))
383 {
384 fOpen |= RTFILE_O_OPEN | RTFILE_O_TRUNCATE;
385 Log(("FLAGS: SHFL_CF_ACT_REPLACE_IF_EXISTS and SHFL_CF_ACT_FAIL_IF_NEW\n"));
386 }
387 else
388 {
389 Log(("FLAGS: invalid open/create action combination\n"));
390 rc = VERR_INVALID_PARAMETER;
391 }
392 break;
393 case SHFL_CF_ACT_OVERWRITE_IF_EXISTS:
394 if (SHFL_CF_ACT_CREATE_IF_NEW == BIT_FLAG(fShflFlags, SHFL_CF_ACT_MASK_IF_NEW))
395 {
396 fOpen |= RTFILE_O_CREATE_REPLACE;
397 Log(("FLAGS: SHFL_CF_ACT_OVERWRITE_IF_EXISTS and SHFL_CF_ACT_CREATE_IF_NEW\n"));
398 }
399 else if (SHFL_CF_ACT_FAIL_IF_NEW == BIT_FLAG(fShflFlags, SHFL_CF_ACT_MASK_IF_NEW))
400 {
401 fOpen |= RTFILE_O_OPEN | RTFILE_O_TRUNCATE;
402 Log(("FLAGS: SHFL_CF_ACT_OVERWRITE_IF_EXISTS and SHFL_CF_ACT_FAIL_IF_NEW\n"));
403 }
404 else
405 {
406 Log(("FLAGS: invalid open/create action combination\n"));
407 rc = VERR_INVALID_PARAMETER;
408 }
409 break;
410 default:
411 rc = VERR_INVALID_PARAMETER;
412 Log(("FLAG: SHFL_CF_ACT_MASK_IF_EXISTS - invalid parameter\n"));
413 }
414
415 if (RT_SUCCESS(rc))
416 {
417 if (!fWritable)
418 fOpen &= ~RTFILE_O_WRITE;
419
420 *pfOpen = fOpen;
421 }
422 return rc;
423}
424
425/**
426 * Open a file or create and open a new one.
427 *
428 * @returns IPRT status code
429 * @param pClient Data structure describing the client accessing the shared folder
430 * @param root The index of the shared folder in the table of mappings.
431 * @param pszPath Path to the file or folder on the host.
432 * @param pParms @a CreateFlags Creation or open parameters, see include/VBox/shflsvc.h
433 * @param pParms @a Info When a new file is created this specifies the initial parameters.
434 * When a file is created or overwritten, it also specifies the
435 * initial size.
436 * @retval pParms @a Resulte Shared folder status code, see include/VBox/shflsvc.h
437 * @retval pParms @a Handle On success the (shared folder) handle of the file opened or
438 * created
439 * @retval pParms @a Info On success the parameters of the file opened or created
440 */
441static int vbsfOpenFile(SHFLCLIENTDATA *pClient, SHFLROOT root, const char *pszPath, SHFLCREATEPARMS *pParms)
442{
443 LogFlow(("vbsfOpenFile: pszPath = %s, pParms = %p\n", pszPath, pParms));
444 Log(("SHFL create flags %08x\n", pParms->CreateFlags));
445
446 SHFLHANDLE handle = SHFL_HANDLE_NIL;
447 SHFLFILEHANDLE *pHandle = 0;
448 /* Open or create a file. */
449 uint32_t fOpen = 0;
450 bool fNoError = false;
451 static int cErrors;
452
453 /* is the guest allowed to write to this share? */
454 bool fWritable;
455 int rc = vbsfMappingsQueryWritable(pClient, root, &fWritable);
456 if (RT_FAILURE(rc))
457 fWritable = false;
458
459 rc = vbsfConvertFileOpenFlags(fWritable, pParms->CreateFlags, pParms->Info.Attr.fMode, pParms->Handle, &fOpen);
460 if (RT_SUCCESS(rc))
461 {
462 rc = VERR_NO_MEMORY; /* Default error. */
463 handle = vbsfAllocFileHandle(pClient);
464 if (handle != SHFL_HANDLE_NIL)
465 {
466 pHandle = vbsfQueryFileHandle(pClient, handle);
467 if (pHandle)
468 {
469 pHandle->root = root;
470 rc = RTFileOpen(&pHandle->file.Handle, pszPath, fOpen);
471 }
472 }
473 }
474 if (RT_FAILURE(rc))
475 {
476 switch (rc)
477 {
478 case VERR_FILE_NOT_FOUND:
479 pParms->Result = SHFL_FILE_NOT_FOUND;
480
481 /* This actually isn't an error, so correct the rc before return later,
482 because the driver (VBoxSF.sys) expects rc = VINF_SUCCESS and checks the result code. */
483 fNoError = true;
484 break;
485 case VERR_PATH_NOT_FOUND:
486 pParms->Result = SHFL_PATH_NOT_FOUND;
487
488 /* This actually isn't an error, so correct the rc before return later,
489 because the driver (VBoxSF.sys) expects rc = VINF_SUCCESS and checks the result code. */
490 fNoError = true;
491 break;
492 case VERR_ALREADY_EXISTS:
493 RTFSOBJINFO info;
494
495 /** @todo Possible race left here. */
496 if (RT_SUCCESS(RTPathQueryInfoEx(pszPath, &info, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient))))
497 {
498#ifdef RT_OS_WINDOWS
499 info.Attr.fMode |= 0111;
500#endif
501 vbfsCopyFsObjInfoFromIprt(&pParms->Info, &info);
502 }
503 pParms->Result = SHFL_FILE_EXISTS;
504
505 /* This actually isn't an error, so correct the rc before return later,
506 because the driver (VBoxSF.sys) expects rc = VINF_SUCCESS and checks the result code. */
507 fNoError = true;
508 break;
509 case VERR_TOO_MANY_OPEN_FILES:
510 if (cErrors < 32)
511 {
512 LogRel(("SharedFolders host service: Cannot open '%s' -- too many open files.\n", pszPath));
513#if defined RT_OS_LINUX || defined(RT_OS_SOLARIS)
514 if (cErrors < 1)
515 LogRel(("SharedFolders host service: Try to increase the limit for open files (ulimit -n)\n"));
516#endif
517 cErrors++;
518 }
519 pParms->Result = SHFL_NO_RESULT;
520 break;
521 default:
522 pParms->Result = SHFL_NO_RESULT;
523 }
524 }
525 else
526 {
527 /** @note The shared folder status code is very approximate, as the runtime
528 * does not really provide this information. */
529 pParms->Result = SHFL_FILE_EXISTS; /* We lost the information as to whether it was
530 created when we eliminated the race. */
531 if ( ( SHFL_CF_ACT_REPLACE_IF_EXISTS
532 == BIT_FLAG(pParms->CreateFlags, SHFL_CF_ACT_MASK_IF_EXISTS))
533 || ( SHFL_CF_ACT_OVERWRITE_IF_EXISTS
534 == BIT_FLAG(pParms->CreateFlags, SHFL_CF_ACT_MASK_IF_EXISTS)))
535 {
536 /* For now, we do not treat a failure here as fatal. */
537 /** @todo Also set the size for SHFL_CF_ACT_CREATE_IF_NEW if
538 SHFL_CF_ACT_FAIL_IF_EXISTS is set. */
539 RTFileSetSize(pHandle->file.Handle, pParms->Info.cbObject);
540 pParms->Result = SHFL_FILE_REPLACED;
541 }
542 if ( ( SHFL_CF_ACT_FAIL_IF_EXISTS
543 == BIT_FLAG(pParms->CreateFlags, SHFL_CF_ACT_MASK_IF_EXISTS))
544 || ( SHFL_CF_ACT_CREATE_IF_NEW
545 == BIT_FLAG(pParms->CreateFlags, SHFL_CF_ACT_MASK_IF_NEW)))
546 {
547 pParms->Result = SHFL_FILE_CREATED;
548 }
549#if 0
550 /** @todo */
551 /* Set new attributes. */
552 if ( ( SHFL_CF_ACT_REPLACE_IF_EXISTS
553 == BIT_FLAG(pParms->CreateFlags, SHFL_CF_ACT_MASK_IF_EXISTS))
554 || ( SHFL_CF_ACT_CREATE_IF_NEW
555 == BIT_FLAG(pParms->CreateFlags, SHFL_CF_ACT_MASK_IF_NEW)))
556 {
557 RTFileSetTimes(pHandle->file.Handle,
558 &pParms->Info.AccessTime,
559 &pParms->Info.ModificationTime,
560 &pParms->Info.ChangeTime,
561 &pParms->Info.BirthTime
562 );
563
564 RTFileSetMode (pHandle->file.Handle, pParms->Info.Attr.fMode);
565 }
566#endif
567 RTFSOBJINFO info;
568
569 /* Get file information */
570 rc = RTFileQueryInfo(pHandle->file.Handle, &info, RTFSOBJATTRADD_NOTHING);
571 if (RT_SUCCESS(rc))
572 {
573#ifdef RT_OS_WINDOWS
574 info.Attr.fMode |= 0111;
575#endif
576 vbfsCopyFsObjInfoFromIprt(&pParms->Info, &info);
577 }
578 }
579 /* Free resources if any part of the function has failed. */
580 if (RT_FAILURE(rc))
581 {
582 if ( (0 != pHandle)
583 && (NIL_RTFILE != pHandle->file.Handle)
584 && (0 != pHandle->file.Handle))
585 {
586 RTFileClose(pHandle->file.Handle);
587 pHandle->file.Handle = NIL_RTFILE;
588 }
589 if (SHFL_HANDLE_NIL != handle)
590 {
591 vbsfFreeFileHandle(pClient, handle);
592 }
593 pParms->Handle = SHFL_HANDLE_NIL;
594 }
595 else
596 {
597 pParms->Handle = handle;
598 }
599
600 /* Report the driver that all is okay, we're done here */
601 if (fNoError)
602 rc = VINF_SUCCESS;
603
604 LogFlow(("vbsfOpenFile: rc = %Rrc\n", rc));
605 return rc;
606}
607
608/**
609 * Open a folder or create and open a new one.
610 *
611 * @returns IPRT status code
612 * @param pClient Data structure describing the client accessing the shared folder
613 * @param root The index of the shared folder in the table of mappings.
614 * @param pszPath Path to the file or folder on the host.
615 * @param pParms @a CreateFlags Creation or open parameters, see include/VBox/shflsvc.h
616 * @retval pParms @a Result Shared folder status code, see include/VBox/shflsvc.h
617 * @retval pParms @a Handle On success the (shared folder) handle of the folder opened or
618 * created
619 * @retval pParms @a Info On success the parameters of the folder opened or created
620 *
621 * @note folders are created with fMode = 0777
622 */
623static int vbsfOpenDir(SHFLCLIENTDATA *pClient, SHFLROOT root, const char *pszPath,
624 SHFLCREATEPARMS *pParms)
625{
626 LogFlow(("vbsfOpenDir: pszPath = %s, pParms = %p\n", pszPath, pParms));
627 Log(("SHFL create flags %08x\n", pParms->CreateFlags));
628
629 int rc = VERR_NO_MEMORY;
630 SHFLHANDLE handle = vbsfAllocDirHandle(pClient);
631 SHFLFILEHANDLE *pHandle = vbsfQueryDirHandle(pClient, handle);
632 if (0 != pHandle)
633 {
634 pHandle->root = root;
635 rc = VINF_SUCCESS;
636 pParms->Result = SHFL_FILE_EXISTS; /* May be overwritten with SHFL_FILE_CREATED. */
637 /** @todo Can anyone think of a sensible, race-less way to do this? Although
638 I suspect that the race is inherent, due to the API available... */
639 /* Try to create the folder first if "create if new" is specified. If this
640 fails, and "open if exists" is specified, then we ignore the failure and try
641 to open the folder anyway. */
642 if ( SHFL_CF_ACT_CREATE_IF_NEW
643 == BIT_FLAG(pParms->CreateFlags, SHFL_CF_ACT_MASK_IF_NEW))
644 {
645 /** @todo render supplied attributes.
646 * bird: The guest should specify this. For windows guests RTFS_DOS_DIRECTORY should suffice. */
647 RTFMODE fMode = 0777;
648
649 pParms->Result = SHFL_FILE_CREATED;
650 rc = RTDirCreate(pszPath, fMode, 0);
651 if (RT_FAILURE(rc))
652 {
653 switch (rc)
654 {
655 case VERR_ALREADY_EXISTS:
656 pParms->Result = SHFL_FILE_EXISTS;
657 break;
658 case VERR_PATH_NOT_FOUND:
659 pParms->Result = SHFL_PATH_NOT_FOUND;
660 break;
661 default:
662 pParms->Result = SHFL_NO_RESULT;
663 }
664 }
665 }
666 if ( RT_SUCCESS(rc)
667 || (SHFL_CF_ACT_OPEN_IF_EXISTS == BIT_FLAG(pParms->CreateFlags, SHFL_CF_ACT_MASK_IF_EXISTS)))
668 {
669 /* Open the directory now */
670 rc = RTDirOpenFiltered(&pHandle->dir.Handle, pszPath, RTDIRFILTER_NONE, 0);
671 if (RT_SUCCESS(rc))
672 {
673 RTFSOBJINFO info;
674
675 rc = RTDirQueryInfo(pHandle->dir.Handle, &info, RTFSOBJATTRADD_NOTHING);
676 if (RT_SUCCESS(rc))
677 {
678 vbfsCopyFsObjInfoFromIprt(&pParms->Info, &info);
679 }
680 }
681 else
682 {
683 switch (rc)
684 {
685 case VERR_FILE_NOT_FOUND: /* Does this make sense? */
686 pParms->Result = SHFL_FILE_NOT_FOUND;
687 break;
688 case VERR_PATH_NOT_FOUND:
689 pParms->Result = SHFL_PATH_NOT_FOUND;
690 break;
691 case VERR_ACCESS_DENIED:
692 pParms->Result = SHFL_FILE_EXISTS;
693 break;
694 default:
695 pParms->Result = SHFL_NO_RESULT;
696 }
697 }
698 }
699 }
700 if (RT_FAILURE(rc))
701 {
702 if ( (0 != pHandle)
703 && (0 != pHandle->dir.Handle))
704 {
705 RTDirClose(pHandle->dir.Handle);
706 pHandle->dir.Handle = 0;
707 }
708 if (SHFL_HANDLE_NIL != handle)
709 {
710 vbsfFreeFileHandle(pClient, handle);
711 }
712 pParms->Handle = SHFL_HANDLE_NIL;
713 }
714 else
715 {
716 pParms->Handle = handle;
717 }
718 LogFlow(("vbsfOpenDir: rc = %Rrc\n", rc));
719 return rc;
720}
721
722static int vbsfCloseDir(SHFLFILEHANDLE *pHandle)
723{
724 int rc = VINF_SUCCESS;
725
726 LogFlow(("vbsfCloseDir: Handle = %08X Search Handle = %08X\n",
727 pHandle->dir.Handle, pHandle->dir.SearchHandle));
728
729 RTDirClose(pHandle->dir.Handle);
730
731 if (pHandle->dir.SearchHandle)
732 RTDirClose(pHandle->dir.SearchHandle);
733
734 if (pHandle->dir.pLastValidEntry)
735 {
736 RTMemFree(pHandle->dir.pLastValidEntry);
737 pHandle->dir.pLastValidEntry = NULL;
738 }
739
740 LogFlow(("vbsfCloseDir: rc = %d\n", rc));
741
742 return rc;
743}
744
745
746static int vbsfCloseFile(SHFLFILEHANDLE *pHandle)
747{
748 int rc = VINF_SUCCESS;
749
750 LogFlow(("vbsfCloseFile: Handle = %08X\n",
751 pHandle->file.Handle));
752
753 rc = RTFileClose(pHandle->file.Handle);
754
755 LogFlow(("vbsfCloseFile: rc = %d\n", rc));
756
757 return rc;
758}
759
760/**
761 * Look up file or folder information by host path.
762 *
763 * @returns iprt status code (currently VINF_SUCCESS)
764 * @param pClient client data
765 * @param pszPath The path of the file to be looked up
766 * @retval pParms->Result Status of the operation (success or error)
767 * @retval pParms->Info On success, information returned about the file
768 */
769static int vbsfLookupFile(SHFLCLIENTDATA *pClient, char *pszPath, SHFLCREATEPARMS *pParms)
770{
771 RTFSOBJINFO info;
772 int rc;
773
774 rc = RTPathQueryInfoEx(pszPath, &info, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient));
775 LogFlow(("SHFL_CF_LOOKUP\n"));
776 /* Client just wants to know if the object exists. */
777 switch (rc)
778 {
779 case VINF_SUCCESS:
780 {
781#ifdef RT_OS_WINDOWS
782 info.Attr.fMode |= 0111;
783#endif
784 vbfsCopyFsObjInfoFromIprt(&pParms->Info, &info);
785 pParms->Result = SHFL_FILE_EXISTS;
786 break;
787 }
788
789 case VERR_FILE_NOT_FOUND:
790 {
791 pParms->Result = SHFL_FILE_NOT_FOUND;
792 rc = VINF_SUCCESS;
793 break;
794 }
795
796 case VERR_PATH_NOT_FOUND:
797 {
798 pParms->Result = SHFL_PATH_NOT_FOUND;
799 rc = VINF_SUCCESS;
800 break;
801 }
802 }
803 pParms->Handle = SHFL_HANDLE_NIL;
804 return rc;
805}
806
807#ifdef UNITTEST
808/** Unit test the SHFL_FN_CREATE API. Located here as a form of API
809 * documentation. */
810void testCreate(RTTEST hTest)
811{
812 /* Simple opening of an existing file. */
813 testCreateFileSimple(hTest);
814 testCreateFileSimpleCaseInsensitive(hTest);
815 /* Simple opening of an existing directory. */
816 /** @todo How do wildcards in the path name work? */
817 testCreateDirSimple(hTest);
818 /* If the number or types of parameters are wrong the API should fail. */
819 testCreateBadParameters(hTest);
820 /* Add tests as required... */
821}
822#endif
823
824/**
825 * Create or open a file or folder. Perform character set and case
826 * conversion on the file name if necessary.
827 *
828 * @returns IPRT status code, but see note below
829 * @param pClient Data structure describing the client accessing the shared
830 * folder
831 * @param root The index of the shared folder in the table of mappings.
832 * The host path of the shared folder is found using this.
833 * @param pPath The path of the file or folder relative to the host path
834 * indexed by root.
835 * @param cbPath Presumably the length of the path in pPath. Actually
836 * ignored, as pPath contains a length parameter.
837 * @param pParms @a Info If a new file is created or an old one overwritten, set
838 * these attributes
839 * @retval pParms @a Result Shared folder result code, see include/VBox/shflsvc.h
840 * @retval pParms @a Handle Shared folder handle to the newly opened file
841 * @retval pParms @a Info Attributes of the file or folder opened
842 *
843 * @note This function returns success if a "non-exceptional" error occurred,
844 * such as "no such file". In this case, the caller should check the
845 * pParms->Result return value and whether pParms->Handle is valid.
846 */
847int vbsfCreate(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pPath, uint32_t cbPath, SHFLCREATEPARMS *pParms)
848{
849 int rc = VINF_SUCCESS;
850
851 LogFlow(("vbsfCreate: pClient = %p, pPath = %p, cbPath = %d, pParms = %p CreateFlags=%x\n",
852 pClient, pPath, cbPath, pParms, pParms->CreateFlags));
853
854 /* Check the client access rights to the root. */
855 /** @todo */
856
857 /* Build a host full path for the given path, handle file name case issues (if the guest
858 * expects case-insensitive paths but the host is case-sensitive) and convert ucs2 to utf8 if
859 * necessary.
860 */
861 char *pszFullPath = NULL;
862 uint32_t cbFullPathRoot = 0;
863
864 rc = vbsfBuildFullPath(pClient, root, pPath, cbPath, &pszFullPath, &cbFullPathRoot);
865 if (RT_SUCCESS(rc))
866 {
867 /* Reset return value in case client forgot to do so.
868 * pParms->Handle must not be reset here, as it is used
869 * in vbsfOpenFile to detect old additions.
870 */
871 pParms->Result = SHFL_NO_RESULT;
872
873 if (BIT_FLAG(pParms->CreateFlags, SHFL_CF_LOOKUP))
874 {
875 rc = vbsfLookupFile(pClient, pszFullPath, pParms);
876 }
877 else
878 {
879 /* Query path information. */
880 RTFSOBJINFO info;
881
882 rc = RTPathQueryInfoEx(pszFullPath, &info, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient));
883 LogFlow(("RTPathQueryInfoEx returned %Rrc\n", rc));
884
885 if (RT_SUCCESS(rc))
886 {
887 /* Mark it as a directory in case the caller didn't. */
888 /**
889 * @todo I left this in in order not to change the behaviour of the
890 * function too much. Is it really needed, and should it really be
891 * here?
892 */
893 if (BIT_FLAG(info.Attr.fMode, RTFS_DOS_DIRECTORY))
894 {
895 pParms->CreateFlags |= SHFL_CF_DIRECTORY;
896 }
897
898 /**
899 * @todo This should be in the Windows Guest Additions, as no-one else
900 * needs it.
901 */
902 if (BIT_FLAG(pParms->CreateFlags, SHFL_CF_OPEN_TARGET_DIRECTORY))
903 {
904 vbsfStripLastComponent(pszFullPath, cbFullPathRoot);
905 pParms->CreateFlags &= ~SHFL_CF_ACT_MASK_IF_EXISTS;
906 pParms->CreateFlags &= ~SHFL_CF_ACT_MASK_IF_NEW;
907 pParms->CreateFlags |= SHFL_CF_DIRECTORY;
908 pParms->CreateFlags |= SHFL_CF_ACT_OPEN_IF_EXISTS;
909 pParms->CreateFlags |= SHFL_CF_ACT_FAIL_IF_NEW;
910 }
911 }
912
913 rc = VINF_SUCCESS;
914
915 /* Note: do not check the SHFL_CF_ACCESS_WRITE here, only check if the open operation
916 * will cause changes.
917 *
918 * Actual operations (write, set attr, etc), which can write to a shared folder, have
919 * the check and will return VERR_WRITE_PROTECT if the folder is not writable.
920 */
921 if ( (pParms->CreateFlags & SHFL_CF_ACT_MASK_IF_EXISTS) == SHFL_CF_ACT_REPLACE_IF_EXISTS
922 || (pParms->CreateFlags & SHFL_CF_ACT_MASK_IF_EXISTS) == SHFL_CF_ACT_OVERWRITE_IF_EXISTS
923 || (pParms->CreateFlags & SHFL_CF_ACT_MASK_IF_NEW) == SHFL_CF_ACT_CREATE_IF_NEW
924 )
925 {
926 /* is the guest allowed to write to this share? */
927 bool fWritable;
928 rc = vbsfMappingsQueryWritable(pClient, root, &fWritable);
929 if (RT_FAILURE(rc) || !fWritable)
930 rc = VERR_WRITE_PROTECT;
931 }
932
933 if (RT_SUCCESS(rc))
934 {
935 if (BIT_FLAG(pParms->CreateFlags, SHFL_CF_DIRECTORY))
936 {
937 rc = vbsfOpenDir(pClient, root, pszFullPath, pParms);
938 }
939 else
940 {
941 rc = vbsfOpenFile(pClient, root, pszFullPath, pParms);
942 }
943 }
944 else
945 {
946 pParms->Handle = SHFL_HANDLE_NIL;
947 }
948 }
949
950 /* free the path string */
951 vbsfFreeFullPath(pszFullPath);
952 }
953
954 Log(("vbsfCreate: handle = %RX64 rc = %Rrc result=%x\n", (uint64_t)pParms->Handle, rc, pParms->Result));
955
956 return rc;
957}
958
959#ifdef UNITTEST
960/** Unit test the SHFL_FN_CLOSE API. Located here as a form of API
961 * documentation. */
962void testClose(RTTEST hTest)
963{
964 /* If the API parameters are invalid the API should fail. */
965 testCloseBadParameters(hTest);
966 /* Add tests as required... */
967}
968#endif
969
970int vbsfClose(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle)
971{
972 LogFunc(("pClient = %p, root 0x%RX32, Handle = 0x%RX64\n",
973 pClient, root, Handle));
974
975 int rc = VERR_INVALID_HANDLE;
976 uint32_t type = vbsfQueryHandleType(pClient, Handle);
977 Assert((type & ~(SHFL_HF_TYPE_DIR | SHFL_HF_TYPE_FILE)) == 0);
978 switch (type & (SHFL_HF_TYPE_DIR | SHFL_HF_TYPE_FILE))
979 {
980 case SHFL_HF_TYPE_DIR:
981 {
982 SHFLFILEHANDLE *pHandle = vbsfQueryDirHandle(pClient, Handle);
983 if (RT_LIKELY(pHandle && root == pHandle->root))
984 {
985 rc = vbsfCloseDir(pHandle);
986 vbsfFreeFileHandle(pClient, Handle);
987 }
988 break;
989 }
990 case SHFL_HF_TYPE_FILE:
991 {
992 SHFLFILEHANDLE *pHandle = vbsfQueryFileHandle(pClient, Handle);
993 if (RT_LIKELY(pHandle && root == pHandle->root))
994 {
995 rc = vbsfCloseFile(pHandle);
996 vbsfFreeFileHandle(pClient, Handle);
997 }
998 break;
999 }
1000 default:
1001 break;
1002 }
1003
1004 LogFunc(("rc = %Rrc\n", rc));
1005 return rc;
1006}
1007
1008#ifdef UNITTEST
1009/** Unit test the SHFL_FN_READ API. Located here as a form of API
1010 * documentation. */
1011void testRead(RTTEST hTest)
1012{
1013 /* If the number or types of parameters are wrong the API should fail. */
1014 testReadBadParameters(hTest);
1015 /* Basic reading from a file. */
1016 testReadFileSimple(hTest);
1017 /* Add tests as required... */
1018}
1019#endif
1020int vbsfRead(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint64_t offset, uint32_t *pcbBuffer, uint8_t *pBuffer)
1021{
1022 LogFunc(("pClient %p, root 0x%RX32, Handle 0x%RX64, offset 0x%RX64, bytes 0x%RX32\n",
1023 pClient, root, Handle, offset, pcbBuffer? *pcbBuffer: 0));
1024
1025 AssertPtrReturn(pClient, VERR_INVALID_PARAMETER);
1026
1027 SHFLFILEHANDLE *pHandle = vbsfQueryFileHandle(pClient, Handle);
1028 int rc = vbsfCheckHandleAccess(pClient, root, pHandle, VBSF_CHECK_ACCESS_READ);
1029 if (RT_SUCCESS(rc))
1030 { /* likely */ }
1031 else
1032 return rc;
1033
1034 if (RT_LIKELY(*pcbBuffer != 0))
1035 {
1036 rc = RTFileSeek(pHandle->file.Handle, offset, RTFILE_SEEK_BEGIN, NULL);
1037 if (RT_SUCCESS(rc))
1038 {
1039 size_t count = 0;
1040 rc = RTFileRead(pHandle->file.Handle, pBuffer, *pcbBuffer, &count);
1041 *pcbBuffer = (uint32_t)count;
1042 }
1043 else
1044 AssertRC(rc);
1045 }
1046 else
1047 {
1048 /* Reading zero bytes always succeeds. */
1049 rc = VINF_SUCCESS;
1050 }
1051
1052 LogFunc(("%Rrc bytes read 0x%RX32\n", rc, *pcbBuffer));
1053 return rc;
1054}
1055
1056#ifdef UNITTEST
1057/** Unit test the SHFL_FN_WRITE API. Located here as a form of API
1058 * documentation. */
1059void testWrite(RTTEST hTest)
1060{
1061 /* If the number or types of parameters are wrong the API should fail. */
1062 testWriteBadParameters(hTest);
1063 /* Simple test of writing to a file. */
1064 testWriteFileSimple(hTest);
1065 /* Add tests as required... */
1066}
1067#endif
1068int vbsfWrite(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint64_t offset, uint32_t *pcbBuffer, uint8_t *pBuffer)
1069{
1070 LogFunc(("pClient %p, root 0x%RX32, Handle 0x%RX64, offset 0x%RX64, bytes 0x%RX32\n",
1071 pClient, root, Handle, offset, pcbBuffer? *pcbBuffer: 0));
1072
1073 AssertPtrReturn(pClient, VERR_INVALID_PARAMETER);
1074
1075 SHFLFILEHANDLE *pHandle = vbsfQueryFileHandle(pClient, Handle);
1076 int rc = vbsfCheckHandleAccess(pClient, root, pHandle, VBSF_CHECK_ACCESS_WRITE);
1077 if (RT_SUCCESS(rc))
1078 { /* likely */ }
1079 else
1080 return rc;
1081
1082 if (RT_LIKELY(*pcbBuffer != 0))
1083 {
1084 rc = RTFileSeek(pHandle->file.Handle, offset, RTFILE_SEEK_BEGIN, NULL);
1085 if (RT_SUCCESS(rc))
1086 {
1087 size_t count = 0;
1088 rc = RTFileWrite(pHandle->file.Handle, pBuffer, *pcbBuffer, &count);
1089 *pcbBuffer = (uint32_t)count;
1090 }
1091 else
1092 AssertRC(rc);
1093 }
1094 else
1095 {
1096 /** @todo: What writing zero bytes should do? */
1097 rc = VINF_SUCCESS;
1098 }
1099
1100 LogFunc(("%Rrc bytes written 0x%RX32\n", rc, *pcbBuffer));
1101 return rc;
1102}
1103
1104
1105#ifdef UNITTEST
1106/** Unit test the SHFL_FN_FLUSH API. Located here as a form of API
1107 * documentation. */
1108void testFlush(RTTEST hTest)
1109{
1110 /* If the number or types of parameters are wrong the API should fail. */
1111 testFlushBadParameters(hTest);
1112 /* Simple opening and flushing of a file. */
1113 testFlushFileSimple(hTest);
1114 /* Add tests as required... */
1115}
1116#endif
1117
1118int vbsfFlush(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle)
1119{
1120 LogFunc(("pClient %p, root 0x%RX32, Handle 0x%RX64\n",
1121 pClient, root, Handle));
1122
1123 AssertPtrReturn(pClient, VERR_INVALID_PARAMETER);
1124
1125 SHFLFILEHANDLE *pHandle = vbsfQueryFileHandle(pClient, Handle);
1126 int rc = vbsfCheckHandleAccess(pClient, root, pHandle, VBSF_CHECK_ACCESS_WRITE);
1127 if (RT_SUCCESS(rc))
1128 { /* likely */ }
1129 else
1130 return rc;
1131
1132 rc = RTFileFlush(pHandle->file.Handle);
1133
1134 LogFunc(("%Rrc\n", rc));
1135 return rc;
1136}
1137
1138#ifdef UNITTEST
1139/** Unit test the SHFL_FN_LIST API. Located here as a form of API
1140 * documentation. */
1141void testDirList(RTTEST hTest)
1142{
1143 /* If the number or types of parameters are wrong the API should fail. */
1144 testDirListBadParameters(hTest);
1145 /* Test listing an empty directory (simple edge case). */
1146 testDirListEmpty(hTest);
1147 /* Add tests as required... */
1148}
1149#endif
1150int vbsfDirList(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, SHFLSTRING *pPath, uint32_t flags,
1151 uint32_t *pcbBuffer, uint8_t *pBuffer, uint32_t *pIndex, uint32_t *pcFiles)
1152{
1153 PRTDIRENTRYEX pDirEntry = 0, pDirEntryOrg;
1154 uint32_t cbDirEntry, cbBufferOrg;
1155 PSHFLDIRINFO pSFDEntry;
1156 PRTUTF16 pwszString;
1157 PRTDIR DirHandle;
1158 const bool fUtf8 = BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8) != 0;
1159
1160 AssertPtrReturn(pClient, VERR_INVALID_PARAMETER);
1161
1162 SHFLFILEHANDLE *pHandle = vbsfQueryDirHandle(pClient, Handle);
1163 int rc = vbsfCheckHandleAccess(pClient, root, pHandle, VBSF_CHECK_ACCESS_READ);
1164 if (RT_SUCCESS(rc))
1165 { /* likely */ }
1166 else
1167 return rc;
1168
1169 Assert(*pIndex == 0);
1170 DirHandle = pHandle->dir.Handle;
1171
1172 cbDirEntry = 4096;
1173 pDirEntryOrg = pDirEntry = (PRTDIRENTRYEX)RTMemAlloc(cbDirEntry);
1174 if (pDirEntry == 0)
1175 {
1176 AssertFailed();
1177 return VERR_NO_MEMORY;
1178 }
1179
1180 cbBufferOrg = *pcbBuffer;
1181 *pcbBuffer = 0;
1182 pSFDEntry = (PSHFLDIRINFO)pBuffer;
1183
1184 *pIndex = 1; /* not yet complete */
1185 *pcFiles = 0;
1186
1187 if (pPath)
1188 {
1189 if (pHandle->dir.SearchHandle == 0)
1190 {
1191 /* Build a host full path for the given path
1192 * and convert ucs2 to utf8 if necessary.
1193 */
1194 char *pszFullPath = NULL;
1195
1196 Assert(pHandle->dir.pLastValidEntry == 0);
1197
1198 rc = vbsfBuildFullPath(pClient, root, pPath, pPath->u16Size + SHFLSTRING_HEADER_SIZE, &pszFullPath, NULL, true);
1199
1200 if (RT_SUCCESS(rc))
1201 {
1202 rc = RTDirOpenFiltered(&pHandle->dir.SearchHandle, pszFullPath, RTDIRFILTER_WINNT, 0);
1203
1204 /* free the path string */
1205 vbsfFreeFullPath(pszFullPath);
1206
1207 if (RT_FAILURE(rc))
1208 goto end;
1209 }
1210 else
1211 goto end;
1212 }
1213 Assert(pHandle->dir.SearchHandle);
1214 DirHandle = pHandle->dir.SearchHandle;
1215 }
1216
1217 while (cbBufferOrg)
1218 {
1219 size_t cbDirEntrySize = cbDirEntry;
1220 uint32_t cbNeeded;
1221
1222 /* Do we still have a valid last entry for the active search? If so, then return it here */
1223 if (pHandle->dir.pLastValidEntry)
1224 {
1225 pDirEntry = pHandle->dir.pLastValidEntry;
1226 }
1227 else
1228 {
1229 pDirEntry = pDirEntryOrg;
1230
1231 rc = RTDirReadEx(DirHandle, pDirEntry, &cbDirEntrySize, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient));
1232 if (rc == VERR_NO_MORE_FILES)
1233 {
1234 *pIndex = 0; /* listing completed */
1235 break;
1236 }
1237
1238 if ( rc != VINF_SUCCESS
1239 && rc != VWRN_NO_DIRENT_INFO)
1240 {
1241 //AssertFailed();
1242 if ( rc == VERR_NO_TRANSLATION
1243 || rc == VERR_INVALID_UTF8_ENCODING)
1244 continue;
1245 break;
1246 }
1247 }
1248
1249 cbNeeded = RT_OFFSETOF(SHFLDIRINFO, name.String);
1250 if (fUtf8)
1251 cbNeeded += pDirEntry->cbName + 1;
1252 else
1253 /* Overestimating, but that's ok */
1254 cbNeeded += (pDirEntry->cbName + 1) * 2;
1255
1256 if (cbBufferOrg < cbNeeded)
1257 {
1258 /* No room, so save this directory entry, or else it's lost forever */
1259 pHandle->dir.pLastValidEntry = pDirEntry;
1260
1261 if (*pcFiles == 0)
1262 {
1263 AssertFailed();
1264 return VINF_BUFFER_OVERFLOW; /* Return directly and don't free pDirEntry */
1265 }
1266 return VINF_SUCCESS; /* Return directly and don't free pDirEntry */
1267 }
1268
1269#ifdef RT_OS_WINDOWS
1270 pDirEntry->Info.Attr.fMode |= 0111;
1271#endif
1272 vbfsCopyFsObjInfoFromIprt(&pSFDEntry->Info, &pDirEntry->Info);
1273 pSFDEntry->cucShortName = 0;
1274
1275 if (fUtf8)
1276 {
1277 void *src, *dst;
1278
1279 src = &pDirEntry->szName[0];
1280 dst = &pSFDEntry->name.String.utf8[0];
1281
1282 memcpy(dst, src, pDirEntry->cbName + 1);
1283
1284 pSFDEntry->name.u16Size = pDirEntry->cbName + 1;
1285 pSFDEntry->name.u16Length = pDirEntry->cbName;
1286 }
1287 else
1288 {
1289 pSFDEntry->name.String.ucs2[0] = 0;
1290 pwszString = pSFDEntry->name.String.ucs2;
1291 int rc2 = RTStrToUtf16Ex(pDirEntry->szName, RTSTR_MAX, &pwszString, pDirEntry->cbName+1, NULL);
1292 AssertRC(rc2);
1293
1294#ifdef RT_OS_DARWIN
1295/** @todo This belongs in rtPathToNative or in the windows shared folder file system driver...
1296 * The question is simply whether the NFD normalization is actually applied on a (virtual) file
1297 * system level in darwin, or just by the user mode application libs. */
1298 {
1299 // Convert to
1300 // Normalization Form C (composed Unicode). We need this because
1301 // Mac OS X file system uses NFD (Normalization Form D :decomposed Unicode)
1302 // while most other OS', server-side programs usually expect NFC.
1303 uint16_t ucs2Length;
1304 CFRange rangeCharacters;
1305 CFMutableStringRef inStr = ::CFStringCreateMutable(NULL, 0);
1306
1307 ::CFStringAppendCharacters(inStr, (UniChar *)pwszString, RTUtf16Len(pwszString));
1308 ::CFStringNormalize(inStr, kCFStringNormalizationFormC);
1309 ucs2Length = ::CFStringGetLength(inStr);
1310
1311 rangeCharacters.location = 0;
1312 rangeCharacters.length = ucs2Length;
1313 ::CFStringGetCharacters(inStr, rangeCharacters, pwszString);
1314 pwszString[ucs2Length] = 0x0000; // NULL terminated
1315
1316 CFRelease(inStr);
1317 }
1318#endif
1319 pSFDEntry->name.u16Length = (uint32_t)RTUtf16Len(pSFDEntry->name.String.ucs2) * 2;
1320 pSFDEntry->name.u16Size = pSFDEntry->name.u16Length + 2;
1321
1322 Log(("SHFL: File name size %d\n", pSFDEntry->name.u16Size));
1323 Log(("SHFL: File name %ls\n", &pSFDEntry->name.String.ucs2));
1324
1325 // adjust cbNeeded (it was overestimated before)
1326 cbNeeded = RT_OFFSETOF(SHFLDIRINFO, name.String) + pSFDEntry->name.u16Size;
1327 }
1328
1329 pSFDEntry = (PSHFLDIRINFO)((uintptr_t)pSFDEntry + cbNeeded);
1330 *pcbBuffer += cbNeeded;
1331 cbBufferOrg-= cbNeeded;
1332
1333 *pcFiles += 1;
1334
1335 /* Free the saved last entry, that we've just returned */
1336 if (pHandle->dir.pLastValidEntry)
1337 {
1338 RTMemFree(pHandle->dir.pLastValidEntry);
1339 pHandle->dir.pLastValidEntry = NULL;
1340 }
1341
1342 if (flags & SHFL_LIST_RETURN_ONE)
1343 break; /* we're done */
1344 }
1345 Assert(rc != VINF_SUCCESS || *pcbBuffer > 0);
1346
1347end:
1348 if (pDirEntry)
1349 RTMemFree(pDirEntry);
1350
1351 return rc;
1352}
1353
1354#ifdef UNITTEST
1355/** Unit test the SHFL_FN_READLINK API. Located here as a form of API
1356 * documentation. */
1357void testReadLink(RTTEST hTest)
1358{
1359 /* If the number or types of parameters are wrong the API should fail. */
1360 testReadLinkBadParameters(hTest);
1361 /* Add tests as required... */
1362}
1363#endif
1364int vbsfReadLink(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pPath, uint32_t cbPath, uint8_t *pBuffer, uint32_t cbBuffer)
1365{
1366 int rc = VINF_SUCCESS;
1367
1368 if (pPath == 0 || pBuffer == 0)
1369 {
1370 AssertFailed();
1371 return VERR_INVALID_PARAMETER;
1372 }
1373
1374 /* Build a host full path for the given path, handle file name case issues
1375 * (if the guest expects case-insensitive paths but the host is
1376 * case-sensitive) and convert ucs2 to utf8 if necessary.
1377 */
1378 char *pszFullPath = NULL;
1379 uint32_t cbFullPathRoot = 0;
1380
1381 rc = vbsfBuildFullPath(pClient, root, pPath, cbPath, &pszFullPath, &cbFullPathRoot);
1382
1383 if (RT_SUCCESS(rc))
1384 {
1385 rc = RTSymlinkRead(pszFullPath, (char *) pBuffer, cbBuffer, 0);
1386 if (RT_SUCCESS(rc))
1387 {
1388 /* Convert the slashes in the link target to the guest path separator characters. */
1389 char *psz = (char *)pBuffer;
1390 while (*psz != '\0')
1391 {
1392 if (*psz == RTPATH_DELIMITER)
1393 *psz = pClient->PathDelimiter;
1394 psz++;
1395 }
1396 }
1397
1398 /* free the path string */
1399 vbsfFreeFullPath(pszFullPath);
1400 }
1401
1402 return rc;
1403}
1404
1405int vbsfQueryFileInfo(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint32_t flags,
1406 uint32_t *pcbBuffer, uint8_t *pBuffer)
1407{
1408 RT_NOREF1(flags);
1409 uint32_t type = vbsfQueryHandleType(pClient, Handle);
1410 int rc = VINF_SUCCESS;
1411 SHFLFSOBJINFO *pObjInfo = (SHFLFSOBJINFO *)pBuffer;
1412 RTFSOBJINFO fileinfo;
1413
1414
1415 AssertReturn(type == SHFL_HF_TYPE_DIR || type == SHFL_HF_TYPE_FILE, VERR_INVALID_PARAMETER);
1416 AssertReturn(pcbBuffer != NULL, VERR_INVALID_PARAMETER);
1417 AssertReturn(pObjInfo != NULL, VERR_INVALID_PARAMETER);
1418 AssertReturn(*pcbBuffer >= sizeof(SHFLFSOBJINFO), VERR_INVALID_PARAMETER);
1419
1420 /** @todo other options */
1421 Assert(flags == (SHFL_INFO_GET|SHFL_INFO_FILE));
1422
1423 *pcbBuffer = 0;
1424
1425 if (type == SHFL_HF_TYPE_DIR)
1426 {
1427 SHFLFILEHANDLE *pHandle = vbsfQueryDirHandle(pClient, Handle);
1428 rc = vbsfCheckHandleAccess(pClient, root, pHandle, VBSF_CHECK_ACCESS_READ);
1429 if (RT_SUCCESS(rc))
1430 rc = RTDirQueryInfo(pHandle->dir.Handle, &fileinfo, RTFSOBJATTRADD_NOTHING);
1431 }
1432 else
1433 {
1434 SHFLFILEHANDLE *pHandle = vbsfQueryFileHandle(pClient, Handle);
1435 rc = vbsfCheckHandleAccess(pClient, root, pHandle, VBSF_CHECK_ACCESS_READ);
1436 if (RT_SUCCESS(rc))
1437 rc = RTFileQueryInfo(pHandle->file.Handle, &fileinfo, RTFSOBJATTRADD_NOTHING);
1438#ifdef RT_OS_WINDOWS
1439 if (RT_SUCCESS(rc) && RTFS_IS_FILE(pObjInfo->Attr.fMode))
1440 pObjInfo->Attr.fMode |= 0111;
1441#endif
1442 }
1443 if (rc == VINF_SUCCESS)
1444 {
1445 vbfsCopyFsObjInfoFromIprt(pObjInfo, &fileinfo);
1446 *pcbBuffer = sizeof(SHFLFSOBJINFO);
1447 }
1448 else
1449 AssertFailed();
1450
1451 return rc;
1452}
1453
1454static int vbsfSetFileInfo(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint32_t flags,
1455 uint32_t *pcbBuffer, uint8_t *pBuffer)
1456{
1457 RT_NOREF1(flags);
1458 uint32_t type = vbsfQueryHandleType(pClient, Handle);
1459 int rc = VINF_SUCCESS;
1460 SHFLFSOBJINFO *pSFDEntry;
1461
1462 if ( !(type == SHFL_HF_TYPE_DIR || type == SHFL_HF_TYPE_FILE)
1463 || pcbBuffer == 0
1464 || pBuffer == 0
1465 || *pcbBuffer < sizeof(SHFLFSOBJINFO))
1466 {
1467 AssertFailed();
1468 return VERR_INVALID_PARAMETER;
1469 }
1470
1471 *pcbBuffer = 0;
1472 pSFDEntry = (SHFLFSOBJINFO *)pBuffer;
1473
1474 Assert(flags == (SHFL_INFO_SET | SHFL_INFO_FILE));
1475
1476 /* Change only the time values that are not zero */
1477 if (type == SHFL_HF_TYPE_DIR)
1478 {
1479 SHFLFILEHANDLE *pHandle = vbsfQueryDirHandle(pClient, Handle);
1480 rc = vbsfCheckHandleAccess(pClient, root, pHandle, VBSF_CHECK_ACCESS_WRITE);
1481 if (RT_SUCCESS(rc))
1482 rc = RTDirSetTimes(pHandle->dir.Handle,
1483 (RTTimeSpecGetNano(&pSFDEntry->AccessTime)) ? &pSFDEntry->AccessTime : NULL,
1484 (RTTimeSpecGetNano(&pSFDEntry->ModificationTime)) ? &pSFDEntry->ModificationTime: NULL,
1485 (RTTimeSpecGetNano(&pSFDEntry->ChangeTime)) ? &pSFDEntry->ChangeTime: NULL,
1486 (RTTimeSpecGetNano(&pSFDEntry->BirthTime)) ? &pSFDEntry->BirthTime: NULL
1487 );
1488 }
1489 else
1490 {
1491 SHFLFILEHANDLE *pHandle = vbsfQueryFileHandle(pClient, Handle);
1492 rc = vbsfCheckHandleAccess(pClient, root, pHandle, VBSF_CHECK_ACCESS_WRITE);
1493 if (RT_SUCCESS(rc))
1494 rc = RTFileSetTimes(pHandle->file.Handle,
1495 (RTTimeSpecGetNano(&pSFDEntry->AccessTime)) ? &pSFDEntry->AccessTime : NULL,
1496 (RTTimeSpecGetNano(&pSFDEntry->ModificationTime)) ? &pSFDEntry->ModificationTime: NULL,
1497 (RTTimeSpecGetNano(&pSFDEntry->ChangeTime)) ? &pSFDEntry->ChangeTime: NULL,
1498 (RTTimeSpecGetNano(&pSFDEntry->BirthTime)) ? &pSFDEntry->BirthTime: NULL
1499 );
1500 }
1501 if (rc != VINF_SUCCESS)
1502 {
1503 Log(("RTFileSetTimes failed with %Rrc\n", rc));
1504 Log(("AccessTime %RX64\n", RTTimeSpecGetNano(&pSFDEntry->AccessTime)));
1505 Log(("ModificationTime %RX64\n", RTTimeSpecGetNano(&pSFDEntry->ModificationTime)));
1506 Log(("ChangeTime %RX64\n", RTTimeSpecGetNano(&pSFDEntry->ChangeTime)));
1507 Log(("BirthTime %RX64\n", RTTimeSpecGetNano(&pSFDEntry->BirthTime)));
1508 /* temporary hack */
1509 rc = VINF_SUCCESS;
1510 }
1511
1512 if (type == SHFL_HF_TYPE_FILE)
1513 {
1514 SHFLFILEHANDLE *pHandle = vbsfQueryFileHandle(pClient, Handle);
1515 rc = vbsfCheckHandleAccess(pClient, root, pHandle, VBSF_CHECK_ACCESS_WRITE);
1516 if (RT_SUCCESS(rc))
1517 {
1518 /* Change file attributes if necessary */
1519 if (pSFDEntry->Attr.fMode)
1520 {
1521 RTFMODE fMode = pSFDEntry->Attr.fMode;
1522
1523#ifndef RT_OS_WINDOWS
1524 /* Don't allow the guest to clear the own bit, otherwise the guest wouldn't be
1525 * able to access this file anymore. Only for guests, which set the UNIX mode. */
1526 if (fMode & RTFS_UNIX_MASK)
1527 fMode |= RTFS_UNIX_IRUSR;
1528#endif
1529
1530 rc = RTFileSetMode(pHandle->file.Handle, fMode);
1531 if (rc != VINF_SUCCESS)
1532 {
1533 Log(("RTFileSetMode %x failed with %Rrc\n", fMode, rc));
1534 /* silent failure, because this tends to fail with e.g. windows guest & linux host */
1535 rc = VINF_SUCCESS;
1536 }
1537 }
1538 }
1539 }
1540 /** @todo mode for directories */
1541
1542 if (rc == VINF_SUCCESS)
1543 {
1544 uint32_t bufsize = sizeof(*pSFDEntry);
1545
1546 rc = vbsfQueryFileInfo(pClient, root, Handle, SHFL_INFO_GET|SHFL_INFO_FILE, &bufsize, (uint8_t *)pSFDEntry);
1547 if (rc == VINF_SUCCESS)
1548 {
1549 *pcbBuffer = sizeof(SHFLFSOBJINFO);
1550 }
1551 else
1552 AssertFailed();
1553 }
1554
1555 return rc;
1556}
1557
1558
1559static int vbsfSetEndOfFile(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint32_t flags,
1560 uint32_t *pcbBuffer, uint8_t *pBuffer)
1561{
1562 RT_NOREF1(flags);
1563 SHFLFILEHANDLE *pHandle = vbsfQueryFileHandle(pClient, Handle);
1564 SHFLFSOBJINFO *pSFDEntry;
1565
1566 if (pHandle == 0 || pcbBuffer == 0 || pBuffer == 0 || *pcbBuffer < sizeof(SHFLFSOBJINFO))
1567 {
1568 AssertFailed();
1569 return VERR_INVALID_PARAMETER;
1570 }
1571
1572 int rc = vbsfCheckHandleAccess(pClient, root, pHandle, VBSF_CHECK_ACCESS_WRITE);
1573 if (RT_SUCCESS(rc))
1574 { /* likely */ }
1575 else
1576 return rc;
1577
1578 *pcbBuffer = 0;
1579 pSFDEntry = (SHFLFSOBJINFO *)pBuffer;
1580
1581 if (flags & SHFL_INFO_SIZE)
1582 {
1583 rc = RTFileSetSize(pHandle->file.Handle, pSFDEntry->cbObject);
1584 if (rc != VINF_SUCCESS)
1585 AssertFailed();
1586 }
1587 else
1588 AssertFailed();
1589
1590 if (rc == VINF_SUCCESS)
1591 {
1592 RTFSOBJINFO fileinfo;
1593
1594 /* Query the new object info and return it */
1595 rc = RTFileQueryInfo(pHandle->file.Handle, &fileinfo, RTFSOBJATTRADD_NOTHING);
1596 if (rc == VINF_SUCCESS)
1597 {
1598#ifdef RT_OS_WINDOWS
1599 fileinfo.Attr.fMode |= 0111;
1600#endif
1601 vbfsCopyFsObjInfoFromIprt(pSFDEntry, &fileinfo);
1602 *pcbBuffer = sizeof(SHFLFSOBJINFO);
1603 }
1604 else
1605 AssertFailed();
1606 }
1607
1608 return rc;
1609}
1610
1611int vbsfQueryVolumeInfo(SHFLCLIENTDATA *pClient, SHFLROOT root, uint32_t flags, uint32_t *pcbBuffer, uint8_t *pBuffer)
1612{
1613 RT_NOREF2(root, flags);
1614 int rc = VINF_SUCCESS;
1615 SHFLVOLINFO *pSFDEntry;
1616 char *pszFullPath = NULL;
1617 SHFLSTRING dummy;
1618
1619 if (pcbBuffer == 0 || pBuffer == 0 || *pcbBuffer < sizeof(SHFLVOLINFO))
1620 {
1621 AssertFailed();
1622 return VERR_INVALID_PARAMETER;
1623 }
1624
1625 /** @todo other options */
1626 Assert(flags == (SHFL_INFO_GET|SHFL_INFO_VOLUME));
1627
1628 *pcbBuffer = 0;
1629 pSFDEntry = (PSHFLVOLINFO)pBuffer;
1630
1631 ShflStringInitBuffer(&dummy, sizeof(dummy));
1632 dummy.String.ucs2[0] = '\0';
1633 rc = vbsfBuildFullPath(pClient, root, &dummy, sizeof(dummy), &pszFullPath, NULL);
1634
1635 if (RT_SUCCESS(rc))
1636 {
1637 rc = RTFsQuerySizes(pszFullPath, &pSFDEntry->ullTotalAllocationBytes, &pSFDEntry->ullAvailableAllocationBytes, &pSFDEntry->ulBytesPerAllocationUnit, &pSFDEntry->ulBytesPerSector);
1638 if (rc != VINF_SUCCESS)
1639 goto exit;
1640
1641 rc = RTFsQuerySerial(pszFullPath, &pSFDEntry->ulSerial);
1642 if (rc != VINF_SUCCESS)
1643 goto exit;
1644
1645 RTFSPROPERTIES FsProperties;
1646 rc = RTFsQueryProperties(pszFullPath, &FsProperties);
1647 if (rc != VINF_SUCCESS)
1648 goto exit;
1649 vbfsCopyFsPropertiesFromIprt(&pSFDEntry->fsProperties, &FsProperties);
1650
1651 *pcbBuffer = sizeof(SHFLVOLINFO);
1652 }
1653 else AssertFailed();
1654
1655exit:
1656 AssertMsg(rc == VINF_SUCCESS, ("failure: rc = %Rrc\n", rc));
1657 /* free the path string */
1658 vbsfFreeFullPath(pszFullPath);
1659 return rc;
1660}
1661
1662int vbsfQueryFSInfo(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint32_t flags, uint32_t *pcbBuffer, uint8_t *pBuffer)
1663{
1664 if (pcbBuffer == 0 || pBuffer == 0)
1665 {
1666 AssertFailed();
1667 return VERR_INVALID_PARAMETER;
1668 }
1669
1670 if (flags & SHFL_INFO_FILE)
1671 return vbsfQueryFileInfo(pClient, root, Handle, flags, pcbBuffer, pBuffer);
1672
1673 if (flags & SHFL_INFO_VOLUME)
1674 return vbsfQueryVolumeInfo(pClient, root, flags, pcbBuffer, pBuffer);
1675
1676 AssertFailed();
1677 return VERR_INVALID_PARAMETER;
1678}
1679
1680#ifdef UNITTEST
1681/** Unit test the SHFL_FN_INFORMATION API. Located here as a form of API
1682 * documentation. */
1683void testFSInfo(RTTEST hTest)
1684{
1685 /* If the number or types of parameters are wrong the API should fail. */
1686 testFSInfoBadParameters(hTest);
1687 /* Basic get and set file size test. */
1688 testFSInfoQuerySetFMode(hTest);
1689 /* Basic get and set dir atime test. */
1690 testFSInfoQuerySetDirATime(hTest);
1691 /* Basic get and set file atime test. */
1692 testFSInfoQuerySetFileATime(hTest);
1693 /* Basic set end of file. */
1694 testFSInfoQuerySetEndOfFile(hTest);
1695 /* Add tests as required... */
1696}
1697#endif
1698int vbsfSetFSInfo(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint32_t flags, uint32_t *pcbBuffer, uint8_t *pBuffer)
1699{
1700 uint32_t type = vbsfQueryHandleType(pClient, Handle)
1701 & (SHFL_HF_TYPE_DIR|SHFL_HF_TYPE_FILE|SHFL_HF_TYPE_VOLUME);
1702
1703 if (type == 0 || pcbBuffer == 0 || pBuffer == 0)
1704 {
1705 AssertFailed();
1706 return VERR_INVALID_PARAMETER;
1707 }
1708
1709 if (flags & SHFL_INFO_FILE)
1710 return vbsfSetFileInfo(pClient, root, Handle, flags, pcbBuffer, pBuffer);
1711
1712 if (flags & SHFL_INFO_SIZE)
1713 return vbsfSetEndOfFile(pClient, root, Handle, flags, pcbBuffer, pBuffer);
1714
1715// if (flags & SHFL_INFO_VOLUME)
1716// return vbsfVolumeInfo(pClient, root, Handle, flags, pcbBuffer, pBuffer);
1717 AssertFailed();
1718 return VERR_INVALID_PARAMETER;
1719}
1720
1721#ifdef UNITTEST
1722/** Unit test the SHFL_FN_LOCK API. Located here as a form of API
1723 * documentation. */
1724void testLock(RTTEST hTest)
1725{
1726 /* If the number or types of parameters are wrong the API should fail. */
1727 testLockBadParameters(hTest);
1728 /* Simple file locking and unlocking test. */
1729 testLockFileSimple(hTest);
1730 /* Add tests as required... */
1731}
1732#endif
1733
1734int vbsfLock(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint64_t offset, uint64_t length, uint32_t flags)
1735{
1736 SHFLFILEHANDLE *pHandle = vbsfQueryFileHandle(pClient, Handle);
1737 uint32_t fRTLock = 0;
1738
1739 Assert((flags & SHFL_LOCK_MODE_MASK) != SHFL_LOCK_CANCEL);
1740
1741 int rc = vbsfCheckHandleAccess(pClient, root, pHandle, VBSF_CHECK_ACCESS_READ);
1742 if (RT_SUCCESS(rc))
1743 { /* likely */ }
1744 else
1745 return rc;
1746
1747 if ( ((flags & SHFL_LOCK_MODE_MASK) == SHFL_LOCK_CANCEL)
1748 || (flags & SHFL_LOCK_ENTIRE)
1749 )
1750 {
1751 AssertFailed();
1752 return VERR_INVALID_PARAMETER;
1753 }
1754
1755 /* Lock type */
1756 switch(flags & SHFL_LOCK_MODE_MASK)
1757 {
1758 case SHFL_LOCK_SHARED:
1759 fRTLock = RTFILE_LOCK_READ;
1760 break;
1761
1762 case SHFL_LOCK_EXCLUSIVE:
1763 fRTLock = RTFILE_LOCK_READ | RTFILE_LOCK_WRITE;
1764 break;
1765
1766 default:
1767 AssertFailed();
1768 return VERR_INVALID_PARAMETER;
1769 }
1770
1771 /* Lock wait type */
1772 if (flags & SHFL_LOCK_WAIT)
1773 fRTLock |= RTFILE_LOCK_WAIT;
1774 else
1775 fRTLock |= RTFILE_LOCK_IMMEDIATELY;
1776
1777#ifdef RT_OS_WINDOWS
1778 rc = RTFileLock(pHandle->file.Handle, fRTLock, offset, length);
1779 if (rc != VINF_SUCCESS)
1780 Log(("RTFileLock %RTfile %RX64 %RX64 failed with %Rrc\n", pHandle->file.Handle, offset, length, rc));
1781#else
1782 Log(("vbsfLock: Pretend success handle=%x\n", Handle));
1783 rc = VINF_SUCCESS;
1784 RT_NOREF2(offset, length);
1785#endif
1786 return rc;
1787}
1788
1789int vbsfUnlock(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint64_t offset, uint64_t length, uint32_t flags)
1790{
1791 SHFLFILEHANDLE *pHandle = vbsfQueryFileHandle(pClient, Handle);
1792
1793 Assert((flags & SHFL_LOCK_MODE_MASK) == SHFL_LOCK_CANCEL);
1794
1795 int rc = vbsfCheckHandleAccess(pClient, root, pHandle, VBSF_CHECK_ACCESS_READ);
1796 if (RT_SUCCESS(rc))
1797 { /* likely */ }
1798 else
1799 return rc;
1800
1801 if ( ((flags & SHFL_LOCK_MODE_MASK) != SHFL_LOCK_CANCEL)
1802 || (flags & SHFL_LOCK_ENTIRE)
1803 )
1804 {
1805 return VERR_INVALID_PARAMETER;
1806 }
1807
1808#ifdef RT_OS_WINDOWS
1809 rc = RTFileUnlock(pHandle->file.Handle, offset, length);
1810 if (rc != VINF_SUCCESS)
1811 Log(("RTFileUnlock %RTfile %RX64 %RTX64 failed with %Rrc\n", pHandle->file.Handle, offset, length, rc));
1812#else
1813 Log(("vbsfUnlock: Pretend success handle=%x\n", Handle));
1814 rc = VINF_SUCCESS;
1815 RT_NOREF2(offset, length);
1816#endif
1817
1818 return rc;
1819}
1820
1821
1822#ifdef UNITTEST
1823/** Unit test the SHFL_FN_REMOVE API. Located here as a form of API
1824 * documentation. */
1825void testRemove(RTTEST hTest)
1826{
1827 /* If the number or types of parameters are wrong the API should fail. */
1828 testRemoveBadParameters(hTest);
1829 /* Add tests as required... */
1830}
1831#endif
1832int vbsfRemove(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pPath, uint32_t cbPath, uint32_t flags)
1833{
1834 int rc = VINF_SUCCESS;
1835
1836 /* Validate input */
1837 if ( flags & ~(SHFL_REMOVE_FILE|SHFL_REMOVE_DIR|SHFL_REMOVE_SYMLINK)
1838 || cbPath == 0
1839 || pPath == 0)
1840 {
1841 AssertFailed();
1842 return VERR_INVALID_PARAMETER;
1843 }
1844
1845 /* Build a host full path for the given path
1846 * and convert ucs2 to utf8 if necessary.
1847 */
1848 char *pszFullPath = NULL;
1849
1850 rc = vbsfBuildFullPath(pClient, root, pPath, cbPath, &pszFullPath, NULL);
1851 if (RT_SUCCESS(rc))
1852 {
1853 /* is the guest allowed to write to this share? */
1854 bool fWritable;
1855 rc = vbsfMappingsQueryWritable(pClient, root, &fWritable);
1856 if (RT_FAILURE(rc) || !fWritable)
1857 rc = VERR_WRITE_PROTECT;
1858
1859 if (RT_SUCCESS(rc))
1860 {
1861 if (flags & SHFL_REMOVE_SYMLINK)
1862 rc = RTSymlinkDelete(pszFullPath, 0);
1863 else if (flags & SHFL_REMOVE_FILE)
1864 rc = RTFileDelete(pszFullPath);
1865 else
1866 rc = RTDirRemove(pszFullPath);
1867 }
1868
1869#ifndef DEBUG_dmik
1870 // VERR_ACCESS_DENIED for example?
1871 // Assert(rc == VINF_SUCCESS || rc == VERR_DIR_NOT_EMPTY);
1872#endif
1873 /* free the path string */
1874 vbsfFreeFullPath(pszFullPath);
1875 }
1876 return rc;
1877}
1878
1879
1880#ifdef UNITTEST
1881/** Unit test the SHFL_FN_RENAME API. Located here as a form of API
1882 * documentation. */
1883void testRename(RTTEST hTest)
1884{
1885 /* If the number or types of parameters are wrong the API should fail. */
1886 testRenameBadParameters(hTest);
1887 /* Add tests as required... */
1888}
1889#endif
1890int vbsfRename(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pSrc, SHFLSTRING *pDest, uint32_t flags)
1891{
1892 int rc = VINF_SUCCESS;
1893
1894 /* Validate input */
1895 if ( flags & ~(SHFL_REMOVE_FILE|SHFL_REMOVE_DIR|SHFL_RENAME_REPLACE_IF_EXISTS)
1896 || pSrc == 0
1897 || pDest == 0)
1898 {
1899 AssertFailed();
1900 return VERR_INVALID_PARAMETER;
1901 }
1902
1903 /* Build a host full path for the given path
1904 * and convert ucs2 to utf8 if necessary.
1905 */
1906 char *pszFullPathSrc = NULL;
1907 char *pszFullPathDest = NULL;
1908
1909 rc = vbsfBuildFullPath(pClient, root, pSrc, pSrc->u16Size + SHFLSTRING_HEADER_SIZE, &pszFullPathSrc, NULL);
1910 if (rc != VINF_SUCCESS)
1911 return rc;
1912
1913 rc = vbsfBuildFullPath(pClient, root, pDest, pDest->u16Size + SHFLSTRING_HEADER_SIZE, &pszFullPathDest, NULL, false, true);
1914 if (RT_SUCCESS (rc))
1915 {
1916 Log(("Rename %s to %s\n", pszFullPathSrc, pszFullPathDest));
1917
1918 /* is the guest allowed to write to this share? */
1919 bool fWritable;
1920 rc = vbsfMappingsQueryWritable(pClient, root, &fWritable);
1921 if (RT_FAILURE(rc) || !fWritable)
1922 rc = VERR_WRITE_PROTECT;
1923
1924 if (RT_SUCCESS(rc))
1925 {
1926 if (flags & SHFL_RENAME_FILE)
1927 {
1928 rc = RTFileMove(pszFullPathSrc, pszFullPathDest,
1929 ((flags & SHFL_RENAME_REPLACE_IF_EXISTS) ? RTFILEMOVE_FLAGS_REPLACE : 0));
1930 }
1931 else
1932 {
1933 /* NT ignores the REPLACE flag and simply return and already exists error. */
1934 rc = RTDirRename(pszFullPathSrc, pszFullPathDest,
1935 ((flags & SHFL_RENAME_REPLACE_IF_EXISTS) ? RTPATHRENAME_FLAGS_REPLACE : 0));
1936 }
1937 }
1938
1939 /* free the path string */
1940 vbsfFreeFullPath(pszFullPathDest);
1941 }
1942 /* free the path string */
1943 vbsfFreeFullPath(pszFullPathSrc);
1944 return rc;
1945}
1946
1947#ifdef UNITTEST
1948/** Unit test the SHFL_FN_SYMLINK API. Located here as a form of API
1949 * documentation. */
1950void testSymlink(RTTEST hTest)
1951{
1952 /* If the number or types of parameters are wrong the API should fail. */
1953 testSymlinkBadParameters(hTest);
1954 /* Add tests as required... */
1955}
1956#endif
1957int vbsfSymlink(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pNewPath, SHFLSTRING *pOldPath, SHFLFSOBJINFO *pInfo)
1958{
1959 int rc = VINF_SUCCESS;
1960
1961 char *pszFullNewPath = NULL;
1962 char *pszFullOldPath = NULL;
1963
1964 /* XXX: no support for UCS2 at the moment. */
1965 if (!BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
1966 return VERR_NOT_IMPLEMENTED;
1967
1968 bool fSymlinksCreate;
1969 rc = vbsfMappingsQuerySymlinksCreate(pClient, root, &fSymlinksCreate);
1970 AssertRCReturn(rc, rc);
1971 if (!fSymlinksCreate)
1972 return VERR_WRITE_PROTECT; /* XXX or VERR_TOO_MANY_SYMLINKS? */
1973
1974 rc = vbsfBuildFullPath(pClient, root, pNewPath, pNewPath->u16Size + SHFLSTRING_HEADER_SIZE, &pszFullNewPath, NULL);
1975 AssertRCReturn(rc, rc);
1976
1977 /* Verify that the link target can be a valid host path, i.e. does not contain invalid characters. */
1978 uint32_t fu32PathFlags = 0;
1979 uint32_t fu32Options = 0;
1980 rc = vbsfPathGuestToHost(pClient, root, pOldPath, pOldPath->u16Size + SHFLSTRING_HEADER_SIZE,
1981 &pszFullOldPath, NULL, fu32Options, &fu32PathFlags);
1982 if (RT_FAILURE(rc))
1983 {
1984 vbsfFreeFullPath(pszFullNewPath);
1985 return rc;
1986 }
1987
1988 rc = RTSymlinkCreate(pszFullNewPath, (const char *)pOldPath->String.utf8,
1989 RTSYMLINKTYPE_UNKNOWN, 0);
1990 if (RT_SUCCESS(rc))
1991 {
1992 RTFSOBJINFO info;
1993 rc = RTPathQueryInfoEx(pszFullNewPath, &info, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient));
1994 if (RT_SUCCESS(rc))
1995 vbfsCopyFsObjInfoFromIprt(pInfo, &info);
1996 }
1997
1998 vbsfFreeFullPath(pszFullOldPath);
1999 vbsfFreeFullPath(pszFullNewPath);
2000
2001 return rc;
2002}
2003
2004/*
2005 * Clean up our mess by freeing all handles that are still valid.
2006 *
2007 */
2008int vbsfDisconnect(SHFLCLIENTDATA *pClient)
2009{
2010 for (int i = 0; i < SHFLHANDLE_MAX; ++i)
2011 {
2012 SHFLFILEHANDLE *pHandle = NULL;
2013 SHFLHANDLE Handle = (SHFLHANDLE)i;
2014
2015 uint32_t type = vbsfQueryHandleType(pClient, Handle);
2016 switch (type & (SHFL_HF_TYPE_DIR | SHFL_HF_TYPE_FILE))
2017 {
2018 case SHFL_HF_TYPE_DIR:
2019 {
2020 pHandle = vbsfQueryDirHandle(pClient, Handle);
2021 break;
2022 }
2023 case SHFL_HF_TYPE_FILE:
2024 {
2025 pHandle = vbsfQueryFileHandle(pClient, Handle);
2026 break;
2027 }
2028 default:
2029 break;
2030 }
2031
2032 if (pHandle)
2033 {
2034 LogFunc(("Opened handle 0x%08x\n", i));
2035 vbsfClose(pClient, pHandle->root, Handle);
2036 }
2037 }
2038 return VINF_SUCCESS;
2039}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette