-
Notifications
You must be signed in to change notification settings - Fork 6
/
directory.d
168 lines (146 loc) · 4.95 KB
/
directory.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
module thBase.directory;
import core.sys.windows.windows;
import core.stdc.string;
import core.stdc.stdlib;
import thBase.windows;
import thBase.enumbitfield;
import thBase.string;
import thBase.format;
import thBase.casts;
import thBase.allocator;
version(Windows)
{
class DirectoryWatcher
{
private:
HANDLE m_directoryHandle;
HANDLE m_completionPort;
WatchSubdirs m_watchSubdirs;
DWORD m_filter;
OVERLAPPED m_overlapped;
void[4096] m_buffer = void;
public:
enum Watch : uint
{
Reads = 1 << 0,
Writes = 1 << 1,
Creates = 1 << 2,
Renames = 1 << 3
}
enum WatchSubdirs
{
No = 0,
Yes = 1
}
enum Action
{
Added = FILE_ACTION_ADDED,
Removed = FILE_ACTION_REMOVED,
Modified = FILE_ACTION_MODIFIED,
RenamedOldName = FILE_ACTION_RENAMED_OLD_NAME,
RenamedNewName = FILE_ACTION_RENAMED_NEW_NAME
}
this(const(char)[] path, WatchSubdirs watchSubdirs, EnumBitfield!Watch watch)
{
m_watchSubdirs = watchSubdirs;
m_filter = 0;
if(watch.IsSet(Watch.Reads))
m_filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
if(watch.IsSet(Watch.Writes))
m_filter |= FILE_NOTIFY_CHANGE_LAST_WRITE;
if(watch.IsSet(Watch.Creates))
m_filter |= FILE_NOTIFY_CHANGE_CREATION;
if(watch.IsSet(Watch.Renames))
m_filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
mixin(stackCString("path", "cstrPath"));
m_directoryHandle = CreateFileA(
cstrPath.ptr,
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
null,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
null);
if(m_directoryHandle == INVALID_HANDLE_VALUE)
{
throw New!RCException(format("Couldn't open directory '%s'. Maybe it does not exist?", path));
}
m_completionPort = CreateIoCompletionPort(m_directoryHandle, null, 0, 1);
if(m_completionPort == INVALID_HANDLE_VALUE)
{
throw New!RCException(format("Couldn't create io completion port for directory '%s'", path));
}
DoRead();
}
~this()
{
CancelIo(m_directoryHandle);
CloseHandle(m_completionPort);
CloseHandle(m_directoryHandle);
}
private final DoRead()
{
memset(&m_overlapped, 0, m_overlapped.sizeof);
ReadDirectoryChangesW(m_directoryHandle, m_buffer.ptr, m_buffer.length, (m_watchSubdirs == WatchSubdirs.Yes),
m_filter, null, &m_overlapped, null);
}
final void EnumerateChanges(scope void delegate(const(char)[] filename, Action action) func)
{
OVERLAPPED* lpOverlapped;
uint numberOfBytes;
ULONG_PTR completionKey;
while( GetQueuedCompletionStatus(m_completionPort, &numberOfBytes, &completionKey, &lpOverlapped, 0) != 0)
{
//Copy the buffer
assert(numberOfBytes > 0);
void[] buffer = alloca(numberOfBytes)[0..numberOfBytes];
buffer[0..$] = m_buffer[0..numberOfBytes];
//Reissue the read request
DoRead();
//Progress the messages
auto info = cast(const(FILE_NOTIFY_INFORMATION)*)buffer.ptr;
while(true)
{
const(WCHAR)[] directory = info.FileName.ptr[0..(info.FileNameLength/2)];
int bytesNeeded = WideCharToMultiByte(CP_UTF8, 0, directory.ptr, int_cast!int(directory.length), null, 0, null, null);
if(bytesNeeded > 0)
{
char[] dir = (cast(char*)alloca(bytesNeeded))[0..bytesNeeded];
WideCharToMultiByte(CP_UTF8, 0, directory.ptr, int_cast!int(directory.length), dir.ptr, int_cast!int(dir.length), null, null);
func(dir, cast(Action)info.Action);
}
if(info.NextEntryOffset == 0)
break;
else
info = cast(const(FILE_NOTIFY_INFORMATION)*)((cast(void*)info) + info.NextEntryOffset);
}
}
}
}
bool exists(const(char)[] filename)
{
mixin(stackCString("filename", "cstr"));
DWORD attributes = GetFileAttributesA(cstr.ptr);
return (attributes != 0xFFFFFFFF &&
(attributes & FILE_ATTRIBUTE_DIRECTORY));
}
bool create(const(char)[] path)
{
mixin(stackCString("path", "cstr"));
return CreateDirectoryA(cstr.ptr, null) != 0;
}
size_t getWorkingDirectory(char[] buffer)
{
return int_cast!size_t(GetCurrentDirectoryA(int_cast!DWORD(buffer.length), buffer.ptr));
}
void setWorkingDirectory(const(char)[] directory)
{
mixin(stackCString("directory", "cstr"));
SetCurrentDirectoryA(cstr.ptr);
}
void setDllDirectory(const(char)[] directory)
{
mixin(stackCString("directory", "cstr"));
SetDllDirectoryA(cstr.ptr);
}
}