1
0
mirror of https://github.com/danog/file.git synced 2024-11-26 20:04:51 +01:00

Add optional time parameters to touch function (#22)

This commit is contained in:
eugene kirillov 2017-11-23 04:55:30 +02:00 committed by Aaron Piotrowski
parent 9a375918ae
commit b67c83752c
8 changed files with 23 additions and 15 deletions

View File

@ -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));
}
/**

View File

@ -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.

View File

@ -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();
}

View File

@ -1,4 +1,5 @@
<?php
namespace Amp\File\Internal;
use Amp\File\BlockingDriver;

View File

@ -264,8 +264,8 @@ class ParallelDriver implements Driver {
/**
* {@inheritdoc}
*/
public function touch(string $path): Promise {
return new Coroutine($this->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])));
}
/**

View File

@ -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);
});

View File

@ -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<null>
*/
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);
}
/**

View File

@ -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);