| 1 |
(* |
| 2 |
* Copyright (C) 2006-2008 Vincent Hanquez <[email protected]> |
| 3 |
* Copyright (C) 2014 Peter Zotov <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU Lesser General Public License as published |
| 7 |
* by the Free Software Foundation; version 2.1 only. with the special |
| 8 |
* exception on linking described in file LICENSE. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU Lesser General Public License for more details. |
| 14 |
* |
| 15 |
*) |
| 16 |
|
| 17 |
(** Inotify binding for OCaml |
| 18 |
|
| 19 |
@see <http://man7.org/linux/man-pages/man7/inotify.7.html> Inotify manual page *) |
| 20 |
|
| 21 |
(** Type of event masks. *) |
| 22 |
type selector = |
| 23 |
| S_Access |
| 24 |
| S_Attrib |
| 25 |
| S_Close_write |
| 26 |
| S_Close_nowrite |
| 27 |
| S_Create |
| 28 |
| S_Delete |
| 29 |
| S_Delete_self |
| 30 |
| S_Modify |
| 31 |
| S_Move_self |
| 32 |
| S_Moved_from |
| 33 |
| S_Moved_to |
| 34 |
| S_Open |
| 35 |
| S_Dont_follow |
| 36 |
| S_Mask_add |
| 37 |
| S_Oneshot |
| 38 |
| S_Onlydir |
| 39 |
| S_Move |
| 40 |
| S_Close |
| 41 |
| S_All |
| 42 |
|
| 43 |
(** Type of observed events. *) |
| 44 |
type event_kind = |
| 45 |
| Access |
| 46 |
| Attrib |
| 47 |
| Close_write |
| 48 |
| Close_nowrite |
| 49 |
| Create |
| 50 |
| Delete |
| 51 |
| Delete_self |
| 52 |
| Modify |
| 53 |
| Move_self |
| 54 |
| Moved_from |
| 55 |
| Moved_to |
| 56 |
| Open |
| 57 |
| Ignored |
| 58 |
| Isdir |
| 59 |
| Q_overflow |
| 60 |
| Unmount |
| 61 |
|
| 62 |
(** Type of watch descriptors. *) |
| 63 |
type watch |
| 64 |
|
| 65 |
(** Type of received events, corresponding to [struct inotify_event]. |
| 66 |
In event [wd, kinds, cookie, path], [wd] corresponds to [inotify_event.wd], |
| 67 |
[kinds] corresponds to the bits set in [inotify_event.mask], [cookie] |
| 68 |
corresponds to [inotify_event.cookie], [path] is [Some filename] if |
| 69 |
[inotify_event.len > 0] and [None] otherwise. *) |
| 70 |
type event = watch * event_kind list * int32 * string option |
| 71 |
|
| 72 |
(** [int_of_watch wd] returns the underlying integer representation of |
| 73 |
watch descriptor [wd]. *) |
| 74 |
val int_of_watch : watch -> int |
| 75 |
|
| 76 |
(**/**) |
| 77 |
|
| 78 |
(* [watch_of_int i] is the {!watch} corresponding to the integer |
| 79 |
[i]. It violates the construction privacy of the {!watch} type but |
| 80 |
is useful when using {!event} as a network portable type. *) |
| 81 |
val watch_of_int : int -> watch |
| 82 |
|
| 83 |
(**/**) |
| 84 |
|
| 85 |
(** [string_of_event_kind ek] returns the string representation of event kind [ek], |
| 86 |
e.g. [string_of_event_kind Move_self] ≡ ["MOVE_SELF"]. *) |
| 87 |
val string_of_event_kind : event_kind -> string |
| 88 |
|
| 89 |
(** [string_of_event event] returns the string representation of event [ev], |
| 90 |
e.g. [string_of_event] *) |
| 91 |
val string_of_event : event -> string |
| 92 |
|
| 93 |
(** [create ()] returns a fresh inotify file descriptor or raises |
| 94 |
[Unix.Unix_error(errno, "inotify_init", "")]. *) |
| 95 |
val create : unit -> Unix.file_descr |
| 96 |
|
| 97 |
(** [add_watch fd path events] starts observing events from [events] for path [path] |
| 98 |
at inotify file descriptor [fd] and returns a fresh watch descriptor, or raises |
| 99 |
[Unix.Unix_error(errno, "inotify_add_watch", path)]. *) |
| 100 |
val add_watch : Unix.file_descr -> string -> selector list -> watch |
| 101 |
|
| 102 |
(** [rm_watch fd watch] stops observing events corresponding to watch descriptor [watch] |
| 103 |
at inotify file descriptor [fd], or raises |
| 104 |
[Unix.Unix_error(errno, "inotify_rm_watch", path)]. *) |
| 105 |
val rm_watch : Unix.file_descr -> watch -> unit |
| 106 |
|
| 107 |
(** [read fd] requests a list of events for inotify file descriptor [fd]. Each event |
| 108 |
will include the watch descriptor, which can be used to determine the path that |
| 109 |
caused it, and [Moved_to] and [Moved_from] events will include a cookie that allows |
| 110 |
to associate them with each other. |
| 111 |
|
| 112 |
If {!read} is not called often enough, the kernel event buffer may overflow, in which |
| 113 |
case the event kind list will consist of [[Q_overflow]]. Such an event would be |
| 114 |
associated with a watch descriptor [-1], never returned from {!add_watch}. *) |
| 115 |
val read : Unix.file_descr -> event list |
| 116 |
|
| 117 |
(** Returns [true] if inotify is supported by the OS. |
| 118 |
So effectively it returns true on linux and false elsewhere. *) |
| 119 |
val supported_by_the_os : unit -> bool |