There's a lot of code in nbdkit-file-plugin which it would be nice to
reuse elsewhere. One possible approach (as outlined here) is simply
to move the file callbacks (like file.pread, file.pwrite, file.zero
etc) to a new mini-library. They can then be consumed by other
plugins fairly easily by doing:
static void *
foo_open (int readonly)
{
struct fileops *fops;
int fd, flags;
/* Allocate *fops */
/* Set up fd however you want */
if (init_fileops (fd, fops) == -1) {
free (fops);
return NULL;
}
return fops;
}
static struct nbdkit_plugin plugin = {
.name = "foo",
.open = foo_open,
.close = foo_close,
FILEOPS_CALLBACKS
};
If we did this then it would only work for plugins which need to serve
exactly one whole file or block device. In fact the only plugins we
could possibly use it for are iso and tmpdisk.
We can't use it for plugins like split, partitioning, linuxdisk
because those don't only serve a single file. Instead they all either
serve multiple files or have some extra structure (like a virtual
partition table) around a single file.
Another approach (not explored yet) might be to expose more
fundamental primitives like "zeroing part of a file efficiently".
I think possibly best is a mixed approach: We add fileops, but also we
expose fundamental primitives (in libutils). Simpler plugins would
use fileops, others would have a more complex implementation using the
fundamental primitives.
Rich.