From 3170e3511baaa67590bef0bef92e734cc2226042 Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Thu, 6 Jun 2013 23:20:39 +0200 Subject: [PATCH] - implement touch() to at least create a file which doesn't exist - implement a work around for folder mtimes - irods doesn't provide updated mtimes --- apps/files_external/lib/irods.php | 97 +++++++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 11 deletions(-) diff --git a/apps/files_external/lib/irods.php b/apps/files_external/lib/irods.php index 29a15b60fd..1a0eb63d80 100644 --- a/apps/files_external/lib/irods.php +++ b/apps/files_external/lib/irods.php @@ -26,19 +26,20 @@ class iRODS extends \OC\Files\Storage\StreamWrapper{ public function __construct($params) { if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { - $this->host=$params['host']; - $this->port=$params['port']; - $this->user=$params['user']; - $this->password=$params['password']; - $this->use_logon_credentials=$params['use_logon_credentials']; - $this->zone=$params['zone']; - $this->auth_mode=isset($params['auth_mode']) ? $params['auth_mode'] : ''; + $this->host = $params['host']; + $this->port = $params['port']; + $this->user = $params['user']; + $this->password = $params['password']; + $this->use_logon_credentials = $params['use_logon_credentials']; + $this->zone = $params['zone']; + $this->auth_mode = isset($params['auth_mode']) ? $params['auth_mode'] : ''; - $this->root=isset($params['root'])?$params['root']:'/'; - if ( ! $this->root || $this->root[0]!='/') { + $this->root = isset($params['root']) ? $params['root'] : '/'; + if ( ! $this->root || $this->root[0] !== '/') { $this->root='/'.$this->root; } + // take user and password from the session if ($this->use_logon_credentials && isset($_SESSION['irods-credentials']) ) { $this->user = $_SESSION['irods-credentials']['uid']; @@ -69,10 +70,84 @@ class iRODS extends \OC\Files\Storage\StreamWrapper{ * @return string */ public function constructUrl($path) { + $path = rtrim($path,'/'); + if ( $path === '' || $path[0] !== '/') { + $path = '/'.$path; + } + + // adding auth method $userWithZone = $this->user.'.'.$this->zone; - if ($this->auth_mode === '') { - $userWithZone .= $this->auth_mode; + if ($this->auth_mode !== '') { + $userWithZone .= '.'.$this->auth_mode; } return 'rods://'.$userWithZone.':'.$this->password.'@'.$this->host.':'.$this->port.$this->root.$path; } + + public function filetype($path) { + $this->init(); + return @filetype($this->constructUrl($path)); + } + + public function mkdir($path) { + $this->init(); + return @mkdir($this->constructUrl($path)); + } + + public function touch($path, $mtime=null) { + + // we cannot set a time + if ($mtime != null) { + return false; + } + + $this->init(); + + $path = $this->constructUrl($path); + + // if the file doesn't exist we create it + if (!file_exists($path)) { + file_put_contents($path, ''); + return true; + } + + // mtime updates are not supported + return false; + } + + /** + * check if a file or folder has been updated since $time + * @param string $path + * @param int $time + * @return bool + */ + public function hasUpdated($path,$time) { + $this->init(); + + // this it a work around for folder mtimes -> we loop it's content + if ( $this->is_dir($path)) { + $actualTime=$this->collectionMTime($path); + return $actualTime>$time; + } + + $actualTime=$this->filemtime($path); + return $actualTime>$time; + } + + /** + * get the best guess for the modification time of an iRODS collection + */ + private function collectionMTime($path) { + $dh = $this->opendir($path); + $lastCTime = $this->filemtime($path); + while ($file = readdir($dh)) { + if ($file != '.' and $file != '..') { + $time = $this->filemtime($file); + if ($time > $lastCTime) { + $lastCTime = $time; + } + } + } + return $lastCTime; + } + }