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;
031
032/**
033 * A local file system.
034 */
035public class LocalFileSystem
036    extends AbstractFileSystem
037{
038    private final String rootFile;
039
040    public LocalFileSystem(final FileName rootName,
041                           final String rootFile,
042                           final FileSystemOptions opts)
043    {
044        super(rootName, null, opts);
045        this.rootFile = rootFile;
046    }
047
048    /**
049     * Creates a file object.
050     */
051    @Override
052    protected FileObject createFile(final AbstractFileName name) throws FileSystemException
053    {
054        // Create the file
055        return new LocalFile(this, rootFile, name);
056    }
057
058    /**
059     * Returns the capabilities of this file system.
060     */
061    @Override
062    protected void addCapabilities(final Collection<Capability> caps)
063    {
064        caps.addAll(DefaultLocalFileProvider.capabilities);
065    }
066
067    /**
068     * Creates a temporary local copy of a file and its descendants.
069     */
070    @Override
071    protected File doReplicateFile(final FileObject fileObject,
072                                   final FileSelector selector)
073        throws Exception
074    {
075        final LocalFile localFile = (LocalFile) fileObject;
076        final File file = localFile.getLocalFile();
077        final SecurityManager sm = System.getSecurityManager();
078        if (sm != null)
079        {
080            final FilePermission requiredPerm = new FilePermission(file.getAbsolutePath(), "read");
081            sm.checkPermission(requiredPerm);
082        }
083        return file;
084    }
085
086}