diff options
Diffstat (limited to 'buildscripts/phing/classes/phing/system/io/PhingFile.php')
-rwxr-xr-x[-rw-r--r--] | buildscripts/phing/classes/phing/system/io/PhingFile.php | 350 |
1 files changed, 240 insertions, 110 deletions
diff --git a/buildscripts/phing/classes/phing/system/io/PhingFile.php b/buildscripts/phing/classes/phing/system/io/PhingFile.php index cd881963..871afedd 100644..100755 --- a/buildscripts/phing/classes/phing/system/io/PhingFile.php +++ b/buildscripts/phing/classes/phing/system/io/PhingFile.php @@ -1,6 +1,6 @@ <?php /* - * $Id: PhingFile.php,v 1.1 2005/05/26 13:10:52 mrook Exp $ + * $Id: d2cfaf834a2cc605a3aedf37285f547010b8b4f4 $ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT @@ -25,7 +25,7 @@ include_once 'phing/system/lang/NullPointerException.php'; /** * An abstract representation of file and directory pathnames. * - * @version $Revision: 1.1 $ + * @version $Id$ * @package phing.system.io */ class PhingFile { @@ -43,7 +43,10 @@ class PhingFile { */ private $path = null; - /** The length of this abstract pathname's prefix, or zero if it has no prefix. */ + /** + * The length of this abstract pathname's prefix, or zero if it has no prefix. + * @var int + */ private $prefixLength = 0; /** constructor */ @@ -71,14 +74,23 @@ class PhingFile { } } - /** Returns the length of this abstract pathname's prefix. */ + /** + * Returns the length of this abstract pathname's prefix. + * + * @return int + */ function getPrefixLength() { return (int) $this->prefixLength; } /* -- constructors not called by signature match, so we need some helpers --*/ - function _constructPathname($pathname) { + /** + * + * Enter description here ... + * @param unknown_type $pathname + */ + protected function _constructPathname($pathname) { // obtain ref to the filesystem layer $fs = FileSystem::getFileSystem(); @@ -90,7 +102,13 @@ class PhingFile { $this->prefixLength = (int) $fs->prefixLength($this->path); } - function _constructStringParentStringChild($parent, $child = null) { + /** + * + * Enter description here ... + * @param unknown_type $parent + * @param unknown_type $child + */ + protected function _constructStringParentStringChild($parent, $child = null) { // obtain ref to the filesystem layer $fs = FileSystem::getFileSystem(); @@ -109,7 +127,13 @@ class PhingFile { $this->prefixLength = (int) $fs->prefixLength($this->path); } - function _constructFileParentStringChild($parent, $child = null) { + /** + * + * Enter description here ... + * @param unknown_type $parent + * @param unknown_type $child + */ + protected function _constructFileParentStringChild($parent, $child = null) { // obtain ref to the filesystem layer $fs = FileSystem::getFileSystem(); @@ -166,7 +190,7 @@ class PhingFile { // that's a lastIndexOf $index = ((($res = strrpos($this->path, self::$separator)) === false) ? -1 : $res); if ($index < $this->prefixLength) { - if (($this->prefixLength > 0) && (strlen($this->path > $this->prefixLength))) { + if (($this->prefixLength > 0) && (strlen($this->path) > $this->prefixLength)) { return substr($this->path, 0, $this->prefixLength); } return null; @@ -200,20 +224,42 @@ class PhingFile { * string uses the default name-separator character to separate the names * in the name sequence. * - * @return The string form of this abstract pathname + * @return string The string form of this abstract pathname */ function getPath() { return (string) $this->path; } /** + * Returns path without leading basedir. + * + * @param string $basedir Base directory to strip + * + * @return string Path without basedir + * + * @uses getPath() + */ + function getPathWithoutBase($basedir) + { + if (!StringHelper::endsWith(self::$separator, $basedir)) { + $basedir .= self::$separator; + } + $path = $this->getPath(); + if (!substr($path, 0, strlen($basedir)) == $basedir) { + //path does not begin with basedir, we don't modify it + return $path; + } + return substr($path, strlen($basedir)); + } + + /** * Tests whether this abstract pathname is absolute. The definition of * absolute pathname is system dependent. On UNIX systems, a pathname is * absolute if its prefix is "/". On Win32 systems, a pathname is absolute * if its prefix is a drive specifier followed by "\\", or if its prefix * is "\\". * - * @return true if this abstract pathname is absolute, false otherwise + * @return boolean true if this abstract pathname is absolute, false otherwise */ function isAbsolute() { return ($this->prefixLength !== 0); @@ -235,9 +281,9 @@ class PhingFile { * pathname, if any; if not, it is resolved against the current user * directory. * - * @return The absolute pathname string denoting the same file or - * directory as this abstract pathname - * @see #isAbsolute() + * @return string The absolute pathname string denoting the same file or + * directory as this abstract pathname + * @see #isAbsolute() */ function getAbsolutePath() { $fs = FileSystem::getFileSystem(); @@ -248,8 +294,8 @@ class PhingFile { * Returns the absolute form of this abstract pathname. Equivalent to * getAbsolutePath. * - * @return The absolute abstract pathname denoting the same file or - * directory as this abstract pathname + * @return string The absolute abstract pathname denoting the same file or + * directory as this abstract pathname */ function getAbsoluteFile() { return new PhingFile((string) $this->getAbsolutePath()); @@ -277,8 +323,8 @@ class PhingFile { * file or directory may be different from the canonical form of the same * pathname after the file or directory is deleted. * - * @return The canonical pathname string denoting the same file or - * directory as this abstract pathname + * @return string The canonical pathname string denoting the same file or + * directory as this abstract pathname */ function getCanonicalPath() { $fs = FileSystem::getFileSystem(); @@ -309,8 +355,8 @@ class PhingFile { * URI, via the toURI() method, and then converting the URI * into a URL via the URI::toURL() * - * @return A URL object representing the equivalent file URL - * + * @return void A URL object representing the equivalent file URL + * @todo Not implemented yet * */ function toURL() { @@ -322,7 +368,8 @@ class PhingFile { /** * Constructs a file: URI that represents this abstract pathname. - * Not implemented yet + * @todo Not implemented yet + * @return void */ function toURI() { /* @@ -334,6 +381,13 @@ class PhingFile { */ } + /** + * + * Enter description here ... + * @param PhingFile|string $path + * @param boolean $isDirectory + * @return string + */ function _slashify($path, $isDirectory) { $p = (string) $path; @@ -358,15 +412,15 @@ class PhingFile { * Tests whether the application can read the file denoted by this * abstract pathname. * - * @return true if and only if the file specified by this - * abstract pathname exists and can be read by the - * application; false otherwise + * @return boolean true if and only if the file specified by this + * abstract pathname exists and can be read by the + * application; false otherwise */ function canRead() { $fs = FileSystem::getFileSystem(); if ($fs->checkAccess($this)) { - return (boolean) @is_readable($this->getAbsolutePath()); + return (boolean) @is_link($this->getAbsolutePath()) || @is_readable($this->getAbsolutePath()); } return false; } @@ -375,11 +429,10 @@ class PhingFile { * Tests whether the application can modify to the file denoted by this * abstract pathname. * - * @return true if and only if the file system actually - * contains a file denoted by this abstract pathname and - * the application is allowed to write to the file; - * false otherwise. - * + * @return boolean true if and only if the file system actually + * contains a file denoted by this abstract pathname and + * the application is allowed to write to the file; + * false otherwise. */ function canWrite() { $fs = FileSystem::getFileSystem(); @@ -389,13 +442,16 @@ class PhingFile { /** * Tests whether the file denoted by this abstract pathname exists. * - * @return true if and only if the file denoted by this - * abstract pathname exists; false otherwise - * + * @return boolean true if and only if the file denoted by this + * abstract pathname exists; false otherwise */ function exists() { - if ($this->isFile()) { - return @file_exists($this->path); + clearstatcache(); + + if (is_link($this->path)) { + return true; + } else if ($this->isFile()) { + return @file_exists($this->path) || is_link($this->path); } else { return @is_dir($this->path); } @@ -405,17 +461,17 @@ class PhingFile { * Tests whether the file denoted by this abstract pathname is a * directory. * - * @return true if and only if the file denoted by this - * abstract pathname exists and is a directory; - * false otherwise - * + * @return boolean true if and only if the file denoted by this + * abstract pathname exists and is a directory; + * false otherwise */ function isDirectory() { + clearstatcache(); $fs = FileSystem::getFileSystem(); if ($fs->checkAccess($this) !== true) { throw new IOException("No read access to ".$this->path); } - return @is_dir($this->path); + return @is_dir($this->path) && !@is_link($this->path); } /** @@ -424,11 +480,12 @@ class PhingFile { * addition, satisfies other system-dependent criteria. Any non-directory * file created by a Java application is guaranteed to be a normal file. * - * @return true if and only if the file denoted by this - * abstract pathname exists and is a normal file; - * false otherwise + * @return boolean true if and only if the file denoted by this + * abstract pathname exists and is a normal file; + * false otherwise */ function isFile() { + clearstatcache(); //$fs = FileSystem::getFileSystem(); return @is_file($this->path); } @@ -441,9 +498,9 @@ class PhingFile { * hidden if it has been marked as such in the filesystem. Currently there * seems to be no way to dermine isHidden on Win file systems via PHP * - * @return true if and only if the file denoted by this - * abstract pathname is hidden according to the conventions of the - * underlying platform + * @return boolean true if and only if the file denoted by this + * abstract pathname is hidden according to the conventions of the + * underlying platform */ function isHidden() { $fs = FileSystem::getFileSystem(); @@ -452,15 +509,42 @@ class PhingFile { } return (($fs->getBooleanAttributes($this) & $fs->BA_HIDDEN) !== 0); } + + /** + * Tests whether the file denoted by this abstract pathname is a symbolic link. + * + * @return boolean true if and only if the file denoted by this + * abstract pathname exists and is a symbolic link; + * false otherwise + */ + public function isLink() + { + clearstatcache(); + $fs = FileSystem::getFileSystem(); + if ($fs->checkAccess($this) !== true) { + throw new IOException("No read access to ".$this->path); + } + return @is_link($this->path); + } + + /** + * Returns the target of the symbolic link denoted by this abstract pathname + * + * @return string the target of the symbolic link denoted by this abstract pathname + */ + public function getLinkTarget() + { + return @readlink($this->path); + } /** * Returns the time that the file denoted by this abstract pathname was * last modified. * - * @return A integer value representing the time the file was - * last modified, measured in milliseconds since the epoch - * (00:00:00 GMT, January 1, 1970), or 0 if the - * file does not exist or if an I/O error occurs + * @return int An integer value representing the time the file was + * last modified, measured in milliseconds since the epoch + * (00:00:00 GMT, January 1, 1970), or 0 if the + * file does not exist or if an I/O error occurs */ function lastModified() { $fs = FileSystem::getFileSystem(); @@ -474,8 +558,8 @@ class PhingFile { * Returns the length of the file denoted by this abstract pathname. * The return value is unspecified if this pathname denotes a directory. * - * @return The length, in bytes, of the file denoted by this abstract - * pathname, or 0 if the file does not exist + * @return int The length, in bytes, of the file denoted by this abstract + * pathname, or 0 if the file does not exist */ function length() { $fs = FileSystem::getFileSystem(); @@ -507,9 +591,9 @@ class PhingFile { * are a single operation that is atomic with respect to all other * filesystem activities that might affect the file. * - * @return true if the named file does not exist and was - * successfully created; <code>false</code> if the named file - * already exists + * @return boolean true if the named file does not exist and was + * successfully created; <code>false</code> if the named file + * already exists * @throws IOException if file can't be created */ function createNewFile($parents=true, $mode=0777) { @@ -522,15 +606,15 @@ class PhingFile { * this pathname denotes a directory, then the directory must be empty in * order to be deleted. * - * @return true if and only if the file or directory is - * successfully deleted; false otherwise + * @return boolean true if and only if the file or directory is + * successfully deleted; false otherwise */ - function delete() { + function delete($recursive = false) { $fs = FileSystem::getFileSystem(); - if ($fs->checkAccess($this, true) !== true) { - throw new IOException("No read access to " . $this->path."\n"); + if ($fs->canDelete($this) !== true) { + throw new IOException("Cannot delete " . $this->path . "\n"); } - return $fs->delete($this); + return $fs->delete($this, $recursive); } /** @@ -563,18 +647,23 @@ class PhingFile { * will appear in any specific order; they are not, in particular, * guaranteed to appear in alphabetical order. * - * @return An array of strings naming the files and directories in the - * directory denoted by this abstract pathname. The array will be - * empty if the directory is empty. Returns null if - * this abstract pathname does not denote a directory, or if an - * I/O error occurs. - * + * @param $filter string + * @return array An array of strings naming the files and directories in the + * directory denoted by this abstract pathname. The array will be + * empty if the directory is empty. Returns null if + * this abstract pathname does not denote a directory, or if an + * I/O error occurs. */ function listDir($filter = null) { $fs = FileSystem::getFileSystem(); return $fs->lister($this, $filter); } + /** + * + * Enter description here ... + * @param PhingFile[] $filter + */ function listFiles($filter = null) { $ss = $this->listDir($filter); if ($ss === null) { @@ -594,46 +683,46 @@ class PhingFile { * operation fails it may have succeeded in creating some of the necessary * parent directories. * - * @return true if and only if the directory was created, - * along with all necessary parent directories; false - * otherwise - * @throws IOException + * @return boolean true if and only if the directory was created, + * along with all necessary parent directories; false + * otherwise + * @throws IOException */ - function mkdirs() { + function mkdirs($mode = 0755) { if ($this->exists()) { return false; } - try { - if ($this->mkdir()) { - return true; - } - } catch (IOException $ioe) { - // IOException from mkdir() means that directory propbably didn't exist. - } + try { + if ($this->mkdir($mode)) { + return true; + } + } catch (IOException $ioe) { + // IOException from mkdir() means that directory propbably didn't exist. + } $parentFile = $this->getParentFile(); - return (($parentFile !== null) && ($parentFile->mkdirs() && $this->mkdir())); + return (($parentFile !== null) && ($parentFile->mkdirs($mode) && $this->mkdir($mode))); } /** * Creates the directory named by this abstract pathname. * - * @return true if and only if the directory was created; false otherwise - * @throws IOException + * @return boolean true if and only if the directory was created; false otherwise + * @throws IOException */ - function mkdir() { + function mkdir($mode = 0755) { $fs = FileSystem::getFileSystem(); if ($fs->checkAccess(new PhingFile($this->path), true) !== true) { throw new IOException("No write access to " . $this->getPath()); } - return $fs->createDirectory($this); + return $fs->createDirectory($this, $mode); } /** * Renames the file denoted by this abstract pathname. * - * @param destFile The new abstract pathname for the named file - * @return true if and only if the renaming succeeded; false otherwise + * @param PhingFile $destFile The new abstract pathname for the named file + * @return boolean true if and only if the renaming succeeded; false otherwise */ function renameTo(PhingFile $destFile) { $fs = FileSystem::getFileSystem(); @@ -647,8 +736,8 @@ class PhingFile { * Simple-copies file denoted by this abstract pathname into another * PhingFile * - * @param PhingFile $destFile The new abstract pathname for the named file - * @return true if and only if the renaming succeeded; false otherwise + * @param PhingFile $destFile The new abstract pathname for the named file + * @return boolean true if and only if the renaming succeeded; false otherwise */ function copyTo(PhingFile $destFile) { $fs = FileSystem::getFileSystem(); @@ -674,9 +763,9 @@ class PhingFile { * lastModified method will return the (possibly truncated) time argument * that was passed to this method. * - * @param time The new last-modified time, measured in milliseconds since - * the epoch (00:00:00 GMT, January 1, 1970) - * @return true if and only if the operation succeeded; false otherwise + * @param int $time The new last-modified time, measured in milliseconds since + * the epoch (00:00:00 GMT, January 1, 1970) + * @return boolean true if and only if the operation succeeded; false otherwise */ function setLastModified($time) { $time = (int) $time; @@ -684,11 +773,7 @@ class PhingFile { throw new Exception("IllegalArgumentException, Negative $time\n"); } - // FIXME check if accessible $fs = FileSystem::getFileSystem(); - if ($fs->checkAccess($this, true) !== true) { - throw new IOException("File::setLastModified(). No write access to file\n"); - } return $fs->setLastModifiedTime($this, $time); } @@ -699,7 +784,7 @@ class PhingFile { * marked to allow write access. Whether or not a read-only file or * directory may be deleted depends upon the underlying system. * - * @return true if and only if the operation succeeded; false otherwise + * @return boolean true if and only if the operation succeeded; false otherwise */ function setReadOnly() { $fs = FileSystem::getFileSystem(); @@ -711,6 +796,40 @@ class PhingFile { } /** + * Sets the owner of the file. + * @param mixed $user User name or number. + */ + public function setUser($user) { + $fs = FileSystem::getFileSystem(); + return $fs->chown($this->getPath(), $user); + } + + /** + * Retrieve the owner of this file. + * @return int User ID of the owner of this file. + */ + function getUser() { + return @fileowner($this->getPath()); + } + + /** + * Sets the group of the file. + * @param mixed $user User name or number. + */ + public function setGroup($group) { + $fs = FileSystem::getFileSystem(); + return $fs->chgrp($this->getPath(), $group); + } + + /** + * Retrieve the group of this file. + * @return int User ID of the owner of this file. + */ + function getGroup() { + return @filegroup($this->getPath()); + } + + /** * Sets the mode of the file * @param int $mode Ocatal mode. */ @@ -757,10 +876,10 @@ class PhingFile { * platform will be returned by this method, while PhingFile * objects containing UNC pathnames will not be returned by this method. * - * @return An array of PhingFile objects denoting the available - * filesystem roots, or null if the set of roots - * could not be determined. The array will be empty if there are - * no filesystem roots. + * @return array An array of PhingFile objects denoting the available + * filesystem roots, or null if the set of roots + * could not be determined. The array will be empty if there are + * no filesystem roots. */ function listRoots() { $fs = FileSystem::getFileSystem(); @@ -771,8 +890,9 @@ class PhingFile { /** * Returns the path to the temp directory. + * @return string */ - function getTempDir() { + public static function getTempDir() { return Phing::getProperty('php.tmpdir'); } @@ -782,11 +902,11 @@ class PhingFile { * is a reference to a PhingFile Object. * Then, the file is locked for exclusive reading/writing. * - * @author manuel holtgrewe, grin@gmx.net - * @throws IOException - * @access public + * @author manuel holtgrewe, grin@gmx.net + * @throws IOException + * @return PhingFile */ - function createTempFile($prefix, $suffix, PhingFile $directory) { + public static function createTempFile($prefix, $suffix, PhingFile $directory) { // quick but efficient hack to create a unique filename ;-) $result = null; @@ -853,14 +973,24 @@ class PhingFile { return false; } - /** Backwards compatibility -- use PHP5's native __tostring method. */ - function toString() { - return $this->getPath(); + /** + * Backwards compatibility - @see __toString() + * + * @return string + */ + public function toString() + { + return $this->__toString(); } - /** PHP5's native method. */ - function __toString() { + /** + * Return string representation of the object + * + * @return string + */ + public function __toString() + { return $this->getPath(); } } -?> + |