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