author | stuefe |
Thu, 01 Jun 2017 08:59:24 +0200 | |
changeset 45297 | df90da0c10aa |
parent 45178 | 9ff621041842 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
45178
9ff621041842
8177809: File.lastModified() is losing milliseconds (always ends in 000)
bpb
parents:
42944
diff
changeset
|
2 |
* Copyright (c) 1998, 2017, 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 |
||
42777
a94fc33e9866
8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents:
31144
diff
changeset
|
26 |
#include <unistd.h> |
2 | 27 |
#include <assert.h> |
28 |
#include <sys/types.h> |
|
29 |
#include <sys/time.h> |
|
30 |
#include <sys/stat.h> |
|
31144
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
31 |
#ifdef MACOSX |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
32 |
#include <sys/param.h> |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
33 |
#include <sys/mount.h> |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
34 |
#else |
2 | 35 |
#include <sys/statvfs.h> |
31144
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
36 |
#endif |
2 | 37 |
#include <string.h> |
38 |
#include <stdlib.h> |
|
39 |
#include <dlfcn.h> |
|
40 |
#include <limits.h> |
|
41 |
||
42 |
#include "jni.h" |
|
43 |
#include "jni_util.h" |
|
44 |
#include "jlong.h" |
|
45 |
#include "jvm.h" |
|
46 |
#include "io_util.h" |
|
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
|
47 |
#include "io_util_md.h" |
2 | 48 |
#include "java_io_FileSystem.h" |
49 |
#include "java_io_UnixFileSystem.h" |
|
50 |
||
42944 | 51 |
#if defined(_AIX) |
52 |
#if !defined(NAME_MAX) |
|
53 |
#define NAME_MAX MAXNAMLEN |
|
54 |
#endif |
|
55 |
#define DIR DIR64 |
|
56 |
#define opendir opendir64 |
|
57 |
#define closedir closedir64 |
|
58 |
#endif |
|
59 |
||
60 |
#if defined(__solaris__) && !defined(NAME_MAX) |
|
61 |
#define NAME_MAX MAXNAMLEN |
|
62 |
#endif |
|
63 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
7668
diff
changeset
|
64 |
#if defined(_ALLBSD_SOURCE) |
42944 | 65 |
#define dirent64 dirent |
66 |
#define readdir64_r readdir_r |
|
67 |
#define stat64 stat |
|
68 |
#ifndef MACOSX |
|
69 |
#define statvfs64 statvfs |
|
70 |
#endif |
|
31144
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
71 |
#endif |
2 | 72 |
|
73 |
/* -- Field IDs -- */ |
|
74 |
||
75 |
static struct { |
|
76 |
jfieldID path; |
|
77 |
} ids; |
|
78 |
||
79 |
||
80 |
JNIEXPORT void JNICALL |
|
81 |
Java_java_io_UnixFileSystem_initIDs(JNIEnv *env, jclass cls) |
|
82 |
{ |
|
83 |
jclass fileClass = (*env)->FindClass(env, "java/io/File"); |
|
84 |
if (!fileClass) return; |
|
85 |
ids.path = (*env)->GetFieldID(env, fileClass, |
|
86 |
"path", "Ljava/lang/String;"); |
|
87 |
} |
|
88 |
||
89 |
/* -- Path operations -- */ |
|
90 |
||
91 |
extern int canonicalize(char *path, const char *out, int len); |
|
92 |
||
93 |
JNIEXPORT jstring JNICALL |
|
94 |
Java_java_io_UnixFileSystem_canonicalize0(JNIEnv *env, jobject this, |
|
95 |
jstring pathname) |
|
96 |
{ |
|
97 |
jstring rv = NULL; |
|
98 |
||
99 |
WITH_PLATFORM_STRING(env, pathname, path) { |
|
100 |
char canonicalPath[JVM_MAXPATHLEN]; |
|
16476
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
14342
diff
changeset
|
101 |
if (canonicalize((char *)path, |
2 | 102 |
canonicalPath, JVM_MAXPATHLEN) < 0) { |
103 |
JNU_ThrowIOExceptionWithLastError(env, "Bad pathname"); |
|
104 |
} else { |
|
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
|
105 |
#ifdef MACOSX |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
106 |
rv = newStringPlatform(env, canonicalPath); |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
107 |
#else |
2 | 108 |
rv = JNU_NewStringPlatform(env, canonicalPath); |
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
|
109 |
#endif |
2 | 110 |
} |
111 |
} END_PLATFORM_STRING(env, path); |
|
112 |
return rv; |
|
113 |
} |
|
114 |
||
115 |
||
116 |
/* -- Attribute accessors -- */ |
|
117 |
||
118 |
||
119 |
static jboolean |
|
120 |
statMode(const char *path, int *mode) |
|
121 |
{ |
|
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
122 |
struct stat64 sb; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
123 |
if (stat64(path, &sb) == 0) { |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
124 |
*mode = sb.st_mode; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
125 |
return JNI_TRUE; |
2 | 126 |
} |
127 |
return JNI_FALSE; |
|
128 |
} |
|
129 |
||
130 |
||
131 |
JNIEXPORT jint JNICALL |
|
132 |
Java_java_io_UnixFileSystem_getBooleanAttributes0(JNIEnv *env, jobject this, |
|
133 |
jobject file) |
|
134 |
{ |
|
135 |
jint rv = 0; |
|
136 |
||
137 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
138 |
int mode; |
|
139 |
if (statMode(path, &mode)) { |
|
140 |
int fmt = mode & S_IFMT; |
|
141 |
rv = (jint) (java_io_FileSystem_BA_EXISTS |
|
142 |
| ((fmt == S_IFREG) ? java_io_FileSystem_BA_REGULAR : 0) |
|
143 |
| ((fmt == S_IFDIR) ? java_io_FileSystem_BA_DIRECTORY : 0)); |
|
144 |
} |
|
145 |
} END_PLATFORM_STRING(env, path); |
|
146 |
return rv; |
|
147 |
} |
|
148 |
||
149 |
JNIEXPORT jboolean JNICALL |
|
150 |
Java_java_io_UnixFileSystem_checkAccess(JNIEnv *env, jobject this, |
|
151 |
jobject file, jint a) |
|
152 |
{ |
|
153 |
jboolean rv = JNI_FALSE; |
|
6850
56966b0a6a0d
6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents:
5506
diff
changeset
|
154 |
int mode = 0; |
2 | 155 |
switch (a) { |
156 |
case java_io_FileSystem_ACCESS_READ: |
|
157 |
mode = R_OK; |
|
158 |
break; |
|
159 |
case java_io_FileSystem_ACCESS_WRITE: |
|
160 |
mode = W_OK; |
|
161 |
break; |
|
162 |
case java_io_FileSystem_ACCESS_EXECUTE: |
|
163 |
mode = X_OK; |
|
164 |
break; |
|
165 |
default: assert(0); |
|
166 |
} |
|
167 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
168 |
if (access(path, mode) == 0) { |
|
169 |
rv = JNI_TRUE; |
|
170 |
} |
|
171 |
} END_PLATFORM_STRING(env, path); |
|
172 |
return rv; |
|
173 |
} |
|
174 |
||
175 |
||
176 |
JNIEXPORT jboolean JNICALL |
|
177 |
Java_java_io_UnixFileSystem_setPermission(JNIEnv *env, jobject this, |
|
178 |
jobject file, |
|
179 |
jint access, |
|
180 |
jboolean enable, |
|
181 |
jboolean owneronly) |
|
182 |
{ |
|
183 |
jboolean rv = JNI_FALSE; |
|
184 |
||
185 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
6850
56966b0a6a0d
6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents:
5506
diff
changeset
|
186 |
int amode = 0; |
56966b0a6a0d
6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents:
5506
diff
changeset
|
187 |
int mode; |
2 | 188 |
switch (access) { |
189 |
case java_io_FileSystem_ACCESS_READ: |
|
190 |
if (owneronly) |
|
191 |
amode = S_IRUSR; |
|
192 |
else |
|
193 |
amode = S_IRUSR | S_IRGRP | S_IROTH; |
|
194 |
break; |
|
195 |
case java_io_FileSystem_ACCESS_WRITE: |
|
196 |
if (owneronly) |
|
197 |
amode = S_IWUSR; |
|
198 |
else |
|
199 |
amode = S_IWUSR | S_IWGRP | S_IWOTH; |
|
200 |
break; |
|
201 |
case java_io_FileSystem_ACCESS_EXECUTE: |
|
202 |
if (owneronly) |
|
203 |
amode = S_IXUSR; |
|
204 |
else |
|
205 |
amode = S_IXUSR | S_IXGRP | S_IXOTH; |
|
206 |
break; |
|
207 |
default: |
|
208 |
assert(0); |
|
209 |
} |
|
210 |
if (statMode(path, &mode)) { |
|
211 |
if (enable) |
|
212 |
mode |= amode; |
|
213 |
else |
|
214 |
mode &= ~amode; |
|
215 |
if (chmod(path, mode) >= 0) { |
|
216 |
rv = JNI_TRUE; |
|
217 |
} |
|
218 |
} |
|
219 |
} END_PLATFORM_STRING(env, path); |
|
220 |
return rv; |
|
221 |
} |
|
222 |
||
223 |
JNIEXPORT jlong JNICALL |
|
224 |
Java_java_io_UnixFileSystem_getLastModifiedTime(JNIEnv *env, jobject this, |
|
225 |
jobject file) |
|
226 |
{ |
|
227 |
jlong rv = 0; |
|
228 |
||
229 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
230 |
struct stat64 sb; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
231 |
if (stat64(path, &sb) == 0) { |
45297 | 232 |
#if defined(_AIX) |
233 |
rv = (jlong)sb.st_mtime * 1000; |
|
234 |
rv += (jlong)sb.st_mtime_n / 1000000; |
|
235 |
#elif defined(MACOSX) |
|
236 |
rv = (jlong)sb.st_mtimespec.tv_sec * 1000; |
|
237 |
rv += (jlong)sb.st_mtimespec.tv_nsec / 1000000; |
|
238 |
#else |
|
45178
9ff621041842
8177809: File.lastModified() is losing milliseconds (always ends in 000)
bpb
parents:
42944
diff
changeset
|
239 |
rv = (jlong)sb.st_mtim.tv_sec * 1000; |
9ff621041842
8177809: File.lastModified() is losing milliseconds (always ends in 000)
bpb
parents:
42944
diff
changeset
|
240 |
rv += (jlong)sb.st_mtim.tv_nsec / 1000000; |
9ff621041842
8177809: File.lastModified() is losing milliseconds (always ends in 000)
bpb
parents:
42944
diff
changeset
|
241 |
#endif |
2 | 242 |
} |
243 |
} END_PLATFORM_STRING(env, path); |
|
244 |
return rv; |
|
245 |
} |
|
246 |
||
247 |
||
248 |
JNIEXPORT jlong JNICALL |
|
249 |
Java_java_io_UnixFileSystem_getLength(JNIEnv *env, jobject this, |
|
250 |
jobject file) |
|
251 |
{ |
|
252 |
jlong rv = 0; |
|
253 |
||
254 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
255 |
struct stat64 sb; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
256 |
if (stat64(path, &sb) == 0) { |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
257 |
rv = sb.st_size; |
2 | 258 |
} |
259 |
} END_PLATFORM_STRING(env, path); |
|
260 |
return rv; |
|
261 |
} |
|
262 |
||
263 |
||
264 |
/* -- File operations -- */ |
|
265 |
||
266 |
||
267 |
JNIEXPORT jboolean JNICALL |
|
268 |
Java_java_io_UnixFileSystem_createFileExclusively(JNIEnv *env, jclass cls, |
|
269 |
jstring pathname) |
|
270 |
{ |
|
271 |
jboolean rv = JNI_FALSE; |
|
272 |
||
273 |
WITH_PLATFORM_STRING(env, pathname, path) { |
|
16476
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
14342
diff
changeset
|
274 |
FD fd; |
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
14342
diff
changeset
|
275 |
/* The root directory always exists */ |
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
14342
diff
changeset
|
276 |
if (strcmp (path, "/")) { |
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
14342
diff
changeset
|
277 |
fd = handleOpen(path, O_RDWR | O_CREAT | O_EXCL, 0666); |
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
14342
diff
changeset
|
278 |
if (fd < 0) { |
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
14342
diff
changeset
|
279 |
if (errno != EEXIST) |
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
14342
diff
changeset
|
280 |
JNU_ThrowIOExceptionWithLastError(env, path); |
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
14342
diff
changeset
|
281 |
} else { |
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
14342
diff
changeset
|
282 |
if (close(fd) == -1) |
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
14342
diff
changeset
|
283 |
JNU_ThrowIOExceptionWithLastError(env, path); |
e269be167fae
8001334: Remove use of JVM_* functions from java.io code
dxu
parents:
14342
diff
changeset
|
284 |
rv = JNI_TRUE; |
2 | 285 |
} |
286 |
} |
|
287 |
} END_PLATFORM_STRING(env, path); |
|
288 |
return rv; |
|
289 |
} |
|
290 |
||
291 |
||
292 |
JNIEXPORT jboolean JNICALL |
|
293 |
Java_java_io_UnixFileSystem_delete0(JNIEnv *env, jobject this, |
|
294 |
jobject file) |
|
295 |
{ |
|
296 |
jboolean rv = JNI_FALSE; |
|
297 |
||
298 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
299 |
if (remove(path) == 0) { |
|
300 |
rv = JNI_TRUE; |
|
301 |
} |
|
302 |
} END_PLATFORM_STRING(env, path); |
|
303 |
return rv; |
|
304 |
} |
|
305 |
||
306 |
||
307 |
JNIEXPORT jobjectArray JNICALL |
|
308 |
Java_java_io_UnixFileSystem_list(JNIEnv *env, jobject this, |
|
309 |
jobject file) |
|
310 |
{ |
|
311 |
DIR *dir = NULL; |
|
312 |
struct dirent64 *ptr; |
|
313 |
struct dirent64 *result; |
|
314 |
int len, maxlen; |
|
315 |
jobjectArray rv, old; |
|
22631
ac85b05a53f4
8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents:
16476
diff
changeset
|
316 |
jclass str_class; |
ac85b05a53f4
8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents:
16476
diff
changeset
|
317 |
|
ac85b05a53f4
8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents:
16476
diff
changeset
|
318 |
str_class = JNU_ClassString(env); |
ac85b05a53f4
8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents:
16476
diff
changeset
|
319 |
CHECK_NULL_RETURN(str_class, NULL); |
2 | 320 |
|
321 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
322 |
dir = opendir(path); |
|
323 |
} END_PLATFORM_STRING(env, path); |
|
324 |
if (dir == NULL) return NULL; |
|
325 |
||
326 |
ptr = malloc(sizeof(struct dirent64) + (PATH_MAX + 1)); |
|
327 |
if (ptr == NULL) { |
|
328 |
JNU_ThrowOutOfMemoryError(env, "heap allocation failed"); |
|
329 |
closedir(dir); |
|
330 |
return NULL; |
|
331 |
} |
|
332 |
||
333 |
/* Allocate an initial String array */ |
|
334 |
len = 0; |
|
335 |
maxlen = 16; |
|
22631
ac85b05a53f4
8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents:
16476
diff
changeset
|
336 |
rv = (*env)->NewObjectArray(env, maxlen, str_class, NULL); |
2 | 337 |
if (rv == NULL) goto error; |
338 |
||
339 |
/* Scan the directory */ |
|
340 |
while ((readdir64_r(dir, ptr, &result) == 0) && (result != NULL)) { |
|
341 |
jstring name; |
|
342 |
if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, "..")) |
|
343 |
continue; |
|
344 |
if (len == maxlen) { |
|
345 |
old = rv; |
|
22631
ac85b05a53f4
8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents:
16476
diff
changeset
|
346 |
rv = (*env)->NewObjectArray(env, maxlen <<= 1, str_class, NULL); |
2 | 347 |
if (rv == NULL) goto error; |
348 |
if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error; |
|
349 |
(*env)->DeleteLocalRef(env, old); |
|
350 |
} |
|
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
|
351 |
#ifdef MACOSX |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
352 |
name = newStringPlatform(env, ptr->d_name); |
b62b5e86f92a
7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents:
12047
diff
changeset
|
353 |
#else |
2 | 354 |
name = JNU_NewStringPlatform(env, ptr->d_name); |
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
|
355 |
#endif |
2 | 356 |
if (name == NULL) goto error; |
357 |
(*env)->SetObjectArrayElement(env, rv, len++, name); |
|
358 |
(*env)->DeleteLocalRef(env, name); |
|
359 |
} |
|
360 |
closedir(dir); |
|
361 |
free(ptr); |
|
362 |
||
363 |
/* Copy the final results into an appropriately-sized array */ |
|
364 |
old = rv; |
|
22631
ac85b05a53f4
8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents:
16476
diff
changeset
|
365 |
rv = (*env)->NewObjectArray(env, len, str_class, NULL); |
2 | 366 |
if (rv == NULL) { |
367 |
return NULL; |
|
368 |
} |
|
369 |
if (JNU_CopyObjectArray(env, rv, old, len) < 0) { |
|
370 |
return NULL; |
|
371 |
} |
|
372 |
return rv; |
|
373 |
||
374 |
error: |
|
375 |
closedir(dir); |
|
376 |
free(ptr); |
|
377 |
return NULL; |
|
378 |
} |
|
379 |
||
380 |
||
381 |
JNIEXPORT jboolean JNICALL |
|
382 |
Java_java_io_UnixFileSystem_createDirectory(JNIEnv *env, jobject this, |
|
383 |
jobject file) |
|
384 |
{ |
|
385 |
jboolean rv = JNI_FALSE; |
|
386 |
||
387 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
388 |
if (mkdir(path, 0777) == 0) { |
|
389 |
rv = JNI_TRUE; |
|
390 |
} |
|
391 |
} END_PLATFORM_STRING(env, path); |
|
392 |
return rv; |
|
393 |
} |
|
394 |
||
395 |
||
396 |
JNIEXPORT jboolean JNICALL |
|
397 |
Java_java_io_UnixFileSystem_rename0(JNIEnv *env, jobject this, |
|
398 |
jobject from, jobject to) |
|
399 |
{ |
|
400 |
jboolean rv = JNI_FALSE; |
|
401 |
||
402 |
WITH_FIELD_PLATFORM_STRING(env, from, ids.path, fromPath) { |
|
403 |
WITH_FIELD_PLATFORM_STRING(env, to, ids.path, toPath) { |
|
404 |
if (rename(fromPath, toPath) == 0) { |
|
405 |
rv = JNI_TRUE; |
|
406 |
} |
|
407 |
} END_PLATFORM_STRING(env, toPath); |
|
408 |
} END_PLATFORM_STRING(env, fromPath); |
|
409 |
return rv; |
|
410 |
} |
|
411 |
||
412 |
JNIEXPORT jboolean JNICALL |
|
413 |
Java_java_io_UnixFileSystem_setLastModifiedTime(JNIEnv *env, jobject this, |
|
414 |
jobject file, jlong time) |
|
415 |
{ |
|
416 |
jboolean rv = JNI_FALSE; |
|
417 |
||
418 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
419 |
struct stat64 sb; |
2 | 420 |
|
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
421 |
if (stat64(path, &sb) == 0) { |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
422 |
struct timeval tv[2]; |
2 | 423 |
|
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
424 |
/* Preserve access time */ |
45297 | 425 |
#if defined(_AIX) |
426 |
tv[0].tv_sec = sb.st_atime; |
|
427 |
tv[0].tv_usec = sb.st_atime_n / 1000; |
|
428 |
#elif defined(MACOSX) |
|
429 |
tv[0].tv_sec = sb.st_atimespec.tv_sec; |
|
430 |
tv[0].tv_usec = sb.st_atimespec.tv_nsec / 1000; |
|
431 |
#else |
|
45178
9ff621041842
8177809: File.lastModified() is losing milliseconds (always ends in 000)
bpb
parents:
42944
diff
changeset
|
432 |
tv[0].tv_sec = sb.st_atim.tv_sec; |
9ff621041842
8177809: File.lastModified() is losing milliseconds (always ends in 000)
bpb
parents:
42944
diff
changeset
|
433 |
tv[0].tv_usec = sb.st_atim.tv_nsec / 1000; |
9ff621041842
8177809: File.lastModified() is losing milliseconds (always ends in 000)
bpb
parents:
42944
diff
changeset
|
434 |
#endif |
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
435 |
/* Change last-modified time */ |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
436 |
tv[1].tv_sec = time / 1000; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
437 |
tv[1].tv_usec = (time % 1000) * 1000; |
2 | 438 |
|
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
439 |
if (utimes(path, tv) == 0) |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
440 |
rv = JNI_TRUE; |
2 | 441 |
} |
442 |
} END_PLATFORM_STRING(env, path); |
|
443 |
||
444 |
return rv; |
|
445 |
} |
|
446 |
||
447 |
||
448 |
JNIEXPORT jboolean JNICALL |
|
449 |
Java_java_io_UnixFileSystem_setReadOnly(JNIEnv *env, jobject this, |
|
450 |
jobject file) |
|
451 |
{ |
|
452 |
jboolean rv = JNI_FALSE; |
|
453 |
||
454 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
455 |
int mode; |
|
456 |
if (statMode(path, &mode)) { |
|
457 |
if (chmod(path, mode & ~(S_IWUSR | S_IWGRP | S_IWOTH)) >= 0) { |
|
458 |
rv = JNI_TRUE; |
|
459 |
} |
|
460 |
} |
|
461 |
} END_PLATFORM_STRING(env, path); |
|
462 |
return rv; |
|
463 |
} |
|
464 |
||
465 |
JNIEXPORT jlong JNICALL |
|
466 |
Java_java_io_UnixFileSystem_getSpace(JNIEnv *env, jobject this, |
|
467 |
jobject file, jint t) |
|
468 |
{ |
|
469 |
jlong rv = 0L; |
|
470 |
||
471 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
31144
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
472 |
#ifdef MACOSX |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
473 |
struct statfs fsstat; |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
474 |
#else |
4981
3a3f498acc86
6815768: File.getxxxSpace() methods fail for very large file systems under 32bit Java
alanb
parents:
715
diff
changeset
|
475 |
struct statvfs64 fsstat; |
31144
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
476 |
#endif |
4981
3a3f498acc86
6815768: File.getxxxSpace() methods fail for very large file systems under 32bit Java
alanb
parents:
715
diff
changeset
|
477 |
memset(&fsstat, 0, sizeof(fsstat)); |
31144
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
478 |
#ifdef MACOSX |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
479 |
if (statfs(path, &fsstat) == 0) { |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
480 |
switch(t) { |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
481 |
case java_io_FileSystem_SPACE_TOTAL: |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
482 |
rv = jlong_mul(long_to_jlong(fsstat.f_bsize), |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
483 |
long_to_jlong(fsstat.f_blocks)); |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
484 |
break; |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
485 |
case java_io_FileSystem_SPACE_FREE: |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
486 |
rv = jlong_mul(long_to_jlong(fsstat.f_bsize), |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
487 |
long_to_jlong(fsstat.f_bfree)); |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
488 |
break; |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
489 |
case java_io_FileSystem_SPACE_USABLE: |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
490 |
rv = jlong_mul(long_to_jlong(fsstat.f_bsize), |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
491 |
long_to_jlong(fsstat.f_bavail)); |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
492 |
break; |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
493 |
default: |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
494 |
assert(0); |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
495 |
} |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
496 |
} |
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
497 |
#else |
4981
3a3f498acc86
6815768: File.getxxxSpace() methods fail for very large file systems under 32bit Java
alanb
parents:
715
diff
changeset
|
498 |
if (statvfs64(path, &fsstat) == 0) { |
2 | 499 |
switch(t) { |
500 |
case java_io_FileSystem_SPACE_TOTAL: |
|
501 |
rv = jlong_mul(long_to_jlong(fsstat.f_frsize), |
|
502 |
long_to_jlong(fsstat.f_blocks)); |
|
503 |
break; |
|
504 |
case java_io_FileSystem_SPACE_FREE: |
|
505 |
rv = jlong_mul(long_to_jlong(fsstat.f_frsize), |
|
506 |
long_to_jlong(fsstat.f_bfree)); |
|
507 |
break; |
|
508 |
case java_io_FileSystem_SPACE_USABLE: |
|
509 |
rv = jlong_mul(long_to_jlong(fsstat.f_frsize), |
|
510 |
long_to_jlong(fsstat.f_bavail)); |
|
511 |
break; |
|
512 |
default: |
|
513 |
assert(0); |
|
514 |
} |
|
515 |
} |
|
31144
8154c9d7bc32
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents:
25859
diff
changeset
|
516 |
#endif |
2 | 517 |
} END_PLATFORM_STRING(env, path); |
518 |
return rv; |
|
519 |
} |
|
42777
a94fc33e9866
8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents:
31144
diff
changeset
|
520 |
|
a94fc33e9866
8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents:
31144
diff
changeset
|
521 |
JNIEXPORT jlong JNICALL |
a94fc33e9866
8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents:
31144
diff
changeset
|
522 |
Java_java_io_UnixFileSystem_getNameMax0(JNIEnv *env, jobject this, |
a94fc33e9866
8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents:
31144
diff
changeset
|
523 |
jstring pathname) |
a94fc33e9866
8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents:
31144
diff
changeset
|
524 |
{ |
a94fc33e9866
8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents:
31144
diff
changeset
|
525 |
jlong length = -1; |
a94fc33e9866
8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents:
31144
diff
changeset
|
526 |
WITH_PLATFORM_STRING(env, pathname, path) { |
a94fc33e9866
8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents:
31144
diff
changeset
|
527 |
length = (jlong)pathconf(path, _PC_NAME_MAX); |
a94fc33e9866
8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents:
31144
diff
changeset
|
528 |
} END_PLATFORM_STRING(env, path); |
a94fc33e9866
8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents:
31144
diff
changeset
|
529 |
return length != -1 ? length : (jlong)NAME_MAX; |
a94fc33e9866
8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents:
31144
diff
changeset
|
530 |
} |