Add Augeas.source
authorPino Toscano <ptoscano@redhat.com>
Fri, 15 Sep 2017 14:42:08 +0000 (16:42 +0200)
committerRichard W.M. Jones <rjones@redhat.com>
Fri, 15 Sep 2017 15:45:28 +0000 (16:45 +0100)
Easy way to get the root node representing a source file.

augeas-c.c
augeas.ml
augeas.mli

index 160fe34..885b829 100644 (file)
@@ -385,3 +385,30 @@ ocaml_augeas_transform (value tv, value lensv, value filev, value modev)
 
   CAMLreturn (Val_unit);
 }
 
   CAMLreturn (Val_unit);
 }
+
+/* val source : t -> path -> path option */
+CAMLprim value
+ocaml_augeas_source (value tv, value pathv)
+{
+  CAMLparam2 (tv, pathv);
+  CAMLlocal2 (optv, v);
+  augeas_t t = Augeas_t_val (tv);
+  const char *path = String_val (pathv);
+  char *file_path;
+  int r;
+
+  r = aug_source (t, path, &file_path);
+  if (r == 0) {
+    if (file_path) {   /* Return Some file_path */
+      v = caml_copy_string (file_path);
+      optv = caml_alloc (1, 0);
+      Field (optv, 0) = v;
+      free (file_path);
+    } else             /* Return None */
+      optv = Val_int (0);
+  }
+  else                 /* Error */
+    raise_error (t, "Augeas.source");
+
+  CAMLreturn (optv);
+}
index 8d6ac6d..a2d345d 100644 (file)
--- a/augeas.ml
+++ b/augeas.ml
@@ -78,6 +78,8 @@ external set : t -> path -> value option -> unit
   = "ocaml_augeas_set"
 external transform : t -> string -> string -> transform_mode -> unit
   = "ocaml_augeas_transform"
   = "ocaml_augeas_set"
 external transform : t -> string -> string -> transform_mode -> unit
   = "ocaml_augeas_transform"
+external source : t -> path -> path option
+  = "ocaml_augeas_source"
 
 let () =
   Callback.register_exception "Augeas.Error" (Error (AugErrInternal, "", "", ""))
 
 let () =
   Callback.register_exception "Augeas.Error" (Error (AugErrInternal, "", "", ""))
index 327dc02..dfada4a 100644 (file)
@@ -127,3 +127,8 @@ val set : t -> path -> value option -> unit
 val transform : t -> string -> string -> transform_mode -> unit
   (** [transform t lens file mode] adds or removes (depending on
       [mode]) the transformation of the specified [lens] for [file]. *)
 val transform : t -> string -> string -> transform_mode -> unit
   (** [transform t lens file mode] adds or removes (depending on
       [mode]) the transformation of the specified [lens] for [file]. *)
+
+val source : t -> path -> path option
+  (** [source t path] returns the path to the node representing the
+      file to which [path] belongs, or [None] if [path] does not
+      represent any file. *)