Local.php 19 KB

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