提交 a4ff16cd 编写于 作者: A alanb

6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating...

6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
Reviewed-by: sherman
上级 f1e6c75a
......@@ -1177,14 +1177,20 @@ class WindowsPath extends AbstractPath {
/*
* Windows treates symbolic links to directories differently than it
* does to other file types. For that reason we check if the exists and
* is a directory.
* does to other file types. For that reason we need to check if the
* target is a directory (or a directory junction).
*/
WindowsPath resolvedTarget;
if (target.type == WindowsPathType.RELATIVE) {
WindowsPath parent = getParent();
resolvedTarget = (parent == null) ? target : parent.resolve(target);
} else {
resolvedTarget = resolve(target);
}
int flags = 0;
WindowsPath resolvedTarget =
WindowsPath.createFromNormalizedPath(getFileSystem(), resolve(target).path);
try {
if (WindowsFileAttributes.get(resolvedTarget, true).isDirectory())
WindowsFileAttributes wattrs = WindowsFileAttributes.get(resolvedTarget, false);
if (wattrs.isDirectory() || wattrs.isDirectoryLink())
flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
} catch (WindowsException x) {
// unable to access target so assume target is not a directory
......
......@@ -22,7 +22,7 @@
*/
/* @test
* @bug 4313887 6838333
* @bug 4313887 6838333 6863864
* @summary Unit test for java.nio.file.Path createSymbolicLink,
* readSymbolicLink, and createLink methods
* @library ..
......@@ -31,7 +31,6 @@
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.io.*;
import java.util.*;
public class Links {
......@@ -47,7 +46,7 @@ public class Links {
* Exercise createSymbolicLink and readLink methods
*/
static void testSymLinks(Path dir) throws IOException {
Path link = dir.resolve("link");
final Path link = dir.resolve("link");
// Check if sym links are supported
try {
......@@ -76,6 +75,63 @@ public class Links {
link.delete();
}
}
// Test links to directory
Path mydir = dir.resolve("mydir");
Path myfile = mydir.resolve("myfile");
try {
mydir.createDirectory();
myfile.createFile();
// link -> "mydir"
link.createSymbolicLink(mydir.getName());
assertTrue(link.readSymbolicLink().equals(mydir.getName()));
// Test access to directory via link
DirectoryStream<Path> stream = link.newDirectoryStream();
try {
boolean found = false;
for (Path entry: stream) {
if (entry.getName().equals(myfile.getName())) {
found = true;
break;
}
}
assertTrue(found);
} finally {
stream.close();
}
// Test link2 -> link -> mydir
final Path link2 = dir.resolve("link2");
Path target2 = link.getName();
link2.createSymbolicLink(target2);
try {
assertTrue(link2.readSymbolicLink().equals(target2));
link2.newDirectoryStream().close();
} finally {
link2.delete();
}
// Remove mydir and re-create link2 before re-creating mydir
// (This is a useful test on Windows to ensure that creating a
// sym link to a directory sym link creates the right type of link).
myfile.delete();
mydir.delete();
link2.createSymbolicLink(target2);
try {
assertTrue(link2.readSymbolicLink().equals(target2));
mydir.createDirectory();
link2.newDirectoryStream().close();
} finally {
link2.delete();
}
} finally {
myfile.deleteIfExists();
mydir.deleteIfExists();
link.deleteIfExists();
}
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册