8298478: (fs) Path.of should allow input to include long path prefix

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2023-02-08 19:20:39 +00:00
parent 10dd98d0dd
commit 638d612c6b
2 changed files with 53 additions and 3 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -92,6 +92,20 @@ class WindowsPathParser {
* Indicates if the path requires to be normalized
*/
private static Result parse(String input, boolean requireToNormalize) {
// if a prefix is present, remove it and note the expected path type
final WindowsPathType expectedType;
if (input.startsWith("\\\\?\\")) {
if (input.startsWith("UNC\\", 4)) {
expectedType = WindowsPathType.UNC;
input = "\\\\" + input.substring(8);
} else {
expectedType = WindowsPathType.ABSOLUTE;
input = input.substring(4);
}
} else {
expectedType = null;
}
String root = "";
WindowsPathType type = null;
@ -147,6 +161,14 @@ class WindowsPathParser {
}
}
if (expectedType != null && type != expectedType) {
if (expectedType == WindowsPathType.ABSOLUTE) { // long path prefix
throw new InvalidPathException(input, "Long path prefix can only be used with an absolute path");
} else if (expectedType == WindowsPathType.UNC) { // long UNC path prefix
throw new InvalidPathException(input, "Long UNC path prefix can only be used with a UNC path");
}
}
if (requireToNormalize) {
StringBuilder sb = new StringBuilder(input.length());
sb.append(root);