NAME

UniEvent::FsEvent - cross-platform file monitoring

SYNOPSIS

use UniEvent::FsEvent; # imports constants

my $loop = UniEvent::Loop->default_loop;
my $h = UniEvent::FsEvent->new($loop);
my $dir = "/tmp/";
$h->start($dir);
$h->callback(sub {
    my ($handle, $filename, $events, $error_code) = @_;
        if ($events & RENAME) {
        say "file $dir/$filename has been renamed";
    }
});
$loop->run;

DESCRIPTION

The FsEvent is useful for monitoring individual file events , e.g. when file is renamed, created or when its content was changed. It works on major operational systems, including Windows; however the order of events might very system dependent.

The UniEvent::FsEvent is inherited from UniEvent::Handle.

METHODS

new([$loop = UniEvent::Loop->default_loop])

Constructs new FsEvent handle and binds it to the specified event loop

start($path [, $flags = 0 [, $callback]] )

Starts monitoring file system events on the specified $path, i.e. makes it active for the next even loop iteration. The $path can point to file as well as to directory.

The $flags is a bitmask of interested events (see FLAGS below)

The $callback is invoked with the current handle, $filename where change occured, the bitmask of actually occurred event and error code, i.e.

$callback->($handle, $filename, $events, $error_code)

callback($code)

Sets the callback, which will be triggered once an event occur.

event()

Returns XS::Framework::CallbackDispatcher instance, where callback(s) can be assigned with. The callbacks will be invoked upon changes on a file. The C++ interface is:

void(const FsEventSP&, const string_view& file, int events, const std::error_code&);

Multiple callbacks can be added via the XS::Framework::CallbackDispatcher interface.

event_listener($delegate [, $weak = false])

Delegates callback invocation to the $delegate object of arbitrary class having on_fs_event method. The object can optionally be weakened.

The method will be invoked as

$obj->on_fs_event($filename, $events, $error_code);

path()

Returns the currently monitored path.

CONSTANTS

FLAGS

RECURSIVE

Watch for changes recursively (might not be available on all OSes)

RENAME

Watch for file or directory renaming

CHANGE

Watch for the file data (content) or metadata changes (e.g. mtime)

TYPE

FsEvent type constant

REFERENCES

UniEvent::Handle

XS::Framework::CallbackDispatcher