Local.php 18 KB

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