using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Interop.VixCOM;
using System.IO;
namespace Vestris.VMWareLib
{
///
/// A collection of root snapshots.
///
///
/// Shared snapshots will only be accessible inside the guest operating system if snapshots are
/// enabled for the virtual machine.
///
public class VMWareRootSnapshotCollection : VMWareSnapshotCollection
{
///
/// A collection of snapshots that belong to a virtual machine.
///
/// A virtual machine instance.
public VMWareRootSnapshotCollection(IVM2 vm)
: base(vm, null)
{
}
///
/// A list of root snapshots on the current virtual machine.
///
///
/// The list is populated on first access, this may time some time.
///
/// A list of snapshots.
protected override List Snapshots
{
get
{
if (_snapshots == null)
{
List snapshots = new List();
int nSnapshots = 0;
VMWareInterop.Check(_vm.GetNumRootSnapshots(out nSnapshots));
for (int i = 0; i < nSnapshots; i++)
{
ISnapshot snapshot = null;
VMWareInterop.Check(_vm.GetRootSnapshot(i, out snapshot));
snapshots.Add(new VMWareSnapshot(_vm, snapshot, null));
}
_snapshots = snapshots;
}
return _snapshots;
}
}
///
/// Get a snapshot by its exact name.
///
/// Snapshot name.
/// A snapshot.
/// This function will throw an exception if more than one snapshot with the same exists or if the snapshot doesn't exist.
public VMWareSnapshot GetNamedSnapshot(string name)
{
ISnapshot snapshot = null;
ulong rc = _vm.GetNamedSnapshot(name, out snapshot);
switch (rc)
{
case Constants.VIX_OK:
return new VMWareSnapshot(_vm, snapshot, null);
default:
VMWareInterop.Check(rc);
break;
}
return null;
}
///
/// Current snapshot.
///
/// Current snapshot.
public VMWareSnapshot GetCurrentSnapshot()
{
ISnapshot snapshot = null;
VMWareInterop.Check(_vm.GetCurrentSnapshot(out snapshot));
return new VMWareSnapshot(_vm, snapshot, null);
}
///
/// Delete/remove a snapshot.
///
/// Snapshot to delete.
/// True if the snapshot was deleted.
public void RemoveSnapshot(VMWareSnapshot item)
{
RemoveSnapshot(item, VMWareInterop.Timeouts.RemoveSnapshotTimeout);
}
///
/// Delete/remove a snapshot.
///
/// Snapshot to delete.
/// Timeout in seconds.
/// True if the snapshot was deleted.
public void RemoveSnapshot(VMWareSnapshot item, int timeoutInSeconds)
{
item.RemoveSnapshot(timeoutInSeconds);
RemoveAll();
}
///
/// Delete a snapshot.
///
/// Name of the snapshot to delete.
public void RemoveSnapshot(string name)
{
RemoveSnapshot(GetNamedSnapshot(name));
}
///
/// Delete a snapshot.
///
/// Name of the snapshot to delete.
/// Timeout in seconds.
public void RemoveSnapshot(string name, int timeoutInSeconds)
{
RemoveSnapshot(GetNamedSnapshot(name), timeoutInSeconds);
}
///
/// Create a new snapshot, child of the current snapshot.
///
/// Snapshot name.
/// Snapshot description.
public VMWareSnapshot CreateSnapshot(string name, string description)
{
return CreateSnapshot(name, description, 0, VMWareInterop.Timeouts.CreateSnapshotTimeout);
}
///
/// Create a new snapshot, child of the current snapshot.
///
/// Snapshot name.
/// Snapshot description.
/// Flags, one of
///
/// - VIX_SNAPSHOT_INCLUDE_MEMORY: Captures the full state of a running virtual machine, including the memory
///
///
/// Timeout in seconds.
public VMWareSnapshot CreateSnapshot(string name, string description, int flags, int timeoutInSeconds)
{
try
{
VMWareJobCallback callback = new VMWareJobCallback();
using (VMWareJob job = new VMWareJob(_vm.CreateSnapshot(
name, description, flags, null, callback), callback))
{
ISnapshot snapshot = (ISnapshot)job.Wait(
Constants.VIX_PROPERTY_JOB_RESULT_HANDLE,
timeoutInSeconds);
RemoveAll();
return new VMWareSnapshot(_vm, snapshot, null);
}
}
catch (Exception ex)
{
throw new Exception(
string.Format("Failed to create snapshot: name=\"{0}\" description=\"{1}\" flags={2} timeoutInSeconds={3}",
name, description, flags, timeoutInSeconds), ex);
}
}
}
}