-
Notifications
You must be signed in to change notification settings - Fork 3
/
inotify.ml
114 lines (101 loc) · 2.71 KB
/
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
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
(*
* Copyright (C) 2006-2008 Vincent Hanquez <vincent@snarc.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 only. with the special
* exception on linking described in file LICENSE.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* Inotify OCaml binding
*)
type select_event =
| S_Access
| S_Attrib
| S_Close_write
| S_Close_nowrite
| S_Create
| S_Delete
| S_Delete_self
| S_Modify
| S_Move_self
| S_Moved_from
| S_Moved_to
| S_Open
| S_Dont_follow
| S_Mask_add
| S_Oneshot
| S_Onlydir
(* convenience *)
| S_Move
| S_Close
| S_All
type type_event =
| Access
| Attrib
| Close_write
| Close_nowrite
| Create
| Delete
| Delete_self
| Modify
| Move_self
| Moved_from
| Moved_to
| Open
| Ignored
| Isdir
| Q_overflow
| Unmount
let string_of_event = function
| Access -> "ACCESS"
| Attrib -> "ATTRIB"
| Close_write -> "CLOSE_WRITE"
| Close_nowrite -> "CLOSE_NOWRITE"
| Create -> "CREATE"
| Delete -> "DELETE"
| Delete_self -> "DELETE_SELF"
| Modify -> "MODIFY"
| Move_self -> "MOVE_SELF"
| Moved_from -> "MOVED_FROM"
| Moved_to -> "MOVED_TO"
| Open -> "OPEN"
| Ignored -> "IGNORED"
| Isdir -> "ISDIR"
| Q_overflow -> "Q_OVERFLOW"
| Unmount -> "UNMOUNT"
let int_of_wd wd = wd
type wd = int
type event = wd * type_event list * int32 * string option
external init : unit -> Unix.file_descr = "stub_inotify_init"
external add_watch : Unix.file_descr -> string -> select_event list -> wd
= "stub_inotify_add_watch"
external rm_watch : Unix.file_descr -> wd -> unit = "stub_inotify_rm_watch"
external convert : string -> (wd * type_event list * int32 * int)
= "stub_inotify_convert"
external struct_size : unit -> int = "stub_inotify_struct_size"
external to_read : Unix.file_descr -> int = "stub_inotify_ioctl_fionread"
let read fd =
let ss = struct_size () in
let toread = to_read fd in
let ret = ref [] in
let buf = String.make toread '\000' in
let toread = Unix.read fd buf 0 toread in
let read_c_string offset len =
let index = ref 0 in
while !index < len && buf.[offset + !index] <> '\000' do incr index done;
String.sub buf offset !index
in
let i = ref 0 in
while !i < toread
do
let wd, l, cookie, len = convert (String.sub buf !i ss) in
let s = if len > 0 then Some (read_c_string (!i + ss) len) else None in
ret := (wd, l, cookie, s) :: !ret;
i := !i + (ss + len);
done;
List.rev !ret