Skip to content

Commit

Permalink
8276661: Change getPathWithPrefixForWin32Calls to getPathWithPadForWi…
Browse files Browse the repository at this point in the history
…n32Calls
  • Loading branch information
bplb committed Nov 23, 2021
1 parent aecffaf commit 5703b01
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
25 changes: 14 additions & 11 deletions src/java.base/windows/classes/sun/nio/fs/WindowsPath.java
Expand Up @@ -174,20 +174,23 @@ String getPathForPermissionCheck() {
// use this path for Win32 calls
// This method will prefix long paths with \\?\ or \\?\UNC as required.
String getPathForWin32Calls() throws WindowsException {
return getPathForWin32Calls(false);
return getPathForWin32Calls(0);
}

String getPathWithPrefixForWin32Calls() throws WindowsException {
return getPathForWin32Calls(true);
// This method will prefix long paths with \\?\ or \\?\UNC if
// path.length + pad > MAX_PATH. The parameter represents the
// length by which the path will be extended.
String getPathWithPadForWin32Calls(int pad) throws WindowsException {
return getPathForWin32Calls(Math.max(0, pad));
}

private String getPathForWin32Calls(boolean forceLongPrefix) throws WindowsException {
// short absolute paths can be used directly
if (isAbsolute() && path.length() <= MAX_PATH)
return forceLongPrefix ? addPrefix(path) : path;
private String getPathForWin32Calls(int pad) throws WindowsException {
if (pad == 0) {
// short absolute paths can be used directly
if (isAbsolute() && path.length() <= MAX_PATH)
return path;

// return cached value if available
if (!forceLongPrefix) {
// return cached value if available
WeakReference<String> ref = pathForWin32Calls;
String cached = (ref != null) ? ref.get() : null;
if (cached != null) {
Expand All @@ -204,7 +207,7 @@ private String getPathForWin32Calls(boolean forceLongPrefix) throws WindowsExcep
// a link - for example, it is okay for foo/link/../bar to be changed
// to foo/bar. The reason is that Win32 APIs to access foo/link/../bar
// will access foo/bar anyway (which differs to Unix systems)
if (resolved.length() > MAX_PATH || forceLongPrefix) {
if (resolved.length() + pad > MAX_PATH) {
if (resolved.length() > MAX_LONG_PATH) {
throw new WindowsException("Cannot access file with path exceeding "
+ MAX_LONG_PATH + " characters");
Expand All @@ -215,7 +218,7 @@ private String getPathForWin32Calls(boolean forceLongPrefix) throws WindowsExcep
// cache the resolved path (except drive relative paths as the working
// directory on removal media devices can change during the lifetime
// of the VM)
if (!forceLongPrefix && type != WindowsPathType.DRIVE_RELATIVE) {
if (pad == 0 && type != WindowsPathType.DRIVE_RELATIVE) {
synchronized (this) {
pathForWin32Calls = new WeakReference<String>(resolved);
}
Expand Down
Expand Up @@ -61,7 +61,8 @@ private String join(WindowsPath file, String name) throws WindowsException {
throw new IllegalArgumentException("'name' has a root component");
if (namePath.getParent() != null)
throw new IllegalArgumentException("'name' has more than one element");
return join(file.getPathWithPrefixForWin32Calls(), name);
int pad = 1 + name.length(); // colon + name length
return join(file.getPathWithPadForWin32Calls(pad), name);
}

private final WindowsPath file;
Expand Down

0 comments on commit 5703b01

Please sign in to comment.