public static String readSymlinkTarget(File file) { try { return Files.readSymbolicLink(Paths.get(file.getAbsolutePath())).toString(); } catch (IOException e) { return null; } }
@Test public void testCreateRelativeSymlinkToFilesInRoot() throws IOException { ProjectFilesystem projectFilesystem = new ProjectFilesystem(tmp.getRoot()); tmp.newFile("biz.txt"); Path pathToDesiredLinkUnderProjectRoot = Paths.get("gamma.txt"); Path pathToExistingFileUnderProjectRoot = Paths.get("biz.txt"); Path relativePath = MorePaths.createRelativeSymlink( pathToDesiredLinkUnderProjectRoot, pathToExistingFileUnderProjectRoot, projectFilesystem); assertEquals("biz.txt", relativePath.toString()); Path absolutePathToDesiredLinkUnderProjectRoot = projectFilesystem.resolve(pathToDesiredLinkUnderProjectRoot); assertTrue(Files.isSymbolicLink(absolutePathToDesiredLinkUnderProjectRoot)); Path targetOfSymbolicLink = Files.readSymbolicLink(absolutePathToDesiredLinkUnderProjectRoot); assertEquals(relativePath, targetOfSymbolicLink); Path absolutePathToExistingFileUnderProjectRoot = projectFilesystem.resolve(pathToExistingFileUnderProjectRoot); Files.write(absolutePathToExistingFileUnderProjectRoot, "Hello, World!".getBytes()); String dataReadFromSymlink = new String(Files.readAllBytes(absolutePathToDesiredLinkUnderProjectRoot)); assertEquals("Hello, World!", dataReadFromSymlink); }
/** * Read local data revision number. Db data directory is a symbol link to a data revision * directory as the following /data/db/1 -> /data/db/1459567039514.0 Here data version number is * 1459567039514 and 0 is incremental snapshot number. It is always 0 for db revisions * * @return * @throws IOException */ private String getLocalDataRevision() { Path dbDataDir = Paths.get(dbDir, "1"); try { if (Files.isSymbolicLink(dbDataDir)) { Path symDir = Files.readSymbolicLink(dbDataDir); String versionName = symDir.toFile().getName(); int i = versionName.lastIndexOf("."); return versionName.substring(0, i); } } catch (Exception ex) { _log.error("Retrieve local data revision error", ex); } return null; }
public FileAttrs getAttrs() throws IOException { FileAttrs result; BasicFileAttributes attr; DosFileAttributes dosAttr; PosixFileAttributes posixAttr; result = new FileAttrs(); attr = Files.readAttributes(this.path.toPath(), BasicFileAttributes.class); result.setCtime(attr.creationTime().toMillis()); result.setMtime(attr.lastModifiedTime().toMillis()); // result.append("symlink", attr.isSymbolicLink()); //Redundant result.setSize(attr.size()); if (System.getProperty("os.name").startsWith("Windows")) { dosAttr = Files.readAttributes(this.path.toPath(), DosFileAttributes.class); result.setDosArchive(dosAttr.isArchive()); result.setDosHidden(dosAttr.isHidden()); result.setDosReadonly(dosAttr.isReadOnly()); result.setDosSystem(dosAttr.isSystem()); } else { posixAttr = Files.readAttributes(this.path.toPath(), PosixFileAttributes.class); result.setPosixSymlink(this.isSymlink()); if (result.getPosixSymlink()) { result.setLinkTo(Files.readSymbolicLink(this.path.toPath()).toString()); } result.setPosixOwner(posixAttr.owner().getName()); result.setPosixGroup(posixAttr.group().getName()); result.setPosixPermission(PosixFilePermissions.toString(posixAttr.permissions())); } return result; }
private static void assertIsSymbolicLink(Path link, Path target) throws IOException { assertTrue(Files.isSymbolicLink(link)); assertEquals(target, Files.readSymbolicLink(link)); }
private TarArchiveEntry buildTarArchiveEntry(ProjectArchive project, Path absPath, String name) throws IOException { TarArchiveEntry e; if (Files.isSymbolicLink(absPath)) { e = new TarArchiveEntry(name, TarConstants.LF_SYMLINK); Path rawDest = Files.readSymbolicLink(absPath); Path normalizedAbsDest = absPath.getParent().resolve(rawDest).normalize(); try { project.pathToResourceName(normalizedAbsDest); } catch (IllegalArgumentException ex) { throw new IllegalArgumentException("Invalid symbolic link: " + ex.getMessage()); } // absolute path will be invalid on a server. convert it to a relative path Path normalizedRelativeDest = absPath.getParent().relativize(normalizedAbsDest); String linkName = normalizedRelativeDest.toString(); // TarArchiveEntry(File) does this normalization but setLinkName doesn't. So do it here: linkName = linkName.replace(File.separatorChar, '/'); e.setLinkName(linkName); } else { e = new TarArchiveEntry(absPath.toFile(), name); try { int mode = 0; for (PosixFilePermission perm : Files.getPosixFilePermissions(absPath)) { switch (perm) { case OWNER_READ: mode |= 0400; break; case OWNER_WRITE: mode |= 0200; break; case OWNER_EXECUTE: mode |= 0100; break; case GROUP_READ: mode |= 0040; break; case GROUP_WRITE: mode |= 0020; break; case GROUP_EXECUTE: mode |= 0010; break; case OTHERS_READ: mode |= 0004; break; case OTHERS_WRITE: mode |= 0002; break; case OTHERS_EXECUTE: mode |= 0001; break; default: // ignore } } e.setMode(mode); } catch (UnsupportedOperationException ex) { // ignore custom mode } } return e; }
/** Returns the target of the specified symbolic link. */ public Path readSymLink(Path path) throws IOException { return Files.readSymbolicLink(getPathForRelativePath(path)); }