1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
|
/* $Id: ClipboardDataObjectImpl-win.cpp $ */
/** @file
* ClipboardDataObjectImpl-win.cpp - Shared Clipboard IDataObject implementation.
*/
/*
* Copyright (C) 2019-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
#include <VBox/GuestHost/SharedClipboard-win.h>
#include <VBox/GuestHost/SharedClipboard-transfers.h>
#include <iprt/win/windows.h>
#include <iprt/win/shlobj.h>
#include <iprt/win/shlwapi.h>
#include <iprt/asm.h>
#include <iprt/err.h>
#include <iprt/path.h>
#include <iprt/semaphore.h>
#include <iprt/uri.h>
#include <iprt/utf16.h>
#include <iprt/errcore.h>
#include <VBox/log.h>
/** @todo Also handle Unicode entries.
* !!! WARNING: Buggy, doesn't work yet (some memory corruption / garbage in the file name descriptions) !!! */
//#define VBOX_CLIPBOARD_WITH_UNICODE_SUPPORT 1
SharedClipboardWinDataObject::SharedClipboardWinDataObject(PSHCLTRANSFER pTransfer,
LPFORMATETC pFormatEtc, LPSTGMEDIUM pStgMed, ULONG cFormats)
: m_enmStatus(Uninitialized)
, m_lRefCount(0)
, m_cFormats(0)
, m_pTransfer(pTransfer)
, m_pStream(NULL)
, m_uObjIdx(0)
, m_fRunning(false)
, m_EventListComplete(NIL_RTSEMEVENT)
, m_EventTransferComplete(NIL_RTSEMEVENT)
{
AssertPtr(m_pTransfer);
HRESULT hr;
ULONG cFixedFormats = 3; /* CFSTR_FILEDESCRIPTORA + CFSTR_FILECONTENTS + CFSTR_PERFORMEDDROPEFFECT */
#ifdef VBOX_CLIPBOARD_WITH_UNICODE_SUPPORT
cFixedFormats++; /* CFSTR_FILEDESCRIPTORW */
#endif
const ULONG cAllFormats = cFormats + cFixedFormats;
try
{
m_pFormatEtc = new FORMATETC[cAllFormats];
RT_BZERO(m_pFormatEtc, sizeof(FORMATETC) * cAllFormats);
m_pStgMedium = new STGMEDIUM[cAllFormats];
RT_BZERO(m_pStgMedium, sizeof(STGMEDIUM) * cAllFormats);
/** @todo Do we need CFSTR_FILENAME / CFSTR_SHELLIDLIST here? */
/*
* Register fixed formats.
*/
unsigned uIdx = 0;
LogFlowFunc(("Registering CFSTR_FILEDESCRIPTORA ...\n"));
m_cfFileDescriptorA = RegisterClipboardFormat(CFSTR_FILEDESCRIPTORA);
registerFormat(&m_pFormatEtc[uIdx++], m_cfFileDescriptorA);
#ifdef VBOX_CLIPBOARD_WITH_UNICODE_SUPPORT
LogFlowFunc(("Registering CFSTR_FILEDESCRIPTORW ...\n"));
m_cfFileDescriptorW = RegisterClipboardFormat(CFSTR_FILEDESCRIPTORW);
registerFormat(&m_pFormatEtc[uIdx++], m_cfFileDescriptorW);
#endif
/* IStream interface, implemented in ClipboardStreamImpl-win.cpp. */
LogFlowFunc(("Registering CFSTR_FILECONTENTS ...\n"));
m_cfFileContents = RegisterClipboardFormat(CFSTR_FILECONTENTS);
registerFormat(&m_pFormatEtc[uIdx++], m_cfFileContents, TYMED_ISTREAM, 0 /* lIndex */);
/* We want to know from the target what the outcome of the operation was to react accordingly (e.g. abort a transfer). */
LogFlowFunc(("Registering CFSTR_PERFORMEDDROPEFFECT ...\n"));
m_cfPerformedDropEffect = RegisterClipboardFormat(CFSTR_PERFORMEDDROPEFFECT);
registerFormat(&m_pFormatEtc[uIdx++], m_cfPerformedDropEffect, TYMED_HGLOBAL, -1 /* lIndex */, DVASPECT_CONTENT);
/*
* Registration of dynamic formats needed?
*/
LogFlowFunc(("%RU32 dynamic formats\n", cFormats));
if (cFormats)
{
AssertPtr(pFormatEtc);
AssertPtr(pStgMed);
for (ULONG i = 0; i < cFormats; i++)
{
LogFlowFunc(("Format %RU32: cfFormat=%RI16, tyMed=%RU32, dwAspect=%RU32\n",
i, pFormatEtc[i].cfFormat, pFormatEtc[i].tymed, pFormatEtc[i].dwAspect));
m_pFormatEtc[cFixedFormats + i] = pFormatEtc[i];
m_pStgMedium[cFixedFormats + i] = pStgMed[i];
}
}
hr = S_OK;
}
catch (std::bad_alloc &)
{
hr = E_OUTOFMEMORY;
}
if (SUCCEEDED(hr))
{
m_cFormats = cAllFormats;
m_enmStatus = Initialized;
int rc2 = RTSemEventCreate(&m_EventListComplete);
AssertRC(rc2);
rc2 = RTSemEventCreate(&m_EventTransferComplete);
AssertRC(rc2);
}
LogFlowFunc(("cAllFormats=%RU32, hr=%Rhrc\n", cAllFormats, hr));
}
SharedClipboardWinDataObject::~SharedClipboardWinDataObject(void)
{
LogFlowFuncEnter();
RTSemEventDestroy(m_EventListComplete);
m_EventListComplete = NIL_RTSEMEVENT;
RTSemEventDestroy(m_EventTransferComplete);
m_EventTransferComplete = NIL_RTSEMEVENT;
if (m_pStream)
m_pStream->Release();
if (m_pFormatEtc)
delete[] m_pFormatEtc;
if (m_pStgMedium)
delete[] m_pStgMedium;
LogFlowFunc(("mRefCount=%RI32\n", m_lRefCount));
}
/*
* IUnknown methods.
*/
STDMETHODIMP_(ULONG) SharedClipboardWinDataObject::AddRef(void)
{
LONG lCount = InterlockedIncrement(&m_lRefCount);
LogFlowFunc(("lCount=%RI32\n", lCount));
return lCount;
}
STDMETHODIMP_(ULONG) SharedClipboardWinDataObject::Release(void)
{
LONG lCount = InterlockedDecrement(&m_lRefCount);
LogFlowFunc(("lCount=%RI32\n", m_lRefCount));
if (lCount == 0)
{
delete this;
return 0;
}
return lCount;
}
STDMETHODIMP SharedClipboardWinDataObject::QueryInterface(REFIID iid, void **ppvObject)
{
AssertPtrReturn(ppvObject, E_INVALIDARG);
if ( iid == IID_IDataObject
|| iid == IID_IUnknown)
{
AddRef();
*ppvObject = this;
return S_OK;
}
*ppvObject = 0;
return E_NOINTERFACE;
}
/**
* Copies a chunk of data into a HGLOBAL object.
*
* @returns VBox status code.
* @param pvData Data to copy.
* @param cbData Size (in bytes) to copy.
* @param fFlags GlobalAlloc flags, used for allocating the HGLOBAL block.
* @param phGlobal Where to store the allocated HGLOBAL object.
*/
int SharedClipboardWinDataObject::copyToHGlobal(const void *pvData, size_t cbData, UINT fFlags, HGLOBAL *phGlobal)
{
AssertPtrReturn(phGlobal, VERR_INVALID_POINTER);
HGLOBAL hGlobal = GlobalAlloc(fFlags, cbData);
if (!hGlobal)
return VERR_NO_MEMORY;
void *pvAlloc = GlobalLock(hGlobal);
if (pvAlloc)
{
CopyMemory(pvAlloc, pvData, cbData);
GlobalUnlock(hGlobal);
*phGlobal = hGlobal;
return VINF_SUCCESS;
}
GlobalFree(hGlobal);
return VERR_ACCESS_DENIED;
}
/**
* Reads (handles) a specific directory reursively and inserts its entry into the
* objects's entry list.
*
* @returns VBox status code.
* @param pTransfer Shared Clipboard transfer object to handle.
* @param strDir Directory path to handle.
*/
int SharedClipboardWinDataObject::readDir(PSHCLTRANSFER pTransfer, const Utf8Str &strDir)
{
LogFlowFunc(("strDir=%s\n", strDir.c_str()));
SHCLLISTOPENPARMS openParmsList;
int rc = ShClTransferListOpenParmsInit(&openParmsList);
if (RT_SUCCESS(rc))
{
rc = RTStrCopy(openParmsList.pszPath, openParmsList.cbPath, strDir.c_str());
if (RT_SUCCESS(rc))
{
SHCLLISTHANDLE hList;
rc = ShClTransferListOpen(pTransfer, &openParmsList, &hList);
if (RT_SUCCESS(rc))
{
LogFlowFunc(("strDir=%s -> hList=%RU64\n", strDir.c_str(), hList));
SHCLLISTHDR hdrList;
rc = ShClTransferListGetHeader(pTransfer, hList, &hdrList);
if (RT_SUCCESS(rc))
{
LogFlowFunc(("cTotalObjects=%RU64, cbTotalSize=%RU64\n\n",
hdrList.cTotalObjects, hdrList.cbTotalSize));
for (uint64_t o = 0; o < hdrList.cTotalObjects; o++)
{
SHCLLISTENTRY entryList;
rc = ShClTransferListEntryInit(&entryList);
if (RT_SUCCESS(rc))
{
rc = ShClTransferListRead(pTransfer, hList, &entryList);
if (RT_SUCCESS(rc))
{
if (ShClTransferListEntryIsValid(&entryList))
{
PSHCLFSOBJINFO pFsObjInfo = (PSHCLFSOBJINFO)entryList.pvInfo;
Assert(entryList.cbInfo == sizeof(SHCLFSOBJINFO));
Utf8Str strPath = strDir + Utf8Str("\\") + Utf8Str(entryList.pszName);
LogFlowFunc(("\t%s (%RU64 bytes) -> %s\n",
entryList.pszName, pFsObjInfo->cbObject, strPath.c_str()));
if (RTFS_IS_DIRECTORY(pFsObjInfo->Attr.fMode))
{
FSOBJENTRY objEntry = { strPath.c_str(), *pFsObjInfo };
m_lstEntries.push_back(objEntry); /** @todo Can this throw? */
rc = readDir(pTransfer, strPath.c_str());
}
else if (RTFS_IS_FILE(pFsObjInfo->Attr.fMode))
{
FSOBJENTRY objEntry = { strPath.c_str(), *pFsObjInfo };
m_lstEntries.push_back(objEntry); /** @todo Can this throw? */
}
else
rc = VERR_NOT_SUPPORTED;
/** @todo Handle symlinks. */
}
else
rc = VERR_INVALID_PARAMETER;
}
ShClTransferListEntryDestroy(&entryList);
}
if ( RT_FAILURE(rc)
&& pTransfer->Thread.fStop)
break;
}
}
ShClTransferListClose(pTransfer, hList);
}
}
ShClTransferListOpenParmsDestroy(&openParmsList);
}
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* Thread for reading transfer data.
* The data object needs the (high level, root) transfer listing at the time of ::GetData(), so we need
* to block and wait until we have this data (via this thread) and continue.
*
* @returns VBox status code.
* @param ThreadSelf Thread handle. Unused at the moment.
* @param pvUser Pointer to user-provided data. Of type SharedClipboardWinDataObject.
*/
/* static */
DECLCALLBACK(int) SharedClipboardWinDataObject::readThread(RTTHREAD ThreadSelf, void *pvUser)
{
RT_NOREF(ThreadSelf);
LogFlowFuncEnter();
SharedClipboardWinDataObject *pThis = (SharedClipboardWinDataObject *)pvUser;
PSHCLTRANSFER pTransfer = pThis->m_pTransfer;
AssertPtr(pTransfer);
pTransfer->Thread.fStarted = true;
pTransfer->Thread.fStop = false;
RTThreadUserSignal(RTThreadSelf());
LogRel2(("Shared Clipboard: Calculating transfer ...\n"));
PSHCLROOTLIST pRootList;
int rc = ShClTransferRootsGet(pTransfer, &pRootList);
if (RT_SUCCESS(rc))
{
LogFlowFunc(("cRoots=%RU32\n\n", pRootList->Hdr.cRoots));
for (uint32_t i = 0; i < pRootList->Hdr.cRoots; i++)
{
PSHCLLISTENTRY pRootEntry = &pRootList->paEntries[i];
AssertPtr(pRootEntry);
Assert(pRootEntry->cbInfo == sizeof(SHCLFSOBJINFO));
PSHCLFSOBJINFO pFsObjInfo = (PSHCLFSOBJINFO)pRootEntry->pvInfo;
LogFlowFunc(("pszRoot=%s, fMode=0x%x\n", pRootEntry->pszName, pFsObjInfo->Attr.fMode));
if (RTFS_IS_DIRECTORY(pFsObjInfo->Attr.fMode))
{
FSOBJENTRY objEntry = { pRootEntry->pszName, *pFsObjInfo };
pThis->m_lstEntries.push_back(objEntry); /** @todo Can this throw? */
rc = pThis->readDir(pTransfer, pRootEntry->pszName);
}
else if (RTFS_IS_FILE(pFsObjInfo->Attr.fMode))
{
FSOBJENTRY objEntry = { pRootEntry->pszName, *pFsObjInfo };
pThis->m_lstEntries.push_back(objEntry); /** @todo Can this throw? */
}
else
rc = VERR_NOT_SUPPORTED;
if (ASMAtomicReadBool(&pTransfer->Thread.fStop))
{
LogRel2(("Shared Clipboard: Stopping transfer calculation ...\n"));
break;
}
if (RT_FAILURE(rc))
break;
}
ShClTransferRootListFree(pRootList);
pRootList = NULL;
if ( RT_SUCCESS(rc)
&& !ASMAtomicReadBool(&pTransfer->Thread.fStop))
{
LogRel2(("Shared Clipboard: Transfer calculation complete (%zu root entries)\n", pThis->m_lstEntries.size()));
/*
* Signal the "list complete" event so that this data object can return (valid) data via ::GetData().
* This in turn then will create IStream instances (by the OS) for each file system object to handle.
*/
int rc2 = RTSemEventSignal(pThis->m_EventListComplete);
AssertRC(rc2);
if (pThis->m_lstEntries.size())
{
LogRel2(("Shared Clipboard: Waiting for transfer to complete ...\n"));
LogFlowFunc(("Waiting for transfer to complete ...\n"));
/* Transferring stuff can take a while, so don't use any timeout here. */
rc2 = RTSemEventWait(pThis->m_EventTransferComplete, RT_INDEFINITE_WAIT);
AssertRC(rc2);
switch (pThis->m_enmStatus)
{
case Completed:
LogRel2(("Shared Clipboard: Transfer complete\n"));
break;
case Canceled:
LogRel2(("Shared Clipboard: Transfer canceled\n"));
break;
case Error:
LogRel2(("Shared Clipboard: Transfer error occurred\n"));
break;
default:
break;
}
}
else
LogRel(("Shared Clipboard: No transfer root entries found -- should not happen, please file a bug report\n"));
}
else if (RT_FAILURE(rc))
LogRel(("Shared Clipboard: Transfer failed with %Rrc\n", rc));
}
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* Creates a FILEGROUPDESCRIPTOR object from a given Shared Clipboard transfer and stores the result into an HGLOBAL object.
*
* @returns VBox status code.
* @param pTransfer Shared Clipboard transfer to create file grou desciprtor for.
* @param fUnicode Whether the FILEGROUPDESCRIPTOR object shall contain Unicode data or not.
* @param phGlobal Where to store the allocated HGLOBAL object on success.
*/
int SharedClipboardWinDataObject::createFileGroupDescriptorFromTransfer(PSHCLTRANSFER pTransfer,
bool fUnicode, HGLOBAL *phGlobal)
{
AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
AssertPtrReturn(phGlobal, VERR_INVALID_POINTER);
LogFlowFuncEnter();
const size_t cbFileGroupDescriptor = fUnicode ? sizeof(FILEGROUPDESCRIPTORW) : sizeof(FILEGROUPDESCRIPTORA);
const size_t cbFileDescriptor = fUnicode ? sizeof(FILEDESCRIPTORW) : sizeof(FILEDESCRIPTORA);
const UINT cItems = (UINT)m_lstEntries.size(); /** UINT vs. size_t. */
if (!cItems)
return VERR_NOT_FOUND;
UINT curIdx = 0; /* Current index of the handled file group descriptor (FGD). */
const size_t cbFGD = cbFileGroupDescriptor + (cbFileDescriptor * (cItems - 1));
LogFunc(("fUnicode=%RTbool, cItems=%u, cbFileDescriptor=%zu\n", fUnicode, cItems, cbFileDescriptor));
/* FILEGROUPDESCRIPTORA / FILEGROUPDESCRIPTOR matches except the cFileName member (TCHAR vs. WCHAR). */
FILEGROUPDESCRIPTOR *pFGD = (FILEGROUPDESCRIPTOR *)RTMemAllocZ(cbFGD);
if (!pFGD)
return VERR_NO_MEMORY;
int rc = VINF_SUCCESS;
pFGD->cItems = cItems;
char *pszFileSpec = NULL;
FsObjEntryList::const_iterator itRoot = m_lstEntries.begin();
while (itRoot != m_lstEntries.end())
{
FILEDESCRIPTOR *pFD = &pFGD->fgd[curIdx];
RT_BZERO(pFD, cbFileDescriptor);
const char *pszFile = itRoot->strPath.c_str();
AssertPtr(pszFile);
pszFileSpec = RTStrDup(pszFile);
AssertBreakStmt(pszFileSpec != NULL, rc = VERR_NO_MEMORY);
if (fUnicode)
{
PRTUTF16 pwszFileSpec;
rc = RTStrToUtf16(pszFileSpec, &pwszFileSpec);
if (RT_SUCCESS(rc))
{
rc = RTUtf16CopyEx((PRTUTF16 )pFD->cFileName, sizeof(pFD->cFileName) / sizeof(WCHAR),
pwszFileSpec, RTUtf16Len(pwszFileSpec));
RTUtf16Free(pwszFileSpec);
LogFlowFunc(("pFD->cFileNameW=%ls\n", pFD->cFileName));
}
}
else
{
rc = RTStrCopy(pFD->cFileName, sizeof(pFD->cFileName), pszFileSpec);
LogFlowFunc(("pFD->cFileNameA=%s\n", pFD->cFileName));
}
RTStrFree(pszFileSpec);
pszFileSpec = NULL;
if (RT_FAILURE(rc))
break;
pFD->dwFlags = FD_PROGRESSUI | FD_ATTRIBUTES;
if (fUnicode) /** @todo Only >= Vista. */
pFD->dwFlags |= FD_UNICODE;
pFD->dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
const SHCLFSOBJINFO *pObjInfo = &itRoot->objInfo;
if (RTFS_IS_DIRECTORY(pObjInfo->Attr.fMode))
{
pFD->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
}
else if (RTFS_IS_FILE(pObjInfo->Attr.fMode))
{
pFD->dwFlags |= FD_FILESIZE;
const uint64_t cbObjSize = pObjInfo->cbObject;
pFD->nFileSizeHigh = RT_HI_U32(cbObjSize);
pFD->nFileSizeLow = RT_LO_U32(cbObjSize);
}
else if (RTFS_IS_SYMLINK(pObjInfo->Attr.fMode))
{
/** @todo Implement. */
}
#if 0 /** @todo Implement this. */
pFD->dwFlags = FD_ATTRIBUTES | FD_CREATETIME | FD_ACCESSTIME | FD_WRITESTIME | FD_FILESIZE;
pFD->dwFileAttributes =
pFD->ftCreationTime =
pFD->ftLastAccessTime =
pFD->ftLastWriteTime =
#endif
++curIdx;
++itRoot;
}
if (pszFileSpec)
RTStrFree(pszFileSpec);
if (RT_SUCCESS(rc))
rc = copyToHGlobal(pFGD, cbFGD, GMEM_MOVEABLE, phGlobal);
RTMemFree(pFGD);
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* Retrieves the data stored in this object and store the result in
* pMedium.
*
* @return IPRT status code.
* @return HRESULT
* @param pFormatEtc
* @param pMedium
*/
STDMETHODIMP SharedClipboardWinDataObject::GetData(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium)
{
AssertPtrReturn(pFormatEtc, DV_E_FORMATETC);
AssertPtrReturn(pMedium, DV_E_FORMATETC);
LogFlowFuncEnter();
LogFlowFunc(("lIndex=%RI32\n", pFormatEtc->lindex));
/*
* Initialize default values.
*/
RT_BZERO(pMedium, sizeof(STGMEDIUM));
HRESULT hr = DV_E_FORMATETC; /* Play safe. */
if ( pFormatEtc->cfFormat == m_cfFileDescriptorA
#ifdef VBOX_CLIPBOARD_WITH_UNICODE_SUPPORT
|| pFormatEtc->cfFormat == m_cfFileDescriptorW
#endif
)
{
const bool fUnicode = pFormatEtc->cfFormat == m_cfFileDescriptorW;
const uint32_t enmTransferStatus = ShClTransferGetStatus(m_pTransfer);
RT_NOREF(enmTransferStatus);
LogFlowFunc(("FormatIndex_FileDescriptor%s, enmTransferStatus=%s, m_fRunning=%RTbool\n",
fUnicode ? "W" : "A", ShClTransferStatusToStr(enmTransferStatus), m_fRunning));
int rc;
/* The caller can call GetData() several times, so make sure we don't do the same transfer multiple times. */
if (!m_fRunning)
{
/* Start the transfer asynchronously in a separate thread. */
rc = ShClTransferRun(m_pTransfer, &SharedClipboardWinDataObject::readThread, this);
if (RT_SUCCESS(rc))
{
m_fRunning = true;
/* Don't block for too long here, as this also will screw other apps running on the OS. */
LogFunc(("Waiting for listing to arrive ...\n"));
rc = RTSemEventWait(m_EventListComplete, 30 * 1000 /* 30s timeout */);
if (RT_SUCCESS(rc))
{
LogFunc(("Listing complete\n"));
}
}
}
else
rc = VINF_SUCCESS;
if (RT_SUCCESS(rc))
{
HGLOBAL hGlobal;
rc = createFileGroupDescriptorFromTransfer(m_pTransfer, fUnicode, &hGlobal);
if (RT_SUCCESS(rc))
{
pMedium->tymed = TYMED_HGLOBAL;
pMedium->hGlobal = hGlobal;
/* Note: hGlobal now is being owned by pMedium / the caller. */
hr = S_OK;
}
else /* We can't tell any better to the caller, unfortunately. */
hr = E_UNEXPECTED;
}
if (RT_FAILURE(rc))
LogRel(("Shared Clipboard: Data object unable to get data, rc=%Rrc\n", rc));
}
if (pFormatEtc->cfFormat == m_cfFileContents)
{
if ( pFormatEtc->lindex >= 0
&& (ULONG)pFormatEtc->lindex < m_lstEntries.size())
{
m_uObjIdx = pFormatEtc->lindex; /* lIndex of FormatEtc contains the actual index to the object being handled. */
FSOBJENTRY &fsObjEntry = m_lstEntries.at(m_uObjIdx);
LogFlowFunc(("FormatIndex_FileContents: m_uObjIdx=%u (entry '%s')\n", m_uObjIdx, fsObjEntry.strPath.c_str()));
LogRel2(("Shared Clipboard: Receiving object '%s' ...\n", fsObjEntry.strPath.c_str()));
/* Hand-in the provider so that our IStream implementation can continue working with it. */
hr = SharedClipboardWinStreamImpl::Create(this /* pParent */, m_pTransfer,
fsObjEntry.strPath.c_str()/* File name */, &fsObjEntry.objInfo /* PSHCLFSOBJINFO */,
&m_pStream);
if (SUCCEEDED(hr))
{
/* Hand over the stream to the caller. */
pMedium->tymed = TYMED_ISTREAM;
pMedium->pstm = m_pStream;
}
}
}
else if (pFormatEtc->cfFormat == m_cfPerformedDropEffect)
{
HGLOBAL hGlobal = GlobalAlloc(GHND, sizeof(DWORD));
DWORD* pdwDropEffect = (DWORD*)GlobalLock(hGlobal);
*pdwDropEffect = DROPEFFECT_COPY;
GlobalUnlock(hGlobal);
pMedium->tymed = TYMED_HGLOBAL;
pMedium->hGlobal = hGlobal;
pMedium->pUnkForRelease = NULL;
}
if ( FAILED(hr)
&& hr != DV_E_FORMATETC) /* Can happen if the caller queries unknown / unhandled formats. */
{
LogRel(("Shared Clipboard: Error returning data from data object (%Rhrc)\n", hr));
}
LogFlowFunc(("hr=%Rhrc\n", hr));
return hr;
}
/**
* Only required for IStream / IStorage interfaces.
*
* @return IPRT status code.
* @return HRESULT
* @param pFormatEtc
* @param pMedium
*/
STDMETHODIMP SharedClipboardWinDataObject::GetDataHere(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium)
{
RT_NOREF(pFormatEtc, pMedium);
LogFlowFunc(("\n"));
return E_NOTIMPL;
}
/**
* Query if this objects supports a specific format.
*
* @return IPRT status code.
* @return HRESULT
* @param pFormatEtc
*/
STDMETHODIMP SharedClipboardWinDataObject::QueryGetData(LPFORMATETC pFormatEtc)
{
LogFlowFunc(("\n"));
return lookupFormatEtc(pFormatEtc, NULL /* puIndex */) ? S_OK : DV_E_FORMATETC;
}
STDMETHODIMP SharedClipboardWinDataObject::GetCanonicalFormatEtc(LPFORMATETC pFormatEtc, LPFORMATETC pFormatEtcOut)
{
RT_NOREF(pFormatEtc);
LogFlowFunc(("\n"));
/* Set this to NULL in any case. */
pFormatEtcOut->ptd = NULL;
return E_NOTIMPL;
}
STDMETHODIMP SharedClipboardWinDataObject::SetData(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium, BOOL fRelease)
{
if ( pFormatEtc == NULL
|| pMedium == NULL)
return E_INVALIDARG;
if (pFormatEtc->lindex != -1)
return DV_E_LINDEX;
if (pFormatEtc->tymed != TYMED_HGLOBAL)
return DV_E_TYMED;
if (pFormatEtc->dwAspect != DVASPECT_CONTENT)
return DV_E_DVASPECT;
LogFlowFunc(("cfFormat=%RU16, lookupFormatEtc=%RTbool\n",
pFormatEtc->cfFormat, lookupFormatEtc(pFormatEtc, NULL /* puIndex */)));
/* CFSTR_PERFORMEDDROPEFFECT is used by the drop target (caller of this IDataObject) to communicate
* the outcome of the overall operation. */
if ( pFormatEtc->cfFormat == m_cfPerformedDropEffect
&& pMedium->tymed == TYMED_HGLOBAL)
{
DWORD dwEffect = *(DWORD *)GlobalLock(pMedium->hGlobal);
GlobalUnlock(pMedium->hGlobal);
LogFlowFunc(("dwEffect=%RI32\n", dwEffect));
/* Did the user cancel the operation via UI (shell)? This also might happen when overwriting an existing file
* and the user doesn't want to allow this. */
if (dwEffect == DROPEFFECT_NONE)
{
LogRel2(("Shared Clipboard: Transfer canceled by user interaction\n"));
OnTransferCanceled();
}
/** @todo Detect move / overwrite actions here. */
if (fRelease)
ReleaseStgMedium(pMedium);
return S_OK;
}
return E_NOTIMPL;
}
STDMETHODIMP SharedClipboardWinDataObject::EnumFormatEtc(DWORD dwDirection, IEnumFORMATETC **ppEnumFormatEtc)
{
LogFlowFunc(("dwDirection=%RI32, mcFormats=%RI32, mpFormatEtc=%p\n", dwDirection, m_cFormats, m_pFormatEtc));
HRESULT hr;
if (dwDirection == DATADIR_GET)
hr = SharedClipboardWinEnumFormatEtc::CreateEnumFormatEtc(m_cFormats, m_pFormatEtc, ppEnumFormatEtc);
else
hr = E_NOTIMPL;
LogFlowFunc(("hr=%Rhrc\n", hr));
return hr;
}
STDMETHODIMP SharedClipboardWinDataObject::DAdvise(LPFORMATETC pFormatEtc, DWORD fAdvise, IAdviseSink *pAdvSink, DWORD *pdwConnection)
{
RT_NOREF(pFormatEtc, fAdvise, pAdvSink, pdwConnection);
return OLE_E_ADVISENOTSUPPORTED;
}
STDMETHODIMP SharedClipboardWinDataObject::DUnadvise(DWORD dwConnection)
{
RT_NOREF(dwConnection);
return OLE_E_ADVISENOTSUPPORTED;
}
STDMETHODIMP SharedClipboardWinDataObject::EnumDAdvise(IEnumSTATDATA **ppEnumAdvise)
{
RT_NOREF(ppEnumAdvise);
return OLE_E_ADVISENOTSUPPORTED;
}
#ifdef VBOX_WITH_SHARED_CLIPBOARD_WIN_ASYNC
/*
* IDataObjectAsyncCapability methods.
*/
STDMETHODIMP SharedClipboardWinDataObject::EndOperation(HRESULT hResult, IBindCtx *pbcReserved, DWORD dwEffects)
{
RT_NOREF(hResult, pbcReserved, dwEffects);
return E_NOTIMPL;
}
STDMETHODIMP SharedClipboardWinDataObject::GetAsyncMode(BOOL *pfIsOpAsync)
{
RT_NOREF(pfIsOpAsync);
return E_NOTIMPL;
}
STDMETHODIMP SharedClipboardWinDataObject::InOperation(BOOL *pfInAsyncOp)
{
RT_NOREF(pfInAsyncOp);
return E_NOTIMPL;
}
STDMETHODIMP SharedClipboardWinDataObject::SetAsyncMode(BOOL fDoOpAsync)
{
RT_NOREF(fDoOpAsync);
return E_NOTIMPL;
}
STDMETHODIMP SharedClipboardWinDataObject::StartOperation(IBindCtx *pbcReserved)
{
RT_NOREF(pbcReserved);
return E_NOTIMPL;
}
#endif /* VBOX_WITH_SHARED_CLIPBOARD_WIN_ASYNC */
/*
* Own stuff.
*/
int SharedClipboardWinDataObject::Init(void)
{
LogFlowFuncLeaveRC(VINF_SUCCESS);
return VINF_SUCCESS;
}
void SharedClipboardWinDataObject::OnTransferComplete(int rc /* = VINF_SUCESS */)
{
RT_NOREF(rc);
LogFlowFunc(("m_uObjIdx=%RU32 (total: %zu)\n", m_uObjIdx, m_lstEntries.size()));
if (RT_SUCCESS(rc))
{
const bool fComplete = m_uObjIdx == m_lstEntries.size() - 1 /* Object index is zero-based */;
if (fComplete)
{
m_enmStatus = Completed;
}
}
else
m_enmStatus = Error;
if (m_enmStatus != Initialized)
{
if (m_EventTransferComplete != NIL_RTSEMEVENT)
{
int rc2 = RTSemEventSignal(m_EventTransferComplete);
AssertRC(rc2);
}
}
LogFlowFuncLeaveRC(rc);
}
void SharedClipboardWinDataObject::OnTransferCanceled(void)
{
LogFlowFuncEnter();
m_enmStatus = Canceled;
if (m_EventTransferComplete != NIL_RTSEMEVENT)
{
int rc2 = RTSemEventSignal(m_EventTransferComplete);
AssertRC(rc2);
}
LogFlowFuncLeave();
}
/* static */
void SharedClipboardWinDataObject::logFormat(CLIPFORMAT fmt)
{
char szFormat[128];
if (GetClipboardFormatName(fmt, szFormat, sizeof(szFormat)))
{
LogFlowFunc(("clipFormat=%RI16 -> %s\n", fmt, szFormat));
}
else
LogFlowFunc(("clipFormat=%RI16 is unknown\n", fmt));
}
bool SharedClipboardWinDataObject::lookupFormatEtc(LPFORMATETC pFormatEtc, ULONG *puIndex)
{
AssertReturn(pFormatEtc, false);
/* puIndex is optional. */
for (ULONG i = 0; i < m_cFormats; i++)
{
if( (pFormatEtc->tymed & m_pFormatEtc[i].tymed)
&& pFormatEtc->cfFormat == m_pFormatEtc[i].cfFormat)
/* Note: Do *not* compare dwAspect here, as this can be dynamic, depending on how the object should be represented. */
//&& pFormatEtc->dwAspect == m_pFormatEtc[i].dwAspect)
{
LogRel2(("Shared Clipboard: Format found: tyMed=%RI32, cfFormat=%RI16, dwAspect=%RI32, ulIndex=%RU32\n",
pFormatEtc->tymed, pFormatEtc->cfFormat, pFormatEtc->dwAspect, i));
if (puIndex)
*puIndex = i;
return true;
}
}
LogRel2(("Shared Clipboard: Format NOT found: tyMed=%RI32, cfFormat=%RI16, dwAspect=%RI32\n",
pFormatEtc->tymed, pFormatEtc->cfFormat, pFormatEtc->dwAspect));
logFormat(pFormatEtc->cfFormat);
return false;
}
void SharedClipboardWinDataObject::registerFormat(LPFORMATETC pFormatEtc, CLIPFORMAT clipFormat,
TYMED tyMed, LONG lIndex, DWORD dwAspect,
DVTARGETDEVICE *pTargetDevice)
{
AssertPtr(pFormatEtc);
pFormatEtc->cfFormat = clipFormat;
pFormatEtc->tymed = tyMed;
pFormatEtc->lindex = lIndex;
pFormatEtc->dwAspect = dwAspect;
pFormatEtc->ptd = pTargetDevice;
LogFlowFunc(("Registered format=%ld\n", pFormatEtc->cfFormat));
logFormat(pFormatEtc->cfFormat);
}
|