propagator.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Files\Cache;
  23. use OCP\Files\Cache\IPropagator;
  24. /**
  25. * Propagate etags and mtimes within the storage
  26. */
  27. class Propagator implements IPropagator {
  28. /**
  29. * @var \OC\Files\Storage\Storage
  30. */
  31. protected $storage;
  32. /**
  33. * @param \OC\Files\Storage\Storage $storage
  34. */
  35. public function __construct(\OC\Files\Storage\Storage $storage) {
  36. $this->storage = $storage;
  37. }
  38. /**
  39. * @param string $internalPath
  40. * @param int $time
  41. * @param int $sizeDifference number of bytes the file has grown
  42. * @return array[] all propagated entries
  43. */
  44. public function propagateChange($internalPath, $time, $sizeDifference = 0) {
  45. $cache = $this->storage->getCache($internalPath);
  46. $parentId = $cache->getParentId($internalPath);
  47. $propagatedEntries = [];
  48. while ($parentId !== -1) {
  49. $entry = $cache->get($parentId);
  50. $propagatedEntries[] = $entry;
  51. if (!$entry) {
  52. return $propagatedEntries;
  53. }
  54. $mtime = max($time, $entry['mtime']);
  55. if ($entry['size'] === -1) {
  56. $newSize = -1;
  57. } else {
  58. $newSize = $entry['size'] + $sizeDifference;
  59. }
  60. $cache->update($parentId, ['mtime' => $mtime, 'etag' => $this->storage->getETag($entry['path']), 'size' => $newSize]);
  61. $parentId = $entry['parent'];
  62. }
  63. return $propagatedEntries;
  64. }
  65. }