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-2009 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 16541 2009-07-07 06:59:03Z bkarwin $
23 /** Zend_Search_Lucene_Storage_File */
24 require_once 'Zend/Search/Lucene/Storage/File.php';
28 * @package Zend_Search_Lucene
30 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
31 * @license http://framework.zend.com/license/new-bsd New BSD License
33 class Zend_Search_Lucene_Storage_File_Filesystem extends Zend_Search_Lucene_Storage_File
36 * Resource of the open file
40 protected $_fileHandle;
44 * Class constructor. Open the file.
46 * @param string $filename
49 public function __construct($filename, $mode='r+b')
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.');
59 $trackErrors = ini_get('track_errors');
60 ini_set('track_errors', '1');
62 $this->_fileHandle = @fopen($filename, $mode);
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);
70 ini_set('track_errors', $trackErrors);
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
83 * SEEK_CUR is the only supported offset type for compound files
85 * Upon success, returns 0; otherwise, returns -1
87 * @param integer $offset
88 * @param integer $whence
91 public function seek($offset, $whence=SEEK_SET)
93 return fseek($this->_fileHandle, $offset, $whence);
102 public function tell()
104 return ftell($this->_fileHandle);
110 * Returns true on success or false on failure.
114 public function flush()
116 return fflush($this->_fileHandle);
122 public function close()
124 if ($this->_fileHandle !== null ) {
125 @fclose($this->_fileHandle);
126 $this->_fileHandle = null;
131 * Get the size of the already opened file
135 public function size()
137 $position = ftell($this->_fileHandle);
138 fseek($this->_fileHandle, 0, SEEK_END);
139 $size = ftell($this->_fileHandle);
140 fseek($this->_fileHandle,$position);
146 * Read a $length bytes from the file and advance the file pointer.
148 * @param integer $length
151 protected function _fread($length=1)
157 if ($length < 1024) {
158 return fread($this->_fileHandle, $length);
162 while ( $length > 0 && ($nextBlock = fread($this->_fileHandle, $length)) != false ) {
164 $length -= strlen($nextBlock);
171 * Writes $length number of bytes (all, if $length===null) to the end
174 * @param string $data
175 * @param integer $length
177 protected function _fwrite($data, $length=null)
179 if ($length === null ) {
180 fwrite($this->_fileHandle, $data);
182 fwrite($this->_fileHandle, $data, $length);
189 * Lock type may be a LOCK_SH (shared lock) or a LOCK_EX (exclusive lock)
191 * @param integer $lockType
192 * @param boolean $nonBlockingLock
195 public function lock($lockType, $nonBlockingLock = false)
197 if ($nonBlockingLock) {
198 return flock($this->_fileHandle, $lockType | LOCK_NB);
200 return flock($this->_fileHandle, $lockType);
207 * Returns true on success
211 public function unlock()
213 if ($this->_fileHandle !== null ) {
214 return flock($this->_fileHandle, LOCK_UN);