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