jdk/src/windows/native/java/io/WinNTFileSystem_md.c
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8186 d0e2cc8b3073
child 9050 26c2c1de1631
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8186
diff changeset
     2
 * Copyright (c) 2001, 2011, 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: 3627
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: 3627
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: 3627
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3627
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3627
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
/* Access APIs for Win2K and above */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#ifndef _WIN32_WINNT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#define _WIN32_WINNT 0x0500
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <direct.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <io.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "io_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include "io_util_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include "dirent_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#include "java_io_FileSystem.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#define MAX_PATH_LENGTH 1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
static struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    jfieldID path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
} ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    54
/**
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    55
 * GetFinalPathNameByHandle is available on Windows Vista and newer
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    56
 */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    57
typedef BOOL (WINAPI* GetFinalPathNameByHandleProc) (HANDLE, LPWSTR, DWORD, DWORD);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    58
static GetFinalPathNameByHandleProc GetFinalPathNameByHandle_func;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    59
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
Java_java_io_WinNTFileSystem_initIDs(JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
{
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    63
    HANDLE handle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    jclass fileClass = (*env)->FindClass(env, "java/io/File");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    if (!fileClass) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    ids.path =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
             (*env)->GetFieldID(env, fileClass, "path", "Ljava/lang/String;");
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    68
    handle = LoadLibrary("kernel32");
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    69
    if (handle != NULL) {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    70
        GetFinalPathNameByHandle_func = (GetFinalPathNameByHandleProc)
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    71
            GetProcAddress(handle, "GetFinalPathNameByHandleW");
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    72
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/* -- Path operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
extern int wcanonicalize(const WCHAR *path, WCHAR *out, int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
extern int wcanonicalizeWithPrefix(const WCHAR *canonicalPrefix, const WCHAR *pathWithCanonicalPrefix, WCHAR *out, int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    80
/**
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    81
 * Retrieves the fully resolved (final) path for the given path or NULL
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    82
 * if the function fails.
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    83
 */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    84
static WCHAR* getFinalPath(const WCHAR *path)
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    85
{
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    86
    HANDLE h;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    87
    WCHAR *result;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    88
    DWORD error;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    89
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    90
    /* Need Windows Vista or newer to get the final path */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    91
    if (GetFinalPathNameByHandle_func == NULL)
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    92
        return NULL;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    93
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    94
    h = CreateFileW(path,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    95
                    FILE_READ_ATTRIBUTES,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    96
                    FILE_SHARE_DELETE |
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    97
                        FILE_SHARE_READ | FILE_SHARE_WRITE,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    98
                    NULL,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
    99
                    OPEN_EXISTING,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   100
                    FILE_FLAG_BACKUP_SEMANTICS,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   101
                    NULL);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   102
    if (h == INVALID_HANDLE_VALUE)
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   103
        return NULL;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   104
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   105
    /**
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   106
     * Allocate a buffer for the resolved path. For a long path we may need
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   107
     * to allocate a larger buffer.
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   108
     */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   109
    result = (WCHAR*)malloc(MAX_PATH * sizeof(WCHAR));
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   110
    if (result != NULL) {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   111
        DWORD len = (*GetFinalPathNameByHandle_func)(h, result, MAX_PATH, 0);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   112
        if (len >= MAX_PATH) {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   113
            /* retry with a buffer of the right size */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   114
            result = (WCHAR*)realloc(result, (len+1) * sizeof(WCHAR));
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   115
            if (result != NULL) {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   116
                len = (*GetFinalPathNameByHandle_func)(h, result, len, 0);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   117
            } else {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   118
                len = 0;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   119
            }
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   120
        }
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   121
        if (len > 0) {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   122
            /**
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   123
             * Strip prefix (should be \\?\ or \\?\UNC)
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   124
             */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   125
            if (result[0] == L'\\' && result[1] == L'\\' &&
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   126
                result[2] == L'?' && result[3] == L'\\')
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   127
            {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   128
                int isUnc = (result[4] == L'U' &&
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   129
                             result[5] == L'N' &&
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   130
                             result[6] == L'C');
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   131
                int prefixLen = (isUnc) ? 7 : 4;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   132
                /* actual result length (includes terminator) */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   133
                int resultLen = len - prefixLen + (isUnc ? 1 : 0) + 1;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   134
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   135
                /* copy result without prefix into new buffer */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   136
                WCHAR *tmp = (WCHAR*)malloc(resultLen * sizeof(WCHAR));
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   137
                if (tmp == NULL) {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   138
                    len = 0;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   139
                } else {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   140
                    WCHAR *p = result;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   141
                    p += prefixLen;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   142
                    if (isUnc) {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   143
                        WCHAR *p2 = tmp;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   144
                        p2[0] = L'\\';
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   145
                        p2++;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   146
                        wcscpy(p2, p);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   147
                    } else {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   148
                        wcscpy(tmp, p);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   149
                    }
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   150
                    free(result);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   151
                    result = tmp;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   152
                }
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   153
            }
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   154
        }
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   155
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   156
        /* unable to get final path */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   157
        if (len == 0 && result != NULL) {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   158
            free(result);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   159
            result = NULL;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   160
        }
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   161
    }
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   162
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   163
    error = GetLastError();
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   164
    if (CloseHandle(h))
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   165
        SetLastError(error);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   166
    return result;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   167
}
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   168
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   169
/**
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   170
 * Retrieves file information for the specified file. If the file is
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   171
 * symbolic link then the information on fully resolved target is
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   172
 * returned.
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   173
 */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   174
static BOOL getFileInformation(const WCHAR *path,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   175
                               BY_HANDLE_FILE_INFORMATION *finfo)
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   176
{
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   177
    BOOL result;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   178
    DWORD error;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   179
    HANDLE h = CreateFileW(path,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   180
                           FILE_READ_ATTRIBUTES,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   181
                           FILE_SHARE_DELETE |
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   182
                               FILE_SHARE_READ | FILE_SHARE_WRITE,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   183
                           NULL,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   184
                           OPEN_EXISTING,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   185
                           FILE_FLAG_BACKUP_SEMANTICS,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   186
                           NULL);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   187
    if (h == INVALID_HANDLE_VALUE)
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   188
        return FALSE;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   189
    result = GetFileInformationByHandle(h, finfo);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   190
    error = GetLastError();
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   191
    if (CloseHandle(h))
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   192
        SetLastError(error);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   193
    return result;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   194
}
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   195
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   196
/**
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   197
 * If the given attributes are the attributes of a reparse point, then
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   198
 * read and return the attributes of the final target.
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   199
 */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   200
DWORD getFinalAttributesIfReparsePoint(WCHAR *path, DWORD a)
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   201
{
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   202
    if ((a != INVALID_FILE_ATTRIBUTES) &&
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   203
        ((a & FILE_ATTRIBUTE_REPARSE_POINT) != 0))
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   204
    {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   205
        BY_HANDLE_FILE_INFORMATION finfo;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   206
        BOOL res = getFileInformation(path, &finfo);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   207
        a = (res) ? finfo.dwFileAttributes : INVALID_FILE_ATTRIBUTES;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   208
    }
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   209
    return a;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   210
}
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   211
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
Java_java_io_WinNTFileSystem_canonicalize0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                           jstring pathname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    jstring rv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    WCHAR canonicalPath[MAX_PATH_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    WITH_UNICODE_STRING(env, pathname, path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        /*we estimate the max length of memory needed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
          "currentDir. length + pathname.length"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         */
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   223
        int len = (int)wcslen(path);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        len += currentDirLength(path, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (len  > MAX_PATH_LENGTH - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            WCHAR *cp = (WCHAR*)malloc(len * sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (cp != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                if (wcanonicalize(path, cp, len) >= 0) {
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   229
                    rv = (*env)->NewString(env, cp, (jsize)wcslen(cp));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                free(cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (wcanonicalize(path, canonicalPath, MAX_PATH_LENGTH) >= 0) {
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   235
            rv = (*env)->NewString(env, canonicalPath, (jsize)wcslen(canonicalPath));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    } END_UNICODE_STRING(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    if (rv == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
Java_java_io_WinNTFileSystem_canonicalizeWithPrefix0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                                     jstring canonicalPrefixString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                                     jstring pathWithCanonicalPrefixString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    jstring rv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    WCHAR canonicalPath[MAX_PATH_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    WITH_UNICODE_STRING(env, canonicalPrefixString, canonicalPrefix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        WITH_UNICODE_STRING(env, pathWithCanonicalPrefixString, pathWithCanonicalPrefix) {
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   254
            int len = (int)wcslen(canonicalPrefix) + MAX_PATH;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if (len > MAX_PATH_LENGTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                WCHAR *cp = (WCHAR*)malloc(len * sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                if (cp != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    if (wcanonicalizeWithPrefix(canonicalPrefix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                                pathWithCanonicalPrefix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                                cp, len) >= 0) {
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   261
                      rv = (*env)->NewString(env, cp, (jsize)wcslen(cp));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    free(cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (wcanonicalizeWithPrefix(canonicalPrefix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                        pathWithCanonicalPrefix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                        canonicalPath, MAX_PATH_LENGTH) >= 0) {
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   269
                rv = (*env)->NewString(env, canonicalPath, (jsize)wcslen(canonicalPath));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } END_UNICODE_STRING(env, pathWithCanonicalPrefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    } END_UNICODE_STRING(env, canonicalPrefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    if (rv == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
/* -- Attribute accessors -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
/* Check whether or not the file name in "path" is a Windows reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
   device name (CON, PRN, AUX, NUL, COM[1-9], LPT[1-9]) based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
   returned result from GetFullPathName, which should be in thr form of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
   "\\.\[ReservedDeviceName]" if the path represents a reserved device
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
   name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
   Note1: GetFullPathName doesn't think "CLOCK$" (which is no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
   important anyway) is a device name, so we don't check it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
   GetFileAttributesEx will catch it later by returning 0 on NT/XP/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
   200X.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
   Note2: Theoretically the implementation could just lookup the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
   below linearly if the first 4 characters of the fullpath returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
   from GetFullPathName are "\\.\". The current implementation should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
   achieve the same result. If Microsoft add more names into their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
   reserved device name repository in the future, which probably will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
   never happen, we will need to revisit the lookup implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
static WCHAR* ReservedDEviceNames[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    L"CON", L"PRN", L"AUX", L"NUL",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    L"COM1", L"COM2", L"COM3", L"COM4", L"COM5", L"COM6", L"COM7", L"COM8", L"COM9",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    L"LPT1", L"LPT2", L"LPT3", L"LPT4", L"LPT5", L"LPT6", L"LPT7", L"LPT8", L"LPT9",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    L"CLOCK$"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
static BOOL isReservedDeviceNameW(WCHAR* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
#define BUFSIZE 9
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    WCHAR buf[BUFSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    WCHAR *lpf = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    DWORD retLen = GetFullPathNameW(path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                   BUFSIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                   buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                   &lpf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    if ((retLen == BUFSIZE - 1 || retLen == BUFSIZE - 2) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        buf[0] == L'\\' && buf[1] == L'\\' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        buf[2] == L'.' && buf[3] == L'\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        WCHAR* dname = _wcsupr(buf + 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (wcscmp(dname, L"CON") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            wcscmp(dname, L"PRN") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            wcscmp(dname, L"AUX") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            wcscmp(dname, L"NUL") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if ((wcsncmp(dname, L"COM", 3) == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
             wcsncmp(dname, L"LPT", 3) == 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            dname[3] - L'0' > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            dname[3] - L'0' <= 9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
Java_java_io_WinNTFileSystem_getBooleanAttributes(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                                  jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    jint rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    jint pathlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /* both pagefile.sys and hiberfil.sys have length 12 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
#define SPECIALFILE_NAMELEN 12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    WCHAR *pathbuf = fileToNTPath(env, file, ids.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    WIN32_FILE_ATTRIBUTE_DATA wfad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    if (pathbuf == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    if (!isReservedDeviceNameW(pathbuf)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (GetFileAttributesExW(pathbuf, GetFileExInfoStandard, &wfad)) {
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   349
            DWORD a = getFinalAttributesIfReparsePoint(pathbuf, wfad.dwFileAttributes);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   350
            if (a != INVALID_FILE_ATTRIBUTES) {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   351
                rv = (java_io_FileSystem_BA_EXISTS
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   352
                    | ((a & FILE_ATTRIBUTE_DIRECTORY)
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   353
                        ? java_io_FileSystem_BA_DIRECTORY
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   354
                        : java_io_FileSystem_BA_REGULAR)
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   355
                    | ((a & FILE_ATTRIBUTE_HIDDEN)
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   356
                        ? java_io_FileSystem_BA_HIDDEN : 0));
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   357
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        } else { /* pagefile.sys is a special case */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            if (GetLastError() == ERROR_SHARING_VIOLATION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                rv = java_io_FileSystem_BA_EXISTS;
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   361
                if ((pathlen = (jint)wcslen(pathbuf)) >= SPECIALFILE_NAMELEN &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    (_wcsicmp(pathbuf + pathlen - SPECIALFILE_NAMELEN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                              L"pagefile.sys") == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    (_wcsicmp(pathbuf + pathlen - SPECIALFILE_NAMELEN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                              L"hiberfil.sys") == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                  rv |= java_io_FileSystem_BA_REGULAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
JNIEXPORT jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
JNICALL Java_java_io_WinNTFileSystem_checkAccess(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                                 jobject file, jint access)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
{
691
2033c9b86813 4939819: File.canWrite() returns false for the "My Documents" directory (win)
alanb
parents: 2
diff changeset
   379
    DWORD attr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    WCHAR *pathbuf = fileToNTPath(env, file, ids.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    if (pathbuf == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return JNI_FALSE;
691
2033c9b86813 4939819: File.canWrite() returns false for the "My Documents" directory (win)
alanb
parents: 2
diff changeset
   383
    attr = GetFileAttributesW(pathbuf);
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   384
    attr = getFinalAttributesIfReparsePoint(pathbuf, attr);
691
2033c9b86813 4939819: File.canWrite() returns false for the "My Documents" directory (win)
alanb
parents: 2
diff changeset
   385
    free(pathbuf);
2033c9b86813 4939819: File.canWrite() returns false for the "My Documents" directory (win)
alanb
parents: 2
diff changeset
   386
    if (attr == INVALID_FILE_ATTRIBUTES)
2033c9b86813 4939819: File.canWrite() returns false for the "My Documents" directory (win)
alanb
parents: 2
diff changeset
   387
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    switch (access) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    case java_io_FileSystem_ACCESS_READ:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    case java_io_FileSystem_ACCESS_EXECUTE:
691
2033c9b86813 4939819: File.canWrite() returns false for the "My Documents" directory (win)
alanb
parents: 2
diff changeset
   391
        return JNI_TRUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    case java_io_FileSystem_ACCESS_WRITE:
691
2033c9b86813 4939819: File.canWrite() returns false for the "My Documents" directory (win)
alanb
parents: 2
diff changeset
   393
        /* Read-only attribute ignored on directories */
2033c9b86813 4939819: File.canWrite() returns false for the "My Documents" directory (win)
alanb
parents: 2
diff changeset
   394
        if ((attr & FILE_ATTRIBUTE_DIRECTORY) ||
2033c9b86813 4939819: File.canWrite() returns false for the "My Documents" directory (win)
alanb
parents: 2
diff changeset
   395
            (attr & FILE_ATTRIBUTE_READONLY) == 0)
2033c9b86813 4939819: File.canWrite() returns false for the "My Documents" directory (win)
alanb
parents: 2
diff changeset
   396
            return JNI_TRUE;
2033c9b86813 4939819: File.canWrite() returns false for the "My Documents" directory (win)
alanb
parents: 2
diff changeset
   397
        else
2033c9b86813 4939819: File.canWrite() returns false for the "My Documents" directory (win)
alanb
parents: 2
diff changeset
   398
            return JNI_FALSE;
2033c9b86813 4939819: File.canWrite() returns false for the "My Documents" directory (win)
alanb
parents: 2
diff changeset
   399
    default:
2033c9b86813 4939819: File.canWrite() returns false for the "My Documents" directory (win)
alanb
parents: 2
diff changeset
   400
        assert(0);
2033c9b86813 4939819: File.canWrite() returns false for the "My Documents" directory (win)
alanb
parents: 2
diff changeset
   401
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
Java_java_io_WinNTFileSystem_setPermission(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                                           jobject file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                                           jint access,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                                           jboolean enable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                                           jboolean owneronly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    WCHAR *pathbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    DWORD a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    if (access == java_io_FileSystem_ACCESS_READ ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        access == java_io_FileSystem_ACCESS_EXECUTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        return enable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    pathbuf = fileToNTPath(env, file, ids.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    if (pathbuf == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    a = GetFileAttributesW(pathbuf);
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   423
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   424
    /* if reparse point, get final target */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   425
    if ((a != INVALID_FILE_ATTRIBUTES) &&
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   426
        ((a & FILE_ATTRIBUTE_REPARSE_POINT) != 0))
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   427
    {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   428
        WCHAR *fp = getFinalPath(pathbuf);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   429
        if (fp == NULL) {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   430
            a = INVALID_FILE_ATTRIBUTES;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   431
        } else {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   432
            free(pathbuf);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   433
            pathbuf = fp;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   434
            a = GetFileAttributesW(pathbuf);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   435
        }
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   436
    }
6884
65b1fa0a0fae 6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents: 5506
diff changeset
   437
    if ((a != INVALID_FILE_ATTRIBUTES) &&
65b1fa0a0fae 6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents: 5506
diff changeset
   438
        ((a & FILE_ATTRIBUTE_DIRECTORY) == 0))
65b1fa0a0fae 6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents: 5506
diff changeset
   439
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (enable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            a =  a & ~FILE_ATTRIBUTE_READONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            a =  a | FILE_ATTRIBUTE_READONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (SetFileAttributesW(pathbuf, a))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
Java_java_io_WinNTFileSystem_getLastModifiedTime(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                                 jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    jlong rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    LARGE_INTEGER modTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    FILETIME t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    HANDLE h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    WCHAR *pathbuf = fileToNTPath(env, file, ids.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    if (pathbuf == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    h = CreateFileW(pathbuf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    /* Device query access */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    /* Share it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    /* No security attributes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    /* Open existing or fail */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    OPEN_EXISTING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    /* Backup semantics for directories */
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   472
                    FILE_FLAG_BACKUP_SEMANTICS,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    /* No template file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    if (h != INVALID_HANDLE_VALUE) {
2426
a528c0830862 6819689: File.lastModified can return bogus value for remote file accessed as it is being deleted [win]
alanb
parents: 715
diff changeset
   476
        if (GetFileTime(h, NULL, NULL, &t)) {
a528c0830862 6819689: File.lastModified can return bogus value for remote file accessed as it is being deleted [win]
alanb
parents: 715
diff changeset
   477
            modTime.LowPart = (DWORD) t.dwLowDateTime;
a528c0830862 6819689: File.lastModified can return bogus value for remote file accessed as it is being deleted [win]
alanb
parents: 715
diff changeset
   478
            modTime.HighPart = (LONG) t.dwHighDateTime;
a528c0830862 6819689: File.lastModified can return bogus value for remote file accessed as it is being deleted [win]
alanb
parents: 715
diff changeset
   479
            rv = modTime.QuadPart / 10000;
a528c0830862 6819689: File.lastModified can return bogus value for remote file accessed as it is being deleted [win]
alanb
parents: 715
diff changeset
   480
            rv -= 11644473600000;
a528c0830862 6819689: File.lastModified can return bogus value for remote file accessed as it is being deleted [win]
alanb
parents: 715
diff changeset
   481
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        CloseHandle(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
Java_java_io_WinNTFileSystem_getLength(JNIEnv *env, jobject this, jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    jlong rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    WIN32_FILE_ATTRIBUTE_DATA wfad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    WCHAR *pathbuf = fileToNTPath(env, file, ids.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    if (pathbuf == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    if (GetFileAttributesExW(pathbuf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                             GetFileExInfoStandard,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                             &wfad)) {
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   499
        if ((wfad.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0) {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   500
            rv = wfad.nFileSizeHigh * ((jlong)MAXDWORD + 1) + wfad.nFileSizeLow;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   501
        } else {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   502
            /* file is a reparse point so read attributes of final target */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   503
            BY_HANDLE_FILE_INFORMATION finfo;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   504
            if (getFileInformation(pathbuf, &finfo)) {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   505
                rv = finfo.nFileSizeHigh * ((jlong)MAXDWORD + 1) +
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   506
                    finfo.nFileSizeLow;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   507
            }
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   508
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        if (GetLastError() == ERROR_SHARING_VIOLATION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            /* The error is "share violation", which means the file/dir
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
               must exists. Try _wstati64, we know this at least works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
               for pagefile.sys and hiberfil.sys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            struct _stati64 sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            if (_wstati64(pathbuf, &sb) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                rv = sb.st_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
/* -- File operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
Java_java_io_WinNTFileSystem_createFileExclusively(JNIEnv *env, jclass cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                                                   jstring path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    HANDLE h = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    WCHAR *pathbuf = pathToNTPath(env, path, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    if (pathbuf == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    h = CreateFileW(
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   536
        pathbuf,                              /* Wide char path name */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   537
        GENERIC_READ | GENERIC_WRITE,         /* Read and write permission */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        FILE_SHARE_READ | FILE_SHARE_WRITE,   /* File sharing flags */
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   539
        NULL,                                 /* Security attributes */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   540
        CREATE_NEW,                           /* creation disposition */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   541
        FILE_ATTRIBUTE_NORMAL |
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   542
            FILE_FLAG_OPEN_REPARSE_POINT,     /* flags and attributes */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    if (h == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        DWORD error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if ((error != ERROR_FILE_EXISTS) && (error != ERROR_ALREADY_EXISTS)) {
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   548
            // return false rather than throwing an exception when there is
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   549
            // an existing file.
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   550
            DWORD a = GetFileAttributesW(pathbuf);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   551
            if (a == INVALID_FILE_ATTRIBUTES) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                SetLastError(error);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                JNU_ThrowIOExceptionWithLastError(env, "Could not open file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
         free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
         return JNI_FALSE;
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   558
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    CloseHandle(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
removeFileOrDirectory(const jchar *path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /* Returns 0 on success */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    DWORD a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   570
    SetFileAttributesW(path, FILE_ATTRIBUTE_NORMAL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    a = GetFileAttributesW(path);
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   572
    if (a == INVALID_FILE_ATTRIBUTES) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    } else if (a & FILE_ATTRIBUTE_DIRECTORY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return !RemoveDirectoryW(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        return !DeleteFileW(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
Java_java_io_WinNTFileSystem_delete0(JNIEnv *env, jobject this, jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    WCHAR *pathbuf = fileToNTPath(env, file, ids.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    if (pathbuf == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    if (removeFileOrDirectory(pathbuf) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
Java_java_io_WinNTFileSystem_list(JNIEnv *env, jobject this, jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    WCHAR *search_path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    HANDLE handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    WIN32_FIND_DATAW find_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    int len, maxlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    jobjectArray rv, old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    DWORD fattr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    jstring name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    WCHAR *pathbuf = fileToNTPath(env, file, ids.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    if (pathbuf == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    search_path = (WCHAR*)malloc(2*wcslen(pathbuf) + 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    if (search_path == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        free (pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        errno = ENOMEM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    wcscpy(search_path, pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    fattr = GetFileAttributesW(search_path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    if (fattr == INVALID_FILE_ATTRIBUTES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        free(search_path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    } else if ((fattr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        free(search_path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /* Remove trailing space chars from directory name */
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   628
    len = (int)wcslen(search_path);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    while (search_path[len-1] == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        len--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    search_path[len] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    /* Append "*", or possibly "\\*", to path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    if ((search_path[0] == L'\\' && search_path[1] == L'\0') ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        (search_path[1] == L':'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        && (search_path[2] == L'\0'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        || (search_path[2] == L'\\' && search_path[3] == L'\0')))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        /* No '\\' needed for cases like "\" or "Z:" or "Z:\" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        wcscat(search_path, L"*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        wcscat(search_path, L"\\*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    /* Open handle to the first file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    handle = FindFirstFileW(search_path, &find_data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    free(search_path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    if (handle == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        if (GetLastError() != ERROR_FILE_NOT_FOUND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            // error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            // No files found - return an empty array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            rv = (*env)->NewObjectArray(env, 0, JNU_ClassString(env), NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    /* Allocate an initial String array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    maxlen = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    rv = (*env)->NewObjectArray(env, maxlen, JNU_ClassString(env), NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    if (rv == NULL) // Couldn't allocate an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    /* Scan the directory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        if (!wcscmp(find_data.cFileName, L".")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                                || !wcscmp(find_data.cFileName, L".."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
           continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        name = (*env)->NewString(env, find_data.cFileName,
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   671
                                 (jsize)wcslen(find_data.cFileName));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (name == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            return NULL; // error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        if (len == maxlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            old = rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            rv = (*env)->NewObjectArray(env, maxlen <<= 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                                            JNU_ClassString(env), NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            if ( rv == NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                         || JNU_CopyObjectArray(env, rv, old, len) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                return NULL; // error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            (*env)->DeleteLocalRef(env, old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        (*env)->SetObjectArrayElement(env, rv, len++, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        (*env)->DeleteLocalRef(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    } while (FindNextFileW(handle, &find_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    if (GetLastError() != ERROR_NO_MORE_FILES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        return NULL; // error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    FindClose(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    /* Copy the final results into an appropriately-sized array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    old = rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    rv = (*env)->NewObjectArray(env, len, JNU_ClassString(env), NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    if (rv == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        return NULL; /* error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    if (JNU_CopyObjectArray(env, rv, old, len) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        return NULL; /* error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
Java_java_io_WinNTFileSystem_createDirectory(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                                             jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    BOOL h = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    WCHAR *pathbuf = fileToNTPath(env, file, ids.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    if (pathbuf == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        /* Exception is pending */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    h = CreateDirectoryW(pathbuf, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    if (h == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
Java_java_io_WinNTFileSystem_rename0(JNIEnv *env, jobject this, jobject from,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                                     jobject to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    WCHAR *frompath = fileToNTPath(env, from, ids.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    WCHAR *topath = fileToNTPath(env, to, ids.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    if (frompath == NULL || topath == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    if (_wrename(frompath, topath) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    free(frompath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    free(topath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
Java_java_io_WinNTFileSystem_setLastModifiedTime(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                                                 jobject file, jlong time)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    WCHAR *pathbuf = fileToNTPath(env, file, ids.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    HANDLE h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    if (pathbuf == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        return JNI_FALSE;
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   752
    h = CreateFileW(pathbuf,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   753
                    FILE_WRITE_ATTRIBUTES,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   754
                    FILE_SHARE_READ | FILE_SHARE_WRITE,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   755
                    NULL,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   756
                    OPEN_EXISTING,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   757
                    FILE_FLAG_BACKUP_SEMANTICS,
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   758
                    0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        LARGE_INTEGER modTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        FILETIME t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        modTime.QuadPart = (time + 11644473600000L) * 10000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        t.dwLowDateTime = (DWORD)modTime.LowPart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        t.dwHighDateTime = (DWORD)modTime.HighPart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        if (SetFileTime(h, NULL, NULL, &t)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            rv = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        CloseHandle(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
Java_java_io_WinNTFileSystem_setReadOnly(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                                         jobject file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    jboolean rv = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    DWORD a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    WCHAR *pathbuf = fileToNTPath(env, file, ids.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    if (pathbuf == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    a = GetFileAttributesW(pathbuf);
3627
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   786
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   787
    /* if reparse point, get final target */
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   788
    if ((a != INVALID_FILE_ATTRIBUTES) &&
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   789
        ((a & FILE_ATTRIBUTE_REPARSE_POINT) != 0))
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   790
    {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   791
        WCHAR *fp = getFinalPath(pathbuf);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   792
        if (fp == NULL) {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   793
            a = INVALID_FILE_ATTRIBUTES;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   794
        } else {
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   795
            free(pathbuf);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   796
            pathbuf = fp;
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   797
            a = GetFileAttributesW(pathbuf);
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   798
        }
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   799
    }
d0ad40d5adab 6595866: File does work with symbolic links (win,vista)
alanb
parents: 2426
diff changeset
   800
6884
65b1fa0a0fae 6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents: 5506
diff changeset
   801
    if ((a != INVALID_FILE_ATTRIBUTES) &&
65b1fa0a0fae 6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents: 5506
diff changeset
   802
        ((a & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        if (SetFileAttributesW(pathbuf, a | FILE_ATTRIBUTE_READONLY))
6884
65b1fa0a0fae 6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents: 5506
diff changeset
   804
            rv = JNI_TRUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
/* -- Filesystem interface -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
Java_java_io_WinNTFileSystem_getDriveDirectory(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                                               jint drive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    jstring ret = NULL;
6885
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6884
diff changeset
   818
    jchar *p = currentDir(drive);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    jchar *pf = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    if (p == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    if (iswalpha(*p) && (p[1] == L':')) p += 2;
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   822
    ret = (*env)->NewString(env, p, (jsize)wcslen(p));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    free (pf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
typedef BOOL (WINAPI* GetVolumePathNameProc) (LPCWSTR, LPWSTR, DWORD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
Java_java_io_WinNTFileSystem_getSpace0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                                       jobject file, jint t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    WCHAR volname[MAX_PATH_LENGTH + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    jlong rv = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    WCHAR *pathbuf = fileToNTPath(env, file, ids.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    HMODULE h = LoadLibrary("kernel32");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    GetVolumePathNameProc getVolumePathNameW = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    if (h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        getVolumePathNameW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            = (GetVolumePathNameProc)GetProcAddress(h, "GetVolumePathNameW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    if (getVolumePathNameW(pathbuf, volname, MAX_PATH_LENGTH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        ULARGE_INTEGER totalSpace, freeSpace, usableSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        if (GetDiskFreeSpaceExW(volname, &usableSpace, &totalSpace, &freeSpace)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            switch(t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            case java_io_FileSystem_SPACE_TOTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                rv = long_to_jlong(totalSpace.QuadPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            case java_io_FileSystem_SPACE_FREE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                rv = long_to_jlong(freeSpace.QuadPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            case java_io_FileSystem_SPACE_USABLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                rv = long_to_jlong(usableSpace.QuadPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                assert(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    if (h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        FreeLibrary(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
}