Inotify.dcl 5.67 KB
Newer Older
Camil Staps's avatar
Camil Staps committed
1 2
definition module Inotify

Camil Staps's avatar
Camil Staps committed
3 4 5 6
/**
 * Links with Linux's inotify
 */

Camil Staps's avatar
Camil Staps committed
7 8 9
from Data.Either import ::Either
from Data.Maybe import ::Maybe

Camil Staps's avatar
Camil Staps committed
10
/**
11 12
 * An inotify file descriptor.
 * @var A state that can be used for application-specific information.
Camil Staps's avatar
Camil Staps committed
13
 */
14 15 16 17 18
:: *Inotify st =
	{ fd      :: !Int
	, watches :: ![(INWatch, INCallback st)]
	, state   :: !st
	}
Camil Staps's avatar
Camil Staps committed
19

20
//* An inotify watch descriptor
Camil Staps's avatar
Camil Staps committed
21 22
:: INWatch

23
//* An inotify event mask
Camil Staps's avatar
Camil Staps committed
24 25
:: INMask :== Int

26
//* An inotify event
Camil Staps's avatar
Camil Staps committed
27
:: INEvent :== Int
28

Camil Staps's avatar
Camil Staps committed
29 30 31 32 33
/**
 * An inotify callback: event, maybe filename, state, world -> state, world
 *
 * @var A state that is passed on.
 */
34
:: INCallback st :== INEvent (Maybe String) *(Inotify st) -> *(*World -> *(*Inotify st, *World))
Camil Staps's avatar
Camil Staps committed
35

36
//* Bitwise OR for event masks
Camil Staps's avatar
Camil Staps committed
37 38
(|-) infixl 6 :: (INMask INMask -> INMask)

Camil Staps's avatar
Camil Staps committed
39 40 41 42 43 44
/**
 * Initialise an inotify file descriptor with some state.
 *
 * @param The initial state
 * @result A file descriptor or Nothing if the C initialisation failed
 */
Camil Staps's avatar
Camil Staps committed
45
inotify_init :: st -> Maybe *(Inotify st)
46

Camil Staps's avatar
Camil Staps committed
47 48 49 50 51 52
/**
 * Close an inotify file descriptor and get back the state
 *
 * @param The file descriptor
 * @result The final state
 */
Camil Staps's avatar
Camil Staps committed
53
inotify_close :: *(Inotify st) -> st
Camil Staps's avatar
Camil Staps committed
54

55 56 57
/**
 * Add a watch on some file
 *
Camil Staps's avatar
Camil Staps committed
58 59 60 61 62
 * @param The callback for events
 * @param A mask of events to watch for
 * @param The filename
 * @param The inotify file descriptor
 * @result Either an error code or a watch descriptor, and the inotify file descriptor
63 64
 */
inotify_add_watch :: (INCallback st) !INMask !String !*(Inotify st)
Camil Staps's avatar
Camil Staps committed
65
	-> *(!Either Int INWatch, !*Inotify st)
66 67 68 69

/**
 * Remove a watch
 *
Camil Staps's avatar
Camil Staps committed
70 71 72
 * @param The watch to remove
 * @param The inotify file descriptor
 * @result A boolean indicating success and the inotify file descriptor
73 74 75 76 77 78
 */
inotify_rm_watch :: !INWatch !*(Inotify st) -> *(!Bool, !*Inotify st)

/**
 * Poll an inotify file descriptor; i.e. wait for new events
 *
Camil Staps's avatar
Camil Staps committed
79 80 81
 * @param Timeout in milliseconds (Nothing for no timeout)
 * @param The inotify file descriptor
 * @result The number of events, and the inotify file descriptor
82 83
 */
inotify_poll :: !(Maybe Int) !*(Inotify st) -> *(!Int, !*Inotify st)
Camil Staps's avatar
Camil Staps committed
84

85 86 87 88
/**
 * Check for new events and call callbacks
 */
inotify_check :: !*(Inotify st) !*World -> *(!*Inotify st, !*World)
Camil Staps's avatar
Camil Staps committed
89

90 91
/**
 * Check if an event matches a mask
Camil Staps's avatar
Camil Staps committed
92 93 94 95
 *
 * @param The mask
 * @param The event
 * @result True iff the event matches the mask
96 97 98 99 100 101
 */
inotify_is_event :: INMask INEvent -> Bool

/**
 * Combination of inotify_poll and inotify_check that will return only if no
 * events were given when a timeout occurred.
Camil Staps's avatar
Camil Staps committed
102 103 104 105 106 107 108
 *
 * @param Timeout in milliseconds (Nothing for no timeout)
 * @param The inotify file descriptor
 * @param The World
 * @result The inotify file descriptor and the World. However, the function
 *   only returns if no events were given when the timeout occurred. Otherwise,
 *   it will loop.
109
 */
Camil Staps's avatar
Camil Staps committed
110
inotify_loop_with_timeout :: !(Maybe Int) !*(Inotify st) !*World
Camil Staps's avatar
Camil Staps committed
111
	-> *(!*Inotify st, !*World)
112 113 114

/**
 * inotify_loop_with_timeout with Nothing as timeout (will never return)
Camil Staps's avatar
Camil Staps committed
115 116 117 118
 *
 * @param The inotify file descriptor
 * @param The World
 * @result The parameters back, however, this function will never return.
119 120 121 122
 */
inotify_loop_forever :: !*(Inotify st) !*World -> *(!*Inotify st, !*World)

/*** Begin inotify.h ***/
123

124
//* Inotify event mask: file was accessed
Camil Staps's avatar
Camil Staps committed
125
IN_ACCESS        :== 0x00000001
126
//* Inotify event mask: file was modified
Camil Staps's avatar
Camil Staps committed
127
IN_MODIFY        :== 0x00000002
128
//* Inotify event mask: metadata changed
Camil Staps's avatar
Camil Staps committed
129
IN_ATTRIB        :== 0x00000004
130
//* Inotify event mask: file opened for writing was closed
Camil Staps's avatar
Camil Staps committed
131
IN_CLOSE_WRITE   :== 0x00000008
132
//* Inotify event mask: file not opened for writing was closed
Camil Staps's avatar
Camil Staps committed
133
IN_CLOSE_NOWRITE :== 0x00000010
134
//* Inotify event mask: file was opened
Camil Staps's avatar
Camil Staps committed
135
IN_OPEN          :== 0x00000020
136
//* Inotify event mask: file was moved from watched directory
Camil Staps's avatar
Camil Staps committed
137
IN_MOVED_FROM    :== 0x00000040
138
//* Inotify event mask: file was moved to watched directory
Camil Staps's avatar
Camil Staps committed
139
IN_MOVED_TO      :== 0x00000080
140
//* Inotify event mask: file was created in watched directory
Camil Staps's avatar
Camil Staps committed
141
IN_CREATE        :== 0x00000100
142
//* Inotify event mask: file in watched directory was deleted
Camil Staps's avatar
Camil Staps committed
143
IN_DELETE        :== 0x00000200
144
//* Inotify event mask: watched file was deleted
Camil Staps's avatar
Camil Staps committed
145
IN_DELETE_SELF   :== 0x00000400
146
//* Inotify event mask: watched file was moved
Camil Staps's avatar
Camil Staps committed
147
IN_MOVE_SELF     :== 0x00000800
148
//* Inotify event mask: backing fs of watched file was unmounted
Camil Staps's avatar
Camil Staps committed
149
IN_UNMOUNT       :== 0x00002000
150
//* Inotify event mask: the inotify event queue overflowed
Camil Staps's avatar
Camil Staps committed
151 152 153 154 155 156 157 158
IN_Q_OVERFLOW    :== 0x00004000
/**
 * Inotify event mask: the watch has been removed, either through
 * inotify_rm_watch or because it was deleted, the fs was unmounted, etc.
 */
IN_IGNORED       :== 0x00008000
/**
 * Inotify event mask: watched file was closed
159
 * @type Int
Camil Staps's avatar
Camil Staps committed
160 161 162 163
 */
IN_CLOSE         :== (IN_CLOSE_WRITE |- IN_CLOSE_NOWRITE)
/**
 * Inotify event mask: a file was moved from or to a watched directory
164
 * @type Int
Camil Staps's avatar
Camil Staps committed
165 166
 */
IN_MOVE          :== (IN_MOVED_FROM |- IN_MOVED_TO)
167
//* Inotify event mask: only watch the path if it is a directory
Camil Staps's avatar
Camil Staps committed
168
IN_ONLYDIR       :== 0x01000000
169
//* Inotify event mask: don't follow symlinks
Camil Staps's avatar
Camil Staps committed
170
IN_DONT_FOLLOW   :== 0x02000000
171
//* Inotify event mask: stop watching files when they get unlinked
Camil Staps's avatar
Camil Staps committed
172 173 174 175 176 177
IN_EXCL_UNLINK   :== 0x04000000
/**
 * Inotify event mask: when adding a watch on a path for which a watch already
 * exists, OR the new event mask with the old one instead of replacing it.
 */
IN_MASK_ADD      :== 0x20000000
178
//* Inotify event mask: the event occurred against a directory
Camil Staps's avatar
Camil Staps committed
179
IN_ISDIR         :== 0x40000000
180
//* Inotify event mask: monitor a watch only for one event, then remove it
Camil Staps's avatar
Camil Staps committed
181 182 183
IN_ONESHOT       :== 0x80000000
/**
 * Inotify event mask: OR of all events
184
 * @type Int
Camil Staps's avatar
Camil Staps committed
185
 */
Camil Staps's avatar
Camil Staps committed
186 187 188 189
IN_ALL_EVENTS    :==
	(IN_ACCESS |- IN_MODIFY |- IN_ATTRIB |- IN_CLOSE_WRITE |- IN_CLOSE_NOWRITE
	|- IN_OPEN |- IN_MOVED_FROM |- IN_MOVED_TO |- IN_DELETE |- IN_CREATE |-
	IN_DELETE_SELF |- IN_MOVE_SELF)
190 191

/*** End inotify.h ***/