Completed
Push — master ( 88ba65...9a0892 )
by Christoph
24:20
created
apps/contactsinteraction/composer/composer/autoload_real.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -9,7 +9,7 @@  discard block
 block discarded – undo
9 9
     public static function loadClassLoader($class)
10 10
     {
11 11
         if ('Composer\Autoload\ClassLoader' === $class) {
12
-            require __DIR__ . '/ClassLoader.php';
12
+            require __DIR__.'/ClassLoader.php';
13 13
         }
14 14
     }
15 15
 
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
         self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
27 27
         spl_autoload_unregister(array('ComposerAutoloaderInitContactsInteraction', 'loadClassLoader'));
28 28
 
29
-        require __DIR__ . '/autoload_static.php';
29
+        require __DIR__.'/autoload_static.php';
30 30
         call_user_func(\Composer\Autoload\ComposerStaticInitContactsInteraction::getInitializer($loader));
31 31
 
32 32
         $loader->setClassMapAuthoritative(true);
Please login to merge, or discard this patch.
apps/encryption/composer/composer/autoload_real.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -9,7 +9,7 @@  discard block
 block discarded – undo
9 9
     public static function loadClassLoader($class)
10 10
     {
11 11
         if ('Composer\Autoload\ClassLoader' === $class) {
12
-            require __DIR__ . '/ClassLoader.php';
12
+            require __DIR__.'/ClassLoader.php';
13 13
         }
14 14
     }
15 15
 
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
         self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
27 27
         spl_autoload_unregister(array('ComposerAutoloaderInitEncryption', 'loadClassLoader'));
28 28
 
29
-        require __DIR__ . '/autoload_static.php';
29
+        require __DIR__.'/autoload_static.php';
30 30
         call_user_func(\Composer\Autoload\ComposerStaticInitEncryption::getInitializer($loader));
31 31
 
32 32
         $loader->setClassMapAuthoritative(true);
Please login to merge, or discard this patch.
apps/files_trashbin/composer/composer/autoload_real.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -9,7 +9,7 @@  discard block
 block discarded – undo
9 9
     public static function loadClassLoader($class)
10 10
     {
11 11
         if ('Composer\Autoload\ClassLoader' === $class) {
12
-            require __DIR__ . '/ClassLoader.php';
12
+            require __DIR__.'/ClassLoader.php';
13 13
         }
14 14
     }
15 15
 
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
         self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
27 27
         spl_autoload_unregister(array('ComposerAutoloaderInitFiles_Trashbin', 'loadClassLoader'));
28 28
 
29
-        require __DIR__ . '/autoload_static.php';
29
+        require __DIR__.'/autoload_static.php';
30 30
         call_user_func(\Composer\Autoload\ComposerStaticInitFiles_Trashbin::getInitializer($loader));
31 31
 
32 32
         $loader->setClassMapAuthoritative(true);
Please login to merge, or discard this patch.
apps/theming/lib/Themes/DyslexiaFont.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -53,7 +53,7 @@
 block discarded – undo
53 53
 		$originalFontFace = $variables['--font-face'];
54 54
 
55 55
 		$variables = [
56
-			'--font-face' => 'OpenDyslexic, ' . $originalFontFace
56
+			'--font-face' => 'OpenDyslexic, '.$originalFontFace
57 57
 		];
58 58
 
59 59
 		return $variables;
Please login to merge, or discard this patch.
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -11,42 +11,42 @@  discard block
 block discarded – undo
11 11
 
12 12
 class DyslexiaFont extends DefaultTheme implements ITheme {
13 13
 
14
-	public function getId(): string {
15
-		return 'opendyslexic';
16
-	}
14
+    public function getId(): string {
15
+        return 'opendyslexic';
16
+    }
17 17
 
18
-	public function getType(): int {
19
-		return ITheme::TYPE_FONT;
20
-	}
18
+    public function getType(): int {
19
+        return ITheme::TYPE_FONT;
20
+    }
21 21
 
22
-	public function getTitle(): string {
23
-		return $this->l->t('Dyslexia font');
24
-	}
22
+    public function getTitle(): string {
23
+        return $this->l->t('Dyslexia font');
24
+    }
25 25
 
26
-	public function getEnableLabel(): string {
27
-		return $this->l->t('Enable dyslexia font');
28
-	}
26
+    public function getEnableLabel(): string {
27
+        return $this->l->t('Enable dyslexia font');
28
+    }
29 29
 
30
-	public function getDescription(): string {
31
-		return $this->l->t('OpenDyslexic is a free typeface/font designed to mitigate some of the common reading errors caused by dyslexia.');
32
-	}
30
+    public function getDescription(): string {
31
+        return $this->l->t('OpenDyslexic is a free typeface/font designed to mitigate some of the common reading errors caused by dyslexia.');
32
+    }
33 33
 
34
-	public function getCSSVariables(): array {
35
-		$variables = parent::getCSSVariables();
36
-		$originalFontFace = $variables['--font-face'];
34
+    public function getCSSVariables(): array {
35
+        $variables = parent::getCSSVariables();
36
+        $originalFontFace = $variables['--font-face'];
37 37
 
38
-		$variables = [
39
-			'--font-face' => 'OpenDyslexic, ' . $originalFontFace
40
-		];
38
+        $variables = [
39
+            '--font-face' => 'OpenDyslexic, ' . $originalFontFace
40
+        ];
41 41
 
42
-		return $variables;
43
-	}
42
+        return $variables;
43
+    }
44 44
 
45
-	public function getCustomCss(): string {
46
-		$fontPathOtf = $this->urlGenerator->linkTo('theming', 'fonts/OpenDyslexic-Regular.otf');
47
-		$boldFontPathOtf = $this->urlGenerator->linkTo('theming', 'fonts/OpenDyslexic-Bold.otf');
45
+    public function getCustomCss(): string {
46
+        $fontPathOtf = $this->urlGenerator->linkTo('theming', 'fonts/OpenDyslexic-Regular.otf');
47
+        $boldFontPathOtf = $this->urlGenerator->linkTo('theming', 'fonts/OpenDyslexic-Bold.otf');
48 48
 
49
-		return "
49
+        return "
50 50
 			@font-face {
51 51
 				font-family: 'OpenDyslexic';
52 52
 				font-style: normal;
@@ -61,5 +61,5 @@  discard block
 block discarded – undo
61 61
 				src: url('$boldFontPathOtf') format('opentype');
62 62
 			}
63 63
 		";
64
-	}
64
+    }
65 65
 }
Please login to merge, or discard this patch.
core/Migrations/Version24000Date20220425072957.php 1 patch
Indentation   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -32,22 +32,22 @@
 block discarded – undo
32 32
 use OCP\Migration\SimpleMigrationStep;
33 33
 
34 34
 class Version24000Date20220425072957 extends SimpleMigrationStep {
35
-	/**
36
-	 * @param IOutput $output
37
-	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
38
-	 * @param array $options
39
-	 * @return null|ISchemaWrapper
40
-	 */
41
-	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
42
-		/** @var ISchemaWrapper $schema */
43
-		$schema = $schemaClosure();
35
+    /**
36
+     * @param IOutput $output
37
+     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
38
+     * @param array $options
39
+     * @return null|ISchemaWrapper
40
+     */
41
+    public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
42
+        /** @var ISchemaWrapper $schema */
43
+        $schema = $schemaClosure();
44 44
 
45
-		$table = $schema->getTable('mounts');
46
-		if (!$table->hasIndex('mount_user_storage')) {
47
-			$table->addIndex(['storage_id', 'user_id'], 'mount_user_storage');
48
-			return $schema;
49
-		}
45
+        $table = $schema->getTable('mounts');
46
+        if (!$table->hasIndex('mount_user_storage')) {
47
+            $table->addIndex(['storage_id', 'user_id'], 'mount_user_storage');
48
+            return $schema;
49
+        }
50 50
 
51
-		return null;
52
-	}
51
+        return null;
52
+    }
53 53
 }
Please login to merge, or discard this patch.
lib/public/IEmojiHelper.php 1 patch
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -27,13 +27,13 @@
 block discarded – undo
27 27
  * @since 24.0.0
28 28
  */
29 29
 interface IEmojiHelper {
30
-	/**
31
-	 * @since 24.0.0
32
-	 */
33
-	public function doesPlatformSupportEmoji(): bool;
30
+    /**
31
+     * @since 24.0.0
32
+     */
33
+    public function doesPlatformSupportEmoji(): bool;
34 34
 
35
-	/**
36
-	 * @since 24.0.0
37
-	 */
38
-	public function isValidSingleEmoji(string $emoji): bool;
35
+    /**
36
+     * @since 24.0.0
37
+     */
38
+    public function isValidSingleEmoji(string $emoji): bool;
39 39
 }
Please login to merge, or discard this patch.
lib/public/Lock/ILockingProvider.php 1 patch
Indentation   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -36,47 +36,47 @@
 block discarded – undo
36 36
  * @since 8.1.0
37 37
  */
38 38
 interface ILockingProvider {
39
-	/**
40
-	 * @since 8.1.0
41
-	 */
42
-	public const LOCK_SHARED = 1;
43
-	/**
44
-	 * @since 8.1.0
45
-	 */
46
-	public const LOCK_EXCLUSIVE = 2;
39
+    /**
40
+     * @since 8.1.0
41
+     */
42
+    public const LOCK_SHARED = 1;
43
+    /**
44
+     * @since 8.1.0
45
+     */
46
+    public const LOCK_EXCLUSIVE = 2;
47 47
 
48
-	/**
49
-	 * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $type
50
-	 * @since 8.1.0
51
-	 */
52
-	public function isLocked(string $path, int $type): bool;
48
+    /**
49
+     * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $type
50
+     * @since 8.1.0
51
+     */
52
+    public function isLocked(string $path, int $type): bool;
53 53
 
54
-	/**
55
-	 * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $type
56
-	 * @param ?string $readablePath A human-readable path to use in error messages, since 20.0.0
57
-	 * @throws LockedException
58
-	 * @since 8.1.0
59
-	 */
60
-	public function acquireLock(string $path, int $type, ?string $readablePath = null): void;
54
+    /**
55
+     * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $type
56
+     * @param ?string $readablePath A human-readable path to use in error messages, since 20.0.0
57
+     * @throws LockedException
58
+     * @since 8.1.0
59
+     */
60
+    public function acquireLock(string $path, int $type, ?string $readablePath = null): void;
61 61
 
62
-	/**
63
-	 * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $type
64
-	 * @since 8.1.0
65
-	 */
66
-	public function releaseLock(string $path, int $type): void;
62
+    /**
63
+     * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $type
64
+     * @since 8.1.0
65
+     */
66
+    public function releaseLock(string $path, int $type): void;
67 67
 
68
-	/**
69
-	 * Change the target type of an existing lock
70
-	 *
71
-	 * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $targetType
72
-	 * @throws LockedException
73
-	 * @since 8.1.0
74
-	 */
75
-	public function changeLock(string $path, int $targetType): void;
68
+    /**
69
+     * Change the target type of an existing lock
70
+     *
71
+     * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $targetType
72
+     * @throws LockedException
73
+     * @since 8.1.0
74
+     */
75
+    public function changeLock(string $path, int $targetType): void;
76 76
 
77
-	/**
78
-	 * Release all lock acquired by this instance
79
-	 * @since 8.1.0
80
-	 */
81
-	public function releaseAll(): void;
77
+    /**
78
+     * Release all lock acquired by this instance
79
+     * @since 8.1.0
80
+     */
81
+    public function releaseAll(): void;
82 82
 }
Please login to merge, or discard this patch.
lib/private/Lock/DBLockingProvider.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -113,7 +113,7 @@  discard block
 block discarded – undo
113 113
 			->from('file_locks')
114 114
 			->where($query->expr()->eq('key', $query->createNamedParameter($path)));
115 115
 		$result = $query->executeQuery();
116
-		$lockValue = (int)$result->fetchOne();
116
+		$lockValue = (int) $result->fetchOne();
117 117
 		if ($type === self::LOCK_SHARED) {
118 118
 			if ($this->isLocallyLocked($path)) {
119 119
 				// if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db
@@ -247,7 +247,7 @@  discard block
 block discarded – undo
247 247
 		}
248 248
 		// since we keep shared locks we need to manually clean those
249 249
 		$lockedPaths = array_keys($this->sharedLocks);
250
-		$lockedPaths = array_filter($lockedPaths, function ($path) {
250
+		$lockedPaths = array_filter($lockedPaths, function($path) {
251 251
 			return $this->sharedLocks[$path];
252 252
 		});
253 253
 
Please login to merge, or discard this patch.
Indentation   +219 added lines, -219 removed lines patch added patch discarded remove patch
@@ -18,223 +18,223 @@
 block discarded – undo
18 18
  * Locking provider that stores the locks in the database
19 19
  */
20 20
 class DBLockingProvider extends AbstractLockingProvider {
21
-	private array $sharedLocks = [];
22
-
23
-	public function __construct(
24
-		private IDBConnection $connection,
25
-		private ITimeFactory $timeFactory,
26
-		int $ttl = 3600,
27
-		private bool $cacheSharedLocks = true,
28
-	) {
29
-		parent::__construct($ttl);
30
-	}
31
-
32
-	/**
33
-	 * Check if we have an open shared lock for a path
34
-	 */
35
-	protected function isLocallyLocked(string $path): bool {
36
-		return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path];
37
-	}
38
-
39
-	/** @inheritDoc */
40
-	protected function markAcquire(string $path, int $targetType): void {
41
-		parent::markAcquire($path, $targetType);
42
-		if ($this->cacheSharedLocks) {
43
-			if ($targetType === self::LOCK_SHARED) {
44
-				$this->sharedLocks[$path] = true;
45
-			}
46
-		}
47
-	}
48
-
49
-	/**
50
-	 * Change the type of an existing tracked lock
51
-	 */
52
-	protected function markChange(string $path, int $targetType): void {
53
-		parent::markChange($path, $targetType);
54
-		if ($this->cacheSharedLocks) {
55
-			if ($targetType === self::LOCK_SHARED) {
56
-				$this->sharedLocks[$path] = true;
57
-			} elseif ($targetType === self::LOCK_EXCLUSIVE) {
58
-				$this->sharedLocks[$path] = false;
59
-			}
60
-		}
61
-	}
62
-
63
-	/**
64
-	 * Insert a file locking row if it does not exists.
65
-	 */
66
-	protected function initLockField(string $path, int $lock = 0): int {
67
-		$expire = $this->getExpireTime();
68
-		return $this->connection->insertIgnoreConflict('file_locks', [
69
-			'key' => $path,
70
-			'lock' => $lock,
71
-			'ttl' => $expire
72
-		]);
73
-	}
74
-
75
-	protected function getExpireTime(): int {
76
-		return $this->timeFactory->getTime() + $this->ttl;
77
-	}
78
-
79
-	/** @inheritDoc */
80
-	public function isLocked(string $path, int $type): bool {
81
-		if ($this->hasAcquiredLock($path, $type)) {
82
-			return true;
83
-		}
84
-		$query = $this->connection->getQueryBuilder();
85
-		$query->select('lock')
86
-			->from('file_locks')
87
-			->where($query->expr()->eq('key', $query->createNamedParameter($path)));
88
-		$result = $query->executeQuery();
89
-		$lockValue = (int)$result->fetchOne();
90
-		if ($type === self::LOCK_SHARED) {
91
-			if ($this->isLocallyLocked($path)) {
92
-				// if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db
93
-				return $lockValue > 1;
94
-			} else {
95
-				return $lockValue > 0;
96
-			}
97
-		} elseif ($type === self::LOCK_EXCLUSIVE) {
98
-			return $lockValue === -1;
99
-		} else {
100
-			return false;
101
-		}
102
-	}
103
-
104
-	/** @inheritDoc */
105
-	public function acquireLock(string $path, int $type, ?string $readablePath = null): void {
106
-		$expire = $this->getExpireTime();
107
-		if ($type === self::LOCK_SHARED) {
108
-			if (!$this->isLocallyLocked($path)) {
109
-				$result = $this->initLockField($path, 1);
110
-				if ($result <= 0) {
111
-					$query = $this->connection->getQueryBuilder();
112
-					$query->update('file_locks')
113
-						->set('lock', $query->func()->add('lock', $query->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
114
-						->set('ttl', $query->createNamedParameter($expire))
115
-						->where($query->expr()->eq('key', $query->createNamedParameter($path)))
116
-						->andWhere($query->expr()->gte('lock', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
117
-					$result = $query->executeStatement();
118
-				}
119
-			} else {
120
-				$result = 1;
121
-			}
122
-		} else {
123
-			$existing = 0;
124
-			if ($this->hasAcquiredLock($path, ILockingProvider::LOCK_SHARED) === false && $this->isLocallyLocked($path)) {
125
-				$existing = 1;
126
-			}
127
-			$result = $this->initLockField($path, -1);
128
-			if ($result <= 0) {
129
-				$query = $this->connection->getQueryBuilder();
130
-				$query->update('file_locks')
131
-					->set('lock', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT))
132
-					->set('ttl', $query->createNamedParameter($expire, IQueryBuilder::PARAM_INT))
133
-					->where($query->expr()->eq('key', $query->createNamedParameter($path)))
134
-					->andWhere($query->expr()->eq('lock', $query->createNamedParameter($existing)));
135
-				$result = $query->executeStatement();
136
-			}
137
-		}
138
-		if ($result !== 1) {
139
-			throw new LockedException($path, null, null, $readablePath);
140
-		}
141
-		$this->markAcquire($path, $type);
142
-	}
143
-
144
-	/** @inheritDoc */
145
-	public function releaseLock(string $path, int $type): void {
146
-		$this->markRelease($path, $type);
147
-
148
-		// we keep shared locks till the end of the request so we can re-use them
149
-		if ($type === self::LOCK_EXCLUSIVE) {
150
-			$qb = $this->connection->getQueryBuilder();
151
-			$qb->update('file_locks')
152
-				->set('lock', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))
153
-				->where($qb->expr()->eq('key', $qb->createNamedParameter($path)))
154
-				->andWhere($qb->expr()->eq('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT)));
155
-			$qb->executeStatement();
156
-		} elseif (!$this->cacheSharedLocks) {
157
-			$qb = $this->connection->getQueryBuilder();
158
-			$qb->update('file_locks')
159
-				->set('lock', $qb->func()->subtract('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
160
-				->where($qb->expr()->eq('key', $qb->createNamedParameter($path)))
161
-				->andWhere($qb->expr()->gt('lock', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
162
-			$qb->executeStatement();
163
-		}
164
-	}
165
-
166
-	/** @inheritDoc */
167
-	public function changeLock(string $path, int $targetType): void {
168
-		$expire = $this->getExpireTime();
169
-		if ($targetType === self::LOCK_SHARED) {
170
-			$qb = $this->connection->getQueryBuilder();
171
-			$result = $qb->update('file_locks')
172
-				->set('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT))
173
-				->set('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT))
174
-				->where($qb->expr()->andX(
175
-					$qb->expr()->eq('key', $qb->createNamedParameter($path)),
176
-					$qb->expr()->eq('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT))
177
-				))->executeStatement();
178
-		} else {
179
-			// since we only keep one shared lock in the db we need to check if we have more then one shared lock locally manually
180
-			if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 1) {
181
-				throw new LockedException($path);
182
-			}
183
-			$qb = $this->connection->getQueryBuilder();
184
-			$result = $qb->update('file_locks')
185
-				->set('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT))
186
-				->set('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT))
187
-				->where($qb->expr()->andX(
188
-					$qb->expr()->eq('key', $qb->createNamedParameter($path)),
189
-					$qb->expr()->eq('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT))
190
-				))->executeStatement();
191
-		}
192
-		if ($result !== 1) {
193
-			throw new LockedException($path);
194
-		}
195
-		$this->markChange($path, $targetType);
196
-	}
197
-
198
-	/** @inheritDoc */
199
-	public function cleanExpiredLocks(): void {
200
-		$expire = $this->timeFactory->getTime();
201
-		try {
202
-			$qb = $this->connection->getQueryBuilder();
203
-			$qb->delete('file_locks')
204
-				->where($qb->expr()->lt('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT)))
205
-				->executeStatement();
206
-		} catch (\Exception $e) {
207
-			// If the table is missing, the clean up was successful
208
-			if ($this->connection->tableExists('file_locks')) {
209
-				throw $e;
210
-			}
211
-		}
212
-	}
213
-
214
-	/** @inheritDoc */
215
-	public function releaseAll(): void {
216
-		parent::releaseAll();
217
-
218
-		if (!$this->cacheSharedLocks) {
219
-			return;
220
-		}
221
-		// since we keep shared locks we need to manually clean those
222
-		$lockedPaths = array_keys($this->sharedLocks);
223
-		$lockedPaths = array_filter($lockedPaths, function ($path) {
224
-			return $this->sharedLocks[$path];
225
-		});
226
-
227
-		$chunkedPaths = array_chunk($lockedPaths, 100);
228
-
229
-		$qb = $this->connection->getQueryBuilder();
230
-		$qb->update('file_locks')
231
-			->set('lock', $qb->func()->subtract('lock', $qb->expr()->literal(1)))
232
-			->where($qb->expr()->in('key', $qb->createParameter('chunk')))
233
-			->andWhere($qb->expr()->gt('lock', new Literal(0)));
234
-
235
-		foreach ($chunkedPaths as $chunk) {
236
-			$qb->setParameter('chunk', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
237
-			$qb->executeStatement();
238
-		}
239
-	}
21
+    private array $sharedLocks = [];
22
+
23
+    public function __construct(
24
+        private IDBConnection $connection,
25
+        private ITimeFactory $timeFactory,
26
+        int $ttl = 3600,
27
+        private bool $cacheSharedLocks = true,
28
+    ) {
29
+        parent::__construct($ttl);
30
+    }
31
+
32
+    /**
33
+     * Check if we have an open shared lock for a path
34
+     */
35
+    protected function isLocallyLocked(string $path): bool {
36
+        return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path];
37
+    }
38
+
39
+    /** @inheritDoc */
40
+    protected function markAcquire(string $path, int $targetType): void {
41
+        parent::markAcquire($path, $targetType);
42
+        if ($this->cacheSharedLocks) {
43
+            if ($targetType === self::LOCK_SHARED) {
44
+                $this->sharedLocks[$path] = true;
45
+            }
46
+        }
47
+    }
48
+
49
+    /**
50
+     * Change the type of an existing tracked lock
51
+     */
52
+    protected function markChange(string $path, int $targetType): void {
53
+        parent::markChange($path, $targetType);
54
+        if ($this->cacheSharedLocks) {
55
+            if ($targetType === self::LOCK_SHARED) {
56
+                $this->sharedLocks[$path] = true;
57
+            } elseif ($targetType === self::LOCK_EXCLUSIVE) {
58
+                $this->sharedLocks[$path] = false;
59
+            }
60
+        }
61
+    }
62
+
63
+    /**
64
+     * Insert a file locking row if it does not exists.
65
+     */
66
+    protected function initLockField(string $path, int $lock = 0): int {
67
+        $expire = $this->getExpireTime();
68
+        return $this->connection->insertIgnoreConflict('file_locks', [
69
+            'key' => $path,
70
+            'lock' => $lock,
71
+            'ttl' => $expire
72
+        ]);
73
+    }
74
+
75
+    protected function getExpireTime(): int {
76
+        return $this->timeFactory->getTime() + $this->ttl;
77
+    }
78
+
79
+    /** @inheritDoc */
80
+    public function isLocked(string $path, int $type): bool {
81
+        if ($this->hasAcquiredLock($path, $type)) {
82
+            return true;
83
+        }
84
+        $query = $this->connection->getQueryBuilder();
85
+        $query->select('lock')
86
+            ->from('file_locks')
87
+            ->where($query->expr()->eq('key', $query->createNamedParameter($path)));
88
+        $result = $query->executeQuery();
89
+        $lockValue = (int)$result->fetchOne();
90
+        if ($type === self::LOCK_SHARED) {
91
+            if ($this->isLocallyLocked($path)) {
92
+                // if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db
93
+                return $lockValue > 1;
94
+            } else {
95
+                return $lockValue > 0;
96
+            }
97
+        } elseif ($type === self::LOCK_EXCLUSIVE) {
98
+            return $lockValue === -1;
99
+        } else {
100
+            return false;
101
+        }
102
+    }
103
+
104
+    /** @inheritDoc */
105
+    public function acquireLock(string $path, int $type, ?string $readablePath = null): void {
106
+        $expire = $this->getExpireTime();
107
+        if ($type === self::LOCK_SHARED) {
108
+            if (!$this->isLocallyLocked($path)) {
109
+                $result = $this->initLockField($path, 1);
110
+                if ($result <= 0) {
111
+                    $query = $this->connection->getQueryBuilder();
112
+                    $query->update('file_locks')
113
+                        ->set('lock', $query->func()->add('lock', $query->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
114
+                        ->set('ttl', $query->createNamedParameter($expire))
115
+                        ->where($query->expr()->eq('key', $query->createNamedParameter($path)))
116
+                        ->andWhere($query->expr()->gte('lock', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
117
+                    $result = $query->executeStatement();
118
+                }
119
+            } else {
120
+                $result = 1;
121
+            }
122
+        } else {
123
+            $existing = 0;
124
+            if ($this->hasAcquiredLock($path, ILockingProvider::LOCK_SHARED) === false && $this->isLocallyLocked($path)) {
125
+                $existing = 1;
126
+            }
127
+            $result = $this->initLockField($path, -1);
128
+            if ($result <= 0) {
129
+                $query = $this->connection->getQueryBuilder();
130
+                $query->update('file_locks')
131
+                    ->set('lock', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT))
132
+                    ->set('ttl', $query->createNamedParameter($expire, IQueryBuilder::PARAM_INT))
133
+                    ->where($query->expr()->eq('key', $query->createNamedParameter($path)))
134
+                    ->andWhere($query->expr()->eq('lock', $query->createNamedParameter($existing)));
135
+                $result = $query->executeStatement();
136
+            }
137
+        }
138
+        if ($result !== 1) {
139
+            throw new LockedException($path, null, null, $readablePath);
140
+        }
141
+        $this->markAcquire($path, $type);
142
+    }
143
+
144
+    /** @inheritDoc */
145
+    public function releaseLock(string $path, int $type): void {
146
+        $this->markRelease($path, $type);
147
+
148
+        // we keep shared locks till the end of the request so we can re-use them
149
+        if ($type === self::LOCK_EXCLUSIVE) {
150
+            $qb = $this->connection->getQueryBuilder();
151
+            $qb->update('file_locks')
152
+                ->set('lock', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))
153
+                ->where($qb->expr()->eq('key', $qb->createNamedParameter($path)))
154
+                ->andWhere($qb->expr()->eq('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT)));
155
+            $qb->executeStatement();
156
+        } elseif (!$this->cacheSharedLocks) {
157
+            $qb = $this->connection->getQueryBuilder();
158
+            $qb->update('file_locks')
159
+                ->set('lock', $qb->func()->subtract('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
160
+                ->where($qb->expr()->eq('key', $qb->createNamedParameter($path)))
161
+                ->andWhere($qb->expr()->gt('lock', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
162
+            $qb->executeStatement();
163
+        }
164
+    }
165
+
166
+    /** @inheritDoc */
167
+    public function changeLock(string $path, int $targetType): void {
168
+        $expire = $this->getExpireTime();
169
+        if ($targetType === self::LOCK_SHARED) {
170
+            $qb = $this->connection->getQueryBuilder();
171
+            $result = $qb->update('file_locks')
172
+                ->set('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT))
173
+                ->set('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT))
174
+                ->where($qb->expr()->andX(
175
+                    $qb->expr()->eq('key', $qb->createNamedParameter($path)),
176
+                    $qb->expr()->eq('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT))
177
+                ))->executeStatement();
178
+        } else {
179
+            // since we only keep one shared lock in the db we need to check if we have more then one shared lock locally manually
180
+            if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 1) {
181
+                throw new LockedException($path);
182
+            }
183
+            $qb = $this->connection->getQueryBuilder();
184
+            $result = $qb->update('file_locks')
185
+                ->set('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT))
186
+                ->set('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT))
187
+                ->where($qb->expr()->andX(
188
+                    $qb->expr()->eq('key', $qb->createNamedParameter($path)),
189
+                    $qb->expr()->eq('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT))
190
+                ))->executeStatement();
191
+        }
192
+        if ($result !== 1) {
193
+            throw new LockedException($path);
194
+        }
195
+        $this->markChange($path, $targetType);
196
+    }
197
+
198
+    /** @inheritDoc */
199
+    public function cleanExpiredLocks(): void {
200
+        $expire = $this->timeFactory->getTime();
201
+        try {
202
+            $qb = $this->connection->getQueryBuilder();
203
+            $qb->delete('file_locks')
204
+                ->where($qb->expr()->lt('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT)))
205
+                ->executeStatement();
206
+        } catch (\Exception $e) {
207
+            // If the table is missing, the clean up was successful
208
+            if ($this->connection->tableExists('file_locks')) {
209
+                throw $e;
210
+            }
211
+        }
212
+    }
213
+
214
+    /** @inheritDoc */
215
+    public function releaseAll(): void {
216
+        parent::releaseAll();
217
+
218
+        if (!$this->cacheSharedLocks) {
219
+            return;
220
+        }
221
+        // since we keep shared locks we need to manually clean those
222
+        $lockedPaths = array_keys($this->sharedLocks);
223
+        $lockedPaths = array_filter($lockedPaths, function ($path) {
224
+            return $this->sharedLocks[$path];
225
+        });
226
+
227
+        $chunkedPaths = array_chunk($lockedPaths, 100);
228
+
229
+        $qb = $this->connection->getQueryBuilder();
230
+        $qb->update('file_locks')
231
+            ->set('lock', $qb->func()->subtract('lock', $qb->expr()->literal(1)))
232
+            ->where($qb->expr()->in('key', $qb->createParameter('chunk')))
233
+            ->andWhere($qb->expr()->gt('lock', new Literal(0)));
234
+
235
+        foreach ($chunkedPaths as $chunk) {
236
+            $qb->setParameter('chunk', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
237
+            $qb->executeStatement();
238
+        }
239
+    }
240 240
 }
Please login to merge, or discard this patch.
lib/private/Diagnostics/QueryLogger.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -69,7 +69,7 @@
 block discarded – undo
69 69
 	public function stopQuery() {
70 70
 		if ($this->activated && $this->activeQuery) {
71 71
 			$this->activeQuery->end(microtime(true));
72
-			$this->queries[(string)$this->index] = $this->activeQuery;
72
+			$this->queries[(string) $this->index] = $this->activeQuery;
73 73
 			$this->index++;
74 74
 			$this->activeQuery = null;
75 75
 		}
Please login to merge, or discard this patch.
Indentation   +52 added lines, -52 removed lines patch added patch discarded remove patch
@@ -11,64 +11,64 @@
 block discarded – undo
11 11
 use OCP\Diagnostics\IQueryLogger;
12 12
 
13 13
 class QueryLogger implements IQueryLogger {
14
-	protected int $index = 0;
15
-	protected ?Query $activeQuery = null;
16
-	/** @var CappedMemoryCache<Query> */
17
-	protected CappedMemoryCache $queries;
14
+    protected int $index = 0;
15
+    protected ?Query $activeQuery = null;
16
+    /** @var CappedMemoryCache<Query> */
17
+    protected CappedMemoryCache $queries;
18 18
 
19
-	/**
20
-	 * QueryLogger constructor.
21
-	 */
22
-	public function __construct() {
23
-		$this->queries = new CappedMemoryCache(1024);
24
-	}
19
+    /**
20
+     * QueryLogger constructor.
21
+     */
22
+    public function __construct() {
23
+        $this->queries = new CappedMemoryCache(1024);
24
+    }
25 25
 
26 26
 
27
-	/**
28
-	 * @var bool - Module needs to be activated by some app
29
-	 */
30
-	private $activated = false;
27
+    /**
28
+     * @var bool - Module needs to be activated by some app
29
+     */
30
+    private $activated = false;
31 31
 
32
-	/**
33
-	 * @inheritdoc
34
-	 */
35
-	public function startQuery($sql, ?array $params = null, ?array $types = null) {
36
-		if ($this->activated) {
37
-			$this->activeQuery = new Query($sql, $params, microtime(true), $this->getStack());
38
-		}
39
-	}
32
+    /**
33
+     * @inheritdoc
34
+     */
35
+    public function startQuery($sql, ?array $params = null, ?array $types = null) {
36
+        if ($this->activated) {
37
+            $this->activeQuery = new Query($sql, $params, microtime(true), $this->getStack());
38
+        }
39
+    }
40 40
 
41
-	private function getStack() {
42
-		$stack = debug_backtrace();
43
-		array_shift($stack);
44
-		array_shift($stack);
45
-		array_shift($stack);
46
-		return $stack;
47
-	}
41
+    private function getStack() {
42
+        $stack = debug_backtrace();
43
+        array_shift($stack);
44
+        array_shift($stack);
45
+        array_shift($stack);
46
+        return $stack;
47
+    }
48 48
 
49
-	/**
50
-	 * @inheritdoc
51
-	 */
52
-	public function stopQuery() {
53
-		if ($this->activated && $this->activeQuery) {
54
-			$this->activeQuery->end(microtime(true));
55
-			$this->queries[(string)$this->index] = $this->activeQuery;
56
-			$this->index++;
57
-			$this->activeQuery = null;
58
-		}
59
-	}
49
+    /**
50
+     * @inheritdoc
51
+     */
52
+    public function stopQuery() {
53
+        if ($this->activated && $this->activeQuery) {
54
+            $this->activeQuery->end(microtime(true));
55
+            $this->queries[(string)$this->index] = $this->activeQuery;
56
+            $this->index++;
57
+            $this->activeQuery = null;
58
+        }
59
+    }
60 60
 
61
-	/**
62
-	 * @inheritdoc
63
-	 */
64
-	public function getQueries() {
65
-		return $this->queries->getData();
66
-	}
61
+    /**
62
+     * @inheritdoc
63
+     */
64
+    public function getQueries() {
65
+        return $this->queries->getData();
66
+    }
67 67
 
68
-	/**
69
-	 * @inheritdoc
70
-	 */
71
-	public function activate() {
72
-		$this->activated = true;
73
-	}
68
+    /**
69
+     * @inheritdoc
70
+     */
71
+    public function activate() {
72
+        $this->activated = true;
73
+    }
74 74
 }
Please login to merge, or discard this patch.