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 + 1 to get_max_fd ( ) do close ' i done ; for i = 0 to highest_to_keep - 1 do if not ( List . mem i fds ' ) then close ' i done
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_path path = if Filename . is_relative path then Filename . concat " " / path else path in rev_split ( abs_path x ) in let to_string ( x : string list ) = List . fold_left Filename . concat " " / ( List . rev x ) in let rec remove_dots ( n : int ) ( x : string list ) = match x , n with | [ ] , _ -> [ ] | " . " :: rest , _ -> remove_dots n rest | " . . " :: rest , _ -> remove_dots ( n + 1 ) rest | x :: rest , 0 -> x :: ( remove_dots 0 rest ) | _ :: rest , n -> remove_dots ( n - 1 ) rest in to_string ( remove_dots 0 ( of_string path ) )
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 ) external unsafe_write : t -> bytes -> int -> int -> int = " stub_stdext_unix_write " let write fd buf ofs len = if ofs < 0 || len < 0 || ofs > Bytes . length buf - len then invalid_arg " Unixext . write " else unsafe_write fd buf ofs len let copy_from_fd ? limit socket fd = copy_file_internal ? limit ( Unix . read socket ) ( write fd ) let fsync x = fsync x let lseek fd x cmd = Unix . LargeFile . lseek fd x cmd end
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 err | ` Write_error err -> Flow . pp_write_error ppf err let make flow = { flow ; queue = Ke . Rke . create ~ capacity : 0x1000 Bigarray . char } let recv flow payload = if Ke . Rke . is_empty flow . queue then ( Flow . read flow . flow >|= R . reword_error ( fun err -> ` Error err ) >>? function | ` Eof -> Lwt . return_ok ` End_of_flow | ` Data res -> Ke . Rke . N . push flow . queue ~ blit : blit0 ~ length : Cstruct . length res ; let len = min ( Cstruct . length payload ) ( Ke . Rke . length flow . queue ) in Ke . Rke . N . keep_exn flow . queue ~ blit : blit1 ~ length : Cstruct . length ~ off : 0 ~ len payload ; Ke . Rke . N . shift_exn flow . queue len ; Lwt . return_ok ( ` Input len ) ) else let len = min ( Cstruct . length payload ) ( Ke . Rke . length flow . queue ) in Ke . Rke . N . keep_exn flow . queue ~ blit : blit1 ~ length : Cstruct . length ~ len payload ; Ke . Rke . N . shift_exn flow . queue len ; Lwt . return_ok ( ` Input len ) let send flow payload = Flow . write flow . flow payload >|= function | Error ` Closed -> R . error ( ` Write_error ` Closed ) | Error err -> R . error ( ` Write_error err ) | Ok ( ) -> R . ok ( Cstruct . length payload ) end
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 StringExt . split ~ map : fn_part_of_string sep fn end else [ CurrentDir Short ]
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 bindings " ; ]
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_out_channel stubs_oc in Format . fprintf fmt " % s . " @ headers ; Cstubs . write_c ~ errno ~ concurrency fmt ~ prefix ( module Unix_dirent_bindings . C ) ; close_out stubs_oc ; let generated_oc = open_out ! ml_file in let fmt = Format . formatter_of_out_channel generated_oc in Cstubs . write_ml ~ errno ~ concurrency fmt ~ prefix ( module Unix_dirent_bindings . C ) ; close_out generated_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 ) ) | 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 ) @-> returning int ) ) let closedir = F . ( foreign " closedir " ( dir_handle @-> returning int ) ) end
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_errno_el2nsync " ( void @-> returning sint ) ) let el3hlt = F . ( foreign " unix_errno_el3hlt " ( void @-> returning sint ) ) let el3rst = F . ( foreign " unix_errno_el3rst " ( void @-> returning sint ) ) let elnrng = F . ( foreign " unix_errno_elnrng " ( void @-> returning sint ) ) let eunatch = F . ( foreign " unix_errno_eunatch " ( void @-> returning sint ) ) let enocsi = F . ( foreign " unix_errno_enocsi " ( void @-> returning sint ) ) let el2hlt = F . ( foreign " unix_errno_el2hlt " ( void @-> returning sint ) ) let ebade = F . ( foreign " unix_errno_ebade " ( void @-> returning sint ) ) let ebadr = F . ( foreign " unix_errno_ebadr " ( void @-> returning sint ) ) let exfull = F . ( foreign " unix_errno_exfull " ( void @-> returning sint ) ) let enoano = F . ( foreign " unix_errno_enoano " ( void @-> returning sint ) ) let ebadrqc = F . ( foreign " unix_errno_ebadrqc " ( void @-> returning sint ) ) let ebadslt = F . ( foreign " unix_errno_ebadslt " ( void @-> returning sint ) ) let ebfont = F . ( foreign " unix_errno_ebfont " ( void @-> returning sint ) ) let enonet = F . ( foreign " unix_errno_enonet " ( void @-> returning sint ) ) let enopkg = F . ( foreign " unix_errno_enopkg " ( void @-> returning sint ) ) let eadv = F . ( foreign " unix_errno_eadv " ( void @-> returning sint ) ) let esrmnt = F . ( foreign " unix_errno_esrmnt " ( void @-> returning sint ) ) let ecomm = F . ( foreign " unix_errno_ecomm " ( void @-> returning sint ) ) let edotdot = F . ( foreign " unix_errno_edotdot " ( void @-> returning sint ) ) let enotuniq = F . ( foreign " unix_errno_enotuniq " ( void @-> returning sint ) ) let ebadfd = F . ( foreign " unix_errno_ebadfd " ( void @-> returning sint ) ) let eremchg = F . ( foreign " unix_errno_eremchg " ( void @-> returning sint ) ) let elibacc = F . ( foreign " unix_errno_elibacc " ( void @-> returning sint ) ) let elibbad = F . ( foreign " unix_errno_elibbad " ( void @-> returning sint ) ) let elibscn = F . ( foreign " unix_errno_elibscn " ( void @-> returning sint ) ) let elibmax = F . ( foreign " unix_errno_elibmax " ( void @-> returning sint ) ) let elibexec = F . ( foreign " unix_errno_elibexec " ( void @-> returning sint ) ) let erestart = F . ( foreign " unix_errno_erestart " ( void @-> returning sint ) ) let estrpipe = F . ( foreign " unix_errno_estrpipe " ( void @-> returning sint ) ) let euclean = F . ( foreign " unix_errno_euclean " ( void @-> returning sint ) ) let enotnam = F . ( foreign " unix_errno_enotnam " ( void @-> returning sint ) ) let enavail = F . ( foreign " unix_errno_enavail " ( void @-> returning sint ) ) let eisnam = F . ( foreign " unix_errno_eisnam " ( void @-> returning sint ) ) let eremoteio = F . ( foreign " unix_errno_eremoteio " ( void @-> returning sint ) ) let enomedium = F . ( foreign " unix_errno_enomedium " ( void @-> returning sint ) ) let emediumtype = F . ( foreign " unix_errno_emediumtype " ( void @-> returning sint ) ) let enokey = F . ( foreign " unix_errno_enokey " ( void @-> returning sint ) ) let ekeyexpired = F . ( foreign " unix_errno_ekeyexpired " ( void @-> returning sint ) ) let ekeyrevoked = F . ( foreign " unix_errno_ekeyrevoked " ( void @-> returning sint ) ) let ekeyrejected = F . ( foreign " unix_errno_ekeyrejected " ( void @-> returning sint ) ) let erfkill = F . ( foreign " unix_errno_erfkill " ( void @-> returning sint ) ) let ehwpoison = F . ( foreign " unix_errno_ehwpoison " ( void @-> returning sint ) ) let epwroff = F . ( foreign " unix_errno_epwroff " ( void @-> returning sint ) ) let edeverr = F . ( foreign " unix_errno_edeverr " ( void @-> returning sint ) ) let ebadexec = F . ( foreign " unix_errno_ebadexec " ( void @-> returning sint ) ) let ebadarch = F . ( foreign " unix_errno_ebadarch " ( void @-> returning sint ) ) let eshlibvers = F . ( foreign " unix_errno_eshlibvers " ( void @-> returning sint ) ) let ebadmacho = F . ( foreign " unix_errno_ebadmacho " ( void @-> returning sint ) ) let enopolicy = F . ( foreign " unix_errno_enopolicy " ( void @-> returning sint ) ) let eqfull = F . ( foreign " unix_errno_eqfull " ( void @-> returning sint ) ) let edoofus = F . ( foreign " unix_errno_edoofus " ( void @-> returning sint ) ) let enotcapable = F . ( foreign " unix_errno_enotcapable " ( void @-> returning sint ) ) let ecapmode = F . ( foreign " unix_errno_ecapmode " ( void @-> returning sint ) ) let eproclim = F . ( foreign " unix_errno_eproclim " ( void @-> returning sint ) ) let ebadrpc = F . ( foreign " unix_errno_ebadrpc " ( void @-> returning sint ) ) let erpcmismatch = F . ( foreign " unix_errno_erpcmismatch " ( void @-> returning sint ) ) let eprogunavail = F . ( foreign " unix_errno_eprogunavail " ( void @-> returning sint ) ) let eprogmismatch = F . ( foreign " unix_errno_eprogmismatch " ( void @-> returning sint ) ) let eprocunavail = F . ( foreign " unix_errno_eprocunavail " ( void @-> returning sint ) ) let eftype = F . ( foreign " unix_errno_eftype " ( void @-> returning sint ) ) let eauth = F . ( foreign " unix_errno_eauth " ( void @-> returning sint ) ) let eneedauth = F . ( foreign " unix_errno_eneedauth " ( void @-> returning sint ) ) let enoattr = F . ( foreign " unix_errno_enoattr " ( void @-> returning sint ) ) let enostr = F . ( foreign " unix_errno_enostr " ( void @-> returning sint ) ) let enodata = F . ( foreign " unix_errno_enodata " ( void @-> returning sint ) ) let etime = F . ( foreign " unix_errno_etime " ( void @-> returning sint ) ) let enosr = F . ( foreign " unix_errno_enosr " ( void @-> returning sint ) ) end
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 = F . ( constant " EAGAIN " sint ) let ealready = F . ( constant " EALREADY " sint ) let ebadf = F . ( constant " EBADF " sint ) let ebadmsg = F . ( constant " EBADMSG " sint ) let ebusy = F . ( constant " EBUSY " sint ) let ecanceled = F . ( constant " ECANCELED " sint ) let echild = F . ( constant " ECHILD " sint ) let econnaborted = F . ( constant " ECONNABORTED " sint ) let econnrefused = F . ( constant " ECONNREFUSED " sint ) let econnreset = F . ( constant " ECONNRESET " sint ) let edeadlk = F . ( constant " EDEADLK " sint ) let edestaddrreq = F . ( constant " EDESTADDRREQ " sint ) let edom = F . ( constant " EDOM " sint ) let edquot = F . ( constant " EDQUOT " sint ) let eexist = F . ( constant " EEXIST " sint ) let efault = F . ( constant " EFAULT " sint ) let efbig = F . ( constant " EFBIG " sint ) let ehostdown = F . ( constant " EHOSTDOWN " sint ) let ehostunreach = F . ( constant " EHOSTUNREACH " sint ) let eidrm = F . ( constant " EIDRM " sint ) let eilseq = F . ( constant " EILSEQ " sint ) let einprogress = F . ( constant " EINPROGRESS " sint ) let eintr = F . ( constant " EINTR " sint ) let einval = F . ( constant " EINVAL " sint ) let eio = F . ( constant " EIO " sint ) let eisconn = F . ( constant " EISCONN " sint ) let eisdir = F . ( constant " EISDIR " sint ) let eloop = F . ( constant " ELOOP " sint ) let emfile = F . ( constant " EMFILE " sint ) let emlink = F . ( constant " EMLINK " sint ) let emsgsize = F . ( constant " EMSGSIZE " sint ) let emultihop = F . ( constant " EMULTIHOP " sint ) let enametoolong = F . ( constant " ENAMETOOLONG " sint ) let enetdown = F . ( constant " ENETDOWN " sint ) let enetreset = F . ( constant " ENETRESET " sint ) let enetunreach = F . ( constant " ENETUNREACH " sint ) let enfile = F . ( constant " ENFILE " sint ) let enobufs = F . ( constant " ENOBUFS " sint ) let enodev = F . ( constant " ENODEV " sint ) let enoent = F . ( constant " ENOENT " sint ) let enoexec = F . ( constant " ENOEXEC " sint ) let enolck = F . ( constant " ENOLCK " sint ) let enolink = F . ( constant " ENOLINK " sint ) let enomem = F . ( constant " ENOMEM " sint ) let enomsg = F . ( constant " ENOMSG " sint ) let enoprotoopt = F . ( constant " ENOPROTOOPT " sint ) let enospc = F . ( constant " ENOSPC " sint ) let enosys = F . ( constant " ENOSYS " sint ) let enotblk = F . ( constant " ENOTBLK " sint ) let enotconn = F . ( constant " ENOTCONN " sint ) let enotdir = F . ( constant " ENOTDIR " sint ) let enotempty = F . ( constant " ENOTEMPTY " sint ) let enotrecoverable = F . ( constant " ENOTRECOVERABLE " sint ) let enotsock = F . ( constant " ENOTSOCK " sint ) let enotsup = F . ( constant " ENOTSUP " sint ) let enotty = F . ( constant " ENOTTY " sint ) let enxio = F . ( constant " ENXIO " sint ) let eopnotsupp = F . ( constant " EOPNOTSUPP " sint ) let eoverflow = F . ( constant " EOVERFLOW " sint ) let eownerdead = F . ( constant " EOWNERDEAD " sint ) let eperm = F . ( constant " EPERM " sint ) let epfnosupport = F . ( constant " EPFNOSUPPORT " sint ) let epipe = F . ( constant " EPIPE " sint ) let eproto = F . ( constant " EPROTO " sint ) let eprotonosupport = F . ( constant " EPROTONOSUPPORT " sint ) let eprototype = F . ( constant " EPROTOTYPE " sint ) let erange = F . ( constant " ERANGE " sint ) let eremote = F . ( constant " EREMOTE " sint ) let erofs = F . ( constant " EROFS " sint ) let eshutdown = F . ( constant " ESHUTDOWN " sint ) let esocktnosupport = F . ( constant " ESOCKTNOSUPPORT " sint ) let espipe = F . ( constant " ESPIPE " sint ) let esrch = F . ( constant " ESRCH " sint ) let estale = F . ( constant " ESTALE " sint ) let etimedout = F . ( constant " ETIMEDOUT " sint ) let etoomanyrefs = F . ( constant " ETOOMANYREFS " sint ) let etxtbsy = F . ( constant " ETXTBSY " sint ) let eusers = F . ( constant " EUSERS " sint ) let ewouldblock = F . ( constant " EWOULDBLOCK " sint ) let exdev = F . ( constant " EXDEV " sint ) end
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 | ENOENT -> 20 | ENOEXEC -> 21 | ENOLCK -> 22 | ENOMEM -> 23 | ENOSPC -> 24 | ENOSYS -> 25 | ENOTDIR -> 26 | ENOTEMPTY -> 27 | ENOTTY -> 28 | ENXIO -> 29 | EPERM -> 30 | EPIPE -> 31 | ERANGE -> 32 | EROFS -> 33 | ESPIPE -> 34 | ESRCH -> 35 | EXDEV -> 36 | EWOULDBLOCK -> 37 | EINPROGRESS -> 38 | EALREADY -> 39 | ENOTSOCK -> 40 | EDESTADDRREQ -> 41 | EMSGSIZE -> 42 | EPROTOTYPE -> 43 | ENOPROTOOPT -> 44 | EPROTONOSUPPORT -> 45 | ESOCKTNOSUPPORT -> 46 | EOPNOTSUPP -> 47 | EPFNOSUPPORT -> 48 | EAFNOSUPPORT -> 49 | EADDRINUSE -> 50 | EADDRNOTAVAIL -> 51 | ENETDOWN -> 52 | ENETUNREACH -> 53 | ENETRESET -> 54 | ECONNABORTED -> 55 | ECONNRESET -> 56 | ENOBUFS -> 57 | EISCONN -> 58 | ENOTCONN -> 59 | ESHUTDOWN -> 60 | ETOOMANYREFS -> 61 | ETIMEDOUT -> 62 | ECONNREFUSED -> 63 | EHOSTDOWN -> 64 | EHOSTUNREACH -> 65 | ELOOP -> 66 | EOVERFLOW -> 67 | EUNKNOWNERR _ -> 68
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 " | EIO -> " EIO " | EISDIR -> " EISDIR " | EMFILE -> " EMFILE " | EMLINK -> " EMLINK " | ENAMETOOLONG -> " ENAMETOOLONG " | ENFILE -> " ENFILE " | ENODEV -> " ENODEV " | ENOENT -> " ENOENT " | ENOEXEC -> " ENOEXEC " | ENOLCK -> " ENOLCK " | ENOMEM -> " ENOMEM " | ENOSPC -> " ENOSPC " | ENOSYS -> " ENOSYS " | ENOTDIR -> " ENOTDIR " | ENOTEMPTY -> " ENOTEMPTY " | ENOTTY -> " ENOTTY " | ENXIO -> " ENXIO " | EPERM -> " EPERM " | EPIPE -> " EPIPE " | ERANGE -> " ERANGE " | EROFS -> " EROFS " | ESPIPE -> " ESPIPE " | ESRCH -> " ESRCH " | EXDEV -> " EXDEV " | EWOULDBLOCK -> " EWOULDBLOCK " | EINPROGRESS -> " EINPROGRESS " | EALREADY -> " EALREADY " | ENOTSOCK -> " ENOTSOCK " | EDESTADDRREQ -> " EDESTADDRREQ " | EMSGSIZE -> " EMSGSIZE " | EPROTOTYPE -> " EPROTOTYPE " | ENOPROTOOPT -> " ENOPROTOOPT " | EPROTONOSUPPORT -> " EPROTONOSUPPORT " | ESOCKTNOSUPPORT -> " ESOCKTNOSUPPORT " | EOPNOTSUPP -> " EOPNOTSUPP " | EPFNOSUPPORT -> " EPFNOSUPPORT " | EAFNOSUPPORT -> " EAFNOSUPPORT " | EADDRINUSE -> " EADDRINUSE " | EADDRNOTAVAIL -> " EADDRNOTAVAIL " | ENETDOWN -> " ENETDOWN " | ENETUNREACH -> " ENETUNREACH " | ENETRESET -> " ENETRESET " | ECONNABORTED -> " ECONNABORTED " | ECONNRESET -> " ECONNRESET " | ENOBUFS -> " ENOBUFS " | EISCONN -> " EISCONN " | ENOTCONN -> " ENOTCONN " | ESHUTDOWN -> " ESHUTDOWN " | ETOOMANYREFS -> " ETOOMANYREFS " | ETIMEDOUT -> " ETIMEDOUT " | ECONNREFUSED -> " ECONNREFUSED " | EHOSTDOWN -> " EHOSTDOWN " | EHOSTUNREACH -> " EHOSTUNREACH " | ELOOP -> " ELOOP " | EOVERFLOW -> " EOVERFLOW " | EUNKNOWNERR _ -> " EUNKNOWNERR "
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_case EBUSY ; constant_case ECHILD ; constant_case EDEADLK ; constant_case EDOM ; constant_case EEXIST ; constant_case EFAULT ; constant_case EFBIG ; constant_case EINTR ; constant_case EINVAL ; constant_case EIO ; constant_case EISDIR ; constant_case EMFILE ; constant_case EMLINK ; constant_case ENAMETOOLONG ; constant_case ENFILE ; constant_case ENODEV ; constant_case ENOENT ; constant_case ENOEXEC ; constant_case ENOLCK ; constant_case ENOMEM ; constant_case ENOSPC ; constant_case ENOSYS ; constant_case ENOTDIR ; constant_case ENOTEMPTY ; constant_case ENOTTY ; constant_case ENXIO ; constant_case EPERM ; constant_case EPIPE ; constant_case ERANGE ; constant_case EROFS ; constant_case ESPIPE ; constant_case ESRCH ; constant_case EXDEV ; constant_case EWOULDBLOCK ; constant_case EINPROGRESS ; constant_case EALREADY ; constant_case ENOTSOCK ; constant_case EDESTADDRREQ ; constant_case EMSGSIZE ; constant_case EPROTOTYPE ; constant_case ENOPROTOOPT ; constant_case EPROTONOSUPPORT ; constant_case ESOCKTNOSUPPORT ; constant_case EOPNOTSUPP ; constant_case EPFNOSUPPORT ; constant_case EAFNOSUPPORT ; constant_case EADDRINUSE ; constant_case EADDRNOTAVAIL ; constant_case ENETDOWN ; constant_case ENETUNREACH ; constant_case ENETRESET ; constant_case ECONNABORTED ; constant_case ECONNRESET ; constant_case ENOBUFS ; constant_case EISCONN ; constant_case ENOTCONN ; constant_case ESHUTDOWN ; constant_case ETOOMANYREFS ; constant_case ETIMEDOUT ; constant_case ECONNREFUSED ; constant_case EHOSTDOWN ; constant_case EHOSTUNREACH ; constant_case ELOOP ; constant_case EOVERFLOW ; case ~ title ( : get_constructor_name @@ EUNKNOWNERR 0 ) ( Tag ( unix_error_to_tag @@ EUNKNOWNERR 0 ) ) eunknownerr_encoding ( function EUNKNOWNERR e -> Some e | _ -> None ) ( fun e -> EUNKNOWNERR e ) ; ]
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 " unix_fcntl_o_path " ( void @-> returning int ) ) let o_tmpfile = F . ( foreign " unix_fcntl_o_tmpfile " ( void @-> returning int ) ) let o_dsync = F . ( foreign " unix_fcntl_o_dsync " ( void @-> returning int ) ) let o_exlock = F . ( foreign " unix_fcntl_o_exlock " ( void @-> returning int ) ) let o_shlock = F . ( foreign " unix_fcntl_o_shlock " ( void @-> returning int ) ) let o_symlink = F . ( foreign " unix_fcntl_o_symlink " ( void @-> returning int ) ) let o_evtonly = F . ( foreign " unix_fcntl_o_evtonly " ( void @-> returning int ) ) let o_exec = F . ( foreign " unix_fcntl_o_exec " ( void @-> returning int ) ) let o_tty_init = F . ( foreign " unix_fcntl_o_tty_init " ( void @-> returning int ) ) let o_search = F . ( foreign " unix_fcntl_o_search " ( void @-> returning int ) ) let open_perms = F . ( foreign " open " ( string_opt @-> int @-> int @-> returning int ) ) let open_ = F . ( foreign " open " ( string_opt @-> int @-> returning int ) ) end
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 ) | _ -> print_endline " UNEXPECTED SUCCESS " ) ; Unix . rmdir " aaa / bbb " ; Unix . rmdir " aaa " in [ % expect { | bbb bbb } ] | compile_and_run ~ unix : true { |
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 : mkdir } ] | compile_and_run ~ unix : true { |
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_endline ( " EXPECTED ERROR : " ^ syscall ) | exception err -> print_endline ( Printexc . to_string err ) | _ -> print_endline " UNEXPECTED SUCCESS " ) ; Sys . remove " aaa " in [ % expect { | EXPECTED ERROR : mkdir EXPECTED ERROR : mkdir } ] | compile_and_run ~ unix : true { |
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 : rmdir } ] | compile_and_run ~ unix : true { |
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 . ENOENT , syscall , path ) -> print_endline ( " EXPECTED ERROR : " ^ syscall ) | exception err -> print_endline ( Printexc . to_string err ) | _ -> print_endline " UNEXPECTED SUCCESS " ) ; Sys . remove " aaa / bbb " ; Unix . rmdir " aaa " in [ % expect { | EXPECTED ERROR : rmdir EXPECTED ERROR : rmdir } ] | compile_and_run ~ unix : true { |
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 " UNEXPECTED SUCCESS " ) ; Sys . remove " aaa " in [ % expect { | File size : 3 Failure ( " unix_stat : not implemented " ) } ] | compile_and_run ~ unix : true { |
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 implemented " ) } ] | compile_and_run ~ unix : true { |
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 } -> print_string " File size : " ; print_int st_size ; print_newline ( ) ; | _ -> print_endline " UNEXPECTED SUCCESS " ) ; ( try Sys . remove " ccc " with | _ -> ( ) ) ; Sys . remove " aaa " ; in [ % expect { | File size : 3 Failure ( " unix_symlink : not implemented " ) Failure ( " unix_stat : not implemented " ) } ] | compile_and_run ~ unix : true { |
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 contents = input_line ic in print_endline contents ; close_in ic ) ) ; ( try Sys . remove " ccc " with | _ -> ( ) ) ; Sys . remove " aaa " ; in [ % expect { | bbb Failure ( " unix_symlink : not implemented " ) Failure ( " unix_readlink : not implemented " ) } ] | compile_and_run ~ unix : true { |
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 err ) | _ -> print_endline " UNEXPECTED SUCCESS " ) ; in [ % expect { | EXPECTED ERROR Failure ( " unix_readlink : not implemented " ) } ] | compile_and_run ~ unix : true { |
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 " UNEXPECTED SUCCESS " ) ; Sys . remove " aaa " ; in [ % expect { | File size : 3 Failure ( " unix_lstat : not implemented " ) } ] | compile_and_run ~ unix : true { |
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 } -> print_endline " Found link " | _ -> print_endline " UNEXPECTED SUCCESS " ) ; ( try Sys . remove " ccc " with | _ -> ( ) ) ; Sys . remove " aaa " ; in [ % expect { | Found link Failure ( " unix_symlink : not implemented " ) Failure ( " unix_lstat : not implemented " ) } ] |
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_DSYNC | ` Sync -> O_SYNC | ` Rsync -> O_RSYNC ; ;
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_descr in Fd . create kind file_descr ( Info . of_string 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 | EAGAIN ) , _ , _ ) -> false ) ; ;
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_type = Command . Param . Arg_type . Export . sexp_conv T . t_of_sexp end
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 -> ` File | S_DIR -> ` Directory | S_CHR -> ` Char | S_BLK -> ` Block | S_LNK -> ` Link | S_FIFO -> ` Fifo | S_SOCK -> ` Socket ; ; end
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 , compare ] let of_unix ( u : Unix . stats ) = let of_float_sec f = Time . of_span_since_epoch ( Time . Span . of_sec f ) in { dev = u . st_dev ; ino = u . st_ino ; kind = File_kind . of_unix u . st_kind ; perm = u . st_perm ; nlink = u . st_nlink ; uid = u . st_uid ; gid = u . st_gid ; rdev = u . st_rdev ; size = u . st_size ; atime = of_float_sec u . st_atime ; mtime = of_float_sec u . st_mtime ; ctime = of_float_sec u . st_ctime } ; ; let to_string t = Sexp . to_string ( sexp_of_t t ) end
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 ) ; ;