Java Is Symbolic Link isSymlink(File base, File curious)

Here you can find the source of isSymlink(File base, File curious)

Description

Check if a file relative to a base path has a symlink, not counting any symlinks in base or above.

License

Open Source License

Declaration

public static boolean isSymlink(File base, File curious) throws IOException 

Method Source Code


//package com.java2s;
/*/*www . ja v  a  2  s . co  m*/
 * Copyright 2012 - 2014 Eric Myhre <http://exultant.us>
 *
 * This file is part of mdm <https://github.com/heavenlyhash/mdm/>.
 *
 * mdm is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

public class Main {
    /**
     * Check if a file relative to a base path has a symlink, not counting any symlinks in base or above.
     */
    public static boolean isSymlink(File base, File curious) throws IOException {
        if (base == null) {
            throw new NullPointerException("Base file must not be null");
        }
        if (curious == null) {
            throw new NullPointerException("Curious file must not be null");
        }
        File baseCanonical = base.getCanonicalFile();
        File curiousResolved = new File(baseCanonical, curious.getPath());
        File curiousCanonical = curiousResolved.getCanonicalFile();
        return !curiousCanonical.equals(curiousResolved.getAbsoluteFile());
    }

    /**
     * Check if this single file is a symlink.
     */
    public static boolean isSymlink(File file) throws IOException {
        if (file == null) {
            throw new NullPointerException("File must not be null");
        }
        File fileInCanonicalDir = fileInCanonicalDir(file);

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

    private static File fileInCanonicalDir(File file) throws IOException {
        if (file.getParent() == null) {
            return file;
        } else {
            File canonicalDir = file.getParentFile().getCanonicalFile();
            return new File(canonicalDir, file.getName());
        }
    }
}

Related

  1. isSymbolicLink(File file)
  2. isSymbolicLink(File file)
  3. isSymbolicLink(File file)
  4. isSymbolicLink(File file)
  5. isSymbolicLink(File parent, String name)
  6. isSymLink(File file)
  7. isSymlink(File file)
  8. isSymlink(File file)
  9. isSymlink(File file)