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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
<?php
/*
* $Id: fe077b4174763861e773f5e5e55bbfc5030cac4d $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
include_once 'phing/system/lang/Character.php';
include_once 'phing/util/StringHelper.php';
include_once 'phing/system/io/BufferedReader.php';
include_once 'phing/system/io/BufferedWriter.php';
include_once 'phing/filters/util/ChainReaderHelper.php';
include_once 'phing/system/io/PhingFile.php';
/**
* File utility class.
* - handles os independent stuff etc
* - mapper stuff
* - filter stuff
*
* @package phing.util
* @version $Id$
*/
class FileUtils {
/**
* Returns a new Reader with filterchains applied. If filterchains are empty,
* simply returns passed reader.
*
* @param Reader $in Reader to modify (if appropriate).
* @param array &$filterChains filter chains to apply.
* @param Project $project
* @return Reader Assembled Reader (w/ filter chains).
*/
public static function getChainedReader(Reader $in, &$filterChains, Project $project) {
if (!empty($filterChains)) {
$crh = new ChainReaderHelper();
$crh->setBufferSize(65536); // 64k buffer, but isn't being used (yet?)
$crh->setPrimaryReader($in);
$crh->setFilterChains($filterChains);
$crh->setProject($project);
$rdr = $crh->getAssembledReader();
return $rdr;
} else {
return $in;
}
}
/**
* Copies a file using filter chains.
*
* @param PhingFile $sourceFile
* @param PhingFile $destFile
* @param boolean $overwrite
* @param boolean $preserveLastModified
* @param array $filterChains
* @param Project $project
* @param integer $mode
* @return void
*/
function copyFile(PhingFile $sourceFile, PhingFile $destFile, $overwrite = false, $preserveLastModified = true, &$filterChains = null, Project $project, $mode = 0755) {
if ($overwrite || !$destFile->exists() || $destFile->lastModified() < $sourceFile->lastModified()) {
if ($destFile->exists() && $destFile->isFile()) {
$destFile->delete();
}
// ensure that parent dir of dest file exists!
$parent = $destFile->getParentFile();
if ($parent !== null && !$parent->exists()) {
$parent->mkdirs($mode);
}
if ((is_array($filterChains)) && (!empty($filterChains))) {
$in = self::getChainedReader(new BufferedReader(new FileReader($sourceFile)), $filterChains, $project);
$out = new BufferedWriter(new FileWriter($destFile));
// New read() methods returns a big buffer.
while(-1 !== ($buffer = $in->read())) { // -1 indicates EOF
$out->write($buffer);
}
if ( $in !== null )
$in->close();
if ( $out !== null )
$out->close();
$destFile->setMode($sourceFile->getMode());
} else {
// simple copy (no filtering)
$sourceFile->copyTo($destFile);
}
if ($preserveLastModified) {
$destFile->setLastModified($sourceFile->lastModified());
}
}
}
/**
* Interpret the filename as a file relative to the given file -
* unless the filename already represents an absolute filename.
*
* @param $file the "reference" file for relative paths. This
* instance must be an absolute file and must not contain
* ./ or ../ sequences (same for \ instead of /).
* @param $filename a file name
*
* @return PhingFile A PhingFile object pointing to an absolute file that doesn't contain ./ or ../ sequences
* and uses the correct separator for the current platform.
*/
function resolveFile($file, $filename) {
// remove this and use the static class constant File::seperator
// as soon as ZE2 is ready
$fs = FileSystem::getFileSystem();
$filename = str_replace('/', $fs->getSeparator(), str_replace('\\', $fs->getSeparator(), $filename));
// deal with absolute files
if (StringHelper::startsWith($fs->getSeparator(), $filename) ||
(strlen($filename) >= 2 && Character::isLetter($filename{0}) && $filename{1} === ':')) {
return new PhingFile($this->normalize($filename));
}
if (strlen($filename) >= 2 && Character::isLetter($filename{0}) && $filename{1} === ':') {
return new PhingFile($this->normalize($filename));
}
$helpFile = new PhingFile($file->getAbsolutePath());
$tok = strtok($filename, $fs->getSeparator());
while ($tok !== false) {
$part = $tok;
if ($part === '..') {
$parentFile = $helpFile->getParent();
if ($parentFile === null) {
$msg = "The file or path you specified ($filename) is invalid relative to ".$file->getPath();
throw new IOException($msg);
}
$helpFile = new PhingFile($parentFile);
} else if ($part === '.') {
// Do nothing here
} else {
$helpFile = new PhingFile($helpFile, $part);
}
$tok = strtok($fs->getSeparator());
}
return new PhingFile($helpFile->getAbsolutePath());
}
/**
* Normalize the given absolute path.
*
* This includes:
* - Uppercase the drive letter if there is one.
* - Remove redundant slashes after the drive spec.
* - resolve all ./, .\, ../ and ..\ sequences.
* - DOS style paths that start with a drive letter will have
* \ as the separator.
* @param string $path Path to normalize.
* @return string
*/
function normalize($path) {
$path = (string) $path;
$orig = $path;
$path = str_replace('/', DIRECTORY_SEPARATOR, str_replace('\\', DIRECTORY_SEPARATOR, $path));
// make sure we are dealing with an absolute path
if (!StringHelper::startsWith(DIRECTORY_SEPARATOR, $path)
&& !(strlen($path) >= 2 && Character::isLetter($path{0}) && $path{1} === ':')) {
throw new IOException("$path is not an absolute path");
}
$dosWithDrive = false;
$root = null;
// Eliminate consecutive slashes after the drive spec
if (strlen($path) >= 2 && Character::isLetter($path{0}) && $path{1} === ':') {
$dosWithDrive = true;
$ca = str_replace('/', '\\', $path);
$ca = StringHelper::toCharArray($ca);
$path = strtoupper($ca[0]).':';
for ($i=2, $_i=count($ca); $i < $_i; $i++) {
if (($ca[$i] !== '\\') ||
($ca[$i] === '\\' && $ca[$i - 1] !== '\\')
) {
$path .= $ca[$i];
}
}
$path = str_replace('\\', DIRECTORY_SEPARATOR, $path);
if (strlen($path) == 2) {
$root = $path;
$path = "";
} else {
$root = substr($path, 0, 3);
$path = substr($path, 3);
}
} else {
if (strlen($path) == 1) {
$root = DIRECTORY_SEPARATOR;
$path = "";
} else if ($path{1} == DIRECTORY_SEPARATOR) {
// UNC drive
$root = DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR;
$path = substr($path, 2);
}
else {
$root = DIRECTORY_SEPARATOR;
$path = substr($path, 1);
}
}
$s = array();
array_push($s, $root);
$tok = strtok($path, DIRECTORY_SEPARATOR);
while ($tok !== false) {
$thisToken = $tok;
if ("." === $thisToken) {
$tok = strtok(DIRECTORY_SEPARATOR);
continue;
} elseif (".." === $thisToken) {
if (count($s) < 2) {
// using '..' in path that is too short
throw new IOException("Cannot resolve path: $orig");
} else {
array_pop($s);
}
} else { // plain component
array_push($s, $thisToken);
}
$tok = strtok(DIRECTORY_SEPARATOR);
}
$sb = "";
for ($i=0,$_i=count($s); $i < $_i; $i++) {
if ($i > 1) {
// not before the filesystem root and not after it, since root
// already contains one
$sb .= DIRECTORY_SEPARATOR;
}
$sb .= (string) $s[$i];
}
$path = (string) $sb;
if ($dosWithDrive === true) {
$path = str_replace('/', '\\', $path);
}
return $path;
}
/**
* @return boolean Whether contents of two files is the same.
*/
public function contentEquals(PhingFile $file1, PhingFile $file2) {
if (!($file1->exists() || $file2->exists())) {
return false;
}
if (!($file1->canRead() || $file2->canRead())) {
return false;
}
$c1 = file_get_contents($file1->getAbsolutePath());
$c2 = file_get_contents($file2->getAbsolutePath());
return trim($c1) == trim($c2);
}
}
|