Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / search / Zend / Search / Lucene / Storage / File / Filesystem.php
blob1f2097eb4f67d7eab5dacb512a1db7b9ce1d826b
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Search_Lucene
17 * @subpackage Storage
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';
30 /**
31 * @category Zend
32 * @package Zend_Search_Lucene
33 * @subpackage Storage
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
39 /**
40 * Resource of the open file
42 * @var resource
44 private $_fileHandle;
46 /**
47 * Class constructor. Open the file.
49 * @param string $filename
50 * @param string $mode
52 public function __construct($filename, $mode='r+b')
54 global $php_errormsg;
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);
69 /**
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
78 * in offset.)
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
85 * @return integer
87 public function seek($offset, $whence=SEEK_SET)
89 return fseek($this->_fileHandle, $offset, $whence);
93 /**
94 * Get file position.
96 * @return integer
98 public function tell()
100 return ftell($this->_fileHandle);
104 * Flush output.
106 * Returns true on success or false on failure.
108 * @return boolean
110 public function flush()
112 return fflush($this->_fileHandle);
116 * Close File object
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
129 * @return integer
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);
138 return $size;
142 * Read a $length bytes from the file and advance the file pointer.
144 * @param integer $length
145 * @return string
147 protected function _fread($length=1)
149 if ($length == 0) {
150 return '';
153 if ($length < 1024) {
154 return fread($this->_fileHandle, $length);
157 $data = '';
158 while ( $length > 0 && ($nextBlock = fread($this->_fileHandle, $length)) != false ) {
159 $data .= $nextBlock;
160 $length -= strlen($nextBlock);
162 return $data;
167 * Writes $length number of bytes (all, if $length===null) to the end
168 * of the file.
170 * @param string $data
171 * @param integer $length
173 protected function _fwrite($data, $length=null)
175 if ($length === null ) {
176 fwrite($this->_fileHandle, $data);
177 } else {
178 fwrite($this->_fileHandle, $data, $length);
183 * Lock file
185 * Lock type may be a LOCK_SH (shared lock) or a LOCK_EX (exclusive lock)
187 * @param integer $lockType
188 * @param boolean $nonBlockinLock
189 * @return boolean
191 public function lock($lockType, $nonBlockinLock = false)
193 if ($nonBlockinLock) {
194 return flock($this->_fileHandle, $lockType | LOCK_NB);
195 } else {
196 return flock($this->_fileHandle, $lockType);
201 * Unlock file
203 * Returns true on success
205 * @return boolean
207 public function unlock()
209 if ($this->_fileHandle !== null ) {
210 return flock($this->_fileHandle, LOCK_UN);
211 } else {
212 return true;