Skip to content

Commit a377265

Browse files
committed
fix: Directory separator inconsistencies causing bad resolution of internal/external path.
1 parent 747a0e0 commit a377265

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

src/FireFS/FireFS.php

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,12 @@ public function cleanPath(string $path): string
207207
}
208208
}
209209

210-
if ($path != '/') {
211-
$beautifiedPath = implode(DIRECTORY_SEPARATOR, $cleanDirs);
210+
if ($path != DIRECTORY_SEPARATOR) {
211+
$beautifiedPath = $this->makePath(...$cleanDirs);
212212
}
213213

214214
if (empty($beautifiedPath)) {
215-
$beautifiedPath = (substr($path, 0, 1) == '/') ? '/' : '.';
215+
$beautifiedPath = (substr($path, 0, 1) == DIRECTORY_SEPARATOR) ? DIRECTORY_SEPARATOR : '.';
216216
}
217217

218218
return $beautifiedPath;
@@ -275,7 +275,7 @@ public function getAliases(): array
275275
*/
276276
public function newAlias(string $key, string $val)
277277
{
278-
if (substr($val, -1) == '/') {
278+
if (substr($val, -1) == DIRECTORY_SEPARATOR) {
279279
$val = substr($val, 0, -1);
280280
}
281281

@@ -324,17 +324,28 @@ public function toInternalPath(string $path): string
324324
return $internalPath;
325325
}
326326

327+
// Remove root path
327328
if (substr($internalPath, 0, strlen($this->rootPath())) == $this->rootPath()) {
328-
$internalPath = str_replace($this->rootPath(), './', $internalPath);
329+
$internalPath = substr($internalPath, strlen($this->rootPath()));
329330
} else {
330331
$realRootPath = realpath($this->rootPath());
331332
if (substr($internalPath, 0, strlen($realRootPath)) == $realRootPath) {
332333
$internalPath = substr($internalPath, strlen($realRootPath));
333334
}
334335
}
335336

336-
// Convert relative path to absolute path
337-
if (preg_match('#^(\.)+/#', $internalPath)) {
337+
// Remove workdir if any
338+
if (substr($internalPath, 0, strlen($this->workingDir())) == $this->workingDir()) {
339+
$internalPath = str_replace($this->workingDir(), '.', $internalPath);
340+
} else {
341+
$realPath = realpath($this->workingDir());
342+
if (substr($internalPath, 0, strlen($realPath)) == $realPath) {
343+
$internalPath = substr($internalPath, strlen($realPath));
344+
}
345+
}
346+
347+
// Append workdir
348+
if (preg_match('#^(\.)+\\' . DIRECTORY_SEPARATOR . '#', $internalPath)) {
338349
$internalPath = $this->makePath($this->workingDir(), $internalPath);
339350
}
340351

@@ -548,26 +559,13 @@ protected function pathInfo(string $path, int $info): string
548559
return "file";
549560
}
550561
}
551-
break;
552562

553563
case PATHINFO_FILENAME :
554564
$basename = basename($internalPath);
555565
return substr($basename, 0, strrpos($basename, '.'));
556566

557567
case PATHINFO_DIRNAME:
558-
if (strpos($path, "/", 1) !== false) {
559-
$dirname = preg_replace('#/[^/]*/?$#', '', $path);
560-
} elseif (strpos($path, "/") === 0) {
561-
$dirname = "/";
562-
} else {
563-
$dirname = false;
564-
}
565-
566-
if ($dirname == ".") {
567-
$dirname = false;
568-
}
569-
570-
return $dirname;
568+
return dirname($path);
571569

572570
default:
573571
return "unknown";
@@ -847,6 +845,7 @@ public function toExternalPath(string $internalPath): string
847845
return $externalPath;
848846
}
849847

848+
// Remove root path
850849
if (substr($externalPath, 0, strlen($this->rootPath())) == $this->rootPath()) {
851850
$externalPath = substr($externalPath, strlen($this->rootPath()));
852851
} else {
@@ -856,21 +855,22 @@ public function toExternalPath(string $internalPath): string
856855
}
857856
}
858857

858+
// If the internal path was the root path, set it to the dot slash notation
859859
if (strlen($externalPath) === 0) {
860860
return $this->cleanPath("./");
861861
}
862862

863-
if ($externalPath[0] != '/') {
863+
if ($externalPath[0] != DIRECTORY_SEPARATOR) {
864864
return $internalPath;
865865
}
866866

867867
$nbrTurns = 0;
868868
$maxNbrTurns = count($this->aliases);
869-
do {
870-
$appliedAliasesNbr = 0;
869+
$appliedAliasesNbr = 0;
871870

871+
do {
872872
foreach ($this->aliases as $key => $value) {
873-
$value = '/' . $value;
873+
$value = DIRECTORY_SEPARATOR . $value;
874874
if (substr($externalPath, 0, strlen($value)) == $value) {
875875
$externalPath = $this->makePath($key, substr($externalPath, strlen($value)));
876876
$appliedAliasesNbr++;
@@ -880,6 +880,9 @@ public function toExternalPath(string $internalPath): string
880880
$nbrTurns++;
881881
} while ($appliedAliasesNbr > 0 && $nbrTurns <= $maxNbrTurns);
882882

883+
if ($appliedAliasesNbr == 0 && $externalPath[0] == DIRECTORY_SEPARATOR)
884+
$externalPath = $this->makePath( '.', $externalPath);
885+
883886
return $this->cleanPath($externalPath);
884887
}
885888

@@ -924,7 +927,7 @@ public function toFileSystemPath(string $internalPath): string
924927
$nbrTurns++;
925928
} while ($appliedAliasesNbr > 0 && $nbrTurns <= $maxNbrTurns);
926929

927-
while (substr($externalPath, 0, 1) == '/' || substr($externalPath, 0, 2) == './') {
930+
while (substr($externalPath, 0, 1) == DIRECTORY_SEPARATOR || substr($externalPath, 0, 2) == '.' . DIRECTORY_SEPARATOR) {
928931
$externalPath = substr($externalPath, 1);
929932
}
930933

0 commit comments

Comments
 (0)