v1.3.0.0
FSWatcher

Description

The FSWatcher class provides an interface for monitoring files and directories for modifications.

FSWatcher monitors the file system for changes to files and directories by watching a list of specified paths.

Call addPath() to watch a particular file or directory. Multiple paths can be added using the addPaths() method. Existing paths can be removed by using the removePath() and removePaths() methods.

FSWatcher examines each path added to it. Files that have been added to the FSWatcher can be accessed using the files() method, and directories using directories().

The fileChanged() event is emitted when a file has been modified, renamed or removed from disk. Similarly, the directoryChanged() event is emitted when a directory or its contents is modified or removed. Note that FSWatcher stops monitoring files once they have been renamed or removed from disk, and directories once they have been removed from disk.

Keep in mind that the instance of FSWatcher must persist for as long as you need the path(s) to be watched. If the variable holding the instance goes out of scope, it may get garbage-collected at any point and stop emitting events.

To terminate watching all paths and delete an instance of FSWatcher, use the destroy() method and set your instance variable value to null or undefined.

See the Tail Example for sample usage.

Notes:

  • On systems running a Linux kernel without inotify support, file systems that contain watched paths cannot be unmounted.
  • The act of monitoring files and directories for modifications consumes system resources. This implies there is a limit to the number of files and directories your process can monitor simultaneously. Some system limits the number of open file descriptors to 256 by default. This means that addPath() and addPaths() will fail if your process tries to add more than 256 files or directories to the file system monitor. Also note that your process may have other file descriptors open in addition to the ones for files being monitored, and these other open descriptors also count in the total. MacOS uses a different backend and does not suffer from this issue.
Note
The FSWatcher class is a proxy for QFileSystemWatcher, part of the underlying Qt C++ library. Most of the documentation text here is originally from the QFileSystemWatcher documentation (used under the GNU Free Documentation License version 1.3).
See also
File, Dir, Tail Example
Since
v1.3

Public Member Functions

 FSWatcher ()
 
 FSWatcher (Array< string > &paths)
 
bool addPath (string &path)
 
Array< string > addPaths (Array< string > &paths)
 
bool removePath (string &path)
 
Array< string > removePaths (Array< string > &paths)
 
Array< string > directories ()
 
Array< string > files ()
 
void destroy ()
 
Events

See Event Handling for details about connecting handlers to events.

void fileChanged (string path)
 
void directoryChanged (string path)
 

Constructor & Destructor Documentation

◆ FSWatcher() [1/2]

FSWatcher ( )
explicit

Constructs a new file system watcher object that isn't monitoring any paths. Path(s) can be added later with addPath() or addPaths().

◆ FSWatcher() [2/2]

FSWatcher ( Array< string > &  paths)
explicit

Constructs a new file system watcher object which monitors the specified paths list. See addPaths() for details of how the paths argument is treated.

Member Function Documentation

◆ addPath()

bool addPath ( string &  path)

Adds path to the file system watcher if path exists.

The path is not added if it does not exist, or if it is already being monitored by this file system watcher.

If path specifies a directory, the directoryChanged() event will be emitted when path is modified or removed from disk; otherwise the fileChanged() event is emitted when path is modified, renamed or removed.

If the watch was successful, true is returned.

Reasons for a watch failure are generally system-dependent, but may include the resource not existing, access failures, or the total watch count limit, if the platform has one.

See also
addPaths(), removePath()

◆ addPaths()

Array< string > addPaths ( Array< string > &  paths)

Adds each path in paths to the file system watcher.

Paths are not added if they do not exist, or if they are already being monitored by the file system watcher.

If a path specifies a directory, the directoryChanged() signal will be emitted when the path is modified or removed from disk; otherwise the fileChanged() signal is emitted when the path is modified, renamed, or removed.

The return value is a list of paths that could not be watched.

Reasons for a watch failure are generally system-dependent, but may include the resource not existing, access failures, or the total watch count limit, if the platform has one.

Note
There may be a system dependent limit to the number of files and directories that can be monitored simultaneously. If this limit has been reached, the excess paths will not be monitored, and they will be added to the returned list.
See also
addPath(), removePaths()

◆ removePath()

bool removePath ( string &  path)

Removes the specified path from the file system watcher.

If the watch is successfully removed, true is returned.

Reasons for watch removal failing are generally system-dependent, but may be due to the path having already been deleted, for example.

See also
removePaths(), addPath()

◆ removePaths()

Array< string > removePaths ( Array< string > &  paths)

Removes the specified paths from the file system watcher.

The return value is a list of paths which were not able to be un-watched successfully.

Reasons for watch removal failing are generally system-dependent, but may be due to the path having already been deleted, for example.

See also
removePath(), addPaths()

◆ directories()

Array< string > directories ( )

Returns a list of paths to directories that are being watched.

See also
files()

◆ files()

Array< string > files ( )

Returns a list of paths to files that are being watched.

See also
directories()

◆ destroy()

void destroy ( )

Stops watching all paths and destroys this instance of FSWatcher. Trying to use this instance after calling destroy() is undefined behavior.

◆ fileChanged()

void fileChanged ( string  path)

This event is emitted when the file at the specified path is modified, renamed or removed from disk.

Note
As a safety measure, many applications save an open file by writing a new file and then deleting the old one. In your handler function, you can check if watcher.files().contains(path). If it returns false, check whether the file still exists and then call addPath() to continue watching it.
See also
directoryChanged()

◆ directoryChanged()

void directoryChanged ( string  path)

This event is emitted when the directory at a specified path is modified (e.g., when a file is added or deleted) or removed from disk.

Note that if there are several changes during a short period of time, some of the changes might not emit this event. However, the last change in the sequence of changes will always generate this event.

See also
fileChanged()