Filesystem.php 6.1 KB

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