Passed
Push — master ( 568762...700449 )
by Morris
27:38 queued 15:10
created

Cache::search()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christopher Schäpers <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Jörn Friedrich Dreyer <[email protected]>
9
 * @author Michael Gapczynski <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Robin Appelman <[email protected]>
12
 * @author Roeland Jago Douma <[email protected]>
13
 *
14
 * @license AGPL-3.0
15
 *
16
 * This code is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License, version 3,
18
 * as published by the Free Software Foundation.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>
27
 *
28
 */
29
30
namespace OCA\Files_Sharing;
31
32
use OC\Files\Cache\FailedCache;
33
use OC\Files\Cache\Wrapper\CacheJail;
34
use OC\Files\Storage\Wrapper\Jail;
35
use OCP\Files\Cache\ICacheEntry;
36
use OCP\Files\StorageNotAvailableException;
37
38
/**
39
 * Metadata cache for shared files
40
 *
41
 * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
42
 */
43
class Cache extends CacheJail {
44
	/**
45
	 * @var \OCA\Files_Sharing\SharedStorage
46
	 */
47
	private $storage;
48
49
	/**
50
	 * @var ICacheEntry
51
	 */
52
	private $sourceRootInfo;
53
54
	private $rootUnchanged = true;
55
56
	private $ownerDisplayName;
57
58
	private $numericId;
59
60
	/**
61
	 * @param \OCA\Files_Sharing\SharedStorage $storage
62
	 * @param ICacheEntry $sourceRootInfo
63
	 */
64
	public function __construct($storage, ICacheEntry $sourceRootInfo) {
65
		$this->storage = $storage;
66
		$this->sourceRootInfo = $sourceRootInfo;
67
		$this->numericId = $sourceRootInfo->getStorageId();
68
69
		parent::__construct(
70
			null,
71
			''
72
		);
73
	}
74
75
	protected function getRoot() {
76
		if ($this->root === '') {
77
			$absoluteRoot = $this->sourceRootInfo->getPath();
78
79
			// the sourceRootInfo path is the absolute path of the folder in the "real" storage
80
			// in the case where a folder is shared from a Jail we need to ensure that the share Jail
81
			// has it's root set relative to the source Jail
82
			$currentStorage = $this->storage->getSourceStorage();
83
			if ($currentStorage->instanceOfStorage(Jail::class)) {
84
				/** @var Jail $currentStorage */
85
				$absoluteRoot = $currentStorage->getJailedPath($absoluteRoot);
86
			}
87
			$this->root = $absoluteRoot;
88
		}
89
		return $this->root;
90
	}
91
92
	public function getCache() {
93
		if (is_null($this->cache)) {
94
			$sourceStorage = $this->storage->getSourceStorage();
95
			if ($sourceStorage) {
0 ignored issues
show
introduced by
$sourceStorage is of type OCP\Files\Storage\IStorage, thus it always evaluated to true.
Loading history...
96
				$this->cache = $sourceStorage->getCache();
97
			} else {
98
				// don't set $this->cache here since sourceStorage will be set later
99
				return new FailedCache();
100
			}
101
		}
102
		return $this->cache;
103
	}
104
105
	public function getNumericStorageId() {
106
		if (isset($this->numericId)) {
107
			return $this->numericId;
108
		} else {
109
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by OCP\Files\Cache\ICache::getNumericStorageId() of integer.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
110
		}
111
	}
112
113
	public function get($file) {
114
		if ($this->rootUnchanged && ($file === '' || $file === $this->sourceRootInfo->getId())) {
115
			return $this->formatCacheEntry(clone $this->sourceRootInfo, '');
116
		}
117
		return parent::get($file);
118
	}
119
120
	public function update($id, array $data) {
121
		$this->rootUnchanged = false;
122
		parent::update($id, $data);
123
	}
124
125
	public function insert($file, array $data) {
126
		$this->rootUnchanged = false;
127
		return parent::insert($file, $data);
128
	}
129
130
	public function remove($file) {
131
		$this->rootUnchanged = false;
132
		parent::remove($file);
133
	}
134
135
	public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
136
		$this->rootUnchanged = false;
137
		return parent::moveFromCache($sourceCache, $sourcePath, $targetPath);
138
	}
139
140
	protected function formatCacheEntry($entry, $path = null) {
141
		if (is_null($path)) {
142
			$path = $entry['path'] ?? '';
143
			$entry['path'] = $this->getJailedPath($path);
144
		} else {
145
			$entry['path'] = $path;
146
		}
147
148
		try {
149
			if (isset($entry['permissions'])) {
150
				$entry['permissions'] &= $this->storage->getShare()->getPermissions();
151
			} else {
152
				$entry['permissions'] = $this->storage->getPermissions($entry['path']);
153
			}
154
		} catch (StorageNotAvailableException $e) {
155
			// thrown by FailedStorage e.g. when the sharer does not exist anymore
156
			// (IDE may say the exception is never thrown – false negative)
157
			$sharePermissions = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $sharePermissions is dead and can be removed.
Loading history...
158
		}
159
		$entry['uid_owner'] = $this->storage->getOwner('');
160
		$entry['displayname_owner'] = $this->getOwnerDisplayName();
161
		if ($path === '') {
162
			$entry['is_share_mount_point'] = true;
163
		}
164
		return $entry;
165
	}
166
167
	private function getOwnerDisplayName() {
168
		if (!$this->ownerDisplayName) {
169
			$this->ownerDisplayName = \OC_User::getDisplayName($this->storage->getOwner(''));
170
		}
171
		return $this->ownerDisplayName;
172
	}
173
174
	/**
175
	 * remove all entries for files that are stored on the storage from the cache
176
	 */
177
	public function clear() {
178
		// Not a valid action for Shared Cache
179
	}
180
181
	public function search($pattern) {
182
		// Do the normal search on the whole storage for non files
183
		if ($this->storage->getItemType() !== 'file') {
184
			return parent::search($pattern);
185
		}
186
187
		$regex = '/' . str_replace('%', '.*', $pattern) . '/i';
188
189
		$data = $this->get('');
190
		if (preg_match($regex, $data->getName()) === 1) {
191
			return [$data];
0 ignored issues
show
introduced by
The expression return array($data) returns an array which contains values of type false which are incompatible with the return type OCP\Files\Cache\ICacheEntry mandated by OCP\Files\Cache\ICache::search().
Loading history...
192
		}
193
194
		return [];
195
	}
196
}
197