| 1 |
;;; |
| 2 |
;;; file.event - Interface for system file event API |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2023-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(select-module file.event) |
| 35 |
|
| 36 |
(inline-stub |
| 37 |
(.include <gauche/priv/configP.h>) |
| 38 |
;; |
| 39 |
;; Linux (inotify) |
| 40 |
;; |
| 41 |
(.when (defined HAVE_SYS_INOTIFY_H) |
| 42 |
(.include <sys/inotify.h>) |
| 43 |
|
| 44 |
(define-ctype ScmSysInotifyEvent::(.struct |
| 45 |
(SCM_HEADER :: "" |
| 46 |
data::(struct inotify_event *)))) |
| 47 |
|
| 48 |
(define-cclass <inotify-event> :private |
| 49 |
ScmSysInotifyEvent* "Scm_SysInotifyEventClass" () |
| 50 |
((wd :type <int> |
| 51 |
:c-name "data->wd") |
| 52 |
(mask :type <uint32> |
| 53 |
:c-name "data->mask") |
| 54 |
(cookie :type <uint32> |
| 55 |
:c-name "data->cookie") |
| 56 |
(name :getter "if (obj->data->len) {\ |
| 57 |
return SCM_MAKE_STR_IMMUTABLE(obj->data->name);\ |
| 58 |
} else {\ |
| 59 |
return SCM_FALSE;\ |
| 60 |
}" |
| 61 |
:setter #f) |
| 62 |
)) |
| 63 |
|
| 64 |
(define-enum IN_ACCESS) |
| 65 |
(define-enum IN_ATTRIB) |
| 66 |
(define-enum IN_CLOSE_WRITE) |
| 67 |
(define-enum IN_CLOSE_NOWRITE) |
| 68 |
(define-enum IN_CREATE) |
| 69 |
(define-enum IN_DELETE) |
| 70 |
(define-enum IN_DELETE_SELF) |
| 71 |
(define-enum IN_MODIFY) |
| 72 |
(define-enum IN_MOVE_SELF) |
| 73 |
(define-enum IN_MOVED_FROM) |
| 74 |
(define-enum IN_MOVED_TO) |
| 75 |
(define-enum IN_OPEN) |
| 76 |
;; event masks |
| 77 |
(define-enum IN_ALL_EVENTS) |
| 78 |
(define-enum IN_MOVE) |
| 79 |
(define-enum IN_CLOSE) |
| 80 |
;; further event bits |
| 81 |
(define-enum IN_DONT_FOLLOW) |
| 82 |
(define-enum IN_EXCL_UNLINK) |
| 83 |
(define-enum IN_MASK_ADD) |
| 84 |
(define-enum IN_ONESHOT) |
| 85 |
(define-enum IN_ONLYDIR) |
| 86 |
;(define-enum IN_MASK_CREATE) |
| 87 |
;; returned bits |
| 88 |
(define-enum IN_IGNORED) |
| 89 |
(define-enum IN_ISDIR) |
| 90 |
(define-enum IN_Q_OVERFLOW) |
| 91 |
(define-enum IN_UNMOUNT) |
| 92 |
|
| 93 |
;; flags for inotify_init1 |
| 94 |
(define-enum IN_NONBLOCK) |
| 95 |
(define-enum IN_CLOEXEC) |
| 96 |
|
| 97 |
(define-cproc inotify-init () ::<int> |
| 98 |
(let* ([r::int (inotify_init)]) |
| 99 |
(when (< r 0) (Scm_SysError "inotify_init failed")) |
| 100 |
(return r))) |
| 101 |
|
| 102 |
(define-cproc inotify-init1 (flags::<int>) ::<int> |
| 103 |
(let* ([r::int (inotify_init1 flags)]) |
| 104 |
(when (< r 0) (Scm_SysError "inotify_init1 failed")) |
| 105 |
(return r))) |
| 106 |
|
| 107 |
(define-cproc inotify-add-watch (fd::<int> |
| 108 |
pathname::<const-cstring> |
| 109 |
mask::<uint32>) |
| 110 |
::<int> |
| 111 |
(let* ([r::int (inotify_add_watch fd pathname mask)]) |
| 112 |
(when (< r 0) (Scm_SysError "inotify_add_watch failed")) |
| 113 |
(return r))) |
| 114 |
|
| 115 |
(define-cproc inotify-rm-watch (fd::<int> wd::<int>) |
| 116 |
::<void> |
| 117 |
(let* ([r::int (inotify_rm_watch fd wd)]) |
| 118 |
(when (< r 0) (Scm_SysError "inotify_rm_watch failed")))) |
| 119 |
|
| 120 |
(define-cfn inotify-event-extract (buf::(struct inotify_event*)) |
| 121 |
(let* ([z::ScmSysInotifyEvent* (SCM_NEW ScmSysInotifyEvent)]) |
| 122 |
(SCM_SET_CLASS z (& Scm_SysInotifyEventClass)) |
| 123 |
(set! (-> z data) (cast (struct inotify_event*) buf)) |
| 124 |
(return (SCM_OBJ z)))) |
| 125 |
|
| 126 |
(define-cproc inotify-event-copy (ev::<inotify-event>) |
| 127 |
(let* ([dst::(struct inotify_event*) |
| 128 |
(SCM_NEW2 (struct inotify_event*) |
| 129 |
(+ (sizeof (struct inotify_event)) |
| 130 |
(-> ev data len)))]) |
| 131 |
(set! (* dst) (* (-> ev data))) ; verbatim copy |
| 132 |
(return (inotify-event-extract dst)))) |
| 133 |
|
| 134 |
;; NB: Returned events share the memory with the buffer. Copy it |
| 135 |
;; if you want to keep the event after reusing the buffer. |
| 136 |
(define-cproc inotify-read-events (fd::<int> buf::<u8vector>) |
| 137 |
(let* ([buffer::char* (cast char* (SCM_U8VECTOR_ELEMENTS buf))] |
| 138 |
[len::ssize_t (read fd buffer (SCM_U8VECTOR_SIZE buf))] |
| 139 |
[h SCM_NIL] [t SCM_NIL] [p::char* buffer]) |
| 140 |
(when (< len 0) (Scm_SysError "reading inotify events failed")) |
| 141 |
(while (< p (+ buffer len)) |
| 142 |
(let* ([ev (inotify-event-extract (cast (struct inotify_event*) p))]) |
| 143 |
(SCM_APPEND1 h t ev) |
| 144 |
(set! p (+ p |
| 145 |
(sizeof (struct inotify_event)) |
| 146 |
(-> (SCM_INOTIFY_EVENT ev) data len))))) |
| 147 |
(return h))) |
| 148 |
|
| 149 |
(initcode |
| 150 |
(Scm_AddFeature "gauche.sys.inotify" NULL)) |
| 151 |
|
| 152 |
) ;; defined(HAVE_SYS_INOTIFY_H) |
| 153 |
|
| 154 |
;; |
| 155 |
;; kqueue (BSD) |
| 156 |
;; |
| 157 |
(.when (defined HAVE_SYS_EVENT_H) |
| 158 |
(declcode |
| 159 |
(.include <sys/event.h>) |
| 160 |
|
| 161 |
(define-ctype ScmSysKevent |
| 162 |
::(.struct |
| 163 |
(SCM_HEADER :: "" |
| 164 |
data::(struct kevent)))) |
| 165 |
) |
| 166 |
|
| 167 |
(define-cclass <sys-kevent> :private |
| 168 |
ScmSysKevent* "Scm_SysKeventClass" () |
| 169 |
()) |
| 170 |
|
| 171 |
(define-enum EV_ADD) |
| 172 |
(define-enum EV_ENABLE) |
| 173 |
(define-enum EV_DISABLE) |
| 174 |
(define-enum EV_DISPATCH) |
| 175 |
(define-enum EV_DELETE) |
| 176 |
(define-enum EV_RECEIPT) |
| 177 |
(define-enum EV_ONESHOT) |
| 178 |
(define-enum EV_CLEAR) |
| 179 |
(define-enum EV_EOF) |
| 180 |
(define-enum EV_ERROR) |
| 181 |
|
| 182 |
(define-enum EVFILT_VNODE) |
| 183 |
|
| 184 |
(define-enum NOTE_ATTRIB) |
| 185 |
;(define-enum NOTE_CLOSE) |
| 186 |
;(define-enum NOTE_CLOSE_WRITE) |
| 187 |
(define-enum NOTE_DELETE) |
| 188 |
(define-enum NOTE_LINK) |
| 189 |
;(define-enum NOTE_OPEN) |
| 190 |
;(define-enum NOTE_READ) |
| 191 |
(define-enum NOTE_RENAME) |
| 192 |
(define-enum NOTE_REVOKE) |
| 193 |
(define-enum NOTE_WRITE) |
| 194 |
|
| 195 |
(define-cproc make-kevent (fd::<int> |
| 196 |
filter::<short> |
| 197 |
flags::<ushort> |
| 198 |
fflags::<uint> |
| 199 |
data::<int64>) |
| 200 |
(let* ([kev::ScmSysKevent* (SCM_NEW ScmSysKevent)]) |
| 201 |
(SCM_SET_CLASS kev (& Scm_SysKeventClass)) |
| 202 |
(EV_SET (& (-> kev data)) fd filter flags fflags data |
| 203 |
(cast void* kev)) |
| 204 |
(return (SCM_OBJ kev)))) |
| 205 |
|
| 206 |
(define-cfn list->keventArray (lis nelts::int*) |
| 207 |
::(struct kevent*) :static |
| 208 |
(let* ([n::ScmSmallInt (Scm_Length lis)]) |
| 209 |
(when (< n 0) |
| 210 |
(Scm_Error "list of <sys-kevent> required, but got: %S" lis)) |
| 211 |
(let* ([p::(struct kevent*) |
| 212 |
(?: n (SCM_NEW_ATOMIC_ARRAY (struct kevent) n) NULL)] |
| 213 |
[i::ScmSmallInt 0]) |
| 214 |
(while (< i n) |
| 215 |
(unless (SCM_SYS_KEVENT_P (SCM_CAR lis)) |
| 216 |
(Scm_Error "<sys-kevent> required, but got: %S" (SCM_CAR lis))) |
| 217 |
(let* ([kev::ScmSysKevent* (SCM_SYS_KEVENT (SCM_CAR lis))]) |
| 218 |
(EV_SET (+ p i) |
| 219 |
(ref (-> kev data) ident) |
| 220 |
(ref (-> kev data) filter) |
| 221 |
(ref (-> kev data) flags) |
| 222 |
(ref (-> kev data) fflags) |
| 223 |
(ref (-> kev data) data) |
| 224 |
(cast void* kev))) |
| 225 |
(pre++ i) |
| 226 |
(set! lis (SCM_CDR lis))) |
| 227 |
(set! (* nelts) (cast int n)) |
| 228 |
(return p)))) |
| 229 |
|
| 230 |
(define-cfn keventArray->list (p::(struct kevent*) nelts::int) |
| 231 |
(let* ([h SCM_NIL] [t SCM_NIL] [i::int 0]) |
| 232 |
(while (< i nelts) |
| 233 |
(let* ([kev::ScmSysKevent* (SCM_NEW ScmSysKevent)]) |
| 234 |
(EV_SET (& (-> kev data)) |
| 235 |
(-> p ident) |
| 236 |
(-> p filter) |
| 237 |
(-> p flags) |
| 238 |
(-> p fflags) |
| 239 |
(-> p data) |
| 240 |
kev) |
| 241 |
(SCM_SET_CLASS kev (& Scm_SysKeventClass)) |
| 242 |
(SCM_APPEND1 h t (SCM_OBJ kev)))) |
| 243 |
(return h))) |
| 244 |
|
| 245 |
(define-cproc kevent (kq::<int> |
| 246 |
changeList |
| 247 |
timeout) |
| 248 |
(let* ([ts::ScmTimeSpec] |
| 249 |
[pts::ScmTimeSpec* (Scm_GetTimeSpec timeout (& ts))] |
| 250 |
[ncl::int] |
| 251 |
[pcl::(struct kevent*) (list->keventArray changeList (& ncl))] |
| 252 |
[res::(.array (struct kevent) [128])] |
| 253 |
[r::int 0]) |
| 254 |
(SCM_SYSCALL r (kevent kq pcl ncl res 128 pts)) |
| 255 |
(cond [(< r 0) (Scm_SysError "kqueue failed" r)] |
| 256 |
[(== r 0) (return SCM_NIL)] |
| 257 |
[else (return (keventArray->list res r))]))) |
| 258 |
|
| 259 |
(initcode |
| 260 |
(Scm_AddFeature "gauche.sys.kqueue" NULL)) |
| 261 |
|
| 262 |
) ;; defined(HAVE_SYS_EVENT_H) |
| 263 |
|
| 264 |
;; |
| 265 |
;; OSX |
| 266 |
;; |
| 267 |
(.when (and (defined __APPLE__) (defined __MACH__)) |
| 268 |
(declcode |
| 269 |
(.include <CoreServices/CoreServices.h>) |
| 270 |
|
| 271 |
(define-ctype ScmSysFileSystemEventStream |
| 272 |
::(.struct |
| 273 |
(SCM_HEADER :: "" |
| 274 |
data::FSEventStreamRef |
| 275 |
queue |
| 276 |
closed::int))) |
| 277 |
|
| 278 |
(define-ctype ScmSysFileSystemEvent |
| 279 |
::(.struct |
| 280 |
(SCM_HEADER :: "" |
| 281 |
name ; <string> |
| 282 |
flags |
| 283 |
id))) |
| 284 |
) |
| 285 |
|
| 286 |
(define-cclass <file-system-event-stream> :private |
| 287 |
ScmSysFileSystemEventStream* "Scm_SysFileSystemEventStreamClass" () |
| 288 |
()) |
| 289 |
|
| 290 |
(define-cclass <file-system-event> :private |
| 291 |
ScmSysFileSystemEvent* "Scm_SysFileSystemEventClass" () |
| 292 |
((name) |
| 293 |
(flags) |
| 294 |
(id))) |
| 295 |
|
| 296 |
(define-cfn fsestream-cleanup (stream::ScmSysFileSystemEventStream*) |
| 297 |
::void :static |
| 298 |
(unless (-> stream closed) |
| 299 |
(FSEventStreamStop (-> stream data)) |
| 300 |
(FSEventStreamInvalidate (-> stream data)) |
| 301 |
(FSEventStreamRelease (-> stream data)) |
| 302 |
(set! (-> stream closed) TRUE))) |
| 303 |
|
| 304 |
(define-cfn fsestream-finalize (stream _::void*) ::void :static |
| 305 |
(fsestream-cleanup (cast ScmSysFileSystemEventStream* stream))) |
| 306 |
|
| 307 |
(define-cfn make-fsevent (path::char* flags::uint64_t id::uint64_t) |
| 308 |
(let* ([z::ScmSysFileSystemEvent* (SCM_NEW ScmSysFileSystemEvent)]) |
| 309 |
(SCM_SET_CLASS z (& Scm_SysFileSystemEventClass)) |
| 310 |
(set! (-> z name) (SCM_MAKE_STR_COPYING path)) |
| 311 |
(set! (-> z flags) (Scm_MakeIntegerU64 flags)) |
| 312 |
(set! (-> z id) (Scm_MakeIntegerU64 id)) |
| 313 |
(return (SCM_OBJ z)))) |
| 314 |
|
| 315 |
(define-cfn fsestream-callback (_::ConstFSEventStreamRef |
| 316 |
streamobj::void* |
| 317 |
num-events::size_t |
| 318 |
event-paths::void* |
| 319 |
event-flags::(const FSEventStreamEventFlags*) |
| 320 |
event-ids::(const FSEventStreamEventId*)) |
| 321 |
::void :static |
| 322 |
(let* ([stream::ScmSysFileSystemEventStream* |
| 323 |
(cast ScmSysFileSystemEventStream* streamobj)] |
| 324 |
[paths::char** (cast char** event-paths)] |
| 325 |
[h (-> stream queue)] |
| 326 |
[t (?: (SCM_PAIRP h) (Scm_LastPair h) SCM_NIL)] |
| 327 |
[i::size_t 0]) |
| 328 |
(while (< i num-events) |
| 329 |
(let* ([ev (make-fsevent (aref paths i) |
| 330 |
(aref event-flags i) |
| 331 |
(aref event-ids i))]) |
| 332 |
(SCM_APPEND1 h t ev)) |
| 333 |
(pre++ i)) |
| 334 |
(set! (-> stream queue) h))) |
| 335 |
|
| 336 |
(define-cproc make-file-system-event-stream (path::<const-cstring> |
| 337 |
flags::<uint64> |
| 338 |
id::<uint64> |
| 339 |
latency::<double>) |
| 340 |
(let* ([cfpath::CFStringRef |
| 341 |
(CFStringCreateWithCString kCFAllocatorDefault |
| 342 |
path |
| 343 |
kCFStringEncodingUTF8)] |
| 344 |
[cfpatharray::CFArrayRef |
| 345 |
(CFArrayCreate NULL |
| 346 |
(cast (const void**) cfpath) |
| 347 |
1 NULL)] |
| 348 |
[stream::ScmSysFileSystemEventStream* |
| 349 |
(SCM_NEW ScmSysFileSystemEventStream)]) |
| 350 |
(SCM_SET_CLASS stream (& Scm_SysFileSystemEventStreamClass)) |
| 351 |
(set! (-> stream data) NULL |
| 352 |
(-> stream queue) SCM_NIL |
| 353 |
(-> stream closed) FALSE) |
| 354 |
(Scm_RegisterFinalizer (SCM_OBJ stream) fsestream-finalize NULL) |
| 355 |
(set! (-> stream data) |
| 356 |
(FSEventStreamCreate NULL |
| 357 |
(& fsestream-callback) |
| 358 |
(cast void* stream) |
| 359 |
cfpatharray |
| 360 |
id |
| 361 |
latency |
| 362 |
flags)) |
| 363 |
;; TODO: We need a run loop to handle the events. |
| 364 |
(return (SCM_OBJ stream)))) |
| 365 |
|
| 366 |
(define-cproc close-file-system-event-stream (stream::<file-system-event-stream>) |
| 367 |
::<void> |
| 368 |
(fsestream-cleanup stream)) |
| 369 |
) ;; defined(__APPLE__) && defined(__MACH__) |
| 370 |
) |