2016-12-30 03:59:59 +01:00
|
|
|
<?php
|
2015-07-11 03:59:39 +02:00
|
|
|
|
2015-08-05 16:55:56 +02:00
|
|
|
namespace Amp\File;
|
2015-07-11 03:59:39 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
class BlockingDriver implements Driver
|
|
|
|
{
|
|
|
|
/** @inheritdoc */
|
|
|
|
public function open(string $path, string $mode): Handle
|
|
|
|
{
|
2017-10-30 05:10:35 +01:00
|
|
|
$mode = \str_replace(['b', 't', 'e'], '', $mode);
|
|
|
|
|
|
|
|
switch ($mode) {
|
|
|
|
case "r":
|
|
|
|
case "r+":
|
|
|
|
case "w":
|
|
|
|
case "w+":
|
|
|
|
case "a":
|
|
|
|
case "a+":
|
|
|
|
case "x":
|
|
|
|
case "x+":
|
|
|
|
case "c":
|
|
|
|
case "c+":
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \Error("Invalid file mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$fh = \fopen($path, $mode . 'be')) {
|
2018-07-27 21:00:26 +02:00
|
|
|
throw new FilesystemException("Failed opening file handle");
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
return new BlockingHandle($fh, $path, $mode);
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function stat(string $path): ?array
|
|
|
|
{
|
2015-08-29 13:34:18 +02:00
|
|
|
if ($stat = StatCache::get($path)) {
|
2018-07-27 21:00:26 +02:00
|
|
|
return $stat;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stat = @\stat($path)) {
|
2015-08-08 16:09:07 +02:00
|
|
|
StatCache::set($path, $stat);
|
2015-07-30 15:07:01 +02:00
|
|
|
\clearstatcache(true, $path);
|
2018-07-27 21:00:26 +02:00
|
|
|
|
|
|
|
return $stat;
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
return null;
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function exists(string $path): bool
|
|
|
|
{
|
2015-08-08 16:09:07 +02:00
|
|
|
if ($exists = @\file_exists($path)) {
|
|
|
|
\clearstatcache(true, $path);
|
|
|
|
}
|
2017-06-17 23:41:57 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
return $exists;
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function size(string $path): int
|
|
|
|
{
|
2015-08-08 16:09:07 +02:00
|
|
|
if (!@\file_exists($path)) {
|
2018-07-27 21:00:26 +02:00
|
|
|
throw new FilesystemException("Path does not exist");
|
2017-06-17 23:41:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!@\is_file($path)) {
|
2018-07-27 21:00:26 +02:00
|
|
|
throw new FilesystemException("Path is not a regular file");
|
2017-06-17 23:41:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (($size = @\filesize($path)) === false) {
|
2018-07-27 21:00:26 +02:00
|
|
|
throw new FilesystemException(\error_get_last()["message"]);
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
2017-06-17 23:41:57 +02:00
|
|
|
|
|
|
|
\clearstatcache(true, $path);
|
2018-07-27 21:00:26 +02:00
|
|
|
|
|
|
|
return $size;
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function isDir(string $path): bool
|
|
|
|
{
|
2015-08-08 16:09:07 +02:00
|
|
|
if (!@\file_exists($path)) {
|
2018-07-27 21:00:26 +02:00
|
|
|
return false;
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
2017-06-17 23:41:57 +02:00
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
$isDir = @\is_dir($path);
|
2015-10-25 02:50:30 +01:00
|
|
|
\clearstatcache(true, $path);
|
2015-08-08 16:09:07 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
return $isDir;
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function isFile(string $path): bool
|
|
|
|
{
|
2015-08-08 16:09:07 +02:00
|
|
|
if (!@\file_exists($path)) {
|
2018-07-27 21:00:26 +02:00
|
|
|
return false;
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
2017-06-17 23:41:57 +02:00
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
$isFile = @\is_file($path);
|
2015-10-25 02:50:30 +01:00
|
|
|
\clearstatcache(true, $path);
|
2015-08-08 16:09:07 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
return $isFile;
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function mtime(string $path): int
|
|
|
|
{
|
2015-08-08 16:09:07 +02:00
|
|
|
if (!@\file_exists($path)) {
|
2018-07-27 21:00:26 +02:00
|
|
|
throw new FilesystemException("Path does not exist");
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
2017-06-17 23:41:57 +02:00
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
$mtime = @\filemtime($path);
|
2015-10-25 02:50:30 +01:00
|
|
|
\clearstatcache(true, $path);
|
2015-08-08 16:09:07 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
return $mtime;
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function atime(string $path): int
|
|
|
|
{
|
2015-08-08 16:09:07 +02:00
|
|
|
if (!@\file_exists($path)) {
|
2018-07-27 21:00:26 +02:00
|
|
|
throw new FilesystemException("Path does not exist");
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
2018-07-27 21:00:26 +02:00
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
$atime = @\fileatime($path);
|
2015-10-25 02:50:30 +01:00
|
|
|
\clearstatcache(true, $path);
|
2015-08-08 16:09:07 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
return $atime;
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function ctime(string $path): int
|
|
|
|
{
|
2015-08-08 16:09:07 +02:00
|
|
|
if (!@\file_exists($path)) {
|
2018-07-27 21:00:26 +02:00
|
|
|
throw new FilesystemException("Path does not exist");
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
2017-06-17 23:41:57 +02:00
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
$ctime = @\filectime($path);
|
2015-10-25 02:50:30 +01:00
|
|
|
\clearstatcache(true, $path);
|
2015-08-08 16:09:07 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
return $ctime;
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function lstat(string $path): ?array
|
|
|
|
{
|
2015-07-30 15:07:01 +02:00
|
|
|
if ($stat = @\lstat($path)) {
|
|
|
|
\clearstatcache(true, $path);
|
2018-07-27 21:00:26 +02:00
|
|
|
|
|
|
|
return $stat;
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
return null;
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function symlink(string $target, string $link): void
|
|
|
|
{
|
2016-08-24 06:55:06 +02:00
|
|
|
if (!@\symlink($target, $link)) {
|
2018-07-27 21:00:26 +02:00
|
|
|
throw new FilesystemException("Could not create symbolic link");
|
2016-08-24 06:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function link(string $target, string $link): void
|
|
|
|
{
|
2016-08-24 06:55:06 +02:00
|
|
|
if (!@\link($target, $link)) {
|
2018-07-27 21:00:26 +02:00
|
|
|
throw new FilesystemException("Could not create hard link");
|
2016-08-24 06:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function readlink(string $path): string
|
|
|
|
{
|
|
|
|
if (false === ($result = @\readlink($path))) {
|
|
|
|
throw new FilesystemException("Could not read symbolic link");
|
2016-08-24 06:55:06 +02:00
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
return $result;
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function rename(string $from, string $to): void
|
|
|
|
{
|
2016-08-24 06:55:06 +02:00
|
|
|
if (!@\rename($from, $to)) {
|
2018-07-27 21:00:26 +02:00
|
|
|
throw new FilesystemException("Could not rename file");
|
2016-08-24 06:55:06 +02:00
|
|
|
}
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function unlink(string $path): void
|
|
|
|
{
|
2015-09-09 15:19:40 +02:00
|
|
|
StatCache::clear($path);
|
2018-07-27 21:00:26 +02:00
|
|
|
@\unlink($path);
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function mkdir(string $path, int $mode = 0777, bool $recursive = false): void
|
|
|
|
{
|
|
|
|
if (!\mkdir($path, $mode, $recursive) && !\is_dir($path)) {
|
2018-07-27 21:00:56 +02:00
|
|
|
throw new FilesystemException(\sprintf('Directory "%s" was not created', $path));
|
2018-07-27 21:00:26 +02:00
|
|
|
}
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function rmdir(string $path): void
|
|
|
|
{
|
2015-09-09 15:19:40 +02:00
|
|
|
StatCache::clear($path);
|
2018-07-27 21:00:26 +02:00
|
|
|
@\rmdir($path);
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function scandir(string $path): array
|
|
|
|
{
|
2015-08-08 16:09:07 +02:00
|
|
|
if (!@\is_dir($path)) {
|
2018-07-27 21:00:26 +02:00
|
|
|
throw new FilesystemException("Not a directory");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($entries = @\scandir($path, SCANDIR_SORT_NONE)) {
|
2015-07-30 15:07:01 +02:00
|
|
|
\clearstatcache(true, $path);
|
2018-07-27 21:00:26 +02:00
|
|
|
|
|
|
|
return \array_values(\array_filter($entries, function ($entry) {
|
|
|
|
return !($entry === "." || $entry === "..");
|
|
|
|
}));
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
2017-06-17 23:41:57 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
throw new FilesystemException("Failed reading contents from {$path}");
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function chmod(string $path, int $mode): void
|
|
|
|
{
|
|
|
|
@\chmod($path, $mode);
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function chown(string $path, int $uid, int $gid): void
|
|
|
|
{
|
2015-12-09 21:45:28 +01:00
|
|
|
if ($uid !== -1 && !@\chown($path, $uid)) {
|
2018-07-27 21:00:26 +02:00
|
|
|
throw new FilesystemException(\error_get_last()["message"]);
|
2015-12-09 21:45:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($gid !== -1 && !@\chgrp($path, $gid)) {
|
2018-07-27 21:00:26 +02:00
|
|
|
throw new FilesystemException(\error_get_last()["message"]);
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function touch(string $path, int $time = null, int $atime = null): void
|
|
|
|
{
|
2017-11-23 03:55:30 +01:00
|
|
|
$time = $time ?? \time();
|
|
|
|
$atime = $atime ?? $time;
|
2018-07-27 21:00:26 +02:00
|
|
|
|
|
|
|
/** @noinspection PotentialMalwareInspection */
|
|
|
|
\touch($path, $time, $atime);
|
2015-07-18 20:53:46 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function get(string $path): string
|
|
|
|
{
|
2015-07-30 15:07:01 +02:00
|
|
|
$result = @\file_get_contents($path);
|
2018-07-27 21:00:26 +02:00
|
|
|
|
|
|
|
if ($result === false) {
|
|
|
|
throw new FilesystemException(\error_get_last()["message"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
/** @inheritdoc */
|
|
|
|
public function put(string $path, string $contents): void
|
|
|
|
{
|
2015-07-30 15:07:01 +02:00
|
|
|
$result = @\file_put_contents($path, $contents);
|
2018-07-27 21:00:26 +02:00
|
|
|
|
|
|
|
if ($result === false) {
|
|
|
|
throw new FilesystemException(\error_get_last()["message"]);
|
|
|
|
}
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
}
|