1 /*
2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
60 Java_java_io_UnixFileSystem_initIDs(JNIEnv *env, jclass cls)
61 {
62 jclass fileClass = (*env)->FindClass(env, "java/io/File");
63 if (!fileClass) return;
64 ids.path = (*env)->GetFieldID(env, fileClass,
65 "path", "Ljava/lang/String;");
66 }
67
68 /* -- Path operations -- */
69
70 extern int canonicalize(char *path, const char *out, int len);
71
72 JNIEXPORT jstring JNICALL
73 Java_java_io_UnixFileSystem_canonicalize0(JNIEnv *env, jobject this,
74 jstring pathname)
75 {
76 jstring rv = NULL;
77
78 WITH_PLATFORM_STRING(env, pathname, path) {
79 char canonicalPath[JVM_MAXPATHLEN];
80 if (canonicalize(JVM_NativePath((char *)path),
81 canonicalPath, JVM_MAXPATHLEN) < 0) {
82 JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
83 } else {
84 #ifdef MACOSX
85 rv = newStringPlatform(env, canonicalPath);
86 #else
87 rv = JNU_NewStringPlatform(env, canonicalPath);
88 #endif
89 }
90 } END_PLATFORM_STRING(env, path);
91 return rv;
92 }
93
94
95 /* -- Attribute accessors -- */
96
97
98 static jboolean
99 statMode(const char *path, int *mode)
100 {
224 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
225 struct stat64 sb;
226 if (stat64(path, &sb) == 0) {
227 rv = sb.st_size;
228 }
229 } END_PLATFORM_STRING(env, path);
230 return rv;
231 }
232
233
234 /* -- File operations -- */
235
236
237 JNIEXPORT jboolean JNICALL
238 Java_java_io_UnixFileSystem_createFileExclusively(JNIEnv *env, jclass cls,
239 jstring pathname)
240 {
241 jboolean rv = JNI_FALSE;
242
243 WITH_PLATFORM_STRING(env, pathname, path) {
244 int fd;
245 if (!strcmp (path, "/")) {
246 fd = JVM_EEXIST; /* The root directory always exists */
247 } else {
248 fd = JVM_Open(path, JVM_O_RDWR | JVM_O_CREAT | JVM_O_EXCL, 0666);
249 }
250 if (fd < 0) {
251 if (fd != JVM_EEXIST) {
252 JNU_ThrowIOExceptionWithLastError(env, path);
253 }
254 } else {
255 JVM_Close(fd);
256 rv = JNI_TRUE;
257 }
258 } END_PLATFORM_STRING(env, path);
259 return rv;
260 }
261
262
263 JNIEXPORT jboolean JNICALL
264 Java_java_io_UnixFileSystem_delete0(JNIEnv *env, jobject this,
265 jobject file)
266 {
267 jboolean rv = JNI_FALSE;
268
269 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
270 if (remove(path) == 0) {
271 rv = JNI_TRUE;
272 }
273 } END_PLATFORM_STRING(env, path);
274 return rv;
275 }
276
|
1 /*
2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
60 Java_java_io_UnixFileSystem_initIDs(JNIEnv *env, jclass cls)
61 {
62 jclass fileClass = (*env)->FindClass(env, "java/io/File");
63 if (!fileClass) return;
64 ids.path = (*env)->GetFieldID(env, fileClass,
65 "path", "Ljava/lang/String;");
66 }
67
68 /* -- Path operations -- */
69
70 extern int canonicalize(char *path, const char *out, int len);
71
72 JNIEXPORT jstring JNICALL
73 Java_java_io_UnixFileSystem_canonicalize0(JNIEnv *env, jobject this,
74 jstring pathname)
75 {
76 jstring rv = NULL;
77
78 WITH_PLATFORM_STRING(env, pathname, path) {
79 char canonicalPath[JVM_MAXPATHLEN];
80 if (canonicalize((char *)path,
81 canonicalPath, JVM_MAXPATHLEN) < 0) {
82 JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
83 } else {
84 #ifdef MACOSX
85 rv = newStringPlatform(env, canonicalPath);
86 #else
87 rv = JNU_NewStringPlatform(env, canonicalPath);
88 #endif
89 }
90 } END_PLATFORM_STRING(env, path);
91 return rv;
92 }
93
94
95 /* -- Attribute accessors -- */
96
97
98 static jboolean
99 statMode(const char *path, int *mode)
100 {
224 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
225 struct stat64 sb;
226 if (stat64(path, &sb) == 0) {
227 rv = sb.st_size;
228 }
229 } END_PLATFORM_STRING(env, path);
230 return rv;
231 }
232
233
234 /* -- File operations -- */
235
236
237 JNIEXPORT jboolean JNICALL
238 Java_java_io_UnixFileSystem_createFileExclusively(JNIEnv *env, jclass cls,
239 jstring pathname)
240 {
241 jboolean rv = JNI_FALSE;
242
243 WITH_PLATFORM_STRING(env, pathname, path) {
244 FD fd;
245 /* The root directory always exists */
246 if (strcmp (path, "/")) {
247 fd = handleOpen(path, O_RDWR | O_CREAT | O_EXCL, 0666);
248 if (fd < 0) {
249 if (errno != EEXIST)
250 JNU_ThrowIOExceptionWithLastError(env, path);
251 } else {
252 if (close(fd) == -1)
253 JNU_ThrowIOExceptionWithLastError(env, path);
254 rv = JNI_TRUE;
255 }
256 }
257 } END_PLATFORM_STRING(env, path);
258 return rv;
259 }
260
261
262 JNIEXPORT jboolean JNICALL
263 Java_java_io_UnixFileSystem_delete0(JNIEnv *env, jobject this,
264 jobject file)
265 {
266 jboolean rv = JNI_FALSE;
267
268 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
269 if (remove(path) == 0) {
270 rv = JNI_TRUE;
271 }
272 } END_PLATFORM_STRING(env, path);
273 return rv;
274 }
275
|