From 815db0727c6374a2fcfb6e2fb6795b1e4ed953a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Mon, 20 Nov 2017 22:23:30 +0100 Subject: [PATCH] Change all invalid handle values to ~0 and improve FileChange. FileChange now has the full path of a file split into the base path (as specified when creating the watcher), the sub directory, and the file name. This allows to work with less dynamic memory allocations internally. --- source/eventcore/driver.d | 25 ++++++++++++++-------- source/eventcore/drivers/winapi/watchers.d | 8 ++++--- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/source/eventcore/driver.d b/source/eventcore/driver.d index 8b4f07c..a411bd1 100644 --- a/source/eventcore/driver.d +++ b/source/eventcore/driver.d @@ -652,14 +652,21 @@ struct FileChange { /// The type of change FileChangeKind kind; - /// Directory containing the changed file - string directory; + /// The root directory of the watcher + string baseDirectory; - /// Determines if the changed entity is a file or a directory. - bool isDirectory; + /// Subdirectory containing the changed file + string directory; /// Name of the changed file const(char)[] name; + + /** Determines if the changed entity is a file or a directory. + + Note that depending on the platform this may not be accurate for + `FileChangeKind.removed`. + */ + bool isDirectory; } struct Handle(string NAME, T, T invalid_value = T.init) { @@ -695,8 +702,8 @@ alias StreamListenSocketFD = Handle!("streamListen", SocketFD); alias DatagramSocketFD = Handle!("datagramSocket", SocketFD); alias FileFD = Handle!("file", FD); alias EventID = Handle!("event", FD); -alias TimerID = Handle!("timer", size_t); -alias WatcherID = Handle!("watcher", size_t); -alias EventWaitID = Handle!("eventWait", size_t); -alias SignalListenID = Handle!("signal", size_t); -alias DNSLookupID = Handle!("dns", size_t); +alias TimerID = Handle!("timer", size_t, size_t.max); +alias WatcherID = Handle!("watcher", size_t, size_t.max); +alias EventWaitID = Handle!("eventWait", size_t, size_t.max); +alias SignalListenID = Handle!("signal", size_t, size_t.max); +alias DNSLookupID = Handle!("dns", size_t, size_t.max); diff --git a/source/eventcore/drivers/winapi/watchers.d b/source/eventcore/drivers/winapi/watchers.d index 1911cd7..f7fc0a4 100644 --- a/source/eventcore/drivers/winapi/watchers.d +++ b/source/eventcore/drivers/winapi/watchers.d @@ -78,6 +78,7 @@ final class WinAPIEventDriverWatchers : EventDriverWatchers { void onIOCompleted(DWORD dwError, DWORD cbTransferred, OVERLAPPED* overlapped) { import std.conv : to; + import std.path : dirName, baseName; if (dwError != 0) { // FIXME: this must be propagated to the caller @@ -114,9 +115,10 @@ final class WinAPIEventDriverWatchers : EventDriverWatchers { case 0x4: ch.kind = FileChangeKind.removed; break; case 0x5: ch.kind = FileChangeKind.added; break; } - ch.directory = slot.directory; - ch.isDirectory = false; // FIXME: is this right? - ch.name = () @trusted { scope (failure) assert(false); return to!string(fni.FileName[0 .. fni.FileNameLength/2]); } (); + ch.baseDirectory = slot.directory; + auto path = () @trusted { scope (failure) assert(false); return to!string(fni.FileName[0 .. fni.FileNameLength/2]); } (); + ch.directory = dirName(path); + ch.name = baseName(path); slot.callback(id, ch); if (fni.NextEntryOffset == 0) break; result = result[fni.NextEntryOffset .. $];