/** * @return bool TRUE if the file has been successfully moved, FALSE otherwise */ public function moveTo(IFile $file) { if ($this->realFile === null) { throw new EyeUnsupportedOperationException(__METHOD__ . ' on ' . $this->path); } if ($this->isRoot()) { throw new EyeUnsupportedOperationException('Cannot move root folder ' . $this->path . '.'); } //if the destination ($file) is a a directory AND: the source is not a directory OR is a directory with a different name if ($file->isDirectory() && (!$this->isDirectory() || $file->getName() != $this->getName())) { if ($file->getName() != '/' || $this->getName() != '/') { //we redirect the move operation to a file located within the destination directory ($file) return $this->moveTo($file->getChildFile($this->getName())); } } $this->checkReadPermission(); $parentFile = $file->getParentFile(); if ($parentFile instanceof ISecurableFile) { $parentFile->checkWritePermission(); } if ($file->exists()) { $file->checkWritePermission(); } try { $success = false; if ($file instanceof EyeosAbstractVirtualFile) { $success = $file->copyFromAndKeepOGP($this, true); } else { $success = $this->copyTo($file); } if ($success) { // Ensure the "listeners" property has been initialized *before* deleting the file $this->getAllFileListeners(); // Delete file *without firing deletion event* (from outside, file has been moved, not deleted) if ($this->deleteImpl(true, false, false)) { //notify listeners $this->fireEvent('fileMoved', new FileEvent($this, $file)); return true; } } } catch (EyeException $e) { throw new EyeIOException('Cannot move ' . $this->path . ' to ' . $file->getPath() . '.', 0, $e); } throw new EyeIOException('Cannot move ' . $this->path . ' to ' . $file->getPath() . ' (Unknown error).'); }
/** * @param IFile $file The source file to copy from. * @return bool TRUE if the file has been successfully copied from $file, FALSE if an error occured * Note: An exception is raised if an important error is detected (the copy of a single file failed), * but during a folder copy, a failure during the copy of a single "sub-file" is ignored and no * exception is raised. * Nevertheless, you can check if the copy was a full success by testing the returned value. * @throws EyeIOException * @throws EyeFileNotFoundException */ protected function copyFrom(IFile $file, $overwrite = true) { if ($this->isDirectory() && (!$file->isDirectory() || $this->getName() != $file->getName())) { if ($this->getName() != '/' || $file->getName() != '/') { return $this->getChildFile($file->getName())->copyFrom($file, $overwrite); } } if ($this->exists() && !$overwrite) { throw new EyeIOException($this->path . '" exists and can\'t be overwritten.'); } //FILE or LINK if ($file->isFile() || $file->isLink()) { $srcPath = AdvancedPathLib::getPhpLocalHackPath($file->getPath()); $destPath = AdvancedPathLib::getPhpLocalHackPath($this->path); // First, let's try with the function provided by PHP, but only working with // a very restricted range of filesystems if (copy($srcPath, $destPath)) { return true; } if (!$this->exists() && !$this->createNewFile(true)) { throw new EyeIOException('Unable to create destination file ' . $this->path . '.'); } try { $fileWriter = new FileWriter($this->getOutputStream()); $fileReader = new FileReader($file->getInputStream()); $buffer = null; while ($fileReader->read($buffer) !== 0) { $fileWriter->write($buffer); } $fileReader->close(); $fileWriter->close(); return true; } catch (Exception $e) { if (is_object($fileReader)) { $fileReader->close(); } if (is_object($fileWriter)) { $fileWriter->close(); } throw new EyeIOException('Unable to transfer files contents ' . $file->getPath() . ' => ' . $this->path . '.', 0, $e); } } elseif ($file->isDirectory()) { if ($this->isDirectory() || $this->mkdirs()) { $success = true; foreach ($file->listFiles() as $subFile) { try { if (!$subFile->copyTo($this)) { $success = false; } } catch (Exception $e) { $success = false; } } return $success; } else { throw new EyeIOException('Unable to create destination directory ' . $this->path . '.'); } } else { throw new EyeFileNotFoundException($file->getPath() . ' does not exist.'); } }