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