incubator-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ran...@apache.org
Subject cvs commit: incubator-ftpserver/src/java/org/apache/ftpserver/filesystem NativeFileObject.java NativeFileSystemManager.java NativeFileSystemView.java
Date Fri, 09 Sep 2005 11:17:04 GMT
rana_b      2005/09/09 04:17:04

  Added:       src/java/org/apache/ftpserver/filesystem
                        NativeFileObject.java NativeFileSystemManager.java
                        NativeFileSystemView.java
  Log:
  file system manager changed to NativeFileSystemManager
  
  Revision  Changes    Path
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/filesystem/NativeFileObject.java
  
  Index: NativeFileObject.java
  ===================================================================
  // $Id: NativeFileObject.java,v 1.1 2005/09/09 11:17:03 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed 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.ftpserver.filesystem;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.io.RandomAccessFile;
  import java.util.StringTokenizer;
  
  import org.apache.ftpserver.ftplet.FileObject;
  
  /**
   * This class wraps native file object. 
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class NativeFileObject implements FileObject {
  
      // the file name with respect to the user root.
      // The path separator character will be '/' and
      // it will always begin with '/'.
      private String m_fileName;
      
      private File m_file;
      private boolean m_writePermission;
      
      
      /**
       * Constructor.
       */
      protected NativeFileObject(String fileName, File file, boolean writePerm) {
          m_fileName = fileName;
          m_file = file;
          m_writePermission = writePerm;
      }
      
      /**
       * Get full name.
       */
      public String getFullName() {
          
          // strip the last '/' if necessary
          String fullName = m_fileName;
          int filelen = fullName.length();
          if( (filelen != 1)&& (fullName.charAt(filelen - 1) == '/') ) {
              fullName = fullName.substring(0, filelen - 1);
          }
          
          return fullName;
      }
      
      /**
       * Get short name.
       */
      public String getShortName() {
          
          // root - the short name will be '/'
          if(m_fileName.equals("/")) {
              return "/";
          }
          
          // strip the last '/'
          String shortName = m_fileName;
          int filelen = m_fileName.length();
          if(m_fileName.charAt(filelen - 1) == '/') {
              shortName = shortName.substring(0, filelen - 1);
          }
          
          // return from the last '/'
          int slashIndex = m_fileName.lastIndexOf('/');
          if(slashIndex != -1) {
              shortName = shortName.substring(slashIndex + 1);
          }
          return shortName;
      }
      
      /**
       * Is a hidden file?
       */
      public boolean isHidden() {
          return m_file.isHidden();
      }
       
      /**
       * Is it a directory?
       */
      public boolean isDirectory() {
          return m_file.isDirectory();
      }
      
      /**
       * Is it a file?
       */
      public boolean isFile() {
          return m_file.isFile();
      }
      
      /**
       * Does this file exists?
       */
      public boolean doesExist() {
          return m_file.exists();
      }
      
      /**
       * Get file size.
       */
      public long getSize() {
          return m_file.length();
      }
      
      /**
       * Get file owner.
       */
      public String getOwnerName() {
          return "user";
      }
      
      /**
       * Get group name
       */
      public String getGroupName() {
          return "group";
      }
      
      /**
       * Get link count
       */
      public int getLinkCount() {
          return m_file.isDirectory() ? 3 : 1;
      }
      
      /**
       * Get last modified time.
       */ 
      public long getLastModified() {
          return m_file.lastModified();
      }
      
      /**
       * Check read permission.
       */
      public boolean hasReadPermission() {
          return m_file.canRead();
      }
      
      /**
       * Chech file write permission.
       */
      public boolean hasWritePermission() {
          if(!m_writePermission) {
              return false;
          }
          
          if(m_file.exists()) {
              return m_file.canWrite();
          }
          return true;
      }
      
      /**
       * Has delete permission.
       */
      public boolean hasDeletePermission() {
          
          // root cannot be deleted
          if( "/".equals(m_fileName) ) {
              return false;
          }
          
          return hasWritePermission();
      }
      
      /**
       * Delete file.
       */
      public boolean delete() {
          boolean retVal = false;
          if( hasDeletePermission() ) {
              retVal = m_file.delete();
          }
          return retVal;
      }
      
      /**
       * Move file object.
       */
      public boolean move(FileObject dest) {
          boolean retVal = false;
          if(dest.hasWritePermission() && hasReadPermission()) {
              File destFile = ((NativeFileObject)dest).m_file;
              retVal = m_file.renameTo(destFile);
          }
          return retVal;
      }
      
      /**
       * Create directory.
       */
      public boolean mkdir() {
          boolean retVal = false;
          if(hasWritePermission()) {
              retVal = m_file.mkdirs();
          }
          return retVal;
      }
      
      /**
       * Get the physical file object.
       */
      public File getPhysicalFile() {
          return m_file;
      }
      
      /**
       * Create output stream for writing.
       */
      public OutputStream createOutputStream(boolean append) throws IOException {
          
          // permission check
          if(!hasWritePermission()) {
              throw new IOException("No write permission : " + m_file.getName());
          }
          
          // create output stream
          OutputStream out = null;
          if(append && m_file.exists()) {
              RandomAccessFile raf = new RandomAccessFile(m_file, "rw");
              raf.seek(raf.length());
              out = new FileOutputStream(raf.getFD());
          }
          else {
              out = new FileOutputStream(m_file);
          }
          
          return out;
      }
      
      /**
       * Create output stream for writing.
       */
      public OutputStream createOutputStream(long offset) throws IOException {
          
          // permission check
          if(!hasWritePermission()) {
              throw new IOException("No write permission : " + m_file.getName());
          }
          
          // create output stream
          RandomAccessFile raf = new RandomAccessFile(m_file, "rw");
          raf.setLength(offset);
          raf.seek(offset);
          return new FileOutputStream(raf.getFD());
      }
      
      /**
       * Create input stream for reading.
       */
      public InputStream createInputStream(long offset) throws IOException {
          
          // permission check
          if(!hasReadPermission()) {
              throw new IOException("No read permission : " + m_file.getName());
          }
          
          // move to the appropriate offset and create input stream
          RandomAccessFile raf = new RandomAccessFile(m_file, "r");
          raf.seek(offset);
          return new FileInputStream(raf.getFD());
      }
      
      /**
       * Normalize separate characher. Separate character should be '/' always.
       */
      public final static String normalizeSeparateChar(String pathName) {
         pathName = pathName.replace(File.separatorChar, '/');
         pathName = pathName.replace('\\', '/');
         return pathName;
      }
      
      /**
       * Get the physical canonical file name. It works like 
       * File.getCanonicalPath().
       * 
       * @param rootDir The root directory. The path separator will always 
       * be '/'. It will always end with '/'.
       * 
       * @param currDir The current directory. It will always be with
       * respect to the root directory. The first and last characters 
       * will always be '/'.
       * 
       * @param fileName The input file name.
       * 
       * @return The return string will always begin with the root directory.
       * It will never be null.
       */
      public final static String getPhysicalName(String rootDir, 
                                                 String currDir, 
                                                 String fileName) {
          
          // get the starting directory
          fileName = normalizeSeparateChar(fileName);
          String resArg;
          if(fileName.charAt(0) != '/') {
              resArg = rootDir + currDir.substring(1);
          }
          else {
              resArg = rootDir;
          }
          
          // strip last '/'
          if(resArg.charAt(resArg.length() -1) == '/') {
              resArg = resArg.substring(0, resArg.length()-1);
          }
          
          // replace ., ~ and ..
          // in this loop resArg will never end with '/'
          StringTokenizer st = new StringTokenizer(fileName, "/");
          while(st.hasMoreTokens()) {
              String tok = st.nextToken().trim();
              
              // . => current directory
              if(tok.equals(".")) {
                  continue;
              }
              
              // .. => parent directory (if not root)
              if(tok.equals("..")) {
                  if(resArg.startsWith(rootDir)) {
                      int slashIndex = resArg.lastIndexOf('/');
                      if(slashIndex != -1) {
                          resArg = resArg.substring(0, slashIndex);
                      }
                  }
                  continue;
              }
              
              // ~ => home directory (in this case the root directory)
              if (tok.equals("~")) {
                  resArg = rootDir.substring(0, rootDir.length()-1);
                  continue;
              }
              
              resArg = resArg + '/' + tok;
          }
          
          // add last slash if necessary
          if( (resArg.length()) + 1 == rootDir.length() ) {
              resArg += '/';
          }
          
          // final check
          if ( !resArg.regionMatches(0, rootDir, 0, rootDir.length()) ) {
              resArg = rootDir;
          }
          
          return resArg;
      }
  }
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/filesystem/NativeFileSystemManager.java
  
  Index: NativeFileSystemManager.java
  ===================================================================
  // $Id: NativeFileSystemManager.java,v 1.1 2005/09/09 11:17:03 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed 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.ftpserver.filesystem;
  
  import java.io.File;
  
  import org.apache.ftpserver.ftplet.Configuration;
  import org.apache.ftpserver.ftplet.FileSystemManager;
  import org.apache.ftpserver.ftplet.FileSystemView;
  import org.apache.ftpserver.ftplet.FtpException;
  import org.apache.ftpserver.ftplet.Logger;
  import org.apache.ftpserver.ftplet.User;
  
  /**
   * Native file system manager. It uses the OS file system.
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class NativeFileSystemManager implements FileSystemManager {
  
      private Logger m_logger;
      private boolean m_createHome;
      
      /**
       * Set the logger object.
       */
      public void setLogger(Logger logger) {
          m_logger = logger;
      }
      
      /**
       * Configure the file system manager - does nothing.
       */
      public void configure(Configuration conf) throws FtpException {
          m_createHome = conf.getBoolean("create-home", false);
      }
      
      /**
       * Dispose the file system manager.
       */
      public void dispose() {
      }
      
      /**
       * Create the appropriate user file system view.
       */
      public FileSystemView createFileSystemView(User user) throws FtpException {
  
          // create home if does not exist
          if(m_createHome) {
              String homeDirStr = user.getHomeDirectory();
              File homeDir = new File(homeDirStr);
              if(homeDir.isFile()) {
                  m_logger.warn("Not a directory :: " + homeDirStr);
                  throw new FtpException("Not a directory :: " + homeDirStr);
              }
              if( (!homeDir.exists()) && (!homeDir.mkdirs()) ) {
                  m_logger.warn("Cannot create user home :: " + homeDirStr);
                  throw new FtpException("Cannot create user home :: " + homeDirStr);
              }
          }
          
          FileSystemView fsView = new NativeFileSystemView(user);
          return fsView;
      }
      
  }
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/filesystem/NativeFileSystemView.java
  
  Index: NativeFileSystemView.java
  ===================================================================
  // $Id: NativeFileSystemView.java,v 1.1 2005/09/09 11:17:03 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed 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.ftpserver.filesystem;
  
  import java.io.File;
  
  import org.apache.ftpserver.ftplet.FileObject;
  import org.apache.ftpserver.ftplet.FileSystemView;
  import org.apache.ftpserver.ftplet.FtpException;
  import org.apache.ftpserver.ftplet.User;
  
  /**
   * File system view based on native file system. Here the root directory
   * will be user virtual root (/).
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class NativeFileSystemView implements FileSystemView {
      
      // the root directory will always end with '/'.
      private String m_rootDir;
      
      // the first and the last character will always be '/'
      // It is always with respect to the root directory.
      private String m_currDir;
      
      private boolean m_writePermission;
      
      
      /**
       * Constructor - set the user and logger objects.
       */
      protected NativeFileSystemView(User user) throws FtpException {
          
          // add last '/' if necessary
          String rootDir = user.getHomeDirectory();
          rootDir = NativeFileObject.normalizeSeparateChar(rootDir);
          if(!rootDir.endsWith("/")) {
              rootDir += '/';
          }
          m_rootDir = rootDir;
          
          m_writePermission = user.getWritePermission();
          m_currDir = "/";
      }
      
      /**
       * Get the root directory.
       */
      public FileObject getRootDirectory() {
          return new NativeFileObject("/", new File(m_rootDir), m_writePermission);
      }
      
      /**
       * Get the current directory.
       */
      public FileObject getCurrentDirectory() {
          FileObject fileObj = null;
          if(m_currDir.equals("/")) {
              fileObj = new NativeFileObject("/", new File(m_rootDir), m_writePermission);

          }
          else {
              File file = new File(m_rootDir, m_currDir.substring(1));
              fileObj = new NativeFileObject(m_currDir, file, m_writePermission);
              
          }
          return fileObj;
      }
      
      /**
       * Get file object.
       */
      public FileObject getFileObject(String file) {
          
          // get actual file object
          String physicalName = NativeFileObject.getPhysicalName(m_rootDir, m_currDir, file);
          File fileObj = new File(physicalName);
          
          // strip the root directory and return
          String userFileName = physicalName.substring(m_rootDir.length() - 1);
          return new NativeFileObject(userFileName, fileObj, m_writePermission);
      }
      
      /**
       * Change directory.
       */
      public boolean changeDirectory(String dir) {
          
          // not a directory - return false
          dir = NativeFileObject.getPhysicalName(m_rootDir, m_currDir, dir);
          File dirObj = new File(dir); 
          if(!dirObj.isDirectory()) {
              return false;
          }
          
          // strip user root and add last '/' if necessary
          dir = dir.substring(m_rootDir.length() - 1);
          if(dir.charAt(dir.length() - 1) != '/') {
              dir = dir + '/';
          }
          
          m_currDir = dir;
          return true;
      } 
      
      /**
       * List files.
       */
      public FileObject[] listFiles(String str) {
          
          // get the physical file object
          FileObject virtualFile = getFileObject(str);
          File physicalFile = ((NativeFileObject)virtualFile).getPhysicalFile();
          
          // does not exist - return null
          if(!physicalFile.exists()) {
              return null;
          } 
          
          // file - return a single element array 
          if(physicalFile.isFile()) {
              return new FileObject[] { virtualFile };
          }
          
          // directory - return all the files
          File[] physicalFiles = physicalFile.listFiles();
          if(physicalFiles == null) {
              return null;
          }
          
          // get the virtual name of the base directory
          String virtualFileStr = virtualFile.getFullName();
          if(virtualFileStr.charAt(virtualFileStr.length() - 1) != '/') {
              virtualFileStr += '/';
          }
          
          // now return all the files under the directory
          FileObject[] virtualFiles = new FileObject[physicalFiles.length];
          for(int i=0; i<physicalFiles.length; ++i) {
              File fileObj = physicalFiles[i];
              String fileName = virtualFileStr + fileObj.getName();
              virtualFiles[i] = new NativeFileObject(fileName, fileObj, m_writePermission);
          }
          return virtualFiles;
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@incubator.apache.org
For additional commands, e-mail: cvs-help@incubator.apache.org


Mime
View raw message