Common.php 21 KB

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