jdk/src/windows/native/java/io/canonicalize_md.c
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8186 d0e2cc8b3073
child 19418 bb4ae810197e
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8186
diff changeset
     2
 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Pathname canonicalization for Win32 file systems
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <sys/stat.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <winbase.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include "io_util_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#undef DEBUG_PATH        /* Define this to debug path code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#define isfilesep(c) ((c) == '/' || (c) == '\\')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#define wisfilesep(c) ((c) == L'/' || (c) == L'\\')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#define islb(c)      (IsDBCSLeadByte((BYTE)(c)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/* Copy bytes to dst, not going past dend; return dst + number of bytes copied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
   or NULL if dend would have been exceeded.  If first != '\0', copy that byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
   before copying bytes from src to send - 1. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
static char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
cp(char *dst, char *dend, char first, char *src, char *send)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    char *p = src, *q = dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    if (first != '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (q < dend) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            *q++ = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            errno = ENAMETOOLONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    if (send - p > dend - q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        errno = ENAMETOOLONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    while (p < send) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        *q++ = *p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    return q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/* Wide character version of cp */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
static WCHAR*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
wcp(WCHAR *dst, WCHAR *dend, WCHAR first, WCHAR *src, WCHAR *send)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    WCHAR *p = src, *q = dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    if (first != L'\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (q < dend) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            *q++ = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            errno = ENAMETOOLONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    if (send - p > dend - q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        errno = ENAMETOOLONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    while (p < send)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        *q++ = *p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    return q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
/* Find first instance of '\\' at or following start.  Return the address of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
   that byte or the address of the null terminator if '\\' is not found. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
static char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
nextsep(char *start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    char *p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    while ((c = *p) && (c != '\\')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        p += ((islb(c) && p[1]) ? 2 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
/* Wide character version of nextsep */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
static WCHAR *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
wnextsep(WCHAR *start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    WCHAR *p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    while ((c = *p) && (c != L'\\'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
/* Tell whether the given string contains any wildcard characters */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
wild(char *start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    char *p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    while (c = *p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if ((c == '*') || (c == '?')) return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        p += ((islb(c) && p[1]) ? 2 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
/* Wide character version of wild */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
wwild(WCHAR *start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    WCHAR *p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    while (c = *p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if ((c == L'*') || (c == L'?'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
/* Tell whether the given string contains prohibited combinations of dots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
   In the canonicalized form no path element may have dots at its end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
   Allowed canonical paths: c:\xa...dksd\..ksa\.lk    c:\...a\.b\cd..x.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
   Prohibited canonical paths: c:\..\x  c:\x.\d c:\...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
dots(char *start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    char *p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    while (*p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if ((p = strchr(p, '.')) == NULL) // find next occurence of '.'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return 0; // no more dots
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        p++; // next char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        while ((*p) == '.') // go to the end of dots
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (*p && (*p != '\\')) // path element does not end with a dot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            p++; // go to the next char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return 1; // path element does end with a dot - prohibited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    return 0; // no prohibited combinations of dots found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
/* Wide character version of dots */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
wdots(WCHAR *start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    WCHAR *p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    while (*p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if ((p = wcschr(p, L'.')) == NULL) // find next occurence of '.'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return 0; // no more dots
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        p++; // next char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        while ((*p) == L'.') // go to the end of dots
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (*p && (*p != L'\\')) // path element does not end with a dot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            p++; // go to the next char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return 1; // path element does end with a dot - prohibited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    return 0; // no prohibited combinations of dots found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
/* If the lookup of a particular prefix fails because the file does not exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
   because it is of the wrong type, because access is denied, or because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
   network is unreachable then canonicalization does not fail, it terminates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
   successfully after copying the rest of the original path to the result path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
   Other I/O errors cause an error return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
lastErrorReportable()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    DWORD errval = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    if ((errval == ERROR_FILE_NOT_FOUND)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        || (errval == ERROR_DIRECTORY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        || (errval == ERROR_PATH_NOT_FOUND)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        || (errval == ERROR_BAD_NETPATH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        || (errval == ERROR_BAD_NET_NAME)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        || (errval == ERROR_ACCESS_DENIED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        || (errval == ERROR_NETWORK_UNREACHABLE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        || (errval == ERROR_NETWORK_ACCESS_DENIED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
#ifdef DEBUG_PATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    jio_fprintf(stderr, "canonicalize: errval %d\n", errval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
/* Convert a pathname to canonical form.  The input orig_path is assumed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
   have been converted to native form already, via JVM_NativePath().  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
   necessary because _fullpath() rejects duplicate separator characters on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
   Win95, though it accepts them on NT. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
canonicalize(char *orig_path, char *result, int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    WIN32_FIND_DATA fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    HANDLE h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    char path[1024];    /* Working copy of path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    char *src, *dst, *dend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /* Reject paths that contain wildcards */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    if (wild(orig_path)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        errno = EINVAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /* Collapse instances of "foo\.." and ensure absoluteness.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
       contrary to the documentation, the _fullpath procedure does not require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
       the drive to be available.  It also does not reliably change all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
       occurrences of '/' to '\\' on Win95, so now JVM_NativePath does that. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    if(!_fullpath(path, orig_path, sizeof(path))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /* Correction for Win95: _fullpath may leave a trailing "\\"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
       on a UNC pathname */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    if ((path[0] == '\\') && (path[1] == '\\')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        char *p = path + strlen(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if ((p[-1] == '\\') && !islb(p[-2])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            p[-1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    if (dots(path)) /* Check for prohibited combinations of dots */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    src = path;            /* Start scanning here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    dst = result;        /* Place results here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    dend = dst + size;        /* Don't go to or past here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /* Copy prefix, assuming path is absolute */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    if (isalpha(src[0]) && (src[1] == ':') && (src[2] == '\\')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        /* Drive specifier */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        *src = toupper(*src);    /* Canonicalize drive letter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (!(dst = cp(dst, dend, '\0', src, src + 2))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        src += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    } else if ((src[0] == '\\') && (src[1] == '\\')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        /* UNC pathname */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        char *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        p = nextsep(src + 2);    /* Skip past host name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (!*p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        /* A UNC pathname must begin with "\\\\host\\share",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
           so reject this path as invalid if there is no share name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            errno = EINVAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    p = nextsep(p + 1);    /* Skip past share name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    if (!(dst = cp(dst, dend, '\0', src, p))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    src = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        /* Invalid path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        errno = EINVAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /* Windows 95/98/Me bug - FindFirstFile fails on network mounted drives */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /* for root pathes like "E:\" . If the path has this form, we should  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /* simply return it, it is already canonicalized. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    if (strlen(path) == 3 && path[1] == ':' && path[2] == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        /* At this point we have already copied the drive specifier ("z:")*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        /* so we need to copy "\" and the null character. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        result[2] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        result[3] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /* At this point we have copied either a drive specifier ("z:") or a UNC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
       prefix ("\\\\host\\share") to the result buffer, and src points to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
       first byte of the remainder of the path.  We now scan through the rest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
       of the path, looking up each prefix in order to find the true name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
       the last element of each prefix, thereby computing the full true name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
       the original path. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    while (*src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        char *p = nextsep(src + 1);    /* Find next separator */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        char c = *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        assert(*src == '\\');        /* Invariant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        *p = '\0';            /* Temporarily clear separator */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        h = FindFirstFile(path, &fd);    /* Look up prefix */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        *p = c;                /* Restore separator */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            /* Lookup succeeded; append true name to result and continue */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            FindClose(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            if (!(dst = cp(dst, dend, '\\',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                           fd.cFileName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                           fd.cFileName + strlen(fd.cFileName)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            src = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            if (!lastErrorReportable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                if (!(dst = cp(dst, dend, '\0', src, src + strlen(src)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    if (dst >= dend) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    errno = ENAMETOOLONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    *dst = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
/* Convert a pathname to canonical form.  The input prefix is assumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
   to be in canonical form already, and the trailing filename must not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
   contain any wildcard, dot/double dot, or other "tricky" characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
   that are rejected by the canonicalize() routine above.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
   routine is present to allow the canonicalization prefix cache to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
   used while still returning canonical names with the correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
   capitalization. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
canonicalizeWithPrefix(char* canonicalPrefix, char* pathWithCanonicalPrefix, char *result, int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    WIN32_FIND_DATA fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    HANDLE h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    char *src, *dst, *dend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    src = pathWithCanonicalPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    dst = result;        /* Place results here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    dend = dst + size;   /* Don't go to or past here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    h = FindFirstFile(pathWithCanonicalPrefix, &fd);    /* Look up file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        /* Lookup succeeded; concatenate true name to prefix */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        FindClose(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (!(dst = cp(dst, dend, '\0',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                       canonicalPrefix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                       canonicalPrefix + strlen(canonicalPrefix)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (!(dst = cp(dst, dend, '\\',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                       fd.cFileName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                       fd.cFileName + strlen(fd.cFileName)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (!lastErrorReportable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            if (!(dst = cp(dst, dend, '\0', src, src + strlen(src)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    if (dst >= dend) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        errno = ENAMETOOLONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    *dst = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
/* Wide character version of canonicalize. Size is a wide-character size. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
wcanonicalize(WCHAR *orig_path, WCHAR *result, int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    WIN32_FIND_DATAW fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    HANDLE h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    WCHAR *path;    /* Working copy of path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    WCHAR *src, *dst, *dend, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /* Reject paths that contain wildcards */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    if (wwild(orig_path)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        errno = EINVAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    if ((path = (WCHAR*)malloc(size * sizeof(WCHAR))) == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /* Collapse instances of "foo\.." and ensure absoluteness.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
       contrary to the documentation, the _fullpath procedure does not require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
       the drive to be available.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    if(!_wfullpath(path, orig_path, size)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    if (wdots(path)) /* Check for prohibited combinations of dots */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    src = path;            /* Start scanning here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    dst = result;        /* Place results here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    dend = dst + size;        /* Don't go to or past here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /* Copy prefix, assuming path is absolute */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    c = src[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    if (((c <= L'z' && c >= L'a') || (c <= L'Z' && c >= L'A'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
       && (src[1] == L':') && (src[2] == L'\\')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        /* Drive specifier */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        *src = towupper(*src);    /* Canonicalize drive letter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        if (!(dst = wcp(dst, dend, L'\0', src, src + 2))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        src += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    } else if ((src[0] == L'\\') && (src[1] == L'\\')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        /* UNC pathname */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        WCHAR *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        p = wnextsep(src + 2);    /* Skip past host name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (!*p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            /* A UNC pathname must begin with "\\\\host\\share",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
               so reject this path as invalid if there is no share name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            errno = EINVAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        p = wnextsep(p + 1);    /* Skip past share name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (!(dst = wcp(dst, dend, L'\0', src, p)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        src = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        /* Invalid path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        errno = EINVAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    /* At this point we have copied either a drive specifier ("z:") or a UNC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
       prefix ("\\\\host\\share") to the result buffer, and src points to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
       first byte of the remainder of the path.  We now scan through the rest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
       of the path, looking up each prefix in order to find the true name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
       the last element of each prefix, thereby computing the full true name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
       the original path. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    while (*src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        WCHAR *p = wnextsep(src + 1);    /* Find next separator */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        WCHAR c = *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        WCHAR *pathbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        int pathlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        assert(*src == L'\\');        /* Invariant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        *p = L'\0';            /* Temporarily clear separator */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 5506
diff changeset
   482
        if ((pathlen = (int)wcslen(path)) > MAX_PATH - 1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            pathbuf = getPrefixed(path, pathlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            h = FindFirstFileW(pathbuf, &fd);    /* Look up prefix */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            h = FindFirstFileW(path, &fd);    /* Look up prefix */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        *p = c;                /* Restore separator */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            /* Lookup succeeded; append true name to result and continue */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            FindClose(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if (!(dst = wcp(dst, dend, L'\\', fd.cFileName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                            fd.cFileName + wcslen(fd.cFileName)))){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            src = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            if (!lastErrorReportable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
               if (!(dst = wcp(dst, dend, L'\0', src, src + wcslen(src)))){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                   goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    if (dst >= dend) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    errno = ENAMETOOLONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    *dst = L'\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    free(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
 err:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    free(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
/* Wide character version of canonicalizeWithPrefix. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
wcanonicalizeWithPrefix(WCHAR *canonicalPrefix, WCHAR *pathWithCanonicalPrefix, WCHAR *result, int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    WIN32_FIND_DATAW fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    HANDLE h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    WCHAR *src, *dst, *dend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    WCHAR *pathbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    int pathlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    src = pathWithCanonicalPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    dst = result;        /* Place results here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    dend = dst + size;   /* Don't go to or past here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 5506
diff changeset
   541
    if ((pathlen=(int)wcslen(pathWithCanonicalPrefix)) > MAX_PATH - 1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        pathbuf = getPrefixed(pathWithCanonicalPrefix, pathlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        h = FindFirstFileW(pathbuf, &fd);    /* Look up prefix */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        h = FindFirstFileW(pathWithCanonicalPrefix, &fd);    /* Look up prefix */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        /* Lookup succeeded; append true name to result and continue */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        FindClose(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        if (!(dst = wcp(dst, dend, L'\0',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                        canonicalPrefix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        canonicalPrefix + wcslen(canonicalPrefix)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        if (!(dst = wcp(dst, dend, L'\\',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                        fd.cFileName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                        fd.cFileName + wcslen(fd.cFileName)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        if (!lastErrorReportable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            if (!(dst = wcp(dst, dend, L'\0', src, src + wcslen(src)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    if (dst >= dend) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        errno = ENAMETOOLONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    *dst = L'\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
/* The appropriate location of getPrefixed() should be io_util_md.c, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
   java.lang.instrument package has hardwired canonicalize_md.c into their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
   dll, to avoid complicate solution such as including io_util_md.c into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
   that package, as a workaround we put this method here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
/* copy \\?\ or \\?\UNC\ to the front of path*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
WCHAR*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
getPrefixed(const WCHAR* path, int pathlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    WCHAR* pathbuf = (WCHAR*)malloc((pathlen + 10) * sizeof (WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    if (pathbuf != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        if (path[0] == L'\\' && path[1] == L'\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            if (path[2] == L'?' && path[3] == L'\\'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                /* if it already has a \\?\ don't do the prefix */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                wcscpy(pathbuf, path );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                /* only UNC pathname includes double slashes here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                wcscpy(pathbuf, L"\\\\?\\UNC\0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                wcscat(pathbuf, path + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            wcscpy(pathbuf, L"\\\\?\\\0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            wcscat(pathbuf, path );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    return pathbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
}