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.opensolaris.opengrok.history.BitKeeperRepository.java

/**
 * Annotate the specified file/revision. The options `-aur` to `bk annotate` specify that bitkeeper will output the
 * last user to edit the line, the last revision the line was edited, and then the line itself, each separated by a
 * hard tab.//from w w  w. ja v  a2s  . c o  m
 *
 * @param file file to annotate
 * @param revision revision to annotate, or null for latest
 * @return annotation file annotation
 */
@Override
public Annotation annotate(File file, String revision) throws IOException {
    final File absolute = file.getCanonicalFile();
    final File directory = absolute.getParentFile();
    final String basename = absolute.getName();

    final ArrayList<String> argv = new ArrayList<String>();
    ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
    argv.add(RepoCommand);
    argv.add("annotate");
    argv.add("-aur");
    if (revision != null) {
        argv.add("-r" + revision);
    }
    argv.add(basename);

    final Executor executor = new Executor(argv, directory);
    final BitKeeperAnnotationParser parser = new BitKeeperAnnotationParser(basename);
    if (executor.exec(true, parser) != 0) {
        throw new IOException(executor.getErrorString());
    } else {
        return parser.getAnnotation();
    }
}

From source file:org.apache.sling.commons.fsclassloader.impl.FSClassLoaderWebConsole.java

/**
 * Checks whether the specified file is a file and is underneath the root
 * directory./* ww w . j a  v  a  2s .  c om*/
 *
 * @param file
 *            the file to check
 * @return false if not a file or not under the root directory, true
 *         otherwise
 * @throws IOException
 */
private boolean isValid(File file) throws IOException {
    if (file.isFile()) {
        File parent = file.getCanonicalFile().getAbsoluteFile().getParentFile();
        while (parent != null) {
            if (parent.getCanonicalPath().equals(root.getCanonicalPath())) {
                return true;
            }
            parent = parent.getParentFile();
        }
    }
    return false;
}

From source file:org.squidy.common.dynamiccode.DynamicCodeClassLoader.java

/**
 * Add a directory that contains the source of dynamic java code.
 * /*from  w  w w. j  av a 2  s .  co  m*/
 * @param srcDir
 * @return true if the add is successful
 */
public boolean addSourceDir(File srcDir) {

    try {
        srcDir = srcDir.getCanonicalFile();
    } catch (IOException e) {
        // ignore
    }

    synchronized (sourceDirs) {

        // check existence
        for (int i = 0; i < sourceDirs.size(); i++) {
            SourceDir src = (SourceDir) sourceDirs.get(i);
            if (src.srcDir.equals(srcDir)) {
                return false;
            }
        }

        // add new
        SourceDir src = new SourceDir(srcDir);
        sourceDirs.add(src);

        if (LOG.isInfoEnabled()) {
            LOG.info("Added source directory: " + srcDir);
        }
    }

    return true;
}

From source file:com.digitalpebble.stormcrawler.protocol.file.FileResponse.java

public FileResponse(String u, Metadata md, FileProtocol fp) throws MalformedURLException, IOException {

    fileProtocol = fp;/*from   ww w  .  ja va  2  s  .c o m*/
    metadata = md;

    URL url = new URL(u);

    if (!url.getPath().equals(url.getFile())) {
        LOG.warn("url.getPath() != url.getFile(): {}.", url);
    }

    String path = "".equals(url.getPath()) ? "/" : url.getPath();

    File file = new File(URLDecoder.decode(path, fileProtocol.getEncoding()));

    if (!file.exists()) {
        statusCode = HttpStatus.SC_NOT_FOUND;
        return;
    }

    if (!file.canRead()) {
        statusCode = HttpStatus.SC_UNAUTHORIZED;
        return;
    }

    if (!file.equals(file.getCanonicalFile())) {
        metadata.setValue(HttpHeaders.LOCATION, file.getCanonicalFile().toURI().toURL().toString());
        statusCode = HttpStatus.SC_MULTIPLE_CHOICES;
        return;
    }

    if (file.isDirectory()) {
        getDirAsHttpResponse(file);
    } else if (file.isFile()) {
        getFileAsHttpResponse(file);
    } else {
        statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        return;
    }

    if (content == null) {
        content = new byte[0];
    }
}

From source file:org.auraframework.impl.source.file.FileSourceLoader.java

private boolean isFilePresent(File file) {
    // addresses MacOSx issue: file.exists() is case insensitive
    try {/* ww w.java 2s  . c  o  m*/
        if (file.exists()) {
            File cFile = file.getCanonicalFile();
            if (cFile != null && cFile.exists() && cFile.getName().equals(file.getName())) {
                return true;
            }
        }
    } catch (IOException e) {
        return false;
    }
    return false;
}

From source file:org.gradle.api.internal.file.FileNormaliser.java

public File normalise(File file) {
    try {/* w  w w. j a v a 2  s .c  o  m*/
        if (!file.isAbsolute()) {
            throw new IllegalArgumentException(String.format("Cannot normalize a relative file: '%s'", file));
        }

        if (isWindowsOs) {
            // on Windows, File.getCanonicalFile() doesn't resolve symlinks
            return file.getCanonicalFile();
        }

        File candidate;
        String filePath = file.getPath();
        List<String> path = null;
        if (isNormalisingRequiredForAbsolutePath(filePath)) {
            path = splitAndNormalisePath(filePath);
            String resolvedPath = CollectionUtils.join(File.separator, path);
            boolean needLeadingSeparator = File.listRoots()[0].getPath().startsWith(File.separator);
            if (needLeadingSeparator) {
                resolvedPath = File.separator + resolvedPath;
            }
            candidate = new File(resolvedPath);
        } else {
            candidate = file;
        }

        // If the file system is case sensitive, we don't have to normalise it further
        if (fileSystem.isCaseSensitive()) {
            return candidate;
        }

        // Short-circuit the slower lookup method by using the canonical file
        File canonical = candidate.getCanonicalFile();
        if (candidate.getPath().equalsIgnoreCase(canonical.getPath())) {
            return canonical;
        }

        // Canonical path is different to what we expected (eg there is a link somewhere in there). Normalise a segment at a time
        // TODO - start resolving only from where the expected and canonical paths are different
        if (path == null) {
            path = splitAndNormalisePath(filePath);
        }
        return normaliseUnixPathIgnoringCase(path);
    } catch (IOException e) {
        throw new UncheckedIOException(String.format("Could not normalize path for file '%s'.", file), e);
    }
}

From source file:org.photovault.imginfo.VolumeBase.java

/**
 *     Checks whether a certain file is part  of the volume (i.e. in the directory 
 *     hierarchy under the base directory. Note that existence of the file is not 
 *     ckecked, nor whether the file is really an instance of a PhotoInfo.
 * @return true if the file belongs to the volume, false otherwise
 * @param f File //w w  w .  j  av  a2  s  .  c  o m
 * @throws IOException if is an error when creating canonical form of f
 */
@Transient
public boolean isFileInVolume(File f) throws IOException {
    boolean isInVolume = false;

    if (f == null) {
        return false;
    }

    // First get the canonical forms of both f and volumeBaseDir
    // After that check if f or some of its parents matches f
    File basedir = getBaseDir();
    if (basedir == null) {
        return false;
    }
    File vbdCanon = basedir.getCanonicalFile();
    File fCanon = f.getCanonicalFile();
    File p = fCanon;
    while (p != null) {
        if (p.equals(vbdCanon)) {
            isInVolume = true;
            break;
        }
        p = p.getParentFile();
    }
    return isInVolume;

}

From source file:de.codesourcery.jasm16.compiler.io.FileResourceResolver.java

@Override
public IResource resolve(String identifier) throws ResourceNotFoundException {
    if (StringUtils.isBlank(identifier)) {
        throw new IllegalArgumentException("identifier must not be NULL/blank.");
    }//from www .  j  av a2  s .  c  om

    final File file = new File(identifier);
    if (!file.isFile()) {
        throw new ResourceNotFoundException("File " + file.getAbsolutePath() + " does not exist.", identifier);
    }

    if (!file.isFile()) {
        throw new ResourceNotFoundException(file.getAbsolutePath() + " is not a regular file.", identifier);
    }

    try {
        final File canonicalFile = file.getCanonicalFile();
        return new FileResource(canonicalFile, determineResourceType(canonicalFile));
    } catch (IOException e) {
        throw new RuntimeException("While resolving '" + identifier + "'", e);
    }
}

From source file:org.codehaus.plexus.redback.struts2.action.admin.BackupRestoreAction.java

public File getFile(String filename) {
    if (filename == null) {
        return null;
    }// w w w. j  a  v  a  2  s  .c om

    File f = null;

    if (filename != null && filename.length() != 0) {
        f = new File(filename);

        if (!f.isAbsolute()) {
            f = new File(applicationHome, filename);
        }
    }

    try {
        return f.getCanonicalFile();
    } catch (IOException e) {
        return f;
    }
}

From source file:com.nesscomputing.mojo.numbers.PropertyCache.java

@VisibleForTesting
Properties getProperties(final AbstractDefinition<?> definition) throws IOException {
    final File definitionPropertyFile = definition.getPropertyFile();

    // Ephemeral, so return null.
    if (definitionPropertyFile == null) {
        return null;
    }/*from   w  ww.  j a  v  a 2s .  com*/

    PropertyCacheEntry propertyCacheEntry;
    final File propertyFile = definitionPropertyFile.getCanonicalFile();

    // Throws an exception if the file must exist and does not.
    final boolean createFile = IWFCEnum.checkState(definition.getOnMissingFile(), propertyFile.exists(),
            definitionPropertyFile.getCanonicalPath());

    propertyCacheEntry = propFiles.get(propertyFile);

    if (propertyCacheEntry != null) {
        // If there is a cache hit, something either has loaded the file
        // or another property has already put in a creation order.
        // Make sure that if this number has a creation order it is obeyed.
        if (createFile) {
            propertyCacheEntry.doCreate();
        }
    } else {
        // Try loading or creating properties.
        final Properties props = new Properties();

        if (!propertyFile.exists()) {
            propertyCacheEntry = new PropertyCacheEntry(props, false, createFile); // does not exist
        } else {
            if (propertyFile.isFile() && propertyFile.canRead()) {
                InputStream stream = null;
                try {
                    stream = new FileInputStream(propertyFile);
                    props.load(stream);
                    propertyCacheEntry = new PropertyCacheEntry(props, true, createFile);
                    propFiles.put(propertyFile, propertyCacheEntry);
                } finally {
                    Closeables.closeQuietly(stream);
                }
            } else {
                throw new IllegalStateException(
                        format("Can not load %s, not a file!", definitionPropertyFile.getCanonicalPath()));
            }
        }
    }

    return propertyCacheEntry.getProps();
}