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 (... |
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 " Fp... |
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 b... |
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 = Fpa... |
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... |
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 = tmp... |
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 tr... |
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 ... |
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 . appl... |
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 (... |
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 _ a... |
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 , _ ,... |
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 , _ , _ ... |
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 , ... |
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 :... |
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 :... |
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 | U... |
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 f... |
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 -> ... |
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 . ENO... |
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 ) ... |
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 ... |
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 | ... |
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 ~ forc... |
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 ( ) -> sym... |
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 , _... |
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 ( " . . " | " . " ) -> re... |
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 ... |
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 ~... |
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 readd... |
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 =... |
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 . a... |
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 = escap... |
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 . ... |
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 ad... |
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 ) ... |
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_er... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.