mappedlocal.php 9.3 KB

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