swift.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. require_once('php-cloudfiles/cloudfiles.php');
  9. class OC_FileStorage_SWIFT extends OC_Filestorage_Common{
  10. private $host;
  11. private $root;
  12. private $user;
  13. private $token;
  14. private $secure;
  15. /**
  16. * @var CF_Authentication auth
  17. */
  18. private $auth;
  19. /**
  20. * @var CF_Connection conn
  21. */
  22. private $conn;
  23. /**
  24. * @var CF_Container rootContainer
  25. */
  26. private $rootContainer;
  27. private static $tempFiles=array();
  28. private $objects=array();
  29. private $containers=array();
  30. const SUBCONTAINER_FILE='.subcontainers';
  31. /**
  32. * translate directory path to container name
  33. * @param string path
  34. * @return string
  35. */
  36. private function getContainerName($path){
  37. $path=trim($this->root.$path,'/');
  38. return str_replace('/','\\',$path);
  39. }
  40. /**
  41. * get container by path
  42. * @param string path
  43. * @return CF_Container
  44. */
  45. private function getContainer($path){
  46. if($path=='' or $path=='/'){
  47. return $this->rootContainer;
  48. }
  49. if(isset($this->containers[$path])){
  50. return $this->containers[$path];
  51. }
  52. try{
  53. $container=$this->conn->get_container($this->getContainerName($path));
  54. $this->containers[$path]=$container;
  55. return $container;
  56. }catch(NoSuchContainerException $e){
  57. return null;
  58. }
  59. }
  60. /**
  61. * create container
  62. * @param string path
  63. * @return CF_Container
  64. */
  65. private function createContainer($path){
  66. if($path=='' or $path=='/'){
  67. return $this->conn->create_container($this->getContainerName($path));
  68. }
  69. $parent=dirname($path);
  70. if($parent=='' or $parent=='/'){
  71. $parentContainer=$this->rootContainer;
  72. }else{
  73. if(!$this->containerExists($parent)){
  74. $parentContainer=$this->createContainer($parent);
  75. }else{
  76. $parentContainer=$this->getContainer($parent);
  77. }
  78. }
  79. $this->addSubContainer($parentContainer,basename($path));
  80. return $this->conn->create_container($this->getContainerName($path));
  81. }
  82. /**
  83. * get object by path
  84. * @param string path
  85. * @return CF_Object
  86. */
  87. private function getObject($path){
  88. if(isset($this->objects[$path])){
  89. return $this->objects[$path];
  90. }
  91. $container=$this->getContainer(dirname($path));
  92. if(is_null($container)){
  93. return null;
  94. }else{
  95. try{
  96. $obj=$container->get_object(basename($path));
  97. $this->objects[$path]=$obj;
  98. return $obj;
  99. }catch(NoSuchObjectException $e){
  100. return null;
  101. }
  102. }
  103. }
  104. /**
  105. * get the names of all objects in a container
  106. * @param CF_Container
  107. * @return array
  108. */
  109. private function getObjects($container){
  110. if(is_null($container)){
  111. return array();
  112. }else{
  113. $files=$container->get_objects();
  114. foreach($files as &$file){
  115. $file=$file->name;
  116. }
  117. return $files;
  118. }
  119. }
  120. /**
  121. * create object
  122. * @param string path
  123. * @return CF_Object
  124. */
  125. private function createObject($path){
  126. $container=$this->getContainer(dirname($path));
  127. if(!is_null($container)){
  128. $container=$this->createContainer($path);
  129. }
  130. return $container->create_object(basename($path));
  131. }
  132. /**
  133. * check if an object exists
  134. * @param string
  135. * @return bool
  136. */
  137. private function objectExists($path){
  138. return !is_null($this->getObject($path));
  139. }
  140. /**
  141. * check if container for path exists
  142. * @param string path
  143. * @return bool
  144. */
  145. private function containerExists($path){
  146. return !is_null($this->getContainer($path));
  147. }
  148. /**
  149. * get the list of emulated sub containers
  150. * @param CF_Container container
  151. * @return array
  152. */
  153. private function getSubContainers($container){
  154. $tmpFile=OCP\Files::tmpFile();
  155. $obj=$this->getSubContainerFile($container);
  156. try{
  157. $obj->save_to_filename($tmpFile);
  158. }catch(Exception $e){
  159. return array();
  160. }
  161. $obj->save_to_filename($tmpFile);
  162. $containers=file($tmpFile);
  163. unlink($tmpFile);
  164. foreach($containers as &$sub){
  165. $sub=trim($sub);
  166. }
  167. return $containers;
  168. }
  169. /**
  170. * add an emulated sub container
  171. * @param CF_Container container
  172. * @param string name
  173. * @return bool
  174. */
  175. private function addSubContainer($container,$name){
  176. if(!$name){
  177. return false;
  178. }
  179. $tmpFile=OCP\Files::tmpFile();
  180. $obj=$this->getSubContainerFile($container);
  181. try{
  182. $obj->save_to_filename($tmpFile);
  183. $containers=file($tmpFile);
  184. foreach($containers as &$sub){
  185. $sub=trim($sub);
  186. }
  187. if(array_search($name,$containers)!==false){
  188. unlink($tmpFile);
  189. return false;
  190. }else{
  191. $fh=fopen($tmpFile,'a');
  192. fwrite($fh,$name."\n");
  193. }
  194. }catch(Exception $e){
  195. $containers=array();
  196. file_put_contents($tmpFile,$name."\n");
  197. }
  198. $obj->load_from_filename($tmpFile);
  199. unlink($tmpFile);
  200. return true;
  201. }
  202. /**
  203. * remove an emulated sub container
  204. * @param CF_Container container
  205. * @param string name
  206. * @return bool
  207. */
  208. private function removeSubContainer($container,$name){
  209. if(!$name){
  210. return false;
  211. }
  212. $tmpFile=OCP\Files::tmpFile();
  213. $obj=$this->getSubContainerFile($container);
  214. try{
  215. $obj->save_to_filename($tmpFile);
  216. $containers=file($tmpFile);
  217. }catch(Exception $e){
  218. return false;
  219. }
  220. foreach($containers as &$sub){
  221. $sub=trim($sub);
  222. }
  223. $i=array_search($name,$containers);
  224. if($i===false){
  225. unlink($tmpFile);
  226. return false;
  227. }else{
  228. unset($containers[$i]);
  229. file_put_contents($tmpFile,implode("\n",$containers)."\n");
  230. }
  231. $obj->load_from_filename($tmpFile);
  232. unlink($tmpFile);
  233. return true;
  234. }
  235. /**
  236. * ensure a subcontainer file exists and return it's object
  237. * @param CF_Container container
  238. * @return CF_Object
  239. */
  240. private function getSubContainerFile($container){
  241. try{
  242. return $container->get_object(self::SUBCONTAINER_FILE);
  243. }catch(NoSuchObjectException $e){
  244. return $container->create_object(self::SUBCONTAINER_FILE);
  245. }
  246. }
  247. public function __construct($params){
  248. $this->token=$params['token'];
  249. $this->host=$params['host'];
  250. $this->user=$params['user'];
  251. $this->root=isset($params['root'])?$params['root']:'/';
  252. $this->secure=isset($params['secure'])?(bool)$params['secure']:true;
  253. if(substr($this->root,0,1)!='/'){
  254. $this->root='/'.$this->root;
  255. }
  256. $this->auth = new CF_Authentication($this->user, $this->token, null, $this->host);
  257. $this->auth->authenticate();
  258. $this->conn = new CF_Connection($this->auth);
  259. if(!$this->containerExists($this->root)){
  260. $this->rootContainer=$this->createContainer('/');
  261. }else{
  262. $this->rootContainer=$this->getContainer('/');
  263. }
  264. }
  265. public function mkdir($path){
  266. if($this->containerExists($path)){
  267. return false;
  268. }else{
  269. $this->createContainer($path);
  270. return true;
  271. }
  272. }
  273. public function rmdir($path){
  274. if(!$this->containerExists($path)){
  275. return false;
  276. }else{
  277. $this->emptyContainer($path);
  278. if($path!='' and $path!='/'){
  279. $parentContainer=$this->getContainer(dirname($path));
  280. $this->removeSubContainer($parentContainer,basename($path));
  281. }
  282. $this->conn->delete_container($this->getContainerName($path));
  283. unset($this->containers[$path]);
  284. return true;
  285. }
  286. }
  287. private function emptyContainer($path){
  288. $container=$this->getContainer($path);
  289. if(is_null($container)){
  290. return;
  291. }
  292. $subContainers=$this->getSubContainers($container);
  293. foreach($subContainers as $sub){
  294. if($sub){
  295. $this->emptyContainer($path.'/'.$sub);
  296. $this->conn->delete_container($this->getContainerName($path.'/'.$sub));
  297. unset($this->containers[$path.'/'.$sub]);
  298. }
  299. }
  300. $objects=$this->getObjects($container);
  301. foreach($objects as $object){
  302. $container->delete_object($object);
  303. unset($this->objects[$path.'/'.$object]);
  304. }
  305. }
  306. public function opendir($path){
  307. $container=$this->getContainer($path);
  308. $files=$this->getObjects($container);
  309. $i=array_search(self::SUBCONTAINER_FILE,$files);
  310. if($i!==false){
  311. unset($files[$i]);
  312. }
  313. $subContainers=$this->getSubContainers($container);
  314. $files=array_merge($files,$subContainers);
  315. $id=$this->getContainerName($path);
  316. OC_FakeDirStream::$dirs[$id]=$files;
  317. return opendir('fakedir://'.$id);
  318. }
  319. public function filetype($path){
  320. if($this->containerExists($path)){
  321. return 'dir';
  322. }else{
  323. return 'file';
  324. }
  325. }
  326. public function is_readable($path){
  327. return true;
  328. }
  329. public function is_writable($path){
  330. return true;
  331. }
  332. public function file_exists($path){
  333. if($this->is_dir($path)){
  334. return true;
  335. }else{
  336. return $this->objectExists($path);
  337. }
  338. }
  339. public function file_get_contents($path){
  340. $obj=$this->getObject($path);
  341. if(is_null($obj)){
  342. return false;
  343. }
  344. return $obj->read();
  345. }
  346. public function file_put_contents($path,$content){
  347. $obj=$this->getObject($path);
  348. if(is_null($obj)){
  349. $container=$this->getContainer(dirname($path));
  350. if(is_null($container)){
  351. return false;
  352. }
  353. $obj=$container->create_object(basename($path));
  354. }
  355. $this->resetMTime($obj);
  356. return $obj->write($content);
  357. }
  358. public function unlink($path){
  359. if($this->objectExists($path)){
  360. $container=$this->getContainer(dirname($path));
  361. $container->delete_object(basename($path));
  362. unset($this->objects[$path]);
  363. }else{
  364. return false;
  365. }
  366. }
  367. public function fopen($path,$mode){
  368. $obj=$this->getObject($path);
  369. if(is_null($obj)){
  370. return false;
  371. }
  372. switch($mode){
  373. case 'r':
  374. case 'rb':
  375. $fp = fopen('php://temp', 'r+');
  376. $obj->stream($fp);
  377. rewind($fp);
  378. return $fp;
  379. case 'w':
  380. case 'wb':
  381. case 'a':
  382. case 'ab':
  383. case 'r+':
  384. case 'w+':
  385. case 'wb+':
  386. case 'a+':
  387. case 'x':
  388. case 'x+':
  389. case 'c':
  390. case 'c+':
  391. $tmpFile=$this->getTmpFile($path);
  392. OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this,'writeBack');
  393. self::$tempFiles[$tmpFile]=$path;
  394. return fopen('close://'.$tmpFile,$mode);
  395. }
  396. }
  397. public function writeBack($tmpFile){
  398. if(isset(self::$tempFiles[$tmpFile])){
  399. $this->fromTmpFile($tmpFile,self::$tempFiles[$tmpFile]);
  400. unlink($tmpFile);
  401. }
  402. }
  403. public function free_space($path){
  404. return 0;
  405. }
  406. public function touch($path,$mtime=null){
  407. $obj=$this->getObject($path);
  408. if(is_null($obj)){
  409. return false;
  410. }
  411. if(is_null($mtime)){
  412. $mtime=time();
  413. }
  414. //emulate setting mtime with metadata
  415. $obj->metadata['Mtime']=$mtime;
  416. $obj->sync_metadata();
  417. }
  418. public function rename($path1,$path2){
  419. $sourceContainer=$this->getContainer(dirname($path1));
  420. $targetContainer=$this->getContainer(dirname($path2));
  421. $result=$sourceContainer->move_object_to(basename($path1),$targetContainer,basename($path2));
  422. unset($this->objects[$path1]);
  423. if($result){
  424. $targetObj=$this->getObject($path2);
  425. $this->resetMTime($targetObj);
  426. }
  427. return $result;
  428. }
  429. public function copy($path1,$path2){
  430. $sourceContainer=$this->getContainer(dirname($path1));
  431. $targetContainer=$this->getContainer(dirname($path2));
  432. $result=$sourceContainer->copy_object_to(basename($path1),$targetContainer,basename($path2));
  433. if($result){
  434. $targetObj=$this->getObject($path2);
  435. $this->resetMTime($targetObj);
  436. }
  437. return $result;
  438. }
  439. public function stat($path){
  440. $obj=$this->getObject($path);
  441. if(is_null($obj)){
  442. return false;
  443. }
  444. if(isset($obj->metadata['Mtime']) and $obj->metadata['Mtime']>-1){
  445. $mtime=$obj->metadata['Mtime'];
  446. }else{
  447. $mtime=strtotime($obj->last_modified);
  448. }
  449. return array(
  450. 'mtime'=>$mtime,
  451. 'size'=>$obj->content_length,
  452. 'ctime'=>-1,
  453. );
  454. }
  455. private function getTmpFile($path){
  456. $obj=$this->getObject($path);
  457. if(!is_null($obj)){
  458. $tmpFile=OCP\Files::tmpFile();
  459. $obj->save_to_filename($tmpFile);
  460. return $tmpFile;
  461. }else{
  462. return false;
  463. }
  464. }
  465. private function fromTmpFile($tmpFile,$path){
  466. $obj=$this->getObject($path);
  467. if(is_null($obj)){
  468. $obj=$this->createObject($path);
  469. }
  470. $obj->load_from_filename($tmpFile);
  471. $this->resetMTime($obj);
  472. }
  473. /**
  474. * remove custom mtime metadata
  475. * @param CF_Object obj
  476. */
  477. private function resetMTime($obj){
  478. if(isset($obj->metadata['Mtime'])){
  479. $obj->metadata['Mtime']=-1;
  480. $obj->sync_metadata();
  481. }
  482. }
  483. }