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