Exemple #1
0
 public function mkdir($path, $mode = 0777, $options = null)
 {
     $path = Util\Path::normalise("/{$path}");
     $target = $this->getInternalNode($path);
     if ($target) {
         throw FileException::EEXIST();
     }
     if ($options & STREAM_MKDIR_RECURSIVE) {
         $bits = "";
         $make = false;
         foreach (Util\Path::split($path) as $part) {
             $bits .= "/{$part}";
             if (!$make) {
                 $node = $this->getInternalNode($bits);
                 if (!$node) {
                     $make = true;
                 }
             }
             if ($make) {
                 if (!$this->mkdir($bits, $mode, $options & ~STREAM_MKDIR_RECURSIVE)) {
                     throw new \Exception();
                 }
             }
         }
         return true;
     } else {
         $dir = $this->getInternalNode(dirname($path));
         if (!$dir) {
             throw FileException::ENOENT();
         } elseif ($dir->node->type != Node::DIR) {
             throw FileException::ENOTDIR();
         }
         $node = $this->createNode($path, Node::DIR, ['perms' => $mode]);
         $dir->data[basename($path)] = (object) ['node' => $node, 'data' => [], 'len' => 0];
         return true;
     }
 }
Exemple #2
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}");
     }
 }