PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Search/Lucene/Storage/File/Filesystem.php

https://bitbucket.org/Ebozavrik/test-application
PHP | 234 lines | 98 code | 26 blank | 110 comment | 20 complexity | fdf1c349b76118f87f3cad2ab1e6ddfb MD5 | raw file
  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-2012 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 24593 2012-01-05 20:35:02Z matthew $
  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-2012 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. *
  80. * @return integer
  81. */
  82. public function seek ($offset, $whence = SEEK_SET)
  83. {
  84. return fseek($this->_fileHandle, $offset, $whence);
  85. }
  86. /**
  87. * Get file position.
  88. *
  89. * @return integer
  90. */
  91. public function tell ()
  92. {
  93. return ftell($this->_fileHandle);
  94. }
  95. /**
  96. * Flush output.
  97. *
  98. * Returns true on success or false on failure.
  99. *
  100. * @return boolean
  101. */
  102. public function flush ()
  103. {
  104. return fflush($this->_fileHandle);
  105. }
  106. /**
  107. * Close File object
  108. */
  109. public function close ()
  110. {
  111. if ($this->_fileHandle !== null) {
  112. @fclose($this->_fileHandle);
  113. $this->_fileHandle = null;
  114. }
  115. }
  116. /**
  117. * Get the size of the already opened file
  118. *
  119. * @return integer
  120. */
  121. public function size ()
  122. {
  123. $position = ftell($this->_fileHandle);
  124. fseek($this->_fileHandle, 0, SEEK_END);
  125. $size = ftell($this->_fileHandle);
  126. fseek($this->_fileHandle, $position);
  127. return $size;
  128. }
  129. /**
  130. * Read a $length bytes from the file and advance the file pointer.
  131. *
  132. * @param integer $length
  133. *
  134. * @return string
  135. */
  136. protected function _fread ($length = 1)
  137. {
  138. if ($length == 0) {
  139. return '';
  140. }
  141. if ($length < 1024) {
  142. return fread($this->_fileHandle, $length);
  143. }
  144. $data = '';
  145. while ($length > 0 && !feof($this->_fileHandle)) {
  146. $nextBlock = fread($this->_fileHandle, $length);
  147. if ($nextBlock === false) {
  148. require_once 'Zend/Search/Lucene/Exception.php';
  149. throw new Zend_Search_Lucene_Exception( "Error occured while file reading." );
  150. }
  151. $data .= $nextBlock;
  152. $length -= strlen($nextBlock);
  153. }
  154. if ($length != 0) {
  155. require_once 'Zend/Search/Lucene/Exception.php';
  156. throw new Zend_Search_Lucene_Exception( "Error occured while file reading." );
  157. }
  158. return $data;
  159. }
  160. /**
  161. * Writes $length number of bytes (all, if $length===null) to the end
  162. * of the file.
  163. *
  164. * @param string $data
  165. * @param integer $length
  166. */
  167. protected function _fwrite ($data, $length = null)
  168. {
  169. if ($length === null) {
  170. fwrite($this->_fileHandle, $data);
  171. } else {
  172. fwrite($this->_fileHandle, $data, $length);
  173. }
  174. }
  175. /**
  176. * Lock file
  177. *
  178. * Lock type may be a LOCK_SH (shared lock) or a LOCK_EX (exclusive lock)
  179. *
  180. * @param integer $lockType
  181. * @param boolean $nonBlockingLock
  182. *
  183. * @return boolean
  184. */
  185. public function lock ($lockType, $nonBlockingLock = false)
  186. {
  187. if ($nonBlockingLock) {
  188. return flock($this->_fileHandle, $lockType | LOCK_NB);
  189. } else {
  190. return flock($this->_fileHandle, $lockType);
  191. }
  192. }
  193. /**
  194. * Unlock file
  195. *
  196. * Returns true on success
  197. *
  198. * @return boolean
  199. */
  200. public function unlock ()
  201. {
  202. if ($this->_fileHandle !== null) {
  203. return flock($this->_fileHandle, LOCK_UN);
  204. } else {
  205. return true;
  206. }
  207. }
  208. }