-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathlwt_unix.cppo.mli
1591 lines (1208 loc) · 51.3 KB
/
lwt_unix.cppo.mli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* This file is part of Lwt, released under the MIT license. See LICENSE.md for
details, or visit https://github.com/ocsigen/lwt/blob/master/LICENSE.md. *)
(** Cooperative system calls *)
(** This modules maps system calls, like those of the standard
library's [Unix] module, to cooperative ones, which will not block
the program.
The semantics of all operations is the following: if the action
(for example reading from a {b file descriptor}) can be performed
immediately, it is performed and returns an already resolved promise,
otherwise it returns a pending promise which is resolved when the operation
completes.
Most operations on sockets and pipes (on Windows it is only
sockets) are {b cancelable}, meaning you can cancel them
with {!Lwt.cancel}. For example if you want to read something from
a {b file descriptor} with a timeout, you can cancel the action
after the timeout and the reading will not be performed if not
already done.
For example, consider that you have two sockets [sock1] and
[sock2]. You want to read something from [sock1] or exclusively
from [sock2] and fail with an exception if a timeout of 1 second
expires, without reading anything from [sock1] and [sock2], even
if they become readable in the future.
Then you can do:
{[
Lwt.pick
[Lwt_unix.timeout 1.0;
read sock1 buf1 ofs1 len1;
read sock2 buf2 ofs2 len2]
]}
In this case, it is guaranteed that exactly one of the three
operations will complete, and the others will be cancelled.
*)
val handle_unix_error : ('a -> 'b Lwt.t) -> 'a -> 'b Lwt.t
(** Same as [Unix.handle_unix_error] but catches lwt-level
exceptions *)
(** {2 Sleeping} *)
val sleep : float -> unit Lwt.t
(** [sleep d] is a promise that remains in a pending state for [d] seconds
and after which it is resolved with value [()]. *)
val yield : unit -> unit Lwt.t
(** [yield ()] is a promise in a pending state. It resumes itself as soon as
possible and resolves with value [()]. *)
val auto_yield : float -> (unit -> unit Lwt.t)
(** [auto_yield timeout] returns a function [f], and [f ()] has the following
behavior:
- If it has been more than [timeout] seconds since the last time [f ()]
behaved like {!Lwt_unix.yield}, [f ()] calls {!Lwt_unix.yield}.
- Otherwise, if it has been less than [timeout] seconds, [f ()] behaves
like {!Lwt.return_unit}, i.e. it does not yield. *)
exception Timeout
(** Exception raised by timeout operations *)
val timeout : float -> 'a Lwt.t
(** [timeout d] is a promise that remains pending for [d] seconds
and then is rejected with {!Timeout}. *)
val with_timeout : float -> (unit -> 'a Lwt.t) -> 'a Lwt.t
(** [with_timeout d f] is a short-hand for:
{[
Lwt.pick [Lwt_unix.timeout d; f ()]
]}
*)
(** {2 Operation on file-descriptors} *)
type file_descr
(** The abstract type for {b file descriptor}s. A Lwt {b file
descriptor} is a pair of a unix {b file descriptor} (of type
[Unix.file_descr]) and a {b state}.
A {b file descriptor} may be:
- {b opened}, in which case it is fully usable
- {b closed} or {b aborted}, in which case it is no longer
usable *)
(** State of a {b file descriptor} *)
type state =
| Opened
(** The {b file descriptor} is opened *)
| Closed
(** The {b file descriptor} has been closed by {!close}. It must
not be used for any operation. *)
| Aborted of exn
(** The {b file descriptor} has been aborted, the only operation
possible is {!close}, all others will fail. *)
val state : file_descr -> state
(** [state fd] returns the state of [fd] *)
val unix_file_descr : file_descr -> Unix.file_descr
(** Returns the underlying unix {b file descriptor}. It always
succeeds, even if the {b file descriptor}'s state is not
[Open]. *)
val of_unix_file_descr : ?blocking : bool -> ?set_flags : bool -> Unix.file_descr -> file_descr
(** Wraps a [Unix] file descriptor [fd] in an [Lwt_unix.file_descr] [fd'].
[~blocking] controls the {e internal} strategy Lwt uses to perform I/O on
the underlying [fd]. Regardless of [~blocking], at the API level,
[Lwt_unix.read], [Lwt_unix.write], etc. on [fd'] {e always} block the Lwt
thread, but {e never} block the whole process. However, for performance
reasons, it is important that [~blocking] match the actual blocking mode of
[fd].
If [~blocking] is not specified, [of_unix_file_descr] chooses non-blocking
mode for Unix sockets, Unix pipes, and Windows sockets, and blocking mode
for everything else. {b Note:} not specifying [~blocking] causes [fstat] to
be lazily called on [fd], the first time your code performs I/O on [fd'].
This [fstat] call can be expensive, so if you use [of_unix_file_descr] a
lot, be sure to specify [~blocking] explicitly.
[of_unix_file_descr] runs a system call to set the specified or chosen
blocking mode on the underlying [fd].
To prevent [of_unix_file_descr] from running this system call, you can pass
[~set_flags:false]. Note that, in this case, if [~blocking], whether passed
explicitly or chosen by Lwt, does not match the true blocking mode of the
underlying [fd], I/O on [fd'] will suffer performance degradation.
Note that [~set_flags] is effectively always [false] if running on Windows
and [fd] is not a socket.
Generally, non-blocking I/O is faster: for blocking I/O, Lwt typically has
to run system calls in worker threads to avoid blocking the process. See
your system documentation for whether particular kinds of file descriptors
support non-blocking I/O. *)
val blocking : file_descr -> bool Lwt.t
(** [blocking fd] indicates whether Lwt is internally using blocking or
non-blocking I/O with [fd].
Note that this may differ from the blocking mode of the underlying [Unix]
file descriptor (i.e. [unix_file_descr fd]).
See {!of_unix_file_descr} for details. *)
val set_blocking : ?set_flags : bool -> file_descr -> bool -> unit
(** [set_blocking fd b] causes Lwt to internally use blocking or non-blocking
I/O with [fd], according to the value of [b].
If [~set_flags] is [true] (the default), Lwt also makes a system call to set
the underlying file descriptor's blocking mode to match. Otherwise,
[set_blocking] is only informational for Lwt.
It is important that the underlying file descriptor actually have the same
blocking mode as that indicated by [b].
See {!of_unix_file_descr} for details. *)
val abort : file_descr -> exn -> unit
(** [abort fd exn] makes all current and further uses of the file
descriptor fail with the given exception. This put the {b file
descriptor} into the [Aborted] state.
If the {b file descriptor} is closed, this does nothing, if it is
aborted, this replace the abort exception by [exn].
Note that this only works for reading and writing operations on
file descriptors supporting non-blocking mode. *)
(** {2 Process handling} *)
val fork : unit -> int
(** [fork ()] does the same as [Unix.fork]. You must use this
function instead of [Unix.fork] when you want to use Lwt in the
child process.
Notes:
- In the child process all pending [Lwt_unix] I/O jobs are abandoned.
This may cause the child's copy of their associated promises to remain
forever pending.
- If you are going to use Lwt in the parent and the child, it is
a good idea to call {!Lwt_io.flush_all} before callling
{!fork} to avoid double-flush.
- Otherwise, if you will not use Lwt in the child, call
{!Lwt_main.Exit_hooks.remove_all} to avoid Lwt calling {!Lwt_main.run}
during process exit.
- None of the above is necessary if you intend to call [exec]. Indeed, in
that case, it is not even necessary to use [Lwt_unix.fork]. You can use
[Unix.fork].
- To abandon some more promises, see
{!Lwt_main.abandon_yielded_and_paused}. *)
type process_status =
Unix.process_status =
| WEXITED of int
| WSIGNALED of int
| WSTOPPED of int
type wait_flag =
Unix.wait_flag =
| WNOHANG
| WUNTRACED
val wait : unit -> (int * process_status) Lwt.t
(** Wrapper for [Unix.wait] *)
val waitpid : wait_flag list -> int -> (int * process_status) Lwt.t
(** A promise-returning analog to
{{: https://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#VALwaitpid}
[Unix.waitpid]}. This call is non-blocking on Unix-like systems, but is
always blocking on Windows. *)
(** Resource usages *)
type resource_usage = {
ru_utime : float;
(** User time used *)
ru_stime : float;
(** System time used *)
}
val wait4 : wait_flag list -> int -> (int * process_status * resource_usage) Lwt.t
(** [wait4 flags pid] returns [(pid, status, rusage)] where [(pid, status)]
is the same result as [Unix.waitpid flags pid], and
[rusage] contains accounting information about the child.
On windows it will always returns [{ utime = 0.0; stime = 0.0 }]. *)
val wait_count : unit -> int
(** Returns the number of threads waiting for a child to
terminate. *)
val system : string -> process_status Lwt.t
(** Executes the given command, waits until it terminates, and
return its termination status. The string is interpreted by the
shell [/bin/sh] on Unix and [cmd.exe] on Windows. The result
[WEXITED 127] indicates that the shell couldn't be executed. *)
(** {2 Basic file input/output} *)
val stdin : file_descr
(** The standard {b file descriptor} for input. This one is usually
a terminal is the program is started from a terminal. *)
val stdout : file_descr
(** The standard {b file descriptor} for output *)
val stderr : file_descr
(** The standard {b file descriptor} for printing error messages *)
type file_perm = Unix.file_perm
type open_flag =
Unix.open_flag =
| O_RDONLY
| O_WRONLY
| O_RDWR
| O_NONBLOCK
| O_APPEND
| O_CREAT
| O_TRUNC
| O_EXCL
| O_NOCTTY
| O_DSYNC
| O_SYNC
| O_RSYNC
| O_SHARE_DELETE
| O_CLOEXEC
#if OCAML_VERSION >= (4, 05, 0)
| O_KEEPEXEC
#endif
val openfile : string -> open_flag list -> file_perm -> file_descr Lwt.t
(** Wrapper for [Unix.openfile]. *)
val close : file_descr -> unit Lwt.t
(** Close a {b file descriptor}. This close the underlying unix {b
file descriptor} and set its state to [Closed]. *)
val read : file_descr -> bytes -> int -> int -> int Lwt.t
(** [read fd buf ofs len] reads up to [len] bytes from [fd], and writes them to
[buf], starting at offset [ofs]. The function immediately evaluates to an
Lwt thread, which waits for the operation to complete. If it completes
successfully, the thread indicates the number of bytes actually read, or
zero if the end of file has been reached.
Note that the Lwt thread waits for data (or end of file) even if the
underlying file descriptor is in non-blocking mode. See
{!of_unix_file_descr} for a discussion of non-blocking I/O and Lwt.
If Lwt is using blocking I/O on [fd], [read] writes data into a temporary
buffer, then copies it into [buf].
The thread can fail with any exception that can be raised by [Unix.read],
except [Unix.Unix_error Unix.EAGAIN], [Unix.Unix_error Unix.EWOULDBLOCK] or
[Unix.Unix_error Unix.EINTR]. *)
val pread : file_descr -> bytes -> file_offset:int -> int -> int -> int Lwt.t
(** [pread fd buf ~file_offset ofs len] on file descriptors allowing seek,
reads up to [len] bytes from [fd] at offset [file_offset] from the
beginning of the file, and writes them to [buf], starting at offset [ofs].
On Unix systems, the file descriptor position is unaffected. On Windows
it is changed to be just after the last read position.
The thread can fail with any exception that can be raised by [read] or
[lseek]. *)
val write : file_descr -> bytes -> int -> int -> int Lwt.t
(** [write fd buf ofs len] writes up to [len] bytes to [fd] from [buf], starting
at buffer offset [ofs]. The function immediately evaluates to an Lwt thread,
which waits for the operation to complete. If the operation completes
successfully, the thread indicates the number of bytes actually written,
which may be less than [len].
Note that the Lwt thread waits to write even if the underlying file
descriptor is in non-blocking mode. See {!of_unix_file_descr} for a
discussion of non-blocking I/O and Lwt.
If Lwt is using blocking I/O on [fd], [buf] is copied before writing.
The thread can fail with any exception that can be raised by
[Unix.single_write], except [Unix.Unix_error Unix.EAGAIN],
[Unix.Unix_error Unix.EWOULDBLOCK] or [Unix.Unix_error Unix.EINTR]. *)
val pwrite : file_descr -> bytes -> file_offset:int -> int -> int -> int Lwt.t
(** [pwrite fd buf ~file_offset ofs len] on file descriptors allowing seek,
writes up to [len] bytes to [fd] from [buf], starting at buffer offset
[ofs]. The data is written at offset [file_offset] from the beginning
of [fd].
On Unix systems, the file descriptor position is unaffected. On Windows
it is changed to be just after the last written position.
The thread can fail with any exception that can be raised by [write] or
[lseek]. *)
val write_string : file_descr -> string -> int -> int -> int Lwt.t
(** See {!write}. *)
val pwrite_string :
file_descr -> string -> file_offset:int -> int -> int -> int Lwt.t
(** See {!pwrite}. *)
(** Sequences of buffer slices for {!writev}. *)
module IO_vectors :
sig
type t
(** Mutable sequences of I/O vectors. An I/O vector describes a slice of a
[bytes] or [Bigarray] buffer. Each I/O vector is a triple containing a
reference to the buffer, an offset into the buffer where the slice begins,
and the length of the slice. *)
type _bigarray =
(char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
(** Type abbreviation equivalent to {!Lwt_bytes.t}. Do not use this type name
directly; use {!Lwt_bytes.t} instead. *)
val create : unit -> t
(** Creates an empty I/O vector sequence. *)
val append_bytes : t -> bytes -> int -> int -> unit
(** [append_bytes vs buffer offset length] appends a slice of the [bytes]
buffer [buffer] beginning at [offset] and with length [length] to the
I/O vector sequence [vs]. *)
val append_bigarray : t -> _bigarray -> int -> int -> unit
(** [append_bigarray vs buffer offset length] appends a slice of the
[Bigarray] buffer [buffer] beginning at [offset] and with length [length]
to the I/O vector sequence [vs]. *)
val drop : t -> int -> unit
(** [drop vs n] adjusts the I/O vector sequence [vs] so that it no longer
includes its first [n] bytes. *)
val is_empty : t -> bool
(** [is_empty vs] is [true] if and only if [vs] has no I/O vectors, or all I/O
vectors in [vs] have zero bytes. *)
val byte_count : t -> int
(** [byte_count vs] is the total number of bytes in [vs].
@since 4.2.0 *)
val system_limit : int option
(** Some systems limit the number of I/O vectors that can be passed in a
single call to their [writev] or [readv] system calls. On those systems,
if the limit is [n], this value is equal to [Some n]. On systems without
such a limit, the value is equal to [None].
Unless you need atomic I/O operations, you can ignore this limit. The Lwt
binding automatically respects it internally. See {!Lwt_unix.writev}.
A typical limit is 1024 vectors. *)
end
val readv : file_descr -> IO_vectors.t -> int Lwt.t
(** [readv fd vs] reads bytes from [fd] into the buffer slices [vs]. If the
operation completes successfully, the resulting thread indicates the number
of bytes read.
Data is always read directly into [Bigarray] slices. If the Unix file
descriptor underlying [fd] is in non-blocking mode, data is also read
directly into [bytes] slices. Otherwise, data for [bytes] slices is first
read into temporary buffers, then copied.
Note that the returned Lwt thread is blocked until failure or a successful
read, even if the underlying file descriptor is in non-blocking mode. See
{!of_unix_file_descr} for a discussion of non-blocking I/O and Lwt.
If {!IO_vectors.system_limit} is [Some n] and the count of slices in [vs]
exceeds [n], then [Lwt_unix.readv] reads only into the first [n] slices of
[vs].
Not implemented on Windows. It should be possible to implement, upon
request, for Windows sockets only.
See {{:http://man7.org/linux/man-pages/man3/readv.3p.html} [readv(3p)]}.
@since 2.7.0 *)
val writev : file_descr -> IO_vectors.t -> int Lwt.t
(** [writev fd vs] writes the bytes in the buffer slices [vs] to the file
descriptor [fd]. If the operation completes successfully, the resulting
thread indicates the number of bytes written.
If the Unix file descriptor underlying [fd] is in non-blocking mode,
[writev] does not make a copy the bytes before writing. Otherwise, it copies
[bytes] slices, but not [Bigarray] slices.
Note that the returned Lwt thread is blocked until failure or a successful
write, even if the underlying descriptor is in non-blocking mode. See
{!of_unix_file_descr} for a discussion of non-blocking I/O and Lwt.
If {!IO_vectors.system_limit} is [Some n] and the count of slices in [vs]
exceeds [n], then [Lwt_unix.writev] passes only the first [n] slices in [vs]
to the underlying [writev] system call.
Not implemented on Windows. It should be possible to implement, upon
request, for Windows sockets only.
The behavior of [writev] when [vs] has zero slices depends on the system,
and may change in future versions of Lwt. On Linux, [writev] will succeed
and write zero bytes. On BSD (including macOS), [writev] will fail with
[Unix.Unix_error (Unix.EINVAL, "writev", ...)].
See {{:http://man7.org/linux/man-pages/man3/writev.3p.html}
[writev(3p)]}.
@since 2.7.0 *)
val readable : file_descr -> bool
(** Returns whether the given file descriptor is currently
readable. *)
val writable : file_descr -> bool
(** Returns whether the given file descriptor is currently
writable. *)
val wait_read : file_descr -> unit Lwt.t
(** Waits (without blocking other threads) until there is something
to read from the file descriptor.
Note that you don't need to use this function if you are
using Lwt I/O functions for reading, since they provide
non-blocking waiting automatically.
The intended use case for this function is interfacing with
existing libraries that are known to be blocking. *)
val wait_write : file_descr -> unit Lwt.t
(** Waits (without blocking other threads) until it is possible to
write on the file descriptor.
Note that you don't need to use this function if you are
using Lwt I/O functions for writing, since they provide
non-blocking waiting automatically.
The intended use case for this function is interfacing with
existing libraries that are known to be blocking. *)
(** {2 Seeking and truncating} *)
type seek_command =
Unix.seek_command =
| SEEK_SET
| SEEK_CUR
| SEEK_END
val lseek : file_descr -> int -> seek_command -> int Lwt.t
(** Wrapper for [Unix.lseek] *)
val truncate : string -> int -> unit Lwt.t
(** Wrapper for [Unix.truncate] *)
val ftruncate : file_descr -> int -> unit Lwt.t
(** Wrapper for [Unix.ftruncate] *)
(** {2 Syncing} *)
val fsync : file_descr -> unit Lwt.t
(** Synchronise all data and metadata of the file descriptor with
the disk. On Windows it uses [FlushFileBuffers]. *)
val fdatasync : file_descr -> unit Lwt.t
(** Synchronise all data (but not metadata) of the file descriptor
with the disk.
Note that [fdatasync] is not available on Windows and OS X. *)
(** {2 File status} *)
type file_kind =
Unix.file_kind =
| S_REG
| S_DIR
| S_CHR
| S_BLK
| S_LNK
| S_FIFO
| S_SOCK
type stats =
Unix.stats =
{
st_dev : int;
st_ino : int;
st_kind : file_kind;
st_perm : file_perm;
st_nlink : int;
st_uid : int;
st_gid : int;
st_rdev : int;
st_size : int;
st_atime : float;
st_mtime : float;
st_ctime : float;
}
val stat : string -> stats Lwt.t
(** Wrapper for [Unix.stat] *)
val lstat : string -> stats Lwt.t
(** Wrapper for [Unix.lstat] *)
val fstat : file_descr -> stats Lwt.t
(** Wrapper for [Unix.fstat] *)
val file_exists : string -> bool Lwt.t
(** [file_exists name] tests if a file named [name] exists.
Note that [file_exists] behaves similarly to
{{:http://caml.inria.fr/pub/docs/manual-ocaml/libref/Sys.html#VALfile_exists}
[Sys.file_exists]}:
- "file" is interpreted as "directory entry" in this context
- [file_exists name] will return [false] in
circumstances that would make {!stat} raise a
{{:http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#EXCEPTIONUnix_error}
[Unix.Unix_error]} exception.
*)
val utimes : string -> float -> float -> unit Lwt.t
(** [utimes path atime mtime] updates the access and modification times of the
file at [path]. The access time is set to [atime] and the modification time
to [mtime]. To set both to the current time, call [utimes path 0. 0.].
This function corresponds to
{{:http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#VALutimes}
[Unix.utimes]}. See also
{{:http://man7.org/linux/man-pages/man3/utimes.3p.html} [utimes(3p)]}.
@since 2.6.0 *)
val isatty : file_descr -> bool Lwt.t
(** Wrapper for [Unix.isatty] *)
(** {2 File operations on large files} *)
module LargeFile : sig
val lseek : file_descr -> int64 -> seek_command -> int64 Lwt.t
(** Wrapper for [Unix.LargeFile.lseek] *)
val truncate : string -> int64 -> unit Lwt.t
(** Wrapper for [Unix.LargeFile.truncate] *)
val ftruncate : file_descr -> int64 -> unit Lwt.t
(** Wrapper for [Unix.LargeFile.ftruncate] *)
type stats =
Unix.LargeFile.stats =
{
st_dev : int;
st_ino : int;
st_kind : file_kind;
st_perm : file_perm;
st_nlink : int;
st_uid : int;
st_gid : int;
st_rdev : int;
st_size : int64;
st_atime : float;
st_mtime : float;
st_ctime : float;
}
val stat : string -> stats Lwt.t
(** Wrapper for [Unix.LargeFile.stat] *)
val lstat : string -> stats Lwt.t
(** Wrapper for [Unix.LargeFile.lstat] *)
val fstat : file_descr -> stats Lwt.t
(** Wrapper for [Unix.LargeFile.fstat] *)
val file_exists : string -> bool Lwt.t
(** [file_exists name] tests if a file named [name] exists.
Note that [file_exists] behaves similarly to
{{:http://caml.inria.fr/pub/docs/manual-ocaml/libref/Sys.html#VALfile_exists}
[Sys.file_exists]}:
- "file" is interpreted as "directory entry" in this context
- [file_exists name] will return [false] in
circumstances that would make {!stat} raise a
{{:http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#EXCEPTIONUnix_error}
[Unix.Unix_error]} exception.
*)
end
(** {2 Operations on file names} *)
val unlink : string -> unit Lwt.t
(** Wrapper for [Unix.unlink] *)
val rename : string -> string -> unit Lwt.t
(** Wrapper for [Unix.rename] *)
val link : string -> string -> unit Lwt.t
(** Wrapper for [Unix.link] *)
(** {2 File permissions and ownership} *)
val chmod : string -> file_perm -> unit Lwt.t
(** Wrapper for [Unix.chmod] *)
val fchmod : file_descr -> file_perm -> unit Lwt.t
(** Wrapper for [Unix.fchmod] *)
val chown : string -> int -> int -> unit Lwt.t
(** Wrapper for [Unix.chown] *)
val fchown : file_descr -> int -> int -> unit Lwt.t
(** Wrapper for [Unix.fchown] *)
type access_permission =
Unix.access_permission =
| R_OK
| W_OK
| X_OK
| F_OK
val access : string -> access_permission list -> unit Lwt.t
(** Wrapper for [Unix.access] *)
(** {2 Operations on file descriptors} *)
val dup : file_descr -> file_descr
(** Wrapper for [Unix.dup] *)
val dup2 : file_descr -> file_descr -> unit
(** Wrapper for [Unix.dup2] *)
val set_close_on_exec : file_descr -> unit
(** Wrapper for [Unix.set_close_on_exec] *)
val clear_close_on_exec : file_descr -> unit
(** Wrapper for [Unix.clear_close_on_exec] *)
(** {2 Directories} *)
val mkdir : string -> file_perm -> unit Lwt.t
(** Wrapper for [Unix.mkdir] *)
val rmdir : string -> unit Lwt.t
(** Wrapper for [Unix.rmdir] *)
val chdir : string -> unit Lwt.t
(** Wrapper for [Unix.chdir] *)
val getcwd : unit -> string Lwt.t
(** Wrapper for [Unix.getcwd]
@since 3.1.0 *)
val chroot : string -> unit Lwt.t
(** Wrapper for [Unix.chroot] *)
type dir_handle = Unix.dir_handle
val opendir : string -> dir_handle Lwt.t
(** Opens a directory for listing. Directories opened with this function must be
explicitly closed with {!closedir}. This is a cooperative analog of
{{:http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#VALopendir}
[Unix.opendir]}. *)
val readdir : dir_handle -> string Lwt.t
(** Reads the next directory entry from the given directory. Special entries
such as [.] and [..] are included. If all entries have been read, raises
[End_of_file]. This is a cooperative analog of
{{:http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#VALreaddir}
[Unix.readdir]}. *)
val readdir_n : dir_handle -> int -> string array Lwt.t
(** [readdir_n handle count] reads at most [count] entries from the
given directory. It is more efficient than calling [readdir]
[count] times. If the length of the returned array is smaller
than [count], this means that the end of the directory has been
reached. *)
val rewinddir : dir_handle -> unit Lwt.t
(** Resets the given directory handle, so that directory listing can be
restarted. Cooperative analog of
{{:http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#VALrewinddir}
[Unix.rewinddir]}. *)
val closedir : dir_handle -> unit Lwt.t
(** Closes a directory handle. Cooperative analog of
{{:http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#VALclosedir}
[Unix.closedir]}. *)
val files_of_directory : string -> string Lwt_stream.t
(** [files_of_directory dir] returns the stream of all files of
[dir]. *)
(** {2 Pipes and redirections} *)
val pipe : unit -> file_descr * file_descr
(** [pipe ()] creates pipe using [Unix.pipe] and returns two lwt {b
file descriptor}s created from unix {b file_descriptor} *)
val pipe_in : unit -> file_descr * Unix.file_descr
(** [pipe_in ()] is the same as {!pipe} but maps only the unix {b
file descriptor} for reading into a lwt one. The second is not
put into non-blocking mode. You usually want to use this before
forking to receive data from the child process. *)
val pipe_out : unit -> Unix.file_descr * file_descr
(** [pipe_out ()] is the inverse of {!pipe_in}. You usually want to
use this before forking to send data to the child process *)
val mkfifo : string -> file_perm -> unit Lwt.t
(** Wrapper for [Unix.mkfifo] *)
(** {2 Symbolic links} *)
val symlink : string -> string -> unit Lwt.t
(** Wrapper for [Unix.symlink] *)
val readlink : string -> string Lwt.t
(** Wrapper for [Unix.readlink] *)
(** {2 Locking} *)
type lock_command =
Unix.lock_command =
| F_ULOCK
| F_LOCK
| F_TLOCK
| F_TEST
| F_RLOCK
| F_TRLOCK
val lockf : file_descr -> lock_command -> int -> unit Lwt.t
(** Wrapper for [Unix.lockf] *)
(** {2 User id, group id} *)
type passwd_entry =
Unix.passwd_entry =
{
pw_name : string;
pw_passwd : string;
pw_uid : int;
pw_gid : int;
pw_gecos : string;
pw_dir : string;
pw_shell : string
}
type group_entry =
Unix.group_entry =
{
gr_name : string;
gr_passwd : string;
gr_gid : int;
gr_mem : string array
}
val getlogin : unit -> string Lwt.t
(** Wrapper for [Unix.getlogin] *)
val getpwnam : string -> passwd_entry Lwt.t
(** Wrapper for [Unix.getpwnam] *)
val getgrnam : string -> group_entry Lwt.t
(** Wrapper for [Unix.getgrnam] *)
val getpwuid : int -> passwd_entry Lwt.t
(** Wrapper for [Unix.getpwuid] *)
val getgrgid : int -> group_entry Lwt.t
(** Wrapper for [Unix.getgrgid] *)
(** {2 Signals} *)
type signal_handler_id
(** Id of a signal handler, used to cancel it *)
val on_signal : int -> (int -> unit) -> signal_handler_id
(** [on_signal signum f] calls [f] each time the signal with numnber
[signum] is received by the process. It returns a signal handler
identifier that can be used to stop monitoring [signum]. *)
val on_signal_full : int -> (signal_handler_id -> int -> unit) -> signal_handler_id
(** [on_signal_full f] is the same as [on_signal f] except that [f]
also receive the signal handler identifier as argument so it can
disable it. *)
val disable_signal_handler : signal_handler_id -> unit
(** Stops receiving this signal *)
val signal_count : unit -> int
(** Returns the number of registered signal handler. *)
val reinstall_signal_handler : int -> unit
(** [reinstall_signal_handler signum] if any signal handler is
registered for this signal with {!on_signal}, it reinstall the
signal handler (with [Sys.set_signal]). This is useful in case
another part of the program install another signal handler. *)
(** {2 Sockets} *)
type inet_addr = Unix.inet_addr
type socket_domain =
Unix.socket_domain =
| PF_UNIX
| PF_INET
| PF_INET6
type socket_type =
Unix.socket_type =
| SOCK_STREAM
| SOCK_DGRAM
| SOCK_RAW
| SOCK_SEQPACKET
type sockaddr = Unix.sockaddr = ADDR_UNIX of string | ADDR_INET of inet_addr * int
val socket : socket_domain -> socket_type -> int -> file_descr
(** [socket domain type proto] is the same as [Unix.socket] but maps
the result into a lwt {b file descriptor} *)
val socketpair : socket_domain -> socket_type -> int -> file_descr * file_descr
(** Wrapper for [Unix.socketpair] *)
val bind : file_descr -> sockaddr -> unit Lwt.t
(** Binds an address to the given socket. This is the cooperative analog of
{{:http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#VALbind}
[Unix.bind]}. See also
{{:http://man7.org/linux/man-pages/man3/bind.3p.html} [bind(3p)]}.
@since 3.0.0 *)
val listen : file_descr -> int -> unit
(** Wrapper for [Unix.listen] *)
val accept : file_descr -> (file_descr * sockaddr) Lwt.t
(** Wrapper for [Unix.accept] *)
val accept_n : file_descr -> int -> ((file_descr * sockaddr) list * exn option) Lwt.t
(** [accept_n fd count] accepts up to [count] connections at one time.
- if no connection is available right now, it returns a sleeping
thread
- if more than 1 and less than [count] are available, it returns
all of them
- if more than [count] are available, it returns the next
[count] of them
- if an error happens, it returns the connections that have been
successfully accepted so far and the error
[accept_n] has the advantage of improving performance. If you
want a more detailed description, you can have a look at:
{{:http://portal.acm.org/citation.cfm?id=1247435}Acceptable strategies for improving web server performance} *)
val connect : file_descr -> sockaddr -> unit Lwt.t
(** Wrapper for [Unix.connect] *)
type shutdown_command =
Unix.shutdown_command =
| SHUTDOWN_RECEIVE
| SHUTDOWN_SEND
| SHUTDOWN_ALL
val shutdown : file_descr -> shutdown_command -> unit
(** Wrapper for [Unix.shutdown] *)
val getsockname : file_descr -> sockaddr
(** Wrapper for [Unix.getsockname] *)
val getpeername : file_descr -> sockaddr
(** Wrapper for [Unix.getpeername] *)
type msg_flag =
Unix.msg_flag =
| MSG_OOB
| MSG_DONTROUTE
| MSG_PEEK
val recv : file_descr -> bytes -> int -> int -> msg_flag list -> int Lwt.t
(** Wrapper for [Unix.recv].
On Windows, [recv] writes data into a temporary buffer, then copies it into
the given one. *)
val recvfrom : file_descr -> bytes -> int -> int -> msg_flag list -> (int * sockaddr) Lwt.t
(** Wrapper for [Unix.recvfrom].
On Windows, [recvfrom] writes data into a temporary buffer, then copies it
into the given one. *)
val send : file_descr -> bytes -> int -> int -> msg_flag list -> int Lwt.t
(** Wrapper for [Unix.send].
On Windows, [send] copies the given buffer before writing. *)
val sendto : file_descr -> bytes -> int -> int -> msg_flag list -> sockaddr -> int Lwt.t
(** Wrapper for [Unix.sendto].
On Windows, [sendto] copies the given buffer before writing. *)
val recv_msg :
socket:file_descr -> io_vectors:IO_vectors.t ->
(int * Unix.file_descr list) Lwt.t
(** [recv_msg ~socket ~io_vectors] receives data into a list of
io-vectors, plus any file-descriptors that may accompany the
messages. It returns a tuple whose first field is the number of
bytes received and second is a list of received file
descriptors. The messages themselves will be recorded in the
provided [io_vectors] list. Data is written directly into the
[iov_buffer] buffers.
Not implemented on Windows.
@since 5.0.0 *)
val send_msg :
socket:file_descr -> io_vectors:IO_vectors.t -> fds:Unix.file_descr list ->
int Lwt.t
(** [send_msg ~socket ~io_vectors ~fds] sends data from a list of
io-vectors, accompanied with a list of file-descriptors. It
returns the number of bytes sent. If fd-passing is not possible on
the current system and [fds] is not empty, it raises
[Lwt_sys.Not_available "fd_passing"]. Data is written directly from
the [io_vectors] buffers.
Not implemented on Windows.
@since 5.0.0 *)
val send_msgto :
socket:file_descr -> io_vectors:IO_vectors.t -> fds:Unix.file_descr list ->
dest:Unix.sockaddr ->
int Lwt.t
(** [send_msgto ~socket ~io_vectors ~fds ~dest] is similar to [send_msg] but
takes an additional [dest] argument to set the address when using a
connection-less socket.
Not implemented on Windows.