author | tbell |
Fri, 04 Jul 2008 14:41:34 -0700 | |
changeset 798 | 75b3a644ef91 |
parent 785 | 36c29b2692f1 |
parent 715 | f16baef3a20e |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2001-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
#include "jni.h" |
|
27 |
#include "jni_util.h" |
|
28 |
#include "jvm.h" |
|
29 |
#include "io_util.h" |
|
30 |
#include "io_util_md.h" |
|
31 |
#include <stdio.h> |
|
32 |
||
33 |
#include <wchar.h> |
|
34 |
#include <io.h> |
|
35 |
#include <fcntl.h> |
|
36 |
#include <errno.h> |
|
37 |
#include <string.h> |
|
38 |
#include <sys/types.h> |
|
39 |
#include <sys/stat.h> |
|
40 |
#include <limits.h> |
|
41 |
#include <wincon.h> |
|
42 |
||
43 |
extern jboolean onNT = JNI_FALSE; |
|
44 |
||
47
c8f0e41aea68
6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
45
diff
changeset
|
45 |
static DWORD MAX_INPUT_EVENTS = 2000; |
2 | 46 |
|
47 |
void |
|
48 |
initializeWindowsVersion() { |
|
49 |
OSVERSIONINFO ver; |
|
50 |
ver.dwOSVersionInfoSize = sizeof(ver); |
|
51 |
GetVersionEx(&ver); |
|
52 |
if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) { |
|
53 |
onNT = JNI_TRUE; |
|
54 |
} else { |
|
55 |
onNT = JNI_FALSE; |
|
56 |
} |
|
57 |
} |
|
58 |
||
59 |
/* If this returns NULL then an exception is pending */ |
|
60 |
WCHAR* |
|
61 |
fileToNTPath(JNIEnv *env, jobject file, jfieldID id) { |
|
62 |
jstring path = NULL; |
|
63 |
if (file != NULL) { |
|
64 |
path = (*env)->GetObjectField(env, file, id); |
|
65 |
} |
|
66 |
return pathToNTPath(env, path, JNI_FALSE); |
|
67 |
} |
|
68 |
||
69 |
/* We cache the length of current working dir here to avoid |
|
70 |
calling _wgetcwd() every time we need to resolve a relative |
|
71 |
path. This piece of code needs to be revisited if chdir |
|
72 |
makes its way into java runtime. |
|
73 |
*/ |
|
74 |
||
75 |
int |
|
76 |
currentDirLength(const WCHAR* ps, int pathlen) { |
|
77 |
WCHAR *dir; |
|
78 |
if (pathlen > 2 && ps[1] == L':' && ps[2] != L'\\') { |
|
79 |
//drive-relative |
|
80 |
WCHAR d = ps[0]; |
|
81 |
int dirlen = 0; |
|
82 |
int di = 0; |
|
83 |
if ((d >= L'a') && (d <= L'z')) di = d - L'a' + 1; |
|
84 |
else if ((d >= L'A') && (d <= L'Z')) di = d - L'A' + 1; |
|
85 |
else return 0; /* invalid drive name. */ |
|
86 |
dir = _wgetdcwd(di, NULL, MAX_PATH); |
|
87 |
if (dir != NULL){ |
|
88 |
dirlen = wcslen(dir); |
|
89 |
free(dir); |
|
90 |
} |
|
91 |
return dirlen; |
|
92 |
} else { |
|
93 |
static int curDirLenCached = -1; |
|
94 |
//relative to both drive and directory |
|
95 |
if (curDirLenCached == -1) { |
|
96 |
int dirlen = -1; |
|
97 |
dir = _wgetcwd(NULL, MAX_PATH); |
|
98 |
if (dir != NULL) { |
|
99 |
curDirLenCached = wcslen(dir); |
|
100 |
free(dir); |
|
101 |
} |
|
102 |
} |
|
103 |
return curDirLenCached; |
|
104 |
} |
|
105 |
} |
|
106 |
||
785
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
107 |
/* |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
108 |
The "abpathlen" is the size of the buffer needed by _wfullpath. If the |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
109 |
"path" is a relative path, it is "the length of the current dir" + "the |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
110 |
length of the path", if it's "absolute" already, it's the same as |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
111 |
pathlen which is the length of "path". |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
112 |
*/ |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
113 |
WCHAR* prefixAbpath(const WCHAR* path, int pathlen, int abpathlen) { |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
114 |
WCHAR* pathbuf = NULL; |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
115 |
WCHAR* abpath = NULL; |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
116 |
|
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
117 |
abpathlen += 10; //padding |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
118 |
abpath = (WCHAR*)malloc(abpathlen * sizeof(WCHAR)); |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
119 |
if (abpath) { |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
120 |
/* Collapse instances of "foo\.." and ensure absoluteness before |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
121 |
going down to prefixing. |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
122 |
*/ |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
123 |
if (_wfullpath(abpath, path, abpathlen)) { |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
124 |
pathbuf = getPrefixed(abpath, abpathlen); |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
125 |
} else { |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
126 |
/* _wfullpath fails if the pathlength exceeds 32k wchar. |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
127 |
Instead of doing more fancy things we simply copy the |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
128 |
ps into the return buffer, the subsequent win32 API will |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
129 |
probably fail with FileNotFoundException, which is expected |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
130 |
*/ |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
131 |
pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR)); |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
132 |
if (pathbuf != 0) { |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
133 |
wcscpy(pathbuf, path); |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
134 |
} |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
135 |
} |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
136 |
free(abpath); |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
137 |
} |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
138 |
return pathbuf; |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
139 |
} |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
140 |
|
2 | 141 |
/* If this returns NULL then an exception is pending */ |
142 |
WCHAR* |
|
143 |
pathToNTPath(JNIEnv *env, jstring path, jboolean throwFNFE) { |
|
144 |
int pathlen = 0; |
|
145 |
WCHAR *pathbuf = NULL; |
|
785
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
146 |
int max_path = 248; /* CreateDirectoryW() has the limit of 248 */ |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
147 |
|
2 | 148 |
WITH_UNICODE_STRING(env, path, ps) { |
149 |
pathlen = wcslen(ps); |
|
150 |
if (pathlen != 0) { |
|
151 |
if (pathlen > 2 && |
|
152 |
(ps[0] == L'\\' && ps[1] == L'\\' || //UNC |
|
785
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
153 |
ps[1] == L':' && ps[2] == L'\\')) //absolute |
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
154 |
{ |
2 | 155 |
if (pathlen > max_path - 1) { |
785
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
156 |
pathbuf = prefixAbpath(ps, pathlen, pathlen); |
2 | 157 |
} else { |
158 |
pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR)); |
|
159 |
if (pathbuf != 0) { |
|
160 |
wcscpy(pathbuf, ps); |
|
161 |
} |
|
162 |
} |
|
163 |
} else { |
|
164 |
/* If the path came in as a relative path, need to verify if |
|
165 |
its absolute form is bigger than max_path or not, if yes |
|
166 |
need to (1)convert it to absolute and (2)prefix. This is |
|
167 |
obviously a burden to all relative paths (The current dir/len |
|
785
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
168 |
for "drive & directory" relative path is cached, so we only |
2 | 169 |
calculate it once but for "drive-relative path we call |
170 |
_wgetdcwd() and wcslen() everytime), but a hit we have |
|
171 |
to take if we want to support relative path beyond max_path. |
|
172 |
There is no way to predict how long the absolute path will be |
|
173 |
(therefor allocate the sufficient memory block) before calling |
|
174 |
_wfullpath(), we have to get the length of "current" dir first. |
|
175 |
*/ |
|
176 |
WCHAR *abpath = NULL; |
|
177 |
int dirlen = currentDirLength(ps, pathlen); |
|
178 |
if (dirlen + pathlen + 1 > max_path - 1) { |
|
785
36c29b2692f1
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents:
47
diff
changeset
|
179 |
pathbuf = prefixAbpath(ps, pathlen, dirlen + pathlen); |
2 | 180 |
} else { |
181 |
pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR)); |
|
182 |
if (pathbuf != 0) { |
|
183 |
wcscpy(pathbuf, ps); |
|
184 |
} |
|
185 |
} |
|
186 |
} |
|
187 |
} |
|
188 |
} END_UNICODE_STRING(env, ps); |
|
189 |
||
190 |
if (pathlen == 0) { |
|
191 |
if (throwFNFE == JNI_TRUE) { |
|
192 |
throwFileNotFoundException(env, path); |
|
193 |
return NULL; |
|
194 |
} else { |
|
195 |
pathbuf = (WCHAR*)malloc(sizeof(WCHAR)); |
|
196 |
pathbuf[0] = L'\0'; |
|
197 |
} |
|
198 |
} |
|
199 |
if (pathbuf == 0) { |
|
200 |
JNU_ThrowOutOfMemoryError(env, 0); |
|
201 |
return NULL; |
|
202 |
} |
|
203 |
return pathbuf; |
|
204 |
} |
|
205 |
||
206 |
jlong |
|
207 |
winFileHandleOpen(JNIEnv *env, jstring path, int flags) |
|
208 |
{ |
|
47
c8f0e41aea68
6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
45
diff
changeset
|
209 |
/* To implement O_APPEND, we use the strategy from |
c8f0e41aea68
6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
45
diff
changeset
|
210 |
http://msdn2.microsoft.com/en-us/library/aa363858.aspx |
c8f0e41aea68
6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
45
diff
changeset
|
211 |
"You can get atomic append by opening a file with |
c8f0e41aea68
6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
45
diff
changeset
|
212 |
FILE_APPEND_DATA access and _without_ FILE_WRITE_DATA access. |
c8f0e41aea68
6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
45
diff
changeset
|
213 |
If you do this then all writes will ignore the current file |
c8f0e41aea68
6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
45
diff
changeset
|
214 |
pointer and be done at the end-of file." */ |
2 | 215 |
const DWORD access = |
47
c8f0e41aea68
6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
45
diff
changeset
|
216 |
(flags & O_APPEND) ? (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA) : |
2 | 217 |
(flags & O_WRONLY) ? GENERIC_WRITE : |
47
c8f0e41aea68
6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
45
diff
changeset
|
218 |
(flags & O_RDWR) ? (GENERIC_READ | GENERIC_WRITE) : |
2 | 219 |
GENERIC_READ; |
220 |
const DWORD sharing = |
|
221 |
FILE_SHARE_READ | FILE_SHARE_WRITE; |
|
222 |
const DWORD disposition = |
|
223 |
/* Note: O_TRUNC overrides O_CREAT */ |
|
224 |
(flags & O_TRUNC) ? CREATE_ALWAYS : |
|
225 |
(flags & O_CREAT) ? OPEN_ALWAYS : |
|
226 |
OPEN_EXISTING; |
|
227 |
const DWORD maybeWriteThrough = |
|
228 |
(flags & (O_SYNC | O_DSYNC)) ? |
|
229 |
FILE_FLAG_WRITE_THROUGH : |
|
230 |
FILE_ATTRIBUTE_NORMAL; |
|
231 |
const DWORD maybeDeleteOnClose = |
|
232 |
(flags & O_TEMPORARY) ? |
|
233 |
FILE_FLAG_DELETE_ON_CLOSE : |
|
234 |
FILE_ATTRIBUTE_NORMAL; |
|
235 |
const DWORD flagsAndAttributes = maybeWriteThrough | maybeDeleteOnClose; |
|
236 |
HANDLE h = NULL; |
|
237 |
||
238 |
if (onNT) { |
|
239 |
WCHAR *pathbuf = pathToNTPath(env, path, JNI_TRUE); |
|
240 |
if (pathbuf == NULL) { |
|
241 |
/* Exception already pending */ |
|
242 |
return -1; |
|
243 |
} |
|
244 |
h = CreateFileW( |
|
245 |
pathbuf, /* Wide char path name */ |
|
246 |
access, /* Read and/or write permission */ |
|
247 |
sharing, /* File sharing flags */ |
|
248 |
NULL, /* Security attributes */ |
|
249 |
disposition, /* creation disposition */ |
|
250 |
flagsAndAttributes, /* flags and attributes */ |
|
251 |
NULL); |
|
252 |
free(pathbuf); |
|
253 |
} else { |
|
254 |
WITH_PLATFORM_STRING(env, path, _ps) { |
|
255 |
h = CreateFile(_ps, access, sharing, NULL, disposition, |
|
256 |
flagsAndAttributes, NULL); |
|
257 |
} END_PLATFORM_STRING(env, _ps); |
|
258 |
} |
|
259 |
if (h == INVALID_HANDLE_VALUE) { |
|
260 |
int error = GetLastError(); |
|
261 |
if (error == ERROR_TOO_MANY_OPEN_FILES) { |
|
262 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "IOException", |
|
263 |
"Too many open files"); |
|
264 |
return -1; |
|
265 |
} |
|
266 |
throwFileNotFoundException(env, path); |
|
267 |
return -1; |
|
268 |
} |
|
269 |
return (jlong) h; |
|
270 |
} |
|
271 |
||
272 |
void |
|
273 |
fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags) |
|
274 |
{ |
|
275 |
jlong h = winFileHandleOpen(env, path, flags); |
|
276 |
if (h >= 0) { |
|
277 |
SET_FD(this, h, fid); |
|
278 |
} |
|
279 |
} |
|
280 |
||
281 |
/* These are functions that use a handle fd instead of the |
|
282 |
old C style int fd as is used in HPI layer */ |
|
283 |
||
284 |
static int |
|
285 |
handleNonSeekAvailable(jlong, long *); |
|
286 |
static int |
|
287 |
handleStdinAvailable(jlong, long *); |
|
288 |
||
289 |
int |
|
290 |
handleAvailable(jlong fd, jlong *pbytes) { |
|
291 |
jlong current, end; |
|
292 |
HANDLE h = (HANDLE)fd; |
|
293 |
DWORD type = 0; |
|
294 |
||
295 |
type = GetFileType(h); |
|
296 |
/* Handle is for keyboard or pipe */ |
|
297 |
if (type == FILE_TYPE_CHAR || type == FILE_TYPE_PIPE) { |
|
298 |
int ret; |
|
299 |
long lpbytes; |
|
300 |
HANDLE stdInHandle = GetStdHandle(STD_INPUT_HANDLE); |
|
301 |
if (stdInHandle == h) { |
|
302 |
ret = handleStdinAvailable(fd, &lpbytes); /* keyboard */ |
|
303 |
} else { |
|
304 |
ret = handleNonSeekAvailable(fd, &lpbytes); /* pipe */ |
|
305 |
} |
|
306 |
(*pbytes) = (jlong)(lpbytes); |
|
307 |
return ret; |
|
308 |
} |
|
309 |
/* Handle is for regular file */ |
|
310 |
if (type == FILE_TYPE_DISK) { |
|
311 |
long highPos = 0; |
|
312 |
DWORD sizeLow = 0; |
|
313 |
DWORD sizeHigh = 0; |
|
314 |
DWORD lowPos = SetFilePointer(h, 0, &highPos, FILE_CURRENT); |
|
315 |
if (lowPos == ((DWORD)-1)) { |
|
316 |
return FALSE; |
|
317 |
} |
|
318 |
current = (((jlong)highPos) << 32) | lowPos; |
|
319 |
end = GetFileSize(h, &sizeHigh); |
|
320 |
if (sizeLow == ((DWORD)-1)) { |
|
321 |
return FALSE; |
|
322 |
} |
|
323 |
*pbytes = end - current; |
|
324 |
return TRUE; |
|
325 |
} |
|
326 |
return FALSE; |
|
327 |
} |
|
328 |
||
329 |
static int |
|
330 |
handleNonSeekAvailable(jlong fd, long *pbytes) { |
|
331 |
/* This is used for available on non-seekable devices |
|
332 |
* (like both named and anonymous pipes, such as pipes |
|
333 |
* connected to an exec'd process). |
|
334 |
* Standard Input is a special case. |
|
335 |
* |
|
336 |
*/ |
|
337 |
HANDLE han; |
|
338 |
||
339 |
if ((han = (HANDLE) fd) == INVALID_HANDLE_VALUE) { |
|
340 |
return FALSE; |
|
341 |
} |
|
342 |
||
343 |
if (! PeekNamedPipe(han, NULL, 0, NULL, pbytes, NULL)) { |
|
344 |
/* PeekNamedPipe fails when at EOF. In that case we |
|
345 |
* simply make *pbytes = 0 which is consistent with the |
|
346 |
* behavior we get on Solaris when an fd is at EOF. |
|
347 |
* The only alternative is to raise and Exception, |
|
348 |
* which isn't really warranted. |
|
349 |
*/ |
|
350 |
if (GetLastError() != ERROR_BROKEN_PIPE) { |
|
351 |
return FALSE; |
|
352 |
} |
|
353 |
*pbytes = 0; |
|
354 |
} |
|
355 |
return TRUE; |
|
356 |
} |
|
357 |
||
358 |
static int |
|
359 |
handleStdinAvailable(jlong fd, long *pbytes) { |
|
360 |
HANDLE han; |
|
361 |
DWORD numEventsRead = 0; /* Number of events read from buffer */ |
|
362 |
DWORD numEvents = 0; /* Number of events in buffer */ |
|
363 |
DWORD i = 0; /* Loop index */ |
|
364 |
DWORD curLength = 0; /* Position marker */ |
|
365 |
DWORD actualLength = 0; /* Number of bytes readable */ |
|
366 |
BOOL error = FALSE; /* Error holder */ |
|
367 |
INPUT_RECORD *lpBuffer; /* Pointer to records of input events */ |
|
368 |
DWORD bufferSize = 0; |
|
369 |
||
370 |
if ((han = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) { |
|
371 |
return FALSE; |
|
372 |
} |
|
373 |
||
374 |
/* Construct an array of input records in the console buffer */ |
|
375 |
error = GetNumberOfConsoleInputEvents(han, &numEvents); |
|
376 |
if (error == 0) { |
|
377 |
return handleNonSeekAvailable(fd, pbytes); |
|
378 |
} |
|
379 |
||
380 |
/* lpBuffer must fit into 64K or else PeekConsoleInput fails */ |
|
381 |
if (numEvents > MAX_INPUT_EVENTS) { |
|
382 |
numEvents = MAX_INPUT_EVENTS; |
|
383 |
} |
|
384 |
||
385 |
bufferSize = numEvents * sizeof(INPUT_RECORD); |
|
386 |
if (bufferSize == 0) |
|
387 |
bufferSize = 1; |
|
388 |
lpBuffer = malloc(bufferSize); |
|
389 |
if (lpBuffer == NULL) { |
|
390 |
return FALSE; |
|
391 |
} |
|
392 |
||
393 |
error = PeekConsoleInput(han, lpBuffer, numEvents, &numEventsRead); |
|
394 |
if (error == 0) { |
|
395 |
free(lpBuffer); |
|
396 |
return FALSE; |
|
397 |
} |
|
398 |
||
399 |
/* Examine input records for the number of bytes available */ |
|
400 |
for(i=0; i<numEvents; i++) { |
|
401 |
if (lpBuffer[i].EventType == KEY_EVENT) { |
|
402 |
KEY_EVENT_RECORD *keyRecord = (KEY_EVENT_RECORD *) |
|
403 |
&(lpBuffer[i].Event); |
|
404 |
if (keyRecord->bKeyDown == TRUE) { |
|
405 |
CHAR *keyPressed = (CHAR *) &(keyRecord->uChar); |
|
406 |
curLength++; |
|
407 |
if (*keyPressed == '\r') |
|
408 |
actualLength = curLength; |
|
409 |
} |
|
410 |
} |
|
411 |
} |
|
412 |
if(lpBuffer != NULL) |
|
413 |
free(lpBuffer); |
|
414 |
*pbytes = (long) actualLength; |
|
415 |
return TRUE; |
|
416 |
} |
|
417 |
||
418 |
/* |
|
419 |
* This is documented to succeed on read-only files, but Win32's |
|
420 |
* FlushFileBuffers functions fails with "access denied" in such a |
|
421 |
* case. So we only signal an error if the error is *not* "access |
|
422 |
* denied". |
|
423 |
*/ |
|
424 |
||
425 |
JNIEXPORT int |
|
426 |
handleSync(jlong fd) { |
|
427 |
/* |
|
428 |
* From the documentation: |
|
429 |
* |
|
430 |
* On Windows NT, the function FlushFileBuffers fails if hFile |
|
431 |
* is a handle to console output. That is because console |
|
432 |
* output is not buffered. The function returns FALSE, and |
|
433 |
* GetLastError returns ERROR_INVALID_HANDLE. |
|
434 |
* |
|
435 |
* On the other hand, on Win95, it returns without error. I cannot |
|
436 |
* assume that 0, 1, and 2 are console, because if someone closes |
|
437 |
* System.out and then opens a file, they might get file descriptor |
|
438 |
* 1. An error on *that* version of 1 should be reported, whereas |
|
439 |
* an error on System.out (which was the original 1) should be |
|
440 |
* ignored. So I use isatty() to ensure that such an error was due |
|
441 |
* to this bogosity, and if it was, I ignore the error. |
|
442 |
*/ |
|
443 |
||
444 |
HANDLE handle = (HANDLE)fd; |
|
445 |
||
446 |
if (!FlushFileBuffers(handle)) { |
|
447 |
if (GetLastError() != ERROR_ACCESS_DENIED) { /* from winerror.h */ |
|
448 |
return -1; |
|
449 |
} |
|
450 |
} |
|
451 |
return 0; |
|
452 |
} |
|
453 |
||
454 |
||
455 |
int |
|
456 |
handleSetLength(jlong fd, jlong length) { |
|
457 |
HANDLE h = (HANDLE)fd; |
|
458 |
long high = (long)(length >> 32); |
|
459 |
DWORD ret; |
|
460 |
||
461 |
if (h == (HANDLE)(-1)) return -1; |
|
462 |
ret = SetFilePointer(h, (long)(length), &high, FILE_BEGIN); |
|
463 |
if (ret == 0xFFFFFFFF && GetLastError() != NO_ERROR) { |
|
464 |
return -1; |
|
465 |
} |
|
466 |
if (SetEndOfFile(h) == FALSE) return -1; |
|
467 |
return 0; |
|
468 |
} |
|
469 |
||
470 |
JNIEXPORT |
|
471 |
size_t |
|
472 |
handleRead(jlong fd, void *buf, jint len) |
|
473 |
{ |
|
474 |
DWORD read = 0; |
|
475 |
BOOL result = 0; |
|
476 |
HANDLE h = (HANDLE)fd; |
|
477 |
if (h == INVALID_HANDLE_VALUE) { |
|
478 |
return -1; |
|
479 |
} |
|
480 |
result = ReadFile(h, /* File handle to read */ |
|
481 |
buf, /* address to put data */ |
|
482 |
len, /* number of bytes to read */ |
|
483 |
&read, /* number of bytes read */ |
|
484 |
NULL); /* no overlapped struct */ |
|
485 |
if (result == 0) { |
|
486 |
int error = GetLastError(); |
|
487 |
if (error == ERROR_BROKEN_PIPE) { |
|
488 |
return 0; /* EOF */ |
|
489 |
} |
|
490 |
return -1; |
|
491 |
} |
|
492 |
return read; |
|
493 |
} |
|
494 |
||
495 |
JNIEXPORT |
|
496 |
size_t |
|
497 |
handleWrite(jlong fd, const void *buf, jint len) |
|
498 |
{ |
|
499 |
BOOL result = 0; |
|
500 |
DWORD written = 0; |
|
501 |
HANDLE h = (HANDLE)fd; |
|
502 |
if (h != INVALID_HANDLE_VALUE) { |
|
503 |
result = WriteFile(h, /* File handle to write */ |
|
504 |
buf, /* pointers to the buffers */ |
|
505 |
len, /* number of bytes to write */ |
|
506 |
&written, /* receives number of bytes written */ |
|
507 |
NULL); /* no overlapped struct */ |
|
508 |
} |
|
509 |
if ((h == INVALID_HANDLE_VALUE) || (result == 0)) { |
|
510 |
return -1; |
|
511 |
} |
|
512 |
return written; |
|
513 |
} |
|
514 |
||
515 |
jint |
|
516 |
handleClose(JNIEnv *env, jobject this, jfieldID fid) |
|
517 |
{ |
|
518 |
FD fd = GET_FD(this, fid); |
|
519 |
HANDLE h = (HANDLE)fd; |
|
520 |
||
47
c8f0e41aea68
6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
45
diff
changeset
|
521 |
if (h == INVALID_HANDLE_VALUE) { |
2 | 522 |
return 0; |
523 |
} |
|
524 |
||
525 |
/* Set the fd to -1 before closing it so that the timing window |
|
526 |
* of other threads using the wrong fd (closed but recycled fd, |
|
527 |
* that gets re-opened with some other filename) is reduced. |
|
528 |
* Practically the chance of its occurance is low, however, we are |
|
529 |
* taking extra precaution over here. |
|
530 |
*/ |
|
531 |
SET_FD(this, -1, fid); |
|
532 |
||
533 |
if (CloseHandle(h) == 0) { /* Returns zero on failure */ |
|
534 |
SET_FD(this, fd, fid); // restore fd |
|
535 |
JNU_ThrowIOExceptionWithLastError(env, "close failed"); |
|
536 |
} |
|
537 |
return 0; |
|
538 |
} |
|
539 |
||
540 |
jlong |
|
541 |
handleLseek(jlong fd, jlong offset, jint whence) |
|
542 |
{ |
|
543 |
DWORD lowPos = 0; |
|
544 |
long highPos = 0; |
|
545 |
DWORD op = FILE_CURRENT; |
|
546 |
HANDLE h = (HANDLE)fd; |
|
547 |
||
548 |
if (whence == SEEK_END) { |
|
549 |
op = FILE_END; |
|
550 |
} |
|
551 |
if (whence == SEEK_CUR) { |
|
552 |
op = FILE_CURRENT; |
|
553 |
} |
|
554 |
if (whence == SEEK_SET) { |
|
555 |
op = FILE_BEGIN; |
|
556 |
} |
|
557 |
||
558 |
lowPos = (DWORD)offset; |
|
559 |
highPos = (long)(offset >> 32); |
|
560 |
lowPos = SetFilePointer(h, lowPos, &highPos, op); |
|
561 |
if (lowPos == ((DWORD)-1)) { |
|
562 |
if (GetLastError() != ERROR_SUCCESS) { |
|
563 |
return -1; |
|
564 |
} |
|
565 |
} |
|
566 |
return (((jlong)highPos) << 32) | lowPos; |
|
567 |
} |