Blame view

sources/lib/private/connector/sabre/objecttree.php 6.02 KB
31b7f2792   Kload   Upgrade to ownclo...
1
2
3
4
5
6
7
8
9
  <?php
  /**
   * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
   * This file is licensed under the Affero General Public License version 3 or
   * later.
   * See the COPYING-README file.
   */
  
  namespace OC\Connector\Sabre;
6d9380f96   Cédric Dupont   Update sources OC...
10
  use OC\Files\FileInfo;
31b7f2792   Kload   Upgrade to ownclo...
11
  use OC\Files\Filesystem;
6d9380f96   Cédric Dupont   Update sources OC...
12
13
14
  use OC\Files\Mount\MoveableMount;
  use OCP\Files\StorageInvalidException;
  use OCP\Files\StorageNotAvailableException;
31b7f2792   Kload   Upgrade to ownclo...
15

6d9380f96   Cédric Dupont   Update sources OC...
16
  class ObjectTree extends \Sabre\DAV\ObjectTree {
31b7f2792   Kload   Upgrade to ownclo...
17
18
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
19
20
  	 * @var \OC\Files\View
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  	protected $fileView;
  
  	/**
  	 * @var \OC\Files\Mount\Manager
  	 */
  	protected $mountManager;
  
  	/**
  	 * Creates the object
  	 *
  	 * This method expects the rootObject to be passed as a parameter
  	 */
  	public function __construct() {
  	}
  
  	/**
  	 * @param \Sabre\DAV\ICollection $rootNode
  	 * @param \OC\Files\View $view
  	 * @param \OC\Files\Mount\Manager $mountManager
  	 */
  	public function init(\Sabre\DAV\ICollection $rootNode, \OC\Files\View $view, \OC\Files\Mount\Manager $mountManager) {
  		$this->rootNode = $rootNode;
  		$this->fileView = $view;
  		$this->mountManager = $mountManager;
  	}
31b7f2792   Kload   Upgrade to ownclo...
46
47
48
49
50
  
  	/**
  	 * Returns the INode object for the requested path
  	 *
  	 * @param string $path
6d9380f96   Cédric Dupont   Update sources OC...
51
52
53
  	 * @throws \Sabre\DAV\Exception\ServiceUnavailable
  	 * @throws \Sabre\DAV\Exception\NotFound
  	 * @return \Sabre\DAV\INode
31b7f2792   Kload   Upgrade to ownclo...
54
55
  	 */
  	public function getNodeForPath($path) {
6d9380f96   Cédric Dupont   Update sources OC...
56
57
58
  		if (!$this->fileView) {
  			throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
  		}
31b7f2792   Kload   Upgrade to ownclo...
59
60
61
62
63
64
65
66
67
68
  
  		$path = trim($path, '/');
  		if (isset($this->cache[$path])) {
  			return $this->cache[$path];
  		}
  
  		// Is it the root node?
  		if (!strlen($path)) {
  			return $this->rootNode;
  		}
a293d369c   Kload   Update sources to...
69
70
  		if (pathinfo($path, PATHINFO_EXTENSION) === 'part') {
  			// read from storage
6d9380f96   Cédric Dupont   Update sources OC...
71
  			$absPath = $this->fileView->getAbsolutePath($path);
a293d369c   Kload   Update sources to...
72
73
  			list($storage, $internalPath) = Filesystem::resolvePath('/' . $absPath);
  			if ($storage) {
6d9380f96   Cédric Dupont   Update sources OC...
74
75
76
  				/**
  				 * @var \OC\Files\Storage\Storage $storage
  				 */
a293d369c   Kload   Update sources to...
77
78
  				$scanner = $storage->getScanner($internalPath);
  				// get data directly
6d9380f96   Cédric Dupont   Update sources OC...
79
80
81
82
  				$data = $scanner->getData($internalPath);
  				$info = new FileInfo($absPath, $storage, $internalPath, $data);
  			} else {
  				$info = null;
a293d369c   Kload   Update sources to...
83
  			}
6d9380f96   Cédric Dupont   Update sources OC...
84
  		} else {
a293d369c   Kload   Update sources to...
85
  			// read from cache
6d9380f96   Cédric Dupont   Update sources OC...
86
87
88
89
90
91
92
  			try {
  				$info = $this->fileView->getFileInfo($path);
  			} catch (StorageNotAvailableException $e) {
  				throw new \Sabre\DAV\Exception\ServiceUnavailable('Storage not available');
  			} catch (StorageInvalidException $e){
  				throw new \Sabre\DAV\Exception\NotFound('Storage ' . $path . ' is invalid');
  			}
a293d369c   Kload   Update sources to...
93
  		}
31b7f2792   Kload   Upgrade to ownclo...
94
95
  
  		if (!$info) {
6d9380f96   Cédric Dupont   Update sources OC...
96
  			throw new \Sabre\DAV\Exception\NotFound('File with name ' . $path . ' could not be located');
31b7f2792   Kload   Upgrade to ownclo...
97
  		}
6d9380f96   Cédric Dupont   Update sources OC...
98
99
  		if ($info->getType() === 'dir') {
  			$node = new \OC_Connector_Sabre_Directory($this->fileView, $info);
31b7f2792   Kload   Upgrade to ownclo...
100
  		} else {
6d9380f96   Cédric Dupont   Update sources OC...
101
  			$node = new \OC_Connector_Sabre_File($this->fileView, $info);
31b7f2792   Kload   Upgrade to ownclo...
102
  		}
31b7f2792   Kload   Upgrade to ownclo...
103
104
105
106
107
108
109
110
111
112
  		$this->cache[$path] = $node;
  		return $node;
  
  	}
  
  	/**
  	 * Moves a file from one location to another
  	 *
  	 * @param string $sourcePath The path to the file which should be moved
  	 * @param string $destinationPath The full destination path, so not just the destination parent node
6d9380f96   Cédric Dupont   Update sources OC...
113
114
115
  	 * @throws \Sabre\DAV\Exception\BadRequest
  	 * @throws \Sabre\DAV\Exception\ServiceUnavailable
  	 * @throws \Sabre\DAV\Exception\Forbidden
31b7f2792   Kload   Upgrade to ownclo...
116
117
118
  	 * @return int
  	 */
  	public function move($sourcePath, $destinationPath) {
6d9380f96   Cédric Dupont   Update sources OC...
119
120
121
  		if (!$this->fileView) {
  			throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
  		}
31b7f2792   Kload   Upgrade to ownclo...
122
123
  
  		$sourceNode = $this->getNodeForPath($sourcePath);
6d9380f96   Cédric Dupont   Update sources OC...
124
125
126
127
128
129
130
131
132
133
134
  		if ($sourceNode instanceof \Sabre\DAV\ICollection and $this->nodeExists($destinationPath)) {
  			throw new \Sabre\DAV\Exception\Forbidden('Could not copy directory ' . $sourceNode . ', target exists');
  		}
  		list($sourceDir,) = \Sabre\DAV\URLUtil::splitPath($sourcePath);
  		list($destinationDir,) = \Sabre\DAV\URLUtil::splitPath($destinationPath);
  
  		$isMovableMount = false;
  		$sourceMount = $this->mountManager->find($this->fileView->getAbsolutePath($sourcePath));
  		$internalPath = $sourceMount->getInternalPath($this->fileView->getAbsolutePath($sourcePath));
  		if ($sourceMount instanceof MoveableMount && $internalPath === '') {
  			$isMovableMount = true;
31b7f2792   Kload   Upgrade to ownclo...
135
  		}
31b7f2792   Kload   Upgrade to ownclo...
136
137
  
  		// check update privileges
6d9380f96   Cédric Dupont   Update sources OC...
138
139
  		if (!$this->fileView->isUpdatable($sourcePath) && !$isMovableMount) {
  			throw new \Sabre\DAV\Exception\Forbidden();
31b7f2792   Kload   Upgrade to ownclo...
140
141
  		}
  		if ($sourceDir !== $destinationDir) {
6d9380f96   Cédric Dupont   Update sources OC...
142
143
  			if (!$this->fileView->isCreatable($destinationDir)) {
  				throw new \Sabre\DAV\Exception\Forbidden();
31b7f2792   Kload   Upgrade to ownclo...
144
  			}
6d9380f96   Cédric Dupont   Update sources OC...
145
146
  			if (!$this->fileView->isDeletable($sourcePath) && !$isMovableMount) {
  				throw new \Sabre\DAV\Exception\Forbidden();
31b7f2792   Kload   Upgrade to ownclo...
147
148
  			}
  		}
6d9380f96   Cédric Dupont   Update sources OC...
149
150
151
152
153
154
  		$fileName = basename($destinationPath);
  		if (!\OCP\Util::isValidFileName($fileName)) {
  			throw new \Sabre\DAV\Exception\BadRequest();
  		}
  
  		$renameOkay = $this->fileView->rename($sourcePath, $destinationPath);
31b7f2792   Kload   Upgrade to ownclo...
155
  		if (!$renameOkay) {
6d9380f96   Cédric Dupont   Update sources OC...
156
  			throw new \Sabre\DAV\Exception\Forbidden('');
31b7f2792   Kload   Upgrade to ownclo...
157
158
159
  		}
  
  		// update properties
6d9380f96   Cédric Dupont   Update sources OC...
160
161
162
163
  		$query = \OC_DB::prepare('UPDATE `*PREFIX*properties` SET `propertypath` = ?'
  			. ' WHERE `userid` = ? AND `propertypath` = ?');
  		$query->execute(array(\OC\Files\Filesystem::normalizePath($destinationPath), \OC_User::getUser(),
  			\OC\Files\Filesystem::normalizePath($sourcePath)));
31b7f2792   Kload   Upgrade to ownclo...
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  
  		$this->markDirty($sourceDir);
  		$this->markDirty($destinationDir);
  
  	}
  
  	/**
  	 * Copies a file or directory.
  	 *
  	 * This method must work recursively and delete the destination
  	 * if it exists
  	 *
  	 * @param string $source
  	 * @param string $destination
6d9380f96   Cédric Dupont   Update sources OC...
178
  	 * @throws \Sabre\DAV\Exception\ServiceUnavailable
31b7f2792   Kload   Upgrade to ownclo...
179
180
181
  	 * @return void
  	 */
  	public function copy($source, $destination) {
6d9380f96   Cédric Dupont   Update sources OC...
182
183
184
  		if (!$this->fileView) {
  			throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
  		}
31b7f2792   Kload   Upgrade to ownclo...
185

6d9380f96   Cédric Dupont   Update sources OC...
186
187
  		if ($this->fileView->is_file($source)) {
  			$this->fileView->copy($source, $destination);
31b7f2792   Kload   Upgrade to ownclo...
188
  		} else {
6d9380f96   Cédric Dupont   Update sources OC...
189
190
191
192
  			$this->fileView->mkdir($destination);
  			$dh = $this->fileView->opendir($source);
  			if (is_resource($dh)) {
  				while (($subNode = readdir($dh)) !== false) {
31b7f2792   Kload   Upgrade to ownclo...
193

6d9380f96   Cédric Dupont   Update sources OC...
194
195
  					if ($subNode == '.' || $subNode == '..') continue;
  					$this->copy($source . '/' . $subNode, $destination . '/' . $subNode);
31b7f2792   Kload   Upgrade to ownclo...
196
197
198
199
  
  				}
  			}
  		}
6d9380f96   Cédric Dupont   Update sources OC...
200
  		list($destinationDir,) = \Sabre\DAV\URLUtil::splitPath($destination);
31b7f2792   Kload   Upgrade to ownclo...
201
202
  		$this->markDirty($destinationDir);
  	}
31b7f2792   Kload   Upgrade to ownclo...
203
  }