VirtualBox

source: vbox/trunk/src/VBox/GuestHost/SharedClipboard/clipboard-transfers.cpp@ 100676

Last change on this file since 100676 was 100676, checked in by vboxsync, 17 months ago

Shared Clipboard: HTTP transfers with multiple files are also working now. bugref:9437

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 107.6 KB
Line 
1/* $Id: clipboard-transfers.cpp 100676 2023-07-21 11:26:04Z vboxsync $ */
2/** @file
3 * Shared Clipboard: Common clipboard transfer handling code.
4 */
5
6/*
7 * Copyright (C) 2019-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
29#include <VBox/log.h>
30
31#include <iprt/dir.h>
32#include <iprt/file.h>
33#include <iprt/list.h>
34#include <iprt/path.h>
35#include <iprt/rand.h>
36#include <iprt/semaphore.h>
37#include <iprt/uri.h>
38#include <iprt/utf16.h>
39
40#include <VBox/err.h>
41#include <VBox/HostServices/VBoxClipboardSvc.h>
42#include <VBox/GuestHost/clipboard-helper.h>
43#include <VBox/GuestHost/SharedClipboard-transfers.h>
44
45
46
47/*********************************************************************************************************************************
48 * Prototypes *
49 ********************************************************************************************************************************/
50
51static void shClTransferCopyCallbacks(PSHCLTRANSFERCALLBACKS pCallbacksDst, PSHCLTRANSFERCALLBACKS pCallbacksSrc);
52DECLINLINE(void) shClTransferLock(PSHCLTRANSFER pTransfer);
53DECLINLINE(void) shClTransferUnlock(PSHCLTRANSFER pTransfer);
54static void shClTransferSetCallbacks(PSHCLTRANSFER pTransfer, PSHCLTRANSFERCALLBACKS pCallbacks);
55static int shClTransferSetStatus(PSHCLTRANSFER pTransfer, SHCLTRANSFERSTATUS enmStatus);
56static int shClTransferThreadCreate(PSHCLTRANSFER pTransfer, PFNSHCLTRANSFERTHREAD pfnThreadFunc, void *pvUser);
57static int shClTransferThreadDestroy(PSHCLTRANSFER pTransfer, RTMSINTERVAL uTimeoutMs);
58
59static void shclTransferCtxTransferRemoveAndUnregister(PSHCLTRANSFERCTX pTransferCtx, PSHCLTRANSFER pTransfer);
60static PSHCLTRANSFER shClTransferCtxGetTransferByIdInternal(PSHCLTRANSFERCTX pTransferCtx, SHCLTRANSFERID uId);
61static PSHCLTRANSFER shClTransferCtxGetTransferByIndexInternal(PSHCLTRANSFERCTX pTransferCtx, uint32_t uIdx);
62
63
64/*********************************************************************************************************************************
65 * Transfer List *
66 ********************************************************************************************************************************/
67
68/**
69 * Initializes a transfer list.
70 *
71 * @param pList Transfer list to initialize.
72 */
73void ShClTransferListInit(PSHCLLIST pList)
74{
75 RT_ZERO(pList->Hdr);
76 RTListInit(&pList->lstEntries);
77}
78
79/**
80 * Destroys a transfer list.
81 *
82 * @param pList Transfer list to destroy.
83 */
84void ShClTransferListDestroy(PSHCLLIST pList)
85{
86 if (!pList)
87 return;
88
89 PSHCLLISTENTRY pEntry, pEntryNext;
90 RTListForEachSafe(&pList->lstEntries, pEntry, pEntryNext, SHCLLISTENTRY, Node)
91 {
92 RTListNodeRemove(&pEntry->Node);
93 ShClTransferListEntryDestroy(pEntry);
94 RTMemFree(pEntry);
95 }
96
97 RT_ZERO(pList->Hdr);
98}
99
100/**
101 * Adds a list entry to a transfer list.
102 *
103 * @returns VBox status code.
104 * @param pList Transfer list to add entry to.
105 * @param pEntry Entry to add.
106 * @param fAppend \c true to append to a list, or \c false to prepend.
107 */
108int ShClTransferListAddEntry(PSHCLLIST pList, PSHCLLISTENTRY pEntry, bool fAppend)
109{
110 AssertReturn(ShClTransferListEntryIsValid(pEntry), VERR_INVALID_PARAMETER);
111
112 if (fAppend)
113 RTListAppend(&pList->lstEntries, &pEntry->Node);
114 else
115 RTListPrepend(&pList->lstEntries, &pEntry->Node);
116 pList->Hdr.cEntries++;
117
118 LogFlowFunc(("%p: '%s' (%RU32 bytes) + %RU32 bytes info -> now %RU32 entries\n",
119 pList, pEntry->pszName, pEntry->cbName, pEntry->cbInfo, pList->Hdr.cEntries));
120
121 return VINF_SUCCESS;
122}
123
124/**
125 * Allocates a new transfer list.
126 *
127 * @returns Allocated transfer list on success, or NULL on failure.
128 */
129PSHCLLIST ShClTransferListAlloc(void)
130{
131 PSHCLLIST pList = (PSHCLLIST)RTMemAllocZ(sizeof(SHCLLIST));
132 if (pList)
133 {
134 ShClTransferListInit(pList);
135 return pList;
136 }
137
138 return NULL;
139}
140
141/**
142 * Frees a transfer list.
143 *
144 * @param pList Transfer list to free. The pointer will be
145 * invalid after returning from this function.
146 */
147void ShClTransferListFree(PSHCLLIST pList)
148{
149 if (!pList)
150 return;
151
152 ShClTransferListDestroy(pList);
153
154 RTMemFree(pList);
155 pList = NULL;
156}
157
158/**
159 * Returns a specific list entry of a transfer list.
160 *
161 * @returns Pointer to list entry if found, or NULL if not found.
162 * @param pList Clipboard transfer list to get list entry from.
163 * @param uIdx Index of list entry to return.
164 */
165DECLINLINE(PSHCLLISTENTRY) shClTransferListGetEntryById(PSHCLLIST pList, uint32_t uIdx)
166{
167 if (uIdx >= pList->Hdr.cEntries)
168 return NULL;
169
170 Assert(!RTListIsEmpty(&pList->lstEntries));
171
172 PSHCLLISTENTRY pIt = RTListGetFirst(&pList->lstEntries, SHCLLISTENTRY, Node);
173 while (uIdx) /** @todo Slow, but works for now. */
174 {
175 pIt = RTListGetNext(&pList->lstEntries, pIt, SHCLLISTENTRY, Node);
176 uIdx--;
177 }
178
179 return pIt;
180}
181
182/**
183 * Initializes an list handle info structure.
184 *
185 * @returns VBox status code.
186 * @param pInfo List handle info structure to initialize.
187 */
188int ShClTransferListHandleInfoInit(PSHCLLISTHANDLEINFO pInfo)
189{
190 AssertPtrReturn(pInfo, VERR_INVALID_POINTER);
191
192 pInfo->hList = NIL_SHCLLISTHANDLE;
193 pInfo->enmType = SHCLOBJTYPE_INVALID;
194
195 pInfo->pszPathLocalAbs = NULL;
196
197 RT_ZERO(pInfo->u);
198
199 return VINF_SUCCESS;
200}
201
202/**
203 * Destroys a list handle info structure.
204 *
205 * @param pInfo List handle info structure to destroy.
206 */
207void ShClTransferListHandleInfoDestroy(PSHCLLISTHANDLEINFO pInfo)
208{
209 if (!pInfo)
210 return;
211
212 if (pInfo->pszPathLocalAbs)
213 {
214 RTStrFree(pInfo->pszPathLocalAbs);
215 pInfo->pszPathLocalAbs = NULL;
216 }
217}
218
219/**
220 * Allocates a transfer list header structure.
221 *
222 * @returns VBox status code.
223 * @param ppListHdr Where to store the allocated transfer list header structure on success.
224 */
225int ShClTransferListHdrAlloc(PSHCLLISTHDR *ppListHdr)
226{
227 int rc;
228
229 PSHCLLISTHDR pListHdr = (PSHCLLISTHDR)RTMemAllocZ(sizeof(SHCLLISTHDR));
230 if (pListHdr)
231 {
232 *ppListHdr = pListHdr;
233 rc = VINF_SUCCESS;
234 }
235 else
236 rc = VERR_NO_MEMORY;
237
238 LogFlowFuncLeaveRC(rc);
239 return rc;
240}
241
242/**
243 * Frees a transfer list header structure.
244 *
245 * @param pListEntry Transfer list header structure to free.
246 * The pointer will be invalid on return.
247 */
248void ShClTransferListHdrFree(PSHCLLISTHDR pListHdr)
249{
250 if (!pListHdr)
251 return;
252
253 LogFlowFuncEnter();
254
255 ShClTransferListHdrDestroy(pListHdr);
256
257 RTMemFree(pListHdr);
258 pListHdr = NULL;
259}
260
261/**
262 * Duplicates (allocates) a transfer list header structure.
263 *
264 * @returns Duplicated transfer list header structure on success.
265 * @param pListHdr Transfer list header to duplicate.
266 */
267PSHCLLISTHDR ShClTransferListHdrDup(PSHCLLISTHDR pListHdr)
268{
269 AssertPtrReturn(pListHdr, NULL);
270
271 PSHCLLISTHDR pListHdrDup = (PSHCLLISTHDR)RTMemAlloc(sizeof(SHCLLISTHDR));
272 if (pListHdrDup)
273 *pListHdrDup = *pListHdr;
274
275 return pListHdrDup;
276}
277
278/**
279 * Initializes a transfer list header structure.
280 *
281 * @returns VBox status code.
282 * @param pListHdr Transfer list header struct to initialize.
283 */
284int ShClTransferListHdrInit(PSHCLLISTHDR pListHdr)
285{
286 AssertPtrReturn(pListHdr, VERR_INVALID_POINTER);
287
288 LogFlowFuncEnter();
289
290 ShClTransferListHdrReset(pListHdr);
291
292 return VINF_SUCCESS;
293}
294
295/**
296 * Destroys a transfer list header structure.
297 *
298 * @param pListHdr Transfer list header struct to destroy.
299 */
300void ShClTransferListHdrDestroy(PSHCLLISTHDR pListHdr)
301{
302 if (!pListHdr)
303 return;
304
305 LogFlowFuncEnter();
306}
307
308/**
309 * Resets a transfer list header structure.
310 *
311 * @returns VBox status code.
312 * @param pListHdr Transfer list header struct to reset.
313 */
314void ShClTransferListHdrReset(PSHCLLISTHDR pListHdr)
315{
316 AssertPtrReturnVoid(pListHdr);
317
318 LogFlowFuncEnter();
319
320 RT_BZERO(pListHdr, sizeof(SHCLLISTHDR));
321}
322
323/**
324 * Returns whether a given transfer list header is valid or not.
325 *
326 * @returns \c true if valid, \c false if not.
327 * @param pListHdr Transfer list header to validate.
328 */
329bool ShClTransferListHdrIsValid(PSHCLLISTHDR pListHdr)
330{
331 RT_NOREF(pListHdr);
332 return true; /** @todo Implement this. */
333}
334
335/**
336 * (Deep-)Copies a transfer list open parameters structure from one into another.
337 *
338 * @returns VBox status code.
339 * @param pDst Destination parameters to copy to.
340 * @param pSrc Source parameters to copy from.
341 */
342int ShClTransferListOpenParmsCopy(PSHCLLISTOPENPARMS pDst, PSHCLLISTOPENPARMS pSrc)
343{
344 AssertPtrReturn(pDst, VERR_INVALID_POINTER);
345 AssertPtrReturn(pSrc, VERR_INVALID_POINTER);
346
347 int rc = VINF_SUCCESS;
348
349 if (pSrc->pszFilter)
350 {
351 pDst->pszFilter = RTStrDup(pSrc->pszFilter);
352 if (!pDst->pszFilter)
353 rc = VERR_NO_MEMORY;
354 }
355
356 if ( RT_SUCCESS(rc)
357 && pSrc->pszPath)
358 {
359 pDst->pszPath = RTStrDup(pSrc->pszPath);
360 if (!pDst->pszPath)
361 rc = VERR_NO_MEMORY;
362 }
363
364 if (RT_SUCCESS(rc))
365 {
366 pDst->fList = pDst->fList;
367 pDst->cbFilter = pSrc->cbFilter;
368 pDst->cbPath = pSrc->cbPath;
369 }
370
371 return rc;
372}
373
374/**
375 * Duplicates a transfer list open parameters structure.
376 *
377 * @returns Duplicated transfer list open parameters structure on success, or NULL on failure.
378 * @param pParms Transfer list open parameters structure to duplicate.
379 */
380PSHCLLISTOPENPARMS ShClTransferListOpenParmsDup(PSHCLLISTOPENPARMS pParms)
381{
382 AssertPtrReturn(pParms, NULL);
383
384 PSHCLLISTOPENPARMS pParmsDup = (PSHCLLISTOPENPARMS)RTMemAllocZ(sizeof(SHCLLISTOPENPARMS));
385 if (!pParmsDup)
386 return NULL;
387
388 int rc = ShClTransferListOpenParmsCopy(pParmsDup, pParms);
389 if (RT_FAILURE(rc))
390 {
391 ShClTransferListOpenParmsDestroy(pParmsDup);
392
393 RTMemFree(pParmsDup);
394 pParmsDup = NULL;
395 }
396
397 return pParmsDup;
398}
399
400/**
401 * Initializes a transfer list open parameters structure.
402 *
403 * @returns VBox status code.
404 * @param pParms Transfer list open parameters structure to initialize.
405 */
406int ShClTransferListOpenParmsInit(PSHCLLISTOPENPARMS pParms)
407{
408 AssertPtrReturn(pParms, VERR_INVALID_POINTER);
409
410 RT_BZERO(pParms, sizeof(SHCLLISTOPENPARMS));
411
412 pParms->cbFilter = SHCL_TRANSFER_PATH_MAX; /** @todo Make this dynamic. */
413 pParms->pszFilter = RTStrAlloc(pParms->cbFilter);
414
415 pParms->cbPath = SHCL_TRANSFER_PATH_MAX; /** @todo Make this dynamic. */
416 pParms->pszPath = RTStrAlloc(pParms->cbPath);
417
418 LogFlowFuncLeave();
419 return VINF_SUCCESS;
420}
421
422/**
423 * Destroys a transfer list open parameters structure.
424 *
425 * @param pParms Transfer list open parameters structure to destroy.
426 */
427void ShClTransferListOpenParmsDestroy(PSHCLLISTOPENPARMS pParms)
428{
429 if (!pParms)
430 return;
431
432 if (pParms->pszFilter)
433 {
434 RTStrFree(pParms->pszFilter);
435 pParms->pszFilter = NULL;
436 }
437
438 if (pParms->pszPath)
439 {
440 RTStrFree(pParms->pszPath);
441 pParms->pszPath = NULL;
442 }
443}
444
445/**
446 * Creates (allocates) and initializes a clipboard list entry structure.
447 *
448 * @returns VBox status code.
449 * @param ppListEntry Where to return the created clipboard list entry structure on success.
450 * Must be free'd with ShClTransferListEntryFree().
451 */
452int ShClTransferListEntryAlloc(PSHCLLISTENTRY *ppListEntry)
453{
454 AssertPtrReturn(ppListEntry, VERR_INVALID_POINTER);
455
456 PSHCLLISTENTRY pListEntry = (PSHCLLISTENTRY)RTMemAllocZ(sizeof(SHCLLISTENTRY));
457 if (!pListEntry)
458 return VERR_NO_MEMORY;
459
460 *ppListEntry = pListEntry;
461
462 return VINF_SUCCESS;
463}
464
465/**
466 * Frees a clipboard list entry structure.
467 *
468 * @param pEntry Clipboard list entry structure to free.
469 * The pointer will be invalid on return.
470 */
471void ShClTransferListEntryFree(PSHCLLISTENTRY pEntry)
472{
473 if (!pEntry)
474 return;
475
476 /* Make sure to destroy the entry properly, in case the caller forgot this. */
477 ShClTransferListEntryDestroy(pEntry);
478
479 RTMemFree(pEntry);
480 pEntry = NULL;
481}
482
483/**
484 * (Deep-)Copies a clipboard list entry structure.
485 *
486 * @returns VBox status code.
487 * @param pDst Destination list entry to copy to.
488 * @param pSrc Source list entry to copy from.
489 */
490int ShClTransferListEntryCopy(PSHCLLISTENTRY pDst, PSHCLLISTENTRY pSrc)
491{
492 AssertPtrReturn(pDst, VERR_INVALID_POINTER);
493 AssertPtrReturn(pSrc, VERR_INVALID_POINTER);
494
495 int rc = VINF_SUCCESS;
496
497 *pDst = *pSrc;
498
499 if (pSrc->pszName)
500 {
501 pDst->pszName = RTStrDup(pSrc->pszName);
502 if (!pDst->pszName)
503 rc = VERR_NO_MEMORY;
504 }
505
506 if ( RT_SUCCESS(rc)
507 && pSrc->pvInfo)
508 {
509 pDst->pvInfo = RTMemDup(pSrc->pvInfo, pSrc->cbInfo);
510 if (pDst->pvInfo)
511 {
512 pDst->cbInfo = pSrc->cbInfo;
513 }
514 else
515 rc = VERR_NO_MEMORY;
516 }
517
518 if (RT_FAILURE(rc))
519 {
520 if (pDst->pvInfo)
521 {
522 RTMemFree(pDst->pvInfo);
523 pDst->pvInfo = NULL;
524 pDst->cbInfo = 0;
525 }
526 }
527
528 return rc;
529}
530
531/**
532 * Duplicates (allocates) a clipboard list entry structure.
533 *
534 * @returns Duplicated clipboard list entry structure on success.
535 * @param pEntry Clipboard list entry to duplicate.
536 */
537PSHCLLISTENTRY ShClTransferListEntryDup(PSHCLLISTENTRY pEntry)
538{
539 AssertPtrReturn(pEntry, NULL);
540
541 int rc = VINF_SUCCESS;
542
543 PSHCLLISTENTRY pListEntryDup = (PSHCLLISTENTRY)RTMemAllocZ(sizeof(SHCLLISTENTRY));
544 if (pListEntryDup)
545 rc = ShClTransferListEntryCopy(pListEntryDup, pEntry);
546
547 if (RT_FAILURE(rc))
548 {
549 ShClTransferListEntryDestroy(pListEntryDup);
550
551 RTMemFree(pListEntryDup);
552 pListEntryDup = NULL;
553 }
554
555 return pListEntryDup;
556}
557
558/**
559 * Returns whether a given list entry name is valid or not.
560 *
561 * @returns \c true if valid, or \c false if not.
562 * @param pszName Name to check.
563 * @param cbName Size (in bytes) of \a pszName to check.
564 * Includes terminator.
565 */
566static bool shclTransferListEntryNameIsValid(const char *pszName, size_t cbName)
567{
568 if (!pszName)
569 return false;
570
571 size_t const cchLen = strlen(pszName);
572
573 if ( !cbName
574 || cchLen == 0
575 || cchLen > cbName /* Includes zero termination */ - 1
576 || cchLen > SHCLLISTENTRY_MAX_NAME /* Ditto */ - 1)
577 {
578 return false;
579 }
580
581 int rc = ShClTransferValidatePath(pszName, false /* fMustExist */);
582 if (RT_FAILURE(rc))
583 return false;
584
585 return true;
586}
587
588/**
589 * Initializes a clipboard list entry structure, extended version.
590 *
591 * @returns VBox status code.
592 * @param pListEntry Clipboard list entry structure to initialize.
593 * @param fInfo Info flags (of type VBOX_SHCL_INFO_F_XXX).
594 * @param pszName Name (e.g. filename) to use. Can be NULL if not being used.
595 * Up to SHCLLISTENTRY_MAX_NAME characters.
596 * @param pvInfo Pointer to info data to assign. Must match \a fInfo.
597 * The list entry takes the ownership of the data on success.
598 * @param cbInfo Size (in bytes) of \a pvInfo data to assign.
599 */
600int ShClTransferListEntryInitEx(PSHCLLISTENTRY pListEntry, uint32_t fInfo, const char *pszName, void *pvInfo, uint32_t cbInfo)
601{
602 AssertPtrReturn(pListEntry, VERR_INVALID_POINTER);
603 AssertReturn ( pszName == NULL
604 || shclTransferListEntryNameIsValid(pszName, strlen(pszName) + 1), VERR_INVALID_PARAMETER);
605 /* pvInfo + cbInfo depend on fInfo. See below. */
606
607 RT_BZERO(pListEntry, sizeof(SHCLLISTENTRY));
608
609 if (pszName)
610 {
611 pListEntry->pszName = RTStrDupN(pszName, SHCLLISTENTRY_MAX_NAME);
612 AssertPtrReturn(pListEntry->pszName, VERR_NO_MEMORY);
613 pListEntry->cbName = (uint32_t)strlen(pListEntry->pszName) + 1 /* Include terminator */;
614 }
615
616 pListEntry->pvInfo = pvInfo;
617 pListEntry->cbInfo = cbInfo;
618 pListEntry->fInfo = fInfo;
619
620 return VINF_SUCCESS;
621}
622
623/**
624 * Initializes a clipboard list entry structure (as empty / invalid).
625 *
626 * @returns VBox status code.
627 * @param pListEntry Clipboard list entry structure to initialize.
628 */
629int ShClTransferListEntryInit(PSHCLLISTENTRY pListEntry)
630{
631 return ShClTransferListEntryInitEx(pListEntry, VBOX_SHCL_INFO_F_NONE, NULL /* pszName */, NULL /* pvInfo */, 0 /* cbInfo */);
632}
633
634/**
635 * Destroys a clipboard list entry structure.
636 *
637 * @param pListEntry Clipboard list entry structure to destroy.
638 */
639void ShClTransferListEntryDestroy(PSHCLLISTENTRY pListEntry)
640{
641 if (!pListEntry)
642 return;
643
644 if (pListEntry->pszName)
645 {
646 RTStrFree(pListEntry->pszName);
647
648 pListEntry->pszName = NULL;
649 pListEntry->cbName = 0;
650 }
651
652 if (pListEntry->pvInfo)
653 {
654 RTMemFree(pListEntry->pvInfo);
655 pListEntry->pvInfo = NULL;
656 pListEntry->cbInfo = 0;
657 }
658}
659
660/**
661 * Returns whether a given clipboard list entry is valid or not.
662 *
663 * @returns \c true if valid, \c false if not.
664 * @param pListEntry Clipboard list entry to validate.
665 */
666bool ShClTransferListEntryIsValid(PSHCLLISTENTRY pListEntry)
667{
668 AssertPtrReturn(pListEntry, false);
669
670 bool fValid = false;
671
672 if (shclTransferListEntryNameIsValid(pListEntry->pszName, pListEntry->cbName))
673 {
674 fValid = pListEntry->cbInfo == 0 /* cbInfo / pvInfo is optional. */
675 || pListEntry->pvInfo != NULL;
676 }
677
678 if (!fValid)
679 LogRel2(("Shared Clipboard: List entry '%s' is invalid\n", pListEntry->pszName));
680
681 return fValid;
682}
683
684
685/*********************************************************************************************************************************
686 * Transfer Object *
687 ********************************************************************************************************************************/
688
689/**
690 * Initializes a transfer object context.
691 *
692 * @returns VBox status code.
693 * @param pObjCtx Transfer object context to initialize.
694 */
695int ShClTransferObjCtxInit(PSHCLCLIENTTRANSFEROBJCTX pObjCtx)
696{
697 AssertPtrReturn(pObjCtx, VERR_INVALID_POINTER);
698
699 LogFlowFuncEnter();
700
701 pObjCtx->uHandle = NIL_SHCLOBJHANDLE;
702
703 return VINF_SUCCESS;
704}
705
706/**
707 * Destroys a transfer object context.
708 *
709 * @param pObjCtx Transfer object context to destroy.
710 */
711void ShClTransferObjCtxDestroy(PSHCLCLIENTTRANSFEROBJCTX pObjCtx)
712{
713 AssertPtrReturnVoid(pObjCtx);
714
715 LogFlowFuncEnter();
716}
717
718/**
719 * Returns if a transfer object context is valid or not.
720 *
721 * @returns \c true if valid, \c false if not.
722 * @param pObjCtx Transfer object context to check.
723 */
724bool ShClTransferObjCtxIsValid(PSHCLCLIENTTRANSFEROBJCTX pObjCtx)
725{
726 return ( pObjCtx
727 && pObjCtx->uHandle != NIL_SHCLOBJHANDLE);
728}
729
730/**
731 * Initializes a transfer object structure.
732 *
733 * @returns VBox status code.
734 * @param pObj Object structure to initialize.
735 */
736int ShClTransferObjInit(PSHCLTRANSFEROBJ pObj)
737{
738 AssertPtrReturn(pObj, VERR_INVALID_POINTER);
739
740 pObj->hObj = NIL_SHCLOBJHANDLE;
741 pObj->enmType = SHCLOBJTYPE_INVALID;
742
743 pObj->pszPathLocalAbs = NULL;
744
745 RT_ZERO(pObj->u);
746
747 return VINF_SUCCESS;
748}
749
750/**
751 * Destroys a transfer object structure.
752 *
753 * @param pObj Object structure to destroy.
754 */
755void ShClTransferObjDestroy(PSHCLTRANSFEROBJ pObj)
756{
757 if (!pObj)
758 return;
759
760 if (pObj->pszPathLocalAbs)
761 {
762 RTStrFree(pObj->pszPathLocalAbs);
763 pObj->pszPathLocalAbs = NULL;
764 }
765}
766
767/**
768 * Initializes a transfer object open parameters structure.
769 *
770 * @returns VBox status code.
771 * @param pParms Transfer object open parameters structure to initialize.
772 */
773int ShClTransferObjOpenParmsInit(PSHCLOBJOPENCREATEPARMS pParms)
774{
775 AssertPtrReturn(pParms, VERR_INVALID_POINTER);
776
777 int rc;
778
779 RT_BZERO(pParms, sizeof(SHCLOBJOPENCREATEPARMS));
780
781 pParms->cbPath = RTPATH_MAX; /** @todo Make this dynamic. */
782 pParms->pszPath = RTStrAlloc(pParms->cbPath);
783 if (pParms->pszPath)
784 {
785 rc = VINF_SUCCESS;
786 }
787 else
788 rc = VERR_NO_MEMORY;
789
790 LogFlowFuncLeaveRC(rc);
791 return rc;
792}
793
794/**
795 * Copies a transfer object open parameters structure from source to destination.
796 *
797 * @returns VBox status code.
798 * @param pParmsDst Where to copy the source transfer object open parameters to.
799 * @param pParmsSrc Which source transfer object open parameters to copy.
800 */
801int ShClTransferObjOpenParmsCopy(PSHCLOBJOPENCREATEPARMS pParmsDst, PSHCLOBJOPENCREATEPARMS pParmsSrc)
802{
803 int rc;
804
805 *pParmsDst = *pParmsSrc;
806
807 if (pParmsSrc->pszPath)
808 {
809 Assert(pParmsSrc->cbPath);
810 pParmsDst->pszPath = RTStrDup(pParmsSrc->pszPath);
811 if (pParmsDst->pszPath)
812 {
813 rc = VINF_SUCCESS;
814 }
815 else
816 rc = VERR_NO_MEMORY;
817 }
818 else
819 rc = VINF_SUCCESS;
820
821 LogFlowFuncLeaveRC(rc);
822 return rc;
823}
824
825/**
826 * Destroys a transfer object open parameters structure.
827 *
828 * @param pParms Transfer object open parameters structure to destroy.
829 */
830void ShClTransferObjOpenParmsDestroy(PSHCLOBJOPENCREATEPARMS pParms)
831{
832 if (!pParms)
833 return;
834
835 if (pParms->pszPath)
836 {
837 RTStrFree(pParms->pszPath);
838 pParms->pszPath = NULL;
839 }
840}
841
842/**
843 * Returns a specific transfer object of a transfer.
844 *
845 * @returns Pointer to transfer object if found, or NULL if not found.
846 * @param pTransfer Clipboard transfer to get transfer object from.
847 * @param hObj Object handle of the object to get handle info for.
848 */
849PSHCLTRANSFEROBJ ShClTransferObjGet(PSHCLTRANSFER pTransfer, SHCLOBJHANDLE hObj)
850{
851 PSHCLTRANSFEROBJ pIt;
852 RTListForEach(&pTransfer->lstObj, pIt, SHCLTRANSFEROBJ, Node) /** @todo Slooow ...but works for now. */
853 {
854 if (pIt->hObj == hObj)
855 return pIt;
856 }
857
858 return NULL;
859}
860
861/**
862 * Opens a transfer object.
863 *
864 * @returns VBox status code.
865 * @param pTransfer Clipboard transfer to open the object for.
866 * @param pOpenCreateParms Open / create parameters of transfer object to open / create.
867 * @param phObj Where to store the handle of transfer object opened on success.
868 */
869int ShClTransferObjOpen(PSHCLTRANSFER pTransfer, PSHCLOBJOPENCREATEPARMS pOpenCreateParms, PSHCLOBJHANDLE phObj)
870{
871 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
872 AssertPtrReturn(pOpenCreateParms, VERR_INVALID_POINTER);
873 AssertPtrReturn(phObj, VERR_INVALID_POINTER);
874 AssertMsgReturn(pTransfer->pszPathRootAbs, ("Transfer has no root path set\n"), VERR_INVALID_PARAMETER);
875 /** @todo Check pOpenCreateParms->fCreate flags. */
876 AssertMsgReturn(pOpenCreateParms->pszPath, ("No path in open/create params set\n"), VERR_INVALID_PARAMETER);
877
878 if (pTransfer->cObjHandles >= pTransfer->cMaxObjHandles)
879 return VERR_SHCLPB_MAX_OBJECTS_REACHED;
880
881 LogFlowFunc(("pszPath=%s, fCreate=0x%x\n", pOpenCreateParms->pszPath, pOpenCreateParms->fCreate));
882
883 int rc;
884 if (pTransfer->ProviderIface.pfnObjOpen)
885 rc = pTransfer->ProviderIface.pfnObjOpen(&pTransfer->ProviderCtx, pOpenCreateParms, phObj);
886 else
887 rc = VERR_NOT_SUPPORTED;
888
889 if (RT_FAILURE(rc))
890 LogRel(("Shared Clipboard: Opening object '%s' (flags %#x) failed with %Rrc\n",
891 pOpenCreateParms->pszPath, pOpenCreateParms->fCreate, rc));
892
893 LogFlowFuncLeaveRC(rc);
894 return rc;
895}
896
897/**
898 * Closes a transfer object.
899 *
900 * @returns VBox status code.
901 * @param pTransfer Clipboard transfer that contains the object to close.
902 * @param hObj Handle of transfer object to close.
903 */
904int ShClTransferObjClose(PSHCLTRANSFER pTransfer, SHCLOBJHANDLE hObj)
905{
906 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
907
908 int rc;
909 if (pTransfer->ProviderIface.pfnObjClose)
910 rc = pTransfer->ProviderIface.pfnObjClose(&pTransfer->ProviderCtx, hObj);
911 else
912 rc = VERR_NOT_SUPPORTED;
913
914 if (RT_FAILURE(rc))
915 LogRel(("Shared Clipboard: Reading object 0x%x failed with %Rrc\n", hObj, rc));
916
917 LogFlowFuncLeaveRC(rc);
918 return rc;
919}
920
921/**
922 * Reads from a transfer object.
923 *
924 * @returns VBox status code.
925 * @param pTransfer Clipboard transfer that contains the object to read from.
926 * @param hObj Handle of transfer object to read from.
927 * @param pvBuf Buffer for where to store the read data.
928 * @param cbBuf Size (in bytes) of buffer.
929 * @param fFlags Read flags. Optional.
930 * @param pcbRead Where to return how much bytes were read on success. Optional.
931 */
932int ShClTransferObjRead(PSHCLTRANSFER pTransfer,
933 SHCLOBJHANDLE hObj, void *pvBuf, uint32_t cbBuf, uint32_t fFlags, uint32_t *pcbRead)
934{
935 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
936 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
937 AssertReturn (cbBuf, VERR_INVALID_PARAMETER);
938 /* pcbRead is optional. */
939 /** @todo Validate fFlags. */
940
941 int rc;
942 if (pTransfer->ProviderIface.pfnObjRead)
943 rc = pTransfer->ProviderIface.pfnObjRead(&pTransfer->ProviderCtx, hObj, pvBuf, cbBuf, fFlags, pcbRead);
944 else
945 rc = VERR_NOT_SUPPORTED;
946
947 if (RT_FAILURE(rc))
948 LogRel(("Shared Clipboard: Reading object 0x%x failed with %Rrc\n", hObj, rc));
949
950 LogFlowFuncLeaveRC(rc);
951 return rc;
952}
953
954/**
955 * Writes to a transfer object.
956 *
957 * @returns VBox status code.
958 * @param pTransfer Clipboard transfer that contains the object to write to.
959 * @param hObj Handle of transfer object to write to.
960 * @param pvBuf Buffer of data to write.
961 * @param cbBuf Size (in bytes) of buffer to write.
962 * @param fFlags Write flags. Optional.
963 * @param pcbWritten How much bytes were writtenon success. Optional.
964 */
965int ShClTransferObjWrite(PSHCLTRANSFER pTransfer,
966 SHCLOBJHANDLE hObj, void *pvBuf, uint32_t cbBuf, uint32_t fFlags, uint32_t *pcbWritten)
967{
968 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
969 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
970 AssertReturn (cbBuf, VERR_INVALID_PARAMETER);
971 /* pcbWritten is optional. */
972
973 int rc;
974 if (pTransfer->ProviderIface.pfnObjWrite)
975 rc = pTransfer->ProviderIface.pfnObjWrite(&pTransfer->ProviderCtx, hObj, pvBuf, cbBuf, fFlags, pcbWritten);
976 else
977 rc = VERR_NOT_SUPPORTED;
978
979 if (RT_FAILURE(rc))
980 LogRel(("Shared Clipboard: Writing object 0x%x failed with %Rrc\n", hObj, rc));
981
982 LogFlowFuncLeaveRC(rc);
983 return rc;
984}
985
986/**
987 * Duplicates a transfer object data chunk.
988 *
989 * @returns Duplicated object data chunk on success, or NULL on failure.
990 * @param pDataChunk Transfer object data chunk to duplicate.
991 */
992PSHCLOBJDATACHUNK ShClTransferObjDataChunkDup(PSHCLOBJDATACHUNK pDataChunk)
993{
994 AssertPtrReturn(pDataChunk, NULL);
995
996 PSHCLOBJDATACHUNK pDataChunkDup = (PSHCLOBJDATACHUNK)RTMemAllocZ(sizeof(SHCLOBJDATACHUNK));
997 if (!pDataChunkDup)
998 return NULL;
999
1000 if (pDataChunk->pvData)
1001 {
1002 Assert(pDataChunk->cbData);
1003
1004 pDataChunkDup->uHandle = pDataChunk->uHandle;
1005 pDataChunkDup->pvData = RTMemDup(pDataChunk->pvData, pDataChunk->cbData);
1006 AssertPtrReturn(pDataChunkDup->pvData, NULL);
1007 pDataChunkDup->cbData = pDataChunk->cbData;
1008 }
1009
1010 return pDataChunkDup;
1011}
1012
1013/**
1014 * Destroys a transfer object data chunk.
1015 *
1016 * @param pDataChunk Transfer object data chunk to destroy.
1017 */
1018void ShClTransferObjDataChunkDestroy(PSHCLOBJDATACHUNK pDataChunk)
1019{
1020 if (!pDataChunk)
1021 return;
1022
1023 if (pDataChunk->pvData)
1024 {
1025 Assert(pDataChunk->cbData);
1026
1027 RTMemFree(pDataChunk->pvData);
1028
1029 pDataChunk->pvData = NULL;
1030 pDataChunk->cbData = 0;
1031 }
1032
1033 pDataChunk->uHandle = 0;
1034}
1035
1036/**
1037 * Frees a transfer object data chunk.
1038 *
1039 * @param pDataChunk Transfer object data chunk to free.
1040 * The pointer will be invalid on return.
1041 */
1042void ShClTransferObjDataChunkFree(PSHCLOBJDATACHUNK pDataChunk)
1043{
1044 if (!pDataChunk)
1045 return;
1046
1047 ShClTransferObjDataChunkDestroy(pDataChunk);
1048
1049 RTMemFree(pDataChunk);
1050 pDataChunk = NULL;
1051}
1052
1053
1054/*********************************************************************************************************************************
1055 * Transfer *
1056 ********************************************************************************************************************************/
1057
1058/**
1059 * Creates a clipboard transfer, extended version.
1060 *
1061 * @returns VBox status code.
1062 * @param enmDir Specifies the transfer direction of this transfer.
1063 * @param enmSource Specifies the data source of the transfer.
1064 * @param pCallbacks Callback table to use. Optional and can be NULL.
1065 * @param cbMaxChunkSize Maximum transfer chunk size (in bytes) to use.
1066 * @param cMaxListHandles Maximum list entries the transfer can have.
1067 * @param cMaxObjHandles Maximum transfer objects the transfer can have.
1068 * @param ppTransfer Where to return the created clipboard transfer struct.
1069 * Must be destroyed by ShClTransferDestroy().
1070 */
1071int ShClTransferCreateEx(SHCLTRANSFERDIR enmDir, SHCLSOURCE enmSource, PSHCLTRANSFERCALLBACKS pCallbacks,
1072 uint32_t cbMaxChunkSize, uint32_t cMaxListHandles, uint32_t cMaxObjHandles, PSHCLTRANSFER *ppTransfer)
1073{
1074 AssertPtrReturn(ppTransfer, VERR_INVALID_POINTER);
1075 /* pCallbacks can be NULL. */
1076
1077 LogFlowFuncEnter();
1078
1079 PSHCLTRANSFER pTransfer = (PSHCLTRANSFER)RTMemAllocZ(sizeof(SHCLTRANSFER));
1080 AssertPtrReturn(pTransfer, VERR_NO_MEMORY);
1081
1082 pTransfer->State.uID = NIL_SHCLTRANSFERID;
1083 pTransfer->State.enmStatus = SHCLTRANSFERSTATUS_NONE;
1084 pTransfer->State.enmDir = enmDir;
1085 pTransfer->State.enmSource = enmSource;
1086
1087 pTransfer->Thread.hThread = NIL_RTTHREAD;
1088 pTransfer->Thread.fCancelled = false;
1089 pTransfer->Thread.fStarted = false;
1090 pTransfer->Thread.fStop = false;
1091
1092 pTransfer->pszPathRootAbs = NULL;
1093
1094 pTransfer->uTimeoutMs = SHCL_TIMEOUT_DEFAULT_MS;
1095 pTransfer->cbMaxChunkSize = cbMaxChunkSize;
1096 pTransfer->cMaxListHandles = cMaxListHandles;
1097 pTransfer->cMaxObjHandles = cMaxObjHandles;
1098
1099 pTransfer->pvUser = NULL;
1100 pTransfer->cbUser = 0;
1101
1102 RTListInit(&pTransfer->lstHandles);
1103 RTListInit(&pTransfer->lstObj);
1104
1105 /* The provider context + interface is NULL by default. */
1106 RT_ZERO(pTransfer->ProviderCtx);
1107 RT_ZERO(pTransfer->ProviderIface);
1108
1109 /* Make sure to set the callbacks before calling pfnOnCreate below. */
1110 shClTransferSetCallbacks(pTransfer, pCallbacks);
1111
1112 ShClTransferListInit(&pTransfer->lstRoots);
1113
1114 int rc = RTCritSectInit(&pTransfer->CritSect);
1115 AssertRCReturn(rc, rc);
1116
1117 rc = RTSemEventCreate(&pTransfer->StatusChangeEvent);
1118 AssertRCReturn(rc, rc);
1119
1120 rc = ShClEventSourceCreate(&pTransfer->Events, 0 /* uID */);
1121 if (RT_SUCCESS(rc))
1122 {
1123 if (pTransfer->Callbacks.pfnOnCreated)
1124 pTransfer->Callbacks.pfnOnCreated(&pTransfer->CallbackCtx);
1125
1126 *ppTransfer = pTransfer;
1127 }
1128 else
1129 {
1130 if (pTransfer)
1131 {
1132 ShClTransferDestroy(pTransfer);
1133 RTMemFree(pTransfer);
1134 }
1135 }
1136
1137 LogFlowFuncLeaveRC(rc);
1138 return rc;
1139}
1140
1141/**
1142 * Creates a clipboard transfer with default settings.
1143 *
1144 * @returns VBox status code.
1145 * @param enmDir Specifies the transfer direction of this transfer.
1146 * @param enmSource Specifies the data source of the transfer.
1147 * @param pCallbacks Callback table to use. Optional and can be NULL.
1148 * @param ppTransfer Where to return the created clipboard transfer struct.
1149 * Must be destroyed by ShClTransferDestroy().
1150 */
1151int ShClTransferCreate(SHCLTRANSFERDIR enmDir, SHCLSOURCE enmSource, PSHCLTRANSFERCALLBACKS pCallbacks, PSHCLTRANSFER *ppTransfer)
1152{
1153 return ShClTransferCreateEx(enmDir, enmSource, pCallbacks,
1154 SHCL_TRANSFER_DEFAULT_MAX_CHUNK_SIZE,
1155 SHCL_TRANSFER_DEFAULT_MAX_LIST_HANDLES,
1156 SHCL_TRANSFER_DEFAULT_MAX_OBJ_HANDLES,
1157 ppTransfer);
1158}
1159
1160/**
1161 * Destroys a clipboard transfer.
1162 *
1163 * @returns VBox status code.
1164 * @param pTransferCtx Clipboard transfer to destroy.
1165 */
1166int ShClTransferDestroy(PSHCLTRANSFER pTransfer)
1167{
1168 if (!pTransfer)
1169 return VINF_SUCCESS;
1170
1171 if (!RTCritSectIsInitialized(&pTransfer->CritSect))
1172 return VINF_SUCCESS;
1173
1174 /* Must come before the refcount check below, as the callback might release a reference. */
1175 if (pTransfer->Callbacks.pfnOnDestroy)
1176 pTransfer->Callbacks.pfnOnDestroy(&pTransfer->CallbackCtx);
1177
1178 AssertMsgReturn(ASMAtomicReadU32(&pTransfer->cRefs) == 0,
1179 ("Number of references > 0 (%RU32)\n", pTransfer->cRefs), VERR_WRONG_ORDER);
1180
1181 LogFlowFuncEnter();
1182
1183 int rc = shClTransferThreadDestroy(pTransfer, SHCL_TIMEOUT_DEFAULT_MS);
1184 if (RT_FAILURE(rc))
1185 return rc;
1186
1187 ShClTransferReset(pTransfer);
1188
1189 if (RTCritSectIsInitialized(&pTransfer->CritSect))
1190 RTCritSectDelete(&pTransfer->CritSect);
1191
1192 rc = RTSemEventDestroy(pTransfer->StatusChangeEvent);
1193 AssertRCReturn(rc, rc);
1194 pTransfer->StatusChangeEvent = NIL_RTSEMEVENT;
1195
1196 ShClEventSourceDestroy(&pTransfer->Events);
1197
1198 LogFlowFuncLeave();
1199 return VINF_SUCCESS;
1200}
1201
1202/**
1203 * Initializes a clipboard transfer.
1204 *
1205 * @returns VBox status code.
1206 * @param pTransfer Transfer to initialize.
1207 */
1208int ShClTransferInit(PSHCLTRANSFER pTransfer)
1209{
1210 shClTransferLock(pTransfer);
1211
1212 AssertMsgReturnStmt(pTransfer->State.enmStatus < SHCLTRANSFERSTATUS_INITIALIZED,
1213 ("Wrong status (currently is %s)\n", ShClTransferStatusToStr(pTransfer->State.enmStatus)),
1214 shClTransferUnlock(pTransfer), VERR_WRONG_ORDER);
1215
1216 pTransfer->cRefs = 0;
1217
1218 LogFlowFunc(("uID=%RU32, enmDir=%RU32, enmSource=%RU32\n",
1219 pTransfer->State.uID, pTransfer->State.enmDir, pTransfer->State.enmSource));
1220
1221 pTransfer->cListHandles = 0;
1222 pTransfer->uListHandleNext = 1;
1223
1224 pTransfer->cObjHandles = 0;
1225 pTransfer->uObjHandleNext = 1;
1226
1227 pTransfer->Thread.fStarted = false;
1228 pTransfer->Thread.fStop = false;
1229 pTransfer->Thread.fCancelled = false;
1230
1231 int rc = shClTransferSetStatus(pTransfer, SHCLTRANSFERSTATUS_INITIALIZED);
1232
1233 shClTransferUnlock(pTransfer);
1234
1235 if (RT_SUCCESS(rc))
1236 {
1237 if (pTransfer->Callbacks.pfnOnInitialized)
1238 pTransfer->Callbacks.pfnOnInitialized(&pTransfer->CallbackCtx);
1239 }
1240
1241 LogFlowFuncLeaveRC(rc);
1242 return rc;
1243}
1244
1245/**
1246 * Returns whether a transfer is in a running state or not.
1247 *
1248 * @returns @c true if in running state, or @c false if not.
1249 * @param pTransfer Clipboard transfer to return status for.
1250 */
1251bool ShClTransferIsRunning(PSHCLTRANSFER pTransfer)
1252{
1253 shClTransferLock(pTransfer);
1254
1255 bool const fRunning = pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_STARTED;
1256
1257 shClTransferUnlock(pTransfer);
1258
1259 return fRunning;
1260}
1261
1262/**
1263 * Returns whether a transfer has been (successfully) completed or not.
1264 *
1265 * @returns @c true if complete, or @c false if not.
1266 * @param pTransfer Clipboard transfer to return status for.
1267 */
1268bool ShClTransferIsComplete(PSHCLTRANSFER pTransfer)
1269{
1270 shClTransferLock(pTransfer);
1271
1272 bool const fCompleted = pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_COMPLETED;
1273
1274 shClTransferUnlock(pTransfer);
1275
1276 return fCompleted;
1277}
1278
1279/**
1280 * Returns whether a transfer has been aborted due to cancelling, killing or an error.
1281 *
1282 * @returns @c true if in aborted state, or @c false if not.
1283 * @param pTransfer Clipboard transfer to return status for.
1284 */
1285bool ShClTransferIsAborted(PSHCLTRANSFER pTransfer)
1286{
1287 shClTransferLock(pTransfer);
1288
1289 bool const fAborted = pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_CANCELED
1290 || pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_KILLED
1291 || pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_ERROR;
1292
1293 shClTransferUnlock(pTransfer);
1294
1295 return fAborted;
1296}
1297
1298/**
1299 * Locks a transfer.
1300 *
1301 * @param pTransfer Transfer to lock.
1302 */
1303DECLINLINE(void) shClTransferLock(PSHCLTRANSFER pTransfer)
1304{
1305 int rc2 = RTCritSectEnter(&pTransfer->CritSect);
1306 AssertRC(rc2);
1307}
1308
1309/**
1310 * Unlocks a transfer.
1311 *
1312 * @param pTransfer Transfer to unlock.
1313 */
1314DECLINLINE(void) shClTransferUnlock(PSHCLTRANSFER pTransfer)
1315{
1316 int rc2 = RTCritSectLeave(&pTransfer->CritSect);
1317 AssertRC(rc2);
1318}
1319
1320/**
1321 * Acquires a reference to this transfer.
1322 *
1323 * @returns New reference count.
1324 * @param pTransfer Transfer to acquire reference for.
1325 */
1326uint32_t ShClTransferAcquire(PSHCLTRANSFER pTransfer)
1327{
1328 return ASMAtomicIncU32(&pTransfer->cRefs);
1329}
1330
1331/**
1332 * Releases a reference to this transfer.
1333 *
1334 * @returns New reference count.
1335 * @param pTransfer Transfer to release reference for.
1336 */
1337uint32_t ShClTransferRelease(PSHCLTRANSFER pTransfer)
1338{
1339 const uint32_t cRefs = ASMAtomicDecU32(&pTransfer->cRefs);
1340 Assert(pTransfer->cRefs <= VBOX_SHCL_MAX_TRANSFERS); /* Not perfect, but better than nothing. */
1341 return cRefs;
1342}
1343
1344/**
1345 * Opens a transfer list.
1346 *
1347 * @returns VBox status code.
1348 * @param pTransfer Clipboard transfer to handle.
1349 * @param pOpenParms List open parameters to use for opening.
1350 * @param phList Where to store the List handle of opened list on success.
1351 */
1352int ShClTransferListOpen(PSHCLTRANSFER pTransfer, PSHCLLISTOPENPARMS pOpenParms,
1353 PSHCLLISTHANDLE phList)
1354{
1355 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
1356 AssertPtrReturn(pOpenParms, VERR_INVALID_POINTER);
1357 AssertPtrReturn(phList, VERR_INVALID_POINTER);
1358
1359 if (pTransfer->cListHandles == pTransfer->cMaxListHandles)
1360 return VERR_SHCLPB_MAX_LISTS_REACHED;
1361
1362 int rc;
1363 if (pTransfer->ProviderIface.pfnListOpen)
1364 rc = pTransfer->ProviderIface.pfnListOpen(&pTransfer->ProviderCtx, pOpenParms, phList);
1365 else
1366 rc = VERR_NOT_SUPPORTED;
1367
1368 if (RT_FAILURE(rc))
1369 LogRel(("Shared Clipboard: Opening list '%s' (fiter '%s', flags %#x) failed with %Rrc\n",
1370 pOpenParms->pszPath, pOpenParms->pszFilter, pOpenParms->fList, rc));
1371
1372 LogFlowFuncLeaveRC(rc);
1373 return rc;
1374}
1375
1376/**
1377 * Closes a transfer list.
1378 *
1379 * @returns VBox status code.
1380 * @param pTransfer Clipboard transfer to handle.
1381 * @param hList Handle of list to close.
1382 */
1383int ShClTransferListClose(PSHCLTRANSFER pTransfer, SHCLLISTHANDLE hList)
1384{
1385 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
1386
1387 if (hList == NIL_SHCLLISTHANDLE)
1388 return VINF_SUCCESS;
1389
1390 int rc;
1391 if (pTransfer->ProviderIface.pfnListClose)
1392 rc = pTransfer->ProviderIface.pfnListClose(&pTransfer->ProviderCtx, hList);
1393 else
1394 rc = VERR_NOT_SUPPORTED;
1395
1396 if (RT_FAILURE(rc))
1397 LogRel(("Shared Clipboard: Closing list 0x%x entry failed with %Rrc\n", hList, rc));
1398
1399 LogFlowFuncLeaveRC(rc);
1400 return rc;
1401}
1402
1403/**
1404 * Retrieves the header of a transfer list.
1405 *
1406 * @returns VBox status code.
1407 * @param pTransfer Clipboard transfer to handle.
1408 * @param hList Handle of list to get header for.
1409 * @param pHdr Where to store the returned list header information.
1410 */
1411int ShClTransferListGetHeader(PSHCLTRANSFER pTransfer, SHCLLISTHANDLE hList,
1412 PSHCLLISTHDR pHdr)
1413{
1414 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
1415 AssertPtrReturn(pHdr, VERR_INVALID_POINTER);
1416
1417 LogFlowFunc(("hList=%RU64\n", hList));
1418
1419 int rc;
1420 if (pTransfer->ProviderIface.pfnListHdrRead)
1421 rc = pTransfer->ProviderIface.pfnListHdrRead(&pTransfer->ProviderCtx, hList, pHdr);
1422 else
1423 rc = VERR_NOT_SUPPORTED;
1424
1425 if (RT_FAILURE(rc))
1426 LogRel(("Shared Clipboard: Reading list header list 0x%x entry failed with %Rrc\n", hList, rc));
1427
1428 LogFlowFuncLeaveRC(rc);
1429 return rc;
1430}
1431
1432/**
1433 * Returns a specific list handle info of a clipboard transfer.
1434 *
1435 * @returns Pointer to list handle info if found, or NULL if not found.
1436 * @param pTransfer Clipboard transfer to get list handle info from.
1437 * @param hList List handle of the list to get handle info for.
1438 */
1439PSHCLLISTHANDLEINFO ShClTransferListGetByHandle(PSHCLTRANSFER pTransfer, SHCLLISTHANDLE hList)
1440{
1441 PSHCLLISTHANDLEINFO pIt;
1442 RTListForEach(&pTransfer->lstHandles, pIt, SHCLLISTHANDLEINFO, Node) /** @todo Sloooow ... improve this. */
1443 {
1444 if (pIt->hList == hList)
1445 return pIt;
1446 }
1447
1448 return NULL;
1449}
1450
1451/**
1452 * Returns the a transfer object of a transfer list.
1453 *
1454 * Currently not implemented and wil return NULL.
1455 *
1456 * @returns Pointer to transfer object, or NULL if not found / invalid.
1457 * @param pTransfer Clipboard transfer to return transfer object for.
1458 * @param hList Handle of clipboard transfer list to get object for.
1459 * @param uIdx Index of object to get.
1460 */
1461PSHCLTRANSFEROBJ ShClTransferListGetObj(PSHCLTRANSFER pTransfer,
1462 SHCLLISTHANDLE hList, uint64_t uIdx)
1463{
1464 AssertPtrReturn(pTransfer, NULL);
1465
1466 RT_NOREF(hList, uIdx);
1467
1468 LogFlowFunc(("hList=%RU64\n", hList));
1469
1470 return NULL;
1471}
1472
1473/**
1474 * Reads a single transfer list entry.
1475 *
1476 * @returns VBox status code or VERR_NO_MORE_FILES if the end of the list has been reached.
1477 * @param pTransfer Clipboard transfer to handle.
1478 * @param hList List handle of list to read from.
1479 * @param pEntry Where to store the read information.
1480 */
1481int ShClTransferListRead(PSHCLTRANSFER pTransfer, SHCLLISTHANDLE hList,
1482 PSHCLLISTENTRY pEntry)
1483{
1484 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
1485 AssertPtrReturn(pEntry, VERR_INVALID_POINTER);
1486
1487 LogFlowFunc(("hList=%RU64\n", hList));
1488
1489 int rc;
1490 if (pTransfer->ProviderIface.pfnListEntryRead)
1491 rc = pTransfer->ProviderIface.pfnListEntryRead(&pTransfer->ProviderCtx, hList, pEntry);
1492 else
1493 rc = VERR_NOT_SUPPORTED;
1494
1495 if (RT_FAILURE(rc))
1496 LogRel(("Shared Clipboard: Reading list for list 0x%x entry failed with %Rrc\n", hList, rc));
1497
1498 LogFlowFuncLeaveRC(rc);
1499 return rc;
1500}
1501
1502/**
1503 * Writes a single transfer list entry.
1504 *
1505 * @returns VBox status code.
1506 * @param pTransfer Clipboard transfer to handle.
1507 * @param hList List handle of list to write to.
1508 * @param pEntry Entry information to write.
1509 */
1510int ShClTransferListWrite(PSHCLTRANSFER pTransfer, SHCLLISTHANDLE hList,
1511 PSHCLLISTENTRY pEntry)
1512{
1513 RT_NOREF(pTransfer, hList, pEntry);
1514
1515 int rc = VINF_SUCCESS;
1516
1517#if 0
1518 if (pTransfer->ProviderIface.pfnListEntryWrite)
1519 rc = pTransfer->ProviderIface.pfnListEntryWrite(&pTransfer->ProviderCtx, hList, pEntry);
1520#endif
1521
1522 if (RT_FAILURE(rc))
1523 LogRel(("Shared Clipboard: Writing list entry to list 0x%x failed with %Rrc\n", hList, rc));
1524
1525 LogFlowFuncLeaveRC(rc);
1526 return rc;
1527}
1528
1529/**
1530 * Returns whether a given transfer list handle is valid or not.
1531 *
1532 * @returns \c true if list handle is valid, \c false if not.
1533 * @param pTransfer Clipboard transfer to handle.
1534 * @param hList List handle to check.
1535 */
1536bool ShClTransferListHandleIsValid(PSHCLTRANSFER pTransfer, SHCLLISTHANDLE hList)
1537{
1538 bool fIsValid = false;
1539
1540 if (pTransfer->State.enmSource == SHCLSOURCE_LOCAL)
1541 {
1542 fIsValid = ShClTransferListGetByHandle(pTransfer, hList) != NULL;
1543 }
1544 else if (pTransfer->State.enmSource == SHCLSOURCE_REMOTE)
1545 {
1546 AssertFailed(); /** @todo Implement. */
1547 }
1548 else
1549 AssertFailedStmt(fIsValid = false);
1550
1551 return fIsValid;
1552}
1553
1554/**
1555 * Copies a transfer callback table from source to destination.
1556 *
1557 * @param pCallbacksDst Callback destination.
1558 * @param pCallbacksSrc Callback source. If set to NULL, the
1559 * destination callback table will be unset.
1560 */
1561static void shClTransferCopyCallbacks(PSHCLTRANSFERCALLBACKS pCallbacksDst, PSHCLTRANSFERCALLBACKS pCallbacksSrc)
1562{
1563 AssertPtrReturnVoid(pCallbacksDst);
1564 /* pCallbacksSrc can be NULL */
1565
1566 if (pCallbacksSrc) /* Set */
1567 {
1568#define SET_CALLBACK(a_pfnCallback) \
1569 if (pCallbacksSrc->a_pfnCallback) \
1570 pCallbacksDst->a_pfnCallback = pCallbacksSrc->a_pfnCallback
1571
1572 SET_CALLBACK(pfnOnCreated);
1573 SET_CALLBACK(pfnOnInitialized);
1574 SET_CALLBACK(pfnOnDestroy);
1575 SET_CALLBACK(pfnOnStarted);
1576 SET_CALLBACK(pfnOnCompleted);
1577 SET_CALLBACK(pfnOnError);
1578 SET_CALLBACK(pfnOnRegistered);
1579 SET_CALLBACK(pfnOnUnregistered);
1580
1581#undef SET_CALLBACK
1582
1583 pCallbacksDst->pvUser = pCallbacksSrc->pvUser;
1584 pCallbacksDst->cbUser = pCallbacksSrc->cbUser;
1585 }
1586 else /* Unset */
1587 RT_BZERO(pCallbacksDst, sizeof(SHCLTRANSFERCALLBACKS));
1588}
1589
1590/**
1591 * Sets or unsets the callback table to be used for a clipboard transfer.
1592 *
1593 * @returns VBox status code.
1594 * @param pTransfer Clipboard transfer to set callbacks for.
1595 * @param pCallbacks Pointer to callback table to set. If set to NULL,
1596 * existing callbacks for this transfer will be unset.
1597 *
1598 * @note Must come before initializing the transfer via ShClTransferInit().
1599 */
1600static void shClTransferSetCallbacks(PSHCLTRANSFER pTransfer, PSHCLTRANSFERCALLBACKS pCallbacks)
1601{
1602 AssertPtrReturnVoid(pTransfer);
1603 /* pCallbacks can be NULL. */
1604
1605 shClTransferCopyCallbacks(&pTransfer->Callbacks, pCallbacks);
1606
1607 /* Make sure that the callback context has all values set according to the callback table.
1608 * This only needs to be done once, so do this here. */
1609 pTransfer->CallbackCtx.pTransfer = pTransfer;
1610 pTransfer->CallbackCtx.pvUser = pTransfer->Callbacks.pvUser;
1611 pTransfer->CallbackCtx.cbUser = pTransfer->Callbacks.cbUser;
1612}
1613
1614/**
1615 * Sets the transfer provider for a given transfer.
1616 *
1617 * @returns VBox status code.
1618 * @param pTransfer Transfer to create transfer provider for.
1619 * @param pProvider Provider to use.
1620 */
1621int ShClTransferSetProvider(PSHCLTRANSFER pTransfer, PSHCLTXPROVIDER pProvider)
1622{
1623 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
1624 AssertPtrReturn(pProvider, VERR_INVALID_POINTER);
1625
1626 LogFlowFuncEnter();
1627
1628 int rc = VINF_SUCCESS;
1629
1630 pTransfer->ProviderIface = pProvider->Interface;
1631 pTransfer->ProviderCtx.pTransfer = pTransfer;
1632 pTransfer->ProviderCtx.pvUser = pProvider->pvUser;
1633 pTransfer->ProviderCtx.cbUser = pProvider->cbUser;
1634
1635 LogFlowFuncLeaveRC(rc);
1636 return rc;
1637}
1638
1639/**
1640 * Sets the current status.
1641 *
1642 * @returns VBox status code.
1643 * @param pTransfer Clipboard transfer to set status for.
1644 * @param enmStatus Status to set.
1645 *
1646 * @note Caller needs to take critical section.
1647 */
1648static int shClTransferSetStatus(PSHCLTRANSFER pTransfer, SHCLTRANSFERSTATUS enmStatus)
1649{
1650 Assert(RTCritSectIsOwner(&pTransfer->CritSect));
1651#if 0
1652 AssertMsgReturn(pTransfer->State.enmStatus != enmStatus,
1653 ("Setting the same status twice in a row (%#x), please report this!\n", enmStatus), VERR_WRONG_ORDER);
1654#endif
1655 pTransfer->State.enmStatus = enmStatus;
1656
1657 LogFlowFunc(("enmStatus=%s\n", ShClTransferStatusToStr(pTransfer->State.enmStatus)));
1658
1659 return RTSemEventSignal(pTransfer->StatusChangeEvent);
1660}
1661
1662/**
1663 * Returns the number of transfer root list entries.
1664 *
1665 * @returns Root list entry count.
1666 * @param pTransfer Clipboard transfer to return root entry count for.
1667 */
1668uint64_t ShClTransferRootsCount(PSHCLTRANSFER pTransfer)
1669{
1670 AssertPtrReturn(pTransfer, 0);
1671
1672 shClTransferLock(pTransfer);
1673
1674 uint32_t const cRoots = pTransfer->lstRoots.Hdr.cEntries;
1675
1676 shClTransferUnlock(pTransfer);
1677
1678 return cRoots;
1679}
1680
1681/**
1682 * Resets the root list of a clipboard transfer.
1683 *
1684 * @param pTransfer Transfer to clear transfer root list for.
1685 *
1686 * @note Caller needs to take critical section.
1687 */
1688static void shClTransferRootsReset(PSHCLTRANSFER pTransfer)
1689{
1690 AssertPtrReturnVoid(pTransfer);
1691 Assert(RTCritSectIsOwner(&pTransfer->CritSect));
1692
1693 if (pTransfer->pszPathRootAbs)
1694 {
1695 RTStrFree(pTransfer->pszPathRootAbs);
1696 pTransfer->pszPathRootAbs = NULL;
1697 }
1698
1699 ShClTransferListDestroy(&pTransfer->lstRoots);
1700}
1701
1702/**
1703 * Resets a clipboard transfer.
1704 *
1705 * @param pTransfer Clipboard transfer to reset.
1706 */
1707void ShClTransferReset(PSHCLTRANSFER pTransfer)
1708{
1709 AssertPtrReturnVoid(pTransfer);
1710
1711 LogFlowFuncEnter();
1712
1713 shClTransferLock(pTransfer);
1714
1715 shClTransferRootsReset(pTransfer);
1716
1717 PSHCLLISTHANDLEINFO pItList, pItListNext;
1718 RTListForEachSafe(&pTransfer->lstHandles, pItList, pItListNext, SHCLLISTHANDLEINFO, Node)
1719 {
1720 ShClTransferListHandleInfoDestroy(pItList);
1721
1722 RTListNodeRemove(&pItList->Node);
1723
1724 RTMemFree(pItList);
1725 }
1726
1727 PSHCLTRANSFEROBJ pItObj, pItObjNext;
1728 RTListForEachSafe(&pTransfer->lstObj, pItObj, pItObjNext, SHCLTRANSFEROBJ, Node)
1729 {
1730 ShClTransferObjDestroy(pItObj);
1731
1732 RTListNodeRemove(&pItObj->Node);
1733
1734 RTMemFree(pItObj);
1735 }
1736
1737 shClTransferUnlock(pTransfer);
1738}
1739
1740/**
1741 * Get a specific root list entry.
1742 *
1743 * @returns Const pointer to root list entry if found, or NULL if not found..
1744 * @param pTransfer Clipboard transfer to get root list entry of.
1745 * @param uIndex Index (zero-based) of entry to get.
1746 */
1747PCSHCLLISTENTRY ShClTransferRootsEntryGet(PSHCLTRANSFER pTransfer, uint64_t uIndex)
1748{
1749 AssertPtrReturn(pTransfer, NULL);
1750
1751 shClTransferLock(pTransfer);
1752
1753 if (uIndex >= pTransfer->lstRoots.Hdr.cEntries)
1754 {
1755 shClTransferUnlock(pTransfer);
1756 return NULL;
1757 }
1758
1759 PCSHCLLISTENTRY pEntry = shClTransferListGetEntryById(&pTransfer->lstRoots, uIndex);
1760
1761 shClTransferUnlock(pTransfer);
1762
1763 return pEntry;
1764}
1765
1766/**
1767 * Reads the root entries of a clipboard transfer.
1768 *
1769 * This gives the provider interface the chance of reading root entries information.
1770 *
1771 * @returns VBox status code.
1772 * @param pTransfer Clipboard transfer to read root list for.
1773 * Must be in STARTED state.
1774 */
1775int ShClTransferRootListRead(PSHCLTRANSFER pTransfer)
1776{
1777 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
1778
1779 LogFlowFuncEnter();
1780
1781#ifdef DEBUG
1782 shClTransferLock(pTransfer);
1783 AssertMsgReturn( pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_INITIALIZED
1784 || pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_STARTED,
1785 ("Cannot read root list in status %s\n", ShClTransferStatusToStr(pTransfer->State.enmStatus)),
1786 VERR_WRONG_ORDER);
1787 shClTransferUnlock(pTransfer);
1788#endif
1789
1790 int rc;
1791 if (pTransfer->ProviderIface.pfnRootListRead)
1792 rc = pTransfer->ProviderIface.pfnRootListRead(&pTransfer->ProviderCtx);
1793 else
1794 rc = VERR_NOT_SUPPORTED;
1795
1796 shClTransferLock(pTransfer);
1797
1798 /* Make sure that we have at least an empty root path set. */
1799 if ( RT_SUCCESS(rc)
1800 && !pTransfer->pszPathRootAbs)
1801 {
1802 if (RTStrAPrintf(&pTransfer->pszPathRootAbs, "") < 0)
1803 rc = VERR_NO_MEMORY;
1804 }
1805
1806 shClTransferUnlock(pTransfer);
1807
1808 LogFlowFuncLeaveRC(rc);
1809 return rc;
1810}
1811
1812/**
1813 * Initializes the root list entries for a given clipboard transfer, extended version.
1814 *
1815 * @returns VBox status code.
1816 * @param pTransfer Transfer to set transfer list entries for.
1817 * @param pszRoots String list (separated by \a pszSep) of root entries to set.
1818 * All entries must have the same root path.
1819 * @param cbRoots Size (in bytes) of string list. Includes zero terminator.
1820 * @param pszSep String separator to use for splitting up the root entries.
1821 *
1822 * @note Accepts local paths or URI string lists (absolute only).
1823 */
1824int ShClTransferRootsInitFromStringListEx(PSHCLTRANSFER pTransfer, const char *pszRoots, size_t cbRoots, const char *pszSep)
1825{
1826 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
1827 AssertPtrReturn(pszRoots, VERR_INVALID_POINTER);
1828 AssertReturn(cbRoots, VERR_INVALID_PARAMETER);
1829 AssertPtrReturn(pszSep, VERR_INVALID_POINTER);
1830
1831#ifdef DEBUG_andy
1832 LogFlowFunc(("Data:\n%.*Rhxd\n", cbRoots, pszRoots));
1833#endif
1834
1835 if (!RTStrIsValidEncoding(pszRoots))
1836 return VERR_INVALID_UTF8_ENCODING;
1837
1838 int rc = VINF_SUCCESS;
1839
1840 shClTransferLock(pTransfer);
1841
1842 shClTransferRootsReset(pTransfer);
1843
1844 PSHCLLIST pLstRoots = &pTransfer->lstRoots;
1845 char *pszPathRootAbs = NULL;
1846
1847 RTCList<RTCString> lstRootEntries = RTCString(pszRoots, cbRoots).split(pszSep);
1848 if (!lstRootEntries.size())
1849 {
1850 shClTransferUnlock(pTransfer);
1851 return VINF_SUCCESS;
1852 }
1853
1854 for (size_t i = 0; i < lstRootEntries.size(); ++i)
1855 {
1856 char *pszPathCur = NULL;
1857
1858 char *pszPath = NULL;
1859 rc = RTUriFilePathEx(lstRootEntries.at(i).c_str(),
1860 RTPATH_STR_F_STYLE_UNIX, &pszPath, 0 /*cbPath*/, NULL /*pcchPath*/);
1861 if (RT_SUCCESS(rc))
1862 {
1863 pszPathCur = pszPath;
1864 pszPath = NULL; /* pszPath has ownership now. */
1865 }
1866 else if (rc == VERR_URI_NOT_FILE_SCHEME) /* Local file path? */
1867 {
1868 pszPathCur = RTStrDup(lstRootEntries.at(i).c_str());
1869 rc = VINF_SUCCESS;
1870 }
1871
1872 LogFlowFunc(("pszPathCur=%s\n", pszPathCur));
1873
1874 rc = ShClTransferValidatePath(pszPathCur, false /* fMustExist */);
1875 if (RT_FAILURE(rc))
1876 break;
1877
1878 /* No root path determined yet? */
1879 if (!pszPathRootAbs)
1880 {
1881 pszPathRootAbs = RTStrDup(pszPathCur);
1882 if (pszPathRootAbs)
1883 {
1884 RTPathStripFilename(pszPathRootAbs);
1885
1886 LogFlowFunc(("pszPathRootAbs=%s\n", pszPathRootAbs));
1887
1888 /* We don't want to have a relative directory here. */
1889 if (RTPathStartsWithRoot(pszPathRootAbs))
1890 {
1891 rc = ShClTransferValidatePath(pszPathRootAbs, true /* Path must exist */);
1892 }
1893 else
1894 rc = VERR_PATH_IS_RELATIVE;
1895 }
1896 else
1897 rc = VERR_NO_MEMORY;
1898 }
1899
1900 if (RT_SUCCESS(rc))
1901 {
1902 PSHCLLISTENTRY pEntry;
1903 rc = ShClTransferListEntryAlloc(&pEntry);
1904 if (RT_SUCCESS(rc))
1905 {
1906 PSHCLFSOBJINFO pFsObjInfo = (PSHCLFSOBJINFO)RTMemAllocZ(sizeof(SHCLFSOBJINFO));
1907 if (pFsObjInfo)
1908 {
1909 if (pTransfer->State.enmDir == SHCLTRANSFERDIR_TO_REMOTE)
1910 rc = ShClFsObjInfoQueryLocal(pszPathCur, pFsObjInfo);
1911 if (RT_SUCCESS(rc))
1912 {
1913 /* Calculate the relative path within the root path. */
1914 const char *pszPathRelToRoot = &pszPathCur[strlen(pszPathRootAbs) + 1 /* Skip terminator or (back)slash. */];
1915 if ( pszPathRelToRoot
1916 && *pszPathRelToRoot != '\0')
1917 {
1918 LogFlowFunc(("pszPathRelToRoot=%s\n", pszPathRelToRoot));
1919
1920 rc = ShClTransferListEntryInitEx(pEntry, VBOX_SHCL_INFO_F_FSOBJINFO, pszPathRelToRoot,
1921 pFsObjInfo, sizeof(SHCLFSOBJINFO));
1922 if (RT_SUCCESS(rc))
1923 {
1924 rc = ShClTransferListAddEntry(pLstRoots, pEntry, true /* fAppend */);
1925 if (RT_SUCCESS(rc))
1926 {
1927 pFsObjInfo = NULL; /* pEntry has ownership now. */
1928 }
1929 }
1930 }
1931 else
1932 LogRel(("Shared Clipboard: Unable to construct relative path for '%s' (root is '%s')\n",
1933 pszPathCur, pszPathRootAbs));
1934 }
1935
1936 if (pFsObjInfo)
1937 {
1938 RTMemFree(pFsObjInfo);
1939 pFsObjInfo = NULL;
1940 }
1941 }
1942 else
1943 rc = VERR_NO_MEMORY;
1944
1945 if (RT_FAILURE(rc))
1946 ShClTransferListEntryFree(pEntry);
1947 }
1948 }
1949
1950 RTStrFree(pszPathCur);
1951 }
1952
1953 /* No (valid) root directory found? Bail out early. */
1954 if (!pszPathRootAbs)
1955 rc = VERR_PATH_DOES_NOT_START_WITH_ROOT;
1956
1957 if (RT_SUCCESS(rc))
1958 {
1959 pTransfer->pszPathRootAbs = pszPathRootAbs;
1960 LogFlowFunc(("pszPathRootAbs=%s, cRoots=%zu\n", pTransfer->pszPathRootAbs, pTransfer->lstRoots.Hdr.cEntries));
1961
1962 LogRel2(("Shared Clipboard: Transfer uses root '%s'\n", pTransfer->pszPathRootAbs));
1963 }
1964 else
1965 {
1966 LogRel(("Shared Clipboard: Unable to set roots for transfer, rc=%Rrc\n", rc));
1967 ShClTransferListDestroy(pLstRoots);
1968 RTStrFree(pszPathRootAbs);
1969 }
1970
1971 shClTransferUnlock(pTransfer);
1972
1973 LogFlowFuncLeaveRC(rc);
1974 return rc;
1975}
1976
1977/**
1978 * Initializes the root list entries for a given clipboard transfer.
1979 *
1980 * @returns VBox status code.
1981 * @param pTransfer Transfer to set transfer list entries for.
1982 * @param pszRoots String list (separated by SHCL_TRANSFER_URI_LIST_SEP_STR) of root entries to set.
1983 * All entries must have the same root path.
1984 * @param cbRoots Size (in bytes) of string list. Includes zero terminator.
1985 *
1986 * @note Accepts local paths or URI string lists (absolute only).
1987 */
1988int ShClTransferRootsInitFromStringList(PSHCLTRANSFER pTransfer, const char *pszRoots, size_t cbRoots)
1989{
1990 return ShClTransferRootsInitFromStringListEx(pTransfer, pszRoots, cbRoots, SHCL_TRANSFER_URI_LIST_SEP_STR);
1991}
1992
1993/**
1994 * Initializes the root list entries for a given clipboard transfer, UTF-16 (Unicode) version.
1995 *
1996 * @returns VBox status code.
1997 * @param pTransfer Transfer to set transfer list entries for.
1998 * @param pwszRoots Unicode string list (separated by CRLF) of root entries to set.
1999 * All entries must have the same root path.
2000 * @param cbRoots Size (in bytes) of string list. Includes zero terminator.
2001 *
2002 * @note Accepts local paths or URI string lists (absolute only).
2003 */
2004int ShClTransferRootsInitFromStringListUnicode(PSHCLTRANSFER pTransfer, PRTUTF16 pwszRoots, size_t cbRoots)
2005{
2006 AssertPtrReturn(pwszRoots, VERR_INVALID_POINTER);
2007 AssertReturn(cbRoots, VERR_INVALID_PARAMETER);
2008 AssertReturn(cbRoots % sizeof(RTUTF16) == 0, VERR_INVALID_PARAMETER);
2009
2010 size_t cwcRoots = cbRoots / sizeof(RTUTF16);
2011
2012 /* This may slightly overestimate the space needed. */
2013 size_t chDst = 0;
2014 int rc = ShClUtf16LenUtf8(pwszRoots, cwcRoots, &chDst);
2015 if (RT_SUCCESS(rc))
2016 {
2017 chDst++; /* Add space for terminator. */
2018
2019 char *pszDst = (char *)RTStrAlloc(chDst);
2020 if (pszDst)
2021 {
2022 size_t cbActual = 0;
2023 rc = ShClConvUtf16CRLFToUtf8LF(pwszRoots, cwcRoots, pszDst, chDst, &cbActual);
2024 if (RT_SUCCESS(rc))
2025 rc = ShClTransferRootsInitFromStringList(pTransfer, pszDst, cbActual + 1 /* Include terminator */);
2026
2027 RTStrFree(pszDst);
2028 }
2029 else
2030 rc = VERR_NO_MEMORY;
2031 }
2032
2033 return rc;
2034}
2035
2036/**
2037 * Initializes a single file as a transfer root.
2038 *
2039 * @returns VBox status code.
2040 * @param pTransfer Transfer to set transfer list entries for.
2041 * @param pszFile File to use as transfer root.
2042 *
2043 * @note Convenience function, uses ShClTransferRootsSet() internally.
2044 */
2045int ShClTransferRootsInitFromFile(PSHCLTRANSFER pTransfer, const char *pszFile)
2046{
2047 char *pszRoots = NULL;
2048
2049 LogFlowFuncEnter();
2050
2051 int rc = RTStrAAppend(&pszRoots, pszFile);
2052 AssertRCReturn(rc, rc);
2053 rc = RTStrAAppend(&pszRoots, "\r\n");
2054 AssertRCReturn(rc, rc);
2055 rc = ShClTransferRootsInitFromStringList(pTransfer, pszRoots, strlen(pszRoots) + 1 /* Include terminator */);
2056 RTStrFree(pszRoots);
2057 return rc;
2058}
2059
2060/**
2061 * Returns the clipboard transfer's ID.
2062 *
2063 * @returns The transfer's ID.
2064 * @param pTransfer Clipboard transfer to return ID for.
2065 */
2066SHCLTRANSFERID ShClTransferGetID(PSHCLTRANSFER pTransfer)
2067{
2068 AssertPtrReturn(pTransfer, 0);
2069
2070 shClTransferLock(pTransfer);
2071
2072 SHCLTRANSFERID const uID = pTransfer->State.uID;
2073
2074 shClTransferUnlock(pTransfer);
2075
2076 return uID;
2077}
2078
2079/**
2080 * Returns the clipboard transfer's direction.
2081 *
2082 * @returns The transfer's direction.
2083 * @param pTransfer Clipboard transfer to return direction for.
2084 */
2085SHCLTRANSFERDIR ShClTransferGetDir(PSHCLTRANSFER pTransfer)
2086{
2087 AssertPtrReturn(pTransfer, SHCLTRANSFERDIR_UNKNOWN);
2088
2089 shClTransferLock(pTransfer);
2090
2091 SHCLTRANSFERDIR const enmDir = pTransfer->State.enmDir;
2092
2093 shClTransferUnlock(pTransfer);
2094
2095 return enmDir;
2096}
2097
2098/**
2099 * Returns the absolute root path of a transfer.
2100 *
2101 * @returns VBox status code.
2102 * @param pTransfer Clipboard transfer to return absolute root path for.
2103 * @param pszPath Where to store the returned path.
2104 * @param cbPath Size (in bytes) of \a pszPath.
2105 */
2106int ShClTransferGetRootPathAbs(PSHCLTRANSFER pTransfer, char *pszPath, size_t cbPath)
2107{
2108 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2109
2110 shClTransferLock(pTransfer);
2111
2112 AssertMsgReturn(pTransfer->pszPathRootAbs, ("Transfer has no root path set (yet)\n"), VERR_WRONG_ORDER);
2113
2114 int const rc = RTStrCopy(pszPath, cbPath, pTransfer->pszPathRootAbs);
2115
2116 shClTransferUnlock(pTransfer);
2117
2118 return rc;
2119}
2120
2121/**
2122 * Returns the transfer's source.
2123 *
2124 * @returns The transfer's source.
2125 * @param pTransfer Clipboard transfer to return source for.
2126 */
2127SHCLSOURCE ShClTransferGetSource(PSHCLTRANSFER pTransfer)
2128{
2129 AssertPtrReturn(pTransfer, SHCLSOURCE_INVALID);
2130
2131 shClTransferLock(pTransfer);
2132
2133 SHCLSOURCE const enmSource = pTransfer->State.enmSource;
2134
2135 shClTransferUnlock(pTransfer);
2136
2137 return enmSource;
2138}
2139
2140/**
2141 * Returns the current transfer status.
2142 *
2143 * @returns Current transfer status.
2144 * @param pTransfer Clipboard transfer to return status for.
2145 *
2146 * @note Caller needs to take critical section.
2147 */
2148DECLINLINE(SHCLTRANSFERSTATUS) shClTransferGetStatusLocked(PSHCLTRANSFER pTransfer)
2149{
2150 Assert(RTCritSectIsOwner(&pTransfer->CritSect));
2151
2152 shClTransferLock(pTransfer);
2153
2154 SHCLTRANSFERSTATUS const enmStatus = pTransfer->State.enmStatus;
2155
2156 shClTransferUnlock(pTransfer);
2157
2158 return enmStatus;
2159}
2160
2161/**
2162 * Returns the current transfer status.
2163 *
2164 * @returns Current transfer status.
2165 * @param pTransfer Clipboard transfer to return status for.
2166 */
2167SHCLTRANSFERSTATUS ShClTransferGetStatus(PSHCLTRANSFER pTransfer)
2168{
2169 AssertPtrReturn(pTransfer, SHCLTRANSFERSTATUS_NONE);
2170
2171 shClTransferLock(pTransfer);
2172
2173 SHCLTRANSFERSTATUS const enmSts = shClTransferGetStatusLocked(pTransfer);
2174
2175 shClTransferUnlock(pTransfer);
2176
2177 return enmSts;
2178}
2179
2180/**
2181 * Runs a started clipboard transfer in a dedicated thread.
2182 *
2183 * @returns VBox status code.
2184 * @param pTransfer Clipboard transfer to run.
2185 * @param pfnThreadFunc Pointer to thread function to use.
2186 * @param pvUser Pointer to user-provided data. Optional.
2187 */
2188int ShClTransferRun(PSHCLTRANSFER pTransfer, PFNSHCLTRANSFERTHREAD pfnThreadFunc, void *pvUser)
2189{
2190 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2191 AssertPtrReturn(pfnThreadFunc, VERR_INVALID_POINTER);
2192 /* pvUser is optional. */
2193
2194 AssertMsgReturn(pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_STARTED,
2195 ("Wrong status (currently is %s)\n", ShClTransferStatusToStr(pTransfer->State.enmStatus)),
2196 VERR_WRONG_ORDER);
2197
2198 int rc = shClTransferThreadCreate(pTransfer, pfnThreadFunc, pvUser);
2199
2200 LogFlowFuncLeaveRC(rc);
2201 return rc;
2202}
2203
2204/**
2205 * Starts an initialized transfer.
2206 *
2207 * @returns VBox status code.
2208 * @param pTransfer Clipboard transfer to start.
2209 */
2210int ShClTransferStart(PSHCLTRANSFER pTransfer)
2211{
2212 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2213
2214 LogFlowFuncEnter();
2215
2216 shClTransferLock(pTransfer);
2217
2218 /* Ready to start? */
2219 AssertMsgReturnStmt(pTransfer->ProviderIface.pfnRootListRead != NULL,
2220 ("No provider interface set (yet)\n"),
2221 shClTransferUnlock(pTransfer), VERR_WRONG_ORDER);
2222 AssertMsgReturnStmt(pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_INITIALIZED,
2223 ("Wrong status (currently is %s)\n", ShClTransferStatusToStr(pTransfer->State.enmStatus)),
2224 shClTransferUnlock(pTransfer), VERR_WRONG_ORDER);
2225
2226 int rc = shClTransferSetStatus(pTransfer, SHCLTRANSFERSTATUS_STARTED);
2227
2228 shClTransferUnlock(pTransfer);
2229
2230 if (pTransfer->Callbacks.pfnOnStarted)
2231 pTransfer->Callbacks.pfnOnStarted(&pTransfer->CallbackCtx);
2232
2233 LogFlowFuncLeaveRC(rc);
2234 return rc;
2235}
2236
2237/**
2238 * Stops a started transfer.
2239 *
2240 * @returns VBox status code.
2241 * @param pTransfer Clipboard transfer to stop.
2242 */
2243int ShClTransferStop(PSHCLTRANSFER pTransfer)
2244{
2245 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2246
2247 LogFlowFuncEnter();
2248
2249 int rc = shClTransferThreadDestroy(pTransfer, SHCL_TIMEOUT_DEFAULT_MS);
2250
2251 LogFlowFuncLeaveRC(rc);
2252 return rc;
2253}
2254
2255/**
2256 * Completes a transfer (as successful).
2257 *
2258 * @returns VBox status code.
2259 * @param pTransfer Clipboard transfer to complete.
2260 */
2261int ShClTransferComplete(PSHCLTRANSFER pTransfer)
2262{
2263 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2264
2265 LogFlowFuncEnter();
2266
2267 shClTransferLock(pTransfer);
2268
2269 AssertMsgReturnStmt( pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_INITIALIZED
2270 || pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_STARTED,
2271 ("Wrong status (currently is %s)\n", ShClTransferStatusToStr(pTransfer->State.enmStatus)),
2272 shClTransferUnlock(pTransfer), VERR_WRONG_ORDER);
2273
2274 int rc = shClTransferSetStatus(pTransfer, SHCLTRANSFERSTATUS_COMPLETED);
2275
2276 shClTransferUnlock(pTransfer);
2277
2278 if (pTransfer->Callbacks.pfnOnCompleted)
2279 pTransfer->Callbacks.pfnOnCompleted(&pTransfer->CallbackCtx, rc);
2280
2281 LogFlowFuncLeaveRC(rc);
2282 return rc;
2283}
2284
2285/**
2286 * Cancels or sets an error for a transfer.
2287 *
2288 * @returns VBox status code.
2289 * @param pTransfer Clipboard transfer to cancel or set error for.
2290 * @param rc Error code to set.
2291 * If set to VERR_CANCELLED, the transfer will be canceled.
2292 */
2293static int shClTransferCancelOrError(PSHCLTRANSFER pTransfer, int rc)
2294{
2295 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2296
2297 LogFlowFunc(("%Rrc\n", rc));
2298
2299 shClTransferLock(pTransfer);
2300
2301 int rc2;
2302
2303 if ( pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_INITIALIZED
2304 || pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_STARTED)
2305 {
2306 if (rc == VERR_CANCELLED)
2307 {
2308 rc2 = shClTransferSetStatus(pTransfer, SHCLTRANSFERSTATUS_CANCELED);
2309
2310 if (pTransfer->Callbacks.pfnOnCompleted)
2311 pTransfer->Callbacks.pfnOnCompleted(&pTransfer->CallbackCtx, VERR_CANCELLED);
2312 }
2313 else
2314 {
2315 rc2 = shClTransferSetStatus(pTransfer, SHCLTRANSFERSTATUS_ERROR);
2316
2317 if (pTransfer->Callbacks.pfnOnError)
2318 pTransfer->Callbacks.pfnOnError(&pTransfer->CallbackCtx, rc);
2319 }
2320 }
2321 else /* Nothing to do. */
2322 rc2 = VINF_SUCCESS;
2323
2324 shClTransferUnlock(pTransfer);
2325
2326 LogFlowFuncLeaveRC(rc2);
2327 return rc2;
2328}
2329
2330/**
2331 * Cancels a transfer.
2332 *
2333 * @returns VBox status code.
2334 * @param pTransfer Clipboard transfer to cancel.
2335 */
2336int ShClTransferCancel(PSHCLTRANSFER pTransfer)
2337{
2338 return shClTransferCancelOrError(pTransfer, VERR_CANCELLED);
2339}
2340
2341/**
2342 * Kills a transfer.
2343 *
2344 * Currently cancels it internally.
2345 *
2346 * @returns VBox status code.
2347 * @param pTransfer Clipboard transfer to kill.
2348 */
2349int ShClTransferKill(PSHCLTRANSFER pTransfer)
2350{
2351 return ShClTransferCancel(pTransfer);
2352}
2353
2354/**
2355 * Sets an error for a transfer.
2356 *
2357 * @returns VBox status code.
2358 * @param pTransfer Clipboard transfer to set error for.
2359 * @param rc Error code to set.
2360 */
2361int ShClTransferError(PSHCLTRANSFER pTransfer, int rc)
2362{
2363 return shClTransferCancelOrError(pTransfer, rc);
2364}
2365
2366/**
2367 * Internal struct for keeping a transfer thread context.
2368 */
2369typedef struct _SHCLTRANSFERTHREADCTX
2370{
2371 /** Pointer to transfer. */
2372 PSHCLTRANSFER pTransfer;
2373 /** User-supplied context data. Can be NULL if not being used. */
2374 void *pvUser;
2375 /** Pointer to thread function to use. */
2376 PFNSHCLTRANSFERTHREAD pfnThread;
2377} SHCLTRANSFERTHREADCTX;
2378/** Pointer to internal struct for keeping a transfer thread context. */
2379typedef SHCLTRANSFERTHREADCTX *PSHCLTRANSFERTHREADCTX;
2380
2381/**
2382 * Worker thread for a transfer.
2383 *
2384 * @returns VBox status code.
2385 * @param ThreadSelf Thread self handle. Not being used.
2386 * @param pvUser Context data of type PSHCLTRANSFERTHREADCTX.
2387 */
2388static DECLCALLBACK(int) shClTransferThreadWorker(RTTHREAD ThreadSelf, void *pvUser)
2389{
2390 RT_NOREF(ThreadSelf);
2391
2392 LogFlowFuncEnter();
2393
2394 SHCLTRANSFERTHREADCTX Ctx;
2395 memcpy(&Ctx, pvUser, sizeof(SHCLTRANSFERTHREADCTX));
2396
2397 LogFlowFunc(("pfnThread=%p, pTransfer=%p, pvUser=%p\n", Ctx.pfnThread, Ctx.pTransfer, Ctx.pvUser));
2398
2399 PSHCLTRANSFER pTransfer = Ctx.pTransfer;
2400
2401 shClTransferLock(pTransfer);
2402
2403 pTransfer->Thread.fStarted = true;
2404 pTransfer->Thread.fStop = false;
2405
2406 shClTransferUnlock(pTransfer);
2407
2408 RTThreadUserSignal(RTThreadSelf());
2409
2410 int rc = Ctx.pfnThread(pTransfer, Ctx.pvUser);
2411
2412 if (pTransfer->Callbacks.pfnOnCompleted)
2413 pTransfer->Callbacks.pfnOnCompleted(&pTransfer->CallbackCtx, rc);
2414
2415 LogFlowFuncLeaveRC(rc);
2416 return rc;
2417}
2418
2419/**
2420 * Creates a thread for a clipboard transfer.
2421 *
2422 * @returns VBox status code.
2423 * @param pTransfer Clipboard transfer to create thread for.
2424 * @param pfnThreadFunc Thread function to use for this transfer.
2425 * @param pvUser Pointer to user-provided data.
2426 */
2427static int shClTransferThreadCreate(PSHCLTRANSFER pTransfer, PFNSHCLTRANSFERTHREAD pfnThreadFunc, void *pvUser)
2428
2429{
2430 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2431
2432 shClTransferLock(pTransfer);
2433
2434 /* Already marked for stopping? */
2435 AssertMsgReturn(pTransfer->Thread.fStop == false,
2436 ("Transfer thread already marked for stopping"), VERR_WRONG_ORDER);
2437 /* Already started? */
2438 AssertMsgReturn(pTransfer->Thread.fStarted == false,
2439 ("Transfer thread already started"), VERR_WRONG_ORDER);
2440
2441 SHCLTRANSFERTHREADCTX Ctx = { pTransfer, pvUser, pfnThreadFunc };
2442
2443 /* Spawn a worker thread, so that we don't block the window thread for too long. */
2444 int rc = RTThreadCreate(&pTransfer->Thread.hThread, shClTransferThreadWorker,
2445 &Ctx, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE,
2446 "shcltx");
2447 if (RT_SUCCESS(rc))
2448 {
2449 shClTransferUnlock(pTransfer); /* Leave lock while waiting. */
2450
2451 int rc2 = RTThreadUserWait(pTransfer->Thread.hThread, SHCL_TIMEOUT_DEFAULT_MS);
2452 AssertRC(rc2);
2453
2454 shClTransferLock(pTransfer);
2455
2456 if (pTransfer->Thread.fStarted) /* Did the thread indicate that it started correctly? */
2457 {
2458 /* Nothing to do in here. */
2459 }
2460 else
2461 rc = VERR_GENERAL_FAILURE; /** @todo Find a better rc. */
2462 }
2463
2464 shClTransferUnlock(pTransfer);
2465
2466 LogFlowFuncLeaveRC(rc);
2467 return rc;
2468}
2469
2470/**
2471 * Destroys the thread of a clipboard transfer.
2472 *
2473 * @returns VBox status code. Will return thread rc.
2474 * @param pTransfer Clipboard transfer to destroy thread for.
2475 * @param uTimeoutMs Timeout (in ms) to wait for thread destruction.
2476 */
2477static int shClTransferThreadDestroy(PSHCLTRANSFER pTransfer, RTMSINTERVAL uTimeoutMs)
2478{
2479 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2480
2481 shClTransferLock(pTransfer);
2482
2483 if (!pTransfer->Thread.fStarted)
2484 {
2485 shClTransferUnlock(pTransfer);
2486 return VINF_SUCCESS;
2487 }
2488
2489 LogFlowFuncEnter();
2490
2491 /* Set stop indicator. */
2492 pTransfer->Thread.fStop = true;
2493
2494 shClTransferUnlock(pTransfer); /* Leave lock while waiting. */
2495
2496 int rcThread = VERR_IPE_UNINITIALIZED_STATUS;
2497 Assert(pTransfer->Thread.hThread != NIL_RTTHREAD);
2498 int rc = RTThreadWait(pTransfer->Thread.hThread, uTimeoutMs, &rcThread);
2499
2500 LogFlowFunc(("Waiting for thread resulted in %Rrc (thread exited with %Rrc)\n", rc, rcThread));
2501
2502 if (RT_SUCCESS(rc))
2503 {
2504 pTransfer->Thread.fStarted = false;
2505 pTransfer->Thread.hThread = NIL_RTTHREAD;
2506
2507 rc = rcThread; /* Return the thread rc to the caller. */
2508 }
2509 else
2510 LogRel(("Shared Clipboard: Waiting for thread of transfer %RU16 failed with %Rrc\n", pTransfer->State.uID, rc));
2511
2512 LogFlowFuncLeaveRC(rc);
2513 return rc;
2514}
2515
2516/**
2517 * Waits for the transfer status to change, internal version.
2518 *
2519 * @returns VBox status code.
2520 * @param pTransfer Clipboard transfer to wait for.
2521 * @param msTimeout Timeout (in ms) to wait.
2522 * @param penmStatus Where to return the new (current) transfer status on success.
2523 * Optional and can be NULL.
2524 */
2525static int shClTransferWaitForStatusChangeInternal(PSHCLTRANSFER pTransfer, RTMSINTERVAL msTimeout, SHCLTRANSFERSTATUS *penmStatus)
2526{
2527 LogFlowFunc(("Waiting for status change (%RU32 timeout) ...\n", msTimeout));
2528
2529 int rc = RTSemEventWait(pTransfer->StatusChangeEvent, msTimeout);
2530 if (RT_SUCCESS(rc))
2531 {
2532 if (penmStatus)
2533 {
2534 shClTransferLock(pTransfer);
2535
2536 *penmStatus = pTransfer->State.enmStatus;
2537
2538 shClTransferUnlock(pTransfer);
2539 }
2540 }
2541
2542 return rc;
2543}
2544
2545/**
2546 * Waits for the transfer status to change.
2547 *
2548 * @returns VBox status code.
2549 * @param pTransfer Clipboard transfer to wait for.
2550 * @param msTimeout Timeout (in ms) to wait.
2551 * @param penmStatus Where to return the new (current) transfer status on success.
2552 * Optional and can be NULL.
2553 */
2554int ShClTransferWaitForStatusChange(PSHCLTRANSFER pTransfer, RTMSINTERVAL msTimeout, SHCLTRANSFERSTATUS *penmStatus)
2555{
2556 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2557
2558 int rc = shClTransferWaitForStatusChangeInternal(pTransfer, msTimeout, penmStatus);
2559
2560 LogFlowFuncLeaveRC(rc);
2561 return rc;
2562}
2563
2564/**
2565 * Waits for a specific transfer status.
2566 *
2567 * @returns VBox status code.
2568 * @param pTransfer Clipboard transfer to wait for.
2569 * @param msTimeout Timeout (in ms) to wait.
2570 * @param enmStatus Transfer status to wait for.
2571 */
2572int ShClTransferWaitForStatus(PSHCLTRANSFER pTransfer, RTMSINTERVAL msTimeout, SHCLTRANSFERSTATUS enmStatus)
2573{
2574 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2575
2576 int rc = VINF_SUCCESS;
2577
2578 uint64_t const tsStartMs = RTTimeMilliTS();
2579 uint64_t msLeft = msTimeout;
2580 for (;;)
2581 {
2582 SHCLTRANSFERSTATUS enmCurStatus;
2583 rc = shClTransferWaitForStatusChangeInternal(pTransfer, msLeft, &enmCurStatus);
2584 if (RT_FAILURE(rc))
2585 break;
2586
2587 if (enmCurStatus == enmStatus)
2588 break;
2589
2590 msLeft -= RT_MIN(msLeft, RTTimeMilliTS() - tsStartMs);
2591 if (msLeft == 0)
2592 {
2593 rc = VERR_TIMEOUT;
2594 break;
2595 }
2596 }
2597
2598 LogFlowFuncLeaveRC(rc);
2599 return rc;
2600}
2601
2602
2603/*********************************************************************************************************************************
2604 * Transfer Context *
2605 ********************************************************************************************************************************/
2606
2607/**
2608 * Locks a transfer context.
2609 *
2610 * @param pTransferCtx Transfer context to lock.
2611 */
2612DECLINLINE(void) shClTransferCtxLock(PSHCLTRANSFERCTX pTransferCtx)
2613{
2614 int rc2 = RTCritSectEnter(&pTransferCtx->CritSect);
2615 AssertRC(rc2);
2616}
2617
2618/**
2619 * Unlocks a transfer context.
2620 *
2621 * @param pTransferCtx Transfer context to unlock.
2622 */
2623DECLINLINE(void) shClTransferCtxUnlock(PSHCLTRANSFERCTX pTransferCtx)
2624{
2625 int rc2 = RTCritSectLeave(&pTransferCtx->CritSect);
2626 AssertRC(rc2);
2627}
2628
2629/**
2630 * Initializes a clipboard transfer context.
2631 *
2632 * @returns VBox status code.
2633 * @param pTransferCtx Transfer context to initialize.
2634 */
2635int ShClTransferCtxInit(PSHCLTRANSFERCTX pTransferCtx)
2636{
2637 AssertPtrReturn(pTransferCtx, VERR_INVALID_POINTER);
2638
2639 LogFlowFunc(("pTransferCtx=%p\n", pTransferCtx));
2640
2641 int rc = RTCritSectInit(&pTransferCtx->CritSect);
2642 if (RT_SUCCESS(rc))
2643 {
2644 rc = RTSemEventCreate(&pTransferCtx->ChangedEvent);
2645 if (RT_SUCCESS(rc))
2646 {
2647 RT_ZERO(pTransferCtx->ChangedEventData);
2648
2649 RTListInit(&pTransferCtx->List);
2650
2651 pTransferCtx->cTransfers = 0;
2652 pTransferCtx->cRunning = 0;
2653 pTransferCtx->cMaxRunning = 64; /** @todo Make this configurable? */
2654
2655 RT_ZERO(pTransferCtx->bmTransferIds);
2656
2657 ShClTransferCtxReset(pTransferCtx);
2658 }
2659 }
2660
2661 return VINF_SUCCESS;
2662}
2663
2664/**
2665 * Destroys a clipboard transfer context.
2666 *
2667 * @param pTransferCtx Transfer context to destroy.
2668 */
2669void ShClTransferCtxDestroy(PSHCLTRANSFERCTX pTransferCtx)
2670{
2671 if (!pTransferCtx)
2672 return;
2673
2674 LogFlowFunc(("pTransferCtx=%p\n", pTransferCtx));
2675
2676 shClTransferCtxLock(pTransferCtx);
2677
2678 PSHCLTRANSFER pTransfer, pTransferNext;
2679 RTListForEachSafe(&pTransferCtx->List, pTransfer, pTransferNext, SHCLTRANSFER, Node)
2680 {
2681 ShClTransferDestroy(pTransfer);
2682
2683 shclTransferCtxTransferRemoveAndUnregister(pTransferCtx, pTransfer);
2684
2685 RTMemFree(pTransfer);
2686 pTransfer = NULL;
2687 }
2688
2689 pTransferCtx->cRunning = 0;
2690 pTransferCtx->cTransfers = 0;
2691
2692 shClTransferCtxUnlock(pTransferCtx);
2693
2694 RTSemEventDestroy(pTransferCtx->ChangedEvent);
2695 pTransferCtx->ChangedEvent = NIL_RTSEMEVENT;
2696
2697 if (RTCritSectIsInitialized(&pTransferCtx->CritSect))
2698 RTCritSectDelete(&pTransferCtx->CritSect);
2699}
2700
2701/**
2702 * Resets a clipboard transfer context.
2703 *
2704 * @param pTransferCtx Transfer context to reset.
2705 */
2706void ShClTransferCtxReset(PSHCLTRANSFERCTX pTransferCtx)
2707{
2708 AssertPtrReturnVoid(pTransferCtx);
2709
2710 shClTransferCtxLock(pTransferCtx);
2711
2712 LogFlowFuncEnter();
2713
2714 PSHCLTRANSFER pTransfer;
2715 RTListForEach(&pTransferCtx->List, pTransfer, SHCLTRANSFER, Node)
2716 ShClTransferReset(pTransfer);
2717
2718#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
2719 /** @todo Anything to do here? */
2720#endif
2721
2722 shClTransferCtxUnlock(pTransferCtx);
2723}
2724
2725/**
2726 * Signals a change event.
2727 *
2728 * @returns VBox status code.
2729 * @param pTransferCtx Transfer context to return transfer for.
2730 * @param fRegistered Whether a transfer got registered or unregistered.
2731 * @param pTransfer Transfer bound to the event.
2732 */
2733static int shClTransferCtxSignal(PSHCLTRANSFERCTX pTransferCtx, bool fRegistered, PSHCLTRANSFER pTransfer)
2734{
2735 Assert(RTCritSectIsOwner(&pTransferCtx->CritSect));
2736
2737 LogFlowFunc(("fRegistered=%RTbool, pTransfer=%p\n", fRegistered, pTransfer));
2738
2739 pTransferCtx->ChangedEventData.fRegistered = fRegistered;
2740 pTransferCtx->ChangedEventData.pTransfer = pTransfer;
2741
2742 return RTSemEventSignal(pTransferCtx->ChangedEvent);
2743}
2744
2745/**
2746 * Returns a specific clipboard transfer, internal version.
2747 *
2748 * @returns Clipboard transfer found, or NULL if not found.
2749 * @param pTransferCtx Transfer context to return transfer for.
2750 * @param idTransfer ID of the transfer to return.
2751 *
2752 * @note Caller needs to take critical section.
2753 */
2754static PSHCLTRANSFER shClTransferCtxGetTransferByIdInternal(PSHCLTRANSFERCTX pTransferCtx, SHCLTRANSFERID idTransfer)
2755{
2756 Assert(RTCritSectIsOwner(&pTransferCtx->CritSect));
2757
2758 PSHCLTRANSFER pTransfer;
2759 RTListForEach(&pTransferCtx->List, pTransfer, SHCLTRANSFER, Node) /** @todo Slow, but works for now. */
2760 {
2761 if (pTransfer->State.uID == idTransfer)
2762 return pTransfer;
2763 }
2764
2765 return NULL;
2766}
2767
2768/**
2769 * Returns a specific clipboard transfer by index, internal version.
2770 *
2771 * @returns Clipboard transfer found, or NULL if not found.
2772 * @param pTransferCtx Transfer context to return transfer for.
2773 * @param uIdx Index of the transfer to return.
2774 *
2775 * @note Caller needs to take critical section.
2776 */
2777static PSHCLTRANSFER shClTransferCtxGetTransferByIndexInternal(PSHCLTRANSFERCTX pTransferCtx, uint32_t uIdx)
2778{
2779 Assert(RTCritSectIsOwner(&pTransferCtx->CritSect));
2780
2781 uint32_t idx = 0;
2782
2783 PSHCLTRANSFER pTransfer;
2784 RTListForEach(&pTransferCtx->List, pTransfer, SHCLTRANSFER, Node) /** @todo Slow, but works for now. */
2785 {
2786 if (uIdx == idx)
2787 return pTransfer;
2788 idx++;
2789 }
2790
2791 return NULL;
2792}
2793
2794/**
2795 * Returns a clipboard transfer for a specific transfer ID.
2796 *
2797 * @returns Clipboard transfer found, or NULL if not found.
2798 * @param pTransferCtx Transfer context to return transfer for.
2799 * @param uID ID of the transfer to return.
2800 */
2801PSHCLTRANSFER ShClTransferCtxGetTransferById(PSHCLTRANSFERCTX pTransferCtx, uint32_t uID)
2802{
2803 shClTransferCtxLock(pTransferCtx);
2804
2805 PSHCLTRANSFER const pTransfer = shClTransferCtxGetTransferByIdInternal(pTransferCtx, uID);
2806
2807 shClTransferCtxUnlock(pTransferCtx);
2808
2809 return pTransfer;
2810}
2811
2812/**
2813 * Returns a clipboard transfer for a specific list index.
2814 *
2815 * @returns Clipboard transfer found, or NULL if not found.
2816 * @param pTransferCtx Transfer context to return transfer for.
2817 * @param uIdx List index of the transfer to return.
2818 */
2819PSHCLTRANSFER ShClTransferCtxGetTransferByIndex(PSHCLTRANSFERCTX pTransferCtx, uint32_t uIdx)
2820{
2821 shClTransferCtxLock(pTransferCtx);
2822
2823 PSHCLTRANSFER const pTransfer = shClTransferCtxGetTransferByIndexInternal(pTransferCtx, uIdx);
2824
2825 shClTransferCtxUnlock(pTransferCtx);
2826
2827 return pTransfer;
2828}
2829
2830
2831/**
2832 * Returns the last clipboard transfer registered.
2833 *
2834 * @returns Clipboard transfer found, or NULL if not found.
2835 * @param pTransferCtx Transfer context to return transfer for.
2836 */
2837PSHCLTRANSFER ShClTransferCtxGetTransferLast(PSHCLTRANSFERCTX pTransferCtx)
2838{
2839 shClTransferCtxLock(pTransferCtx);
2840
2841 PSHCLTRANSFER const pTransfer = RTListGetLast(&pTransferCtx->List, SHCLTRANSFER, Node);
2842
2843 shClTransferCtxUnlock(pTransferCtx);
2844
2845 return pTransfer;
2846}
2847
2848/**
2849 * Returns the number of running clipboard transfers for a given transfer context.
2850 *
2851 * @returns Number of running transfers.
2852 * @param pTransferCtx Transfer context to return number for.
2853 */
2854uint32_t ShClTransferCtxGetRunningTransfers(PSHCLTRANSFERCTX pTransferCtx)
2855{
2856 AssertPtrReturn(pTransferCtx, 0);
2857
2858 shClTransferCtxLock(pTransferCtx);
2859
2860 uint32_t const cRunning = pTransferCtx->cRunning;
2861
2862 shClTransferCtxUnlock(pTransferCtx);
2863
2864 return cRunning;
2865}
2866
2867/**
2868 * Returns the number of total clipboard transfers for a given transfer context.
2869 *
2870 * @returns Number of total transfers.
2871 * @param pTransferCtx Transfer context to return number for.
2872 */
2873uint32_t ShClTransferCtxGetTotalTransfers(PSHCLTRANSFERCTX pTransferCtx)
2874{
2875 AssertPtrReturn(pTransferCtx, 0);
2876
2877 shClTransferCtxLock(pTransferCtx);
2878
2879 uint32_t const cTransfers = pTransferCtx->cTransfers;
2880
2881 shClTransferCtxUnlock(pTransferCtx);
2882
2883 return cTransfers;
2884}
2885
2886static int shClTransferCreateIDInternal(PSHCLTRANSFERCTX pTransferCtx, SHCLTRANSFERID *pidTransfer)
2887{
2888 /*
2889 * Pick a random bit as starting point. If it's in use, search forward
2890 * for a free one, wrapping around. We've reserved both the zero'th and
2891 * max-1 IDs.
2892 */
2893 SHCLTRANSFERID idTransfer = RTRandU32Ex(1, VBOX_SHCL_MAX_TRANSFERS - 2);
2894
2895 if (!ASMBitTestAndSet(&pTransferCtx->bmTransferIds[0], idTransfer))
2896 { /* likely */ }
2897 else if (pTransferCtx->cTransfers < VBOX_SHCL_MAX_TRANSFERS - 2 /* First and last are not used */)
2898 {
2899 /* Forward search. */
2900 int iHit = ASMBitNextClear(&pTransferCtx->bmTransferIds[0], VBOX_SHCL_MAX_TRANSFERS, idTransfer);
2901 if (iHit < 0)
2902 iHit = ASMBitFirstClear(&pTransferCtx->bmTransferIds[0], VBOX_SHCL_MAX_TRANSFERS);
2903 AssertLogRelMsgReturn(iHit >= 0, ("Transfer count: %RU16\n", pTransferCtx->cTransfers), VERR_SHCLPB_MAX_TRANSFERS_REACHED);
2904 idTransfer = iHit;
2905 AssertLogRelMsgReturn(!ASMBitTestAndSet(&pTransferCtx->bmTransferIds[0], idTransfer), ("idObject=%#x\n", idTransfer), VERR_INTERNAL_ERROR_2);
2906 }
2907 else
2908 {
2909 LogFunc(("Maximum number of transfers reached (%RU16 transfers)\n", pTransferCtx->cTransfers));
2910 return VERR_SHCLPB_MAX_TRANSFERS_REACHED;
2911 }
2912
2913 *pidTransfer = idTransfer;
2914
2915 return VINF_SUCCESS;
2916}
2917
2918/**
2919 * Creates a new transfer ID for a given transfer context.
2920 *
2921 * @returns VBox status code.
2922 * @retval VERR_SHCLPB_MAX_TRANSFERS_REACHED if the maximum of concurrent transfers is reached.
2923 * @param pTransferCtx Transfer context to create transfer ID for.
2924 * @param pidTransfer Where to return the transfer ID on success.
2925 */
2926int ShClTransferCtxCreateId(PSHCLTRANSFERCTX pTransferCtx, PSHCLTRANSFERID pidTransfer)
2927{
2928 AssertPtrReturn(pTransferCtx, VERR_INVALID_POINTER);
2929 AssertPtrReturn(pidTransfer, VERR_INVALID_POINTER);
2930
2931 shClTransferCtxLock(pTransferCtx);
2932
2933 int rc = shClTransferCreateIDInternal(pTransferCtx, pidTransfer);
2934
2935 shClTransferCtxUnlock(pTransferCtx);
2936
2937 return rc;
2938}
2939
2940/**
2941 * Registers a clipboard transfer with a new transfer ID.
2942 *
2943 * @return VBox status code.
2944 * @retval VERR_SHCLPB_MAX_TRANSFERS_REACHED if the maximum of concurrent transfers is reached.
2945 * @param pTransferCtx Transfer context to register transfer to.
2946 * @param pTransfer Transfer to register. The context takes ownership of the transfer on success.
2947 * @param idTransfer Transfer ID to use for registering the given transfer.
2948 */
2949static int shClTransferCtxTransferRegisterExInternal(PSHCLTRANSFERCTX pTransferCtx, PSHCLTRANSFER pTransfer, SHCLTRANSFERID idTransfer)
2950{
2951 AssertPtrReturn(pTransferCtx, VERR_INVALID_POINTER);
2952 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2953 Assert(idTransfer != NIL_SHCLTRANSFERID);
2954
2955 shClTransferCtxLock(pTransferCtx);
2956
2957 pTransfer->State.uID = idTransfer;
2958
2959 RTListAppend(&pTransferCtx->List, &pTransfer->Node);
2960
2961 pTransferCtx->cTransfers++;
2962
2963 Log2Func(("pTransfer=%p, idTransfer=%RU32 -- now %RU16 transfer(s)\n", pTransfer, idTransfer, pTransferCtx->cTransfers));
2964
2965 shClTransferCtxUnlock(pTransferCtx);
2966
2967 if (pTransfer->Callbacks.pfnOnRegistered)
2968 pTransfer->Callbacks.pfnOnRegistered(&pTransfer->CallbackCtx, pTransferCtx);
2969
2970 LogFlowFuncLeaveRC(VINF_SUCCESS);
2971 return VINF_SUCCESS;
2972}
2973
2974/**
2975 * Registers a clipboard transfer with a transfer context, i.e. allocates a transfer ID.
2976 *
2977 * @return VBox status code.
2978 * @retval VERR_SHCLPB_MAX_TRANSFERS_REACHED if the maximum of concurrent transfers for this context has been reached.
2979 * @param pTransferCtx Transfer context to register transfer to.
2980 * @param pTransfer Transfer to register. The context takes ownership of the transfer on success.
2981 * @param pidTransfer Where to return the transfer ID on success. Optional.
2982 */
2983int ShClTransferCtxRegister(PSHCLTRANSFERCTX pTransferCtx, PSHCLTRANSFER pTransfer, PSHCLTRANSFERID pidTransfer)
2984{
2985 AssertPtrReturn(pTransferCtx, VERR_INVALID_POINTER);
2986 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2987
2988 shClTransferCtxLock(pTransferCtx);
2989
2990 SHCLTRANSFERID idTransfer;
2991 int rc = shClTransferCreateIDInternal(pTransferCtx, &idTransfer);
2992 if (RT_SUCCESS(rc))
2993 {
2994 rc = shClTransferCtxTransferRegisterExInternal(pTransferCtx, pTransfer, idTransfer);
2995 if (RT_SUCCESS(rc))
2996 {
2997 if (pidTransfer)
2998 *pidTransfer = idTransfer;
2999 }
3000 }
3001
3002 shClTransferCtxUnlock(pTransferCtx);
3003
3004 return rc;
3005}
3006
3007/**
3008 * Registers a clipboard transfer with a transfer context by specifying an ID for the transfer.
3009 *
3010 * @return VBox status code.
3011 * @retval VERR_ALREADY_EXISTS if a transfer with the given ID already exists.
3012 * @retval VERR_SHCLPB_MAX_TRANSFERS_REACHED if the maximum of concurrent transfers for this context has been reached.
3013 * @param pTransferCtx Transfer context to register transfer to.
3014 * @param pTransfer Transfer to register.
3015 * @param idTransfer Transfer ID to use for registration.
3016 *
3017 * @note This function ASSUMES you have created \a idTransfer with ShClTransferCtxCreateId().
3018 */
3019int ShClTransferCtxRegisterById(PSHCLTRANSFERCTX pTransferCtx, PSHCLTRANSFER pTransfer, SHCLTRANSFERID idTransfer)
3020{
3021 shClTransferCtxLock(pTransferCtx);
3022
3023 if (pTransferCtx->cTransfers < VBOX_SHCL_MAX_TRANSFERS - 2 /* First and last are not used */)
3024 {
3025 RTListAppend(&pTransferCtx->List, &pTransfer->Node);
3026
3027 shClTransferLock(pTransfer);
3028
3029 pTransfer->State.uID = idTransfer;
3030
3031 shClTransferUnlock(pTransfer);
3032
3033 int rc = shClTransferCtxSignal(pTransferCtx, true /* fRegistered */, pTransfer);
3034
3035 pTransferCtx->cTransfers++;
3036
3037 LogFunc(("Registered transfer ID %RU16 -- now %RU16 transfers total\n", idTransfer, pTransferCtx->cTransfers));
3038
3039 shClTransferCtxUnlock(pTransferCtx);
3040
3041 if (pTransfer->Callbacks.pfnOnRegistered)
3042 pTransfer->Callbacks.pfnOnRegistered(&pTransfer->CallbackCtx, pTransferCtx);
3043
3044 return rc;
3045 }
3046
3047 LogFunc(("Maximum number of transfers reached (%RU16 transfers)\n", pTransferCtx->cTransfers));
3048
3049 shClTransferCtxUnlock(pTransferCtx);
3050
3051 return VERR_SHCLPB_MAX_TRANSFERS_REACHED;
3052}
3053
3054/**
3055 * Removes and unregisters a transfer from a transfer context.
3056 *
3057 * @param pTransferCtx Transfer context to remove transfer from.
3058 * @param pTransfer Transfer to remove.
3059 *
3060 * @note Caller needs to take critical section.
3061 */
3062static void shclTransferCtxTransferRemoveAndUnregister(PSHCLTRANSFERCTX pTransferCtx, PSHCLTRANSFER pTransfer)
3063{
3064 Assert(RTCritSectIsOwner(&pTransferCtx->CritSect));
3065
3066 RTListNodeRemove(&pTransfer->Node);
3067
3068 Assert(pTransferCtx->cTransfers);
3069 pTransferCtx->cTransfers--;
3070
3071 Assert(pTransferCtx->cTransfers >= pTransferCtx->cRunning);
3072
3073 shClTransferCtxUnlock(pTransferCtx);
3074
3075 if (pTransfer->Callbacks.pfnOnUnregistered)
3076 pTransfer->Callbacks.pfnOnUnregistered(&pTransfer->CallbackCtx, pTransferCtx);
3077
3078 shClTransferCtxLock(pTransferCtx);
3079
3080 LogFlowFunc(("Now %RU32 transfers left\n", pTransferCtx->cTransfers));
3081}
3082
3083/**
3084 * Unregisters a transfer from an transfer context, given by its ID.
3085 *
3086 * @retval VINF_SUCCESS on success.
3087 * @retval VERR_NOT_FOUND if the transfer ID was not found.
3088 * @param pTransferCtx Transfer context to unregister transfer from.
3089 * @param idTransfer Transfer ID to unregister.
3090 */
3091int ShClTransferCtxUnregisterById(PSHCLTRANSFERCTX pTransferCtx, SHCLTRANSFERID idTransfer)
3092{
3093 AssertPtrReturn(pTransferCtx, VERR_INVALID_POINTER);
3094 AssertReturn(idTransfer, VERR_INVALID_PARAMETER);
3095
3096 shClTransferCtxLock(pTransferCtx);
3097
3098 int rc = VINF_SUCCESS;
3099
3100 LogFlowFunc(("idTransfer=%RU32\n", idTransfer));
3101
3102 if (ASMBitTestAndClear(&pTransferCtx->bmTransferIds, idTransfer), ("idTransfer=%#x\n", idTransfer))
3103 {
3104 PSHCLTRANSFER pTransfer = shClTransferCtxGetTransferByIdInternal(pTransferCtx, idTransfer);
3105 if (pTransfer)
3106 {
3107 shclTransferCtxTransferRemoveAndUnregister(pTransferCtx, pTransfer);
3108
3109 rc = shClTransferCtxSignal(pTransferCtx, false /* fRegistered */, pTransfer);
3110 }
3111 }
3112 else
3113 rc = VERR_NOT_FOUND;
3114
3115 shClTransferCtxUnlock(pTransferCtx);
3116
3117 LogFlowFuncLeaveRC(rc);
3118 return rc;
3119}
3120
3121/**
3122 * Waits for a transfer context event.
3123 *
3124 * @returns VBox status code.
3125 * @param pTransferCtx Transfer context to wait for.
3126 * @param msTimeout Timeout (in ms) to wait.
3127 * @param pEvent Where to return the event data on success.
3128 */
3129static int shClTransferCtxWaitInternal(PSHCLTRANSFERCTX pTransferCtx, RTMSINTERVAL msTimeout, PSHCLTRANSFERCTXEVENT pEvent)
3130{
3131 LogFlowFunc(("Waiting for transfer context change (%RU32 timeout) ...\n", msTimeout));
3132
3133 int rc = RTSemEventWait(pTransferCtx->ChangedEvent, msTimeout);
3134 if (RT_SUCCESS(rc))
3135 {
3136 shClTransferCtxLock(pTransferCtx);
3137
3138 memcpy(pEvent, &pTransferCtx->ChangedEventData, sizeof(SHCLTRANSFERCTXEVENT));
3139
3140 shClTransferCtxUnlock(pTransferCtx);
3141 }
3142
3143 LogFlowFuncLeaveRC(rc);
3144 return rc;
3145}
3146
3147/**
3148 * Waits for transfer to be (un-)registered.
3149 *
3150 * @returns VBox status code.
3151 * @param pTransferCtx Transfer context to wait for.
3152 * @param msTimeout Timeout (in ms) to wait.
3153 * @param fRegister Pass \c true for registering, or \c false for unregistering a transfer.
3154 * @param idTransfer Transfer ID to wait for.
3155 * Pass NIL_SHCLTRANSFERID for any transfer.
3156 * @param ppTransfer Where to return the transfer being (un-)registered. Optional and can be NULL.
3157 */
3158int ShClTransferCtxWait(PSHCLTRANSFERCTX pTransferCtx, RTMSINTERVAL msTimeout, bool fRegister, SHCLTRANSFERID idTransfer,
3159 PSHCLTRANSFER *ppTransfer)
3160{
3161 AssertPtrReturn(pTransferCtx, VERR_INVALID_POINTER);
3162
3163 int rc = VERR_TIMEOUT;
3164
3165 uint64_t const tsStartMs = RTTimeMilliTS();
3166 uint64_t msLeft = msTimeout;
3167 for (;;)
3168 {
3169 SHCLTRANSFERCTXEVENT Event;
3170 rc = shClTransferCtxWaitInternal(pTransferCtx, msLeft, &Event);
3171 if (RT_FAILURE(rc))
3172 break;
3173
3174 shClTransferCtxLock(pTransferCtx);
3175
3176 if (Event.fRegistered == fRegister)
3177 {
3178 if ( idTransfer != NIL_SHCLTRANSFERID
3179 && Event.pTransfer
3180 && ShClTransferGetID(Event.pTransfer) == idTransfer)
3181 {
3182 if (ppTransfer)
3183 *ppTransfer = Event.pTransfer;
3184 rc = VINF_SUCCESS;
3185 }
3186 }
3187
3188 shClTransferCtxUnlock(pTransferCtx);
3189
3190 if (RT_SUCCESS(rc))
3191 break;
3192
3193 msLeft -= RT_MIN(msLeft, RTTimeMilliTS() - tsStartMs);
3194 if (msLeft == 0)
3195 break;
3196 }
3197
3198 LogFlowFuncLeaveRC(rc);
3199 return rc;
3200}
3201
3202/**
3203 * Cleans up all associated transfers which are not needed (anymore).
3204 * This can be due to transfers which only have been announced but not / never being run.
3205 *
3206 * @param pTransferCtx Transfer context to cleanup transfers for.
3207 */
3208void ShClTransferCtxCleanup(PSHCLTRANSFERCTX pTransferCtx)
3209{
3210 AssertPtrReturnVoid(pTransferCtx);
3211
3212 shClTransferCtxLock(pTransferCtx);
3213
3214 LogFlowFunc(("pTransferCtx=%p, cTransfers=%RU16 cRunning=%RU16\n",
3215 pTransferCtx, pTransferCtx->cTransfers, pTransferCtx->cRunning));
3216
3217 if (pTransferCtx->cTransfers == 0)
3218 {
3219 shClTransferCtxUnlock(pTransferCtx);
3220 return;
3221 }
3222
3223 /* Remove all transfers which are not in a running state (e.g. only announced). */
3224 PSHCLTRANSFER pTransfer, pTransferNext;
3225 RTListForEachSafe(&pTransferCtx->List, pTransfer, pTransferNext, SHCLTRANSFER, Node)
3226 {
3227 shClTransferLock(pTransfer);
3228
3229 SHCLTRANSFERSTATUS const enmStatus = shClTransferGetStatusLocked(pTransfer);
3230 LogFlowFunc(("\tTransfer #%RU16: %s\n", pTransfer->State.uID, ShClTransferStatusToStr(enmStatus)));
3231
3232 if (enmStatus != SHCLTRANSFERSTATUS_STARTED)
3233 {
3234 shClTransferUnlock(pTransfer);
3235
3236 shclTransferCtxTransferRemoveAndUnregister(pTransferCtx, pTransfer);
3237
3238 ShClTransferDestroy(pTransfer);
3239
3240 RTMemFree(pTransfer);
3241 pTransfer = NULL;
3242 }
3243 else
3244 shClTransferUnlock(pTransfer);
3245 }
3246
3247 shClTransferCtxUnlock(pTransferCtx);
3248}
3249
3250/**
3251 * Returns whether the maximum of concurrent transfers of a specific transfer contexthas been reached or not.
3252 *
3253 * @returns \c if maximum has been reached, \c false if not.
3254 * @param pTransferCtx Transfer context to determine value for.
3255 */
3256bool ShClTransferCtxIsMaximumReached(PSHCLTRANSFERCTX pTransferCtx)
3257{
3258 AssertPtrReturn(pTransferCtx, true);
3259
3260 shClTransferCtxLock(pTransferCtx);
3261
3262 LogFlowFunc(("cRunning=%RU32, cMaxRunning=%RU32\n", pTransferCtx->cRunning, pTransferCtx->cMaxRunning));
3263
3264 Assert(pTransferCtx->cRunning <= pTransferCtx->cMaxRunning);
3265 bool const fMaximumReached = pTransferCtx->cRunning == pTransferCtx->cMaxRunning;
3266
3267 shClTransferCtxUnlock(pTransferCtx);
3268
3269 return fMaximumReached;
3270}
3271
3272/**
3273 * Copies file system objinfo from IPRT to Shared Clipboard format.
3274 *
3275 * @return VBox status code.
3276 * @param pDst The Shared Clipboard structure to convert data to.
3277 * @param pSrc The IPRT structure to convert data from.
3278 */
3279int ShClFsObjInfoFromIPRT(PSHCLFSOBJINFO pDst, PCRTFSOBJINFO pSrc)
3280{
3281 AssertPtrReturn(pDst, VERR_INVALID_POINTER);
3282 AssertPtrReturn(pSrc, VERR_INVALID_POINTER);
3283
3284 pDst->cbObject = pSrc->cbObject;
3285 pDst->cbAllocated = pSrc->cbAllocated;
3286 pDst->AccessTime = pSrc->AccessTime;
3287 pDst->ModificationTime = pSrc->ModificationTime;
3288 pDst->ChangeTime = pSrc->ChangeTime;
3289 pDst->BirthTime = pSrc->BirthTime;
3290 pDst->Attr.fMode = pSrc->Attr.fMode;
3291 /* Clear bits which we don't pass through for security reasons. */
3292 pDst->Attr.fMode &= ~(RTFS_UNIX_ISUID | RTFS_UNIX_ISGID | RTFS_UNIX_ISTXT);
3293 RT_ZERO(pDst->Attr.u);
3294 switch (pSrc->Attr.enmAdditional)
3295 {
3296 default:
3297 RT_FALL_THROUGH();
3298 case RTFSOBJATTRADD_NOTHING:
3299 pDst->Attr.enmAdditional = SHCLFSOBJATTRADD_NOTHING;
3300 break;
3301
3302 case RTFSOBJATTRADD_UNIX:
3303 pDst->Attr.enmAdditional = SHCLFSOBJATTRADD_UNIX;
3304 pDst->Attr.u.Unix.uid = pSrc->Attr.u.Unix.uid;
3305 pDst->Attr.u.Unix.gid = pSrc->Attr.u.Unix.gid;
3306 pDst->Attr.u.Unix.cHardlinks = pSrc->Attr.u.Unix.cHardlinks;
3307 pDst->Attr.u.Unix.INodeIdDevice = pSrc->Attr.u.Unix.INodeIdDevice;
3308 pDst->Attr.u.Unix.INodeId = pSrc->Attr.u.Unix.INodeId;
3309 pDst->Attr.u.Unix.fFlags = pSrc->Attr.u.Unix.fFlags;
3310 pDst->Attr.u.Unix.GenerationId = pSrc->Attr.u.Unix.GenerationId;
3311 pDst->Attr.u.Unix.Device = pSrc->Attr.u.Unix.Device;
3312 break;
3313
3314 case RTFSOBJATTRADD_EASIZE:
3315 pDst->Attr.enmAdditional = SHCLFSOBJATTRADD_EASIZE;
3316 pDst->Attr.u.EASize.cb = pSrc->Attr.u.EASize.cb;
3317 break;
3318 }
3319
3320 return VINF_SUCCESS;
3321}
3322
3323/**
3324 * Queries local file system information from a given path.
3325 *
3326 * @returns VBox status code.
3327 * @param pszPath Path to query file system information for.
3328 * @param pObjInfo Where to return the queried file system information on success.
3329 */
3330int ShClFsObjInfoQueryLocal(const char *pszPath, PSHCLFSOBJINFO pObjInfo)
3331{
3332 RTFSOBJINFO objInfo;
3333 int rc = RTPathQueryInfo(pszPath, &objInfo,
3334#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
3335 RTFSOBJATTRADD_NOTHING
3336#else
3337 RTFSOBJATTRADD_UNIX
3338#endif
3339 );
3340 if (RT_SUCCESS(rc))
3341 rc = ShClFsObjInfoFromIPRT(pObjInfo, &objInfo);
3342
3343 return rc;
3344}
3345
3346/**
3347 * Translates a clipboard transfer status (SHCLTRANSFERSTATUS_XXX) into a string.
3348 *
3349 * @returns Transfer status string name.
3350 * @param enmStatus The transfer status to translate.
3351 */
3352const char *ShClTransferStatusToStr(SHCLTRANSFERSTATUS enmStatus)
3353{
3354 switch (enmStatus)
3355 {
3356 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_NONE);
3357 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_REQUESTED);
3358 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_INITIALIZED);
3359 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_UNINITIALIZED);
3360 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_STARTED);
3361 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_COMPLETED);
3362 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_CANCELED);
3363 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_KILLED);
3364 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_ERROR);
3365 }
3366 return "Unknown";
3367}
3368
3369/**
3370 * Validates whether a given path matches our set of rules or not.
3371 *
3372 * @returns VBox status code.
3373 * @param pcszPath Path to validate.
3374 * @param fMustExist Whether the path to validate also must exist.
3375 */
3376int ShClTransferValidatePath(const char *pcszPath, bool fMustExist)
3377{
3378 int rc = VINF_SUCCESS;
3379
3380 if (!strlen(pcszPath))
3381 rc = VERR_INVALID_PARAMETER;
3382
3383 if ( RT_SUCCESS(rc)
3384 && !RTStrIsValidEncoding(pcszPath))
3385 {
3386 rc = VERR_INVALID_UTF8_ENCODING;
3387 }
3388
3389 if ( RT_SUCCESS(rc)
3390 && RTStrStr(pcszPath, ".."))
3391 {
3392 rc = VERR_INVALID_PARAMETER;
3393 }
3394
3395 if ( RT_SUCCESS(rc)
3396 && fMustExist)
3397 {
3398 RTFSOBJINFO objInfo;
3399 rc = RTPathQueryInfo(pcszPath, &objInfo, RTFSOBJATTRADD_NOTHING);
3400 if (RT_SUCCESS(rc))
3401 {
3402 if (RTFS_IS_DIRECTORY(objInfo.Attr.fMode))
3403 {
3404 if (!RTDirExists(pcszPath)) /* Path must exist. */
3405 rc = VERR_PATH_NOT_FOUND;
3406 }
3407 else if (RTFS_IS_FILE(objInfo.Attr.fMode))
3408 {
3409 if (!RTFileExists(pcszPath)) /* File must exist. */
3410 rc = VERR_FILE_NOT_FOUND;
3411 }
3412 else /* Everything else (e.g. symbolic links) are not supported. */
3413 {
3414 LogRelMax(64, ("Shared Clipboard: Path '%s' contains a symbolic link or junction, which are not supported\n", pcszPath));
3415 rc = VERR_NOT_SUPPORTED;
3416 }
3417 }
3418 }
3419
3420 if (RT_FAILURE(rc))
3421 LogRelMax(64, ("Shared Clipboard: Validating path '%s' failed: %Rrc\n", pcszPath, rc));
3422
3423 LogFlowFuncLeaveRC(rc);
3424 return rc;
3425}
3426
3427/**
3428 * Resolves a relative path of a specific transfer to its absolute path.
3429 *
3430 * @returns VBox status code.
3431 * @param pTransfer Clipboard transfer to resolve path for.
3432 * @param pszPath Relative path to resolve.
3433 * Paths have to end with a (back-)slash, otherwise this is considered to be a file.
3434 * @param fFlags Resolve flags. Currently not used and must be 0.
3435 * @param ppszResolved Where to store the allocated resolved path. Must be free'd by the called using RTStrFree().
3436 */
3437int ShClTransferResolvePathAbs(PSHCLTRANSFER pTransfer, const char *pszPath, uint32_t fFlags, char **ppszResolved)
3438{
3439 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
3440 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
3441 AssertReturn (fFlags == 0, VERR_INVALID_PARAMETER);
3442 AssertPtrReturn(ppszResolved, VERR_INVALID_POINTER);
3443
3444 LogFlowFunc(("pszPath=%s, fFlags=%#x (pszPathRootAbs=%s, cRootListEntries=%RU64)\n",
3445 pszPath, fFlags, pTransfer->pszPathRootAbs, pTransfer->lstRoots.Hdr.cEntries));
3446
3447 int rc = ShClTransferValidatePath(pszPath, false /* fMustExist */);
3448 if (RT_SUCCESS(rc))
3449 {
3450 rc = VERR_PATH_NOT_FOUND; /* Play safe by default. */
3451
3452 PSHCLLISTENTRY pEntry;
3453 RTListForEach(&pTransfer->lstRoots.lstEntries, pEntry, SHCLLISTENTRY, Node)
3454 {
3455 LogFlowFunc(("\tpEntry->pszName=%s\n", pEntry->pszName));
3456
3457 if (RTStrStartsWith(pszPath, pEntry->pszName)) /* Case-sensitive! */
3458 {
3459 rc = VINF_SUCCESS;
3460 break;
3461 }
3462 }
3463
3464 if (RT_SUCCESS(rc))
3465 {
3466 char *pszPathAbs = RTPathJoinA(pTransfer->pszPathRootAbs, pszPath);
3467 if (pszPathAbs)
3468 {
3469 char szResolved[RTPATH_MAX];
3470 size_t cbResolved = sizeof(szResolved);
3471 rc = RTPathAbsEx(pTransfer->pszPathRootAbs, pszPathAbs, RTPATH_STR_F_STYLE_HOST, szResolved, &cbResolved);
3472
3473 RTStrFree(pszPathAbs);
3474 pszPathAbs = NULL;
3475
3476 if (RT_SUCCESS(rc))
3477 {
3478 LogRel2(("Shared Clipboard: Resolved: '%s' -> '%s'\n", pszPath, szResolved));
3479
3480 *ppszResolved = RTStrDup(szResolved);
3481 }
3482 }
3483 else
3484 rc = VERR_NO_MEMORY;
3485 }
3486 }
3487
3488 if (RT_FAILURE(rc))
3489 LogRel(("Shared Clipboard: Resolving absolute path for '%s' failed, rc=%Rrc\n", pszPath, rc));
3490
3491 LogFlowFuncLeaveRC(rc);
3492 return rc;
3493}
3494
3495/**
3496 * Converts Shared Clipboard create flags (see SharedClipboard-transfers.h) into IPRT create flags.
3497 *
3498 * @returns IPRT status code.
3499 * @param fShClFlags Shared clipboard create flags.
3500 * @param[out] pfOpen Where to store the RTFILE_O_XXX flags for
3501 * RTFileOpen.
3502 *
3503 * @sa Initially taken from vbsfConvertFileOpenFlags().
3504 */
3505int ShClTransferConvertFileCreateFlags(uint32_t fShClFlags, uint64_t *pfOpen)
3506{
3507 AssertMsgReturnStmt(!(fShClFlags & ~SHCL_OBJ_CF_VALID_MASK), ("%#x4\n", fShClFlags), *pfOpen = 0, VERR_INVALID_FLAGS);
3508
3509 uint64_t fOpen = 0;
3510
3511 switch (fShClFlags & SHCL_OBJ_CF_ACCESS_MASK_RW)
3512 {
3513 case SHCL_OBJ_CF_ACCESS_NONE:
3514 {
3515#ifdef RT_OS_WINDOWS
3516 if ((fShClFlags & SHCL_OBJ_CF_ACCESS_MASK_ATTR) != SHCL_OBJ_CF_ACCESS_ATTR_NONE)
3517 fOpen |= RTFILE_O_OPEN | RTFILE_O_ATTR_ONLY;
3518 else
3519#endif
3520 fOpen |= RTFILE_O_OPEN | RTFILE_O_READ;
3521 LogFlowFunc(("SHCL_OBJ_CF_ACCESS_NONE\n"));
3522 break;
3523 }
3524
3525 case SHCL_OBJ_CF_ACCESS_READ:
3526 {
3527 fOpen |= RTFILE_O_OPEN | RTFILE_O_READ;
3528 LogFlowFunc(("SHCL_OBJ_CF_ACCESS_READ\n"));
3529 break;
3530 }
3531
3532 default:
3533 AssertFailedReturn(VERR_IPE_NOT_REACHED_DEFAULT_CASE);
3534 }
3535
3536 switch (fShClFlags & SHCL_OBJ_CF_ACCESS_MASK_ATTR)
3537 {
3538 case SHCL_OBJ_CF_ACCESS_ATTR_NONE:
3539 {
3540 fOpen |= RTFILE_O_ACCESS_ATTR_DEFAULT;
3541 LogFlowFunc(("SHCL_OBJ_CF_ACCESS_ATTR_NONE\n"));
3542 break;
3543 }
3544
3545 case SHCL_OBJ_CF_ACCESS_ATTR_READ:
3546 {
3547 fOpen |= RTFILE_O_ACCESS_ATTR_READ;
3548 LogFlowFunc(("SHCL_OBJ_CF_ACCESS_ATTR_READ\n"));
3549 break;
3550 }
3551
3552 default:
3553 AssertFailedReturn(VERR_IPE_NOT_REACHED_DEFAULT_CASE);
3554 }
3555
3556 /* Sharing mask */
3557 switch (fShClFlags & SHCL_OBJ_CF_ACCESS_MASK_DENY)
3558 {
3559 case SHCL_OBJ_CF_ACCESS_DENYNONE:
3560 fOpen |= RTFILE_O_DENY_NONE;
3561 LogFlowFunc(("SHCL_OBJ_CF_ACCESS_DENYNONE\n"));
3562 break;
3563
3564 case SHCL_OBJ_CF_ACCESS_DENYWRITE:
3565 fOpen |= RTFILE_O_DENY_WRITE;
3566 LogFlowFunc(("SHCL_OBJ_CF_ACCESS_DENYWRITE\n"));
3567 break;
3568
3569 default:
3570 AssertFailedReturn(VERR_IPE_NOT_REACHED_DEFAULT_CASE);
3571 }
3572
3573 *pfOpen = fOpen;
3574
3575 LogFlowFuncLeaveRC(VINF_SUCCESS);
3576 return VINF_SUCCESS;
3577}
3578
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