From: Pino Toscano <ptoscano(a)redhat.com>
Add a simple OCaml-based implementation of reader of the osinfo-db:
the only interface is an iterator that invokes an user-supplied
function with each XML file found.
This implementation behaves like the current C implementation, and
still supports the old libosinfo db.
---
.gitignore | 1 +
builder/Makefile.am | 4 +++
builder/osinfo.ml | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++
builder/osinfo.mli | 22 ++++++++++++++++
4 files changed, 103 insertions(+)
create mode 100644 builder/osinfo.ml
create mode 100644 builder/osinfo.mli
diff --git a/.gitignore b/.gitignore
index 54dd5c6d0..59bf52f2b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -93,6 +93,7 @@ Makefile.in
/builder/index-scan.c
/builder/libguestfs.conf
/builder/opensuse.conf
+/builder/osinfo_config.ml
/builder/oUnit-*
/builder/*.out
/builder/*.qcow2
diff --git a/builder/Makefile.am b/builder/Makefile.am
index 7aa97e31d..4a2f639c3 100644
--- a/builder/Makefile.am
+++ b/builder/Makefile.am
@@ -213,6 +213,10 @@ CLEANFILES += *.qcow2 *.xz
check_DATA = $(disk_images)
+osinfo_config.ml: Makefile
+ echo 'let libosinfo_db_path = "$(datadir)/libosinfo/db"' > $@-t
+ mv $@-t $@
+
fedora.qcow2: ../test-data/phony-guests/fedora.img
rm -f $@ $@-t
qemu-img convert -f raw -O qcow2 $< $@-t
diff --git a/builder/osinfo.ml b/builder/osinfo.ml
new file mode 100644
index 000000000..9d1b0169e
--- /dev/null
+++ b/builder/osinfo.ml
@@ -0,0 +1,76 @@
+(* virt-builder
+ * Copyright (C) 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 Tools_utils
+open Osinfo_config
+
+let rec fold fn base =
+ let locations =
+ (* (1) Try the shared osinfo directory, using either the
+ * $OSINFO_SYSTEM_DIR envvar or its default value.
+ *)
+ let dir =
+ try Sys.getenv "OSINFO_SYSTEM_DIR"
+ with Not_found -> "/usr/share/osinfo" in
+ ((dir // "os"), read_osinfo_db_three_levels) ::
+
+ (* (2) Try the libosinfo directory, using the newer three-directory
+ * layout ($LIBOSINFO_DB_PATH / "os" / $group-ID / [file.xml]).
+ *)
+ let path = Osinfo_config.libosinfo_db_path // "os" in
+ (path, read_osinfo_db_three_levels) ::
+
+ (* (3) Try the libosinfo directory, using the old flat directory
+ * layout ($LIBOSINFO_DB_PATH / "oses" / [file.xml]).
+ *)
+ let path = Osinfo_config.libosinfo_db_path // "oses" in
+ (path, read_osinfo_db_flat) :: [] in
+
+
+ let files =
+ List.flatten (
+ filter_map (
+ fun (path, f) ->
+ if is_directory path then Some (f path)
+ (* This is not an error: RHBZ#948324. *)
+ else None
+ ) locations
+ ) in
+
+ List.fold_left fn base files
+
+and read_osinfo_db_three_levels path =
+ debug "osinfo: loading 3-level-directories database from %s" path;
+ let entries = Array.to_list (Sys.readdir path) in
+ let entries = List.map ((//) path) entries in
+ (* Iterate only on directories. *)
+ let entries = List.filter is_directory entries in
+ List.flatten (List.map read_osinfo_db_directory entries)
+
+and read_osinfo_db_flat path =
+ debug "osinfo: loading flat database from %s" path;
+ read_osinfo_db_directory path
+
+and read_osinfo_db_directory path =
+ let entries = Sys.readdir path in
+ let entries = Array.to_list entries in
+ let entries = List.filter (fun x -> Filename.check_suffix x ".xml")
entries in
+ let entries = List.map ((//) path) entries in
+ let entries = List.filter is_regular_file entries in
+ entries
diff --git a/builder/osinfo.mli b/builder/osinfo.mli
new file mode 100644
index 000000000..fa179509d
--- /dev/null
+++ b/builder/osinfo.mli
@@ -0,0 +1,22 @@
+(* virt-builder
+ * Copyright (C) 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 fold : ('a -> string -> 'a) -> 'a -> 'a
+(** [fold f base] folds function [f] over every file in the
+ osinfo-db/libosinfo database of OS definitions.
+ *)
--
2.13.2