Passed
Push — master ( ad6eb8...bba3a1 )
by Julius
14:09 queued 18s
created

ObjectTree::resolveChunkFile()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 5
nop 1
dl 0
loc 18
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bjoern Schiessle <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 * @author Vincent Petry <[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
namespace OCA\DAV\Connector\Sabre;
30
31
use OC\Files\FileInfo;
32
use OC\Files\Storage\FailedStorage;
33
use OCA\DAV\Connector\Sabre\Exception\FileLocked;
34
use OCA\DAV\Connector\Sabre\Exception\Forbidden;
35
use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
36
use OCP\Files\ForbiddenException;
37
use OCP\Files\StorageInvalidException;
38
use OCP\Files\StorageNotAvailableException;
39
use OCP\Lock\LockedException;
40
41
class ObjectTree extends CachingTree {
42
43
	/**
44
	 * @var \OC\Files\View
45
	 */
46
	protected $fileView;
47
48
	/**
49
	 * @var  \OCP\Files\Mount\IMountManager
50
	 */
51
	protected $mountManager;
52
53
	/**
54
	 * Creates the object
55
	 */
56
	public function __construct() {
57
	}
58
59
	/**
60
	 * @param \Sabre\DAV\INode $rootNode
61
	 * @param \OC\Files\View $view
62
	 * @param  \OCP\Files\Mount\IMountManager $mountManager
63
	 */
64
	public function init(\Sabre\DAV\INode $rootNode, \OC\Files\View $view, \OCP\Files\Mount\IMountManager $mountManager) {
65
		$this->rootNode = $rootNode;
0 ignored issues
show
Documentation Bug introduced by
$rootNode is of type Sabre\DAV\INode, but the property $rootNode was declared to be of type Sabre\DAV\ICollection. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
66
		$this->fileView = $view;
67
		$this->mountManager = $mountManager;
68
	}
69
70
	/**
71
	 * Returns the INode object for the requested path
72
	 *
73
	 * @param string $path
74
	 * @return \Sabre\DAV\INode
75
	 * @throws InvalidPath
76
	 * @throws \Sabre\DAV\Exception\Locked
77
	 * @throws \Sabre\DAV\Exception\NotFound
78
	 * @throws \Sabre\DAV\Exception\ServiceUnavailable
79
	 */
80
	public function getNodeForPath($path) {
81
		if (!$this->fileView) {
82
			throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
83
		}
84
85
		$path = trim($path, '/');
86
87
		if (isset($this->cache[$path])) {
88
			return $this->cache[$path];
89
		}
90
91
		if ($path) {
92
			try {
93
				$this->fileView->verifyPath($path, basename($path));
94
			} catch (\OCP\Files\InvalidPathException $ex) {
95
				throw new InvalidPath($ex->getMessage());
96
			}
97
		}
98
99
		// Is it the root node?
100
		if (!strlen($path)) {
101
			return $this->rootNode;
102
		}
103
104
		if (pathinfo($path, PATHINFO_EXTENSION) === 'part') {
105
			// read from storage
106
			$absPath = $this->fileView->getAbsolutePath($path);
107
			$mount = $this->fileView->getMount($path);
108
			$storage = $mount->getStorage();
109
			$internalPath = $mount->getInternalPath($absPath);
110
			if ($storage && $storage->file_exists($internalPath)) {
111
				/**
112
				 * @var \OC\Files\Storage\Storage $storage
113
				 */
114
				// get data directly
115
				$data = $storage->getMetaData($internalPath);
116
				$info = new FileInfo($absPath, $storage, $internalPath, $data, $mount);
117
			} else {
118
				$info = null;
119
			}
120
		} else {
121
			// read from cache
122
			try {
123
				$info = $this->fileView->getFileInfo($path);
124
125
				if ($info instanceof \OCP\Files\FileInfo && $info->getStorage()->instanceOfStorage(FailedStorage::class)) {
126
					throw new StorageNotAvailableException();
127
				}
128
			} catch (StorageNotAvailableException $e) {
129
				throw new \Sabre\DAV\Exception\ServiceUnavailable('Storage is temporarily not available', 0, $e);
130
			}
131
		}
132
133
		if (!$info) {
134
			throw new \Sabre\DAV\Exception\NotFound('File with name ' . $path . ' could not be located');
135
		}
136
137
		if ($info->getType() === 'dir') {
138
			$node = new \OCA\DAV\Connector\Sabre\Directory($this->fileView, $info, $this);
139
		} else {
140
			$node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info);
141
		}
142
143
		$this->cache[$path] = $node;
144
		return $node;
145
	}
146
147
	/**
148
	 * Copies a file or directory.
149
	 *
150
	 * This method must work recursively and delete the destination
151
	 * if it exists
152
	 *
153
	 * @param string $sourcePath
154
	 * @param string $destinationPath
155
	 * @throws FileLocked
156
	 * @throws Forbidden
157
	 * @throws InvalidPath
158
	 * @throws \Exception
159
	 * @throws \Sabre\DAV\Exception\Forbidden
160
	 * @throws \Sabre\DAV\Exception\Locked
161
	 * @throws \Sabre\DAV\Exception\NotFound
162
	 * @throws \Sabre\DAV\Exception\ServiceUnavailable
163
	 * @return void
164
	 */
165
	public function copy($sourcePath, $destinationPath) {
166
		if (!$this->fileView) {
167
			throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
168
		}
169
170
171
		$info = $this->fileView->getFileInfo(dirname($destinationPath));
172
		if ($this->fileView->file_exists($destinationPath)) {
173
			$destinationPermission = $info && $info->isUpdateable();
174
		} else {
175
			$destinationPermission = $info && $info->isCreatable();
176
		}
177
		if (!$destinationPermission) {
178
			throw new Forbidden('No permissions to copy object.');
179
		}
180
181
		// this will trigger existence check
182
		$this->getNodeForPath($sourcePath);
183
184
		[$destinationDir, $destinationName] = \Sabre\Uri\split($destinationPath);
185
		try {
186
			$this->fileView->verifyPath($destinationDir, $destinationName);
187
		} catch (\OCP\Files\InvalidPathException $ex) {
188
			throw new InvalidPath($ex->getMessage());
189
		}
190
191
		try {
192
			$this->fileView->copy($sourcePath, $destinationPath);
193
		} catch (StorageNotAvailableException $e) {
194
			throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
195
		} catch (ForbiddenException $ex) {
196
			throw new Forbidden($ex->getMessage(), $ex->getRetry());
197
		} catch (LockedException $e) {
198
			throw new FileLocked($e->getMessage(), $e->getCode(), $e);
199
		}
200
201
		[$destinationDir,] = \Sabre\Uri\split($destinationPath);
202
		$this->markDirty($destinationDir);
203
	}
204
}
205