text
stringlengths
12
786k
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 then invalid_arg err_invalid_input else let rc = input ic b 0 bsize in if rc = 0 then None else Some ( b , 0 , rc ) in try Ok ( Bos_base . apply ( f input ) v ~ finally : close ic ) with | Sys_error e -> Fmt . error_msg " % a : % s " Fpath . pp file e with | Sys_error e -> Error ( ` Msg e )
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 -> Fmt . error_msg " % a : unexpected end of file " Fpath . pp file | Sys_error e -> Error ( ` Msg e )
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 = input ic b 0 bsize in if rc = 0 then Ok ( Buffer . contents buf ) else ( Buffer . add_substring buf ( Bytes . unsafe_to_string b ) 0 rc ; loop ( ) ) in loop ( ) in let input ic ( ) = if is_stream ic then input_stream ic else let len = in_channel_length ic in if len <= Sys . max_string_length then begin let s = Bytes . create len in really_input ic s 0 len ; Ok ( Bytes . unsafe_to_string s ) end else begin Fmt . error_msg " read % a : file too large ( % a , max supported size : % a ) " Fpath . pp file Fmt . byte_size len Fmt . byte_size Sys . max_string_length end in match with_ic file input ( ) with | Ok ( Ok _ as v ) -> v | Ok ( Error _ as e ) -> e | Error _ as e -> e
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_flags = Unix . ( [ O_WRONLY ; O_CREAT ; O_EXCL ; O_SHARE_DELETE ] ) in try Ok ( file , Unix . ( openfile sfile open_flags mode ) ) with | Unix . Unix_error ( Unix . EEXIST , _ , _ ) -> loop ( count - 1 ) | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> loop count | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " create temporary file % a : % s " Fpath . pp file ( uerror e ) in loop 10000
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 ( e , _ , _ ) -> ( ) in close fd ; tmps_add file ; Ok file
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_add file ; try Ok ( Bos_base . apply ( f file oc ) v ~ finally : delete_close oc ) with | Sys_error e -> Fmt . error_msg " % a : % s " Fpath . pp file e with Sys_error e -> Error ( ` Msg e )
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 := false ; tmps_rem file ; close_out oc in let output b = if not ! oc_valid then invalid_arg err_invalid_output else match b with | Some ( b , pos , len ) -> output oc b pos len | None -> flush oc in tmps_add file ; try Ok ( Bos_base . apply ( f file output ) v ~ finally : delete_close oc ) with | Sys_error e -> Fmt . error_msg " % a : % s " Fpath . pp file e with Sys_error e -> Error ( ` Msg e )
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 : close ( ) ) with Sys_error e -> Error ( ` Msg e )
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 ) " bos -% s . tmp " do_write v with | Ok ( Ok _ as r ) -> r | Ok ( Error _ as e ) -> e | Error _ as e -> e
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_oc ~ mode ~ dir ( : Fpath . parent file ) " bos -% s . tmp " do_write v with | Ok ( Ok _ as r ) -> r | Ok ( Error _ as e ) -> e | Error _ as e -> e
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 file ( uerror e )
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 dir ( uerror e )
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 . pp path ( uerror e )
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 ( Unix . EINTR , _ , _ ) -> file_must_exist file | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " file % a must exist : % s " Fpath . pp file ( uerror e )
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 ( Unix . EINTR , _ , _ ) -> dir_must_exist dir | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " directory % a must exist : % s " Fpath . pp dir ( uerror e )
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 " path % a must exist : % s " Fpath . pp path ( uerror e )
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 , _ , _ ) -> unlink file | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " delete file % a : % s " Fpath . pp file ( uerror e ) in unlink file
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 ( " . . " | " . " ) -> delete_dir_files dh dirs | Some file -> let rec try_unlink file = try ( Unix . unlink ( Fpath . to_string file ) ; Ok dirs ) with | Unix . Unix_error ( Unix . ENOENT , _ , _ ) -> Ok dirs | Unix . Unix_error ( ( Unix . EISDIR | Unix . EPERM ) , _ , _ ) -> Ok ( file :: dirs ) | Unix . Unix_error ( ( Unix . EACCES , _ , _ ) ) when Sys . win32 -> Ok ( file :: dirs ) | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> try_unlink file | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " % a : % s " Fpath . pp file ( uerror e ) in match try_unlink Fpath . ( dir / file ) with | Ok dirs -> delete_dir_files dh dirs | Error _ as e -> e in try let dh = Unix . opendir ( Fpath . to_string dir ) in match Bos_base . apply ( delete_dir_files dh ) [ ] ~ finally : Unix . closedir dh with | Ok dirs -> delete_files ( dir :: to_rmdir ) ( List . rev_append dirs todo ) | Error _ as e -> e with | Unix . Unix_error ( Unix . ENOENT , _ , _ ) -> delete_files to_rmdir todo | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> delete_files to_rmdir dirs | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " % a : % s " Fpath . pp dir ( uerror e ) in let rec delete_dirs = function | [ ] -> Ok ( ) | dir :: dirs -> let rec rmdir dir = try Ok ( Unix . rmdir ( Fpath . to_string dir ) ) with | Unix . Unix_error ( Unix . ENOENT , _ , _ ) -> Ok ( ) | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> rmdir dir | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " % a : % s " Fpath . pp dir ( uerror e ) in match rmdir dir with | Ok ( ) -> delete_dirs dirs | Error _ as e -> e in let delete recurse dir = if not recurse then let rec rmdir dir = try Ok ( Unix . rmdir ( Fpath . to_string dir ) ) with | Unix . Unix_error ( Unix . ENOENT , _ , _ ) -> Ok ( ) | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> rmdir dir | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " % s " ( uerror e ) in rmdir dir else Result . bind ( delete_files [ ] [ dir ] ) @@ fun rmdirs -> delete_dirs rmdirs in begin Result . bind ( if must then dir_must_exist dir else Ok dir ) @@ fun dir -> delete recurse dir end |> function | Ok _ as v -> v | Error ( ` Msg e ) -> Fmt . error_msg " delete directory % a : % s " Fpath . pp dir e
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 ( ) else Fmt . error_msg " delete path % a : No such path " Fpath . pp path | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> delete ~ must_exist ~ recurse path | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " delete path % a : % s " Fpath . pp path ( uerror 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 ( exists dst ) @@ function | false -> rename src dst | true -> Fmt . error_msg " move % a to % a : Destination exists " Fpath . pp src Fpath . pp dst
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 ( Fpath . to_string p ) m ) with | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> set p m | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " set mode % a : % s " Fpath . pp p ( uerror e ) end
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 " force % s % a to % a : % s " op Fpath . pp target Fpath . pp p ( uerror e )
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 , _ , _ ) -> link ~ force ~ target p | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " link % a to % a : % s " Fpath . pp target Fpath . pp p ( uerror e )
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 , _ , _ ) -> symlink ~ force ~ target p | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " symlink % a to % a : % s " Fpath . pp target Fpath . pp p ( uerror e )
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 % a : Not a symbolic link " Fpath . pp p | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> symlink_target p | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " target of % a : % s " Fpath . pp p ( uerror e )
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 > 1 && e . [ 0 ] = ' . ' && not dotfiles && var_start -> readdir dh acc | Some e -> match Fpath . is_seg e with | true -> begin match Bos_pat . match_pat ~ env 0 e seg with | None -> readdir dh acc | Some _ as m -> let p = if path = " " then e else Fpath . ( to_string ( add_seg ( Fpath . v path ) e ) ) in readdir dh ( ( p , m ) :: acc ) end | false -> Fmt . error_msg " directory % a : cannot parse element to a path ( % a ) " Fpath . pp ( Fpath . v path ) String . dump e in try let path = if path = " " then " . " else path in let dh = Unix . opendir path in Bos_base . apply ( readdir dh ) acc ~ finally : Unix . closedir dh with | Unix . Unix_error ( Unix . ENOTDIR , _ , _ ) -> Ok acc | Unix . Unix_error ( Unix . ENOENT , _ , _ ) -> Ok acc | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> match_segment dotfiles ~ env acc path seg | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " directory % a : % s " Fpath . pp ( Fpath . v path ) ( uerror 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 , ss | ss -> vol , " " , ss in let rec match_segs acc = function | [ ] -> Ok acc | " " :: [ ] -> let rec loop acc = function | [ ] -> Ok acc | ( p , env ) :: matches -> let r = try Ok ( Unix . ( ( stat p ) . st_kind = Unix . S_DIR ) ) with | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " % s : % s " p ( uerror e ) in match r with | Error _ as e -> e | Ok false -> loop acc matches | Ok true -> let acc ' = Fpath . ( to_string ( add_seg ( v p ) " " ) , env ) :: acc in loop acc ' matches in loop [ ] acc | ( " . . " | " . " as e ) :: segs -> let rec loop acc = function | [ ] -> acc | ( p , env ) :: matches -> let p = if p = vol then p ^ e else Fpath . ( to_string ( add_seg ( v p ) e ) ) in loop ( ( p , env ) :: acc ) matches in match_segs ( loop [ ] acc ) segs | seg :: segs -> match Bos_pat . of_string seg with | Error _ as e -> e | Ok seg -> let rec loop acc = function | [ ] -> Ok acc | ( p , env ) :: matches -> match match_segment dotfiles ~ env acc p seg with | Error _ as e -> e | Ok acc -> loop acc matches in match loop [ ] acc with | Error _ as e -> e | Ok acc -> match_segs acc segs in let start_exists vol start = let start = if start = " " then " . " else start in exists ( Fpath . v ( vol ^ start ) ) in Result . bind ( start_exists vol start ) @@ function | false -> Ok [ ] | true -> let start = if start = " " then vol else vol ^ start in match match_segs [ start , env ] segs with | Ok _ as v -> v | Error ( ` Msg e ) -> Fmt . error_msg " % s \ n % s " e ( err ( ) )
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_path p ( acc , to_traverse ) = ( if is_element p then ( f p acc ) else acc ) , ( if is_dir p && do_traverse p then p :: to_traverse else to_traverse ) in let dir_child d acc bname = if not dotfiles && String . is_prefix ~ affix " . " : bname then acc else process_path Fpath . ( d / bname ) acc in let rec loop acc = function | ( d :: ds ) :: up -> let childs = readdir d in let acc , to_traverse = Array . fold_left ( dir_child d ) ( acc , [ ] ) childs in loop acc ( to_traverse :: ds :: up ) | [ ] :: [ ] -> acc | [ ] :: up -> loop acc up | _ -> assert false in let init acc p = let base = Fpath . ( basename @@ normalize p ) in if not dotfiles && String . is_prefix ~ affix " . " : base then acc else process_path p acc in let acc , to_traverse = List . fold_left init ( acc , [ ] ) paths in ( Ok ( loop acc ( to_traverse :: [ ] ) ) ) with Fold_stop ( ` Msg _ as e ) -> Error e
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 )
let stat p = try Ok ( Unix . stat ( Fpath . to_string p ) ) with
let lstat p = try Ok ( Unix . lstat ( Fpath . to_string p ) ) with
let rec truncate p size = try Ok ( Unix . truncate ( Fpath . to_string p ) size ) with
let err_malformed_pat s = strf " malformed named string pattern : % a " String . dump s
type lexeme = Lit of string | Var of string
type t = lexeme list
let dom p = let add acc = function Lit _ -> acc | Var v -> String . Set . add v acc in List . fold_left add String . Set . empty p
let equal p p ' = p = p '
let compare p p ' = Stdlib . compare p p '
type parse_state = S_lit | S_dollar | S_var
let of_string s = let b = Buffer . create 255 in let flush b = let s = Buffer . contents b in ( Buffer . clear b ; s ) in let err ( ) = Error ( ` Msg ( err_malformed_pat s ) ) in let push_lit b acc = if Buffer . length b <> 0 then Lit ( flush b ) :: acc else acc in let max_i = String . length s - 1 in let rec loop acc state i = if i > max_i then if state <> S_lit then err ( ) else ( Ok ( List . rev ( push_lit b acc ) ) ) else match state with | S_lit -> begin match s . [ i ] with | ' ' $ -> loop acc S_dollar ( i + 1 ) | c -> Buffer . add_char b c ; loop acc S_lit ( i + 1 ) end | S_dollar -> begin match s . [ i ] with | ' ' $ -> Buffer . add_char b ' ' ; $ loop acc S_lit ( i + 1 ) | ' ( ' -> loop ( push_lit b acc ) S_var ( i + 1 ) | _ -> err ( ) end | S_var -> begin match s . [ i ] with | ' ) ' -> loop ( Var ( flush b ) :: acc ) S_lit ( i + 1 ) ; | ' , ' -> err ( ) | c -> Buffer . add_char b c ; loop acc S_var ( i + 1 ) end in loop [ ] S_lit 0
let v s = match of_string s with Ok v -> v | Error ( ` Msg e ) -> invalid_arg e
let to_string p = let b = Buffer . create 255 in let add = function | Lit l -> let max_i = String . length l - 1 in let rec loop start i = if i > max_i then Buffer . add_substring b l start ( i - start ) else if l . [ i ] <> ' ' $ then loop start ( i + 1 ) else begin Buffer . add_substring b l start ( i - start + 1 ) ; Buffer . add_char b ' ' ; $ let next = i + 1 in loop next next end in loop 0 0 | Var v -> Buffer . ( add_string b " ( " ; $ add_string b v ; add_char b ' ) ' ) in List . iter add p ; Buffer . contents b
let escape_dollar s = let len = String . length s in let max_idx = len - 1 in let rec escaped_len i l = if i > max_idx then l else match String . unsafe_get s i with | ' ' $ -> escaped_len ( i + 1 ) ( l + 2 ) | _ -> escaped_len ( i + 1 ) ( l + 1 ) in let escaped_len = escaped_len 0 0 in if escaped_len = len then s else let b = Bytes . create escaped_len in let rec loop i k = if i > max_idx then Bytes . unsafe_to_string b else match String . unsafe_get s i with | ' ' $ -> Bytes . unsafe_set b k ' ' ; $ Bytes . unsafe_set b ( k + 1 ) ' ' ; $ loop ( i + 1 ) ( k + 2 ) | c -> Bytes . unsafe_set b k c ; loop ( i + 1 ) ( k + 1 ) in loop 0 0
let rec pp ppf = function
let dump ppf p = let rec dump ppf = function | [ ] -> ( ) | Lit l :: p -> Fmt . string ppf ( String . Ascii . escape_string ( escape_dollar l ) ) ; pp ppf p | Var v :: p -> Fmt . pf ppf " ( $% s ) " v ; pp ppf p in Fmt . pf ppf " " \% a " " \ dump p
type defs = string String . map
let subst ( ? undef = fun _ -> None ) defs p = let subst acc = function | Lit _ as l -> l :: acc | Var v as var -> match String . Map . find v defs with | Some lit -> ( Lit lit ) :: acc | None -> match undef v with | Some lit -> ( Lit lit ) :: acc | None -> var :: acc in List . ( rev ( fold_left subst [ ] p ) )
let format ( ? undef = fun _ -> " " ) defs p = let b = Buffer . create 255 in let add = function | Lit l -> Buffer . add_string b l | Var v -> match String . Map . find v defs with | Some s -> Buffer . add_string b s | None -> Buffer . add_string b ( undef v ) in List . iter add p ; Buffer . contents b
let match_literal pos s lit = let l_len = String . length lit in let s_len = String . length s - pos in if l_len > s_len then None else try for i = 0 to l_len - 1 do if lit . [ i ] <> s . [ pos + i ] then raise Exit done ; Some ( pos + l_len ) with Exit -> None
let match_pat ~ env pos s pat = let init , no_env = match env with | None -> Some String . Map . empty , true | Some m as init -> init , false in let rec loop pos = function | [ ] -> if pos = String . length s then init else None | Lit lit :: p -> begin match ( match_literal pos s lit ) with | None -> None | Some pos -> loop pos p end | Var n :: p -> let rec try_match next_pos = if next_pos < pos then None else match loop next_pos p with | None -> try_match ( next_pos - 1 ) | Some m as r -> if no_env then r else Some ( String . Map . add n ( String . with_index_range s ~ first : pos ~ last ( : next_pos - 1 ) ) m ) in try_match ( String . length s ) in loop pos pat
let matches p s = ( match_pat ~ env : None 0 s p ) <> None
let query ( ? init = String . Map . empty ) p s = match_pat ~ env ( : Some init ) 0 s p
type t = Continuation . t list
let print ppf t = Format . pp_print_list ~ pp_sep : Format . pp_print_space Continuation . print ppf t
let create conts = match conts with | [ ] -> Misc . fatal_error " No continuations provided to [ Bound_continuations . create ] " | _ :: _ -> let conts_set = Continuation . Set . of_list conts in if List . length conts <> Continuation . Set . cardinal conts_set then Misc . fatal_errorf " Continuations provided to [ Bound_continuations . create ] must be \ disjoint :@ % a " print conts ; conts