add -fPIC option to the C compiler, required in f31
[vsys.git] / ocaml_inotify-0.4 / inotify.ml
1 (*
2  * Copyright (C) 2006 Vincent Hanquez <vincent@snarc.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; version 2 only.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * Inotify OCaml binding
14  *)
15
16 type select_event =
17         | S_Access
18         | S_Attrib
19         | S_Close_write
20         | S_Close_nowrite
21         | S_Create
22         | S_Delete
23         | S_Delete_self
24         | S_Modify
25         | S_Move_self
26         | S_Moved_from
27         | S_Moved_to
28         | S_Open
29         | S_Dont_follow
30         | S_Mask_add
31         | S_Oneshot
32         | S_Onlydir
33         (* convenience *)
34         | S_Move
35         | S_Close
36         | S_All
37
38 type type_event =
39         | Access
40         | Attrib
41         | Close_write
42         | Close_nowrite
43         | Create
44         | Delete
45         | Delete_self
46         | Modify
47         | Move_self
48         | Moved_from
49         | Moved_to
50         | Open
51         | Ignored
52         | Isdir
53         | Q_overflow
54         | Unmount
55
56 let string_of_event = function
57         | Access -> "ACCESS"
58         | Attrib -> "ATTRIB"
59         | Close_write -> "CLOSE_WRITE"
60         | Close_nowrite -> "CLOSE_NOWRITE"
61         | Create -> "CREATE"
62         | Delete -> "DELETE"
63         | Delete_self -> "DELETE_SELF"
64         | Modify -> "MODIFY"
65         | Move_self -> "MOVE_SELF"
66         | Moved_from -> "MOVED_FROM"
67         | Moved_to -> "MOVED_TO"
68         | Open -> "OPEN"
69         | Ignored -> "IGNORED"
70         | Isdir -> "ISDIR"
71         | Q_overflow -> "Q_OVERFLOW"
72         | Unmount -> "UNMOUNT"
73
74 let int_of_wd wd = wd
75
76 type wd = int
77 type event = wd * type_event list * int32 * string option
78
79 external init : unit -> Unix.file_descr = "stub_inotify_init"
80 external add_watch : Unix.file_descr -> string -> select_event list -> wd
81                    = "stub_inotify_add_watch"
82 external rm_watch : Unix.file_descr -> wd -> unit = "stub_inotify_rm_watch"
83 external convert : string -> (wd * type_event list * int32 * int)
84                  = "stub_inotify_convert"
85 external struct_size : unit -> int = "stub_inotify_struct_size"
86
87 external to_read : Unix.file_descr -> int = "stub_inotify_ioctl_fionread"
88
89 let read fd =
90         let ss = struct_size () in
91         let toread = to_read fd in
92
93         let ret = ref [] in
94         let buf = String.make toread '\000' in
95         let toread = Unix.read fd buf 0 toread in
96
97         let i = ref 0 in
98
99         while !i < toread
100         do
101                 let wd, l, cookie, len = convert (String.sub buf !i ss) in
102                 let s = if len > 0 then Some (String.sub buf (!i + ss) len) else None in
103                 ret := (wd, l, cookie, s) :: !ret;
104                 i := !i + (ss + len);
105         done;
106
107         List.rev !ret