author | tbell |
Fri, 27 Feb 2009 10:54:11 -0800 | |
changeset 2091 | 7faffd237305 |
parent 715 | f16baef3a20e |
child 4981 | 3a3f498acc86 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 1998-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 <assert.h> |
|
27 |
#include <sys/types.h> |
|
28 |
#include <sys/time.h> |
|
29 |
#include <sys/stat.h> |
|
30 |
#include <sys/statvfs.h> |
|
31 |
#include <string.h> |
|
32 |
#include <stdlib.h> |
|
33 |
#include <dlfcn.h> |
|
34 |
#include <limits.h> |
|
35 |
||
36 |
#include "jni.h" |
|
37 |
#include "jni_util.h" |
|
38 |
#include "jlong.h" |
|
39 |
#include "jvm.h" |
|
40 |
#include "io_util.h" |
|
41 |
#include "java_io_FileSystem.h" |
|
42 |
#include "java_io_UnixFileSystem.h" |
|
43 |
||
44 |
||
45 |
/* -- Field IDs -- */ |
|
46 |
||
47 |
static struct { |
|
48 |
jfieldID path; |
|
49 |
} ids; |
|
50 |
||
51 |
||
52 |
JNIEXPORT void JNICALL |
|
53 |
Java_java_io_UnixFileSystem_initIDs(JNIEnv *env, jclass cls) |
|
54 |
{ |
|
55 |
jclass fileClass = (*env)->FindClass(env, "java/io/File"); |
|
56 |
if (!fileClass) return; |
|
57 |
ids.path = (*env)->GetFieldID(env, fileClass, |
|
58 |
"path", "Ljava/lang/String;"); |
|
59 |
} |
|
60 |
||
61 |
/* -- Path operations -- */ |
|
62 |
||
63 |
extern int canonicalize(char *path, const char *out, int len); |
|
64 |
||
65 |
JNIEXPORT jstring JNICALL |
|
66 |
Java_java_io_UnixFileSystem_canonicalize0(JNIEnv *env, jobject this, |
|
67 |
jstring pathname) |
|
68 |
{ |
|
69 |
jstring rv = NULL; |
|
70 |
||
71 |
WITH_PLATFORM_STRING(env, pathname, path) { |
|
72 |
char canonicalPath[JVM_MAXPATHLEN]; |
|
73 |
if (canonicalize(JVM_NativePath((char *)path), |
|
74 |
canonicalPath, JVM_MAXPATHLEN) < 0) { |
|
75 |
JNU_ThrowIOExceptionWithLastError(env, "Bad pathname"); |
|
76 |
} else { |
|
77 |
rv = JNU_NewStringPlatform(env, canonicalPath); |
|
78 |
} |
|
79 |
} END_PLATFORM_STRING(env, path); |
|
80 |
return rv; |
|
81 |
} |
|
82 |
||
83 |
||
84 |
/* -- Attribute accessors -- */ |
|
85 |
||
86 |
||
87 |
static jboolean |
|
88 |
statMode(const char *path, int *mode) |
|
89 |
{ |
|
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
90 |
struct stat64 sb; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
91 |
if (stat64(path, &sb) == 0) { |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
92 |
*mode = sb.st_mode; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
93 |
return JNI_TRUE; |
2 | 94 |
} |
95 |
return JNI_FALSE; |
|
96 |
} |
|
97 |
||
98 |
||
99 |
JNIEXPORT jint JNICALL |
|
100 |
Java_java_io_UnixFileSystem_getBooleanAttributes0(JNIEnv *env, jobject this, |
|
101 |
jobject file) |
|
102 |
{ |
|
103 |
jint rv = 0; |
|
104 |
||
105 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
106 |
int mode; |
|
107 |
if (statMode(path, &mode)) { |
|
108 |
int fmt = mode & S_IFMT; |
|
109 |
rv = (jint) (java_io_FileSystem_BA_EXISTS |
|
110 |
| ((fmt == S_IFREG) ? java_io_FileSystem_BA_REGULAR : 0) |
|
111 |
| ((fmt == S_IFDIR) ? java_io_FileSystem_BA_DIRECTORY : 0)); |
|
112 |
} |
|
113 |
} END_PLATFORM_STRING(env, path); |
|
114 |
return rv; |
|
115 |
} |
|
116 |
||
117 |
JNIEXPORT jboolean JNICALL |
|
118 |
Java_java_io_UnixFileSystem_checkAccess(JNIEnv *env, jobject this, |
|
119 |
jobject file, jint a) |
|
120 |
{ |
|
121 |
jboolean rv = JNI_FALSE; |
|
122 |
int mode; |
|
123 |
switch (a) { |
|
124 |
case java_io_FileSystem_ACCESS_READ: |
|
125 |
mode = R_OK; |
|
126 |
break; |
|
127 |
case java_io_FileSystem_ACCESS_WRITE: |
|
128 |
mode = W_OK; |
|
129 |
break; |
|
130 |
case java_io_FileSystem_ACCESS_EXECUTE: |
|
131 |
mode = X_OK; |
|
132 |
break; |
|
133 |
default: assert(0); |
|
134 |
} |
|
135 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
136 |
if (access(path, mode) == 0) { |
|
137 |
rv = JNI_TRUE; |
|
138 |
} |
|
139 |
} END_PLATFORM_STRING(env, path); |
|
140 |
return rv; |
|
141 |
} |
|
142 |
||
143 |
||
144 |
JNIEXPORT jboolean JNICALL |
|
145 |
Java_java_io_UnixFileSystem_setPermission(JNIEnv *env, jobject this, |
|
146 |
jobject file, |
|
147 |
jint access, |
|
148 |
jboolean enable, |
|
149 |
jboolean owneronly) |
|
150 |
{ |
|
151 |
jboolean rv = JNI_FALSE; |
|
152 |
||
153 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
154 |
int amode, mode; |
|
155 |
switch (access) { |
|
156 |
case java_io_FileSystem_ACCESS_READ: |
|
157 |
if (owneronly) |
|
158 |
amode = S_IRUSR; |
|
159 |
else |
|
160 |
amode = S_IRUSR | S_IRGRP | S_IROTH; |
|
161 |
break; |
|
162 |
case java_io_FileSystem_ACCESS_WRITE: |
|
163 |
if (owneronly) |
|
164 |
amode = S_IWUSR; |
|
165 |
else |
|
166 |
amode = S_IWUSR | S_IWGRP | S_IWOTH; |
|
167 |
break; |
|
168 |
case java_io_FileSystem_ACCESS_EXECUTE: |
|
169 |
if (owneronly) |
|
170 |
amode = S_IXUSR; |
|
171 |
else |
|
172 |
amode = S_IXUSR | S_IXGRP | S_IXOTH; |
|
173 |
break; |
|
174 |
default: |
|
175 |
assert(0); |
|
176 |
} |
|
177 |
if (statMode(path, &mode)) { |
|
178 |
if (enable) |
|
179 |
mode |= amode; |
|
180 |
else |
|
181 |
mode &= ~amode; |
|
182 |
if (chmod(path, mode) >= 0) { |
|
183 |
rv = JNI_TRUE; |
|
184 |
} |
|
185 |
} |
|
186 |
} END_PLATFORM_STRING(env, path); |
|
187 |
return rv; |
|
188 |
} |
|
189 |
||
190 |
JNIEXPORT jlong JNICALL |
|
191 |
Java_java_io_UnixFileSystem_getLastModifiedTime(JNIEnv *env, jobject this, |
|
192 |
jobject file) |
|
193 |
{ |
|
194 |
jlong rv = 0; |
|
195 |
||
196 |
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
|
197 |
struct stat64 sb; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
198 |
if (stat64(path, &sb) == 0) { |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
199 |
rv = 1000 * (jlong)sb.st_mtime; |
2 | 200 |
} |
201 |
} END_PLATFORM_STRING(env, path); |
|
202 |
return rv; |
|
203 |
} |
|
204 |
||
205 |
||
206 |
JNIEXPORT jlong JNICALL |
|
207 |
Java_java_io_UnixFileSystem_getLength(JNIEnv *env, jobject this, |
|
208 |
jobject file) |
|
209 |
{ |
|
210 |
jlong rv = 0; |
|
211 |
||
212 |
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
|
213 |
struct stat64 sb; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
214 |
if (stat64(path, &sb) == 0) { |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
215 |
rv = sb.st_size; |
2 | 216 |
} |
217 |
} END_PLATFORM_STRING(env, path); |
|
218 |
return rv; |
|
219 |
} |
|
220 |
||
221 |
||
222 |
/* -- File operations -- */ |
|
223 |
||
224 |
||
225 |
JNIEXPORT jboolean JNICALL |
|
226 |
Java_java_io_UnixFileSystem_createFileExclusively(JNIEnv *env, jclass cls, |
|
227 |
jstring pathname) |
|
228 |
{ |
|
229 |
jboolean rv = JNI_FALSE; |
|
230 |
||
231 |
WITH_PLATFORM_STRING(env, pathname, path) { |
|
232 |
int fd; |
|
233 |
if (!strcmp (path, "/")) { |
|
234 |
fd = JVM_EEXIST; /* The root directory always exists */ |
|
235 |
} else { |
|
236 |
fd = JVM_Open(path, JVM_O_RDWR | JVM_O_CREAT | JVM_O_EXCL, 0666); |
|
237 |
} |
|
238 |
if (fd < 0) { |
|
239 |
if (fd != JVM_EEXIST) { |
|
240 |
JNU_ThrowIOExceptionWithLastError(env, path); |
|
241 |
} |
|
242 |
} else { |
|
243 |
JVM_Close(fd); |
|
244 |
rv = JNI_TRUE; |
|
245 |
} |
|
246 |
} END_PLATFORM_STRING(env, path); |
|
247 |
return rv; |
|
248 |
} |
|
249 |
||
250 |
||
251 |
JNIEXPORT jboolean JNICALL |
|
252 |
Java_java_io_UnixFileSystem_delete0(JNIEnv *env, jobject this, |
|
253 |
jobject file) |
|
254 |
{ |
|
255 |
jboolean rv = JNI_FALSE; |
|
256 |
||
257 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
258 |
if (remove(path) == 0) { |
|
259 |
rv = JNI_TRUE; |
|
260 |
} |
|
261 |
} END_PLATFORM_STRING(env, path); |
|
262 |
return rv; |
|
263 |
} |
|
264 |
||
265 |
||
266 |
JNIEXPORT jobjectArray JNICALL |
|
267 |
Java_java_io_UnixFileSystem_list(JNIEnv *env, jobject this, |
|
268 |
jobject file) |
|
269 |
{ |
|
270 |
DIR *dir = NULL; |
|
271 |
struct dirent64 *ptr; |
|
272 |
struct dirent64 *result; |
|
273 |
int len, maxlen; |
|
274 |
jobjectArray rv, old; |
|
275 |
||
276 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
277 |
dir = opendir(path); |
|
278 |
} END_PLATFORM_STRING(env, path); |
|
279 |
if (dir == NULL) return NULL; |
|
280 |
||
281 |
ptr = malloc(sizeof(struct dirent64) + (PATH_MAX + 1)); |
|
282 |
if (ptr == NULL) { |
|
283 |
JNU_ThrowOutOfMemoryError(env, "heap allocation failed"); |
|
284 |
closedir(dir); |
|
285 |
return NULL; |
|
286 |
} |
|
287 |
||
288 |
/* Allocate an initial String array */ |
|
289 |
len = 0; |
|
290 |
maxlen = 16; |
|
291 |
rv = (*env)->NewObjectArray(env, maxlen, JNU_ClassString(env), NULL); |
|
292 |
if (rv == NULL) goto error; |
|
293 |
||
294 |
/* Scan the directory */ |
|
295 |
while ((readdir64_r(dir, ptr, &result) == 0) && (result != NULL)) { |
|
296 |
jstring name; |
|
297 |
if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, "..")) |
|
298 |
continue; |
|
299 |
if (len == maxlen) { |
|
300 |
old = rv; |
|
301 |
rv = (*env)->NewObjectArray(env, maxlen <<= 1, |
|
302 |
JNU_ClassString(env), NULL); |
|
303 |
if (rv == NULL) goto error; |
|
304 |
if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error; |
|
305 |
(*env)->DeleteLocalRef(env, old); |
|
306 |
} |
|
307 |
name = JNU_NewStringPlatform(env, ptr->d_name); |
|
308 |
if (name == NULL) goto error; |
|
309 |
(*env)->SetObjectArrayElement(env, rv, len++, name); |
|
310 |
(*env)->DeleteLocalRef(env, name); |
|
311 |
} |
|
312 |
closedir(dir); |
|
313 |
free(ptr); |
|
314 |
||
315 |
/* Copy the final results into an appropriately-sized array */ |
|
316 |
old = rv; |
|
317 |
rv = (*env)->NewObjectArray(env, len, JNU_ClassString(env), NULL); |
|
318 |
if (rv == NULL) { |
|
319 |
return NULL; |
|
320 |
} |
|
321 |
if (JNU_CopyObjectArray(env, rv, old, len) < 0) { |
|
322 |
return NULL; |
|
323 |
} |
|
324 |
return rv; |
|
325 |
||
326 |
error: |
|
327 |
closedir(dir); |
|
328 |
free(ptr); |
|
329 |
return NULL; |
|
330 |
} |
|
331 |
||
332 |
||
333 |
JNIEXPORT jboolean JNICALL |
|
334 |
Java_java_io_UnixFileSystem_createDirectory(JNIEnv *env, jobject this, |
|
335 |
jobject file) |
|
336 |
{ |
|
337 |
jboolean rv = JNI_FALSE; |
|
338 |
||
339 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
340 |
if (mkdir(path, 0777) == 0) { |
|
341 |
rv = JNI_TRUE; |
|
342 |
} |
|
343 |
} END_PLATFORM_STRING(env, path); |
|
344 |
return rv; |
|
345 |
} |
|
346 |
||
347 |
||
348 |
JNIEXPORT jboolean JNICALL |
|
349 |
Java_java_io_UnixFileSystem_rename0(JNIEnv *env, jobject this, |
|
350 |
jobject from, jobject to) |
|
351 |
{ |
|
352 |
jboolean rv = JNI_FALSE; |
|
353 |
||
354 |
WITH_FIELD_PLATFORM_STRING(env, from, ids.path, fromPath) { |
|
355 |
WITH_FIELD_PLATFORM_STRING(env, to, ids.path, toPath) { |
|
356 |
if (rename(fromPath, toPath) == 0) { |
|
357 |
rv = JNI_TRUE; |
|
358 |
} |
|
359 |
} END_PLATFORM_STRING(env, toPath); |
|
360 |
} END_PLATFORM_STRING(env, fromPath); |
|
361 |
return rv; |
|
362 |
} |
|
363 |
||
364 |
JNIEXPORT jboolean JNICALL |
|
365 |
Java_java_io_UnixFileSystem_setLastModifiedTime(JNIEnv *env, jobject this, |
|
366 |
jobject file, jlong time) |
|
367 |
{ |
|
368 |
jboolean rv = JNI_FALSE; |
|
369 |
||
370 |
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
|
371 |
struct stat64 sb; |
2 | 372 |
|
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
373 |
if (stat64(path, &sb) == 0) { |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
374 |
struct timeval tv[2]; |
2 | 375 |
|
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
376 |
/* Preserve access time */ |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
377 |
tv[0].tv_sec = sb.st_atime; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
378 |
tv[0].tv_usec = 0; |
2 | 379 |
|
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
380 |
/* Change last-modified time */ |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
381 |
tv[1].tv_sec = time / 1000; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
382 |
tv[1].tv_usec = (time % 1000) * 1000; |
2 | 383 |
|
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
384 |
if (utimes(path, tv) == 0) |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
385 |
rv = JNI_TRUE; |
2 | 386 |
} |
387 |
} END_PLATFORM_STRING(env, path); |
|
388 |
||
389 |
return rv; |
|
390 |
} |
|
391 |
||
392 |
||
393 |
JNIEXPORT jboolean JNICALL |
|
394 |
Java_java_io_UnixFileSystem_setReadOnly(JNIEnv *env, jobject this, |
|
395 |
jobject file) |
|
396 |
{ |
|
397 |
jboolean rv = JNI_FALSE; |
|
398 |
||
399 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
400 |
int mode; |
|
401 |
if (statMode(path, &mode)) { |
|
402 |
if (chmod(path, mode & ~(S_IWUSR | S_IWGRP | S_IWOTH)) >= 0) { |
|
403 |
rv = JNI_TRUE; |
|
404 |
} |
|
405 |
} |
|
406 |
} END_PLATFORM_STRING(env, path); |
|
407 |
return rv; |
|
408 |
} |
|
409 |
||
410 |
JNIEXPORT jlong JNICALL |
|
411 |
Java_java_io_UnixFileSystem_getSpace(JNIEnv *env, jobject this, |
|
412 |
jobject file, jint t) |
|
413 |
{ |
|
414 |
jlong rv = 0L; |
|
415 |
||
416 |
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { |
|
417 |
struct statvfs fsstat; |
|
418 |
memset(&fsstat, 0, sizeof(struct statvfs)); |
|
419 |
if (statvfs(path, &fsstat) == 0) { |
|
420 |
switch(t) { |
|
421 |
case java_io_FileSystem_SPACE_TOTAL: |
|
422 |
rv = jlong_mul(long_to_jlong(fsstat.f_frsize), |
|
423 |
long_to_jlong(fsstat.f_blocks)); |
|
424 |
break; |
|
425 |
case java_io_FileSystem_SPACE_FREE: |
|
426 |
rv = jlong_mul(long_to_jlong(fsstat.f_frsize), |
|
427 |
long_to_jlong(fsstat.f_bfree)); |
|
428 |
break; |
|
429 |
case java_io_FileSystem_SPACE_USABLE: |
|
430 |
rv = jlong_mul(long_to_jlong(fsstat.f_frsize), |
|
431 |
long_to_jlong(fsstat.f_bavail)); |
|
432 |
break; |
|
433 |
default: |
|
434 |
assert(0); |
|
435 |
} |
|
436 |
} |
|
437 |
} END_PLATFORM_STRING(env, path); |
|
438 |
return rv; |
|
439 |
} |