VirtualBox

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

Last change on this file since 100636 was 100636, checked in by vboxsync, 21 months ago

Shared Clipboard: Added some more transfer utility functions handy to have. bugref:9437

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 105.7 KB
Line 
1/* $Id: clipboard-transfers.cpp 100636 2023-07-18 15:59:11Z 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 int rc = shClTransferSetStatus(pTransfer, SHCLTRANSFERSTATUS_INITIALIZED);
1228
1229 shClTransferUnlock(pTransfer);
1230
1231 if (RT_SUCCESS(rc))
1232 {
1233 if (pTransfer->Callbacks.pfnOnInitialized)
1234 pTransfer->Callbacks.pfnOnInitialized(&pTransfer->CallbackCtx);
1235 }
1236
1237 LogFlowFuncLeaveRC(rc);
1238 return rc;
1239}
1240
1241/**
1242 * Returns whether a transfer is in a running state or not.
1243 *
1244 * @returns @c true if in running state, or @c false if not.
1245 * @param pTransfer Clipboard transfer to return status for.
1246 */
1247bool ShClTransferIsRunning(PSHCLTRANSFER pTransfer)
1248{
1249 shClTransferLock(pTransfer);
1250
1251 bool const fRunning = pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_STARTED;
1252
1253 shClTransferUnlock(pTransfer);
1254
1255 return fRunning;
1256}
1257
1258/**
1259 * Returns whether a transfer has been (successfully) completed or not.
1260 *
1261 * @returns @c true if complete, or @c false if not.
1262 * @param pTransfer Clipboard transfer to return status for.
1263 */
1264bool ShClTransferIsComplete(PSHCLTRANSFER pTransfer)
1265{
1266 shClTransferLock(pTransfer);
1267
1268 bool const fCompleted = pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_COMPLETED;
1269
1270 shClTransferUnlock(pTransfer);
1271
1272 return fCompleted;
1273}
1274
1275/**
1276 * Returns whether a transfer has been aborted due to cancelling, killing or an error.
1277 *
1278 * @returns @c true if in aborted state, or @c false if not.
1279 * @param pTransfer Clipboard transfer to return status for.
1280 */
1281bool ShClTransferIsAborted(PSHCLTRANSFER pTransfer)
1282{
1283 shClTransferLock(pTransfer);
1284
1285 bool const fAborted = pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_CANCELED
1286 || pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_KILLED
1287 || pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_ERROR;
1288
1289 shClTransferUnlock(pTransfer);
1290
1291 return fAborted;
1292}
1293
1294/**
1295 * Locks a transfer.
1296 *
1297 * @param pTransfer Transfer to lock.
1298 */
1299DECLINLINE(void) shClTransferLock(PSHCLTRANSFER pTransfer)
1300{
1301 int rc2 = RTCritSectEnter(&pTransfer->CritSect);
1302 AssertRC(rc2);
1303}
1304
1305/**
1306 * Unlocks a transfer.
1307 *
1308 * @param pTransfer Transfer to unlock.
1309 */
1310DECLINLINE(void) shClTransferUnlock(PSHCLTRANSFER pTransfer)
1311{
1312 int rc2 = RTCritSectLeave(&pTransfer->CritSect);
1313 AssertRC(rc2);
1314}
1315
1316/**
1317 * Acquires a reference to this transfer.
1318 *
1319 * @returns New reference count.
1320 * @param pTransfer Transfer to acquire reference for.
1321 */
1322uint32_t ShClTransferAcquire(PSHCLTRANSFER pTransfer)
1323{
1324 return ASMAtomicIncU32(&pTransfer->cRefs);
1325}
1326
1327/**
1328 * Releases a reference to this transfer.
1329 *
1330 * @returns New reference count.
1331 * @param pTransfer Transfer to release reference for.
1332 */
1333uint32_t ShClTransferRelease(PSHCLTRANSFER pTransfer)
1334{
1335 const uint32_t cRefs = ASMAtomicDecU32(&pTransfer->cRefs);
1336 Assert(pTransfer->cRefs <= VBOX_SHCL_MAX_TRANSFERS); /* Not perfect, but better than nothing. */
1337 return cRefs;
1338}
1339
1340/**
1341 * Opens a transfer list.
1342 *
1343 * @returns VBox status code.
1344 * @param pTransfer Clipboard transfer to handle.
1345 * @param pOpenParms List open parameters to use for opening.
1346 * @param phList Where to store the List handle of opened list on success.
1347 */
1348int ShClTransferListOpen(PSHCLTRANSFER pTransfer, PSHCLLISTOPENPARMS pOpenParms,
1349 PSHCLLISTHANDLE phList)
1350{
1351 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
1352 AssertPtrReturn(pOpenParms, VERR_INVALID_POINTER);
1353 AssertPtrReturn(phList, VERR_INVALID_POINTER);
1354
1355 if (pTransfer->cListHandles == pTransfer->cMaxListHandles)
1356 return VERR_SHCLPB_MAX_LISTS_REACHED;
1357
1358 int rc;
1359 if (pTransfer->ProviderIface.pfnListOpen)
1360 rc = pTransfer->ProviderIface.pfnListOpen(&pTransfer->ProviderCtx, pOpenParms, phList);
1361 else
1362 rc = VERR_NOT_SUPPORTED;
1363
1364 if (RT_FAILURE(rc))
1365 LogRel(("Shared Clipboard: Opening list '%s' (fiter '%s', flags %#x) failed with %Rrc\n",
1366 pOpenParms->pszPath, pOpenParms->pszFilter, pOpenParms->fList, rc));
1367
1368 LogFlowFuncLeaveRC(rc);
1369 return rc;
1370}
1371
1372/**
1373 * Closes a transfer list.
1374 *
1375 * @returns VBox status code.
1376 * @param pTransfer Clipboard transfer to handle.
1377 * @param hList Handle of list to close.
1378 */
1379int ShClTransferListClose(PSHCLTRANSFER pTransfer, SHCLLISTHANDLE hList)
1380{
1381 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
1382
1383 if (hList == NIL_SHCLLISTHANDLE)
1384 return VINF_SUCCESS;
1385
1386 int rc;
1387 if (pTransfer->ProviderIface.pfnListClose)
1388 rc = pTransfer->ProviderIface.pfnListClose(&pTransfer->ProviderCtx, hList);
1389 else
1390 rc = VERR_NOT_SUPPORTED;
1391
1392 if (RT_FAILURE(rc))
1393 LogRel(("Shared Clipboard: Closing list 0x%x entry failed with %Rrc\n", hList, rc));
1394
1395 LogFlowFuncLeaveRC(rc);
1396 return rc;
1397}
1398
1399/**
1400 * Retrieves the header of a transfer list.
1401 *
1402 * @returns VBox status code.
1403 * @param pTransfer Clipboard transfer to handle.
1404 * @param hList Handle of list to get header for.
1405 * @param pHdr Where to store the returned list header information.
1406 */
1407int ShClTransferListGetHeader(PSHCLTRANSFER pTransfer, SHCLLISTHANDLE hList,
1408 PSHCLLISTHDR pHdr)
1409{
1410 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
1411 AssertPtrReturn(pHdr, VERR_INVALID_POINTER);
1412
1413 LogFlowFunc(("hList=%RU64\n", hList));
1414
1415 int rc;
1416 if (pTransfer->ProviderIface.pfnListHdrRead)
1417 rc = pTransfer->ProviderIface.pfnListHdrRead(&pTransfer->ProviderCtx, hList, pHdr);
1418 else
1419 rc = VERR_NOT_SUPPORTED;
1420
1421 if (RT_FAILURE(rc))
1422 LogRel(("Shared Clipboard: Reading list header list 0x%x entry failed with %Rrc\n", hList, rc));
1423
1424 LogFlowFuncLeaveRC(rc);
1425 return rc;
1426}
1427
1428/**
1429 * Returns a specific list handle info of a clipboard transfer.
1430 *
1431 * @returns Pointer to list handle info if found, or NULL if not found.
1432 * @param pTransfer Clipboard transfer to get list handle info from.
1433 * @param hList List handle of the list to get handle info for.
1434 */
1435PSHCLLISTHANDLEINFO ShClTransferListGetByHandle(PSHCLTRANSFER pTransfer, SHCLLISTHANDLE hList)
1436{
1437 PSHCLLISTHANDLEINFO pIt;
1438 RTListForEach(&pTransfer->lstHandles, pIt, SHCLLISTHANDLEINFO, Node) /** @todo Sloooow ... improve this. */
1439 {
1440 if (pIt->hList == hList)
1441 return pIt;
1442 }
1443
1444 return NULL;
1445}
1446
1447/**
1448 * Returns the a transfer object of a transfer list.
1449 *
1450 * Currently not implemented and wil return NULL.
1451 *
1452 * @returns Pointer to transfer object, or NULL if not found / invalid.
1453 * @param pTransfer Clipboard transfer to return transfer object for.
1454 * @param hList Handle of clipboard transfer list to get object for.
1455 * @param uIdx Index of object to get.
1456 */
1457PSHCLTRANSFEROBJ ShClTransferListGetObj(PSHCLTRANSFER pTransfer,
1458 SHCLLISTHANDLE hList, uint64_t uIdx)
1459{
1460 AssertPtrReturn(pTransfer, NULL);
1461
1462 RT_NOREF(hList, uIdx);
1463
1464 LogFlowFunc(("hList=%RU64\n", hList));
1465
1466 return NULL;
1467}
1468
1469/**
1470 * Reads a single transfer list entry.
1471 *
1472 * @returns VBox status code or VERR_NO_MORE_FILES if the end of the list has been reached.
1473 * @param pTransfer Clipboard transfer to handle.
1474 * @param hList List handle of list to read from.
1475 * @param pEntry Where to store the read information.
1476 */
1477int ShClTransferListRead(PSHCLTRANSFER pTransfer, SHCLLISTHANDLE hList,
1478 PSHCLLISTENTRY pEntry)
1479{
1480 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
1481 AssertPtrReturn(pEntry, VERR_INVALID_POINTER);
1482
1483 LogFlowFunc(("hList=%RU64\n", hList));
1484
1485 int rc;
1486 if (pTransfer->ProviderIface.pfnListEntryRead)
1487 rc = pTransfer->ProviderIface.pfnListEntryRead(&pTransfer->ProviderCtx, hList, pEntry);
1488 else
1489 rc = VERR_NOT_SUPPORTED;
1490
1491 if (RT_FAILURE(rc))
1492 LogRel(("Shared Clipboard: Reading list for list 0x%x entry failed with %Rrc\n", hList, rc));
1493
1494 LogFlowFuncLeaveRC(rc);
1495 return rc;
1496}
1497
1498/**
1499 * Writes a single transfer list entry.
1500 *
1501 * @returns VBox status code.
1502 * @param pTransfer Clipboard transfer to handle.
1503 * @param hList List handle of list to write to.
1504 * @param pEntry Entry information to write.
1505 */
1506int ShClTransferListWrite(PSHCLTRANSFER pTransfer, SHCLLISTHANDLE hList,
1507 PSHCLLISTENTRY pEntry)
1508{
1509 RT_NOREF(pTransfer, hList, pEntry);
1510
1511 int rc = VINF_SUCCESS;
1512
1513#if 0
1514 if (pTransfer->ProviderIface.pfnListEntryWrite)
1515 rc = pTransfer->ProviderIface.pfnListEntryWrite(&pTransfer->ProviderCtx, hList, pEntry);
1516#endif
1517
1518 if (RT_FAILURE(rc))
1519 LogRel(("Shared Clipboard: Writing list entry to list 0x%x failed with %Rrc\n", hList, rc));
1520
1521 LogFlowFuncLeaveRC(rc);
1522 return rc;
1523}
1524
1525/**
1526 * Returns whether a given transfer list handle is valid or not.
1527 *
1528 * @returns \c true if list handle is valid, \c false if not.
1529 * @param pTransfer Clipboard transfer to handle.
1530 * @param hList List handle to check.
1531 */
1532bool ShClTransferListHandleIsValid(PSHCLTRANSFER pTransfer, SHCLLISTHANDLE hList)
1533{
1534 bool fIsValid = false;
1535
1536 if (pTransfer->State.enmSource == SHCLSOURCE_LOCAL)
1537 {
1538 fIsValid = ShClTransferListGetByHandle(pTransfer, hList) != NULL;
1539 }
1540 else if (pTransfer->State.enmSource == SHCLSOURCE_REMOTE)
1541 {
1542 AssertFailed(); /** @todo Implement. */
1543 }
1544 else
1545 AssertFailedStmt(fIsValid = false);
1546
1547 return fIsValid;
1548}
1549
1550/**
1551 * Copies a transfer callback table from source to destination.
1552 *
1553 * @param pCallbacksDst Callback destination.
1554 * @param pCallbacksSrc Callback source. If set to NULL, the
1555 * destination callback table will be unset.
1556 */
1557static void shClTransferCopyCallbacks(PSHCLTRANSFERCALLBACKS pCallbacksDst, PSHCLTRANSFERCALLBACKS pCallbacksSrc)
1558{
1559 AssertPtrReturnVoid(pCallbacksDst);
1560 /* pCallbacksSrc can be NULL */
1561
1562 if (pCallbacksSrc) /* Set */
1563 {
1564#define SET_CALLBACK(a_pfnCallback) \
1565 if (pCallbacksSrc->a_pfnCallback) \
1566 pCallbacksDst->a_pfnCallback = pCallbacksSrc->a_pfnCallback
1567
1568 SET_CALLBACK(pfnOnCreated);
1569 SET_CALLBACK(pfnOnInitialized);
1570 SET_CALLBACK(pfnOnDestroy);
1571 SET_CALLBACK(pfnOnStarted);
1572 SET_CALLBACK(pfnOnCompleted);
1573 SET_CALLBACK(pfnOnError);
1574 SET_CALLBACK(pfnOnRegistered);
1575 SET_CALLBACK(pfnOnUnregistered);
1576
1577#undef SET_CALLBACK
1578
1579 pCallbacksDst->pvUser = pCallbacksSrc->pvUser;
1580 pCallbacksDst->cbUser = pCallbacksSrc->cbUser;
1581 }
1582 else /* Unset */
1583 RT_BZERO(pCallbacksDst, sizeof(SHCLTRANSFERCALLBACKS));
1584}
1585
1586/**
1587 * Sets or unsets the callback table to be used for a clipboard transfer.
1588 *
1589 * @returns VBox status code.
1590 * @param pTransfer Clipboard transfer to set callbacks for.
1591 * @param pCallbacks Pointer to callback table to set. If set to NULL,
1592 * existing callbacks for this transfer will be unset.
1593 *
1594 * @note Must come before initializing the transfer via ShClTransferInit().
1595 */
1596static void shClTransferSetCallbacks(PSHCLTRANSFER pTransfer, PSHCLTRANSFERCALLBACKS pCallbacks)
1597{
1598 AssertPtrReturnVoid(pTransfer);
1599 /* pCallbacks can be NULL. */
1600
1601 shClTransferCopyCallbacks(&pTransfer->Callbacks, pCallbacks);
1602
1603 /* Make sure that the callback context has all values set according to the callback table.
1604 * This only needs to be done once, so do this here. */
1605 pTransfer->CallbackCtx.pTransfer = pTransfer;
1606 pTransfer->CallbackCtx.pvUser = pTransfer->Callbacks.pvUser;
1607 pTransfer->CallbackCtx.cbUser = pTransfer->Callbacks.cbUser;
1608}
1609
1610/**
1611 * Sets the transfer provider for a given transfer.
1612 *
1613 * @returns VBox status code.
1614 * @param pTransfer Transfer to create transfer provider for.
1615 * @param pProvider Provider to use.
1616 */
1617int ShClTransferSetProvider(PSHCLTRANSFER pTransfer, PSHCLTXPROVIDER pProvider)
1618{
1619 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
1620 AssertPtrReturn(pProvider, VERR_INVALID_POINTER);
1621
1622 LogFlowFuncEnter();
1623
1624 int rc = VINF_SUCCESS;
1625
1626 pTransfer->ProviderIface = pProvider->Interface;
1627 pTransfer->ProviderCtx.pTransfer = pTransfer;
1628 pTransfer->ProviderCtx.pvUser = pProvider->pvUser;
1629 pTransfer->ProviderCtx.cbUser = pProvider->cbUser;
1630
1631 LogFlowFuncLeaveRC(rc);
1632 return rc;
1633}
1634
1635/**
1636 * Sets the current status.
1637 *
1638 * @returns VBox status code.
1639 * @param pTransfer Clipboard transfer to set status for.
1640 * @param enmStatus Status to set.
1641 *
1642 * @note Caller needs to take critical section.
1643 */
1644static int shClTransferSetStatus(PSHCLTRANSFER pTransfer, SHCLTRANSFERSTATUS enmStatus)
1645{
1646 Assert(RTCritSectIsOwner(&pTransfer->CritSect));
1647#if 0
1648 AssertMsgReturn(pTransfer->State.enmStatus != enmStatus,
1649 ("Setting the same status twice in a row (%#x), please report this!\n", enmStatus), VERR_WRONG_ORDER);
1650#endif
1651 pTransfer->State.enmStatus = enmStatus;
1652
1653 LogFlowFunc(("enmStatus=%s\n", ShClTransferStatusToStr(pTransfer->State.enmStatus)));
1654
1655 return RTSemEventSignal(pTransfer->StatusChangeEvent);
1656}
1657
1658/**
1659 * Returns the number of transfer root list entries.
1660 *
1661 * @returns Root list entry count.
1662 * @param pTransfer Clipboard transfer to return root entry count for.
1663 */
1664uint64_t ShClTransferRootsCount(PSHCLTRANSFER pTransfer)
1665{
1666 AssertPtrReturn(pTransfer, 0);
1667
1668 shClTransferLock(pTransfer);
1669
1670 uint32_t const cRoots = pTransfer->lstRoots.Hdr.cEntries;
1671
1672 shClTransferUnlock(pTransfer);
1673
1674 return cRoots;
1675}
1676
1677/**
1678 * Resets the root list of a clipboard transfer.
1679 *
1680 * @param pTransfer Transfer to clear transfer root list for.
1681 *
1682 * @note Caller needs to take critical section.
1683 */
1684static void shClTransferRootsReset(PSHCLTRANSFER pTransfer)
1685{
1686 AssertPtrReturnVoid(pTransfer);
1687 Assert(RTCritSectIsOwner(&pTransfer->CritSect));
1688
1689 if (pTransfer->pszPathRootAbs)
1690 {
1691 RTStrFree(pTransfer->pszPathRootAbs);
1692 pTransfer->pszPathRootAbs = NULL;
1693 }
1694
1695 ShClTransferListDestroy(&pTransfer->lstRoots);
1696}
1697
1698/**
1699 * Resets a clipboard transfer.
1700 *
1701 * @param pTransfer Clipboard transfer to reset.
1702 */
1703void ShClTransferReset(PSHCLTRANSFER pTransfer)
1704{
1705 AssertPtrReturnVoid(pTransfer);
1706
1707 LogFlowFuncEnter();
1708
1709 shClTransferLock(pTransfer);
1710
1711 shClTransferRootsReset(pTransfer);
1712
1713 PSHCLLISTHANDLEINFO pItList, pItListNext;
1714 RTListForEachSafe(&pTransfer->lstHandles, pItList, pItListNext, SHCLLISTHANDLEINFO, Node)
1715 {
1716 ShClTransferListHandleInfoDestroy(pItList);
1717
1718 RTListNodeRemove(&pItList->Node);
1719
1720 RTMemFree(pItList);
1721 }
1722
1723 PSHCLTRANSFEROBJ pItObj, pItObjNext;
1724 RTListForEachSafe(&pTransfer->lstObj, pItObj, pItObjNext, SHCLTRANSFEROBJ, Node)
1725 {
1726 ShClTransferObjDestroy(pItObj);
1727
1728 RTListNodeRemove(&pItObj->Node);
1729
1730 RTMemFree(pItObj);
1731 }
1732
1733 shClTransferUnlock(pTransfer);
1734}
1735
1736/**
1737 * Get a specific root list entry.
1738 *
1739 * @returns Const pointer to root list entry if found, or NULL if not found..
1740 * @param pTransfer Clipboard transfer to get root list entry of.
1741 * @param uIndex Index (zero-based) of entry to get.
1742 */
1743PCSHCLLISTENTRY ShClTransferRootsEntryGet(PSHCLTRANSFER pTransfer, uint64_t uIndex)
1744{
1745 AssertPtrReturn(pTransfer, NULL);
1746
1747 shClTransferLock(pTransfer);
1748
1749 if (uIndex >= pTransfer->lstRoots.Hdr.cEntries)
1750 {
1751 shClTransferUnlock(pTransfer);
1752 return NULL;
1753 }
1754
1755 PCSHCLLISTENTRY pEntry = shClTransferListGetEntryById(&pTransfer->lstRoots, uIndex);
1756
1757 shClTransferUnlock(pTransfer);
1758
1759 return pEntry;
1760}
1761
1762/**
1763 * Reads the root entries of a clipboard transfer.
1764 *
1765 * This gives the provider interface the chance of reading root entries information.
1766 *
1767 * @returns VBox status code.
1768 * @param pTransfer Clipboard transfer to read root list for.
1769 * Must be in STARTED state.
1770 */
1771int ShClTransferRootListRead(PSHCLTRANSFER pTransfer)
1772{
1773 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
1774
1775 LogFlowFuncEnter();
1776
1777#ifdef DEBUG
1778 shClTransferLock(pTransfer);
1779 AssertMsgReturn( pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_INITIALIZED
1780 || pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_STARTED,
1781 ("Cannot read root list in status %s\n", ShClTransferStatusToStr(pTransfer->State.enmStatus)),
1782 VERR_WRONG_ORDER);
1783 shClTransferUnlock(pTransfer);
1784#endif
1785
1786 int rc;
1787 if (pTransfer->ProviderIface.pfnRootListRead)
1788 rc = pTransfer->ProviderIface.pfnRootListRead(&pTransfer->ProviderCtx);
1789 else
1790 rc = VERR_NOT_SUPPORTED;
1791
1792 shClTransferLock(pTransfer);
1793
1794 /* Make sure that we have at least an empty root path set. */
1795 if ( RT_SUCCESS(rc)
1796 && !pTransfer->pszPathRootAbs)
1797 {
1798 if (RTStrAPrintf(&pTransfer->pszPathRootAbs, "") < 0)
1799 rc = VERR_NO_MEMORY;
1800 }
1801
1802 shClTransferUnlock(pTransfer);
1803
1804 LogFlowFuncLeaveRC(rc);
1805 return rc;
1806}
1807
1808/**
1809 * Initializes the root list entries for a given clipboard transfer.
1810 *
1811 * @returns VBox status code.
1812 * @param pTransfer Transfer to set transfer list entries for.
1813 * @param pszRoots String list (separated by CRLF) of root entries to set.
1814 * All entries must have the same root path.
1815 * @param cbRoots Size (in bytes) of string list. Includes zero terminator.
1816 *
1817 * @note Accepts local paths or URI string lists (absolute only).
1818 */
1819int ShClTransferRootsInitFromStringList(PSHCLTRANSFER pTransfer, const char *pszRoots, size_t cbRoots)
1820{
1821 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
1822 AssertPtrReturn(pszRoots, VERR_INVALID_POINTER);
1823 AssertReturn(cbRoots, VERR_INVALID_PARAMETER);
1824
1825#ifdef DEBUG_andy
1826 LogFlowFunc(("Data:\n%.*Rhxd\n", cbRoots, pszRoots));
1827#endif
1828
1829 if (!RTStrIsValidEncoding(pszRoots))
1830 return VERR_INVALID_UTF8_ENCODING;
1831
1832 int rc = VINF_SUCCESS;
1833
1834 shClTransferLock(pTransfer);
1835
1836 shClTransferRootsReset(pTransfer);
1837
1838 PSHCLLIST pLstRoots = &pTransfer->lstRoots;
1839 char *pszPathRootAbs = NULL;
1840
1841 RTCList<RTCString> lstRootEntries = RTCString(pszRoots, cbRoots).split(SHCL_TRANSFER_URI_LIST_SEP_STR);
1842 if (!lstRootEntries.size())
1843 {
1844 shClTransferUnlock(pTransfer);
1845 return VINF_SUCCESS;
1846 }
1847
1848 for (size_t i = 0; i < lstRootEntries.size(); ++i)
1849 {
1850 char *pszPathCur = NULL;
1851
1852 char *pszPath = NULL;
1853 rc = RTUriFilePathEx(lstRootEntries.at(i).c_str(),
1854 RTPATH_STR_F_STYLE_UNIX, &pszPath, 0 /*cbPath*/, NULL /*pcchPath*/);
1855 if (RT_SUCCESS(rc))
1856 {
1857 pszPathCur = pszPath;
1858 pszPath = NULL; /* pszPath has ownership now. */
1859 }
1860 else if (rc == VERR_URI_NOT_FILE_SCHEME) /* Local file path? */
1861 {
1862 pszPathCur = RTStrDup(lstRootEntries.at(i).c_str());
1863 rc = VINF_SUCCESS;
1864 }
1865
1866 LogFlowFunc(("pszPathCur=%s\n", pszPathCur));
1867
1868 rc = ShClTransferValidatePath(pszPathCur, false /* fMustExist */);
1869 if (RT_FAILURE(rc))
1870 break;
1871
1872 /* No root path determined yet? */
1873 if (!pszPathRootAbs)
1874 {
1875 pszPathRootAbs = RTStrDup(pszPathCur);
1876 if (pszPathRootAbs)
1877 {
1878 RTPathStripFilename(pszPathRootAbs);
1879
1880 LogFlowFunc(("pszPathRootAbs=%s\n", pszPathRootAbs));
1881
1882 /* We don't want to have a relative directory here. */
1883 if (RTPathStartsWithRoot(pszPathRootAbs))
1884 {
1885 rc = ShClTransferValidatePath(pszPathRootAbs, true /* Path must exist */);
1886 }
1887 else
1888 rc = VERR_PATH_IS_RELATIVE;
1889 }
1890 else
1891 rc = VERR_NO_MEMORY;
1892 }
1893
1894 if (RT_SUCCESS(rc))
1895 {
1896 PSHCLLISTENTRY pEntry;
1897 rc = ShClTransferListEntryAlloc(&pEntry);
1898 if (RT_SUCCESS(rc))
1899 {
1900 PSHCLFSOBJINFO pFsObjInfo = (PSHCLFSOBJINFO)RTMemAllocZ(sizeof(SHCLFSOBJINFO));
1901 if (pFsObjInfo)
1902 {
1903 if (pTransfer->State.enmDir == SHCLTRANSFERDIR_TO_REMOTE)
1904 rc = ShClFsObjInfoQueryLocal(pszPathCur, pFsObjInfo);
1905 if (RT_SUCCESS(rc))
1906 {
1907 /* Calculate the relative path within the root path. */
1908 const char *pszPathRelToRoot = &pszPathCur[strlen(pszPathRootAbs) + 1 /* Skip terminator or (back)slash. */];
1909 if ( pszPathRelToRoot
1910 && *pszPathRelToRoot != '\0')
1911 {
1912 LogFlowFunc(("pszPathRelToRoot=%s\n", pszPathRelToRoot));
1913
1914 rc = ShClTransferListEntryInitEx(pEntry, VBOX_SHCL_INFO_F_FSOBJINFO, pszPathRelToRoot,
1915 pFsObjInfo, sizeof(SHCLFSOBJINFO));
1916 if (RT_SUCCESS(rc))
1917 {
1918 rc = ShClTransferListAddEntry(pLstRoots, pEntry, true /* fAppend */);
1919 if (RT_SUCCESS(rc))
1920 {
1921 pFsObjInfo = NULL; /* pEntry has ownership now. */
1922 }
1923 }
1924 }
1925 else
1926 LogRel(("Shared Clipboard: Unable to construct relative path for '%s' (root is '%s')\n",
1927 pszPathCur, pszPathRootAbs));
1928 }
1929
1930 if (pFsObjInfo)
1931 {
1932 RTMemFree(pFsObjInfo);
1933 pFsObjInfo = NULL;
1934 }
1935 }
1936 else
1937 rc = VERR_NO_MEMORY;
1938
1939 if (RT_FAILURE(rc))
1940 ShClTransferListEntryFree(pEntry);
1941 }
1942 }
1943
1944 RTStrFree(pszPathCur);
1945 }
1946
1947 /* No (valid) root directory found? Bail out early. */
1948 if (!pszPathRootAbs)
1949 rc = VERR_PATH_DOES_NOT_START_WITH_ROOT;
1950
1951 if (RT_SUCCESS(rc))
1952 {
1953 pTransfer->pszPathRootAbs = pszPathRootAbs;
1954 LogFlowFunc(("pszPathRootAbs=%s, cRoots=%zu\n", pTransfer->pszPathRootAbs, pTransfer->lstRoots.Hdr.cEntries));
1955
1956 LogRel2(("Shared Clipboard: Transfer uses root '%s'\n", pTransfer->pszPathRootAbs));
1957 }
1958 else
1959 {
1960 LogRel(("Shared Clipboard: Unable to set roots for transfer, rc=%Rrc\n", rc));
1961 ShClTransferListDestroy(pLstRoots);
1962 RTStrFree(pszPathRootAbs);
1963 }
1964
1965 shClTransferUnlock(pTransfer);
1966
1967 LogFlowFuncLeaveRC(rc);
1968 return rc;
1969}
1970
1971/**
1972 * Initializes the root list entries for a given clipboard transfer, UTF-16 (Unicode) version.
1973 *
1974 * @returns VBox status code.
1975 * @param pTransfer Transfer to set transfer list entries for.
1976 * @param pwszRoots Unicode string list (separated by CRLF) of root entries to set.
1977 * All entries must have the same root path.
1978 * @param cbRoots Size (in bytes) of string list. Includes zero terminator.
1979 *
1980 * @note Accepts local paths or URI string lists (absolute only).
1981 */
1982int ShClTransferRootsInitFromStringListUnicode(PSHCLTRANSFER pTransfer, PRTUTF16 pwszRoots, size_t cbRoots)
1983{
1984 AssertPtrReturn(pwszRoots, VERR_INVALID_POINTER);
1985 AssertReturn(cbRoots, VERR_INVALID_PARAMETER);
1986 AssertReturn(cbRoots % sizeof(RTUTF16) == 0, VERR_INVALID_PARAMETER);
1987
1988 size_t cwcRoots = cbRoots / sizeof(RTUTF16);
1989
1990 /* This may slightly overestimate the space needed. */
1991 size_t chDst = 0;
1992 int rc = ShClUtf16LenUtf8(pwszRoots, cwcRoots, &chDst);
1993 if (RT_SUCCESS(rc))
1994 {
1995 chDst++; /* Add space for terminator. */
1996
1997 char *pszDst = (char *)RTStrAlloc(chDst);
1998 if (pszDst)
1999 {
2000 size_t cbActual = 0;
2001 rc = ShClConvUtf16CRLFToUtf8LF(pwszRoots, cwcRoots, pszDst, chDst, &cbActual);
2002 if (RT_SUCCESS(rc))
2003 rc = ShClTransferRootsInitFromStringList(pTransfer, pszDst, cbActual + 1 /* Include terminator */);
2004
2005 RTStrFree(pszDst);
2006 }
2007 else
2008 rc = VERR_NO_MEMORY;
2009 }
2010
2011 return rc;
2012}
2013
2014/**
2015 * Initializes a single file as a transfer root.
2016 *
2017 * @returns VBox status code.
2018 * @param pTransfer Transfer to set transfer list entries for.
2019 * @param pszFile File to use as transfer root.
2020 *
2021 * @note Convenience function, uses ShClTransferRootsSet() internally.
2022 */
2023int ShClTransferRootsInitFromFile(PSHCLTRANSFER pTransfer, const char *pszFile)
2024{
2025 char *pszRoots = NULL;
2026
2027 LogFlowFuncEnter();
2028
2029 int rc = RTStrAAppend(&pszRoots, pszFile);
2030 AssertRCReturn(rc, rc);
2031 rc = RTStrAAppend(&pszRoots, "\r\n");
2032 AssertRCReturn(rc, rc);
2033 rc = ShClTransferRootsInitFromStringList(pTransfer, pszRoots, strlen(pszRoots) + 1 /* Include terminator */);
2034 RTStrFree(pszRoots);
2035 return rc;
2036}
2037
2038/**
2039 * Returns the clipboard transfer's ID.
2040 *
2041 * @returns The transfer's ID.
2042 * @param pTransfer Clipboard transfer to return ID for.
2043 */
2044SHCLTRANSFERID ShClTransferGetID(PSHCLTRANSFER pTransfer)
2045{
2046 AssertPtrReturn(pTransfer, 0);
2047
2048 shClTransferLock(pTransfer);
2049
2050 SHCLTRANSFERID const uID = pTransfer->State.uID;
2051
2052 shClTransferUnlock(pTransfer);
2053
2054 return uID;
2055}
2056
2057/**
2058 * Returns the clipboard transfer's direction.
2059 *
2060 * @returns The transfer's direction.
2061 * @param pTransfer Clipboard transfer to return direction for.
2062 */
2063SHCLTRANSFERDIR ShClTransferGetDir(PSHCLTRANSFER pTransfer)
2064{
2065 AssertPtrReturn(pTransfer, SHCLTRANSFERDIR_UNKNOWN);
2066
2067 shClTransferLock(pTransfer);
2068
2069 SHCLTRANSFERDIR const enmDir = pTransfer->State.enmDir;
2070
2071 shClTransferUnlock(pTransfer);
2072
2073 return enmDir;
2074}
2075
2076/**
2077 * Returns the absolute root path of a transfer.
2078 *
2079 * @returns VBox status code.
2080 * @param pTransfer Clipboard transfer to return absolute root path for.
2081 * @param pszPath Where to store the returned path.
2082 * @param cbPath Size (in bytes) of \a pszPath.
2083 */
2084int ShClTransferGetRootPathAbs(PSHCLTRANSFER pTransfer, char *pszPath, size_t cbPath)
2085{
2086 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2087
2088 shClTransferLock(pTransfer);
2089
2090 AssertMsgReturn(pTransfer->pszPathRootAbs, ("Transfer has no root path set (yet)\n"), VERR_WRONG_ORDER);
2091
2092 int const rc = RTStrCopy(pszPath, cbPath, pTransfer->pszPathRootAbs);
2093
2094 shClTransferUnlock(pTransfer);
2095
2096 return rc;
2097}
2098
2099/**
2100 * Returns the transfer's source.
2101 *
2102 * @returns The transfer's source.
2103 * @param pTransfer Clipboard transfer to return source for.
2104 */
2105SHCLSOURCE ShClTransferGetSource(PSHCLTRANSFER pTransfer)
2106{
2107 AssertPtrReturn(pTransfer, SHCLSOURCE_INVALID);
2108
2109 shClTransferLock(pTransfer);
2110
2111 SHCLSOURCE const enmSource = pTransfer->State.enmSource;
2112
2113 shClTransferUnlock(pTransfer);
2114
2115 return enmSource;
2116}
2117
2118/**
2119 * Returns the current transfer status.
2120 *
2121 * @returns Current transfer status.
2122 * @param pTransfer Clipboard transfer to return status for.
2123 *
2124 * @note Caller needs to take critical section.
2125 */
2126DECLINLINE(SHCLTRANSFERSTATUS) shClTransferGetStatusLocked(PSHCLTRANSFER pTransfer)
2127{
2128 Assert(RTCritSectIsOwner(&pTransfer->CritSect));
2129
2130 shClTransferLock(pTransfer);
2131
2132 SHCLTRANSFERSTATUS const enmStatus = pTransfer->State.enmStatus;
2133
2134 shClTransferUnlock(pTransfer);
2135
2136 return enmStatus;
2137}
2138
2139/**
2140 * Returns the current transfer status.
2141 *
2142 * @returns Current transfer status.
2143 * @param pTransfer Clipboard transfer to return status for.
2144 */
2145SHCLTRANSFERSTATUS ShClTransferGetStatus(PSHCLTRANSFER pTransfer)
2146{
2147 AssertPtrReturn(pTransfer, SHCLTRANSFERSTATUS_NONE);
2148
2149 shClTransferLock(pTransfer);
2150
2151 SHCLTRANSFERSTATUS const enmSts = shClTransferGetStatusLocked(pTransfer);
2152
2153 shClTransferUnlock(pTransfer);
2154
2155 return enmSts;
2156}
2157
2158/**
2159 * Runs a started clipboard transfer in a dedicated thread.
2160 *
2161 * @returns VBox status code.
2162 * @param pTransfer Clipboard transfer to run.
2163 * @param pfnThreadFunc Pointer to thread function to use.
2164 * @param pvUser Pointer to user-provided data. Optional.
2165 */
2166int ShClTransferRun(PSHCLTRANSFER pTransfer, PFNSHCLTRANSFERTHREAD pfnThreadFunc, void *pvUser)
2167{
2168 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2169 AssertPtrReturn(pfnThreadFunc, VERR_INVALID_POINTER);
2170 /* pvUser is optional. */
2171
2172 AssertMsgReturn(pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_STARTED,
2173 ("Wrong status (currently is %s)\n", ShClTransferStatusToStr(pTransfer->State.enmStatus)),
2174 VERR_WRONG_ORDER);
2175
2176 int rc = shClTransferThreadCreate(pTransfer, pfnThreadFunc, pvUser);
2177
2178 LogFlowFuncLeaveRC(rc);
2179 return rc;
2180}
2181
2182/**
2183 * Starts an initialized transfer.
2184 *
2185 * @returns VBox status code.
2186 * @param pTransfer Clipboard transfer to start.
2187 */
2188int ShClTransferStart(PSHCLTRANSFER pTransfer)
2189{
2190 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2191
2192 LogFlowFuncEnter();
2193
2194 shClTransferLock(pTransfer);
2195
2196 /* Ready to start? */
2197 AssertMsgReturnStmt(pTransfer->ProviderIface.pfnRootListRead != NULL,
2198 ("No provider interface set (yet)\n"),
2199 shClTransferUnlock(pTransfer), VERR_WRONG_ORDER);
2200 AssertMsgReturnStmt(pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_INITIALIZED,
2201 ("Wrong status (currently is %s)\n", ShClTransferStatusToStr(pTransfer->State.enmStatus)),
2202 shClTransferUnlock(pTransfer), VERR_WRONG_ORDER);
2203
2204 int rc = shClTransferSetStatus(pTransfer, SHCLTRANSFERSTATUS_STARTED);
2205
2206 shClTransferUnlock(pTransfer);
2207
2208 if (pTransfer->Callbacks.pfnOnStarted)
2209 pTransfer->Callbacks.pfnOnStarted(&pTransfer->CallbackCtx);
2210
2211 LogFlowFuncLeaveRC(rc);
2212 return rc;
2213}
2214
2215/**
2216 * Completes a transfer (as successful).
2217 *
2218 * @returns VBox status code.
2219 * @param pTransfer Clipboard transfer to complete.
2220 */
2221int ShClTransferComplete(PSHCLTRANSFER pTransfer)
2222{
2223 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2224
2225 LogFlowFuncEnter();
2226
2227 shClTransferLock(pTransfer);
2228
2229 AssertMsgReturnStmt(pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_STARTED,
2230 ("Wrong status (currently is %s)\n", ShClTransferStatusToStr(pTransfer->State.enmStatus)),
2231 shClTransferUnlock(pTransfer), VERR_WRONG_ORDER);
2232
2233 int rc = shClTransferSetStatus(pTransfer, SHCLTRANSFERSTATUS_COMPLETED);
2234
2235 shClTransferUnlock(pTransfer);
2236
2237 if (pTransfer->Callbacks.pfnOnCompleted)
2238 pTransfer->Callbacks.pfnOnCompleted(&pTransfer->CallbackCtx, rc);
2239
2240 LogFlowFuncLeaveRC(rc);
2241 return rc;
2242}
2243
2244/**
2245 * Cancels or sets an error for a transfer.
2246 *
2247 * @returns VBox status code.
2248 * @param pTransfer Clipboard transfer to cancel or set error for.
2249 * @param rc Error code to set.
2250 * If set to VERR_CANCELLED, the transfer will be canceled.
2251 */
2252static int shClTransferCancelOrError(PSHCLTRANSFER pTransfer, int rc)
2253{
2254 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2255
2256 LogFlowFunc(("%Rrc\n", rc));
2257
2258 shClTransferLock(pTransfer);
2259
2260 AssertMsgReturnStmt(pTransfer->State.enmStatus == SHCLTRANSFERSTATUS_STARTED,
2261 ("Wrong status (currently is %s)\n", ShClTransferStatusToStr(pTransfer->State.enmStatus)),
2262 shClTransferUnlock(pTransfer), VERR_WRONG_ORDER);
2263
2264 int rc2;
2265 if (rc == VERR_CANCELLED)
2266 {
2267 rc2 = shClTransferSetStatus(pTransfer, SHCLTRANSFERSTATUS_CANCELED);
2268
2269 if (pTransfer->Callbacks.pfnOnCompleted)
2270 pTransfer->Callbacks.pfnOnCompleted(&pTransfer->CallbackCtx, VERR_CANCELLED);
2271 }
2272 else
2273 {
2274 rc2 = shClTransferSetStatus(pTransfer, SHCLTRANSFERSTATUS_ERROR);
2275
2276 if (pTransfer->Callbacks.pfnOnError)
2277 pTransfer->Callbacks.pfnOnError(&pTransfer->CallbackCtx, rc);
2278 }
2279
2280 shClTransferUnlock(pTransfer);
2281
2282 LogFlowFuncLeaveRC(rc2);
2283 return rc2;
2284}
2285
2286/**
2287 * Cancels a transfer.
2288 *
2289 * @returns VBox status code.
2290 * @param pTransfer Clipboard transfer to cancel.
2291 */
2292int ShClTransferCancel(PSHCLTRANSFER pTransfer)
2293{
2294 return shClTransferCancelOrError(pTransfer, VERR_CANCELLED);
2295}
2296
2297/**
2298 * Kills a transfer.
2299 *
2300 * Currently cancels it internally.
2301 *
2302 * @returns VBox status code.
2303 * @param pTransfer Clipboard transfer to kill.
2304 */
2305int ShClTransferKill(PSHCLTRANSFER pTransfer)
2306{
2307 return ShClTransferCancel(pTransfer);
2308}
2309
2310/**
2311 * Sets an error for a transfer.
2312 *
2313 * @returns VBox status code.
2314 * @param pTransfer Clipboard transfer to set error for.
2315 * @param rc Error code to set.
2316 */
2317int ShClTransferError(PSHCLTRANSFER pTransfer, int rc)
2318{
2319 return shClTransferCancelOrError(pTransfer, rc);
2320}
2321
2322/**
2323 * Internal struct for keeping a transfer thread context.
2324 */
2325typedef struct _SHCLTRANSFERTHREADCTX
2326{
2327 /** Pointer to transfer. */
2328 PSHCLTRANSFER pTransfer;
2329 /** User-supplied context data. Can be NULL if not being used. */
2330 void *pvUser;
2331 /** Pointer to thread function to use. */
2332 PFNSHCLTRANSFERTHREAD pfnThread;
2333} SHCLTRANSFERTHREADCTX;
2334/** Pointer to internal struct for keeping a transfer thread context. */
2335typedef SHCLTRANSFERTHREADCTX *PSHCLTRANSFERTHREADCTX;
2336
2337/**
2338 * Worker thread for a transfer.
2339 *
2340 * @returns VBox status code.
2341 * @param ThreadSelf Thread self handle. Not being used.
2342 * @param pvUser Context data of type PSHCLTRANSFERTHREADCTX.
2343 */
2344static DECLCALLBACK(int) shClTransferThreadWorker(RTTHREAD ThreadSelf, void *pvUser)
2345{
2346 RT_NOREF(ThreadSelf);
2347
2348 LogFlowFuncEnter();
2349
2350 SHCLTRANSFERTHREADCTX Ctx;
2351 memcpy(&Ctx, pvUser, sizeof(SHCLTRANSFERTHREADCTX));
2352
2353 LogFlowFunc(("pfnThread=%p, pTransfer=%p, pvUser=%p\n", Ctx.pfnThread, Ctx.pTransfer, Ctx.pvUser));
2354
2355 PSHCLTRANSFER pTransfer = Ctx.pTransfer;
2356
2357 shClTransferLock(pTransfer);
2358
2359 pTransfer->Thread.fStarted = true;
2360 pTransfer->Thread.fStop = false;
2361
2362 shClTransferUnlock(pTransfer);
2363
2364 RTThreadUserSignal(RTThreadSelf());
2365
2366 int rc = Ctx.pfnThread(pTransfer, Ctx.pvUser);
2367
2368 if (pTransfer->Callbacks.pfnOnCompleted)
2369 pTransfer->Callbacks.pfnOnCompleted(&pTransfer->CallbackCtx, rc);
2370
2371 LogFlowFuncLeaveRC(rc);
2372 return rc;
2373}
2374
2375/**
2376 * Creates a thread for a clipboard transfer.
2377 *
2378 * @returns VBox status code.
2379 * @param pTransfer Clipboard transfer to create thread for.
2380 * @param pfnThreadFunc Thread function to use for this transfer.
2381 * @param pvUser Pointer to user-provided data.
2382 */
2383static int shClTransferThreadCreate(PSHCLTRANSFER pTransfer, PFNSHCLTRANSFERTHREAD pfnThreadFunc, void *pvUser)
2384
2385{
2386 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2387
2388 shClTransferLock(pTransfer);
2389
2390 /* Already marked for stopping? */
2391 AssertMsgReturn(pTransfer->Thread.fStop == false,
2392 ("Transfer thread already marked for stopping"), VERR_WRONG_ORDER);
2393 /* Already started? */
2394 AssertMsgReturn(pTransfer->Thread.fStarted == false,
2395 ("Transfer thread already started"), VERR_WRONG_ORDER);
2396
2397 SHCLTRANSFERTHREADCTX Ctx = { pTransfer, pvUser, pfnThreadFunc };
2398
2399 /* Spawn a worker thread, so that we don't block the window thread for too long. */
2400 int rc = RTThreadCreate(&pTransfer->Thread.hThread, shClTransferThreadWorker,
2401 &Ctx, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE,
2402 "shcltx");
2403 if (RT_SUCCESS(rc))
2404 {
2405 shClTransferUnlock(pTransfer); /* Leave lock while waiting. */
2406
2407 int rc2 = RTThreadUserWait(pTransfer->Thread.hThread, SHCL_TIMEOUT_DEFAULT_MS);
2408 AssertRC(rc2);
2409
2410 shClTransferLock(pTransfer);
2411
2412 if (pTransfer->Thread.fStarted) /* Did the thread indicate that it started correctly? */
2413 {
2414 /* Nothing to do in here. */
2415 }
2416 else
2417 rc = VERR_GENERAL_FAILURE; /** @todo Find a better rc. */
2418 }
2419
2420 shClTransferUnlock(pTransfer);
2421
2422 LogFlowFuncLeaveRC(rc);
2423 return rc;
2424}
2425
2426/**
2427 * Destroys the thread of a clipboard transfer.
2428 *
2429 * @returns VBox status code.
2430 * @param pTransfer Clipboard transfer to destroy thread for.
2431 * @param uTimeoutMs Timeout (in ms) to wait for thread destruction.
2432 */
2433static int shClTransferThreadDestroy(PSHCLTRANSFER pTransfer, RTMSINTERVAL uTimeoutMs)
2434{
2435 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2436
2437 shClTransferLock(pTransfer);
2438
2439 if (pTransfer->Thread.hThread == NIL_RTTHREAD)
2440 {
2441 shClTransferUnlock(pTransfer);
2442 return VINF_SUCCESS;
2443 }
2444
2445 LogFlowFuncEnter();
2446
2447 /* Set stop indicator. */
2448 pTransfer->Thread.fStop = true;
2449
2450 shClTransferUnlock(pTransfer); /* Leave lock while waiting. */
2451
2452 int rcThread = VERR_IPE_UNINITIALIZED_STATUS;
2453 int rc = RTThreadWait(pTransfer->Thread.hThread, uTimeoutMs, &rcThread);
2454
2455 LogFlowFunc(("Waiting for thread resulted in %Rrc (thread exited with %Rrc)\n", rc, rcThread));
2456
2457 return rc;
2458}
2459
2460/**
2461 * Waits for the transfer status to change, internal version.
2462 *
2463 * @returns VBox status code.
2464 * @param pTransfer Clipboard transfer to wait for.
2465 * @param msTimeout Timeout (in ms) to wait.
2466 * @param penmStatus Where to return the new (current) transfer status on success.
2467 * Optional and can be NULL.
2468 */
2469static int shClTransferWaitForStatusChangeInternal(PSHCLTRANSFER pTransfer, RTMSINTERVAL msTimeout, SHCLTRANSFERSTATUS *penmStatus)
2470{
2471 LogFlowFunc(("Waiting for status change (%RU32 timeout) ...\n", msTimeout));
2472
2473 int rc = RTSemEventWait(pTransfer->StatusChangeEvent, msTimeout);
2474 if (RT_SUCCESS(rc))
2475 {
2476 if (penmStatus)
2477 {
2478 shClTransferLock(pTransfer);
2479
2480 *penmStatus = pTransfer->State.enmStatus;
2481
2482 shClTransferUnlock(pTransfer);
2483 }
2484 }
2485
2486 return rc;
2487}
2488
2489/**
2490 * Waits for the transfer status to change.
2491 *
2492 * @returns VBox status code.
2493 * @param pTransfer Clipboard transfer to wait for.
2494 * @param msTimeout Timeout (in ms) to wait.
2495 * @param penmStatus Where to return the new (current) transfer status on success.
2496 * Optional and can be NULL.
2497 */
2498int ShClTransferWaitForStatusChange(PSHCLTRANSFER pTransfer, RTMSINTERVAL msTimeout, SHCLTRANSFERSTATUS *penmStatus)
2499{
2500 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2501
2502 int rc = shClTransferWaitForStatusChangeInternal(pTransfer, msTimeout, penmStatus);
2503
2504 LogFlowFuncLeaveRC(rc);
2505 return rc;
2506}
2507
2508/**
2509 * Waits for a specific transfer status.
2510 *
2511 * @returns VBox status code.
2512 * @param pTransfer Clipboard transfer to wait for.
2513 * @param msTimeout Timeout (in ms) to wait.
2514 * @param enmStatus Transfer status to wait for.
2515 */
2516int ShClTransferWaitForStatus(PSHCLTRANSFER pTransfer, RTMSINTERVAL msTimeout, SHCLTRANSFERSTATUS enmStatus)
2517{
2518 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2519
2520 int rc = VINF_SUCCESS;
2521
2522 uint64_t const tsStartMs = RTTimeMilliTS();
2523 uint64_t msLeft = msTimeout;
2524 for (;;)
2525 {
2526 SHCLTRANSFERSTATUS enmCurStatus;
2527 rc = shClTransferWaitForStatusChangeInternal(pTransfer, msLeft, &enmCurStatus);
2528 if (RT_FAILURE(rc))
2529 break;
2530
2531 if (enmCurStatus == enmStatus)
2532 break;
2533
2534 msLeft -= RT_MIN(msLeft, RTTimeMilliTS() - tsStartMs);
2535 if (msLeft == 0)
2536 {
2537 rc = VERR_TIMEOUT;
2538 break;
2539 }
2540 }
2541
2542 LogFlowFuncLeaveRC(rc);
2543 return rc;
2544}
2545
2546
2547/*********************************************************************************************************************************
2548 * Transfer Context *
2549 ********************************************************************************************************************************/
2550
2551/**
2552 * Locks a transfer context.
2553 *
2554 * @param pTransferCtx Transfer context to lock.
2555 */
2556DECLINLINE(void) shClTransferCtxLock(PSHCLTRANSFERCTX pTransferCtx)
2557{
2558 int rc2 = RTCritSectEnter(&pTransferCtx->CritSect);
2559 AssertRC(rc2);
2560}
2561
2562/**
2563 * Unlocks a transfer context.
2564 *
2565 * @param pTransferCtx Transfer context to unlock.
2566 */
2567DECLINLINE(void) shClTransferCtxUnlock(PSHCLTRANSFERCTX pTransferCtx)
2568{
2569 int rc2 = RTCritSectLeave(&pTransferCtx->CritSect);
2570 AssertRC(rc2);
2571}
2572
2573/**
2574 * Initializes a clipboard transfer context.
2575 *
2576 * @returns VBox status code.
2577 * @param pTransferCtx Transfer context to initialize.
2578 */
2579int ShClTransferCtxInit(PSHCLTRANSFERCTX pTransferCtx)
2580{
2581 AssertPtrReturn(pTransferCtx, VERR_INVALID_POINTER);
2582
2583 LogFlowFunc(("pTransferCtx=%p\n", pTransferCtx));
2584
2585 int rc = RTCritSectInit(&pTransferCtx->CritSect);
2586 if (RT_SUCCESS(rc))
2587 {
2588 rc = RTSemEventCreate(&pTransferCtx->ChangedEvent);
2589 if (RT_SUCCESS(rc))
2590 {
2591 RT_ZERO(pTransferCtx->ChangedEventData);
2592
2593 RTListInit(&pTransferCtx->List);
2594
2595 pTransferCtx->cTransfers = 0;
2596 pTransferCtx->cRunning = 0;
2597 pTransferCtx->cMaxRunning = 64; /** @todo Make this configurable? */
2598
2599 RT_ZERO(pTransferCtx->bmTransferIds);
2600
2601 ShClTransferCtxReset(pTransferCtx);
2602 }
2603 }
2604
2605 return VINF_SUCCESS;
2606}
2607
2608/**
2609 * Destroys a clipboard transfer context.
2610 *
2611 * @param pTransferCtx Transfer context to destroy.
2612 */
2613void ShClTransferCtxDestroy(PSHCLTRANSFERCTX pTransferCtx)
2614{
2615 if (!pTransferCtx)
2616 return;
2617
2618 LogFlowFunc(("pTransferCtx=%p\n", pTransferCtx));
2619
2620 shClTransferCtxLock(pTransferCtx);
2621
2622 PSHCLTRANSFER pTransfer, pTransferNext;
2623 RTListForEachSafe(&pTransferCtx->List, pTransfer, pTransferNext, SHCLTRANSFER, Node)
2624 {
2625 ShClTransferDestroy(pTransfer);
2626
2627 shclTransferCtxTransferRemoveAndUnregister(pTransferCtx, pTransfer);
2628
2629 RTMemFree(pTransfer);
2630 pTransfer = NULL;
2631 }
2632
2633 pTransferCtx->cRunning = 0;
2634 pTransferCtx->cTransfers = 0;
2635
2636 shClTransferCtxUnlock(pTransferCtx);
2637
2638 RTSemEventDestroy(pTransferCtx->ChangedEvent);
2639 pTransferCtx->ChangedEvent = NIL_RTSEMEVENT;
2640
2641 if (RTCritSectIsInitialized(&pTransferCtx->CritSect))
2642 RTCritSectDelete(&pTransferCtx->CritSect);
2643}
2644
2645/**
2646 * Resets a clipboard transfer context.
2647 *
2648 * @param pTransferCtx Transfer context to reset.
2649 */
2650void ShClTransferCtxReset(PSHCLTRANSFERCTX pTransferCtx)
2651{
2652 AssertPtrReturnVoid(pTransferCtx);
2653
2654 shClTransferCtxLock(pTransferCtx);
2655
2656 LogFlowFuncEnter();
2657
2658 PSHCLTRANSFER pTransfer;
2659 RTListForEach(&pTransferCtx->List, pTransfer, SHCLTRANSFER, Node)
2660 ShClTransferReset(pTransfer);
2661
2662#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
2663 /** @todo Anything to do here? */
2664#endif
2665
2666 shClTransferCtxUnlock(pTransferCtx);
2667}
2668
2669/**
2670 * Signals a change event.
2671 *
2672 * @returns VBox status code.
2673 * @param pTransferCtx Transfer context to return transfer for.
2674 * @param fRegistered Whether a transfer got registered or unregistered.
2675 * @param pTransfer Transfer bound to the event.
2676 */
2677static int shClTransferCtxSignal(PSHCLTRANSFERCTX pTransferCtx, bool fRegistered, PSHCLTRANSFER pTransfer)
2678{
2679 Assert(RTCritSectIsOwner(&pTransferCtx->CritSect));
2680
2681 LogFlowFunc(("fRegistered=%RTbool, pTransfer=%p\n", fRegistered, pTransfer));
2682
2683 pTransferCtx->ChangedEventData.fRegistered = fRegistered;
2684 pTransferCtx->ChangedEventData.pTransfer = pTransfer;
2685
2686 return RTSemEventSignal(pTransferCtx->ChangedEvent);
2687}
2688
2689/**
2690 * Returns a specific clipboard transfer, internal version.
2691 *
2692 * @returns Clipboard transfer found, or NULL if not found.
2693 * @param pTransferCtx Transfer context to return transfer for.
2694 * @param idTransfer ID of the transfer to return.
2695 *
2696 * @note Caller needs to take critical section.
2697 */
2698static PSHCLTRANSFER shClTransferCtxGetTransferByIdInternal(PSHCLTRANSFERCTX pTransferCtx, SHCLTRANSFERID idTransfer)
2699{
2700 Assert(RTCritSectIsOwner(&pTransferCtx->CritSect));
2701
2702 PSHCLTRANSFER pTransfer;
2703 RTListForEach(&pTransferCtx->List, pTransfer, SHCLTRANSFER, Node) /** @todo Slow, but works for now. */
2704 {
2705 if (pTransfer->State.uID == idTransfer)
2706 return pTransfer;
2707 }
2708
2709 return NULL;
2710}
2711
2712/**
2713 * Returns a specific clipboard transfer by index, internal version.
2714 *
2715 * @returns Clipboard transfer found, or NULL if not found.
2716 * @param pTransferCtx Transfer context to return transfer for.
2717 * @param uIdx Index of the transfer to return.
2718 *
2719 * @note Caller needs to take critical section.
2720 */
2721static PSHCLTRANSFER shClTransferCtxGetTransferByIndexInternal(PSHCLTRANSFERCTX pTransferCtx, uint32_t uIdx)
2722{
2723 Assert(RTCritSectIsOwner(&pTransferCtx->CritSect));
2724
2725 uint32_t idx = 0;
2726
2727 PSHCLTRANSFER pTransfer;
2728 RTListForEach(&pTransferCtx->List, pTransfer, SHCLTRANSFER, Node) /** @todo Slow, but works for now. */
2729 {
2730 if (uIdx == idx)
2731 return pTransfer;
2732 idx++;
2733 }
2734
2735 return NULL;
2736}
2737
2738/**
2739 * Returns a clipboard transfer for a specific transfer ID.
2740 *
2741 * @returns Clipboard transfer found, or NULL if not found.
2742 * @param pTransferCtx Transfer context to return transfer for.
2743 * @param uID ID of the transfer to return.
2744 */
2745PSHCLTRANSFER ShClTransferCtxGetTransferById(PSHCLTRANSFERCTX pTransferCtx, uint32_t uID)
2746{
2747 shClTransferCtxLock(pTransferCtx);
2748
2749 PSHCLTRANSFER const pTransfer = shClTransferCtxGetTransferByIdInternal(pTransferCtx, uID);
2750
2751 shClTransferCtxUnlock(pTransferCtx);
2752
2753 return pTransfer;
2754}
2755
2756/**
2757 * Returns a clipboard transfer for a specific list index.
2758 *
2759 * @returns Clipboard transfer found, or NULL if not found.
2760 * @param pTransferCtx Transfer context to return transfer for.
2761 * @param uIdx List index of the transfer to return.
2762 */
2763PSHCLTRANSFER ShClTransferCtxGetTransferByIndex(PSHCLTRANSFERCTX pTransferCtx, uint32_t uIdx)
2764{
2765 shClTransferCtxLock(pTransferCtx);
2766
2767 PSHCLTRANSFER const pTransfer = shClTransferCtxGetTransferByIndexInternal(pTransferCtx, uIdx);
2768
2769 shClTransferCtxUnlock(pTransferCtx);
2770
2771 return pTransfer;
2772}
2773
2774
2775/**
2776 * Returns the last clipboard transfer registered.
2777 *
2778 * @returns Clipboard transfer found, or NULL if not found.
2779 * @param pTransferCtx Transfer context to return transfer for.
2780 */
2781PSHCLTRANSFER ShClTransferCtxGetTransferLast(PSHCLTRANSFERCTX pTransferCtx)
2782{
2783 shClTransferCtxLock(pTransferCtx);
2784
2785 PSHCLTRANSFER const pTransfer = RTListGetLast(&pTransferCtx->List, SHCLTRANSFER, Node);
2786
2787 shClTransferCtxUnlock(pTransferCtx);
2788
2789 return pTransfer;
2790}
2791
2792/**
2793 * Returns the number of running clipboard transfers for a given transfer context.
2794 *
2795 * @returns Number of running transfers.
2796 * @param pTransferCtx Transfer context to return number for.
2797 */
2798uint32_t ShClTransferCtxGetRunningTransfers(PSHCLTRANSFERCTX pTransferCtx)
2799{
2800 AssertPtrReturn(pTransferCtx, 0);
2801
2802 shClTransferCtxLock(pTransferCtx);
2803
2804 uint32_t const cRunning = pTransferCtx->cRunning;
2805
2806 shClTransferCtxUnlock(pTransferCtx);
2807
2808 return cRunning;
2809}
2810
2811/**
2812 * Returns the number of total clipboard transfers for a given transfer context.
2813 *
2814 * @returns Number of total transfers.
2815 * @param pTransferCtx Transfer context to return number for.
2816 */
2817uint32_t ShClTransferCtxGetTotalTransfers(PSHCLTRANSFERCTX pTransferCtx)
2818{
2819 AssertPtrReturn(pTransferCtx, 0);
2820
2821 shClTransferCtxLock(pTransferCtx);
2822
2823 uint32_t const cTransfers = pTransferCtx->cTransfers;
2824
2825 shClTransferCtxUnlock(pTransferCtx);
2826
2827 return cTransfers;
2828}
2829
2830static int shClTransferCreateIDInternal(PSHCLTRANSFERCTX pTransferCtx, SHCLTRANSFERID *pidTransfer)
2831{
2832 /*
2833 * Pick a random bit as starting point. If it's in use, search forward
2834 * for a free one, wrapping around. We've reserved both the zero'th and
2835 * max-1 IDs.
2836 */
2837 SHCLTRANSFERID idTransfer = RTRandU32Ex(1, VBOX_SHCL_MAX_TRANSFERS - 2);
2838
2839 if (!ASMBitTestAndSet(&pTransferCtx->bmTransferIds[0], idTransfer))
2840 { /* likely */ }
2841 else if (pTransferCtx->cTransfers < VBOX_SHCL_MAX_TRANSFERS - 2 /* First and last are not used */)
2842 {
2843 /* Forward search. */
2844 int iHit = ASMBitNextClear(&pTransferCtx->bmTransferIds[0], VBOX_SHCL_MAX_TRANSFERS, idTransfer);
2845 if (iHit < 0)
2846 iHit = ASMBitFirstClear(&pTransferCtx->bmTransferIds[0], VBOX_SHCL_MAX_TRANSFERS);
2847 AssertLogRelMsgReturn(iHit >= 0, ("Transfer count: %RU16\n", pTransferCtx->cTransfers), VERR_SHCLPB_MAX_TRANSFERS_REACHED);
2848 idTransfer = iHit;
2849 AssertLogRelMsgReturn(!ASMBitTestAndSet(&pTransferCtx->bmTransferIds[0], idTransfer), ("idObject=%#x\n", idTransfer), VERR_INTERNAL_ERROR_2);
2850 }
2851 else
2852 {
2853 LogFunc(("Maximum number of transfers reached (%RU16 transfers)\n", pTransferCtx->cTransfers));
2854 return VERR_SHCLPB_MAX_TRANSFERS_REACHED;
2855 }
2856
2857 *pidTransfer = idTransfer;
2858
2859 return VINF_SUCCESS;
2860}
2861
2862/**
2863 * Creates a new transfer ID for a given transfer context.
2864 *
2865 * @returns VBox status code.
2866 * @retval VERR_SHCLPB_MAX_TRANSFERS_REACHED if the maximum of concurrent transfers is reached.
2867 * @param pTransferCtx Transfer context to create transfer ID for.
2868 * @param pidTransfer Where to return the transfer ID on success.
2869 */
2870int ShClTransferCtxCreateId(PSHCLTRANSFERCTX pTransferCtx, PSHCLTRANSFERID pidTransfer)
2871{
2872 AssertPtrReturn(pTransferCtx, VERR_INVALID_POINTER);
2873 AssertPtrReturn(pidTransfer, VERR_INVALID_POINTER);
2874
2875 shClTransferCtxLock(pTransferCtx);
2876
2877 int rc = shClTransferCreateIDInternal(pTransferCtx, pidTransfer);
2878
2879 shClTransferCtxUnlock(pTransferCtx);
2880
2881 return rc;
2882}
2883
2884/**
2885 * Registers a clipboard transfer with a new transfer ID.
2886 *
2887 * @return VBox status code.
2888 * @retval VERR_SHCLPB_MAX_TRANSFERS_REACHED if the maximum of concurrent transfers is reached.
2889 * @param pTransferCtx Transfer context to register transfer to.
2890 * @param pTransfer Transfer to register. The context takes ownership of the transfer on success.
2891 * @param idTransfer Transfer ID to use for registering the given transfer.
2892 */
2893static int shClTransferCtxTransferRegisterExInternal(PSHCLTRANSFERCTX pTransferCtx, PSHCLTRANSFER pTransfer, SHCLTRANSFERID idTransfer)
2894{
2895 AssertPtrReturn(pTransferCtx, VERR_INVALID_POINTER);
2896 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2897 Assert(idTransfer != NIL_SHCLTRANSFERID);
2898
2899 shClTransferCtxLock(pTransferCtx);
2900
2901 pTransfer->State.uID = idTransfer;
2902
2903 RTListAppend(&pTransferCtx->List, &pTransfer->Node);
2904
2905 pTransferCtx->cTransfers++;
2906
2907 Log2Func(("pTransfer=%p, idTransfer=%RU32 -- now %RU16 transfer(s)\n", pTransfer, idTransfer, pTransferCtx->cTransfers));
2908
2909 shClTransferCtxUnlock(pTransferCtx);
2910
2911 if (pTransfer->Callbacks.pfnOnRegistered)
2912 pTransfer->Callbacks.pfnOnRegistered(&pTransfer->CallbackCtx, pTransferCtx);
2913
2914 LogFlowFuncLeaveRC(VINF_SUCCESS);
2915 return VINF_SUCCESS;
2916}
2917
2918/**
2919 * Registers a clipboard transfer with a transfer context, i.e. allocates a transfer ID.
2920 *
2921 * @return VBox status code.
2922 * @retval VERR_SHCLPB_MAX_TRANSFERS_REACHED if the maximum of concurrent transfers for this context has been reached.
2923 * @param pTransferCtx Transfer context to register transfer to.
2924 * @param pTransfer Transfer to register. The context takes ownership of the transfer on success.
2925 * @param pidTransfer Where to return the transfer ID on success. Optional.
2926 */
2927int ShClTransferCtxRegister(PSHCLTRANSFERCTX pTransferCtx, PSHCLTRANSFER pTransfer, PSHCLTRANSFERID pidTransfer)
2928{
2929 AssertPtrReturn(pTransferCtx, VERR_INVALID_POINTER);
2930 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
2931
2932 shClTransferCtxLock(pTransferCtx);
2933
2934 SHCLTRANSFERID idTransfer;
2935 int rc = shClTransferCreateIDInternal(pTransferCtx, &idTransfer);
2936 if (RT_SUCCESS(rc))
2937 {
2938 rc = shClTransferCtxTransferRegisterExInternal(pTransferCtx, pTransfer, idTransfer);
2939 if (RT_SUCCESS(rc))
2940 {
2941 if (pidTransfer)
2942 *pidTransfer = idTransfer;
2943 }
2944 }
2945
2946 shClTransferCtxUnlock(pTransferCtx);
2947
2948 return rc;
2949}
2950
2951/**
2952 * Registers a clipboard transfer with a transfer context by specifying an ID for the transfer.
2953 *
2954 * @return VBox status code.
2955 * @retval VERR_ALREADY_EXISTS if a transfer with the given ID already exists.
2956 * @retval VERR_SHCLPB_MAX_TRANSFERS_REACHED if the maximum of concurrent transfers for this context has been reached.
2957 * @param pTransferCtx Transfer context to register transfer to.
2958 * @param pTransfer Transfer to register.
2959 * @param idTransfer Transfer ID to use for registration.
2960 *
2961 * @note This function ASSUMES you have created \a idTransfer with ShClTransferCtxCreateId().
2962 */
2963int ShClTransferCtxRegisterById(PSHCLTRANSFERCTX pTransferCtx, PSHCLTRANSFER pTransfer, SHCLTRANSFERID idTransfer)
2964{
2965 shClTransferCtxLock(pTransferCtx);
2966
2967 if (pTransferCtx->cTransfers < VBOX_SHCL_MAX_TRANSFERS - 2 /* First and last are not used */)
2968 {
2969 RTListAppend(&pTransferCtx->List, &pTransfer->Node);
2970
2971 shClTransferLock(pTransfer);
2972
2973 pTransfer->State.uID = idTransfer;
2974
2975 shClTransferUnlock(pTransfer);
2976
2977 int rc = shClTransferCtxSignal(pTransferCtx, true /* fRegistered */, pTransfer);
2978
2979 pTransferCtx->cTransfers++;
2980
2981 LogFunc(("Registered transfer ID %RU16 -- now %RU16 transfers total\n", idTransfer, pTransferCtx->cTransfers));
2982
2983 shClTransferCtxUnlock(pTransferCtx);
2984
2985 if (pTransfer->Callbacks.pfnOnRegistered)
2986 pTransfer->Callbacks.pfnOnRegistered(&pTransfer->CallbackCtx, pTransferCtx);
2987
2988 return rc;
2989 }
2990
2991 LogFunc(("Maximum number of transfers reached (%RU16 transfers)\n", pTransferCtx->cTransfers));
2992
2993 shClTransferCtxUnlock(pTransferCtx);
2994
2995 return VERR_SHCLPB_MAX_TRANSFERS_REACHED;
2996}
2997
2998/**
2999 * Removes and unregisters a transfer from a transfer context.
3000 *
3001 * @param pTransferCtx Transfer context to remove transfer from.
3002 * @param pTransfer Transfer to remove.
3003 *
3004 * @note Caller needs to take critical section.
3005 */
3006static void shclTransferCtxTransferRemoveAndUnregister(PSHCLTRANSFERCTX pTransferCtx, PSHCLTRANSFER pTransfer)
3007{
3008 Assert(RTCritSectIsOwner(&pTransferCtx->CritSect));
3009
3010 RTListNodeRemove(&pTransfer->Node);
3011
3012 Assert(pTransferCtx->cTransfers);
3013 pTransferCtx->cTransfers--;
3014
3015 Assert(pTransferCtx->cTransfers >= pTransferCtx->cRunning);
3016
3017 shClTransferCtxUnlock(pTransferCtx);
3018
3019 if (pTransfer->Callbacks.pfnOnUnregistered)
3020 pTransfer->Callbacks.pfnOnUnregistered(&pTransfer->CallbackCtx, pTransferCtx);
3021
3022 shClTransferCtxLock(pTransferCtx);
3023
3024 LogFlowFunc(("Now %RU32 transfers left\n", pTransferCtx->cTransfers));
3025}
3026
3027/**
3028 * Unregisters a transfer from an transfer context, given by its ID.
3029 *
3030 * @retval VINF_SUCCESS on success.
3031 * @retval VERR_NOT_FOUND if the transfer ID was not found.
3032 * @param pTransferCtx Transfer context to unregister transfer from.
3033 * @param idTransfer Transfer ID to unregister.
3034 */
3035int ShClTransferCtxUnregisterById(PSHCLTRANSFERCTX pTransferCtx, SHCLTRANSFERID idTransfer)
3036{
3037 AssertPtrReturn(pTransferCtx, VERR_INVALID_POINTER);
3038 AssertReturn(idTransfer, VERR_INVALID_PARAMETER);
3039
3040 shClTransferCtxLock(pTransferCtx);
3041
3042 int rc = VINF_SUCCESS;
3043
3044 LogFlowFunc(("idTransfer=%RU32\n", idTransfer));
3045
3046 if (ASMBitTestAndClear(&pTransferCtx->bmTransferIds, idTransfer), ("idTransfer=%#x\n", idTransfer))
3047 {
3048 PSHCLTRANSFER pTransfer = shClTransferCtxGetTransferByIdInternal(pTransferCtx, idTransfer);
3049 if (pTransfer)
3050 {
3051 shclTransferCtxTransferRemoveAndUnregister(pTransferCtx, pTransfer);
3052
3053 rc = shClTransferCtxSignal(pTransferCtx, false /* fRegistered */, pTransfer);
3054 }
3055 }
3056 else
3057 rc = VERR_NOT_FOUND;
3058
3059 shClTransferCtxUnlock(pTransferCtx);
3060
3061 LogFlowFuncLeaveRC(rc);
3062 return rc;
3063}
3064
3065/**
3066 * Waits for a transfer context event.
3067 *
3068 * @returns VBox status code.
3069 * @param pTransferCtx Transfer context to wait for.
3070 * @param msTimeout Timeout (in ms) to wait.
3071 * @param pEvent Where to return the event data on success.
3072 */
3073static int shClTransferCtxWaitInternal(PSHCLTRANSFERCTX pTransferCtx, RTMSINTERVAL msTimeout, PSHCLTRANSFERCTXEVENT pEvent)
3074{
3075 LogFlowFunc(("Waiting for transfer context change (%RU32 timeout) ...\n", msTimeout));
3076
3077 int rc = RTSemEventWait(pTransferCtx->ChangedEvent, msTimeout);
3078 if (RT_SUCCESS(rc))
3079 {
3080 shClTransferCtxLock(pTransferCtx);
3081
3082 memcpy(pEvent, &pTransferCtx->ChangedEventData, sizeof(SHCLTRANSFERCTXEVENT));
3083
3084 shClTransferCtxUnlock(pTransferCtx);
3085 }
3086
3087 LogFlowFuncLeaveRC(rc);
3088 return rc;
3089}
3090
3091/**
3092 * Waits for transfer to be (un-)registered.
3093 *
3094 * @returns VBox status code.
3095 * @param pTransferCtx Transfer context to wait for.
3096 * @param msTimeout Timeout (in ms) to wait.
3097 * @param fRegister Pass \c true for registering, or \c false for unregistering a transfer.
3098 * @param idTransfer Transfer ID to wait for.
3099 * Pass NIL_SHCLTRANSFERID for any transfer.
3100 * @param ppTransfer Where to return the transfer being (un-)registered. Optional and can be NULL.
3101 */
3102int ShClTransferCtxWait(PSHCLTRANSFERCTX pTransferCtx, RTMSINTERVAL msTimeout, bool fRegister, SHCLTRANSFERID idTransfer,
3103 PSHCLTRANSFER *ppTransfer)
3104{
3105 AssertPtrReturn(pTransferCtx, VERR_INVALID_POINTER);
3106
3107 int rc = VERR_TIMEOUT;
3108
3109 uint64_t const tsStartMs = RTTimeMilliTS();
3110 uint64_t msLeft = msTimeout;
3111 for (;;)
3112 {
3113 SHCLTRANSFERCTXEVENT Event;
3114 rc = shClTransferCtxWaitInternal(pTransferCtx, msLeft, &Event);
3115 if (RT_FAILURE(rc))
3116 break;
3117
3118 shClTransferCtxLock(pTransferCtx);
3119
3120 if (Event.fRegistered == fRegister)
3121 {
3122 if ( idTransfer != NIL_SHCLTRANSFERID
3123 && Event.pTransfer
3124 && ShClTransferGetID(Event.pTransfer) == idTransfer)
3125 {
3126 if (ppTransfer)
3127 *ppTransfer = Event.pTransfer;
3128 rc = VINF_SUCCESS;
3129 }
3130 }
3131
3132 shClTransferCtxUnlock(pTransferCtx);
3133
3134 if (RT_SUCCESS(rc))
3135 break;
3136
3137 msLeft -= RT_MIN(msLeft, RTTimeMilliTS() - tsStartMs);
3138 if (msLeft == 0)
3139 break;
3140 }
3141
3142 LogFlowFuncLeaveRC(rc);
3143 return rc;
3144}
3145
3146/**
3147 * Cleans up all associated transfers which are not needed (anymore).
3148 * This can be due to transfers which only have been announced but not / never being run.
3149 *
3150 * @param pTransferCtx Transfer context to cleanup transfers for.
3151 */
3152void ShClTransferCtxCleanup(PSHCLTRANSFERCTX pTransferCtx)
3153{
3154 AssertPtrReturnVoid(pTransferCtx);
3155
3156 shClTransferCtxLock(pTransferCtx);
3157
3158 LogFlowFunc(("pTransferCtx=%p, cTransfers=%RU16 cRunning=%RU16\n",
3159 pTransferCtx, pTransferCtx->cTransfers, pTransferCtx->cRunning));
3160
3161 if (pTransferCtx->cTransfers == 0)
3162 {
3163 shClTransferCtxUnlock(pTransferCtx);
3164 return;
3165 }
3166
3167 /* Remove all transfers which are not in a running state (e.g. only announced). */
3168 PSHCLTRANSFER pTransfer, pTransferNext;
3169 RTListForEachSafe(&pTransferCtx->List, pTransfer, pTransferNext, SHCLTRANSFER, Node)
3170 {
3171 shClTransferLock(pTransfer);
3172
3173 SHCLTRANSFERSTATUS const enmStatus = shClTransferGetStatusLocked(pTransfer);
3174 LogFlowFunc(("\tTransfer #%RU16: %s\n", pTransfer->State.uID, ShClTransferStatusToStr(enmStatus)));
3175
3176 if (enmStatus != SHCLTRANSFERSTATUS_STARTED)
3177 {
3178 shClTransferUnlock(pTransfer);
3179
3180 shclTransferCtxTransferRemoveAndUnregister(pTransferCtx, pTransfer);
3181
3182 ShClTransferDestroy(pTransfer);
3183
3184 RTMemFree(pTransfer);
3185 pTransfer = NULL;
3186 }
3187 else
3188 shClTransferUnlock(pTransfer);
3189 }
3190
3191 shClTransferCtxUnlock(pTransferCtx);
3192}
3193
3194/**
3195 * Returns whether the maximum of concurrent transfers of a specific transfer contexthas been reached or not.
3196 *
3197 * @returns \c if maximum has been reached, \c false if not.
3198 * @param pTransferCtx Transfer context to determine value for.
3199 */
3200bool ShClTransferCtxIsMaximumReached(PSHCLTRANSFERCTX pTransferCtx)
3201{
3202 AssertPtrReturn(pTransferCtx, true);
3203
3204 shClTransferCtxLock(pTransferCtx);
3205
3206 LogFlowFunc(("cRunning=%RU32, cMaxRunning=%RU32\n", pTransferCtx->cRunning, pTransferCtx->cMaxRunning));
3207
3208 Assert(pTransferCtx->cRunning <= pTransferCtx->cMaxRunning);
3209 bool const fMaximumReached = pTransferCtx->cRunning == pTransferCtx->cMaxRunning;
3210
3211 shClTransferCtxUnlock(pTransferCtx);
3212
3213 return fMaximumReached;
3214}
3215
3216/**
3217 * Copies file system objinfo from IPRT to Shared Clipboard format.
3218 *
3219 * @return VBox status code.
3220 * @param pDst The Shared Clipboard structure to convert data to.
3221 * @param pSrc The IPRT structure to convert data from.
3222 */
3223int ShClFsObjInfoFromIPRT(PSHCLFSOBJINFO pDst, PCRTFSOBJINFO pSrc)
3224{
3225 AssertPtrReturn(pDst, VERR_INVALID_POINTER);
3226 AssertPtrReturn(pSrc, VERR_INVALID_POINTER);
3227
3228 pDst->cbObject = pSrc->cbObject;
3229 pDst->cbAllocated = pSrc->cbAllocated;
3230 pDst->AccessTime = pSrc->AccessTime;
3231 pDst->ModificationTime = pSrc->ModificationTime;
3232 pDst->ChangeTime = pSrc->ChangeTime;
3233 pDst->BirthTime = pSrc->BirthTime;
3234 pDst->Attr.fMode = pSrc->Attr.fMode;
3235 /* Clear bits which we don't pass through for security reasons. */
3236 pDst->Attr.fMode &= ~(RTFS_UNIX_ISUID | RTFS_UNIX_ISGID | RTFS_UNIX_ISTXT);
3237 RT_ZERO(pDst->Attr.u);
3238 switch (pSrc->Attr.enmAdditional)
3239 {
3240 default:
3241 RT_FALL_THROUGH();
3242 case RTFSOBJATTRADD_NOTHING:
3243 pDst->Attr.enmAdditional = SHCLFSOBJATTRADD_NOTHING;
3244 break;
3245
3246 case RTFSOBJATTRADD_UNIX:
3247 pDst->Attr.enmAdditional = SHCLFSOBJATTRADD_UNIX;
3248 pDst->Attr.u.Unix.uid = pSrc->Attr.u.Unix.uid;
3249 pDst->Attr.u.Unix.gid = pSrc->Attr.u.Unix.gid;
3250 pDst->Attr.u.Unix.cHardlinks = pSrc->Attr.u.Unix.cHardlinks;
3251 pDst->Attr.u.Unix.INodeIdDevice = pSrc->Attr.u.Unix.INodeIdDevice;
3252 pDst->Attr.u.Unix.INodeId = pSrc->Attr.u.Unix.INodeId;
3253 pDst->Attr.u.Unix.fFlags = pSrc->Attr.u.Unix.fFlags;
3254 pDst->Attr.u.Unix.GenerationId = pSrc->Attr.u.Unix.GenerationId;
3255 pDst->Attr.u.Unix.Device = pSrc->Attr.u.Unix.Device;
3256 break;
3257
3258 case RTFSOBJATTRADD_EASIZE:
3259 pDst->Attr.enmAdditional = SHCLFSOBJATTRADD_EASIZE;
3260 pDst->Attr.u.EASize.cb = pSrc->Attr.u.EASize.cb;
3261 break;
3262 }
3263
3264 return VINF_SUCCESS;
3265}
3266
3267/**
3268 * Queries local file system information from a given path.
3269 *
3270 * @returns VBox status code.
3271 * @param pszPath Path to query file system information for.
3272 * @param pObjInfo Where to return the queried file system information on success.
3273 */
3274int ShClFsObjInfoQueryLocal(const char *pszPath, PSHCLFSOBJINFO pObjInfo)
3275{
3276 RTFSOBJINFO objInfo;
3277 int rc = RTPathQueryInfo(pszPath, &objInfo,
3278#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
3279 RTFSOBJATTRADD_NOTHING
3280#else
3281 RTFSOBJATTRADD_UNIX
3282#endif
3283 );
3284 if (RT_SUCCESS(rc))
3285 rc = ShClFsObjInfoFromIPRT(pObjInfo, &objInfo);
3286
3287 return rc;
3288}
3289
3290/**
3291 * Translates a clipboard transfer status (SHCLTRANSFERSTATUS_XXX) into a string.
3292 *
3293 * @returns Transfer status string name.
3294 * @param enmStatus The transfer status to translate.
3295 */
3296const char *ShClTransferStatusToStr(SHCLTRANSFERSTATUS enmStatus)
3297{
3298 switch (enmStatus)
3299 {
3300 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_NONE);
3301 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_REQUESTED);
3302 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_INITIALIZED);
3303 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_UNINITIALIZED);
3304 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_STARTED);
3305 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_COMPLETED);
3306 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_CANCELED);
3307 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_KILLED);
3308 RT_CASE_RET_STR(SHCLTRANSFERSTATUS_ERROR);
3309 }
3310 return "Unknown";
3311}
3312
3313/**
3314 * Validates whether a given path matches our set of rules or not.
3315 *
3316 * @returns VBox status code.
3317 * @param pcszPath Path to validate.
3318 * @param fMustExist Whether the path to validate also must exist.
3319 */
3320int ShClTransferValidatePath(const char *pcszPath, bool fMustExist)
3321{
3322 int rc = VINF_SUCCESS;
3323
3324 if (!strlen(pcszPath))
3325 rc = VERR_INVALID_PARAMETER;
3326
3327 if ( RT_SUCCESS(rc)
3328 && !RTStrIsValidEncoding(pcszPath))
3329 {
3330 rc = VERR_INVALID_UTF8_ENCODING;
3331 }
3332
3333 if ( RT_SUCCESS(rc)
3334 && RTStrStr(pcszPath, ".."))
3335 {
3336 rc = VERR_INVALID_PARAMETER;
3337 }
3338
3339 if ( RT_SUCCESS(rc)
3340 && fMustExist)
3341 {
3342 RTFSOBJINFO objInfo;
3343 rc = RTPathQueryInfo(pcszPath, &objInfo, RTFSOBJATTRADD_NOTHING);
3344 if (RT_SUCCESS(rc))
3345 {
3346 if (RTFS_IS_DIRECTORY(objInfo.Attr.fMode))
3347 {
3348 if (!RTDirExists(pcszPath)) /* Path must exist. */
3349 rc = VERR_PATH_NOT_FOUND;
3350 }
3351 else if (RTFS_IS_FILE(objInfo.Attr.fMode))
3352 {
3353 if (!RTFileExists(pcszPath)) /* File must exist. */
3354 rc = VERR_FILE_NOT_FOUND;
3355 }
3356 else /* Everything else (e.g. symbolic links) are not supported. */
3357 {
3358 LogRelMax(64, ("Shared Clipboard: Path '%s' contains a symbolic link or junction, which are not supported\n", pcszPath));
3359 rc = VERR_NOT_SUPPORTED;
3360 }
3361 }
3362 }
3363
3364 if (RT_FAILURE(rc))
3365 LogRelMax(64, ("Shared Clipboard: Validating path '%s' failed: %Rrc\n", pcszPath, rc));
3366
3367 LogFlowFuncLeaveRC(rc);
3368 return rc;
3369}
3370
3371/**
3372 * Resolves a relative path of a specific transfer to its absolute path.
3373 *
3374 * @returns VBox status code.
3375 * @param pTransfer Clipboard transfer to resolve path for.
3376 * @param pszPath Relative path to resolve.
3377 * Paths have to end with a (back-)slash, otherwise this is considered to be a file.
3378 * @param fFlags Resolve flags. Currently not used and must be 0.
3379 * @param ppszResolved Where to store the allocated resolved path. Must be free'd by the called using RTStrFree().
3380 */
3381int ShClTransferResolvePathAbs(PSHCLTRANSFER pTransfer, const char *pszPath, uint32_t fFlags, char **ppszResolved)
3382{
3383 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
3384 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
3385 AssertReturn (fFlags == 0, VERR_INVALID_PARAMETER);
3386 AssertPtrReturn(ppszResolved, VERR_INVALID_POINTER);
3387
3388 LogFlowFunc(("pszPath=%s, fFlags=%#x (pszPathRootAbs=%s, cRootListEntries=%RU64)\n",
3389 pszPath, fFlags, pTransfer->pszPathRootAbs, pTransfer->lstRoots.Hdr.cEntries));
3390
3391 int rc = ShClTransferValidatePath(pszPath, false /* fMustExist */);
3392 if (RT_SUCCESS(rc))
3393 {
3394 rc = VERR_PATH_NOT_FOUND; /* Play safe by default. */
3395
3396 PSHCLLISTENTRY pEntry;
3397 RTListForEach(&pTransfer->lstRoots.lstEntries, pEntry, SHCLLISTENTRY, Node)
3398 {
3399 LogFlowFunc(("\tpEntry->pszName=%s\n", pEntry->pszName));
3400
3401 if (RTStrStartsWith(pszPath, pEntry->pszName)) /* Case-sensitive! */
3402 {
3403 rc = VINF_SUCCESS;
3404 break;
3405 }
3406 }
3407
3408 if (RT_SUCCESS(rc))
3409 {
3410 char *pszPathAbs = RTPathJoinA(pTransfer->pszPathRootAbs, pszPath);
3411 if (pszPathAbs)
3412 {
3413 char szResolved[RTPATH_MAX];
3414 size_t cbResolved = sizeof(szResolved);
3415 rc = RTPathAbsEx(pTransfer->pszPathRootAbs, pszPathAbs, RTPATH_STR_F_STYLE_HOST, szResolved, &cbResolved);
3416
3417 RTStrFree(pszPathAbs);
3418 pszPathAbs = NULL;
3419
3420 if (RT_SUCCESS(rc))
3421 {
3422 LogRel2(("Shared Clipboard: Resolved: '%s' -> '%s'\n", pszPath, szResolved));
3423
3424 *ppszResolved = RTStrDup(szResolved);
3425 }
3426 }
3427 else
3428 rc = VERR_NO_MEMORY;
3429 }
3430 }
3431
3432 if (RT_FAILURE(rc))
3433 LogRel(("Shared Clipboard: Resolving absolute path for '%s' failed, rc=%Rrc\n", pszPath, rc));
3434
3435 LogFlowFuncLeaveRC(rc);
3436 return rc;
3437}
3438
3439/**
3440 * Converts Shared Clipboard create flags (see SharedClipboard-transfers.h) into IPRT create flags.
3441 *
3442 * @returns IPRT status code.
3443 * @param fShClFlags Shared clipboard create flags.
3444 * @param[out] pfOpen Where to store the RTFILE_O_XXX flags for
3445 * RTFileOpen.
3446 *
3447 * @sa Initially taken from vbsfConvertFileOpenFlags().
3448 */
3449int ShClTransferConvertFileCreateFlags(uint32_t fShClFlags, uint64_t *pfOpen)
3450{
3451 AssertMsgReturnStmt(!(fShClFlags & ~SHCL_OBJ_CF_VALID_MASK), ("%#x4\n", fShClFlags), *pfOpen = 0, VERR_INVALID_FLAGS);
3452
3453 uint64_t fOpen = 0;
3454
3455 switch (fShClFlags & SHCL_OBJ_CF_ACCESS_MASK_RW)
3456 {
3457 case SHCL_OBJ_CF_ACCESS_NONE:
3458 {
3459#ifdef RT_OS_WINDOWS
3460 if ((fShClFlags & SHCL_OBJ_CF_ACCESS_MASK_ATTR) != SHCL_OBJ_CF_ACCESS_ATTR_NONE)
3461 fOpen |= RTFILE_O_OPEN | RTFILE_O_ATTR_ONLY;
3462 else
3463#endif
3464 fOpen |= RTFILE_O_OPEN | RTFILE_O_READ;
3465 LogFlowFunc(("SHCL_OBJ_CF_ACCESS_NONE\n"));
3466 break;
3467 }
3468
3469 case SHCL_OBJ_CF_ACCESS_READ:
3470 {
3471 fOpen |= RTFILE_O_OPEN | RTFILE_O_READ;
3472 LogFlowFunc(("SHCL_OBJ_CF_ACCESS_READ\n"));
3473 break;
3474 }
3475
3476 default:
3477 AssertFailedReturn(VERR_IPE_NOT_REACHED_DEFAULT_CASE);
3478 }
3479
3480 switch (fShClFlags & SHCL_OBJ_CF_ACCESS_MASK_ATTR)
3481 {
3482 case SHCL_OBJ_CF_ACCESS_ATTR_NONE:
3483 {
3484 fOpen |= RTFILE_O_ACCESS_ATTR_DEFAULT;
3485 LogFlowFunc(("SHCL_OBJ_CF_ACCESS_ATTR_NONE\n"));
3486 break;
3487 }
3488
3489 case SHCL_OBJ_CF_ACCESS_ATTR_READ:
3490 {
3491 fOpen |= RTFILE_O_ACCESS_ATTR_READ;
3492 LogFlowFunc(("SHCL_OBJ_CF_ACCESS_ATTR_READ\n"));
3493 break;
3494 }
3495
3496 default:
3497 AssertFailedReturn(VERR_IPE_NOT_REACHED_DEFAULT_CASE);
3498 }
3499
3500 /* Sharing mask */
3501 switch (fShClFlags & SHCL_OBJ_CF_ACCESS_MASK_DENY)
3502 {
3503 case SHCL_OBJ_CF_ACCESS_DENYNONE:
3504 fOpen |= RTFILE_O_DENY_NONE;
3505 LogFlowFunc(("SHCL_OBJ_CF_ACCESS_DENYNONE\n"));
3506 break;
3507
3508 case SHCL_OBJ_CF_ACCESS_DENYWRITE:
3509 fOpen |= RTFILE_O_DENY_WRITE;
3510 LogFlowFunc(("SHCL_OBJ_CF_ACCESS_DENYWRITE\n"));
3511 break;
3512
3513 default:
3514 AssertFailedReturn(VERR_IPE_NOT_REACHED_DEFAULT_CASE);
3515 }
3516
3517 *pfOpen = fOpen;
3518
3519 LogFlowFuncLeaveRC(VINF_SUCCESS);
3520 return VINF_SUCCESS;
3521}
3522
Note: See TracBrowser for help on using the repository browser.

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