zig libvirt: implement lotsa functions

This commit is contained in:
Jeeves 2024-06-25 12:46:03 -06:00
parent 60364765be
commit bbff1add94

View file

@ -340,75 +340,75 @@ pub const Connection = struct {
_ = 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 = string(uri);
return if (str.len == 0) handleError() else try self.allocator.dupe(u8, str); // return if (str.len == 0) 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);
} // }
pub fn numOfDefinedPools(self: *const Connection) !u32 { // pub fn numOfDefinedPools(self: *const Connection) !u32 {
return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDefinedStoragePools); // return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDefinedStoragePools);
} // }
pub fn iteratePools(self: *const Connection, flags: []const Pool.ListFlags) !Pool.PoolIterator { // pub fn iteratePools(self: *const Connection, flags: []const Pool.ListFlags) !Pool.PoolIterator {
return Pool.PoolIterator.init( // return Pool.PoolIterator.init(
c.virConnect, // c.virConnect,
Connection, // Connection,
Pool.ListFlags, // Pool.ListFlags,
self, // self,
flags, // flags,
self.allocator, // self.allocator,
c.virConnectListAllStoragePools, // c.virConnectListAllStoragePools,
); // );
} // }
pub fn createPool(self: *const Connection, xml: String, flags: []const Pool.CreateFlags) !Pool { // pub fn createPool(self: *const Connection, xml: String, flags: []const Pool.CreateFlags) !Pool {
const pool = c.virStoragePoolCreateXML(self.ptr, @ptrCast(xml.str), intFromFlags(Pool.CreateFlags, flags)); // const pool = c.virStoragePoolCreateXML(self.ptr, @ptrCast(xml.str), intFromFlags(Pool.CreateFlags, flags));
return if (pool) |p| Pool.init(p, self.allocator) else handleError(); // return if (pool) |p| Pool.init(p, self.allocator) else handleError();
} // }
pub fn definePoolFlags(self: *const Connection, xml: String, flags: []const Pool.DefineFlags) !Pool { // pub fn definePoolFlags(self: *const Connection, xml: String, flags: []const Pool.DefineFlags) !Pool {
const pool = c.virStoragePoolCreateXML(self.ptr, @ptrCast(xml.str), intFromFlags(Pool.DefineFlags, flags)); // const pool = c.virStoragePoolCreateXML(self.ptr, @ptrCast(xml.str), intFromFlags(Pool.DefineFlags, flags));
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) !u32 {
return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDomains); // return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDomains);
} // }
pub fn numOfDefinedDomains(self: *const Connection) !u32 { // pub fn numOfDefinedDomains(self: *const Connection) !u32 {
return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDefinedDomains); // return numOf(c.virConnectPtr, self.ptr, c.virConnectNumOfDefinedDomains);
} // }
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(
c.virConnect, // c.virConnect,
Connection, // Connection,
Domain.ListFlags, // Domain.ListFlags,
self, // self,
flags, // flags,
self.allocator, // self.allocator,
c.virConnectListAllDomains, // c.virConnectListAllDomains,
); // );
} // }
pub fn createDomain(self: *const Connection, xml: String, flags: []const Domain.CreateFlags) !Domain { // pub fn createDomain(self: *const Connection, xml: String, flags: []const Domain.CreateFlags) !Domain {
const domain = c.virDomainCreateXML(self.ptr, @ptrCast(xml.str), intFromFlags(Domain.CreateFlags, flags)); // const domain = c.virDomainCreateXML(self.ptr, @ptrCast(xml.str), intFromFlags(Domain.CreateFlags, flags));
return if (domain) |d| Domain.init(d, self.allocator) else handleError(); // return if (domain) |d| Domain.init(d, self.allocator) else handleError();
} // }
pub fn defineDomain(self: *const Connection, xml: String) !Domain { // pub fn defineDomain(self: *const Connection, xml: String) !Domain {
const domain = c.virDomainDefineXML(self.ptr, @ptrCast(xml.str)); // const domain = c.virDomainDefineXML(self.ptr, @ptrCast(xml.str));
return if (domain) |d| Domain.init(d, self.allocator) else handleError(); // return if (domain) |d| Domain.init(d, self.allocator) else handleError();
} // }
pub fn defineDomainFlags(self: *const Connection, xml: String, flags: []const Domain.DefineFlags) !Domain { // pub fn defineDomainFlags(self: *const Connection, xml: String, flags: []const Domain.DefineFlags) !Domain {
const domain = c.virDomainDefineXMLFlags(self.ptr, @ptrCast(xml.str), intFromFlags(Domain.DefineFlags, flags)); // const domain = c.virDomainDefineXMLFlags(self.ptr, @ptrCast(xml.str), intFromFlags(Domain.DefineFlags, flags));
return if (domain) |d| Domain.init(d, self.allocator) else handleError(); // return if (domain) |d| Domain.init(d, self.allocator) else handleError();
} // }
}; };
pub const Pool = struct { pub const Pool = struct {
@ -426,51 +426,104 @@ pub const Pool = struct {
self.arena.deinit(); self.arena.deinit();
} }
pub fn findStoragePoolSources() void {} pub fn findStoragePoolSources(
pub fn getStoragePoolCapabilities() void {} conn: *const Connection,
pub fn listAllStoragePools() void {} type: []const u8,
pub fn listDefinedStoragePools() void {} srcSpec: []const u8,
pub fn listStoragePools() void {} flags: []const Flags,
pub fn numOfDefinedStoragePools() void {} ) VirError![]u8 {} // TODO
pub fn numOfStoragePools() void {} pub fn getStoragePoolCapabilities(
conn: *const Connection,
flags: []const Flags,
) VirError![]u8 {} // TODO
pub fn listAllStoragePools(
conn: *const Connection,
pools: [*]c.virStoragePoolPtr,
flags: []const ListFlags,
) VirError![]const c.virStoragePoolPtr {
const num = c.virConnectListAllStoragePools(
conn.ptr,
@ptrCast(&pools),
intFromFlags(ListFlags, flags),
);
return if (num < 0) handleError() else pools[0..num];
}
pub fn listDefinedStoragePools(
conn: *const Connection,
names: [*]const []const u8,
maxnames: u32,
) VirError!u32 {} // TODO
pub fn listStoragePools(
conn: *const Connection,
names: [*]const []const u8,
maxnames: u32,
) VirError!u32 {} // TODO
pub fn numOfDefinedStoragePools(conn: *const Connection) VirError!u32 {}
pub fn numOfStoragePools(conn: *const Connection) VirError!u32 {}
// TODO event handling callbacks // TODO event handling callbacks
pub fn build() void {} pub fn build(self: *const Pool) VirError!void {} // TODO
pub fn create() void {} pub fn create(conn: *const Connection) VirError!void {} // TODO
pub fn createXML() void {} pub fn createXML(conn: *const Connection) VirError!void {} // TODO
pub fn defineXML() void {} pub fn defineXML(conn: *const Connection) VirError!void {} // TODO
pub fn delete() void {} pub fn delete(self: *const Pool) VirError!void {} // TODO
pub fn destroy() void {} pub fn destroy(self: *const Pool) VirError!void {} // TODO
pub fn free() void {} pub fn free(self: *const Pool) VirError!void {
if (c.virStoragePoolFree(self.ptr) < 0) return handleError();
}
pub fn getAutostart() void {} pub fn getAutostart(self: *const Pool, autostart: *c_int) VirError!void {
pub fn getConnect() void {} if (c.virStoragePoolGetAutostart(self.ptr, autostart) < 0) return handleError();
pub fn getInfo() void {} }
pub fn getConnect(self: *const Pool) VirError!c.virConnectPtr {
return if (c.virStoragePoolGetConnect(self.ptr)) |ptr| ptr else handleError();
}
pub fn getInfo(self: *const Pool, info: c.virStoragePoolInfoPtr) VirError!void {
if (c.virStoragePoolGetInfo(self.ptr, info) < 0) return handleError();
}
pub fn getName(self: *const Pool) VirError![]const u8 { pub fn getName(self: *const Pool) VirError![]const u8 {
const name = c.virStoragePoolGetName(self.ptr); const name = c.virStoragePoolGetName(self.ptr);
const str = string(name); const str = string(name);
return if (str.len == 0) handleError() else str; return if (str.len == 0) handleError() else str;
} }
pub fn getUUID() void {} pub fn getUUID(self: *const Pool) VirError!void {} // TODO
pub fn getUUIDString() void {} pub fn getUUIDString(self: *const Pool) VirError!void {} // TODO
pub fn getXMLDesc() void {} pub fn getXMLDesc(self: *const Pool, flags: []const XMLFlags) VirError![]const u8 {
const desc = c.virStoragePoolGetXMLDesc(self.ptr, intFromFlags(XMLFlags, flags));
const str = string(desc);
return if (str.len == 0) handleError() else str;
}
pub fn isActive() void {} pub fn isActive(self: *const Pool) VirError!bool {
pub fn isPersistent() void {} return switch (c.virStoragePoolIsActive(self.ptr)) {
pub fn listAllVolumes() void {} 0 => false,
pub fn listVolumes() void {} 1 => true,
else => handleError(),
};
}
pub fn isPersistent(self: *const Pool) VirError!bool {
return switch (c.virStoragePoolIsPersistent(self.ptr)) {
0 => false,
1 => true,
else => handleError(),
};
}
pub fn listAllVolumes(self: *const Pool) void {} // TODO
pub fn listVolumes(self: *const Pool) void {} // TODO
pub fn lookupByName() void {} pub fn lookupByName() void {} // TODO
pub fn lookupByTargetPath() void {} pub fn lookupByTargetPath() void {} // TODO
pub fn lookupByUUID() void {} pub fn lookupByUUID() void {} // TODO
pub fn lookupByUUIDString() void {} pub fn lookupByUUIDString() void {} // TODO
pub fn lookupByVolume() void {} pub fn lookupByVolume() void {} // TODO
pub fn numOfVolumes() void {} pub fn numOfVolumes() void {} // TODO
pub fn ref() void {} pub fn ref(self: *const Pool) VirError!void {
pub fn refresh() void {} if (c.virStoragePoolRef(self.ptr) < 0) return handleError();
pub fn setAutostart() void {} }
pub fn undefine() void {} pub fn refresh() void {} // TODO
pub fn setAutostart() void {} // TODO
pub fn undefine() void {} // TODO
// pub fn createVolume(self: *const Pool, xml: String, flags: []const Volume.CreateFlags) !Volume { // 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)); // const volume = c.virStorageVolCreateXML(self.ptr, @ptrCast(xml.str), intFromFlags(Volume.CreateFlags, flags));
@ -494,6 +547,19 @@ pub const Pool = struct {
// ); // );
// } // }
pub fn lookupVolumeByKey(self: *const Pool, key: []const u8) VirError!Volume {
const volume = Volume.lookupByKey(self, key);
return if (volume) |v| v else handleError();
}
pub fn lookupVolumeByName(self: *const Pool, name: []const u8) VirError!Volume {
const volume = Volume.lookupByName(self, name);
return if (volume) |v| v else handleError();
}
pub fn lookupVolumeByPath(self: *const Pool, path: []const u8) VirError!Volume {
const volume = Volume.lookupByPath(self, path);
return if (volume) |v| v else handleError();
}
pub const ListFlags = enum(c_uint) { pub const ListFlags = enum(c_uint) {
Inactive = c.VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE, Inactive = c.VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE,
Active = c.VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE, Active = c.VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE,
@ -520,6 +586,9 @@ pub const Pool = struct {
pub const CreateFlags = enum(c_uint) {}; pub const CreateFlags = enum(c_uint) {};
pub const DefineFlags = enum(c_uint) {}; pub const DefineFlags = enum(c_uint) {};
pub const DeleteFlags = enum(c_uint) {}; pub const DeleteFlags = enum(c_uint) {};
pub const XMLFlags = enum(c_uint) {
Inactive = c.VIR_STORAGE_XML_INACTIVE,
};
pub const Info = struct { pub const Info = struct {
state: State, state: State,
@ -568,29 +637,59 @@ pub const Pool = struct {
self.arena.deinit(); self.arena.deinit();
} }
pub fn createXML() void {} pub fn createXML(
pub fn createXMLFrom() void {} pool: *const Pool,
pub fn delete() void {} xml: String,
pub fn download() void {} flags: []const CreateFlags,
pub fn free() void {} ) void {} // TODO
pub fn createXMLFrom(
pool: *const Pool,
xml: String,
flags: []const CreateFlags,
) void {} // TODO
pub fn delete(self: *const Volume, flags: []const DeleteFlags) VirError!void {
if (c.virStorageVolDelete(self.ptr, intFromFlags(DeleteFlags, flags)) < 0)
return handleError();
}
pub fn download(self: *const Volume) VirError!void {} // TODO
pub fn free(self: *const Volume) VirError!void {
if (c.virStorageVolFree(self.ptr) < 0) return handleError();
}
pub fn getConnect() void {} pub fn getConnect(self: *const Volume) VirError!c.virConnectPtr {
pub fn getInfo() void {} return if (c.virStorageVolGetConnect(self.ptr)) |c| c else handleError();
pub fn getInfoFlags() void {} }
pub fn getKey() void {} pub fn getInfo(self: *const Volume) void {} // TODO
pub fn getName() void {} pub fn getInfoFlags(self: *const Volume) void {} // TODO
pub fn getPath() void {} pub fn getKey(self: *const Volume) VirError![]const u8 {
pub fn getXMLDesc() void {} return if (c.virStorageVolGetKey(self.ptr)) |v| v else handleError();
}
pub fn getName(self: *const Volume) VirError![]const u8 {
return if (c.virStorageVolGetName(self.ptr)) |v| v else handleError();
}
pub fn getPath(self: *const Volume) VirError![]const u8 {
// TODO use String?
return if (c.virStorageVolGetPath(self.ptr)) |v| v else handleError();
}
pub fn getXMLDesc(self: *const Volume) void {} // TODO
pub fn lookupByKey() void {} pub fn lookupByKey(conn: *const Connection, key: []const u8) VirError!Volume {
pub fn lookupByName() void {} return if (c.virStorageVolLookupByKey(conn.ptr, key)) |v| v else handleError();
pub fn lookupByPath() void {} }
pub fn lookupByName(conn: *const Connection, name: []const u8) VirError!Volume {
return if (c.virStorageVolLookupByName(conn.ptr, name)) |v| v else handleError();
}
pub fn lookupByPath(conn: *const Connection, path: []const u8) VirError!Volume {
return if (c.virStorageVolLookupByPath(conn.ptr, path)) |v| v else handleError();
}
pub fn ref() void {} pub fn ref(self: *const Volume) VirError!void {
pub fn resize() void {} if (c.virStorageVolRef(self.ptr) < 0) return handleError();
pub fn upload() void {} }
pub fn wipe() void {} pub fn resize(self: *const Volume) void {} // TODO
pub fn wipePattern() void {} pub fn upload(self: *const Volume) void {} // TODO
pub fn wipe(self: *const Volume) void {} // TODO
pub fn wipePattern(self: *const Volume) void {} // TODO
pub const ListFlags = enum(c_uint) {}; pub const ListFlags = enum(c_uint) {};
pub const CreateFlags = enum(c_uint) { pub const CreateFlags = enum(c_uint) {
@ -716,7 +815,9 @@ pub const Domain = struct {
pub fn fsInfoFree() void {} pub fn fsInfoFree() void {}
pub fn fsThaw() void {} pub fn fsThaw() void {}
pub fn fsTrim() void {} pub fn fsTrim() void {}
pub fn free() void {} pub fn free(self: *const Domain) VirError!void {
if (c.virDomainFree(self.ptr) < 0) return handleError();
}
pub fn getAutostart() void {} pub fn getAutostart() void {}
pub fn getBlkioParameters() void {} pub fn getBlkioParameters() void {}
@ -806,7 +907,9 @@ pub const Domain = struct {
pub fn pinVcpuFlags() void {} pub fn pinVcpuFlags() void {}
pub fn reboot() void {} pub fn reboot() void {}
pub fn ref() void {} pub fn ref(self: *const Domain) VirError!void {
if (c.virDomainRef(self.ptr) < 0) return handleError();
}
pub fn rename() void {} pub fn rename() void {}
pub fn reset() void {} pub fn reset() void {}
pub fn restore() void {} pub fn restore() void {}
@ -925,7 +1028,9 @@ pub const Domain = struct {
pub fn current() void {} pub fn current() void {}
pub fn delete() void {} pub fn delete() void {}
pub fn free() void {} pub fn free(self: *const Snapshot) VirError!void {
if (c.virDomainSnapshotFree(self.ptr) < 0) return handleError();
}
pub fn getConnect() void {} pub fn getConnect() void {}
pub fn getDomain() void {} pub fn getDomain() void {}
pub fn getName() void {} pub fn getName() void {}
@ -941,7 +1046,9 @@ pub const Domain = struct {
pub fn lookupByName() void {} pub fn lookupByName() void {}
pub fn num() void {} pub fn num() void {}
pub fn numChildren() void {} pub fn numChildren() void {}
pub fn ref() void {} pub fn ref(self: *const Snapshot) VirError!void {
if (c.virDomainSnapshotRef(self.ptr) < 0) return handleError();
}
pub const ListFlags = enum(c_uint) { pub const ListFlags = enum(c_uint) {
Descendants = c.VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS, Descendants = c.VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS,