mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-22 07:03:38 +00:00
mllib: move which and its exception from dib
Rename it from "tool" to "executable" in the process, but otherwise it is just code motion (with minimal adjustments in dib).
This commit is contained in:
@@ -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\"
|
||||
|
||||
16
dib/utils.ml
16
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 =
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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. *)
|
||||
|
||||
Reference in New Issue
Block a user