2012-04-18 18:54:07 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2012-09-05 21:28:59 +00:00
|
|
|
require_once 'php-cloudfiles/cloudfiles.php';
|
2012-04-18 18:54:07 +00:00
|
|
|
|
2012-09-07 16:30:48 +00:00
|
|
|
namespace OC\Files\Storage;
|
|
|
|
|
|
|
|
class SWIFT extends \OC\Files\Storage\Common{
|
2012-04-18 18:54:07 +00:00
|
|
|
private $host;
|
|
|
|
private $root;
|
|
|
|
private $user;
|
|
|
|
private $token;
|
|
|
|
private $secure;
|
|
|
|
/**
|
|
|
|
* @var CF_Authentication auth
|
|
|
|
*/
|
|
|
|
private $auth;
|
|
|
|
/**
|
|
|
|
* @var CF_Connection conn
|
|
|
|
*/
|
|
|
|
private $conn;
|
|
|
|
/**
|
|
|
|
* @var CF_Container rootContainer
|
|
|
|
*/
|
|
|
|
private $rootContainer;
|
|
|
|
|
|
|
|
private static $tempFiles=array();
|
2012-04-24 22:11:10 +00:00
|
|
|
private $objects=array();
|
|
|
|
private $containers=array();
|
2012-04-18 18:54:07 +00:00
|
|
|
|
|
|
|
const SUBCONTAINER_FILE='.subcontainers';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* translate directory path to container name
|
|
|
|
* @param string path
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private function getContainerName($path) {
|
2012-10-11 13:52:21 +00:00
|
|
|
$path=trim(trim($this->root,'/')."/".$path,'/.');
|
2012-04-24 22:11:10 +00:00
|
|
|
return str_replace('/','\\',$path);
|
2012-04-18 18:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get container by path
|
|
|
|
* @param string path
|
|
|
|
* @return CF_Container
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private function getContainer($path) {
|
|
|
|
if($path=='' or $path=='/') {
|
2012-04-18 18:54:07 +00:00
|
|
|
return $this->rootContainer;
|
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
if(isset($this->containers[$path])) {
|
2012-04-24 22:11:10 +00:00
|
|
|
return $this->containers[$path];
|
|
|
|
}
|
2012-04-18 18:54:07 +00:00
|
|
|
try{
|
|
|
|
$container=$this->conn->get_container($this->getContainerName($path));
|
2012-04-24 22:11:10 +00:00
|
|
|
$this->containers[$path]=$container;
|
2012-04-18 18:54:07 +00:00
|
|
|
return $container;
|
2012-09-07 13:22:01 +00:00
|
|
|
}catch(NoSuchContainerException $e) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create container
|
|
|
|
* @param string path
|
|
|
|
* @return CF_Container
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private function createContainer($path) {
|
2012-10-11 13:52:21 +00:00
|
|
|
if($path=='' or $path=='/' or $path=='.') {
|
2012-04-18 18:54:07 +00:00
|
|
|
return $this->conn->create_container($this->getContainerName($path));
|
|
|
|
}
|
|
|
|
$parent=dirname($path);
|
2012-10-11 13:52:21 +00:00
|
|
|
if($parent=='' or $parent=='/' or $parent=='.') {
|
2012-04-18 18:54:07 +00:00
|
|
|
$parentContainer=$this->rootContainer;
|
|
|
|
}else{
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!$this->containerExists($parent)) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$parentContainer=$this->createContainer($parent);
|
|
|
|
}else{
|
|
|
|
$parentContainer=$this->getContainer($parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->addSubContainer($parentContainer,basename($path));
|
|
|
|
return $this->conn->create_container($this->getContainerName($path));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get object by path
|
|
|
|
* @param string path
|
|
|
|
* @return CF_Object
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private function getObject($path) {
|
|
|
|
if(isset($this->objects[$path])) {
|
2012-04-24 22:11:10 +00:00
|
|
|
return $this->objects[$path];
|
|
|
|
}
|
2012-04-18 18:54:07 +00:00
|
|
|
$container=$this->getContainer(dirname($path));
|
2012-09-07 13:22:01 +00:00
|
|
|
if(is_null($container)) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return null;
|
|
|
|
}else{
|
2012-10-11 13:52:21 +00:00
|
|
|
if ($path=="/" or $path=='') {
|
|
|
|
return null;
|
|
|
|
}
|
2012-04-18 18:54:07 +00:00
|
|
|
try{
|
|
|
|
$obj=$container->get_object(basename($path));
|
2012-04-24 22:11:10 +00:00
|
|
|
$this->objects[$path]=$obj;
|
2012-04-18 18:54:07 +00:00
|
|
|
return $obj;
|
2012-09-07 13:22:01 +00:00
|
|
|
}catch(NoSuchObjectException $e) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the names of all objects in a container
|
|
|
|
* @param CF_Container
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private function getObjects($container) {
|
|
|
|
if(is_null($container)) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return array();
|
|
|
|
}else{
|
|
|
|
$files=$container->get_objects();
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach($files as &$file) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$file=$file->name;
|
|
|
|
}
|
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create object
|
|
|
|
* @param string path
|
|
|
|
* @return CF_Object
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private function createObject($path) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$container=$this->getContainer(dirname($path));
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!is_null($container)) {
|
2012-10-11 13:52:21 +00:00
|
|
|
$container=$this->createContainer(dirname($path));
|
2012-04-18 18:54:07 +00:00
|
|
|
}
|
|
|
|
return $container->create_object(basename($path));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if an object exists
|
|
|
|
* @param string
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private function objectExists($path) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return !is_null($this->getObject($path));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if container for path exists
|
|
|
|
* @param string path
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private function containerExists($path) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return !is_null($this->getContainer($path));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the list of emulated sub containers
|
|
|
|
* @param CF_Container container
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private function getSubContainers($container) {
|
2012-05-02 10:54:31 +00:00
|
|
|
$tmpFile=OCP\Files::tmpFile();
|
2012-04-18 18:54:07 +00:00
|
|
|
$obj=$this->getSubContainerFile($container);
|
|
|
|
try{
|
|
|
|
$obj->save_to_filename($tmpFile);
|
2012-09-07 13:22:01 +00:00
|
|
|
}catch(Exception $e) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
$obj->save_to_filename($tmpFile);
|
|
|
|
$containers=file($tmpFile);
|
|
|
|
unlink($tmpFile);
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach($containers as &$sub) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$sub=trim($sub);
|
|
|
|
}
|
|
|
|
return $containers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add an emulated sub container
|
|
|
|
* @param CF_Container container
|
|
|
|
* @param string name
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private function addSubContainer($container,$name) {
|
|
|
|
if(!$name) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-05-02 10:54:31 +00:00
|
|
|
$tmpFile=OCP\Files::tmpFile();
|
2012-04-18 18:54:07 +00:00
|
|
|
$obj=$this->getSubContainerFile($container);
|
|
|
|
try{
|
|
|
|
$obj->save_to_filename($tmpFile);
|
|
|
|
$containers=file($tmpFile);
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach($containers as &$sub) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$sub=trim($sub);
|
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
if(array_search($name,$containers)!==false) {
|
2012-04-18 18:54:07 +00:00
|
|
|
unlink($tmpFile);
|
|
|
|
return false;
|
|
|
|
}else{
|
|
|
|
$fh=fopen($tmpFile,'a');
|
|
|
|
fwrite($fh,$name."\n");
|
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
}catch(Exception $e) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$containers=array();
|
|
|
|
file_put_contents($tmpFile,$name."\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
$obj->load_from_filename($tmpFile);
|
|
|
|
unlink($tmpFile);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove an emulated sub container
|
|
|
|
* @param CF_Container container
|
|
|
|
* @param string name
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private function removeSubContainer($container,$name) {
|
|
|
|
if(!$name) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-05-02 10:54:31 +00:00
|
|
|
$tmpFile=OCP\Files::tmpFile();
|
2012-04-18 18:54:07 +00:00
|
|
|
$obj=$this->getSubContainerFile($container);
|
|
|
|
try{
|
|
|
|
$obj->save_to_filename($tmpFile);
|
|
|
|
$containers=file($tmpFile);
|
2012-09-07 13:22:01 +00:00
|
|
|
}catch(Exception $e) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach($containers as &$sub) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$sub=trim($sub);
|
|
|
|
}
|
|
|
|
$i=array_search($name,$containers);
|
2012-09-07 13:22:01 +00:00
|
|
|
if($i===false) {
|
2012-04-18 18:54:07 +00:00
|
|
|
unlink($tmpFile);
|
|
|
|
return false;
|
|
|
|
}else{
|
|
|
|
unset($containers[$i]);
|
|
|
|
file_put_contents($tmpFile,implode("\n",$containers)."\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
$obj->load_from_filename($tmpFile);
|
|
|
|
unlink($tmpFile);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ensure a subcontainer file exists and return it's object
|
|
|
|
* @param CF_Container container
|
|
|
|
* @return CF_Object
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private function getSubContainerFile($container) {
|
2012-04-18 18:54:07 +00:00
|
|
|
try{
|
|
|
|
return $container->get_object(self::SUBCONTAINER_FILE);
|
2012-09-07 13:22:01 +00:00
|
|
|
}catch(NoSuchObjectException $e) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return $container->create_object(self::SUBCONTAINER_FILE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function __construct($params) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$this->token=$params['token'];
|
|
|
|
$this->host=$params['host'];
|
|
|
|
$this->user=$params['user'];
|
|
|
|
$this->root=isset($params['root'])?$params['root']:'/';
|
|
|
|
$this->secure=isset($params['secure'])?(bool)$params['secure']:true;
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!$this->root || $this->root[0]!='/') {
|
2012-04-18 18:54:07 +00:00
|
|
|
$this->root='/'.$this->root;
|
|
|
|
}
|
2012-09-07 16:30:48 +00:00
|
|
|
$this->auth = new \CF_Authentication($this->user, $this->token, null, $this->host);
|
2012-04-18 18:54:07 +00:00
|
|
|
$this->auth->authenticate();
|
2012-08-29 06:42:49 +00:00
|
|
|
|
2012-09-07 16:30:48 +00:00
|
|
|
$this->conn = new \CF_Connection($this->auth);
|
2012-04-18 18:54:07 +00:00
|
|
|
|
2012-10-11 13:52:21 +00:00
|
|
|
if(!$this->containerExists('/')) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$this->rootContainer=$this->createContainer('/');
|
|
|
|
}else{
|
|
|
|
$this->rootContainer=$this->getContainer('/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function mkdir($path) {
|
|
|
|
if($this->containerExists($path)) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return false;
|
|
|
|
}else{
|
|
|
|
$this->createContainer($path);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function rmdir($path) {
|
|
|
|
if(!$this->containerExists($path)) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return false;
|
|
|
|
}else{
|
|
|
|
$this->emptyContainer($path);
|
2012-09-07 13:22:01 +00:00
|
|
|
if($path!='' and $path!='/') {
|
2012-04-18 18:54:07 +00:00
|
|
|
$parentContainer=$this->getContainer(dirname($path));
|
|
|
|
$this->removeSubContainer($parentContainer,basename($path));
|
|
|
|
}
|
2012-08-29 06:42:49 +00:00
|
|
|
|
2012-04-18 18:54:07 +00:00
|
|
|
$this->conn->delete_container($this->getContainerName($path));
|
2012-04-24 22:11:10 +00:00
|
|
|
unset($this->containers[$path]);
|
2012-04-18 18:54:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
private function emptyContainer($path) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$container=$this->getContainer($path);
|
2012-09-07 13:22:01 +00:00
|
|
|
if(is_null($container)) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$subContainers=$this->getSubContainers($container);
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach($subContainers as $sub) {
|
|
|
|
if($sub) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$this->emptyContainer($path.'/'.$sub);
|
|
|
|
$this->conn->delete_container($this->getContainerName($path.'/'.$sub));
|
2012-04-24 22:11:10 +00:00
|
|
|
unset($this->containers[$path.'/'.$sub]);
|
2012-04-18 18:54:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$objects=$this->getObjects($container);
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach($objects as $object) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$container->delete_object($object);
|
2012-04-24 22:11:10 +00:00
|
|
|
unset($this->objects[$path.'/'.$object]);
|
2012-04-18 18:54:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function opendir($path) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$container=$this->getContainer($path);
|
|
|
|
$files=$this->getObjects($container);
|
|
|
|
$i=array_search(self::SUBCONTAINER_FILE,$files);
|
2012-09-07 13:22:01 +00:00
|
|
|
if($i!==false) {
|
2012-04-18 18:54:07 +00:00
|
|
|
unset($files[$i]);
|
|
|
|
}
|
|
|
|
$subContainers=$this->getSubContainers($container);
|
|
|
|
$files=array_merge($files,$subContainers);
|
|
|
|
$id=$this->getContainerName($path);
|
2012-09-07 16:30:48 +00:00
|
|
|
\OC_FakeDirStream::$dirs[$id]=$files;
|
2012-04-18 18:54:07 +00:00
|
|
|
return opendir('fakedir://'.$id);
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function filetype($path) {
|
|
|
|
if($this->containerExists($path)) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return 'dir';
|
|
|
|
}else{
|
|
|
|
return 'file';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function isReadable($path) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function isUpdatable($path) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function file_exists($path) {
|
|
|
|
if($this->is_dir($path)) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return $this->objectExists($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function file_get_contents($path) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$obj=$this->getObject($path);
|
2012-09-07 13:22:01 +00:00
|
|
|
if(is_null($obj)) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $obj->read();
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function file_put_contents($path,$content) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$obj=$this->getObject($path);
|
2012-09-07 13:22:01 +00:00
|
|
|
if(is_null($obj)) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$container=$this->getContainer(dirname($path));
|
2012-09-07 13:22:01 +00:00
|
|
|
if(is_null($container)) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$obj=$container->create_object(basename($path));
|
|
|
|
}
|
2012-04-18 22:25:47 +00:00
|
|
|
$this->resetMTime($obj);
|
2012-04-18 18:54:07 +00:00
|
|
|
return $obj->write($content);
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function unlink($path) {
|
2012-10-11 13:52:21 +00:00
|
|
|
if($this->containerExists($path)) {
|
|
|
|
return $this->rmdir($path);
|
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
if($this->objectExists($path)) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$container=$this->getContainer(dirname($path));
|
|
|
|
$container->delete_object(basename($path));
|
2012-04-24 22:11:10 +00:00
|
|
|
unset($this->objects[$path]);
|
2012-04-18 18:54:07 +00:00
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function fopen($path,$mode) {
|
|
|
|
switch($mode) {
|
2012-04-18 18:54:07 +00:00
|
|
|
case 'r':
|
|
|
|
case 'rb':
|
2012-10-11 13:52:21 +00:00
|
|
|
$obj=$this->getObject($path);
|
|
|
|
if (is_null($obj)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-18 18:54:07 +00:00
|
|
|
$fp = fopen('php://temp', 'r+');
|
|
|
|
$obj->stream($fp);
|
2012-08-29 06:42:49 +00:00
|
|
|
|
2012-04-18 18:54:07 +00:00
|
|
|
rewind($fp);
|
|
|
|
return $fp;
|
|
|
|
case 'w':
|
|
|
|
case 'wb':
|
|
|
|
case 'a':
|
|
|
|
case 'ab':
|
|
|
|
case 'r+':
|
|
|
|
case 'w+':
|
|
|
|
case 'wb+':
|
|
|
|
case 'a+':
|
|
|
|
case 'x':
|
|
|
|
case 'x+':
|
|
|
|
case 'c':
|
|
|
|
case 'c+':
|
|
|
|
$tmpFile=$this->getTmpFile($path);
|
2012-09-07 16:30:48 +00:00
|
|
|
\OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this,'writeBack');
|
2012-04-18 18:54:07 +00:00
|
|
|
self::$tempFiles[$tmpFile]=$path;
|
|
|
|
return fopen('close://'.$tmpFile,$mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function writeBack($tmpFile) {
|
|
|
|
if(isset(self::$tempFiles[$tmpFile])) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$this->fromTmpFile($tmpFile,self::$tempFiles[$tmpFile]);
|
|
|
|
unlink($tmpFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function free_space($path) {
|
2012-10-11 13:52:21 +00:00
|
|
|
return 1024*1024*1024*8;
|
2012-04-18 18:54:07 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function touch($path,$mtime=null) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$obj=$this->getObject($path);
|
2012-09-07 13:22:01 +00:00
|
|
|
if(is_null($obj)) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
if(is_null($mtime)) {
|
2012-04-18 22:25:47 +00:00
|
|
|
$mtime=time();
|
|
|
|
}
|
2012-08-29 06:42:49 +00:00
|
|
|
|
2012-04-18 22:25:47 +00:00
|
|
|
//emulate setting mtime with metadata
|
|
|
|
$obj->metadata['Mtime']=$mtime;
|
|
|
|
$obj->sync_metadata();
|
2012-04-18 18:54:07 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function rename($path1,$path2) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$sourceContainer=$this->getContainer(dirname($path1));
|
|
|
|
$targetContainer=$this->getContainer(dirname($path2));
|
2012-04-18 22:25:47 +00:00
|
|
|
$result=$sourceContainer->move_object_to(basename($path1),$targetContainer,basename($path2));
|
2012-04-24 22:11:10 +00:00
|
|
|
unset($this->objects[$path1]);
|
2012-09-07 13:22:01 +00:00
|
|
|
if($result) {
|
2012-04-18 22:25:47 +00:00
|
|
|
$targetObj=$this->getObject($path2);
|
|
|
|
$this->resetMTime($targetObj);
|
|
|
|
}
|
|
|
|
return $result;
|
2012-04-18 18:54:07 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function copy($path1,$path2) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$sourceContainer=$this->getContainer(dirname($path1));
|
|
|
|
$targetContainer=$this->getContainer(dirname($path2));
|
2012-04-18 22:25:47 +00:00
|
|
|
$result=$sourceContainer->copy_object_to(basename($path1),$targetContainer,basename($path2));
|
2012-09-07 13:22:01 +00:00
|
|
|
if($result) {
|
2012-04-18 22:25:47 +00:00
|
|
|
$targetObj=$this->getObject($path2);
|
|
|
|
$this->resetMTime($targetObj);
|
|
|
|
}
|
|
|
|
return $result;
|
2012-04-18 18:54:07 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function stat($path) {
|
2012-10-11 13:52:21 +00:00
|
|
|
$container=$this->getContainer($path);
|
|
|
|
if (!is_null($container)) {
|
|
|
|
return array(
|
|
|
|
'mtime'=>-1,
|
|
|
|
'size'=>$container->bytes_used,
|
|
|
|
'ctime'=>-1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-04-18 18:54:07 +00:00
|
|
|
$obj=$this->getObject($path);
|
2012-10-11 13:52:21 +00:00
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
if(is_null($obj)) {
|
2012-04-18 18:54:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-18 22:25:47 +00:00
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
if(isset($obj->metadata['Mtime']) and $obj->metadata['Mtime']>-1) {
|
2012-04-18 22:25:47 +00:00
|
|
|
$mtime=$obj->metadata['Mtime'];
|
|
|
|
}else{
|
|
|
|
$mtime=strtotime($obj->last_modified);
|
|
|
|
}
|
2012-04-18 18:54:07 +00:00
|
|
|
return array(
|
2012-04-18 22:25:47 +00:00
|
|
|
'mtime'=>$mtime,
|
2012-04-18 18:54:07 +00:00
|
|
|
'size'=>$obj->content_length,
|
|
|
|
'ctime'=>-1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
private function getTmpFile($path) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$obj=$this->getObject($path);
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!is_null($obj)) {
|
2012-05-02 10:54:31 +00:00
|
|
|
$tmpFile=OCP\Files::tmpFile();
|
2012-04-18 18:54:07 +00:00
|
|
|
$obj->save_to_filename($tmpFile);
|
|
|
|
return $tmpFile;
|
|
|
|
}else{
|
2012-10-11 13:52:21 +00:00
|
|
|
return OCP\Files::tmpFile();
|
2012-04-18 18:54:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
private function fromTmpFile($tmpFile,$path) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$obj=$this->getObject($path);
|
2012-09-07 13:22:01 +00:00
|
|
|
if(is_null($obj)) {
|
2012-04-18 18:54:07 +00:00
|
|
|
$obj=$this->createObject($path);
|
|
|
|
}
|
|
|
|
$obj->load_from_filename($tmpFile);
|
2012-04-18 22:25:47 +00:00
|
|
|
$this->resetMTime($obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove custom mtime metadata
|
|
|
|
* @param CF_Object obj
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private function resetMTime($obj) {
|
|
|
|
if(isset($obj->metadata['Mtime'])) {
|
2012-04-18 22:25:47 +00:00
|
|
|
$obj->metadata['Mtime']=-1;
|
|
|
|
$obj->sync_metadata();
|
|
|
|
}
|
2012-04-18 18:54:07 +00:00
|
|
|
}
|
|
|
|
}
|