2015-07-10 21:59:39 -04:00
|
|
|
<?php
|
|
|
|
|
2015-08-05 10:55:56 -04:00
|
|
|
namespace Amp\File;
|
2015-07-10 21:59:39 -04:00
|
|
|
|
2015-07-17 10:27:38 -04:00
|
|
|
use Amp\Success;
|
|
|
|
use Amp\Failure;
|
2015-07-10 21:59:39 -04:00
|
|
|
|
2015-07-30 10:10:53 -04:00
|
|
|
class BlockingDriver implements Driver {
|
2015-08-12 19:02:41 -04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function open($path, $mode) {
|
|
|
|
if (!$fh = \fopen($path, $mode)) {
|
|
|
|
return new Failure(new FilesystemException(
|
|
|
|
"Failed opening file handle"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Success(new BlockingHandle($fh, $path, $mode));
|
|
|
|
}
|
|
|
|
|
2015-07-10 21:59:39 -04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-07-17 10:27:38 -04:00
|
|
|
public function stat($path) {
|
2015-08-29 07:34:18 -04:00
|
|
|
if ($stat = StatCache::get($path)) {
|
|
|
|
return new Success($stat);
|
|
|
|
} elseif ($stat = @\stat($path)) {
|
2015-08-08 10:09:07 -04:00
|
|
|
StatCache::set($path, $stat);
|
2015-07-30 09:07:01 -04:00
|
|
|
\clearstatcache(true, $path);
|
2015-07-10 21:59:39 -04:00
|
|
|
} else {
|
|
|
|
$stat = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Success($stat);
|
|
|
|
}
|
|
|
|
|
2015-08-08 10:09:07 -04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function exists($path) {
|
|
|
|
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
|
2016-07-21 01:33:03 +02:00
|
|
|
* @return \Interop\Async\Awaitable<int>
|
2015-08-08 10:09:07 -04:00
|
|
|
*/
|
|
|
|
public function size($path) {
|
|
|
|
if (!@\file_exists($path)) {
|
|
|
|
return new Failure(new FilesystemException(
|
|
|
|
"Path does not exist"
|
|
|
|
));
|
|
|
|
} elseif (!@\is_file($path)) {
|
|
|
|
return new Failure(new FilesystemException(
|
|
|
|
"Path is not a regular file"
|
|
|
|
));
|
|
|
|
} elseif (($size = @\filesize($path)) === false) {
|
|
|
|
return new Failure(new FilesystemException(
|
|
|
|
\error_get_last()["message"]
|
|
|
|
));
|
|
|
|
} else {
|
2015-10-25 02:50:30 +01:00
|
|
|
\clearstatcache(true, $path);
|
2015-08-08 10:09:07 -04:00
|
|
|
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
|
2016-07-21 01:33:03 +02:00
|
|
|
* @return \Interop\Async\Awaitable<bool>
|
2015-08-08 10:09:07 -04:00
|
|
|
*/
|
|
|
|
public function isdir($path) {
|
|
|
|
if (!@\file_exists($path)) {
|
|
|
|
return new Success(false);
|
|
|
|
}
|
|
|
|
$isDir = @\is_dir($path);
|
2015-10-25 02:50:30 +01:00
|
|
|
\clearstatcache(true, $path);
|
2015-08-08 10:09:07 -04:00
|
|
|
|
|
|
|
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
|
2016-07-21 01:33:03 +02:00
|
|
|
* @return \Interop\Async\Awaitable<bool>
|
2015-08-08 10:09:07 -04:00
|
|
|
*/
|
|
|
|
public function isfile($path) {
|
|
|
|
if (!@\file_exists($path)) {
|
|
|
|
return new Success(false);
|
|
|
|
}
|
|
|
|
$isFile = @\is_file($path);
|
2015-10-25 02:50:30 +01:00
|
|
|
\clearstatcache(true, $path);
|
2015-08-08 10:09:07 -04:00
|
|
|
|
|
|
|
return new Success($isFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the path's last modification time as a unix timestamp
|
|
|
|
*
|
|
|
|
* @param string $path An absolute file system path
|
2016-07-21 01:33:03 +02:00
|
|
|
* @return \Interop\Async\Awaitable<int>
|
2015-08-08 10:09:07 -04:00
|
|
|
*/
|
|
|
|
public function mtime($path) {
|
|
|
|
if (!@\file_exists($path)) {
|
|
|
|
return new Failure(new FilesystemException(
|
|
|
|
"Path does not exist"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
$mtime = @\filemtime($path);
|
2015-10-25 02:50:30 +01:00
|
|
|
\clearstatcache(true, $path);
|
2015-08-08 10:09:07 -04:00
|
|
|
|
|
|
|
return new Success($mtime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the path's last access time as a unix timestamp
|
|
|
|
*
|
|
|
|
* @param string $path An absolute file system path
|
2016-07-21 01:33:03 +02:00
|
|
|
* @return \Interop\Async\Awaitable<int>
|
2015-08-08 10:09:07 -04:00
|
|
|
*/
|
|
|
|
public function atime($path) {
|
|
|
|
if (!@\file_exists($path)) {
|
|
|
|
return new Failure(new FilesystemException(
|
|
|
|
"Path does not exist"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
$atime = @\fileatime($path);
|
2015-10-25 02:50:30 +01:00
|
|
|
\clearstatcache(true, $path);
|
2015-08-08 10:09:07 -04:00
|
|
|
|
|
|
|
return new Success($atime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the path's creation time as a unix timestamp
|
|
|
|
*
|
|
|
|
* @param string $path An absolute file system path
|
2016-07-21 01:33:03 +02:00
|
|
|
* @return \Interop\Async\Awaitable<int>
|
2015-08-08 10:09:07 -04:00
|
|
|
*/
|
|
|
|
public function ctime($path) {
|
|
|
|
if (!@\file_exists($path)) {
|
|
|
|
return new Failure(new FilesystemException(
|
|
|
|
"Path does not exist"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
$ctime = @\filectime($path);
|
2015-10-25 02:50:30 +01:00
|
|
|
\clearstatcache(true, $path);
|
2015-08-08 10:09:07 -04:00
|
|
|
|
|
|
|
return new Success($ctime);
|
|
|
|
}
|
|
|
|
|
2015-07-10 21:59:39 -04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-07-17 10:27:38 -04:00
|
|
|
public function lstat($path) {
|
2015-07-30 09:07:01 -04:00
|
|
|
if ($stat = @\lstat($path)) {
|
|
|
|
\clearstatcache(true, $path);
|
2015-07-10 21:59:39 -04:00
|
|
|
} else {
|
|
|
|
$stat = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Success($stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-07-17 10:27:38 -04:00
|
|
|
public function symlink($target, $link) {
|
2015-08-08 10:09:07 -04:00
|
|
|
return new Success((bool) @\symlink($target, $link));
|
2015-07-10 21:59:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-07-17 10:27:38 -04:00
|
|
|
public function rename($from, $to) {
|
2015-08-08 10:09:07 -04:00
|
|
|
return new Success((bool) @\rename($from, $to));
|
2015-07-10 21:59:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-07-17 10:27:38 -04:00
|
|
|
public function unlink($path) {
|
2015-09-09 09:19:40 -04:00
|
|
|
StatCache::clear($path);
|
2015-08-08 10:09:07 -04:00
|
|
|
return new Success((bool) @\unlink($path));
|
2015-07-10 21:59:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-07-17 10:27:38 -04:00
|
|
|
public function mkdir($path, $mode = 0644) {
|
2015-08-08 10:09:07 -04:00
|
|
|
return new Success((bool) @\mkdir($path, $mode));
|
2015-07-10 21:59:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-07-17 10:27:38 -04:00
|
|
|
public function rmdir($path) {
|
2015-09-09 09:19:40 -04:00
|
|
|
StatCache::clear($path);
|
2015-08-08 10:09:07 -04:00
|
|
|
return new Success((bool) @\rmdir($path));
|
2015-07-10 21:59:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-07-17 10:27:38 -04:00
|
|
|
public function scandir($path) {
|
2015-08-08 10:09:07 -04:00
|
|
|
if (!@\is_dir($path)) {
|
|
|
|
return new Failure(new FilesystemException(
|
|
|
|
"Not a directory"
|
|
|
|
));
|
|
|
|
} elseif ($arr = @\scandir($path)) {
|
|
|
|
$arr = \array_values(\array_filter($arr, function($el) {
|
2015-07-10 21:59:39 -04:00
|
|
|
return !($el === "." || $el === "..");
|
|
|
|
}));
|
2015-07-30 09:07:01 -04:00
|
|
|
\clearstatcache(true, $path);
|
2015-07-10 21:59:39 -04:00
|
|
|
return new Success($arr);
|
|
|
|
} else {
|
2015-08-08 10:09:07 -04:00
|
|
|
return new Failure(new FilesystemException(
|
2015-07-10 21:59:39 -04:00
|
|
|
"Failed reading contents from {$path}"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-07-17 10:27:38 -04:00
|
|
|
public function chmod($path, $mode) {
|
2015-08-08 10:09:07 -04:00
|
|
|
return new Success((bool) @\chmod($path, $mode));
|
2015-07-10 21:59:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-07-17 10:27:38 -04:00
|
|
|
public function chown($path, $uid, $gid) {
|
2015-12-09 21:45:28 +01:00
|
|
|
if ($uid !== -1 && !@\chown($path, $uid)) {
|
2015-08-08 10:09:07 -04:00
|
|
|
return new Failure(new FilesystemException(
|
2015-07-30 09:07:01 -04:00
|
|
|
\error_get_last()["message"]
|
2015-07-10 21:59:39 -04:00
|
|
|
));
|
2015-12-09 21:45:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($gid !== -1 && !@\chgrp($path, $gid)) {
|
2015-08-08 10:09:07 -04:00
|
|
|
return new Failure(new FilesystemException(
|
2015-07-30 09:07:01 -04:00
|
|
|
\error_get_last()["message"]
|
2015-07-10 21:59:39 -04:00
|
|
|
));
|
|
|
|
}
|
2015-12-09 21:45:28 +01:00
|
|
|
|
|
|
|
return new Success;
|
2015-07-10 21:59:39 -04:00
|
|
|
}
|
|
|
|
|
2015-07-18 14:53:46 -04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function touch($path) {
|
2015-07-30 09:07:01 -04:00
|
|
|
return new Success((bool) \touch($path));
|
2015-07-18 14:53:46 -04:00
|
|
|
}
|
|
|
|
|
2015-07-10 21:59:39 -04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-07-17 10:27:38 -04:00
|
|
|
public function get($path) {
|
2015-07-30 09:07:01 -04:00
|
|
|
$result = @\file_get_contents($path);
|
2015-07-10 21:59:39 -04:00
|
|
|
return ($result === false)
|
2015-08-08 10:09:07 -04:00
|
|
|
? new Failure(new FilesystemException(\error_get_last()["message"]))
|
2015-07-10 21:59:39 -04:00
|
|
|
: new Success($result)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-07-17 10:27:38 -04:00
|
|
|
public function put($path, $contents) {
|
2015-07-30 09:07:01 -04:00
|
|
|
$result = @\file_put_contents($path, $contents);
|
2015-07-10 21:59:39 -04:00
|
|
|
return ($result === false)
|
2015-08-08 10:09:07 -04:00
|
|
|
? new Failure(new FilesystemException(\error_get_last()["message"]))
|
2015-07-10 21:59:39 -04:00
|
|
|
: new Success($result)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|