Author: Raymond Bosman
Date: 2006-08-09 17:12:10 +0200 (Wed, 09 Aug 2006)
New Revision: 3256

Log:
- Cleaned up the code.

Modified:
   trunk/Archive/src/file/block_file.php
   trunk/Archive/src/file/character_file.php
   trunk/Archive/src/file/file.php
   trunk/Archive/src/tar/v7_tar.php

Modified: trunk/Archive/src/file/block_file.php
===================================================================
--- trunk/Archive/src/file/block_file.php       2006-08-09 15:11:39 UTC (rev 
3255)
+++ trunk/Archive/src/file/block_file.php       2006-08-09 15:12:10 UTC (rev 
3256)
@@ -267,7 +267,7 @@
      */
     public function append( $data )
     {
-        if ( $this->readOnly ) 
+        if ( $this->fileAccess == self::READ_ONLY ) 
         {
             throw new ezcBaseFilePermissionException( $this->fileName, 
ezcBaseFilePermissionException::WRITE, "The archive is opened in a read-only 
mode." );
         }
@@ -300,7 +300,7 @@
 
             if( $needToTruncate )
             {
-                if( $this->readWriteSwitch >= 0 )
+                if( $this->fileAccess == self::READ_APPEND )
                 {
                     // Sorry, don't know how to truncate this file (except 
copying everything).
                     throw new ezcArchiveException( "Cannot truncate the file" 
);
@@ -464,7 +464,7 @@
         // Empty files don't need to be truncated.
         if( $this->isEmpty() ) return true;
 
-        if( $this->readWriteSwitch < 0 )
+        if( $this->fileAccess !== self::READ_APPEND )
         {
             // We can read-write in the file. Easy.
             $pos = $blocks * $this->blockSize;
@@ -620,9 +620,9 @@
      */
     public function seek( $blockOffset, $whence = SEEK_SET )
     {
-        if( $this->writeOnly && $blockOffset == 0 && $whence == SEEK_END ) 
return true;
+        if( $this->fileAccess == self::WRITE_ONLY && $blockOffset == 0 && 
$whence == SEEK_END ) return true;
 
-        if( ftell( $this->fp ) === false || $this->readWriteSwitch >= 0)
+        if( ftell( $this->fp ) === false || $this->fileAccess == 
self::READ_APPEND)
         {
             // Okay, cannot tell the current file position. 
             // This happens with some compression streams. 

Modified: trunk/Archive/src/file/character_file.php
===================================================================
--- trunk/Archive/src/file/character_file.php   2006-08-09 15:11:39 UTC (rev 
3255)
+++ trunk/Archive/src/file/character_file.php   2006-08-09 15:12:10 UTC (rev 
3256)
@@ -198,7 +198,7 @@
      */
     public function append( $data )
     {
-        if ( $this->readOnly ) 
+        if ( $this->fileAccess == self::READ_ONLY ) 
             throw new ezcBaseFilePermissionException( $this->fileName, 
ezcBaseFilePermissionException::WRITE, "The archive is opened in a read-only 
mode." );
         
         $pos = ftell( $this->fp );
@@ -371,7 +371,7 @@
      */
     public function write( $data )
     {
-        if ( $this->readOnly ) 
+        if ( $this->fileAccess == self::READ_ONLY ) 
             throw new ezcBaseFilePermissionException( $this->fileName, 
ezcBaseFilePermissionException::WRITE, "The archive is opened in a read-only 
mode." );
        
         $pos = ftell( $this->fp );

Modified: trunk/Archive/src/file/file.php
===================================================================
--- trunk/Archive/src/file/file.php     2006-08-09 15:11:39 UTC (rev 3255)
+++ trunk/Archive/src/file/file.php     2006-08-09 15:12:10 UTC (rev 3256)
@@ -19,80 +19,92 @@
  */ 
 abstract class ezcArchiveFile implements Iterator 
 {
-    /**
-     * The current location in the open file.
-     *
-     * @var resource  
+    /** 
+     * The file is read-only. 
+     * The file permissions can be set to read-only or file is compressed with 
a 
+     * stream that can only be read. E.g. bzip2.
      */
-    protected $fp = null;
+    const READ_ONLY   = 1; 
 
+    /** 
+     * The file is write-only. 
+     * The file permissions can be set to write-only or file should be 
compressed with a 
+     * stream that can only write. E.g. bzip2.
+     */
+    const WRITE_ONLY  = 2; 
+
+    /** 
+     * The file is either read or append mode. 
+     * Some compressed streams (zlib) do not support reading and writing. But 
seperate reading
+     * and appending does work. 
+     */
+    const READ_APPEND = 3; 
+
     /**
-     * True if the current file is opened read-only. 
-     *
-     * @var boolean  
+     * The file is opened in a read and write mode.
      */
-    protected $readOnly = false;
+    const READ_WRITE  = 4; 
 
-    protected $writeOnly = false;
 
     /**
-     * True when the current block is valid, otherwise false.
+     * The mode the file is opened in. It has one of the following constant 
values: 
+     * READ_ONLY, WRITE_ONLY, READ_APPEND, or READ_WRITE.
      *
-     * @var boolean  
+     * @var int
      */
-    protected $isValid = false;
+    protected $fileAccess = null; 
 
     /**
-     * True when the current file does not have any blocks, otherwise false.
-     * 
-     * @var boolean  
+     * The current resource of the opened file. 
+     * If the file is closed, this resource should point to NULL.
+     *
+     * @var resource  
      */
-    protected $isEmpty;
+    protected $fp = null;
 
     /** 
-     * The name of the character of block file.
+     * The name of the file.
      *
      * @var string
      */
     protected $fileName;
 
     /**
+     * True when the current file does not have any blocks, otherwise false.
+     * 
+     * @var boolean  
+     */
+    protected $isEmpty;
+
+    /**
      * True if the file-pointer supports seeking, otherwise false.
      * For example, files that use the bzip2 stream cannot seek.  
      *
      * @var boolean       
      */
-    protected $fpIsSeekable;
+    protected $fileMetaData;
 
     /**
-     * The mode that the file is opened. 
-     * For example: "r+w", "rb", etc. 
+     * True when the current block is valid, otherwise false.
      *
-     * @var string   
+     * @var boolean  
      */
-    protected $fpMode;
+    protected $isValid = false;
 
-    /**
-     * The Uri of the file.
-     *
-     * @var string   
-     */
-    protected $fpUri;
 
-    protected $streamFilters;
 
-    protected $readWriteSwitch = -1;
+    const SWITCH_READ = 0;
+    const SWITCH_APPEND = 1;
 
+    /**
+     *  
+     */
+    protected $readAppendSwitch;
+
     protected $isNew;
 
     protected $isModified;
 
-
-    const FP_POS_BEGIN = -1;
-    const FP_POS_MIDDLE = 0;
-    const FP_POS_END = 1;
-    protected $fpPosition;
-
     protected function openFile( $fileName, $createIfNotExist )
     {
         if( $createIfNotExist && !self::fileExists( $fileName ) )
@@ -111,40 +123,42 @@
         
         // Try to open it in read and write mode.
         $this->fp = @fopen( $fileName, "r+b" );
-        if ( !$this->fp )
+        if ( $this->fp )
         {
+            $this->fileAccess = self::READ_WRITE;
+        }
+        else
+        {
             // Try to open it in read-only mode.
             $this->fp = @fopen( $fileName, "rb" );
-            $this->readOnly = true;
-        }
+            $this->fileAccess = self::READ_ONLY;
 
-        // Check if we opened the file. 
-        if ( !$this->fp )
-        {
-            if( !self::fileExists( $fileName ) )
+            // Check if we opened the file. 
+            if ( !$this->fp )
             {
-                throw new ezcBaseFileNotFoundException( $fileName );
-            }
+                if( !self::fileExists( $fileName ) )
+                {
+                    throw new ezcBaseFileNotFoundException( $fileName );
+                }
 
-            // Cannot read the file.
-            throw new ezcBaseFilePermissionException( $fileName, 
ezcBaseFilePermissionException::READ );
+                // Cannot read the file.
+                throw new ezcBaseFilePermissionException( $fileName, 
ezcBaseFilePermissionException::READ );
+            }
         }
 
-        $status =  stream_get_meta_data( $this->fp );
+        $this->fileMetaData = stream_get_meta_data( $this->fp );
         
-        // Set some modes.
-        $this->fpIsSeekable = $status["seekable"];
-        $this->fpMode = $status["mode"];
-        $this->fpUri = $status["uri"];
-
         // Hardcode BZip2 to read-only.
         // For some reason we can open the file in read-write mode, but we 
cannot rewind the fp. Strange..
-        if ( $status["wrapper_type"] == "BZip2" ) $this->readOnly = true;
+        if ( $this->fileMetaData["wrapper_type"] == "BZip2" ) 
+        {
+            $this->fileAccess = self::READ_ONLY;
+        }
 
         // Why is it read only?
-        if( $this->readOnly )
+        if( $this->fileAccess == self::READ_ONLY )
         {
-            if( $status["wrapper_type"] == "ZLIB" || $status["wrapper_type"] 
== "BZip2" ) 
+            if( $this->fileMetaData["wrapper_type"] == "ZLIB" || 
$this->fileMetaData["wrapper_type"] == "BZip2" ) 
             {
                 // Append mode available?
                 $b = @fopen( $fileName, "ab" );
@@ -154,8 +168,8 @@
                     fclose( $b );
 
                     // The file is either read-only or write-only.
-                    $this->readOnly = false;
-                    $this->readWriteSwitch = 0; // Set to reading.
+                    $this->fileAccess = self::READ_APPEND;
+                    $this->readAppendSwitch = self::SWITCH_READ; 
                 }
                 else
                 {
@@ -171,8 +185,8 @@
                             $this->fp = $b;
 
                             $this->isEmpty = true;
-                            $this->readOnly = false;
-                            $this->writeOnly = true;
+                            $this->fileAccess = self::WRITE_ONLY;
+
                             $this->fileName = $fileName;
                             $this->isModified = false;
 
@@ -259,7 +273,7 @@
     public function switchWriteMode()
     {
         // Switch only when we are in read (only) mode.
-        if( $this->readWriteSwitch == 0 )
+        if( $this->fileAccess == self::READ_APPEND && $this->readAppendSwitch 
== self::SWITCH_READ )
         {
             fclose( $this->fp );
             $this->fp = fopen( $this->fileName, "ab" );
@@ -267,14 +281,14 @@
             {
                 throw new ezcBaseFilePermissionException( 
self::getPureFileName( $this->fileName ), 
ezcBaseFilePermissionException::WRITE, "Cannot switch to write mode");
             }
-            $this->readWriteSwitch = 1;
+            $this->readAppendSwitch = self::SWITCH_APPEND;
         }
     }
 
     public function switchReadMode( $pos = 0)
     {
         // Switch only when we are in write (only) mode.
-        if( $this->readWriteSwitch == 1 )
+        if( $this->fileAccess == self::READ_APPEND && $this->readAppendSwitch 
== self::SWITCH_APPEND )
         {
             fclose( $this->fp );
 
@@ -286,7 +300,8 @@
             {
                 throw new ezcBaseFilePermissionException( 
self::getPureFileName( $this->fileName ), ezcBaseFilePermissionException::READ, 
"Cannot switch back to read mode");
             }
-            $this->readWriteSwitch = 0;
+            $this->readAppendSwitch = self::SWITCH_READ;
+
             //$this->positionSeek( $pos );
             $this->positionSeek( 0, SEEK_END );
 
@@ -298,7 +313,7 @@
 
     public function isReadOnlyWriteOnlyStream()
     {
-        return ( $this->readWriteSwitch >= 0 );
+        return $this->fileAccess == self::READ_APPEND; 
     }
 
 
@@ -367,10 +382,10 @@
         {
             $this->isValid = true;
 
-            if ( !$this->fpIsSeekable )
+            if ( !$this->fileMetaData["seekable"] )
             {
                 fclose( $this->fp );
-                $this->fp = fopen( $this->fpUri, $this->fpMode );
+                $this->fp = fopen( $this->fileMetaData["uri"], 
$this->fileMetaData["mode"] );
             }
             else
             {
@@ -385,24 +400,12 @@
         }
     }
 
-    public function getStreamInformation()
-    {
-        $status =  stream_get_meta_data( $this->fp );
-       // $status =  socket_get_status( $this->fp );
-        $this->fpIsSeekable = $status["seekable"];
-        $this->fpMode = $status["mode"];
-        $this->fpUri = $status["uri"];
-
-        // Hardcode BZip2 to read-only.
-        if ( $status["wrapper_type"] == "BZip2" && !$this->readOnly ) 
$this->readOnly = true;
-    }
-
     protected function positionSeek( $pos, $whence = SEEK_SET)
     {
         // Seek the end of the file in a write only file always succeeds.
-        if( $this->writeOnly && $pos == 0 && $whence == SEEK_END ) return 
true; 
+        if( $this->fileAccess == self::WRITE_ONLY && $pos == 0 && $whence == 
SEEK_END ) return true; 
 
-        if ( $this->fpIsSeekable )
+        if ( $this->fileMetaData["seekable"] )
         {
           return fseek( $this->fp, $pos, $whence );
         }
@@ -426,7 +429,7 @@
             if ( $transPos <  $cur )
             {
                 fclose( $this->fp );
-                $this->fp = fopen( $this->fpUri, $this->fpMode );
+                $this->fp = fopen( $this->fileMetaData["uri"], 
$this->fileMetaData["mode"] );
 
                 $cur = 0;
             }
@@ -441,29 +444,24 @@
         }
     }
 
-    public function isReadOnly()
+    public function getFileAccess()
     {
-        return $this->readOnly;
+        return $this->fileAccess;
     }
 
-    public function isWriteOnly()
+    public function isReadOnly()
     {
-        return $this->writeOnly;
+        return $this->fileAccess == self::READ_ONLY;
     }
 
-
-    public function appendStreamFilter( $filter )
+    public function isWriteOnly()
     {
-        $this->streamFilters[] = stream_filter_append( $this->fp, $filter );
-        //var_dump ( $this->streamFilters );
+        return $this->fileAccess == self::WRITE_ONLY;
     }
 
-    public function removeStreamFilter()
-    {
-        stream_filter_remove( array_pop( $this->streamFilters ) );
-    }
-    
 
+   
+
     public function isNew() 
     {
         return $this->isNew;

Modified: trunk/Archive/src/tar/v7_tar.php
===================================================================
--- trunk/Archive/src/tar/v7_tar.php    2006-08-09 15:11:39 UTC (rev 3255)
+++ trunk/Archive/src/tar/v7_tar.php    2006-08-09 15:12:10 UTC (rev 3256)
@@ -96,7 +96,7 @@
         $this->hasNullBlocks = $this->file->isNew() ?  false : true;
         $this->addedBlocks = 0;
 
-        $this->readCurrentFromArchive();
+        if( $this->file->getFileAccess() !== ezcArchiveFile::WRITE_ONLY) 
$this->readCurrentFromArchive();
     }
 
     public function __destruct()
@@ -143,12 +143,8 @@
      * @return void
      */
     // XXX should be protected?
-    public function readCurrentFromArchive()
+    protected function readCurrentFromArchive()
     {
-        if( $this->file === null ) throw new ezcArchiveException( "The archive 
is closed" );
-
-        if( $this->file->isWriteOnly() ) return; 
-
         // Not cached, read the next block.
         if ( $this->entriesRead == 0 )
         {
@@ -234,7 +230,7 @@
     {
         if( $this->file === null ) throw new ezcArchiveException( "The archive 
is closed" );
 
-        if ( !$this->isWritable() )
+        if ($this->file->getFileAccess() === ezcArchiveFile::READ_ONLY || 
!$this->algorithmCanWrite())
         {
             throw new ezcBaseFilePermissionException( 
$this->file->getFileName(), ezcBaseFilePermissionException::WRITE, "Archive is 
read-only" );
         }
@@ -272,9 +268,6 @@
 
             $this->entriesRead = $fileNumber;
             $this->completed = true;
-
-            //if ( $appendNullBlocks ) $this->appendNullBlocks();
-
             $this->fileNumber = $originalFileNumber;
 
             return $this->valid();
@@ -286,12 +279,12 @@
     {
         if( $this->file === null ) throw new ezcArchiveException( "The archive 
is closed" );
         
-        if ($this->file->isReadOnlyWriteOnlyStream() )
+        if ($this->file->getFileAccess() !== ezcArchiveFile::READ_WRITE )
         {
             throw new ezcArchiveException( "Cannot appendToCurrent when 
writing to a read-only, write-only stream (e.g. compress.zlib)." );
         }
 
-        if ( !$this->isWritable() )
+        if ($this->file->getFileAccess() === ezcArchiveFile::READ_ONLY || 
!$this->algorithmCanWrite())
         {
             throw new ezcBaseFilePermissionException( 
$this->file->getFileName(),  ezcBaseFilePermissionException::WRITE );
         }
@@ -327,21 +320,21 @@
     {
         if( $this->file === null ) throw new ezcArchiveException( "The archive 
is closed" );
 
-        if ( !$this->isWritable() )
+        if ($this->file->getFileAccess() === ezcArchiveFile::READ_ONLY || 
!$this->algorithmCanWrite())
         {
             throw new ezcArchiveException( "Archive is read-only" );
         }
 
         // Appending to an existing archive with a compressed stream does not 
work because we have to remove the NULL-blocks. 
-        if( $this->hasNullBlocks && $this->file->isReadOnlyWriteOnlyStream() )
+        if( $this->hasNullBlocks && $this->file->getFileAccess() !== 
ezcArchiveFile::READ_WRITE )
         {
             throw new ezcArchiveException( "Cannot append to this archive" );
         }
 
         // Existing files need to be read, because we don't know if it 
contains NULL-blocks at the end of the archive.
-        $this->seek( 0, SEEK_END );
+        if( $this->file->getFileAccess() !== ezcArchiveFile::WRITE_ONLY )
+            $this->seek( 0, SEEK_END );
 
-
         // Do the same as in appendToCurrent(). But we know that it's possible.
         $entries = $this->getEntries( $files, $prefix);
         $originalFileNumber = $this->fileNumber;
@@ -382,8 +375,6 @@
                 }
                 else
                 {
-
-
                     // Added Blocks  -  Added null blocks (Block factor 20)
                     // 0             -  0
                     // 1             - 19
@@ -397,14 +388,7 @@
                 $this->hasNullBlocks = true;
                 $this->addedBlocksNotReliable = false;
                 $this->addedBlocks = 0;
-
-
-                    //echo ("Append: ". $this->nullBlocksToAppend );
-                //$this->file->appendNullBlocks( $this->nullBlocksToAppend );
-
-                //die ("SHOULD WRITE NULL_BLOCKS" );
             }
-
         }
     }
 
@@ -428,7 +412,7 @@
         // Are we at a valid entry?
         if ( !$this->isEmpty() && !$this->valid() ) return false;
 
-        if ( !$this->isEmpty() && !$this->file->isWriteOnly())
+        if ( !$this->isEmpty() && $this->file->getFileAccess() !== 
ezcArchiveFile::WRITE_ONLY)
         {
             // Truncate the next file and don't add the null blocks.
             $this->truncate( $this->fileNumber + 1, false );
@@ -459,7 +443,7 @@
             $this->addedBlocks += $this->file->append( file_get_contents( 
$entry->getPath() ) );
         }
 
-        if(!( $this->file->isNew() && $this->file->isWriteOnly() ))
+        if(!( $this->file->isNew() && $this->file->getFileAccess() === 
ezcArchiveFile::WRITE_ONLY  ))
         {
             $this->addedBlocksNotReliable = true;
         }
@@ -472,45 +456,6 @@
         return true;
     }
 
-    protected function appendHeaderAndFileToEnd( $entry, $appendNullBlocks )
-    {
-        // Add the new header to the file map.
-        $header = $this->createTarHeader(); 
-        $header->setHeaderFromArchiveEntry( $entry );
-        $header->writeEncodedHeader( $this->file );
-
-        $this->addedBlocks += 1;
-        if ( $entry->getSize() > 0 ) 
-        {
-            // TODO FIX large file read. 
-            $this->addedBlocks += $this->file->append( file_get_contents( 
$entry->getPath() ) );
-        }
-
-        if ( $this->entriesRead == 0 ) 
-        {
-            $this->readCurrentFromArchive();
-            $this->completed = true;
-            $this->hasNullBlocks = false;
-/*
-            $this->headers[ 0 ] = $header;
-            $this->headerPositions[ 0 ] = 1;
-
-            $this->entriesRead = 1;
-            $this->fileNumber = 1;
-        $this->hasNullBlocks = false;
-        $this->completed = true;
- */
-        }
-        else
-        {
-
-            $this->completed = false;
-        }
-
-        $this->hasNullBlocks = false;
-        return true;
-    }
-
     /**
      * Appends zero or more null blocks to the end of the archive, so that it 
matches the $blockFactor.
      *

-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to