@Override public Path toRealPath(LinkOption... options) throws IOException { return delegate.toRealPath(options); }
private static Path normalize(Path path) { try { return path.normalize().toRealPath(LinkOption.NOFOLLOW_LINKS); } catch (IOException e) { return path.normalize(); } }
private boolean isIllegalSymlink() { // TODO JENKINS-26838 try { String myPath = f.toPath().toRealPath(new LinkOption[0]).toString(); String rootPath = root.toPath().toRealPath(new LinkOption[0]).toString(); if (!myPath.equals(rootPath) && !myPath.startsWith(rootPath + File.separatorChar)) { return true; } } catch (IOException x) { Logger.getLogger(VirtualFile.class.getName()).log(Level.FINE, "could not determine symlink status of " + f, x); } catch (InvalidPathException x2) { // if this cannot be converted to a path, it cannot be an illegal symlink, as it cannot exist Logger.getLogger(VirtualFile.class.getName()).log(Level.FINE, "Could not convert " + f + " to path", x2); } return false; }
/** * Security check for case insensitive file systems. * We make sure the case of the filename matches the case of the request. * This is only a check for case sensitivity, not for non canonical . and ../ which are allowed. * * For example: * file.getName() == "page.jsp" && file.getCanonicalFile().getName() == "page.jsp" should return true * file.getName() == "page.jsp" && file.getCanonicalFile().getName() == "page.JSP" should return false * file.getName() == "./page.jsp" && file.getCanonicalFile().getName() == "page.jsp" should return true */ private boolean isFileSameCase(final Path file, String normalizeFile) throws IOException { String canonicalName = file.toRealPath().toString(); return canonicalName.equals(normalizeFile); }
/** * Canonical file resolution on windows does not resolve links. * Real paths on windows can be resolved only using {@link Path#toRealPath(LinkOption...)}, but file should exist in that case. * We will try to do as much as possible and will try to use {@link Path#toRealPath(LinkOption...)} when file exist and will fallback to only * use {@link File#getCanonicalFile()} if file does not exist. * see JDK-8003887 for details * @param file - file to resolve canonical representation * @return canonical file representation. */ public static File getCanonicalFile( File file ) { try { File fileToResolve = file.exists() ? file.toPath().toRealPath().toFile() : file; return fileToResolve.getCanonicalFile(); } catch ( IOException e ) { throw new UncheckedIOException( e ); } }
private static Path initBaseDir(ProjectDefinition module) { Path result; try { result = module.getBaseDir().toPath().toRealPath(LinkOption.NOFOLLOW_LINKS); } catch (IOException e) { throw new IllegalStateException("Unable to resolve module baseDir", e); } return result; }
private static Path createTempFolder(Path workingPath) { try { Path realPath = workingPath; if (Files.isSymbolicLink(realPath)) { realPath = realPath.toRealPath(); } Files.createDirectories(realPath); } catch (IOException e) { throw new IllegalStateException("Failed to create working path: " + workingPath, e); } try { return Files.createTempDirectory(workingPath, TMP_NAME_PREFIX); } catch (IOException e) { throw new IllegalStateException("Failed to create temporary folder in " + workingPath, e); } }
private File getWorkingDocsDirectory(final File componentDocsDirPath) { File workingDocsDirectory = null; try { workingDocsDirectory = componentDocsDirPath.toPath().toRealPath().getParent().toFile(); } catch (IOException ex) { logger.error("Failed to load :" + componentDocsDirPath.getAbsolutePath()); startUpFailure(ex); } return workingDocsDirectory; }
/** * @param path Path to resolve only relative to IGNITE_HOME. * @return Resolved path as file, or {@code null} if path cannot be resolved. * @throws IOException If failed to resolve path. */ public static File resolveIgnitePath(String path) throws IOException { File folder = U.resolveIgnitePath(path); if (folder == null) return null; if (!folder.toPath().toRealPath(LinkOption.NOFOLLOW_LINKS).startsWith(Paths.get(U.getIgniteHome()))) return null; return folder; }
public static void main(String[] args) throws IOException { final Path rootDirectory = Paths.get(args[0]).resolve("..").toRealPath(); DeadLinksChecker deadLinksChecker = new DeadLinksChecker(); deadLinksChecker.checkDeadLinks(rootDirectory); }
public void assertHasNoParent(final AssertionInfo info, final Path actual) { assertNotNull(info, actual); try { final Path canonicalActual = actual.toRealPath(); if (canonicalActual.getParent() != null) throw failures.failure(info, shouldHaveNoParent(actual)); } catch (IOException e) { throw new PathsException(FAILED_TO_RESOLVE_ACTUAL_REAL_PATH, e); } }
public void assertIsCanonical(final AssertionInfo info, final Path actual) { assertNotNull(info, actual); try { if (!actual.equals(actual.toRealPath())) throw failures.failure(info, shouldBeCanonicalPath(actual)); } catch (IOException e) { throw new PathsException(FAILED_TO_RESOLVE_ACTUAL_REAL_PATH, e); } }
public void assertStartsWith(final AssertionInfo info, final Path actual, final Path start) { assertNotNull(info, actual); assertExpectedStartPathIsNotNull(start); final Path canonicalActual; try { canonicalActual = actual.toRealPath(); } catch (IOException e) { throw new PathsException(FAILED_TO_RESOLVE_ACTUAL_REAL_PATH, e); } final Path canonicalOther; try { canonicalOther = start.toRealPath(); } catch (IOException e) { throw new PathsException(FAILED_TO_RESOLVE_ARGUMENT_REAL_PATH, e); } if (!canonicalActual.startsWith(canonicalOther)) throw failures.failure(info, shouldStartWith(actual, start)); }
public void assertIsCanonical(final AssertionInfo info, final Path actual) { assertNotNull(info, actual); try { if (!actual.equals(actual.toRealPath())) throw failures.failure(info, shouldBeCanonicalPath(actual)); } catch (IOException e) { throw new PathsException(FAILED_TO_RESOLVE_ACTUAL_REAL_PATH, e); } }
public void testCreateParentDirectories_root() throws IOException { Path root = root(); assertNull(root.getParent()); assertNull(root.toRealPath().getParent()); MoreFiles.createParentDirectories(root); // test that there's no exception }
public void assertHasNoParent(final AssertionInfo info, final Path actual) { assertNotNull(info, actual); try { final Path canonicalActual = actual.toRealPath(); if (canonicalActual.getParent() != null) throw failures.failure(info, shouldHaveNoParent(actual)); } catch (IOException e) { throw new PathsException(FAILED_TO_RESOLVE_ACTUAL_REAL_PATH, e); } }
public void assertEndsWith(final AssertionInfo info, final Path actual, final Path end) { assertNotNull(info, actual); assertExpectedEndPathIsNotNull(end); try { final Path canonicalActual = actual.toRealPath(); if (!canonicalActual.endsWith(end.normalize())) throw failures.failure(info, shouldEndWith(actual, end)); } catch (IOException e) { throw new PathsException(FAILED_TO_RESOLVE_ACTUAL_REAL_PATH, e); } }
public void testDeleteRecursively_nonDirectoryFile() throws IOException { try (FileSystem fs = newTestFileSystem(SECURE_DIRECTORY_STREAM)) { Path file = fs.getPath("dir/a"); assertTrue(Files.isRegularFile(file, NOFOLLOW_LINKS)); MoreFiles.deleteRecursively(file); assertFalse(Files.exists(file, NOFOLLOW_LINKS)); Path symlink = fs.getPath("/symlinktodir"); assertTrue(Files.isSymbolicLink(symlink)); Path realSymlinkTarget = symlink.toRealPath(); assertTrue(Files.isDirectory(realSymlinkTarget, NOFOLLOW_LINKS)); MoreFiles.deleteRecursively(symlink); assertFalse(Files.exists(symlink, NOFOLLOW_LINKS)); assertTrue(Files.isDirectory(realSymlinkTarget, NOFOLLOW_LINKS)); } }
public void assertEndsWith(final AssertionInfo info, final Path actual, final Path end) { assertNotNull(info, actual); assertExpectedEndPathIsNotNull(end); try { final Path canonicalActual = actual.toRealPath(); if (!canonicalActual.endsWith(end.normalize())) throw failures.failure(info, shouldEndWith(actual, end)); } catch (IOException e) { throw new PathsException(FAILED_TO_RESOLVE_ACTUAL_REAL_PATH, e); } }
@Test public void testSetPath() throws Exception { Path cwd = Paths.get("."); Map<String,Object> map = new HashMap<>(); ConfigUtils.set(map, "cwd", cwd); ConfigUtils.set(map, "temp", Paths.get("/tmp")); assertEquals("\"" + cwd.toRealPath(LinkOption.NOFOLLOW_LINKS).toUri() + "\"", map.get("cwd")); assertEquals("\"file:///tmp/\"", map.get("temp")); }