/**
  * Copies a file using filter chains.
  *
  * @param  PhingFile $sourceFile
  * @param  PhingFile $destFile
  * @param  boolean   $overwrite
  * @param  boolean   $preserveLastModified
  * @param  array     $filterChains
  * @param  Project   $project
  * @param  integer   $mode
  * @param bool       $preservePermissions
  *
  * @throws Exception
  * @throws IOException
  * @return void
  */
 public function copyFile(PhingFile $sourceFile, PhingFile $destFile, $overwrite = false, $preserveLastModified = true, &$filterChains = null, Project $project, $mode = 0755, $preservePermissions = true)
 {
     if ($overwrite || !$destFile->exists() || $destFile->lastModified() < $sourceFile->lastModified()) {
         if ($destFile->exists() && $destFile->isFile()) {
             $destFile->delete();
         }
         // ensure that parent dir of dest file exists!
         $parent = $destFile->getParentFile();
         if ($parent !== null && !$parent->exists()) {
             // Setting source directory permissions to target
             // (On permissions preservation, the target directory permissions
             // will be inherited from the source directory, otherwise the 'mode'
             // will be used)
             $dirMode = $preservePermissions ? $sourceFile->getParentFile()->getMode() : $mode;
             $parent->mkdirs($dirMode);
         }
         if (is_array($filterChains) && !empty($filterChains)) {
             $in = self::getChainedReader(new BufferedReader(new FileReader($sourceFile)), $filterChains, $project);
             $out = new BufferedWriter(new FileWriter($destFile));
             // New read() methods returns a big buffer.
             while (-1 !== ($buffer = $in->read())) {
                 // -1 indicates EOF
                 $out->write($buffer);
             }
             if ($in !== null) {
                 $in->close();
             }
             if ($out !== null) {
                 $out->close();
             }
             // Set/Copy the permissions on the target
             if ($preservePermissions === true) {
                 $destFile->setMode($sourceFile->getMode());
             }
         } else {
             // simple copy (no filtering)
             $sourceFile->copyTo($destFile);
             // By default, PHP::Copy also copies the file permissions. Therefore,
             // re-setting the mode with the "user file-creation mask" information.
             if ($preservePermissions === false) {
                 $destFile->setMode(FileUtils::getDefaultFileCreationMask(false, true));
             }
         }
         if ($preserveLastModified && !$destFile->isLink()) {
             $destFile->setLastModified($sourceFile->lastModified());
         }
     }
 }
Example #2
0
 /**
  * Copy a file.
  *
  * @param PhingFile $src Source path and name file to copy.
  * @param PhingFile $dest Destination path and name of new file.
  *
  * @return void     
  * @throws Exception if file cannot be copied.
  */
 function copy(PhingFile $src, PhingFile $dest)
 {
     global $php_errormsg;
     $srcPath = $src->getAbsolutePath();
     $destPath = $dest->getAbsolutePath();
     if (false === @copy($srcPath, $destPath)) {
         // Copy FAILED. Log and return err.
         // Add error from php to end of log message. $php_errormsg.
         $msg = "FileSystem::copy() FAILED. Cannot copy {$srcPath} to {$destPath}. {$php_errormsg}";
         throw new Exception($msg);
     }
     try {
         $dest->setMode($src->getMode());
     } catch (Exception $exc) {
         // [MA] does chmod returns an error on systems that do not support it ?
         // eat it up for now.
     }
 }
Example #3
0
 /**
  * Copies a file using filter chains.
  * 
  * @param PhingFile $sourceFile
  * @param PhingFile $destFile
  * @param boolean $overwrite
  * @param boolean $preserveLastModified
  * @param array $filterChains 
  * @param Project $project
  * @param integer $mode
  * @return void
  */
 function copyFile(PhingFile $sourceFile, PhingFile $destFile, $overwrite = false, $preserveLastModified = true, &$filterChains = null, Project $project, $mode = 0755)
 {
     if ($overwrite || !$destFile->exists() || $destFile->lastModified() < $sourceFile->lastModified()) {
         if ($destFile->exists() && $destFile->isFile()) {
             $destFile->delete();
         }
         // ensure that parent dir of dest file exists!
         $parent = $destFile->getParentFile();
         if ($parent !== null && !$parent->exists()) {
             $parent->mkdirs($mode);
         }
         if (is_array($filterChains) && !empty($filterChains)) {
             $in = self::getChainedReader(new BufferedReader(new FileReader($sourceFile)), $filterChains, $project);
             $out = new BufferedWriter(new FileWriter($destFile));
             // New read() methods returns a big buffer.
             while (-1 !== ($buffer = $in->read())) {
                 // -1 indicates EOF
                 $out->write($buffer);
             }
             if ($in !== null) {
                 $in->close();
             }
             if ($out !== null) {
                 $out->close();
             }
             $destFile->setMode($sourceFile->getMode());
         } else {
             // simple copy (no filtering)
             $sourceFile->copyTo($destFile);
         }
         if ($preserveLastModified) {
             $destFile->setLastModified($sourceFile->lastModified());
         }
     }
 }
 /**
  * Actually change the mode for the file.
  * @param PhingFile $file
  * @param int $mode
  */
 private function chmodFile(PhingFile $file, $mode)
 {
     if (!$file->exists()) {
         throw new BuildException("The file " . $file->__toString() . " does not exist");
     }
     try {
         $file->setMode($mode);
         if ($this->verbose) {
             $this->log("Changed file mode on '" . $file->__toString() . "' to " . vsprintf("%o", $mode));
         }
     } catch (Exception $e) {
         if ($this->failonerror) {
             throw $e;
         } else {
             $this->log($e->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
         }
     }
 }
 /**
  * Copy a file.
  *
  * @param PhingFile $src  Source path and name file to copy.
  * @param PhingFile $dest Destination path and name of new file.
  *
  * @return void
  *
  * @throws IOException if file cannot be copied.
  */
 public function copy(PhingFile $src, PhingFile $dest)
 {
     global $php_errormsg;
     // Recursively copy a directory
     if ($src->isDirectory()) {
         return $this->copyr($src->getAbsolutePath(), $dest->getAbsolutePath());
     }
     $srcPath = $src->getAbsolutePath();
     $destPath = $dest->getAbsolutePath();
     if (false === @copy($srcPath, $destPath)) {
         // Copy FAILED. Log and return err.
         // Add error from php to end of log message. $php_errormsg.
         $msg = "FileSystem::copy() FAILED. Cannot copy {$srcPath} to {$destPath}. {$php_errormsg}";
         throw new IOException($msg);
     }
     $dest->setMode($src->getMode());
 }