[Libguestfs] [PATCH 07/27] daemon: Reimplement ‘is_dir’, ‘is_file’ and ‘is_symlink’ APIs in OCaml.

Pino Toscano ptoscano at redhat.com
Wed Jul 19 13:17:59 UTC 2017


On Friday, 14 July 2017 15:39:15 CEST Richard W.M. Jones wrote:
> +let rec is_file ?(followsymlinks = false) path =
> +  let sysroot = Sysroot.sysroot () in
> +  let chroot = Chroot.create sysroot ~name:(sprintf "is_file: %s" path) in
> +  Chroot.f chroot get_kind (path, followsymlinks) = Some S_REG
> +
> +and is_dir ?(followsymlinks = false) path =
> +  let sysroot = Sysroot.sysroot () in
> +  let chroot = Chroot.create sysroot ~name:(sprintf "is_dir: %s" path) in
> +  Chroot.f chroot get_kind (path, followsymlinks) = Some S_DIR
> +
> +and is_symlink path =
> +  let sysroot = Sysroot.sysroot () in
> +  let chroot = Chroot.create sysroot ~name:(sprintf "is_symlink: %s" path) in
> +  Chroot.f chroot get_kind (path, false) = Some S_LNK

Would it be possible to factorize even more of the code for these
functions? Something like (untested):

  let check_type ~followsymlinks path func_string typ =
    let sysroot = Sysroot.sysroot () in
    let chroot = Chroot.create sysroot ~name:(sprintf "%s: %s" func_string path) in
    Chroot.f chroot get_kind (path, followsymlinks) = Some S_DIR
    let statfun = if followsymlinks then stat else lstat in
    try
      let statbuf = statfun path in
      statbuf.st_kind = typ
    with
      Unix_error ((ENOENT|ENOTDIR), _, _) ->
        false  (* File doesn't exist => return false *)

  let is_file ?(followsymlinks = false) path =
    check_type path ~followsymlinks "is_file" S_REG

  let is_symlink path =
    check_type path ~followsymlinks:false "is_symlink" S_LNK

-- 
Pino Toscano
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part.
URL: <http://listman.redhat.com/archives/libguestfs/attachments/20170719/3be4cdf7/attachment.sig>


More information about the Libguestfs mailing list