protected function addNode(Node $node) { $info = pathinfo($node->path); if ($info['dirname'] == '.') { throw new \InvalidArgumentException("Absolute paths only - '{$node->path}' was relative"); } $parentNode = $this->getInternalNode($info['dirname']); if (!$parentNode) { throw FileException::ENOENT("Parent {$info['dirname']} not found"); } if ($node->type == Node::DIR) { $node = (object) ['node' => $node, 'data' => [], 'len' => 0]; } else { $node = (object) ['node' => $node, 'data' => (object) ['data' => ''], 'len' => 0]; } $parentNode->len++; $parentNode->data[$info['basename']] = $node; }
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; }
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}"); } }