VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageSnapshot.cpp@ 17185

Last change on this file since 17185 was 17104, checked in by vboxsync, 16 years ago

VBoxManage: split USB into separate file

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.5 KB
Line 
1/* $Id: VBoxManageSnapshot.cpp 17104 2009-02-24 23:18:45Z vboxsync $ */
2/** @file
3 * VBoxManage - The 'snapshot' command.
4 */
5
6/*
7 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#include <VBox/com/com.h>
26#include <VBox/com/string.h>
27#include <VBox/com/ErrorInfo.h>
28#include <VBox/com/errorprint2.h>
29
30#include <VBox/com/VirtualBox.h>
31
32#include <iprt/stream.h>
33#include <iprt/getopt.h>
34
35#include "VBoxManage.h"
36using namespace com;
37
38int handleSnapshot(HandlerArg *a)
39{
40 HRESULT rc;
41
42 /* we need at least a VM and a command */
43 if (a->argc < 2)
44 return errorSyntax(USAGE_SNAPSHOT, "Not enough parameters");
45
46 /* the first argument must be the VM */
47 ComPtr<IMachine> machine;
48 /* assume it's a UUID */
49 rc = a->virtualBox->GetMachine(Guid(a->argv[0]), machine.asOutParam());
50 if (FAILED(rc) || !machine)
51 {
52 /* must be a name */
53 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
54 }
55 if (!machine)
56 return 1;
57 Guid guid;
58 machine->COMGETTER(Id)(guid.asOutParam());
59
60 do
61 {
62 /* we have to open a session for this task. First try an existing session */
63 rc = a->virtualBox->OpenExistingSession(a->session, guid);
64 if (FAILED(rc))
65 CHECK_ERROR_BREAK(a->virtualBox, OpenSession(a->session, guid));
66 ComPtr<IConsole> console;
67 CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
68
69 /* switch based on the command */
70 if (strcmp(a->argv[1], "take") == 0)
71 {
72 /* there must be a name */
73 if (a->argc < 3)
74 {
75 errorSyntax(USAGE_SNAPSHOT, "Missing snapshot name");
76 rc = E_FAIL;
77 break;
78 }
79 Bstr name(a->argv[2]);
80 if ((a->argc > 3) && ((a->argc != 5) || (strcmp(a->argv[3], "-desc") != 0)))
81 {
82 errorSyntax(USAGE_SNAPSHOT, "Incorrect description format");
83 rc = E_FAIL;
84 break;
85 }
86 Bstr desc;
87 if (a->argc == 5)
88 desc = a->argv[4];
89 ComPtr<IProgress> progress;
90 CHECK_ERROR_BREAK(console, TakeSnapshot(name, desc, progress.asOutParam()));
91
92 showProgress(progress);
93 progress->COMGETTER(ResultCode)(&rc);
94 if (FAILED(rc))
95 {
96 com::ProgressErrorInfo info(progress);
97 if (info.isBasicAvailable())
98 RTPrintf("Error: failed to take snapshot. Error message: %lS\n", info.getText().raw());
99 else
100 RTPrintf("Error: failed to take snapshot. No error message available!\n");
101 }
102 }
103 else if (strcmp(a->argv[1], "discard") == 0)
104 {
105 /* exactly one parameter: snapshot name */
106 if (a->argc != 3)
107 {
108 errorSyntax(USAGE_SNAPSHOT, "Expecting snapshot name only");
109 rc = E_FAIL;
110 break;
111 }
112
113 ComPtr<ISnapshot> snapshot;
114
115 /* assume it's a UUID */
116 Guid guid(a->argv[2]);
117 if (!guid.isEmpty())
118 {
119 CHECK_ERROR_BREAK(machine, GetSnapshot(guid, snapshot.asOutParam()));
120 }
121 else
122 {
123 /* then it must be a name */
124 CHECK_ERROR_BREAK(machine, FindSnapshot(Bstr(a->argv[2]), snapshot.asOutParam()));
125 }
126
127 snapshot->COMGETTER(Id)(guid.asOutParam());
128
129 ComPtr<IProgress> progress;
130 CHECK_ERROR_BREAK(console, DiscardSnapshot(guid, progress.asOutParam()));
131
132 showProgress(progress);
133 progress->COMGETTER(ResultCode)(&rc);
134 if (FAILED(rc))
135 {
136 com::ProgressErrorInfo info(progress);
137 if (info.isBasicAvailable())
138 RTPrintf("Error: failed to discard snapshot. Error message: %lS\n", info.getText().raw());
139 else
140 RTPrintf("Error: failed to discard snapshot. No error message available!\n");
141 }
142 }
143 else if (strcmp(a->argv[1], "discardcurrent") == 0)
144 {
145 if ( (a->argc != 3)
146 || ( (strcmp(a->argv[2], "-state") != 0)
147 && (strcmp(a->argv[2], "-all") != 0)))
148 {
149 errorSyntax(USAGE_SNAPSHOT, "Invalid parameter '%s'", Utf8Str(a->argv[2]).raw());
150 rc = E_FAIL;
151 break;
152 }
153 bool fAll = false;
154 if (strcmp(a->argv[2], "-all") == 0)
155 fAll = true;
156
157 ComPtr<IProgress> progress;
158
159 if (fAll)
160 {
161 CHECK_ERROR_BREAK(console, DiscardCurrentSnapshotAndState(progress.asOutParam()));
162 }
163 else
164 {
165 CHECK_ERROR_BREAK(console, DiscardCurrentState(progress.asOutParam()));
166 }
167
168 showProgress(progress);
169 progress->COMGETTER(ResultCode)(&rc);
170 if (FAILED(rc))
171 {
172 com::ProgressErrorInfo info(progress);
173 if (info.isBasicAvailable())
174 RTPrintf("Error: failed to discard. Error message: %lS\n", info.getText().raw());
175 else
176 RTPrintf("Error: failed to discard. No error message available!\n");
177 }
178
179 }
180 else if (strcmp(a->argv[1], "edit") == 0)
181 {
182 if (a->argc < 3)
183 {
184 errorSyntax(USAGE_SNAPSHOT, "Missing snapshot name");
185 rc = E_FAIL;
186 break;
187 }
188
189 ComPtr<ISnapshot> snapshot;
190
191 if (strcmp(a->argv[2], "-current") == 0)
192 {
193 CHECK_ERROR_BREAK(machine, COMGETTER(CurrentSnapshot)(snapshot.asOutParam()));
194 }
195 else
196 {
197 /* assume it's a UUID */
198 Guid guid(a->argv[2]);
199 if (!guid.isEmpty())
200 {
201 CHECK_ERROR_BREAK(machine, GetSnapshot(guid, snapshot.asOutParam()));
202 }
203 else
204 {
205 /* then it must be a name */
206 CHECK_ERROR_BREAK(machine, FindSnapshot(Bstr(a->argv[2]), snapshot.asOutParam()));
207 }
208 }
209
210 /* parse options */
211 for (int i = 3; i < a->argc; i++)
212 {
213 if (strcmp(a->argv[i], "-newname") == 0)
214 {
215 if (a->argc <= i + 1)
216 {
217 errorArgument("Missing argument to '%s'", a->argv[i]);
218 rc = E_FAIL;
219 break;
220 }
221 i++;
222 snapshot->COMSETTER(Name)(Bstr(a->argv[i]));
223 }
224 else if (strcmp(a->argv[i], "-newdesc") == 0)
225 {
226 if (a->argc <= i + 1)
227 {
228 errorArgument("Missing argument to '%s'", a->argv[i]);
229 rc = E_FAIL;
230 break;
231 }
232 i++;
233 snapshot->COMSETTER(Description)(Bstr(a->argv[i]));
234 }
235 else
236 {
237 errorSyntax(USAGE_SNAPSHOT, "Invalid parameter '%s'", Utf8Str(a->argv[i]).raw());
238 rc = E_FAIL;
239 break;
240 }
241 }
242
243 }
244 else if (strcmp(a->argv[1], "showvminfo") == 0)
245 {
246 /* exactly one parameter: snapshot name */
247 if (a->argc != 3)
248 {
249 errorSyntax(USAGE_SNAPSHOT, "Expecting snapshot name only");
250 rc = E_FAIL;
251 break;
252 }
253
254 ComPtr<ISnapshot> snapshot;
255
256 /* assume it's a UUID */
257 Guid guid(a->argv[2]);
258 if (!guid.isEmpty())
259 {
260 CHECK_ERROR_BREAK(machine, GetSnapshot(guid, snapshot.asOutParam()));
261 }
262 else
263 {
264 /* then it must be a name */
265 CHECK_ERROR_BREAK(machine, FindSnapshot(Bstr(a->argv[2]), snapshot.asOutParam()));
266 }
267
268 /* get the machine of the given snapshot */
269 ComPtr<IMachine> machine;
270 snapshot->COMGETTER(Machine)(machine.asOutParam());
271 showVMInfo(a->virtualBox, machine, VMINFO_NONE, console);
272 }
273 else
274 {
275 errorSyntax(USAGE_SNAPSHOT, "Invalid parameter '%s'", Utf8Str(a->argv[1]).raw());
276 rc = E_FAIL;
277 }
278 } while (0);
279
280 a->session->Close();
281
282 return SUCCEEDED(rc) ? 0 : 1;
283}
284
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