FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
javaxt-core-dll/FileSystemWatcherNative.cpp at main · javaxt-project/javaxt-core-dll · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
javaxt-project
/
javaxt-core-dll
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
javaxt-core-dll
/
FileSystemWatcherNative.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
240 lines (195 loc) · 8.64 KB
Breadcrumbs
javaxt-core-dll
/
FileSystemWatcherNative.cpp
Copy path
File metadata and controls
240 lines (195 loc) · 8.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
******************************************************************************
//
** FileSystemWatcherNative Class
//
******************************************************************************
/*
*
* JNI code used to monitor changes made to a directory.
*
*****************************************************************************
*/
//
This file needs to be compiled with _UNICODE flag, since java string uses unicode.
#
include
"
stdafx.h
"
#
include
"
FileSystemWatcherNative.h
"
#
include
<
sstream
>
#
include
<
ctime
>
using
namespace
std
;
BOOL
bWatchSubtree;
//
flag used to set whether to watch subdirectories
DWORD
dwNotifyFilter;
//
notification filter
HANDLE
hDirectory;
//
handle to the directory being monitored
wstring
sDirectory
;
//
a string representing the path to the directory
size_t
nBufSize =
32
*
1024
;
FILE_NOTIFY_INFORMATION
* pBuffer = (
FILE_NOTIFY_INFORMATION
*)calloc(
1
, nBufSize);
FILE_NOTIFY_INFORMATION
* pBufferCurrent;
//
**************************************************************************
//
** FindFirstChangeNotification
//
**************************************************************************
/*
*
* Class: FileSystemWatcherNative
* Method: FindFirstChangeNotification
* Signature: (Ljava/lang/String;ZI)J
*/
JNIEXPORT
jlong
JNICALL
Java_javaxt_io_FileSystemWatcherNative_FindFirstChangeNotification
(JNIEnv* env, jclass, jstring filename, jboolean javaWatchSubtree, jint javaNotifyFilter)
{
//
Convert jstring to wstring
const
jchar *_filename = env->
GetStringChars
(filename,
0
);
jsize len = env->
GetStringLength
(filename);
sDirectory
.
assign
(_filename, _filename + len);
env->
ReleaseStringChars
(filename, _filename);
//
Update Inputs
bWatchSubtree = (
BOOL
)javaWatchSubtree;
dwNotifyFilter =
//
(DWORD)javaNotifyFilter;
FILE_NOTIFY_CHANGE_LAST_WRITE
|
//
Triggered when a file or directory has been modified
FILE_NOTIFY_CHANGE_DIR_NAME
|
//
Triggered when a directory has been created or deleted
FILE_NOTIFY_CHANGE_FILE_NAME
;
//
Triggered when a file has been created or deleted
//
Call FindFirstChangeNotification
HANDLE
handle =
FindFirstChangeNotificationW
(
sDirectory
.
c_str
(), bWatchSubtree, dwNotifyFilter);
if
(handle ==
INVALID_HANDLE_VALUE
|| handle == (
HANDLE
)
ERROR_INVALID_FUNCTION
){
DWORD
errorCode =
GetLastError
();
stringstream ss;
ss <<
"
FindFirstChangeNotification failed. Error Code:
"
<< errorCode;
const
char
* msg = (
const
char
*)( ss.
str
().
c_str
());
jclass exceptionClass = env->
FindClass
(
"
java/lang/Exception
"
);
env->
ThrowNew
(exceptionClass, msg );
}
//
Create File Handle
hDirectory =
CreateFileW
(
sDirectory
.
c_str
(),
//
pointer to the directory
GENERIC_READ
,
//
access (read/write) mode
FILE_SHARE_READ
|
FILE_SHARE_WRITE
|
FILE_SHARE_DELETE
,
//
share mode
NULL
,
//
security descriptor
OPEN_EXISTING
,
//
how to create
FILE_FLAG_BACKUP_SEMANTICS
,
//
file attributes
NULL
//
file with attributes to copy
);
//
Return FindFirstChangeNotification Handle
return
(jlong)handle;
}
//
**************************************************************************
//
** FindNextChangeNotification
//
**************************************************************************
/*
*
* Class: FileSystemWatcherNative
* Method: FindNextChangeNotification
* Signature: (J)V
*/
JNIEXPORT
void
JNICALL
Java_javaxt_io_FileSystemWatcherNative_FindNextChangeNotification
(JNIEnv *javaEnv, jclass, jlong handle)
{
if
(!
FindNextChangeNotification
((
HANDLE
) handle)){
DWORD
errorCode =
GetLastError
();
stringstream ss;
ss <<
"
FindNextChangeNotification failed. Error Code:
"
<< errorCode;
const
char
* msg = (
const
char
*)( ss.
str
().
c_str
());
jclass exceptionClass = javaEnv->
FindClass
(
"
java/lang/Exception
"
);
javaEnv->
ThrowNew
(exceptionClass, msg);
}
}
//
**************************************************************************
//
** FindCloseChangeNotification
//
**************************************************************************
/*
*
* Class: FileSystemWatcherNative
* Method: FindCloseChangeNotification
* Signature: (J)V
*/
JNIEXPORT
void
JNICALL
Java_javaxt_io_FileSystemWatcherNative_FindCloseChangeNotification
(JNIEnv *javaEnv, jclass, jlong handle)
{
if
(!
FindCloseChangeNotification
((
HANDLE
) handle)){
DWORD
errorCode =
GetLastError
();
stringstream ss;
ss <<
"
FindCloseChangeNotification failed. Error Code:
"
<< errorCode;
const
char
* msg = (
const
char
*)( ss.
str
().
c_str
());
jclass exceptionClass = javaEnv->
FindClass
(
"
java/lang/Exception
"
);
javaEnv->
ThrowNew
(exceptionClass, msg);
}
//
Close File Handle
if
(hDirectory!=
NULL
){
CloseHandle
(hDirectory);
}
}
//
**************************************************************************
//
** WaitForSingleObject
//
**************************************************************************
/*
*
* Class: FileSystemWatcherNative
* Method: WaitForSingleObject
* Signature: (JI)I
*/
JNIEXPORT
jint
JNICALL
Java_javaxt_io_FileSystemWatcherNative_WaitForSingleObject
(JNIEnv *javaEnv , jclass, jlong hWaitHandle, jint waitTimeoutMillis)
{
return
WaitForSingleObject
((
HANDLE
) hWaitHandle, (
DWORD
) waitTimeoutMillis);
}
std::wstring
s2ws
(
const
std::string& s){
int
len;
int
slength = (
int
)s.
length
() +
1
;
len =
MultiByteToWideChar
(
CP_ACP
,
0
, s.
c_str
(), slength,
0
,
0
);
wchar_t
* buf =
new
wchar_t
[len];
MultiByteToWideChar
(
CP_ACP
,
0
, s.
c_str
(), slength, buf, len);
std::wstring
r
(buf);
delete[]
buf;
return
r;
}
//
**************************************************************************
//
** ReadDirectoryChangesW
//
**************************************************************************
/*
*
* Class: FileSystemWatcherNative
* Method: ReadDirectoryChangesW
* Signature: (Ljava/lang/String;ZI)J
*/
JNIEXPORT
jstring
JNICALL
Java_javaxt_io_FileSystemWatcherNative_ReadDirectoryChangesW
(JNIEnv *env, jclass)
{
wstringstream ss;
DWORD
BytesReturned;
if
(
ReadDirectoryChangesW
(
hDirectory,
//
handle to directory
pBuffer,
//
read results buffer
nBufSize,
//
length of buffer
bWatchSubtree,
//
WatchSubtree
dwNotifyFilter,
//
notification filter
&BytesReturned,
//
bytes returned
NULL
,
//
overlapped buffer
NULL
//
completion routine
)){
//
Iterate through the file changes
pBufferCurrent = pBuffer;
while
(pBufferCurrent){
//
Get current timestamp
time_t
d =
time
(
NULL
);
string
date
(
ctime
(&d) );
date.
erase
(date.
find_last_not_of
(
"
\t\n
"
)+
1
);
//
right trim
//
Get action
wstring action =
L"
"
;
switch
(pBufferCurrent->
Action
){
case
FILE_ACTION_ADDED
: action =
L"
Create
"
;
break
;
case
FILE_ACTION_REMOVED
: action =
L"
Delete
"
;
break
;
case
FILE_ACTION_MODIFIED
: action =
L"
Modify
"
;
break
;
case
FILE_ACTION_RENAMED_OLD_NAME
: action =
L"
Rename
"
;
break
;
case
FILE_ACTION_RENAMED_NEW_NAME
: action =
L"
Renam2
"
;
break
;
}
//
Get filename
WCHAR
* _filename = pBufferCurrent->
FileName
;
int
len = (
int
)pBufferCurrent->
FileNameLength
/
2
;
wstring filename;
filename.
assign
(_filename, _filename + len);
//
Join date, action, and filename into 1 event string
ss <<
L"
[
"
<<
s2ws
(date) <<
L"
]
"
<< action <<
L"
"
<<
sDirectory
<< filename <<
L"
\n
"
;
//
Update pBufferCurrent
if
(pBufferCurrent->
NextEntryOffset
)
pBufferCurrent = (
FILE_NOTIFY_INFORMATION
*)(((
BYTE
*)pBufferCurrent) + pBufferCurrent->
NextEntryOffset
);
else
pBufferCurrent =
NULL
;
}
//
end while pBufferCurrent
}
wstring event = ss.
str
();
int
len = event.
size
();
jchar* raw =
new
jchar[len];
memcpy
(raw, event.
c_str
(), len*
sizeof
(
wchar_t
));
jstring result = env->
NewString
(raw, len);
delete[]
raw;
return
result;
}
Back
|
FazBrowse Home
|
New Git URL