VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/MachineImplMoveVM.cpp@ 71566

Last change on this file since 71566 was 71376, checked in by vboxsync, 7 years ago

Main/Machine: bugref:8345 Release Machine lock early enough, since setting the medium location needs higher priority one.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 56.8 KB
Line 
1/* $Id: MachineImplMoveVM.cpp 71376 2018-03-19 16:43:09Z vboxsync $ */
2/** @file
3 * Implementation of MachineMoveVM
4 */
5
6/*
7 * Copyright (C) 2011-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17#include <iprt/fs.h>
18#include <iprt/dir.h>
19#include <iprt/file.h>
20#include <iprt/path.h>
21#include <iprt/cpp/utils.h>
22#include <iprt/stream.h>
23
24#include "MachineImplMoveVM.h"
25#include "MediumFormatImpl.h"
26#include "VirtualBoxImpl.h"
27#include "Logging.h"
28
29/* This variable is global and used in the different places so it must be cleared each time before usage to avoid failure */
30std::vector< ComObjPtr<Machine> > machineList;
31
32typedef std::multimap<Utf8Str, Utf8Str> list_t;
33typedef std::multimap<Utf8Str, Utf8Str>::const_iterator cit_t;
34typedef std::multimap<Utf8Str, Utf8Str>::iterator it_t;
35typedef std::pair <std::multimap<Utf8Str, Utf8Str>::iterator, std::multimap<Utf8Str, Utf8Str>::iterator> rangeRes_t;
36
37struct fileList_t
38{
39 HRESULT add(const Utf8Str& folder, const Utf8Str& file)
40 {
41 HRESULT rc = S_OK;
42 m_list.insert(std::make_pair(folder, file));
43 return rc;
44 }
45
46 HRESULT add(const Utf8Str& fullPath)
47 {
48 HRESULT rc = S_OK;
49 Utf8Str folder = fullPath;
50 folder.stripFilename();
51 Utf8Str filename = fullPath;
52 filename.stripPath();
53 m_list.insert(std::make_pair(folder, filename));
54 return rc;
55 }
56
57 HRESULT removeFileFromList(const Utf8Str& fullPath)
58 {
59 HRESULT rc = S_OK;
60 Utf8Str folder = fullPath;
61 folder.stripFilename();
62 Utf8Str filename = fullPath;
63 filename.stripPath();
64 rangeRes_t res = m_list.equal_range(folder);
65 for (it_t it=res.first; it!=res.second; ++it)
66 {
67 if (it->second.equals(filename))
68 m_list.erase(it);
69 }
70 return rc;
71 }
72
73 HRESULT removeFileFromList(const Utf8Str& path, const Utf8Str& fileName)
74 {
75 HRESULT rc = S_OK;
76 rangeRes_t res = m_list.equal_range(path);
77 for (it_t it=res.first; it!=res.second; ++it)
78 {
79 if (it->second.equals(fileName))
80 m_list.erase(it);
81 }
82 return rc;
83 }
84
85 HRESULT removeFolderFromList(const Utf8Str& path)
86 {
87 HRESULT rc = S_OK;
88 m_list.erase(path);
89 return rc;
90 }
91
92 rangeRes_t getFilesInRange(const Utf8Str& path)
93 {
94 rangeRes_t res;
95 res = m_list.equal_range(path);
96 return res;
97 }
98
99 std::list<Utf8Str> getFilesInList(const Utf8Str& path)
100 {
101 std::list<Utf8Str> list_;
102 rangeRes_t res = m_list.equal_range(path);
103 for (it_t it=res.first; it!=res.second; ++it)
104 list_.push_back(it->second);
105 return list_;
106 }
107
108
109 list_t m_list;
110
111};
112
113HRESULT MachineMoveVM::init()
114{
115 HRESULT rc = S_OK;
116
117 Utf8Str strTargetFolder;
118 /* adding a trailing slash if it's needed */
119 {
120 size_t len = m_targetPath.length() + 2;
121 if (len >=RTPATH_MAX)
122 {
123 throw m_pMachine->setError(VBOX_E_IPRT_ERROR,
124 m_pMachine->tr(" The destination path isn't correct. "
125 "The length of path exceeded the maximum value."));
126 }
127
128 char* path = new char [len];
129 RTStrCopy(path, len, m_targetPath.c_str());
130 RTPathEnsureTrailingSeparator(path, len);
131 strTargetFolder = m_targetPath = path;
132 delete path;
133 }
134
135 /*
136 * We have a mode which user is able to request
137 * basic mode:
138 * - The images which are solely attached to the VM
139 * and located in the original VM folder will be moved.
140 *
141 * Comment: in the future some other modes can be added.
142 */
143
144 try
145 {
146 Utf8Str info;
147 int vrc = 0;
148
149 RTFOFF cbTotal = 0;
150 RTFOFF cbFree = 0;
151 uint32_t cbBlock = 0;
152 uint32_t cbSector = 0;
153
154 vrc = RTFsQuerySizes(strTargetFolder.c_str(), &cbTotal, &cbFree, &cbBlock, &cbSector);
155 if (FAILED(vrc))
156 {
157 RTPrintf("strTargetFolder is %s\n", strTargetFolder.c_str());
158 rc = m_pMachine->setError(E_FAIL,
159 m_pMachine->tr("Unable to move machine. Can't get the destination storage size (%s)"),
160 strTargetFolder.c_str());
161 throw rc;
162 }
163
164 long long totalFreeSpace = cbFree;
165 long long totalSpace = cbTotal;
166 info = Utf8StrFmt("blocks: total %lld, free %u ", cbTotal, cbFree);
167 LogRelFunc(("%s \n", info.c_str()));
168 LogRelFunc(("total space (Kb) %lld (Mb) %lld (Gb) %lld\n",
169 totalSpace/1024, totalSpace/(1024*1024), totalSpace/(1024*1024*1024)));
170 LogRelFunc(("total free space (Kb) %lld (Mb) %lld (Gb) %lld\n",
171 totalFreeSpace/1024, totalFreeSpace/(1024*1024), totalFreeSpace/(1024*1024*1024)));
172
173 RTFSPROPERTIES properties;
174 vrc = RTFsQueryProperties(strTargetFolder.c_str(), &properties);
175 if (FAILED(vrc)) throw vrc;
176 info = Utf8StrFmt("disk properties:\n"
177 "remote: %s \n"
178 "read only: %s \n"
179 "compressed: %s \n",
180 properties.fRemote == true ? "true":"false",
181 properties.fReadOnly == true ? "true":"false",
182 properties.fCompressed == true ? "true":"false");
183
184 LogRelFunc(("%s \n", info.c_str()));
185
186 /* Get the original VM path */
187 Utf8Str strSettingsFilePath;
188 Bstr bstr_settingsFilePath;
189 m_pMachine->COMGETTER(SettingsFilePath)(bstr_settingsFilePath.asOutParam());
190 strSettingsFilePath = bstr_settingsFilePath;
191 strSettingsFilePath.stripFilename();
192
193 vmFolders.insert(std::make_pair(VBox_SettingFolder, strSettingsFilePath));
194
195 /* Collect all files from the VM's folder */
196 fileList_t fullFileList;
197 rc = getFilesList(strSettingsFilePath, fullFileList);
198 if (FAILED(rc)) throw rc;
199
200 /*
201 * Collect all known folders used by the VM:
202 * - log folder;
203 * - state folder;
204 * - snapshot folder.
205 */
206 Utf8Str strLogFolder;
207 Bstr bstr_logFolder;
208 m_pMachine->COMGETTER(LogFolder)(bstr_logFolder.asOutParam());
209 strLogFolder = bstr_logFolder;
210 if ( m_type.equals("basic")
211 && strLogFolder.contains(strSettingsFilePath))
212 {
213 vmFolders.insert(std::make_pair(VBox_LogFolder, strLogFolder));
214 }
215
216 Utf8Str strStateFilePath;
217 Bstr bstr_stateFilePath;
218 MachineState_T machineState;
219 rc = m_pMachine->COMGETTER(State)(&machineState);
220 if (FAILED(rc)) throw rc;
221 else if (machineState == MachineState_Saved)
222 {
223 m_pMachine->COMGETTER(StateFilePath)(bstr_stateFilePath.asOutParam());
224 strStateFilePath = bstr_stateFilePath;
225 strStateFilePath.stripFilename();
226 if ( m_type.equals("basic")
227 && strStateFilePath.contains(strSettingsFilePath))
228 vmFolders.insert(std::make_pair(VBox_StateFolder, strStateFilePath));//Utf8Str(bstr_stateFilePath)));
229 }
230
231 Utf8Str strSnapshotFolder;
232 Bstr bstr_snapshotFolder;
233 m_pMachine->COMGETTER(SnapshotFolder)(bstr_snapshotFolder.asOutParam());
234 strSnapshotFolder = bstr_snapshotFolder;
235 if ( m_type.equals("basic")
236 && strSnapshotFolder.contains(strSettingsFilePath))
237 vmFolders.insert(std::make_pair(VBox_SnapshotFolder, strSnapshotFolder));
238
239 if (m_pMachine->i_isSnapshotMachine())
240 {
241 Bstr bstrSrcMachineId;
242 rc = m_pMachine->COMGETTER(Id)(bstrSrcMachineId.asOutParam());
243 if (FAILED(rc)) throw rc;
244 ComPtr<IMachine> newSrcMachine;
245 rc = m_pMachine->i_getVirtualBox()->FindMachine(bstrSrcMachineId.raw(), newSrcMachine.asOutParam());
246 if (FAILED(rc)) throw rc;
247 }
248
249 /* Add the current machine and all snapshot machines below this machine
250 * in a list for further processing.
251 */
252
253 long long neededFreeSpace = 0;
254
255 /* Actual file list */
256 fileList_t actualFileList;
257 Utf8Str strTargetImageName;
258
259 /* Global variable (defined at the beginning of file), so clear it before usage */
260 machineList.clear();
261 machineList.push_back(m_pMachine);
262
263 {
264 ULONG cSnapshots = 0;
265 rc = m_pMachine->COMGETTER(SnapshotCount)(&cSnapshots);
266 if (FAILED(rc)) throw rc;
267 if (cSnapshots > 0)
268 {
269 Utf8Str id;
270 if (m_pMachine->i_isSnapshotMachine())
271 id = m_pMachine->i_getSnapshotId().toString();
272 ComPtr<ISnapshot> pSnapshot;
273 rc = m_pMachine->FindSnapshot(Bstr(id).raw(), pSnapshot.asOutParam());
274 if (FAILED(rc)) throw rc;
275 rc = createMachineList(pSnapshot, machineList);
276 if (FAILED(rc)) throw rc;
277 }
278 }
279
280 ULONG uCount = 1;//looks like it should be initialized by 1. See assertion in the Progress::setNextOperation()
281 ULONG uTotalWeight = 1;
282
283 /* The lists llMedias and llSaveStateFiles are filled in the queryMediasForAllStates() */
284 queryMediasForAllStates(machineList);
285
286 {
287 uint64_t totalMediumsSize = 0;
288
289 for (size_t i = 0; i < llMedias.size(); ++i)
290 {
291 LONG64 cbSize = 0;
292 MEDIUMTASKCHAIN &mtc = llMedias.at(i);
293 for (size_t a = mtc.chain.size(); a > 0; --a)
294 {
295 Bstr bstrLocation;
296 Utf8Str strLocation;
297 Utf8Str name = mtc.chain[a - 1].strBaseName;
298 ComPtr<IMedium> plMedium = mtc.chain[a - 1].pMedium;
299 rc = plMedium->COMGETTER(Location)(bstrLocation.asOutParam());
300 if (FAILED(rc)) throw rc;
301 strLocation = bstrLocation;
302
303 /*if an image is located in the actual VM folder it will be added to the actual list */
304 if (strLocation.contains(strSettingsFilePath))
305 {
306 rc = plMedium->COMGETTER(Size)(&cbSize);
307 if (FAILED(rc)) throw rc;
308
309 std::pair<std::map<Utf8Str, MEDIUMTASK>::iterator,bool> ret;
310 ret = finalMediumsMap.insert(std::make_pair(name, mtc.chain[a - 1]));
311 if (ret.second == true)
312 {
313 /* Calculate progress data */
314 ++uCount;
315 uTotalWeight += mtc.chain[a - 1].uWeight;
316 totalMediumsSize += cbSize;
317 LogRelFunc(("Image %s was added into the moved list\n", name.c_str()));
318 }
319 }
320 }
321 }
322
323 LogRelFunc(("Total Size of images is %lld bytes\n", totalMediumsSize));
324 neededFreeSpace += totalMediumsSize;
325 }
326
327 /* Prepare data for moving ".sav" files */
328 {
329 uint64_t totalStateSize = 0;
330
331 for (size_t i = 0; i < llSaveStateFiles.size(); ++i)
332 {
333 uint64_t cbFile = 0;
334 SAVESTATETASK &sst = llSaveStateFiles.at(i);
335
336 Utf8Str name = sst.strSaveStateFile;
337 /*if a state file is located in the actual VM folder it will be added to the actual list */
338 if (name.contains(strSettingsFilePath))
339 {
340 vrc = RTFileQuerySize(name.c_str(), &cbFile);
341 if (RT_SUCCESS(vrc))
342 {
343 std::pair<std::map<Utf8Str, SAVESTATETASK>::iterator,bool> ret;
344 ret = finalSaveStateFilesMap.insert(std::make_pair(name, sst));
345 if (ret.second == true)
346 {
347 totalStateSize += cbFile;
348 ++uCount;
349 uTotalWeight += sst.uWeight;
350 LogRelFunc(("The state file %s was added into the moved list\n", name.c_str()));
351 }
352 }
353 else
354 LogRelFunc(("The state file %s wasn't added into the moved list. Couldn't get the file size.\n",
355 name.c_str()));
356 }
357 }
358
359 neededFreeSpace += totalStateSize;
360 }
361
362 /* Prepare data for moving the log files */
363 {
364 Utf8Str strFolder = vmFolders[VBox_LogFolder];
365
366 if (RTPathExists(strFolder.c_str()))
367 {
368 uint64_t totalLogSize = 0;
369 rc = getFolderSize(strFolder, totalLogSize);
370 if (SUCCEEDED(rc))
371 {
372 neededFreeSpace += totalLogSize;
373 if (totalFreeSpace - neededFreeSpace <= 1024*1024)
374 {
375 throw VERR_OUT_OF_RESOURCES;//less than 1Mb free space on the target location
376 }
377
378 fileList_t filesList;
379 getFilesList(strFolder, filesList);
380 cit_t it = filesList.m_list.begin();
381 while(it != filesList.m_list.end())
382 {
383 Utf8Str strFile = it->first.c_str();
384 strFile.append(RTPATH_DELIMITER).append(it->second.c_str());
385
386 uint64_t cbFile = 0;
387 vrc = RTFileQuerySize(strFile.c_str(), &cbFile);
388 if (RT_SUCCESS(vrc))
389 {
390 uCount += 1;
391 uTotalWeight += (ULONG)((cbFile + _1M - 1) / _1M);
392 actualFileList.add(strFile);
393 LogRelFunc(("The log file %s added into the moved list\n", strFile.c_str()));
394 }
395 else
396 LogRelFunc(("The log file %s wasn't added into the moved list. Couldn't get the file size."
397 "\n", strFile.c_str()));
398 ++it;
399 }
400 }
401 }
402 else
403 {
404 LogRelFunc(("Information: The original log folder %s doesn't exist\n", strFolder.c_str()));
405 rc = S_OK;//it's not error in this case if there isn't an original log folder
406 }
407 }
408
409 LogRelFunc(("Total space needed is %lld bytes\n", neededFreeSpace));
410 /* Check a target location on enough room */
411 if (totalFreeSpace - neededFreeSpace <= 1024*1024)
412 {
413 LogRelFunc(("but free space on destination is %lld\n", totalFreeSpace));
414 throw VERR_OUT_OF_RESOURCES;//less than 1Mb free space on the target location
415 }
416
417 /* Add step for .vbox machine setting file */
418 {
419 ++uCount;
420 uTotalWeight += 1;
421 }
422
423 /* Reserve additional steps in case of failure and rollback all changes */
424 {
425 uTotalWeight += uCount;//just add 1 for each possible rollback operation
426 uCount += uCount;//and increase the steps twice
427 }
428
429 /* Init Progress instance */
430 {
431 rc = m_pProgress->init(m_pMachine->i_getVirtualBox(),
432 static_cast<IMachine*>(m_pMachine) /* aInitiator */,
433 Bstr(m_pMachine->tr("Moving Machine")).raw(),
434 true /* fCancellable */,
435 uCount,
436 uTotalWeight,
437 Bstr(m_pMachine->tr("Initialize Moving")).raw(),
438 1);
439 if (FAILED(rc))
440 {
441 throw m_pMachine->setError(VBOX_E_IPRT_ERROR,
442 m_pMachine->tr("Couldn't correctly setup the progress object "
443 "for moving VM operation (%Rrc)"),
444 rc);
445 }
446 }
447
448 /* save all VM data */
449 m_pMachine->i_setModified(Machine::IsModified_MachineData);
450 rc = m_pMachine->SaveSettings();
451 }
452 catch(HRESULT hrc)
453 {
454 rc = hrc;
455 }
456
457 LogFlowFuncLeave();
458
459 return rc;
460}
461
462void MachineMoveVM::printStateFile(settings::SnapshotsList &snl)
463{
464 settings::SnapshotsList::iterator it;
465 for (it = snl.begin(); it != snl.end(); ++it)
466 {
467 if (!it->strStateFile.isEmpty())
468 {
469 settings::Snapshot snap = (settings::Snapshot)(*it);
470 LogRelFunc(("snap.uuid = %s\n", snap.uuid.toStringCurly().c_str()));
471 LogRelFunc(("snap.strStateFile = %s\n", snap.strStateFile.c_str()));
472 }
473
474 if (!it->llChildSnapshots.empty())
475 printStateFile(it->llChildSnapshots);
476 }
477}
478
479/* static */
480DECLCALLBACK(int) MachineMoveVM::updateProgress(unsigned uPercent, void *pvUser)
481{
482 MachineMoveVM* pTask = *(MachineMoveVM**)pvUser;
483
484 if ( pTask
485 && !pTask->m_pProgress.isNull())
486 {
487 BOOL fCanceled;
488 pTask->m_pProgress->COMGETTER(Canceled)(&fCanceled);
489 if (fCanceled)
490 return -1;
491 pTask->m_pProgress->SetCurrentOperationProgress(uPercent);
492 }
493 return VINF_SUCCESS;
494}
495
496/* static */
497DECLCALLBACK(int) MachineMoveVM::copyFileProgress(unsigned uPercentage, void *pvUser)
498{
499 ComObjPtr<Progress> pProgress = *static_cast< ComObjPtr<Progress>* >(pvUser);
500
501 BOOL fCanceled = false;
502 HRESULT rc = pProgress->COMGETTER(Canceled)(&fCanceled);
503 if (FAILED(rc)) return VERR_GENERAL_FAILURE;
504 /* If canceled by the user tell it to the copy operation. */
505 if (fCanceled) return VERR_CANCELLED;
506 /* Set the new process. */
507 rc = pProgress->SetCurrentOperationProgress(uPercentage);
508 if (FAILED(rc)) return VERR_GENERAL_FAILURE;
509
510 return VINF_SUCCESS;
511}
512
513
514/* static */
515void MachineMoveVM::i_MoveVMThreadTask(MachineMoveVM* task)
516{
517 LogFlowFuncEnter();
518 HRESULT rc = S_OK;
519
520 MachineMoveVM* taskMoveVM = task;
521 ComObjPtr<Machine> &machine = taskMoveVM->m_pMachine;
522
523 AutoCaller autoCaller(machine);
524// if (FAILED(autoCaller.rc())) return;//Should we return something here?
525
526 Utf8Str strTargetFolder = taskMoveVM->m_targetPath;
527 {
528 Bstr bstrMachineName;
529 machine->COMGETTER(Name)(bstrMachineName.asOutParam());
530 strTargetFolder.append(Utf8Str(bstrMachineName));
531 }
532
533 RTCList<Utf8Str> newFiles; /* All extra created files (save states, ...) */
534 RTCList<Utf8Str> originalFiles; /* All original files except images */
535 typedef std::map<Utf8Str, ComObjPtr<Medium> > MediumMap;
536 MediumMap mapOriginalMedium;
537
538 /*
539 * We have the couple modes which user is able to request
540 * basic mode:
541 * - The images which are solely attached to the VM
542 * and located in the original VM folder will be moved.
543 * All subfolders related to the original VM are also moved from the original location
544 * (Standard - snapshots and logs folders).
545 *
546 * canonical mode:
547 * - All disks tied with the VM will be moved into a new location if it's possible.
548 * All folders related to the original VM are also moved.
549 * This mode is intended to collect all files/images/snapshots related to the VM in the one place.
550 *
551 */
552
553 /*
554 * A way to handle shareable disk:
555 * Collect the shareable disks attched to the VM.
556 * Get the machines whom the shareable disks attach to.
557 * Return an error if the state of any VM doesn't allow to move a shareable disk and
558 * this disk is located in the VM's folder (it means the disk is intended for "moving").
559 */
560
561
562 /*
563 * Check new destination whether enough room for the VM or not. if "not" return an error.
564 * Make a copy of VM settings and a list with all files which are moved. Save the list on the disk.
565 * Start "move" operation.
566 * Check the result of operation.
567 * if the operation was successful:
568 * - delete all files in the original VM folder;
569 * - update VM disks info with new location;
570 * - update all other VM if it's needed;
571 * - update global settings
572 */
573
574 try
575 {
576 /* Move all disks */
577 rc = taskMoveVM->moveAllDisks(taskMoveVM->finalMediumsMap, &strTargetFolder);
578 if (FAILED(rc))
579 throw rc;
580
581 /* Get Machine::Data here because moveAllDisks() change it */
582 Machine::Data *machineData = machine->mData.data();
583 settings::MachineConfigFile *machineConfFile = machineData->pMachineConfigFile;
584
585 /* Copy all save state files. */
586 Utf8Str strTrgSnapshotFolder;
587 {
588 /* When the current snapshot folder is absolute we reset it to the
589 * default relative folder. */
590 if (RTPathStartsWithRoot((*machineConfFile).machineUserData.strSnapshotFolder.c_str()))
591 (*machineConfFile).machineUserData.strSnapshotFolder = "Snapshots";
592 (*machineConfFile).strStateFile = "";
593
594 /* The absolute name of the snapshot folder. */
595 strTrgSnapshotFolder = Utf8StrFmt("%s%c%s", strTargetFolder.c_str(), RTPATH_DELIMITER,
596 (*machineConfFile).machineUserData.strSnapshotFolder.c_str());
597
598 /* Check if a snapshot folder is necessary and if so doesn't already
599 * exists. */
600 if ( taskMoveVM->finalSaveStateFilesMap.size() != 0
601 && !RTDirExists(strTrgSnapshotFolder.c_str()))
602 {
603 int vrc = RTDirCreateFullPath(strTrgSnapshotFolder.c_str(), 0700);
604 if (RT_FAILURE(vrc))
605 throw machine->setError(VBOX_E_IPRT_ERROR,
606 machine->tr("Could not create snapshots folder '%s' (%Rrc)"),
607 strTrgSnapshotFolder.c_str(), vrc);
608 }
609
610 std::map<Utf8Str, SAVESTATETASK>::iterator itState = taskMoveVM->finalSaveStateFilesMap.begin();
611 while (itState != taskMoveVM->finalSaveStateFilesMap.end())
612 {
613 const SAVESTATETASK &sst = itState->second;
614 const Utf8Str &strTrgSaveState = Utf8StrFmt("%s%c%s", strTrgSnapshotFolder.c_str(), RTPATH_DELIMITER,
615 RTPathFilename(sst.strSaveStateFile.c_str()));
616
617 /* Move to next sub-operation. */
618 rc = taskMoveVM->m_pProgress->SetNextOperation(BstrFmt(machine->tr("Copy the save state file '%s' ..."),
619 RTPathFilename(sst.strSaveStateFile.c_str())).raw(), sst.uWeight);
620 if (FAILED(rc)) throw rc;
621
622 int vrc = RTFileCopyEx(sst.strSaveStateFile.c_str(), strTrgSaveState.c_str(), 0,
623 MachineMoveVM::copyFileProgress, &taskMoveVM->m_pProgress);
624 if (RT_FAILURE(vrc))
625 throw machine->setError(VBOX_E_IPRT_ERROR,
626 machine->tr("Could not copy state file '%s' to '%s' (%Rrc)"),
627 sst.strSaveStateFile.c_str(), strTrgSaveState.c_str(), vrc);
628
629 /* save new file in case of restoring */
630 newFiles.append(strTrgSaveState);
631 /* save original file for deletion in the end */
632 originalFiles.append(sst.strSaveStateFile);
633 ++itState;
634 }
635 }
636
637 /*
638 * Update state file path
639 * very important step!
640 * Not obvious how to do it correctly.
641 */
642 {
643 LogRelFunc(("Update state file path\n"));
644 rc = taskMoveVM->updatePathsToStateFiles(taskMoveVM->finalSaveStateFilesMap,
645 taskMoveVM->vmFolders[VBox_SettingFolder],
646 strTargetFolder);
647 if (FAILED(rc))
648 throw rc;
649 }
650
651 /*
652 * Moving Machine settings file
653 * The settings file are moved after all disks and snapshots because this file should be updated
654 * with actual information and only then should be moved.
655 */
656 {
657 LogRelFunc(("Copy Machine settings file \n"));
658
659 rc = taskMoveVM->m_pProgress->SetNextOperation(BstrFmt(machine->tr("Copy Machine settings file '%s' ..."),
660 (*machineConfFile).machineUserData.strName.c_str()).raw(), 1);
661 if (FAILED(rc)) throw rc;
662
663 Utf8Str strTargetSettingsFilePath = strTargetFolder;
664
665 /* Check a folder existing and create one if it's not */
666 if (!RTDirExists(strTargetSettingsFilePath.c_str()))
667 {
668 int vrc = RTDirCreateFullPath(strTargetSettingsFilePath.c_str(), 0700);
669 if (RT_FAILURE(vrc))
670 throw machine->setError(VBOX_E_IPRT_ERROR,
671 machine->tr("Could not create a home machine folder '%s' (%Rrc)"),
672 strTargetSettingsFilePath.c_str(), vrc);
673 LogRelFunc(("Created a home machine folder %s\n", strTargetSettingsFilePath.c_str()));
674 }
675
676 /* Create a full path */
677 Bstr bstrMachineName;
678 machine->COMGETTER(Name)(bstrMachineName.asOutParam());
679 strTargetSettingsFilePath.append(RTPATH_DELIMITER).append(Utf8Str(bstrMachineName));
680 strTargetSettingsFilePath.append(".vbox");
681
682 Utf8Str strSettingsFilePath;
683 Bstr bstr_settingsFilePath;
684 machine->COMGETTER(SettingsFilePath)(bstr_settingsFilePath.asOutParam());
685 strSettingsFilePath = bstr_settingsFilePath;
686
687 int vrc = RTFileCopyEx(strSettingsFilePath.c_str(), strTargetSettingsFilePath.c_str(), 0,
688 MachineMoveVM::copyFileProgress, &taskMoveVM->m_pProgress);
689 if (RT_FAILURE(vrc))
690 throw machine->setError(VBOX_E_IPRT_ERROR,
691 machine->tr("Could not copy the setting file '%s' to '%s' (%Rrc)"),
692 strSettingsFilePath.c_str(), strTargetSettingsFilePath.stripFilename().c_str(), vrc);
693
694 LogRelFunc(("The setting file %s has been copied into the folder %s\n", strSettingsFilePath.c_str(),
695 strTargetSettingsFilePath.stripFilename().c_str()));
696
697 /* save new file in case of restoring */
698 newFiles.append(strTargetSettingsFilePath);
699 /* save original file for deletion in the end */
700 originalFiles.append(strSettingsFilePath);
701 }
702
703 /* Moving Machine log files */
704 {
705 LogRelFunc(("Copy machine log files \n"));
706
707 if (taskMoveVM->vmFolders[VBox_LogFolder].isNotEmpty())
708 {
709 /* Check an original log folder existence */
710 if (RTDirExists(taskMoveVM->vmFolders[VBox_LogFolder].c_str()))
711 {
712 Utf8Str strTargetLogFolderPath = strTargetFolder;
713 strTargetLogFolderPath.append(RTPATH_DELIMITER).append("Logs");
714
715 /* Check a destination log folder existence and create one if it's not */
716 if (!RTDirExists(strTargetLogFolderPath.c_str()))
717 {
718 int vrc = RTDirCreateFullPath(strTargetLogFolderPath.c_str(), 0700);
719 if (RT_FAILURE(vrc))
720 throw machine->setError(VBOX_E_IPRT_ERROR,
721 machine->tr("Could not create log folder '%s' (%Rrc)"),
722 strTargetLogFolderPath.c_str(), vrc);
723 LogRelFunc(("Created a log machine folder %s\n", strTargetLogFolderPath.c_str()));
724 }
725
726 fileList_t filesList;
727 taskMoveVM->getFilesList(taskMoveVM->vmFolders[VBox_LogFolder], filesList);
728 cit_t it = filesList.m_list.begin();
729 while(it != filesList.m_list.end())
730 {
731 Utf8Str strFullSourceFilePath = it->first.c_str();
732 strFullSourceFilePath.append(RTPATH_DELIMITER).append(it->second.c_str());
733
734 Utf8Str strFullTargetFilePath = strTargetLogFolderPath;
735 strFullTargetFilePath.append(RTPATH_DELIMITER).append(it->second.c_str());
736
737 /* Move to next sub-operation. */
738 rc = taskMoveVM->m_pProgress->SetNextOperation(BstrFmt(machine->tr("Copying the log file '%s' ..."),
739 RTPathFilename(strFullSourceFilePath.c_str())).raw(), 1);
740 if (FAILED(rc)) throw rc;
741
742 int vrc = RTFileCopyEx(strFullSourceFilePath.c_str(), strFullTargetFilePath.c_str(), 0,
743 MachineMoveVM::copyFileProgress, &taskMoveVM->m_pProgress);
744 if (RT_FAILURE(vrc))
745 throw machine->setError(VBOX_E_IPRT_ERROR,
746 machine->tr("Could not copy the log file '%s' to '%s' (%Rrc)"),
747 strFullSourceFilePath.c_str(), strFullTargetFilePath.stripFilename().c_str(), vrc);
748
749 LogRelFunc(("The log file %s has been copied into the folder %s\n", strFullSourceFilePath.c_str(),
750 strFullTargetFilePath.stripFilename().c_str()));
751
752 /* save new file in case of restoring */
753 newFiles.append(strFullTargetFilePath);
754 /* save original file for deletion in the end */
755 originalFiles.append(strFullSourceFilePath);
756
757 ++it;
758 }
759 }
760 }
761 }
762
763 /* save all VM data */
764 {
765 rc = machine->SaveSettings();
766 }
767
768 {
769 LogRelFunc(("Update path to XML setting file\n"));
770 Utf8Str strTargetSettingsFilePath = strTargetFolder;
771 Bstr bstrMachineName;
772 machine->COMGETTER(Name)(bstrMachineName.asOutParam());
773 strTargetSettingsFilePath.append(RTPATH_DELIMITER).append(Utf8Str(bstrMachineName)).append(".vbox");
774 machineData->m_strConfigFileFull = strTargetSettingsFilePath;
775 machine->mParent->i_copyPathRelativeToConfig(strTargetSettingsFilePath, machineData->m_strConfigFile);
776 }
777
778 /* Save global settings in the VirtualBox.xml */
779 {
780 /* Marks the global registry for uuid as modified */
781 Guid uuid = machine->mData->mUuid;
782 machine->mParent->i_markRegistryModified(uuid);
783
784 /* for saving the global settings we should hold only the VirtualBox lock */
785 AutoWriteLock vboxLock(machine->mParent COMMA_LOCKVAL_SRC_POS);
786
787 rc = machine->mParent->i_saveSettings();
788 }
789 }
790 catch(HRESULT hrc)
791 {
792 LogRelFunc(("Moving machine to a new destination was failed. Check original and destination places.\n"));
793 rc = hrc;
794 taskMoveVM->result = rc;
795 }
796 catch (...)
797 {
798 LogRelFunc(("Moving machine to a new destination was failed. Check original and destination places.\n"));
799 rc = VirtualBoxBase::handleUnexpectedExceptions(machine, RT_SRC_POS);
800 taskMoveVM->result = rc;
801 }
802
803 /* Cleanup on failure */
804 if (FAILED(rc))
805 {
806 Machine::Data *machineData = machine->mData.data();
807
808 /* ! Apparently we should update the Progress object !*/
809 ULONG operationCount = 0;
810 rc = taskMoveVM->m_pProgress->COMGETTER(OperationCount)(&operationCount);
811 ULONG operation = 0;
812 rc = taskMoveVM->m_pProgress->COMGETTER(Operation)(&operation);
813 Bstr bstrOperationDescription;
814 rc = taskMoveVM->m_pProgress->COMGETTER(OperationDescription)(bstrOperationDescription.asOutParam());
815 Utf8Str strOperationDescription = bstrOperationDescription;
816 ULONG operationPercent = 0;
817 rc = taskMoveVM->m_pProgress->COMGETTER(OperationPercent)(&operationPercent);
818
819 Bstr bstrMachineName;
820 machine->COMGETTER(Name)(bstrMachineName.asOutParam());
821 LogRelFunc(("Moving machine %s was failed on operation %s\n",
822 Utf8Str(bstrMachineName.raw()).c_str(), Utf8Str(bstrOperationDescription.raw()).c_str()));
823
824 /* Restoring the original mediums */
825 try
826 {
827 /*
828 * Fix the progress count
829 * In instance, the whole "move vm" operation is failed on 9th step. But total count is 20.
830 * Where 20 = 2 * 10 operations, where 10 is the real number of operations. And this value was doubled
831 * earlier in the init() exactly for one reason - rollback operation. Because in this case we must do
832 * the same operations but in backward direction.
833 * Thus now we want to correct progress operation count from 9 to 11. Why?
834 * Because we should have evaluated count as "20/2 + (20/2 - 9)" = 11 or just "20 - 9" = 11
835 */
836 for (ULONG i = operation; i < operationCount - operation; ++i)
837 {
838 rc = taskMoveVM->m_pProgress->SetNextOperation(BstrFmt("Skip the empty operation %d...", i + 1).raw(), 1);
839 if (FAILED(rc)) throw rc;
840 }
841
842 rc = taskMoveVM->moveAllDisks(taskMoveVM->finalMediumsMap);
843 if (FAILED(rc))
844 throw rc;
845 }
846 catch(HRESULT hrc)
847 {
848 LogRelFunc(("Rollback scenario: restoration the original mediums were failed. Machine can be corrupted.\n"));
849 taskMoveVM->result = hrc;
850 }
851 catch (...)
852 {
853 LogRelFunc(("Rollback scenario: restoration the original mediums were failed. Machine can be corrupted.\n"));
854 rc = VirtualBoxBase::handleUnexpectedExceptions(machine, RT_SRC_POS);
855 taskMoveVM->result = rc;
856 }
857
858 /* Revert original paths to the state files */
859 rc = taskMoveVM->updatePathsToStateFiles(taskMoveVM->finalSaveStateFilesMap,
860 strTargetFolder,
861 taskMoveVM->vmFolders[VBox_SettingFolder]);
862 if (FAILED(rc))
863 {
864 LogRelFunc(("Rollback scenario: can't restore the original paths to the state files. "
865 "Machine settings %s can be corrupted.\n", machineData->m_strConfigFileFull.c_str()));
866 }
867
868 /* Delete all created files. Here we update progress object */
869 rc = taskMoveVM->deleteFiles(newFiles);
870 if (FAILED(rc))
871 LogRelFunc(("Rollback scenario: can't delete new created files. Check the destination folder."));
872
873 /* Delete destination folder */
874 RTDirRemove(strTargetFolder.c_str());
875
876 /* save all VM data */
877 {
878 AutoWriteLock srcLock(machine COMMA_LOCKVAL_SRC_POS);
879 srcLock.release();
880 rc = machine->SaveSettings();
881 srcLock.acquire();
882 }
883
884 /* Restore an original path to XML setting file */
885 {
886 LogRelFunc(("Rollback scenario: Restore an original path to XML setting file\n"));
887 Utf8Str strOriginalSettingsFilePath = taskMoveVM->vmFolders[VBox_SettingFolder];
888 strOriginalSettingsFilePath.append(RTPATH_DELIMITER).append(Utf8Str(bstrMachineName)).append(".vbox");
889 machineData->m_strConfigFileFull = strOriginalSettingsFilePath;
890 machine->mParent->i_copyPathRelativeToConfig(strOriginalSettingsFilePath, machineData->m_strConfigFile);
891 }
892
893 /* Marks the global registry for uuid as modified */
894 {
895 AutoWriteLock srcLock(machine COMMA_LOCKVAL_SRC_POS);
896 srcLock.release();
897 Guid uuid = machine->mData->mUuid;
898 machine->mParent->i_markRegistryModified(uuid);
899 srcLock.acquire();
900
901 // save the global settings; for that we should hold only the VirtualBox lock
902 AutoWriteLock vboxLock(machine->mParent COMMA_LOCKVAL_SRC_POS);
903
904 rc = machine->mParent->i_saveSettings();
905 }
906
907 /* In case of failure the progress object on the other side (user side) get notification about operation
908 completion but the operation percentage may not be set to 100% */
909 }
910 else /*Operation was successful and now we can delete the original files like the state files, XML setting, log files */
911 {
912 /*
913 * In case of success it's not urgent to update the progress object because we call i_notifyComplete() with
914 * the success result. As result, the last number of progress operation can be not equal the number of operations
915 * because we doubled the number of operations for rollback case.
916 * But if we want to update the progress object corectly it's needed to add all medium moved by standard
917 * "move medium" logic (for us it's taskMoveVM->finalMediumsMap) to the current number of operation.
918 */
919
920 ULONG operationCount = 0;
921 rc = taskMoveVM->m_pProgress->COMGETTER(OperationCount)(&operationCount);
922 ULONG operation = 0;
923 rc = taskMoveVM->m_pProgress->COMGETTER(Operation)(&operation);
924
925 for (ULONG i = operation; i < operation + taskMoveVM->finalMediumsMap.size() - 1; ++i)
926 {
927 rc = taskMoveVM->m_pProgress->SetNextOperation(BstrFmt("Skip the empty operation %d...", i).raw(), 1);
928 if (FAILED(rc)) throw rc;
929 }
930
931 rc = taskMoveVM->deleteFiles(originalFiles);
932 if (FAILED(rc))
933 LogRelFunc(("Forward scenario: can't delete all original files.\n"));
934 }
935
936 if (!taskMoveVM->m_pProgress.isNull())
937 taskMoveVM->m_pProgress->i_notifyComplete(taskMoveVM->result);
938
939 LogFlowFuncLeave();
940}
941
942HRESULT MachineMoveVM::moveAllDisks(const std::map<Utf8Str, MEDIUMTASK>& listOfDisks,
943 const Utf8Str* strTargetFolder)
944{
945 HRESULT rc = S_OK;
946 ComObjPtr<Machine> &machine = m_pMachine;
947 Utf8Str strLocation;
948
949 AutoWriteLock machineLock(machine COMMA_LOCKVAL_SRC_POS);
950
951 try{
952 std::map<Utf8Str, MEDIUMTASK>::const_iterator itMedium = listOfDisks.begin();
953 while(itMedium != listOfDisks.end())
954 {
955 const MEDIUMTASK &mt = itMedium->second;
956 ComPtr<IMedium> pMedium = mt.pMedium;
957 Utf8Str strTargetImageName;
958 Bstr bstrLocation;
959 Bstr bstrSrcName;
960
961 rc = pMedium->COMGETTER(Name)(bstrSrcName.asOutParam());
962 if (FAILED(rc)) throw rc;
963
964 if (strTargetFolder != NULL && !strTargetFolder->isEmpty())
965 {
966 strTargetImageName = *strTargetFolder;
967 rc = pMedium->COMGETTER(Location)(bstrLocation.asOutParam());
968 if (FAILED(rc)) throw rc;
969 strLocation = bstrLocation;
970
971 if (mt.fSnapshot == true)
972 {
973 strLocation.stripFilename().stripPath().append(RTPATH_DELIMITER).append(Utf8Str(bstrSrcName));
974 }
975 else
976 {
977 strLocation.stripPath();
978 }
979
980 strTargetImageName.append(RTPATH_DELIMITER).append(strLocation);
981 rc = m_pProgress->SetNextOperation(BstrFmt(machine->tr("Moving medium '%ls' ..."),
982 bstrSrcName.raw()).raw(),
983 mt.uWeight);
984 if (FAILED(rc)) throw rc;
985 }
986 else
987 {
988 strTargetImageName = mt.strBaseName;//Should contain full path to the image
989 rc = m_pProgress->SetNextOperation(BstrFmt(machine->tr("Moving medium '%ls' back..."),
990 bstrSrcName.raw()).raw(),
991 mt.uWeight);
992 if (FAILED(rc)) throw rc;
993 }
994
995
996
997 /* consistency: use \ if appropriate on the platform */
998 RTPathChangeToDosSlashes(strTargetImageName.mutableRaw(), false);
999
1000 bstrLocation = strTargetImageName.c_str();
1001
1002 MediumType_T mediumType;//immutable, shared, passthrough
1003 rc = pMedium->COMGETTER(Type)(&mediumType);
1004 if (FAILED(rc)) throw rc;
1005
1006 DeviceType_T deviceType;//floppy, hard, DVD
1007 rc = pMedium->COMGETTER(DeviceType)(&deviceType);
1008 if (FAILED(rc)) throw rc;
1009
1010 /* Drop lock early because IMedium::SetLocation needs to get the VirtualBox one. */
1011 machineLock.release();
1012
1013 ComPtr<IProgress> moveDiskProgress;
1014 rc = pMedium->SetLocation(bstrLocation.raw(), moveDiskProgress.asOutParam());
1015 /* Wait until the async process has finished. */
1016
1017 rc = m_pProgress->WaitForAsyncProgressCompletion(moveDiskProgress);
1018
1019 machineLock.acquire();
1020 if (FAILED(rc)) throw rc;
1021
1022 LogRelFunc(("Moving %s has been finished\n", strTargetImageName.c_str()));
1023
1024 /* Check the result of the async process. */
1025 LONG iRc;
1026 rc = moveDiskProgress->COMGETTER(ResultCode)(&iRc);
1027 if (FAILED(rc)) throw rc;
1028 /* If the thread of the progress object has an error, then
1029 * retrieve the error info from there, or it'll be lost. */
1030 if (FAILED(iRc))
1031 throw machine->setError(ProgressErrorInfo(moveDiskProgress));
1032
1033 ++itMedium;
1034 }
1035
1036 machineLock.release();
1037 }
1038 catch(HRESULT hrc)
1039 {
1040 LogRelFunc(("\nException during moving the disk %s\n", strLocation.c_str()));
1041 rc = hrc;
1042 machineLock.release();
1043 }
1044 catch (...)
1045 {
1046 LogRelFunc(("\nException during moving the disk %s\n", strLocation.c_str()));
1047 rc = VirtualBoxBase::handleUnexpectedExceptions(m_pMachine, RT_SRC_POS);
1048 machineLock.release();
1049 }
1050
1051 return rc;
1052}
1053
1054HRESULT MachineMoveVM::updatePathsToStateFiles(const std::map<Utf8Str, SAVESTATETASK>& listOfFiles,
1055 const Utf8Str& sourcePath, const Utf8Str& targetPath)
1056{
1057 HRESULT rc = S_OK;
1058
1059 std::map<Utf8Str, SAVESTATETASK>::const_iterator itState = listOfFiles.begin();
1060 while (itState != listOfFiles.end())
1061 {
1062 const SAVESTATETASK &sst = itState->second;
1063
1064 if (sst.snapshotUuid != Guid::Empty)
1065 {
1066 Utf8Str strGuidMachine = sst.snapshotUuid.toString();
1067 ComObjPtr<Snapshot> snapshotMachineObj;
1068
1069 rc = m_pMachine->i_findSnapshotById(sst.snapshotUuid, snapshotMachineObj, true);
1070 if (SUCCEEDED(rc) && !snapshotMachineObj.isNull())
1071 {
1072 snapshotMachineObj->i_updateSavedStatePaths(sourcePath.c_str(),
1073 targetPath.c_str());
1074 }
1075 }
1076 else
1077 {
1078 const Utf8Str &path = m_pMachine->mSSData->strStateFilePath;
1079 m_pMachine->mSSData->strStateFilePath = Utf8StrFmt("%s%s",
1080 targetPath.c_str(),
1081 path.c_str() + sourcePath.length());
1082 }
1083
1084 ++itState;
1085 }
1086
1087 return rc;
1088}
1089
1090HRESULT MachineMoveVM::getFilesList(const Utf8Str& strRootFolder, fileList_t &filesList)
1091{
1092 RTDIR hDir;
1093 HRESULT rc = S_OK;
1094 int vrc = RTDirOpen(&hDir, strRootFolder.c_str());
1095 if (RT_SUCCESS(vrc))
1096 {
1097 RTDIRENTRY DirEntry;
1098 while (RT_SUCCESS(RTDirRead(hDir, &DirEntry, NULL)))
1099 {
1100 if (RTDirEntryIsStdDotLink(&DirEntry))
1101 continue;
1102
1103 if (DirEntry.enmType == RTDIRENTRYTYPE_FILE)
1104 {
1105 Utf8Str fullPath(strRootFolder);
1106 filesList.add(strRootFolder, DirEntry.szName);
1107 }
1108 else if (DirEntry.enmType == RTDIRENTRYTYPE_DIRECTORY)
1109 {
1110 Utf8Str strNextFolder(strRootFolder);
1111 strNextFolder.append(RTPATH_DELIMITER).append(DirEntry.szName);
1112 rc = getFilesList(strNextFolder, filesList);
1113 if (FAILED(rc))
1114 break;
1115 }
1116 }
1117
1118 vrc = RTDirClose(hDir);
1119 }
1120 else if (vrc == VERR_FILE_NOT_FOUND)
1121 {
1122 m_pMachine->setError(VBOX_E_IPRT_ERROR,
1123 m_pMachine->tr("Folder '%s' doesn't exist (%Rrc)"),
1124 strRootFolder.c_str(), vrc);
1125 rc = vrc;
1126 }
1127 else
1128 throw m_pMachine->setError(VBOX_E_IPRT_ERROR,
1129 m_pMachine->tr("Could not open folder '%s' (%Rrc)"),
1130 strRootFolder.c_str(), vrc);
1131 return rc;
1132}
1133
1134HRESULT MachineMoveVM::deleteFiles(const RTCList<Utf8Str>& listOfFiles)
1135{
1136 HRESULT rc = S_OK;
1137 /* Delete all created files. */
1138 try
1139 {
1140 for (size_t i = 0; i < listOfFiles.size(); ++i)
1141 {
1142 LogRelFunc(("Deleting file %s ...\n", listOfFiles.at(i).c_str()));
1143 rc = m_pProgress->SetNextOperation(BstrFmt("Deleting file %s...", listOfFiles.at(i).c_str()).raw(), 1);
1144 if (FAILED(rc)) throw rc;
1145
1146 int vrc = RTFileDelete(listOfFiles.at(i).c_str());
1147 if (RT_FAILURE(vrc))
1148 rc = m_pMachine->setError(VBOX_E_IPRT_ERROR,
1149 m_pMachine->tr("Could not delete file '%s' (%Rrc)"),
1150 listOfFiles.at(i).c_str(), rc);
1151 else
1152 LogRelFunc(("File %s has been deleted\n", listOfFiles.at(i).c_str()));
1153 }
1154 }
1155 catch(HRESULT hrc)
1156 {
1157 rc = hrc;
1158 }
1159 catch (...)
1160 {
1161 rc = VirtualBoxBase::handleUnexpectedExceptions(m_pMachine, RT_SRC_POS);
1162 }
1163
1164 return rc;
1165}
1166
1167HRESULT MachineMoveVM::getFolderSize(const Utf8Str& strRootFolder, uint64_t& size)
1168{
1169 HRESULT rc = S_OK;
1170 int vrc = 0;
1171 uint64_t totalFolderSize = 0;
1172 fileList_t filesList;
1173
1174 bool ex = RTPathExists(strRootFolder.c_str());
1175 if (ex == true)
1176 {
1177 rc = getFilesList(strRootFolder, filesList);
1178 if (SUCCEEDED(rc))
1179 {
1180 cit_t it = filesList.m_list.begin();
1181 while(it != filesList.m_list.end())
1182 {
1183 uint64_t cbFile = 0;
1184 Utf8Str fullPath = it->first;
1185 fullPath.append(RTPATH_DELIMITER).append(it->second);
1186 vrc = RTFileQuerySize(fullPath.c_str(), &cbFile);
1187 if (RT_SUCCESS(vrc))
1188 {
1189 totalFolderSize += cbFile;
1190 }
1191 else
1192 throw m_pMachine->setError(VBOX_E_IPRT_ERROR,
1193 m_pMachine->tr("Could not get the size of file '%s' (%Rrc)"),
1194 fullPath.c_str(), vrc);
1195 ++it;
1196 }
1197
1198 size = totalFolderSize;
1199 }
1200 else
1201 m_pMachine->setError(VBOX_E_IPRT_ERROR,
1202 m_pMachine->tr("Could not calculate the size of folder '%s' (%Rrc)"),
1203 strRootFolder.c_str(), vrc);
1204 }
1205 else
1206 size = 0;
1207
1208 return rc;
1209}
1210
1211HRESULT MachineMoveVM::queryBaseName(const ComPtr<IMedium> &pMedium, Utf8Str &strBaseName) const
1212{
1213 ComPtr<IMedium> pBaseMedium;
1214 HRESULT rc = pMedium->COMGETTER(Base)(pBaseMedium.asOutParam());
1215 if (FAILED(rc)) return rc;
1216 Bstr bstrBaseName;
1217 rc = pBaseMedium->COMGETTER(Name)(bstrBaseName.asOutParam());
1218 if (FAILED(rc)) return rc;
1219 strBaseName = bstrBaseName;
1220 return rc;
1221}
1222
1223HRESULT MachineMoveVM::createMachineList(const ComPtr<ISnapshot> &pSnapshot,
1224 std::vector< ComObjPtr<Machine> > &aMachineList) const
1225{
1226 HRESULT rc = S_OK;
1227 Bstr name;
1228 rc = pSnapshot->COMGETTER(Name)(name.asOutParam());
1229 if (FAILED(rc)) return rc;
1230
1231 ComPtr<IMachine> l_pMachine;
1232 rc = pSnapshot->COMGETTER(Machine)(l_pMachine.asOutParam());
1233 if (FAILED(rc)) return rc;
1234 aMachineList.push_back((Machine*)(IMachine*)l_pMachine);
1235
1236 SafeIfaceArray<ISnapshot> sfaChilds;
1237 rc = pSnapshot->COMGETTER(Children)(ComSafeArrayAsOutParam(sfaChilds));
1238 if (FAILED(rc)) return rc;
1239 for (size_t i = 0; i < sfaChilds.size(); ++i)
1240 {
1241 rc = createMachineList(sfaChilds[i], aMachineList);
1242 if (FAILED(rc)) return rc;
1243 }
1244
1245 return rc;
1246}
1247
1248HRESULT MachineMoveVM::queryMediasForAllStates(const std::vector<ComObjPtr<Machine> > &aMachineList)
1249{
1250 /* In this case we create a exact copy of the original VM. This means just
1251 * adding all directly and indirectly attached disk images to the worker
1252 * list. */
1253 HRESULT rc = S_OK;
1254 for (size_t i = 0; i < aMachineList.size(); ++i)
1255 {
1256 const ComObjPtr<Machine> &machine = aMachineList.at(i);
1257
1258 /* Add all attachments (and their parents) of the different
1259 * machines to a worker list. */
1260 SafeIfaceArray<IMediumAttachment> sfaAttachments;
1261 rc = machine->COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(sfaAttachments));
1262 if (FAILED(rc)) return rc;
1263 for (size_t a = 0; a < sfaAttachments.size(); ++a)
1264 {
1265 const ComPtr<IMediumAttachment> &pAtt = sfaAttachments[a];
1266 DeviceType_T deviceType;//floppy, hard, DVD
1267 rc = pAtt->COMGETTER(Type)(&deviceType);
1268 if (FAILED(rc)) return rc;
1269
1270 /* Valid medium attached? */
1271 ComPtr<IMedium> pMedium;
1272 rc = pAtt->COMGETTER(Medium)(pMedium.asOutParam());
1273 if (FAILED(rc)) return rc;
1274
1275 if (pMedium.isNull())
1276 continue;
1277
1278 Bstr bstrLocation;
1279 rc = pMedium->COMGETTER(Location)(bstrLocation.asOutParam());
1280 if (FAILED(rc)) throw rc;
1281
1282 /* Cast to ComObjPtr<Medium> */
1283 ComObjPtr<Medium> pObjMedium = (Medium *)(IMedium *)pMedium;
1284
1285 /*Check for "read-only" medium in terms that VBox can't create this one */
1286 bool fPass = isMediumTypeSupportedForMoving(pMedium);
1287 if(!fPass)
1288 {
1289 LogRelFunc(("Skipping file %s because of this medium type hasn't been supported for moving.\n",
1290 Utf8Str(bstrLocation.raw()).c_str()));
1291 continue;
1292 }
1293
1294 MEDIUMTASKCHAIN mtc;
1295 mtc.devType = deviceType;
1296
1297 while (!pMedium.isNull())
1298 {
1299 /* Refresh the state so that the file size get read. */
1300 MediumState_T e;
1301 rc = pMedium->RefreshState(&e);
1302 if (FAILED(rc)) return rc;
1303
1304 LONG64 lSize;
1305 rc = pMedium->COMGETTER(Size)(&lSize);
1306 if (FAILED(rc)) return rc;
1307
1308 MediumType_T mediumType;//immutable, shared, passthrough
1309 rc = pMedium->COMGETTER(Type)(&mediumType);
1310 if (FAILED(rc)) throw rc;
1311
1312 rc = pMedium->COMGETTER(Location)(bstrLocation.asOutParam());
1313 if (FAILED(rc)) throw rc;
1314
1315 MEDIUMTASK mt;
1316 mt.strBaseName = bstrLocation;
1317 Utf8Str strFolder = vmFolders[VBox_SnapshotFolder];
1318 if (strFolder.isNotEmpty() && mt.strBaseName.contains(strFolder))
1319 {
1320 mt.fSnapshot = true;
1321 }
1322 else
1323 mt.fSnapshot = false;
1324
1325 mt.uIdx = UINT32_MAX;
1326 mt.pMedium = pMedium;
1327 mt.uWeight = (ULONG)((lSize + _1M - 1) / _1M);
1328 mtc.chain.append(mt);
1329
1330 /* Query next parent. */
1331 rc = pMedium->COMGETTER(Parent)(pMedium.asOutParam());
1332 if (FAILED(rc)) return rc;
1333 }
1334
1335 llMedias.append(mtc);
1336 }
1337 /* Add the save state files of this machine if there is one. */
1338 rc = addSaveState(machine);
1339 if (FAILED(rc)) return rc;
1340
1341 }
1342 /* Build up the index list of the image chain. Unfortunately we can't do
1343 * that in the previous loop, cause there we go from child -> parent and
1344 * didn't know how many are between. */
1345 for (size_t i = 0; i < llMedias.size(); ++i)
1346 {
1347 uint32_t uIdx = 0;
1348 MEDIUMTASKCHAIN &mtc = llMedias.at(i);
1349 for (size_t a = mtc.chain.size(); a > 0; --a)
1350 mtc.chain[a - 1].uIdx = uIdx++;
1351 }
1352
1353 return rc;
1354}
1355
1356HRESULT MachineMoveVM::addSaveState(const ComObjPtr<Machine> &machine)
1357{
1358 Bstr bstrSrcSaveStatePath;
1359 HRESULT rc = machine->COMGETTER(StateFilePath)(bstrSrcSaveStatePath.asOutParam());
1360 if (FAILED(rc)) return rc;
1361 if (!bstrSrcSaveStatePath.isEmpty())
1362 {
1363 SAVESTATETASK sst;
1364
1365 sst.snapshotUuid = machine->i_getSnapshotId();
1366 sst.strSaveStateFile = bstrSrcSaveStatePath;
1367 uint64_t cbSize;
1368
1369 int vrc = RTFileQuerySize(sst.strSaveStateFile.c_str(), &cbSize);
1370 if (RT_FAILURE(vrc))
1371 return m_pMachine->setError(VBOX_E_IPRT_ERROR, m_pMachine->tr("Could not query file size of '%s' (%Rrc)"),
1372 sst.strSaveStateFile.c_str(), vrc);
1373 /* same rule as above: count both the data which needs to
1374 * be read and written */
1375 sst.uWeight = (ULONG)(2 * (cbSize + _1M - 1) / _1M);
1376 llSaveStateFiles.append(sst);
1377 }
1378 return S_OK;
1379}
1380
1381void MachineMoveVM::updateProgressStats(MEDIUMTASKCHAIN &mtc, ULONG &uCount, ULONG &uTotalWeight) const
1382{
1383
1384 /* Currently the copying of diff images involves reading at least
1385 * the biggest parent in the previous chain. So even if the new
1386 * diff image is small in size, it could need some time to create
1387 * it. Adding the biggest size in the chain should balance this a
1388 * little bit more, i.e. the weight is the sum of the data which
1389 * needs to be read and written. */
1390 ULONG uMaxWeight = 0;
1391 for (size_t e = mtc.chain.size(); e > 0; --e)
1392 {
1393 MEDIUMTASK &mt = mtc.chain.at(e - 1);
1394 mt.uWeight += uMaxWeight;
1395
1396 /* Calculate progress data */
1397 ++uCount;
1398 uTotalWeight += mt.uWeight;
1399
1400 /* Save the max size for better weighting of diff image
1401 * creation. */
1402 uMaxWeight = RT_MAX(uMaxWeight, mt.uWeight);
1403 }
1404}
1405
1406bool MachineMoveVM::isMediumTypeSupportedForMoving(const ComPtr<IMedium> &pMedium)
1407{
1408 HRESULT rc = S_OK;
1409 bool fSupported = true;
1410 Bstr bstrLocation;
1411 rc = pMedium->COMGETTER(Location)(bstrLocation.asOutParam());
1412 if (FAILED(rc))
1413 {
1414 fSupported = false;
1415 throw rc;
1416 }
1417
1418 DeviceType_T deviceType;
1419 rc = pMedium->COMGETTER(DeviceType)(&deviceType);
1420 if (FAILED(rc))
1421 {
1422 fSupported = false;
1423 throw rc;
1424 }
1425
1426 ComPtr<IMediumFormat> mediumFormat;
1427 rc = pMedium->COMGETTER(MediumFormat)(mediumFormat.asOutParam());
1428 if (FAILED(rc))
1429 {
1430 fSupported = false;
1431 throw rc;
1432 }
1433
1434 /*Check whether VBox is able to create this medium format or not, i.e. medium can be "read-only" */
1435 Bstr bstrFormatName;
1436 rc = mediumFormat->COMGETTER(Name)(bstrFormatName.asOutParam());
1437 if (FAILED(rc))
1438 {
1439 fSupported = false;
1440 throw rc;
1441 }
1442
1443 Utf8Str formatName = Utf8Str(bstrFormatName);
1444 if (formatName.compare("VHDX", Utf8Str::CaseInsensitive) == 0)
1445 {
1446 LogRelFunc(("Skipping medium %s. VHDX format is supported in \"read-only\" mode only. \n",
1447 Utf8Str(bstrLocation.raw()).c_str()));
1448 fSupported = false;
1449 }
1450
1451 /* Check whether medium is represented by file on the disk or not */
1452 if (fSupported)
1453 {
1454 ComObjPtr<Medium> pObjMedium = (Medium *)(IMedium *)pMedium;
1455 fSupported = pObjMedium->i_isMediumFormatFile();
1456 if (!fSupported)
1457 {
1458 LogRelFunc(("Skipping medium %s because it's not a real file on the disk.\n",
1459 Utf8Str(bstrLocation.raw()).c_str()));
1460 }
1461 }
1462
1463 /* some special checks for DVD */
1464 if (fSupported && deviceType == DeviceType_DVD)
1465 {
1466 Utf8Str ext = bstrLocation;
1467 ext.assignEx(RTPathSuffix(ext.c_str()));//returns extension with dot (".iso")
1468
1469 //only ISO image is moved. Otherwise adding some information into log file
1470 int equality = ext.compare(".iso", Utf8Str::CaseInsensitive);
1471 if (equality != false)
1472 {
1473 LogRelFunc(("Skipping file %s. Only ISO images are supported for now.\n",
1474 Utf8Str(bstrLocation.raw()).c_str()));
1475 fSupported = false;
1476 }
1477 }
1478
1479 return fSupported;
1480}
Note: See TracBrowser for help on using the repository browser.

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