FileWatch

An instance of a FileWatcher Contains different implementations (win32 api, inotify and polling using the std.file methods) Specify version = FSWForcePoll; to force using std.file (is slower and more resource intensive than the other implementations)

struct FileWatch {}

Constructors

this
this(string path, bool recursive, bool treatDirAsFile)

Creates an instance using the Win32 API

this
this(string path, bool ignored1, bool ignored2)

Creates an instance using the linux inotify API

this
this(string path, bool recursive, bool treatDirAsFile)

Generic fallback implementation using std.file.dirEntries

Destructor

~this
~this()
Undocumented in source.

Members

Functions

getEvents
FileChangeEvent[] getEvents()

Implementation using Win32 API or polling for files

getEvents
FileChangeEvent[] getEvents()

Generic polling implementation

getEvents
FileChangeEvent[] getEvents()

Implementation using inotify

Variables

path
string path;

Path of the file set using the constructor

Examples

import core.thread;

FileChangeEvent waitForEvent(ref FileWatch watcher)
{
	FileChangeEvent[] ret;
	while ((ret = watcher.getEvents()).length == 0)
	{
		Thread.sleep(1.msecs);
	}
	return ret[0];
}

if (exists("test"))
	rmdirRecurse("test");
scope (exit)
{
	if (exists("test"))
		rmdirRecurse("test");
}

auto watcher = FileWatch("test", true);
mkdir("test");
auto ev = waitForEvent(watcher);
assert(ev.type == FileChangeEventType.createSelf);
write("test/a.txt", "abc");
ev = waitForEvent(watcher);
assert(ev.type == FileChangeEventType.create);
assert(ev.path == "a.txt");
Thread.sleep(2000.msecs); // for polling variant
append("test/a.txt", "def");
ev = waitForEvent(watcher);
assert(ev.type == FileChangeEventType.modify);
assert(ev.path == "a.txt");
rename("test/a.txt", "test/b.txt");
ev = waitForEvent(watcher);
assert(ev.type == FileChangeEventType.rename);
assert(ev.path == "a.txt");
assert(ev.newPath == "b.txt");
remove("test/b.txt");
ev = waitForEvent(watcher);
assert(ev.type == FileChangeEventType.remove);
assert(ev.path == "b.txt");
rmdirRecurse("test");
ev = waitForEvent(watcher);
assert(ev.type == FileChangeEventType.removeSelf);

Meta