You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #26771 [Filesystem] Fix mirroring a directory with a relative path and a custom iterator (fxbt)
This PR was submitted for the 2.8 branch but it was squashed and merged into the 4.2-dev branch instead (closes#26771).
Discussion
----------
[Filesystem] Fix mirroring a directory with a relative path and a custom iterator
| Q | A
| ------------- | ---
| Branch? | 2.8 up to 4.1
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets |
| License | MIT
| Doc PR |
The Filesystem::mirror method with a Finder iterator doesn't work if the origin directory is relative.
This is a case where it won't work:
```
$dir = '/data/../data/';
$finder = (new Finder())->in($dir)->getIterator();
(new Filesystem())->mirror($dir, '/tmp', $finder);
```
The finder will return file objects like this :
```
SplFileInfo {
pathname: "/data/file.tmp"
...
}
```
But the following line will fail because because the `$file->getPathname()` and the `$originDir` are differents.
```
$target = $targetDir.substr($file->getPathname(), $originDirLen);
// $file->getPathname() = '/data/file.tmp'
// $originDirLen = strlen('/data/../data/') = 14
// $target = '/tmp' instead of '/tpm/file.tpm'
```
In some case, it's even worse. If the filename length is bigger than the `$originDirLen`, the target file will be a file with a completely wrong name:
```
// $file->getPathname() = '/data/file123456789.tmp'
// $originDirLen = strlen('/data/../data/') = 14
// $target = '/tmp/56789.tmp' instead of '/tpm/file123456789.tmp'
```
I fixed this on my side by using the realpath function everytime i'm calling the mirror method, but i doubt this is the desired behavior.
Commits
-------
27b673c [Filesystem] Fix mirroring a directory with a relative path and a custom iterator
Copy file name to clipboardExpand all lines: src/Symfony/Component/Filesystem/Filesystem.php
+4Lines changed: 4 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -572,6 +572,10 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
572
572
}
573
573
574
574
foreach ($iteratoras$file) {
575
+
if (false === strpos($file->getPath(), $originDir)) {
576
+
thrownewIOException(sprintf('Unable to mirror "%s" directory. If the origin directory is relative, try using "realpath" before calling the mirror method.', $originDir), 0, null, $originDir);
0 commit comments