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