Re: [Libguestfs] [PATCH v12 08/11] daemon: Implement inspection types and utility functions.

2017-09-20 Thread Richard W.M. Jones
On Tue, Sep 19, 2017 at 04:28:12PM +0200, Pino Toscano wrote:
> On Wednesday, 9 August 2017 19:23:43 CEST Richard W.M. Jones wrote:
> > +let parse_version_from_major_minor str data =
> > +  if verbose () then
> > +eprintf "parse_version_from_major_minor: parsing '%s'\n%!" str;
> > +
> > +  if PCRE.matches re_major_minor str ||
> > + PCRE.matches re_major_no_minor str then (
> > +let major =
> > +  try Some (int_of_string (PCRE.sub 1))
> > +  with Not_found | Invalid_argument _ | Failure _ -> None in
> > +let minor =
> > +  try Some (int_of_string (PCRE.sub 2))
> > +  with Not_found | Invalid_argument _ | Failure _ -> None in
> > +match major, minor with
> > +| None, None
> > +| None, Some _ -> ()
> > +| Some major, None -> data.version <- Some (major, 0)
> > +| Some major, Some minor -> data.version <- Some (major, minor)
> > +  )
> 
> IMHO this is more complex than needed:
> 
>   if PCRE.matches re_major_minor str (
> let major = int_of_string (PCRE.sub 1) in
> let minor = int_of_string (PCRE.sub 2) in
> data.version <- Some (major, minor)
>   )
>   else if PCRE.matches re_major_no_minor str then (
> let major = int_of_string (PCRE.sub 1) in
> data.version <- Some (major, 0)
>   )
> 
> After all, the regexps should already ensure the captures are
> available, and that they caught integer values.

I'll send separate patches to fix/simplify these cases.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW

___
Libguestfs mailing list
Libguestfs@redhat.com
https://www.redhat.com/mailman/listinfo/libguestfs


Re: [Libguestfs] [PATCH v12 08/11] daemon: Implement inspection types and utility functions.

2017-09-19 Thread Pino Toscano
On Wednesday, 9 August 2017 19:23:43 CEST Richard W.M. Jones wrote:
> +let parse_version_from_major_minor str data =
> +  if verbose () then
> +eprintf "parse_version_from_major_minor: parsing '%s'\n%!" str;
> +
> +  if PCRE.matches re_major_minor str ||
> + PCRE.matches re_major_no_minor str then (
> +let major =
> +  try Some (int_of_string (PCRE.sub 1))
> +  with Not_found | Invalid_argument _ | Failure _ -> None in
> +let minor =
> +  try Some (int_of_string (PCRE.sub 2))
> +  with Not_found | Invalid_argument _ | Failure _ -> None in
> +match major, minor with
> +| None, None
> +| None, Some _ -> ()
> +| Some major, None -> data.version <- Some (major, 0)
> +| Some major, Some minor -> data.version <- Some (major, minor)
> +  )

IMHO this is more complex than needed:

  if PCRE.matches re_major_minor str (
let major = int_of_string (PCRE.sub 1) in
let minor = int_of_string (PCRE.sub 2) in
data.version <- Some (major, minor)
  )
  else if PCRE.matches re_major_no_minor str then (
let major = int_of_string (PCRE.sub 1) in
data.version <- Some (major, 0)
  )

After all, the regexps should already ensure the captures are
available, and that they caught integer values.


-- 
Pino Toscano

signature.asc
Description: This is a digitally signed message part.
___
Libguestfs mailing list
Libguestfs@redhat.com
https://www.redhat.com/mailman/listinfo/libguestfs

[Libguestfs] [PATCH v12 08/11] daemon: Implement inspection types and utility functions.

2017-08-09 Thread Richard W.M. Jones
Define the types which will be used to communicate between the
different parts of the inspection code.  The main types are:

  fscorresponds to ‘struct inspect_fs’ in C code

  root  no direct correspondence with the C code, but in the C
code, ‘inspect_fs’ was overloaded to store roots

  inspection_data
the inspection data which is incrementally collected about
each filesystem as we perform inspection steps

Other types have simple and obvious correspondences with the
equivalent C code.

Add some utility function which will be used by inspection.

Note that this commit has no effect on its own, it just links extra
dead code into the daemon.
---
 daemon/Makefile.am   |   4 +
 daemon/inspect_types.ml  | 314 +++
 daemon/inspect_types.mli | 182 +++
 daemon/inspect_utils.ml  | 187 
 daemon/inspect_utils.mli |  53 
 5 files changed, 740 insertions(+)

diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index 3e8bdb44f..51737e511 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -252,6 +252,8 @@ SOURCES_MLI = \
file.mli \
filearch.mli \
findfs.mli \
+   inspect_types.mli \
+   inspect_utils.mli \
is.mli \
ldm.mli \
link.mli \
@@ -287,6 +289,8 @@ SOURCES_ML = \
parted.ml \
listfs.ml \
realpath.ml \
+   inspect_types.ml \
+   inspect_utils.ml \
callbacks.ml \
daemon.ml
 
diff --git a/daemon/inspect_types.ml b/daemon/inspect_types.ml
new file mode 100644
index 0..4570349ba
--- /dev/null
+++ b/daemon/inspect_types.ml
@@ -0,0 +1,314 @@
+(* 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 Std_utils
+
+type fs = {
+  fs_location : location;
+  role : role; (** Special cases: root filesystem or /usr *)
+}
+and root = {
+  root_location : location;
+  inspection_data : inspection_data;
+}
+and location = {
+  mountable : Mountable.t; (** The device name or other mountable object.*)
+  vfs_type : string;   (** Returned from [vfs_type] API. *)
+}
+
+and role =
+  | RoleRoot of inspection_data
+  | RoleUsr of inspection_data
+  | RoleSwap
+  | RoleOther
+and inspection_data = {
+  mutable os_type : os_type option;
+  mutable distro : distro option;
+  mutable package_format : package_format option;
+  mutable package_management : package_management option;
+  mutable product_name : string option;
+  mutable product_variant : string option;
+  mutable version : version option;
+  mutable arch : string option;
+  mutable hostname : string option;
+  mutable fstab : fstab_entry list;
+  mutable windows_systemroot : string option;
+  mutable windows_software_hive : string option;
+  mutable windows_system_hive : string option;
+  mutable windows_current_control_set : string option;
+  mutable drive_mappings : drive_mapping list;
+}
+and os_type =
+  | OS_TYPE_DOS
+  | OS_TYPE_FREEBSD
+  | OS_TYPE_HURD
+  | OS_TYPE_LINUX
+  | OS_TYPE_MINIX
+  | OS_TYPE_NETBSD
+  | OS_TYPE_OPENBSD
+  | OS_TYPE_WINDOWS
+and distro =
+  | DISTRO_ALPINE_LINUX
+  | DISTRO_ALTLINUX
+  | DISTRO_ARCHLINUX
+  | DISTRO_BUILDROOT
+  | DISTRO_CENTOS
+  | DISTRO_CIRROS
+  | DISTRO_COREOS
+  | DISTRO_DEBIAN
+  | DISTRO_FEDORA
+  | DISTRO_FREEBSD
+  | DISTRO_FREEDOS
+  | DISTRO_FRUGALWARE
+  | DISTRO_GENTOO
+  | DISTRO_LINUX_MINT
+  | DISTRO_MAGEIA
+  | DISTRO_MANDRIVA
+  | DISTRO_MEEGO
+  | DISTRO_NETBSD
+  | DISTRO_OPENBSD
+  | DISTRO_OPENSUSE
+  | DISTRO_ORACLE_LINUX
+  | DISTRO_PARDUS
+  | DISTRO_PLD_LINUX
+  | DISTRO_REDHAT_BASED
+  | DISTRO_RHEL
+  | DISTRO_SCIENTIFIC_LINUX
+  | DISTRO_SLACKWARE
+  | DISTRO_SLES
+  | DISTRO_SUSE_BASED
+  | DISTRO_TTYLINUX
+  | DISTRO_UBUNTU
+  | DISTRO_VOID_LINUX
+  | DISTRO_WINDOWS
+and package_format =
+  | PACKAGE_FORMAT_APK
+  | PACKAGE_FORMAT_DEB
+  | PACKAGE_FORMAT_EBUILD
+  | PACKAGE_FORMAT_PACMAN
+  | PACKAGE_FORMAT_PISI
+  | PACKAGE_FORMAT_PKGSRC
+  | PACKAGE_FORMAT_RPM
+  | PACKAGE_FORMAT_XBPS
+and package_management =
+  | PACKAGE_MANAGEMENT_APK
+  | PACKAGE_MANAGEMENT_APT
+  | PACKAGE_MANAGEMENT_DNF
+  | PACKAGE_MANAGEMENT_PACMAN
+  | PACKAGE_MANAGEMENT_PISI
+