daemon: Reimplement ‘vfs_type’ API in OCaml.

This commit is contained in:
Richard W.M. Jones
2017-06-02 17:44:35 +01:00
parent 032148d416
commit d573cfde5b
7 changed files with 141 additions and 6 deletions

View File

@@ -241,9 +241,11 @@ guestfsd_CFLAGS = \
# library and then linked to the daemon. See
# https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html
SOURCES_MLI = \
blkid.mli \
chroot.mli \
sysroot.mli \
file.mli \
mountable.mli \
utils.mli
SOURCES_ML = \
@@ -252,7 +254,9 @@ SOURCES_ML = \
structs.ml \
optgroups.ml \
sysroot.ml \
mountable.ml \
chroot.ml \
blkid.ml \
file.ml \
callbacks.ml \
daemon.ml

View File

@@ -66,12 +66,6 @@ get_blkid_tag (const char *device, const char *tag)
return out; /* caller frees */
}
char *
do_vfs_type (const mountable_t *mountable)
{
return get_blkid_tag (mountable->device, "TYPE");
}
char *
do_vfs_label (const mountable_t *mountable)
{

40
daemon/blkid.ml Normal file
View File

@@ -0,0 +1,40 @@
(* 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 Std_utils
open Utils
let rec vfs_type { Mountable.m_device = device } =
get_blkid_tag device "TYPE"
and get_blkid_tag device tag =
let r, out, err =
commandr "blkid"
[(* Adding -c option kills all caching, even on RHEL 5. *)
"-c"; "/dev/null";
"-o"; "value"; "-s"; tag; device] in
match r with
| 0 -> (* success *)
String.chomp out
| 2 -> (* means tag not found, we return "" *)
""
| _ ->
failwithf "blkid: %s: %s: %s" device tag err

19
daemon/blkid.mli Normal file
View File

@@ -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 vfs_type : Mountable.t -> string

43
daemon/mountable.ml Normal file
View File

@@ -0,0 +1,43 @@
(* 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
type t = {
m_type : mountable_type;
m_device : string;
}
and mountable_type =
| MountableDevice
| MountablePath
| MountableBtrfsVol of string (* volume *)
let to_string { m_type = t; m_device = device } =
match t with
| MountableDevice | MountablePath -> device
| MountableBtrfsVol volume ->
sprintf "btrfsvol:%s/%s" device volume
let of_device device =
{ m_type = MountableDevice; m_device = device }
let of_path path =
{ m_type = MountablePath; m_device = path }
let of_btrfsvol device volume =
{ m_type = MountableBtrfsVol volume; m_device = device }

34
daemon/mountable.mli Normal file
View File

@@ -0,0 +1,34 @@
(* 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.
*)
type t = {
m_type : mountable_type;
m_device : string;
}
and mountable_type =
| MountableDevice
| MountablePath
| MountableBtrfsVol of string (* volume *)
val to_string : t -> string
(** Convert the mountable back to the string used in the public API. *)
val of_device : string -> t
val of_path : string -> t
val of_btrfsvol : string -> string -> t
(** Create a mountable from various objects. *)

View File

@@ -4872,6 +4872,7 @@ See also C<guestfs_realpath>." };
{ defaults with
name = "vfs_type"; added = (1, 0, 75);
style = RString (RPlainString, "fstype"), [String (Mountable, "mountable")], [];
impl = OCaml "Blkid.vfs_type";
tests = [
InitScratchFS, Always, TestResultString (
[["vfs_type"; "/dev/sdb1"]], "ext2"), []