chickadee » inotify

inotify

Introduction

This egg provides a nearly complete set of bindings to the Linux inotify API for watching file events. The authoritative source on its behavior is inotify(7).

Author

Vasilij Schneidermann

Repository

https://depp.brause.cc/inotify

Current state of the bindings

Requirements

This library requires a sufficiently recent Linux kernel, but has been tested on Linux 4.8.13 only. Please report any incompatibilities you run into with older Linux versions.

Common API

Events

event? Xprocedure
event-wd EVENTprocedure
event-flags EVENTprocedure
event-name EVENTprocedure

Event predicate and accessors for event records. An event has a watch descriptor for associating it with a watch, a list of flags associated with the event, a cookie that is set for rename events to allow connecting the old and new file name and a name that is set for file events inside a watched directory.

The event flag list consists of at least one of the following symbols:

  • access
  • attrib
  • close-write
  • close-nowrite
  • create
  • delete
  • delete-self
  • ignore
  • isdir
  • modify
  • move-self
  • moved-from
  • moved-to
  • open
  • q-overflow
  • unmount

Low-level API

Setup and Teardown

%init!procedure

Initializes an user instance and returns its file descriptor.

%clean-up! FDprocedure

Frees the user instance associated with FD and all associated watches.

Watches

%add-watch! FD PATH FLAGSprocedure

Adds a watch for PATH which must be an absolute or relative path pointing to a file or directory. FLAGS is a list of at least one symbol describing the events one is interested in. Returns a watch descriptor associated with the watch.

The event flag list consists of at least one of the following event type symbols:

  • access
  • attrib
  • close-write
  • close-nowrite
  • create
  • delete
  • delete-self
  • modify
  • move-self
  • moved-from
  • moved-to
  • open

Further convenience flags:

  • move (equivalent to (moved-from moved-to))
  • close (equivalent to (close-write close-nowrite))
  • all-events (catches all event types)

Further options:

  • dont-follow
  • excl-unlink
  • mask-add
  • oneshot
  • onlydir
%remove-watch! FD WDprocedure

Removes a watch as identified by the given watch descriptor WD.

Events

%next-events! FDprocedure

Blocks the current thread to wait for events and returns a list of at least one event record. Note that calling this procedure does not block other SRFI-18 threads. In other words, it is possible to have a background thread reacting to file events while other threads proceed normally.

High-level API

Setup and Teardown

init!procedure

Initializes an user instance, returning #t when successful and #f if it has been initialized before. This procedure must be called before manipulating watches and fetching events.

clean-up!procedure

Frees the user instance and all associated watches, returning #t when successful and #f on subsequent attempts.

%fdparameter
%fd FDparameter

Parameter holding the user instance file descriptor. You shouldn't need to mess with this unless you insist on using more than one user instance for watching events.

Watches

add-watch! PATH FLAGSprocedure

Adds a watch for PATH which must be an absolute or relative path pointing to a file or directory. FLAGS is a list of at least one symbol describing the events one is interested in. Returns a watch descriptor associated with the watch.

The event flag list consists of at least one of the following event type symbols:

  • access
  • attrib
  • close-write
  • close-nowrite
  • create
  • delete
  • delete-self
  • modify
  • move-self
  • moved-from
  • moved-to
  • open

Further convenience flags:

  • move (equivalent to (moved-from moved-to))
  • close (equivalent to (close-write close-nowrite))
  • all-events (catches all event types)

Further options:

  • dont-follow
  • excl-unlink
  • mask-add
  • oneshot
  • onlydir
remove-watch! WDprocedure

Removes a watch as identified by the given watch descriptor WD.

add-watch-recursively! PATH FLAGSprocedure

Adds a watch for PATH and traverses it recursively to add watches for any directories found in it. Each watch is applied with FLAGS, refer to the documentation of add-watch! for further details. Returns a list of watch descriptors associated with each established watch. Note that this procedure does not work in an atomic manner, it cannot be guaranteed that all directories in PATH will receive watches.

wd->path WDprocedure

Returns the path the watch associated with WD was created with.

wd-listprocedure

Returns a list of all known watch descriptors.

path-listprocedure

Returns a list of all watched paths.

Events

next-events!procedure

Blocks the current thread to wait for events and returns a list of at least one event record. Note that calling this procedure does not block other SRFI-18 threads. In other words, it is possible to have a background thread reacting to file events while other threads proceed normally.

%eventsparameter
next-event!procedure

Convenience procedure for fetching one event at a time. This calls next-events! internally for filling up an event queue and returns one event from it. The event queue itself is available under %events and can be manipulated with the queue procedures from the data-structures unit.

event->pathname EVENTprocedure

Convenience procedure for reconstructing the full path name from an event.

/proc Interface

max-queued-eventsprocedure
max-user-instancesprocedure
max-user-watchesprocedure

Convenience procedures for querying usage limits from /proc/sys/fs/inotify.

Examples

(import inotify)

(init!)
(on-exit clean-up!)
(add-watch! "." '(all-events))

(let loop ()
  (print (next-event!))
  (loop))

More realistic examples can be found in the repository.

License

Copyright (c) 2017, Vasilij Schneidermann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Version History

1.2

1.1

1.0

0.3

0.2

0.1

Contents »