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

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 237 lines · 100 code · 27 blank · 110 comment · 20 complexity · 35822493629f55e200374c9ecd257e56 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: Filesystem.php 23395 2010-11-19 15:30:47Z alexander $
  21. */
  22. /**
  23. * @namespace
  24. */
  25. namespace Zend\Search\Lucene\Storage\File;
  26. use Zend\Search\Lucene;
  27. /** Zend_Search_Lucene_Storage_File */
  28. require_once 'Zend/Search/Lucene/Storage/File.php';
  29. /**
  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 File
  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. require_once 'Zend/Search/Lucene/Exception.php';
  56. throw new Lucene\Exception('File \'' . $filename . '\' is not readable.');
  57. }
  58. $trackErrors = ini_get('track_errors');
  59. ini_set('track_errors', '1');
  60. $this->_fileHandle = @fopen($filename, $mode);
  61. if ($this->_fileHandle === false) {
  62. ini_set('track_errors', $trackErrors);
  63. require_once 'Zend/Search/Lucene/Exception.php';
  64. throw new Lucene\Exception($php_errormsg);
  65. }
  66. ini_set('track_errors', $trackErrors);
  67. }
  68. /**
  69. * Sets the file position indicator and advances the file pointer.
  70. * The new position, measured in bytes from the beginning of the file,
  71. * is obtained by adding offset to the position specified by whence,
  72. * whose values are defined as follows:
  73. * SEEK_SET - Set position equal to offset bytes.
  74. * SEEK_CUR - Set position to current location plus offset.
  75. * SEEK_END - Set position to end-of-file plus offset. (To move to
  76. * a position before the end-of-file, you need to pass a negative value
  77. * in offset.)
  78. * SEEK_CUR is the only supported offset type for compound files
  79. *
  80. * Upon success, returns 0; otherwise, returns -1
  81. *
  82. * @param integer $offset
  83. * @param integer $whence
  84. * @return integer
  85. */
  86. public function seek($offset, $whence=\SEEK\SET)
  87. {
  88. return fseek($this->_fileHandle, $offset, $whence);
  89. }
  90. /**
  91. * Get file position.
  92. *
  93. * @return integer
  94. */
  95. public function tell()
  96. {
  97. return ftell($this->_fileHandle);
  98. }
  99. /**
  100. * Flush output.
  101. *
  102. * Returns true on success or false on failure.
  103. *
  104. * @return boolean
  105. */
  106. public function flush()
  107. {
  108. return fflush($this->_fileHandle);
  109. }
  110. /**
  111. * Close File object
  112. */
  113. public function close()
  114. {
  115. if ($this->_fileHandle !== null ) {
  116. @fclose($this->_fileHandle);
  117. $this->_fileHandle = null;
  118. }
  119. }
  120. /**
  121. * Get the size of the already opened file
  122. *
  123. * @return integer
  124. */
  125. public function size()
  126. {
  127. $position = ftell($this->_fileHandle);
  128. fseek($this->_fileHandle, 0, SEEK_END);
  129. $size = ftell($this->_fileHandle);
  130. fseek($this->_fileHandle,$position);
  131. return $size;
  132. }
  133. /**
  134. * Read a $length bytes from the file and advance the file pointer.
  135. *
  136. * @param integer $length
  137. * @return string
  138. */
  139. protected function _fread($length=1)
  140. {
  141. if ($length == 0) {
  142. return '';
  143. }
  144. if ($length < 1024) {
  145. return fread($this->_fileHandle, $length);
  146. }
  147. $data = '';
  148. while ($length > 0 && !feof($this->_fileHandle)) {
  149. $nextBlock = fread($this->_fileHandle, $length);
  150. if ($nextBlock === false) {
  151. require_once 'Zend/Search/Lucene/Exception.php';
  152. throw new Lucene\Exception( "Error occured while file reading." );
  153. }
  154. $data .= $nextBlock;
  155. $length -= strlen($nextBlock);
  156. }
  157. if ($length != 0) {
  158. require_once 'Zend/Search/Lucene/Exception.php';
  159. throw new Lucene\Exception( "Error occured while file reading." );
  160. }
  161. return $data;
  162. }
  163. /**
  164. * Writes $length number of bytes (all, if $length===null) to the end
  165. * of the file.
  166. *
  167. * @param string $data
  168. * @param integer $length
  169. */
  170. protected function _fwrite($data, $length=null)
  171. {
  172. if ($length === null ) {
  173. fwrite($this->_fileHandle, $data);
  174. } else {
  175. fwrite($this->_fileHandle, $data, $length);
  176. }
  177. }
  178. /**
  179. * Lock file
  180. *
  181. * Lock type may be a LOCK_SH (shared lock) or a LOCK_EX (exclusive lock)
  182. *
  183. * @param integer $lockType
  184. * @param boolean $nonBlockingLock
  185. * @return boolean
  186. */
  187. public function lock($lockType, $nonBlockingLock = false)
  188. {
  189. if ($nonBlockingLock) {
  190. return flock($this->_fileHandle, $lockType | LOCK_NB);
  191. } else {
  192. return flock($this->_fileHandle, $lockType);
  193. }
  194. }
  195. /**
  196. * Unlock file
  197. *
  198. * Returns true on success
  199. *
  200. * @return boolean
  201. */
  202. public function unlock()
  203. {
  204. if ($this->_fileHandle !== null ) {
  205. return flock($this->_fileHandle, LOCK_UN);
  206. } else {
  207. return true;
  208. }
  209. }
  210. }