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)

Constructors

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

Creates an instance using the Win32 API

this
this(string path, bool recursive = false, bool ignored = false)

Creates an instance using the linux inotify API

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

Generic fallback implementation using std.file.dirEntries

Destructor

A destructor is present on this object, but not explicitly documented in the 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

1 import core.thread;
2 
3 FileChangeEvent waitForEvent(ref FileWatch watcher)
4 {
5 	FileChangeEvent[] ret;
6 	while ((ret = watcher.getEvents()).length == 0)
7 	{
8 		Thread.sleep(1.msecs);
9 	}
10 	return ret[0];
11 }
12 
13 if (exists("test"))
14 	rmdirRecurse("test");
15 scope (exit)
16 {
17 	if (exists("test"))
18 		rmdirRecurse("test");
19 }
20 
21 auto watcher = FileWatch("test", true);
22 mkdir("test");
23 auto ev = waitForEvent(watcher);
24 assert(ev.type == FileChangeEventType.createSelf);
25 write("test/a.txt", "abc");
26 ev = waitForEvent(watcher);
27 assert(ev.type == FileChangeEventType.create);
28 assert(ev.path == "a.txt");
29 Thread.sleep(2000.msecs); // for polling variant
30 append("test/a.txt", "def");
31 ev = waitForEvent(watcher);
32 assert(ev.type == FileChangeEventType.modify);
33 assert(ev.path == "a.txt");
34 rename("test/a.txt", "test/b.txt");
35 ev = waitForEvent(watcher);
36 assert(ev.type == FileChangeEventType.rename);
37 assert(ev.path == "a.txt");
38 assert(ev.newPath == "b.txt");
39 remove("test/b.txt");
40 ev = waitForEvent(watcher);
41 assert(ev.type == FileChangeEventType.remove);
42 assert(ev.path == "b.txt");
43 rmdirRecurse("test");
44 ev = waitForEvent(watcher);
45 assert(ev.type == FileChangeEventType.removeSelf);

Meta