public function truncate($offset = 0, $cb = null, $pri = EIO_PRI_DEFAULT) { if (!FS::$supported) { $fp = fopen($this->path, 'r+'); $r = $fp && ftruncate($fp, $offset); if ($cb) { call_user_func($cb, $this, $r); } return; } eio_ftruncate($this->fd, $offset, $pri, $cb, $this); }
/** * Truncates this file * @param integer $offset Offset. Default is 0 * @param callable $cb Callback * @param integer $pri Priority * @return resource|boolean */ public function truncate($offset = 0, $cb = null, $pri = EIO_PRI_DEFAULT) { $cb = CallbackWrapper::forceWrap($cb); if (!$this->fd || $this->fd === -1) { if ($cb) { $cb($this, false); } return false; } if (!FileSystem::$supported) { $fp = fopen($this->path, 'r+'); $r = $fp && ftruncate($fp, $offset); if ($cb) { $cb($this, $r); } return $r; } return eio_ftruncate($this->fd, $offset, $pri, $cb, $this); }
private function onOpenHandle($openArr, $result, $req) { list($mode, $path, $promisor) = $openArr; if ($result === -1) { \call_user_func($this->incrementor, -1); $promisor->fail(new FilesystemException(\eio_get_last_error($req))); } elseif ($mode[0] === "a") { \array_unshift($openArr, $result); \eio_ftruncate($result, $offset = 0, $priority = null, [$this, "onOpenFtruncate"], $openArr); } else { \array_unshift($openArr, $result); \eio_fstat($result, $priority = null, [$this, "onOpenFstat"], $openArr); } }
/** * {@inheritdoc} */ public function truncate(int $size) : \Generator { if (!$this->isOpen()) { throw new FileException('The file has been closed.'); } if (0 >= $size) { $size = 0; } $delayed = new Delayed(); \eio_ftruncate($this->handle, $size, null, function (Delayed $delayed, $result, $req) { if (-1 === $result) { $delayed->reject(new FileException(sprintf('Truncating the file failed: %s.', \eio_get_last_error($req)))); } else { $delayed->resolve(true); } }, $delayed); $this->poll->listen(); try { $result = (yield $delayed); $this->size = $size; if ($this->position > $size) { $this->position = $size; } } finally { $this->poll->done(); } return $result; }