Connecting to an NBD server temporarily, for a "one-shot" operation, is
quite similar to "Std_utils.with_open_in" and
"Std_utils.with_open_out",
as there are cleanup operations regardless of whether the "one-shot"
operation completes successfully or throws an exception.
Introduce the "Nbdkit.with_connect_unix" function, which takes a Unix
domain socket pathname, a list of metadata contexts to request from the
NBD server, and calls a function with the live NBD server connection.
Bugzilla:
https://bugzilla.redhat.com/show_bug.cgi?id=2027598
Signed-off-by: Laszlo Ersek <lersek(a)redhat.com>
---
lib/Makefile.am | 2 +-
lib/nbdkit.mli | 10 ++++++++++
lib/nbdkit.ml | 12 ++++++++++++
3 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/lib/Makefile.am b/lib/Makefile.am
index c274b9ecf6c7..1fab25b326f5 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -80,7 +80,7 @@ BOBJECTS = config.cmo $(SOURCES_ML:.ml=.cmo)
XOBJECTS = $(BOBJECTS:.cmo=.cmx)
OCAMLPACKAGES = \
- -package str,unix \
+ -package str,unix,nbd \
-I $(builddir) \
-I $(top_builddir)/common/mlgettext \
-I $(top_builddir)/common/mlpcre \
diff --git a/lib/nbdkit.mli b/lib/nbdkit.mli
index ae19295186ed..825755e61dbe 100644
--- a/lib/nbdkit.mli
+++ b/lib/nbdkit.mli
@@ -107,3 +107,13 @@ val run_unix : ?socket:string -> cmd -> string * int
The --exit-with-parent, --foreground, --pidfile, --newstyle and
--unix flags are added automatically. Other flags are set as
in the {!cmd} struct. *)
+
+val with_connect_unix : socket:string ->
+ meta_contexts:string list ->
+ f:(NBD.t -> 'a) ->
+ 'a
+(** [with_connect_unix socket meta_contexts f] calls function [f] with the NBD
+ server at Unix domain socket [socket] connected, and the metadata contexts
+ in [meta_contexts] requested (each of which is not necessarily supported by
+ the server though). The connection is torn down either on normal return or
+ if the function [f] throws an exception. *)
diff --git a/lib/nbdkit.ml b/lib/nbdkit.ml
index 8d0bfdf9cff1..43e0fc5c728a 100644
--- a/lib/nbdkit.ml
+++ b/lib/nbdkit.ml
@@ -217,3 +217,15 @@ If the messages above are not sufficient to diagnose the problem then
add the
chmod socket 0o777;
socket, pid
+
+let with_connect_unix ~socket ~meta_contexts ~f =
+ let nbd = NBD.create () in
+ protect
+ ~f:(fun () ->
+ List.iter (NBD.add_meta_context nbd) meta_contexts;
+ NBD.connect_unix nbd socket;
+ protect
+ ~f:(fun () -> f nbd)
+ ~finally:(fun () -> NBD.shutdown nbd)
+ )
+ ~finally:(fun () -> NBD.close nbd)
--
2.19.1.3.g30247aa5d201