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://github.com/wasamasa/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? X) procedure
(event-wd EVENT) procedure
(event-flags EVENT) procedure
(event-name EVENT) procedure

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! FD) procedure

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

Watches

(%add-watch! FD PATH FLAGS) procedure

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 WD) procedure

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

Events

(%next-events! FD) 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.

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.

(%fd) parameter
(%fd FD) parameter

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 FLAGS) procedure

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! WD) procedure

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

(add-watch-recursively! PATH FLAGS) procedure

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 WD) procedure

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

(wd-list) procedure

Returns a list of all known watch descriptors.

(path-list) procedure

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.

(%events) parameter
(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 EVENT) procedure

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

/proc Interface

(max-queued-events) procedure
(max-user-instances) procedure
(max-user-watches) procedure

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

Examples

(use inotify)

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

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

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

0.2

0.1

Contents »