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
|
/* $Id: VFSExplorerImpl.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
/** @file
*
* IVFSExplorer COM class implementations.
*/
/*
* Copyright (C) 2009 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include <iprt/dir.h>
#include <iprt/path.h>
#include <iprt/file.h>
#include <iprt/s3.h>
#include <VBox/com/array.h>
#include <VBox/param.h>
#include <VBox/version.h>
#include "VFSExplorerImpl.h"
#include "VirtualBoxImpl.h"
#include "ProgressImpl.h"
#include "AutoCaller.h"
#include "Logging.h"
////////////////////////////////////////////////////////////////////////////////
//
// VFSExplorer definitions
//
////////////////////////////////////////////////////////////////////////////////
/* opaque private instance data of VFSExplorer class */
struct VFSExplorer::Data
{
struct DirEntry
{
DirEntry(Utf8Str aName, VFSFileType_T aType)
: name(aName)
, type(aType) {}
Utf8Str name;
VFSFileType_T type;
};
VFSType_T storageType;
Utf8Str strUsername;
Utf8Str strPassword;
Utf8Str strHostname;
Utf8Str strPath;
Utf8Str strBucket;
std::list<DirEntry> entryList;
};
VFSExplorer::VFSExplorer()
: mVirtualBox(NULL)
{
}
VFSExplorer::~VFSExplorer()
{
}
/**
* VFSExplorer COM initializer.
* @param
* @return
*/
HRESULT VFSExplorer::init(VFSType_T aType, Utf8Str aFilePath, Utf8Str aHostname, Utf8Str aUsername, Utf8Str aPassword, VirtualBox *aVirtualBox)
{
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan(this);
AssertReturn(autoInitSpan.isOk(), E_FAIL);
/* Weak reference to a VirtualBox object */
unconst(mVirtualBox) = aVirtualBox;
/* initialize data */
m = new Data;
m->storageType = aType;
m->strPath = aFilePath;
m->strHostname = aHostname;
m->strUsername = aUsername;
m->strPassword = aPassword;
if (m->storageType == VFSType_S3)
{
size_t bpos = aFilePath.find("/", 1);
if (bpos != Utf8Str::npos)
{
m->strBucket = aFilePath.substr(1, bpos - 1); /* The bucket without any slashes */
aFilePath = aFilePath.substr(bpos); /* The rest of the file path */
}
}
/* Confirm a successful initialization */
autoInitSpan.setSucceeded();
return S_OK;
}
/**
* VFSExplorer COM uninitializer.
* @return
*/
void VFSExplorer::uninit()
{
delete m;
m = NULL;
}
/**
* Public method implementation.
* @param
* @return
*/
STDMETHODIMP VFSExplorer::COMGETTER(Path)(BSTR *aPath)
{
if (!aPath)
return E_POINTER;
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
Bstr bstrPath(m->strPath);
bstrPath.cloneTo(aPath);
return S_OK;
}
STDMETHODIMP VFSExplorer::COMGETTER(Type)(VFSType_T *aType)
{
if (!aType)
return E_POINTER;
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aType = m->storageType;
return S_OK;
}
struct VFSExplorer::TaskVFSExplorer
{
enum TaskType
{
Update,
Delete
};
TaskVFSExplorer(TaskType aTaskType, VFSExplorer *aThat, Progress *aProgress)
: taskType(aTaskType),
pVFSExplorer(aThat),
progress(aProgress),
rc(S_OK)
{}
~TaskVFSExplorer() {}
int startThread();
static int taskThread(RTTHREAD aThread, void *pvUser);
static int uploadProgress(unsigned uPercent, void *pvUser);
TaskType taskType;
VFSExplorer *pVFSExplorer;
ComObjPtr<Progress> progress;
HRESULT rc;
/* task data */
std::list<Utf8Str> filenames;
};
int VFSExplorer::TaskVFSExplorer::startThread()
{
int vrc = RTThreadCreate(NULL, VFSExplorer::TaskVFSExplorer::taskThread, this,
0, RTTHREADTYPE_MAIN_HEAVY_WORKER, 0,
"Explorer::Task");
ComAssertMsgRCRet(vrc,
("Could not create taskThreadVFS (%Rrc)\n", vrc), E_FAIL);
return vrc;
}
/* static */
DECLCALLBACK(int) VFSExplorer::TaskVFSExplorer::taskThread(RTTHREAD /* aThread */, void *pvUser)
{
std::auto_ptr<TaskVFSExplorer> task(static_cast<TaskVFSExplorer*>(pvUser));
AssertReturn(task.get(), VERR_GENERAL_FAILURE);
VFSExplorer *pVFSExplorer = task->pVFSExplorer;
LogFlowFuncEnter();
LogFlowFunc(("VFSExplorer %p\n", pVFSExplorer));
HRESULT rc = S_OK;
switch(task->taskType)
{
case TaskVFSExplorer::Update:
{
if (pVFSExplorer->m->storageType == VFSType_File)
rc = pVFSExplorer->updateFS(task.get());
else if (pVFSExplorer->m->storageType == VFSType_S3)
rc = pVFSExplorer->updateS3(task.get());
break;
}
case TaskVFSExplorer::Delete:
{
if (pVFSExplorer->m->storageType == VFSType_File)
rc = pVFSExplorer->deleteFS(task.get());
else if (pVFSExplorer->m->storageType == VFSType_S3)
rc = pVFSExplorer->deleteS3(task.get());
break;
}
}
LogFlowFunc(("rc=%Rhrc\n", rc));
LogFlowFuncLeave();
return VINF_SUCCESS;
}
/* static */
int VFSExplorer::TaskVFSExplorer::uploadProgress(unsigned uPercent, void *pvUser)
{
VFSExplorer::TaskVFSExplorer* pTask = *(VFSExplorer::TaskVFSExplorer**)pvUser;
if (pTask &&
!pTask->progress.isNull())
{
BOOL fCanceled;
pTask->progress->COMGETTER(Canceled)(&fCanceled);
if (fCanceled)
return -1;
pTask->progress->SetCurrentOperationProgress(uPercent);
}
return VINF_SUCCESS;
}
VFSFileType_T VFSExplorer::RTToVFSFileType(int aType) const
{
VFSFileType_T t;
switch(aType)
{
default:
case RTDIRENTRYTYPE_UNKNOWN: t = VFSFileType_Unknown; break;
case RTDIRENTRYTYPE_FIFO: t = VFSFileType_Fifo; break;
case RTDIRENTRYTYPE_DEV_CHAR: t = VFSFileType_DevChar; break;
case RTDIRENTRYTYPE_DIRECTORY: t = VFSFileType_Directory; break;
case RTDIRENTRYTYPE_DEV_BLOCK: t = VFSFileType_DevBlock; break;
case RTDIRENTRYTYPE_FILE: t = VFSFileType_File; break;
case RTDIRENTRYTYPE_SYMLINK: t = VFSFileType_SymLink; break;
case RTDIRENTRYTYPE_SOCKET: t = VFSFileType_Socket; break;
case RTDIRENTRYTYPE_WHITEOUT: t = VFSFileType_WhiteOut; break;
}
return t;
}
HRESULT VFSExplorer::updateFS(TaskVFSExplorer *aTask)
{
LogFlowFuncEnter();
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS);
HRESULT rc = S_OK;
std::list<VFSExplorer::Data::DirEntry> fileList;
char *pszPath = NULL;
PRTDIR pDir = NULL;
try
{
pszPath = RTStrDup(m->strPath.c_str());
RTPathStripFilename(pszPath);
int vrc = RTDirOpen(&pDir, pszPath);
if (RT_FAILURE(vrc))
throw setError(VBOX_E_FILE_ERROR, tr ("Can't open directory '%s' (%Rrc)"), pszPath, vrc);
if (aTask->progress)
aTask->progress->SetCurrentOperationProgress(33);
RTDIRENTRY entry;
while (RT_SUCCESS(vrc))
{
vrc = RTDirRead(pDir, &entry, NULL);
if (RT_SUCCESS(vrc))
{
Utf8Str name(entry.szName);
if (name != "." &&
name != "..")
fileList.push_back(VFSExplorer::Data::DirEntry(name, RTToVFSFileType(entry.enmType)));
}
}
if (aTask->progress)
aTask->progress->SetCurrentOperationProgress(66);
}
catch(HRESULT aRC)
{
rc = aRC;
}
/* Clean up */
if (pszPath)
RTStrFree(pszPath);
if (pDir)
RTDirClose(pDir);
if (aTask->progress)
aTask->progress->SetCurrentOperationProgress(99);
/* Assign the result on success (this clears the old list) */
if (rc == S_OK)
m->entryList.assign(fileList.begin(), fileList.end());
aTask->rc = rc;
if (!aTask->progress.isNull())
aTask->progress->notifyComplete(rc);
LogFlowFunc(("rc=%Rhrc\n", rc));
LogFlowFuncLeave();
return VINF_SUCCESS;
}
HRESULT VFSExplorer::deleteFS(TaskVFSExplorer *aTask)
{
LogFlowFuncEnter();
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS);
HRESULT rc = S_OK;
float fPercentStep = 100.0f / aTask->filenames.size();
try
{
char szPath[RTPATH_MAX];
std::list<Utf8Str>::const_iterator it;
size_t i = 0;
for (it = aTask->filenames.begin();
it != aTask->filenames.end();
++it, ++i)
{
memcpy(szPath, m->strPath.c_str(), strlen(m->strPath.c_str()) + 1);
RTPathStripFilename(szPath);
RTPathAppend(szPath, sizeof(szPath), (*it).c_str());
int vrc = RTFileDelete(szPath);
if (RT_FAILURE(vrc))
throw setError(VBOX_E_FILE_ERROR, tr ("Can't delete file '%s' (%Rrc)"), szPath, vrc);
if (aTask->progress)
aTask->progress->SetCurrentOperationProgress((ULONG)(fPercentStep * i));
}
}
catch(HRESULT aRC)
{
rc = aRC;
}
aTask->rc = rc;
if (!aTask->progress.isNull())
aTask->progress->notifyComplete(rc);
LogFlowFunc(("rc=%Rhrc\n", rc));
LogFlowFuncLeave();
return VINF_SUCCESS;
}
HRESULT VFSExplorer::updateS3(TaskVFSExplorer *aTask)
{
LogFlowFuncEnter();
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS);
HRESULT rc = S_OK;
RTS3 hS3 = NULL;
std::list<VFSExplorer::Data::DirEntry> fileList;
try
{
int vrc = RTS3Create(&hS3, m->strUsername.c_str(), m->strPassword.c_str(), m->strHostname.c_str(), "virtualbox-agent/"VBOX_VERSION_STRING);
if (RT_FAILURE(vrc))
throw setError(E_FAIL, tr ("Can't open S3 storage service (%Rrc)"), vrc);
RTS3SetProgressCallback(hS3, VFSExplorer::TaskVFSExplorer::uploadProgress, &aTask);
/* Do we need the list of buckets or keys? */
if (m->strBucket.isEmpty())
{
PCRTS3BUCKETENTRY pBuckets = NULL;
vrc = RTS3GetBuckets(hS3, &pBuckets);
if (RT_FAILURE(vrc))
throw setError(E_FAIL, tr ("Can't get buckets (%Rrc)"), vrc);
PCRTS3BUCKETENTRY pTmpBuckets = pBuckets;
while (pBuckets)
{
fileList.push_back(VFSExplorer::Data::DirEntry(pBuckets->pszName, VFSFileType_Directory));
pBuckets = pBuckets->pNext;
}
RTS3BucketsDestroy(pTmpBuckets);
}
else
{
PCRTS3KEYENTRY pKeys = NULL;
vrc = RTS3GetBucketKeys(hS3, m->strBucket.c_str(), &pKeys);
if (RT_FAILURE(vrc))
throw setError(E_FAIL, tr ("Can't get keys for bucket (%Rrc)"), vrc);
PCRTS3KEYENTRY pTmpKeys = pKeys;
while (pKeys)
{
Utf8Str name(pKeys->pszName);
fileList.push_back(VFSExplorer::Data::DirEntry(pKeys->pszName, VFSFileType_File));
pKeys = pKeys->pNext;
}
RTS3KeysDestroy(pTmpKeys);
}
}
catch(HRESULT aRC)
{
rc = aRC;
}
if (hS3 != NULL)
RTS3Destroy(hS3);
/* Assign the result on success (this clears the old list) */
if (rc == S_OK)
m->entryList.assign(fileList.begin(), fileList.end());
aTask->rc = rc;
if (!aTask->progress.isNull())
aTask->progress->notifyComplete(rc);
LogFlowFunc(("rc=%Rhrc\n", rc));
LogFlowFuncLeave();
return VINF_SUCCESS;
}
HRESULT VFSExplorer::deleteS3(TaskVFSExplorer *aTask)
{
LogFlowFuncEnter();
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS);
HRESULT rc = S_OK;
RTS3 hS3 = NULL;
float fPercentStep = 100.0f / aTask->filenames.size();
try
{
int vrc = RTS3Create(&hS3, m->strUsername.c_str(), m->strPassword.c_str(), m->strHostname.c_str(), "virtualbox-agent/"VBOX_VERSION_STRING);
if (RT_FAILURE(vrc))
throw setError(E_FAIL, tr ("Can't open S3 storage service (%Rrc)"), vrc);
RTS3SetProgressCallback(hS3, VFSExplorer::TaskVFSExplorer::uploadProgress, &aTask);
std::list<Utf8Str>::const_iterator it;
size_t i = 0;
for (it = aTask->filenames.begin();
it != aTask->filenames.end();
++it, ++i)
{
vrc = RTS3DeleteKey(hS3, m->strBucket.c_str(), (*it).c_str());
if (RT_FAILURE(vrc))
throw setError(VBOX_E_FILE_ERROR, tr ("Can't delete file '%s' (%Rrc)"), (*it).c_str(), vrc);
if (aTask->progress)
aTask->progress->SetCurrentOperationProgress((ULONG)(fPercentStep * i));
}
}
catch(HRESULT aRC)
{
rc = aRC;
}
aTask->rc = rc;
if (hS3 != NULL)
RTS3Destroy(hS3);
if (!aTask->progress.isNull())
aTask->progress->notifyComplete(rc);
LogFlowFunc(("rc=%Rhrc\n", rc));
LogFlowFuncLeave();
return VINF_SUCCESS;
}
STDMETHODIMP VFSExplorer::Update(IProgress **aProgress)
{
CheckComArgOutPointerValid(aProgress);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
HRESULT rc = S_OK;
ComObjPtr<Progress> progress;
try
{
Bstr progressDesc = BstrFmt(tr("Update directory info for '%s'"),
m->strPath.raw());
/* Create the progress object */
progress.createObject();
rc = progress->init(mVirtualBox, static_cast<IVFSExplorer*>(this),
progressDesc,
TRUE /* aCancelable */);
if (FAILED(rc)) throw rc;
/* Initialize our worker task */
std::auto_ptr<TaskVFSExplorer> task(new TaskVFSExplorer(TaskVFSExplorer::Update, this, progress));
rc = task->startThread();
if (FAILED(rc)) throw rc;
/* Don't destruct on success */
task.release();
}
catch (HRESULT aRC)
{
rc = aRC;
}
if (SUCCEEDED(rc))
/* Return progress to the caller */
progress.queryInterfaceTo(aProgress);
return rc;
}
STDMETHODIMP VFSExplorer::Cd(IN_BSTR aDir, IProgress **aProgress)
{
CheckComArgStrNotEmptyOrNull(aDir);
CheckComArgOutPointerValid(aProgress);
return E_NOTIMPL;
}
STDMETHODIMP VFSExplorer::CdUp(IProgress **aProgress)
{
CheckComArgOutPointerValid(aProgress);
return E_NOTIMPL;
}
STDMETHODIMP VFSExplorer::EntryList(ComSafeArrayOut(BSTR, aNames), ComSafeArrayOut(VFSFileType_T, aTypes))
{
if (ComSafeArrayOutIsNull(aNames) ||
ComSafeArrayOutIsNull(aTypes))
return E_POINTER;
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
com::SafeArray<BSTR> sfaNames((ULONG)m->entryList.size());
com::SafeArray<ULONG> sfaTypes((VFSFileType_T)m->entryList.size());
std::list<VFSExplorer::Data::DirEntry>::const_iterator it;
size_t i = 0;
for (it = m->entryList.begin();
it != m->entryList.end();
++it, ++i)
{
const VFSExplorer::Data::DirEntry &entry = (*it);
Bstr bstr(entry.name);
bstr.cloneTo(&sfaNames[i]);
sfaTypes[i] = entry.type;
}
sfaNames.detachTo(ComSafeArrayOutArg(aNames));
sfaTypes.detachTo(ComSafeArrayOutArg(aTypes));
return S_OK;
}
STDMETHODIMP VFSExplorer::Exists(ComSafeArrayIn(IN_BSTR, aNames), ComSafeArrayOut(BSTR, aExists))
{
CheckComArgSafeArrayNotNull(aNames);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
com::SafeArray<IN_BSTR> sfaNames(ComSafeArrayInArg(aNames));
std::list<BSTR> listExists;
std::list<VFSExplorer::Data::DirEntry>::const_iterator it;
for (it = m->entryList.begin();
it != m->entryList.end();
++it)
{
const VFSExplorer::Data::DirEntry &entry = (*it);
for (size_t a=0; a < sfaNames.size(); ++a)
{
if (entry.name == RTPathFilename(Utf8Str(sfaNames[a]).c_str()))
{
BSTR name;
Bstr tmp(sfaNames[a]); /* gcc-3.3 cruft */
tmp.cloneTo(&name);
listExists.push_back(name);
}
}
}
com::SafeArray<BSTR> sfaExists(listExists);
sfaExists.detachTo(ComSafeArrayOutArg(aExists));
return S_OK;
}
STDMETHODIMP VFSExplorer::Remove(ComSafeArrayIn(IN_BSTR, aNames), IProgress **aProgress)
{
CheckComArgSafeArrayNotNull(aNames);
CheckComArgOutPointerValid(aProgress);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
HRESULT rc = S_OK;
com::SafeArray<IN_BSTR> sfaNames(ComSafeArrayInArg(aNames));
ComObjPtr<Progress> progress;
try
{
/* Create the progress object */
progress.createObject();
rc = progress->init(mVirtualBox, static_cast<IVFSExplorer*>(this),
Bstr(tr("Delete files")),
TRUE /* aCancelable */);
if (FAILED(rc)) throw rc;
/* Initialize our worker task */
std::auto_ptr<TaskVFSExplorer> task(new TaskVFSExplorer(TaskVFSExplorer::Delete, this, progress));
/* Add all filenames to delete as task data */
for (size_t a=0; a < sfaNames.size(); ++a)
task->filenames.push_back(Utf8Str(sfaNames[a]));
rc = task->startThread();
if (FAILED(rc)) throw rc;
/* Don't destruct on success */
task.release();
}
catch (HRESULT aRC)
{
rc = aRC;
}
if (SUCCEEDED(rc))
/* Return progress to the caller */
progress.queryInterfaceTo(aProgress);
return rc;
}
|