@@ -35,396 +35,396 @@ |
||
| 35 | 35 | |
| 36 | 36 | // FIXME: this class really should be abstract |
| 37 | 37 | class Node implements \OCP\Files\Node { |
| 38 | - /** |
|
| 39 | - * @var \OC\Files\View $view |
|
| 40 | - */ |
|
| 41 | - protected $view; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var \OC\Files\Node\Root $root |
|
| 45 | - */ |
|
| 46 | - protected $root; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @var string $path |
|
| 50 | - */ |
|
| 51 | - protected $path; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @var \OCP\Files\FileInfo |
|
| 55 | - */ |
|
| 56 | - protected $fileInfo; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @param \OC\Files\View $view |
|
| 60 | - * @param \OCP\Files\IRootFolder $root |
|
| 61 | - * @param string $path |
|
| 62 | - * @param FileInfo $fileInfo |
|
| 63 | - */ |
|
| 64 | - public function __construct($root, $view, $path, $fileInfo = null) { |
|
| 65 | - $this->view = $view; |
|
| 66 | - $this->root = $root; |
|
| 67 | - $this->path = $path; |
|
| 68 | - $this->fileInfo = $fileInfo; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Creates a Node of the same type that represents a non-existing path |
|
| 73 | - * |
|
| 74 | - * @param string $path path |
|
| 75 | - * @return string non-existing node class |
|
| 76 | - */ |
|
| 77 | - protected function createNonExistingNode($path) { |
|
| 78 | - throw new \Exception('Must be implemented by subclasses'); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Returns the matching file info |
|
| 83 | - * |
|
| 84 | - * @return FileInfo |
|
| 85 | - * @throws InvalidPathException |
|
| 86 | - * @throws NotFoundException |
|
| 87 | - */ |
|
| 88 | - public function getFileInfo() { |
|
| 89 | - if (!Filesystem::isValidPath($this->path)) { |
|
| 90 | - throw new InvalidPathException(); |
|
| 91 | - } |
|
| 92 | - if (!$this->fileInfo) { |
|
| 93 | - $fileInfo = $this->view->getFileInfo($this->path); |
|
| 94 | - if ($fileInfo instanceof FileInfo) { |
|
| 95 | - $this->fileInfo = $fileInfo; |
|
| 96 | - } else { |
|
| 97 | - throw new NotFoundException(); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - return $this->fileInfo; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param string[] $hooks |
|
| 105 | - */ |
|
| 106 | - protected function sendHooks($hooks) { |
|
| 107 | - foreach ($hooks as $hook) { |
|
| 108 | - $this->root->emit('\OC\Files', $hook, array($this)); |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @param int $permissions |
|
| 114 | - * @return bool |
|
| 115 | - */ |
|
| 116 | - protected function checkPermissions($permissions) { |
|
| 117 | - return ($this->getPermissions() & $permissions) === $permissions; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - public function delete() { |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * @param int $mtime |
|
| 125 | - * @throws \OCP\Files\NotPermittedException |
|
| 126 | - */ |
|
| 127 | - public function touch($mtime = null) { |
|
| 128 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) { |
|
| 129 | - $this->sendHooks(array('preTouch')); |
|
| 130 | - $this->view->touch($this->path, $mtime); |
|
| 131 | - $this->sendHooks(array('postTouch')); |
|
| 132 | - if ($this->fileInfo) { |
|
| 133 | - if (is_null($mtime)) { |
|
| 134 | - $mtime = time(); |
|
| 135 | - } |
|
| 136 | - $this->fileInfo['mtime'] = $mtime; |
|
| 137 | - } |
|
| 138 | - } else { |
|
| 139 | - throw new NotPermittedException(); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * @return \OC\Files\Storage\Storage |
|
| 145 | - * @throws \OCP\Files\NotFoundException |
|
| 146 | - */ |
|
| 147 | - public function getStorage() { |
|
| 148 | - list($storage,) = $this->view->resolvePath($this->path); |
|
| 149 | - return $storage; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @return string |
|
| 154 | - */ |
|
| 155 | - public function getPath() { |
|
| 156 | - return $this->path; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @return string |
|
| 161 | - */ |
|
| 162 | - public function getInternalPath() { |
|
| 163 | - list(, $internalPath) = $this->view->resolvePath($this->path); |
|
| 164 | - return $internalPath; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @return int |
|
| 169 | - * @throws InvalidPathException |
|
| 170 | - * @throws NotFoundException |
|
| 171 | - */ |
|
| 172 | - public function getId() { |
|
| 173 | - return $this->getFileInfo()->getId(); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * @return array |
|
| 178 | - */ |
|
| 179 | - public function stat() { |
|
| 180 | - return $this->view->stat($this->path); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * @return int |
|
| 185 | - * @throws InvalidPathException |
|
| 186 | - * @throws NotFoundException |
|
| 187 | - */ |
|
| 188 | - public function getMTime() { |
|
| 189 | - return $this->getFileInfo()->getMTime(); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * @return int |
|
| 194 | - * @throws InvalidPathException |
|
| 195 | - * @throws NotFoundException |
|
| 196 | - */ |
|
| 197 | - public function getSize() { |
|
| 198 | - return $this->getFileInfo()->getSize(); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * @return string |
|
| 203 | - * @throws InvalidPathException |
|
| 204 | - * @throws NotFoundException |
|
| 205 | - */ |
|
| 206 | - public function getEtag() { |
|
| 207 | - return $this->getFileInfo()->getEtag(); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * @return int |
|
| 212 | - * @throws InvalidPathException |
|
| 213 | - * @throws NotFoundException |
|
| 214 | - */ |
|
| 215 | - public function getPermissions() { |
|
| 216 | - return $this->getFileInfo()->getPermissions(); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * @return bool |
|
| 221 | - * @throws InvalidPathException |
|
| 222 | - * @throws NotFoundException |
|
| 223 | - */ |
|
| 224 | - public function isReadable() { |
|
| 225 | - return $this->getFileInfo()->isReadable(); |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * @return bool |
|
| 230 | - * @throws InvalidPathException |
|
| 231 | - * @throws NotFoundException |
|
| 232 | - */ |
|
| 233 | - public function isUpdateable() { |
|
| 234 | - return $this->getFileInfo()->isUpdateable(); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * @return bool |
|
| 239 | - * @throws InvalidPathException |
|
| 240 | - * @throws NotFoundException |
|
| 241 | - */ |
|
| 242 | - public function isDeletable() { |
|
| 243 | - return $this->getFileInfo()->isDeletable(); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * @return bool |
|
| 248 | - * @throws InvalidPathException |
|
| 249 | - * @throws NotFoundException |
|
| 250 | - */ |
|
| 251 | - public function isShareable() { |
|
| 252 | - return $this->getFileInfo()->isShareable(); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * @return bool |
|
| 257 | - * @throws InvalidPathException |
|
| 258 | - * @throws NotFoundException |
|
| 259 | - */ |
|
| 260 | - public function isCreatable() { |
|
| 261 | - return $this->getFileInfo()->isCreatable(); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * @return Node |
|
| 266 | - */ |
|
| 267 | - public function getParent() { |
|
| 268 | - $newPath = dirname($this->path); |
|
| 269 | - if ($newPath === '' || $newPath === '.' || $newPath === '/') { |
|
| 270 | - return $this->root; |
|
| 271 | - } |
|
| 272 | - return $this->root->get($newPath); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * @return string |
|
| 277 | - */ |
|
| 278 | - public function getName() { |
|
| 279 | - return basename($this->path); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * @param string $path |
|
| 284 | - * @return string |
|
| 285 | - */ |
|
| 286 | - protected function normalizePath($path) { |
|
| 287 | - if ($path === '' or $path === '/') { |
|
| 288 | - return '/'; |
|
| 289 | - } |
|
| 290 | - //no windows style slashes |
|
| 291 | - $path = str_replace('\\', '/', $path); |
|
| 292 | - //add leading slash |
|
| 293 | - if ($path[0] !== '/') { |
|
| 294 | - $path = '/' . $path; |
|
| 295 | - } |
|
| 296 | - //remove duplicate slashes |
|
| 297 | - while (strpos($path, '//') !== false) { |
|
| 298 | - $path = str_replace('//', '/', $path); |
|
| 299 | - } |
|
| 300 | - //remove trailing slash |
|
| 301 | - $path = rtrim($path, '/'); |
|
| 302 | - |
|
| 303 | - return $path; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * check if the requested path is valid |
|
| 308 | - * |
|
| 309 | - * @param string $path |
|
| 310 | - * @return bool |
|
| 311 | - */ |
|
| 312 | - public function isValidPath($path) { |
|
| 313 | - if (!$path || $path[0] !== '/') { |
|
| 314 | - $path = '/' . $path; |
|
| 315 | - } |
|
| 316 | - if (strstr($path, '/../') || strrchr($path, '/') === '/..') { |
|
| 317 | - return false; |
|
| 318 | - } |
|
| 319 | - return true; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - public function isMounted() { |
|
| 323 | - return $this->getFileInfo()->isMounted(); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - public function isShared() { |
|
| 327 | - return $this->getFileInfo()->isShared(); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - public function getMimeType() { |
|
| 331 | - return $this->getFileInfo()->getMimetype(); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - public function getMimePart() { |
|
| 335 | - return $this->getFileInfo()->getMimePart(); |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - public function getType() { |
|
| 339 | - return $this->getFileInfo()->getType(); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - public function isEncrypted() { |
|
| 343 | - return $this->getFileInfo()->isEncrypted(); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - public function getMountPoint() { |
|
| 347 | - return $this->getFileInfo()->getMountPoint(); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - public function getOwner() { |
|
| 351 | - return $this->getFileInfo()->getOwner(); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - public function getChecksum() { |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 359 | - * @throws \OCP\Lock\LockedException |
|
| 360 | - */ |
|
| 361 | - public function lock($type) { |
|
| 362 | - $this->view->lockFile($this->path, $type); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 367 | - * @throws \OCP\Lock\LockedException |
|
| 368 | - */ |
|
| 369 | - public function changeLock($type) { |
|
| 370 | - $this->view->changeLock($this->path, $type); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 375 | - * @throws \OCP\Lock\LockedException |
|
| 376 | - */ |
|
| 377 | - public function unlock($type) { |
|
| 378 | - $this->view->unlockFile($this->path, $type); |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * @param string $targetPath |
|
| 383 | - * @throws \OCP\Files\NotPermittedException if copy not allowed or failed |
|
| 384 | - * @return \OC\Files\Node\Node |
|
| 385 | - */ |
|
| 386 | - public function copy($targetPath) { |
|
| 387 | - $targetPath = $this->normalizePath($targetPath); |
|
| 388 | - $parent = $this->root->get(dirname($targetPath)); |
|
| 389 | - if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) { |
|
| 390 | - $nonExisting = $this->createNonExistingNode($targetPath); |
|
| 391 | - $this->root->emit('\OC\Files', 'preCopy', [$this, $nonExisting]); |
|
| 392 | - $this->root->emit('\OC\Files', 'preWrite', [$nonExisting]); |
|
| 393 | - if (!$this->view->copy($this->path, $targetPath)) { |
|
| 394 | - throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath); |
|
| 395 | - } |
|
| 396 | - $targetNode = $this->root->get($targetPath); |
|
| 397 | - $this->root->emit('\OC\Files', 'postCopy', [$this, $targetNode]); |
|
| 398 | - $this->root->emit('\OC\Files', 'postWrite', [$targetNode]); |
|
| 399 | - return $targetNode; |
|
| 400 | - } else { |
|
| 401 | - throw new NotPermittedException('No permission to copy to path ' . $targetPath); |
|
| 402 | - } |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * @param string $targetPath |
|
| 407 | - * @throws \OCP\Files\NotPermittedException if move not allowed or failed |
|
| 408 | - * @return \OC\Files\Node\Node |
|
| 409 | - */ |
|
| 410 | - public function move($targetPath) { |
|
| 411 | - $targetPath = $this->normalizePath($targetPath); |
|
| 412 | - $parent = $this->root->get(dirname($targetPath)); |
|
| 413 | - if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) { |
|
| 414 | - $nonExisting = $this->createNonExistingNode($targetPath); |
|
| 415 | - $this->root->emit('\OC\Files', 'preRename', [$this, $nonExisting]); |
|
| 416 | - $this->root->emit('\OC\Files', 'preWrite', [$nonExisting]); |
|
| 417 | - if (!$this->view->rename($this->path, $targetPath)) { |
|
| 418 | - throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath); |
|
| 419 | - } |
|
| 420 | - $targetNode = $this->root->get($targetPath); |
|
| 421 | - $this->root->emit('\OC\Files', 'postRename', [$this, $targetNode]); |
|
| 422 | - $this->root->emit('\OC\Files', 'postWrite', [$targetNode]); |
|
| 423 | - $this->path = $targetPath; |
|
| 424 | - return $targetNode; |
|
| 425 | - } else { |
|
| 426 | - throw new NotPermittedException('No permission to move to path ' . $targetPath); |
|
| 427 | - } |
|
| 428 | - } |
|
| 38 | + /** |
|
| 39 | + * @var \OC\Files\View $view |
|
| 40 | + */ |
|
| 41 | + protected $view; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var \OC\Files\Node\Root $root |
|
| 45 | + */ |
|
| 46 | + protected $root; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @var string $path |
|
| 50 | + */ |
|
| 51 | + protected $path; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @var \OCP\Files\FileInfo |
|
| 55 | + */ |
|
| 56 | + protected $fileInfo; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @param \OC\Files\View $view |
|
| 60 | + * @param \OCP\Files\IRootFolder $root |
|
| 61 | + * @param string $path |
|
| 62 | + * @param FileInfo $fileInfo |
|
| 63 | + */ |
|
| 64 | + public function __construct($root, $view, $path, $fileInfo = null) { |
|
| 65 | + $this->view = $view; |
|
| 66 | + $this->root = $root; |
|
| 67 | + $this->path = $path; |
|
| 68 | + $this->fileInfo = $fileInfo; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Creates a Node of the same type that represents a non-existing path |
|
| 73 | + * |
|
| 74 | + * @param string $path path |
|
| 75 | + * @return string non-existing node class |
|
| 76 | + */ |
|
| 77 | + protected function createNonExistingNode($path) { |
|
| 78 | + throw new \Exception('Must be implemented by subclasses'); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Returns the matching file info |
|
| 83 | + * |
|
| 84 | + * @return FileInfo |
|
| 85 | + * @throws InvalidPathException |
|
| 86 | + * @throws NotFoundException |
|
| 87 | + */ |
|
| 88 | + public function getFileInfo() { |
|
| 89 | + if (!Filesystem::isValidPath($this->path)) { |
|
| 90 | + throw new InvalidPathException(); |
|
| 91 | + } |
|
| 92 | + if (!$this->fileInfo) { |
|
| 93 | + $fileInfo = $this->view->getFileInfo($this->path); |
|
| 94 | + if ($fileInfo instanceof FileInfo) { |
|
| 95 | + $this->fileInfo = $fileInfo; |
|
| 96 | + } else { |
|
| 97 | + throw new NotFoundException(); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + return $this->fileInfo; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param string[] $hooks |
|
| 105 | + */ |
|
| 106 | + protected function sendHooks($hooks) { |
|
| 107 | + foreach ($hooks as $hook) { |
|
| 108 | + $this->root->emit('\OC\Files', $hook, array($this)); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @param int $permissions |
|
| 114 | + * @return bool |
|
| 115 | + */ |
|
| 116 | + protected function checkPermissions($permissions) { |
|
| 117 | + return ($this->getPermissions() & $permissions) === $permissions; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + public function delete() { |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * @param int $mtime |
|
| 125 | + * @throws \OCP\Files\NotPermittedException |
|
| 126 | + */ |
|
| 127 | + public function touch($mtime = null) { |
|
| 128 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) { |
|
| 129 | + $this->sendHooks(array('preTouch')); |
|
| 130 | + $this->view->touch($this->path, $mtime); |
|
| 131 | + $this->sendHooks(array('postTouch')); |
|
| 132 | + if ($this->fileInfo) { |
|
| 133 | + if (is_null($mtime)) { |
|
| 134 | + $mtime = time(); |
|
| 135 | + } |
|
| 136 | + $this->fileInfo['mtime'] = $mtime; |
|
| 137 | + } |
|
| 138 | + } else { |
|
| 139 | + throw new NotPermittedException(); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * @return \OC\Files\Storage\Storage |
|
| 145 | + * @throws \OCP\Files\NotFoundException |
|
| 146 | + */ |
|
| 147 | + public function getStorage() { |
|
| 148 | + list($storage,) = $this->view->resolvePath($this->path); |
|
| 149 | + return $storage; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @return string |
|
| 154 | + */ |
|
| 155 | + public function getPath() { |
|
| 156 | + return $this->path; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @return string |
|
| 161 | + */ |
|
| 162 | + public function getInternalPath() { |
|
| 163 | + list(, $internalPath) = $this->view->resolvePath($this->path); |
|
| 164 | + return $internalPath; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @return int |
|
| 169 | + * @throws InvalidPathException |
|
| 170 | + * @throws NotFoundException |
|
| 171 | + */ |
|
| 172 | + public function getId() { |
|
| 173 | + return $this->getFileInfo()->getId(); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * @return array |
|
| 178 | + */ |
|
| 179 | + public function stat() { |
|
| 180 | + return $this->view->stat($this->path); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * @return int |
|
| 185 | + * @throws InvalidPathException |
|
| 186 | + * @throws NotFoundException |
|
| 187 | + */ |
|
| 188 | + public function getMTime() { |
|
| 189 | + return $this->getFileInfo()->getMTime(); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * @return int |
|
| 194 | + * @throws InvalidPathException |
|
| 195 | + * @throws NotFoundException |
|
| 196 | + */ |
|
| 197 | + public function getSize() { |
|
| 198 | + return $this->getFileInfo()->getSize(); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * @return string |
|
| 203 | + * @throws InvalidPathException |
|
| 204 | + * @throws NotFoundException |
|
| 205 | + */ |
|
| 206 | + public function getEtag() { |
|
| 207 | + return $this->getFileInfo()->getEtag(); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * @return int |
|
| 212 | + * @throws InvalidPathException |
|
| 213 | + * @throws NotFoundException |
|
| 214 | + */ |
|
| 215 | + public function getPermissions() { |
|
| 216 | + return $this->getFileInfo()->getPermissions(); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * @return bool |
|
| 221 | + * @throws InvalidPathException |
|
| 222 | + * @throws NotFoundException |
|
| 223 | + */ |
|
| 224 | + public function isReadable() { |
|
| 225 | + return $this->getFileInfo()->isReadable(); |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * @return bool |
|
| 230 | + * @throws InvalidPathException |
|
| 231 | + * @throws NotFoundException |
|
| 232 | + */ |
|
| 233 | + public function isUpdateable() { |
|
| 234 | + return $this->getFileInfo()->isUpdateable(); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * @return bool |
|
| 239 | + * @throws InvalidPathException |
|
| 240 | + * @throws NotFoundException |
|
| 241 | + */ |
|
| 242 | + public function isDeletable() { |
|
| 243 | + return $this->getFileInfo()->isDeletable(); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * @return bool |
|
| 248 | + * @throws InvalidPathException |
|
| 249 | + * @throws NotFoundException |
|
| 250 | + */ |
|
| 251 | + public function isShareable() { |
|
| 252 | + return $this->getFileInfo()->isShareable(); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * @return bool |
|
| 257 | + * @throws InvalidPathException |
|
| 258 | + * @throws NotFoundException |
|
| 259 | + */ |
|
| 260 | + public function isCreatable() { |
|
| 261 | + return $this->getFileInfo()->isCreatable(); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * @return Node |
|
| 266 | + */ |
|
| 267 | + public function getParent() { |
|
| 268 | + $newPath = dirname($this->path); |
|
| 269 | + if ($newPath === '' || $newPath === '.' || $newPath === '/') { |
|
| 270 | + return $this->root; |
|
| 271 | + } |
|
| 272 | + return $this->root->get($newPath); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * @return string |
|
| 277 | + */ |
|
| 278 | + public function getName() { |
|
| 279 | + return basename($this->path); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * @param string $path |
|
| 284 | + * @return string |
|
| 285 | + */ |
|
| 286 | + protected function normalizePath($path) { |
|
| 287 | + if ($path === '' or $path === '/') { |
|
| 288 | + return '/'; |
|
| 289 | + } |
|
| 290 | + //no windows style slashes |
|
| 291 | + $path = str_replace('\\', '/', $path); |
|
| 292 | + //add leading slash |
|
| 293 | + if ($path[0] !== '/') { |
|
| 294 | + $path = '/' . $path; |
|
| 295 | + } |
|
| 296 | + //remove duplicate slashes |
|
| 297 | + while (strpos($path, '//') !== false) { |
|
| 298 | + $path = str_replace('//', '/', $path); |
|
| 299 | + } |
|
| 300 | + //remove trailing slash |
|
| 301 | + $path = rtrim($path, '/'); |
|
| 302 | + |
|
| 303 | + return $path; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * check if the requested path is valid |
|
| 308 | + * |
|
| 309 | + * @param string $path |
|
| 310 | + * @return bool |
|
| 311 | + */ |
|
| 312 | + public function isValidPath($path) { |
|
| 313 | + if (!$path || $path[0] !== '/') { |
|
| 314 | + $path = '/' . $path; |
|
| 315 | + } |
|
| 316 | + if (strstr($path, '/../') || strrchr($path, '/') === '/..') { |
|
| 317 | + return false; |
|
| 318 | + } |
|
| 319 | + return true; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + public function isMounted() { |
|
| 323 | + return $this->getFileInfo()->isMounted(); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + public function isShared() { |
|
| 327 | + return $this->getFileInfo()->isShared(); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + public function getMimeType() { |
|
| 331 | + return $this->getFileInfo()->getMimetype(); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + public function getMimePart() { |
|
| 335 | + return $this->getFileInfo()->getMimePart(); |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + public function getType() { |
|
| 339 | + return $this->getFileInfo()->getType(); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + public function isEncrypted() { |
|
| 343 | + return $this->getFileInfo()->isEncrypted(); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + public function getMountPoint() { |
|
| 347 | + return $this->getFileInfo()->getMountPoint(); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + public function getOwner() { |
|
| 351 | + return $this->getFileInfo()->getOwner(); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + public function getChecksum() { |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 359 | + * @throws \OCP\Lock\LockedException |
|
| 360 | + */ |
|
| 361 | + public function lock($type) { |
|
| 362 | + $this->view->lockFile($this->path, $type); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 367 | + * @throws \OCP\Lock\LockedException |
|
| 368 | + */ |
|
| 369 | + public function changeLock($type) { |
|
| 370 | + $this->view->changeLock($this->path, $type); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 375 | + * @throws \OCP\Lock\LockedException |
|
| 376 | + */ |
|
| 377 | + public function unlock($type) { |
|
| 378 | + $this->view->unlockFile($this->path, $type); |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * @param string $targetPath |
|
| 383 | + * @throws \OCP\Files\NotPermittedException if copy not allowed or failed |
|
| 384 | + * @return \OC\Files\Node\Node |
|
| 385 | + */ |
|
| 386 | + public function copy($targetPath) { |
|
| 387 | + $targetPath = $this->normalizePath($targetPath); |
|
| 388 | + $parent = $this->root->get(dirname($targetPath)); |
|
| 389 | + if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) { |
|
| 390 | + $nonExisting = $this->createNonExistingNode($targetPath); |
|
| 391 | + $this->root->emit('\OC\Files', 'preCopy', [$this, $nonExisting]); |
|
| 392 | + $this->root->emit('\OC\Files', 'preWrite', [$nonExisting]); |
|
| 393 | + if (!$this->view->copy($this->path, $targetPath)) { |
|
| 394 | + throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath); |
|
| 395 | + } |
|
| 396 | + $targetNode = $this->root->get($targetPath); |
|
| 397 | + $this->root->emit('\OC\Files', 'postCopy', [$this, $targetNode]); |
|
| 398 | + $this->root->emit('\OC\Files', 'postWrite', [$targetNode]); |
|
| 399 | + return $targetNode; |
|
| 400 | + } else { |
|
| 401 | + throw new NotPermittedException('No permission to copy to path ' . $targetPath); |
|
| 402 | + } |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * @param string $targetPath |
|
| 407 | + * @throws \OCP\Files\NotPermittedException if move not allowed or failed |
|
| 408 | + * @return \OC\Files\Node\Node |
|
| 409 | + */ |
|
| 410 | + public function move($targetPath) { |
|
| 411 | + $targetPath = $this->normalizePath($targetPath); |
|
| 412 | + $parent = $this->root->get(dirname($targetPath)); |
|
| 413 | + if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) { |
|
| 414 | + $nonExisting = $this->createNonExistingNode($targetPath); |
|
| 415 | + $this->root->emit('\OC\Files', 'preRename', [$this, $nonExisting]); |
|
| 416 | + $this->root->emit('\OC\Files', 'preWrite', [$nonExisting]); |
|
| 417 | + if (!$this->view->rename($this->path, $targetPath)) { |
|
| 418 | + throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath); |
|
| 419 | + } |
|
| 420 | + $targetNode = $this->root->get($targetPath); |
|
| 421 | + $this->root->emit('\OC\Files', 'postRename', [$this, $targetNode]); |
|
| 422 | + $this->root->emit('\OC\Files', 'postWrite', [$targetNode]); |
|
| 423 | + $this->path = $targetPath; |
|
| 424 | + return $targetNode; |
|
| 425 | + } else { |
|
| 426 | + throw new NotPermittedException('No permission to move to path ' . $targetPath); |
|
| 427 | + } |
|
| 428 | + } |
|
| 429 | 429 | |
| 430 | 430 | } |