jdk/src/solaris/native/java/io/UnixFileSystem_md.c
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6850 56966b0a6a0d
child 12047 320a714614e9
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6850
diff changeset
     2
 * Copyright (c) 1998, 2010, 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>
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;
6850
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 5506
diff changeset
   122
    int mode = 0;
2
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) {
6850
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 5506
diff changeset
   154
        int amode = 0;
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 5506
diff changeset
   155
        int mode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        switch (access) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        case java_io_FileSystem_ACCESS_READ:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (owneronly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                amode = S_IRUSR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                amode = S_IRUSR | S_IRGRP | S_IROTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        case java_io_FileSystem_ACCESS_WRITE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (owneronly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                amode = S_IWUSR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                amode = S_IWUSR | S_IWGRP | S_IWOTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        case java_io_FileSystem_ACCESS_EXECUTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (owneronly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                amode = S_IXUSR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                amode = S_IXUSR | S_IXGRP | S_IXOTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            assert(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (statMode(path, &mode)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (enable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                mode |= amode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                mode &= ~amode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (chmod(path, mode) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
Java_java_io_UnixFileSystem_getLastModifiedTime(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                                jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    jlong rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    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
   198
        struct stat64 sb;
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   199
        if (stat64(path, &sb) == 0) {
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   200
            rv = 1000 * (jlong)sb.st_mtime;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
Java_java_io_UnixFileSystem_getLength(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                      jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    jlong rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    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
   214
        struct stat64 sb;
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   215
        if (stat64(path, &sb) == 0) {
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   216
            rv = sb.st_size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
/* -- File operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
Java_java_io_UnixFileSystem_createFileExclusively(JNIEnv *env, jclass cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                                  jstring pathname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    WITH_PLATFORM_STRING(env, pathname, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (!strcmp (path, "/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            fd = JVM_EEXIST;    /* The root directory always exists */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            fd = JVM_Open(path, JVM_O_RDWR | JVM_O_CREAT | JVM_O_EXCL, 0666);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            if (fd != JVM_EEXIST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                JNU_ThrowIOExceptionWithLastError(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            JVM_Close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
Java_java_io_UnixFileSystem_delete0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                    jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (remove(path) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
Java_java_io_UnixFileSystem_list(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                 jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    DIR *dir = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    struct dirent64 *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    struct dirent64 *result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    int len, maxlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    jobjectArray rv, old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        dir = opendir(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    if (dir == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    ptr = malloc(sizeof(struct dirent64) + (PATH_MAX + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    if (ptr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        closedir(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /* Allocate an initial String array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    maxlen = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    rv = (*env)->NewObjectArray(env, maxlen, JNU_ClassString(env), NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    if (rv == NULL) goto error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /* Scan the directory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    while ((readdir64_r(dir, ptr, &result) == 0)  && (result != NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        jstring name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, ".."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (len == maxlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            old = rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            rv = (*env)->NewObjectArray(env, maxlen <<= 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                        JNU_ClassString(env), NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (rv == NULL) goto error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            (*env)->DeleteLocalRef(env, old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        name = JNU_NewStringPlatform(env, ptr->d_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (name == NULL) goto error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        (*env)->SetObjectArrayElement(env, rv, len++, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        (*env)->DeleteLocalRef(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    closedir(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /* Copy the final results into an appropriately-sized array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    old = rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    rv = (*env)->NewObjectArray(env, len, JNU_ClassString(env), NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    if (rv == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    if (JNU_CopyObjectArray(env, rv, old, len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
 error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    closedir(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
Java_java_io_UnixFileSystem_createDirectory(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                            jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (mkdir(path, 0777) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
Java_java_io_UnixFileSystem_rename0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                    jobject from, jobject to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    WITH_FIELD_PLATFORM_STRING(env, from, ids.path, fromPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        WITH_FIELD_PLATFORM_STRING(env, to, ids.path, toPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (rename(fromPath, toPath) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        } END_PLATFORM_STRING(env, toPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    } END_PLATFORM_STRING(env, fromPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
Java_java_io_UnixFileSystem_setLastModifiedTime(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                                jobject file, jlong time)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    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
   372
        struct stat64 sb;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
692
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   374
        if (stat64(path, &sb) == 0) {
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   375
            struct timeval tv[2];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
692
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   377
            /* Preserve access time */
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   378
            tv[0].tv_sec = sb.st_atime;
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   379
            tv[0].tv_usec = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
692
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   381
            /* Change last-modified time */
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   382
            tv[1].tv_sec = time / 1000;
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   383
            tv[1].tv_usec = (time % 1000) * 1000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
692
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   385
            if (utimes(path, tv) == 0)
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   386
                rv = JNI_TRUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
Java_java_io_UnixFileSystem_setReadOnly(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                                        jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        int mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (statMode(path, &mode)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (chmod(path, mode & ~(S_IWUSR | S_IWGRP | S_IWOTH)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
Java_java_io_UnixFileSystem_getSpace(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                                     jobject file, jint t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    jlong rv = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
4981
3a3f498acc86 6815768: File.getxxxSpace() methods fail for very large file systems under 32bit Java
alanb
parents: 715
diff changeset
   418
        struct statvfs64 fsstat;
3a3f498acc86 6815768: File.getxxxSpace() methods fail for very large file systems under 32bit Java
alanb
parents: 715
diff changeset
   419
        memset(&fsstat, 0, sizeof(fsstat));
3a3f498acc86 6815768: File.getxxxSpace() methods fail for very large file systems under 32bit Java
alanb
parents: 715
diff changeset
   420
        if (statvfs64(path, &fsstat) == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            switch(t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            case java_io_FileSystem_SPACE_TOTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                               long_to_jlong(fsstat.f_blocks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            case java_io_FileSystem_SPACE_FREE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                               long_to_jlong(fsstat.f_bfree));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            case java_io_FileSystem_SPACE_USABLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                               long_to_jlong(fsstat.f_bavail));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                assert(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
}