KnownMtime.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace OC\Files\Storage\Wrapper;
  3. use OCP\Cache\CappedMemoryCache;
  4. use OCP\Files\Storage\IStorage;
  5. use Psr\Clock\ClockInterface;
  6. /**
  7. * Wrapper that overwrites the mtime return by stat/getMetaData if the returned value
  8. * is lower than when we last modified the file.
  9. *
  10. * This is useful because some storage servers can return an outdated mtime right after writes
  11. */
  12. class KnownMtime extends Wrapper {
  13. private CappedMemoryCache $knowMtimes;
  14. private ClockInterface $clock;
  15. public function __construct($arguments) {
  16. parent::__construct($arguments);
  17. $this->knowMtimes = new CappedMemoryCache();
  18. $this->clock = $arguments['clock'];
  19. }
  20. public function file_put_contents($path, $data) {
  21. $result = parent::file_put_contents($path, $data);
  22. if ($result) {
  23. $now = $this->clock->now()->getTimestamp();
  24. $this->knowMtimes->set($path, $this->clock->now()->getTimestamp());
  25. }
  26. return $result;
  27. }
  28. public function stat($path) {
  29. $stat = parent::stat($path);
  30. if ($stat) {
  31. $this->applyKnownMtime($path, $stat);
  32. }
  33. return $stat;
  34. }
  35. public function getMetaData($path) {
  36. $stat = parent::getMetaData($path);
  37. if ($stat) {
  38. $this->applyKnownMtime($path, $stat);
  39. }
  40. return $stat;
  41. }
  42. private function applyKnownMtime(string $path, array &$stat) {
  43. if (isset($stat['mtime'])) {
  44. $knownMtime = $this->knowMtimes->get($path) ?? 0;
  45. $stat['mtime'] = max($stat['mtime'], $knownMtime);
  46. }
  47. }
  48. public function filemtime($path) {
  49. $knownMtime = $this->knowMtimes->get($path) ?? 0;
  50. return max(parent::filemtime($path), $knownMtime);
  51. }
  52. public function mkdir($path) {
  53. $result = parent::mkdir($path);
  54. if ($result) {
  55. $this->knowMtimes->set($path, $this->clock->now()->getTimestamp());
  56. }
  57. return $result;
  58. }
  59. public function rmdir($path) {
  60. $result = parent::rmdir($path);
  61. if ($result) {
  62. $this->knowMtimes->set($path, $this->clock->now()->getTimestamp());
  63. }
  64. return $result;
  65. }
  66. public function unlink($path) {
  67. $result = parent::unlink($path);
  68. if ($result) {
  69. $this->knowMtimes->set($path, $this->clock->now()->getTimestamp());
  70. }
  71. return $result;
  72. }
  73. public function rename($source, $target) {
  74. $result = parent::rename($source, $target);
  75. if ($result) {
  76. $this->knowMtimes->set($target, $this->clock->now()->getTimestamp());
  77. $this->knowMtimes->set($source, $this->clock->now()->getTimestamp());
  78. }
  79. return $result;
  80. }
  81. public function copy($source, $target) {
  82. $result = parent::copy($source, $target);
  83. if ($result) {
  84. $this->knowMtimes->set($target, $this->clock->now()->getTimestamp());
  85. }
  86. return $result;
  87. }
  88. public function fopen($path, $mode) {
  89. $result = parent::fopen($path, $mode);
  90. if ($result && $mode === 'w') {
  91. $this->knowMtimes->set($path, $this->clock->now()->getTimestamp());
  92. }
  93. return $result;
  94. }
  95. public function touch($path, $mtime = null) {
  96. $result = parent::touch($path, $mtime);
  97. if ($result) {
  98. $this->knowMtimes->set($path, $mtime ?? $this->clock->now()->getTimestamp());
  99. }
  100. return $result;
  101. }
  102. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  103. $result = parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  104. if ($result) {
  105. $this->knowMtimes->set($targetInternalPath, $this->clock->now()->getTimestamp());
  106. }
  107. return $result;
  108. }
  109. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  110. $result = parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  111. if ($result) {
  112. $this->knowMtimes->set($targetInternalPath, $this->clock->now()->getTimestamp());
  113. }
  114. return $result;
  115. }
  116. public function writeStream(string $path, $stream, int $size = null): int {
  117. $result = parent::writeStream($path, $stream, $size);
  118. if ($result) {
  119. $this->knowMtimes->set($path, $this->clock->now()->getTimestamp());
  120. }
  121. return $result;
  122. }
  123. }