Ejemplo n.º 1
0
 public function test_readfile_eio()
 {
     $time_start = microtime(true);
     echo "reading file is started....<br/>";
     $file_open_cb = function ($data, $result) {
         eio_read($result, 10485760, 0, EIO_PRI_DEFAULT, function ($data, $result) {
             echo str_word_count($result) . "<br/>";
             eio_close($data);
         }, $result);
     };
     $filename = './assets/data/access.log';
     eio_open($filename, EIO_O_RDWR, NULL, EIO_PRI_DEFAULT, $file_open_cb, $filename);
     $filename = './assets/data/error.log';
     eio_open($filename, EIO_O_RDWR, NULL, EIO_PRI_DEFAULT, $file_open_cb, $filename);
     echo "reading file is finish....<br/>";
     eio_event_loop();
     $time_end = microtime(true);
     $time = $time_end - $time_start;
     echo "Execution time: {$time} seconds<br/>";
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function put($path, $contents)
 {
     $flags = \EIO_O_RDWR | \EIO_O_CREAT;
     $mode = \EIO_S_IRUSR | \EIO_S_IWUSR | \EIO_S_IXUSR;
     $priority = \EIO_PRI_DEFAULT;
     $this->incrementPending();
     $promisor = new Deferred();
     $data = [$contents, $promisor];
     \eio_open($path, $flags, $mode, $priority, [$this, "onPutOpen"], $data);
     return $promisor->promise();
 }
Ejemplo n.º 3
0
 /**
  * Open file
  * @param  string   $path  Path
  * @param  string   $flags Flags
  * @param  callable $cb    Callback (File)
  * @param  integer  $mode  Mode (see EIO_S_I* constants)
  * @param  integer  $pri   Priority
  * @return resource
  */
 public static function open($path, $flags, $cb, $mode = null, $pri = EIO_PRI_DEFAULT)
 {
     $cb = CallbackWrapper::forceWrap($cb);
     if (!FileSystem::$supported) {
         $mode = File::convertFlags($flags, true);
         $fd = fopen($path, $mode);
         if (!$fd) {
             call_user_func($cb, false);
             return false;
         }
         $file = new File($fd, $path);
         call_user_func($cb, $file);
         return true;
     }
     $fdCacheKey = $path . "" . $flags;
     $noncache = strpos($flags, '!') !== false;
     $flags = File::convertFlags($flags);
     if (!$noncache && ($item = FileSystem::$fdCache->get($fdCacheKey))) {
         // cache hit
         $file = $item->getValue();
         if ($file === null) {
             // operation in progress
             $item->addListener($cb);
         } else {
             // hit
             call_user_func($cb, $file);
         }
         return null;
     } elseif (!$noncache) {
         $item = FileSystem::$fdCache->put($fdCacheKey, null);
         $item->addListener($cb);
     }
     return eio_open($path, $flags, $mode, $pri, function ($path, $fd) use($cb, $flags, $fdCacheKey, $noncache) {
         if ($fd === -1) {
             if ($noncache) {
                 call_user_func($cb, false);
             } else {
                 FileSystem::$fdCache->put($fdCacheKey, false, self::$badFDttl);
             }
             return;
         }
         $file = new File($fd, $path);
         $file->append = ($flags | EIO_O_APPEND) === $flags;
         if ($file->append) {
             $file->stat(function ($file, $stat) use($cb, $noncache, $fdCacheKey) {
                 $file->offset = $stat['size'];
                 if (!$noncache) {
                     $file->fdCacheKey = $fdCacheKey;
                     FileSystem::$fdCache->put($fdCacheKey, $file);
                 } else {
                     call_user_func($cb, $file);
                 }
             });
         } else {
             if (!$noncache) {
                 $file->fdCacheKey = $fdCacheKey;
                 FileSystem::$fdCache->put($fdCacheKey, $file);
             } else {
                 call_user_func($cb, $file);
             }
         }
     }, $path);
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function open(string $path, string $mode) : \Generator
 {
     $flags = $this->makeFlags($mode);
     $chmod = $flags & \EIO_O_CREAT ? 0644 : 0;
     $delayed = new Delayed();
     \eio_open($path, $flags, $chmod, null, function (Delayed $delayed, $handle, $req) {
         if (-1 === $handle) {
             $delayed->reject(new FileException(sprintf('Opening the file failed: %s.', \eio_get_last_error($req))));
         } else {
             $delayed->resolve($handle);
         }
     }, $delayed);
     $this->poll->listen();
     try {
         $handle = (yield $delayed);
     } finally {
         $this->poll->done();
     }
     if ($flags & \EIO_O_TRUNC) {
         // File truncated.
         $size = 0;
     } else {
         $delayed = new Delayed();
         \eio_fstat($handle, null, function (Delayed $delayed, $result, $req) {
             if (-1 === $result) {
                 $delayed->reject(new FileException(sprintf('Finding file size failed: %s.', \eio_get_last_error($req))));
             } else {
                 $delayed->resolve($result['size']);
             }
         }, $delayed);
         $this->poll->listen();
         try {
             $size = (yield $delayed);
         } finally {
             $this->poll->done();
         }
     }
     return new EioFile($this->poll, $handle, $path, $size, (bool) ($flags & \EIO_O_APPEND));
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function put($path, $contents)
 {
     $flags = \EIO_O_WRONLY | \EIO_O_CREAT | \EIO_O_TRUNC;
     $mode = 0666;
     $priority = \EIO_PRI_DEFAULT;
     \call_user_func($this->incrementor, 1);
     $promisor = new Deferred();
     $data = [$contents, $promisor];
     \eio_open($path, $flags, $mode, $priority, [$this, "onPutOpen"], $data);
     return $promisor->promise();
 }
Ejemplo n.º 6
0
 /**
  * @coroutine
  *
  * @param string $path
  *
  * @return \Generator
  *
  * @resolve int
  */
 public function copy(string $path) : \Generator
 {
     $delayed = new Delayed();
     \eio_open($path, \EIO_O_WRONLY | \EIO_O_CREAT | \EIO_O_TRUNC, 0644, null, function (Delayed $delayed, $handle, $req) {
         if (-1 === $handle) {
             $delayed->reject(new FileException(sprintf('Opening the file failed: %s.', \eio_get_last_error($req))));
         } else {
             $delayed->resolve($handle);
         }
     }, $delayed);
     $this->poll->listen();
     try {
         $handle = (yield $delayed);
     } finally {
         $this->poll->done();
     }
     $delayed = new Delayed();
     \eio_sendfile($handle, $this->handle, 0, $this->size, null, function (Delayed $delayed, $result, $req) {
         if (-1 === $result) {
             $delayed->reject(new FileException(sprintf('Copying the file failed: %s.', \eio_get_last_error($req))));
         } else {
             $delayed->resolve(true);
         }
     }, $delayed);
     $this->poll->listen();
     try {
         (yield $delayed);
     } finally {
         $this->poll->done();
         \eio_close($handle);
     }
     return $this->size;
 }