本文整理了Java中java.nio.file.Files.createSymbolicLink()
方法的一些代码示例,展示了Files.createSymbolicLink()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Files.createSymbolicLink()
方法的具体详情如下:
包路径:java.nio.file.Files
类名称:Files
方法名:createSymbolicLink
暂无
代码示例来源:origin: google/guava
@Override
public void run() {
boolean createSymlink = false;
while (!Thread.interrupted()) {
try {
// trying to switch between a real directory and a symlink (dir -> /a)
if (Files.deleteIfExists(file)) {
if (createSymlink) {
Files.createSymbolicLink(file, target);
} else {
Files.createDirectory(file);
}
createSymlink = !createSymlink;
}
} catch (IOException tolerated) {
// it's expected that some of these will fail
}
Thread.yield();
}
}
});
代码示例来源:origin: google/guava
public void testCreateParentDirectories_symlinkParentExists() throws IOException {
Path symlink = tempDir.resolve("linkToDir");
Files.createSymbolicLink(symlink, root());
Path file = symlink.resolve("foo");
MoreFiles.createParentDirectories(file);
}
代码示例来源:origin: eclipse-vertx/vert.x
public Void perform() {
try {
Path source = vertx.resolveFile(link).toPath();
Path target = vertx.resolveFile(existing).toPath();
if (symbolic) {
Files.createSymbolicLink(source, target);
} else {
Files.createLink(source, target);
}
} catch (IOException e) {
throw new FileSystemException(e);
}
return null;
}
};
代码示例来源:origin: google/guava
@AndroidIncompatible // Path (for symlink creation)
public void testScanDirectory_symlinkToRootCycle() throws IOException {
ClassLoader loader = ClassPathTest.class.getClassLoader();
// directory with a cycle,
// /root
// /child
// /[grandchild -> root]
java.nio.file.Path root = createTempDirectory("ClassPathTest");
try {
createFile(root.resolve("some.txt"));
java.nio.file.Path child = createDirectory(root.resolve("child"));
createSymbolicLink(child.resolve("grandchild"), root);
ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner();
scanner.scan(root.toFile(), loader);
assertEquals(ImmutableSet.of(new ResourceInfo("some.txt", loader)), scanner.getResources());
} finally {
deleteRecursivelyOrLog(root);
}
}
代码示例来源:origin: google/guava
public void testByteSource_size_ofSymlinkToRegularFile() throws IOException {
try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
Path file = fs.getPath("file");
Files.write(file, new byte[10]);
Path link = fs.getPath("link");
Files.createSymbolicLink(link, file);
ByteSource source = MoreFiles.asByteSource(link);
assertEquals(10L, (long) source.sizeIfKnown().get());
assertEquals(10L, source.size());
}
}
代码示例来源:origin: neo4j/neo4j
@Test
@DisabledOnOs( OS.WINDOWS )
void shouldHandleSymlinkToDatabaseDir() throws IOException, CommandFailed, IncorrectUsage, IncorrectFormat
{
Path symDir = testDirectory.directory( "path-to-links" ).toPath();
Path realDatabaseDir = symDir.resolve( "foo.db" );
Path dataDir = testDirectory.directory( "some-other-path" ).toPath();
Path databaseDir = dataDir.resolve( "databases/foo.db" );
Files.createDirectories( realDatabaseDir );
Files.createDirectories( dataDir.resolve( "databases" ) );
Files.createSymbolicLink( databaseDir, realDatabaseDir );
Files.write( configDir.resolve( Config.DEFAULT_CONFIG_FILE_NAME ), singletonList( formatProperty( data_directory, dataDir ) ) );
execute( "foo.db" );
verify( loader ).load( any(), eq( realDatabaseDir ), eq( realDatabaseDir ) );
}
代码示例来源:origin: google/guava
@AndroidIncompatible // Path (for symlink creation)
public void testScanDirectory_symlinkCycle() throws IOException {
ClassLoader loader = ClassPathTest.class.getClassLoader();
// directory with a cycle,
// /root
// /left
// /[sibling -> right]
// /right
// /[sibling -> left]
java.nio.file.Path root = createTempDirectory("ClassPathTest");
try {
java.nio.file.Path left = createDirectory(root.resolve("left"));
createFile(left.resolve("some.txt"));
java.nio.file.Path right = createDirectory(root.resolve("right"));
createFile(right.resolve("another.txt"));
createSymbolicLink(left.resolve("sibling"), right);
createSymbolicLink(right.resolve("sibling"), left);
ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner();
scanner.scan(root.toFile(), loader);
assertEquals(
ImmutableSet.of(
new ResourceInfo("left/some.txt", loader),
new ResourceInfo("left/sibling/another.txt", loader),
new ResourceInfo("right/another.txt", loader),
new ResourceInfo("right/sibling/some.txt", loader)),
scanner.getResources());
} finally {
deleteRecursivelyOrLog(root);
}
}
代码示例来源:origin: gocd/gocd
@Test
public void shouldNotValidateNestingOfMaterialDirectoriesBasedOnServerSideFileSystem() throws IOException {
final File workingDir = temporaryFolder.newFolder("go-working-dir");
final File material1 = new File(workingDir, "material1");
material1.mkdirs();
final Path material2 = Files.createSymbolicLink(Paths.get(new File(workingDir, "material2").getPath()), Paths.get(material1.getPath()));
pluggableSCMMaterialConfig.setFolder(material1.getAbsolutePath());
pluggableSCMMaterialConfig.validateNotSubdirectoryOf(material2.toAbsolutePath().toString());
assertNull(pluggableSCMMaterialConfig.errors().getAllOn(FOLDER));
}
}
代码示例来源:origin: google/guava
public void testByteSource_size_ofSymlinkToDirectory() throws IOException {
try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
Path dir = fs.getPath("dir");
Files.createDirectory(dir);
Path link = fs.getPath("link");
Files.createSymbolicLink(link, dir);
ByteSource source = MoreFiles.asByteSource(link);
assertThat(source.sizeIfKnown()).isAbsent();
try {
source.size();
fail();
} catch (IOException expected) {
}
}
}
代码示例来源:origin: gocd/gocd
@Test
public void shouldNotValidateNestingOfMaterialDirectoriesBasedOnServerSideFileSystem() throws IOException {
final File workingDir = temporaryFolder.newFolder("go-working-dir");
final File material1 = new File(workingDir, "material1");
material1.mkdirs();
final Path material2 = Files.createSymbolicLink(Paths.get(new File(workingDir, "material2").getPath()), Paths.get(material1.getPath()));
material.setFolder(material1.getAbsolutePath());
material.validateNotSubdirectoryOf(material2.toAbsolutePath().toString());
assertNull(material.errors().getAllOn(FOLDER));
}
}
代码示例来源:origin: google/guava
public void testByteSource_size_ofSymlinkToRegularFile_nofollowLinks() throws IOException {
try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
Path file = fs.getPath("file");
Files.write(file, new byte[10]);
Path link = fs.getPath("link");
Files.createSymbolicLink(link, file);
ByteSource source = MoreFiles.asByteSource(link, NOFOLLOW_LINKS);
assertThat(source.sizeIfKnown()).isAbsent();
try {
source.size();
fail();
} catch (IOException expected) {
}
}
}
代码示例来源:origin: google/guava
public void testEqual_links() throws IOException {
try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
Path fooPath = fs.getPath("foo");
MoreFiles.asCharSink(fooPath, UTF_8).write("foo");
Path fooSymlink = fs.getPath("symlink");
Files.createSymbolicLink(fooSymlink, fooPath);
Path fooHardlink = fs.getPath("hardlink");
Files.createLink(fooHardlink, fooPath);
assertThat(MoreFiles.equal(fooPath, fooSymlink)).isTrue();
assertThat(MoreFiles.equal(fooPath, fooHardlink)).isTrue();
assertThat(MoreFiles.equal(fooSymlink, fooHardlink)).isTrue();
}
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void deleteQuietly_deletes_symbolicLink() throws IOException {
assumeTrue(SystemUtils.IS_OS_UNIX);
Path folder = temporaryFolder.newFolder().toPath();
Path file1 = Files.createFile(folder.resolve("file1.txt"));
Path symLink = Files.createSymbolicLink(folder.resolve("link1"), file1);
assertThat(file1).isRegularFile();
assertThat(symLink).isSymbolicLink();
FileUtils.deleteQuietly(symLink.toFile());
assertThat(symLink).doesNotExist();
assertThat(file1).isRegularFile();
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void deleteQuietly_deletes_symbolicLink() throws IOException {
assumeTrue(SystemUtils.IS_OS_UNIX);
Path folder = temporaryFolder.newFolder().toPath();
Path file1 = Files.createFile(folder.resolve("file1.txt"));
Path symLink = Files.createSymbolicLink(folder.resolve("link1"), file1);
assertThat(file1).isRegularFile();
assertThat(symLink).isSymbolicLink();
FileUtils2.deleteQuietly(symLink.toFile());
assertThat(symLink).doesNotExist();
assertThat(file1).isRegularFile();
}
代码示例来源:origin: neo4j/neo4j
@Test
void storeLayoutResolvesLinks() throws IOException
{
Path basePath = testDirectory.directory().toPath();
File storeDir = testDirectory.storeDir("notAbsolute");
Path linkPath = basePath.resolve( "link" );
Path symbolicLink = Files.createSymbolicLink( linkPath, storeDir.toPath() );
StoreLayout storeLayout = StoreLayout.of( symbolicLink.toFile() );
assertEquals( storeDir, storeLayout.storeDirectory() );
}
代码示例来源:origin: neo4j/neo4j
@Test
void databaseLayoutResolvesLinks() throws IOException
{
Path basePath = testDirectory.directory().toPath();
File databaseDir = testDirectory.databaseDir("notAbsolute");
Path linkPath = basePath.resolve( "link" );
Path symbolicLink = Files.createSymbolicLink( linkPath, databaseDir.toPath() );
DatabaseLayout databaseLayout = DatabaseLayout.of( symbolicLink.toFile() );
assertEquals( databaseLayout.databaseDirectory(), databaseDir );
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void deleteDirectory_throws_IOE_if_file_is_symbolicLink() throws IOException {
assumeTrue(SystemUtils.IS_OS_UNIX);
Path folder = temporaryFolder.newFolder().toPath();
Path file1 = Files.createFile(folder.resolve("file1.txt"));
Path symLink = Files.createSymbolicLink(folder.resolve("link1"), file1);
assertThat(file1).isRegularFile();
assertThat(symLink).isSymbolicLink();
expectedException.expect(IOException.class);
expectedException.expectMessage("Directory '" + symLink.toFile().getAbsolutePath() + "' is a symbolic link");
FileUtils.deleteDirectory(symLink.toFile());
}
代码示例来源:origin: google/guava
public void testPredicates() throws IOException {
Path file = createTempFile();
Path dir = tempDir.resolve("dir");
Files.createDirectory(dir);
assertTrue(MoreFiles.isDirectory().apply(dir));
assertFalse(MoreFiles.isRegularFile().apply(dir));
assertFalse(MoreFiles.isDirectory().apply(file));
assertTrue(MoreFiles.isRegularFile().apply(file));
Path symlinkToDir = tempDir.resolve("symlinkToDir");
Path symlinkToFile = tempDir.resolve("symlinkToFile");
Files.createSymbolicLink(symlinkToDir, dir);
Files.createSymbolicLink(symlinkToFile, file);
assertTrue(MoreFiles.isDirectory().apply(symlinkToDir));
assertFalse(MoreFiles.isRegularFile().apply(symlinkToDir));
assertFalse(MoreFiles.isDirectory().apply(symlinkToFile));
assertTrue(MoreFiles.isRegularFile().apply(symlinkToFile));
assertFalse(MoreFiles.isDirectory(NOFOLLOW_LINKS).apply(symlinkToDir));
assertFalse(MoreFiles.isRegularFile(NOFOLLOW_LINKS).apply(symlinkToFile));
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testReadSymLink() throws Exception {
// Symlinks require a modified security policy in Windows. -- See http://stackoverflow.com/questions/23217460/how-to-create-soft-symbolic-link-using-java-nio-files
Assume.assumeFalse(Utils.isWindows());
String fileName = "some-file.txt";
long fileSize = 1234;
createFileWithJunk(fileName, fileSize);
String linkName = "some-link.txt";
Files.createSymbolicLink(Paths.get(testDir + pathSep + linkName), Paths.get(fileName));
vertx.fileSystem().readSymlink(testDir + pathSep + linkName, ar -> {
if (ar.failed()) {
fail(ar.cause().getMessage());
} else {
assertEquals(fileName, ar.result());
testComplete();
}
});
await();
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testPropsDontFollowLink() throws Exception {
// Symlinks require a modified security policy in Windows. -- See http://stackoverflow.com/questions/23217460/how-to-create-soft-symbolic-link-using-java-nio-files
Assume.assumeFalse(Utils.isWindows());
String fileName = "some-file.txt";
long fileSize = 1234;
createFileWithJunk(fileName, fileSize);
String linkName = "some-link.txt";
Files.createSymbolicLink(Paths.get(testDir + pathSep + linkName), Paths.get(fileName));
testProps(linkName, true, true, st -> {
assertNotNull(st != null);
assertTrue(st.isSymbolicLink());
});
await();
}
内容来源于网络,如有侵权,请联系作者删除!