/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sshd.server.filesystem;
import org.apache.sshd.common.util.LogUtils;
import org.apache.sshd.server.SshFile;
import org.apache.sshd.server.FileSystemView;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.File;
/**
* <strong>Internal class, do not use directly.</strong>
*
* File system view based on native file system. Here the root directory will be
* user virtual root (/).
*
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public class NativeFileSystemView implements FileSystemView {
private final Log LOG = LogFactory
.getLog(NativeFileSystemView.class);
// the first and the last character will always be '/'
// It is always with respect to the root directory.
private String currDir;
private String userName;
private boolean caseInsensitive = false;
/**
* Constructor - internal do not use directly, use {@link NativeFileSystemFactory} instead
*/
protected NativeFileSystemView(String userName) {
this(userName, false);
}
/**
* Constructor - internal do not use directly, use {@link NativeFileSystemFactory} instead
*/
public NativeFileSystemView(String userName, boolean caseInsensitive) {
if (userName == null) {
throw new IllegalArgumentException("user can not be null");
}
this.caseInsensitive = caseInsensitive;
currDir = System.getProperty("user.dir");
this.userName = userName;
// add last '/' if necessary
LogUtils.debug(LOG,"Native filesystem view created for user \"{0}\" with root \"{1}\"", userName, currDir);
}
/**
* Get file object.
*/
public SshFile getFile(String file) {
// get actual file object
String physicalName = NativeSshFile.getPhysicalName("/",
currDir, file, caseInsensitive);
File fileObj = new File(physicalName);
// strip the root directory and return
String userFileName = physicalName.substring("/".length() - 1);
return new NativeSshFile(userFileName, fileObj, userName);
}
}