Exemple #1
0
 public function read($length = null)
 {
     if (!($this->mode & Stream::READ)) {
         throw FileException::ENOTREADABLE();
     }
     list($buf, $bytesRead) = $this->doRead($length);
     if ($length === null || $bytesRead != $length) {
         $this->eof = true;
     }
     return $buf;
 }
Exemple #2
0
 function resize($size)
 {
     if (!($this->streamMode & Stream::WRITE)) {
         throw FileException::ENOTWRITABLE();
     }
     return ftruncate($this->handle, $size);
 }
Exemple #3
0
 public function rename($from, $to)
 {
     $fullPathFrom = $this->getFullPath($from);
     $fullPathTo = $this->getFullPath($to);
     $fromNode = $this->getNode($from);
     if (!$fromNode) {
         throw FileException::ENOENT();
     }
     $toNode = $this->getNode($to);
     if ($toNode && $toNode->type == Node::DIR) {
         $toParent = $toNode;
         $fullPathTo = $this->getFullPath($toNode->path . "/" . basename($from));
     } else {
         $toParent = $this->getNode(dirname($to));
     }
     if (!$toParent) {
         throw FileException::ENOENT();
     }
     $ret = rename($fullPathFrom, $fullPathTo);
     return $ret;
 }
Exemple #4
0
 protected function createStream(Node $node, $streamMode)
 {
     $memNode = $this->getInternalNode($node->path);
     if (!$memNode && $streamMode & Stream::WRITE) {
         $this->addNode($node);
         $memNode = $this->getInternalNode($node->path);
     }
     if (!$memNode) {
         return false;
     }
     if ($memNode->node->type == Node::DIR) {
         throw FileException::EISDIR();
     }
     return new Stream\Memory($this, $node, $streamMode, $memNode->data);
 }
Exemple #5
0
 public function resize($size)
 {
     if (!($this->mode & Stream::WRITE)) {
         throw FileException::ENOTWRITABLE();
     }
     // ftruncate manual page states that the file pointer is not changed,
     // hence this is disabled:
     // $this->pos = min($this->pos, $size);
     if ($size > $this->node->length) {
         $this->buffer->data = str_pad($this->buffer->data, $size, "");
     } elseif ($size < $this->node->length) {
         $new = substr($this->buffer->data, 0, $size);
         if ($new === false) {
             throw new \UnexpectedValueException("Buffer of length {$this->node->length} could not be truncated to size {$size}");
         }
         $this->buffer->data = $new;
     }
     $this->node->length = $size;
     return true;
 }
Exemple #6
0
 public function open($path, $mode)
 {
     $path = Util\Path::normalise("/{$path}");
     $streamMode = Stream\Base::calculateStreamMode($mode);
     switch ($mode) {
         case 'r':
         case 'rb':
         case 'r+':
         case 'r+b':
         case 'rb+':
             $node = $this->getNode($path);
             if (!$node) {
                 throw FileException::ENOENT();
             }
             return $this->createStream($node, $streamMode);
             break;
         case 'x':
         case 'xb':
         case 'x+':
         case 'x+b':
         case 'xb+':
             $node = $this->getNode($path);
             if ($node) {
                 throw FileException::EEXIST();
             } else {
                 $node = $this->createNode($path, Node::FILE);
                 $this->addNode($node);
             }
             return $this->createStream($node, $streamMode);
             break;
         case 'w':
         case 'wb':
         case 'w+':
         case 'w+b':
         case 'wb+':
         case 'c':
         case 'cb':
         case 'c+':
         case 'c+b':
         case 'cb+':
         case 'a':
         case 'ab':
         case 'a+':
         case 'a+b':
         case 'ab+':
             if (!$this->exists($path)) {
                 $node = $this->createNode($path, Node::FILE);
                 $this->addNode($node);
             } else {
                 $node = $this->getNode($path);
                 if ($mode[0] == 'w') {
                     $this->setMeta($path, self::M_MTIME, time());
                 }
             }
             $stream = $this->createStream($node, $streamMode);
             if ($mode[0] == 'w') {
                 $stream->resize(0);
             } elseif ($mode[0] == 'a') {
                 $stream->seek(0, SEEK_END);
             }
             return $stream;
             break;
         default:
             throw new \InvalidArgumentException("Unknown mode {$mode}");
     }
 }