Local.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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\Filesystem;
  41. use OC\Files\Storage\Wrapper\Jail;
  42. use OCP\Files\ForbiddenException;
  43. use OCP\Files\Storage\IStorage;
  44. use OCP\ILogger;
  45. /**
  46. * for local filestore, we only have to map the paths
  47. */
  48. class Local extends \OC\Files\Storage\Common {
  49. protected $datadir;
  50. protected $dataDirLength;
  51. protected $allowSymlinks = false;
  52. protected $realDataDir;
  53. public function __construct($arguments) {
  54. if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) {
  55. throw new \InvalidArgumentException('No data directory set for local storage');
  56. }
  57. $this->datadir = str_replace('//', '/', $arguments['datadir']);
  58. // some crazy code uses a local storage on root...
  59. if ($this->datadir === '/') {
  60. $this->realDataDir = $this->datadir;
  61. } else {
  62. $realPath = realpath($this->datadir) ?: $this->datadir;
  63. $this->realDataDir = rtrim($realPath, '/') . '/';
  64. }
  65. if (substr($this->datadir, -1) !== '/') {
  66. $this->datadir .= '/';
  67. }
  68. $this->dataDirLength = strlen($this->realDataDir);
  69. }
  70. public function __destruct() {
  71. }
  72. public function getId() {
  73. return 'local::' . $this->datadir;
  74. }
  75. public function mkdir($path) {
  76. return @mkdir($this->getSourcePath($path), 0777, true);
  77. }
  78. public function rmdir($path) {
  79. if (!$this->isDeletable($path)) {
  80. return false;
  81. }
  82. try {
  83. $it = new \RecursiveIteratorIterator(
  84. new \RecursiveDirectoryIterator($this->getSourcePath($path)),
  85. \RecursiveIteratorIterator::CHILD_FIRST
  86. );
  87. /**
  88. * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach
  89. * This bug is fixed in PHP 5.5.9 or before
  90. * See #8376
  91. */
  92. $it->rewind();
  93. while ($it->valid()) {
  94. /**
  95. * @var \SplFileInfo $file
  96. */
  97. $file = $it->current();
  98. if (in_array($file->getBasename(), array('.', '..'))) {
  99. $it->next();
  100. continue;
  101. } elseif ($file->isDir()) {
  102. rmdir($file->getPathname());
  103. } elseif ($file->isFile() || $file->isLink()) {
  104. unlink($file->getPathname());
  105. }
  106. $it->next();
  107. }
  108. return rmdir($this->getSourcePath($path));
  109. } catch (\UnexpectedValueException $e) {
  110. return false;
  111. }
  112. }
  113. public function opendir($path) {
  114. return opendir($this->getSourcePath($path));
  115. }
  116. public function is_dir($path) {
  117. if (substr($path, -1) == '/') {
  118. $path = substr($path, 0, -1);
  119. }
  120. return is_dir($this->getSourcePath($path));
  121. }
  122. public function is_file($path) {
  123. return is_file($this->getSourcePath($path));
  124. }
  125. public function stat($path) {
  126. clearstatcache();
  127. $fullPath = $this->getSourcePath($path);
  128. $statResult = stat($fullPath);
  129. if (PHP_INT_SIZE === 4 && !$this->is_dir($path)) {
  130. $filesize = $this->filesize($path);
  131. $statResult['size'] = $filesize;
  132. $statResult[7] = $filesize;
  133. }
  134. return $statResult;
  135. }
  136. public function filetype($path) {
  137. $filetype = filetype($this->getSourcePath($path));
  138. if ($filetype == 'link') {
  139. $filetype = filetype(realpath($this->getSourcePath($path)));
  140. }
  141. return $filetype;
  142. }
  143. public function filesize($path) {
  144. if ($this->is_dir($path)) {
  145. return 0;
  146. }
  147. $fullPath = $this->getSourcePath($path);
  148. if (PHP_INT_SIZE === 4) {
  149. $helper = new \OC\LargeFileHelper;
  150. return $helper->getFileSize($fullPath);
  151. }
  152. return filesize($fullPath);
  153. }
  154. public function isReadable($path) {
  155. return is_readable($this->getSourcePath($path));
  156. }
  157. public function isUpdatable($path) {
  158. return is_writable($this->getSourcePath($path));
  159. }
  160. public function file_exists($path) {
  161. return file_exists($this->getSourcePath($path));
  162. }
  163. public function filemtime($path) {
  164. $fullPath = $this->getSourcePath($path);
  165. clearstatcache(true, $fullPath);
  166. if (!$this->file_exists($path)) {
  167. return false;
  168. }
  169. if (PHP_INT_SIZE === 4) {
  170. $helper = new \OC\LargeFileHelper();
  171. return $helper->getFileMtime($fullPath);
  172. }
  173. return filemtime($fullPath);
  174. }
  175. public function touch($path, $mtime = null) {
  176. // sets the modification time of the file to the given value.
  177. // If mtime is nil the current time is set.
  178. // note that the access time of the file always changes to the current time.
  179. if ($this->file_exists($path) and !$this->isUpdatable($path)) {
  180. return false;
  181. }
  182. if (!is_null($mtime)) {
  183. $result = touch($this->getSourcePath($path), $mtime);
  184. } else {
  185. $result = touch($this->getSourcePath($path));
  186. }
  187. if ($result) {
  188. clearstatcache(true, $this->getSourcePath($path));
  189. }
  190. return $result;
  191. }
  192. public function file_get_contents($path) {
  193. return file_get_contents($this->getSourcePath($path));
  194. }
  195. public function file_put_contents($path, $data) {
  196. return file_put_contents($this->getSourcePath($path), $data);
  197. }
  198. public function unlink($path) {
  199. if ($this->is_dir($path)) {
  200. return $this->rmdir($path);
  201. } else if ($this->is_file($path)) {
  202. return unlink($this->getSourcePath($path));
  203. } else {
  204. return false;
  205. }
  206. }
  207. private function treeContainsBlacklistedFile(string $path): bool {
  208. $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
  209. foreach ($iterator as $file) {
  210. /** @var \SplFileInfo $file */
  211. if (Filesystem::isFileBlacklisted($file->getBasename())) {
  212. return true;
  213. }
  214. }
  215. return false;
  216. }
  217. public function rename($path1, $path2) {
  218. $srcParent = dirname($path1);
  219. $dstParent = dirname($path2);
  220. if (!$this->isUpdatable($srcParent)) {
  221. \OCP\Util::writeLog('core', 'unable to rename, source directory is not writable : ' . $srcParent, ILogger::ERROR);
  222. return false;
  223. }
  224. if (!$this->isUpdatable($dstParent)) {
  225. \OCP\Util::writeLog('core', 'unable to rename, destination directory is not writable : ' . $dstParent, ILogger::ERROR);
  226. return false;
  227. }
  228. if (!$this->file_exists($path1)) {
  229. \OCP\Util::writeLog('core', 'unable to rename, file does not exists : ' . $path1, ILogger::ERROR);
  230. return false;
  231. }
  232. if ($this->is_dir($path2)) {
  233. $this->rmdir($path2);
  234. } else if ($this->is_file($path2)) {
  235. $this->unlink($path2);
  236. }
  237. if ($this->is_dir($path1)) {
  238. // we can't move folders across devices, use copy instead
  239. $stat1 = stat(dirname($this->getSourcePath($path1)));
  240. $stat2 = stat(dirname($this->getSourcePath($path2)));
  241. if ($stat1['dev'] !== $stat2['dev']) {
  242. $result = $this->copy($path1, $path2);
  243. if ($result) {
  244. $result &= $this->rmdir($path1);
  245. }
  246. return $result;
  247. }
  248. if ($this->treeContainsBlacklistedFile($this->getSourcePath($path1))) {
  249. throw new ForbiddenException('Invalid path', false);
  250. }
  251. }
  252. return rename($this->getSourcePath($path1), $this->getSourcePath($path2));
  253. }
  254. public function copy($path1, $path2) {
  255. if ($this->is_dir($path1)) {
  256. return parent::copy($path1, $path2);
  257. } else {
  258. return copy($this->getSourcePath($path1), $this->getSourcePath($path2));
  259. }
  260. }
  261. public function fopen($path, $mode) {
  262. return fopen($this->getSourcePath($path), $mode);
  263. }
  264. public function hash($type, $path, $raw = false) {
  265. return hash_file($type, $this->getSourcePath($path), $raw);
  266. }
  267. public function free_space($path) {
  268. $sourcePath = $this->getSourcePath($path);
  269. // using !is_dir because $sourcePath might be a part file or
  270. // non-existing file, so we'd still want to use the parent dir
  271. // in such cases
  272. if (!is_dir($sourcePath)) {
  273. // disk_free_space doesn't work on files
  274. $sourcePath = dirname($sourcePath);
  275. }
  276. $space = @disk_free_space($sourcePath);
  277. if ($space === false || is_null($space)) {
  278. return \OCP\Files\FileInfo::SPACE_UNKNOWN;
  279. }
  280. return $space;
  281. }
  282. public function search($query) {
  283. return $this->searchInDir($query);
  284. }
  285. public function getLocalFile($path) {
  286. return $this->getSourcePath($path);
  287. }
  288. public function getLocalFolder($path) {
  289. return $this->getSourcePath($path);
  290. }
  291. /**
  292. * @param string $query
  293. * @param string $dir
  294. * @return array
  295. */
  296. protected function searchInDir($query, $dir = '') {
  297. $files = array();
  298. $physicalDir = $this->getSourcePath($dir);
  299. foreach (scandir($physicalDir) as $item) {
  300. if (\OC\Files\Filesystem::isIgnoredDir($item))
  301. continue;
  302. $physicalItem = $physicalDir . '/' . $item;
  303. if (strstr(strtolower($item), strtolower($query)) !== false) {
  304. $files[] = $dir . '/' . $item;
  305. }
  306. if (is_dir($physicalItem)) {
  307. $files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
  308. }
  309. }
  310. return $files;
  311. }
  312. /**
  313. * check if a file or folder has been updated since $time
  314. *
  315. * @param string $path
  316. * @param int $time
  317. * @return bool
  318. */
  319. public function hasUpdated($path, $time) {
  320. if ($this->file_exists($path)) {
  321. return $this->filemtime($path) > $time;
  322. } else {
  323. return true;
  324. }
  325. }
  326. /**
  327. * Get the source path (on disk) of a given path
  328. *
  329. * @param string $path
  330. * @return string
  331. * @throws ForbiddenException
  332. */
  333. public function getSourcePath($path) {
  334. if (Filesystem::isFileBlacklisted($path)) {
  335. throw new ForbiddenException('Invalid path', false);
  336. }
  337. $fullPath = $this->datadir . $path;
  338. $currentPath = $path;
  339. if ($this->allowSymlinks || $currentPath === '') {
  340. return $fullPath;
  341. }
  342. $pathToResolve = $fullPath;
  343. $realPath = realpath($pathToResolve);
  344. while ($realPath === false) { // for non existing files check the parent directory
  345. $currentPath = dirname($currentPath);
  346. if ($currentPath === '' || $currentPath === '.') {
  347. return $fullPath;
  348. }
  349. $realPath = realpath($this->datadir . $currentPath);
  350. }
  351. if ($realPath) {
  352. $realPath = $realPath . '/';
  353. }
  354. if (substr($realPath, 0, $this->dataDirLength) === $this->realDataDir) {
  355. return $fullPath;
  356. }
  357. \OCP\Util::writeLog('core', "Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", ILogger::ERROR);
  358. throw new ForbiddenException('Following symlinks is not allowed', false);
  359. }
  360. /**
  361. * {@inheritdoc}
  362. */
  363. public function isLocal() {
  364. return true;
  365. }
  366. /**
  367. * get the ETag for a file or folder
  368. *
  369. * @param string $path
  370. * @return string
  371. */
  372. public function getETag($path) {
  373. if ($this->is_file($path)) {
  374. $stat = $this->stat($path);
  375. return md5(
  376. $stat['mtime'] .
  377. $stat['ino'] .
  378. $stat['dev'] .
  379. $stat['size']
  380. );
  381. } else {
  382. return parent::getETag($path);
  383. }
  384. }
  385. /**
  386. * @param IStorage $sourceStorage
  387. * @param string $sourceInternalPath
  388. * @param string $targetInternalPath
  389. * @param bool $preserveMtime
  390. * @return bool
  391. */
  392. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) {
  393. if ($sourceStorage->instanceOfStorage(Local::class)) {
  394. if ($sourceStorage->instanceOfStorage(Jail::class)) {
  395. /**
  396. * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
  397. */
  398. $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
  399. }
  400. /**
  401. * @var \OC\Files\Storage\Local $sourceStorage
  402. */
  403. $rootStorage = new Local(['datadir' => '/']);
  404. return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
  405. } else {
  406. return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  407. }
  408. }
  409. /**
  410. * @param IStorage $sourceStorage
  411. * @param string $sourceInternalPath
  412. * @param string $targetInternalPath
  413. * @return bool
  414. */
  415. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  416. if ($sourceStorage->instanceOfStorage(Local::class)) {
  417. if ($sourceStorage->instanceOfStorage(Jail::class)) {
  418. /**
  419. * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
  420. */
  421. $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
  422. }
  423. /**
  424. * @var \OC\Files\Storage\Local $sourceStorage
  425. */
  426. $rootStorage = new Local(['datadir' => '/']);
  427. return $rootStorage->rename($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
  428. } else {
  429. return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  430. }
  431. }
  432. public function writeStream(string $path, $stream, int $size = null): int {
  433. return (int)file_put_contents($this->getSourcePath($path), $stream);
  434. }
  435. }