diff options
-rw-r--r-- | ChangeLog | 19 | ||||
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | include/java_io_VMFile.h | 8 | ||||
-rw-r--r-- | java/io/File.java | 67 | ||||
-rw-r--r-- | native/jni/java-io/java_io_VMFile.c | 88 | ||||
-rw-r--r-- | native/jni/native-lib/cpio.c | 42 | ||||
-rw-r--r-- | native/jni/native-lib/cpio.h | 9 | ||||
-rw-r--r-- | vm/reference/java/io/VMFile.java | 15 |
8 files changed, 246 insertions, 3 deletions
@@ -1,3 +1,22 @@ +2007-11-06 Mario Torre <neugens@limasoftware.net> + + * vm/reference/java/io/VMFile.java: + (getTotalSpace): new method. + (getUsableSpace): likewise. + (getFreeSpace): likewise. + * java/io/File.java: (getTotalSpace): new method. + (getUsableSpace): likewise. + (getFreeSpace): likewise. + * native/jni/java-io/java_io_VMFile.c: + (Java_java_io_VMFile_getTotalSpace): new function. + (Java_java_io_VMFile_getFreeSpace): likewise. + (Java_java_io_VMFile_getUsableSpace): likewise. + * native/jni/native-lib/cpio.h: (cpio_df): new function. + (CPFILE_DF_TYPE): enum type for cpio_df. + * native/jni/native-lib/cpio.c: (cpio_df): new function. + * include/java_io_VMFile.h: regenerated. + * configure.ac: added check for statvfs. + 2007-11-04 Andrew John Hughes <gnu_andrew@member.fsf.org> PR classpath/33385: diff --git a/configure.ac b/configure.ac index 52161c63d..4dea1a33d 100644 --- a/configure.ac +++ b/configure.ac @@ -394,6 +394,7 @@ if test "x${COMPILE_JNI}" = xyes; then gethostbyname_r localtime_r \ strerror_r \ fcntl \ + statvfs \ mmap munmap mincore msync madvise getpagesize sysconf \ lstat readlink \ inet_aton inet_addr inet_pton \ diff --git a/include/java_io_VMFile.h b/include/java_io_VMFile.h index 4b0273d11..270217674 100644 --- a/include/java_io_VMFile.h +++ b/include/java_io_VMFile.h @@ -1,10 +1,10 @@ /* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> + #ifndef __java_io_VMFile__ #define __java_io_VMFile__ -#include <jni.h> - #ifdef __cplusplus extern "C" { @@ -20,6 +20,9 @@ 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 jlong JNICALL Java_java_io_VMFile_getTotalSpace (JNIEnv *env, jclass, jstring); +JNIEXPORT jlong JNICALL Java_java_io_VMFile_getFreeSpace (JNIEnv *env, jclass, jstring); +JNIEXPORT jlong JNICALL Java_java_io_VMFile_getUsableSpace (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); @@ -29,6 +32,7 @@ JNIEXPORT jboolean JNICALL Java_java_io_VMFile_canRead (JNIEnv *env, jclass, jst 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 #define java_io_VMFile_IS_CASE_SENSITIVE 1L #undef java_io_VMFile_IS_DOS_8_3 diff --git a/java/io/File.java b/java/io/File.java index f34b4dd2b..cd1116350 100644 --- a/java/io/File.java +++ b/java/io/File.java @@ -1293,6 +1293,73 @@ public class File implements Serializable, Comparable<File> } /** + * Get the total space for the partition pointed by this file path, in bytes. + * + * @return the total number of bytes in this partition. + * @since 1.6 + */ + public long getTotalSpace() + { + // check security manager. + SecurityManager s = System.getSecurityManager(); + if (s != null) + s.checkPermission(new RuntimePermission("getFileSystemAttributes")); + checkRead(); + + return VMFile.getTotalSpace(path); + } + + /** + * Get the free space in the partition pointed by this file path, in bytes. + * + * @return the number of free bytes in this partition. + * @since 1.6 + */ + public long getFreeSpace() + { + // check security manager. + SecurityManager s = System.getSecurityManager(); + if (s != null) + s.checkPermission(new RuntimePermission("getFileSystemAttributes")); + checkRead(); + + return VMFile.getFreeSpace(path); + } + + /** + * Get the usable space in the partition pointed by this file path, in bytes. + * This is not necessarily the same as the number returned by + * {@link #getFreeSpace()}. + * + * <strong>Implementation note</strong>: Unlike the RI, on Linux and UNIX + * like systems this methods take into account the reserved space for the + * "root" user. This means that the returned results will be a little + * different if a normal user or root perform the query. + * + * Also, the bytes returned should be interpreted as an hint, and may be + * different at each call of this method or even right after the method + * returns. + * + * @return the number of usable bytes in this partition. + * @since 1.6 + */ + public long getUsableSpace() + { + // check security manager. + SecurityManager s = System.getSecurityManager(); + if (s != null) + s.checkPermission(new RuntimePermission("getFileSystemAttributes")); + checkRead(); + + // root users can use the reserved extra space + String user = System.getProperty("user.name"); + if (user != null && user.equals("root")) + return VMFile.getFreeSpace(path); + + return VMFile.getUsableSpace(path); + } + + /** * 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 diff --git a/native/jni/java-io/java_io_VMFile.c b/native/jni/java-io/java_io_VMFile.c index 2882075bb..e79704fa6 100644 --- a/native/jni/java-io/java_io_VMFile.c +++ b/native/jni/java-io/java_io_VMFile.c @@ -334,6 +334,94 @@ Java_java_io_VMFile_setExecutable (JNIEnv *env, return set_file_permissions (env, name, executable, ownerOnly, CPFILE_FLAG_EXEC); } + +/*************************************************************************/ + +JNIEXPORT jlong JNICALL +Java_java_io_VMFile_getTotalSpace (JNIEnv *env, + jclass clazz __attribute__ ((__unused__)), + jstring path) +{ +#ifndef WITHOUT_FILESYSTEM + + jlong result; + const char *_path = NULL; + + _path = (*env)->GetStringUTFChars (env, path, 0); + if (_path == NULL) + { + return 0L; + } + + result = cpio_df (_path, TOTAL); + + (*env)->ReleaseStringUTFChars (env, path, _path); + + return result; + +#else /* not WITHOUT_FILESYSTEM */ + return 0L; +#endif /* not WITHOUT_FILESYSTEM */ +} + +/*************************************************************************/ + +JNIEXPORT jlong JNICALL +Java_java_io_VMFile_getFreeSpace (JNIEnv *env, + jclass clazz __attribute__ ((__unused__)), + jstring path) +{ +#ifndef WITHOUT_FILESYSTEM + + jlong result; + const char *_path = NULL; + + _path = (*env)->GetStringUTFChars (env, path, 0); + if (_path == NULL) + { + return 0L; + } + + result = cpio_df (_path, FREE); + + (*env)->ReleaseStringUTFChars (env, path, _path); + + return result; + +#else /* not WITHOUT_FILESYSTEM */ + return 0L; +#endif /* not WITHOUT_FILESYSTEM */ +} + +/*************************************************************************/ + +JNIEXPORT jlong JNICALL +Java_java_io_VMFile_getUsableSpace (JNIEnv *env, + jclass clazz __attribute__ ((__unused__)), + jstring path) +{ +#ifndef WITHOUT_FILESYSTEM + + jlong result; + const char *_path = NULL; + + _path = (*env)->GetStringUTFChars (env, path, 0); + if (_path == NULL) + { + return 0L; + } + + result = cpio_df (_path, USABLE); + + (*env)->ReleaseStringUTFChars (env, path, _path); + + return result; + +#else /* not WITHOUT_FILESYSTEM */ + return 0L; +#endif /* not WITHOUT_FILESYSTEM */ +} + /*************************************************************************/ /* diff --git a/native/jni/native-lib/cpio.c b/native/jni/native-lib/cpio.c index 743968bd3..52b45ad11 100644 --- a/native/jni/native-lib/cpio.c +++ b/native/jni/native-lib/cpio.c @@ -71,6 +71,10 @@ exception statement from your version. */ #include <sys/select.h> #endif +#if defined(HAVE_STATVFS) +#include <sys/statvfs.h> +#endif + #include <utime.h> #include "cpnative.h" @@ -395,6 +399,43 @@ int cpio_chmod (const char *filename, int permissions) return 0; } +JNIEXPORT long long +cpio_df (const char *path, CPFILE_DF_TYPE type) +{ + long long result = 0L; + +#if defined(HAVE_STATVFS) + + long long scale_factor = 0L; + struct statvfs buf; + + if (statvfs (path, &buf) < 0) + return 0L; + + /* f_blocks, f_bfree and f_bavail are defined in terms of f_frsize */ + scale_factor = (long long) (buf.f_frsize); + + switch (type) + { + case TOTAL: + result = (long long) (buf.f_blocks * scale_factor); + break; + case FREE: + result = (long long) (buf.f_bfree * scale_factor); + break; + case USABLE: + result = (long long) (buf.f_bavail * scale_factor); + break; + default: + result = 0L; + break; + } + +#endif + + return result; +} + int cpio_checkAccess (const char *filename, unsigned int flag) { struct stat statbuf; @@ -544,7 +585,6 @@ int cpio_readDir (void *handle, char *filename) return 0; } - int cpio_closeOnExec(int fd) { diff --git a/native/jni/native-lib/cpio.h b/native/jni/native-lib/cpio.h index 259fc62cf..a42fe62db 100644 --- a/native/jni/native-lib/cpio.h +++ b/native/jni/native-lib/cpio.h @@ -83,6 +83,15 @@ JNIEXPORT int cpio_removeFile (const char *filename); JNIEXPORT int cpio_mkdir (const char *filename); JNIEXPORT int cpio_rename (const char *old_name, const char *new_name); +/* to be used with cpio_df */ +typedef enum { + TOTAL = 0, + FREE, + USABLE +} CPFILE_DF_TYPE; + +JNIEXPORT long long cpio_df (const char *path, CPFILE_DF_TYPE type); + JNIEXPORT int cpio_openDir (const char *dirname, void **handle); JNIEXPORT int cpio_closeDir (void *handle); JNIEXPORT int cpio_readDir (void *handle, char *filename); diff --git a/vm/reference/java/io/VMFile.java b/vm/reference/java/io/VMFile.java index 4f708a4a5..4e997b2fb 100644 --- a/vm/reference/java/io/VMFile.java +++ b/vm/reference/java/io/VMFile.java @@ -117,6 +117,21 @@ final class VMFile static native boolean mkdir(String dirpath); /** + * Gets the total bytes of the filesystem named by path. + */ + public static native long getTotalSpace(String path); + + /** + * Gets the total free bytes of the filesystem named by path. + */ + public static native long getFreeSpace(String path); + + /** + * Gets the available bytes of the filesystem named by path. + */ + public static native long getUsableSpace(String path); + + /** * Set the read permission of the file. */ public static synchronized native boolean setReadable(String path, |