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.
16 * @package Zend_Search_Lucene
18 * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
23 /** Zend_Search_Lucene_Storage_File */
24 require_once $CFG->dirroot
.'/search/Zend/Search/Lucene/Storage/File.php';
26 /** Zend_Search_Lucene_Exception */
27 require_once $CFG->dirroot
.'/search/Zend/Search/Lucene/Exception.php';
32 * @package Zend_Search_Lucene
34 * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
35 * @license http://framework.zend.com/license/new-bsd New BSD License
37 class Zend_Search_Lucene_Storage_File_Filesystem
extends Zend_Search_Lucene_Storage_File
40 * Resource of the open file
47 * Class constructor. Open the file.
49 * @param string $filename
52 public function __construct($filename, $mode='r+b')
56 $trackErrors = ini_get('track_errors');
57 ini_set('track_errors', '1');
59 $this->_fileHandle
= @fopen
($filename, $mode);
61 if ($this->_fileHandle
=== false) {
62 ini_set('track_errors', $trackErrors);
63 throw new Zend_Search_Lucene_Exception($php_errormsg);
66 ini_set('track_errors', $trackErrors);
70 * Sets the file position indicator and advances the file pointer.
71 * The new position, measured in bytes from the beginning of the file,
72 * is obtained by adding offset to the position specified by whence,
73 * whose values are defined as follows:
74 * SEEK_SET - Set position equal to offset bytes.
75 * SEEK_CUR - Set position to current location plus offset.
76 * SEEK_END - Set position to end-of-file plus offset. (To move to
77 * a position before the end-of-file, you need to pass a negative value
79 * SEEK_CUR is the only supported offset type for compound files
81 * Upon success, returns 0; otherwise, returns -1
83 * @param integer $offset
84 * @param integer $whence
87 public function seek($offset, $whence=SEEK_SET
)
89 return fseek($this->_fileHandle
, $offset, $whence);
98 public function tell()
100 return ftell($this->_fileHandle
);
106 * Returns true on success or false on failure.
110 public function flush()
112 return fflush($this->_fileHandle
);
118 public function close()
120 if ($this->_fileHandle
!== null ) {
121 @fclose
($this->_fileHandle
);
122 $this->_fileHandle
= null;
127 * Get the size of the already opened file
131 public function size()
133 $position = ftell($this->_fileHandle
);
134 fseek($this->_fileHandle
, 0, SEEK_END
);
135 $size = ftell($this->_fileHandle
);
136 fseek($this->_fileHandle
,$position);
142 * Read a $length bytes from the file and advance the file pointer.
144 * @param integer $length
147 protected function _fread($length=1)
153 if ($length < 1024) {
154 return fread($this->_fileHandle
, $length);
158 while ( $length > 0 && ($nextBlock = fread($this->_fileHandle
, $length)) != false ) {
160 $length -= strlen($nextBlock);
167 * Writes $length number of bytes (all, if $length===null) to the end
170 * @param string $data
171 * @param integer $length
173 protected function _fwrite($data, $length=null)
175 if ($length === null ) {
176 fwrite($this->_fileHandle
, $data);
178 fwrite($this->_fileHandle
, $data, $length);
185 * Lock type may be a LOCK_SH (shared lock) or a LOCK_EX (exclusive lock)
187 * @param integer $lockType
188 * @param boolean $nonBlockinLock
191 public function lock($lockType, $nonBlockinLock = false)
193 if ($nonBlockinLock) {
194 return flock($this->_fileHandle
, $lockType | LOCK_NB
);
196 return flock($this->_fileHandle
, $lockType);
203 * Returns true on success
207 public function unlock()
209 if ($this->_fileHandle
!== null ) {
210 return flock($this->_fileHandle
, LOCK_UN
);