001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.provider.local;
018
019import java.io.File;
020import java.io.FilePermission;
021import java.util.Collection;
022
023import org.apache.commons.vfs2.Capability;
024import org.apache.commons.vfs2.FileName;
025import org.apache.commons.vfs2.FileObject;
026import org.apache.commons.vfs2.FileSelector;
027import org.apache.commons.vfs2.FileSystemException;
028import org.apache.commons.vfs2.FileSystemOptions;
029import org.apache.commons.vfs2.provider.AbstractFileName;
030import org.apache.commons.vfs2.provider.AbstractFileSystem;
031import org.apache.commons.vfs2.util.FileObjectUtils;
032
033/**
034 * A local file system.
035 */
036public class LocalFileSystem extends AbstractFileSystem {
037
038    private final String rootFile;
039
040    /**
041     * Constructs a new instance.
042     *
043     * @param rootFileName The root file name of this file system.
044     * @param rootFile The root of this file system.
045     * @param fileSystemOptions Options to build this file system.
046     */
047    public LocalFileSystem(final FileName rootFileName, final String rootFile, final FileSystemOptions fileSystemOptions) {
048        super(rootFileName, null, fileSystemOptions);
049        this.rootFile = rootFile;
050    }
051
052    /**
053     * Returns the capabilities of this file system.
054     */
055    @Override
056    protected void addCapabilities(final Collection<Capability> caps) {
057        caps.addAll(DefaultLocalFileProvider.capabilities);
058    }
059
060    /**
061     * Creates a file object.
062     */
063    @Override
064    protected FileObject createFile(final AbstractFileName name) throws FileSystemException {
065        // Create the file
066        return new LocalFile(this, rootFile, name);
067    }
068
069    /**
070     * Creates a temporary local copy of a file and its descendants.
071     */
072    @Override
073    protected File doReplicateFile(final FileObject fileObject, final FileSelector selector) throws Exception {
074        final LocalFile localFile = (LocalFile) FileObjectUtils.getAbstractFileObject(fileObject);
075        final File file = localFile.getLocalFile();
076        final SecurityManager sm = System.getSecurityManager();
077        if (sm != null) {
078            sm.checkPermission(new FilePermission(file.getAbsolutePath(), "read"));
079        }
080        return file;
081    }
082
083}