diff --git a/dib/dib.ml b/dib/dib.ml index de4f2422e..87af4eb3c 100644 --- a/dib/dib.ml +++ b/dib/dib.ml @@ -297,7 +297,7 @@ $cmd \"$@\" (try let loc = which "dib-run-parts" in do_cp loc (destdir // "fake-bin") - with Tool_not_found _ -> + with Executable_not_found _ -> let fake_dib_run_parts = "\ #!/bin/sh echo \"Please install dib-run-parts on the host\" diff --git a/dib/utils.ml b/dib/utils.ml index f31626454..a2046cbe6 100644 --- a/dib/utils.ml +++ b/dib/utils.ml @@ -21,8 +21,6 @@ open Common_utils open Printf -exception Tool_not_found of string (* tool *) - let quote = Filename.quote let unit_GB howmany = @@ -97,21 +95,9 @@ let rec remove_dups = function | [] -> [] | x :: xs -> x :: (remove_dups (List.filter ((<>) x) xs)) -let which tool = - let paths = String.nsplit ":" (Sys.getenv "PATH") in - let paths = filter_map ( - fun p -> - let path = p // tool in - try Unix.access path [Unix.X_OK]; Some path - with Unix.Unix_error _ -> None - ) paths in - match paths with - | [] -> raise (Tool_not_found tool) - | x :: _ -> x - let require_tool tool = try ignore (which tool) - with Tool_not_found tool -> + with Executable_not_found tool -> error (f_"%s needed but not found") tool let do_cp src destdir = diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml index e7ee84a47..14f493583 100644 --- a/mllib/common_utils.ml +++ b/mllib/common_utils.ml @@ -142,6 +142,8 @@ module String = struct ) end +exception Executable_not_found of string (* executable *) + let (//) = Filename.concat let ( +^ ) = Int64.add @@ -316,6 +318,18 @@ let protect ~f ~finally = finally (); match r with Either ret -> ret | Or exn -> raise exn +let which executable = + let paths = String.nsplit ":" (Sys.getenv "PATH") in + let paths = filter_map ( + fun p -> + let path = p // executable in + try Unix.access path [Unix.X_OK]; Some path + with Unix.Unix_error _ -> None + ) paths in + match paths with + | [] -> raise (Executable_not_found executable) + | x :: _ -> x + (* Program name. *) let prog = Filename.basename Sys.executable_name diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli index de97815aa..4959de6e2 100644 --- a/mllib/common_utils.mli +++ b/mllib/common_utils.mli @@ -78,6 +78,10 @@ module String : sig end (** Override the String module from stdlib. *) +(** Exception thrown by [which] when the specified executable is not found + in [$PATH]. *) +exception Executable_not_found of string (* executable *) + val ( // ) : string -> string -> string (** Concatenate directory and filename. *) @@ -379,3 +383,8 @@ val inspect_mount_root_ro : Guestfs.guestfs -> string -> unit val is_btrfs_subvolume : Guestfs.guestfs -> string -> bool (** Checks if a filesystem is a btrfs subvolume. *) + +val which : string -> string +(** Return the full path of the specified executable from [$PATH]. + + Throw [Executable_not_found] if not available. *)