/*****************************************************************************/
/* File name: ActionTable.java                                               */
/* Purpose: to keep an option and a corresponding flag for Jtar              */
/* Last modified: 08.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 jtar;


import java.io.*;
import java.util.*;
import general.*;
import io.*;


public
class

ActionTable

                   extends ActionRead

     implements jtar.Constants,
                general.Constants,
                Serializable

{

  private final static String RCSID =
"$Header: /d/NFS/jtar/RCS/ActionTable.java,v 1.5 1999/08/08 20:28:03 vklimov Exp $";

  private final static String HEADER_DIALOG_TITLE   = "List";
  private final static int    HEADER_DIALOG_ROWS    = 23;
  private final static int    HEADER_DIALOG_COLUMNS = 77;

  private final static String LIST_COMPLETED_MSG = "\nList completed";

  public
  ActionTable( char    option,
               boolean flag   )
/*****************************************************************************/
/* Function name: ActionTable                                                */
/* File name: ActionTable.java                                               */
/* Purpose: constructor for the ActionTable object                           */
/* Parameters: option - 't', 'x', 'c' etc                                    */
/*             flag - corresponding flag                                     */
/* Returns:                                                                  */
/* Last modified: 11.12.98                                                   */
/* Author: Victor Klimov, 1998                                               */
/*****************************************************************************/
    {
                                                        /* ActionTable */
/*---------------------------------------------------------------------------*/

      super( option, flag );

    }
/*end of ActionTable*/

  public
  boolean
  action( Vector commandLineOptionsVector,
          Header dummyTarHeader,
          BufferRead dummyReadBuffer,
          boolean fillFileSystem,
          io.TarFileSystem ramFileSystem,
          AllActions allActions            )
/*****************************************************************************/
/* Function name: action to list archive members                             */
/* File name: ActionTable.java                                               */
/* Purpose: to list elements of a tar archive                                */
/* Parameters: commandLineOptionsVector - vector, containig strings          */
/*                                 of command line arguments to Jtar         */
/*             dummyTarHeader                                                */
/*             dummyReadBuffer                                               */
/*             fillFileSystem - if true, we won't list, we just fill         */
/*                              the contents of the TarFileSystem object     */
/*             ramFileSystem - must me not null if fillFileSystem is true    */
/*             allActions                                                    */
/* Returns: true - if everything is all right, false - otherwise             */
/* Last modified: 08.08.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      int size = 0;
      int written = 0;
      boolean check = false;
      Block dataBlock = null;
      boolean readOK = true;
      boolean resultSuperAction = true;

      BufferRead readBuffer = new BufferRead( allActions );
      Header tarHeader = new Header();

      boolean isextended = false;        /* to remember if currentHeader */
                                         /* is extended                   */

                                                          /* action */
/*---------------------------------------------------------------------------*/


      if ( (fillFileSystem == true) &&
           (ramFileSystem == null)
         )
        {
          return false;                                  /* return ! */
        }
      else
        ;
    /*end of if on null*/

      readOK =
        readBuffer.open( allActions );

      if (!readOK)
        return false;                                  /* return ! */
      else
        ;
    /*end of if on readOK*/

      while( true )
        {
          resultSuperAction =
            super.action( commandLineOptionsVector,
                          tarHeader,
                          readBuffer,
                          allActions                );

          if ( (tarHeader.status == HEADER_ZERO_BLOCK) ||
               (tarHeader.status == HEADER_END_OF_FILE) ||
               (tarHeader.status == HEADER_FAILURE)
             )

            break;                                          /* break ! */

          else
            ;
        /*end of if on status*/

                                                  /* Print the header block */
          tarHeader.decodeHeader( false,  /* no user group */
                                  allActions );

          tarHeader.printHeader( (Buffer) readBuffer,
                                 fillFileSystem,
                                 ramFileSystem,
                                 allActions           );

          if (allActions.incremental.flag &&
              tarHeader.currentHeader.
                               typeflag == GNUTYPE_DUMPDIR)
            {
              readBuffer.
                permitToFillUpNewCurrentBlock();

              if (allActions.multiVolume)
                {
                  readBuffer.saveName = allActions.currentFileName;
                  readBuffer.saveTotsize =
                    allActions.currentStat.size;
                }
              else
                ;
            /*end of if on multiVolume*/

              for (size = allActions.
                     currentStat.size;
                   size > 0; size -= written)
                {
                  if (allActions.multiVolume)
                    readBuffer.saveSizeleft = size;
                  else
                    ;
                /*end of if on multiVolume*/

                  dataBlock = readBuffer.
                                 getCurrentDataBlock( allActions );

                  if (dataBlock == null)
                    {
                      System.out.println(
                        "EOF in archive file"    );
                                  /* FIXME: What happens, then?  */
                      break;                                   /* break ! */
                    }
                  else
                    ;
                /*end of if on multiVolume*/

                  written = readBuffer.
                    availableSpaceAfter();

                  if (written > size)
                    written = size;
                  else
                    ;
                /*end of if on size*/

                  System.out.write( dataBlock.buffer,
                                    0, written         );

                  readBuffer.
                    permitToFillUpNewCurrentBlock();

                  check =
                    System.out.checkError();
                  if (check)
                    {
                      System.out.println(
                          "Error in dumping"      );
                      readBuffer.skipFile( (size - written),
                                           allActions        );
                      break;                           /* break ! */
                    }
                  else
                    ;
                /*end of if on check*/
                }
            /*end of for-loop on size*/

              if (allActions.multiVolume)
                readBuffer.saveName = null;
              else
                ;
            /*end of if on multiVolume*/

              System.out.println();
              System.out.flush();
              readBuffer.close();
              return true;                             /* return ! */

            }
          else
            ;
        /*end of if on GNUTYPE_DUMPDIR*/
                                      /* Check to see if we have an extended */
                                      /* header to skip over also            */

          if (tarHeader.currentHeader.
                             isextended != 0)
            isextended = true;
          else
            ;
        /*end of if on isextended*/
                                      /* Skip past the header in the archive */

          readBuffer.
            permitToFillUpNewCurrentBlock();
                                                 /* If we needed to skip     */
                                                 /* any extended headers, do */
                                                 /* so now, by reading       */
                                                 /* extended headers and     */
                                                 /* skipping past them       */
                                                 /* in the archive           */

          if (isextended)
            readBuffer.skipExtendedHeaders( allActions );
          else
            ;
        /*end of if on isextended*/

          if (allActions.multiVolume)
            readBuffer.saveName =
              allActions.currentFileName;
          else
            ;
        /*end of if on multiVolume*/
                                                  /* skip to the next header */
                                                  /* on the archive          */

          readBuffer.skipFile(
            allActions.currentStat.size,
            allActions                   );

          if (allActions.multiVolume)
            readBuffer.saveName = null;
          else
            ;
        /*end of if on multiVolume*/

        }
    /*end of while-loop*/

      readBuffer.close();
      Names.namesNotFound();            /* print names not found */

      return resultSuperAction;

    }
/*end of action*/


} /*end of class ActionTable */

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