diff options
author | Mario Torre <neugens@limasoftware.net> | 2007-02-09 19:51:07 +0000 |
---|---|---|
committer | Mario Torre <neugens@limasoftware.net> | 2007-02-09 19:51:07 +0000 |
commit | f367015ea8656d668a519556118ffb2e722ba539 (patch) | |
tree | a6c89de103b44316de7b12b2a25cb20577025660 | |
parent | 4e687652bad7ffdf53467a937a1f3c2093dcc152 (diff) | |
download | classpath-f367015ea8656d668a519556118ffb2e722ba539.tar.gz |
2007-02-09 Mario Torre <neugens@limasoftware.net>
* vm/reference/java/io/VMFile.java:
(canExecute): new 1.6 native method.
(setReadable): likewise.
(setWritable): likewise.
(setExecutable): likewise.
* java/io/File.java: added import for gnu.classpath.NotImplementedException.
(setReadOnly): new 1.6 method.
(canExecute): likewise.
(setReadable): likewise.
(setWritable): likewise.
(setExecutable): likewise.
(getUsableSpace): added stub for new 1.6 method.
(getFreeSpace): likewise.
(getTotalSpace): likewise.
(checkExec): new private method to support new 1.6 additions.
* native/jni/java-io/java_io_VMFile.c:
set_file_permissions: new helper function.
Java_java_io_VMFile_setReadable: new native method to bakcup 1.6 methods
in VMFile.java.
Java_java_io_VMFile_setWritable: likewise.
Java_java_io_VMFile_setExecutable: likewise.
Java_java_io_VMFile_canExecute: likewise.
* native/jni/native-lib/cpio.h: added new flags: CPFILE_FLAG_EXEC,
CPFILE_FLAG_USR and CPFILE_FLAG_OFF.
cpio_chmod: new function declaration.
cpio_checkAccess: likewise.
* native/jni/native-lib/cpio.c:
cpio_chmod: new function definition.
cpio_checkAccess: likewise.
-rw-r--r-- | ChangeLog | 32 | ||||
-rw-r--r-- | include/java_io_VMFile.h | 4 | ||||
-rw-r--r-- | java/io/File.java | 213 | ||||
-rw-r--r-- | native/jni/java-io/java_io_VMFile.c | 153 | ||||
-rw-r--r-- | native/jni/native-lib/cpio.c | 70 | ||||
-rw-r--r-- | native/jni/native-lib/cpio.h | 5 | ||||
-rw-r--r-- | vm/reference/java/io/VMFile.java | 28 |
7 files changed, 504 insertions, 1 deletions
@@ -1,3 +1,35 @@ +2007-02-09 Mario Torre <neugens@limasoftware.net> + + * vm/reference/java/io/VMFile.java: + (canExecute): new 1.6 native method. + (setReadable): likewise. + (setWritable): likewise. + (setExecutable): likewise. + * java/io/File.java: added import for gnu.classpath.NotImplementedException. + (setReadOnly): new 1.6 method. + (canExecute): likewise. + (setReadable): likewise. + (setWritable): likewise. + (setExecutable): likewise. + (getUsableSpace): added stub for new 1.6 method. + (getFreeSpace): likewise. + (getTotalSpace): likewise. + (checkExec): new private method to support new 1.6 additions. + * native/jni/java-io/java_io_VMFile.c: + set_file_permissions: new helper function. + Java_java_io_VMFile_setReadable: new native method to bakcup 1.6 methods + in VMFile.java. + Java_java_io_VMFile_setWritable: likewise. + Java_java_io_VMFile_setExecutable: likewise. + Java_java_io_VMFile_canExecute: likewise. + * native/jni/native-lib/cpio.h: added new flags: CPFILE_FLAG_EXEC, + CPFILE_FLAG_USR and CPFILE_FLAG_OFF. + cpio_chmod: new function declaration. + cpio_checkAccess: likewise. + * native/jni/native-lib/cpio.c: + cpio_chmod: new function definition. + cpio_checkAccess: likewise. + 2007-02-09 Gary Benson <gbenson@redhat.com> * javax/management/ObjectName.java diff --git a/include/java_io_VMFile.h b/include/java_io_VMFile.h index fa1079f12..4b0273d11 100644 --- a/include/java_io_VMFile.h +++ b/include/java_io_VMFile.h @@ -20,9 +20,13 @@ JNIEXPORT jboolean JNICALL Java_java_io_VMFile_exists (JNIEnv *env, jclass, jstr JNIEXPORT jboolean JNICALL Java_java_io_VMFile_delete (JNIEnv *env, jclass, jstring); JNIEXPORT jboolean JNICALL Java_java_io_VMFile_setLastModified (JNIEnv *env, jclass, jstring, jlong); JNIEXPORT jboolean JNICALL Java_java_io_VMFile_mkdir (JNIEnv *env, jclass, jstring); +JNIEXPORT jboolean JNICALL Java_java_io_VMFile_setReadable (JNIEnv *env, jclass, jstring, jboolean, jboolean); +JNIEXPORT jboolean JNICALL Java_java_io_VMFile_setWritable (JNIEnv *env, jclass, jstring, jboolean, jboolean); +JNIEXPORT jboolean JNICALL Java_java_io_VMFile_setExecutable (JNIEnv *env, jclass, jstring, jboolean, jboolean); JNIEXPORT jboolean JNICALL Java_java_io_VMFile_isFile (JNIEnv *env, jclass, jstring); JNIEXPORT jboolean JNICALL Java_java_io_VMFile_canWrite (JNIEnv *env, jclass, jstring); JNIEXPORT jboolean JNICALL Java_java_io_VMFile_canRead (JNIEnv *env, jclass, jstring); +JNIEXPORT jboolean JNICALL Java_java_io_VMFile_canExecute (JNIEnv *env, jclass, jstring); JNIEXPORT jboolean JNICALL Java_java_io_VMFile_isDirectory (JNIEnv *env, jclass, jstring); JNIEXPORT jstring JNICALL Java_java_io_VMFile_toCanonicalForm (JNIEnv *env, jclass, jstring); #undef java_io_VMFile_IS_CASE_SENSITIVE diff --git a/java/io/File.java b/java/io/File.java index 5d1b3ec85..d560eba3a 100644 --- a/java/io/File.java +++ b/java/io/File.java @@ -39,6 +39,7 @@ exception statement from your version. */ package java.io; +import gnu.classpath.NotImplementedException; import gnu.classpath.SystemProperties; import java.net.MalformedURLException; @@ -164,6 +165,29 @@ public class File implements Serializable, Comparable<File> } /** + * This method tests whether or not the current thread is allowed to + * to execute the file pointed to by this object. This will be true if and + * and only if 1) the file exists and 2) the <code>SecurityManager</code> + * (if any) allows access to the file via it's <code>checkExec</code> + * method 3) the file is executable. + * + * @return <code>true</code> if execution is allowed, + * <code>false</code> otherwise + * + * @exception SecurityException If the <code>SecurityManager</code> + * does not allow access to the file + */ + public boolean canExecute() + { + if (!VMFile.exists(path)) + return false; + + checkExec(); + + return VMFile.canExecute(path); + } + + /** * This method creates a new file of zero length with the same name as * the path of this <code>File</code> object if an only if that file * does not already exist. @@ -1123,6 +1147,186 @@ public class File implements Serializable, Comparable<File> } /** + * This method sets the owner's read permission for the File represented by + * this object. + * + * It is the same as calling <code>setReadable(readable, true)</code>. + * + * @param <code>readable</code> <code>true</code> to set read permission, + * <code>false</code> to unset the read permission. + * @return <code>true</code> if the file permissions are changed, + * <code>false</code> otherwise. + * @exception SecurityException If write access of the file is not permitted. + * @see #setReadable(boolean, boolean) + * @since 1.6 + */ + public boolean setReadable(boolean readable) + { + return setReadable(readable, true); + } + + /** + * This method sets the read permissions for the File represented by + * this object. + * + * If <code>ownerOnly</code> is set to <code>true</code> then only the + * read permission bit for the owner of the file is changed. + * + * If <code>ownerOnly</code> is set to <code>false</code>, the file + * permissions are changed so that the file can be read by everyone. + * + * On unix like systems this sets the <code>user</code>, <code>group</code> + * and <code>other</code> read bits and is equal to call + * <code>chmod a+r</code> on the file. + * + * @param <code>readable</code> <code>true</code> to set read permission, + * <code>false</code> to unset the read permission. + * @param <code>ownerOnly</code> <code>true</code> to set read permission + * for owner only, <code>false</code> for all. + * @return <code>true</code> if the file permissions are changed, + * <code>false</code> otherwise. + * @exception SecurityException If write access of the file is not permitted. + * @see #setReadable(boolean) + * @since 1.6 + */ + public boolean setReadable(boolean readable, boolean ownerOnly) + { + checkWrite(); + return VMFile.setReadable(path, readable, ownerOnly); + } + + /** + * This method sets the owner's write permission for the File represented by + * this object. + * + * It is the same as calling <code>setWritable(readable, true)</code>. + * + * @param <code>writable</code> <code>true</code> to set write permission, + * <code>false</code> to unset write permission. + * @return <code>true</code> if the file permissions are changed, + * <code>false</code> otherwise. + * @exception SecurityException If write access of the file is not permitted. + * @see #setWritable(boolean, boolean) + * @since 1.6 + */ + public boolean setWritable(boolean writable) + { + return setWritable(writable, true); + } + + /** + * This method sets the write permissions for the File represented by + * this object. + * + * If <code>ownerOnly</code> is set to <code>true</code> then only the + * write permission bit for the owner of the file is changed. + * + * If <code>ownerOnly</code> is set to <code>false</code>, the file + * permissions are changed so that the file can be written by everyone. + * + * On unix like systems this set the <code>user</code>, <code>group</code> + * and <code>other</code> write bits and is equal to call + * <code>chmod a+w</code> on the file. + * + * @param <code>writable</code> <code>true</code> to set write permission, + * <code>false</code> to unset write permission. + * @param <code>ownerOnly</code> <code>true</code> to set write permission + * for owner only, <code>false</code> for all. + * @return <code>true</code> if the file permissions are changed, + * <code>false</code> otherwise. + * @exception SecurityException If write access of the file is not permitted. + * @see #setWritable(boolean) + * @since 1.6 + */ + public boolean setWritable(boolean writable, boolean ownerOnly) + { + checkWrite(); + return VMFile.setWritable(path, writable, ownerOnly); + } + + /** + * This method sets the owner's execute permission for the File represented + * by this object. + * + * It is the same as calling <code>setExecutable(readable, true)</code>. + * + * @param <code>executable</code> <code>true</code> to set execute permission, + * <code>false</code> to unset execute permission. + * @return <code>true</code> if the file permissions are changed, + * <code>false</code> otherwise. + * @exception SecurityException If write access of the file is not permitted. + * @see #setExecutable(boolean, boolean) + * @since 1.6 + */ + public boolean setExecutable(boolean executable) + { + return setExecutable(executable, true); + } + + /** + * This method sets the execute permissions for the File represented by + * this object. + * + * If <code>ownerOnly</code> is set to <code>true</code> then only the + * execute permission bit for the owner of the file is changed. + * + * If <code>ownerOnly</code> is set to <code>false</code>, the file + * permissions are changed so that the file can be executed by everyone. + * + * On unix like systems this set the <code>user</code>, <code>group</code> + * and <code>other</code> write bits and is equal to call + * <code>chmod a+x</code> on the file. + * + * @param <code>executable</code> <code>true</code> to set write permission, + * <code>false</code> to unset write permission. + * @param <code>ownerOnly</code> <code>true</code> to set write permission + * for owner only, <code>false</code> for all. + * @return <code>true</code> if the file permissions are changed, + * <code>false</code> otherwise. + * @exception SecurityException If write access of the file is not permitted. + * @see #setExecutable(boolean) + * @since 1.6 + */ + public boolean setExecutable(boolean executable, boolean ownerOnly) + { + checkWrite(); + return VMFile.setExecutable(path, executable, ownerOnly); + } + + /** + * + * @return + * @since 1.6 + */ + public long getTotalSpace() + throws NotImplementedException + { + return 0L; + } + + /** + * + * @return + * @since 1.6 + */ + public long getFreeSpace() + throws NotImplementedException + { + return 0L; + } + + /** + * + * @return + * @since 1.6 + */ + public long getUsableSpace() + throws NotImplementedException + { + return 0L; + } + + /** * This method sets the file represented by this object to be read only. * A read only file or directory cannot be modified. Please note that * GNU systems allow read only files to be deleted if the directory it @@ -1315,6 +1519,15 @@ public class File implements Serializable, Comparable<File> s.checkRead(path); } + private void checkExec() + { + // Check the SecurityManager + SecurityManager s = System.getSecurityManager(); + + if (s != null) + s.checkExec(path); + } + /** * Calling this method requests that the file represented by this object * be deleted when the virtual machine exits. Note that this request cannot diff --git a/native/jni/java-io/java_io_VMFile.c b/native/jni/java-io/java_io_VMFile.c index de1320b0c..2882075bb 100644 --- a/native/jni/java-io/java_io_VMFile.c +++ b/native/jni/java-io/java_io_VMFile.c @@ -55,6 +55,18 @@ exception statement from your version. */ #include "java_io_VMFile.h" +/* ***** PRIVATE FUNCTIONS DELCARATION ***** */ + +/** + * Enables of disables the passed permission bit of a file. + */ +static jboolean set_file_permissions (JNIEnv *env, jstring name, + jboolean enable, + jboolean ownerOnly, + int permissions); + +/* ***** END: PRIVATE FUNCTIONS DELCARATION ***** */ + /*************************************************************************/ /* @@ -189,6 +201,46 @@ Java_java_io_VMFile_canWrite (JNIEnv * env, /*************************************************************************/ /* + * This method checks to see if we have execute permission on a file. + * + * Class: java_io_VMFile + * Method: canExecute + * Signature: (Ljava/lang/String;)Z + */ + +JNIEXPORT jboolean JNICALL +Java_java_io_VMFile_canExecute (JNIEnv * env, + jclass clazz __attribute__ ((__unused__)), + jstring name) +{ +#ifndef WITHOUT_FILESYSTEM + const char *filename; + int result; + + /* Don't use the JCL convert function because it throws an exception + on failure */ + filename = (*env)->GetStringUTFChars (env, name, 0); + if (filename == NULL) + { + return JNI_FALSE; + } + + result = cpio_checkAccess (filename, CPFILE_FLAG_EXEC); + + (*env)->ReleaseStringUTFChars (env, name, filename); + if (result != CPNATIVE_OK) + return JNI_FALSE; + + return JNI_TRUE; +#else /* not WITHOUT_FILESYSTEM */ + return JNI_FALSE; +#endif /* not WITHOUT_FILESYSTEM */ +} + + +/*************************************************************************/ + +/* * This method makes a file read only. * * Class: java_io_VMFile @@ -225,6 +277,66 @@ Java_java_io_VMFile_setReadOnly (JNIEnv * env, /*************************************************************************/ /* + * This method changes the read permission bit of a file. + * + * Class: java_io_VMFile + * Method: setReadable + * Signature: (Ljava/lang/String;ZZ)Z + */ +JNIEXPORT jboolean JNICALL +Java_java_io_VMFile_setReadable (JNIEnv *env, + jclass clazz __attribute__ ((__unused__)), + jstring name, + jboolean readable, + jboolean ownerOnly) +{ + return set_file_permissions (env, name, readable, ownerOnly, + CPFILE_FLAG_READ); +} + + +/*************************************************************************/ + +/* + * This method changes the write permission bit of a file. + * + * Class: java_io_VMFile + * Method: setWritable + * Signature: (Ljava/lang/String;ZZ)Z + */ +JNIEXPORT jboolean JNICALL +Java_java_io_VMFile_setWritable (JNIEnv *env, + jclass clazz __attribute__ ((__unused__)), + jstring name, + jboolean writable, + jboolean ownerOnly) +{ + return set_file_permissions (env, name, writable, ownerOnly, + CPFILE_FLAG_WRITE); +} + +/*************************************************************************/ + +/* + * This method changes the execute permission bit of a file. + * + * Class: java_io_VMFile + * Method: setExecutable + * Signature: (Ljava/lang/String;ZZ)Z + */ +JNIEXPORT jboolean JNICALL +Java_java_io_VMFile_setExecutable (JNIEnv *env, + jclass clazz __attribute__ ((__unused__)), + jstring name, + jboolean executable, + jboolean ownerOnly) +{ + return set_file_permissions (env, name, executable, ownerOnly, + CPFILE_FLAG_EXEC); +} +/*************************************************************************/ + +/* * This method checks to see if a file exists. * * Class: java_io_VMFile @@ -967,3 +1079,44 @@ Java_java_io_VMFile_toCanonicalForm (JNIEnv *env, return NULL; #endif /* not WITHOUT_FILESYSTEM */ } + +/*************************************************************************/ + +/* ***** PRIVATE FUNCTIONS IMPLEMENTATION ***** */ + +static jboolean set_file_permissions (JNIEnv *env, jstring name, + jboolean enable, + jboolean ownerOnly, + int permissions) +{ +#ifndef WITHOUT_FILESYSTEM + const char *filename; + int result = JNI_FALSE; + + /* Don't use the JCL convert function because it throws an exception + on failure */ + filename = (*env)->GetStringUTFChars (env, name, 0); + if (filename == NULL) + { + return JNI_FALSE; + } + + if (ownerOnly) + { + permissions |= CPFILE_FLAG_USR; + } + + if (!enable) + { + permissions |= CPFILE_FLAG_OFF; + } + + result = cpio_chmod (filename, permissions); + (*env)->ReleaseStringUTFChars (env, name, filename); + + return result == CPNATIVE_OK ? JNI_TRUE : JNI_FALSE; + +#else /* not WITHOUT_FILESYSTEM */ + return JNI_FALSE; +#endif /* not WITHOUT_FILESYSTEM */ +} diff --git a/native/jni/native-lib/cpio.c b/native/jni/native-lib/cpio.c index 2777a31b2..ac3c0b5e7 100644 --- a/native/jni/native-lib/cpio.c +++ b/native/jni/native-lib/cpio.c @@ -352,6 +352,76 @@ int cpio_setFileReadonly (const char *filename) return 0; } +int cpio_chmod (const char *filename, int permissions) +{ + struct stat statbuf; + int perms = 0; + + if (stat(filename, &statbuf) < 0) + return errno; + + /* check for permission flags */ + if (permissions & CPFILE_FLAG_USR) + { + if (permissions & CPFILE_FLAG_READ) + perms |= S_IRUSR; + + if (permissions & CPFILE_FLAG_WRITE) + perms |= S_IWUSR; + + if (permissions & CPFILE_FLAG_EXEC) + perms |= S_IXUSR; + } + else + { + if (permissions & CPFILE_FLAG_READ) + perms |= (S_IRUSR | S_IRGRP | S_IROTH); + + if (permissions & CPFILE_FLAG_WRITE) + perms |= (S_IWUSR | S_IWGRP | S_IWOTH); + + if (permissions & CPFILE_FLAG_EXEC) + perms |= (S_IXUSR | S_IXGRP | S_IXOTH); + } + + if (permissions & CPFILE_FLAG_OFF) + perms = statbuf.st_mode & ~perms; + else + perms = statbuf.st_mode | perms; + + if (chmod(filename, perms) < 0) + return errno; + + return 0; +} + +int cpio_checkAccess (const char *filename, unsigned int flag) +{ + struct stat statbuf; + unsigned int perms = 0; + + if (stat(filename, &statbuf) < 0) + return errno; + + switch (flag) + { + case CPFILE_FLAG_READ: + perms = R_OK; + break; + + case CPFILE_FLAG_WRITE: + perms = W_OK; + break; + + case CPFILE_FLAG_EXEC: + default: + perms = X_OK; + break; + } + + return (access (filename, perms)); +} + int cpio_isFileExists (const char *filename) { struct stat statbuf; diff --git a/native/jni/native-lib/cpio.h b/native/jni/native-lib/cpio.h index b388b5b50..259fc62cf 100644 --- a/native/jni/native-lib/cpio.h +++ b/native/jni/native-lib/cpio.h @@ -48,6 +48,9 @@ exception statement from your version. */ #define CPFILE_FLAG_BINARY 0x0020 #define CPFILE_FLAG_READ 0x0040 #define CPFILE_FLAG_WRITE 0x0080 +#define CPFILE_FLAG_EXEC 0x0100 +#define CPFILE_FLAG_USR 0x0400 +#define CPFILE_FLAG_OFF 0x0800 #define CPFILE_PERMISSION_NORMAL 1 @@ -70,6 +73,8 @@ JNIEXPORT int cpio_closeOnExec(int fd); #define CPFILE_DIRECTORY 1 JNIEXPORT int cpio_setFileReadonly (const char *filename); +JNIEXPORT int cpio_chmod (const char *filename, int permissions); +JNIEXPORT int cpio_checkAccess (const char *filename, unsigned int flag); JNIEXPORT int cpio_isFileExists (const char *filename); JNIEXPORT int cpio_checkType (const char *filename, jint *entryType); JNIEXPORT int cpio_getModificationTime (const char *filename, jlong *mtime); diff --git a/vm/reference/java/io/VMFile.java b/vm/reference/java/io/VMFile.java index 13d256d42..4f708a4a5 100644 --- a/vm/reference/java/io/VMFile.java +++ b/vm/reference/java/io/VMFile.java @@ -116,6 +116,27 @@ final class VMFile */ static native boolean mkdir(String dirpath); + /** + * Set the read permission of the file. + */ + public static synchronized native boolean setReadable(String path, + boolean readable, + boolean ownerOnly); + + /** + * Set the write permission of the file. + */ + public static synchronized native boolean setWritable(String path, + boolean writable, + boolean ownerOnly); + + /** + * Set the execute permission of the file. + */ + public static synchronized native boolean setExecutable(String path, + boolean executable, + boolean ownerOnly); + /* * This native method does the actual check of whether or not a file * is a plain file or not. It also handles the existence check to @@ -127,7 +148,7 @@ final class VMFile * This native method checks file permissions for writing */ static synchronized native boolean canWrite(String path); - + /** * This methods checks if a directory can be written to. */ @@ -150,6 +171,11 @@ final class VMFile */ static synchronized native boolean canRead(String path); + /** + * This native method checks file permissions for execution + */ + static synchronized native boolean canExecute(String path); + /* * This method does the actual check of whether or not a file is a * directory or not. It also handle the existence check to eliminate |