Local.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Boris Rybalkin <ribalkin@gmail.com>
  7. * @author Brice Maron <brice@bmaron.net>
  8. * @author Jakob Sack <mail@jakobsack.de>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Klaas Freitag <freitag@owncloud.com>
  12. * @author Lukas Reschke <lukas@statuscode.ch>
  13. * @author Martin Mattel <martin.mattel@diemattels.at>
  14. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  15. * @author Morris Jobke <hey@morrisjobke.de>
  16. * @author Robin Appelman <robin@icewind.nl>
  17. * @author Roeland Jago Douma <roeland@famdouma.nl>
  18. * @author Sjors van der Pluijm <sjors@desjors.nl>
  19. * @author Stefan Weil <sw@weilnetz.de>
  20. * @author Thomas Müller <thomas.mueller@tmit.eu>
  21. * @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
  22. * @author Vincent Petry <pvince81@owncloud.com>
  23. *
  24. * @license AGPL-3.0
  25. *
  26. * This code is free software: you can redistribute it and/or modify
  27. * it under the terms of the GNU Affero General Public License, version 3,
  28. * as published by the Free Software Foundation.
  29. *
  30. * This program is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. * GNU Affero General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU Affero General Public License, version 3,
  36. * along with this program. If not, see <http://www.gnu.org/licenses/>
  37. *
  38. */
  39. namespace OC\Files\Storage;
  40. use OC\Files\Storage\Wrapper\Jail;
  41. use OCP\Files\ForbiddenException;
  42. use OCP\Files\Storage\IStorage;
  43. /**
  44. * for local filestore, we only have to map the paths
  45. */
  46. class Local extends \OC\Files\Storage\Common {
  47. protected $datadir;
  48. protected $dataDirLength;
  49. protected $allowSymlinks = false;
  50. protected $realDataDir;
  51. public function __construct($arguments) {
  52. if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) {
  53. throw new \InvalidArgumentException('No data directory set for local storage');
  54. }
  55. $this->datadir = str_replace('//', '/', $arguments['datadir']);
  56. // some crazy code uses a local storage on root...
  57. if ($this->datadir === '/') {
  58. $this->realDataDir = $this->datadir;
  59. } else {
  60. $realPath = realpath($this->datadir) ?: $this->datadir;
  61. $this->realDataDir = rtrim($realPath, '/') . '/';
  62. }
  63. if (substr($this->datadir, -1) !== '/') {
  64. $this->datadir .= '/';
  65. }
  66. $this->dataDirLength = strlen($this->realDataDir);
  67. }
  68. public function __destruct() {
  69. }
  70. public function getId() {
  71. return 'local::' . $this->datadir;
  72. }
  73. public function mkdir($path) {
  74. return @mkdir($this->getSourcePath($path), 0777, true);
  75. }
  76. public function rmdir($path) {
  77. if (!$this->isDeletable($path)) {
  78. return false;
  79. }
  80. try {
  81. $it = new \RecursiveIteratorIterator(
  82. new \RecursiveDirectoryIterator($this->getSourcePath($path)),
  83. \RecursiveIteratorIterator::CHILD_FIRST
  84. );
  85. /**
  86. * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach
  87. * This bug is fixed in PHP 5.5.9 or before
  88. * See #8376
  89. */
  90. $it->rewind();
  91. while ($it->valid()) {
  92. /**
  93. * @var \SplFileInfo $file
  94. */
  95. $file = $it->current();
  96. if (in_array($file->getBasename(), array('.', '..'))) {
  97. $it->next();
  98. continue;
  99. } elseif ($file->isDir()) {
  100. rmdir($file->getPathname());
  101. } elseif ($file->isFile() || $file->isLink()) {
  102. unlink($file->getPathname());
  103. }
  104. $it->next();
  105. }
  106. return rmdir($this->getSourcePath($path));
  107. } catch (\UnexpectedValueException $e) {
  108. return false;
  109. }
  110. }
  111. public function opendir($path) {
  112. return opendir($this->getSourcePath($path));
  113. }
  114. public function is_dir($path) {
  115. if (substr($path, -1) == '/') {
  116. $path = substr($path, 0, -1);
  117. }
  118. return is_dir($this->getSourcePath($path));
  119. }
  120. public function is_file($path) {
  121. return is_file($this->getSourcePath($path));
  122. }
  123. public function stat($path) {
  124. clearstatcache();
  125. $fullPath = $this->getSourcePath($path);
  126. $statResult = stat($fullPath);
  127. if (PHP_INT_SIZE === 4 && !$this->is_dir($path)) {
  128. $filesize = $this->filesize($path);
  129. $statResult['size'] = $filesize;
  130. $statResult[7] = $filesize;
  131. }
  132. return $statResult;
  133. }
  134. public function filetype($path) {
  135. $filetype = filetype($this->getSourcePath($path));
  136. if ($filetype == 'link') {
  137. $filetype = filetype(realpath($this->getSourcePath($path)));
  138. }
  139. return $filetype;
  140. }
  141. public function filesize($path) {
  142. if ($this->is_dir($path)) {
  143. return 0;
  144. }
  145. $fullPath = $this->getSourcePath($path);
  146. if (PHP_INT_SIZE === 4) {
  147. $helper = new \OC\LargeFileHelper;
  148. return $helper->getFileSize($fullPath);
  149. }
  150. return filesize($fullPath);
  151. }
  152. public function isReadable($path) {
  153. return is_readable($this->getSourcePath($path));
  154. }
  155. public function isUpdatable($path) {
  156. return is_writable($this->getSourcePath($path));
  157. }
  158. public function file_exists($path) {
  159. return file_exists($this->getSourcePath($path));
  160. }
  161. public function filemtime($path) {
  162. $fullPath = $this->getSourcePath($path);
  163. clearstatcache(true, $fullPath);
  164. if (!$this->file_exists($path)) {
  165. return false;
  166. }
  167. if (PHP_INT_SIZE === 4) {
  168. $helper = new \OC\LargeFileHelper();
  169. return $helper->getFileMtime($fullPath);
  170. }
  171. return filemtime($fullPath);
  172. }
  173. public function touch($path, $mtime = null) {
  174. // sets the modification time of the file to the given value.
  175. // If mtime is nil the current time is set.
  176. // note that the access time of the file always changes to the current time.
  177. if ($this->file_exists($path) and !$this->isUpdatable($path)) {
  178. return false;
  179. }
  180. if (!is_null($mtime)) {
  181. $result = touch($this->getSourcePath($path), $mtime);
  182. } else {
  183. $result = touch($this->getSourcePath($path));
  184. }
  185. if ($result) {
  186. clearstatcache(true, $this->getSourcePath($path));
  187. }
  188. return $result;
  189. }
  190. public function file_get_contents($path) {
  191. return file_get_contents($this->getSourcePath($path));
  192. }
  193. public function file_put_contents($path, $data) {
  194. return file_put_contents($this->getSourcePath($path), $data);
  195. }
  196. public function unlink($path) {
  197. if ($this->is_dir($path)) {
  198. return $this->rmdir($path);
  199. } else if ($this->is_file($path)) {
  200. return unlink($this->getSourcePath($path));
  201. } else {
  202. return false;
  203. }
  204. }
  205. public function rename($path1, $path2) {
  206. $srcParent = dirname($path1);
  207. $dstParent = dirname($path2);
  208. if (!$this->isUpdatable($srcParent)) {
  209. \OCP\Util::writeLog('core', 'unable to rename, source directory is not writable : ' . $srcParent, \OCP\Util::ERROR);
  210. return false;
  211. }
  212. if (!$this->isUpdatable($dstParent)) {
  213. \OCP\Util::writeLog('core', 'unable to rename, destination directory is not writable : ' . $dstParent, \OCP\Util::ERROR);
  214. return false;
  215. }
  216. if (!$this->file_exists($path1)) {
  217. \OCP\Util::writeLog('core', 'unable to rename, file does not exists : ' . $path1, \OCP\Util::ERROR);
  218. return false;
  219. }
  220. if ($this->is_dir($path2)) {
  221. $this->rmdir($path2);
  222. } else if ($this->is_file($path2)) {
  223. $this->unlink($path2);
  224. }
  225. if ($this->is_dir($path1)) {
  226. // we can't move folders across devices, use copy instead
  227. $stat1 = stat(dirname($this->getSourcePath($path1)));
  228. $stat2 = stat(dirname($this->getSourcePath($path2)));
  229. if ($stat1['dev'] !== $stat2['dev']) {
  230. $result = $this->copy($path1, $path2);
  231. if ($result) {
  232. $result &= $this->rmdir($path1);
  233. }
  234. return $result;
  235. }
  236. }
  237. return rename($this->getSourcePath($path1), $this->getSourcePath($path2));
  238. }
  239. public function copy($path1, $path2) {
  240. if ($this->is_dir($path1)) {
  241. return parent::copy($path1, $path2);
  242. } else {
  243. return copy($this->getSourcePath($path1), $this->getSourcePath($path2));
  244. }
  245. }
  246. public function fopen($path, $mode) {
  247. return fopen($this->getSourcePath($path), $mode);
  248. }
  249. public function hash($type, $path, $raw = false) {
  250. return hash_file($type, $this->getSourcePath($path), $raw);
  251. }
  252. public function free_space($path) {
  253. $sourcePath = $this->getSourcePath($path);
  254. // using !is_dir because $sourcePath might be a part file or
  255. // non-existing file, so we'd still want to use the parent dir
  256. // in such cases
  257. if (!is_dir($sourcePath)) {
  258. // disk_free_space doesn't work on files
  259. $sourcePath = dirname($sourcePath);
  260. }
  261. $space = @disk_free_space($sourcePath);
  262. if ($space === false || is_null($space)) {
  263. return \OCP\Files\FileInfo::SPACE_UNKNOWN;
  264. }
  265. return $space;
  266. }
  267. public function search($query) {
  268. return $this->searchInDir($query);
  269. }
  270. public function getLocalFile($path) {
  271. return $this->getSourcePath($path);
  272. }
  273. public function getLocalFolder($path) {
  274. return $this->getSourcePath($path);
  275. }
  276. /**
  277. * @param string $query
  278. * @param string $dir
  279. * @return array
  280. */
  281. protected function searchInDir($query, $dir = '') {
  282. $files = array();
  283. $physicalDir = $this->getSourcePath($dir);
  284. foreach (scandir($physicalDir) as $item) {
  285. if (\OC\Files\Filesystem::isIgnoredDir($item))
  286. continue;
  287. $physicalItem = $physicalDir . '/' . $item;
  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. *
  300. * @param string $path
  301. * @param int $time
  302. * @return bool
  303. */
  304. public function hasUpdated($path, $time) {
  305. if ($this->file_exists($path)) {
  306. return $this->filemtime($path) > $time;
  307. } else {
  308. return true;
  309. }
  310. }
  311. /**
  312. * Get the source path (on disk) of a given path
  313. *
  314. * @param string $path
  315. * @return string
  316. * @throws ForbiddenException
  317. */
  318. public function getSourcePath($path) {
  319. $fullPath = $this->datadir . $path;
  320. $currentPath = $path;
  321. if ($this->allowSymlinks || $currentPath === '') {
  322. return $fullPath;
  323. }
  324. $pathToResolve = $fullPath;
  325. $realPath = realpath($pathToResolve);
  326. while ($realPath === false) { // for non existing files check the parent directory
  327. $currentPath = dirname($currentPath);
  328. if ($currentPath === '' || $currentPath === '.') {
  329. return $fullPath;
  330. }
  331. $realPath = realpath($this->datadir . $currentPath);
  332. }
  333. if ($realPath) {
  334. $realPath = $realPath . '/';
  335. }
  336. if (substr($realPath, 0, $this->dataDirLength) === $this->realDataDir) {
  337. return $fullPath;
  338. }
  339. \OCP\Util::writeLog('core', "Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", \OCP\Util::ERROR);
  340. throw new ForbiddenException('Following symlinks is not allowed', false);
  341. }
  342. /**
  343. * {@inheritdoc}
  344. */
  345. public function isLocal() {
  346. return true;
  347. }
  348. /**
  349. * get the ETag for a file or folder
  350. *
  351. * @param string $path
  352. * @return string
  353. */
  354. public function getETag($path) {
  355. if ($this->is_file($path)) {
  356. $stat = $this->stat($path);
  357. return md5(
  358. $stat['mtime'] .
  359. $stat['ino'] .
  360. $stat['dev'] .
  361. $stat['size']
  362. );
  363. } else {
  364. return parent::getETag($path);
  365. }
  366. }
  367. /**
  368. * @param IStorage $sourceStorage
  369. * @param string $sourceInternalPath
  370. * @param string $targetInternalPath
  371. * @param bool $preserveMtime
  372. * @return bool
  373. */
  374. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) {
  375. if ($sourceStorage->instanceOfStorage(Local::class)) {
  376. if ($sourceStorage->instanceOfStorage(Jail::class)) {
  377. /**
  378. * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
  379. */
  380. $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
  381. }
  382. /**
  383. * @var \OC\Files\Storage\Local $sourceStorage
  384. */
  385. $rootStorage = new Local(['datadir' => '/']);
  386. return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
  387. } else {
  388. return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  389. }
  390. }
  391. /**
  392. * @param IStorage $sourceStorage
  393. * @param string $sourceInternalPath
  394. * @param string $targetInternalPath
  395. * @return bool
  396. */
  397. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  398. if ($sourceStorage->instanceOfStorage(Local::class)) {
  399. if ($sourceStorage->instanceOfStorage(Jail::class)) {
  400. /**
  401. * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
  402. */
  403. $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
  404. }
  405. /**
  406. * @var \OC\Files\Storage\Local $sourceStorage
  407. */
  408. $rootStorage = new Local(['datadir' => '/']);
  409. return $rootStorage->rename($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
  410. } else {
  411. return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  412. }
  413. }
  414. }