![]() |
v1.3.0.0
|
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:
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.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).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) |
|
explicit |
Constructs a new file system watcher object that isn't monitoring any paths. Path(s) can be added later with addPath()
or addPaths()
.
|
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.
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.
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.
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.
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.
Array< string > directories | ( | ) |
Returns a list of paths to directories that are being watched.
Array< string > files | ( | ) |
Returns a list of paths to files that are being watched.
void destroy | ( | ) |
void fileChanged | ( | string | path | ) |
This event is emitted when the file at the specified path is modified, renamed or removed from disk.
watcher.files().contains(path)
. If it returns false
, check whether the file still exists and then call addPath()
to continue watching it. 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.