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