Source file lwt_inotify.ml
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
open Lwt
type t = {
queue : Inotify.event Queue.t;
unix_fd : Unix.file_descr;
lwt_fd : Lwt_unix.file_descr;
}
let create' () =
let unix_fd = Inotify.create () in
{
queue = Queue.create ();
lwt_fd = Lwt_unix.of_unix_file_descr unix_fd;
unix_fd;
}
let create () = try return (create' ()) with exn -> Lwt.fail exn
let add_watch' inotify path selector =
Inotify.add_watch inotify.unix_fd path selector
let add_watch inotify path selector =
try
return (add_watch' inotify path selector)
with exn -> Lwt.fail exn
let rm_watch' inotify wd =
Inotify.rm_watch inotify.unix_fd wd
let rm_watch inotify wd =
try
return (rm_watch' inotify wd)
with exn ->
Lwt.fail exn
let rec read inotify =
try
return (Queue.take inotify.queue)
with Queue.Empty ->
Lwt_unix.wait_read inotify.lwt_fd >>= fun () ->
begin try
let events = Inotify.read inotify.unix_fd in
List.iter (fun event -> Queue.push event inotify.queue) events;
return_unit
with exn ->
Lwt.fail exn
end >>= fun () ->
read inotify
let rec try_read inotify =
try
return (Some (Queue.take inotify.queue))
with Queue.Empty ->
if Lwt_unix.readable inotify.lwt_fd
then read inotify >|= fun x -> Some x
else return_none
let close inotify =
Lwt_unix.close inotify.lwt_fd