From 2362114b60076d4c322a7a10c6c84f2f9d7fe8e0 Mon Sep 17 00:00:00 2001
From: Jeeves <guydoodlesdev@gmail.com>
Date: Tue, 25 Jun 2024 14:02:17 -0600
Subject: [PATCH] zig libvirt: define almost everything in Pool

---
 server/src/libvirt-error.zig  |  2 +-
 server/src/libvirt-helper.zig |  4 +-
 server/src/libvirt-pool.zig   | 75 +++++++++++++++++++++++++++--------
 3 files changed, 62 insertions(+), 19 deletions(-)

diff --git a/server/src/libvirt-error.zig b/server/src/libvirt-error.zig
index b35bb3e..d08b76f 100644
--- a/server/src/libvirt-error.zig
+++ b/server/src/libvirt-error.zig
@@ -2,7 +2,7 @@ const std = @import("std");
 
 const c = @import("libvirt-c.zig").c;
 
-fn handleError() VirError {
+pub fn handleError() VirError {
     const err = c.virGetLastError();
     // std.debug.print("err: {any}\n", .{err});
     return switch (err.*.code) {
diff --git a/server/src/libvirt-helper.zig b/server/src/libvirt-helper.zig
index 256ed0c..7f9c40a 100644
--- a/server/src/libvirt-helper.zig
+++ b/server/src/libvirt-helper.zig
@@ -24,11 +24,11 @@ pub const String = struct {
     }
 };
 
-fn string(str: [*c]const u8) []const u8 {
+pub fn string(str: [*c]const u8) []const u8 {
     return mem.span(str);
 }
 
-fn intFromFlags(comptime T: type, flags: []const T) c_uint {
+pub fn intFromFlags(comptime T: type, flags: []const T) c_uint {
     var flags_int: c_uint = 0;
     for (flags) |f| flags_int |= @intFromEnum(f);
     return flags_int;
diff --git a/server/src/libvirt-pool.zig b/server/src/libvirt-pool.zig
index a635a37..55edde0 100644
--- a/server/src/libvirt-pool.zig
+++ b/server/src/libvirt-pool.zig
@@ -60,12 +60,18 @@ pub fn listAllStoragePools(
 // pub fn numOfStoragePools(conn: *const Connection) VirError!u32 {}
 // TODO event handling callbacks
 
-// pub fn build(self: *const Pool) VirError!void {} // TODO
+pub fn build(self: *const Pool, flags: []const BuildFlags) VirError!void {
+    if (c.virStoragePoolBuild(self.ptr, h.intFromFlags(BuildFlags, flags)) < 0) return err.handleError();
+}
 // pub fn create(conn: *const Connection) VirError!void {} // TODO
 // pub fn createXML(conn: *const Connection) VirError!void {} // TODO
 // pub fn defineXML(conn: *const Connection) VirError!void {} // TODO
-// pub fn delete(self: *const Pool) VirError!void {} // TODO
-// pub fn destroy(self: *const Pool) VirError!void {} // TODO
+pub fn delete(self: *const Pool, flags: []const DeleteFlags) VirError!void {
+    if (c.virStoragePoolDelete(self.ptr, h.intFromFlags(DeleteFlags, flags)) < 0) return err.handleError();
+}
+pub fn destroy(self: *const Pool) VirError!void {
+    if (c.virStoragePoolDestroy(self.ptr) < 0) return err.handleError();
+}
 pub fn free(self: *const Pool) VirError!void {
     if (c.virStoragePoolFree(self.ptr) < 0) return err.handleError();
 }
@@ -109,19 +115,39 @@ pub fn isPersistent(self: *const Pool) VirError!bool {
 // pub fn listAllVolumes(self: *const Pool) void {} // TODO
 // pub fn listVolumes(self: *const Pool) void {} // TODO
 
-pub fn lookupByName() void {} // TODO
-pub fn lookupByTargetPath() void {} // TODO
-pub fn lookupByUUID() void {} // TODO
-pub fn lookupByUUIDString() void {} // TODO
-pub fn lookupByVolume() void {} // TODO
+pub fn lookupByName(conn: *const Connection, name: []const u8) VirError!Pool {
+    if (c.vir(conn.ptr, name)) |p| p else err.handleError();
+}
+pub fn lookupByTargetPath(conn: *const Connection, path: []const u8) VirError!Pool {
+    if (c.vir(conn.ptr, path)) |p| p else err.handleError();
+}
+pub fn lookupByUUID(conn: *const Connection, uuid: []const u8) VirError!Pool {
+    if (c.vir(conn.ptr, uuid)) |p| p else err.handleError();
+}
+pub fn lookupByUUIDString(conn: *const Connection, uuid: []const u8) VirError!Pool {
+    if (c.vir(conn.ptr, uuid)) |p| p else err.handleError();
+}
+pub fn lookupByVolume(vol: *const Volume) VirError!Pool {
+    if (c.vir(vol.ptr)) |p| p else err.handleError();
+}
 
-pub fn numOfVolumes() void {} // TODO
+pub fn numOfVolumes(self: *const Pool) VirError!u32 {
+    const num = c.virStoragePoolNumOfVolumes(self.ptr);
+    return if (num < 0) err.handleError() else num;
+}
 pub fn ref(self: *const Pool) VirError!void {
     if (c.virStoragePoolRef(self.ptr) < 0) return err.handleError();
 }
-pub fn refresh() void {} // TODO
-pub fn setAutostart() void {} // TODO
-pub fn undefine() void {} // TODO
+pub fn refresh(self: *const Pool, flags: []const RefreshFlags) VirError!void {
+    if (c.virStoragePoolRefresh(self.ptr, h.intFromFlags(RefreshFlags, flags)) < 0) return err.handleError();
+}
+pub fn setAutostart(self: *const Pool, autostart: bool) VirError!void {
+    const i = if (autostart) 1 else 0;
+    if (c.virStoragePoolSetAutostart(self.ptr, i) < 0) return err.handleError();
+}
+pub fn undefine(self: *const Pool) VirError!void {
+    if (c.virStoragePoolUndefine(self.ptr) < 0) return err.handleError();
+}
 
 // pub fn createVolume(self: *const Pool, xml: String, flags: []const Volume.CreateFlags) !Volume {
 //     const volume = c.virStorageVolCreateXML(self.ptr, @ptrCast(xml.str), intFromFlags(Volume.CreateFlags, flags));
@@ -180,10 +206,27 @@ pub const ListFlags = enum(c_uint) {
     Vstorage = c.VIR_CONNECT_LIST_STORAGE_POOLS_VSTORAGE,
     IscsiDirect = c.VIR_CONNECT_LIST_STORAGE_POOLS_ISCSI_DIRECT,
 };
-pub const BuildFlags = enum(c_uint) {};
-pub const CreateFlags = enum(c_uint) {};
-pub const DefineFlags = enum(c_uint) {};
-pub const DeleteFlags = enum(c_uint) {};
+pub const BuildFlags = enum(c_uint) {
+    New = c.VIR_STORAGE_POOL_BUILD_NEW,
+    Repair = c.VIR_STORAGE_POOL_BUILD_REPAIR,
+    Resize = c.VIR_STORAGE_POOL_BUILD_RESIZE,
+    NoOverwrite = c.VIR_STORAGE_POOL_BUILD_NO_OVERWRITE,
+    Overwrite = c.VIR_STORAGE_POOL_BUILD_OVERWRITE,
+};
+pub const CreateFlags = enum(c_uint) {
+    Normal = c.VIR_STORAGE_POOL_CREATE_NORMAL,
+    WithBuild = c.VIR_STORAGE_POOL_CREATEWITH_BUILD_,
+    WithBuildOverwrite = c.VIR_STORAGE_POOL_CREATE_WITH_BUILD_OVERWRITE,
+    WithBuildNoOverwrite = c.VIR_STORAGE_POOL_CREATE_WITH_BUILD_NO_OVERWRITE,
+};
+pub const DefineFlags = enum(c_uint) {
+    Validate = c.VIR_STORAGE_POOL_DEFINE_VALIDATE,
+};
+pub const DeleteFlags = enum(c_uint) {
+    Normal = c.VIR_STORAGE_POOL_DELETE_NORMAL,
+    Zeroed = c.VIR_STORAGE_POOL_DELETE_ZEROED,
+};
+pub const RefreshFlags = enum(c_uint) {};
 pub const XMLFlags = enum(c_uint) {
     Inactive = c.VIR_STORAGE_XML_INACTIVE,
 };