Completed
Pull Request — master (#31651)
by Thomas
12:19 queued 40s
created

LockMapper::cleanup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @author Thomas Müller <[email protected]>
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 */
20
21
namespace OC\Lock\Persistent;
22
23
use OCP\AppFramework\Db\Entity;
24
use OCP\AppFramework\Db\Mapper;
25
use OCP\IDBConnection;
26
27
class LockMapper extends Mapper {
28
	public function __construct(IDBConnection $db) {
29
		parent::__construct($db, 'persistent_locks', null);
30
	}
31
32
	/**
33
	 * Selects all locks from the database for the given storage and path.
34
	 * Also parent folders are returned and in case $returnChildLocks is true all
35
	 * children locks as well.
36
	 *
37
	 * @param int $storageId
38
	 * @param string $internalPath
39
	 * @param bool $returnChildLocks
40
	 * @return Lock[]
41
	 */
42
	public function getLocksByPath(int $storageId, string $internalPath, bool $returnChildLocks) : array {
43
		$query = $this->db->getQueryBuilder();
44
		$pathPattern = $this->db->escapeLikeParameter($internalPath) . '%';
45
46
		$query->select(['id', 'owner', 'timeout', 'created_at', 'token', 'token', 'scope', 'depth', 'file_id', 'path', 'owner_account_id'])
47
			->from($this->getTableName(), 'l')
48
			->join('l', 'filecache', 'f', $query->expr()->eq('l.file_id', 'f.fileid'))
49
			->where($query->expr()->eq('storage', $query->createNamedParameter($storageId)))
50
			->andWhere($query->expr()->gt('created_at', $query->createFunction('(' . $query->createNamedParameter(\time()) . ' - `timeout`)')));
51
52
		if ($returnChildLocks) {
53
			$query->andWhere($query->expr()->like('f.path', $query->createNamedParameter($pathPattern)));
54
		} else {
55
			$query->andWhere($query->expr()->eq('f.path', $query->createNamedParameter($internalPath)));
56
		}
57
58
		// We need to check locks for every part in the uri.
59
		$uriParts = \explode('/', $internalPath);
60
61
		// We already covered the last part of the uri
62
		\array_pop($uriParts);
63
64
		$currentPath = '';
65
		foreach ($uriParts as $part) {
66
			if ($currentPath) {
67
				$currentPath .= '/';
68
			}
69
			$currentPath .= $part;
70
			$query->orWhere(
71
				$query->expr()->andX(
72
					// TODO: think about parent locks for depth 1
73
					$query->expr()->neq('depth', $query->createNamedParameter(0)),
74
					$query->expr()->eq('path', $query->createNamedParameter($currentPath))
0 ignored issues
show
Unused Code introduced by
The call to IExpressionBuilder::andX() has too many arguments starting with $query->expr()->eq('path...arameter($currentPath)).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
75
				)
76
			);
77
		}
78
79
		return $this->findEntities($query->getSQL(), $query->getParameters());
80
	}
81
82
	/**
83
	 * @param int $fileId
84
	 * @param string $token
85
	 * @return bool
86
	 */
87 View Code Duplication
	public function deleteByFileIdAndToken(int $fileId, string $token) : bool {
88
		$query = $this->db->getQueryBuilder();
89
90
		$rowCount = $query->delete($this->getTableName())
91
			->where($query->expr()->eq('file_id', $query->createNamedParameter($fileId)))
92
			->andWhere($query->expr()->eq('token_hash', $query->createNamedParameter(\md5($token))))
93
			->execute();
94
95
		return $rowCount === 1;
96
	}
97
98
	/**
99
	 * @param string $token
100
	 * @return Lock
101
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
102
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
103
	 */
104
	public function getLockByToken(string $token) : Lock {
105
		$query = $this->db->getQueryBuilder();
106
107
		$query->select(['id', 'owner', 'timeout', 'created_at', 'token', 'token_hash', 'scope', 'depth', 'file_id', 'owner_account_id'])
108
			->from($this->getTableName(), 'l')
109
			->where($query->expr()->eq('token_hash', $query->createNamedParameter(\md5($token))));
110
111
		return $this->findEntity($query->getSQL(), $query->getParameters());
112
	}
113
114 View Code Duplication
	public function insert(Entity $entity) {
115
		if (!$entity instanceof Lock) {
116
			throw new \InvalidArgumentException('Wrong entity type used');
117
		}
118
		if (\md5($entity->getToken()) !== $entity->getTokenHash()) {
119
			throw new \InvalidArgumentException('token_hash does not match the token of the lock');
120
		}
121
		return parent::insert($entity);
122
	}
123
124 View Code Duplication
	public function update(Entity $entity) {
125
		if (!$entity instanceof Lock) {
126
			throw new \InvalidArgumentException('Wrong entity type used');
127
		}
128
		if (\md5($entity->getToken()) !== $entity->getTokenHash()) {
129
			throw new \InvalidArgumentException('token_hash does not match the token of the lock');
130
		}
131
		return parent::update($entity);
132
	}
133
134
	public function cleanup() {
135
		$query = $this->db->getQueryBuilder();
136
		$query->delete($this->getTableName())
137
			->where($query->expr()->lt('created_at',
138
				$query->createFunction('(' . $query->createNamedParameter(\time()) . ' - `timeout`)')))
139
			->execute();
140
	}
141
}
142