This is useful for probing probing for cache files such as:
external_command ?ignore_error:(Some true) ~prog "ls .cache/something.*"
will return command output (matched files) on its success or empty list
whenits exit code is other than 0 (there are no such files).
---
mllib/common_utils.ml | 15 ++++++++++-----
mllib/common_utils.mli | 2 +-
2 files changed, 11 insertions(+), 6 deletions(-)
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index 76d8b79..9719b16 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -528,23 +528,28 @@ let compare_lvm2_uuids uuid1 uuid2 =
loop 0 0
(* Run an external command, slurp up the output as a list of lines. *)
-let external_command ~prog cmd =
+let external_command ~prog ?(ignore_error = false) cmd =
let chan = Unix.open_process_in cmd in
let lines = ref [] in
(try while true do lines := input_line chan :: !lines done
with End_of_file -> ());
let lines = List.rev !lines in
let stat = Unix.close_process_in chan in
- (match stat with
- | Unix.WEXITED 0 -> ()
+ match stat with
+ | Unix.WEXITED 0 ->
+ (* Command exited correctly, return its output *)
+ lines
+ | Unix.WEXITED i when ignore_error ->
+ (* Command failed. in case such as 'ls something*' when path doesn't
exist
+ * and desired behavior is to get empty list instead of error.
+ * This is useful for probing partial downloads *)
+ []
| Unix.WEXITED i ->
error ~prog (f_"external command '%s' exited with error %d") cmd i
| Unix.WSIGNALED i ->
error ~prog (f_"external command '%s' killed by signal %d") cmd i
| Unix.WSTOPPED i ->
error ~prog (f_"external command '%s' stopped by signal %d") cmd i
- );
- lines
(* Run uuidgen to return a random UUID. *)
let uuidgen ~prog () =
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index 28ba648..ce2242a 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -109,7 +109,7 @@ val compare_version : string -> string -> int
val compare_lvm2_uuids : string -> string -> int
(** Compare two LVM2 UUIDs, ignoring '-' characters. *)
-val external_command : prog:string -> string -> string list
+val external_command : prog:string -> ?ignore_error:bool -> string -> string
list
(** Run an external command, slurp up the output as a list of lines. *)
val uuidgen : prog:string -> unit -> string
--
1.9.3