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.
This commit is contained in:
Sönke Ludwig 2017-11-20 22:23:30 +01:00
parent 5bba45485c
commit 815db0727c
2 changed files with 21 additions and 12 deletions

View file

@ -652,14 +652,21 @@ struct FileChange {
/// The type of change /// The type of change
FileChangeKind kind; FileChangeKind kind;
/// Directory containing the changed file /// The root directory of the watcher
string directory; string baseDirectory;
/// Determines if the changed entity is a file or a directory. /// Subdirectory containing the changed file
bool isDirectory; string directory;
/// Name of the changed file /// Name of the changed file
const(char)[] name; 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) { 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 DatagramSocketFD = Handle!("datagramSocket", SocketFD);
alias FileFD = Handle!("file", FD); alias FileFD = Handle!("file", FD);
alias EventID = Handle!("event", FD); alias EventID = Handle!("event", FD);
alias TimerID = Handle!("timer", size_t); alias TimerID = Handle!("timer", size_t, size_t.max);
alias WatcherID = Handle!("watcher", size_t); alias WatcherID = Handle!("watcher", size_t, size_t.max);
alias EventWaitID = Handle!("eventWait", size_t); alias EventWaitID = Handle!("eventWait", size_t, size_t.max);
alias SignalListenID = Handle!("signal", size_t); alias SignalListenID = Handle!("signal", size_t, size_t.max);
alias DNSLookupID = Handle!("dns", size_t); alias DNSLookupID = Handle!("dns", size_t, size_t.max);

View file

@ -78,6 +78,7 @@ final class WinAPIEventDriverWatchers : EventDriverWatchers {
void onIOCompleted(DWORD dwError, DWORD cbTransferred, OVERLAPPED* overlapped) void onIOCompleted(DWORD dwError, DWORD cbTransferred, OVERLAPPED* overlapped)
{ {
import std.conv : to; import std.conv : to;
import std.path : dirName, baseName;
if (dwError != 0) { if (dwError != 0) {
// FIXME: this must be propagated to the caller // FIXME: this must be propagated to the caller
@ -114,9 +115,10 @@ final class WinAPIEventDriverWatchers : EventDriverWatchers {
case 0x4: ch.kind = FileChangeKind.removed; break; case 0x4: ch.kind = FileChangeKind.removed; break;
case 0x5: ch.kind = FileChangeKind.added; break; case 0x5: ch.kind = FileChangeKind.added; break;
} }
ch.directory = slot.directory; ch.baseDirectory = slot.directory;
ch.isDirectory = false; // FIXME: is this right? auto path = () @trusted { scope (failure) assert(false); return to!string(fni.FileName[0 .. fni.FileNameLength/2]); } ();
ch.name = () @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); slot.callback(id, ch);
if (fni.NextEntryOffset == 0) break; if (fni.NextEntryOffset == 0) break;
result = result[fni.NextEntryOffset .. $]; result = result[fni.NextEntryOffset .. $];