FrameworkUploader implements Runnable { @VisibleForTesting boolean checkSymlink(File jar) { if (Files.isSymbolicLink(jar.toPath())) { try { java.nio.file.Path link = Files.readSymbolicLink(jar.toPath()); java.nio.file.Path jarPath = Paths.get(jar.getAbsolutePath()); String linkString = link.toString(); java.nio.file.Path jarParent = jarPath.getParent(); java.nio.file.Path linkPath = jarParent == null ? null : jarParent.resolve(linkString); java.nio.file.Path linkPathParent = linkPath == null ? null : linkPath.getParent(); java.nio.file.Path normalizedLinkPath = linkPathParent == null ? null : linkPathParent.normalize(); if (normalizedLinkPath != null && jarParent.normalize().equals( normalizedLinkPath)) { LOG.info(String.format("Ignoring same directory link %s to %s", jarPath.toString(), link.toString())); return true; } } catch (NotLinkException ex) { LOG.debug("Not a link", jar); } catch (IOException ex) { LOG.warn("Cannot read symbolic link on", jar); } } return false; }  void run(); static void main(String[] args);  }
@Test public void testNativeIO() throws IOException { FrameworkUploader uploader = new FrameworkUploader(); File parent = new File(testDir); try { parent.deleteOnExit(); Assert.assertTrue(parent.mkdirs()); File targetFile = new File(parent, "a.txt"); try(FileOutputStream os = new FileOutputStream(targetFile)) { IOUtils.writeLines(Lists.newArrayList("a", "b"), null, os); } Assert.assertFalse(uploader.checkSymlink(targetFile)); File symlinkToTarget = new File(parent, "symlinkToTarget.txt"); try { Files.createSymbolicLink( Paths.get(symlinkToTarget.getAbsolutePath()), Paths.get(targetFile.getAbsolutePath())); } catch (UnsupportedOperationException e) { Assume.assumeTrue(false); } Assert.assertTrue(uploader.checkSymlink(symlinkToTarget)); symlinkToTarget = new File(parent.getAbsolutePath() + "/./symlinkToTarget2.txt"); try { Files.createSymbolicLink( Paths.get(symlinkToTarget.getAbsolutePath()), Paths.get(targetFile.getAbsolutePath())); } catch (UnsupportedOperationException e) { Assume.assumeTrue(false); } Assert.assertTrue(uploader.checkSymlink(symlinkToTarget)); File symlinkOutside = new File(parent, "symlinkToParent.txt"); try { Files.createSymbolicLink( Paths.get(symlinkOutside.getAbsolutePath()), Paths.get(parent.getAbsolutePath())); } catch (UnsupportedOperationException e) { Assume.assumeTrue(false); } Assert.assertFalse(uploader.checkSymlink(symlinkOutside)); } finally { FileUtils.deleteDirectory(parent); } }