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