jdk/src/windows/native/java/io/Win32FileSystem_md.c
author sherman
Wed, 25 Jun 2008 13:58:22 -0700
changeset 785 36c29b2692f1
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win) Summary: Uncanonicalized absolute filepath with length 248-260 no longer works (win) Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
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 <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <direct.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <io.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "io_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include "dirent_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include "java_io_FileSystem.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/* This macro relies upon the fact that JNU_GetStringPlatformChars always makes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
   a copy of the string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#define WITH_NATIVE_PATH(env, object, id, var)                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    WITH_FIELD_PLATFORM_STRING(env, object, id, var)                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        JVM_NativePath((char *)var);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#define END_NATIVE_PATH(env, var)    END_PLATFORM_STRING(env, var)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
static struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    jfieldID path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
} ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
Java_java_io_Win32FileSystem_initIDs(JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    jclass fileClass = (*env)->FindClass(env, "java/io/File");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    if (!fileClass) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    ids.path = (*env)->GetFieldID(env, fileClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                  "path", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
/* -- Path operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
extern int canonicalize(char *path, const char *out, int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
extern int canonicalizeWithPrefix(const char* canonicalPrefix, const char *pathWithCanonicalPrefix, char *out, int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
Java_java_io_Win32FileSystem_canonicalize0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                           jstring pathname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    jstring rv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    WITH_PLATFORM_STRING(env, pathname, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        char canonicalPath[JVM_MAXPATHLEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (canonicalize(JVM_NativePath((char *)path),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                         canonicalPath, JVM_MAXPATHLEN) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            rv = JNU_NewStringPlatform(env, canonicalPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
Java_java_io_Win32FileSystem_canonicalizeWithPrefix0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                                     jstring canonicalPrefixString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                                     jstring pathWithCanonicalPrefixString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    jstring rv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    char canonicalPath[JVM_MAXPATHLEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    WITH_PLATFORM_STRING(env, canonicalPrefixString, canonicalPrefix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        WITH_PLATFORM_STRING(env, pathWithCanonicalPrefixString, pathWithCanonicalPrefix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (canonicalizeWithPrefix(canonicalPrefix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                       pathWithCanonicalPrefix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                       canonicalPath, JVM_MAXPATHLEN) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                rv = JNU_NewStringPlatform(env, canonicalPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } END_PLATFORM_STRING(env, pathWithCanonicalPrefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    } END_PLATFORM_STRING(env, canonicalPrefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
/* -- Attribute accessors -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
/* Check whether or not the file name in "path" is a Windows reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
   device name (CON, PRN, AUX, NUL, COM[1-9], LPT[1-9]) based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
   returned result from GetFullPathName. If the file name in the path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
   is indeed a reserved device name GetFuulPathName returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
   "\\.\[ReservedDeviceName]".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
BOOL isReservedDeviceName(const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
#define BUFSIZE 9
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    char buf[BUFSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    char *lpf = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    DWORD retLen = GetFullPathName(path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                   BUFSIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                   buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                   &lpf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    if ((retLen == BUFSIZE - 1 || retLen == BUFSIZE - 2) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        buf[0] == '\\' && buf[1] == '\\' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        buf[2] == '.' && buf[3] == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        char* dname = _strupr(buf + 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (strcmp(dname, "CON") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            strcmp(dname, "PRN") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            strcmp(dname, "AUX") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            strcmp(dname, "NUL") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if ((strncmp(dname, "COM", 3) == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
             strncmp(dname, "LPT", 3) == 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            dname[3] - '0' > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            dname[3] - '0' <= 9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
Java_java_io_Win32FileSystem_getBooleanAttributes(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                                  jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    jint rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    WITH_NATIVE_PATH(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        WIN32_FILE_ATTRIBUTE_DATA wfad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (!isReservedDeviceName(path) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            GetFileAttributesEx(path, GetFileExInfoStandard, &wfad)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            rv = (java_io_FileSystem_BA_EXISTS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                  | ((wfad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                     ? java_io_FileSystem_BA_DIRECTORY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                     : java_io_FileSystem_BA_REGULAR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                  | ((wfad.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                     ? java_io_FileSystem_BA_HIDDEN : 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    } END_NATIVE_PATH(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
Java_java_io_Win32FileSystem_checkAccess(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                         jobject file, jint a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    int mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    switch (a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    case java_io_FileSystem_ACCESS_READ:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    case java_io_FileSystem_ACCESS_EXECUTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        mode = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    case java_io_FileSystem_ACCESS_WRITE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        mode = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    default: assert(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    WITH_NATIVE_PATH(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (access(path, mode) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    } END_NATIVE_PATH(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
Java_java_io_Win32FileSystem_setPermission(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                           jobject file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                           jint access,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                           jboolean enable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                           jboolean owneronly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    if (access == java_io_FileSystem_ACCESS_READ ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        access == java_io_FileSystem_ACCESS_EXECUTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return enable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    WITH_NATIVE_PATH(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        DWORD a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        a = GetFileAttributes(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (a != INVALID_FILE_ATTRIBUTES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (enable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                a =  a & ~FILE_ATTRIBUTE_READONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                a =  a | FILE_ATTRIBUTE_READONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (SetFileAttributes(path, a))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    } END_NATIVE_PATH(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
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
Java_java_io_Win32FileSystem_getLastModifiedTime(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                                 jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    jlong rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    WITH_NATIVE_PATH(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /* Win95, Win98, WinME */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    WIN32_FIND_DATA fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    jlong temp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    LARGE_INTEGER modTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    HANDLE h = FindFirstFile(path, &fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        FindClose(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        modTime.LowPart = (DWORD) fd.ftLastWriteTime.dwLowDateTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        modTime.HighPart = (LONG) fd.ftLastWriteTime.dwHighDateTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        rv = modTime.QuadPart / 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        rv -= 11644473600000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    } END_NATIVE_PATH(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    return rv;
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 jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
Java_java_io_Win32FileSystem_getLength(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                       jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    jlong rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    WITH_NATIVE_PATH(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        struct _stati64 sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (_stati64(path, &sb) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            rv = sb.st_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    } END_NATIVE_PATH(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
/* -- File operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
Java_java_io_Win32FileSystem_createFileExclusively(JNIEnv *env, jclass cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                                   jstring pathname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    DWORD a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    WITH_PLATFORM_STRING(env, pathname, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        int orv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        int error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        JVM_NativePath((char *)path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        orv = JVM_Open(path, JVM_O_RDWR | JVM_O_CREAT | JVM_O_EXCL, 0666);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (orv < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (orv != JVM_EEXIST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                // If a directory by the named path already exists,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                // return false (behavior of solaris and linux) instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                // throwing an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                a = GetFileAttributes(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                if ((a == INVALID_FILE_ATTRIBUTES) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                        !(a & FILE_ATTRIBUTE_DIRECTORY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    SetLastError(error);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    JNU_ThrowIOExceptionWithLastError(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            JVM_Close(orv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    } END_PLATFORM_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
removeFileOrDirectory(const char *path) /* Returns 0 on success */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    DWORD a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    SetFileAttributes(path, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    a = GetFileAttributes(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    if (a == INVALID_FILE_ATTRIBUTES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    } else if (a & FILE_ATTRIBUTE_DIRECTORY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return !RemoveDirectory(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return !DeleteFile(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
Java_java_io_Win32FileSystem_delete0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                     jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    WITH_NATIVE_PATH(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (removeFileOrDirectory(path) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    } END_NATIVE_PATH(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
/* ## Clean this up to use direct Win32 calls */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
Java_java_io_Win32FileSystem_list(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
    DIR *dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    struct dirent *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    int len, maxlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    jobjectArray rv, old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    WITH_NATIVE_PATH(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        dir = opendir(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    } END_NATIVE_PATH(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    if (dir == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /* Allocate an initial String array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    maxlen = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    rv = (*env)->NewObjectArray(env, maxlen, JNU_ClassString(env), NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    if (rv == NULL) goto error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /* Scan the directory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    while ((ptr = readdir(dir)) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        jstring name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, ".."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (len == maxlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            old = rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            rv = (*env)->NewObjectArray(env, maxlen <<= 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                        JNU_ClassString(env), NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            if (rv == NULL) goto error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            (*env)->DeleteLocalRef(env, old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        name = JNU_NewStringPlatform(env, ptr->d_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (name == NULL) goto error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        (*env)->SetObjectArrayElement(env, rv, len++, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        (*env)->DeleteLocalRef(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    closedir(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /* Copy the final results into an appropriately-sized array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    old = rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    rv = (*env)->NewObjectArray(env, len, JNU_ClassString(env), NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    if (rv == NULL) goto error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
 error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    closedir(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
Java_java_io_Win32FileSystem_createDirectory(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                                             jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    WITH_NATIVE_PATH(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (mkdir(path) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    } END_NATIVE_PATH(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
Java_java_io_Win32FileSystem_rename0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                                     jobject from, jobject to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    WITH_NATIVE_PATH(env, from, ids.path, fromPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        WITH_NATIVE_PATH(env, to, ids.path, toPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (rename(fromPath, toPath) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        } END_NATIVE_PATH(env, toPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    } END_NATIVE_PATH(env, fromPath);
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_Win32FileSystem_setLastModifiedTime(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                                                 jobject file, jlong time)
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_NATIVE_PATH(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        HANDLE h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        h = CreateFile(path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                       FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            LARGE_INTEGER modTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            FILETIME t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            modTime.QuadPart = (time + 11644473600000L) * 10000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            t.dwLowDateTime = (DWORD)modTime.LowPart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            t.dwHighDateTime = (DWORD)modTime.HighPart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if (SetFileTime(h, NULL, NULL, &t)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            CloseHandle(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    } END_NATIVE_PATH(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
Java_java_io_Win32FileSystem_setReadOnly(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                                         jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    WITH_NATIVE_PATH(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        DWORD a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        a = GetFileAttributes(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (a != INVALID_FILE_ATTRIBUTES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if (SetFileAttributes(path, a | FILE_ATTRIBUTE_READONLY))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    } END_NATIVE_PATH(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
/* -- Filesystem interface -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
Java_java_io_Win32FileSystem_getDriveDirectory(JNIEnv *env, jclass ignored,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                                               jint drive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    char buf[_MAX_PATH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    char *p = _getdcwd(drive, buf, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    if (p == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    if (isalpha(*p) && (p[1] == ':')) p += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    return JNU_NewStringPlatform(env, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
Java_java_io_Win32FileSystem_listRoots0(JNIEnv *env, jclass ignored)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    return GetLogicalDrives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
Java_java_io_Win32FileSystem_getSpace0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                       jobject file, jint t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    jlong rv = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    WITH_NATIVE_PATH(env, file, ids.path, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        ULARGE_INTEGER totalSpace, freeSpace, usableSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (GetDiskFreeSpaceEx(path, &usableSpace, &totalSpace, &freeSpace)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            switch(t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            case java_io_FileSystem_SPACE_TOTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                rv = long_to_jlong(totalSpace.QuadPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            case java_io_FileSystem_SPACE_FREE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                rv = long_to_jlong(freeSpace.QuadPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            case java_io_FileSystem_SPACE_USABLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                rv = long_to_jlong(usableSpace.QuadPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                assert(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    } END_NATIVE_PATH(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
}