# Poco

# class DirectoryWatcher

Library: Foundation
Package: Filesystem
Header: Poco/DirectoryWatcher.h

## Description

This class is used to get notifications about changes to the filesystem, more specifically, to a specific directory. Changes to a directory are reported via events.

A thread will be created that watches the specified directory for changes. Events are reported in the context of this thread.

Note that changes to files in subdirectories of the watched directory are not reported. Separate DirectoryWatcher objects must be created for these directories if they should be watched.

Changes to file attributes are not reported.

On Windows, this class is implemented using FindFirstChangeNotification()/FindNextChangeNotification(). On Linux, this class is implemented using inotify. On FreeBSD and Darwin (Mac OS X, iOS), this class uses kevent/kqueue. On all other platforms, the watched directory is periodically scanned for changes. This can negatively affect performance if done too often. Therefore, the interval in which scans are done can be specified in the constructor. Note that periodic scanning will also be done on FreeBSD and Darwin if events for changes to files (DW_ITEM_MODIFIED) are enabled.

DW_ITEM_MOVED_FROM and DW_ITEM_MOVED_TO events will only be reported on Linux. On other platforms, a file rename or move operation will be reported via a DW_ITEM_REMOVED and a DW_ITEM_ADDED event. The order of these two events is not defined.

An event mask can be specified to enable only certain events.

## Inheritance

Direct Base Classes: Runnable

All Base Classes: Runnable

## Member Summary

Member Functions: directory, eventMask, eventsSuspended, init, resumeEvents, run, scanInterval, stop, supportsMoveEvents, suspendEvents

Inherited Functions: run

## Nested Classes

### struct DirectoryEvent

## Enumerations

### Anonymous

DW_DEFAULT_SCAN_INTERVAL = 5

Default scan interval for platforms that don't provide a native notification mechanism.

### DirectoryEventMask

DW_FILTER_ENABLE_ALL = 31

Enables all event types.

DW_FILTER_DISABLE_ALL = 0

Disables all event types.

### DirectoryEventType

DW_ITEM_ADDED = 1

A new item has been created and added to the directory.

DW_ITEM_REMOVED = 2

An item has been removed from the directory.

DW_ITEM_MODIFIED = 4

An item has been modified.

DW_ITEM_MOVED_FROM = 8

An item has been renamed or moved. This event delivers the old name.

DW_ITEM_MOVED_TO = 16

An item has been renamed or moved. This event delivers the new name.

## Constructors

### DirectoryWatcher

DirectoryWatcher(
const std::string & path,
int eventMask = DW_FILTER_ENABLE_ALL,
int scanInterval = DW_DEFAULT_SCAN_INTERVAL
);

Creates a DirectoryWatcher for the directory given in path. To enable only specific events, an eventMask can be specified by OR-ing the desired event IDs (e.g., DW_ITEM_ADDED | DW_ITEM_MODIFIED). On platforms where no native filesystem notifications are available, scanInterval specifies the interval in seconds between scans of the directory.

### DirectoryWatcher

DirectoryWatcher(
const File & directory,
int eventMask = DW_FILTER_ENABLE_ALL,
int scanInterval = DW_DEFAULT_SCAN_INTERVAL
);

Creates a DirectoryWatcher for the specified directory To enable only specific events, an eventMask can be specified by OR-ing the desired event IDs (e.g., DW_ITEM_ADDED | DW_ITEM_MODIFIED). On platforms where no native filesystem notifications are available, scanInterval specifies the interval in seconds between scans of the directory.

## Destructor

### ~DirectoryWatcher

~DirectoryWatcher();

Destroys the DirectoryWatcher.

## Member Functions

### directory

const File & directory() const;

Returns the directory being watched.

### eventMask

int eventMask() const;

Returns the value of the eventMask passed to the constructor.

### eventsSuspended

bool eventsSuspended() const;

Returns true if and only if events are suspended.

### resumeEvents

void resumeEvents();

Resumes events, after they have been suspended with a call to suspendEvents().

### scanInterval

int scanInterval() const;

Returns the scan interval in seconds.

### supportsMoveEvents

bool supportsMoveEvents() const;

Returns true if and only if the platform supports DW_ITEM_MOVED_FROM/itemMovedFrom and DW_ITEM_MOVED_TO/itemMovedTo events.

### suspendEvents

void suspendEvents();

Suspends sending of events. Can be called multiple times, but every call to suspendEvent() must be matched by a call to resumeEvents().

### init

void init();

### run

void run();

See also: Poco::Runnable::run()

### stop

void stop();

## Variables

### itemAdded

BasicEvent < const DirectoryEvent > itemAdded;

Fired when a file or directory has been created or added to the directory.

### itemModified

BasicEvent < const DirectoryEvent > itemModified;

Fired when a file or directory has been modified.

### itemMovedFrom

BasicEvent < const DirectoryEvent > itemMovedFrom;

Fired when a file or directory has been renamed. This event delivers the old name.

### itemMovedTo

BasicEvent < const DirectoryEvent > itemMovedTo;

Fired when a file or directory has been moved. This event delivers the new name.

### itemRemoved

BasicEvent < const DirectoryEvent > itemRemoved;

Fired when a file or directory has been removed from the directory.

### scanError

BasicEvent < const Exception > scanError;

Fired when an error occurs while scanning for changes.