PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/standard/tags/release-1.5.2/library/Zend/Search/Lucene/Storage/Directory/Filesystem.php

https://github.com/bhaumik25/zend-framework
PHP | 363 lines | 209 code | 38 blank | 116 comment | 18 complexity | 6f383f18ff513ec0c6619f9d14665aec 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_Directory */
  22. require_once 'Zend/Search/Lucene/Storage/Directory.php';
  23. /** Zend_Search_Lucene_Storage_File_Filesystem */
  24. require_once 'Zend/Search/Lucene/Storage/File/Filesystem.php';
  25. /**
  26. * FileSystem implementation of Directory abstraction.
  27. *
  28. * @category Zend
  29. * @package Zend_Search_Lucene
  30. * @subpackage Storage
  31. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Search_Lucene_Storage_Directory_Filesystem extends Zend_Search_Lucene_Storage_Directory
  35. {
  36. /**
  37. * Filesystem path to the directory
  38. *
  39. * @var string
  40. */
  41. protected $_dirPath = null;
  42. /**
  43. * Cache for Zend_Search_Lucene_Storage_File_Filesystem objects
  44. * Array: filename => Zend_Search_Lucene_Storage_File object
  45. *
  46. * @var array
  47. * @throws Zend_Search_Lucene_Exception
  48. */
  49. protected $_fileHandlers;
  50. /**
  51. * Default file permissions
  52. *
  53. * @var integer
  54. */
  55. protected static $_defaultFilePermissions = 0666;
  56. /**
  57. * Get default file permissions
  58. *
  59. * @return integer
  60. */
  61. public static function getDefaultFilePermissions()
  62. {
  63. return self::$_defaultFilePermissions;
  64. }
  65. /**
  66. * Set default file permissions
  67. *
  68. * @param integer $mode
  69. */
  70. public static function setDefaultFilePermissions($mode)
  71. {
  72. self::$_defaultFilePermissions = $mode;
  73. }
  74. /**
  75. * Utility function to recursive directory creation
  76. *
  77. * @param string $dir
  78. * @param integer $mode
  79. * @param boolean $recursive
  80. * @return boolean
  81. */
  82. public static function mkdirs($dir, $mode = 0777, $recursive = true)
  83. {
  84. if (is_null($dir) || $dir === '') {
  85. return false;
  86. }
  87. if (is_dir($dir) || $dir === '/') {
  88. return true;
  89. }
  90. if (self::mkdirs(dirname($dir), $mode, $recursive)) {
  91. return mkdir($dir, $mode);
  92. }
  93. return false;
  94. }
  95. /**
  96. * Object constructor
  97. * Checks if $path is a directory or tries to create it.
  98. *
  99. * @param string $path
  100. * @throws Zend_Search_Lucene_Exception
  101. */
  102. public function __construct($path)
  103. {
  104. if (!is_dir($path)) {
  105. if (file_exists($path)) {
  106. throw new Zend_Search_Lucene_Exception('Path exists, but it\'s not a directory');
  107. } else {
  108. if (!self::mkdirs($path)) {
  109. throw new Zend_Search_Lucene_Exception("Can't create directory '$path'.");
  110. }
  111. }
  112. }
  113. $this->_dirPath = $path;
  114. $this->_fileHandlers = array();
  115. }
  116. /**
  117. * Closes the store.
  118. *
  119. * @return void
  120. */
  121. public function close()
  122. {
  123. foreach ($this->_fileHandlers as $fileObject) {
  124. $fileObject->close();
  125. }
  126. $this->_fileHandlers = array();
  127. }
  128. /**
  129. * Returns an array of strings, one for each file in the directory.
  130. *
  131. * @return array
  132. */
  133. public function fileList()
  134. {
  135. $result = array();
  136. $dirContent = opendir( $this->_dirPath );
  137. while (($file = readdir($dirContent)) !== false) {
  138. if (($file == '..')||($file == '.')) continue;
  139. if( !is_dir($this->_dirPath . '/' . $file) ) {
  140. $result[] = $file;
  141. }
  142. }
  143. closedir($dirContent);
  144. return $result;
  145. }
  146. /**
  147. * Creates a new, empty file in the directory with the given $filename.
  148. *
  149. * @param string $filename
  150. * @return Zend_Search_Lucene_Storage_File
  151. * @throws Zend_Search_Lucene_Exception
  152. */
  153. public function createFile($filename)
  154. {
  155. if (isset($this->_fileHandlers[$filename])) {
  156. $this->_fileHandlers[$filename]->close();
  157. }
  158. unset($this->_fileHandlers[$filename]);
  159. $this->_fileHandlers[$filename] = new Zend_Search_Lucene_Storage_File_Filesystem($this->_dirPath . '/' . $filename, 'w+b');
  160. global $php_errormsg;
  161. $trackErrors = ini_get('track_errors'); ini_set('track_errors', '1');
  162. if (!@chmod($this->_dirPath . '/' . $filename, self::$_defaultFilePermissions)) {
  163. ini_set('track_errors', $trackErrors);
  164. throw new Zend_Search_Lucene_Exception($php_errormsg);
  165. }
  166. ini_set('track_errors', $trackErrors);
  167. return $this->_fileHandlers[$filename];
  168. }
  169. /**
  170. * Removes an existing $filename in the directory.
  171. *
  172. * @param string $filename
  173. * @return void
  174. * @throws Zend_Search_Lucene_Exception
  175. */
  176. public function deleteFile($filename)
  177. {
  178. if (isset($this->_fileHandlers[$filename])) {
  179. $this->_fileHandlers[$filename]->close();
  180. }
  181. unset($this->_fileHandlers[$filename]);
  182. global $php_errormsg;
  183. $trackErrors = ini_get('track_errors'); ini_set('track_errors', '1');
  184. if (!@unlink($this->_dirPath . '/' . $filename)) {
  185. ini_set('track_errors', $trackErrors);
  186. throw $e;
  187. throw new Zend_Search_Lucene_Exception('Can\'t delete file: ' . $php_errormsg);
  188. }
  189. ini_set('track_errors', $trackErrors);
  190. }
  191. /**
  192. * Purge file if it's cached by directory object
  193. *
  194. * Method is used to prevent 'too many open files' error
  195. *
  196. * @param string $filename
  197. * @return void
  198. */
  199. public function purgeFile($filename)
  200. {
  201. if (isset($this->_fileHandlers[$filename])) {
  202. $this->_fileHandlers[$filename]->close();
  203. }
  204. unset($this->_fileHandlers[$filename]);
  205. }
  206. /**
  207. * Returns true if a file with the given $filename exists.
  208. *
  209. * @param string $filename
  210. * @return boolean
  211. */
  212. public function fileExists($filename)
  213. {
  214. return isset($this->_fileHandlers[$filename]) ||
  215. file_exists($this->_dirPath . '/' . $filename);
  216. }
  217. /**
  218. * Returns the length of a $filename in the directory.
  219. *
  220. * @param string $filename
  221. * @return integer
  222. */
  223. public function fileLength($filename)
  224. {
  225. if (isset( $this->_fileHandlers[$filename] )) {
  226. return $this->_fileHandlers[$filename]->size();
  227. }
  228. return filesize($this->_dirPath .'/'. $filename);
  229. }
  230. /**
  231. * Returns the UNIX timestamp $filename was last modified.
  232. *
  233. * @param string $filename
  234. * @return integer
  235. */
  236. public function fileModified($filename)
  237. {
  238. return filemtime($this->_dirPath .'/'. $filename);
  239. }
  240. /**
  241. * Renames an existing file in the directory.
  242. *
  243. * @param string $from
  244. * @param string $to
  245. * @return void
  246. * @throws Zend_Search_Lucene_Exception
  247. */
  248. public function renameFile($from, $to)
  249. {
  250. global $php_errormsg;
  251. if (isset($this->_fileHandlers[$from])) {
  252. $this->_fileHandlers[$from]->close();
  253. }
  254. unset($this->_fileHandlers[$from]);
  255. if (isset($this->_fileHandlers[$to])) {
  256. $this->_fileHandlers[$to]->close();
  257. }
  258. unset($this->_fileHandlers[$to]);
  259. if (file_exists($this->_dirPath . '/' . $to)) {
  260. if (!unlink($this->_dirPath . '/' . $to)) {
  261. throw new Zend_Search_Lucene_Exception('Delete operation failed');
  262. }
  263. }
  264. $trackErrors = ini_get('track_errors');
  265. ini_set('track_errors', '1');
  266. $success = @rename($this->_dirPath . '/' . $from, $this->_dirPath . '/' . $to);
  267. if (!$success) {
  268. ini_set('track_errors', $trackErrors);
  269. throw new Zend_Search_Lucene_Exception($php_errormsg);
  270. }
  271. ini_set('track_errors', $trackErrors);
  272. return $success;
  273. }
  274. /**
  275. * Sets the modified time of $filename to now.
  276. *
  277. * @param string $filename
  278. * @return void
  279. */
  280. public function touchFile($filename)
  281. {
  282. return touch($this->_dirPath .'/'. $filename);
  283. }
  284. /**
  285. * Returns a Zend_Search_Lucene_Storage_File object for a given $filename in the directory.
  286. *
  287. * If $shareHandler option is true, then file handler can be shared between File Object
  288. * requests. It speed-ups performance, but makes problems with file position.
  289. * Shared handler are good for short atomic requests.
  290. * Non-shared handlers are useful for stream file reading (especial for compound files).
  291. *
  292. * @param string $filename
  293. * @param boolean $shareHandler
  294. * @return Zend_Search_Lucene_Storage_File
  295. */
  296. public function getFileObject($filename, $shareHandler = true)
  297. {
  298. $fullFilename = $this->_dirPath . '/' . $filename;
  299. if (!$shareHandler) {
  300. return new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
  301. }
  302. if (isset( $this->_fileHandlers[$filename] )) {
  303. $this->_fileHandlers[$filename]->seek(0);
  304. return $this->_fileHandlers[$filename];
  305. }
  306. $this->_fileHandlers[$filename] = new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
  307. return $this->_fileHandlers[$filename];
  308. }
  309. }