Example usage for java.nio.file Files isSymbolicLink

List of usage examples for java.nio.file Files isSymbolicLink

Introduction

In this page you can find the example usage for java.nio.file Files isSymbolicLink.

Prototype

public static boolean isSymbolicLink(Path path) 

Source Link

Document

Tests whether a file is a symbolic link.

Usage

From source file:hudson.UtilSEC904Test.java

@Test
public void resolveSymlinkToFile() throws Exception {
    //  root/*w w  w  .  ja v a 2 s  .c om*/
    //      /a
    //          /aa
    //              aa.txt
    //          /_b => symlink to /root/b
    //      /b
    //          /_a => symlink to /root/a
    File root = tmp.getRoot();
    File a = new File(root, "a");
    File aa = new File(a, "aa");
    aa.mkdirs();
    File aaTxt = new File(aa, "aa.txt");
    FileUtils.write(aaTxt, "aa");

    File b = new File(root, "b");
    b.mkdir();

    File _a = new File(b, "_a");
    Util.createSymlink(_a.getParentFile(), a.getAbsolutePath(), _a.getName(), TaskListener.NULL);

    File _b = new File(a, "_b");
    Util.createSymlink(_b.getParentFile(), b.getAbsolutePath(), _b.getName(), TaskListener.NULL);

    assertTrue(Files.isSymbolicLink(_a.toPath()));
    assertTrue(Files.isSymbolicLink(_b.toPath()));

    // direct symlinks are resolved
    assertEquals(Util.resolveSymlinkToFile(_a), a);
    assertEquals(Util.resolveSymlinkToFile(_b), b);

    // intermediate symlinks are NOT resolved
    assertNull(Util.resolveSymlinkToFile(new File(_a, "aa")));
    assertNull(Util.resolveSymlinkToFile(new File(_a, "aa/aa.txt")));
}

From source file:controller.FileWatcher.java

@Override
public void onFileCreate(File arg0) {
    String log = "File is created " + arg0.getAbsolutePath() + "\n";
    System.out.println(log);//from ww  w . java2 s .c  o  m
    jTextArea1.append(log);
    if (!Files.isSymbolicLink(arg0.toPath())) {
        String fileName = ServerUtil.convertPath(arg0.getAbsolutePath(), MyDropboxSwing.urls);
        FileCreate fileCreate = new FileCreate(fileName, Constants.IS_FILE);
        lstCommit.add(fileCreate);
    }
}

From source file:org.sonar.process.FileUtils.java

/**
 * Deletes a directory recursively. Does not support symbolic link to directories.
 *
 * @param directory  directory to delete
 * @throws IOException in case deletion is unsuccessful
 *//* w w w  . ja  va  2s .co m*/
public static void deleteDirectory(File directory) throws IOException {
    requireNonNull(directory, DIRECTORY_CAN_NOT_BE_NULL);

    if (!directory.exists()) {
        return;
    }

    Path path = directory.toPath();
    if (Files.isSymbolicLink(path)) {
        throw new IOException(format("Directory '%s' is a symbolic link", directory));
    }
    if (directory.isFile()) {
        throw new IOException(format("Directory '%s' is a file", directory));
    }
    deleteDirectoryImpl(path);

    if (directory.exists()) {
        throw new IOException(format("Unable to delete directory '%s'", directory));
    }
}

From source file:com.liferay.sync.engine.util.FileUtil.java

public static boolean isIgnoredFilePath(Path filePath) throws Exception {
    String fileName = String.valueOf(filePath.getFileName());

    if (_syncFileIgnoreNames.contains(fileName)
            || (PropsValues.SYNC_FILE_IGNORE_HIDDEN && Files.isHidden(filePath))
            || Files.isSymbolicLink(filePath) || fileName.endsWith(".lnk")) {

        return true;
    }//from w ww. j a  v a2  s. co  m

    return false;
}

From source file:controller.FileWatcher.java

@Override
public void onFileDelete(File arg0) {
    if (!Files.isSymbolicLink(arg0.toPath())) {
        String fileName = ServerUtil.convertPath(arg0.getAbsolutePath(), MyDropboxSwing.urls);
        FileDelete fileDelete = new FileDelete(fileName, Constants.IS_FILE);
        lstCommit.add(fileDelete);//from ww w .  j a va 2s  .  co m
    }
}

From source file:org.apache.solr.util.FileUtils.java

public static Path createDirectories(Path path) throws IOException {
    if (Files.exists(path) && Files.isSymbolicLink(path)) {
        Path real = path.toRealPath();
        if (Files.isDirectory(real))
            return real;
        throw new FileExistsException(
                "Tried to create a directory at to an existing non-directory symlink: " + path.toString());
    }//www  .jav a  2s .co  m
    return Files.createDirectories(path);
}

From source file:org.sonar.core.util.FileUtils.java

private static void deleteDirectoryImpl(Path path) throws IOException {
    requireNonNull(path, DIRECTORY_CAN_NOT_BE_NULL);
    File file = path.toFile();//from w w w .  j  a  v  a2s  .  c o m
    if (!file.exists()) {
        return;
    }

    checkIO(!Files.isSymbolicLink(path), "Directory '%s' is a symbolic link", path);
    checkIO(!file.isFile(), "Directory '%s' is a file", path);

    Files.walkFileTree(path, DeleteRecursivelyFileVisitor.INSTANCE);

    checkIO(!file.exists(), "Unable to delete directory '%s'", path);
}

From source file:nl.mpi.lamus.filesystem.implementation.LamusWorkspaceFileHandler.java

/**
 * @see WorkspaceFileHandler#moveFile(java.io.File, java.io.File)
 *//* ww  w .j a va  2 s. c  om*/
@Override
public void moveFile(File originNodeFile, File targetNodeFile) throws IOException {
    //file was probably not uploaded via lamus. Move target file if symbolic link
    if (Files.isSymbolicLink(originNodeFile.toPath())) {
        File originLinkToNodeFile = originNodeFile;
        originNodeFile = Files.readSymbolicLink(originNodeFile.toPath()).toFile();
        moveOrCopyFile(originNodeFile, targetNodeFile);
        Files.delete(originLinkToNodeFile.toPath());
    } else {
        moveOrCopyFile(originNodeFile, targetNodeFile);
    }
}

From source file:VOBackupFile.java

/**
 * Wrapper for metadata of backuped file.
 *
 * @param file backuped file//  w  ww  .j a va  2  s . c o m
 */
public VOBackupFile(File file) {
    this.path = file.getAbsolutePath();
    this.directory = file.isDirectory();
    this.symLink = Files.isSymbolicLink(file.toPath());

    this.size = ((file.isDirectory() || symLink) ? 0 : file.length());
    this.modify = new Date(file.lastModified());

    if (symLink) {
        try {
            symlinkTarget = Files.readSymbolicLink(file.toPath()).toString();
        } catch (IOException e) {
            e.printStackTrace(); //TODO Lebeda - oetit do logu
        }
    } else {
        // advanced attributes

        try {

            owner = Files.getOwner(file.toPath(), LinkOption.NOFOLLOW_LINKS).getName();

            if (Files.getFileStore(file.toPath()).supportsFileAttributeView(DosFileAttributeView.class)) {
                dosAttr = Boolean.TRUE;
                final DosFileAttributes dosFileAttributes = Files.readAttributes(file.toPath(),
                        DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);

                dosArchive = dosFileAttributes.isArchive();
                dosHidden = dosFileAttributes.isHidden();
                dosSystem = dosFileAttributes.isSystem();
                dosReadOnly = dosFileAttributes.isReadOnly();
            } else {
                dosAttr = Boolean.FALSE;
            }

            if (Files.getFileStore(file.toPath()).supportsFileAttributeView(PosixFileAttributeView.class)) {
                posixAttr = Boolean.TRUE;
                final PosixFileAttributes posixFileAttributes = Files.readAttributes(file.toPath(),
                        PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
                posixGroup = posixFileAttributes.group().getName();
                posixPermitions = PosixFilePermissions.toString(posixFileAttributes.permissions());
            } else {
                posixAttr = Boolean.FALSE;
            }

        } catch (IOException e) {
            e.printStackTrace(); //Todo implementovat
        }
    }

}

From source file:org.dataconservancy.packaging.tool.impl.GeneralPackageDescriptionCreator.java

private void visitFile(FileContext cxt, Map<String, PackageArtifact> artifacts)
        throws PackageDescriptionCreatorException {

    try {/*from   w  w  w.jav  a 2  s.  c  o  m*/
        String path = cxt.getFile().getCanonicalPath();
        if (visitedFiles.contains(path)) {
            if (Files.isSymbolicLink(cxt.getFile().toPath())) {
                throw new PackageDescriptionCreatorException("Symbolic link cycle detected",
                        "Fix offending symbolic link at " + cxt.getFile().toString() + ", which points to "
                                + path);
            } else {
                throw new PackageDescriptionCreatorException("Symbolic link cycle detected",
                        "There is a symbolic link under " + cxt.getRoot().toString() + " which points to "
                                + path + ".  Find the link and remove it.");
            }
        } else {
            visitedFiles.add(path);
        }
    } catch (IOException e) {
        throw new PackageDescriptionCreatorException("Error determining canonical path of " + cxt.getFile(), e);
    }

    try {
        for (Rule rule : rules) {
            if (rule.select(cxt)) {
                if (Action.EXCLUDE.equals(rule.getAction())) {
                    cxt.setIgnored(true);
                    continue;
                } else if (Action.INCLUDE.equals(rule.getAction())) {
                    populate(cxt, rule, artifacts);
                }

                break;
            }
        }

    } catch (Exception e) {
        throw new PackageDescriptionCreatorException(
                "Error applying package description generation rules to pathname " + cxt.getFile().toString()
                        + ": \n" + e.getMessage(),
                e);
    }

    if (cxt.getFile().isDirectory()) {
        for (File child : cxt.getFile().listFiles()) {
            //To support the cancelling of package description creation we check here to see if the thread has been interrupted.
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            visitFile(new FileContextImpl(child, cxt.getRoot(), cxt.isIgnored()), artifacts);
        }
    }
}