text
stringlengths
12
786k
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...
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 i...
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 ) ...
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_err...
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 " " ...
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 " " ] ~ st...
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 ( ) ...
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 ...
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 ) ...
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_hand...
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 @-> ret...
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 eafnosuppo...
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 ...
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 " ...
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 ; const...
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 @...
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 , _ , _ ) ) ...
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 " UNEXPECTE...
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 . ...
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 SUCCE...
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 : " ...
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...
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...
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_s...
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 . ...
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 " |...
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_siz...
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_...
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 ( )
let getppid ( ) = Unix . getppid ( )
let getppid_exn ( ) = Unix . getppid_exn ( )
let this_process_became_child_of_init ( ? poll_delay = sec 1 . ) ( ) = Deferred . create ( fun i -> Clock . every poll_delay ~ stop ( : Ivar . read i ) ( fun ( ) -> if Pid . equal ( getppid_exn ( ) ) Pid . init then Ivar . fill i ( ) ) ) ; ;
let nice i = Unix . nice i
let cores = Or_error . map Linux_ext . cores ~ f ( : fun cores ( ) -> In_thread . syscall_exn ~ name " : cores " cores ) ; ;
let convert_open_flag : _ -> Unix . open_flag = function | ` Rdonly -> O_RDONLY | ` Wronly -> O_WRONLY | ` Rdwr -> O_RDWR | ` Nonblock -> O_NONBLOCK | ` Append -> O_APPEND | ` Creat -> O_CREAT | ` Trunc -> O_TRUNC | ` Excl -> O_EXCL | ` Noctty -> O_NOCTTY | ` Dsync -> O_DSY...
type open_flag = [ ` Rdonly | ` Wronly | ` Rdwr | ` Nonblock | ` Append | ` Creat | ` Trunc | ` Excl | ` Noctty | ` Dsync | ` Sync | ` Rsync ]
type file_perm = int [ @@ deriving sexp , bin_io , compare ]
let openfile ? perm file ~ mode = let mode = List . map mode ~ f : convert_open_flag @ [ O_CLOEXEC ] in let % bind file_descr = In_thread . syscall_exn ~ name " : openfile " ( fun ( ) -> Unix . openfile ? perm file ~ mode ) in let % map kind = Fd . Kind . infer_using_stat file_...
let fcntl_getfl fd = Fd . syscall_in_thread_exn fd ~ name " : fcntl_getfl " ( fun file_descr -> Unix . fcntl_getfl file_descr ) ; ;
let fcntl_setfl fd flags = Fd . syscall_in_thread_exn fd ~ name " : fcntl_setfl " ( fun file_descr -> Unix . fcntl_setfl file_descr flags ) ; ;
let lseek fd pos ~ mode = let mode : Unix . seek_command = match mode with | ` Set -> SEEK_SET | ` Cur -> SEEK_CUR | ` End -> SEEK_END in Fd . syscall_in_thread_exn fd ~ name " : lseek " ( fun file_descr -> Unix . lseek file_descr pos ~ mode ) ; ;
let truncate filename ~ len = In_thread . syscall_exn ~ name " : truncate " ( fun ( ) -> Unix . truncate filename ~ len ) ; ;
let ftruncate fd ~ len = Fd . syscall_in_thread_exn fd ~ name " : ftruncate " ( fun file_descr -> Unix . ftruncate file_descr ~ len ) ; ;
let fsync fd = Fd . syscall_in_thread_exn fd ~ name " : fsync " Unix . fsync
let fdatasync fd = Fd . syscall_in_thread_exn fd ~ name " : fdatasync " Unix . fdatasync
let sync ( ) = In_thread . syscall_exn ~ name " : sync " Unix . sync
module Lock_mode = struct type t = | Shared | Exclusive [ @@ deriving sexp_of ] let flock_command : t -> _ = function | Shared -> Unix . Flock_command . lock_shared | Exclusive -> Unix . Flock_command . lock_exclusive ; ; end
let lockf ( ? len = 0L ) fd ( lock_mode : Lock_mode . t ) = let mode : Unix . lock_command = match lock_mode with | Shared -> F_RLOCK | Exclusive -> F_LOCK in Fd . syscall_in_thread_exn fd ~ name " : lockf " ( fun file_descr -> Unix . lockf file_descr ~ mode ~ len ) ; ;
let try_lockf ( ? len = 0L ) fd ( lock_mode : Lock_mode . t ) = let mode : Unix . lock_command = match lock_mode with | Shared -> F_TRLOCK | Exclusive -> F_TLOCK in Fd . syscall_exn fd ( fun file_descr -> try Unix . lockf file_descr ~ mode ~ len ; true with | Unix_error ( ( EACCES...
let test_lockf ( ? len = 0L ) fd = Fd . syscall_exn fd ( fun file_descr -> try Unix . lockf file_descr ~ mode : F_TEST ~ len ; true with | Unix_error ( ( EACCES | EAGAIN ) , _ , _ ) -> false ) ; ;
let unlockf ( ? len = 0L ) fd = Fd . syscall_exn fd ( fun file_descr -> Unix . lockf file_descr ~ mode : F_ULOCK ~ len ) ; ;
let flock fd lock_mode = let mode = Lock_mode . flock_command lock_mode in Fd . syscall_in_thread_exn fd ~ name " : flock " ( fun file_descr -> Unix . flock_blocking file_descr mode ) ; ;
let try_flock fd lock_mode = let mode = Lock_mode . flock_command lock_mode in Fd . syscall_exn fd ( fun file_descr -> Unix . flock file_descr mode ) ; ;
let funlock fd = Fd . syscall_exn fd ( fun file_descr -> ignore ( Unix . flock file_descr Unix . Flock_command . unlock : bool ) ) ; ;
module Lock_mechanism = struct module T = struct type t = | Lockf | Flock [ @@ deriving compare , enumerate , sexp , variants ] end let lock ( t : T . t ) = match t with | Lockf -> lockf ? len : None | Flock -> flock ; ; include T include Sexpable . To_stringable ( T ) let arg_ty...
module Lock = struct type t = { mode : Lock_mode . t ; mechanism : Lock_mechanism . t } [ @@ deriving sexp_of ] end
let with_file ? lock ? perm file ~ mode ~ f = let doit f = let % bind fd = openfile file ~ mode ? perm in Fd . with_close fd ~ f in match lock with | None -> doit f | Some { Lock . mode ; mechanism } -> doit ( fun fd -> let % bind ( ) = Lock_mechanism . lock mechanism fd mode in f fd...
module File_kind = struct module T = struct type t = [ ` File | ` Directory | ` Char | ` Block | ` Link | ` Fifo | ` Socket ] [ @@ deriving compare , sexp , bin_io ] end include T include Comparable . Make ( T ) let of_unix : Unix . file_kind -> _ = function | S_REG -> ...
module Stats = struct type t = { dev : int ; ino : int ; kind : File_kind . t ; perm : file_perm ; nlink : int ; uid : int ; gid : int ; rdev : int ; size : int64 ; atime : Time . t ; mtime : Time . t ; ctime : Time . t } [ @@ deriving fields , sexp , bin_io , co...
let fstat fd = Fd . syscall_in_thread_exn fd ~ name " : fstat " Unix . fstat >>| Stats . of_unix
let stat filename = In_thread . syscall_exn ~ name " : stat " ( fun ( ) -> Unix . stat filename ) >>| Stats . of_unix ; ;
let lstat filename = In_thread . syscall_exn ~ name " : lstat " ( fun ( ) -> Unix . lstat filename ) >>| Stats . of_unix ; ;
let isatty fd = Fd . syscall_in_thread_exn fd ~ name " : isatty " Unix . isatty
let unlink filename = In_thread . syscall_exn ~ name " : unlink " ( fun ( ) -> Unix . unlink filename ) ; ;
let remove filename = In_thread . syscall_exn ~ name " : remove " ( fun ( ) -> Unix . remove filename ) ; ;
let rename ~ src ~ dst = In_thread . syscall_exn ~ name " : rename " ( fun ( ) -> Unix . rename ~ src ~ dst ) ; ;