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 tmp_fd ; result in let write_and_persist ( ) = let result = finally write_tmp_file ( fun ( ) -> Stdlib . close_out tmp_chan ) in Unix . rename tmp_path fname ; let dir_fd = Unix . openfile dir_path [ O_RDONLY ] 0 in finally ( fun ( ) -> Unix . fsync dir_fd ) ( fun ( ) -> Unix . close dir_fd ) ; result in finally write_and_persist ( fun ( ) -> unlink_safe tmp_path ) |
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 . execv cmd args with _ -> exit 127 end | pid -> Unix . close pipe_entrance ; pid , pipe_exit |
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_len ! limit ) buffer_len in let num = reader buffer 0 ( Int64 . to_int requested ) in let num64 = Int64 . of_int num in limit := Option . map ( fun x -> Int64 . sub x num64 ) ! limit ; ignore_int ( writer buffer 0 num ) ; total_bytes := Int64 . add ! total_bytes num64 ; finished := num = 0 || ! limit = Some 0L ; done ; ! total_bytes |
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 ai . ai_addr ; s with e -> Backtrace . is_important e ; close s ; raise e |
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 ( Printf . sprintf " drop % d > % d " n x . len ) ; x . start <- ( x . start + n ) mod ( Bytes . length x . buffer ) ; x . len <- x . len - n let should_read ( x : t ) = not x . r_closed && ( x . len < ( Bytes . length x . buffer - 1 ) ) let should_write ( x : t ) = not x . w_closed && ( x . len > 0 ) let end_of_reads ( x : t ) = x . r_closed && x . len = 0 let end_of_writes ( x : t ) = x . w_closed let write ( x : t ) fd = let next = min ( Bytes . length x . buffer ) ( x . start + x . len ) in let len = next - x . start in let written = try Unix . single_write fd x . buffer x . start len with _ -> x . w_closed <- true ; len in drop x written let read ( x : t ) fd = let next = ( x . start + x . len ) mod ( Bytes . length x . buffer ) in let len = min ( Bytes . length x . buffer - next ) ( Bytes . length x . buffer - x . len ) in let read = Unix . read fd x . buffer next len in if read = 0 then x . r_closed <- true ; x . len <- x . len + read end |
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 readcmdline pid = try string_of_file ( Printf . sprintf " / proc /% d / cmdline " pid ) with _ -> " " in let reference = readcmdline pid and quit = ref false in Unix . kill pid signal ; while proc_entry_exists pid && not ! quit && ! left > 0 . do let cmdline = readcmdline pid in if cmdline = reference then ( ignore ( Unix . select [ ] [ ] [ ] loop_time_waiting ) ; left := ! left . - loop_time_waiting ) else ( quit := true ) done ; if ! left <= 0 . then raise Process_still_alive ; ) |
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 , " SIGSEGV " ; Sys . sigterm , " SIGTERM " ; Sys . sigusr1 , " SIGUSR1 " ; Sys . sigusr2 , " SIGUSR2 " ; Sys . sigchld , " SIGCHLD " ; Sys . sigcont , " SIGCONT " ; Sys . sigstop , " SIGSTOP " ; Sys . sigttin , " SIGTTIN " ; Sys . sigttou , " SIGTTOU " ; Sys . sigvtalrm , " SIGVTALRM " ; Sys . sigprof , " SIGPROF " ; ] in if List . mem_assoc x table then List . assoc x table else ( Printf . sprintf " ( ocaml signal % d with an unknown name ) " x ) |
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 [ ] ) in let w = ( if CBuf . should_write a ' then [ b ] else [ ] ) @ ( if CBuf . should_write b ' then [ a ] else [ ] ) in if r = [ ] && w = [ ] then raise End_of_file ; let r , w , _ = Unix . select r w [ ] ( - 1 . 0 ) in List . iter ( fun fd -> if a = fd then CBuf . write b ' a else CBuf . write a ' b ) w ; List . iter ( fun fd -> if a = fd then CBuf . read a ' a else CBuf . read b ' b ) r ; List . iter ( fun ( buf , fd ) -> if CBuf . end_of_reads buf then Unix . shutdown fd Unix . SHUTDOWN_SEND ; if CBuf . end_of_writes buf then Unix . shutdown fd Unix . SHUTDOWN_RECEIVE ) [ a ' , b ; b ' , a ] done with _ -> ( try Unix . clear_nonblock a with _ -> ( ) ) ; ( try Unix . clear_nonblock b with _ -> ( ) ) ; ( try Unix . close a with _ -> ( ) ) ; ( try Unix . close b with _ -> ( ) ) |
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 in let read_bytes = Unix . read fd cache 0 to_read in Buffer . add_subbytes buf cache 0 read_bytes ; if read_bytes = 0 then finished := true done ; Buffer . contents buf |
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_time do let remaining_time = target_response_time . - ! now in let ( _ , ready_to_write , _ ) = Unix . select [ ] [ filedesc ] [ ] remaining_time in if List . mem filedesc ready_to_write then begin let bytes_to_write = total_bytes_to_write - ! bytes_written in let bytes = ( try write filedesc data ! bytes_written bytes_to_write with Unix . Unix_error ( Unix . EAGAIN , _ , _ ) | Unix . Unix_error ( Unix . EWOULDBLOCK , _ , _ ) -> 0 ) in bytes_written := bytes + ! bytes_written ; end ; now := Unix . gettimeofday ( ) done ; if ! bytes_written = total_bytes_to_write then ( ) else raise Timeout |
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 = target_response_time . - ! now in let ( ready_to_read , _ , _ ) = Unix . select [ filedesc ] [ ] [ ] remaining_time in if List . mem filedesc ready_to_read then begin let bytes_to_read = total_bytes_to_read - ! bytes_read in let bytes = ( try Unix . read filedesc buf ! bytes_read bytes_to_read with Unix . Unix_error ( Unix . EAGAIN , _ , _ ) | Unix . Unix_error ( Unix . EWOULDBLOCK , _ , _ ) -> 0 ) in if bytes = 0 then raise End_of_file else bytes_read := bytes + ! bytes_read end ; now := Unix . gettimeofday ( ) done ; if ! bytes_read = total_bytes_to_read then ( Bytes . unsafe_to_string buf ) else raise Timeout |
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_read = ( if max_bytes < 0 || remaining_bytes > block_size then block_size else remaining_bytes ) in let bytes_read = Unix . read from_fd buf 0 bytes_to_read in if bytes_read = 0 then acc else begin f ( sub buf 0 bytes_read ) bytes_read ; do_read ( acc + bytes_read ) end end in do_read 0 |
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 ; 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 ( ) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.