PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/files/storage/mappedlocal.php

https://github.com/sezuan/core
PHP | 363 lines | 312 code | 27 blank | 24 comment | 53 complexity | 77142f829b49e7e848e2bc0ce5850353 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  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. namespace OC\Files\Storage;
  9. /**
  10. * for local filestore, we only have to map the paths
  11. */
  12. class MappedLocal extends \OC\Files\Storage\Common{
  13. protected $datadir;
  14. private $mapper;
  15. public function __construct($arguments) {
  16. $this->datadir=$arguments['datadir'];
  17. if(substr($this->datadir, -1)!=='/') {
  18. $this->datadir.='/';
  19. }
  20. $this->mapper= new \OC\Files\Mapper($this->datadir);
  21. }
  22. public function __destruct() {
  23. if (defined('PHPUNIT_RUN')) {
  24. $this->mapper->removePath($this->datadir, true, true);
  25. }
  26. }
  27. public function getId(){
  28. return 'local::'.$this->datadir;
  29. }
  30. public function mkdir($path) {
  31. return @mkdir($this->buildPath($path));
  32. }
  33. public function rmdir($path) {
  34. try {
  35. $it = new \RecursiveIteratorIterator(
  36. new \RecursiveDirectoryIterator($this->buildPath($path)),
  37. \RecursiveIteratorIterator::CHILD_FIRST
  38. );
  39. foreach ($it as $file) {
  40. /**
  41. * @var \SplFileInfo $file
  42. */
  43. if (in_array($file->getBasename(), array('.', '..'))) {
  44. continue;
  45. } elseif ($file->isDir()) {
  46. rmdir($file->getPathname());
  47. } elseif ($file->isFile() || $file->isLink()) {
  48. unlink($file->getPathname());
  49. }
  50. }
  51. if ($result = @rmdir($this->buildPath($path))) {
  52. $this->cleanMapper($path);
  53. }
  54. return $result;
  55. } catch (\UnexpectedValueException $e) {
  56. return false;
  57. }
  58. }
  59. public function opendir($path) {
  60. $files = array('.', '..');
  61. $physicalPath= $this->buildPath($path);
  62. $logicalPath = $this->mapper->physicalToLogic($physicalPath);
  63. $dh = opendir($physicalPath);
  64. while ($file = readdir($dh)) {
  65. if ($file === '.' or $file === '..') {
  66. continue;
  67. }
  68. $logicalFilePath = $this->mapper->physicalToLogic($physicalPath.'/'.$file);
  69. $file= $this->mapper->stripRootFolder($logicalFilePath, $logicalPath);
  70. $file = $this->stripLeading($file);
  71. $files[]= $file;
  72. }
  73. \OC\Files\Stream\Dir::register('local-win32'.$path, $files);
  74. return opendir('fakedir://local-win32'.$path);
  75. }
  76. public function is_dir($path) {
  77. if(substr($path, -1)=='/') {
  78. $path=substr($path, 0, -1);
  79. }
  80. return is_dir($this->buildPath($path));
  81. }
  82. public function is_file($path) {
  83. return is_file($this->buildPath($path));
  84. }
  85. public function stat($path) {
  86. $fullPath = $this->buildPath($path);
  87. $statResult = stat($fullPath);
  88. if ($statResult['size'] < 0) {
  89. $size = self::getFileSizeFromOS($fullPath);
  90. $statResult['size'] = $size;
  91. $statResult[7] = $size;
  92. }
  93. return $statResult;
  94. }
  95. public function filetype($path) {
  96. $filetype=filetype($this->buildPath($path));
  97. if($filetype=='link') {
  98. $filetype=filetype(realpath($this->buildPath($path)));
  99. }
  100. return $filetype;
  101. }
  102. public function filesize($path) {
  103. if($this->is_dir($path)) {
  104. return 0;
  105. }else{
  106. $fullPath = $this->buildPath($path);
  107. $fileSize = filesize($fullPath);
  108. if ($fileSize < 0) {
  109. return self::getFileSizeFromOS($fullPath);
  110. }
  111. return $fileSize;
  112. }
  113. }
  114. public function isReadable($path) {
  115. return is_readable($this->buildPath($path));
  116. }
  117. public function isUpdatable($path) {
  118. return is_writable($this->buildPath($path));
  119. }
  120. public function file_exists($path) {
  121. return file_exists($this->buildPath($path));
  122. }
  123. public function filemtime($path) {
  124. return filemtime($this->buildPath($path));
  125. }
  126. public function touch($path, $mtime=null) {
  127. // sets the modification time of the file to the given value.
  128. // If mtime is nil the current time is set.
  129. // note that the access time of the file always changes to the current time.
  130. if(!is_null($mtime)) {
  131. $result=touch( $this->buildPath($path), $mtime );
  132. }else{
  133. $result=touch( $this->buildPath($path));
  134. }
  135. if( $result ) {
  136. clearstatcache( true, $this->buildPath($path) );
  137. }
  138. return $result;
  139. }
  140. public function file_get_contents($path) {
  141. return file_get_contents($this->buildPath($path));
  142. }
  143. public function file_put_contents($path, $data) {
  144. return file_put_contents($this->buildPath($path), $data);
  145. }
  146. public function unlink($path) {
  147. return $this->delTree($path);
  148. }
  149. public function rename($path1, $path2) {
  150. if (!$this->isUpdatable($path1)) {
  151. \OC_Log::write('core', 'unable to rename, file is not writable : '.$path1, \OC_Log::ERROR);
  152. return false;
  153. }
  154. if(! $this->file_exists($path1)) {
  155. \OC_Log::write('core', 'unable to rename, file does not exists : '.$path1, \OC_Log::ERROR);
  156. return false;
  157. }
  158. $physicPath1 = $this->buildPath($path1);
  159. $physicPath2 = $this->buildPath($path2);
  160. if($return=rename($physicPath1, $physicPath2)) {
  161. // mapper needs to create copies or all children
  162. $this->copyMapping($path1, $path2);
  163. $this->cleanMapper($physicPath1, false, true);
  164. }
  165. return $return;
  166. }
  167. public function copy($path1, $path2) {
  168. if($this->is_dir($path2)) {
  169. if(!$this->file_exists($path2)) {
  170. $this->mkdir($path2);
  171. }
  172. $source=substr($path1, strrpos($path1, '/')+1);
  173. $path2.=$source;
  174. }
  175. if($return=copy($this->buildPath($path1), $this->buildPath($path2))) {
  176. // mapper needs to create copies or all children
  177. $this->copyMapping($path1, $path2);
  178. }
  179. return $return;
  180. }
  181. public function fopen($path, $mode) {
  182. if($return=fopen($this->buildPath($path), $mode)) {
  183. switch($mode) {
  184. case 'r':
  185. break;
  186. case 'r+':
  187. case 'w+':
  188. case 'x+':
  189. case 'a+':
  190. break;
  191. case 'w':
  192. case 'x':
  193. case 'a':
  194. break;
  195. }
  196. }
  197. return $return;
  198. }
  199. public function getMimeType($path) {
  200. if($this->isReadable($path)) {
  201. return \OC_Helper::getMimeType($this->buildPath($path));
  202. }else{
  203. return false;
  204. }
  205. }
  206. private function delTree($dir, $isLogicPath=true) {
  207. $dirRelative=$dir;
  208. if ($isLogicPath) {
  209. $dir=$this->buildPath($dir);
  210. }
  211. if (!file_exists($dir)) {
  212. return true;
  213. }
  214. if (!is_dir($dir) || is_link($dir)) {
  215. if($return=unlink($dir)) {
  216. $this->cleanMapper($dir, false);
  217. return $return;
  218. }
  219. }
  220. foreach (scandir($dir) as $item) {
  221. if ($item == '.' || $item == '..') {
  222. continue;
  223. }
  224. if(is_file($dir.'/'.$item)) {
  225. if(unlink($dir.'/'.$item)) {
  226. $this->cleanMapper($dir.'/'.$item, false);
  227. }
  228. }elseif(is_dir($dir.'/'.$item)) {
  229. if (!$this->delTree($dir. "/" . $item, false)) {
  230. return false;
  231. };
  232. }
  233. }
  234. if($return=rmdir($dir)) {
  235. $this->cleanMapper($dir, false);
  236. }
  237. return $return;
  238. }
  239. private static function getFileSizeFromOS($fullPath) {
  240. $name = strtolower(php_uname('s'));
  241. // Windows OS: we use COM to access the filesystem
  242. if (strpos($name, 'win') !== false) {
  243. if (class_exists('COM')) {
  244. $fsobj = new \COM("Scripting.FileSystemObject");
  245. $f = $fsobj->GetFile($fullPath);
  246. return $f->Size;
  247. }
  248. } else if (strpos($name, 'bsd') !== false) {
  249. if (\OC_Helper::is_function_enabled('exec')) {
  250. return (float)exec('stat -f %z ' . escapeshellarg($fullPath));
  251. }
  252. } else if (strpos($name, 'linux') !== false) {
  253. if (\OC_Helper::is_function_enabled('exec')) {
  254. return (float)exec('stat -c %s ' . escapeshellarg($fullPath));
  255. }
  256. } else {
  257. \OC_Log::write('core',
  258. 'Unable to determine file size of "'.$fullPath.'". Unknown OS: '.$name,
  259. \OC_Log::ERROR);
  260. }
  261. return 0;
  262. }
  263. public function hash($path, $type, $raw=false) {
  264. return hash_file($type, $this->buildPath($path), $raw);
  265. }
  266. public function free_space($path) {
  267. return @disk_free_space($this->buildPath($path));
  268. }
  269. public function search($query) {
  270. return $this->searchInDir($query);
  271. }
  272. public function getLocalFile($path) {
  273. return $this->buildPath($path);
  274. }
  275. public function getLocalFolder($path) {
  276. return $this->buildPath($path);
  277. }
  278. protected function searchInDir($query, $dir='') {
  279. $files=array();
  280. $physicalDir = $this->buildPath($dir);
  281. foreach (scandir($physicalDir) as $item) {
  282. if ($item == '.' || $item == '..')
  283. continue;
  284. $physicalItem = $this->mapper->physicalToLogic($physicalDir.'/'.$item);
  285. $item = substr($physicalItem, strlen($physicalDir)+1);
  286. if(strstr(strtolower($item), strtolower($query)) !== false) {
  287. $files[]=$dir.'/'.$item;
  288. }
  289. if(is_dir($physicalItem)) {
  290. $files=array_merge($files, $this->searchInDir($query, $dir.'/'.$item));
  291. }
  292. }
  293. return $files;
  294. }
  295. /**
  296. * check if a file or folder has been updated since $time
  297. * @param string $path
  298. * @param int $time
  299. * @return bool
  300. */
  301. public function hasUpdated($path, $time) {
  302. return $this->filemtime($path)>$time;
  303. }
  304. private function buildPath($path, $create=true) {
  305. $path = $this->stripLeading($path);
  306. $fullPath = $this->datadir.$path;
  307. return $this->mapper->logicToPhysical($fullPath, $create);
  308. }
  309. private function cleanMapper($path, $isLogicPath=true, $recursive=true) {
  310. $fullPath = $path;
  311. if ($isLogicPath) {
  312. $fullPath = $this->datadir.$path;
  313. }
  314. $this->mapper->removePath($fullPath, $isLogicPath, $recursive);
  315. }
  316. private function copyMapping($path1, $path2) {
  317. $path1 = $this->stripLeading($path1);
  318. $path2 = $this->stripLeading($path2);
  319. $fullPath1 = $this->datadir.$path1;
  320. $fullPath2 = $this->datadir.$path2;
  321. $this->mapper->copy($fullPath1, $fullPath2);
  322. }
  323. private function stripLeading($path) {
  324. if(strpos($path, '/') === 0) {
  325. $path = substr($path, 1);
  326. }
  327. if(strpos($path, '\\') === 0) {
  328. $path = substr($path, 1);
  329. }
  330. if ($path === false) {
  331. return '';
  332. }
  333. return $path;
  334. }
  335. }