File indexing completed on 2025-03-02 05:29:45

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Search_Lucene
0017  * @subpackage Storage
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /** Zend_Search_Lucene_Storage_File */
0024 // require_once 'Zend/Search/Lucene/Storage/File.php';
0025 
0026 /**
0027  * @category   Zend
0028  * @package    Zend_Search_Lucene
0029  * @subpackage Storage
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Search_Lucene_Storage_File_Filesystem extends Zend_Search_Lucene_Storage_File
0034 {
0035     /**
0036      * Resource of the open file
0037      *
0038      * @var resource
0039      */
0040     protected $_fileHandle;
0041 
0042 
0043     /**
0044      * Class constructor.  Open the file.
0045      *
0046      * @param string $filename
0047      * @param string $mode
0048      */
0049     public function __construct($filename, $mode='r+b')
0050     {
0051         global $php_errormsg;
0052 
0053         if (strpos($mode, 'w') === false  &&  !is_readable($filename)) {
0054             // opening for reading non-readable file
0055             // require_once 'Zend/Search/Lucene/Exception.php';
0056             throw new Zend_Search_Lucene_Exception('File \'' . $filename . '\' is not readable.');
0057         }
0058 
0059         $trackErrors = ini_get('track_errors');
0060         ini_set('track_errors', '1');
0061 
0062         $this->_fileHandle = @fopen($filename, $mode);
0063 
0064         if ($this->_fileHandle === false) {
0065             ini_set('track_errors', $trackErrors);
0066             // require_once 'Zend/Search/Lucene/Exception.php';
0067             throw new Zend_Search_Lucene_Exception($php_errormsg);
0068         }
0069 
0070         ini_set('track_errors', $trackErrors);
0071     }
0072 
0073     /**
0074      * Sets the file position indicator and advances the file pointer.
0075      * The new position, measured in bytes from the beginning of the file,
0076      * is obtained by adding offset to the position specified by whence,
0077      * whose values are defined as follows:
0078      * SEEK_SET - Set position equal to offset bytes.
0079      * SEEK_CUR - Set position to current location plus offset.
0080      * SEEK_END - Set position to end-of-file plus offset. (To move to
0081      * a position before the end-of-file, you need to pass a negative value
0082      * in offset.)
0083      * SEEK_CUR is the only supported offset type for compound files
0084      *
0085      * Upon success, returns 0; otherwise, returns -1
0086      *
0087      * @param integer $offset
0088      * @param integer $whence
0089      * @return integer
0090      */
0091     public function seek($offset, $whence=SEEK_SET)
0092     {
0093         return fseek($this->_fileHandle, $offset, $whence);
0094     }
0095 
0096 
0097     /**
0098      * Get file position.
0099      *
0100      * @return integer
0101      */
0102     public function tell()
0103     {
0104         return ftell($this->_fileHandle);
0105     }
0106 
0107     /**
0108      * Flush output.
0109      *
0110      * Returns true on success or false on failure.
0111      *
0112      * @return boolean
0113      */
0114     public function flush()
0115     {
0116         return fflush($this->_fileHandle);
0117     }
0118 
0119     /**
0120      * Close File object
0121      */
0122     public function close()
0123     {
0124         if ($this->_fileHandle !== null ) {
0125             @fclose($this->_fileHandle);
0126             $this->_fileHandle = null;
0127         }
0128     }
0129 
0130     /**
0131      * Get the size of the already opened file
0132      *
0133      * @return integer
0134      */
0135     public function size()
0136     {
0137         $position = ftell($this->_fileHandle);
0138         fseek($this->_fileHandle, 0, SEEK_END);
0139         $size = ftell($this->_fileHandle);
0140         fseek($this->_fileHandle,$position);
0141 
0142         return $size;
0143     }
0144 
0145     /**
0146      * Read a $length bytes from the file and advance the file pointer.
0147      *
0148      * @param integer $length
0149      * @return string
0150      */
0151     protected function _fread($length=1)
0152     {
0153         if ($length == 0) {
0154             return '';
0155         }
0156 
0157         if ($length < 1024) {
0158             return fread($this->_fileHandle, $length);
0159         }
0160 
0161         $data = '';
0162         while ($length > 0 && !feof($this->_fileHandle)) {
0163             $nextBlock = fread($this->_fileHandle, $length);
0164             if ($nextBlock === false) {
0165                 // require_once 'Zend/Search/Lucene/Exception.php';
0166                 throw new Zend_Search_Lucene_Exception( "Error occured while file reading." );
0167             }
0168 
0169             $data .= $nextBlock;
0170             $length -= strlen($nextBlock);
0171         }
0172         if ($length != 0) {
0173             // require_once 'Zend/Search/Lucene/Exception.php';
0174             throw new Zend_Search_Lucene_Exception( "Error occured while file reading." );
0175         }
0176 
0177         return $data;
0178     }
0179 
0180 
0181     /**
0182      * Writes $length number of bytes (all, if $length===null) to the end
0183      * of the file.
0184      *
0185      * @param string $data
0186      * @param integer $length
0187      */
0188     protected function _fwrite($data, $length=null)
0189     {
0190         if ($length === null ) {
0191             fwrite($this->_fileHandle, $data);
0192         } else {
0193             fwrite($this->_fileHandle, $data, $length);
0194         }
0195     }
0196 
0197     /**
0198      * Lock file
0199      *
0200      * Lock type may be a LOCK_SH (shared lock) or a LOCK_EX (exclusive lock)
0201      *
0202      * @param integer $lockType
0203      * @param boolean $nonBlockingLock
0204      * @return boolean
0205      */
0206     public function lock($lockType, $nonBlockingLock = false)
0207     {
0208         if ($nonBlockingLock) {
0209             return flock($this->_fileHandle, $lockType | LOCK_NB);
0210         } else {
0211             return flock($this->_fileHandle, $lockType);
0212         }
0213     }
0214 
0215     /**
0216      * Unlock file
0217      *
0218      * Returns true on success
0219      *
0220      * @return boolean
0221      */
0222     public function unlock()
0223     {
0224         if ($this->_fileHandle !== null ) {
0225             return flock($this->_fileHandle, LOCK_UN);
0226         } else {
0227             return true;
0228         }
0229     }
0230 }
0231