1
0

Local.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\Storage;
  8. use OC\Files\Filesystem;
  9. use OC\Files\Storage\Wrapper\Encryption;
  10. use OC\Files\Storage\Wrapper\Jail;
  11. use OCP\Constants;
  12. use OCP\Files\ForbiddenException;
  13. use OCP\Files\GenericFileException;
  14. use OCP\Files\IMimeTypeDetector;
  15. use OCP\Files\Storage\IStorage;
  16. use OCP\Files\StorageNotAvailableException;
  17. use OCP\IConfig;
  18. use OCP\Util;
  19. use Psr\Log\LoggerInterface;
  20. /**
  21. * for local filestore, we only have to map the paths
  22. */
  23. class Local extends \OC\Files\Storage\Common {
  24. protected $datadir;
  25. protected $dataDirLength;
  26. protected $realDataDir;
  27. private IConfig $config;
  28. private IMimeTypeDetector $mimeTypeDetector;
  29. private $defUMask;
  30. protected bool $unlinkOnTruncate;
  31. protected bool $caseInsensitive = false;
  32. public function __construct($arguments) {
  33. if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) {
  34. throw new \InvalidArgumentException('No data directory set for local storage');
  35. }
  36. $this->datadir = str_replace('//', '/', $arguments['datadir']);
  37. // some crazy code uses a local storage on root...
  38. if ($this->datadir === '/') {
  39. $this->realDataDir = $this->datadir;
  40. } else {
  41. $realPath = realpath($this->datadir) ?: $this->datadir;
  42. $this->realDataDir = rtrim($realPath, '/') . '/';
  43. }
  44. if (!str_ends_with($this->datadir, '/')) {
  45. $this->datadir .= '/';
  46. }
  47. $this->dataDirLength = strlen($this->realDataDir);
  48. $this->config = \OC::$server->get(IConfig::class);
  49. $this->mimeTypeDetector = \OC::$server->get(IMimeTypeDetector::class);
  50. $this->defUMask = $this->config->getSystemValue('localstorage.umask', 0022);
  51. $this->caseInsensitive = $this->config->getSystemValueBool('localstorage.case_insensitive', false);
  52. // support Write-Once-Read-Many file systems
  53. $this->unlinkOnTruncate = $this->config->getSystemValueBool('localstorage.unlink_on_truncate', false);
  54. if (isset($arguments['isExternal']) && $arguments['isExternal'] && !$this->stat('')) {
  55. // data dir not accessible or available, can happen when using an external storage of type Local
  56. // on an unmounted system mount point
  57. throw new StorageNotAvailableException('Local storage path does not exist "' . $this->getSourcePath('') . '"');
  58. }
  59. }
  60. public function __destruct() {
  61. }
  62. public function getId() {
  63. return 'local::' . $this->datadir;
  64. }
  65. public function mkdir($path) {
  66. $sourcePath = $this->getSourcePath($path);
  67. $oldMask = umask($this->defUMask);
  68. $result = @mkdir($sourcePath, 0777, true);
  69. umask($oldMask);
  70. return $result;
  71. }
  72. public function rmdir($path) {
  73. if (!$this->isDeletable($path)) {
  74. return false;
  75. }
  76. try {
  77. $it = new \RecursiveIteratorIterator(
  78. new \RecursiveDirectoryIterator($this->getSourcePath($path)),
  79. \RecursiveIteratorIterator::CHILD_FIRST
  80. );
  81. /**
  82. * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach
  83. * This bug is fixed in PHP 5.5.9 or before
  84. * See #8376
  85. */
  86. $it->rewind();
  87. while ($it->valid()) {
  88. /**
  89. * @var \SplFileInfo $file
  90. */
  91. $file = $it->current();
  92. clearstatcache(true, $this->getSourcePath($file));
  93. if (in_array($file->getBasename(), ['.', '..'])) {
  94. $it->next();
  95. continue;
  96. } elseif ($file->isFile() || $file->isLink()) {
  97. unlink($file->getPathname());
  98. } elseif ($file->isDir()) {
  99. rmdir($file->getPathname());
  100. }
  101. $it->next();
  102. }
  103. clearstatcache(true, $this->getSourcePath($path));
  104. return rmdir($this->getSourcePath($path));
  105. } catch (\UnexpectedValueException $e) {
  106. return false;
  107. }
  108. }
  109. public function opendir($path) {
  110. return opendir($this->getSourcePath($path));
  111. }
  112. public function is_dir($path) {
  113. if ($this->caseInsensitive && !$this->file_exists($path)) {
  114. return false;
  115. }
  116. if (str_ends_with($path, '/')) {
  117. $path = substr($path, 0, -1);
  118. }
  119. return is_dir($this->getSourcePath($path));
  120. }
  121. public function is_file($path) {
  122. if ($this->caseInsensitive && !$this->file_exists($path)) {
  123. return false;
  124. }
  125. return is_file($this->getSourcePath($path));
  126. }
  127. public function stat($path) {
  128. $fullPath = $this->getSourcePath($path);
  129. clearstatcache(true, $fullPath);
  130. if (!file_exists($fullPath)) {
  131. return false;
  132. }
  133. $statResult = @stat($fullPath);
  134. if (PHP_INT_SIZE === 4 && $statResult && !$this->is_dir($path)) {
  135. $filesize = $this->filesize($path);
  136. $statResult['size'] = $filesize;
  137. $statResult[7] = $filesize;
  138. }
  139. if (is_array($statResult)) {
  140. $statResult['full_path'] = $fullPath;
  141. }
  142. return $statResult;
  143. }
  144. /**
  145. * @inheritdoc
  146. */
  147. public function getMetaData($path) {
  148. try {
  149. $stat = $this->stat($path);
  150. } catch (ForbiddenException $e) {
  151. return null;
  152. }
  153. if (!$stat) {
  154. return null;
  155. }
  156. $permissions = Constants::PERMISSION_SHARE;
  157. $statPermissions = $stat['mode'];
  158. $isDir = ($statPermissions & 0x4000) === 0x4000 && !($statPermissions & 0x8000);
  159. if ($statPermissions & 0x0100) {
  160. $permissions += Constants::PERMISSION_READ;
  161. }
  162. if ($statPermissions & 0x0080) {
  163. $permissions += Constants::PERMISSION_UPDATE;
  164. if ($isDir) {
  165. $permissions += Constants::PERMISSION_CREATE;
  166. }
  167. }
  168. if (!($path === '' || $path === '/')) { // deletable depends on the parents unix permissions
  169. $parent = dirname($stat['full_path']);
  170. if (is_writable($parent)) {
  171. $permissions += Constants::PERMISSION_DELETE;
  172. }
  173. }
  174. $data = [];
  175. $data['mimetype'] = $isDir ? 'httpd/unix-directory' : $this->mimeTypeDetector->detectPath($path);
  176. $data['mtime'] = $stat['mtime'];
  177. if ($data['mtime'] === false) {
  178. $data['mtime'] = time();
  179. }
  180. if ($isDir) {
  181. $data['size'] = -1; //unknown
  182. } else {
  183. $data['size'] = $stat['size'];
  184. }
  185. $data['etag'] = $this->calculateEtag($path, $stat);
  186. $data['storage_mtime'] = $data['mtime'];
  187. $data['permissions'] = $permissions;
  188. $data['name'] = basename($path);
  189. return $data;
  190. }
  191. public function filetype($path) {
  192. $filetype = filetype($this->getSourcePath($path));
  193. if ($filetype == 'link') {
  194. $filetype = filetype(realpath($this->getSourcePath($path)));
  195. }
  196. return $filetype;
  197. }
  198. public function filesize($path): false|int|float {
  199. if (!$this->is_file($path)) {
  200. return 0;
  201. }
  202. $fullPath = $this->getSourcePath($path);
  203. if (PHP_INT_SIZE === 4) {
  204. $helper = new \OC\LargeFileHelper;
  205. return $helper->getFileSize($fullPath);
  206. }
  207. return filesize($fullPath);
  208. }
  209. public function isReadable($path) {
  210. return is_readable($this->getSourcePath($path));
  211. }
  212. public function isUpdatable($path) {
  213. return is_writable($this->getSourcePath($path));
  214. }
  215. public function file_exists($path) {
  216. if ($this->caseInsensitive) {
  217. $fullPath = $this->getSourcePath($path);
  218. $parentPath = dirname($fullPath);
  219. if (!is_dir($parentPath)) {
  220. return false;
  221. }
  222. $content = scandir($parentPath, SCANDIR_SORT_NONE);
  223. return is_array($content) && array_search(basename($fullPath), $content) !== false;
  224. } else {
  225. return file_exists($this->getSourcePath($path));
  226. }
  227. }
  228. public function filemtime($path) {
  229. $fullPath = $this->getSourcePath($path);
  230. clearstatcache(true, $fullPath);
  231. if (!$this->file_exists($path)) {
  232. return false;
  233. }
  234. if (PHP_INT_SIZE === 4) {
  235. $helper = new \OC\LargeFileHelper();
  236. return $helper->getFileMtime($fullPath);
  237. }
  238. return filemtime($fullPath);
  239. }
  240. public function touch($path, $mtime = null) {
  241. // sets the modification time of the file to the given value.
  242. // If mtime is nil the current time is set.
  243. // note that the access time of the file always changes to the current time.
  244. if ($this->file_exists($path) and !$this->isUpdatable($path)) {
  245. return false;
  246. }
  247. $oldMask = umask($this->defUMask);
  248. if (!is_null($mtime)) {
  249. $result = @touch($this->getSourcePath($path), $mtime);
  250. } else {
  251. $result = @touch($this->getSourcePath($path));
  252. }
  253. umask($oldMask);
  254. if ($result) {
  255. clearstatcache(true, $this->getSourcePath($path));
  256. }
  257. return $result;
  258. }
  259. public function file_get_contents($path) {
  260. return file_get_contents($this->getSourcePath($path));
  261. }
  262. public function file_put_contents($path, $data) {
  263. $oldMask = umask($this->defUMask);
  264. if ($this->unlinkOnTruncate) {
  265. $this->unlink($path);
  266. }
  267. $result = file_put_contents($this->getSourcePath($path), $data);
  268. umask($oldMask);
  269. return $result;
  270. }
  271. public function unlink($path) {
  272. if ($this->is_dir($path)) {
  273. return $this->rmdir($path);
  274. } elseif ($this->is_file($path)) {
  275. return unlink($this->getSourcePath($path));
  276. } else {
  277. return false;
  278. }
  279. }
  280. private function checkTreeForForbiddenItems(string $path) {
  281. $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
  282. foreach ($iterator as $file) {
  283. /** @var \SplFileInfo $file */
  284. if (Filesystem::isFileBlacklisted($file->getBasename())) {
  285. throw new ForbiddenException('Invalid path: ' . $file->getPathname(), false);
  286. }
  287. }
  288. }
  289. public function rename($source, $target): bool {
  290. $srcParent = dirname($source);
  291. $dstParent = dirname($target);
  292. if (!$this->isUpdatable($srcParent)) {
  293. \OC::$server->get(LoggerInterface::class)->error('unable to rename, source directory is not writable : ' . $srcParent, ['app' => 'core']);
  294. return false;
  295. }
  296. if (!$this->isUpdatable($dstParent)) {
  297. \OC::$server->get(LoggerInterface::class)->error('unable to rename, destination directory is not writable : ' . $dstParent, ['app' => 'core']);
  298. return false;
  299. }
  300. if (!$this->file_exists($source)) {
  301. \OC::$server->get(LoggerInterface::class)->error('unable to rename, file does not exists : ' . $source, ['app' => 'core']);
  302. return false;
  303. }
  304. if ($this->is_dir($target)) {
  305. $this->rmdir($target);
  306. } elseif ($this->is_file($target)) {
  307. $this->unlink($target);
  308. }
  309. if ($this->is_dir($source)) {
  310. $this->checkTreeForForbiddenItems($this->getSourcePath($source));
  311. }
  312. if (@rename($this->getSourcePath($source), $this->getSourcePath($target))) {
  313. if ($this->caseInsensitive) {
  314. if (mb_strtolower($target) === mb_strtolower($source) && !$this->file_exists($target)) {
  315. return false;
  316. }
  317. }
  318. return true;
  319. }
  320. return $this->copy($source, $target) && $this->unlink($source);
  321. }
  322. public function copy($source, $target) {
  323. if ($this->is_dir($source)) {
  324. return parent::copy($source, $target);
  325. } else {
  326. $oldMask = umask($this->defUMask);
  327. if ($this->unlinkOnTruncate) {
  328. $this->unlink($target);
  329. }
  330. $result = copy($this->getSourcePath($source), $this->getSourcePath($target));
  331. umask($oldMask);
  332. if ($this->caseInsensitive) {
  333. if (mb_strtolower($target) === mb_strtolower($source) && !$this->file_exists($target)) {
  334. return false;
  335. }
  336. }
  337. return $result;
  338. }
  339. }
  340. public function fopen($path, $mode) {
  341. $sourcePath = $this->getSourcePath($path);
  342. if (!file_exists($sourcePath) && $mode === 'r') {
  343. return false;
  344. }
  345. $oldMask = umask($this->defUMask);
  346. if (($mode === 'w' || $mode === 'w+') && $this->unlinkOnTruncate) {
  347. $this->unlink($path);
  348. }
  349. $result = @fopen($sourcePath, $mode);
  350. umask($oldMask);
  351. return $result;
  352. }
  353. public function hash($type, $path, $raw = false) {
  354. return hash_file($type, $this->getSourcePath($path), $raw);
  355. }
  356. public function free_space($path) {
  357. $sourcePath = $this->getSourcePath($path);
  358. // using !is_dir because $sourcePath might be a part file or
  359. // non-existing file, so we'd still want to use the parent dir
  360. // in such cases
  361. if (!is_dir($sourcePath)) {
  362. // disk_free_space doesn't work on files
  363. $sourcePath = dirname($sourcePath);
  364. }
  365. $space = (function_exists('disk_free_space') && is_dir($sourcePath)) ? disk_free_space($sourcePath) : false;
  366. if ($space === false || is_null($space)) {
  367. return \OCP\Files\FileInfo::SPACE_UNKNOWN;
  368. }
  369. return Util::numericToNumber($space);
  370. }
  371. public function search($query) {
  372. return $this->searchInDir($query);
  373. }
  374. public function getLocalFile($path) {
  375. return $this->getSourcePath($path);
  376. }
  377. /**
  378. * @param string $query
  379. * @param string $dir
  380. * @return array
  381. */
  382. protected function searchInDir($query, $dir = '') {
  383. $files = [];
  384. $physicalDir = $this->getSourcePath($dir);
  385. foreach (scandir($physicalDir) as $item) {
  386. if (\OC\Files\Filesystem::isIgnoredDir($item)) {
  387. continue;
  388. }
  389. $physicalItem = $physicalDir . '/' . $item;
  390. if (strstr(strtolower($item), strtolower($query)) !== false) {
  391. $files[] = $dir . '/' . $item;
  392. }
  393. if (is_dir($physicalItem)) {
  394. $files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
  395. }
  396. }
  397. return $files;
  398. }
  399. /**
  400. * check if a file or folder has been updated since $time
  401. *
  402. * @param string $path
  403. * @param int $time
  404. * @return bool
  405. */
  406. public function hasUpdated($path, $time) {
  407. if ($this->file_exists($path)) {
  408. return $this->filemtime($path) > $time;
  409. } else {
  410. return true;
  411. }
  412. }
  413. /**
  414. * Get the source path (on disk) of a given path
  415. *
  416. * @param string $path
  417. * @return string
  418. * @throws ForbiddenException
  419. */
  420. public function getSourcePath($path) {
  421. if (Filesystem::isFileBlacklisted($path)) {
  422. throw new ForbiddenException('Invalid path: ' . $path, false);
  423. }
  424. $fullPath = $this->datadir . $path;
  425. $currentPath = $path;
  426. $allowSymlinks = $this->config->getSystemValueBool('localstorage.allowsymlinks', false);
  427. if ($allowSymlinks || $currentPath === '') {
  428. return $fullPath;
  429. }
  430. $pathToResolve = $fullPath;
  431. $realPath = realpath($pathToResolve);
  432. while ($realPath === false) { // for non existing files check the parent directory
  433. $currentPath = dirname($currentPath);
  434. /** @psalm-suppress TypeDoesNotContainType Let's be extra cautious and still check for empty string */
  435. if ($currentPath === '' || $currentPath === '.') {
  436. return $fullPath;
  437. }
  438. $realPath = realpath($this->datadir . $currentPath);
  439. }
  440. if ($realPath) {
  441. $realPath = $realPath . '/';
  442. }
  443. if (substr($realPath, 0, $this->dataDirLength) === $this->realDataDir) {
  444. return $fullPath;
  445. }
  446. \OC::$server->get(LoggerInterface::class)->error("Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", ['app' => 'core']);
  447. throw new ForbiddenException('Following symlinks is not allowed', false);
  448. }
  449. /**
  450. * {@inheritdoc}
  451. */
  452. public function isLocal() {
  453. return true;
  454. }
  455. /**
  456. * get the ETag for a file or folder
  457. *
  458. * @param string $path
  459. * @return string
  460. */
  461. public function getETag($path) {
  462. return $this->calculateEtag($path, $this->stat($path));
  463. }
  464. private function calculateEtag(string $path, array $stat): string {
  465. if ($stat['mode'] & 0x4000 && !($stat['mode'] & 0x8000)) { // is_dir & not socket
  466. return parent::getETag($path);
  467. } else {
  468. if ($stat === false) {
  469. return md5('');
  470. }
  471. $toHash = '';
  472. if (isset($stat['mtime'])) {
  473. $toHash .= $stat['mtime'];
  474. }
  475. if (isset($stat['ino'])) {
  476. $toHash .= $stat['ino'];
  477. }
  478. if (isset($stat['dev'])) {
  479. $toHash .= $stat['dev'];
  480. }
  481. if (isset($stat['size'])) {
  482. $toHash .= $stat['size'];
  483. }
  484. return md5($toHash);
  485. }
  486. }
  487. private function canDoCrossStorageMove(IStorage $sourceStorage) {
  488. /** @psalm-suppress UndefinedClass */
  489. return $sourceStorage->instanceOfStorage(Local::class)
  490. // Don't treat ACLStorageWrapper like local storage where copy can be done directly.
  491. // Instead, use the slower recursive copying in php from Common::copyFromStorage with
  492. // more permissions checks.
  493. && !$sourceStorage->instanceOfStorage('OCA\GroupFolders\ACL\ACLStorageWrapper')
  494. // Same for access control
  495. && !$sourceStorage->instanceOfStorage(\OCA\FilesAccessControl\StorageWrapper::class)
  496. // when moving encrypted files we have to handle keys and the target might not be encrypted
  497. && !$sourceStorage->instanceOfStorage(Encryption::class);
  498. }
  499. /**
  500. * @param IStorage $sourceStorage
  501. * @param string $sourceInternalPath
  502. * @param string $targetInternalPath
  503. * @param bool $preserveMtime
  504. * @return bool
  505. */
  506. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) {
  507. if ($this->canDoCrossStorageMove($sourceStorage)) {
  508. if ($sourceStorage->instanceOfStorage(Jail::class)) {
  509. /**
  510. * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
  511. */
  512. $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
  513. }
  514. /**
  515. * @var \OC\Files\Storage\Local $sourceStorage
  516. */
  517. $rootStorage = new Local(['datadir' => '/']);
  518. return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
  519. } else {
  520. return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  521. }
  522. }
  523. /**
  524. * @param IStorage $sourceStorage
  525. * @param string $sourceInternalPath
  526. * @param string $targetInternalPath
  527. * @return bool
  528. */
  529. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  530. if ($this->canDoCrossStorageMove($sourceStorage)) {
  531. if ($sourceStorage->instanceOfStorage(Jail::class)) {
  532. /**
  533. * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
  534. */
  535. $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
  536. }
  537. /**
  538. * @var \OC\Files\Storage\Local $sourceStorage
  539. */
  540. $rootStorage = new Local(['datadir' => '/']);
  541. return $rootStorage->rename($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
  542. } else {
  543. return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  544. }
  545. }
  546. public function writeStream(string $path, $stream, ?int $size = null): int {
  547. /** @var int|false $result We consider here that returned size will never be a float because we write less than 4GB */
  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. }