-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileAttributes.cpp
406 lines (342 loc) · 13.5 KB
/
FileAttributes.cpp
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "stdafx.h"
#include "FileAttributes.h"
#include <sstream>
#include <lm.h>
//#include <iostream> //for cout
using namespace std;
//******************************************************************************
//** FileAttributes Class
//******************************************************************************
/**
* JNI code used to list files, network drives, and file attributes on Windows
* file systems. Requires "mpr.lib" and "Netapi32.lib" for network IO.
*
******************************************************************************/
std::istringstream &operator >>(std::istringstream &iss, __int64 &n){
sscanf(iss.str().c_str(), "%I64d", &n);
return iss;
}
//**************************************************************************
//** from_string
//**************************************************************************
/** Allows users to append/concat an __int64 to a stringstream. This method
* is required for Visual Studio 6 compilers. Visual Studio 2003 and higher
* don't need this. GCC doesn't seem to need it either. Credit:
* http://www.codeguru.com/forum/archive/index.php/t-342716.html
*/
template <typename T> bool from_string(T &t, const std::string &s, std::ios_base & (*f)(std::ios_base&)){
std::istringstream iss(s);
iss >> f, iss >> t;
return !iss.fail();
}
//**************************************************************************
//** date2int
//**************************************************************************
/** Converts FILETIME to a long value representing a date.
*/
__int64 date2int(const FILETIME &ft){
SYSTEMTIME stUTC, stLocal;
// Convert the last-write time to local time.
FileTimeToSystemTime(&ft, &stUTC);
SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
stringstream ss;
ss << stLocal.wYear;
if (stLocal.wMonth<10) ss << "0" << stLocal.wMonth;
else ss << stLocal.wMonth;
if (stLocal.wDay<10) ss << "0" << stLocal.wDay;
else ss << stLocal.wDay;
if (stLocal.wHour<10) ss << "0" << stLocal.wHour;
else ss << stLocal.wHour;
if (stLocal.wMinute<10) ss << "0" << stLocal.wMinute;
else ss << stLocal.wMinute;
if (stLocal.wSecond<10) ss << "0" << stLocal.wSecond;
else ss << stLocal.wSecond;
if (stLocal.wMilliseconds<100 && stLocal.wMilliseconds>10) ss << "0" << stLocal.wMilliseconds;
else if (stLocal.wMilliseconds<10) ss << "00" << stLocal.wMilliseconds;
else ss << stLocal.wMilliseconds;
//cout << ss.str();
//cout << "\n";
__int64 val;
bool bconvert = from_string<__int64>(val, ss.str(), std::dec); //ss >> val;
return val;
}
//**************************************************************************
//** GetFileAttributesEx
//**************************************************************************
/** Returns an array of long values representing WIN32_FILE_ATTRIBUTE_DATA.
* Credit: http://www.cplusplus.com/forum/windows/24603/
*
* Class: javaxt_io_File
* Method: GetFileAttributesEx
* Signature: (Ljava/lang/String;)[J
*/
JNIEXPORT jlongArray JNICALL Java_javaxt_io_File_GetFileAttributesEx(JNIEnv *env, jclass, jstring filename)
{
//Convert jstring to wstring
const jchar *_filename = env->GetStringChars(filename, 0);
jsize len = env->GetStringLength(filename);
wstring path;
path.assign(_filename, _filename + len);
env->ReleaseStringChars(filename, _filename);
//Get attributes
WIN32_FILE_ATTRIBUTE_DATA fileAttrs;
BOOL result = GetFileAttributesExW(path.c_str(), GetFileExInfoStandard, &fileAttrs);
if (!result) {
jclass exceptionClass = env->FindClass("java/lang/Exception");
env->ThrowNew(exceptionClass, "Exception Occurred");
}
/*
typedef struct _WIN32_FILE_ATTRIBUTE_DATA {
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
} WIN32_FILE_ATTRIBUTE_DATA, *LPWIN32_FILE_ATTRIBUTE_DATA;
*/
//Create an array to store the WIN32_FILE_ATTRIBUTE_DATA
jlong buffer[6];
buffer[0] = fileAttrs.dwFileAttributes;
buffer[1] = date2int(fileAttrs.ftCreationTime);
buffer[2] = date2int(fileAttrs.ftLastAccessTime);
buffer[3] = date2int(fileAttrs.ftLastWriteTime);
buffer[4] = fileAttrs.nFileSizeHigh;
buffer[5] = fileAttrs.nFileSizeLow;
jlongArray jLongArray = env->NewLongArray(6);
env->SetLongArrayRegion(jLongArray, 0, 6, buffer);
return jLongArray;
}
//**************************************************************************
//** GetTarget
//**************************************************************************
/** Returns the target of a WIndows Junction. Credit:
* http://www.flexhex.com/docs/articles/hard-links.phtml
*
* Class: javaxt_io_File
* Method: GetTarget
* Signature: (Ljava/lang/String;ZI)J
*/
JNIEXPORT jstring JNICALL Java_javaxt_io_File_GetTarget(JNIEnv *env, jclass, jstring filename)
{
//Convert jstring to wstring
const jchar *_filename = env->GetStringChars(filename, 0);
jsize len = env->GetStringLength(filename);
wstring path;
path.assign(_filename, _filename + len);
env->ReleaseStringChars(filename, _filename);
LPCWSTR pPath = GetTarget(path.c_str());
if (pPath==NULL) return NULL;
else{
jchar * jc = (jchar *) pPath;
int i = wcslen(pPath);
return env->NewString(jc, i);
}
}
//**************************************************************************
//** GetSharedDrives - Requires Netapi32.lib
//**************************************************************************
/** Returns a list of shared drives found on a server
*
* Class: javaxt_io_File
* Method: GetSharedDrives
* Signature: (Ljava/lang/String;ZI)J
*/
JNIEXPORT jstring JNICALL Java_javaxt_io_File_GetSharedDrives(JNIEnv *env, jclass, jstring servername)
{
//Convert jstring to wstring
const jchar *_servername = env->GetStringChars(servername, 0);
jsize x = env->GetStringLength(servername);
wstring str;
str.assign(_servername, _servername + x);
env->ReleaseStringChars(servername, _servername);
wstringstream ss;
PSHARE_INFO_502 BufPtr,p;
NET_API_STATUS res;
DWORD er=0,tr=0,resume=0, i;
do{
res = NetShareEnum ((LPWSTR) str.c_str(), 502, (LPBYTE *) &BufPtr, MAX_PREFERRED_LENGTH, &er, &tr, &resume);
if (res == ERROR_SUCCESS || res == ERROR_MORE_DATA){
p=BufPtr;
//Loop through the entries
for (i=1; i<=er; i++) {
//Iterate through the file changes
wstring type = L"Unknown";
bool ret = true;
switch(p->shi502_type){
case STYPE_DISKTREE: type = L"Disk"; break;
case STYPE_PRINTQ: type = L"Printer"; ret = false; break;
case STYPE_DEVICE: type = L"Device"; ret = false; break;
case STYPE_IPC: type = L"IPC"; ret = false; break;
//The following 2 lines won't compile on my Windows XP x32
//case STYPE_SPECIAL: type = L"Special"; break;
//case STYPE_TEMPORARY: type = L"Temp"; break;
}
wstring path = wstring(p->shi502_path);
if (path.empty()) ret = false;
if (ret) ss << wstring(p->shi502_netname) << L"\t" << type << L"\t" << path << L"\n";
//if (IsValidSecurityDescriptor(p->shi502_security_descriptor))
// printf("Yes\n");
//else
// printf("No\n");
p++;
}
//Free the allocated buffer
NetApiBufferFree(BufPtr);
}
else {
stringstream ss;
ss << res;
jclass exceptionClass = env->FindClass("java/lang/Exception");
env->ThrowNew(exceptionClass, ss.str().c_str());
}
}
while (res==ERROR_MORE_DATA);
wstring cstr = ss.str();
int len = cstr.size();
jchar* raw = new jchar[len];
memcpy(raw, cstr.c_str(), len*sizeof(wchar_t));
jstring result = env->NewString(raw, len);
delete[] raw;
return result;
}
//**************************************************************************
//** GetNetworkDrives
//**************************************************************************
/** Returns a list of network drives mounted on the host computer. This is
* similar to the "net use" command.
*
* Class: javaxt_io_File
* Method: GetNetworkDrives
* Signature: (Ljava/lang/String;ZI)J
*/
JNIEXPORT jstring JNICALL Java_javaxt_io_File_GetNetworkDrives(JNIEnv *env, jclass)
{
wstringstream ss;
HANDLE h;
if (WNetOpenEnum(RESOURCE_REMEMBERED, RESOURCETYPE_DISK, RESOURCEUSAGE_ALL, NULL, &h) == NO_ERROR) {
DWORD is = 2;
NETRESOURCE buf[26] = { 0 };
DWORD c = -1;
DWORD s = sizeof(buf);
DWORD r;
if ((r = WNetEnumResource(h, &c, buf, &s)) != NO_ERROR){
jclass exceptionClass = env->FindClass("java/lang/Exception");
env->ThrowNew(exceptionClass, "Exception Occurred");
}
for (int i = 0; i < c; ++i){
NETRESOURCE ns = buf[i];
ss << ns.lpLocalName << L"\t" << ns.lpRemoteName << L"\n";
}
}
wstring cstr = ss.str();
int len = cstr.size();
jchar* raw = new jchar[len];
memcpy(raw, cstr.c_str(), len*sizeof(wchar_t));
jstring result = env->NewString(raw, len);
delete[] raw;
return result;
}
//**************************************************************************
//** GetFiles
//**************************************************************************
/** Returns a list of files found in the current directory. Note that the
* filename must end with "\*"
*
* Class: javaxt_io_File
* Method: GetFiles
* Signature: (Ljava/lang/String;ZI)J
*/
JNIEXPORT jstring JNICALL Java_javaxt_io_File_GetFiles(JNIEnv *env, jclass, jstring directory)
{
HANDLE hFind;
try {
//Convert jstring to wstring
const jchar *_directory = env->GetStringChars(directory, 0);
jsize x = env->GetStringLength(directory);
wstring path; //L"C:\\temp\\*"; //Prepend "\\?\"
path.assign(_directory, _directory + x);
env->ReleaseStringChars(directory, _directory);
if (x<2){
jclass exceptionClass = env->FindClass("java/lang/Exception");
env->ThrowNew(exceptionClass, "Invalid path, less than 2 characters long.");
}
/*
int pos = path.find_last_of(L'\\');
int size = path.size();
if (pos != size - 1)
{
throw std::exception("CUtils::TraverseFS no trailing slash after path");
}
*/
wstringstream ss;
BOOL bContinue = TRUE;
WIN32_FIND_DATAW data;
hFind = FindFirstFileW(path.c_str(), &data);
if (INVALID_HANDLE_VALUE == hFind){
jclass exceptionClass = env->FindClass("java/lang/Exception");
env->ThrowNew(exceptionClass, "FindFirstFileW returned invalid handle.");
}
//HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//DWORD dwBytesWritten;
// If we have no error, loop thru the files in this dir
while (hFind && bContinue){
/*
//Debug Print Statment. DO NOT DELETE! cout and wcout do not print unicode correctly.
WriteConsole(hStdOut, data.cFileName, (DWORD)_tcslen(data.cFileName), &dwBytesWritten, NULL);
WriteConsole(hStdOut, L"\n", 1, &dwBytesWritten, NULL);
*/
//Check if this entry is a directory
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
// Make sure this dir is not . or ..
if (wstring(data.cFileName) != L"." &&
wstring(data.cFileName) != L"..")
{
ss << wstring(data.cFileName) << L"\\" << L"\n";
}
}
else{
ss << wstring(data.cFileName) << L"\n";
}
bContinue = FindNextFileW(hFind, &data);
}
FindClose(hFind); // Free the dir structure
wstring cstr = ss.str();
int len = cstr.size();
//WriteConsole(hStdOut, cstr.c_str(), len, &dwBytesWritten, NULL);
//WriteConsole(hStdOut, L"\n", 1, &dwBytesWritten, NULL);
jchar* raw = new jchar[len];
memcpy(raw, cstr.c_str(), len*sizeof(wchar_t));
jstring result = env->NewString(raw, len);
delete[] raw;
return result;
}
catch(...){
FindClose(hFind);
jclass exceptionClass = env->FindClass("java/lang/Exception");
env->ThrowNew(exceptionClass, "Exception occured.");
}
return NULL;
}
/*
//Code used to return a list of drives. Does not include disconnected network drives...
DWORD dwLogicalDrives = GetLogicalDrives();
UINT nDrive = 0;
for ( nDrive = 0; nDrive<32; nDrive++ ){
if ( dwLogicalDrives & (1 << nDrive) ){
wchar_t arr[] = { nDrive+'A', ':', '\\' };
wstring drive;
drive.assign(arr, arr + 3);
UINT uType = GetDriveTypeW(drive.c_str());
wstring type = L"";
switch(uType){
case DRIVE_REMOVABLE: type = L"FLOPPY"; break;
case DRIVE_FIXED: type = L"FIXED"; break;
case DRIVE_REMOTE: type = L"NETWORK"; break;
case DRIVE_CDROM: type = L"CDROM"; break;
case DRIVE_RAMDISK: type = L"RAMDISK"; break;
}
ss << drive << L"\t" << type << L"\n";
}
}
*/