示例#1
0
 /**
  * Special copy operation for virtual files.
  * 
  * @param IFile $file The source file to copy data from.
  * @param bool $keepOGP Set to TRUE to force keeping the Owner-Group-Permissions of the current file (target),
  * 				FALSE to overwrite them with the ones from the source file ($file)
  * @param bool $overwrite Set to TRUE to overwrite the current file (target)
  */
 protected function copyFromAndKeepOGP(IFile $file, $keepOGP = false, $overwrite = true)
 {
     if ($this->realFile === null) {
         throw new EyeUnsupportedOperationException(__METHOD__ . ' on ' . $this->path);
     }
     //if the destination ($this) is a a directory AND: the source is not a directory OR is a directory with a different name
     if ($this->isDirectory() && (!$file->isDirectory() || $this->getName() != $file->getName())) {
         if ($this->getName() != '/' || $file->getName() != '/') {
             //we redirect the copy operation to a file located within the destination directory ($this)
             return $this->getChildFile($file->getName())->copyFromAndKeepOGP($file, $keepOGP, $overwrite);
         }
     }
     if ($file instanceof ISecurableFile) {
         $file->checkReadPermission();
     }
     // $this file exists => check write permissions
     if ($this->realFile->exists()) {
         $this->checkWritePermission();
         if (!$overwrite) {
             throw new EyeIOException('Unable to copy: File ' . $this->path . ' exists but overwrite option is not enabled.');
         }
     } else {
         $this->getParentFile()->checkWritePermission();
     }
     //are we trying to copy a file to itself?
     if ($this->equals($file)) {
         throw new EyeIOException('Unable to copy ' . $file->getPath() . ': source and target are the same.');
     }
     //checks ok, we can start the copy process
     $success = true;
     try {
         //use the "real" file instead of the virtual one for next steps
         if ($file instanceof IVirtualFile) {
             $realFile = $file->getRealFile();
         } else {
             $realFile = $file;
         }
         //file to file
         //@todo Find which of these functions creates metadata
         if ($file->isFile()) {
             $fileCreated = false;
             if (!$this->exists()) {
                 $this->createNewFile();
                 $fileCreated = true;
             }
             if ($this->realFile->copyFrom($realFile, $overwrite)) {
                 // using metadata converter
                 $newMetaData = MetaDataConverter::getInstance()->convertThis($file, $this);
                 $this->setMeta($newMetaData);
                 //process metadata
                 /*
                 					$meta = null;
                 					if ($file instanceof IMetaAssociable) {
                 						// ----------------------------------------------------
                 						// FIXME: We need Metadata Converter
                 						$sourceFile = $file->realFile;
                 						$destFile = FSI::getFile($this->path);
                 						if ($sourceFile instanceof LocalFile && $destFile instanceof EyeWorkgroupFile) {
                 							$meta = $file->getMeta();
                 							$meta->set('activity', null);
                 							$meta->set('id', null);
                 							// END OF FIXME
                 						// ----------------------------------------------------
                 						} else {
                 							//Assign owner metadata to current User
                 							$meta = $file->getMeta();
                 							if ($meta->get('owner')) {
                 								$meta->set('owner', ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosUser()->getName());
                 								$meta->set('group', ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosGroup()->getName());
                 								$meta->set('permissions', null);
                 							}
                 							$meta = MetaManager::getInstance()->getNewMetaDataInstance($this, $meta);
                 						}
                 					}
                 					if ($meta !== null) {
                 						if (!$keepOGP && !$fileCreated) {
                 							$currentUser = ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosUser();
                 							$group = ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosGroup();
                 							$meta->set(self::METADATA_KEY_OWNER, $currentUser->getName());
                 							$meta->set(self::METADATA_KEY_GROUP, $group->getName());
                 							$meta->set(self::METADATA_KEY_PERMISSIONS, AdvancedPathLib::permsToUnix(self::PERMISSIONS_MASK_FILE & ~$this->getUMask()));
                 						}
                 						$this->setMeta($meta);
                 					}
                 */
             } else {
                 $success = false;
             }
         } else {
             if ($file->isDirectory()) {
                 //create destination directory if needed
                 if (!$this->exists()) {
                     $this->mkdirs();
                 }
                 //copy subfiles one by one (needed for metadata)
                 foreach ($file->listFiles() as $subFile) {
                     //create child path
                     $urlParts = $this->getURLComponents();
                     $urlParts['path'] .= '/' . $subFile->getName();
                     $params = array('realFile' => $this->realFile->getChildFile($subFile->getName()));
                     //instantiate new file object
                     $thisClass = get_class($this);
                     $thisSubFile = new $thisClass(AdvancedPathLib::buildURL($urlParts), $params);
                     if ($thisSubFile instanceof EyeosAbstractVirtualFile) {
                         $success = $success && $thisSubFile->copyFromAndKeepOGP($subFile, $keepOGP, $overwrite);
                     } else {
                         $success = $success && $thisSubFile->copyFrom($subFile, $overwrite);
                     }
                 }
                 //process metadata
                 $meta = null;
                 if ($file instanceof IMetaAssociable) {
                     $meta = MetaManager::getInstance()->getNewMetaDataInstance($this, $file->getMeta());
                 } else {
                     $meta = MetaManager::getInstance()->getNewMetaDataInstance($this);
                 }
                 if ($meta !== null) {
                     /*
                     					if (!$keepOGP) {
                     						$currentUser = ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosUser();
                     						$group = ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosGroup();
                     						$meta->set(self::METADATA_KEY_OWNER, $currentUser->getName());
                     						$meta->set(self::METADATA_KEY_GROUP, $group->getName());
                     						$meta->set(self::METADATA_KEY_PERMISSIONS, AdvancedPathLib::permsToUnix(self::PERMISSIONS_MASK_DIR & ~$this->getUMask()));
                     					}
                     */
                     //$this->setMeta($meta);
                 }
             } else {
                 if ($file->isLink()) {
                     $linkTarget = $file->getLinkTarget();
                     if ($linkTarget === null) {
                         throw new EyeNullPointerException('Unable to get link\'s target for ' . $file->getPath() . '.');
                     }
                     $this->createNewFile($overwrite);
                     /*
                     				$meta = $file->getMeta();
                     				if (!$keepOGP) {
                     					$currentUser = ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosUser();
                     					$group = ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosGroup();
                     					$meta->set(self::METADATA_KEY_OWNER, $currentUser->getName());
                     					$meta->set(self::METADATA_KEY_GROUP, $group->getName());
                     					$meta->set(self::METADATA_KEY_PERMISSIONS, AdvancedPathLib::permsToUnix(self::PERMISSIONS_MASK_LINK & ~$this->getUMask()));
                     					$meta->set(self::METADATA_KEY_LINKTARGET, $linkTarget);
                     				}
                     */
                     //$this->setMeta($meta);
                 }
             }
         }
     } catch (EyeErrorException $e) {
         throw new EyeIOException('Unable to copy ' . $file->getPath() . ' to ' . $this->path . '.', 0, $e);
     } catch (EyeException $e) {
         throw new EyeIOException('Unable to copy ' . $file->getPath() . ' to ' . $this->path . '.', 0, $e);
     }
     if ($success) {
         return true;
     }
     throw new EyeUnknownErrorException('Unable to (entirely) copy ' . $file->getPath() . ' to ' . $this->path . '.');
 }