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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
<?php
/**
* $Id: 7e42ce5973d6672b4caea89c4bda81fe01556e00 $
*
* 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/Task.php";
require_once 'phing/system/io/PhingFile.php';
/**
* ManifestTask
*
* Generates a simple Manifest file with optional checksums.
*
*
* Manifest schema:
* ...
* path/to/file CHECKSUM [CHECKSUM2] [CHECKSUM3]
* path/to/secondfile CHECKSUM [CHECKSUM2] [CHECKSUM3]
* ...
*
* Example usage:
* <manifest checksum="crc32" file="${dir_build}/Manifest">
* <fileset refid="files_build" />
* </manifest>
*
* <manifest checksum="md5,adler32,sha256" file="${dir_build}/Manifest">
* <fileset refid="files_build" />
* </manifest>
*
*
*
* @author David Persson <davidpersson at qeweurope dot org>
* @package phing.tasks.ext
* @version $Id: 7e42ce5973d6672b4caea89c4bda81fe01556e00 $
* @since 2.3.1
*/
class ManifestTask extends Task
{
var $taskname = 'manifest';
/**
* Action
*
* "w" for reading in files from fileSet
* and writing manifest
*
* or
*
* "r" for reading in files from fileSet
* and checking against manifest
*
* @var string "r" or "w"
*/
private $action = 'w';
/**
* The target file passed in the buildfile.
*/
private $destFile = null;
/**
* Holds filesets
*
* @var array An Array of objects
*/
private $filesets = array();
/**
* Enable/Disable checksuming or/and select algorithm
* true defaults to md5
* false disables checksuming
* string "md5,sha256,..." enables generation of multiple checksums
* string "sha256" generates sha256 checksum only
*
* @var mixed
*/
private $checksum = false;
/**
* A string used in hashing method
*
* @var string
*/
private $salt = '';
/**
* Holds some data collected during runtime
*
* @var array
*/
private $meta = array('totalFileCount' => 0,'totalFileSize' => 0);
/**
* The setter for the attribute "file"
* This is where the manifest will be written to/read from
*
* @param string Path to readable file
* @return void
*/
public function setFile(PhingFile $file)
{
$this->file = $file;
}
/**
* The setter for the attribute "checksum"
*
* @param mixed $mixed
* @return void
*/
public function setChecksum($mixed)
{
if(is_string($mixed)) {
$data = array(strtolower($mixed));
if(strpos($data[0],',')) {
$data = explode(',',$mixed);
}
$this->checksum = $data;
} elseif($mixed === true) {
$this->checksum = array('md5');
}
}
/**
* The setter for the optional attribute "salt"
*
* @param string $string
* @return void
*/
public function setSalt($string)
{
$this->salt = $string;
}
/**
* Nested adder, adds a set of files (nested fileset attribute).
*
* @return void
*/
public function addFileSet(FileSet $fs) {
$this->filesets[] = $fs;
}
/**
* The init method: Do init steps.
*/
public function init()
{
// nothing to do here
}
/**
* Delegate the work
*/
public function main()
{
$this->validateAttributes();
if($this->action == 'w') {
$this->write();
} elseif($this->action == 'r') {
$this->read();
}
}
/**
* Creates Manifest file
* Writes to $this->file
*
* @throws BuildException
*/
private function write()
{
$project = $this->getProject();
if(!touch($this->file->getPath())) {
throw new BuildException("Unable to write to ".$this->file->getPath().".");
}
$this->log("Writing to " . $this->file->__toString(), Project::MSG_INFO);
if(is_array($this->checksum)) {
$this->log("Using " . implode(', ',$this->checksum)." for checksuming.", Project::MSG_INFO);
}
foreach($this->filesets as $fs) {
$dir = $fs->getDir($this->project)->getPath();
$ds = $fs->getDirectoryScanner($project);
$fromDir = $fs->getDir($project);
$srcFiles = $ds->getIncludedFiles();
$srcDirs = $ds->getIncludedDirectories();
foreach($ds->getIncludedFiles() as $file_path) {
$line = $file_path;
if($this->checksum) {
foreach($this->checksum as $algo) {
if(!$hash = $this->hashFile($dir.'/'.$file_path,$algo)) {
throw new BuildException("Hashing $dir/$file_path with $algo failed!");
}
$line .= "\t".$hash;
}
}
$line .= "\n";
$manifest[] = $line;
$this->log("Adding file ".$file_path,Project::MSG_VERBOSE);
$this->meta['totalFileCount'] ++;
$this->meta['totalFileSize'] += filesize($dir.'/'.$file_path);
}
}
file_put_contents($this->file,$manifest);
$this->log("Done. Total files: ".$this->meta['totalFileCount'].". Total file size: ".$this->meta['totalFileSize']." bytes.", Project::MSG_INFO);
}
/**
* @todo implement
*/
private function read()
{
throw new BuildException("Checking against manifest not yet supported.");
}
/**
* Wrapper method for hash generation
* Automatically selects extension
* Falls back to built-in functions
*
* @link http://www.php.net/mhash
* @link http://www.php.net/hash
*
* @param string $msg The string that should be hashed
* @param string $algo Algorithm
* @return mixed String on success, false if $algo is not available
*/
private function hash($msg,$algo)
{
if(extension_loaded('hash')) {
$algo = strtolower($algo);
if(in_array($algo,hash_algos())) {
return hash($algo,$this->salt.$msg);
}
}
if(extension_loaded('mhash')) {
$algo = strtoupper($algo);
if(defined('MHASH_'.$algo)) {
return mhash('MHASH_'.$algo,$this->salt.$msg);
}
}
switch(strtolower($algo)) {
case 'md5':
return md5($this->salt.$msg);
case 'crc32':
return abs(crc32($this->salt.$msg));
}
return false;
}
/**
* Hash a files contents
* plus it's size an modification time
*
* @param string $file
* @param string $algo
* @return mixed String on success, false if $algo is not available
*/
private function hashFile($file,$algo)
{
if(!file_exists($file)) {
return false;
}
$msg = file_get_contents($file).filesize($file).filemtime($file);
return $this->hash($msg,$algo);
}
/**
* Validates attributes coming in from XML
*
* @access private
* @return void
* @throws BuildException
*/
protected function validateAttributes()
{
if($this->action != 'r' && $this->action != 'w') {
throw new BuildException("'action' attribute has non valid value. Use 'r' or 'w'");
}
if(empty($this->salt)) {
$this->log("No salt provided. Specify one with the 'salt' attribute.", Project::MSG_WARN);
}
if (is_null($this->file) && count($this->filesets) === 0) {
throw new BuildException("Specify at least sources and destination - a file or a fileset.");
}
if (!is_null($this->file) && $this->file->exists() && $this->file->isDirectory()) {
throw new BuildException("Destination file cannot be a directory.");
}
}
}
|