zig libvirt: fix main somewhat

This commit is contained in:
Jeeves 2024-06-25 14:36:22 -06:00
parent 55652c0c0b
commit 02bd0b6ccd
3 changed files with 65 additions and 56 deletions

View file

@ -2,8 +2,11 @@ const std = @import("std");
const mem = std.mem; const mem = std.mem;
const heap = std.heap; const heap = std.heap;
const h = @import("libvirt-helper.zig");
const c = @import("libvirt-c.zig").c; const c = @import("libvirt-c.zig").c;
const err = @import("libvirt-error.zig"); const err = @import("libvirt-error.zig");
const Domain = @import("libvirt-domain.zig");
const Pool = @import("libvirt-pool.zig");
const VirError = err.VirError; const VirError = err.VirError;
const Connection = @This(); const Connection = @This();
@ -22,15 +25,15 @@ pub fn deinit(self: *const Connection) void {
_ = c.virConnectClose(self.ptr); _ = c.virConnectClose(self.ptr);
} }
// pub fn getURI(self: *const Connection) ![]u8 { pub fn getURI(self: *const Connection) ![]u8 {
// const uri = c.virConnectGetURI(self.ptr); const uri = c.virConnectGetURI(self.ptr);
// defer std.c.free(uri); defer std.c.free(uri);
// const str = string(uri); const str = h.string(uri);
// return if (str.len == 0) handleError() else try self.allocator.dupe(u8, str); return if (str.len == 0) err.handleError() else try self.allocator.dupe(u8, str);
// } }
// pub fn freeURI(self: *const Connection, uri: []u8) void { pub fn freeURI(self: *const Connection, uri: []u8) void {
// self.allocator.free(uri); self.allocator.free(uri);
// } }
// pub fn numOfPools(self: *const Connection) !u32 { // pub fn numOfPools(self: *const Connection) !u32 {
// return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfStoragePools); // return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfStoragePools);
@ -60,12 +63,12 @@ pub fn deinit(self: *const Connection) void {
// return if (pool) |p| Pool.init(p, self.allocator) else handleError(); // return if (pool) |p| Pool.init(p, self.allocator) else handleError();
// } // }
// pub fn numOfDomains(self: *const Connection) !u32 { pub fn numOfDomains(self: *const Connection) VirError!u32 {
// return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDomains); return Domain.numOfDomains(self);
// } }
// pub fn numOfDefinedDomains(self: *const Connection) !u32 { pub fn numOfDefinedDomains(self: *const Connection) VirError!u32 {
// return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDefinedDomains); return Domain.numOfDefinedDomains(self);
// } }
// pub fn iterateDomains(self: *const Connection, flags: []const Domain.ListFlags) !Domain.DomainIterator { // pub fn iterateDomains(self: *const Connection, flags: []const Domain.ListFlags) !Domain.DomainIterator {
// return Domain.DomainIterator.init( // return Domain.DomainIterator.init(

View file

@ -32,8 +32,14 @@ pub fn getDomainCapabilities() void {}
pub fn listAllDomains() void {} pub fn listAllDomains() void {}
pub fn listDefinedDomains() void {} pub fn listDefinedDomains() void {}
pub fn listDomains() void {} pub fn listDomains() void {}
pub fn numOfDefinedDomains() void {} pub fn numOfDefinedDomains(conn: *const Connection) VirError!u32 {
pub fn numOfDomains() void {} const n = c.virConnectNumOfDomains(conn.ptr);
return if (n < 0) err.handleError() else @intCast(n);
}
pub fn numOfDomains(conn: *const Connection) VirError!u32 {
const n = c.virConnectNumOfDomains(conn.ptr);
return if (n < 0) err.handleError() else @intCast(n);
}
pub fn abortJob() void {} pub fn abortJob() void {}
pub fn abortJobFlags() void {} pub fn abortJobFlags() void {}

View file

@ -10,8 +10,8 @@ pub fn main() !void {
defer arena.deinit(); defer arena.deinit();
const allocator = arena.allocator(); const allocator = arena.allocator();
const connection = try libvirt.Connection.connect("qemu:///system", allocator); const connection = try libvirt.Connection.init("qemu:///system", allocator);
defer connection.close(); defer connection.deinit();
var flake_dir = try fs.cwd().openDir("flakes/windows/test", .{}); var flake_dir = try fs.cwd().openDir("flakes/windows/test", .{});
defer flake_dir.close(); defer flake_dir.close();
@ -42,52 +42,52 @@ pub fn main() !void {
const uri = try connection.getURI(); const uri = try connection.getURI();
defer connection.freeURI(uri); defer connection.freeURI(uri);
// std.debug.print("uri: {s}\n", .{uri}); std.debug.print("uri: {s}\n", .{uri});
const num_active = try connection.numOfDomains(); const num_active = try connection.numOfDomains();
const num_inactive = try connection.numOfDefinedDomains(); const num_inactive = try connection.numOfDefinedDomains();
// std.debug.print("active: {d}, inactive: {d}\n", .{ num_active, num_inactive }); std.debug.print("active: {d}, inactive: {d}\n", .{ num_active, num_inactive });
_ = .{ num_active, num_inactive }; _ = .{ num_active, num_inactive };
var domain_iter = try connection.iterateDomains(&[_]libvirt.Domain.ListFlags{ // var domain_iter = try connection.iterateDomains(&[_]libvirt.Domain.ListFlags{
libvirt.Domain.ListFlags.Active, // libvirt.Domain.ListFlags.Active,
libvirt.Domain.ListFlags.Inactive, // libvirt.Domain.ListFlags.Inactive,
}); // });
defer domain_iter.deinit(); // defer domain_iter.deinit();
while (domain_iter.next()) |domain| { // while (domain_iter.next()) |domain| {
const active = domain.isActive(); // const active = domain.isActive();
const name = domain.getName(); // const name = domain.getName();
// std.debug.print("name: {s}, active: {any}\n", .{ name, active }); // // std.debug.print("name: {s}, active: {any}\n", .{ name, active });
_ = .{ name, active }; // _ = .{ name, active };
} // }
var pool_iter = try connection.iteratePools(&[_]libvirt.Pool.ListFlags{ // var pool_iter = try connection.iteratePools(&[_]libvirt.Pool.ListFlags{
libvirt.Pool.ListFlags.Active, // libvirt.Pool.ListFlags.Active,
libvirt.Pool.ListFlags.Inactive, // libvirt.Pool.ListFlags.Inactive,
}); // });
defer pool_iter.deinit(); // defer pool_iter.deinit();
while (pool_iter.next()) |pool| { // while (pool_iter.next()) |pool| {
const name = try pool.getName(); // const name = try pool.getName();
// std.debug.print("name: {s}\n", .{name}); // // std.debug.print("name: {s}\n", .{name});
if (mem.eql(u8, name, "default")) { // if (mem.eql(u8, name, "default")) {
const vol_str = try libvirt.String.init(allocator, volume_xml); // const vol_str = try libvirt.String.init(allocator, volume_xml);
defer vol_str.deinit(); // defer vol_str.deinit();
const volume = pool.createVolume(vol_str, &[_]libvirt.Pool.Volume.CreateFlags{}) catch |err| blk: { // const volume = pool.createVolume(vol_str, &[_]libvirt.Pool.Volume.CreateFlags{}) catch |err| blk: {
if (err == libvirt.VirError.StorageVolExist) { // if (err == libvirt.VirError.StorageVolExist) {
break :blk try pool.lookupVolumeByName("wintest.qcow2"); // break :blk try pool.lookupVolumeByName("wintest.qcow2");
} else return err; // } else return err;
}; // };
_ = volume; // _ = volume;
break; // break;
} // }
} // }
const dom_str = try libvirt.String.init(allocator, domain_beforeinstall_xml); // const dom_str = try libvirt.String.init(allocator, domain_beforeinstall_xml);
defer dom_str.deinit(); // defer dom_str.deinit();
const domain = try connection.createDomain(dom_str, &[_]libvirt.Domain.CreateFlags{}); // const domain = try connection.createDomain(dom_str, &[_]libvirt.Domain.CreateFlags{});
_ = domain; // _ = domain;
} }
pub const DomainSpec = struct { pub const DomainSpec = struct {