text
stringlengths
0
601k
let read_lines ( ~ path : string ) : string list = List . rev ( file_lines_fold ( fun acc line -> line :: acc ) [ ] path )
let file_lines_iter f = file_lines_fold ( fun ( ) line -> ignore ( f line ) ) ( )
let fd_blocks_fold block_size f start fd = let block = Bytes . create block_size in let rec fold acc = let n = Unix . read fd block 0 block_size in let b = if n = block_size then block else Bytes . sub block 0 n in if n = 0 then acc else fold ( f acc b ) in fold start
let with_directory dir f = let dh = Unix . opendir dir in Xapi_stdext_pervasives . Pervasiveext . finally ( fun ( ) -> f dh ) ( fun ( ) -> Unix . closedir dh )
let buffer_of_fd fd = fd_blocks_fold 1024 ( fun b s -> Buffer . add_bytes b s ; b ) ( Buffer . create 1024 ) fd
let string_of_fd fd = Buffer . contents ( buffer_of_fd fd )
let buffer_of_file file_path = with_file file_path [ Unix . O_RDONLY ] 0 buffer_of_fd
let string_of_file file_path = Buffer . contents ( buffer_of_file file_path )
let atomic_write_to_file fname perms f = let dir_path = Filename . dirname fname in let tmp_path , tmp_chan = Filename . open_temp_file ~ temp_dir : dir_path " " " . tmp " in let tmp_fd = Unix . descr_of_out_channel tmp_chan in let write_tmp_file ( ) = let result = f tmp_fd in Unix . fchmod tmp_fd perms ; Unix . fsync ...
let write_bytes_to_file fname b = atomic_write_to_file fname 0o644 ( fun fd -> let len = Bytes . length b in let written = Unix . write fd b 0 len in if written <> len then ( failwith " Short write occured " ) ) !
let write_string_to_file fname s = write_bytes_to_file fname ( Bytes . unsafe_of_string s )
let execv_get_output cmd args = let ( pipe_exit , pipe_entrance ) = Unix . pipe ( ) in let r = try Unix . set_close_on_exec pipe_exit ; true with _ -> false in match Unix . fork ( ) with | 0 -> Unix . dup2 pipe_entrance Unix . stdout ; Unix . close pipe_entrance ; if not r then Unix . close pipe_exit ; begin try Unix ....
let copy_file_internal ? limit reader writer = let buffer = Bytes . make 65536 ' \ 000 ' in let buffer_len = Int64 . of_int ( Bytes . length buffer ) in let finished = ref false in let total_bytes = ref 0L in let limit = ref limit in while not ( ! finished ) do let requested = min ( Option . value ~ default : buffer_le...
let copy_file ? limit ifd ofd = copy_file_internal ? limit ( Unix . read ifd ) ( Unix . write ofd )
let file_exists file_path = try Unix . access file_path [ Unix . F_OK ] ; true with _ -> false
let touch_file file_path = let fd = Unix . openfile file_path [ Unix . O_WRONLY ; Unix . O_CREAT ; Unix . O_NOCTTY ; Unix . O_NONBLOCK ] 0o666 in Unix . close fd ; Unix . utimes file_path 0 . 0 0 . 0
let is_empty_file file_path = try let stats = Unix . stat file_path in stats . Unix . st_size = 0 with Unix . Unix_error ( Unix . ENOENT , _ , _ ) -> false
let delete_empty_file file_path = if is_empty_file file_path then ( Sys . remove file_path ; true ) else ( false )
let open_connection_fd host port = let open Unix in let addrinfo = getaddrinfo host ( string_of_int port ) [ AI_SOCKTYPE SOCK_STREAM ] in match addrinfo with | [ ] -> failwith ( Printf . sprintf " Couldn ' t resolve hostname : % s " host ) | ai :: _ -> let s = socket ai . ai_family ai . ai_socktype 0 in try connect s a...
let open_connection_unix_fd filename = let s = Unix . socket Unix . PF_UNIX Unix . SOCK_STREAM 0 in try let addr = Unix . ADDR_UNIX ( filename ) in Unix . connect s addr ; s with e -> Backtrace . is_important e ; Unix . close s ; raise e
module CBuf = struct type t = { mutable buffer : bytes ; mutable len : int ; mutable start : int ; mutable r_closed : bool ; mutable w_closed : bool ; } let empty length = { buffer = Bytes . create length ; len = 0 ; start = 0 ; r_closed = false ; w_closed = false ; } let drop ( x : t ) n = if n > x . len then failwith...
let kill_and_wait ( ? signal = Sys . sigterm ) ( ? timeout = 10 . ) pid = let proc_entry_exists pid = try Unix . access ( Printf . sprintf " / proc /% d " pid ) [ Unix . F_OK ] ; true with _ -> false in if pid > 0 && proc_entry_exists pid then ( let loop_time_waiting = 0 . 03 in let left = ref timeout in let readcmdlin...
let string_of_signal x = let table = [ Sys . sigabrt , " SIGABRT " ; Sys . sigalrm , " SIGALRM " ; Sys . sigfpe , " SIGFPE " ; Sys . sighup , " SIGHUP " ; Sys . sigill , " SIGILL " ; Sys . sigint , " SIGINT " ; Sys . sigkill , " SIGKILL " ; Sys . sigpipe , " SIGPIPE " ; Sys . sigquit , " SIGQUIT " ; Sys . sigsegv , " S...
let proxy ( a : Unix . file_descr ) ( b : Unix . file_descr ) = let size = 64 * 1024 in let a ' = CBuf . empty size and b ' = CBuf . empty size in Unix . set_nonblock a ; Unix . set_nonblock b ; try while true do let r = ( if CBuf . should_read a ' then [ a ] else [ ] ) @ ( if CBuf . should_read b ' then [ b ] else [ ]...
let rec really_read fd string off n = if n = 0 then ( ) else let m = Unix . read fd string off n in if m = 0 then raise End_of_file ; really_read fd string ( off + m ) ( n - m )
let really_read_string fd length = let buf = Bytes . make length ' \ 000 ' in really_read fd buf 0 length ; Bytes . unsafe_to_string buf
let try_read_string ? limit fd = let buf = Buffer . create 0 in let chunk = match limit with None -> 4096 | Some x -> x in let cache = Bytes . make chunk ' \ 000 ' in let finished = ref false in while not ! finished do let to_read = match limit with | Some x -> min ( x - ( Buffer . length buf ) ) chunk | None -> chunk ...
let rec restart_on_EINTR f x = try f x with Unix . Unix_error ( Unix . EINTR , _ , _ ) -> restart_on_EINTR f x let n = restart_on_EINTR ( Unix . single_write_substring fd buffer offset ) len in if n < len then really_write fd buffer ( offset + n ) ( len - n ) ; ;
let really_write_string fd string = really_write fd string 0 ( String . length string )
let time_limited_write_internal ( write : Unix . file_descr -> ' a -> int -> int -> int ) filedesc length data target_response_time = let total_bytes_to_write = length in let bytes_written = ref 0 in let now = ref ( Unix . gettimeofday ( ) ) in while ! bytes_written < total_bytes_to_write && ! now < target_response_tim...
let time_limited_write filedesc length data target_response_time = time_limited_write_internal Unix . write filedesc length data target_response_time
let time_limited_write_substring filedesc length data target_response_time = time_limited_write_internal Unix . write_substring filedesc length data target_response_time
let time_limited_read filedesc length target_response_time = let total_bytes_to_read = length in let bytes_read = ref 0 in let buf = Bytes . make total_bytes_to_read ' \ 000 ' in let now = ref ( Unix . gettimeofday ( ) ) in while ! bytes_read < total_bytes_to_read && ! now < target_response_time do let remaining_time =...
let read_data_in_chunks_internal ( sub : bytes -> int -> int -> ' a ) ( f : ' a -> int -> unit ) ( ? block_size = 1024 ) ( ? max_bytes = - 1 ) from_fd = let buf = Bytes . make block_size ' \ 000 ' in let rec do_read acc = let remaining_bytes = max_bytes - acc in if remaining_bytes = 0 then acc else begin let bytes_to_r...
let read_data_in_string_chunks ( f : string -> int -> unit ) ( ? block_size = 1024 ) ( ? max_bytes = - 1 ) from_fd = read_data_in_chunks_internal Bytes . sub_string f ~ block_size ~ max_bytes from_fd
let read_data_in_chunks ( f : bytes -> int -> unit ) ( ? block_size = 1024 ) ( ? max_bytes = - 1 ) from_fd = read_data_in_chunks_internal Bytes . sub f ~ block_size ~ max_bytes from_fd
let spawnvp ( ? pid_callback ( = fun _ -> ( ) ) ) cmd args = match Unix . fork ( ) with | 0 -> Unix . execvp cmd args | pid -> begin try pid_callback pid with _ -> ( ) end ; snd ( Unix . waitpid [ ] pid )
let double_fork f = match Unix . fork ( ) with | 0 -> begin match Unix . fork ( ) with | 0 -> ( try f ( ) with _ -> ( ) ) ; _exit 0 | _ -> _exit 0 end | pid -> ignore ( Unix . waitpid [ ] pid )
let int_of_file_descr ( x : Unix . file_descr ) : int = Obj . magic x
let file_descr_of_int ( x : int ) : Unix . file_descr = Obj . magic x
let close_all_fds_except ( fds : Unix . file_descr list ) = let fds ' = List . map int_of_file_descr fds in let close ' ( x : int ) = try Unix . close ( file_descr_of_int x ) with _ -> ( ) in let highest_to_keep = List . fold_left max ( - 1 ) fds ' in for i = highest_to_keep + 1 to get_max_fd ( ) do close ' i done ; fo...
let resolve_dot_and_dotdot ( path : string ) : string = let of_string ( x : string ) : string list = let rec rev_split path = let basename = Filename . basename path and dirname = Filename . dirname path in let rest = if Filename . dirname dirname = dirname then [ ] else rev_split dirname in basename :: rest in let abs...
let seek_to fd pos = Unix . lseek fd pos Unix . SEEK_SET
let seek_rel fd diff = Unix . lseek fd diff Unix . SEEK_CUR
let current_cursor_pos fd = Unix . lseek fd 0 Unix . SEEK_CUR
let wait_for_path path delay timeout = let rec inner ttl = if ttl = 0 then failwith " No path " ; ! try ignore ( Unix . stat path ) with _ -> delay 0 . 5 ; inner ( ttl - 1 ) in inner ( timeout * 2 )
let _ = Callback . register_exception " unixext . unix_error " ( Unix_error ( 0 ) )
type statvfs_t = { f_bsize : int64 ; f_frsize : int64 ; f_blocks : int64 ; f_bfree : int64 ; f_bavail : int64 ; f_files : int64 ; f_ffree : int64 ; f_favail : int64 ; f_fsid : int64 ; f_flag : int64 ; f_namemax : int64 ; }
let domain_of_addr str = try let addr = Unix . inet_addr_of_string str in Some ( Unix . domain_of_sockaddr ( Unix . ADDR_INET ( addr , 1 ) ) ) with _ -> None
module Direct = struct type t = Unix . file_descr external openfile : string -> Unix . open_flag list -> Unix . file_perm -> t = " stub_stdext_unix_open_direct " let close = Unix . close let with_openfile path flags perms f = let t = openfile path flags perms in finally ( fun ( ) -> f t ) ( fun ( ) -> close t ) externa...
let blit0 src src_off dst dst_off len = let dst = Cstruct . of_bigarray ~ off : dst_off ~ len dst in Cstruct . blit src src_off dst 0 len
let blit1 src src_off dst dst_off len = let src = Cstruct . of_bigarray ~ off : src_off ~ len src in Cstruct . blit src 0 dst dst_off len
let ( >>? ) = Lwt_result . bind
module Make ( Flow : Mirage_flow . S ) = struct type ' + a fiber = ' a Lwt . t type t = { queue : ( char , Bigarray . int8_unsigned_elt ) Ke . Rke . t ; flow : Flow . flow ; } type error = [ ` Error of Flow . error | ` Write_error of Flow . write_error ] let pp_error ppf = function | ` Error err -> Flow . pp_error ppf ...
let rec dir_writer lst = match lst with Root _ :: tl -> " " ( /^ dir_writer tl ) | [ CurrentDir Short ] -> " " | lst -> let dir_writer_aux cmp = match cmp with Root _ -> " " | ParentDir -> " . . " | CurrentDir _ -> " . " | Component s -> s in String . concat " " / ( List . map dir_writer_aux lst )
let dir_reader fn = let sep = ' ' / in let fn_part_of_string = function | " . " -> CurrentDir Long | " . . " -> ParentDir | str -> Component str in if ( String . length fn ) > 0 then begin if fn . [ 0 ] = sep then StringExt . split ~ start_acc [ : Root " " ] ~ start_pos : 1 ~ map : fn_part_of_string sep fn else StringE...
let path_writer lst = String . concat " " : lst
let path_reader str = StringExt . split ~ map ( : fun s -> s ) ' ' : str
let fast_concat fn1 fn2 = let fn1_len = String . length fn1 in if fn1_len = 0 || fn1 . [ fn1_len - 1 ] = ' ' / then fn1 ^ fn2 else fn1 ^ " " / ^ fn2
let fast_basename fn = try let start_pos = ( String . rindex fn ' ' ) / + 1 in let fn_len = String . length fn in if start_pos = fn_len then " " else String . sub fn start_pos ( fn_len - start_pos ) with Not_found -> fn
let fast_dirname fn = try let last_pos = String . rindex fn ' ' / in if last_pos = 0 then " " / else String . sub fn 0 last_pos with Not_found -> " "
let fast_is_relative fn = if String . length fn = 0 || fn . [ 0 ] <> ' ' / then true else false
let fast_is_current fn = if String . length fn = 0 || fn = " . " then true else if fn . [ 0 ] <> ' . ' then false else raise CannotHandleFast
let fast_is_parent fn = if fn = " . . " then true else if String . length fn < 2 || fn . [ 0 ] <> ' . ' || fn . [ 1 ] <> ' . ' then false else raise CannotHandleFast
module Prefixed_bindings ( F : Cstubs . FOREIGN ) = struct include Unix_dirent_bindings . C ( struct include F let foreign f = F . foreign ( " unix_dirent_ " ^ f ) end ) end
type configuration = { errno : Cstubs . errno_policy ; concurrency : Cstubs . concurrency_policy ; headers : string ; bindings : ( module Cstubs . BINDINGS ) ; prefix : string ; }
let standard_configuration = { errno = Cstubs . return_errno ; concurrency = Cstubs . sequential ; headers = " # include < dirent . h >\ n # include " \ unix_dirent_util . h " " ; \ bindings = ( module Prefixed_bindings ) ; prefix = " unix_dirent_ " ; }
let lwt_configuration = { errno = Cstubs . return_errno ; concurrency = Cstubs . lwt_jobs ; headers = " # include < dirent . h >\ n " ; bindings = ( module Unix_dirent_bindings . C ) ; prefix = " unix_dirent_lwt_ " ; }
let configuration = ref standard_configuration
let ml_file = ref " "
let c_file = ref " "
let argspec : ( Arg . key * Arg . spec * Arg . doc ) list = [ " -- ml - file " , Arg . Set_string ml_file , " set the ML output file " ; " -- c - file " , Arg . Set_string c_file , " set the C output file " ; " -- lwt - bindings " , Arg . Unit ( fun ( ) -> configuration := lwt_configuration ) , " generate Lwt jobs bind...
let ( ) = let ( ) = Arg . parse argspec failwith " " in if ! ml_file = " " || ! c_file = " " then failwith " Both -- ml - file and -- c - file arguments must be supplied " ; let { errno ; concurrency ; headers ; bindings ; prefix } = ! configuration in let stubs_oc = open_out ! c_file in let fmt = Format . formatter_of...
let dir_handle = Ctypes . ( view ~ read ( : function | Some ptr -> Some ( Unix_representations . dir_handle_of_nativeint ( raw_address_of_ptr ptr ) ) | None -> None ) ~ write ( : function | Some dir -> Some ( ptr_of_raw_address ( Unix_representations . nativeint_of_dir_handle dir ) ) | None -> None ) ( ptr_opt void ) )
module C ( F : Cstubs . FOREIGN ) = struct let opendir = F . ( foreign " opendir " ( string @-> returning dir_handle ) ) let readdir = F . ( foreign " readdir " ( dir_handle @-> returning ( ptr_opt dirent ) ) ) let readdir_r = F . ( foreign " readdir_r " ( dir_handle @-> ptr dirent @-> ptr ( ptr_opt dirent ) @-> return...
module C ( F : Cstubs . FOREIGN ) = struct let reset_errno = F . ( foreign " unix_errno_reset " ( void @-> returning void ) ) let get_errno = F . ( foreign " unix_errno_get " ( void @-> returning sint ) ) let echrng = F . ( foreign " unix_errno_echrng " ( void @-> returning sint ) ) let el2nsync = F . ( foreign " unix_...
module C ( F : Cstubs . Types . TYPE ) = struct let e2big = F . ( constant " E2BIG " sint ) let eacces = F . ( constant " EACCES " sint ) let eaddrinuse = F . ( constant " EADDRINUSE " sint ) let eaddrnotavail = F . ( constant " EADDRNOTAVAIL " sint ) let eafnosupport = F . ( constant " EAFNOSUPPORT " sint ) let eagain...
let unix_error_to_tag = function | E2BIG -> 0 | EACCES -> 1 | EAGAIN -> 2 | EBADF -> 3 | EBUSY -> 4 | ECHILD -> 5 | EDEADLK -> 6 | EDOM -> 7 | EEXIST -> 8 | EFAULT -> 9 | EFBIG -> 10 | EINTR -> 11 | EINVAL -> 12 | EIO -> 13 | EISDIR -> 14 | EMFILE -> 15 | EMLINK -> 16 | ENAMETOOLONG -> 17 | ENFILE -> 18 | ENODEV -> 19 ...
let get_constructor_name = function | E2BIG -> " E2BIG " | EACCES -> " EACCES " | EAGAIN -> " EAGAIN " | EBADF -> " EBADF " | EBUSY -> " EBUSY " | ECHILD -> " ECHILD " | EDEADLK -> " EDEADLK " | EDOM -> " EDOM " | EEXIST -> " EEXIST " | EFAULT -> " EFAULT " | EFBIG -> " EFBIG " | EINTR -> " EINTR " | EINVAL -> " EINVAL...
let unix_error_constant_encoding error = let error_name = get_constructor_name @@ error in obj1 @@ req " unix - error " ( constant error_name )
let eunknownerr_encoding = let error_name = get_constructor_name @@ EUNKNOWNERR 0 in obj1 @@ req " unix - error " @@ obj1 @@ req error_name int31
let constant_case error = let title = get_constructor_name error in let tag = unix_error_to_tag error in case ~ title ( Tag tag ) ( unix_error_constant_encoding error ) ( fun x -> if x = error then Some ( ) else None ) ( fun ( ) -> error )
let encoding = matching ( fun error -> let tag = unix_error_to_tag error in match error with | EUNKNOWNERR code -> matched tag eunknownerr_encoding code | _ -> matched tag ( unix_error_constant_encoding error ) ( ) ) [ constant_case E2BIG ; constant_case EACCES ; constant_case EAGAIN ; constant_case EBADF ; constant_ca...
module Internal_for_tests = struct let get_constructor_name = get_constructor_name end
module C ( F : Cstubs . FOREIGN ) = struct let o_direct = F . ( foreign " unix_fcntl_o_direct " ( void @-> returning int ) ) let o_rsync = F . ( foreign " unix_fcntl_o_rsync " ( void @-> returning int ) ) let o_noatime = F . ( foreign " unix_fcntl_o_noatime " ( void @-> returning int ) ) let o_path = F . ( foreign " un...
let f ( ) = Unix . mkdir " aaa " 0o777 ; Unix . mkdir " aaa / bbb " 0o777 ; let l = Sys . readdir " aaa " |> Array . to_list in List . iter print_endline l ; ( match Unix . rmdir " aaa " with | exception ( Unix . Unix_error ( Unix . ENOTEMPTY , _ , _ ) ) -> ( ) | exception e -> print_endline ( Printexc . to_string e ) ...
let f ( ) = ( match Unix . mkdir " aaa / bbb / ccc " 0o777 with | exception Unix . Unix_error ( Unix . ENOENT , syscall , path ) -> print_endline ( " ENOENT : " ^ syscall ) | exception err -> print_endline ( Printexc . to_string err ) | _ -> print_endline " UNEXPECTED SUCCESS " ) in [ % expect { | ENOENT : mkdir ENOENT...
let f ( ) = let oc = open_out " aaa " in output_string oc " bbb " ; close_out oc ; ( match Unix . mkdir " aaa / bbb " 0o777 with | exception Unix . Unix_error ( Unix . ENOTDIR , syscall , path ) -> print_endline ( " EXPECTED ERROR : " ^ syscall ) | exception Unix . Unix_error ( Unix . ENOENT , syscall , path ) -> print...
let f ( ) = ( match Unix . rmdir " aaa / bbb / ccc " with | exception Unix . Unix_error ( Unix . ENOENT , syscall , path ) -> print_endline ( " ENOENT : " ^ syscall ) | exception err -> print_endline ( Printexc . to_string err ) | _ -> print_endline " UNEXPECTED SUCCESS " ) in [ % expect { | ENOENT : rmdir ENOENT : rmd...
let f ( ) = Unix . mkdir " aaa " 0o777 ; let oc = open_out " aaa / bbb " in output_string oc " ccc " ; close_out oc ; ( match Unix . rmdir " aaa / bbb " with | exception Unix . Unix_error ( Unix . ENOTDIR , syscall , path ) -> print_endline ( " EXPECTED ERROR : " ^ syscall ) | exception Unix . Unix_error ( Unix . ENOEN...
let f ( ) = let oc = open_out " aaa " in output_string oc " bbb " ; close_out oc ; ( match Unix . stat " aaa " with | exception err -> print_endline ( Printexc . to_string err ) | { st_kind = Unix . S_REG ; st_size } -> print_string " File size : " ; print_int st_size ; print_newline ( ) ; | _ -> print_endline " UNEXPE...
let f ( ) = Unix . mkdir " aaa " 0o777 ; ( match Unix . stat " aaa " with | exception err -> print_endline ( Printexc . to_string err ) | { st_kind = Unix . S_DIR } -> print_endline " Found dir " | _ -> print_endline " UNEXPECTED SUCCESS " ) ; Unix . rmdir " aaa " in [ % expect { | Found dir Failure ( " unix_stat : not...
let f ( ) = let oc = open_out " aaa " in output_string oc " bbb " ; close_out oc ; ( try Unix . symlink " aaa " " ccc " with | err -> print_endline ( Printexc . to_string err ) ) ; ( match Unix . stat " ccc " with | exception err -> print_endline ( Printexc . to_string err ) | { st_kind = Unix . S_REG ; st_size } -> pr...
let f ( ) = let oc = open_out " aaa " in output_string oc " bbb " ; close_out oc ; ( try Unix . symlink " aaa " " ccc " with | err -> print_endline ( Printexc . to_string err ) ) ; ( match Unix . readlink " ccc " with | exception err -> print_endline ( Printexc . to_string err ) | path -> ( let ic = open_in path in let...
let f ( ) = ( match Unix . readlink " . " with | exception Unix . Unix_error ( Unix . EINVAL , syscall , path ) -> print_endline " EXPECTED ERROR " | exception Unix . Unix_error ( Unix . EUNKNOWNERR ( code ) , syscall , path ) -> print_endline " EXPECTED ERROR " | exception err -> print_endline ( Printexc . to_string e...
let f ( ) = let oc = open_out " aaa " in output_string oc " bbb " ; close_out oc ; ( match Unix . lstat " aaa " with | exception err -> print_endline ( Printexc . to_string err ) | { st_kind = Unix . S_REG ; st_size } -> print_string " File size : " ; print_int st_size ; print_newline ( ) ; | _ -> print_endline " UNEXP...
let f ( ) = let oc = open_out " aaa " in output_string oc " bbb " ; close_out oc ; ( try Unix . symlink " aaa " " ccc " with | err -> print_endline ( Printexc . to_string err ) ) ; ( match Unix . lstat " ccc " with | exception err -> print_endline ( Printexc . to_string err ) | { st_kind = Unix . S_LNK ; st_size } -> p...
let system s = In_thread . syscall_exn ~ name " : system " ( fun ( ) -> Unix . system s )
let system_exn s = let % map status = system s in if not ( Result . is_ok status ) then raise_s [ % message " system failed " ~ _ ( : s : string ) ( status : Exit_or_signal . t ) ] ; ;
let getpid ( ) = Unix . getpid ( )