mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 23:04:50 +02:00
8276661: (fs) UserDefinedFileAttributeView no longer works with long path (win)
Reviewed-by: alanb
This commit is contained in:
parent
652b5f8546
commit
15345e3edc
3 changed files with 42 additions and 24 deletions
|
@ -174,38 +174,48 @@ class WindowsPath implements Path {
|
|||
// use this path for Win32 calls
|
||||
// This method will prefix long paths with \\?\ or \\?\UNC as required.
|
||||
String getPathForWin32Calls() throws WindowsException {
|
||||
// short absolute paths can be used directly
|
||||
if (isAbsolute() && path.length() <= MAX_PATH)
|
||||
return path;
|
||||
return getPathForWin32Calls(true);
|
||||
}
|
||||
|
||||
// return cached values if available
|
||||
WeakReference<String> ref = pathForWin32Calls;
|
||||
String resolved = (ref != null) ? ref.get() : null;
|
||||
if (resolved != null) {
|
||||
// Win32 path already available
|
||||
return resolved;
|
||||
String getPathWithPrefixForWin32Calls() throws WindowsException {
|
||||
return getPathForWin32Calls(false);
|
||||
}
|
||||
|
||||
private String getPathForWin32Calls(boolean allowShortPath) throws WindowsException {
|
||||
if (allowShortPath) {
|
||||
// short absolute paths can be used directly
|
||||
if (isAbsolute() && path.length() <= MAX_PATH)
|
||||
return path;
|
||||
|
||||
// returned cached value if possible
|
||||
WeakReference<String> ref = pathForWin32Calls;
|
||||
String cached = (ref != null) ? ref.get() : null;
|
||||
if (cached != null) {
|
||||
// Win32 path already available
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
|
||||
// resolve against default directory
|
||||
resolved = getAbsolutePath();
|
||||
String resolved = getAbsolutePath();
|
||||
|
||||
// Long paths need to have "." and ".." removed and be prefixed with
|
||||
// "\\?\". Note that it is okay to remove ".." even when it follows
|
||||
// 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) {
|
||||
if (resolved.length() > MAX_PATH || !allowShortPath) {
|
||||
if (resolved.length() > MAX_LONG_PATH) {
|
||||
throw new WindowsException("Cannot access file with path exceeding "
|
||||
+ MAX_LONG_PATH + " characters");
|
||||
}
|
||||
resolved = addPrefixIfNeeded(GetFullPathName(resolved));
|
||||
resolved = addPrefix(GetFullPathName(resolved));
|
||||
}
|
||||
|
||||
// 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 (type != WindowsPathType.DRIVE_RELATIVE) {
|
||||
if (allowShortPath && type != WindowsPathType.DRIVE_RELATIVE) {
|
||||
synchronized (this) {
|
||||
pathForWin32Calls = new WeakReference<String>(resolved);
|
||||
}
|
||||
|
@ -279,14 +289,20 @@ class WindowsPath implements Path {
|
|||
Character.toUpperCase(root2.charAt(0));
|
||||
}
|
||||
|
||||
// Add long path prefix to path
|
||||
static String addPrefix(String path) {
|
||||
if (path.startsWith("\\\\")) {
|
||||
path = "\\\\?\\UNC" + path.substring(1, path.length());
|
||||
} else {
|
||||
path = "\\\\?\\" + path;
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
// Add long path prefix to path if required
|
||||
static String addPrefixIfNeeded(String path) {
|
||||
if (path.length() > MAX_PATH) {
|
||||
if (path.startsWith("\\\\")) {
|
||||
path = "\\\\?\\UNC" + path.substring(1, path.length());
|
||||
} else {
|
||||
path = "\\\\?\\" + path;
|
||||
}
|
||||
return addPrefix(path);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
|
|
@ -61,9 +61,7 @@ class WindowsUserDefinedFileAttributeView
|
|||
throw new IllegalArgumentException("'name' has a root component");
|
||||
if (namePath.getParent() != null)
|
||||
throw new IllegalArgumentException("'name' has more than one element");
|
||||
String path = join(file.getPathForWin32Calls(), name);
|
||||
WindowsPath wp = WindowsPath.createFromNormalizedPath(wfs, path);
|
||||
return wp.getPathForWin32Calls();
|
||||
return join(file.getPathWithPrefixForWin32Calls(), name);
|
||||
}
|
||||
|
||||
private final WindowsPath file;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue