Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Filesystem;
|
Chris@0
|
13
|
Chris@4
|
14 use Symfony\Component\Filesystem\Exception\FileNotFoundException;
|
Chris@0
|
15 use Symfony\Component\Filesystem\Exception\IOException;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Provides basic utility to manipulate the file system.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
21 */
|
Chris@0
|
22 class Filesystem
|
Chris@0
|
23 {
|
Chris@0
|
24 private static $lastError;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * Copies a file.
|
Chris@0
|
28 *
|
Chris@0
|
29 * If the target file is older than the origin file, it's always overwritten.
|
Chris@0
|
30 * If the target file is newer, it is overwritten only when the
|
Chris@0
|
31 * $overwriteNewerFiles option is set to true.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @param string $originFile The original filename
|
Chris@0
|
34 * @param string $targetFile The target filename
|
Chris@0
|
35 * @param bool $overwriteNewerFiles If true, target files newer than origin files are overwritten
|
Chris@0
|
36 *
|
Chris@0
|
37 * @throws FileNotFoundException When originFile doesn't exist
|
Chris@0
|
38 * @throws IOException When copy fails
|
Chris@0
|
39 */
|
Chris@0
|
40 public function copy($originFile, $targetFile, $overwriteNewerFiles = false)
|
Chris@0
|
41 {
|
Chris@0
|
42 $originIsLocal = stream_is_local($originFile) || 0 === stripos($originFile, 'file://');
|
Chris@0
|
43 if ($originIsLocal && !is_file($originFile)) {
|
Chris@0
|
44 throw new FileNotFoundException(sprintf('Failed to copy "%s" because file does not exist.', $originFile), 0, null, $originFile);
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@4
|
47 $this->mkdir(\dirname($targetFile));
|
Chris@0
|
48
|
Chris@0
|
49 $doCopy = true;
|
Chris@0
|
50 if (!$overwriteNewerFiles && null === parse_url($originFile, PHP_URL_HOST) && is_file($targetFile)) {
|
Chris@0
|
51 $doCopy = filemtime($originFile) > filemtime($targetFile);
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 if ($doCopy) {
|
Chris@0
|
55 // https://bugs.php.net/bug.php?id=64634
|
Chris@0
|
56 if (false === $source = @fopen($originFile, 'r')) {
|
Chris@0
|
57 throw new IOException(sprintf('Failed to copy "%s" to "%s" because source file could not be opened for reading.', $originFile, $targetFile), 0, null, $originFile);
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 // Stream context created to allow files overwrite when using FTP stream wrapper - disabled by default
|
Chris@4
|
61 if (false === $target = @fopen($targetFile, 'w', null, stream_context_create(['ftp' => ['overwrite' => true]]))) {
|
Chris@0
|
62 throw new IOException(sprintf('Failed to copy "%s" to "%s" because target file could not be opened for writing.', $originFile, $targetFile), 0, null, $originFile);
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 $bytesCopied = stream_copy_to_stream($source, $target);
|
Chris@0
|
66 fclose($source);
|
Chris@0
|
67 fclose($target);
|
Chris@0
|
68 unset($source, $target);
|
Chris@0
|
69
|
Chris@0
|
70 if (!is_file($targetFile)) {
|
Chris@0
|
71 throw new IOException(sprintf('Failed to copy "%s" to "%s".', $originFile, $targetFile), 0, null, $originFile);
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 if ($originIsLocal) {
|
Chris@0
|
75 // Like `cp`, preserve executable permission bits
|
Chris@0
|
76 @chmod($targetFile, fileperms($targetFile) | (fileperms($originFile) & 0111));
|
Chris@0
|
77
|
Chris@0
|
78 if ($bytesCopied !== $bytesOrigin = filesize($originFile)) {
|
Chris@0
|
79 throw new IOException(sprintf('Failed to copy the whole content of "%s" to "%s" (%g of %g bytes copied).', $originFile, $targetFile, $bytesCopied, $bytesOrigin), 0, null, $originFile);
|
Chris@0
|
80 }
|
Chris@0
|
81 }
|
Chris@0
|
82 }
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * Creates a directory recursively.
|
Chris@0
|
87 *
|
Chris@0
|
88 * @param string|iterable $dirs The directory path
|
Chris@0
|
89 * @param int $mode The directory mode
|
Chris@0
|
90 *
|
Chris@0
|
91 * @throws IOException On any directory creation failure
|
Chris@0
|
92 */
|
Chris@0
|
93 public function mkdir($dirs, $mode = 0777)
|
Chris@0
|
94 {
|
Chris@0
|
95 foreach ($this->toIterable($dirs) as $dir) {
|
Chris@0
|
96 if (is_dir($dir)) {
|
Chris@0
|
97 continue;
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 if (!self::box('mkdir', $dir, $mode, true)) {
|
Chris@0
|
101 if (!is_dir($dir)) {
|
Chris@0
|
102 // The directory was not created by a concurrent process. Let's throw an exception with a developer friendly error message if we have one
|
Chris@0
|
103 if (self::$lastError) {
|
Chris@0
|
104 throw new IOException(sprintf('Failed to create "%s": %s.', $dir, self::$lastError), 0, null, $dir);
|
Chris@0
|
105 }
|
Chris@0
|
106 throw new IOException(sprintf('Failed to create "%s"', $dir), 0, null, $dir);
|
Chris@0
|
107 }
|
Chris@0
|
108 }
|
Chris@0
|
109 }
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 /**
|
Chris@0
|
113 * Checks the existence of files or directories.
|
Chris@0
|
114 *
|
Chris@0
|
115 * @param string|iterable $files A filename, an array of files, or a \Traversable instance to check
|
Chris@0
|
116 *
|
Chris@0
|
117 * @return bool true if the file exists, false otherwise
|
Chris@0
|
118 */
|
Chris@0
|
119 public function exists($files)
|
Chris@0
|
120 {
|
Chris@0
|
121 $maxPathLength = PHP_MAXPATHLEN - 2;
|
Chris@0
|
122
|
Chris@0
|
123 foreach ($this->toIterable($files) as $file) {
|
Chris@4
|
124 if (\strlen($file) > $maxPathLength) {
|
Chris@0
|
125 throw new IOException(sprintf('Could not check if file exist because path length exceeds %d characters.', $maxPathLength), 0, null, $file);
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 if (!file_exists($file)) {
|
Chris@0
|
129 return false;
|
Chris@0
|
130 }
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 return true;
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 /**
|
Chris@0
|
137 * Sets access and modification time of file.
|
Chris@0
|
138 *
|
Chris@0
|
139 * @param string|iterable $files A filename, an array of files, or a \Traversable instance to create
|
Chris@5
|
140 * @param int|null $time The touch time as a Unix timestamp, if not supplied the current system time is used
|
Chris@5
|
141 * @param int|null $atime The access time as a Unix timestamp, if not supplied the current system time is used
|
Chris@0
|
142 *
|
Chris@0
|
143 * @throws IOException When touch fails
|
Chris@0
|
144 */
|
Chris@0
|
145 public function touch($files, $time = null, $atime = null)
|
Chris@0
|
146 {
|
Chris@0
|
147 foreach ($this->toIterable($files) as $file) {
|
Chris@0
|
148 $touch = $time ? @touch($file, $time, $atime) : @touch($file);
|
Chris@0
|
149 if (true !== $touch) {
|
Chris@0
|
150 throw new IOException(sprintf('Failed to touch "%s".', $file), 0, null, $file);
|
Chris@0
|
151 }
|
Chris@0
|
152 }
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 /**
|
Chris@0
|
156 * Removes files or directories.
|
Chris@0
|
157 *
|
Chris@0
|
158 * @param string|iterable $files A filename, an array of files, or a \Traversable instance to remove
|
Chris@0
|
159 *
|
Chris@0
|
160 * @throws IOException When removal fails
|
Chris@0
|
161 */
|
Chris@0
|
162 public function remove($files)
|
Chris@0
|
163 {
|
Chris@0
|
164 if ($files instanceof \Traversable) {
|
Chris@0
|
165 $files = iterator_to_array($files, false);
|
Chris@4
|
166 } elseif (!\is_array($files)) {
|
Chris@4
|
167 $files = [$files];
|
Chris@0
|
168 }
|
Chris@0
|
169 $files = array_reverse($files);
|
Chris@0
|
170 foreach ($files as $file) {
|
Chris@0
|
171 if (is_link($file)) {
|
Chris@0
|
172 // See https://bugs.php.net/52176
|
Chris@4
|
173 if (!(self::box('unlink', $file) || '\\' !== \DIRECTORY_SEPARATOR || self::box('rmdir', $file)) && file_exists($file)) {
|
Chris@0
|
174 throw new IOException(sprintf('Failed to remove symlink "%s": %s.', $file, self::$lastError));
|
Chris@0
|
175 }
|
Chris@0
|
176 } elseif (is_dir($file)) {
|
Chris@0
|
177 $this->remove(new \FilesystemIterator($file, \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS));
|
Chris@0
|
178
|
Chris@0
|
179 if (!self::box('rmdir', $file) && file_exists($file)) {
|
Chris@0
|
180 throw new IOException(sprintf('Failed to remove directory "%s": %s.', $file, self::$lastError));
|
Chris@0
|
181 }
|
Chris@0
|
182 } elseif (!self::box('unlink', $file) && file_exists($file)) {
|
Chris@0
|
183 throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, self::$lastError));
|
Chris@0
|
184 }
|
Chris@0
|
185 }
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 /**
|
Chris@0
|
189 * Change mode for an array of files or directories.
|
Chris@0
|
190 *
|
Chris@0
|
191 * @param string|iterable $files A filename, an array of files, or a \Traversable instance to change mode
|
Chris@0
|
192 * @param int $mode The new mode (octal)
|
Chris@0
|
193 * @param int $umask The mode mask (octal)
|
Chris@0
|
194 * @param bool $recursive Whether change the mod recursively or not
|
Chris@0
|
195 *
|
Chris@5
|
196 * @throws IOException When the change fails
|
Chris@0
|
197 */
|
Chris@0
|
198 public function chmod($files, $mode, $umask = 0000, $recursive = false)
|
Chris@0
|
199 {
|
Chris@0
|
200 foreach ($this->toIterable($files) as $file) {
|
Chris@0
|
201 if (true !== @chmod($file, $mode & ~$umask)) {
|
Chris@0
|
202 throw new IOException(sprintf('Failed to chmod file "%s".', $file), 0, null, $file);
|
Chris@0
|
203 }
|
Chris@0
|
204 if ($recursive && is_dir($file) && !is_link($file)) {
|
Chris@0
|
205 $this->chmod(new \FilesystemIterator($file), $mode, $umask, true);
|
Chris@0
|
206 }
|
Chris@0
|
207 }
|
Chris@0
|
208 }
|
Chris@0
|
209
|
Chris@0
|
210 /**
|
Chris@0
|
211 * Change the owner of an array of files or directories.
|
Chris@0
|
212 *
|
Chris@0
|
213 * @param string|iterable $files A filename, an array of files, or a \Traversable instance to change owner
|
Chris@0
|
214 * @param string $user The new owner user name
|
Chris@0
|
215 * @param bool $recursive Whether change the owner recursively or not
|
Chris@0
|
216 *
|
Chris@5
|
217 * @throws IOException When the change fails
|
Chris@0
|
218 */
|
Chris@0
|
219 public function chown($files, $user, $recursive = false)
|
Chris@0
|
220 {
|
Chris@0
|
221 foreach ($this->toIterable($files) as $file) {
|
Chris@0
|
222 if ($recursive && is_dir($file) && !is_link($file)) {
|
Chris@0
|
223 $this->chown(new \FilesystemIterator($file), $user, true);
|
Chris@0
|
224 }
|
Chris@4
|
225 if (is_link($file) && \function_exists('lchown')) {
|
Chris@0
|
226 if (true !== @lchown($file, $user)) {
|
Chris@0
|
227 throw new IOException(sprintf('Failed to chown file "%s".', $file), 0, null, $file);
|
Chris@0
|
228 }
|
Chris@0
|
229 } else {
|
Chris@0
|
230 if (true !== @chown($file, $user)) {
|
Chris@0
|
231 throw new IOException(sprintf('Failed to chown file "%s".', $file), 0, null, $file);
|
Chris@0
|
232 }
|
Chris@0
|
233 }
|
Chris@0
|
234 }
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 /**
|
Chris@0
|
238 * Change the group of an array of files or directories.
|
Chris@0
|
239 *
|
Chris@0
|
240 * @param string|iterable $files A filename, an array of files, or a \Traversable instance to change group
|
Chris@0
|
241 * @param string $group The group name
|
Chris@0
|
242 * @param bool $recursive Whether change the group recursively or not
|
Chris@0
|
243 *
|
Chris@5
|
244 * @throws IOException When the change fails
|
Chris@0
|
245 */
|
Chris@0
|
246 public function chgrp($files, $group, $recursive = false)
|
Chris@0
|
247 {
|
Chris@0
|
248 foreach ($this->toIterable($files) as $file) {
|
Chris@0
|
249 if ($recursive && is_dir($file) && !is_link($file)) {
|
Chris@0
|
250 $this->chgrp(new \FilesystemIterator($file), $group, true);
|
Chris@0
|
251 }
|
Chris@4
|
252 if (is_link($file) && \function_exists('lchgrp')) {
|
Chris@4
|
253 if (true !== @lchgrp($file, $group) || (\defined('HHVM_VERSION') && !posix_getgrnam($group))) {
|
Chris@0
|
254 throw new IOException(sprintf('Failed to chgrp file "%s".', $file), 0, null, $file);
|
Chris@0
|
255 }
|
Chris@0
|
256 } else {
|
Chris@0
|
257 if (true !== @chgrp($file, $group)) {
|
Chris@0
|
258 throw new IOException(sprintf('Failed to chgrp file "%s".', $file), 0, null, $file);
|
Chris@0
|
259 }
|
Chris@0
|
260 }
|
Chris@0
|
261 }
|
Chris@0
|
262 }
|
Chris@0
|
263
|
Chris@0
|
264 /**
|
Chris@0
|
265 * Renames a file or a directory.
|
Chris@0
|
266 *
|
Chris@0
|
267 * @param string $origin The origin filename or directory
|
Chris@0
|
268 * @param string $target The new filename or directory
|
Chris@0
|
269 * @param bool $overwrite Whether to overwrite the target if it already exists
|
Chris@0
|
270 *
|
Chris@0
|
271 * @throws IOException When target file or directory already exists
|
Chris@0
|
272 * @throws IOException When origin cannot be renamed
|
Chris@0
|
273 */
|
Chris@0
|
274 public function rename($origin, $target, $overwrite = false)
|
Chris@0
|
275 {
|
Chris@0
|
276 // we check that target does not exist
|
Chris@0
|
277 if (!$overwrite && $this->isReadable($target)) {
|
Chris@0
|
278 throw new IOException(sprintf('Cannot rename because the target "%s" already exists.', $target), 0, null, $target);
|
Chris@0
|
279 }
|
Chris@0
|
280
|
Chris@0
|
281 if (true !== @rename($origin, $target)) {
|
Chris@0
|
282 if (is_dir($origin)) {
|
Chris@0
|
283 // See https://bugs.php.net/bug.php?id=54097 & http://php.net/manual/en/function.rename.php#113943
|
Chris@4
|
284 $this->mirror($origin, $target, null, ['override' => $overwrite, 'delete' => $overwrite]);
|
Chris@0
|
285 $this->remove($origin);
|
Chris@0
|
286
|
Chris@0
|
287 return;
|
Chris@0
|
288 }
|
Chris@0
|
289 throw new IOException(sprintf('Cannot rename "%s" to "%s".', $origin, $target), 0, null, $target);
|
Chris@0
|
290 }
|
Chris@0
|
291 }
|
Chris@0
|
292
|
Chris@0
|
293 /**
|
Chris@0
|
294 * Tells whether a file exists and is readable.
|
Chris@0
|
295 *
|
Chris@0
|
296 * @param string $filename Path to the file
|
Chris@0
|
297 *
|
Chris@0
|
298 * @return bool
|
Chris@0
|
299 *
|
Chris@0
|
300 * @throws IOException When windows path is longer than 258 characters
|
Chris@0
|
301 */
|
Chris@0
|
302 private function isReadable($filename)
|
Chris@0
|
303 {
|
Chris@0
|
304 $maxPathLength = PHP_MAXPATHLEN - 2;
|
Chris@0
|
305
|
Chris@4
|
306 if (\strlen($filename) > $maxPathLength) {
|
Chris@0
|
307 throw new IOException(sprintf('Could not check if file is readable because path length exceeds %d characters.', $maxPathLength), 0, null, $filename);
|
Chris@0
|
308 }
|
Chris@0
|
309
|
Chris@0
|
310 return is_readable($filename);
|
Chris@0
|
311 }
|
Chris@0
|
312
|
Chris@0
|
313 /**
|
Chris@0
|
314 * Creates a symbolic link or copy a directory.
|
Chris@0
|
315 *
|
Chris@0
|
316 * @param string $originDir The origin directory path
|
Chris@0
|
317 * @param string $targetDir The symbolic link name
|
Chris@0
|
318 * @param bool $copyOnWindows Whether to copy files if on Windows
|
Chris@0
|
319 *
|
Chris@0
|
320 * @throws IOException When symlink fails
|
Chris@0
|
321 */
|
Chris@0
|
322 public function symlink($originDir, $targetDir, $copyOnWindows = false)
|
Chris@0
|
323 {
|
Chris@4
|
324 if ('\\' === \DIRECTORY_SEPARATOR) {
|
Chris@0
|
325 $originDir = strtr($originDir, '/', '\\');
|
Chris@0
|
326 $targetDir = strtr($targetDir, '/', '\\');
|
Chris@0
|
327
|
Chris@0
|
328 if ($copyOnWindows) {
|
Chris@0
|
329 $this->mirror($originDir, $targetDir);
|
Chris@0
|
330
|
Chris@0
|
331 return;
|
Chris@0
|
332 }
|
Chris@0
|
333 }
|
Chris@0
|
334
|
Chris@4
|
335 $this->mkdir(\dirname($targetDir));
|
Chris@0
|
336
|
Chris@0
|
337 if (is_link($targetDir)) {
|
Chris@0
|
338 if (readlink($targetDir) === $originDir) {
|
Chris@0
|
339 return;
|
Chris@0
|
340 }
|
Chris@0
|
341 $this->remove($targetDir);
|
Chris@0
|
342 }
|
Chris@0
|
343
|
Chris@0
|
344 if (!self::box('symlink', $originDir, $targetDir)) {
|
Chris@0
|
345 $this->linkException($originDir, $targetDir, 'symbolic');
|
Chris@0
|
346 }
|
Chris@0
|
347 }
|
Chris@0
|
348
|
Chris@0
|
349 /**
|
Chris@0
|
350 * Creates a hard link, or several hard links to a file.
|
Chris@0
|
351 *
|
Chris@0
|
352 * @param string $originFile The original file
|
Chris@0
|
353 * @param string|string[] $targetFiles The target file(s)
|
Chris@0
|
354 *
|
Chris@0
|
355 * @throws FileNotFoundException When original file is missing or not a file
|
Chris@0
|
356 * @throws IOException When link fails, including if link already exists
|
Chris@0
|
357 */
|
Chris@0
|
358 public function hardlink($originFile, $targetFiles)
|
Chris@0
|
359 {
|
Chris@0
|
360 if (!$this->exists($originFile)) {
|
Chris@0
|
361 throw new FileNotFoundException(null, 0, null, $originFile);
|
Chris@0
|
362 }
|
Chris@0
|
363
|
Chris@0
|
364 if (!is_file($originFile)) {
|
Chris@0
|
365 throw new FileNotFoundException(sprintf('Origin file "%s" is not a file', $originFile));
|
Chris@0
|
366 }
|
Chris@0
|
367
|
Chris@0
|
368 foreach ($this->toIterable($targetFiles) as $targetFile) {
|
Chris@0
|
369 if (is_file($targetFile)) {
|
Chris@0
|
370 if (fileinode($originFile) === fileinode($targetFile)) {
|
Chris@0
|
371 continue;
|
Chris@0
|
372 }
|
Chris@0
|
373 $this->remove($targetFile);
|
Chris@0
|
374 }
|
Chris@0
|
375
|
Chris@0
|
376 if (!self::box('link', $originFile, $targetFile)) {
|
Chris@0
|
377 $this->linkException($originFile, $targetFile, 'hard');
|
Chris@0
|
378 }
|
Chris@0
|
379 }
|
Chris@0
|
380 }
|
Chris@0
|
381
|
Chris@0
|
382 /**
|
Chris@0
|
383 * @param string $origin
|
Chris@0
|
384 * @param string $target
|
Chris@0
|
385 * @param string $linkType Name of the link type, typically 'symbolic' or 'hard'
|
Chris@0
|
386 */
|
Chris@0
|
387 private function linkException($origin, $target, $linkType)
|
Chris@0
|
388 {
|
Chris@0
|
389 if (self::$lastError) {
|
Chris@4
|
390 if ('\\' === \DIRECTORY_SEPARATOR && false !== strpos(self::$lastError, 'error code(1314)')) {
|
Chris@0
|
391 throw new IOException(sprintf('Unable to create %s link due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?', $linkType), 0, null, $target);
|
Chris@0
|
392 }
|
Chris@0
|
393 }
|
Chris@0
|
394 throw new IOException(sprintf('Failed to create %s link from "%s" to "%s".', $linkType, $origin, $target), 0, null, $target);
|
Chris@0
|
395 }
|
Chris@0
|
396
|
Chris@0
|
397 /**
|
Chris@0
|
398 * Resolves links in paths.
|
Chris@0
|
399 *
|
Chris@0
|
400 * With $canonicalize = false (default)
|
Chris@0
|
401 * - if $path does not exist or is not a link, returns null
|
Chris@0
|
402 * - if $path is a link, returns the next direct target of the link without considering the existence of the target
|
Chris@0
|
403 *
|
Chris@0
|
404 * With $canonicalize = true
|
Chris@0
|
405 * - if $path does not exist, returns null
|
Chris@0
|
406 * - if $path exists, returns its absolute fully resolved final version
|
Chris@0
|
407 *
|
Chris@0
|
408 * @param string $path A filesystem path
|
Chris@0
|
409 * @param bool $canonicalize Whether or not to return a canonicalized path
|
Chris@0
|
410 *
|
Chris@0
|
411 * @return string|null
|
Chris@0
|
412 */
|
Chris@0
|
413 public function readlink($path, $canonicalize = false)
|
Chris@0
|
414 {
|
Chris@0
|
415 if (!$canonicalize && !is_link($path)) {
|
Chris@0
|
416 return;
|
Chris@0
|
417 }
|
Chris@0
|
418
|
Chris@0
|
419 if ($canonicalize) {
|
Chris@0
|
420 if (!$this->exists($path)) {
|
Chris@0
|
421 return;
|
Chris@0
|
422 }
|
Chris@0
|
423
|
Chris@4
|
424 if ('\\' === \DIRECTORY_SEPARATOR) {
|
Chris@0
|
425 $path = readlink($path);
|
Chris@0
|
426 }
|
Chris@0
|
427
|
Chris@0
|
428 return realpath($path);
|
Chris@0
|
429 }
|
Chris@0
|
430
|
Chris@4
|
431 if ('\\' === \DIRECTORY_SEPARATOR) {
|
Chris@0
|
432 return realpath($path);
|
Chris@0
|
433 }
|
Chris@0
|
434
|
Chris@0
|
435 return readlink($path);
|
Chris@0
|
436 }
|
Chris@0
|
437
|
Chris@0
|
438 /**
|
Chris@0
|
439 * Given an existing path, convert it to a path relative to a given starting path.
|
Chris@0
|
440 *
|
Chris@0
|
441 * @param string $endPath Absolute path of target
|
Chris@0
|
442 * @param string $startPath Absolute path where traversal begins
|
Chris@0
|
443 *
|
Chris@0
|
444 * @return string Path of target relative to starting path
|
Chris@0
|
445 */
|
Chris@0
|
446 public function makePathRelative($endPath, $startPath)
|
Chris@0
|
447 {
|
Chris@0
|
448 if (!$this->isAbsolutePath($endPath) || !$this->isAbsolutePath($startPath)) {
|
Chris@0
|
449 @trigger_error(sprintf('Support for passing relative paths to %s() is deprecated since Symfony 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
|
Chris@0
|
450 }
|
Chris@0
|
451
|
Chris@0
|
452 // Normalize separators on Windows
|
Chris@4
|
453 if ('\\' === \DIRECTORY_SEPARATOR) {
|
Chris@0
|
454 $endPath = str_replace('\\', '/', $endPath);
|
Chris@0
|
455 $startPath = str_replace('\\', '/', $startPath);
|
Chris@0
|
456 }
|
Chris@0
|
457
|
Chris@0
|
458 $stripDriveLetter = function ($path) {
|
Chris@4
|
459 if (\strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0])) {
|
Chris@0
|
460 return substr($path, 2);
|
Chris@0
|
461 }
|
Chris@0
|
462
|
Chris@0
|
463 return $path;
|
Chris@0
|
464 };
|
Chris@0
|
465
|
Chris@0
|
466 $endPath = $stripDriveLetter($endPath);
|
Chris@0
|
467 $startPath = $stripDriveLetter($startPath);
|
Chris@0
|
468
|
Chris@0
|
469 // Split the paths into arrays
|
Chris@0
|
470 $startPathArr = explode('/', trim($startPath, '/'));
|
Chris@0
|
471 $endPathArr = explode('/', trim($endPath, '/'));
|
Chris@0
|
472
|
Chris@0
|
473 $normalizePathArray = function ($pathSegments, $absolute) {
|
Chris@4
|
474 $result = [];
|
Chris@0
|
475
|
Chris@0
|
476 foreach ($pathSegments as $segment) {
|
Chris@4
|
477 if ('..' === $segment && ($absolute || \count($result))) {
|
Chris@0
|
478 array_pop($result);
|
Chris@0
|
479 } elseif ('.' !== $segment) {
|
Chris@0
|
480 $result[] = $segment;
|
Chris@0
|
481 }
|
Chris@0
|
482 }
|
Chris@0
|
483
|
Chris@0
|
484 return $result;
|
Chris@0
|
485 };
|
Chris@0
|
486
|
Chris@0
|
487 $startPathArr = $normalizePathArray($startPathArr, static::isAbsolutePath($startPath));
|
Chris@0
|
488 $endPathArr = $normalizePathArray($endPathArr, static::isAbsolutePath($endPath));
|
Chris@0
|
489
|
Chris@0
|
490 // Find for which directory the common path stops
|
Chris@0
|
491 $index = 0;
|
Chris@0
|
492 while (isset($startPathArr[$index]) && isset($endPathArr[$index]) && $startPathArr[$index] === $endPathArr[$index]) {
|
Chris@0
|
493 ++$index;
|
Chris@0
|
494 }
|
Chris@0
|
495
|
Chris@0
|
496 // Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels)
|
Chris@4
|
497 if (1 === \count($startPathArr) && '' === $startPathArr[0]) {
|
Chris@0
|
498 $depth = 0;
|
Chris@0
|
499 } else {
|
Chris@4
|
500 $depth = \count($startPathArr) - $index;
|
Chris@0
|
501 }
|
Chris@0
|
502
|
Chris@0
|
503 // Repeated "../" for each level need to reach the common path
|
Chris@0
|
504 $traverser = str_repeat('../', $depth);
|
Chris@0
|
505
|
Chris@4
|
506 $endPathRemainder = implode('/', \array_slice($endPathArr, $index));
|
Chris@0
|
507
|
Chris@0
|
508 // Construct $endPath from traversing to the common path, then to the remaining $endPath
|
Chris@0
|
509 $relativePath = $traverser.('' !== $endPathRemainder ? $endPathRemainder.'/' : '');
|
Chris@0
|
510
|
Chris@0
|
511 return '' === $relativePath ? './' : $relativePath;
|
Chris@0
|
512 }
|
Chris@0
|
513
|
Chris@0
|
514 /**
|
Chris@0
|
515 * Mirrors a directory to another.
|
Chris@0
|
516 *
|
Chris@0
|
517 * Copies files and directories from the origin directory into the target directory. By default:
|
Chris@0
|
518 *
|
Chris@0
|
519 * - existing files in the target directory will be overwritten, except if they are newer (see the `override` option)
|
Chris@0
|
520 * - files in the target directory that do not exist in the source directory will not be deleted (see the `delete` option)
|
Chris@0
|
521 *
|
Chris@5
|
522 * @param string $originDir The origin directory
|
Chris@5
|
523 * @param string $targetDir The target directory
|
Chris@5
|
524 * @param \Traversable|null $iterator Iterator that filters which files and directories to copy, if null a recursive iterator is created
|
Chris@5
|
525 * @param array $options An array of boolean options
|
Chris@5
|
526 * Valid options are:
|
Chris@5
|
527 * - $options['override'] If true, target files newer than origin files are overwritten (see copy(), defaults to false)
|
Chris@5
|
528 * - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink(), defaults to false)
|
Chris@5
|
529 * - $options['delete'] Whether to delete files that are not in the source directory (defaults to false)
|
Chris@0
|
530 *
|
Chris@0
|
531 * @throws IOException When file type is unknown
|
Chris@0
|
532 */
|
Chris@4
|
533 public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = [])
|
Chris@0
|
534 {
|
Chris@0
|
535 $targetDir = rtrim($targetDir, '/\\');
|
Chris@0
|
536 $originDir = rtrim($originDir, '/\\');
|
Chris@4
|
537 $originDirLen = \strlen($originDir);
|
Chris@0
|
538
|
Chris@0
|
539 // Iterate in destination folder to remove obsolete entries
|
Chris@0
|
540 if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) {
|
Chris@0
|
541 $deleteIterator = $iterator;
|
Chris@0
|
542 if (null === $deleteIterator) {
|
Chris@0
|
543 $flags = \FilesystemIterator::SKIP_DOTS;
|
Chris@0
|
544 $deleteIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($targetDir, $flags), \RecursiveIteratorIterator::CHILD_FIRST);
|
Chris@0
|
545 }
|
Chris@4
|
546 $targetDirLen = \strlen($targetDir);
|
Chris@0
|
547 foreach ($deleteIterator as $file) {
|
Chris@0
|
548 $origin = $originDir.substr($file->getPathname(), $targetDirLen);
|
Chris@0
|
549 if (!$this->exists($origin)) {
|
Chris@0
|
550 $this->remove($file);
|
Chris@0
|
551 }
|
Chris@0
|
552 }
|
Chris@0
|
553 }
|
Chris@0
|
554
|
Chris@0
|
555 $copyOnWindows = false;
|
Chris@0
|
556 if (isset($options['copy_on_windows'])) {
|
Chris@0
|
557 $copyOnWindows = $options['copy_on_windows'];
|
Chris@0
|
558 }
|
Chris@0
|
559
|
Chris@0
|
560 if (null === $iterator) {
|
Chris@0
|
561 $flags = $copyOnWindows ? \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS : \FilesystemIterator::SKIP_DOTS;
|
Chris@0
|
562 $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
|
Chris@0
|
563 }
|
Chris@0
|
564
|
Chris@0
|
565 if ($this->exists($originDir)) {
|
Chris@0
|
566 $this->mkdir($targetDir);
|
Chris@0
|
567 }
|
Chris@0
|
568
|
Chris@0
|
569 foreach ($iterator as $file) {
|
Chris@0
|
570 $target = $targetDir.substr($file->getPathname(), $originDirLen);
|
Chris@0
|
571
|
Chris@0
|
572 if ($copyOnWindows) {
|
Chris@0
|
573 if (is_file($file)) {
|
Chris@0
|
574 $this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
|
Chris@0
|
575 } elseif (is_dir($file)) {
|
Chris@0
|
576 $this->mkdir($target);
|
Chris@0
|
577 } else {
|
Chris@0
|
578 throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
|
Chris@0
|
579 }
|
Chris@0
|
580 } else {
|
Chris@0
|
581 if (is_link($file)) {
|
Chris@0
|
582 $this->symlink($file->getLinkTarget(), $target);
|
Chris@0
|
583 } elseif (is_dir($file)) {
|
Chris@0
|
584 $this->mkdir($target);
|
Chris@0
|
585 } elseif (is_file($file)) {
|
Chris@0
|
586 $this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
|
Chris@0
|
587 } else {
|
Chris@0
|
588 throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
|
Chris@0
|
589 }
|
Chris@0
|
590 }
|
Chris@0
|
591 }
|
Chris@0
|
592 }
|
Chris@0
|
593
|
Chris@0
|
594 /**
|
Chris@0
|
595 * Returns whether the file path is an absolute path.
|
Chris@0
|
596 *
|
Chris@0
|
597 * @param string $file A file path
|
Chris@0
|
598 *
|
Chris@0
|
599 * @return bool
|
Chris@0
|
600 */
|
Chris@0
|
601 public function isAbsolutePath($file)
|
Chris@0
|
602 {
|
Chris@0
|
603 return strspn($file, '/\\', 0, 1)
|
Chris@4
|
604 || (\strlen($file) > 3 && ctype_alpha($file[0])
|
Chris@0
|
605 && ':' === $file[1]
|
Chris@0
|
606 && strspn($file, '/\\', 2, 1)
|
Chris@0
|
607 )
|
Chris@0
|
608 || null !== parse_url($file, PHP_URL_SCHEME)
|
Chris@0
|
609 ;
|
Chris@0
|
610 }
|
Chris@0
|
611
|
Chris@0
|
612 /**
|
Chris@0
|
613 * Creates a temporary file with support for custom stream wrappers.
|
Chris@0
|
614 *
|
Chris@0
|
615 * @param string $dir The directory where the temporary filename will be created
|
Chris@0
|
616 * @param string $prefix The prefix of the generated temporary filename
|
Chris@0
|
617 * Note: Windows uses only the first three characters of prefix
|
Chris@0
|
618 *
|
Chris@0
|
619 * @return string The new temporary filename (with path), or throw an exception on failure
|
Chris@0
|
620 */
|
Chris@0
|
621 public function tempnam($dir, $prefix)
|
Chris@0
|
622 {
|
Chris@0
|
623 list($scheme, $hierarchy) = $this->getSchemeAndHierarchy($dir);
|
Chris@0
|
624
|
Chris@0
|
625 // If no scheme or scheme is "file" or "gs" (Google Cloud) create temp file in local filesystem
|
Chris@0
|
626 if (null === $scheme || 'file' === $scheme || 'gs' === $scheme) {
|
Chris@0
|
627 $tmpFile = @tempnam($hierarchy, $prefix);
|
Chris@0
|
628
|
Chris@0
|
629 // If tempnam failed or no scheme return the filename otherwise prepend the scheme
|
Chris@0
|
630 if (false !== $tmpFile) {
|
Chris@0
|
631 if (null !== $scheme && 'gs' !== $scheme) {
|
Chris@0
|
632 return $scheme.'://'.$tmpFile;
|
Chris@0
|
633 }
|
Chris@0
|
634
|
Chris@0
|
635 return $tmpFile;
|
Chris@0
|
636 }
|
Chris@0
|
637
|
Chris@0
|
638 throw new IOException('A temporary file could not be created.');
|
Chris@0
|
639 }
|
Chris@0
|
640
|
Chris@0
|
641 // Loop until we create a valid temp file or have reached 10 attempts
|
Chris@0
|
642 for ($i = 0; $i < 10; ++$i) {
|
Chris@0
|
643 // Create a unique filename
|
Chris@0
|
644 $tmpFile = $dir.'/'.$prefix.uniqid(mt_rand(), true);
|
Chris@0
|
645
|
Chris@0
|
646 // Use fopen instead of file_exists as some streams do not support stat
|
Chris@0
|
647 // Use mode 'x+' to atomically check existence and create to avoid a TOCTOU vulnerability
|
Chris@0
|
648 $handle = @fopen($tmpFile, 'x+');
|
Chris@0
|
649
|
Chris@0
|
650 // If unsuccessful restart the loop
|
Chris@0
|
651 if (false === $handle) {
|
Chris@0
|
652 continue;
|
Chris@0
|
653 }
|
Chris@0
|
654
|
Chris@0
|
655 // Close the file if it was successfully opened
|
Chris@0
|
656 @fclose($handle);
|
Chris@0
|
657
|
Chris@0
|
658 return $tmpFile;
|
Chris@0
|
659 }
|
Chris@0
|
660
|
Chris@0
|
661 throw new IOException('A temporary file could not be created.');
|
Chris@0
|
662 }
|
Chris@0
|
663
|
Chris@0
|
664 /**
|
Chris@0
|
665 * Atomically dumps content into a file.
|
Chris@0
|
666 *
|
Chris@0
|
667 * @param string $filename The file to be written to
|
Chris@0
|
668 * @param string $content The data to write into the file
|
Chris@0
|
669 *
|
Chris@0
|
670 * @throws IOException if the file cannot be written to
|
Chris@0
|
671 */
|
Chris@0
|
672 public function dumpFile($filename, $content)
|
Chris@0
|
673 {
|
Chris@4
|
674 $dir = \dirname($filename);
|
Chris@0
|
675
|
Chris@0
|
676 if (!is_dir($dir)) {
|
Chris@0
|
677 $this->mkdir($dir);
|
Chris@0
|
678 }
|
Chris@0
|
679
|
Chris@0
|
680 if (!is_writable($dir)) {
|
Chris@0
|
681 throw new IOException(sprintf('Unable to write to the "%s" directory.', $dir), 0, null, $dir);
|
Chris@0
|
682 }
|
Chris@0
|
683
|
Chris@0
|
684 // Will create a temp file with 0600 access rights
|
Chris@0
|
685 // when the filesystem supports chmod.
|
Chris@0
|
686 $tmpFile = $this->tempnam($dir, basename($filename));
|
Chris@0
|
687
|
Chris@0
|
688 if (false === @file_put_contents($tmpFile, $content)) {
|
Chris@0
|
689 throw new IOException(sprintf('Failed to write file "%s".', $filename), 0, null, $filename);
|
Chris@0
|
690 }
|
Chris@0
|
691
|
Chris@0
|
692 @chmod($tmpFile, file_exists($filename) ? fileperms($filename) : 0666 & ~umask());
|
Chris@0
|
693
|
Chris@0
|
694 $this->rename($tmpFile, $filename, true);
|
Chris@0
|
695 }
|
Chris@0
|
696
|
Chris@0
|
697 /**
|
Chris@0
|
698 * Appends content to an existing file.
|
Chris@0
|
699 *
|
Chris@0
|
700 * @param string $filename The file to which to append content
|
Chris@0
|
701 * @param string $content The content to append
|
Chris@0
|
702 *
|
Chris@0
|
703 * @throws IOException If the file is not writable
|
Chris@0
|
704 */
|
Chris@0
|
705 public function appendToFile($filename, $content)
|
Chris@0
|
706 {
|
Chris@4
|
707 $dir = \dirname($filename);
|
Chris@0
|
708
|
Chris@0
|
709 if (!is_dir($dir)) {
|
Chris@0
|
710 $this->mkdir($dir);
|
Chris@0
|
711 }
|
Chris@0
|
712
|
Chris@0
|
713 if (!is_writable($dir)) {
|
Chris@0
|
714 throw new IOException(sprintf('Unable to write to the "%s" directory.', $dir), 0, null, $dir);
|
Chris@0
|
715 }
|
Chris@0
|
716
|
Chris@0
|
717 if (false === @file_put_contents($filename, $content, FILE_APPEND)) {
|
Chris@0
|
718 throw new IOException(sprintf('Failed to write file "%s".', $filename), 0, null, $filename);
|
Chris@0
|
719 }
|
Chris@0
|
720 }
|
Chris@0
|
721
|
Chris@0
|
722 /**
|
Chris@0
|
723 * @param mixed $files
|
Chris@0
|
724 *
|
Chris@0
|
725 * @return array|\Traversable
|
Chris@0
|
726 */
|
Chris@0
|
727 private function toIterable($files)
|
Chris@0
|
728 {
|
Chris@4
|
729 return \is_array($files) || $files instanceof \Traversable ? $files : [$files];
|
Chris@0
|
730 }
|
Chris@0
|
731
|
Chris@0
|
732 /**
|
Chris@4
|
733 * Gets a 2-tuple of scheme (may be null) and hierarchical part of a filename (e.g. file:///tmp -> [file, tmp]).
|
Chris@0
|
734 *
|
Chris@0
|
735 * @param string $filename The filename to be parsed
|
Chris@0
|
736 *
|
Chris@0
|
737 * @return array The filename scheme and hierarchical part
|
Chris@0
|
738 */
|
Chris@0
|
739 private function getSchemeAndHierarchy($filename)
|
Chris@0
|
740 {
|
Chris@0
|
741 $components = explode('://', $filename, 2);
|
Chris@0
|
742
|
Chris@4
|
743 return 2 === \count($components) ? [$components[0], $components[1]] : [null, $components[0]];
|
Chris@0
|
744 }
|
Chris@0
|
745
|
Chris@0
|
746 private static function box($func)
|
Chris@0
|
747 {
|
Chris@0
|
748 self::$lastError = null;
|
Chris@0
|
749 \set_error_handler(__CLASS__.'::handleError');
|
Chris@0
|
750 try {
|
Chris@0
|
751 $result = \call_user_func_array($func, \array_slice(\func_get_args(), 1));
|
Chris@0
|
752 \restore_error_handler();
|
Chris@0
|
753
|
Chris@0
|
754 return $result;
|
Chris@0
|
755 } catch (\Throwable $e) {
|
Chris@0
|
756 } catch (\Exception $e) {
|
Chris@0
|
757 }
|
Chris@0
|
758 \restore_error_handler();
|
Chris@0
|
759
|
Chris@0
|
760 throw $e;
|
Chris@0
|
761 }
|
Chris@0
|
762
|
Chris@0
|
763 /**
|
Chris@0
|
764 * @internal
|
Chris@0
|
765 */
|
Chris@0
|
766 public static function handleError($type, $msg)
|
Chris@0
|
767 {
|
Chris@0
|
768 self::$lastError = $msg;
|
Chris@0
|
769 }
|
Chris@0
|
770 }
|