001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.util;
020
021import org.apache.hadoop.fs.FileSystem;
022import org.apache.hadoop.fs.Path;
023
024import java.io.IOException;
025import java.net.URI;
026
027public final class FSUtils {
028    public static final String FILE_SCHEME_PREFIX = "file:";
029
030    public static Path getSymLinkTarget(FileSystem fs, Path p) throws IOException {
031        try {
032            //getSymlink doesn't work with fragment name, need to remove fragment before calling getSymlink
033            Path tempPath = new URI(p.toString()).getFragment() == null ? p : new Path(new URI(p.toString()).getPath());
034            return fs.getFileLinkStatus(tempPath).getSymlink();
035        }
036        catch (java.net.URISyntaxException e) {
037            throw new IOException(e);
038        }
039    }
040
041    public static boolean isSymlink(FileSystem fs, Path p) throws IOException {
042        try {
043            //isSymlink doesn't work with fragment name, need to remove fragment before checking for symlink
044            Path tempPath = new URI(p.toString()).getFragment() == null ? p : new Path(new URI(p.toString()).getPath());
045            return fs.getFileLinkStatus(tempPath).isSymlink();
046        }
047        catch (java.net.URISyntaxException e) {
048            throw new IOException(e);
049        }
050    }
051
052    public static void createSymlink(FileSystem fs, Path target, Path link, boolean createParent) throws IOException {
053        fs.createSymlink(target, link, createParent);
054    }
055
056    public static boolean isLocalFile(String fileName) {
057        return fileName.startsWith(FILE_SCHEME_PREFIX + "/");
058    }
059
060    public static boolean isLocalFile(Path filePath) {
061        return isLocalFile(filePath.toString());
062    }
063
064    public static boolean isNotLocalFile(String fileName) {
065       return !isLocalFile(fileName);
066    }
067}