Java Code Examples for java.nio.file.attribute.BasicFileAttributes#isSymbolicLink()

The following examples show how to use java.nio.file.attribute.BasicFileAttributes#isSymbolicLink() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileTreeWalker.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the attributes of the given file, taking into account whether
 * the walk is following sym links is not. The {@code canUseCached}
 * argument determines whether this method can use cached attributes.
 */
private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
    throws IOException
{
    // if attributes are cached then use them if possible
    if (canUseCached &&
        (file instanceof BasicFileAttributesHolder) &&
        (System.getSecurityManager() == null))
    {
        BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
        if (cached != null && (!followLinks || !cached.isSymbolicLink())) {
            return cached;
        }
    }

    // attempt to get attributes of file. If fails and we are following
    // links then a link target might not exist so get attributes of link
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(file, BasicFileAttributes.class, linkOptions);
    } catch (IOException ioe) {
        if (!followLinks)
            throw ioe;

        // attempt to get attrmptes without following links
        attrs = Files.readAttributes(file,
                                     BasicFileAttributes.class,
                                     LinkOption.NOFOLLOW_LINKS);
    }
    return attrs;
}
 
Example 2
Source File: MCRFileAttributes.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public static FileType fromAttribute(BasicFileAttributes attrs) {
    if (attrs.isRegularFile()) {
        return file;
    }
    if (attrs.isDirectory()) {
        return directory;
    }
    if (attrs.isSymbolicLink()) {
        return link;
    }
    return other;
}
 
Example 3
Source File: FileTreeWalker.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the attributes of the given file, taking into account whether
 * the walk is following sym links is not. The {@code canUseCached}
 * argument determines whether this method can use cached attributes.
 */
private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
    throws IOException
{
    // if attributes are cached then use them if possible
    if (canUseCached &&
        (file instanceof BasicFileAttributesHolder) &&
        (System.getSecurityManager() == null))
    {
        BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
        if (cached != null && (!followLinks || !cached.isSymbolicLink())) {
            return cached;
        }
    }

    // attempt to get attributes of file. If fails and we are following
    // links then a link target might not exist so get attributes of link
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(file, BasicFileAttributes.class, linkOptions);
    } catch (IOException ioe) {
        if (!followLinks)
            throw ioe;

        // attempt to get attrmptes without following links
        attrs = Files.readAttributes(file,
                                     BasicFileAttributes.class,
                                     LinkOption.NOFOLLOW_LINKS);
    }
    return attrs;
}
 
Example 4
Source File: FileTreeWalker.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the attributes of the given file, taking into account whether
 * the walk is following sym links is not. The {@code canUseCached}
 * argument determines whether this method can use cached attributes.
 */
private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
    throws IOException
{
    // if attributes are cached then use them if possible
    if (canUseCached &&
        (file instanceof BasicFileAttributesHolder) &&
        (System.getSecurityManager() == null))
    {
        BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
        if (cached != null && (!followLinks || !cached.isSymbolicLink())) {
            return cached;
        }
    }

    // attempt to get attributes of file. If fails and we are following
    // links then a link target might not exist so get attributes of link
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(file, BasicFileAttributes.class, linkOptions);
    } catch (IOException ioe) {
        if (!followLinks)
            throw ioe;

        // attempt to get attrmptes without following links
        attrs = Files.readAttributes(file,
                                     BasicFileAttributes.class,
                                     LinkOption.NOFOLLOW_LINKS);
    }
    return attrs;
}
 
Example 5
Source File: FileTreeWalker.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the attributes of the given file, taking into account whether
 * the walk is following sym links is not. The {@code canUseCached}
 * argument determines whether this method can use cached attributes.
 */
private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
    throws IOException
{
    // if attributes are cached then use them if possible
    if (canUseCached &&
        (file instanceof BasicFileAttributesHolder) &&
        (System.getSecurityManager() == null))
    {
        BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
        if (cached != null && (!followLinks || !cached.isSymbolicLink())) {
            return cached;
        }
    }

    // attempt to get attributes of file. If fails and we are following
    // links then a link target might not exist so get attributes of link
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(file, BasicFileAttributes.class, linkOptions);
    } catch (IOException ioe) {
        if (!followLinks)
            throw ioe;

        // attempt to get attrmptes without following links
        attrs = Files.readAttributes(file,
                                     BasicFileAttributes.class,
                                     LinkOption.NOFOLLOW_LINKS);
    }
    return attrs;
}
 
Example 6
Source File: PrintFiles.java    From java-tutorial with Apache License 2.0 5 votes vote down vote up
@Override
public FileVisitResult visitFile(Path file,
                                 BasicFileAttributes attr) {
    if (attr.isSymbolicLink()) {
        System.out.format("Symbolic link: %s ", file);
    } else if (attr.isRegularFile()) {
        System.out.format("Regular file: %s ", file);
    } else {
        System.out.format("Other: %s ", file);
    }
    System.out.println("(" + attr.size() + "bytes)");
    return CONTINUE;
}
 
Example 7
Source File: BasicFileAttributeManager.java    From yajsync with GNU General Public License v3.0 5 votes vote down vote up
private int toMode(BasicFileAttributes attrs)
{
    if (attrs.isDirectory()) {
        return FileOps.S_IFDIR | _defaultDirectoryPermissions;
    } else if (attrs.isRegularFile()) {
        return FileOps.S_IFREG | _defaultFilePermissions;
    } else if (attrs.isSymbolicLink()) {
        return FileOps.S_IFLNK | _defaultFilePermissions;
    } else {
        return FileOps.S_IFUNK;
    }
}
 
Example 8
Source File: FileTreeWalker.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the attributes of the given file, taking into account whether
 * the walk is following sym links is not. The {@code canUseCached}
 * argument determines whether this method can use cached attributes.
 */
private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
    throws IOException
{
    // if attributes are cached then use them if possible
    if (canUseCached &&
        (file instanceof BasicFileAttributesHolder) &&
        (System.getSecurityManager() == null))
    {
        BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
        if (cached != null && (!followLinks || !cached.isSymbolicLink())) {
            return cached;
        }
    }

    // attempt to get attributes of file. If fails and we are following
    // links then a link target might not exist so get attributes of link
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(file, BasicFileAttributes.class, linkOptions);
    } catch (IOException ioe) {
        if (!followLinks)
            throw ioe;

        // attempt to get attrmptes without following links
        attrs = Files.readAttributes(file,
                                     BasicFileAttributes.class,
                                     LinkOption.NOFOLLOW_LINKS);
    }
    return attrs;
}
 
Example 9
Source File: FileTreeWalker.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the attributes of the given file, taking into account whether
 * the walk is following sym links is not. The {@code canUseCached}
 * argument determines whether this method can use cached attributes.
 */
private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
    throws IOException
{
    // if attributes are cached then use them if possible
    if (canUseCached &&
        (file instanceof BasicFileAttributesHolder) &&
        (System.getSecurityManager() == null))
    {
        BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
        if (cached != null && (!followLinks || !cached.isSymbolicLink())) {
            return cached;
        }
    }

    // attempt to get attributes of file. If fails and we are following
    // links then a link target might not exist so get attributes of link
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(file, BasicFileAttributes.class, linkOptions);
    } catch (IOException ioe) {
        if (!followLinks)
            throw ioe;

        // attempt to get attrmptes without following links
        attrs = Files.readAttributes(file,
                                     BasicFileAttributes.class,
                                     LinkOption.NOFOLLOW_LINKS);
    }
    return attrs;
}
 
Example 10
Source File: FileTreeWalker.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the attributes of the given file, taking into account whether
 * the walk is following sym links is not. The {@code canUseCached}
 * argument determines whether this method can use cached attributes.
 */
private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
    throws IOException
{
    // if attributes are cached then use them if possible
    if (canUseCached &&
        (file instanceof BasicFileAttributesHolder) &&
        (System.getSecurityManager() == null))
    {
        BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
        if (cached != null && (!followLinks || !cached.isSymbolicLink())) {
            return cached;
        }
    }

    // attempt to get attributes of file. If fails and we are following
    // links then a link target might not exist so get attributes of link
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(file, BasicFileAttributes.class, linkOptions);
    } catch (IOException ioe) {
        if (!followLinks)
            throw ioe;

        // attempt to get attrmptes without following links
        attrs = Files.readAttributes(file,
                                     BasicFileAttributes.class,
                                     LinkOption.NOFOLLOW_LINKS);
    }
    return attrs;
}
 
Example 11
Source File: HashedDir.java    From Launcher with GNU General Public License v3.0 5 votes vote down vote up
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    // Verify is not symlink
    if (!allowSymlinks && attrs.isSymbolicLink())
        throw new SecurityException("Symlinks are not allowed");

    // Add file (may be unhashed, if exclusion)
    path.add(IOHelper.getFileName(file));
    boolean doDigest = digest && (matcher == null || matcher.shouldUpdate(path));
    current.map.put(path.removeLast(), new HashedFile(file, attrs.size(), doDigest));
    return super.visitFile(file, attrs);
}
 
Example 12
Source File: FileTreeWalker.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the attributes of the given file, taking into account whether
 * the walk is following sym links is not. The {@code canUseCached}
 * argument determines whether this method can use cached attributes.
 */
private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
    throws IOException
{
    // if attributes are cached then use them if possible
    if (canUseCached &&
        (file instanceof BasicFileAttributesHolder) &&
        (System.getSecurityManager() == null))
    {
        BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
        if (cached != null && (!followLinks || !cached.isSymbolicLink())) {
            return cached;
        }
    }

    // attempt to get attributes of file. If fails and we are following
    // links then a link target might not exist so get attributes of link
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(file, BasicFileAttributes.class, linkOptions);
    } catch (IOException ioe) {
        if (!followLinks)
            throw ioe;

        // attempt to get attrmptes without following links
        attrs = Files.readAttributes(file,
                                     BasicFileAttributes.class,
                                     LinkOption.NOFOLLOW_LINKS);
    }
    return attrs;
}
 
Example 13
Source File: FileTreeWalker.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Returns the attributes of the given file, taking into account whether
 * the walk is following sym links is not. The {@code canUseCached}
 * argument determines whether this method can use cached attributes.
 */
private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
    throws IOException
{
    // if attributes are cached then use them if possible
    if (canUseCached &&
        (file instanceof BasicFileAttributesHolder) &&
        (System.getSecurityManager() == null))
    {
        BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
        if (cached != null && (!followLinks || !cached.isSymbolicLink())) {
            return cached;
        }
    }

    // attempt to get attributes of file. If fails and we are following
    // links then a link target might not exist so get attributes of link
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(file, BasicFileAttributes.class, linkOptions);
    } catch (IOException ioe) {
        if (!followLinks)
            throw ioe;

        // attempt to get attrmptes without following links
        attrs = Files.readAttributes(file,
                                     BasicFileAttributes.class,
                                     LinkOption.NOFOLLOW_LINKS);
    }
    return attrs;
}
 
Example 14
Source File: FileTreeWalker.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the attributes of the given file, taking into account whether
 * the walk is following sym links is not. The {@code canUseCached}
 * argument determines whether this method can use cached attributes.
 */
private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
    throws IOException
{
    // if attributes are cached then use them if possible
    if (canUseCached &&
        (file instanceof BasicFileAttributesHolder) &&
        (System.getSecurityManager() == null))
    {
        BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
        if (cached != null && (!followLinks || !cached.isSymbolicLink())) {
            return cached;
        }
    }

    // attempt to get attributes of file. If fails and we are following
    // links then a link target might not exist so get attributes of link
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(file, BasicFileAttributes.class, linkOptions);
    } catch (IOException ioe) {
        if (!followLinks)
            throw ioe;

        // attempt to get attrmptes without following links
        attrs = Files.readAttributes(file,
                                     BasicFileAttributes.class,
                                     LinkOption.NOFOLLOW_LINKS);
    }
    return attrs;
}
 
Example 15
Source File: FileTreeWalker.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the attributes of the given file, taking into account whether
 * the walk is following sym links is not. The {@code canUseCached}
 * argument determines whether this method can use cached attributes.
 */
private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
    throws IOException
{
    // if attributes are cached then use them if possible
    if (canUseCached &&
        (file instanceof BasicFileAttributesHolder) &&
        (System.getSecurityManager() == null))
    {
        BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
        if (cached != null && (!followLinks || !cached.isSymbolicLink())) {
            return cached;
        }
    }

    // attempt to get attributes of file. If fails and we are following
    // links then a link target might not exist so get attributes of link
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(file, BasicFileAttributes.class, linkOptions);
    } catch (IOException ioe) {
        if (!followLinks)
            throw ioe;

        // attempt to get attrmptes without following links
        attrs = Files.readAttributes(file,
                                     BasicFileAttributes.class,
                                     LinkOption.NOFOLLOW_LINKS);
    }
    return attrs;
}
 
Example 16
Source File: FileTreeWalker.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the attributes of the given file, taking into account whether
 * the walk is following sym links is not. The {@code canUseCached}
 * argument determines whether this method can use cached attributes.
 */
private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
    throws IOException
{
    // if attributes are cached then use them if possible
    if (canUseCached &&
        (file instanceof BasicFileAttributesHolder) &&
        (System.getSecurityManager() == null))
    {
        BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
        if (cached != null && (!followLinks || !cached.isSymbolicLink())) {
            return cached;
        }
    }

    // attempt to get attributes of file. If fails and we are following
    // links then a link target might not exist so get attributes of link
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(file, BasicFileAttributes.class, linkOptions);
    } catch (IOException ioe) {
        if (!followLinks)
            throw ioe;

        // attempt to get attrmptes without following links
        attrs = Files.readAttributes(file,
                                     BasicFileAttributes.class,
                                     LinkOption.NOFOLLOW_LINKS);
    }
    return attrs;
}
 
Example 17
Source File: FileTreeWalker.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the attributes of the given file, taking into account whether
 * the walk is following sym links is not. The {@code canUseCached}
 * argument determines whether this method can use cached attributes.
 */
private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
    throws IOException
{
    // if attributes are cached then use them if possible
    if (canUseCached &&
        (file instanceof BasicFileAttributesHolder) &&
        (System.getSecurityManager() == null))
    {
        BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
        if (cached != null && (!followLinks || !cached.isSymbolicLink())) {
            return cached;
        }
    }

    // attempt to get attributes of file. If fails and we are following
    // links then a link target might not exist so get attributes of link
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(file, BasicFileAttributes.class, linkOptions);
    } catch (IOException ioe) {
        if (!followLinks)
            throw ioe;

        // attempt to get attrmptes without following links
        attrs = Files.readAttributes(file,
                                     BasicFileAttributes.class,
                                     LinkOption.NOFOLLOW_LINKS);
    }
    return attrs;
}
 
Example 18
Source File: SysToolkit.java    From DAFramework with MIT License 4 votes vote down vote up
public static boolean isSymbolicLink(String path) throws Exception {
	BasicFileAttributes attrs = Files.readAttributes(FileSystems.getDefault().getPath(polishFilePath(path)), BasicFileAttributes.

	class, LinkOption.NOFOLLOW_LINKS);
	return attrs.isSymbolicLink();
}
 
Example 19
Source File: SysToolkit.java    From wES with MIT License 4 votes vote down vote up
public static boolean isSymbolicLink(String path) throws Exception {
	BasicFileAttributes attrs = Files.readAttributes(FileSystems.getDefault().getPath(polishFilePath(path)), BasicFileAttributes.

			class, LinkOption.NOFOLLOW_LINKS);
	return attrs.isSymbolicLink();
}
 
Example 20
Source File: IsFileSymbolicLink.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void file_is_symbolic_link_nio() throws IOException {

	BasicFileAttributes attr = Files.readAttributes(source,
			BasicFileAttributes.class);

	boolean isSymbolicLink = attr.isSymbolicLink();

	assertFalse(isSymbolicLink);
}