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 OSVirtualFileObject.java OSVirualFileSystemManager.java OSVirualFileSystemView.java
Date Wed, 07 Sep 2005 05:05:35 GMT
rana_b      2005/09/06 22:05:35

  Added:       src/java/org/apache/ftpserver/filesystem
                        OSVirtualFileObject.java
                        OSVirualFileSystemManager.java
                        OSVirualFileSystemView.java
  Log:
  Refactored - removed avalon entirely
  
  Revision  Changes    Path
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/filesystem/OSVirtualFileObject.java
  
  Index: OSVirtualFileObject.java
  ===================================================================
  // $Id: OSVirtualFileObject.java,v 1.1 2005/09/07 05:05:35 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 org.apache.ftpserver.ftplet.FileObject;
  
  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;
  
  /**
   * Operating system file.  
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public
  class OSVirtualFileObject implements FileObject {
  
      private File m_file;
      private String m_rootDir;
      private boolean m_writePermission;
      
      /**
       * Protected constructor - used only in the <code>OSVirtualFileSystemView</code>.
       */
      protected OSVirtualFileObject(File file, String rootDir, boolean writePerm) {
          m_file = file;
          m_rootDir = rootDir;
          m_writePermission = writePerm;
      }
  
      /**
       * Get the fully qualified name.
       */
      public String getFullName() {
          String virtualFileStr = null;
          try {
              String physicalFileStr = m_file.getCanonicalPath();
              physicalFileStr = normalizeSeparateChar(physicalFileStr);
              virtualFileStr = physicalFileStr.substring(m_rootDir.length() - 1);
              
              if(m_file.isDirectory()) {
                  if(virtualFileStr.equals("")) {
                      virtualFileStr = "/";
                  }
                  else if( (!virtualFileStr.equals("/")) && virtualFileStr.endsWith("/")
) {
                      virtualFileStr = virtualFileStr.substring(0, virtualFileStr.length()
- 1);
                  }
              }
          }
          catch(Exception ex) {
              ex.printStackTrace();
          }
          return virtualFileStr;
      }
      
      /**
       * Get the file short name.
       */
      public String getShortName() {
          return m_file.getName();
      }
      
      /**
       * 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(getFullName()) ) {
              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 = ((OSVirtualFileObject)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;
      }
      
      /**
       * 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());
      }
      
      /**
       * Get the physical file.
       */
      File getPhysicalFile() {
          return m_file;
      }
      
      /**
       * Normalize separate characher. Separate character should be '/' always.
       */
      public static String normalizeSeparateChar(String pathName) {   
         pathName = pathName.replace(File.separatorChar, '/');
         pathName = pathName.replace('\\', '/');
         return pathName;
      }
  }
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/filesystem/OSVirualFileSystemManager.java
  
  Index: OSVirualFileSystemManager.java
  ===================================================================
  // $Id: OSVirualFileSystemManager.java,v 1.1 2005/09/07 05:05:35 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 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;
  
  import java.io.File;
  
  /**
   * This is a operating system based virtual root file system manager. 
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class OSVirualFileSystemManager 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.exists()) && (!homeDir.mkdirs()) ) {
                  m_logger.warn("Cannot create user home :: " + homeDirStr);
                  throw new FtpException("Cannot create user home :: " + homeDirStr);
              }
          }
          
          OSVirualFileSystemView fsView = new OSVirualFileSystemView(user, m_logger);
          return fsView;
      }
      
  }
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/filesystem/OSVirualFileSystemView.java
  
  Index: OSVirualFileSystemView.java
  ===================================================================
  // $Id: OSVirualFileSystemView.java,v 1.1 2005/09/07 05:05:35 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 org.apache.ftpserver.ftplet.FileObject;
  import org.apache.ftpserver.ftplet.FileSystemView;
  import org.apache.ftpserver.ftplet.FtpException;
  import org.apache.ftpserver.ftplet.Logger;
  import org.apache.ftpserver.ftplet.User;
  
  import java.io.File;
  import java.io.IOException;
  
  
  /**
   * Get the user file system view.
   */
  public 
  class OSVirualFileSystemView implements FileSystemView {
  
      private Logger m_logger;
      
      private String m_rootName;
      private FileObject m_currDir;
      private boolean m_hasWritePermission;
      
      /**
       * Constructor - set the user object.
       */
      public OSVirualFileSystemView(User user, Logger logger) throws FtpException {
          m_logger = logger;
          try {
              File root = new File(user.getHomeDirectory());
              m_rootName = root.getCanonicalPath();
              m_rootName = OSVirtualFileObject.normalizeSeparateChar(m_rootName);
              if( !m_rootName.endsWith("/") ) {
                  m_rootName += '/';
              }
              
              m_hasWritePermission = user.getWritePermission();
              m_currDir = new OSVirtualFileObject(root, m_rootName, m_hasWritePermission);
          }
          catch(IOException ex) {
              m_logger.warn("OSVirualFileSystemView.OSVirualFileSystemView()", ex);
              throw new FtpException("OSVirualFileSystemView.OSVirualFileSystemView()", ex);
          }
      }
          
      /**
       * Get the user root directory.
       */
      public FileObject getRootDirectory() throws FtpException {
          return new OSVirtualFileObject(new File(m_rootName), m_rootName, m_hasWritePermission);
      }
      
      /**
       * Get current directory.
       */
      public FileObject getCurrentDirectory() throws FtpException {
          return m_currDir;
      }
      
      /**
       * Change current directory.
       */
      public boolean changeDirectory(FileObject dir) throws FtpException {
          boolean retVal = false;
          if(dir.isDirectory()) {
              m_currDir = dir;
              retVal = true;
          }
          return retVal;
      }
      
      /**
       * Get the file object.
       */
      public FileObject getFileObject(String fileStr) throws FtpException {
          try {
              
              // get the physical file object
              fileStr = OSVirtualFileObject.normalizeSeparateChar(fileStr);
              File physicalFile = null;
              if(fileStr.startsWith("/")) {
                  physicalFile = new File(m_rootName, fileStr);
              }
              else {
                  File physicalCurrDir = ((OSVirtualFileObject)m_currDir).getPhysicalFile();
                  physicalFile = new File(physicalCurrDir, fileStr);
              }
              
              // get full name of the file
              String physicalFileStr = physicalFile.getCanonicalPath();
              physicalFileStr = OSVirtualFileObject.normalizeSeparateChar(physicalFileStr);
              if( physicalFile.isDirectory() && (!physicalFileStr.endsWith("/")) )
{
                  physicalFileStr += '/';
              }
              
              // not under the virtual root - not available
              FileObject virtualFile = null; 
              if( physicalFileStr.startsWith(m_rootName) ) {
                  virtualFile = new OSVirtualFileObject(physicalFile, m_rootName, m_hasWritePermission);
              }
              return virtualFile;
          }
          catch(IOException ex) {
              m_logger.warn("OSVirtualFileSystemView.getFileObject()", ex);
              throw new FtpException("OSVirtualFileSystemView.getFileObject()", ex);
          }
      }
      
      /**
       * List file objects
       */
      public FileObject[] listFiles(String file) throws FtpException {
          
          // get file object
          FileObject virtualFile = getFileObject(file);
          if(virtualFile == null) {
              return null;
          }
          File physicalFile = ((OSVirtualFileObject)virtualFile).getPhysicalFile();
          
          // does not exist - return null
          if(!physicalFile.exists()) {
              return null;
          } 
          
          // file - return a single element array 
          if(physicalFile.isFile()) {
              return new FileObject[] {
                             new OSVirtualFileObject(physicalFile, m_rootName, m_hasWritePermission)
                         };
          }
          
          // directory - return all the files
          File[] physicalFiles = physicalFile.listFiles();
          if(physicalFiles == null) {
              return null;
          }
          
          // now return all the files under the directory
          FileObject[] virtualFiles = new FileObject[physicalFiles.length];
          for(int i=0; i<physicalFiles.length; ++i) {
              virtualFiles[i] = new OSVirtualFileObject(physicalFiles[i], m_rootName, m_hasWritePermission);
          }
          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