[Libguestfs] [nbdkit PATCH 2/2] ocaml: Implement .list_exports and friends

Eric Blake eblake at redhat.com
Tue Sep 1 13:41:40 UTC 2020


On 9/1/20 8:25 AM, Eric Blake wrote:
> Fairly straightforward. I'd love for type export to be a bit more
> flexible to make description optional, but could not figure out how to
> decode that from the C side of things, so for now this just requires
> the caller to supply a description for all exports during
> .list_exports.
> 

Maybe I did figure it out after all, although I'm still not sure this is 
the best interface.  Applying this on top of the original patch lets me 
use 'string option' instead of 'string' as the second member of the 
export record. 
https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html#s%3Ac-ocaml-datatype-repr 
didn't directly answer my question, but my understanding is that since 
'string option' is the same as:

type 'a t = a' option =
  | None          (* Is_block is false, value is Val_int(0) *)
  | Some of 'a    (* Is_block is true, value is block with tag 0 *)

then checking Is_block tells me whether I have None or Some string, at 
which point another Field() deref gets me to the string contained in 
that block.


diff --git i/plugins/ocaml/NBDKit.mli w/plugins/ocaml/NBDKit.mli
index 0d7e325b..aaec519c 100644
--- i/plugins/ocaml/NBDKit.mli
+++ w/plugins/ocaml/NBDKit.mli
@@ -53,7 +53,7 @@ type extent = {

  type export = {
    name : string;
-  description : string;
+  description : string option;
  }
  (** The type of the export list returned by [.list_exports]. *)

diff --git i/plugins/ocaml/NBDKit.ml w/plugins/ocaml/NBDKit.ml
index 1d014934..1823fc71 100644
--- i/plugins/ocaml/NBDKit.ml
+++ w/plugins/ocaml/NBDKit.ml
@@ -55,7 +55,7 @@ type extent = {

  type export = {
    name : string;
-  description : string;
+  description : string option;
  }

  type 'a plugin = {
diff --git i/plugins/ocaml/example.ml w/plugins/ocaml/example.ml
index 5dc7b374..de493bf2 100644
--- i/plugins/ocaml/example.ml
+++ w/plugins/ocaml/example.ml
@@ -42,8 +42,8 @@ let ocamlexample_config key value =
       failwith (Printf.sprintf "unknown parameter: %s" key)

  let ocamlexample_list_exports ro tls : NBDKit.export list =
-  [ { name = "name1"; description = "desc1" };
-    { name = "name2"; description = "desc2" } ]
+  [ { name = "name1"; description = Some "desc1" };
+    { name = "name2"; description = None } ]

  let ocamlexample_default_export ro tls =
    "name1"
diff --git i/plugins/ocaml/ocaml.c w/plugins/ocaml/ocaml.c
index a34f67ca..ea499454 100644
--- i/plugins/ocaml/ocaml.c
+++ w/plugins/ocaml/ocaml.c
@@ -332,11 +332,12 @@ list_exports_wrapper (int readonly, int is_tls, 
struct nbdkit_exports *exports)

    /* Convert exports list into calls to nbdkit_add_export. */
    while (rv != Val_int (0)) {
-    const char *name, *desc;
+    const char *name, *desc = NULL;

      v = Field (rv, 0);          /* export struct */
      name = String_val (Field (v, 0));
-    desc = String_val (Field (v, 1));
+    if (Is_block (Field (v, 1)))
+      desc = String_val (Field (Field (v, 1), 0));
      if (nbdkit_add_export (exports, name, desc) == -1) {
        caml_enter_blocking_section ();
        CAMLreturnT (int, -1);




-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org




More information about the Libguestfs mailing list