author | alanb |
Fri, 02 Nov 2012 15:50:11 +0000 | |
changeset 14342 | 8435a30053c1 |
parent 13360 | b62b5e86f92a |
child 16476 | e269be167fae |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13360
diff
changeset
|
2 |
* Copyright (c) 2001, 2012, Oracle and/or its affiliates. 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 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 <string.h> |
|
32 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
7668
diff
changeset
|
33 |
#ifdef MACOSX |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
7668
diff
changeset
|
34 |
|
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
7668
diff
changeset
|
35 |
#include <CoreFoundation/CoreFoundation.h> |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
7668
diff
changeset
|
36 |
|
13360
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
37 |
__private_extern__ |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
38 |
jstring newStringPlatform(JNIEnv *env, const char* str) |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
7668
diff
changeset
|
39 |
{ |
13360
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
40 |
jstring rv = NULL; |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
41 |
CFMutableStringRef csref = CFStringCreateMutable(NULL, 0); |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
42 |
if (csref == NULL) { |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
43 |
JNU_ThrowOutOfMemoryError(env, "native heap"); |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
44 |
} else { |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
45 |
CFStringAppendCString(csref, str, kCFStringEncodingUTF8); |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
46 |
CFStringNormalize(csref, kCFStringNormalizationFormC); |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
47 |
int clen = CFStringGetLength(csref); |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
48 |
int ulen = (clen + 1) * 2; // utf16 + zero padding |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
49 |
char* chars = malloc(ulen); |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
50 |
if (chars == NULL) { |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
51 |
CFRelease(csref); |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
52 |
JNU_ThrowOutOfMemoryError(env, "native heap"); |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
53 |
} else { |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
54 |
if (CFStringGetCString(csref, chars, ulen, kCFStringEncodingUTF16)) { |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
55 |
rv = (*env)->NewString(env, (jchar*)chars, clen); |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
56 |
} |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
57 |
free(chars); |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
58 |
CFRelease(csref); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
7668
diff
changeset
|
59 |
} |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
7668
diff
changeset
|
60 |
} |
13360
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
61 |
return rv; |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
7668
diff
changeset
|
62 |
} |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
7668
diff
changeset
|
63 |
#endif |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
7668
diff
changeset
|
64 |
|
2 | 65 |
void |
66 |
fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags) |
|
67 |
{ |
|
68 |
WITH_PLATFORM_STRING(env, path, ps) { |
|
69 |
FD fd; |
|
70 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
7668
diff
changeset
|
71 |
#if defined(__linux__) || defined(_ALLBSD_SOURCE) |
2 | 72 |
/* Remove trailing slashes, since the kernel won't */ |
73 |
char *p = (char *)ps + strlen(ps) - 1; |
|
74 |
while ((p > ps) && (*p == '/')) |
|
75 |
*p-- = '\0'; |
|
76 |
#endif |
|
77 |
fd = JVM_Open(ps, flags, 0666); |
|
78 |
if (fd >= 0) { |
|
79 |
SET_FD(this, fd, fid); |
|
80 |
} else { |
|
81 |
throwFileNotFoundException(env, path); |
|
82 |
} |
|
83 |
} END_PLATFORM_STRING(env, ps); |
|
84 |
} |
|
85 |
||
86 |
||
87 |
void |
|
88 |
fileClose(JNIEnv *env, jobject this, jfieldID fid) |
|
89 |
{ |
|
90 |
FD fd = GET_FD(this, fid); |
|
91 |
if (fd == -1) { |
|
92 |
return; |
|
93 |
} |
|
94 |
||
95 |
/* Set the fd to -1 before closing it so that the timing window |
|
96 |
* of other threads using the wrong fd (closed but recycled fd, |
|
97 |
* that gets re-opened with some other filename) is reduced. |
|
98 |
* Practically the chance of its occurance is low, however, we are |
|
99 |
* taking extra precaution over here. |
|
100 |
*/ |
|
101 |
SET_FD(this, -1, fid); |
|
102 |
||
103 |
/* |
|
104 |
* Don't close file descriptors 0, 1, or 2. If we close these stream |
|
105 |
* then a subsequent file open or socket will use them. Instead we |
|
106 |
* just redirect these file descriptors to /dev/null. |
|
107 |
*/ |
|
108 |
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) { |
|
109 |
int devnull = open("/dev/null", O_WRONLY); |
|
110 |
if (devnull < 0) { |
|
111 |
SET_FD(this, fd, fid); // restore fd |
|
112 |
JNU_ThrowIOExceptionWithLastError(env, "open /dev/null failed"); |
|
113 |
} else { |
|
114 |
dup2(devnull, fd); |
|
115 |
close(devnull); |
|
116 |
} |
|
117 |
} else if (JVM_Close(fd) == -1) { |
|
6690
49644ab3ef0c
6988037: fileClose prints debug message is close fails
alanb
parents:
5506
diff
changeset
|
118 |
JNU_ThrowIOExceptionWithLastError(env, "close failed"); |
2 | 119 |
} |
120 |
} |