Skip to content

Commit dcb1fb4

Browse files
authored
Implement virGetVersion (#2)
1 parent 09807c9 commit dcb1fb4

File tree

5 files changed

+77
-35
lines changed

5 files changed

+77
-35
lines changed

src/libvirt/Connect.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public Connect(string uri)
1919

2020
public string Uri { get; }
2121

22-
public bool IsOpen { get; set; }
22+
public bool IsOpen { get; private set; }
2323

2424
public void Open(bool readOnly = false)
2525
{
2626
if (IsDisposed)
2727
{
28-
throw new ObjectDisposedException("Cannot open a disposed Connect");
28+
throw new ObjectDisposedException(null, "Cannot open a disposed Connect.");
2929
}
3030

3131
if (readOnly)
@@ -51,15 +51,15 @@ public void Close(bool ignoreErrors = false)
5151

5252
int result = Libvirt.virConnectClose(_conn);
5353

54-
if (!ignoreErrors)
54+
if (!ignoreErrors)
5555
{
5656
ThrowExceptionOnError(result);
5757
}
5858

5959
IsOpen = false;
6060
}
6161

62-
public List<Domain> GetDomains(virConnectListAllDomainsFlags flags = default(virConnectListAllDomainsFlags))
62+
public List<Domain> GetDomains(virConnectListAllDomainsFlags flags = default)
6363
{
6464
int result = Libvirt.virConnectListAllDomains(_conn, out IntPtr ptrDomains, flags);
6565

@@ -95,7 +95,7 @@ public Domain CreateDomain(string xml)
9595

9696
public string Type => GetString(() => Libvirt.virConnectGetType(_conn));
9797

98-
private new string GetString(Func<string> func)
98+
protected override string GetString(Func<string> func)
9999
{
100100
if (!IsOpen)
101101
{

src/libvirt/Libvirt.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace libvirt
66
{
7-
public class Libvirt
7+
public static class Libvirt
88
{
99
public const string Name = "libvirt";
1010

@@ -34,6 +34,25 @@ private static IntPtr ImportResolver(string libraryName, Assembly assembly, DllI
3434

3535
public const int VIR_UUID_BUFLEN = 36;
3636

37+
public static Version GetVersion()
38+
{
39+
LibvirtHelper.ThrowExceptionOnError(virGetVersion(out ulong libVer, null, out _));
40+
41+
int release = (int) (libVer % 1000);
42+
int minor = (int) ((libVer % 1000000) / 1000);
43+
int major = (int) (libVer / 1000000);
44+
45+
return new Version(major, minor, release);
46+
47+
}
48+
49+
#region Library
50+
51+
[DllImport(Libvirt.Name, CallingConvention = CallingConvention.Cdecl, EntryPoint = "virGetVersion")]
52+
public static extern int virGetVersion([Out] out ulong libVer, [In] string type, [Out] out ulong typeVer);
53+
54+
#endregion
55+
3756
#region Connect
3857

3958
[DllImport(Libvirt.Name, CallingConvention = CallingConvention.Cdecl, EntryPoint = "virConnectOpen")]

src/libvirt/LibvirtHelper.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace libvirt
5+
{
6+
public static class LibvirtHelper
7+
{
8+
public static void ThrowExceptionOnError(int result) => ThrowExceptionOn(() => result == -1);
9+
10+
public static void ThrowExceptionOnError(IntPtr result) => ThrowExceptionOn(() => result == IntPtr.Zero);
11+
12+
public static void ThrowExceptionOnError(string result) => ThrowExceptionOn(() => result == null);
13+
14+
private static void ThrowExceptionOn(Func<bool> predicate)
15+
{
16+
if (predicate())
17+
{
18+
virError error = GetLastError();
19+
20+
if (error?.level == virErrorLevel.VIR_ERR_ERROR)
21+
{
22+
throw new LibvirtException(error);
23+
}
24+
}
25+
}
26+
27+
private static virError GetLastError()
28+
{
29+
IntPtr errPtr = Libvirt.virGetLastError();
30+
31+
return Marshal.PtrToStructure<virError>(errPtr);
32+
}
33+
}
34+
}

src/libvirt/LibvirtObject.cs

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected void EnsureObjectIsNotDisposed()
1616
}
1717
}
1818

19-
protected string GetString(Func<string> func)
19+
protected virtual string GetString(Func<string> func)
2020
{
2121
EnsureObjectIsNotDisposed();
2222

@@ -32,7 +32,7 @@ protected string GetUUID(Func<char[], int> func)
3232
EnsureObjectIsNotDisposed();
3333

3434
char[] uuid = new char[Libvirt.VIR_UUID_BUFLEN];
35-
35+
3636
var result = func(uuid);
3737

3838
ThrowExceptionOnError(result);
@@ -51,35 +51,11 @@ protected int GetInt32(Func<int> func)
5151
return result;
5252
}
5353

54-
protected void ThrowExceptionOnError(int result) => ThrowExceptionOn(() => result == -1);
55-
56-
protected void ThrowExceptionOnError(IntPtr result) => ThrowExceptionOn(() => result == IntPtr.Zero);
54+
protected void ThrowExceptionOnError(int result) => LibvirtHelper.ThrowExceptionOnError(result);
5755

58-
protected void ThrowExceptionOnError(string result) => ThrowExceptionOn(() => result == null);
56+
protected void ThrowExceptionOnError(IntPtr result) => LibvirtHelper.ThrowExceptionOnError(result);
5957

60-
private void ThrowExceptionOn(Func<bool> predicate)
61-
{
62-
if (predicate())
63-
{
64-
var error = GetLastError();
65-
66-
if (error != null && error.level == virErrorLevel.VIR_ERR_ERROR)
67-
{
68-
throw new LibvirtException(error);
69-
}
70-
}
71-
}
58+
protected void ThrowExceptionOnError(string result) => LibvirtHelper.ThrowExceptionOnError(result);
7259

73-
private virError GetLastError()
74-
{
75-
IntPtr errPtr = Libvirt.virGetLastError();
76-
77-
if (errPtr == IntPtr.Zero)
78-
{
79-
return null;
80-
}
81-
82-
return (virError) Marshal.PtrToStructure(errPtr, typeof(virError));
83-
}
8460
}
8561
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Xunit;
2+
3+
namespace libvirt.Tests
4+
{
5+
public class LibraryTests
6+
{
7+
[Fact]
8+
public void GetVersion_ReturnVersion()
9+
{
10+
Libvirt.GetVersion();
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)