[Libguestfs] [PATCH v6 21/41] daemon: Reimplement ‘readlink’ API in OCaml.

Richard W.M. Jones
Thu, 15 Jun 2017 17:06:11 -0000

---
 daemon/Makefile.am        |  2 ++
 daemon/link.c             | 16 ----------------
 daemon/link.ml            | 25 +++++++++++++++++++++++++
 daemon/link.mli           | 19 +++++++++++++++++++
 generator/actions_core.ml |  1 +
 5 files changed, 47 insertions(+), 16 deletions(-)

diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index 12f1c8c5c..e0c770b76 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -264,6 +264,7 @@ SOURCES_MLI = \
 	devsparts.mli \
 	file.mli \
 	is.mli \
+	link.mli \
 	mountable.mli \
 	utils.mli

@@ -277,6 +278,7 @@ SOURCES_ML = \
 	devsparts.ml \
 	file.ml \
 	is.ml \
+	link.ml \
 	callbacks.ml \
 	daemon.ml

diff --git a/daemon/link.c b/daemon/link.c
index 3ce54fa37..dde61a1c2 100644
--- a/daemon/link.c
+++ b/daemon/link.c
@@ -32,22 +32,6 @@

 GUESTFSD_EXT_CMD(str_ln, ln);

-char *
-do_readlink (const char *path)
-{
-  char *link;
-
-  CHROOT_IN;
-  link = areadlink (path);
-  CHROOT_OUT;
-  if (link == NULL) {
-    reply_with_perror ("%s", path);
-    return NULL;
-  }
-
-  return link;			/* caller frees */
-}
-
 char **
 do_internal_readlinklist (const char *path, char *const *names)
 {
diff --git a/daemon/link.ml b/daemon/link.ml
new file mode 100644
index 000000000..ba53fd6b5
--- /dev/null
+++ b/daemon/link.ml
@@ -0,0 +1,25 @@
+(* guestfs-inspection
+ * Copyright (C) 2009-2017 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+open Printf
+open Unix
+
+let readlink path +  let sysroot = Sysroot.sysroot () in
+  let chroot = Chroot.create sysroot ~name:(sprintf "readlink: %s"
path) in
+  Chroot.f chroot readlink path
diff --git a/daemon/link.mli b/daemon/link.mli
new file mode 100644
index 000000000..6ca0283b4
--- /dev/null
+++ b/daemon/link.mli
@@ -0,0 +1,19 @@
+(* guestfs-inspection
+ * Copyright (C) 2009-2017 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+val readlink : string -> string
diff --git a/generator/actions_core.ml b/generator/actions_core.ml
index 421f3ac6b..7d6755fdc 100644
--- a/generator/actions_core.ml
+++ b/generator/actions_core.ml
@@ -4489,6 +4489,7 @@ The I<-f> option removes the link
(C<linkname>) if it exists already." };
   { defaults with
     name = "readlink"; added = (1, 0, 66);
     style = RString (RPlainString, "link"), [String (Pathname,
"path")], [];
+    impl = OCaml "Link.readlink";
     shortdesc = "read the target of a symbolic link";
     longdesc = "\
 This command reads the target of a symbolic link." };
--
2.13.0