Skip to content

Commit

Permalink
Merge 088b6db into f3d47a3
Browse files Browse the repository at this point in the history
  • Loading branch information
enumag committed May 8, 2020
2 parents f3d47a3 + 088b6db commit a096396
Show file tree
Hide file tree
Showing 9 changed files with 491 additions and 723 deletions.
201 changes: 43 additions & 158 deletions src/BlockingDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,155 +58,6 @@ public function stat(string $path): Promise
return new Success($stat);
}

/**
* {@inheritdoc}
*/
public function exists(string $path): Promise
{
if ($exists = @\file_exists($path)) {
\clearstatcache(true, $path);
}

return new Success($exists);
}

/**
* Retrieve the size in bytes of the file at the specified path.
*
* If the path does not exist or is not a regular file this
* function's returned Promise WILL resolve as a failure.
*
* @param string $path An absolute file system path
* @return \Amp\Promise<int>
*/
public function size(string $path): Promise
{
if (!@\file_exists($path)) {
return new Failure(new FilesystemException(
"Path does not exist"
));
}

if (!@\is_file($path)) {
return new Failure(new FilesystemException(
"Path is not a regular file"
));
}

if (($size = @\filesize($path)) === false) {
$message = 'Could not open the file.';
if ($error = \error_get_last()) {
$message .= \sprintf(" Errno: %d; %s", $error["type"], $error["message"]);
}

return new Failure(new FilesystemException($message));
}

\clearstatcache(true, $path);
return new Success($size);
}

/**
* Does the specified path exist and is it a directory?
*
* If the path does not exist the returned Promise will resolve
* to FALSE. It will NOT reject with an error.
*
* @param string $path An absolute file system path
* @return \Amp\Promise<bool>
*/
public function isdir(string $path): Promise
{
if (!@\file_exists($path)) {
return new Success(false);
}

$isDir = @\is_dir($path);
\clearstatcache(true, $path);

return new Success($isDir);
}

/**
* Does the specified path exist and is it a file?
*
* If the path does not exist the returned Promise will resolve
* to FALSE. It will NOT reject with an error.
*
* @param string $path An absolute file system path
* @return \Amp\Promise<bool>
*/
public function isfile(string $path): Promise
{
if (!@\file_exists($path)) {
return new Success(false);
}

$isFile = @\is_file($path);
\clearstatcache(true, $path);

return new Success($isFile);
}

/**
* Retrieve the path's last modification time as a unix timestamp.
*
* @param string $path An absolute file system path
* @return \Amp\Promise<int>
*/
public function mtime(string $path): Promise
{
if (!@\file_exists($path)) {
return new Failure(new FilesystemException(
"Path does not exist"
));
}

$mtime = @\filemtime($path);
\clearstatcache(true, $path);

return new Success($mtime);
}

/**
* Retrieve the path's last access time as a unix timestamp.
*
* @param string $path An absolute file system path
* @return \Amp\Promise<int>
*/
public function atime(string $path): Promise
{
if (!@\file_exists($path)) {
return new Failure(new FilesystemException(
"Path does not exist"
));
}
$atime = @\fileatime($path);
\clearstatcache(true, $path);

return new Success($atime);
}

/**
* Retrieve the path's creation time as a unix timestamp.
*
* @param string $path An absolute file system path
* @return \Amp\Promise<int>
*/
public function ctime(string $path): Promise
{
if (!@\file_exists($path)) {
return new Failure(new FilesystemException(
"Path does not exist"
));
}

$ctime = @\filectime($path);
\clearstatcache(true, $path);

return new Success($ctime);
}

/**
* {@inheritdoc}
*/
Expand All @@ -230,7 +81,7 @@ public function symlink(string $target, string $link): Promise
return new Failure(new FilesystemException("Could not create symbolic link"));
}

return new Success(true);
return new Success();
}

/**
Expand All @@ -242,7 +93,7 @@ public function link(string $target, string $link): Promise
return new Failure(new FilesystemException("Could not create hard link"));
}

return new Success(true);
return new Success();
}

/**
Expand All @@ -266,7 +117,7 @@ public function rename(string $from, string $to): Promise
return new Failure(new FilesystemException("Could not rename file"));
}

return new Success(true);
return new Success();
}

/**
Expand All @@ -275,15 +126,24 @@ public function rename(string $from, string $to): Promise
public function unlink(string $path): Promise
{
StatCache::clear($path);
return new Success((bool) @\unlink($path));

if (!@\unlink($path)) {
return new Failure(new FilesystemException("Could not unlink file"));
}

return new Success();
}

/**
* {@inheritdoc}
*/
public function mkdir(string $path, int $mode = 0777, bool $recursive = false): Promise
{
return new Success((bool) @\mkdir($path, $mode, $recursive));
if (!@\mkdir($path, $mode, $recursive)) {
return new Failure(new FilesystemException("Could not create directory"));
}

return new Success();
}

/**
Expand All @@ -292,7 +152,12 @@ public function mkdir(string $path, int $mode = 0777, bool $recursive = false):
public function rmdir(string $path): Promise
{
StatCache::clear($path);
return new Success((bool) @\rmdir($path));

if (!@\rmdir($path)) {
return new Failure(new FilesystemException("Could not remove directory"));
}

return new Success();
}

/**
Expand Down Expand Up @@ -322,7 +187,13 @@ public function scandir(string $path): Promise
*/
public function chmod(string $path, int $mode): Promise
{
return new Success((bool) @\chmod($path, $mode));
if (!@\chmod($path, $mode)) {
return new Failure(new FilesystemException("Could not change file permissions"));
}

StatCache::clear();

return new Success();
}

/**
Expand All @@ -348,7 +219,9 @@ public function chown(string $path, int $uid, int $gid): Promise
return new Failure(new FilesystemException($message));
}

return new Success;
StatCache::clear();

return new Success();
}

/**
Expand All @@ -358,7 +231,17 @@ public function touch(string $path, int $time = null, int $atime = null): Promis
{
$time = $time ?? \time();
$atime = $atime ?? $time;
return new Success((bool) \touch($path, $time, $atime));
if (! @\touch($path, $time, $atime)) {
$message = 'Could not touch file.';
if ($error = \error_get_last()) {
$message .= \sprintf(" Errno: %d; %s", $error["type"], $error["message"]);
}
return new Failure(new FilesystemException($message));
}

StatCache::clear();

return new Success();
}

/**
Expand All @@ -374,6 +257,7 @@ public function get(string $path): Promise
}
return new Failure(new FilesystemException($message));
}

return new Success($result);
}

Expand All @@ -390,6 +274,7 @@ public function put(string $path, string $contents): Promise
}
return new Failure(new FilesystemException($message));
}

return new Success($result);
}
}

0 comments on commit a096396

Please sign in to comment.