Preparatory step for caching nbdkit capabilities. This patch implements
the newData and isValid virFileCacheHandlers callback functions.
Signed-off-by: Jonathon Jongsma <jjongsma(a)redhat.com>
---
src/qemu/qemu_nbdkit.c | 91 ++++++++++++++++++++++++++++++++++++++++++
src/qemu/qemu_nbdkit.h | 3 ++
2 files changed, 94 insertions(+)
diff --git a/src/qemu/qemu_nbdkit.c b/src/qemu/qemu_nbdkit.c
index 0ea1b754cd..3faa16674a 100644
--- a/src/qemu/qemu_nbdkit.c
+++ b/src/qemu/qemu_nbdkit.c
@@ -263,3 +263,94 @@ qemuNbdkitCapsSet(qemuNbdkitCaps *nbdkitCaps,
{
ignore_value(virBitmapSetBit(nbdkitCaps->flags, flag));
}
+
+
+static bool
+virNbkditCapsCheckModdir(const char *moddir,
+ time_t expectedMtime)
+{
+ time_t mtime = getDirMtime(moddir);
+
+ if (mtime != expectedMtime) {
+ VIR_DEBUG("Outdated capabilities for nbdkit: module "
+ "directory '%s' changed (%lld vs %lld)",
+ moddir,
+ (long long)mtime, (long long)expectedMtime);
+ return false;
+ }
+ return true;
+}
+
+
+static bool
+virNbdkitCapsIsValid(void *data,
+ void *privData G_GNUC_UNUSED)
+{
+ qemuNbdkitCaps *nbdkitCaps = data;
+ struct stat st;
+
+ if (!nbdkitCaps->path)
+ return true;
+
+ if (!virNbkditCapsCheckModdir(NBDKIT_PLUGINDIR, nbdkitCaps->pluginDirMtime))
+ return false;
+ if (!virNbkditCapsCheckModdir(NBDKIT_FILTERDIR, nbdkitCaps->filterDirMtime))
+ return false;
+
+ if (nbdkitCaps->libvirtCtime != virGetSelfLastChanged() ||
+ nbdkitCaps->libvirtVersion != LIBVIR_VERSION_NUMBER) {
+ VIR_DEBUG("Outdated capabilities for '%s': libvirt changed "
+ "(%lld vs %lld, %lu vs %lu)",
+ nbdkitCaps->path,
+ (long long)nbdkitCaps->libvirtCtime,
+ (long long)virGetSelfLastChanged(),
+ (unsigned long)nbdkitCaps->libvirtVersion,
+ (unsigned long)LIBVIR_VERSION_NUMBER);
+ return false;
+ }
+
+ if (stat(nbdkitCaps->path, &st) < 0) {
+ VIR_DEBUG("Failed to stat nbdkit binary '%s': %s",
+ nbdkitCaps->path,
+ g_strerror(errno));
+ return false;
+ }
+
+ if (st.st_ctime != nbdkitCaps->ctime) {
+ VIR_DEBUG("Outdated capabilities for '%s': nbdkit binary changed
"
+ "(%lld vs %lld)",
+ nbdkitCaps->path,
+ (long long)st.st_ctime, (long long)nbdkitCaps->ctime);
+ return false;
+ }
+
+ return true;
+}
+
+
+static void*
+virNbdkitCapsNewData(const char *binary,
+ void *privData G_GNUC_UNUSED)
+{
+ qemuNbdkitCaps *caps = qemuNbdkitCapsNew(binary);
+ qemuNbdkitCapsQuery(caps);
+
+ return caps;
+}
+
+
+virFileCacheHandlers nbdkitCapsCacheHandlers = {
+ .isValid = virNbdkitCapsIsValid,
+ .newData = virNbdkitCapsNewData,
+ .loadFile = NULL,
+ .saveFile = NULL,
+ .privFree = NULL,
+};
+
+
+virFileCache*
+qemuNbdkitCapsCacheNew(const char *cachedir)
+{
+ g_autofree char *dir = g_build_filename(cachedir, "nbdkitcapabilities",
NULL);
+ return virFileCacheNew(dir, "xml", &nbdkitCapsCacheHandlers);
+}
diff --git a/src/qemu/qemu_nbdkit.h b/src/qemu/qemu_nbdkit.h
index 4baf4d4d31..b65e76895b 100644
--- a/src/qemu/qemu_nbdkit.h
+++ b/src/qemu/qemu_nbdkit.h
@@ -25,6 +25,7 @@
#include "virbitmap.h"
#include "vircgroup.h"
#include "vircommand.h"
+#include "virfilecache.h"
#include "virstorageobj.h"
#include "viruri.h"
@@ -43,6 +44,8 @@ qemuNbdkitCaps* qemuQueryNbdkitCaps(void);
qemuNbdkitCaps* qemuNbdkitCapsNew(const char *path);
+virFileCache* qemuNbdkitCapsCacheNew(const char *cachedir);
+
bool qemuNbdkitCapsGet(qemuNbdkitCaps *nbdkitCaps, qemuNbdkitCapsFlags flag);
void qemuNbdkitCapsSet(qemuNbdkitCaps *nbdkitCaps, qemuNbdkitCapsFlags flag);
--
2.37.1