jdk/src/java.base/unix/native/libjava/UnixFileSystem_md.c
author bpb
Fri, 12 Jun 2015 17:05:26 -0700
changeset 31144 8154c9d7bc32
parent 25859 3317bb8137f4
child 42777 a94fc33e9866
permissions -rw-r--r--
8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems Summary: On Mac OS X replace statvfs64() and struct statvfs64 with statfs() and struct statfs, respectively, and f_frsize with f_bsize. Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31144
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
     2
 * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4981
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4981
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4981
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4981
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4981
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <sys/time.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#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
    30
#ifdef MACOSX
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
    31
#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
    32
#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
    33
#else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#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
    35
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <dlfcn.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <limits.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#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
    46
#include "io_util_md.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include "java_io_FileSystem.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#include "java_io_UnixFileSystem.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    50
#if defined(_ALLBSD_SOURCE)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    51
#define dirent64 dirent
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    52
#define readdir64_r readdir_r
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    53
#define stat64 stat
31144
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
    54
#ifndef MACOSX
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    55
#define statvfs64 statvfs
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    56
#endif
31144
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
    57
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/* -- Field IDs -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
static struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    jfieldID path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
} ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
Java_java_io_UnixFileSystem_initIDs(JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    jclass fileClass = (*env)->FindClass(env, "java/io/File");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    if (!fileClass) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    ids.path = (*env)->GetFieldID(env, fileClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                  "path", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/* -- Path operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
extern int canonicalize(char *path, const char *out, int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
Java_java_io_UnixFileSystem_canonicalize0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                          jstring pathname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    jstring rv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    WITH_PLATFORM_STRING(env, pathname, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        char canonicalPath[JVM_MAXPATHLEN];
16476
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    87
        if (canonicalize((char *)path,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                         canonicalPath, JVM_MAXPATHLEN) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        } 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
    91
#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
    92
            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
    93
#else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            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
    95
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
/* -- Attribute accessors -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
statMode(const char *path, int *mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
{
692
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   108
    struct stat64 sb;
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   109
    if (stat64(path, &sb) == 0) {
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   110
        *mode = sb.st_mode;
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   111
        return JNI_TRUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
Java_java_io_UnixFileSystem_getBooleanAttributes0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                                  jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    jint rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        int mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (statMode(path, &mode)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            int fmt = mode & S_IFMT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            rv = (jint) (java_io_FileSystem_BA_EXISTS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                  | ((fmt == S_IFREG) ? java_io_FileSystem_BA_REGULAR : 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                  | ((fmt == S_IFDIR) ? java_io_FileSystem_BA_DIRECTORY : 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
Java_java_io_UnixFileSystem_checkAccess(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                        jobject file, jint a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    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
   140
    int mode = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    switch (a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    case java_io_FileSystem_ACCESS_READ:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        mode = R_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    case java_io_FileSystem_ACCESS_WRITE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        mode = W_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    case java_io_FileSystem_ACCESS_EXECUTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        mode = X_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    default: assert(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (access(path, mode) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
Java_java_io_UnixFileSystem_setPermission(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                          jobject file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                          jint access,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                          jboolean enable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                          jboolean owneronly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    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
   172
        int amode = 0;
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 5506
diff changeset
   173
        int mode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        switch (access) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        case java_io_FileSystem_ACCESS_READ:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (owneronly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                amode = S_IRUSR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                amode = S_IRUSR | S_IRGRP | S_IROTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        case java_io_FileSystem_ACCESS_WRITE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (owneronly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                amode = S_IWUSR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                amode = S_IWUSR | S_IWGRP | S_IWOTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        case java_io_FileSystem_ACCESS_EXECUTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (owneronly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                amode = S_IXUSR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                amode = S_IXUSR | S_IXGRP | S_IXOTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            assert(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (statMode(path, &mode)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (enable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                mode |= amode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                mode &= ~amode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (chmod(path, mode) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
Java_java_io_UnixFileSystem_getLastModifiedTime(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                                jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    jlong rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    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
   216
        struct stat64 sb;
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   217
        if (stat64(path, &sb) == 0) {
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   218
            rv = 1000 * (jlong)sb.st_mtime;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
Java_java_io_UnixFileSystem_getLength(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                      jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    jlong rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    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
   232
        struct stat64 sb;
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   233
        if (stat64(path, &sb) == 0) {
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   234
            rv = sb.st_size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
/* -- File operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
Java_java_io_UnixFileSystem_createFileExclusively(JNIEnv *env, jclass cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                                  jstring pathname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    WITH_PLATFORM_STRING(env, pathname, path) {
16476
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   251
        FD fd;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   252
        /* The root directory always exists */
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   253
        if (strcmp (path, "/")) {
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   254
            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
   255
            if (fd < 0) {
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   256
                if (errno != EEXIST)
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   257
                    JNU_ThrowIOExceptionWithLastError(env, path);
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   258
            } else {
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   259
                if (close(fd) == -1)
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   260
                    JNU_ThrowIOExceptionWithLastError(env, path);
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   261
                rv = JNI_TRUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
Java_java_io_UnixFileSystem_delete0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                    jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (remove(path) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
Java_java_io_UnixFileSystem_list(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                 jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    DIR *dir = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    struct dirent64 *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    struct dirent64 *result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    int len, maxlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    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
   293
    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
   294
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 16476
diff changeset
   295
    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
   296
    CHECK_NULL_RETURN(str_class, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        dir = opendir(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    if (dir == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    ptr = malloc(sizeof(struct dirent64) + (PATH_MAX + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    if (ptr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        closedir(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /* Allocate an initial String array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    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
   313
    rv = (*env)->NewObjectArray(env, maxlen, str_class, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    if (rv == NULL) goto error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /* Scan the directory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    while ((readdir64_r(dir, ptr, &result) == 0)  && (result != NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        jstring name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, ".."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (len == maxlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            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
   323
            rv = (*env)->NewObjectArray(env, maxlen <<= 1, str_class, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (rv == NULL) goto error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            (*env)->DeleteLocalRef(env, old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
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
   328
#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
   329
        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
   330
#else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        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
   332
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (name == NULL) goto error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        (*env)->SetObjectArrayElement(env, rv, len++, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        (*env)->DeleteLocalRef(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    closedir(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /* Copy the final results into an appropriately-sized array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    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
   342
    rv = (*env)->NewObjectArray(env, len, str_class, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    if (rv == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    if (JNU_CopyObjectArray(env, rv, old, len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
 error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    closedir(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
Java_java_io_UnixFileSystem_createDirectory(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                            jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (mkdir(path, 0777) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
Java_java_io_UnixFileSystem_rename0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                    jobject from, jobject to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    WITH_FIELD_PLATFORM_STRING(env, from, ids.path, fromPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        WITH_FIELD_PLATFORM_STRING(env, to, ids.path, toPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (rename(fromPath, toPath) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        } END_PLATFORM_STRING(env, toPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    } END_PLATFORM_STRING(env, fromPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
Java_java_io_UnixFileSystem_setLastModifiedTime(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                                jobject file, jlong time)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    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
   396
        struct stat64 sb;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
692
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   398
        if (stat64(path, &sb) == 0) {
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   399
            struct timeval tv[2];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
692
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   401
            /* Preserve access time */
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   402
            tv[0].tv_sec = sb.st_atime;
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   403
            tv[0].tv_usec = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
692
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   405
            /* Change last-modified time */
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   406
            tv[1].tv_sec = time / 1000;
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   407
            tv[1].tv_usec = (time % 1000) * 1000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
692
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   409
            if (utimes(path, tv) == 0)
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   410
                rv = JNI_TRUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
Java_java_io_UnixFileSystem_setReadOnly(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                                        jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        int mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (statMode(path, &mode)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (chmod(path, mode & ~(S_IWUSR | S_IWGRP | S_IWOTH)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
Java_java_io_UnixFileSystem_getSpace(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                                     jobject file, jint t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    jlong rv = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    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
   442
#ifdef MACOSX
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
   443
        struct statfs fsstat;
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
   444
#else
4981
3a3f498acc86 6815768: File.getxxxSpace() methods fail for very large file systems under 32bit Java
alanb
parents: 715
diff changeset
   445
        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
   446
#endif
4981
3a3f498acc86 6815768: File.getxxxSpace() methods fail for very large file systems under 32bit Java
alanb
parents: 715
diff changeset
   447
        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
   448
#ifdef MACOSX
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
   449
        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
   450
            switch(t) {
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
   451
                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
   452
                    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
   453
                                   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
   454
                    break;
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
   455
                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
   456
                    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
   457
                                   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
   458
                    break;
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
   459
                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
   460
                    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
   461
                                   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
   462
                    break;
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
   463
                default:
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
   464
                    assert(0);
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
   465
            }
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
   466
        }
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
   467
#else
4981
3a3f498acc86 6815768: File.getxxxSpace() methods fail for very large file systems under 32bit Java
alanb
parents: 715
diff changeset
   468
        if (statvfs64(path, &fsstat) == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            switch(t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            case java_io_FileSystem_SPACE_TOTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                               long_to_jlong(fsstat.f_blocks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            case java_io_FileSystem_SPACE_FREE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                               long_to_jlong(fsstat.f_bfree));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            case java_io_FileSystem_SPACE_USABLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                               long_to_jlong(fsstat.f_bavail));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                assert(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
31144
8154c9d7bc32 8087288: File.get{Free,Total,Usable}Space may return unexpected results with >2TB file systems
bpb
parents: 25859
diff changeset
   486
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
}