Beispiel #1
0
 public function rename($path1, $path2)
 {
     $postFix1 = substr($path1, -1, 1) === '/' ? '/' : '';
     $postFix2 = substr($path2, -1, 1) === '/' ? '/' : '';
     $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1));
     $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2));
     if (\OC_FileProxy::runPreProxies('rename', $absolutePath1, $absolutePath2) and Filesystem::isValidPath($path2) and Filesystem::isValidPath($path1) and !Filesystem::isFileBlacklisted($path2)) {
         $path1 = $this->getRelativePath($absolutePath1);
         $path2 = $this->getRelativePath($absolutePath2);
         $exists = $this->file_exists($path2);
         if ($path1 == null or $path2 == null) {
             return false;
         }
         $run = true;
         if ($this->shouldEmitHooks() && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) {
             // if it was a rename from a part file to a regular file it was a write and not a rename operation
             $this->emit_file_hooks_pre($exists, $path2, $run);
         } elseif ($this->shouldEmitHooks()) {
             \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_rename, array(Filesystem::signal_param_oldpath => $this->getHookPath($path1), Filesystem::signal_param_newpath => $this->getHookPath($path2), Filesystem::signal_param_run => &$run));
         }
         if ($run) {
             $mp1 = $this->getMountPoint($path1 . $postFix1);
             $mp2 = $this->getMountPoint($path2 . $postFix2);
             $manager = Filesystem::getMountManager();
             $mount = $manager->find($absolutePath1 . $postFix1);
             $storage1 = $mount->getStorage();
             $internalPath1 = $mount->getInternalPath($absolutePath1 . $postFix1);
             list(, $internalPath2) = Filesystem::resolvePath($absolutePath2 . $postFix2);
             if ($internalPath1 === '' and $mount instanceof MoveableMount) {
                 if ($this->isTargetAllowed($absolutePath2)) {
                     /**
                      * @var \OC\Files\Mount\Mount | \OC\Files\Mount\MoveableMount $mount
                      */
                     $sourceMountPoint = $mount->getMountPoint();
                     $result = $mount->moveMount($absolutePath2);
                     $manager->moveMount($sourceMountPoint, $mount->getMountPoint());
                     \OC_FileProxy::runPostProxies('rename', $absolutePath1, $absolutePath2);
                 } else {
                     $result = false;
                 }
             } elseif ($mp1 == $mp2) {
                 if ($storage1) {
                     $result = $storage1->rename($internalPath1, $internalPath2);
                     \OC_FileProxy::runPostProxies('rename', $absolutePath1, $absolutePath2);
                 } else {
                     $result = false;
                 }
             } else {
                 if ($this->is_dir($path1)) {
                     $result = $this->copy($path1, $path2);
                     if ($result === true) {
                         $result = $storage1->rmdir($internalPath1);
                     }
                 } else {
                     $source = $this->fopen($path1 . $postFix1, 'r');
                     $target = $this->fopen($path2 . $postFix2, 'w');
                     list($count, $result) = \OC_Helper::streamCopy($source, $target);
                     // close open handle - especially $source is necessary because unlink below will
                     // throw an exception on windows because the file is locked
                     fclose($source);
                     fclose($target);
                     if ($result !== false) {
                         $storage1->unlink($internalPath1);
                     }
                 }
             }
             if ($this->shouldEmitHooks() && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) {
                 // if it was a rename from a part file to a regular file it was a write and not a rename operation
                 Updater::writeHook(array('path' => $this->getHookPath($path2)));
                 $this->emit_file_hooks_post($exists, $path2);
             } elseif ($this->shouldEmitHooks() && $result !== false) {
                 Updater::renameHook(array('oldpath' => $this->getHookPath($path1), 'newpath' => $this->getHookPath($path2)));
                 \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_rename, array(Filesystem::signal_param_oldpath => $this->getHookPath($path1), Filesystem::signal_param_newpath => $this->getHookPath($path2)));
             }
             return $result;
         } else {
             return false;
         }
     } else {
         return false;
     }
 }