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.
16 * @package Zend_Search_Lucene
18 * @copyright Copyright (c) 2005-2009 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 16541 2009-07-07 06:59:03Z bkarwin $
24 /** Zend_Search_Lucene_Storage_Directory */
25 require_once 'Zend/Search/Lucene/Storage/Directory.php';
27 /** Zend_Search_Lucene_Storage_File_Filesystem */
28 require_once 'Zend/Search/Lucene/Storage/File/Filesystem.php';
32 * FileSystem implementation of Directory abstraction.
35 * @package Zend_Search_Lucene
37 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
38 * @license http://framework.zend.com/license/new-bsd New BSD License
40 class Zend_Search_Lucene_Storage_Directory_Filesystem extends Zend_Search_Lucene_Storage_Directory
43 * Filesystem path to the directory
47 protected $_dirPath = null;
50 * Cache for Zend_Search_Lucene_Storage_File_Filesystem objects
51 * Array: filename => Zend_Search_Lucene_Storage_File object
54 * @throws Zend_Search_Lucene_Exception
56 protected $_fileHandlers;
59 * Default file permissions
63 protected static $_defaultFilePermissions = 0666;
67 * Get default file permissions
71 public static function getDefaultFilePermissions()
73 return self::$_defaultFilePermissions;
77 * Set default file permissions
79 * @param integer $mode
81 public static function setDefaultFilePermissions($mode)
83 self::$_defaultFilePermissions = $mode;
88 * Utility function to recursive directory creation
91 * @param integer $mode
92 * @param boolean $recursive
96 public static function mkdirs($dir, $mode = 0777, $recursive = true)
98 if (($dir === null) || $dir === '') {
101 if (is_dir($dir) || $dir === '/') {
104 if (self::mkdirs(dirname($dir), $mode, $recursive)) {
105 return mkdir($dir, $mode);
113 * Checks if $path is a directory or tries to create it.
115 * @param string $path
116 * @throws Zend_Search_Lucene_Exception
118 public function __construct($path)
120 if (!is_dir($path)) {
121 if (file_exists($path)) {
122 require_once 'Zend/Search/Lucene/Exception.php';
123 throw new Zend_Search_Lucene_Exception('Path exists, but it\'s not a directory');
125 if (!self::mkdirs($path)) {
126 require_once 'Zend/Search/Lucene/Exception.php';
127 throw new Zend_Search_Lucene_Exception("Can't create directory '$path'.");
131 $this->_dirPath = $path;
132 $this->_fileHandlers = array();
141 public function close()
143 foreach ($this->_fileHandlers as $fileObject) {
144 $fileObject->close();
147 $this->_fileHandlers = array();
152 * Returns an array of strings, one for each file in the directory.
156 public function fileList()
160 $dirContent = opendir( $this->_dirPath );
161 while (($file = readdir($dirContent)) !== false) {
162 if (($file == '..')||($file == '.')) continue;
164 if( !is_dir($this->_dirPath . '/' . $file) ) {
168 closedir($dirContent);
174 * Creates a new, empty file in the directory with the given $filename.
176 * @param string $filename
177 * @return Zend_Search_Lucene_Storage_File
178 * @throws Zend_Search_Lucene_Exception
180 public function createFile($filename)
182 if (isset($this->_fileHandlers[$filename])) {
183 $this->_fileHandlers[$filename]->close();
185 unset($this->_fileHandlers[$filename]);
186 $this->_fileHandlers[$filename] = new Zend_Search_Lucene_Storage_File_Filesystem($this->_dirPath . '/' . $filename, 'w+b');
188 // Set file permissions, but don't care about any possible failures, since file may be already
189 // created by anther user which has to care about right permissions
190 @chmod($this->_dirPath . '/' . $filename, self::$_defaultFilePermissions);
192 return $this->_fileHandlers[$filename];
197 * Removes an existing $filename in the directory.
199 * @param string $filename
201 * @throws Zend_Search_Lucene_Exception
203 public function deleteFile($filename)
205 if (isset($this->_fileHandlers[$filename])) {
206 $this->_fileHandlers[$filename]->close();
208 unset($this->_fileHandlers[$filename]);
210 global $php_errormsg;
211 $trackErrors = ini_get('track_errors'); ini_set('track_errors', '1');
212 if (!@unlink($this->_dirPath . '/' . $filename)) {
213 ini_set('track_errors', $trackErrors);
214 require_once 'Zend/Search/Lucene/Exception.php';
215 throw new Zend_Search_Lucene_Exception('Can\'t delete file: ' . $php_errormsg);
217 ini_set('track_errors', $trackErrors);
221 * Purge file if it's cached by directory object
223 * Method is used to prevent 'too many open files' error
225 * @param string $filename
228 public function purgeFile($filename)
230 if (isset($this->_fileHandlers[$filename])) {
231 $this->_fileHandlers[$filename]->close();
233 unset($this->_fileHandlers[$filename]);
238 * Returns true if a file with the given $filename exists.
240 * @param string $filename
243 public function fileExists($filename)
245 return isset($this->_fileHandlers[$filename]) ||
246 file_exists($this->_dirPath . '/' . $filename);
251 * Returns the length of a $filename in the directory.
253 * @param string $filename
256 public function fileLength($filename)
258 if (isset( $this->_fileHandlers[$filename] )) {
259 return $this->_fileHandlers[$filename]->size();
261 return filesize($this->_dirPath .'/'. $filename);
266 * Returns the UNIX timestamp $filename was last modified.
268 * @param string $filename
271 public function fileModified($filename)
273 return filemtime($this->_dirPath .'/'. $filename);
278 * Renames an existing file in the directory.
280 * @param string $from
283 * @throws Zend_Search_Lucene_Exception
285 public function renameFile($from, $to)
287 global $php_errormsg;
289 if (isset($this->_fileHandlers[$from])) {
290 $this->_fileHandlers[$from]->close();
292 unset($this->_fileHandlers[$from]);
294 if (isset($this->_fileHandlers[$to])) {
295 $this->_fileHandlers[$to]->close();
297 unset($this->_fileHandlers[$to]);
299 if (file_exists($this->_dirPath . '/' . $to)) {
300 if (!unlink($this->_dirPath . '/' . $to)) {
301 require_once 'Zend/Search/Lucene/Exception.php';
302 throw new Zend_Search_Lucene_Exception('Delete operation failed');
306 $trackErrors = ini_get('track_errors');
307 ini_set('track_errors', '1');
309 $success = @rename($this->_dirPath . '/' . $from, $this->_dirPath . '/' . $to);
311 ini_set('track_errors', $trackErrors);
312 require_once 'Zend/Search/Lucene/Exception.php';
313 throw new Zend_Search_Lucene_Exception($php_errormsg);
316 ini_set('track_errors', $trackErrors);
323 * Sets the modified time of $filename to now.
325 * @param string $filename
328 public function touchFile($filename)
330 return touch($this->_dirPath .'/'. $filename);
335 * Returns a Zend_Search_Lucene_Storage_File object for a given $filename in the directory.
337 * If $shareHandler option is true, then file handler can be shared between File Object
338 * requests. It speed-ups performance, but makes problems with file position.
339 * Shared handler are good for short atomic requests.
340 * Non-shared handlers are useful for stream file reading (especial for compound files).
342 * @param string $filename
343 * @param boolean $shareHandler
344 * @return Zend_Search_Lucene_Storage_File
346 public function getFileObject($filename, $shareHandler = true)
348 $fullFilename = $this->_dirPath . '/' . $filename;
350 if (!$shareHandler) {
351 return new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
354 if (isset( $this->_fileHandlers[$filename] )) {
355 $this->_fileHandlers[$filename]->seek(0);
356 return $this->_fileHandlers[$filename];
359 $this->_fileHandlers[$filename] = new Zend_Search_Lucene_Storage_File_Filesystem($fullFilename);
360 return $this->_fileHandlers[$filename];