PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

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