Common.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Greta Doci <gretadoci@gmail.com>
  9. * @author hkjolhede <hkjolhede@gmail.com>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  12. * @author Julius Härtl <jus@bitgrid.net>
  13. * @author Lukas Reschke <lukas@statuscode.ch>
  14. * @author Martin Mattel <martin.mattel@diemattels.at>
  15. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  16. * @author Morris Jobke <hey@morrisjobke.de>
  17. * @author Robin Appelman <robin@icewind.nl>
  18. * @author Robin McCorkell <robin@mccorkell.me.uk>
  19. * @author Roeland Jago Douma <roeland@famdouma.nl>
  20. * @author Roland Tapken <roland@bitarbeiter.net>
  21. * @author Sam Tuke <mail@samtuke.com>
  22. * @author scambra <sergio@entrecables.com>
  23. * @author Stefan Weil <sw@weilnetz.de>
  24. * @author Thomas Müller <thomas.mueller@tmit.eu>
  25. * @author Vincent Petry <vincent@nextcloud.com>
  26. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  27. *
  28. * @license AGPL-3.0
  29. *
  30. * This code is free software: you can redistribute it and/or modify
  31. * it under the terms of the GNU Affero General Public License, version 3,
  32. * as published by the Free Software Foundation.
  33. *
  34. * This program is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU Affero General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU Affero General Public License, version 3,
  40. * along with this program. If not, see <http://www.gnu.org/licenses/>
  41. *
  42. */
  43. namespace OC\Files\Storage;
  44. use OC\Files\Cache\Cache;
  45. use OC\Files\Cache\CacheDependencies;
  46. use OC\Files\Cache\Propagator;
  47. use OC\Files\Cache\Scanner;
  48. use OC\Files\Cache\Updater;
  49. use OC\Files\Cache\Watcher;
  50. use OC\Files\Filesystem;
  51. use OC\Files\Storage\Wrapper\Jail;
  52. use OC\Files\Storage\Wrapper\Wrapper;
  53. use OCP\Files\EmptyFileNameException;
  54. use OCP\Files\FileNameTooLongException;
  55. use OCP\Files\ForbiddenException;
  56. use OCP\Files\GenericFileException;
  57. use OCP\Files\InvalidCharacterInPathException;
  58. use OCP\Files\InvalidDirectoryException;
  59. use OCP\Files\InvalidPathException;
  60. use OCP\Files\ReservedWordException;
  61. use OCP\Files\Storage\ILockingStorage;
  62. use OCP\Files\Storage\IStorage;
  63. use OCP\Files\Storage\IWriteStreamStorage;
  64. use OCP\Files\StorageNotAvailableException;
  65. use OCP\Lock\ILockingProvider;
  66. use OCP\Lock\LockedException;
  67. use OCP\Server;
  68. use Psr\Log\LoggerInterface;
  69. /**
  70. * Storage backend class for providing common filesystem operation methods
  71. * which are not storage-backend specific.
  72. *
  73. * \OC\Files\Storage\Common is never used directly; it is extended by all other
  74. * storage backends, where its methods may be overridden, and additional
  75. * (backend-specific) methods are defined.
  76. *
  77. * Some \OC\Files\Storage\Common methods call functions which are first defined
  78. * in classes which extend it, e.g. $this->stat() .
  79. */
  80. abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage {
  81. use LocalTempFileTrait;
  82. protected $cache;
  83. protected $scanner;
  84. protected $watcher;
  85. protected $propagator;
  86. protected $storageCache;
  87. protected $updater;
  88. protected $mountOptions = [];
  89. protected $owner = null;
  90. /** @var ?bool */
  91. private $shouldLogLocks = null;
  92. /** @var ?LoggerInterface */
  93. private $logger;
  94. public function __construct($parameters) {
  95. }
  96. /**
  97. * Remove a file or folder
  98. *
  99. * @param string $path
  100. * @return bool
  101. */
  102. protected function remove($path) {
  103. if ($this->is_dir($path)) {
  104. return $this->rmdir($path);
  105. } elseif ($this->is_file($path)) {
  106. return $this->unlink($path);
  107. } else {
  108. return false;
  109. }
  110. }
  111. public function is_dir($path) {
  112. return $this->filetype($path) === 'dir';
  113. }
  114. public function is_file($path) {
  115. return $this->filetype($path) === 'file';
  116. }
  117. public function filesize($path): false|int|float {
  118. if ($this->is_dir($path)) {
  119. return 0; //by definition
  120. } else {
  121. $stat = $this->stat($path);
  122. if (isset($stat['size'])) {
  123. return $stat['size'];
  124. } else {
  125. return 0;
  126. }
  127. }
  128. }
  129. public function isReadable($path) {
  130. // at least check whether it exists
  131. // subclasses might want to implement this more thoroughly
  132. return $this->file_exists($path);
  133. }
  134. public function isUpdatable($path) {
  135. // at least check whether it exists
  136. // subclasses might want to implement this more thoroughly
  137. // a non-existing file/folder isn't updatable
  138. return $this->file_exists($path);
  139. }
  140. public function isCreatable($path) {
  141. if ($this->is_dir($path) && $this->isUpdatable($path)) {
  142. return true;
  143. }
  144. return false;
  145. }
  146. public function isDeletable($path) {
  147. if ($path === '' || $path === '/') {
  148. return $this->isUpdatable($path);
  149. }
  150. $parent = dirname($path);
  151. return $this->isUpdatable($parent) && $this->isUpdatable($path);
  152. }
  153. public function isSharable($path) {
  154. return $this->isReadable($path);
  155. }
  156. public function getPermissions($path) {
  157. $permissions = 0;
  158. if ($this->isCreatable($path)) {
  159. $permissions |= \OCP\Constants::PERMISSION_CREATE;
  160. }
  161. if ($this->isReadable($path)) {
  162. $permissions |= \OCP\Constants::PERMISSION_READ;
  163. }
  164. if ($this->isUpdatable($path)) {
  165. $permissions |= \OCP\Constants::PERMISSION_UPDATE;
  166. }
  167. if ($this->isDeletable($path)) {
  168. $permissions |= \OCP\Constants::PERMISSION_DELETE;
  169. }
  170. if ($this->isSharable($path)) {
  171. $permissions |= \OCP\Constants::PERMISSION_SHARE;
  172. }
  173. return $permissions;
  174. }
  175. public function filemtime($path) {
  176. $stat = $this->stat($path);
  177. if (isset($stat['mtime']) && $stat['mtime'] > 0) {
  178. return $stat['mtime'];
  179. } else {
  180. return 0;
  181. }
  182. }
  183. public function file_get_contents($path) {
  184. $handle = $this->fopen($path, "r");
  185. if (!$handle) {
  186. return false;
  187. }
  188. $data = stream_get_contents($handle);
  189. fclose($handle);
  190. return $data;
  191. }
  192. public function file_put_contents($path, $data) {
  193. $handle = $this->fopen($path, "w");
  194. if (!$handle) {
  195. return false;
  196. }
  197. $this->removeCachedFile($path);
  198. $count = fwrite($handle, $data);
  199. fclose($handle);
  200. return $count;
  201. }
  202. public function rename($source, $target) {
  203. $this->remove($target);
  204. $this->removeCachedFile($source);
  205. return $this->copy($source, $target) and $this->remove($source);
  206. }
  207. public function copy($source, $target) {
  208. if ($this->is_dir($source)) {
  209. $this->remove($target);
  210. $dir = $this->opendir($source);
  211. $this->mkdir($target);
  212. while ($file = readdir($dir)) {
  213. if (!Filesystem::isIgnoredDir($file)) {
  214. if (!$this->copy($source . '/' . $file, $target . '/' . $file)) {
  215. closedir($dir);
  216. return false;
  217. }
  218. }
  219. }
  220. closedir($dir);
  221. return true;
  222. } else {
  223. $sourceStream = $this->fopen($source, 'r');
  224. $targetStream = $this->fopen($target, 'w');
  225. [, $result] = \OC_Helper::streamCopy($sourceStream, $targetStream);
  226. if (!$result) {
  227. \OCP\Server::get(LoggerInterface::class)->warning("Failed to write data while copying $source to $target");
  228. }
  229. $this->removeCachedFile($target);
  230. return $result;
  231. }
  232. }
  233. public function getMimeType($path) {
  234. if ($this->is_dir($path)) {
  235. return 'httpd/unix-directory';
  236. } elseif ($this->file_exists($path)) {
  237. return \OC::$server->getMimeTypeDetector()->detectPath($path);
  238. } else {
  239. return false;
  240. }
  241. }
  242. public function hash($type, $path, $raw = false) {
  243. $fh = $this->fopen($path, 'rb');
  244. $ctx = hash_init($type);
  245. hash_update_stream($ctx, $fh);
  246. fclose($fh);
  247. return hash_final($ctx, $raw);
  248. }
  249. public function search($query) {
  250. return $this->searchInDir($query);
  251. }
  252. public function getLocalFile($path) {
  253. return $this->getCachedFile($path);
  254. }
  255. /**
  256. * @param string $path
  257. * @param string $target
  258. */
  259. private function addLocalFolder($path, $target) {
  260. $dh = $this->opendir($path);
  261. if (is_resource($dh)) {
  262. while (($file = readdir($dh)) !== false) {
  263. if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
  264. if ($this->is_dir($path . '/' . $file)) {
  265. mkdir($target . '/' . $file);
  266. $this->addLocalFolder($path . '/' . $file, $target . '/' . $file);
  267. } else {
  268. $tmp = $this->toTmpFile($path . '/' . $file);
  269. rename($tmp, $target . '/' . $file);
  270. }
  271. }
  272. }
  273. }
  274. }
  275. /**
  276. * @param string $query
  277. * @param string $dir
  278. * @return array
  279. */
  280. protected function searchInDir($query, $dir = '') {
  281. $files = [];
  282. $dh = $this->opendir($dir);
  283. if (is_resource($dh)) {
  284. while (($item = readdir($dh)) !== false) {
  285. if (\OC\Files\Filesystem::isIgnoredDir($item)) {
  286. continue;
  287. }
  288. if (strstr(strtolower($item), strtolower($query)) !== false) {
  289. $files[] = $dir . '/' . $item;
  290. }
  291. if ($this->is_dir($dir . '/' . $item)) {
  292. $files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
  293. }
  294. }
  295. }
  296. closedir($dh);
  297. return $files;
  298. }
  299. /**
  300. * Check if a file or folder has been updated since $time
  301. *
  302. * The method is only used to check if the cache needs to be updated. Storage backends that don't support checking
  303. * the mtime should always return false here. As a result storage implementations that always return false expect
  304. * exclusive access to the backend and will not pick up files that have been added in a way that circumvents
  305. * Nextcloud filesystem.
  306. *
  307. * @param string $path
  308. * @param int $time
  309. * @return bool
  310. */
  311. public function hasUpdated($path, $time) {
  312. return $this->filemtime($path) > $time;
  313. }
  314. protected function getCacheDependencies(): CacheDependencies {
  315. static $dependencies = null;
  316. if (!$dependencies) {
  317. $dependencies = Server::get(CacheDependencies::class);
  318. }
  319. return $dependencies;
  320. }
  321. public function getCache($path = '', $storage = null) {
  322. if (!$storage) {
  323. $storage = $this;
  324. }
  325. if (!isset($storage->cache)) {
  326. $storage->cache = new Cache($storage, $this->getCacheDependencies());
  327. }
  328. return $storage->cache;
  329. }
  330. public function getScanner($path = '', $storage = null) {
  331. if (!$storage) {
  332. $storage = $this;
  333. }
  334. if (!isset($storage->scanner)) {
  335. $storage->scanner = new Scanner($storage);
  336. }
  337. return $storage->scanner;
  338. }
  339. public function getWatcher($path = '', $storage = null) {
  340. if (!$storage) {
  341. $storage = $this;
  342. }
  343. if (!isset($this->watcher)) {
  344. $this->watcher = new Watcher($storage);
  345. $globalPolicy = \OC::$server->getConfig()->getSystemValue('filesystem_check_changes', Watcher::CHECK_NEVER);
  346. $this->watcher->setPolicy((int)$this->getMountOption('filesystem_check_changes', $globalPolicy));
  347. }
  348. return $this->watcher;
  349. }
  350. /**
  351. * get a propagator instance for the cache
  352. *
  353. * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher
  354. * @return \OC\Files\Cache\Propagator
  355. */
  356. public function getPropagator($storage = null) {
  357. if (!$storage) {
  358. $storage = $this;
  359. }
  360. if (!isset($storage->propagator)) {
  361. $config = \OC::$server->getSystemConfig();
  362. $storage->propagator = new Propagator($storage, \OC::$server->getDatabaseConnection(), ['appdata_' . $config->getValue('instanceid')]);
  363. }
  364. return $storage->propagator;
  365. }
  366. public function getUpdater($storage = null) {
  367. if (!$storage) {
  368. $storage = $this;
  369. }
  370. if (!isset($storage->updater)) {
  371. $storage->updater = new Updater($storage);
  372. }
  373. return $storage->updater;
  374. }
  375. public function getStorageCache($storage = null) {
  376. return $this->getCache($storage)->getStorageCache();
  377. }
  378. /**
  379. * get the owner of a path
  380. *
  381. * @param string $path The path to get the owner
  382. * @return string|false uid or false
  383. */
  384. public function getOwner($path) {
  385. if ($this->owner === null) {
  386. $this->owner = \OC_User::getUser();
  387. }
  388. return $this->owner;
  389. }
  390. /**
  391. * get the ETag for a file or folder
  392. *
  393. * @param string $path
  394. * @return string
  395. */
  396. public function getETag($path) {
  397. return uniqid();
  398. }
  399. /**
  400. * clean a path, i.e. remove all redundant '.' and '..'
  401. * making sure that it can't point to higher than '/'
  402. *
  403. * @param string $path The path to clean
  404. * @return string cleaned path
  405. */
  406. public function cleanPath($path) {
  407. if (strlen($path) == 0 or $path[0] != '/') {
  408. $path = '/' . $path;
  409. }
  410. $output = [];
  411. foreach (explode('/', $path) as $chunk) {
  412. if ($chunk == '..') {
  413. array_pop($output);
  414. } elseif ($chunk == '.') {
  415. } else {
  416. $output[] = $chunk;
  417. }
  418. }
  419. return implode('/', $output);
  420. }
  421. /**
  422. * Test a storage for availability
  423. *
  424. * @return bool
  425. */
  426. public function test() {
  427. try {
  428. if ($this->stat('')) {
  429. return true;
  430. }
  431. \OC::$server->get(LoggerInterface::class)->info("External storage not available: stat() failed");
  432. return false;
  433. } catch (\Exception $e) {
  434. \OC::$server->get(LoggerInterface::class)->warning(
  435. "External storage not available: " . $e->getMessage(),
  436. ['exception' => $e]
  437. );
  438. return false;
  439. }
  440. }
  441. /**
  442. * get the free space in the storage
  443. *
  444. * @param string $path
  445. * @return int|float|false
  446. */
  447. public function free_space($path) {
  448. return \OCP\Files\FileInfo::SPACE_UNKNOWN;
  449. }
  450. /**
  451. * {@inheritdoc}
  452. */
  453. public function isLocal() {
  454. // the common implementation returns a temporary file by
  455. // default, which is not local
  456. return false;
  457. }
  458. /**
  459. * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class
  460. *
  461. * @param string $class
  462. * @return bool
  463. */
  464. public function instanceOfStorage($class) {
  465. if (ltrim($class, '\\') === 'OC\Files\Storage\Shared') {
  466. // FIXME Temporary fix to keep existing checks working
  467. $class = '\OCA\Files_Sharing\SharedStorage';
  468. }
  469. return is_a($this, $class);
  470. }
  471. /**
  472. * A custom storage implementation can return an url for direct download of a give file.
  473. *
  474. * For now the returned array can hold the parameter url - in future more attributes might follow.
  475. *
  476. * @param string $path
  477. * @return array|false
  478. */
  479. public function getDirectDownload($path) {
  480. return [];
  481. }
  482. /**
  483. * @inheritdoc
  484. * @throws InvalidPathException
  485. */
  486. public function verifyPath($path, $fileName) {
  487. // verify empty and dot files
  488. $trimmed = trim($fileName);
  489. if ($trimmed === '') {
  490. throw new EmptyFileNameException();
  491. }
  492. if (\OC\Files\Filesystem::isIgnoredDir($trimmed)) {
  493. throw new InvalidDirectoryException();
  494. }
  495. if (!\OC::$server->getDatabaseConnection()->supports4ByteText()) {
  496. // verify database - e.g. mysql only 3-byte chars
  497. if (preg_match('%(?:
  498. \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  499. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  500. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  501. )%xs', $fileName)) {
  502. throw new InvalidCharacterInPathException();
  503. }
  504. }
  505. // 255 characters is the limit on common file systems (ext/xfs)
  506. // oc_filecache has a 250 char length limit for the filename
  507. if (isset($fileName[250])) {
  508. throw new FileNameTooLongException();
  509. }
  510. // NOTE: $path will remain unverified for now
  511. $this->verifyPosixPath($fileName);
  512. }
  513. /**
  514. * @param string $fileName
  515. * @throws InvalidPathException
  516. */
  517. protected function verifyPosixPath($fileName) {
  518. $invalidChars = \OCP\Util::getForbiddenFileNameChars();
  519. $this->scanForInvalidCharacters($fileName, $invalidChars);
  520. $fileName = trim($fileName);
  521. $reservedNames = ['*'];
  522. if (in_array($fileName, $reservedNames)) {
  523. throw new ReservedWordException();
  524. }
  525. }
  526. /**
  527. * @param string $fileName
  528. * @param string[] $invalidChars
  529. * @throws InvalidPathException
  530. */
  531. private function scanForInvalidCharacters(string $fileName, array $invalidChars) {
  532. foreach ($invalidChars as $char) {
  533. if (str_contains($fileName, $char)) {
  534. throw new InvalidCharacterInPathException();
  535. }
  536. }
  537. $sanitizedFileName = filter_var($fileName, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
  538. if ($sanitizedFileName !== $fileName) {
  539. throw new InvalidCharacterInPathException();
  540. }
  541. }
  542. /**
  543. * @param array $options
  544. */
  545. public function setMountOptions(array $options) {
  546. $this->mountOptions = $options;
  547. }
  548. /**
  549. * @param string $name
  550. * @param mixed $default
  551. * @return mixed
  552. */
  553. public function getMountOption($name, $default = null) {
  554. return $this->mountOptions[$name] ?? $default;
  555. }
  556. /**
  557. * @param IStorage $sourceStorage
  558. * @param string $sourceInternalPath
  559. * @param string $targetInternalPath
  560. * @param bool $preserveMtime
  561. * @return bool
  562. */
  563. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) {
  564. if ($sourceStorage === $this) {
  565. return $this->copy($sourceInternalPath, $targetInternalPath);
  566. }
  567. if ($sourceStorage->is_dir($sourceInternalPath)) {
  568. $dh = $sourceStorage->opendir($sourceInternalPath);
  569. $result = $this->mkdir($targetInternalPath);
  570. if (is_resource($dh)) {
  571. $result = true;
  572. while ($result and ($file = readdir($dh)) !== false) {
  573. if (!Filesystem::isIgnoredDir($file)) {
  574. $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file);
  575. }
  576. }
  577. }
  578. } else {
  579. $source = $sourceStorage->fopen($sourceInternalPath, 'r');
  580. $result = false;
  581. if ($source) {
  582. try {
  583. $this->writeStream($targetInternalPath, $source);
  584. $result = true;
  585. } catch (\Exception $e) {
  586. \OC::$server->get(LoggerInterface::class)->warning('Failed to copy stream to storage', ['exception' => $e]);
  587. }
  588. }
  589. if ($result && $preserveMtime) {
  590. $mtime = $sourceStorage->filemtime($sourceInternalPath);
  591. $this->touch($targetInternalPath, is_int($mtime) ? $mtime : null);
  592. }
  593. if (!$result) {
  594. // delete partially written target file
  595. $this->unlink($targetInternalPath);
  596. // delete cache entry that was created by fopen
  597. $this->getCache()->remove($targetInternalPath);
  598. }
  599. }
  600. return (bool)$result;
  601. }
  602. /**
  603. * Check if a storage is the same as the current one, including wrapped storages
  604. *
  605. * @param IStorage $storage
  606. * @return bool
  607. */
  608. private function isSameStorage(IStorage $storage): bool {
  609. while ($storage->instanceOfStorage(Wrapper::class)) {
  610. /**
  611. * @var Wrapper $storage
  612. */
  613. $storage = $storage->getWrapperStorage();
  614. }
  615. return $storage === $this;
  616. }
  617. /**
  618. * @param IStorage $sourceStorage
  619. * @param string $sourceInternalPath
  620. * @param string $targetInternalPath
  621. * @return bool
  622. */
  623. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  624. if ($this->isSameStorage($sourceStorage)) {
  625. // resolve any jailed paths
  626. while ($sourceStorage->instanceOfStorage(Jail::class)) {
  627. /**
  628. * @var Jail $sourceStorage
  629. */
  630. $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
  631. $sourceStorage = $sourceStorage->getUnjailedStorage();
  632. }
  633. return $this->rename($sourceInternalPath, $targetInternalPath);
  634. }
  635. if (!$sourceStorage->isDeletable($sourceInternalPath)) {
  636. return false;
  637. }
  638. $result = $this->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, true);
  639. if ($result) {
  640. if ($sourceStorage->is_dir($sourceInternalPath)) {
  641. $result = $sourceStorage->rmdir($sourceInternalPath);
  642. } else {
  643. $result = $sourceStorage->unlink($sourceInternalPath);
  644. }
  645. }
  646. return $result;
  647. }
  648. /**
  649. * @inheritdoc
  650. */
  651. public function getMetaData($path) {
  652. if (Filesystem::isFileBlacklisted($path)) {
  653. throw new ForbiddenException('Invalid path: ' . $path, false);
  654. }
  655. $permissions = $this->getPermissions($path);
  656. if (!$permissions & \OCP\Constants::PERMISSION_READ) {
  657. //can't read, nothing we can do
  658. return null;
  659. }
  660. $data = [];
  661. $data['mimetype'] = $this->getMimeType($path);
  662. $data['mtime'] = $this->filemtime($path);
  663. if ($data['mtime'] === false) {
  664. $data['mtime'] = time();
  665. }
  666. if ($data['mimetype'] == 'httpd/unix-directory') {
  667. $data['size'] = -1; //unknown
  668. } else {
  669. $data['size'] = $this->filesize($path);
  670. }
  671. $data['etag'] = $this->getETag($path);
  672. $data['storage_mtime'] = $data['mtime'];
  673. $data['permissions'] = $permissions;
  674. $data['name'] = basename($path);
  675. return $data;
  676. }
  677. /**
  678. * @param string $path
  679. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  680. * @param \OCP\Lock\ILockingProvider $provider
  681. * @throws \OCP\Lock\LockedException
  682. */
  683. public function acquireLock($path, $type, ILockingProvider $provider) {
  684. $logger = $this->getLockLogger();
  685. if ($logger) {
  686. $typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive';
  687. $logger->info(
  688. sprintf(
  689. 'acquire %s lock on "%s" on storage "%s"',
  690. $typeString,
  691. $path,
  692. $this->getId()
  693. ),
  694. [
  695. 'app' => 'locking',
  696. ]
  697. );
  698. }
  699. try {
  700. $provider->acquireLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type, $this->getId() . '::' . $path);
  701. } catch (LockedException $e) {
  702. $e = new LockedException($e->getPath(), $e, $e->getExistingLock(), $path);
  703. if ($logger) {
  704. $logger->info($e->getMessage(), ['exception' => $e]);
  705. }
  706. throw $e;
  707. }
  708. }
  709. /**
  710. * @param string $path
  711. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  712. * @param \OCP\Lock\ILockingProvider $provider
  713. * @throws \OCP\Lock\LockedException
  714. */
  715. public function releaseLock($path, $type, ILockingProvider $provider) {
  716. $logger = $this->getLockLogger();
  717. if ($logger) {
  718. $typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive';
  719. $logger->info(
  720. sprintf(
  721. 'release %s lock on "%s" on storage "%s"',
  722. $typeString,
  723. $path,
  724. $this->getId()
  725. ),
  726. [
  727. 'app' => 'locking',
  728. ]
  729. );
  730. }
  731. try {
  732. $provider->releaseLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type);
  733. } catch (LockedException $e) {
  734. $e = new LockedException($e->getPath(), $e, $e->getExistingLock(), $path);
  735. if ($logger) {
  736. $logger->info($e->getMessage(), ['exception' => $e]);
  737. }
  738. throw $e;
  739. }
  740. }
  741. /**
  742. * @param string $path
  743. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  744. * @param \OCP\Lock\ILockingProvider $provider
  745. * @throws \OCP\Lock\LockedException
  746. */
  747. public function changeLock($path, $type, ILockingProvider $provider) {
  748. $logger = $this->getLockLogger();
  749. if ($logger) {
  750. $typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive';
  751. $logger->info(
  752. sprintf(
  753. 'change lock on "%s" to %s on storage "%s"',
  754. $path,
  755. $typeString,
  756. $this->getId()
  757. ),
  758. [
  759. 'app' => 'locking',
  760. ]
  761. );
  762. }
  763. try {
  764. $provider->changeLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type);
  765. } catch (LockedException $e) {
  766. $e = new LockedException($e->getPath(), $e, $e->getExistingLock(), $path);
  767. if ($logger) {
  768. $logger->info($e->getMessage(), ['exception' => $e]);
  769. }
  770. throw $e;
  771. }
  772. }
  773. private function getLockLogger(): ?LoggerInterface {
  774. if (is_null($this->shouldLogLocks)) {
  775. $this->shouldLogLocks = \OC::$server->getConfig()->getSystemValueBool('filelocking.debug', false);
  776. $this->logger = $this->shouldLogLocks ? \OC::$server->get(LoggerInterface::class) : null;
  777. }
  778. return $this->logger;
  779. }
  780. /**
  781. * @return array [ available, last_checked ]
  782. */
  783. public function getAvailability() {
  784. return $this->getStorageCache()->getAvailability();
  785. }
  786. /**
  787. * @param bool $isAvailable
  788. */
  789. public function setAvailability($isAvailable) {
  790. $this->getStorageCache()->setAvailability($isAvailable);
  791. }
  792. /**
  793. * @return bool
  794. */
  795. public function needsPartFile() {
  796. return true;
  797. }
  798. /**
  799. * fallback implementation
  800. *
  801. * @param string $path
  802. * @param resource $stream
  803. * @param int $size
  804. * @return int
  805. */
  806. public function writeStream(string $path, $stream, int $size = null): int {
  807. $target = $this->fopen($path, 'w');
  808. if (!$target) {
  809. throw new GenericFileException("Failed to open $path for writing");
  810. }
  811. try {
  812. [$count, $result] = \OC_Helper::streamCopy($stream, $target);
  813. if (!$result) {
  814. throw new GenericFileException("Failed to copy stream");
  815. }
  816. } finally {
  817. fclose($target);
  818. fclose($stream);
  819. }
  820. return $count;
  821. }
  822. public function getDirectoryContent($directory): \Traversable {
  823. $dh = $this->opendir($directory);
  824. if ($dh === false) {
  825. throw new StorageNotAvailableException('Directory listing failed');
  826. }
  827. if (is_resource($dh)) {
  828. $basePath = rtrim($directory, '/');
  829. while (($file = readdir($dh)) !== false) {
  830. if (!Filesystem::isIgnoredDir($file)) {
  831. $childPath = $basePath . '/' . trim($file, '/');
  832. $metadata = $this->getMetaData($childPath);
  833. if ($metadata !== null) {
  834. yield $metadata;
  835. }
  836. }
  837. }
  838. }
  839. }
  840. }