Something went wrong. Try again.
Correctness- and interoperability-oriented Tangled CLI
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167type tool = Git | Jjtype status = Exited of int | Signaled of int | Stopped of inttype output = { stdout : string; stderr : string; status : status }
type failure = | Unavailable | Timed_out | Output_limit_exceeded | Io_error of string
let executable_name = function Git -> "git" | Jj -> "jj"
let executable_path tool = let name = executable_name tool in let path = Option.value ~default:"" (Sys.getenv_opt "PATH") in let rec find = function | [] -> None | directory :: rest -> if Filename.is_relative directory then find rest else let candidate = Filename.concat directory name in if try Unix.access candidate [ Unix.X_OK ]; not (Sys.is_directory candidate) with Unix.Unix_error _ | Sys_error _ -> false then try Some (Unix.realpath candidate) with Unix.Unix_error _ | Sys_error _ -> find rest else find rest in find (String.split_on_char ':' path)
let environment_name value = match String.index_opt value '=' with | Some separator -> String.sub value 0 separator | None -> value
let inherited_environment_variable = function | "HOME" | "PATH" | "TMPDIR" | "TMP" | "TEMP" | "USER" | "LOGNAME" | "XDG_CONFIG_HOME" | "XDG_DATA_HOME" | "XDG_CACHE_HOME" | "XDG_STATE_HOME" -> true | _ -> false
let process_environment () = Unix.environment () |> Array.to_list |> List.filter (fun value -> inherited_environment_variable (environment_name value)) |> fun values -> Array.of_list ("LANG=C" :: "LC_ALL=C" :: values)
let status = function | Unix.WEXITED code -> Exited code | Unix.WSIGNALED signal -> Signaled signal | Unix.WSTOPPED signal -> Stopped signal
let read_file ~max_bytes path = let channel = open_in_bin path in Fun.protect ~finally:(fun () -> close_in_noerr channel) (fun () -> let length = min (in_channel_length channel) max_bytes in really_input_string channel length)
let exceeds_limit descriptor max_output_bytes = (Unix.fstat descriptor).st_size > max_output_bytes
let reap_after_kill pid = (try Unix.kill pid Sys.sigkill with Unix.Unix_error (Unix.ESRCH, _, _) -> ()); let rec reap () = try ignore (Unix.waitpid [] pid) with | Unix.Unix_error (Unix.EINTR, _, _) -> reap () | Unix.Unix_error (Unix.ECHILD, _, _) -> () in reap ()
let wait ~pid ~stdout_descriptor ~stderr_descriptor ~timeout_s ~max_output_bytes = let deadline = Unix.gettimeofday () +. timeout_s in let rec loop () = let waited = try Some (Unix.waitpid [ Unix.WNOHANG ] pid) with Unix.Unix_error (Unix.EINTR, _, _) -> None in match waited with | None -> loop () | Some (0, _) -> if exceeds_limit stdout_descriptor max_output_bytes || exceeds_limit stderr_descriptor max_output_bytes then ( reap_after_kill pid; Error Output_limit_exceeded) else if Unix.gettimeofday () >= deadline then ( reap_after_kill pid; Error Timed_out) else ( (try ignore (Unix.select [] [] [] 0.05) with Unix.Unix_error (Unix.EINTR, _, _) -> ()); loop ()) | Some (_, process_status) -> if exceeds_limit stdout_descriptor max_output_bytes || exceeds_limit stderr_descriptor max_output_bytes then Error Output_limit_exceeded else Ok (status process_status) in loop ()
let with_temp_file prefix action = let path, channel = Filename.open_temp_file prefix ".tmp" in Fun.protect ~finally:(fun () -> close_out_noerr channel; try Sys.remove path with Sys_error _ -> ()) (fun () -> action path channel)
let run tool ~args ~timeout_s ~max_output_bytes = try match executable_path tool with | None -> Error Unavailable | Some executable -> with_temp_file "camlet-stdout-" (fun stdout_path stdout_channel -> with_temp_file "camlet-stderr-" (fun stderr_path stderr_channel -> let stdin_descriptor = Unix.openfile "/dev/null" [ Unix.O_RDONLY ] 0 in let stdout_descriptor = Unix.descr_of_out_channel stdout_channel in let stderr_descriptor = Unix.descr_of_out_channel stderr_channel in Fun.protect ~finally:(fun () -> try Unix.close stdin_descriptor with Unix.Unix_error _ -> ()) (fun () -> let argv = Array.of_list (executable :: args) in let pid = Unix.create_process_env executable argv (process_environment ()) stdin_descriptor stdout_descriptor stderr_descriptor in let waited = try wait ~pid ~stdout_descriptor ~stderr_descriptor ~timeout_s ~max_output_bytes with exception_ -> reap_after_kill pid; raise exception_ in match waited with | Error failure -> Error failure | Ok process_status -> flush stdout_channel; flush stderr_channel; Ok { stdout = read_file ~max_bytes:max_output_bytes stdout_path ; stderr = read_file ~max_bytes:max_output_bytes stderr_path ; status = process_status }))) with | Unix.Unix_error (error, _, _) -> Error (Io_error (Unix.error_message error)) | Sys_error message -> Error (Io_error message)