Local.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author aler9 <46489434+aler9@users.noreply.github.com>
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Bart Visscher <bartv@thisnet.nl>
  8. * @author Boris Rybalkin <ribalkin@gmail.com>
  9. * @author Brice Maron <brice@bmaron.net>
  10. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  11. * @author J0WI <J0WI@users.noreply.github.com>
  12. * @author Jakob Sack <mail@jakobsack.de>
  13. * @author Joas Schilling <coding@schilljs.com>
  14. * @author Johannes Leuker <j.leuker@hosting.de>
  15. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  16. * @author Klaas Freitag <freitag@owncloud.com>
  17. * @author Lukas Reschke <lukas@statuscode.ch>
  18. * @author Martin Brugnara <martin@0x6d62.eu>
  19. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  20. * @author Morris Jobke <hey@morrisjobke.de>
  21. * @author Robin Appelman <robin@icewind.nl>
  22. * @author Roeland Jago Douma <roeland@famdouma.nl>
  23. * @author Sjors van der Pluijm <sjors@desjors.nl>
  24. * @author Stefan Weil <sw@weilnetz.de>
  25. * @author Thomas Müller <thomas.mueller@tmit.eu>
  26. * @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
  27. * @author Vincent Petry <vincent@nextcloud.com>
  28. *
  29. * @license AGPL-3.0
  30. *
  31. * This code is free software: you can redistribute it and/or modify
  32. * it under the terms of the GNU Affero General Public License, version 3,
  33. * as published by the Free Software Foundation.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU Affero General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU Affero General Public License, version 3,
  41. * along with this program. If not, see <http://www.gnu.org/licenses/>
  42. *
  43. */
  44. namespace OC\Files\Storage;
  45. use OC\Files\Filesystem;
  46. use OC\Files\Storage\Wrapper\Jail;
  47. use OCP\Constants;
  48. use OCP\Files\ForbiddenException;
  49. use OCP\Files\GenericFileException;
  50. use OCP\Files\IMimeTypeDetector;
  51. use OCP\Files\Storage\IStorage;
  52. use OCP\IConfig;
  53. use Psr\Log\LoggerInterface;
  54. /**
  55. * for local filestore, we only have to map the paths
  56. */
  57. class Local extends \OC\Files\Storage\Common {
  58. protected $datadir;
  59. protected $dataDirLength;
  60. protected $realDataDir;
  61. private IConfig $config;
  62. private IMimeTypeDetector $mimeTypeDetector;
  63. private $defUMask;
  64. protected bool $unlinkOnTruncate;
  65. public function __construct($arguments) {
  66. if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) {
  67. throw new \InvalidArgumentException('No data directory set for local storage');
  68. }
  69. $this->datadir = str_replace('//', '/', $arguments['datadir']);
  70. // some crazy code uses a local storage on root...
  71. if ($this->datadir === '/') {
  72. $this->realDataDir = $this->datadir;
  73. } else {
  74. $realPath = realpath($this->datadir) ?: $this->datadir;
  75. $this->realDataDir = rtrim($realPath, '/') . '/';
  76. }
  77. if (substr($this->datadir, -1) !== '/') {
  78. $this->datadir .= '/';
  79. }
  80. $this->dataDirLength = strlen($this->realDataDir);
  81. $this->config = \OC::$server->get(IConfig::class);
  82. $this->mimeTypeDetector = \OC::$server->get(IMimeTypeDetector::class);
  83. $this->defUMask = $this->config->getSystemValue('localstorage.umask', 0022);
  84. // support Write-Once-Read-Many file systems
  85. $this->unlinkOnTruncate = $this->config->getSystemValue('localstorage.unlink_on_truncate', false);
  86. }
  87. public function __destruct() {
  88. }
  89. public function getId() {
  90. return 'local::' . $this->datadir;
  91. }
  92. public function mkdir($path) {
  93. $sourcePath = $this->getSourcePath($path);
  94. $oldMask = umask($this->defUMask);
  95. $result = @mkdir($sourcePath, 0777, true);
  96. umask($oldMask);
  97. return $result;
  98. }
  99. public function rmdir($path) {
  100. if (!$this->isDeletable($path)) {
  101. return false;
  102. }
  103. try {
  104. $it = new \RecursiveIteratorIterator(
  105. new \RecursiveDirectoryIterator($this->getSourcePath($path)),
  106. \RecursiveIteratorIterator::CHILD_FIRST
  107. );
  108. /**
  109. * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach
  110. * This bug is fixed in PHP 5.5.9 or before
  111. * See #8376
  112. */
  113. $it->rewind();
  114. while ($it->valid()) {
  115. /**
  116. * @var \SplFileInfo $file
  117. */
  118. $file = $it->current();
  119. clearstatcache(true, $this->getSourcePath($file));
  120. if (in_array($file->getBasename(), ['.', '..'])) {
  121. $it->next();
  122. continue;
  123. } elseif ($file->isDir()) {
  124. rmdir($file->getPathname());
  125. } elseif ($file->isFile() || $file->isLink()) {
  126. unlink($file->getPathname());
  127. }
  128. $it->next();
  129. }
  130. clearstatcache(true, $this->getSourcePath($path));
  131. return rmdir($this->getSourcePath($path));
  132. } catch (\UnexpectedValueException $e) {
  133. return false;
  134. }
  135. }
  136. public function opendir($path) {
  137. return opendir($this->getSourcePath($path));
  138. }
  139. public function is_dir($path) {
  140. if (substr($path, -1) == '/') {
  141. $path = substr($path, 0, -1);
  142. }
  143. return is_dir($this->getSourcePath($path));
  144. }
  145. public function is_file($path) {
  146. return is_file($this->getSourcePath($path));
  147. }
  148. public function stat($path) {
  149. $fullPath = $this->getSourcePath($path);
  150. clearstatcache(true, $fullPath);
  151. if (!file_exists($fullPath)) {
  152. return false;
  153. }
  154. $statResult = @stat($fullPath);
  155. if (PHP_INT_SIZE === 4 && $statResult && !$this->is_dir($path)) {
  156. $filesize = $this->filesize($path);
  157. $statResult['size'] = $filesize;
  158. $statResult[7] = $filesize;
  159. }
  160. if (is_array($statResult)) {
  161. $statResult['full_path'] = $fullPath;
  162. }
  163. return $statResult;
  164. }
  165. /**
  166. * @inheritdoc
  167. */
  168. public function getMetaData($path) {
  169. try {
  170. $stat = $this->stat($path);
  171. } catch (ForbiddenException $e) {
  172. return null;
  173. }
  174. if (!$stat) {
  175. return null;
  176. }
  177. $permissions = Constants::PERMISSION_SHARE;
  178. $statPermissions = $stat['mode'];
  179. $isDir = ($statPermissions & 0x4000) === 0x4000 && !($statPermissions & 0x8000);
  180. if ($statPermissions & 0x0100) {
  181. $permissions += Constants::PERMISSION_READ;
  182. }
  183. if ($statPermissions & 0x0080) {
  184. $permissions += Constants::PERMISSION_UPDATE;
  185. if ($isDir) {
  186. $permissions += Constants::PERMISSION_CREATE;
  187. }
  188. }
  189. if (!($path === '' || $path === '/')) { // deletable depends on the parents unix permissions
  190. $parent = dirname($stat['full_path']);
  191. if (is_writable($parent)) {
  192. $permissions += Constants::PERMISSION_DELETE;
  193. }
  194. }
  195. $data = [];
  196. $data['mimetype'] = $isDir ? 'httpd/unix-directory' : $this->mimeTypeDetector->detectPath($path);
  197. $data['mtime'] = $stat['mtime'];
  198. if ($data['mtime'] === false) {
  199. $data['mtime'] = time();
  200. }
  201. if ($isDir) {
  202. $data['size'] = -1; //unknown
  203. } else {
  204. $data['size'] = $stat['size'];
  205. }
  206. $data['etag'] = $this->calculateEtag($path, $stat);
  207. $data['storage_mtime'] = $data['mtime'];
  208. $data['permissions'] = $permissions;
  209. $data['name'] = basename($path);
  210. return $data;
  211. }
  212. public function filetype($path) {
  213. $filetype = filetype($this->getSourcePath($path));
  214. if ($filetype == 'link') {
  215. $filetype = filetype(realpath($this->getSourcePath($path)));
  216. }
  217. return $filetype;
  218. }
  219. public function filesize($path) {
  220. if (!$this->is_file($path)) {
  221. return 0;
  222. }
  223. $fullPath = $this->getSourcePath($path);
  224. if (PHP_INT_SIZE === 4) {
  225. $helper = new \OC\LargeFileHelper;
  226. return $helper->getFileSize($fullPath);
  227. }
  228. return filesize($fullPath);
  229. }
  230. public function isReadable($path) {
  231. return is_readable($this->getSourcePath($path));
  232. }
  233. public function isUpdatable($path) {
  234. return is_writable($this->getSourcePath($path));
  235. }
  236. public function file_exists($path) {
  237. return file_exists($this->getSourcePath($path));
  238. }
  239. public function filemtime($path) {
  240. $fullPath = $this->getSourcePath($path);
  241. clearstatcache(true, $fullPath);
  242. if (!$this->file_exists($path)) {
  243. return false;
  244. }
  245. if (PHP_INT_SIZE === 4) {
  246. $helper = new \OC\LargeFileHelper();
  247. return $helper->getFileMtime($fullPath);
  248. }
  249. return filemtime($fullPath);
  250. }
  251. public function touch($path, $mtime = null) {
  252. // sets the modification time of the file to the given value.
  253. // If mtime is nil the current time is set.
  254. // note that the access time of the file always changes to the current time.
  255. if ($this->file_exists($path) and !$this->isUpdatable($path)) {
  256. return false;
  257. }
  258. $oldMask = umask($this->defUMask);
  259. if (!is_null($mtime)) {
  260. $result = @touch($this->getSourcePath($path), $mtime);
  261. } else {
  262. $result = @touch($this->getSourcePath($path));
  263. }
  264. umask($oldMask);
  265. if ($result) {
  266. clearstatcache(true, $this->getSourcePath($path));
  267. }
  268. return $result;
  269. }
  270. public function file_get_contents($path) {
  271. return file_get_contents($this->getSourcePath($path));
  272. }
  273. public function file_put_contents($path, $data) {
  274. $oldMask = umask($this->defUMask);
  275. if ($this->unlinkOnTruncate) {
  276. $this->unlink($path);
  277. }
  278. $result = file_put_contents($this->getSourcePath($path), $data);
  279. umask($oldMask);
  280. return $result;
  281. }
  282. public function unlink($path) {
  283. if ($this->is_dir($path)) {
  284. return $this->rmdir($path);
  285. } elseif ($this->is_file($path)) {
  286. return unlink($this->getSourcePath($path));
  287. } else {
  288. return false;
  289. }
  290. }
  291. private function checkTreeForForbiddenItems(string $path) {
  292. $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
  293. foreach ($iterator as $file) {
  294. /** @var \SplFileInfo $file */
  295. if (Filesystem::isFileBlacklisted($file->getBasename())) {
  296. throw new ForbiddenException('Invalid path: ' . $file->getPathname(), false);
  297. }
  298. }
  299. }
  300. public function rename($path1, $path2) {
  301. $srcParent = dirname($path1);
  302. $dstParent = dirname($path2);
  303. if (!$this->isUpdatable($srcParent)) {
  304. \OC::$server->get(LoggerInterface::class)->error('unable to rename, source directory is not writable : ' . $srcParent, ['app' => 'core']);
  305. return false;
  306. }
  307. if (!$this->isUpdatable($dstParent)) {
  308. \OC::$server->get(LoggerInterface::class)->error('unable to rename, destination directory is not writable : ' . $dstParent, ['app' => 'core']);
  309. return false;
  310. }
  311. if (!$this->file_exists($path1)) {
  312. \OC::$server->get(LoggerInterface::class)->error('unable to rename, file does not exists : ' . $path1, ['app' => 'core']);
  313. return false;
  314. }
  315. if ($this->is_dir($path2)) {
  316. $this->rmdir($path2);
  317. } elseif ($this->is_file($path2)) {
  318. $this->unlink($path2);
  319. }
  320. if ($this->is_dir($path1)) {
  321. // we can't move folders across devices, use copy instead
  322. $stat1 = stat(dirname($this->getSourcePath($path1)));
  323. $stat2 = stat(dirname($this->getSourcePath($path2)));
  324. if ($stat1['dev'] !== $stat2['dev']) {
  325. $result = $this->copy($path1, $path2);
  326. if ($result) {
  327. $result &= $this->rmdir($path1);
  328. }
  329. return $result;
  330. }
  331. $this->checkTreeForForbiddenItems($this->getSourcePath($path1));
  332. }
  333. return rename($this->getSourcePath($path1), $this->getSourcePath($path2));
  334. }
  335. public function copy($path1, $path2) {
  336. if ($this->is_dir($path1)) {
  337. return parent::copy($path1, $path2);
  338. } else {
  339. $oldMask = umask($this->defUMask);
  340. if ($this->unlinkOnTruncate) {
  341. $this->unlink($path2);
  342. }
  343. $result = copy($this->getSourcePath($path1), $this->getSourcePath($path2));
  344. umask($oldMask);
  345. return $result;
  346. }
  347. }
  348. public function fopen($path, $mode) {
  349. $sourcePath = $this->getSourcePath($path);
  350. if (!file_exists($sourcePath) && $mode === 'r') {
  351. return false;
  352. }
  353. $oldMask = umask($this->defUMask);
  354. if (($mode === 'w' || $mode === 'w+') && $this->unlinkOnTruncate) {
  355. $this->unlink($path);
  356. }
  357. $result = @fopen($sourcePath, $mode);
  358. umask($oldMask);
  359. return $result;
  360. }
  361. public function hash($type, $path, $raw = false) {
  362. return hash_file($type, $this->getSourcePath($path), $raw);
  363. }
  364. public function free_space($path) {
  365. $sourcePath = $this->getSourcePath($path);
  366. // using !is_dir because $sourcePath might be a part file or
  367. // non-existing file, so we'd still want to use the parent dir
  368. // in such cases
  369. if (!is_dir($sourcePath)) {
  370. // disk_free_space doesn't work on files
  371. $sourcePath = dirname($sourcePath);
  372. }
  373. $space = function_exists('disk_free_space') ? disk_free_space($sourcePath) : false;
  374. if ($space === false || is_null($space)) {
  375. return \OCP\Files\FileInfo::SPACE_UNKNOWN;
  376. }
  377. return $space;
  378. }
  379. public function search($query) {
  380. return $this->searchInDir($query);
  381. }
  382. public function getLocalFile($path) {
  383. return $this->getSourcePath($path);
  384. }
  385. public function getLocalFolder($path) {
  386. return $this->getSourcePath($path);
  387. }
  388. /**
  389. * @param string $query
  390. * @param string $dir
  391. * @return array
  392. */
  393. protected function searchInDir($query, $dir = '') {
  394. $files = [];
  395. $physicalDir = $this->getSourcePath($dir);
  396. foreach (scandir($physicalDir) as $item) {
  397. if (\OC\Files\Filesystem::isIgnoredDir($item)) {
  398. continue;
  399. }
  400. $physicalItem = $physicalDir . '/' . $item;
  401. if (strstr(strtolower($item), strtolower($query)) !== false) {
  402. $files[] = $dir . '/' . $item;
  403. }
  404. if (is_dir($physicalItem)) {
  405. $files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
  406. }
  407. }
  408. return $files;
  409. }
  410. /**
  411. * check if a file or folder has been updated since $time
  412. *
  413. * @param string $path
  414. * @param int $time
  415. * @return bool
  416. */
  417. public function hasUpdated($path, $time) {
  418. if ($this->file_exists($path)) {
  419. return $this->filemtime($path) > $time;
  420. } else {
  421. return true;
  422. }
  423. }
  424. /**
  425. * Get the source path (on disk) of a given path
  426. *
  427. * @param string $path
  428. * @return string
  429. * @throws ForbiddenException
  430. */
  431. public function getSourcePath($path) {
  432. if (Filesystem::isFileBlacklisted($path)) {
  433. throw new ForbiddenException('Invalid path: ' . $path, false);
  434. }
  435. $fullPath = $this->datadir . $path;
  436. $currentPath = $path;
  437. $allowSymlinks = $this->config->getSystemValue('localstorage.allowsymlinks', false);
  438. if ($allowSymlinks || $currentPath === '') {
  439. return $fullPath;
  440. }
  441. $pathToResolve = $fullPath;
  442. $realPath = realpath($pathToResolve);
  443. while ($realPath === false) { // for non existing files check the parent directory
  444. $currentPath = dirname($currentPath);
  445. if ($currentPath === '' || $currentPath === '.') {
  446. return $fullPath;
  447. }
  448. $realPath = realpath($this->datadir . $currentPath);
  449. }
  450. if ($realPath) {
  451. $realPath = $realPath . '/';
  452. }
  453. if (substr($realPath, 0, $this->dataDirLength) === $this->realDataDir) {
  454. return $fullPath;
  455. }
  456. \OC::$server->get(LoggerInterface::class)->error("Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", ['app' => 'core']);
  457. throw new ForbiddenException('Following symlinks is not allowed', false);
  458. }
  459. /**
  460. * {@inheritdoc}
  461. */
  462. public function isLocal() {
  463. return true;
  464. }
  465. /**
  466. * get the ETag for a file or folder
  467. *
  468. * @param string $path
  469. * @return string
  470. */
  471. public function getETag($path) {
  472. return $this->calculateEtag($path, $this->stat($path));
  473. }
  474. private function calculateEtag(string $path, array $stat): string {
  475. if ($stat['mode'] & 0x4000 && !($stat['mode'] & 0x8000)) { // is_dir & not socket
  476. return parent::getETag($path);
  477. } else {
  478. if ($stat === false) {
  479. return md5('');
  480. }
  481. $toHash = '';
  482. if (isset($stat['mtime'])) {
  483. $toHash .= $stat['mtime'];
  484. }
  485. if (isset($stat['ino'])) {
  486. $toHash .= $stat['ino'];
  487. }
  488. if (isset($stat['dev'])) {
  489. $toHash .= $stat['dev'];
  490. }
  491. if (isset($stat['size'])) {
  492. $toHash .= $stat['size'];
  493. }
  494. return md5($toHash);
  495. }
  496. }
  497. /**
  498. * @param IStorage $sourceStorage
  499. * @param string $sourceInternalPath
  500. * @param string $targetInternalPath
  501. * @param bool $preserveMtime
  502. * @return bool
  503. */
  504. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) {
  505. // Don't treat ACLStorageWrapper like local storage where copy can be done directly.
  506. // Instead use the slower recursive copying in php from Common::copyFromStorage with
  507. // more permissions checks.
  508. if ($sourceStorage->instanceOfStorage(Local::class) && !$sourceStorage->instanceOfStorage('OCA\GroupFolders\ACL\ACLStorageWrapper')) {
  509. if ($sourceStorage->instanceOfStorage(Jail::class)) {
  510. /**
  511. * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
  512. */
  513. $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
  514. }
  515. /**
  516. * @var \OC\Files\Storage\Local $sourceStorage
  517. */
  518. $rootStorage = new Local(['datadir' => '/']);
  519. return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
  520. } else {
  521. return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  522. }
  523. }
  524. /**
  525. * @param IStorage $sourceStorage
  526. * @param string $sourceInternalPath
  527. * @param string $targetInternalPath
  528. * @return bool
  529. */
  530. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  531. if ($sourceStorage->instanceOfStorage(Local::class)) {
  532. if ($sourceStorage->instanceOfStorage(Jail::class)) {
  533. /**
  534. * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
  535. */
  536. $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
  537. }
  538. /**
  539. * @var \OC\Files\Storage\Local $sourceStorage
  540. */
  541. $rootStorage = new Local(['datadir' => '/']);
  542. return $rootStorage->rename($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
  543. } else {
  544. return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  545. }
  546. }
  547. public function writeStream(string $path, $stream, int $size = null): int {
  548. $result = $this->file_put_contents($path, $stream);
  549. if (is_resource($stream)) {
  550. fclose($stream);
  551. }
  552. if ($result === false) {
  553. throw new GenericFileException("Failed write stream to $path");
  554. } else {
  555. return $result;
  556. }
  557. }
  558. }