From b67c83752cf986ddee338d1caebfaf993040442c Mon Sep 17 00:00:00 2001 From: eugene kirillov Date: Thu, 23 Nov 2017 04:55:30 +0200 Subject: [PATCH] Add optional time parameters to touch function (#22) --- lib/BlockingDriver.php | 6 ++++-- lib/Driver.php | 4 +++- lib/EioDriver.php | 7 ++++--- lib/Internal/FileTask.php | 1 + lib/ParallelDriver.php | 4 ++-- lib/UvDriver.php | 7 ++++--- lib/functions.php | 6 ++++-- test/DriverTest.php | 3 +-- 8 files changed, 23 insertions(+), 15 deletions(-) diff --git a/lib/BlockingDriver.php b/lib/BlockingDriver.php index 09903c0..d5de5e6 100644 --- a/lib/BlockingDriver.php +++ b/lib/BlockingDriver.php @@ -324,8 +324,10 @@ class BlockingDriver implements Driver { /** * {@inheritdoc} */ - public function touch(string $path): Promise { - return new Success((bool) \touch($path)); + public function touch(string $path, int $time = null, int $atime = null): Promise { + $time = $time ?? \time(); + $atime = $atime ?? $time; + return new Success((bool) \touch($path, $time, $atime)); } /** diff --git a/lib/Driver.php b/lib/Driver.php index 6068a7a..dfc0623 100644 --- a/lib/Driver.php +++ b/lib/Driver.php @@ -196,9 +196,11 @@ interface Driver { * If the file does not exist it will be created automatically. * * @param string $path + * @param int $time The touch time. If $time is not supplied, the current system time is used. + * @param int $atime The access time. If $atime is not supplied, value passed to the $time parameter is used. * @return \Amp\Promise */ - public function touch(string $path): Promise; + public function touch(string $path, int $time = null, int $atime = null): Promise; /** * Buffer the specified file's contents. diff --git a/lib/EioDriver.php b/lib/EioDriver.php index 19ff1c4..ce95ee8 100644 --- a/lib/EioDriver.php +++ b/lib/EioDriver.php @@ -464,14 +464,15 @@ class EioDriver implements Driver { /** * {@inheritdoc} */ - public function touch(string $path): Promise { - $atime = $mtime = \time(); + public function touch(string $path, int $time = null, int $atime = null): Promise { + $time = $time ?? \time(); + $atime = $atime ?? $time; $deferred = new Deferred; $this->poll->listen($deferred->promise()); $priority = \EIO_PRI_DEFAULT; - \eio_utime($path, $atime, $mtime, $priority, [$this, "onGenericResult"], $deferred); + \eio_utime($path, $atime, $time, $priority, [$this, "onGenericResult"], $deferred); return $deferred->promise(); } diff --git a/lib/Internal/FileTask.php b/lib/Internal/FileTask.php index 44d7fa9..bb95100 100644 --- a/lib/Internal/FileTask.php +++ b/lib/Internal/FileTask.php @@ -1,4 +1,5 @@ runFileTask(new Internal\FileTask("touch", [$path]))); + public function touch(string $path, int $time = null, int $atime = null): Promise { + return new Coroutine($this->runFileTask(new Internal\FileTask("touch", [$path, $time, $atime]))); } /** diff --git a/lib/UvDriver.php b/lib/UvDriver.php index aeb6610..13920a5 100644 --- a/lib/UvDriver.php +++ b/lib/UvDriver.php @@ -458,13 +458,14 @@ class UvDriver implements Driver { /** * {@inheritdoc} */ - public function touch(string $path): Promise { - $atime = $mtime = time(); + public function touch(string $path, int $time = null, int $atime = null): Promise { + $time = $time ?? \time(); + $atime = $atime ?? $time; $deferred = new Deferred; $this->poll->listen($deferred->promise()); - \uv_fs_utime($this->loop, $path, $mtime, $atime, function () use ($deferred) { + \uv_fs_utime($this->loop, $path, $time, $atime, function () use ($deferred) { // The uv_fs_utime() callback does not receive any args at this time $deferred->resolve(true); }); diff --git a/lib/functions.php b/lib/functions.php index a753c1e..46ae318 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -305,10 +305,12 @@ function chown(string $path, int $uid, int $gid = -1): Promise { * If the file does not exist it will be created automatically. * * @param string $path + * @param int $time The touch time. If $time is not supplied, the current system time is used. + * @param int $atime The access time. If $atime is not supplied, value passed to the $time parameter is used. * @return \Amp\Promise */ -function touch(string $path): Promise { - return filesystem()->touch($path); +function touch(string $path, int $time = null, int $atime = null): Promise { + return filesystem()->touch($path, $time, $atime); } /** diff --git a/test/DriverTest.php b/test/DriverTest.php index 9a9fb01..cde6aa8 100644 --- a/test/DriverTest.php +++ b/test/DriverTest.php @@ -316,8 +316,7 @@ abstract class DriverTest extends TestCase { yield File\put($touch, "touch me"); $oldStat = (yield File\stat($touch)); - sleep(1); - yield File\touch($touch); + yield File\touch($touch, \time() + 10, \time() + 20); File\StatCache::clear($touch); $newStat = (yield File\stat($touch)); yield File\unlink($touch);