/*****************************************************************************/
/* File name: FileSystemExpert.java                                          */
/* Purpose: to work with file systems                                        */
/*                                                                           */
/* Last modified: 15.08.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*                                                                           */
/* This program is distributed in the hope that it will be useful, but       */
/* WITHOUT ANY WARRANTY; without even the implied warranty of                */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General */
/* Public License for more details.                                          */
/*                                                                           */
/* This software is distributed under the terms                              */
/* of the GNU General Public License as published                            */
/* by the Free Software Foundation; either version 2, or (at your option)    */
/* any later version.                                                        */
/* See file COPYING for your rights (GNU GPL)                                */
/*****************************************************************************/

package io;


import java.io.*;
import general.*;
import jtar.*;
import mount.*;


public
final
class

FileSystemExpert
{

  private final static String RCSID =
"$Header: /d/NFS/io/RCS/FileSystemExpert.java,v 1.9 1999/08/15 23:38:58 vklimov Exp $";


  public

  FileSystemExpert()
/*****************************************************************************/
/* Function name: FileSystemExpert                                           */
/* File name: FileSystemExpert.java                                          */
/* Purpose: constructor for the FileSystemExpert class                       */
/* Parameters:                                                               */
/* Returns:                                                                  */
/* Last modified: 20.06.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
                                                    /* FileSystemExpert */
/*---------------------------------------------------------------------------*/

    }
/*end of FileSystemExpert*/


  public
  static
  InputStream

  openInputStream( Filable file )
/*****************************************************************************/
/* Function name: openInputStream                                            */
/* File name: FileSystemExpert.java                                          */
/* Purpose: to return an open InputStream object corresponding               */
/*          to the file                                                      */
/* Parameters: file                                                          */
/* Returns: the InputStream object or                                        */
/*          null if the file is not known or                                 */
/*          does not exist or in case of some other error                    */
/* Last modified: 09.08.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      String dirPath = null;
      Fhandle fileHandle = null;
      FileSystemable fileSystem = null;
      TarFileSystem tarFileSystem = null;
      InputStream result = null;
                                                        /* openInputStream */
/*---------------------------------------------------------------------------*/

      if (file == null)
        {
          return null;                                 /* return ! */
        }
      else
        ;
    /*end of if*/

      try
        {
          if ( file.getClass().equals(
                 Class.forName(
                   Constants.
               REGULAR_FILE_CLASS_FULLY_QUALIFIED_NAME ) )
             )
            {
              result = (InputStream) new
                FileInputStream( (File) file );

              return result;                               /* return ! */
            }
          else /* don't know anything else yet */
            ;
        /*end of if*/

          fileSystem = (FileSystemable) file;

          if ( (fileSystem != null) &&
               (fileSystem.getClass().equals( TarFileSystem.class ) )
             )
            {
               tarFileSystem = (TarFileSystem) fileSystem;

               result = tarFileSystem.openInputStream();

               return result;                               /* return ! */

            }
          else
            {
            }
        /*end of if on tar extentions*/

          return null;                               /* return ! */

        }
      catch( ClassNotFoundException e )
        {
          System.out.println( "ClassNotFoundException!\n" +
                              e.getMessage()                );
        }
      catch( FileNotFoundException e )
        {
          System.out.println( "FileNotFoundException!\n" +
                              e.getMessage()                );
        }
      catch( SecurityException e )
        {
          System.out.println( "SecurityException!\n" +
                              e.getMessage()                );
        }
    /*end of try-catch*/

      return null;

    }
/*end of openInputStream*/


  public
  static
  Filable

  getFilable( String dirPath )
/*****************************************************************************/
/* Function name: getFilable                                                 */
/* File name: FileSystemExpert.java                                          */
/* Purpose: to return a Filable object used to provide the same methods      */
/*          as java.io.File do                                               */
/*          That means for this kind of filesystems if it is recognized      */
/* Parameters: dirPath                                                       */
/* Returns: the Filable object or null if the filesystem is not known or     */
/*          does not exist or in case of some other error                    */
/* Last modified: 20.06.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      Filable result = null;
      File dirFile = null;
                                                        /* getFilable */
/*---------------------------------------------------------------------------*/

      if (dirPath == null)
        {
          return null;                                 /* return ! */
        }
      else
        ;
    /*end of if*/

      dirFile = new File( dirPath );

      if ( ( ! dirFile.exists() ) ||
           ( ! dirFile.canRead() )
         )
        {

          System.out.println( "file does not exist or\n" +
                              "can\'t read it" );

          return null;                                      /* return ! */
        }
      else
        ;
    /*end of if*/


      result = (Filable) new RegularFile( dirPath );

      return result;

    }
/*end of getFilable*/


  public
  static
  void

  processAllUnmount( String cacheDir )
/*****************************************************************************/
/* Function name: processAllUnmount                                          */
/* File name: FileSystemExpert.java                                          */
/* Purpose: to clean everything for a request to unmount everything          */
/* Parameters: cacheDir                                                      */
/* Returns:                                                                  */
/* Last modified: 15.08.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
                                                        /* processAllUnmount */
/*---------------------------------------------------------------------------*/

      TarFileSystem.removeAllCacheFiles( cacheDir );

    }
/*end of processAllUnmount*/


  public
  static
  void

  processUnmount( Fhandle fileHandle,
                  String cacheDir      )
/*****************************************************************************/
/* Function name: processUnmount                                             */
/* File name: FileSystemExpert.java                                          */
/* Purpose: to release resources relevant to the given file handle           */
/*          for a unmount request relevant to this file handle               */
/* Parameters: fileHandle                                                    */
/*             cacheDir                                                      */
/* Returns:                                                                  */
/* Last modified: 15.08.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      long fileId;
                                                        /* processUnmount */
/*---------------------------------------------------------------------------*/

      fileId = fileHandle.getFileId();
      TarFileSystem.removeCacheFile( fileId,
                                     cacheDir );

    }
/*end of processUnmount*/


  public
  static
  FileSystemable

  getFileSystem( String dirPath,
                 String cacheDir )
/*****************************************************************************/
/* Function name: getFileSystem                                              */
/* File name: FileSystemExpert.java                                          */
/* Purpose: to return a FileSystemable object                                */
/* Parameters: dirPath                                                       */
/*             cacheDir                                                      */
/* Returns: the FileSystemable object or null if the filesystem is not known */
/*          or does not exist or in case of some other error                 */
/* Last modified: 09.08.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      AllActions allActions = null;
      FileSystemable result = null;
      File dirFile = null;
                                                        /* getFileSystem */
/*---------------------------------------------------------------------------*/

      if (dirPath == null)
        {
          return null;                                 /* return ! */
        }
      else
        ;
    /*end of if*/

      dirFile = new File( dirPath );

      if ( ( ! dirFile.exists() ) ||
           ( ! dirFile.canRead() )
         )
        {

          System.out.println( "file does not exist or\n" +
                              "can\'t read it" );
          return null;                                      /* return ! */
        }
      else
        ;
    /*end of if*/

      allActions = new AllActions();

      if ( dirPath.endsWith(
             Constants.GZIPPED_TAR_EXTENTION_STRING ) ||
           dirPath.endsWith(
             Constants.GZ_AND_TAR_EXTENTION_STRING )
         )
        allActions.gzip.flag = true;
      else
        allActions.gzip.flag = false;
    /*end of if*/

      try
        {
          if ( dirPath.endsWith(
                 Constants.GZIPPED_TAR_EXTENTION_STRING ) ||
               dirPath.endsWith(
                 Constants.GZ_AND_TAR_EXTENTION_STRING ) ||
               dirPath.endsWith(
                 Constants.TAR_EXTENTION_STRING )
             )
            {
              result = (FileSystemable) new
                TarFileSystem( dirPath,
                               allActions,
                               cacheDir    );

            }
          else
            {
              result = (FileSystemable) new RegularFileSystem( dirPath );
            }
        /*end of if on tar extentions*/
        }
      catch( NullPointerException e )
        {
          System.out.println( "getFileSystem: " +
                              "NullPointerException!\n" +
                              e.getMessage()               );
        }
    /*end of try-catch*/

      return result;

    }
/*end of getFileSystem*/


  public
  static
  void

  setFileHandle( Fhandle fileHandle )
/*****************************************************************************/
/* Function name: setFileHandle                                              */
/* File name: FileSystemExpert.java                                          */
/* Purpose: to set file handle for a file system if it needs it              */
/* Parameters: fileHandle                                                    */
/* Returns:                                                                  */
/* Last modified: 09.08.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      String dirPath = null;
      FileSystemable fileSystem = null;
      Filable filable = null;
                                                        /* setFileHandle */
/*---------------------------------------------------------------------------*/

      fileSystem = fileHandle.getFileSystem();
      filable = fileHandle.getFilable();

      if ( (fileSystem != null) &&
           (fileSystem.getClass().equals( TarFileSystem.class ) )
         )
        {
           ( (TarFileSystem)
             fileSystem).setFileHandle( fileHandle );
           ( (TarFileSystem)
             filable).setFileHandle( fileHandle );

        }
      else
        {
        }
    /*end of if on tar extentions*/

    }
/*end of setFileHandle*/


  public
  static
  Class

  getFileClass( String dirPath )
/*****************************************************************************/
/* Function name: getFileClass                                               */
/* File name: FileSystemExpert.java                                          */
/* Purpose: to return a Class object to be used as an equivalent             */
/*          of the java.io.File class to work with this particular dirPath   */
/* Parameters: dirPath                                                       */
/* Returns: the Class object or null if the filesystem is not known or       */
/*          does not exist or in case of some other error                    */
/* Last modified: 08.08.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      Class result = null;
      File dirFile = null;
                                                        /* getFileClass */
/*---------------------------------------------------------------------------*/

      if (dirPath == null)
        {
          return null;                                 /* return ! */
        }
      else
        ;
    /*end of if*/

      dirFile = new File( dirPath );

      if ( ( ! dirFile.exists() ) ||
           ( ! dirFile.canRead() )
         )
        {

          System.out.println( "file does not exist or\n" +
                              "can\'t read it" );

          return null;                                      /* return ! */
        }
      else
        ;
    /*end of if*/

      if ( dirPath.endsWith(
             Constants.GZIPPED_TAR_EXTENTION_STRING ) ||
           dirPath.endsWith(
             Constants.GZ_AND_TAR_EXTENTION_STRING ) ||
           dirPath.endsWith(
             Constants.TAR_EXTENTION_STRING )
         )
        result = (Class) TarFileSystem.class;
      else
        result = (Class) RegularFile.class;
    /*end of if on tar extentions*/

      return result;

    }
/*end of getFileClass*/


  public
  static
  void

  main( String[] args )
/*****************************************************************************/
/* Function name: main                                                       */
/* File name: FileSystemExpert.java                                          */
/* Purpose: main method for the FileSystemExpert class                       */
/* Parameters: agrument strings array                                        */
/* Returns:                                                                  */
/* Last modified: 20.06.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      FileInputStream fileInputStream = null;
      RegularFile file = null;
                                                             /* main */
/*---------------------------------------------------------------------------*/

      file = new RegularFile( "/tmp/t.sh" );

      fileInputStream = (FileInputStream)
        openInputStream( file );

      System.exit( 0 );

    }
/*end of main*/


} /* end of FileSystemExpert class */

/*****************************************************************************/
/* End of file: FileSystemExpert.java                                        */
/*****************************************************************************/
