open Bos let ( >>= ) = Result.bind module StringSet = Set.Make (String) module StringMap = Map.Make (String) let lines_of_channel ic = let rec inner acc = try let l = input_line ic in inner (l :: acc) with End_of_file -> List.rev acc in inner [] let lines_of_process cmd = match OS.Cmd.(run_out ~err:err_null cmd |> to_lines) with | Ok x -> x | Error (`Msg e) -> failwith ("Error: " ^ e) (** Opens a file for writing and calls [f]. The destination directory is created if needed. *) let with_out_to filename f = let filename = Fpath.normalize filename in OS.Dir.create (Fpath.parent filename) >>= fun _ -> OS.File.with_oc filename (fun oc () -> f oc; Ok ()) () |> Result.join let cp src dst = assert (lines_of_process Cmd.(v "cp" % src % dst) = []) (* Move [src] to [dst], creating [dst]'s directory if needed. [Unix.rename] when possible, falling back to copy+unlink across filesystems. *) let mv src dst = (OS.Dir.create (Fpath.parent dst) >>= fun _ -> match Unix.rename (Fpath.to_string src) (Fpath.to_string dst) with | () -> Ok () | exception Unix.Unix_error (Unix.EXDEV, _, _) -> cp (Fpath.to_string src) (Fpath.to_string dst); OS.File.delete src) |> Result.get_ok