package containers
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=92143ceb4785ae5f8a07f3ab4ab9f6f32d31ead0536e9be4fdb818dd3c677e58
sha512=5fa80189d0e177af2302b48e72b70299d51fc36ac2019e1cbf389ff6a7f4705b10089405b5a719b3e4845b0d1349a47a967f865dc2e4e3f0d5a0167ef6c31431
doc/containers.unix/CCUnix/index.html
Module CCUnix
Source
High-level Functions on top of Unix
Some useful functions built on top of Unix.
status: unstable
Calling Commands
Escape a string so it can be a shell argument.
type call_result =
< stdout : string
; stderr : string
; status : Unix.process_status
; errcode : int >
val call_full :
?bufsize:int ->
?stdin:[ `Gen of string gen | `Str of string ] ->
?env:string array ->
('a, Buffer.t, unit, call_result) format4 ->
'a
call_full cmd
wraps the result of Unix.open_process_full cmd
into an object. It reads the full stdout and stderr of the subprocess before returning. cmd
can be a format string as in Printf
.
Example:
# CCUnix.call_full "ls %s" (CCUnix.escape_str "/");;
val call :
?bufsize:int ->
?stdin:[ `Gen of string gen | `Str of string ] ->
?env:string array ->
('a, Buffer.t, unit, string * string * int) format4 ->
'a
call cmd
is similar to call_full
but returns a tuple stdout, stderr, errcode
instead of an object.
type async_call_result =
< stdout : line gen
; stderr : line gen
; stdin : line -> unit
; close_in : unit
; close_err : unit
; close_out : unit
; close_all : unit
; wait : Unix.process_status
; wait_errcode : int >
A subprocess for interactive usage (read/write channels line by line)
Spawns a subprocess, like call
, but the subprocess's channels are line generators and line sinks (for stdin). If p
is async_call "cmd"
, then p#wait
waits for the subprocess to die. Channels can be closed independently.
Accessors
Simple IO
Open an input file with the given optional flag list, calls the function on the input channel. When the function raises or returns, the channel is closed.
val with_out :
?mode:int ->
?flags:Unix.open_flag list ->
string ->
f:(out_channel -> 'a) ->
'a
Same as with_in
but for an output channel.
Subprocesses
Open a shell command in a subprocess and obtain a handle to its stdout.
CCUnix.with_process_in "ls /tmp" ~f:CCIO.read_lines_l;;
Open a shell command in a subprocess and obtain a handle to its stdin.
type process_full =
< stdin : out_channel
; stdout : in_channel
; stderr : in_channel
; close : Unix.process_status >
Handle to a subprocess.
Open a subprocess and obtain a handle to its channels.
On unix, call Unix.setsid()
to make sure subprocesses die at the same time as the current process. Does nothing on windows. Idempotent: it can be called several times but will only have effects, if any, the first time.
Networking
Wrap Unix.open_connection
with a handler.
Listen on the address and calls the handler in a blocking fashion. Using Thread
is recommended if handlers might take time. The callback should raise ExitServer
to stop the loop.
File lock
with_file_lock ~kind filename f
puts a lock on the offset 0 of the file named filename
, calls f
and returns its result after the file is unlocked. If f ()
raises an exception the exception is re-raised after the file is unlocked.
Infix Functions
Temporary directory
Create a temporary directory, call the function, and then destroy the directory afterwards. Usage with_temp_dir pattern f
.
Note that this is implemented following the discussion at: https://discuss.ocaml.org/t/how-to-create-a-temporary-directory-in-ocaml/1815/