Local.php 19 KB

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