Skip to content

Commit

Permalink
FileSystem: Exception message should use normalized path.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Apr 13, 2021
1 parent 0e350ef commit 17b2d14
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/Utils/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class FileSystem
public static function createDir(string $dir, int $mode = 0777): void
{
if (!is_dir($dir) && !@mkdir($dir, $mode, true) && !is_dir($dir)) { // @ - dir may already exist
throw new Nette\IOException("Unable to create directory '$dir' with mode " . decoct($mode) . '. ' . Helpers::getLastError());
throw new Nette\IOException("Unable to create directory '" . self::normalizePath($dir) . "' with mode " . decoct($mode) . '. ' . Helpers::getLastError());
}
}

Expand All @@ -39,10 +39,10 @@ public static function createDir(string $dir, int $mode = 0777): void
public static function copy(string $origin, string $target, bool $overwrite = true): void
{
if (stream_is_local($origin) && !file_exists($origin)) {
throw new Nette\IOException("File or directory '$origin' not found.");
throw new Nette\IOException("File or directory '" . self::normalizePath($origin) . "' not found.");

} elseif (!$overwrite && file_exists($target)) {
throw new Nette\InvalidStateException("File or directory '$target' already exists.");
throw new Nette\InvalidStateException("File or directory '" . self::normalizePath($target) . "' already exists.");

} elseif (is_dir($origin)) {
static::createDir($target);
Expand All @@ -64,7 +64,7 @@ public static function copy(string $origin, string $target, bool $overwrite = tr
&& ($d = @fopen($target, 'wb'))
&& @stream_copy_to_stream($s, $d) === false
) { // @ is escalated to exception
throw new Nette\IOException("Unable to copy file '$origin' to '$target'. " . Helpers::getLastError());
throw new Nette\IOException("Unable to copy file '" . self::normalizePath($origin) . "' to '" . self::normalizePath($target) . "'. " . Helpers::getLastError());
}
}
}
Expand All @@ -79,15 +79,15 @@ public static function delete(string $path): void
if (is_file($path) || is_link($path)) {
$func = DIRECTORY_SEPARATOR === '\\' && is_dir($path) ? 'rmdir' : 'unlink';
if (!@$func($path)) { // @ is escalated to exception
throw new Nette\IOException("Unable to delete '$path'. " . Helpers::getLastError());
throw new Nette\IOException("Unable to delete '" . self::normalizePath($path) . "'. " . Helpers::getLastError());
}

} elseif (is_dir($path)) {
foreach (new \FilesystemIterator($path) as $item) {
static::delete($item->getPathname());
}
if (!@rmdir($path)) { // @ is escalated to exception
throw new Nette\IOException("Unable to delete directory '$path'. " . Helpers::getLastError());
throw new Nette\IOException("Unable to delete directory '" . self::normalizePath($path) . "'. " . Helpers::getLastError());
}
}
}
Expand All @@ -101,18 +101,18 @@ public static function delete(string $path): void
public static function rename(string $origin, string $target, bool $overwrite = true): void
{
if (!$overwrite && file_exists($target)) {
throw new Nette\InvalidStateException("File or directory '$target' already exists.");
throw new Nette\InvalidStateException("File or directory '" . self::normalizePath($target) . "' already exists.");

} elseif (!file_exists($origin)) {
throw new Nette\IOException("File or directory '$origin' not found.");
throw new Nette\IOException("File or directory '" . self::normalizePath($origin) . "' not found.");

} else {
static::createDir(dirname($target));
if (realpath($origin) !== realpath($target)) {
static::delete($target);
}
if (!@rename($origin, $target)) { // @ is escalated to exception
throw new Nette\IOException("Unable to rename file or directory '$origin' to '$target'. " . Helpers::getLastError());
throw new Nette\IOException("Unable to rename file or directory '" . self::normalizePath($origin) . "' to '" . self::normalizePath($target) . "'. " . Helpers::getLastError());
}
}
}
Expand All @@ -126,7 +126,7 @@ public static function read(string $file): string
{
$content = @file_get_contents($file); // @ is escalated to exception
if ($content === false) {
throw new Nette\IOException("Unable to read file '$file'. " . Helpers::getLastError());
throw new Nette\IOException("Unable to read file '" . self::normalizePath($file) . "'. " . Helpers::getLastError());
}
return $content;
}
Expand All @@ -140,10 +140,10 @@ public static function write(string $file, string $content, ?int $mode = 0666):
{
static::createDir(dirname($file));
if (@file_put_contents($file, $content) === false) { // @ is escalated to exception
throw new Nette\IOException("Unable to write file '$file'. " . Helpers::getLastError());
throw new Nette\IOException("Unable to write file '" . self::normalizePath($file) . "'. " . Helpers::getLastError());
}
if ($mode !== null && !@chmod($file, $mode)) { // @ is escalated to exception
throw new Nette\IOException("Unable to chmod file '$file' to mode " . decoct($mode) . '. ' . Helpers::getLastError());
throw new Nette\IOException("Unable to chmod file '" . self::normalizePath($file) . "' to mode " . decoct($mode) . '. ' . Helpers::getLastError());
}
}

Expand Down

0 comments on commit 17b2d14

Please sign in to comment.