Filesystem::chmod()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 9.2222
c 0
b 0
f 0
cc 6
nc 4
nop 4
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symfony\Component\Filesystem;
13
14
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
15
use Symfony\Component\Filesystem\Exception\InvalidArgumentException;
16
use Symfony\Component\Filesystem\Exception\IOException;
17
18
/**
19
 * Provides basic utility to manipulate the file system.
20
 *
21
 * @author Fabien Potencier <[email protected]>
22
 */
23
class Filesystem
24
{
25
    private static ?string $lastError = null;
26
27
    /**
28
     * Copies a file.
29
     *
30
     * If the target file is older than the origin file, it's always overwritten.
31
     * If the target file is newer, it is overwritten only when the
32
     * $overwriteNewerFiles option is set to true.
33
     *
34
     * @throws FileNotFoundException When originFile doesn't exist
35
     * @throws IOException           When copy fails
36
     */
37
    public function copy(string $originFile, string $targetFile, bool $overwriteNewerFiles = false): void
38
    {
39
        $originIsLocal = stream_is_local($originFile) || 0 === stripos($originFile, 'file://');
40
        if ($originIsLocal && !is_file($originFile)) {
41
            throw new FileNotFoundException(\sprintf('Failed to copy "%s" because file does not exist.', $originFile), 0, null, $originFile);
42
        }
43
44
        $this->mkdir(\dirname($targetFile));
45
46
        $doCopy = true;
47
        if (!$overwriteNewerFiles && !parse_url($originFile, \PHP_URL_HOST) && is_file($targetFile)) {
48
            $doCopy = filemtime($originFile) > filemtime($targetFile);
49
        }
50
51
        if ($doCopy) {
52
            // https://bugs.php.net/64634
53
            if (!$source = self::box('fopen', $originFile, 'r')) {
54
                throw new IOException(\sprintf('Failed to copy "%s" to "%s" because source file could not be opened for reading: ', $originFile, $targetFile).self::$lastError, 0, null, $originFile);
55
            }
56
57
            // Stream context created to allow files overwrite when using FTP stream wrapper - disabled by default
58
            if (!$target = self::box('fopen', $targetFile, 'w', false, stream_context_create(['ftp' => ['overwrite' => true]]))) {
59
                throw new IOException(\sprintf('Failed to copy "%s" to "%s" because target file could not be opened for writing: ', $originFile, $targetFile).self::$lastError, 0, null, $originFile);
60
            }
61
62
            $bytesCopied = stream_copy_to_stream($source, $target);
63
            fclose($source);
64
            fclose($target);
65
            unset($source, $target);
66
67
            if (!is_file($targetFile)) {
68
                throw new IOException(\sprintf('Failed to copy "%s" to "%s".', $originFile, $targetFile), 0, null, $originFile);
69
            }
70
71
            if ($originIsLocal) {
72
                // Like `cp`, preserve executable permission bits
73
                self::box('chmod', $targetFile, fileperms($targetFile) | (fileperms($originFile) & 0111));
74
75
                // Like `cp`, preserve the file modification time
76
                self::box('touch', $targetFile, filemtime($originFile));
77
78
                if ($bytesCopied !== $bytesOrigin = filesize($originFile)) {
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);
80
                }
81
            }
82
        }
83
    }
84
85
    /**
86
     * Creates a directory recursively.
87
     *
88
     * @throws IOException On any directory creation failure
89
     */
90
    public function mkdir(string|iterable $dirs, int $mode = 0777): void
91
    {
92
        foreach ($this->toIterable($dirs) as $dir) {
93
            if (is_dir($dir)) {
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type iterable; however, parameter $filename of is_dir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
            if (is_dir(/** @scrutinizer ignore-type */ $dir)) {
Loading history...
94
                continue;
95
            }
96
97
            if (!self::box('mkdir', $dir, $mode, true) && !is_dir($dir)) {
98
                throw new IOException(\sprintf('Failed to create "%s": ', $dir).self::$lastError, 0, null, $dir);
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type iterable; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
                throw new IOException(\sprintf('Failed to create "%s": ', /** @scrutinizer ignore-type */ $dir).self::$lastError, 0, null, $dir);
Loading history...
Bug introduced by
It seems like $dir can also be of type iterable; however, parameter $path of Symfony\Component\Filesy...xception::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
                throw new IOException(\sprintf('Failed to create "%s": ', $dir).self::$lastError, 0, null, /** @scrutinizer ignore-type */ $dir);
Loading history...
99
            }
100
        }
101
    }
102
103
    /**
104
     * Checks the existence of files or directories.
105
     */
106
    public function exists(string|iterable $files): bool
107
    {
108
        $maxPathLength = \PHP_MAXPATHLEN - 2;
109
110
        foreach ($this->toIterable($files) as $file) {
111
            if (\strlen($file) > $maxPathLength) {
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
            if (\strlen(/** @scrutinizer ignore-type */ $file) > $maxPathLength) {
Loading history...
112
                throw new IOException(\sprintf('Could not check if file exist because path length exceeds %d characters.', $maxPathLength), 0, null, $file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $path of Symfony\Component\Filesy...xception::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
                throw new IOException(\sprintf('Could not check if file exist because path length exceeds %d characters.', $maxPathLength), 0, null, /** @scrutinizer ignore-type */ $file);
Loading history...
113
            }
114
115
            if (!file_exists($file)) {
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
            if (!file_exists(/** @scrutinizer ignore-type */ $file)) {
Loading history...
116
                return false;
117
            }
118
        }
119
120
        return true;
121
    }
122
123
    /**
124
     * Sets access and modification time of file.
125
     *
126
     * @param int|null $time  The touch time as a Unix timestamp, if not supplied the current system time is used
127
     * @param int|null $atime The access time as a Unix timestamp, if not supplied the current system time is used
128
     *
129
     * @throws IOException When touch fails
130
     */
131
    public function touch(string|iterable $files, ?int $time = null, ?int $atime = null): void
132
    {
133
        foreach ($this->toIterable($files) as $file) {
134
            if (!($time ? self::box('touch', $file, $time, $atime) : self::box('touch', $file))) {
135
                throw new IOException(\sprintf('Failed to touch "%s": ', $file).self::$lastError, 0, null, $file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $path of Symfony\Component\Filesy...xception::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
                throw new IOException(\sprintf('Failed to touch "%s": ', $file).self::$lastError, 0, null, /** @scrutinizer ignore-type */ $file);
Loading history...
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
                throw new IOException(\sprintf('Failed to touch "%s": ', /** @scrutinizer ignore-type */ $file).self::$lastError, 0, null, $file);
Loading history...
136
            }
137
        }
138
    }
139
140
    /**
141
     * Removes files or directories.
142
     *
143
     * @throws IOException When removal fails
144
     */
145
    public function remove(string|iterable $files): void
146
    {
147
        if ($files instanceof \Traversable) {
148
            $files = iterator_to_array($files, false);
149
        } elseif (!\is_array($files)) {
150
            $files = [$files];
151
        }
152
153
        self::doRemove($files, false);
154
    }
155
156
    private static function doRemove(array $files, bool $isRecursive): void
157
    {
158
        $files = array_reverse($files);
159
        foreach ($files as $file) {
160
            if (is_link($file)) {
161
                // See https://bugs.php.net/52176
162
                if (!(self::box('unlink', $file) || '\\' !== \DIRECTORY_SEPARATOR || self::box('rmdir', $file)) && file_exists($file)) {
163
                    throw new IOException(\sprintf('Failed to remove symlink "%s": ', $file).self::$lastError);
164
                }
165
            } elseif (is_dir($file)) {
166
                if (!$isRecursive) {
167
                    $tmpName = \dirname(realpath($file)).'/.!'.strrev(strtr(base64_encode(random_bytes(2)), '/=', '-!'));
168
169
                    if (file_exists($tmpName)) {
170
                        try {
171
                            self::doRemove([$tmpName], true);
172
                        } catch (IOException) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
173
                        }
174
                    }
175
176
                    if (!file_exists($tmpName) && self::box('rename', $file, $tmpName)) {
177
                        $origFile = $file;
178
                        $file = $tmpName;
179
                    } else {
180
                        $origFile = null;
181
                    }
182
                }
183
184
                $filesystemIterator = new \FilesystemIterator($file, \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS);
185
                self::doRemove(iterator_to_array($filesystemIterator, true), true);
186
187
                if (!self::box('rmdir', $file) && file_exists($file) && !$isRecursive) {
188
                    $lastError = self::$lastError;
189
190
                    if (null !== $origFile && self::box('rename', $file, $origFile)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $origFile does not seem to be defined for all execution paths leading up to this point.
Loading history...
191
                        $file = $origFile;
192
                    }
193
194
                    throw new IOException(\sprintf('Failed to remove directory "%s": ', $file).$lastError);
195
                }
196
            } elseif (!self::box('unlink', $file) && ((self::$lastError && str_contains(self::$lastError, 'Permission denied')) || file_exists($file))) {
197
                throw new IOException(\sprintf('Failed to remove file "%s": ', $file).self::$lastError);
198
            }
199
        }
200
    }
201
202
    /**
203
     * Change mode for an array of files or directories.
204
     *
205
     * @param int  $mode      The new mode (octal)
206
     * @param int  $umask     The mode mask (octal)
207
     * @param bool $recursive Whether change the mod recursively or not
208
     *
209
     * @throws IOException When the change fails
210
     */
211
    public function chmod(string|iterable $files, int $mode, int $umask = 0000, bool $recursive = false): void
212
    {
213
        foreach ($this->toIterable($files) as $file) {
214
            if (!self::box('chmod', $file, $mode & ~$umask)) {
215
                throw new IOException(\sprintf('Failed to chmod file "%s": ', $file).self::$lastError, 0, null, $file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

215
                throw new IOException(\sprintf('Failed to chmod file "%s": ', /** @scrutinizer ignore-type */ $file).self::$lastError, 0, null, $file);
Loading history...
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $path of Symfony\Component\Filesy...xception::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

215
                throw new IOException(\sprintf('Failed to chmod file "%s": ', $file).self::$lastError, 0, null, /** @scrutinizer ignore-type */ $file);
Loading history...
216
            }
217
            if ($recursive && is_dir($file) && !is_link($file)) {
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $filename of is_link() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

217
            if ($recursive && is_dir($file) && !is_link(/** @scrutinizer ignore-type */ $file)) {
Loading history...
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $filename of is_dir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

217
            if ($recursive && is_dir(/** @scrutinizer ignore-type */ $file) && !is_link($file)) {
Loading history...
218
                $this->chmod(new \FilesystemIterator($file), $mode, $umask, true);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $directory of FilesystemIterator::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

218
                $this->chmod(new \FilesystemIterator(/** @scrutinizer ignore-type */ $file), $mode, $umask, true);
Loading history...
219
            }
220
        }
221
    }
222
223
    /**
224
     * Change the owner of an array of files or directories.
225
     *
226
     * This method always throws on Windows, as the underlying PHP function is not supported.
227
     *
228
     * @see https://www.php.net/chown
229
     *
230
     * @param string|int $user      A user name or number
231
     * @param bool       $recursive Whether change the owner recursively or not
232
     *
233
     * @throws IOException When the change fails
234
     */
235
    public function chown(string|iterable $files, string|int $user, bool $recursive = false): void
236
    {
237
        foreach ($this->toIterable($files) as $file) {
238
            if ($recursive && is_dir($file) && !is_link($file)) {
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $filename of is_dir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

238
            if ($recursive && is_dir(/** @scrutinizer ignore-type */ $file) && !is_link($file)) {
Loading history...
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $filename of is_link() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

238
            if ($recursive && is_dir($file) && !is_link(/** @scrutinizer ignore-type */ $file)) {
Loading history...
239
                $this->chown(new \FilesystemIterator($file), $user, true);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $directory of FilesystemIterator::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

239
                $this->chown(new \FilesystemIterator(/** @scrutinizer ignore-type */ $file), $user, true);
Loading history...
240
            }
241
            if (is_link($file) && \function_exists('lchown')) {
242
                if (!self::box('lchown', $file, $user)) {
243
                    throw new IOException(\sprintf('Failed to chown file "%s": ', $file).self::$lastError, 0, null, $file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $path of Symfony\Component\Filesy...xception::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

243
                    throw new IOException(\sprintf('Failed to chown file "%s": ', $file).self::$lastError, 0, null, /** @scrutinizer ignore-type */ $file);
Loading history...
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

243
                    throw new IOException(\sprintf('Failed to chown file "%s": ', /** @scrutinizer ignore-type */ $file).self::$lastError, 0, null, $file);
Loading history...
244
                }
245
            } else {
246
                if (!self::box('chown', $file, $user)) {
247
                    throw new IOException(\sprintf('Failed to chown file "%s": ', $file).self::$lastError, 0, null, $file);
248
                }
249
            }
250
        }
251
    }
252
253
    /**
254
     * Change the group of an array of files or directories.
255
     *
256
     * This method always throws on Windows, as the underlying PHP function is not supported.
257
     *
258
     * @see https://www.php.net/chgrp
259
     *
260
     * @param string|int $group     A group name or number
261
     * @param bool       $recursive Whether change the group recursively or not
262
     *
263
     * @throws IOException When the change fails
264
     */
265
    public function chgrp(string|iterable $files, string|int $group, bool $recursive = false): void
266
    {
267
        foreach ($this->toIterable($files) as $file) {
268
            if ($recursive && is_dir($file) && !is_link($file)) {
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $filename of is_dir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

268
            if ($recursive && is_dir(/** @scrutinizer ignore-type */ $file) && !is_link($file)) {
Loading history...
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $filename of is_link() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

268
            if ($recursive && is_dir($file) && !is_link(/** @scrutinizer ignore-type */ $file)) {
Loading history...
269
                $this->chgrp(new \FilesystemIterator($file), $group, true);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $directory of FilesystemIterator::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

269
                $this->chgrp(new \FilesystemIterator(/** @scrutinizer ignore-type */ $file), $group, true);
Loading history...
270
            }
271
            if (is_link($file) && \function_exists('lchgrp')) {
272
                if (!self::box('lchgrp', $file, $group)) {
273
                    throw new IOException(\sprintf('Failed to chgrp file "%s": ', $file).self::$lastError, 0, null, $file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $path of Symfony\Component\Filesy...xception::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

273
                    throw new IOException(\sprintf('Failed to chgrp file "%s": ', $file).self::$lastError, 0, null, /** @scrutinizer ignore-type */ $file);
Loading history...
Bug introduced by
It seems like $file can also be of type iterable; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

273
                    throw new IOException(\sprintf('Failed to chgrp file "%s": ', /** @scrutinizer ignore-type */ $file).self::$lastError, 0, null, $file);
Loading history...
274
                }
275
            } else {
276
                if (!self::box('chgrp', $file, $group)) {
277
                    throw new IOException(\sprintf('Failed to chgrp file "%s": ', $file).self::$lastError, 0, null, $file);
278
                }
279
            }
280
        }
281
    }
282
283
    /**
284
     * Renames a file or a directory.
285
     *
286
     * @throws IOException When target file or directory already exists
287
     * @throws IOException When origin cannot be renamed
288
     */
289
    public function rename(string $origin, string $target, bool $overwrite = false): void
290
    {
291
        // we check that target does not exist
292
        if (!$overwrite && $this->isReadable($target)) {
293
            throw new IOException(\sprintf('Cannot rename because the target "%s" already exists.', $target), 0, null, $target);
294
        }
295
296
        if (!self::box('rename', $origin, $target)) {
297
            if (is_dir($origin)) {
298
                // See https://bugs.php.net/54097 & https://php.net/rename#113943
299
                $this->mirror($origin, $target, null, ['override' => $overwrite, 'delete' => $overwrite]);
300
                $this->remove($origin);
301
302
                return;
303
            }
304
            throw new IOException(\sprintf('Cannot rename "%s" to "%s": ', $origin, $target).self::$lastError, 0, null, $target);
305
        }
306
    }
307
308
    /**
309
     * Tells whether a file exists and is readable.
310
     *
311
     * @throws IOException When windows path is longer than 258 characters
312
     */
313
    private function isReadable(string $filename): bool
314
    {
315
        $maxPathLength = \PHP_MAXPATHLEN - 2;
316
317
        if (\strlen($filename) > $maxPathLength) {
318
            throw new IOException(\sprintf('Could not check if file is readable because path length exceeds %d characters.', $maxPathLength), 0, null, $filename);
319
        }
320
321
        return is_readable($filename);
322
    }
323
324
    /**
325
     * Creates a symbolic link or copy a directory.
326
     *
327
     * @throws IOException When symlink fails
328
     */
329
    public function symlink(string $originDir, string $targetDir, bool $copyOnWindows = false): void
330
    {
331
        self::assertFunctionExists('symlink');
332
333
        if ('\\' === \DIRECTORY_SEPARATOR) {
334
            $originDir = strtr($originDir, '/', '\\');
335
            $targetDir = strtr($targetDir, '/', '\\');
336
337
            if ($copyOnWindows) {
338
                $this->mirror($originDir, $targetDir);
339
340
                return;
341
            }
342
        }
343
344
        $this->mkdir(\dirname($targetDir));
345
346
        if (is_link($targetDir)) {
347
            if (readlink($targetDir) === $originDir) {
348
                return;
349
            }
350
            $this->remove($targetDir);
351
        }
352
353
        if (!self::box('symlink', $originDir, $targetDir)) {
354
            $this->linkException($originDir, $targetDir, 'symbolic');
355
        }
356
    }
357
358
    /**
359
     * Creates a hard link, or several hard links to a file.
360
     *
361
     * @param string|string[] $targetFiles The target file(s)
362
     *
363
     * @throws FileNotFoundException When original file is missing or not a file
364
     * @throws IOException           When link fails, including if link already exists
365
     */
366
    public function hardlink(string $originFile, string|iterable $targetFiles): void
367
    {
368
        self::assertFunctionExists('link');
369
370
        if (!$this->exists($originFile)) {
371
            throw new FileNotFoundException(null, 0, null, $originFile);
372
        }
373
374
        if (!is_file($originFile)) {
375
            throw new FileNotFoundException(\sprintf('Origin file "%s" is not a file.', $originFile));
376
        }
377
378
        foreach ($this->toIterable($targetFiles) as $targetFile) {
379
            if (is_file($targetFile)) {
0 ignored issues
show
Bug introduced by
It seems like $targetFile can also be of type iterable; however, parameter $filename of is_file() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

379
            if (is_file(/** @scrutinizer ignore-type */ $targetFile)) {
Loading history...
380
                if (fileinode($originFile) === fileinode($targetFile)) {
0 ignored issues
show
Bug introduced by
It seems like $targetFile can also be of type iterable; however, parameter $filename of fileinode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

380
                if (fileinode($originFile) === fileinode(/** @scrutinizer ignore-type */ $targetFile)) {
Loading history...
381
                    continue;
382
                }
383
                $this->remove($targetFile);
384
            }
385
386
            if (!self::box('link', $originFile, $targetFile)) {
387
                $this->linkException($originFile, $targetFile, 'hard');
0 ignored issues
show
Bug introduced by
It seems like $targetFile can also be of type iterable; however, parameter $target of Symfony\Component\Filesy...system::linkException() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

387
                $this->linkException($originFile, /** @scrutinizer ignore-type */ $targetFile, 'hard');
Loading history...
388
            }
389
        }
390
    }
391
392
    /**
393
     * @param string $linkType Name of the link type, typically 'symbolic' or 'hard'
394
     */
395
    private function linkException(string $origin, string $target, string $linkType): never
396
    {
397
        if (self::$lastError) {
398
            if ('\\' === \DIRECTORY_SEPARATOR && str_contains(self::$lastError, 'error code(1314)')) {
399
                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);
400
            }
401
        }
402
        throw new IOException(\sprintf('Failed to create "%s" link from "%s" to "%s": ', $linkType, $origin, $target).self::$lastError, 0, null, $target);
403
    }
404
405
    /**
406
     * Resolves links in paths.
407
     *
408
     * With $canonicalize = false (default)
409
     *      - if $path does not exist or is not a link, returns null
410
     *      - if $path is a link, returns the next direct target of the link without considering the existence of the target
411
     *
412
     * With $canonicalize = true
413
     *      - if $path does not exist, returns null
414
     *      - if $path exists, returns its absolute fully resolved final version
415
     */
416
    public function readlink(string $path, bool $canonicalize = false): ?string
417
    {
418
        if (!$canonicalize && !is_link($path)) {
419
            return null;
420
        }
421
422
        if ($canonicalize) {
423
            if (!$this->exists($path)) {
424
                return null;
425
            }
426
427
            return realpath($path);
428
        }
429
430
        return readlink($path);
431
    }
432
433
    /**
434
     * Given an existing path, convert it to a path relative to a given starting path.
435
     */
436
    public function makePathRelative(string $endPath, string $startPath): string
437
    {
438
        if (!$this->isAbsolutePath($startPath)) {
439
            throw new InvalidArgumentException(\sprintf('The start path "%s" is not absolute.', $startPath));
440
        }
441
442
        if (!$this->isAbsolutePath($endPath)) {
443
            throw new InvalidArgumentException(\sprintf('The end path "%s" is not absolute.', $endPath));
444
        }
445
446
        // Normalize separators on Windows
447
        if ('\\' === \DIRECTORY_SEPARATOR) {
448
            $endPath = str_replace('\\', '/', $endPath);
449
            $startPath = str_replace('\\', '/', $startPath);
450
        }
451
452
        $splitDriveLetter = fn ($path) => (\strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0]))
453
            ? [substr($path, 2), strtoupper($path[0])]
454
            : [$path, null];
455
456
        $splitPath = function ($path) {
457
            $result = [];
458
459
            foreach (explode('/', trim($path, '/')) as $segment) {
460
                if ('..' === $segment) {
461
                    array_pop($result);
462
                } elseif ('.' !== $segment && '' !== $segment) {
463
                    $result[] = $segment;
464
                }
465
            }
466
467
            return $result;
468
        };
469
470
        [$endPath, $endDriveLetter] = $splitDriveLetter($endPath);
471
        [$startPath, $startDriveLetter] = $splitDriveLetter($startPath);
472
473
        $startPathArr = $splitPath($startPath);
474
        $endPathArr = $splitPath($endPath);
475
476
        if ($endDriveLetter && $startDriveLetter && $endDriveLetter != $startDriveLetter) {
477
            // End path is on another drive, so no relative path exists
478
            return $endDriveLetter.':/'.($endPathArr ? implode('/', $endPathArr).'/' : '');
479
        }
480
481
        // Find for which directory the common path stops
482
        $index = 0;
483
        while (isset($startPathArr[$index]) && isset($endPathArr[$index]) && $startPathArr[$index] === $endPathArr[$index]) {
484
            ++$index;
485
        }
486
487
        // Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels)
488
        if (1 === \count($startPathArr) && '' === $startPathArr[0]) {
489
            $depth = 0;
490
        } else {
491
            $depth = \count($startPathArr) - $index;
492
        }
493
494
        // Repeated "../" for each level need to reach the common path
495
        $traverser = str_repeat('../', $depth);
496
497
        $endPathRemainder = implode('/', \array_slice($endPathArr, $index));
498
499
        // Construct $endPath from traversing to the common path, then to the remaining $endPath
500
        $relativePath = $traverser.('' !== $endPathRemainder ? $endPathRemainder.'/' : '');
501
502
        return '' === $relativePath ? './' : $relativePath;
503
    }
504
505
    /**
506
     * Mirrors a directory to another.
507
     *
508
     * Copies files and directories from the origin directory into the target directory. By default:
509
     *
510
     *  - existing files in the target directory will be overwritten, except if they are newer (see the `override` option)
511
     *  - files in the target directory that do not exist in the source directory will not be deleted (see the `delete` option)
512
     *
513
     * @param \Traversable|null $iterator Iterator that filters which files and directories to copy, if null a recursive iterator is created
514
     * @param array             $options  An array of boolean options
515
     *                                    Valid options are:
516
     *                                    - $options['override'] If true, target files newer than origin files are overwritten (see copy(), defaults to false)
517
     *                                    - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink(), defaults to false)
518
     *                                    - $options['delete'] Whether to delete files that are not in the source directory (defaults to false)
519
     *
520
     * @throws IOException When file type is unknown
521
     */
522
    public function mirror(string $originDir, string $targetDir, ?\Traversable $iterator = null, array $options = []): void
523
    {
524
        $targetDir = rtrim($targetDir, '/\\');
525
        $originDir = rtrim($originDir, '/\\');
526
        $originDirLen = \strlen($originDir);
527
528
        if (!$this->exists($originDir)) {
529
            throw new IOException(\sprintf('The origin directory specified "%s" was not found.', $originDir), 0, null, $originDir);
530
        }
531
532
        // Iterate in destination folder to remove obsolete entries
533
        if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) {
534
            $deleteIterator = $iterator;
535
            if (null === $deleteIterator) {
536
                $flags = \FilesystemIterator::SKIP_DOTS;
537
                $deleteIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($targetDir, $flags), \RecursiveIteratorIterator::CHILD_FIRST);
538
            }
539
            $targetDirLen = \strlen($targetDir);
540
            foreach ($deleteIterator as $file) {
541
                $origin = $originDir.substr($file->getPathname(), $targetDirLen);
542
                if (!$this->exists($origin)) {
543
                    $this->remove($file);
544
                }
545
            }
546
        }
547
548
        $copyOnWindows = $options['copy_on_windows'] ?? false;
549
550
        if (null === $iterator) {
551
            $flags = $copyOnWindows ? \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS : \FilesystemIterator::SKIP_DOTS;
552
            $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
553
        }
554
555
        $this->mkdir($targetDir);
556
        $filesCreatedWhileMirroring = [];
557
558
        foreach ($iterator as $file) {
559
            if ($file->getPathname() === $targetDir || $file->getRealPath() === $targetDir || isset($filesCreatedWhileMirroring[$file->getRealPath()])) {
560
                continue;
561
            }
562
563
            $target = $targetDir.substr($file->getPathname(), $originDirLen);
564
            $filesCreatedWhileMirroring[$target] = true;
565
566
            if (!$copyOnWindows && is_link($file)) {
567
                $this->symlink($file->getLinkTarget(), $target);
568
            } elseif (is_dir($file)) {
569
                $this->mkdir($target);
570
            } elseif (is_file($file)) {
571
                $this->copy($file, $target, $options['override'] ?? false);
572
            } else {
573
                throw new IOException(\sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
574
            }
575
        }
576
    }
577
578
    /**
579
     * Returns whether the file path is an absolute path.
580
     */
581
    public function isAbsolutePath(string $file): bool
582
    {
583
        return '' !== $file && (strspn($file, '/\\', 0, 1)
584
            || (\strlen($file) > 3 && ctype_alpha($file[0])
585
                && ':' === $file[1]
586
                && strspn($file, '/\\', 2, 1)
587
            )
588
            || null !== parse_url($file, \PHP_URL_SCHEME)
589
        );
590
    }
591
592
    /**
593
     * Creates a temporary file with support for custom stream wrappers.
594
     *
595
     * @param string $prefix The prefix of the generated temporary filename
596
     *                       Note: Windows uses only the first three characters of prefix
597
     * @param string $suffix The suffix of the generated temporary filename
598
     *
599
     * @return string The new temporary filename (with path), or throw an exception on failure
600
     */
601
    public function tempnam(string $dir, string $prefix, string $suffix = ''): string
602
    {
603
        [$scheme, $hierarchy] = $this->getSchemeAndHierarchy($dir);
604
605
        // If no scheme or scheme is "file" or "gs" (Google Cloud) create temp file in local filesystem
606
        if ((null === $scheme || 'file' === $scheme || 'gs' === $scheme) && '' === $suffix) {
607
            // If tempnam failed or no scheme return the filename otherwise prepend the scheme
608
            if ($tmpFile = self::box('tempnam', $hierarchy, $prefix)) {
609
                if (null !== $scheme && 'gs' !== $scheme) {
610
                    return $scheme.'://'.$tmpFile;
611
                }
612
613
                return $tmpFile;
614
            }
615
616
            throw new IOException('A temporary file could not be created: '.self::$lastError);
617
        }
618
619
        // Loop until we create a valid temp file or have reached 10 attempts
620
        for ($i = 0; $i < 10; ++$i) {
621
            // Create a unique filename
622
            $tmpFile = $dir.'/'.$prefix.bin2hex(random_bytes(4)).$suffix;
623
624
            // Use fopen instead of file_exists as some streams do not support stat
625
            // Use mode 'x+' to atomically check existence and create to avoid a TOCTOU vulnerability
626
            if (!$handle = self::box('fopen', $tmpFile, 'x+')) {
627
                continue;
628
            }
629
630
            // Close the file if it was successfully opened
631
            self::box('fclose', $handle);
632
633
            return $tmpFile;
634
        }
635
636
        throw new IOException('A temporary file could not be created: '.self::$lastError);
637
    }
638
639
    /**
640
     * Atomically dumps content into a file.
641
     *
642
     * @param string|resource $content The data to write into the file
643
     *
644
     * @throws IOException if the file cannot be written to
645
     */
646
    public function dumpFile(string $filename, $content): void
647
    {
648
        if (\is_array($content)) {
0 ignored issues
show
introduced by
The condition is_array($content) is always false.
Loading history...
649
            throw new \TypeError(\sprintf('Argument 2 passed to "%s()" must be string or resource, array given.', __METHOD__));
650
        }
651
652
        $dir = \dirname($filename);
653
654
        if (is_link($filename) && $linkTarget = $this->readlink($filename)) {
655
            $this->dumpFile(Path::makeAbsolute($linkTarget, $dir), $content);
656
657
            return;
658
        }
659
660
        if (!is_dir($dir)) {
661
            $this->mkdir($dir);
662
        }
663
664
        // Will create a temp file with 0600 access rights
665
        // when the filesystem supports chmod.
666
        $tmpFile = $this->tempnam($dir, basename($filename));
667
668
        try {
669
            if (false === self::box('file_put_contents', $tmpFile, $content)) {
670
                throw new IOException(\sprintf('Failed to write file "%s": ', $filename).self::$lastError, 0, null, $filename);
671
            }
672
673
            self::box('chmod', $tmpFile, self::box('fileperms', $filename) ?: 0666 & ~umask());
674
675
            $this->rename($tmpFile, $filename, true);
676
        } finally {
677
            if (file_exists($tmpFile)) {
678
                if ('\\' === \DIRECTORY_SEPARATOR && !is_writable($tmpFile)) {
679
                    self::box('chmod', $tmpFile, self::box('fileperms', $tmpFile) | 0200);
680
                }
681
682
                self::box('unlink', $tmpFile);
683
            }
684
        }
685
    }
686
687
    /**
688
     * Appends content to an existing file.
689
     *
690
     * @param string|resource $content The content to append
691
     * @param bool            $lock    Whether the file should be locked when writing to it
692
     *
693
     * @throws IOException If the file is not writable
694
     */
695
    public function appendToFile(string $filename, $content, bool $lock = false): void
696
    {
697
        if (\is_array($content)) {
0 ignored issues
show
introduced by
The condition is_array($content) is always false.
Loading history...
698
            throw new \TypeError(\sprintf('Argument 2 passed to "%s()" must be string or resource, array given.', __METHOD__));
699
        }
700
701
        $dir = \dirname($filename);
702
703
        if (!is_dir($dir)) {
704
            $this->mkdir($dir);
705
        }
706
707
        if (false === self::box('file_put_contents', $filename, $content, \FILE_APPEND | ($lock ? \LOCK_EX : 0))) {
708
            throw new IOException(\sprintf('Failed to write file "%s": ', $filename).self::$lastError, 0, null, $filename);
709
        }
710
    }
711
712
    /**
713
     * Returns the content of a file as a string.
714
     *
715
     * @throws IOException If the file cannot be read
716
     */
717
    public function readFile(string $filename): string
718
    {
719
        if (is_dir($filename)) {
720
            throw new IOException(\sprintf('Failed to read file "%s": File is a directory.', $filename));
721
        }
722
723
        $content = self::box('file_get_contents', $filename);
724
        if (false === $content) {
725
            throw new IOException(\sprintf('Failed to read file "%s": ', $filename).self::$lastError, 0, null, $filename);
726
        }
727
728
        return $content;
729
    }
730
731
    private function toIterable(string|iterable $files): iterable
732
    {
733
        return is_iterable($files) ? $files : [$files];
734
    }
735
736
    /**
737
     * Gets a 2-tuple of scheme (may be null) and hierarchical part of a filename (e.g. file:///tmp -> [file, tmp]).
738
     */
739
    private function getSchemeAndHierarchy(string $filename): array
740
    {
741
        $components = explode('://', $filename, 2);
742
743
        return 2 === \count($components) ? [$components[0], $components[1]] : [null, $components[0]];
744
    }
745
746
    private static function assertFunctionExists(string $func): void
747
    {
748
        if (!\function_exists($func)) {
749
            throw new IOException(\sprintf('Unable to perform filesystem operation because the "%s()" function has been disabled.', $func));
750
        }
751
    }
752
753
    private static function box(string $func, mixed ...$args): mixed
754
    {
755
        self::assertFunctionExists($func);
756
757
        self::$lastError = null;
758
        set_error_handler(self::handleError(...));
759
        try {
760
            return $func(...$args);
761
        } finally {
762
            restore_error_handler();
763
        }
764
    }
765
766
    /**
767
     * @internal
768
     */
769
    public static function handleError(int $type, string $msg): void
770
    {
771
        self::$lastError = $msg;
772
    }
773
}
774