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

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