HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulestreamly-0.10.1Haskell2010

Streamly.Internal.FileSystem.Event.Linux

Overview

Use watchPaths with a list of file system paths you want to watch as argument. It returns a stream of Event representing the file system events occurring under the watched paths.

Stream.mapM_ (putStrLn . showEvent) $ watchPaths [Array.fromList "dir"]

Event is an opaque type. Accessor functions (e.g. showEvent above) provided in this module are used to determine the attributes of the event.

Identical successive events may be coalesced into a single event.

Design notes

For reference documentation see:

We try to keep the macOS/Linux/Windows event handling APIs and defaults semantically and syntactically as close as possible.

BUGs

When testing on Linux Kernel version 5.3.0-53-generic #47-Ubuntu, the last event for the root path seems to be delayed until one more event occurs.

Differences between macOS and Linux APIs:

  1. macOS watch is based on the path provided to it, if the path is deleted and recreated it will still be watched, if the path moves to another path it won't be watched anymore. Whereas Linux watch is based on a handle to the path, if the path is deleted and recreated it won't be watched, if the path moves to another it can still be watched (though this is configurable).

  1. macOS watches the directory hierarchy recursively, Linux watches only one level of dir, recursive watch has to be built in user space by watching for create events and adding the new directories to the watch. Not sure how this will scale for too many paths.

  2. In macOS the path of the subject of the event is absolute, in Linux the path is the name of the object inside the dir being watched.

  3. On Linux watchPaths fails if a path does not exist, on macOS it does not fail.

  • 3 types
  • 49 values
  • Packagestreamly-0.10.1
  • Exports52
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceLinux.hs

Subscribing to events

0 declarations

Default configuration

Watch Behavior

valuesetRecursiveMode :: Bool -> Config -> Config
#

Watch the whole directory tree recursively instead of watching just one level of directory.

default: False

Pre-release

valuesetOneShot :: Bool -> Config -> Config
#

Watch the object only for one event and then remove it from the watch.

default: False

Pre-release

valuesetOnlyDir :: Bool -> Config -> Config
#

Watch the object only if it is a directory. This provides a race-free way to ensure that the watched object is a directory.

default: False

Pre-release

When adding a new path to the watch, specify what to do if a watch already exists on that path.

default: FailIfExists

Pre-release

Events of Interest

Root Path Events

Item Level Metadata change

valuesetAttrsModified :: Bool -> Config -> Config
#

Report when the metadata e.g. owner, permission modes, modifications times of an object changes.

default: True

Pre-release

Item Level Access

Item CRUD events

valuesetAllEvents :: Bool -> Config -> Config
#

Set all tunable events True or False. Equivalent to setting:

  • setRootDeleted

  • setRootMoved

  • setAttrsModified

  • setAccessed

  • setOpened

  • setWriteClosed

  • setNonWriteClosed

  • setCreated

  • setDeleted

  • setMovedFrom

  • setMovedTo

  • setModified

Pre-release

Watch APIs

Start monitoring a list of file system paths for file system events with the supplied configuration operation over the defaultConfig. The paths could be files or directories. When recursive mode is set and the path is a directory, the whole directory tree under it is watched recursively. Monitoring starts from the current time onwards. The paths are specified as UTF-8 encoded Array of Word8.

Non-existing Paths: the API fails if a watch is started on a non-exsting path.

Performance: Note that recursive watch on a large directory tree could be expensive. When starting a watch, the whole tree must be read and watches are started on each directory in the tree. The initial time to start the watch as well as the memory required is proportional to the number of directories in the tree.

Bugs: When new directories are created under the tree they are added to the watch on receiving the directory create event. However, the creation of a dir and adding a watch for it is not atomic. The implementation takes care of this and makes sure that watches are added for all directories. However, In the mean time, the directory may have received more events which may get lost. Handling of any such lost events is yet to be implemented.

See the Linux inotify man page for more details.

watchwith
     (setFollowSymLinks True . setUnwatchMoved False)
     [Array.fromList "dir"]

Pre-release

valueaddToWatch :: Config -> Watch -> Array Word8 -> Array Word8 -> IO ()
#

addToWatch cfg watch root subpath adds subpath to the list of paths being monitored under root via the watch handle watch. root must be an absolute path and subpath must be relative to root.

Pre-release

valueremoveFromWatch :: Watch -> Array Word8 -> IO ()
#

Remove an absolute root path from a Watch, if a path was moved after adding you need to provide the original path which was used to add the Watch.

Pre-release

Handling Events

5 declarations
datadata Event
#

An Event generated by the file system. Use the accessor functions to examine the event.

Pre-release

Instances3Eq, Ord, Show
  • Eq EventDefined in streamly-0.10.1 · Streamly.Internal.FileSystem.Event.Linux
  • Ord EventDefined in streamly-0.10.1 · Streamly.Internal.FileSystem.Event.Linux
  • Show EventDefined in streamly-0.10.1 · Streamly.Internal.FileSystem.Event.Linux
valuegetRoot :: Event -> Array Word8
#

Get the watch root corresponding to the Event.

Note that if a path was moved after adding to the watch, this will give the original path and not the new path after moving.

TBD: we can possibly update the watch root on a move self event.

Pre-release

valuegetRelPath :: Event -> Array Word8
#

Get the file system object path for which the event is generated, relative to the watched root. The path is a "/" separated array of bytes.

Pre-release

valuegetAbsPath :: Event -> Array Word8
#

Get the absolute file system object path for which the event is generated.

When the watch root is a symlink, the absolute path returned is via the original symlink and not through the resolved path.

Pre-release

valuegetCookie :: Event -> Cookie
#

Cookie is set when a rename occurs. The cookie value can be used to connect the isMovedFrom and isMovedTo events, if both the events belong to the same move operation then they will have the same cookie value.

Pre-release

Root Level Events

valueisRootPathEvent :: Event -> Bool
#

Determine whether the event indicates a change of path of the monitored object itself. Note that the object may become unreachable or deleted after a change of path.

Occurs only for a watched path

Pre-release

valueisRootUnwatched :: Event -> Bool
#

A path was removed from the watch explicitly using removeFromWatch or automatically (file was deleted, or filesystem was unmounted).

Note that in recursive watch mode all the subdirectories are watch roots, therefore, they will all generate this event.

Occurs only for a watched path

Pre-release

valueisRootDeleted :: Event -> Bool
#

Watched file/directory was itself deleted. (This event also occurs if an object is moved to another filesystem, since mv(1) in effect copies the file to the other filesystem and then deletes it from the original filesystem.) In addition, an isRootUnwatched event will subsequently be generated for the watch descriptor.

Note that in recursive watch mode all the subdirectories are watch roots, therefore, they will all generate this event.

Occurs only for a watched path

Pre-release

valueisRootMoved :: Event -> Bool
#

Watched file/directory was itself moved within the file system.

Note that in recursive watch mode all the subdirectories are watch roots, therefore, they will all generate this event.

Occurs only for a watched path

Pre-release

valueisRootUnmounted :: Event -> Bool
#

Filesystem containing watched object was unmounted. In addition, an isRootUnwatched event will subsequently be generated for the watch descriptor.

Occurs only for a watched path

Pre-release

Item Level Metadata change

valueisAttrsModified :: Event -> Bool
#

Determine whether the event indicates inode metadata change for an object contained within the monitored path.

Metadata change may include, permissions (e.g., chmod(2)), timestamps (e.g., utimensat(2)), extended attributes (setxattr(2)), link count (since Linux 2.6.25; e.g., for the target of link(2) and for unlink(2)), and user/group ID (e.g., chown(2)).

Can occur for watched path or a file inside it

Pre-release

Item Level Access

valueisAccessed :: Event -> Bool
#

File was accessed (e.g. read, execve).

Occurs only for a file inside the watched directory

Pre-release

valueisOpened :: Event -> Bool
#

File or directory was opened.

Occurs only for a file inside the watched directory

Pre-release

valueisWriteClosed :: Event -> Bool
#

File opened for writing was closed.

Occurs only for a file inside the watched directory

Pre-release

valueisNonWriteClosed :: Event -> Bool
#

File or directory opened for read but not write was closed.

Can occur for watched path or a file inside it

Pre-release

Item Level CRUD events

valueisCreated :: Event -> Bool
#

File/directory created in watched directory (e.g., open(2) O_CREAT, mkdir(2), link(2), symlink(2), bind(2) on a UNIX domain socket).

Occurs only for an object inside the watched directory

Pre-release

valueisDeleted :: Event -> Bool
#

File/directory deleted from watched directory.

Occurs only for an object inside the watched directory

Pre-release

valueisMovedFrom :: Event -> Bool
#

Generated for the original path when an object is moved from under a monitored directory.

Occurs only for an object inside the watched directory

Pre-release

valueisMovedTo :: Event -> Bool
#

Generated for the new path when an object is moved under a monitored directory.

Occurs only for an object inside the watched directory

Pre-release

valueisMoved :: Event -> Bool
#

Generated for a path that is moved from or moved to the monitored directory.

Example1 expression
isMoved ev = isMovedFrom ev || isMovedTo ev

Occurs only for an object inside the watched directory

Pre-release

valueisModified :: Event -> Bool
#

Determine whether the event indicates modification of an object within the monitored path. This event is generated only for files and not directories.

Occurs only for an object inside the watched directory

Pre-release

Item Path info

valueisDir :: Event -> Bool
#

Determine whether the event is for a directory path.

Pre-release

Exception Conditions

valueisEventsLost :: Event -> Bool
#

Event queue overflowed (WD is invalid for this event) and we may have lost some events.. The user application must scan everything under the watched paths to know the current state.

Pre-release

Debugging

1 declaration