src/java.base/windows/native/libjava/canonicalize_md.c
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File open Sdiff src/java.base/windows/native/libjava

src/java.base/windows/native/libjava/canonicalize_md.c

Print this page




 208 lastErrorReportable()
 209 {
 210     DWORD errval = GetLastError();
 211     if ((errval == ERROR_FILE_NOT_FOUND)
 212         || (errval == ERROR_DIRECTORY)
 213         || (errval == ERROR_PATH_NOT_FOUND)
 214         || (errval == ERROR_BAD_NETPATH)
 215         || (errval == ERROR_BAD_NET_NAME)
 216         || (errval == ERROR_ACCESS_DENIED)
 217         || (errval == ERROR_NETWORK_UNREACHABLE)
 218         || (errval == ERROR_NETWORK_ACCESS_DENIED)) {
 219         return 0;
 220     }
 221 
 222 #ifdef DEBUG_PATH
 223     jio_fprintf(stderr, "canonicalize: errval %d\n", errval);
 224 #endif
 225     return 1;
 226 }
 227 


 228 /* Convert a pathname to canonical form.  The input orig_path is assumed to
 229    have been converted to native form already, via JVM_NativePath().  This is
 230    necessary because _fullpath() rejects duplicate separator characters on
 231    Win95, though it accepts them on NT. */
 232 
 233 int
 234 canonicalize(char *orig_path, char *result, int size)
 235 {
 236     WIN32_FIND_DATA fd;
 237     HANDLE h;
 238     char path[1024];    /* Working copy of path */
 239     char *src, *dst, *dend;
































 240 
 241     /* Reject paths that contain wildcards */
 242     if (wild(orig_path)) {
 243         errno = EINVAL;
 244         return -1;
 245     }
 246 
 247     /* Collapse instances of "foo\.." and ensure absoluteness.  Note that
 248        contrary to the documentation, the _fullpath procedure does not require
 249        the drive to be available.  It also does not reliably change all
 250        occurrences of '/' to '\\' on Win95, so now JVM_NativePath does that. */
 251     if(!_fullpath(path, orig_path, sizeof(path))) {
 252         return -1;
 253     }
 254 
 255     /* Correction for Win95: _fullpath may leave a trailing "\\"
 256        on a UNC pathname */
 257     if ((path[0] == '\\') && (path[1] == '\\')) {
 258         char *p = path + strlen(path);
 259         if ((p[-1] == '\\') && !islb(p[-2])) {
 260             p[-1] = '\0';
 261         }
 262     }
 263 
 264     if (dots(path)) /* Check for prohibited combinations of dots */
 265         return -1;
 266 
 267     src = path;            /* Start scanning here */
 268     dst = result;        /* Place results here */
 269     dend = dst + size;        /* Don't go to or past here */
 270 
 271     /* Copy prefix, assuming path is absolute */


 570             return -1;
 571         }
 572     }
 573 
 574     if (dst >= dend) {
 575         errno = ENAMETOOLONG;
 576         return -1;
 577     }
 578     *dst = L'\0';
 579     return 0;
 580 }
 581 
 582 
 583 /* The appropriate location of getPrefixed() should be io_util_md.c, but
 584    java.lang.instrument package has hardwired canonicalize_md.c into their
 585    dll, to avoid complicate solution such as including io_util_md.c into
 586    that package, as a workaround we put this method here.
 587  */
 588 
 589 /* copy \\?\ or \\?\UNC\ to the front of path*/
 590 WCHAR*
 591 getPrefixed(const WCHAR* path, int pathlen) {
 592     WCHAR* pathbuf = (WCHAR*)malloc((pathlen + 10) * sizeof (WCHAR));
 593     if (pathbuf != 0) {
 594         if (path[0] == L'\\' && path[1] == L'\\') {
 595             if (path[2] == L'?' && path[3] == L'\\'){
 596                 /* if it already has a \\?\ don't do the prefix */
 597                 wcscpy(pathbuf, path );
 598             } else {
 599                 /* only UNC pathname includes double slashes here */
 600                 wcscpy(pathbuf, L"\\\\?\\UNC\0");
 601                 wcscat(pathbuf, path + 1);
 602             }
 603         } else {
 604             wcscpy(pathbuf, L"\\\\?\\\0");
 605             wcscat(pathbuf, path );
 606         }
 607     }
 608     return pathbuf;
 609 }


 208 lastErrorReportable()
 209 {
 210     DWORD errval = GetLastError();
 211     if ((errval == ERROR_FILE_NOT_FOUND)
 212         || (errval == ERROR_DIRECTORY)
 213         || (errval == ERROR_PATH_NOT_FOUND)
 214         || (errval == ERROR_BAD_NETPATH)
 215         || (errval == ERROR_BAD_NET_NAME)
 216         || (errval == ERROR_ACCESS_DENIED)
 217         || (errval == ERROR_NETWORK_UNREACHABLE)
 218         || (errval == ERROR_NETWORK_ACCESS_DENIED)) {
 219         return 0;
 220     }
 221 
 222 #ifdef DEBUG_PATH
 223     jio_fprintf(stderr, "canonicalize: errval %d\n", errval);
 224 #endif
 225     return 1;
 226 }
 227 
 228 int wcanonicalize(WCHAR *orig_path, WCHAR *result, int size);
 229 
 230 /* Convert a pathname to canonical form.  The input orig_path is assumed to
 231    have been converted to native form already, via JVM_NativePath().  This is
 232    necessary because _fullpath() rejects duplicate separator characters on
 233    Win95, though it accepts them on NT. */
 234 
 235 int
 236 canonicalize(char *orig_path, char *result, int size)
 237 {
 238     WIN32_FIND_DATA fd;
 239     HANDLE h;
 240     char path[1024];    /* Working copy of path */
 241     char *src, *dst, *dend;
 242     wchar_t *worig_path, *wresult;
 243     size_t converted_chars = 0;
 244 
 245     /* handle long path with length >= MAX_PATH */
 246     if (strlen(orig_path) >= MAX_PATH) {
 247         if ((worig_path = (WCHAR*)malloc(size * sizeof(WCHAR))) == NULL)
 248             return -1;
 249 
 250         if (mbstowcs_s(&converted_chars, worig_path, (size_t)size, orig_path, (size_t)(size - 1)) != 0) {
 251             free(worig_path);
 252             return -1;
 253         }
 254 
 255         if ((wresult = (WCHAR*)malloc(size * sizeof(WCHAR))) == NULL)
 256             return -1;
 257 
 258         if (wcanonicalize(worig_path, wresult, size) != 0) {
 259             free(worig_path);
 260             free(wresult);
 261             return -1;
 262         }
 263 
 264         if (wcstombs_s(&converted_chars, result, (size_t)size, wresult, (size_t)(size - 1)) != 0) {
 265             free(worig_path);
 266             free(wresult);
 267             return -1;
 268         }
 269 
 270         free(worig_path);
 271         free(wresult);
 272         return 0;
 273     }
 274 
 275     /* Reject paths that contain wildcards */
 276     if (wild(orig_path)) {
 277         errno = EINVAL;
 278         return -1;
 279     }
 280 
 281     /* Collapse instances of "foo\.." and ensure absoluteness.  Note that
 282       contrary to the documentation, the _fullpath procedure does not require
 283       the drive to be available.  It also does not reliably change all
 284       occurrences of '/' to '\\' on Win95, so now JVM_NativePath does that. */
 285     if (!_fullpath(path, orig_path, sizeof(path))) {
 286         return -1;
 287     }
 288 
 289     /* Correction for Win95: _fullpath may leave a trailing "\\"
 290       on a UNC pathname */
 291     if ((path[0] == '\\') && (path[1] == '\\')) {
 292         char *p = path + strlen(path);
 293         if ((p[-1] == '\\') && !islb(p[-2])) {
 294             p[-1] = '\0';
 295         }
 296     }
 297 
 298     if (dots(path)) /* Check for prohibited combinations of dots */
 299         return -1;
 300 
 301     src = path;            /* Start scanning here */
 302     dst = result;        /* Place results here */
 303     dend = dst + size;        /* Don't go to or past here */
 304 
 305     /* Copy prefix, assuming path is absolute */


 604             return -1;
 605         }
 606     }
 607 
 608     if (dst >= dend) {
 609         errno = ENAMETOOLONG;
 610         return -1;
 611     }
 612     *dst = L'\0';
 613     return 0;
 614 }
 615 
 616 
 617 /* The appropriate location of getPrefixed() should be io_util_md.c, but
 618    java.lang.instrument package has hardwired canonicalize_md.c into their
 619    dll, to avoid complicate solution such as including io_util_md.c into
 620    that package, as a workaround we put this method here.
 621  */
 622 
 623 /* copy \\?\ or \\?\UNC\ to the front of path*/
 624 __declspec(dllexport) WCHAR*
 625 getPrefixed(const WCHAR* path, int pathlen) {
 626     WCHAR* pathbuf = (WCHAR*)malloc((pathlen + 10) * sizeof (WCHAR));
 627     if (pathbuf != 0) {
 628         if (path[0] == L'\\' && path[1] == L'\\') {
 629             if (path[2] == L'?' && path[3] == L'\\'){
 630                 /* if it already has a \\?\ don't do the prefix */
 631                 wcscpy(pathbuf, path );
 632             } else {
 633                 /* only UNC pathname includes double slashes here */
 634                 wcscpy(pathbuf, L"\\\\?\\UNC\0");
 635                 wcscat(pathbuf, path + 1);
 636             }
 637         } else {
 638             wcscpy(pathbuf, L"\\\\?\\\0");
 639             wcscat(pathbuf, path );
 640         }
 641     }
 642     return pathbuf;
 643 }
src/java.base/windows/native/libjava/canonicalize_md.c
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File