web/lib/Zend/Search/Lucene/Storage/File/Filesystem.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Search_Lucene
       
    17  * @subpackage Storage
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Filesystem.php 23395 2010-11-19 15:30:47Z alexander $
       
    21  */
       
    22 
       
    23 /** Zend_Search_Lucene_Storage_File */
       
    24 require_once 'Zend/Search/Lucene/Storage/File.php';
       
    25 
       
    26 /**
       
    27  * @category   Zend
       
    28  * @package    Zend_Search_Lucene
       
    29  * @subpackage Storage
       
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    32  */
       
    33 class Zend_Search_Lucene_Storage_File_Filesystem extends Zend_Search_Lucene_Storage_File
       
    34 {
       
    35     /**
       
    36      * Resource of the open file
       
    37      *
       
    38      * @var resource
       
    39      */
       
    40     protected $_fileHandle;
       
    41 
       
    42 
       
    43     /**
       
    44      * Class constructor.  Open the file.
       
    45      *
       
    46      * @param string $filename
       
    47      * @param string $mode
       
    48      */
       
    49     public function __construct($filename, $mode='r+b')
       
    50     {
       
    51         global $php_errormsg;
       
    52 
       
    53         if (strpos($mode, 'w') === false  &&  !is_readable($filename)) {
       
    54             // opening for reading non-readable file
       
    55             require_once 'Zend/Search/Lucene/Exception.php';
       
    56             throw new Zend_Search_Lucene_Exception('File \'' . $filename . '\' is not readable.');
       
    57         }
       
    58 
       
    59         $trackErrors = ini_get('track_errors');
       
    60         ini_set('track_errors', '1');
       
    61 
       
    62         $this->_fileHandle = @fopen($filename, $mode);
       
    63 
       
    64         if ($this->_fileHandle === false) {
       
    65             ini_set('track_errors', $trackErrors);
       
    66             require_once 'Zend/Search/Lucene/Exception.php';
       
    67             throw new Zend_Search_Lucene_Exception($php_errormsg);
       
    68         }
       
    69 
       
    70         ini_set('track_errors', $trackErrors);
       
    71     }
       
    72 
       
    73     /**
       
    74      * Sets the file position indicator and advances the file pointer.
       
    75      * The new position, measured in bytes from the beginning of the file,
       
    76      * is obtained by adding offset to the position specified by whence,
       
    77      * whose values are defined as follows:
       
    78      * SEEK_SET - Set position equal to offset bytes.
       
    79      * SEEK_CUR - Set position to current location plus offset.
       
    80      * SEEK_END - Set position to end-of-file plus offset. (To move to
       
    81      * a position before the end-of-file, you need to pass a negative value
       
    82      * in offset.)
       
    83      * SEEK_CUR is the only supported offset type for compound files
       
    84      *
       
    85      * Upon success, returns 0; otherwise, returns -1
       
    86      *
       
    87      * @param integer $offset
       
    88      * @param integer $whence
       
    89      * @return integer
       
    90      */
       
    91     public function seek($offset, $whence=SEEK_SET)
       
    92     {
       
    93         return fseek($this->_fileHandle, $offset, $whence);
       
    94     }
       
    95 
       
    96 
       
    97     /**
       
    98      * Get file position.
       
    99      *
       
   100      * @return integer
       
   101      */
       
   102     public function tell()
       
   103     {
       
   104         return ftell($this->_fileHandle);
       
   105     }
       
   106 
       
   107     /**
       
   108      * Flush output.
       
   109      *
       
   110      * Returns true on success or false on failure.
       
   111      *
       
   112      * @return boolean
       
   113      */
       
   114     public function flush()
       
   115     {
       
   116         return fflush($this->_fileHandle);
       
   117     }
       
   118 
       
   119     /**
       
   120      * Close File object
       
   121      */
       
   122     public function close()
       
   123     {
       
   124         if ($this->_fileHandle !== null ) {
       
   125             @fclose($this->_fileHandle);
       
   126             $this->_fileHandle = null;
       
   127         }
       
   128     }
       
   129 
       
   130     /**
       
   131      * Get the size of the already opened file
       
   132      *
       
   133      * @return integer
       
   134      */
       
   135     public function size()
       
   136     {
       
   137         $position = ftell($this->_fileHandle);
       
   138         fseek($this->_fileHandle, 0, SEEK_END);
       
   139         $size = ftell($this->_fileHandle);
       
   140         fseek($this->_fileHandle,$position);
       
   141 
       
   142         return $size;
       
   143     }
       
   144 
       
   145     /**
       
   146      * Read a $length bytes from the file and advance the file pointer.
       
   147      *
       
   148      * @param integer $length
       
   149      * @return string
       
   150      */
       
   151     protected function _fread($length=1)
       
   152     {
       
   153         if ($length == 0) {
       
   154             return '';
       
   155         }
       
   156 
       
   157         if ($length < 1024) {
       
   158             return fread($this->_fileHandle, $length);
       
   159         }
       
   160 
       
   161         $data = '';
       
   162         while ($length > 0 && !feof($this->_fileHandle)) {
       
   163             $nextBlock = fread($this->_fileHandle, $length);
       
   164             if ($nextBlock === false) {
       
   165                 require_once 'Zend/Search/Lucene/Exception.php';
       
   166                 throw new Zend_Search_Lucene_Exception( "Error occured while file reading." );
       
   167             }
       
   168 
       
   169             $data .= $nextBlock;
       
   170             $length -= strlen($nextBlock);
       
   171         }
       
   172         if ($length != 0) {
       
   173             require_once 'Zend/Search/Lucene/Exception.php';
       
   174             throw new Zend_Search_Lucene_Exception( "Error occured while file reading." );
       
   175         }
       
   176 
       
   177         return $data;
       
   178     }
       
   179 
       
   180 
       
   181     /**
       
   182      * Writes $length number of bytes (all, if $length===null) to the end
       
   183      * of the file.
       
   184      *
       
   185      * @param string $data
       
   186      * @param integer $length
       
   187      */
       
   188     protected function _fwrite($data, $length=null)
       
   189     {
       
   190         if ($length === null ) {
       
   191             fwrite($this->_fileHandle, $data);
       
   192         } else {
       
   193             fwrite($this->_fileHandle, $data, $length);
       
   194         }
       
   195     }
       
   196 
       
   197     /**
       
   198      * Lock file
       
   199      *
       
   200      * Lock type may be a LOCK_SH (shared lock) or a LOCK_EX (exclusive lock)
       
   201      *
       
   202      * @param integer $lockType
       
   203      * @param boolean $nonBlockingLock
       
   204      * @return boolean
       
   205      */
       
   206     public function lock($lockType, $nonBlockingLock = false)
       
   207     {
       
   208         if ($nonBlockingLock) {
       
   209             return flock($this->_fileHandle, $lockType | LOCK_NB);
       
   210         } else {
       
   211             return flock($this->_fileHandle, $lockType);
       
   212         }
       
   213     }
       
   214 
       
   215     /**
       
   216      * Unlock file
       
   217      *
       
   218      * Returns true on success
       
   219      *
       
   220      * @return boolean
       
   221      */
       
   222     public function unlock()
       
   223     {
       
   224         if ($this->_fileHandle !== null ) {
       
   225             return flock($this->_fileHandle, LOCK_UN);
       
   226         } else {
       
   227             return true;
       
   228         }
       
   229     }
       
   230 }
       
   231