Example usage for java.io File getCanonicalFile

List of usage examples for java.io File getCanonicalFile

Introduction

In this page you can find the example usage for java.io File getCanonicalFile.

Prototype

public File getCanonicalFile() throws IOException 

Source Link

Document

Returns the canonical form of this abstract pathname.

Usage

From source file:org.ensembl.healthcheck.testcase.funcgen.CheckResultSetDBFileLink.java

public static boolean isSymLink(File file) throws IOException {

    if (file == null)
        throw new NullPointerException("File argument cannot be null");

    File cfile;

    if (file.getParent() == null) {
        cfile = file;//from  w  ww .  ja  v  a 2  s.co  m
    } else {
        File canonDir = file.getParentFile().getCanonicalFile();
        cfile = new File(canonDir, file.getName());
    }

    return !cfile.getCanonicalFile().equals(cfile.getAbsoluteFile());
}

From source file:org.sonar.dotnet.tools.commons.visualstudio.ModelFactory.java

/**
 * Checks, whether the child directory is a subdirectory of the base directory.
 * /*from  ww w  .ja v a2  s .  co  m*/
 * @param base
 *          the base directory.
 * @param child
 *          the suspected child directory.
 * @return true, if the child is a subdirectory of the base directory.
 * @throws IOException
 *           if an IOError occured during the test.
 */
public static boolean isSubDirectory(File base, File child) {
    try {
        File baseFile = base.getCanonicalFile();
        File childFile = child.getCanonicalFile();
        File parentFile = childFile;

        // Checks recursively if "base" is one of the parent of "child"
        while (parentFile != null) {
            if (baseFile.equals(parentFile)) {
                return true;
            }
            parentFile = parentFile.getParentFile();
        }
    } catch (IOException ex) {
        // This is false
        if (LOG.isDebugEnabled()) {
            LOG.debug(child + " is not in " + base, ex);
        }
    }
    return false;
}

From source file:com.asakusafw.runtime.util.hadoop.ConfigurationProvider.java

private static File getHadoopInstallationPath(File command) {
    assert command != null;
    File resolved;//  w w w  . ja  va  2 s. co m
    try {
        resolved = command.getCanonicalFile();
    } catch (IOException e) {
        LOG.warn(MessageFormat.format("Failed to resolve Hadoop command (for detecting Hadoop conf dir): {0}",
                command), e);
        resolved = command;
    }
    File parent1 = resolved.getParentFile();
    if (parent1 == null || parent1.isDirectory() == false) {
        return null;
    }
    File parent2 = parent1.getParentFile();
    if (parent2 == null || parent2.isDirectory() == false) {
        return null;
    }
    return parent2;
}

From source file:com.glaf.core.util.FileUtils.java

public static boolean mkdirsWithExistsCheck(File dir) {
    if (dir.mkdir() || dir.exists()) {
        return true;
    }/*  ww  w .java 2  s  .c o m*/
    File canonDir = null;
    try {
        canonDir = dir.getCanonicalFile();
    } catch (IOException e) {
        return false;
    }
    String parent = canonDir.getParent();
    return (parent != null)
            && (mkdirsWithExistsCheck(new File(parent)) && (canonDir.mkdir() || canonDir.exists()));
}

From source file:org.cloudifysource.dsl.internal.packaging.Packager.java

/****************
 * Packs a service folder./*from w ww.j ava 2s .c o  m*/
 * 
 * @param recipeDirOrFile
 *            .
 * @param isDir
 *            true if recipeDirOrFile is a Directory.
 * @param service
 *            .
 * @param additionalServiceFiles
 *            files to add to the service directory.
 * @return the packed file.
 * @throws IOException .
 * @throws PackagingException .
 */
public static File pack(final File recipeDirOrFile, final boolean isDir, final Service service,
        final List<File> additionalServiceFiles) throws IOException, PackagingException {
    File recipeFile = recipeDirOrFile;
    if (isDir) {
        recipeFile = DSLReader.findDefaultDSLFile(DSLUtils.SERVICE_DSL_FILE_NAME_SUFFIX, recipeDirOrFile);
    }

    if (!recipeFile.isFile()) {
        throw new IllegalArgumentException(recipeFile + " is not a file");
    }

    logger.info("packing folder " + recipeFile.getParent());
    final File createdPuFolder = buildPuFolder(service, recipeFile, additionalServiceFiles);
    final File puZipFile = createZippedPu(service, createdPuFolder, recipeFile);
    logger.info("created " + puZipFile.getCanonicalFile());
    if (FileUtils.deleteQuietly(createdPuFolder)) {
        logger.finer("deleted temp pu folder " + createdPuFolder.getAbsolutePath());
    }
    return puZipFile;
}

From source file:com.github.fritaly.dualcommander.DirectoryBrowser.java

private static File getCanonicalFile(File file) {
    try {//  w  w w. j  av a 2 s  . co m
        return file.getCanonicalFile();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:io.druid.java.util.common.CompressionUtils.java

public static void validateZipOutputFile(String sourceFilename, final File outFile, final File outDir)
        throws IOException {
    // check for evil zip exploit that allows writing output to arbitrary directories
    final File canonicalOutFile = outFile.getCanonicalFile();
    final String canonicalOutDir = outDir.getCanonicalPath();
    if (!canonicalOutFile.toPath().startsWith(canonicalOutDir)) {
        throw new ISE("Unzipped output path[%s] of sourceFile[%s] does not start with outDir[%s].",
                canonicalOutFile, sourceFilename, canonicalOutDir);
    }//ww w. j  a  va  2 s. co m
}

From source file:org.pentaho.reporting.libraries.base.util.ObjectUtilities.java

/**
 * Performs a comparison on two file objects to determine if they refer to the same file. The
 * <code>File.equals()</code> method requires that the files refer to the same file in the same way (relative vs.
 * absolute).//from  w  w w. ja v  a2 s . co m
 *
 * @param file1 the first file (<code>null</code> permitted).
 * @param file2 the second file (<code>null</code> permitted).
 * @return <code>true</code> if the files refer to the same file, <code>false</code> otherwise
 */
public static boolean equals(final File file1, final File file2) {
    if (file1 == file2) {
        return true;
    }
    if (file1 != null && file2 != null) {
        try {
            return file1.getCanonicalFile().equals(file2.getCanonicalFile());
        } catch (IOException ioe) {
            // There was an error accessing the filesystem
            return file1.equals(file2);
        }
    }
    return false;
}

From source file:net.minecraftforge.fml.relauncher.CoreModManager.java

/**
 * @param mcDir//  w w  w  . j  ava2s  .  c om
 *            the minecraft home directory
 * @return the coremod directory
 */
private static File setupCoreModDir(File mcDir) {
    File coreModDir = new File(mcDir, "mods");
    try {
        coreModDir = coreModDir.getCanonicalFile();
    } catch (IOException e) {
        throw new RuntimeException(
                String.format("Unable to canonicalize the coremod dir at %s", mcDir.getName()), e);
    }
    if (!coreModDir.exists()) {
        coreModDir.mkdir();
    } else if (coreModDir.exists() && !coreModDir.isDirectory()) {
        throw new RuntimeException(
                String.format("Found a coremod file in %s that's not a directory", mcDir.getName()));
    }
    return coreModDir;
}

From source file:com.drunkendev.io.recurse.tests.RecursionTest.java

/**
 * Given a {@link File} object, test if it is likely to be a symbolic link.
 *
 * @param   file/* www . j  a v  a 2s  .  c  o m*/
 *          File to test for symbolic link.
 * @return  {@code true} if {@code file} is a symbolic link.
 * @throws  NullPointerException
 *          If {@code file} is null.
 * @throws  IOException
 *          If a symbolic link could not be determined. This is ultimately
 *          caused by a call to {@link File#getCanonicalFile()}.
 */
private static boolean isSymbolicLink(File file) throws IOException {
    if (file == null) {
        throw new NullPointerException("File must not be null");
    }
    File canon;
    if (file.getParent() == null) {
        canon = file;
    } else {
        File canonDir = file.getParentFile().getCanonicalFile();
        canon = new File(canonDir, file.getName());
    }
    return !canon.getCanonicalFile().equals(canon.getAbsoluteFile());
}