-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.php
More file actions
282 lines (243 loc) · 7.68 KB
/
File.php
File metadata and controls
282 lines (243 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?PHP
require_once ('qcEvents/IOStream.php');
class qcEvents_File extends qcEvents_IOStream {
/* The filename of this stream */
private $Filename = '';
/* Modification-time to set on close */
private $modificationTime = null;
// {{{ readFileContents
/**
* Read full content of a file and pass to a given callback
*
* @param qcEvents_Base $Base Event-Base to use
* @param string $Filename Path to file
* @param callable $Callback Callback to raise when all contents were read
* @param mixed $Private (optional) Private data to pass to the callback
*
* Once completed the callback will be raised in the form of
*
* function (string $Content = null, mixed $Private = null) { }
*
* @access public
* @return qcEvents_File
**/
public static function readFileContents (qcEvents_Base $Base, $Filename, callable $Callback, $Private = null) {
// Try to create a file-stream
try {
$File = new static ($Base, $Filename, true, false, false);
} catch (Exception $E) {
return call_user_func ($Callback, null, $Private);
}
// Bind Event-Handlers
$Buffer = '';
$File->addHook ('eventReadable', function ($File) use (&$Buffer) {
// Try to read from stream
if (($Data = $File->read ()) === false)
return;
// Push to our buffer
$Buffer .= $Data;
});
$File->addHook ('eventClosed', function ($File) use ($Callback, $Private, &$Buffer) {
// Forward the callback
return call_user_func ($Callback, $Buffer, $Private);
});
return $File;
}
// }}}
// {{{ writeFileContents
/**
* Write full content of a file
*
* @param qcEvents_Base $Base Event-Base to use
* @param string $Filename Path to file
* @param string $Content Bytes to write to that file
* @param callable $Callback Callback to raise when all contents were written
* @param mixed $Private (optional) Private data to pass to the callback
*
* Once completed the callback will be raised in the form of
*
* function (bool $Status, mixed $Private = null) { }
*
* @access public
* @return qcEvents_File
**/
public static function writeFileContents (qcEvents_Base $Base, $Filename, $Content, callable $Callback, $Private = null) {
// Try to create a file-stream
try {
$File = new static ($Base, $Filename, false, true, true);
} catch (Exception $E) {
trigger_error ('Failed to create file-resource: ' . $E->getMessage ());
return call_user_func ($Callback, false, $Private);
}
// Enqueue the write
$File->write ($Content, function (qcEvents_File $File, $Status) use ($Callback, $Private) {
// Close the file when finished
$File->close (function () use ($Status, $Callback, $Private) {
// Forward the callback
call_user_func ($Callback, $Status, $Private);
});
});
return $File;
}
// }}}
// {{{ __construct
/**
* Create a new File I/O-Stream
*
* @param qcEvents_Base $Base
* @param string $Filename
* @param bool $Read (optional) Prepare for reading
* @param bool $Write (optional) Prepare for direct writing
* @param bool $Truncate (optional) Truncate the file
*
* @access friendly
* @return void
**/
function __construct (qcEvents_Base $Base, $Filename, $Read = true, $Write = false, $Truncate = false) {
// Handle stream-wrappers
$Mode = 'c+';
# ZLIB does not support select()-calls
#if (($p = strpos ($Filename, '://')) !== false) {
# $Wrapper = substr ($Filename, 0, $p);
#
# // Wrappers the are read-xor-write
# if (($Wrapper == 'compress.zlib') || ($Wrapper == 'compress.bzip2')) {
# if ($Read)
# $Mode = 'r';
# elseif ($Write)
# $Mode = 'w';
# }
#}
// Try to open the file
if (!is_resource ($fd = fopen ($Filename, $Mode)))
throw new Exception ('Could not open file');
// Store the filename
$this->Filename = $Filename;
// Forward the event-base
parent::__construct ($Base);
// Forward the stream-fd
$this->setStreamFD ($fd);
// Forward the read/write-status
$this->watchRead ($Read);
$this->watchWrite ($Write);
// Check wheter to truncate
if ($Write && $Truncate)
$this->truncate ();
}
// }}}
// {{{ getFilename
/**
* Retrive the filename of this stream
*
* @access public
* @return string
**/
public function getFilename () {
return $this->Filename;
}
// }}}
// {{{ truncate
/**
* Truncate this file to a given size or to the size of the actual position
*
* @access public
* @return bool
**/
public function truncate ($Size = null) {
// Try to access the descriptor
if (!is_resource ($fd = $this->getWriteFD ()))
return false;
// Truncate the file
return ftruncate ($fd, ($Size === null ? ftell ($fd) : $Size));
}
// }}}
// {{{ setModificationTime
/**
* Set the modification-time of this file
*
* @param int $Timestamp
*
* @access public
* @return bool
**/
public function setModificationTime ($Timestamp) {
// Store the timestamp here
$this->modificationTime = $Timestamp;
// Try to set the timestamp initially
return touch ($this->getFilename (), $Timestamp);
}
// }}}
// {{{ ___read
/**
* Read from the underlying stream
*
* @param int $Length (optional)
*
* @access protected
* @return string
**/
protected function ___read ($Length = null) {
// Retrive our descriptor
if (!is_resource ($fd = $this->getReadFD ()))
return false;
// Check wheter to use the default read-length
if ($Length === null)
$Length = $this::DEFAULT_READ_LENGTH;
// Try to read from file
$Result = fread ($fd, $Length);
return $Result;
}
// }}}
// {{{ ___write
/**
* Write to the underlying stream
*
* @param string $Data
*
* @access protected
* @return int The number of bytes that have been written
**/
protected function ___write ($Data) {
// Retrive our descriptor
if (!is_resource ($fd = $this->getWriteFD ()))
return false;
// Just write out and return
return @fwrite ($fd, $Data);
}
// }}}
// {{{ ___close
/**
* Close the stream at the handler
*
* @access protected
* @return bool
**/
protected function ___close () {
// Retrive our descriptor and close it
if ((is_resource ($fd = $this->getReadFD ()) ||
is_resource ($fd = $this->getWriteFD ())) &&
!fclose ($fd))
return false;
// Check wheter to set modification-time
if ($this->modificationTime !== null)
touch ($this->getFilename (), $this->modificationTime);
return true;
}
// }}}
// {{{ raiseRead
/**
* Callback: The Event-Loop detected a read-event
*
* @access public
* @return void
**/
public function raiseRead () {
// Fire callbacks first
parent::raiseRead ();
// Check if we reached end-of-file
if (is_resource ($fd = $this->getReadFD ()) && feof ($fd))
$this->close ();
}
// }}}
}
?>