1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
<?php
/*
* $Id: 940741d8377306f23bb29048dd874e99298ffbd2 $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
require_once 'phing/tasks/system/CopyTask.php';
include_once 'phing/system/io/PhingFile.php';
include_once 'phing/system/io/IOException.php';
/**
* Moves a file or directory to a new file or directory.
*
* By default, the destination file is overwritten if it
* already exists. When overwrite is turned off, then files
* are only moved if the source file is newer than the
* destination file, or when the destination file does not
* exist.
*
* Source files and directories are only deleted when the file or
* directory has been copied to the destination successfully.
*
* @version $Id: 940741d8377306f23bb29048dd874e99298ffbd2 $
* @package phing.tasks.system
*/
class MoveTask extends CopyTask {
function __construct() {
parent::__construct();
$this->forceOverwrite = true;
}
/**
* Validates attributes coming in from XML
*
* @access private
* @return void
* @throws BuildException
*/
protected function validateAttributes() {
if ($this->file !== null && $this->file->isDirectory()) {
if (($this->destFile !== null && $this->destDir !== null)
|| ($this->destFile === null && $this->destDir === null)) {
throw new BuildException("One and only one of tofile and todir must be set.");
}
if ($this->destFile === null)
{
$this->destFile = new PhingFile($this->destDir, $this->file->getName());
}
if ($this->destDir === null)
{
$this->destDir = $this->destFile->getParentFile();
}
$this->completeDirMap[$this->file->getAbsolutePath()] = $this->destFile->getAbsolutePath();
$this->file = null;
} else {
parent::validateAttributes();
}
}
protected function doWork() {
if (count($this->completeDirMap) > 0)
{
foreach ($this->completeDirMap as $from => $to)
{
$f = new PhingFile($from);
$d = new PhingFile($to);
$moved = false;
try { // try to rename
$this->log("Attempting to rename $from to $to", $this->verbosity);
$this->fileUtils->copyFile($f, $d, $this->forceOverwrite, $this->preserveLMT, $this->filterChains, $this->getProject(), $this->mode);
$f->delete(true);
$moved = true;
} catch (IOException $ioe) {
$moved = false;
$this->logError("Failed to rename $from to $to: " . $ioe->getMessage());
}
}
}
$copyMapSize = count($this->fileCopyMap);
if ($copyMapSize > 0) {
// files to move
$this->log("Moving $copyMapSize files to " . $this->destDir->getAbsolutePath());
foreach($this->fileCopyMap as $from => $to) {
if ($from == $to) {
$this->log("Skipping self-move of $from", $this->verbosity);
continue;
}
$f = new PhingFile($from);
$d = new PhingFile($to);
try { // try to move
$this->log("Moving $from to $to", $this->verbosity);
$this->fileUtils->copyFile($f, $d, $this->forceOverwrite, $this->preserveLMT, $this->filterChains, $this->getProject(), $this->mode);
$f->delete();
} catch (IOException $ioe) {
$this->logError("Failed to move $from to $to: " . $ioe->getMessage(), $this->location);
}
} // foreach fileCopyMap
} // if copyMapSize
// handle empty dirs if appropriate
if ($this->includeEmpty) {
$count = 0;
foreach ($this->dirCopyMap as $srcDir => $destDir) {
$d = new PhingFile((string) $destDir);
if (!$d->exists()) {
if (!$d->mkdirs()) {
$this->logError("Unable to create directory " . $d->getAbsolutePath());
} else {
$count++;
}
}
}
if ($count > 0) {
$this->log("moved $count empty director" . ($count == 1 ? "y" : "ies") . " to " . $this->destDir->getAbsolutePath());
}
}
if (count($this->filesets) > 0) {
// process filesets
foreach($this->filesets as $fs) {
$dir = $fs->getDir($this->project);
if ($this->okToDelete($dir)) {
$this->deleteDir($dir);
}
}
}
}
/** Its only ok to delete a dir tree if there are no files in it. */
private function okToDelete($d) {
$list = $d->listDir();
if ($list === null) {
return false; // maybe io error?
}
foreach($list as $s) {
$f = new PhingFile($d, $s);
if ($f->isDirectory()) {
if (!$this->okToDelete($f)) {
return false;
}
} else {
// found a file
return false;
}
}
return true;
}
/** Go and delete the directory tree. */
private function deleteDir($d) {
$list = $d->listDir();
if ($list === null) {
return; // on an io error list() can return null
}
foreach($list as $fname) {
$f = new PhingFile($d, $fname);
if ($f->isDirectory()) {
$this->deleteDir($f);
} else {
throw new BuildException("UNEXPECTED ERROR - The file " . $f->getAbsolutePath() . " should not exist!");
}
}
$this->log("Deleting directory " . $d->getPath(), $this->verbosity);
try {
$d->delete();
} catch (Exception $e) {
$this->logError("Unable to delete directory " . $d->__toString() . ": " . $e->getMessage());
}
}
}
|