summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/tasks/system/MoveTask.php
blob: a3e94536829bde148f6dc081a69745181efe5c72 (plain)
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
<?php
/*
 *  $Id: MoveTask.php 59 2006-04-28 14:49:47Z mrook $
 *
 * 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 $Revision: 1.8 $
 * @package phing.tasks.system
 */
class MoveTask extends CopyTask {

    function __construct() {
        parent::__construct();
        $this->forceOverwrite = true;
    }
    
    protected function doWork() {
    
        $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;
                }

                $moved = false;
                $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->renameFile($f, $d, $this->forceOverwrite);
                    $moved = true;
                } catch (IOException $ioe) {
                    $moved = false;
                    $this->log("Failed to rename $from to $to: " . $ioe->getMessage(), $this->verbosity);
                }

                if (!$moved) {                    
                    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());                        

                        $f = new PhingFile($fromFile);
                        $f->delete();
                    } catch (IOException $ioe) {
                        $msg = "Failed to move $from to $to: " . $ioe->getMessage();
                        throw new BuildException($msg, $this->location);
                    }
                } // if !moved
            } // foreach fileCopyMap
        } // if copyMapSize

        // handle empty dirs if appropriate
        if ($this->includeEmpty) {
            $e = array_keys($this->dirCopyMap);
            $count = 0;
            foreach ($e as $dir) {
                $d = new PhingFile((string) $dir);
                if (!$d->exists()) {
                    if (!$d->mkdirs()) {
                        $this->log("Unable to create directory " . $d->getAbsolutePath(), PROJECT_MSG_ERR);
                    } 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) {
            throw new BuildException("Unable to delete directory " . $d->__toString() . ": " . $e->getMessage());
        }
    }

    /**
     * Attempts to rename a file from a source to a destination.
     * If overwrite is set to true, this method overwrites existing file
     * even if the destination file is newer.
     * Otherwise, the source f
     * ile is renamed only if the destination file #
     * is older than it.
     */
    private function renameFile(PhingFile $sourceFile, PhingFile $destFile, $overwrite) {
        $renamed = true;

        // ensure that parent dir of dest file exists!
        $parent = $destFile->getParentFile();
        if ($parent !== null) {
            if (!$parent->exists()) {
                $parent->mkdirs();
            }
        }
        if ($destFile->exists()) {
            try {
                $destFile->delete();
            } catch (Exception $e) {
                throw new BuildException("Unable to remove existing file " . $destFile->__toString() . ": " . $e->getMessage());
            }
        }
        $renamed = $sourceFile->renameTo($destFile);

        return $renamed;
    }
}
?>