Skip to content

Commit

Permalink
initial implementation for symlinks, see #89
Browse files Browse the repository at this point in the history
In order to make this work you need to compile your own PHP from
https://github.com/thekid/php-src branch rfc/linking_in_stream_wrappers
  • Loading branch information
mikey179 committed Nov 30, 2014
1 parent d3fa7b1 commit ec24fe6
Show file tree
Hide file tree
Showing 4 changed files with 342 additions and 5 deletions.
310 changes: 310 additions & 0 deletions src/main/php/org/bovigo/vfs/Symlink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
<?php
/**
* This file is part of vfsStream.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package org\bovigo\vfs
*/
namespace org\bovigo\vfs;
/**
* Represents a symbolic link.
*
* @since 1.?.0
*/
class Symlink implements vfsStreamContent
{
/**
* name of the link
*
* @type string
*/
private $name;
/**
* target where link points to
*
* @type \org\bovigo\vfs\vfsStreamContent
*/
private $target;
/**
* path to to this content
*
* @type string
*/
private $parentPath;

/**
* constructor
*
* @param string $name name of link
* @param \org\bovigo\vfs\vfsStreamContent $target target where link points to
*/
public function __construct($name, vfsStreamContent $target)
{
$this->name = $name;
$this->target = $target;
}

/**
* resolves the link
*
* @return \org\bovigo\vfs\vfsStreamContent
*/
public function resolve()
{
return $this->target;
}

public function targetName()
{
return $this->target->getName();
}

/**
* returns the file name of the content
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* renames the content
*
* @param string $newName
*/
public function rename($newName)
{
$this->name = $newName;
}

/**
* checks whether the container can be applied to given name
*
* @param string $name
* @return bool
*/
public function appliesTo($name)
{
if ($name === $this->name) {
return true;
}

$segment_name = $this->name.'/';
return (strncmp($segment_name, $name, strlen($segment_name)) == 0);
}

/**
* returns the type of the container
*
* @return int
*/
public function getType()
{
return vfsStreamContent::TYPE_LINK;
}

/**
* returns size of content
*
* @return int
*/
public function size()
{
return $this->target->size();
}

/**
* sets the last modification time of the stream content
*
* @param int $filemtime
* @return vfsStreamContent
*/
public function lastModified($filemtime)
{
$this->target->lastModified($filemtime);
return $this;
}

/**
* returns the last modification time of the stream content
*
* @return int
*/
public function filemtime()
{
return $this->target->filemtime();
}

/**
* adds content to given container
*
* @param vfsStreamContainer $container
* @return vfsStreamContent
*/
public function at(vfsStreamContainer $container)
{
$container->addChild($this);
return $this;
}

/**
* change file mode to given permissions
*
* @param int $permissions
* @return vfsStreamContent
*/
public function chmod($permissions)
{
$this->target->chmod($permissions);
return $this;
}

/**
* returns permissions
*
* @return int
*/
public function getPermissions()
{
return $this->target->getPermissions();
}

/**
* checks whether content is readable
*
* @param int $user id of user to check for
* @param int $group id of group to check for
* @return bool
*/
public function isReadable($user, $group)
{
# self or target?
}

/**
* checks whether content is writable
*
* @param int $user id of user to check for
* @param int $group id of group to check for
* @return bool
*/
public function isWritable($user, $group)
{
# self or target?
}

/**
* checks whether content is executable
*
* @param int $user id of user to check for
* @param int $group id of group to check for
* @return bool
*/
public function isExecutable($user, $group)
{
# self or target?
}

/**
* change owner of file to given user
*
* @param int $user
* @return vfsStreamContent
*/
public function chown($user)
{
# self or target?
}

/**
* checks whether file is owned by given user
*
* @param int $user
* @return bool
*/
public function isOwnedByUser($user)
{
# self or target?
}

/**
* returns owner of file
*
* @return int
*/
public function getUser()
{
# self or target?
}

/**
* change owner group of file to given group
*
* @param int $group
* @return vfsStreamContent
*/
public function chgrp($group)
{
# self or target?
}

/**
* checks whether file is owned by group
*
* @param int $group
* @return bool
*/
public function isOwnedByGroup($group)
{
# self or target?
}

/**
* returns owner group of file
*
* @return int
*/
public function getGroup()
{
# self or target?
}

/**
* sets parent path
*
* @param string $parentPath
* @internal only to be set by parent
*/
public function setParentPath($parentPath)
{
$this->parentPath = $parentPath;
}

/**
* returns path to this content
*
* @return string
*/
public function path()
{
if (null === $this->parentPath) {
return $this->name;
}

return $this->parentPath . '/' . $this->name;
}

/**
* returns complete vfsStream url for this content
*
* @return string
*/
public function url()
{
return vfsStream::url($this->path());
}
}
15 changes: 14 additions & 1 deletion src/main/php/org/bovigo/vfs/vfsStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,26 @@ public static function newDirectory($name, $permissions = null)
*
* @param string $name name of the block device
* @param int $permissions permissions of block to create
* @return vfsStreamBlock
* @return \org\bovigo\vfs\vfsStreamBlock
*/
public static function newBlock($name, $permissions = null)
{
return new vfsStreamBlock($name, $permissions);
}

/**
* returns a new symbolic link with given name pointing to target
*
* @param string $name
* @param \org\bovigo\vfs\vfsStreamContent $target
* @return \org\bovigo\vfs\Symlink
* @since 1.?.0
*/
public static function newSymlink($name, vfsStreamContent $target)
{
return new Symlink($name, $target);
}

/**
* returns current user
*
Expand Down
5 changes: 2 additions & 3 deletions src/main/php/org/bovigo/vfs/vfsStreamContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ interface vfsStreamContent
*
* @see getType();
*/
#const TYPE_LINK = 0120000;

const TYPE_LINK = 0120000;
/**
* stream content type: block
*
Expand Down Expand Up @@ -211,4 +210,4 @@ public function path();
*/
public function url();
}
?>

17 changes: 16 additions & 1 deletion src/main/php/org/bovigo/vfs/vfsStreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -977,5 +977,20 @@ public function url_stat($path, $flags)
);
return array_merge(array_values($fileStat), $fileStat);
}

public function url_readlink($path)
{
$content = $this->getContent($this->resolvePath(vfsStream::path($path)));
if (null === $content) {
trigger_error(' Does not exist: ' . $path, E_USER_WARNING);
return false;
}

if ($content->getType() !== vfsStreamContent::TYPE_LINK) {
trigger_error(' Not a link: ' . $path, E_USER_WARNING);
return false;
}

return $content->targetName();
}
}
?>

0 comments on commit ec24fe6

Please sign in to comment.