text
stringlengths
0
601k
type tmp_name_pat = ( string -> string , Format . formatter , unit , string ) format4
let delete_tmp dir = ignore ( delete ~ recurse : true dir )
let tmps = ref Fpath . Set . empty
let tmps_add file = tmps := Fpath . Set . add file ! tmps
let tmps_rem file = delete_tmp file ; tmps := Fpath . Set . remove file ! tmps
let delete_tmps ( ) = Fpath . Set . iter delete_tmp ! tmps
let ( ) = at_exit delete_tmps
let tmp ( ? mode = default_tmp_mode ) ? dir pat = let dir = match dir with None -> Bos_os_tmp . default_dir ( ) | Some d -> d in let err ( ) = Fmt . error_msg " create temporary directory % s in % a : \ too many failing attempts " ( strf pat " XXXXXX " ) Fpath . pp dir in let rec loop count = if count < 0 then err ( ) ...
let with_tmp ? mode ? dir pat f v = Result . bind ( tmp ? mode ? dir pat ) @@ fun dir -> try let ret = f dir v in tmps_rem dir ; Ok ret with e -> tmps_rem dir ; raise e
type t = string String . map
let current ( ) = try let env = Unix . environment ( ) in let add acc assign = match acc with | Error _ as e -> e | Ok m -> match String . cut ~ sep " " := assign with | Some ( var , value ) -> Ok ( String . Map . add var value m ) | None -> Fmt . error_msg " could not parse process environment variable ( % S ) " assig...
let to_array env = let add_var name value acc = String . concat [ name ; " " ; = value ] :: acc in Array . of_list ( String . Map . fold add_var env [ ] )
let var name = try Some ( Unix . getenv name ) with Not_found -> None
let set_var name v = let v = match v with None -> " " | Some v -> v in try Ok ( Unix . putenv name v ) with | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " set environment variable % s : % s " name ( Unix . error_message e )
let opt_var name ~ absent = try Unix . getenv name with Not_found -> absent
let req_var name = try Ok ( Unix . getenv name ) with
type ' a parser = string -> ( ' a , [ ` Msg of string ] ) result
let parser kind k_of_string = fun s -> match k_of_string s with | None -> Fmt . error_msg " could not parse % s value from % a " kind String . dump s | Some v -> Ok v
let bool = let of_string s = match String . Ascii . lowercase s with | " " | " false " | " no " | " n " | " 0 " -> Some false | " true " | " yes " | " y " | " 1 " -> Some true | _ -> None in parser " bool " of_string
let string = fun s -> Ok s
let cmd = fun s -> match Bos_cmd . of_string s with
let some p = fun s -> match p s with Ok v -> Ok ( Some v ) | Error _ as e -> e
let parse name p ~ absent = match var name with match p s with | Ok _ as r -> r | Error ( ` Msg e ) -> Fmt . error_msg " environment variable % s : % s " name e
let value ( ? log = Logs . Error ) name p ~ absent = Bos_log . on_error_msg ~ level : log ~ use ( : fun ( ) -> absent ) ( parse name p ~ absent )
let err_empty_buf = " buffer size can ' t be 0 "
let err_invalid_input = " input no longer valid , did it escape its scope " ?
let err_invalid_output = " output no longer valid , did it escape its scope " ?
let null = Fpath . v ( if Sys . os_type = " Win32 " then " NUL " else " / dev / null " )
let dash = Fpath . v " " -
let is_dash = Fpath . equal dash
let rec truncate p size = try Ok ( Unix . truncate ( Fpath . to_string p ) size ) with | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> truncate p size | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " truncate file % a : % s " Fpath . pp p ( uerror e )
let _is_executable file = try Unix . access file [ Unix . X_OK ] ; true with
let is_executable file = _is_executable ( Fpath . to_string file )
let bytes_buf = function if Bytes . length bytes <> 0 then bytes else invalid_arg err_empty_buf
type input = unit -> ( Bytes . t * int * int ) option
let with_input ? bytes file f v = try let ic = if is_dash file then stdin else open_in_bin ( Fpath . to_string file ) in let ic_valid = ref true in let close ic = ic_valid := false ; if is_dash file then ( ) else close_in ic in let b = bytes_buf bytes in let bsize = Bytes . length b in let input ( ) = if not ! ic_valid...
let with_ic file f v = try let ic = if is_dash file then stdin else open_in_bin ( Fpath . to_string file ) in let close ic = if is_dash file then ( ) else close_in ic in try Ok ( Bos_base . apply ( f ic ) v ~ finally : close ic ) with | Sys_error e -> Fmt . error_msg " % a : % s " Fpath . pp file e with | End_of_file -...
let read file = let is_stream ic = let fd = Unix . descr_of_in_channel ic in try Unix . lseek fd 0 Unix . SEEK_END = 0 with | Unix . Unix_error ( Unix . ESPIPE , _ , _ ) -> true in let input_stream ic = let bsize = 65536 in let buf = Buffer . create bsize in let b = Bytes . create bsize in let rec loop ( ) = let rc = i...
let fold_lines f acc file = let input ic acc = let rec loop acc = match try Some ( input_line ic ) with End_of_file -> None with | None -> acc | Some line -> loop ( f acc line ) in loop acc in with_ic file input acc
let read_lines file = Result . map List . rev ( fold_lines ( fun acc l -> l :: acc ) [ ] file )
type tmp_name_pat = ( string -> string , Format . formatter , unit , string ) format4
let rec unlink_tmp file = try Unix . unlink ( Fpath . to_string file ) with
let tmps = ref Fpath . Set . empty
let tmps_add file = tmps := Fpath . Set . add file ! tmps
let tmps_rem file = unlink_tmp file ; tmps := Fpath . Set . remove file ! tmps
let unlink_tmps ( ) = Fpath . Set . iter unlink_tmp ! tmps
let ( ) = at_exit unlink_tmps
let create_tmp_path mode dir pat = let err ( ) = Fmt . error_msg " create temporary file % s in % a : too many failing attempts " ( strf pat " XXXXXX " ) Fpath . pp dir in let rec loop count = if count < 0 then err ( ) else let file = Bos_os_tmp . rand_path dir pat in let sfile = Fpath . to_string file in let open_flag...
let tmp ( ? mode = default_tmp_mode ) ? dir pat = let dir = match dir with None -> Bos_os_tmp . default_dir ( ) | Some d -> d in Result . bind ( create_tmp_path mode dir pat ) @@ fun ( file , fd ) -> let rec close fd = try Unix . close fd with | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> close fd | Unix . Unix_error...
let with_tmp_oc ( ? mode = default_tmp_mode ) ? dir pat f v = try let dir = match dir with None -> Bos_os_tmp . default_dir ( ) | Some d -> d in Result . bind ( create_tmp_path mode dir pat ) @@ fun ( file , fd ) -> let oc = Unix . out_channel_of_descr fd in let delete_close oc = tmps_rem file ; close_out oc in tmps_ad...
let with_tmp_output ( ? mode = default_tmp_mode ) ? dir pat f v = try let dir = match dir with None -> Bos_os_tmp . default_dir ( ) | Some d -> d in Result . bind ( create_tmp_path mode dir pat ) @@ fun ( file , fd ) -> let oc = Unix . out_channel_of_descr fd in let oc_valid = ref true in let delete_close oc = oc_valid...
type output = ( Bytes . t * int * int ) option -> unit
let rec rename src dst = try Unix . rename ( Fpath . to_string src ) ( Fpath . to_string dst ) ; Ok ( ) with | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> rename src dst | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " rename % a to % a : % s " Fpath . pp src Fpath . pp dst ( uerror e )
let stdout_with_output f v = try let output_valid = ref true in let close ( ) = output_valid := false in let output b = if not ! output_valid then invalid_arg err_invalid_output else match b with | Some ( b , pos , len ) -> output stdout b pos len | None -> flush stdout in Ok ( Bos_base . apply ( f output ) v ~ finally...
let with_output ( ? mode = default_mode ) file f v = if is_dash file then stdout_with_output f v else let do_write tmp tmp_out v = match f tmp_out v with | Error _ as v -> Ok v | Ok _ as v -> match rename tmp file with | Error _ as e -> e | Ok ( ) -> Ok v in match with_tmp_output ~ mode ~ dir ( : Fpath . parent file ) ...
let with_oc ( ? mode = default_mode ) file f v = if is_dash file then Ok ( Bos_base . apply ( f stdout ) v ~ finally ( : fun ( ) -> ( ) ) ( ) ) else let do_write tmp tmp_oc v = match f tmp_oc v with | Error _ as v -> Ok v | Ok _ as v -> match rename tmp file with | Error _ as e -> e | Ok ( ) -> Ok v in match with_tmp_o...
let write ? mode file contents = let write oc contents = output_string oc contents ; Ok ( ) in Result . join @@ with_oc ? mode file write contents
let writef ? mode file fmt = Fmt . kstr ( fun content -> write ? mode file content ) fmt
let write_lines ? mode file lines = let rec write oc = function | [ ] -> Ok ( ) | l :: ls -> output_string oc l ; if ls <> [ ] then ( output_char oc ' \ n ' ; write oc ls ) else Ok ( ) in Result . join @@ with_oc ? mode file write lines
let rec file_exists file = try Ok ( Unix . ( ( stat @@ Fpath . to_string file ) . st_kind = S_REG ) ) with | Unix . Unix_error ( Unix . ENOENT , _ , _ ) -> Ok false | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> file_exists file | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " file % a exists : % s " Fpath . pp ...
let rec dir_exists dir = try Ok ( Unix . ( ( stat @@ Fpath . to_string dir ) . st_kind = S_DIR ) ) with | Unix . Unix_error ( Unix . ENOENT , _ , _ ) -> Ok false | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> dir_exists dir | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " directory % a exists : % s " Fpath . pp ...
let rec exists path = try Ok ( ignore @@ Unix . stat ( Fpath . to_string path ) ; true ) with | Unix . Unix_error ( ( Unix . ENOENT | Unix . ENOTDIR ) , _ , _ ) -> Ok false | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> exists path | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " path % a exists : % s " Fpath . ...
let rec file_must_exist file = try match Unix . ( ( stat @@ Fpath . to_string file ) . st_kind ) with | Unix . S_REG -> Ok file | _ -> Fmt . error_msg " % a : Not a file " Fpath . pp file with | Unix . Unix_error ( Unix . ENOENT , _ , _ ) -> Fmt . error_msg " % a : No such file " Fpath . pp file | Unix . Unix_error ( U...
let rec dir_must_exist dir = try match Unix . ( ( stat @@ Fpath . to_string dir ) . st_kind ) with | Unix . S_DIR -> Ok dir | _ -> Fmt . error_msg " % a : Not a directory " Fpath . pp dir with | Unix . Unix_error ( Unix . ENOENT , _ , _ ) -> Fmt . error_msg " % a : No such directory " Fpath . pp dir | Unix . Unix_error...
let rec must_exist path = try ignore @@ Unix . stat ( Fpath . to_string path ) ; Ok path with | Unix . Unix_error ( Unix . ENOENT , _ , _ ) -> Fmt . error_msg " % a : No such path " Fpath . pp path | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> must_exist path | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " pat...
let delete_file ( ? must_exist = false ) file = let rec unlink file = try Ok ( Unix . unlink @@ Fpath . to_string file ) with | Unix . Unix_error ( Unix . ENOENT , _ , _ ) -> if not must_exist then Ok ( ) else Fmt . error_msg " delete file % a : No such file " Fpath . pp file | Unix . Unix_error ( Unix . EINTR , _ , _ ...
let delete_dir ? must_exist ( : must = false ) ( ? recurse = false ) dir = let rec delete_files to_rmdir dirs = match dirs with | [ ] -> Ok to_rmdir | dir :: todo -> let rec delete_dir_files dh dirs = match ( try Some ( Unix . readdir dh ) with End_of_file -> None ) with | None -> Ok dirs | Some ( " . . " | " . " ) -> ...
let rec delete ( ? must_exist = false ) ( ? recurse = false ) path = try match Unix . ( ( stat ( Fpath . to_string path ) ) . st_kind ) with | Unix . S_DIR -> delete_dir ~ must_exist ~ recurse path | _ -> delete_file ~ must_exist path with | Unix . Unix_error ( Unix . ENOENT , _ , _ ) -> if not must_exist then Ok ( ) e...
let move ( ? force = false ) src dst = let rename src dst = try Ok ( Unix . rename ( Fpath . to_string src ) ( Fpath . to_string dst ) ) with | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " move % a to % a : % s " Fpath . pp src Fpath . pp dst ( uerror e ) in if force then rename src dst else Result . bind ( exi...
let rec stat p = try Ok ( Unix . stat ( Fpath . to_string p ) ) with Fmt . error_msg " stat % a : % s " Fpath . pp p ( uerror e )
module Mode = struct type t = int let rec get p = try Ok ( Unix . ( ( stat ( Fpath . to_string p ) ) . st_perm ) ) with | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> get p | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " get mode % a : % s " Fpath . pp p ( uerror e ) let rec set p m = try Ok ( Unix . chmod ( Fp...
let rec force_remove op target p = let sp = Fpath . to_string p in try match Unix . ( ( lstat sp ) . st_kind ) with | Unix . S_DIR -> Ok ( Unix . rmdir sp ) | _ -> Ok ( Unix . unlink sp ) with | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> force_remove op target p | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg "...
let rec link ( ? force = false ) ~ target p = try Ok ( Unix . link ( Fpath . to_string target ) ( Fpath . to_string p ) ) with | Unix . Unix_error ( Unix . EEXIST , _ , _ ) when force -> Result . bind ( force_remove " link " target p ) @@ fun ( ) -> link ~ force ~ target p | Unix . Unix_error ( Unix . EINTR , _ , _ ) -...
let rec symlink ( ? force = false ) ~ target p = try Ok ( Unix . symlink ( Fpath . to_string target ) ( Fpath . to_string p ) ) with | Unix . Unix_error ( Unix . EEXIST , _ , _ ) when force -> Result . bind ( force_remove " symlink " target p ) @@ fun ( ) -> symlink ~ force ~ target p | Unix . Unix_error ( Unix . EINTR...
let rec symlink_target p = try let l = Unix . readlink ( Fpath . to_string p ) in match Fpath . of_string l with | Ok l -> Ok l | Error _ -> Fmt . error_msg " target of % a : could not read a path from % a " Fpath . pp p String . dump l with | Unix . Unix_error ( Unix . EINVAL , _ , _ ) -> Fmt . error_msg " target of %...
let rec symlink_stat p = try Ok ( Unix . lstat ( Fpath . to_string p ) ) with Fmt . error_msg " symlink stat % a : % s " Fpath . pp p ( uerror e )
let rec match_segment dotfiles ~ env acc path seg = let var_start = match seg with Bos_pat . Var _ :: _ -> true | _ -> false in let rec readdir dh acc = match ( try Some ( Unix . readdir dh ) with End_of_file -> None ) with | None -> Ok acc | Some ( " . . " | " . " ) -> readdir dh acc | Some e when String . length e > ...
let match_path ( ? dotfiles = false ) ~ env p = let err ( ) = Fmt . str " Unexpected error while matching ` % a ' " Fpath . pp p in let vol , start , segs = let vol , segs = Fpath . split_volume p in match Fpath . segs segs with | " " :: " " :: [ ] -> vol , Fpath . dir_sep , [ ] | " " :: ss -> vol , Fpath . dir_sep , s...
let matches ? dotfiles p = let get_path acc ( p , _ ) = ( Fpath . v p ) :: acc in Result . map ( List . fold_left get_path [ ] ) ( match_path ? dotfiles ~ env : None p )
let query ? dotfiles ( ? init = String . Map . empty ) p = let env = Some init in let unopt_map acc ( p , map ) = match map with | None -> assert false | Some map -> ( Fpath . v p , map ) :: acc in Result . map ( List . fold_left unopt_map [ ] ) ( match_path ? dotfiles ~ env p )
type ' a res = ( ' a , [ ` Msg of string ] ) result
type traverse = [ ` Any | ` None | ` Sat of Fpath . t -> bool res ]
type elements = [ ` Any | ` Files | ` Dirs | ` Sat of Fpath . t -> bool res ]
type ' a fold_error = Fpath . t -> ' a res -> unit res
let log_fold_error ~ level = fun p -> function | Error ( ` Msg e ) -> Bos_log . msg level ( fun m -> m " % s " e ) ; Ok ( ) | Ok _ -> assert false
let err_fun err f ~ backup_value = fun p -> match f p with | Ok v -> v | Error _ as e -> match err p e with | Ok ( ) -> backup_value | Error m -> raise ( Fold_stop m )
let err_predicate_fun err p = err_fun err p ~ backup_value : false
let do_traverse_fun err = function
let is_element_fun err = function
let is_dir_fun err = let is_dir p = try Ok ( Sys . is_directory ( Fpath . to_string p ) ) with | Sys_error e -> Error ( ` Msg e ) in err_predicate_fun err is_dir
let readdir_fun err = let readdir d = try Ok ( Sys . readdir ( Fpath . to_string d ) ) with | Sys_error e -> Error ( ` Msg e ) in err_fun err readdir ~ backup_value [ ] :||
let fold ( ? err = log_fold_error ~ level : Logs . Error ) ( ? dotfiles = false ) ( ? elements = ` Any ) ( ? traverse = ` Any ) f acc paths = try let do_traverse = do_traverse_fun err traverse in let is_element = is_element_fun err elements in let is_dir = is_dir_fun err in let readdir = readdir_fun err in let process_...
type ' a result = ( ' a , [ ` Unix of Unix . error ] ) Stdlib . result
let pp_error ppf ( ` Unix e ) = Fmt . string ppf ( Unix . error_message e )
let open_error = function Ok _ as r -> r | Error ( ` Unix _ ) as r -> r
let rec call f v = try Ok ( f v ) with
let mkdir p m = try Ok ( Unix . mkdir ( Fpath . to_string p ) m ) with
let link p p ' = try Ok ( Unix . link ( Fpath . to_string p ) ( Fpath . to_string p ' ) ) with | Unix . Unix_error ( e , _ , _ ) -> Error ( ` Unix e )
let unlink p = try Ok ( Unix . unlink ( Fpath . to_string p ) ) with
let rename p p ' = try Ok ( Unix . rename ( Fpath . to_string p ) ( Fpath . to_string p ' ) ) with | Unix . Unix_error ( e , _ , _ ) -> Error ( ` Unix e )