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