@@ -35,50 +35,50 @@ |
||
| 35 | 35 | |
| 36 | 36 | class SMB extends Backend { |
| 37 | 37 | |
| 38 | - use LegacyDependencyCheckPolyfill; |
|
| 38 | + use LegacyDependencyCheckPolyfill; |
|
| 39 | 39 | |
| 40 | - public function __construct(IL10N $l, Password $legacyAuth) { |
|
| 41 | - $this |
|
| 42 | - ->setIdentifier('smb') |
|
| 43 | - ->addIdentifierAlias('\OC\Files\Storage\SMB')// legacy compat |
|
| 44 | - ->setStorageClass('\OCA\Files_External\Lib\Storage\SMB') |
|
| 45 | - ->setText($l->t('SMB / CIFS')) |
|
| 46 | - ->addParameters([ |
|
| 47 | - new DefinitionParameter('host', $l->t('Host')), |
|
| 48 | - new DefinitionParameter('share', $l->t('Share')), |
|
| 49 | - (new DefinitionParameter('root', $l->t('Remote subfolder'))) |
|
| 50 | - ->setFlag(DefinitionParameter::FLAG_OPTIONAL), |
|
| 51 | - (new DefinitionParameter('domain', $l->t('Domain'))) |
|
| 52 | - ->setFlag(DefinitionParameter::FLAG_OPTIONAL), |
|
| 53 | - ]) |
|
| 54 | - ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD) |
|
| 55 | - ->addAuthScheme(AuthMechanism::SCHEME_SMB) |
|
| 56 | - ->setLegacyAuthMechanism($legacyAuth); |
|
| 57 | - } |
|
| 40 | + public function __construct(IL10N $l, Password $legacyAuth) { |
|
| 41 | + $this |
|
| 42 | + ->setIdentifier('smb') |
|
| 43 | + ->addIdentifierAlias('\OC\Files\Storage\SMB')// legacy compat |
|
| 44 | + ->setStorageClass('\OCA\Files_External\Lib\Storage\SMB') |
|
| 45 | + ->setText($l->t('SMB / CIFS')) |
|
| 46 | + ->addParameters([ |
|
| 47 | + new DefinitionParameter('host', $l->t('Host')), |
|
| 48 | + new DefinitionParameter('share', $l->t('Share')), |
|
| 49 | + (new DefinitionParameter('root', $l->t('Remote subfolder'))) |
|
| 50 | + ->setFlag(DefinitionParameter::FLAG_OPTIONAL), |
|
| 51 | + (new DefinitionParameter('domain', $l->t('Domain'))) |
|
| 52 | + ->setFlag(DefinitionParameter::FLAG_OPTIONAL), |
|
| 53 | + ]) |
|
| 54 | + ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD) |
|
| 55 | + ->addAuthScheme(AuthMechanism::SCHEME_SMB) |
|
| 56 | + ->setLegacyAuthMechanism($legacyAuth); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * @param StorageConfig $storage |
|
| 61 | - * @param IUser $user |
|
| 62 | - */ |
|
| 63 | - public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) { |
|
| 64 | - $auth = $storage->getAuthMechanism(); |
|
| 65 | - if ($auth->getScheme() === AuthMechanism::SCHEME_PASSWORD) { |
|
| 66 | - $smbAuth = new BasicAuth( |
|
| 67 | - $storage->getBackendOption('user'), |
|
| 68 | - $storage->getBackendOption('domain'), |
|
| 69 | - $storage->getBackendOption('password') |
|
| 70 | - ); |
|
| 71 | - } else { |
|
| 72 | - switch ($auth->getIdentifier()) { |
|
| 73 | - case 'smb::kerberos': |
|
| 74 | - $smbAuth = new KerberosAuth(); |
|
| 75 | - break; |
|
| 76 | - default: |
|
| 77 | - throw new \InvalidArgumentException('unknown authentication backend'); |
|
| 78 | - } |
|
| 79 | - } |
|
| 59 | + /** |
|
| 60 | + * @param StorageConfig $storage |
|
| 61 | + * @param IUser $user |
|
| 62 | + */ |
|
| 63 | + public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) { |
|
| 64 | + $auth = $storage->getAuthMechanism(); |
|
| 65 | + if ($auth->getScheme() === AuthMechanism::SCHEME_PASSWORD) { |
|
| 66 | + $smbAuth = new BasicAuth( |
|
| 67 | + $storage->getBackendOption('user'), |
|
| 68 | + $storage->getBackendOption('domain'), |
|
| 69 | + $storage->getBackendOption('password') |
|
| 70 | + ); |
|
| 71 | + } else { |
|
| 72 | + switch ($auth->getIdentifier()) { |
|
| 73 | + case 'smb::kerberos': |
|
| 74 | + $smbAuth = new KerberosAuth(); |
|
| 75 | + break; |
|
| 76 | + default: |
|
| 77 | + throw new \InvalidArgumentException('unknown authentication backend'); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - $storage->setBackendOption('auth', $smbAuth); |
|
| 82 | - } |
|
| 81 | + $storage->setBackendOption('auth', $smbAuth); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | 84 | } |
@@ -95,8 +95,8 @@ discard block |
||
| 95 | 95 | $this->share = $this->server->getShare(trim($params['share'], '/')); |
| 96 | 96 | |
| 97 | 97 | $this->root = $params['root'] ?? '/'; |
| 98 | - $this->root = '/' . ltrim($this->root, '/'); |
|
| 99 | - $this->root = rtrim($this->root, '/') . '/'; |
|
| 98 | + $this->root = '/'.ltrim($this->root, '/'); |
|
| 99 | + $this->root = rtrim($this->root, '/').'/'; |
|
| 100 | 100 | |
| 101 | 101 | $this->statCache = new CappedMemoryCache(); |
| 102 | 102 | parent::__construct($params); |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | // FIXME: double slash to keep compatible with the old storage ids, |
| 110 | 110 | // failure to do so will lead to creation of a new storage id and |
| 111 | 111 | // loss of shares from the storage |
| 112 | - return 'smb::' . $this->server->getAuth()->getUsername() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root; |
|
| 112 | + return 'smb::'.$this->server->getAuth()->getUsername().'@'.$this->server->getHost().'//'.$this->share->getName().'/'.$this->root; |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | /** |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | * @return string |
| 118 | 118 | */ |
| 119 | 119 | protected function buildPath($path) { |
| 120 | - return Filesystem::normalizePath($this->root . '/' . $path, true, false, true); |
|
| 120 | + return Filesystem::normalizePath($this->root.'/'.$path, true, false, true); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | protected function relativePath($fullPath) { |
@@ -157,9 +157,9 @@ discard block |
||
| 157 | 157 | $path = $this->buildPath($path); |
| 158 | 158 | $files = $this->share->dir($path); |
| 159 | 159 | foreach ($files as $file) { |
| 160 | - $this->statCache[$path . '/' . $file->getName()] = $file; |
|
| 160 | + $this->statCache[$path.'/'.$file->getName()] = $file; |
|
| 161 | 161 | } |
| 162 | - return array_filter($files, function (IFileInfo $file) { |
|
| 162 | + return array_filter($files, function(IFileInfo $file) { |
|
| 163 | 163 | try { |
| 164 | 164 | return !$file->isHidden(); |
| 165 | 165 | } catch (ForbiddenException $e) { |
@@ -333,7 +333,7 @@ discard block |
||
| 333 | 333 | case 'w': |
| 334 | 334 | case 'wb': |
| 335 | 335 | $source = $this->share->write($fullPath); |
| 336 | - return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) { |
|
| 336 | + return CallBackWrapper::wrap($source, null, null, function() use ($fullPath) { |
|
| 337 | 337 | unset($this->statCache[$fullPath]); |
| 338 | 338 | }); |
| 339 | 339 | case 'a': |
@@ -365,7 +365,7 @@ discard block |
||
| 365 | 365 | } |
| 366 | 366 | $source = fopen($tmpFile, $mode); |
| 367 | 367 | $share = $this->share; |
| 368 | - return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) { |
|
| 368 | + return CallbackWrapper::wrap($source, null, null, function() use ($tmpFile, $fullPath, $share) { |
|
| 369 | 369 | unset($this->statCache[$fullPath]); |
| 370 | 370 | $share->put($tmpFile, $fullPath); |
| 371 | 371 | unlink($tmpFile); |
@@ -391,7 +391,7 @@ discard block |
||
| 391 | 391 | $content = $this->share->dir($this->buildPath($path)); |
| 392 | 392 | foreach ($content as $file) { |
| 393 | 393 | if ($file->isDirectory()) { |
| 394 | - $this->rmdir($path . '/' . $file->getName()); |
|
| 394 | + $this->rmdir($path.'/'.$file->getName()); |
|
| 395 | 395 | } else { |
| 396 | 396 | $this->share->del($file->getPath()); |
| 397 | 397 | } |
@@ -428,7 +428,7 @@ discard block |
||
| 428 | 428 | } catch (ForbiddenException $e) { |
| 429 | 429 | return false; |
| 430 | 430 | } |
| 431 | - $names = array_map(function ($info) { |
|
| 431 | + $names = array_map(function($info) { |
|
| 432 | 432 | /** @var \Icewind\SMB\IFileInfo $info */ |
| 433 | 433 | return $info->getName(); |
| 434 | 434 | }, $files); |
@@ -510,7 +510,7 @@ discard block |
||
| 510 | 510 | */ |
| 511 | 511 | public static function checkDependencies() { |
| 512 | 512 | return ( |
| 513 | - (bool)\OC_Helper::findBinaryPath('smbclient') |
|
| 513 | + (bool) \OC_Helper::findBinaryPath('smbclient') |
|
| 514 | 514 | || NativeServer::available(new System()) |
| 515 | 515 | ) ? true : ['smbclient']; |
| 516 | 516 | } |
@@ -529,7 +529,7 @@ discard block |
||
| 529 | 529 | } |
| 530 | 530 | |
| 531 | 531 | public function listen($path, callable $callback) { |
| 532 | - $this->notify($path)->listen(function (IChange $change) use ($callback) { |
|
| 532 | + $this->notify($path)->listen(function(IChange $change) use ($callback) { |
|
| 533 | 533 | if ($change instanceof IRenameChange) { |
| 534 | 534 | return $callback($change->getType(), $change->getPath(), $change->getTargetPath()); |
| 535 | 535 | } else { |
@@ -539,7 +539,7 @@ discard block |
||
| 539 | 539 | } |
| 540 | 540 | |
| 541 | 541 | public function notify($path) { |
| 542 | - $path = '/' . ltrim($path, '/'); |
|
| 542 | + $path = '/'.ltrim($path, '/'); |
|
| 543 | 543 | $shareNotifyHandler = $this->share->notify($this->buildPath($path)); |
| 544 | 544 | return new SMBNotifyHandler($shareNotifyHandler, $this->root); |
| 545 | 545 | } |
@@ -57,501 +57,501 @@ |
||
| 57 | 57 | use OCP\ILogger; |
| 58 | 58 | |
| 59 | 59 | class SMB extends Common implements INotifyStorage { |
| 60 | - /** |
|
| 61 | - * @var \Icewind\SMB\IServer |
|
| 62 | - */ |
|
| 63 | - protected $server; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @var \Icewind\SMB\IShare |
|
| 67 | - */ |
|
| 68 | - protected $share; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @var string |
|
| 72 | - */ |
|
| 73 | - protected $root; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @var \Icewind\SMB\IFileInfo[] |
|
| 77 | - */ |
|
| 78 | - protected $statCache; |
|
| 79 | - |
|
| 80 | - public function __construct($params) { |
|
| 81 | - if (!isset($params['host'])) { |
|
| 82 | - throw new \Exception('Invalid configuration, no host provided'); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - if (isset($params['auth'])) { |
|
| 86 | - $auth = $params['auth']; |
|
| 87 | - } else if (isset($params['user']) && isset($params['password']) && isset($params['share'])) { |
|
| 88 | - list($workgroup, $user) = $this->splitUser($params['user']); |
|
| 89 | - $auth = new BasicAuth($user, $workgroup, $params['password']); |
|
| 90 | - } else { |
|
| 91 | - throw new \Exception('Invalid configuration, no credentials provided'); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - $serverFactory = new ServerFactory(); |
|
| 95 | - $this->server = $serverFactory->createServer($params['host'], $auth); |
|
| 96 | - $this->share = $this->server->getShare(trim($params['share'], '/')); |
|
| 97 | - |
|
| 98 | - $this->root = $params['root'] ?? '/'; |
|
| 99 | - $this->root = '/' . ltrim($this->root, '/'); |
|
| 100 | - $this->root = rtrim($this->root, '/') . '/'; |
|
| 101 | - |
|
| 102 | - $this->statCache = new CappedMemoryCache(); |
|
| 103 | - parent::__construct($params); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - private function splitUser($user) { |
|
| 107 | - if (strpos($user, '/')) { |
|
| 108 | - return explode('/', $user, 2); |
|
| 109 | - } elseif (strpos($user, '\\')) { |
|
| 110 | - return explode('\\', $user); |
|
| 111 | - } else { |
|
| 112 | - return [null, $user]; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @return string |
|
| 118 | - */ |
|
| 119 | - public function getId() { |
|
| 120 | - // FIXME: double slash to keep compatible with the old storage ids, |
|
| 121 | - // failure to do so will lead to creation of a new storage id and |
|
| 122 | - // loss of shares from the storage |
|
| 123 | - return 'smb::' . $this->server->getAuth()->getUsername() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @param string $path |
|
| 128 | - * @return string |
|
| 129 | - */ |
|
| 130 | - protected function buildPath($path) { |
|
| 131 | - return Filesystem::normalizePath($this->root . '/' . $path, true, false, true); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - protected function relativePath($fullPath) { |
|
| 135 | - if ($fullPath === $this->root) { |
|
| 136 | - return ''; |
|
| 137 | - } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
|
| 138 | - return substr($fullPath, strlen($this->root)); |
|
| 139 | - } else { |
|
| 140 | - return null; |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @param string $path |
|
| 146 | - * @return \Icewind\SMB\IFileInfo |
|
| 147 | - * @throws StorageNotAvailableException |
|
| 148 | - */ |
|
| 149 | - protected function getFileInfo($path) { |
|
| 150 | - try { |
|
| 151 | - $path = $this->buildPath($path); |
|
| 152 | - if (!isset($this->statCache[$path])) { |
|
| 153 | - $this->statCache[$path] = $this->share->stat($path); |
|
| 154 | - } |
|
| 155 | - return $this->statCache[$path]; |
|
| 156 | - } catch (ConnectException $e) { |
|
| 157 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @param string $path |
|
| 163 | - * @return \Icewind\SMB\IFileInfo[] |
|
| 164 | - * @throws StorageNotAvailableException |
|
| 165 | - */ |
|
| 166 | - protected function getFolderContents($path) { |
|
| 167 | - try { |
|
| 168 | - $path = $this->buildPath($path); |
|
| 169 | - $files = $this->share->dir($path); |
|
| 170 | - foreach ($files as $file) { |
|
| 171 | - $this->statCache[$path . '/' . $file->getName()] = $file; |
|
| 172 | - } |
|
| 173 | - return array_filter($files, function (IFileInfo $file) { |
|
| 174 | - try { |
|
| 175 | - return !$file->isHidden(); |
|
| 176 | - } catch (ForbiddenException $e) { |
|
| 177 | - return false; |
|
| 178 | - } catch (NotFoundException $e) { |
|
| 179 | - return false; |
|
| 180 | - } |
|
| 181 | - }); |
|
| 182 | - } catch (ConnectException $e) { |
|
| 183 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * @param \Icewind\SMB\IFileInfo $info |
|
| 189 | - * @return array |
|
| 190 | - */ |
|
| 191 | - protected function formatInfo($info) { |
|
| 192 | - $result = [ |
|
| 193 | - 'size' => $info->getSize(), |
|
| 194 | - 'mtime' => $info->getMTime(), |
|
| 195 | - ]; |
|
| 196 | - if ($info->isDirectory()) { |
|
| 197 | - $result['type'] = 'dir'; |
|
| 198 | - } else { |
|
| 199 | - $result['type'] = 'file'; |
|
| 200 | - } |
|
| 201 | - return $result; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Rename the files. If the source or the target is the root, the rename won't happen. |
|
| 206 | - * |
|
| 207 | - * @param string $source the old name of the path |
|
| 208 | - * @param string $target the new name of the path |
|
| 209 | - * @return bool true if the rename is successful, false otherwise |
|
| 210 | - */ |
|
| 211 | - public function rename($source, $target) { |
|
| 212 | - if ($this->isRootDir($source) || $this->isRootDir($target)) { |
|
| 213 | - return false; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - $absoluteSource = $this->buildPath($source); |
|
| 217 | - $absoluteTarget = $this->buildPath($target); |
|
| 218 | - try { |
|
| 219 | - $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
| 220 | - } catch (AlreadyExistsException $e) { |
|
| 221 | - $this->remove($target); |
|
| 222 | - $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
| 223 | - } catch (\Exception $e) { |
|
| 224 | - \OC::$server->getLogger()->logException($e, ['level' => ILogger::WARN]); |
|
| 225 | - return false; |
|
| 226 | - } |
|
| 227 | - unset($this->statCache[$absoluteSource], $this->statCache[$absoluteTarget]); |
|
| 228 | - return $result; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - public function stat($path) { |
|
| 232 | - try { |
|
| 233 | - $result = $this->formatInfo($this->getFileInfo($path)); |
|
| 234 | - } catch (ForbiddenException $e) { |
|
| 235 | - return false; |
|
| 236 | - } catch (NotFoundException $e) { |
|
| 237 | - return false; |
|
| 238 | - } |
|
| 239 | - if ($this->remoteIsShare() && $this->isRootDir($path)) { |
|
| 240 | - $result['mtime'] = $this->shareMTime(); |
|
| 241 | - } |
|
| 242 | - return $result; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * get the best guess for the modification time of the share |
|
| 247 | - * |
|
| 248 | - * @return int |
|
| 249 | - */ |
|
| 250 | - private function shareMTime() { |
|
| 251 | - $highestMTime = 0; |
|
| 252 | - $files = $this->share->dir($this->root); |
|
| 253 | - foreach ($files as $fileInfo) { |
|
| 254 | - try { |
|
| 255 | - if ($fileInfo->getMTime() > $highestMTime) { |
|
| 256 | - $highestMTime = $fileInfo->getMTime(); |
|
| 257 | - } |
|
| 258 | - } catch (NotFoundException $e) { |
|
| 259 | - // Ignore this, can happen on unavailable DFS shares |
|
| 260 | - } |
|
| 261 | - } |
|
| 262 | - return $highestMTime; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * Check if the path is our root dir (not the smb one) |
|
| 267 | - * |
|
| 268 | - * @param string $path the path |
|
| 269 | - * @return bool |
|
| 270 | - */ |
|
| 271 | - private function isRootDir($path) { |
|
| 272 | - return $path === '' || $path === '/' || $path === '.'; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Check if our root points to a smb share |
|
| 277 | - * |
|
| 278 | - * @return bool true if our root points to a share false otherwise |
|
| 279 | - */ |
|
| 280 | - private function remoteIsShare() { |
|
| 281 | - return $this->share->getName() && (!$this->root || $this->root === '/'); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * @param string $path |
|
| 286 | - * @return bool |
|
| 287 | - */ |
|
| 288 | - public function unlink($path) { |
|
| 289 | - if ($this->isRootDir($path)) { |
|
| 290 | - return false; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - try { |
|
| 294 | - if ($this->is_dir($path)) { |
|
| 295 | - return $this->rmdir($path); |
|
| 296 | - } else { |
|
| 297 | - $path = $this->buildPath($path); |
|
| 298 | - unset($this->statCache[$path]); |
|
| 299 | - $this->share->del($path); |
|
| 300 | - return true; |
|
| 301 | - } |
|
| 302 | - } catch (NotFoundException $e) { |
|
| 303 | - return false; |
|
| 304 | - } catch (ForbiddenException $e) { |
|
| 305 | - return false; |
|
| 306 | - } catch (ConnectException $e) { |
|
| 307 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * check if a file or folder has been updated since $time |
|
| 313 | - * |
|
| 314 | - * @param string $path |
|
| 315 | - * @param int $time |
|
| 316 | - * @return bool |
|
| 317 | - */ |
|
| 318 | - public function hasUpdated($path, $time) { |
|
| 319 | - if (!$path and $this->root === '/') { |
|
| 320 | - // mtime doesn't work for shares, but giving the nature of the backend, |
|
| 321 | - // doing a full update is still just fast enough |
|
| 322 | - return true; |
|
| 323 | - } else { |
|
| 324 | - $actualTime = $this->filemtime($path); |
|
| 325 | - return $actualTime > $time; |
|
| 326 | - } |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * @param string $path |
|
| 331 | - * @param string $mode |
|
| 332 | - * @return resource|false |
|
| 333 | - */ |
|
| 334 | - public function fopen($path, $mode) { |
|
| 335 | - $fullPath = $this->buildPath($path); |
|
| 336 | - try { |
|
| 337 | - switch ($mode) { |
|
| 338 | - case 'r': |
|
| 339 | - case 'rb': |
|
| 340 | - if (!$this->file_exists($path)) { |
|
| 341 | - return false; |
|
| 342 | - } |
|
| 343 | - return $this->share->read($fullPath); |
|
| 344 | - case 'w': |
|
| 345 | - case 'wb': |
|
| 346 | - $source = $this->share->write($fullPath); |
|
| 347 | - return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) { |
|
| 348 | - unset($this->statCache[$fullPath]); |
|
| 349 | - }); |
|
| 350 | - case 'a': |
|
| 351 | - case 'ab': |
|
| 352 | - case 'r+': |
|
| 353 | - case 'w+': |
|
| 354 | - case 'wb+': |
|
| 355 | - case 'a+': |
|
| 356 | - case 'x': |
|
| 357 | - case 'x+': |
|
| 358 | - case 'c': |
|
| 359 | - case 'c+': |
|
| 360 | - //emulate these |
|
| 361 | - if (strrpos($path, '.') !== false) { |
|
| 362 | - $ext = substr($path, strrpos($path, '.')); |
|
| 363 | - } else { |
|
| 364 | - $ext = ''; |
|
| 365 | - } |
|
| 366 | - if ($this->file_exists($path)) { |
|
| 367 | - if (!$this->isUpdatable($path)) { |
|
| 368 | - return false; |
|
| 369 | - } |
|
| 370 | - $tmpFile = $this->getCachedFile($path); |
|
| 371 | - } else { |
|
| 372 | - if (!$this->isCreatable(dirname($path))) { |
|
| 373 | - return false; |
|
| 374 | - } |
|
| 375 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
| 376 | - } |
|
| 377 | - $source = fopen($tmpFile, $mode); |
|
| 378 | - $share = $this->share; |
|
| 379 | - return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) { |
|
| 380 | - unset($this->statCache[$fullPath]); |
|
| 381 | - $share->put($tmpFile, $fullPath); |
|
| 382 | - unlink($tmpFile); |
|
| 383 | - }); |
|
| 384 | - } |
|
| 385 | - return false; |
|
| 386 | - } catch (NotFoundException $e) { |
|
| 387 | - return false; |
|
| 388 | - } catch (ForbiddenException $e) { |
|
| 389 | - return false; |
|
| 390 | - } catch (ConnectException $e) { |
|
| 391 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 392 | - } |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - public function rmdir($path) { |
|
| 396 | - if ($this->isRootDir($path)) { |
|
| 397 | - return false; |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - try { |
|
| 401 | - $this->statCache = array(); |
|
| 402 | - $content = $this->share->dir($this->buildPath($path)); |
|
| 403 | - foreach ($content as $file) { |
|
| 404 | - if ($file->isDirectory()) { |
|
| 405 | - $this->rmdir($path . '/' . $file->getName()); |
|
| 406 | - } else { |
|
| 407 | - $this->share->del($file->getPath()); |
|
| 408 | - } |
|
| 409 | - } |
|
| 410 | - $this->share->rmdir($this->buildPath($path)); |
|
| 411 | - return true; |
|
| 412 | - } catch (NotFoundException $e) { |
|
| 413 | - return false; |
|
| 414 | - } catch (ForbiddenException $e) { |
|
| 415 | - return false; |
|
| 416 | - } catch (ConnectException $e) { |
|
| 417 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 418 | - } |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - public function touch($path, $time = null) { |
|
| 422 | - try { |
|
| 423 | - if (!$this->file_exists($path)) { |
|
| 424 | - $fh = $this->share->write($this->buildPath($path)); |
|
| 425 | - fclose($fh); |
|
| 426 | - return true; |
|
| 427 | - } |
|
| 428 | - return false; |
|
| 429 | - } catch (ConnectException $e) { |
|
| 430 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 431 | - } |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - public function opendir($path) { |
|
| 435 | - try { |
|
| 436 | - $files = $this->getFolderContents($path); |
|
| 437 | - } catch (NotFoundException $e) { |
|
| 438 | - return false; |
|
| 439 | - } catch (ForbiddenException $e) { |
|
| 440 | - return false; |
|
| 441 | - } |
|
| 442 | - $names = array_map(function ($info) { |
|
| 443 | - /** @var \Icewind\SMB\IFileInfo $info */ |
|
| 444 | - return $info->getName(); |
|
| 445 | - }, $files); |
|
| 446 | - return IteratorDirectory::wrap($names); |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - public function filetype($path) { |
|
| 450 | - try { |
|
| 451 | - return $this->getFileInfo($path)->isDirectory() ? 'dir' : 'file'; |
|
| 452 | - } catch (NotFoundException $e) { |
|
| 453 | - return false; |
|
| 454 | - } catch (ForbiddenException $e) { |
|
| 455 | - return false; |
|
| 456 | - } |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - public function mkdir($path) { |
|
| 460 | - $path = $this->buildPath($path); |
|
| 461 | - try { |
|
| 462 | - $this->share->mkdir($path); |
|
| 463 | - return true; |
|
| 464 | - } catch (ConnectException $e) { |
|
| 465 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 466 | - } catch (Exception $e) { |
|
| 467 | - return false; |
|
| 468 | - } |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - public function file_exists($path) { |
|
| 472 | - try { |
|
| 473 | - $this->getFileInfo($path); |
|
| 474 | - return true; |
|
| 475 | - } catch (NotFoundException $e) { |
|
| 476 | - return false; |
|
| 477 | - } catch (ForbiddenException $e) { |
|
| 478 | - return false; |
|
| 479 | - } catch (ConnectException $e) { |
|
| 480 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 481 | - } |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - public function isReadable($path) { |
|
| 485 | - try { |
|
| 486 | - $info = $this->getFileInfo($path); |
|
| 487 | - return !$info->isHidden(); |
|
| 488 | - } catch (NotFoundException $e) { |
|
| 489 | - return false; |
|
| 490 | - } catch (ForbiddenException $e) { |
|
| 491 | - return false; |
|
| 492 | - } |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - public function isUpdatable($path) { |
|
| 496 | - try { |
|
| 497 | - $info = $this->getFileInfo($path); |
|
| 498 | - // following windows behaviour for read-only folders: they can be written into |
|
| 499 | - // (https://support.microsoft.com/en-us/kb/326549 - "cause" section) |
|
| 500 | - return !$info->isHidden() && (!$info->isReadOnly() || $this->is_dir($path)); |
|
| 501 | - } catch (NotFoundException $e) { |
|
| 502 | - return false; |
|
| 503 | - } catch (ForbiddenException $e) { |
|
| 504 | - return false; |
|
| 505 | - } |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - public function isDeletable($path) { |
|
| 509 | - try { |
|
| 510 | - $info = $this->getFileInfo($path); |
|
| 511 | - return !$info->isHidden() && !$info->isReadOnly(); |
|
| 512 | - } catch (NotFoundException $e) { |
|
| 513 | - return false; |
|
| 514 | - } catch (ForbiddenException $e) { |
|
| 515 | - return false; |
|
| 516 | - } |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - /** |
|
| 520 | - * check if smbclient is installed |
|
| 521 | - */ |
|
| 522 | - public static function checkDependencies() { |
|
| 523 | - return ( |
|
| 524 | - (bool)\OC_Helper::findBinaryPath('smbclient') |
|
| 525 | - || NativeServer::available(new System()) |
|
| 526 | - ) ? true : ['smbclient']; |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * Test a storage for availability |
|
| 531 | - * |
|
| 532 | - * @return bool |
|
| 533 | - */ |
|
| 534 | - public function test() { |
|
| 535 | - try { |
|
| 536 | - return parent::test(); |
|
| 537 | - } catch (Exception $e) { |
|
| 538 | - return false; |
|
| 539 | - } |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - public function listen($path, callable $callback) { |
|
| 543 | - $this->notify($path)->listen(function (IChange $change) use ($callback) { |
|
| 544 | - if ($change instanceof IRenameChange) { |
|
| 545 | - return $callback($change->getType(), $change->getPath(), $change->getTargetPath()); |
|
| 546 | - } else { |
|
| 547 | - return $callback($change->getType(), $change->getPath()); |
|
| 548 | - } |
|
| 549 | - }); |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - public function notify($path) { |
|
| 553 | - $path = '/' . ltrim($path, '/'); |
|
| 554 | - $shareNotifyHandler = $this->share->notify($this->buildPath($path)); |
|
| 555 | - return new SMBNotifyHandler($shareNotifyHandler, $this->root); |
|
| 556 | - } |
|
| 60 | + /** |
|
| 61 | + * @var \Icewind\SMB\IServer |
|
| 62 | + */ |
|
| 63 | + protected $server; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @var \Icewind\SMB\IShare |
|
| 67 | + */ |
|
| 68 | + protected $share; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @var string |
|
| 72 | + */ |
|
| 73 | + protected $root; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @var \Icewind\SMB\IFileInfo[] |
|
| 77 | + */ |
|
| 78 | + protected $statCache; |
|
| 79 | + |
|
| 80 | + public function __construct($params) { |
|
| 81 | + if (!isset($params['host'])) { |
|
| 82 | + throw new \Exception('Invalid configuration, no host provided'); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + if (isset($params['auth'])) { |
|
| 86 | + $auth = $params['auth']; |
|
| 87 | + } else if (isset($params['user']) && isset($params['password']) && isset($params['share'])) { |
|
| 88 | + list($workgroup, $user) = $this->splitUser($params['user']); |
|
| 89 | + $auth = new BasicAuth($user, $workgroup, $params['password']); |
|
| 90 | + } else { |
|
| 91 | + throw new \Exception('Invalid configuration, no credentials provided'); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + $serverFactory = new ServerFactory(); |
|
| 95 | + $this->server = $serverFactory->createServer($params['host'], $auth); |
|
| 96 | + $this->share = $this->server->getShare(trim($params['share'], '/')); |
|
| 97 | + |
|
| 98 | + $this->root = $params['root'] ?? '/'; |
|
| 99 | + $this->root = '/' . ltrim($this->root, '/'); |
|
| 100 | + $this->root = rtrim($this->root, '/') . '/'; |
|
| 101 | + |
|
| 102 | + $this->statCache = new CappedMemoryCache(); |
|
| 103 | + parent::__construct($params); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + private function splitUser($user) { |
|
| 107 | + if (strpos($user, '/')) { |
|
| 108 | + return explode('/', $user, 2); |
|
| 109 | + } elseif (strpos($user, '\\')) { |
|
| 110 | + return explode('\\', $user); |
|
| 111 | + } else { |
|
| 112 | + return [null, $user]; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @return string |
|
| 118 | + */ |
|
| 119 | + public function getId() { |
|
| 120 | + // FIXME: double slash to keep compatible with the old storage ids, |
|
| 121 | + // failure to do so will lead to creation of a new storage id and |
|
| 122 | + // loss of shares from the storage |
|
| 123 | + return 'smb::' . $this->server->getAuth()->getUsername() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @param string $path |
|
| 128 | + * @return string |
|
| 129 | + */ |
|
| 130 | + protected function buildPath($path) { |
|
| 131 | + return Filesystem::normalizePath($this->root . '/' . $path, true, false, true); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + protected function relativePath($fullPath) { |
|
| 135 | + if ($fullPath === $this->root) { |
|
| 136 | + return ''; |
|
| 137 | + } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
|
| 138 | + return substr($fullPath, strlen($this->root)); |
|
| 139 | + } else { |
|
| 140 | + return null; |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @param string $path |
|
| 146 | + * @return \Icewind\SMB\IFileInfo |
|
| 147 | + * @throws StorageNotAvailableException |
|
| 148 | + */ |
|
| 149 | + protected function getFileInfo($path) { |
|
| 150 | + try { |
|
| 151 | + $path = $this->buildPath($path); |
|
| 152 | + if (!isset($this->statCache[$path])) { |
|
| 153 | + $this->statCache[$path] = $this->share->stat($path); |
|
| 154 | + } |
|
| 155 | + return $this->statCache[$path]; |
|
| 156 | + } catch (ConnectException $e) { |
|
| 157 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @param string $path |
|
| 163 | + * @return \Icewind\SMB\IFileInfo[] |
|
| 164 | + * @throws StorageNotAvailableException |
|
| 165 | + */ |
|
| 166 | + protected function getFolderContents($path) { |
|
| 167 | + try { |
|
| 168 | + $path = $this->buildPath($path); |
|
| 169 | + $files = $this->share->dir($path); |
|
| 170 | + foreach ($files as $file) { |
|
| 171 | + $this->statCache[$path . '/' . $file->getName()] = $file; |
|
| 172 | + } |
|
| 173 | + return array_filter($files, function (IFileInfo $file) { |
|
| 174 | + try { |
|
| 175 | + return !$file->isHidden(); |
|
| 176 | + } catch (ForbiddenException $e) { |
|
| 177 | + return false; |
|
| 178 | + } catch (NotFoundException $e) { |
|
| 179 | + return false; |
|
| 180 | + } |
|
| 181 | + }); |
|
| 182 | + } catch (ConnectException $e) { |
|
| 183 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * @param \Icewind\SMB\IFileInfo $info |
|
| 189 | + * @return array |
|
| 190 | + */ |
|
| 191 | + protected function formatInfo($info) { |
|
| 192 | + $result = [ |
|
| 193 | + 'size' => $info->getSize(), |
|
| 194 | + 'mtime' => $info->getMTime(), |
|
| 195 | + ]; |
|
| 196 | + if ($info->isDirectory()) { |
|
| 197 | + $result['type'] = 'dir'; |
|
| 198 | + } else { |
|
| 199 | + $result['type'] = 'file'; |
|
| 200 | + } |
|
| 201 | + return $result; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Rename the files. If the source or the target is the root, the rename won't happen. |
|
| 206 | + * |
|
| 207 | + * @param string $source the old name of the path |
|
| 208 | + * @param string $target the new name of the path |
|
| 209 | + * @return bool true if the rename is successful, false otherwise |
|
| 210 | + */ |
|
| 211 | + public function rename($source, $target) { |
|
| 212 | + if ($this->isRootDir($source) || $this->isRootDir($target)) { |
|
| 213 | + return false; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + $absoluteSource = $this->buildPath($source); |
|
| 217 | + $absoluteTarget = $this->buildPath($target); |
|
| 218 | + try { |
|
| 219 | + $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
| 220 | + } catch (AlreadyExistsException $e) { |
|
| 221 | + $this->remove($target); |
|
| 222 | + $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
| 223 | + } catch (\Exception $e) { |
|
| 224 | + \OC::$server->getLogger()->logException($e, ['level' => ILogger::WARN]); |
|
| 225 | + return false; |
|
| 226 | + } |
|
| 227 | + unset($this->statCache[$absoluteSource], $this->statCache[$absoluteTarget]); |
|
| 228 | + return $result; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + public function stat($path) { |
|
| 232 | + try { |
|
| 233 | + $result = $this->formatInfo($this->getFileInfo($path)); |
|
| 234 | + } catch (ForbiddenException $e) { |
|
| 235 | + return false; |
|
| 236 | + } catch (NotFoundException $e) { |
|
| 237 | + return false; |
|
| 238 | + } |
|
| 239 | + if ($this->remoteIsShare() && $this->isRootDir($path)) { |
|
| 240 | + $result['mtime'] = $this->shareMTime(); |
|
| 241 | + } |
|
| 242 | + return $result; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * get the best guess for the modification time of the share |
|
| 247 | + * |
|
| 248 | + * @return int |
|
| 249 | + */ |
|
| 250 | + private function shareMTime() { |
|
| 251 | + $highestMTime = 0; |
|
| 252 | + $files = $this->share->dir($this->root); |
|
| 253 | + foreach ($files as $fileInfo) { |
|
| 254 | + try { |
|
| 255 | + if ($fileInfo->getMTime() > $highestMTime) { |
|
| 256 | + $highestMTime = $fileInfo->getMTime(); |
|
| 257 | + } |
|
| 258 | + } catch (NotFoundException $e) { |
|
| 259 | + // Ignore this, can happen on unavailable DFS shares |
|
| 260 | + } |
|
| 261 | + } |
|
| 262 | + return $highestMTime; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * Check if the path is our root dir (not the smb one) |
|
| 267 | + * |
|
| 268 | + * @param string $path the path |
|
| 269 | + * @return bool |
|
| 270 | + */ |
|
| 271 | + private function isRootDir($path) { |
|
| 272 | + return $path === '' || $path === '/' || $path === '.'; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Check if our root points to a smb share |
|
| 277 | + * |
|
| 278 | + * @return bool true if our root points to a share false otherwise |
|
| 279 | + */ |
|
| 280 | + private function remoteIsShare() { |
|
| 281 | + return $this->share->getName() && (!$this->root || $this->root === '/'); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * @param string $path |
|
| 286 | + * @return bool |
|
| 287 | + */ |
|
| 288 | + public function unlink($path) { |
|
| 289 | + if ($this->isRootDir($path)) { |
|
| 290 | + return false; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + try { |
|
| 294 | + if ($this->is_dir($path)) { |
|
| 295 | + return $this->rmdir($path); |
|
| 296 | + } else { |
|
| 297 | + $path = $this->buildPath($path); |
|
| 298 | + unset($this->statCache[$path]); |
|
| 299 | + $this->share->del($path); |
|
| 300 | + return true; |
|
| 301 | + } |
|
| 302 | + } catch (NotFoundException $e) { |
|
| 303 | + return false; |
|
| 304 | + } catch (ForbiddenException $e) { |
|
| 305 | + return false; |
|
| 306 | + } catch (ConnectException $e) { |
|
| 307 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * check if a file or folder has been updated since $time |
|
| 313 | + * |
|
| 314 | + * @param string $path |
|
| 315 | + * @param int $time |
|
| 316 | + * @return bool |
|
| 317 | + */ |
|
| 318 | + public function hasUpdated($path, $time) { |
|
| 319 | + if (!$path and $this->root === '/') { |
|
| 320 | + // mtime doesn't work for shares, but giving the nature of the backend, |
|
| 321 | + // doing a full update is still just fast enough |
|
| 322 | + return true; |
|
| 323 | + } else { |
|
| 324 | + $actualTime = $this->filemtime($path); |
|
| 325 | + return $actualTime > $time; |
|
| 326 | + } |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * @param string $path |
|
| 331 | + * @param string $mode |
|
| 332 | + * @return resource|false |
|
| 333 | + */ |
|
| 334 | + public function fopen($path, $mode) { |
|
| 335 | + $fullPath = $this->buildPath($path); |
|
| 336 | + try { |
|
| 337 | + switch ($mode) { |
|
| 338 | + case 'r': |
|
| 339 | + case 'rb': |
|
| 340 | + if (!$this->file_exists($path)) { |
|
| 341 | + return false; |
|
| 342 | + } |
|
| 343 | + return $this->share->read($fullPath); |
|
| 344 | + case 'w': |
|
| 345 | + case 'wb': |
|
| 346 | + $source = $this->share->write($fullPath); |
|
| 347 | + return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) { |
|
| 348 | + unset($this->statCache[$fullPath]); |
|
| 349 | + }); |
|
| 350 | + case 'a': |
|
| 351 | + case 'ab': |
|
| 352 | + case 'r+': |
|
| 353 | + case 'w+': |
|
| 354 | + case 'wb+': |
|
| 355 | + case 'a+': |
|
| 356 | + case 'x': |
|
| 357 | + case 'x+': |
|
| 358 | + case 'c': |
|
| 359 | + case 'c+': |
|
| 360 | + //emulate these |
|
| 361 | + if (strrpos($path, '.') !== false) { |
|
| 362 | + $ext = substr($path, strrpos($path, '.')); |
|
| 363 | + } else { |
|
| 364 | + $ext = ''; |
|
| 365 | + } |
|
| 366 | + if ($this->file_exists($path)) { |
|
| 367 | + if (!$this->isUpdatable($path)) { |
|
| 368 | + return false; |
|
| 369 | + } |
|
| 370 | + $tmpFile = $this->getCachedFile($path); |
|
| 371 | + } else { |
|
| 372 | + if (!$this->isCreatable(dirname($path))) { |
|
| 373 | + return false; |
|
| 374 | + } |
|
| 375 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
| 376 | + } |
|
| 377 | + $source = fopen($tmpFile, $mode); |
|
| 378 | + $share = $this->share; |
|
| 379 | + return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) { |
|
| 380 | + unset($this->statCache[$fullPath]); |
|
| 381 | + $share->put($tmpFile, $fullPath); |
|
| 382 | + unlink($tmpFile); |
|
| 383 | + }); |
|
| 384 | + } |
|
| 385 | + return false; |
|
| 386 | + } catch (NotFoundException $e) { |
|
| 387 | + return false; |
|
| 388 | + } catch (ForbiddenException $e) { |
|
| 389 | + return false; |
|
| 390 | + } catch (ConnectException $e) { |
|
| 391 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 392 | + } |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + public function rmdir($path) { |
|
| 396 | + if ($this->isRootDir($path)) { |
|
| 397 | + return false; |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + try { |
|
| 401 | + $this->statCache = array(); |
|
| 402 | + $content = $this->share->dir($this->buildPath($path)); |
|
| 403 | + foreach ($content as $file) { |
|
| 404 | + if ($file->isDirectory()) { |
|
| 405 | + $this->rmdir($path . '/' . $file->getName()); |
|
| 406 | + } else { |
|
| 407 | + $this->share->del($file->getPath()); |
|
| 408 | + } |
|
| 409 | + } |
|
| 410 | + $this->share->rmdir($this->buildPath($path)); |
|
| 411 | + return true; |
|
| 412 | + } catch (NotFoundException $e) { |
|
| 413 | + return false; |
|
| 414 | + } catch (ForbiddenException $e) { |
|
| 415 | + return false; |
|
| 416 | + } catch (ConnectException $e) { |
|
| 417 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + public function touch($path, $time = null) { |
|
| 422 | + try { |
|
| 423 | + if (!$this->file_exists($path)) { |
|
| 424 | + $fh = $this->share->write($this->buildPath($path)); |
|
| 425 | + fclose($fh); |
|
| 426 | + return true; |
|
| 427 | + } |
|
| 428 | + return false; |
|
| 429 | + } catch (ConnectException $e) { |
|
| 430 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 431 | + } |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + public function opendir($path) { |
|
| 435 | + try { |
|
| 436 | + $files = $this->getFolderContents($path); |
|
| 437 | + } catch (NotFoundException $e) { |
|
| 438 | + return false; |
|
| 439 | + } catch (ForbiddenException $e) { |
|
| 440 | + return false; |
|
| 441 | + } |
|
| 442 | + $names = array_map(function ($info) { |
|
| 443 | + /** @var \Icewind\SMB\IFileInfo $info */ |
|
| 444 | + return $info->getName(); |
|
| 445 | + }, $files); |
|
| 446 | + return IteratorDirectory::wrap($names); |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + public function filetype($path) { |
|
| 450 | + try { |
|
| 451 | + return $this->getFileInfo($path)->isDirectory() ? 'dir' : 'file'; |
|
| 452 | + } catch (NotFoundException $e) { |
|
| 453 | + return false; |
|
| 454 | + } catch (ForbiddenException $e) { |
|
| 455 | + return false; |
|
| 456 | + } |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + public function mkdir($path) { |
|
| 460 | + $path = $this->buildPath($path); |
|
| 461 | + try { |
|
| 462 | + $this->share->mkdir($path); |
|
| 463 | + return true; |
|
| 464 | + } catch (ConnectException $e) { |
|
| 465 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 466 | + } catch (Exception $e) { |
|
| 467 | + return false; |
|
| 468 | + } |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + public function file_exists($path) { |
|
| 472 | + try { |
|
| 473 | + $this->getFileInfo($path); |
|
| 474 | + return true; |
|
| 475 | + } catch (NotFoundException $e) { |
|
| 476 | + return false; |
|
| 477 | + } catch (ForbiddenException $e) { |
|
| 478 | + return false; |
|
| 479 | + } catch (ConnectException $e) { |
|
| 480 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 481 | + } |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + public function isReadable($path) { |
|
| 485 | + try { |
|
| 486 | + $info = $this->getFileInfo($path); |
|
| 487 | + return !$info->isHidden(); |
|
| 488 | + } catch (NotFoundException $e) { |
|
| 489 | + return false; |
|
| 490 | + } catch (ForbiddenException $e) { |
|
| 491 | + return false; |
|
| 492 | + } |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + public function isUpdatable($path) { |
|
| 496 | + try { |
|
| 497 | + $info = $this->getFileInfo($path); |
|
| 498 | + // following windows behaviour for read-only folders: they can be written into |
|
| 499 | + // (https://support.microsoft.com/en-us/kb/326549 - "cause" section) |
|
| 500 | + return !$info->isHidden() && (!$info->isReadOnly() || $this->is_dir($path)); |
|
| 501 | + } catch (NotFoundException $e) { |
|
| 502 | + return false; |
|
| 503 | + } catch (ForbiddenException $e) { |
|
| 504 | + return false; |
|
| 505 | + } |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + public function isDeletable($path) { |
|
| 509 | + try { |
|
| 510 | + $info = $this->getFileInfo($path); |
|
| 511 | + return !$info->isHidden() && !$info->isReadOnly(); |
|
| 512 | + } catch (NotFoundException $e) { |
|
| 513 | + return false; |
|
| 514 | + } catch (ForbiddenException $e) { |
|
| 515 | + return false; |
|
| 516 | + } |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + /** |
|
| 520 | + * check if smbclient is installed |
|
| 521 | + */ |
|
| 522 | + public static function checkDependencies() { |
|
| 523 | + return ( |
|
| 524 | + (bool)\OC_Helper::findBinaryPath('smbclient') |
|
| 525 | + || NativeServer::available(new System()) |
|
| 526 | + ) ? true : ['smbclient']; |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * Test a storage for availability |
|
| 531 | + * |
|
| 532 | + * @return bool |
|
| 533 | + */ |
|
| 534 | + public function test() { |
|
| 535 | + try { |
|
| 536 | + return parent::test(); |
|
| 537 | + } catch (Exception $e) { |
|
| 538 | + return false; |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + public function listen($path, callable $callback) { |
|
| 543 | + $this->notify($path)->listen(function (IChange $change) use ($callback) { |
|
| 544 | + if ($change instanceof IRenameChange) { |
|
| 545 | + return $callback($change->getType(), $change->getPath(), $change->getTargetPath()); |
|
| 546 | + } else { |
|
| 547 | + return $callback($change->getType(), $change->getPath()); |
|
| 548 | + } |
|
| 549 | + }); |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + public function notify($path) { |
|
| 553 | + $path = '/' . ltrim($path, '/'); |
|
| 554 | + $shareNotifyHandler = $this->share->notify($this->buildPath($path)); |
|
| 555 | + return new SMBNotifyHandler($shareNotifyHandler, $this->root); |
|
| 556 | + } |
|
| 557 | 557 | } |
@@ -25,10 +25,10 @@ |
||
| 25 | 25 | use OCP\IL10N; |
| 26 | 26 | |
| 27 | 27 | class KerberosAuth extends AuthMechanism { |
| 28 | - public function __construct(IL10N $l) { |
|
| 29 | - $this |
|
| 30 | - ->setIdentifier('smb::kerberos') |
|
| 31 | - ->setScheme(self::SCHEME_SMB) |
|
| 32 | - ->setText($l->t('Kerberos ticket')); |
|
| 33 | - } |
|
| 28 | + public function __construct(IL10N $l) { |
|
| 29 | + $this |
|
| 30 | + ->setIdentifier('smb::kerberos') |
|
| 31 | + ->setScheme(self::SCHEME_SMB) |
|
| 32 | + ->setText($l->t('Kerberos ticket')); |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -52,72 +52,72 @@ |
||
| 52 | 52 | */ |
| 53 | 53 | class AuthMechanism implements \JsonSerializable { |
| 54 | 54 | |
| 55 | - /** Standard authentication schemes */ |
|
| 56 | - const SCHEME_NULL = 'null'; |
|
| 57 | - const SCHEME_BUILTIN = 'builtin'; |
|
| 58 | - const SCHEME_PASSWORD = 'password'; |
|
| 59 | - const SCHEME_OAUTH1 = 'oauth1'; |
|
| 60 | - const SCHEME_OAUTH2 = 'oauth2'; |
|
| 61 | - const SCHEME_PUBLICKEY = 'publickey'; |
|
| 62 | - const SCHEME_OPENSTACK = 'openstack'; |
|
| 63 | - const SCHEME_SMB = 'smb'; |
|
| 55 | + /** Standard authentication schemes */ |
|
| 56 | + const SCHEME_NULL = 'null'; |
|
| 57 | + const SCHEME_BUILTIN = 'builtin'; |
|
| 58 | + const SCHEME_PASSWORD = 'password'; |
|
| 59 | + const SCHEME_OAUTH1 = 'oauth1'; |
|
| 60 | + const SCHEME_OAUTH2 = 'oauth2'; |
|
| 61 | + const SCHEME_PUBLICKEY = 'publickey'; |
|
| 62 | + const SCHEME_OPENSTACK = 'openstack'; |
|
| 63 | + const SCHEME_SMB = 'smb'; |
|
| 64 | 64 | |
| 65 | - use VisibilityTrait; |
|
| 66 | - use FrontendDefinitionTrait; |
|
| 67 | - use StorageModifierTrait; |
|
| 68 | - use IdentifierTrait; |
|
| 65 | + use VisibilityTrait; |
|
| 66 | + use FrontendDefinitionTrait; |
|
| 67 | + use StorageModifierTrait; |
|
| 68 | + use IdentifierTrait; |
|
| 69 | 69 | |
| 70 | - /** @var string */ |
|
| 71 | - protected $scheme; |
|
| 70 | + /** @var string */ |
|
| 71 | + protected $scheme; |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Get the authentication scheme implemented |
|
| 75 | - * See self::SCHEME_* constants |
|
| 76 | - * |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - public function getScheme() { |
|
| 80 | - return $this->scheme; |
|
| 81 | - } |
|
| 73 | + /** |
|
| 74 | + * Get the authentication scheme implemented |
|
| 75 | + * See self::SCHEME_* constants |
|
| 76 | + * |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + public function getScheme() { |
|
| 80 | + return $this->scheme; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * @param string $scheme |
|
| 85 | - * @return $this |
|
| 86 | - */ |
|
| 87 | - public function setScheme($scheme) { |
|
| 88 | - $this->scheme = $scheme; |
|
| 89 | - return $this; |
|
| 90 | - } |
|
| 83 | + /** |
|
| 84 | + * @param string $scheme |
|
| 85 | + * @return $this |
|
| 86 | + */ |
|
| 87 | + public function setScheme($scheme) { |
|
| 88 | + $this->scheme = $scheme; |
|
| 89 | + return $this; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Serialize into JSON for client-side JS |
|
| 94 | - * |
|
| 95 | - * @return array |
|
| 96 | - */ |
|
| 97 | - public function jsonSerialize() { |
|
| 98 | - $data = $this->jsonSerializeDefinition(); |
|
| 99 | - $data += $this->jsonSerializeIdentifier(); |
|
| 92 | + /** |
|
| 93 | + * Serialize into JSON for client-side JS |
|
| 94 | + * |
|
| 95 | + * @return array |
|
| 96 | + */ |
|
| 97 | + public function jsonSerialize() { |
|
| 98 | + $data = $this->jsonSerializeDefinition(); |
|
| 99 | + $data += $this->jsonSerializeIdentifier(); |
|
| 100 | 100 | |
| 101 | - $data['scheme'] = $this->getScheme(); |
|
| 102 | - $data['visibility'] = $this->getVisibility(); |
|
| 101 | + $data['scheme'] = $this->getScheme(); |
|
| 102 | + $data['visibility'] = $this->getVisibility(); |
|
| 103 | 103 | |
| 104 | - return $data; |
|
| 105 | - } |
|
| 104 | + return $data; |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * Check if parameters are satisfied in a StorageConfig |
|
| 109 | - * |
|
| 110 | - * @param StorageConfig $storage |
|
| 111 | - * @return bool |
|
| 112 | - */ |
|
| 113 | - public function validateStorage(StorageConfig $storage) { |
|
| 114 | - // does the backend actually support this scheme |
|
| 115 | - $supportedSchemes = $storage->getBackend()->getAuthSchemes(); |
|
| 116 | - if (!isset($supportedSchemes[$this->getScheme()])) { |
|
| 117 | - return false; |
|
| 118 | - } |
|
| 107 | + /** |
|
| 108 | + * Check if parameters are satisfied in a StorageConfig |
|
| 109 | + * |
|
| 110 | + * @param StorageConfig $storage |
|
| 111 | + * @return bool |
|
| 112 | + */ |
|
| 113 | + public function validateStorage(StorageConfig $storage) { |
|
| 114 | + // does the backend actually support this scheme |
|
| 115 | + $supportedSchemes = $storage->getBackend()->getAuthSchemes(); |
|
| 116 | + if (!isset($supportedSchemes[$this->getScheme()])) { |
|
| 117 | + return false; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - return $this->validateStorageDefinition($storage); |
|
| 121 | - } |
|
| 120 | + return $this->validateStorageDefinition($storage); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | 123 | } |
@@ -67,90 +67,90 @@ |
||
| 67 | 67 | */ |
| 68 | 68 | class Application extends App implements IBackendProvider, IAuthMechanismProvider { |
| 69 | 69 | |
| 70 | - public function __construct(array $urlParams = array()) { |
|
| 71 | - parent::__construct('files_external', $urlParams); |
|
| 72 | - |
|
| 73 | - $container = $this->getContainer(); |
|
| 74 | - |
|
| 75 | - $container->registerService(IUserMountCache::class, function (IAppContainer $c) { |
|
| 76 | - return $c->getServer()->query('UserMountCache'); |
|
| 77 | - }); |
|
| 78 | - |
|
| 79 | - $backendService = $container->query(BackendService::class); |
|
| 80 | - $backendService->registerBackendProvider($this); |
|
| 81 | - $backendService->registerAuthMechanismProvider($this); |
|
| 82 | - |
|
| 83 | - // force-load auth mechanisms since some will register hooks |
|
| 84 | - // TODO: obsolete these and use the TokenProvider to get the user's password from the session |
|
| 85 | - $this->getAuthMechanisms(); |
|
| 86 | - |
|
| 87 | - // app developers: do NOT depend on this! it will disappear with oC 9.0! |
|
| 88 | - \OC::$server->getEventDispatcher()->dispatch( |
|
| 89 | - 'OCA\\Files_External::loadAdditionalBackends' |
|
| 90 | - ); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @{inheritdoc} |
|
| 95 | - */ |
|
| 96 | - public function getBackends() { |
|
| 97 | - $container = $this->getContainer(); |
|
| 98 | - |
|
| 99 | - $backends = [ |
|
| 100 | - $container->query(Local::class), |
|
| 101 | - $container->query(FTP::class), |
|
| 102 | - $container->query(DAV::class), |
|
| 103 | - $container->query(OwnCloud::class), |
|
| 104 | - $container->query(SFTP::class), |
|
| 105 | - $container->query(AmazonS3::class), |
|
| 106 | - $container->query(Swift::class), |
|
| 107 | - $container->query(SFTP_Key::class), |
|
| 108 | - $container->query(SMB::class), |
|
| 109 | - $container->query(SMB_OC::class), |
|
| 110 | - ]; |
|
| 111 | - |
|
| 112 | - return $backends; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @{inheritdoc} |
|
| 117 | - */ |
|
| 118 | - public function getAuthMechanisms() { |
|
| 119 | - $container = $this->getContainer(); |
|
| 120 | - |
|
| 121 | - return [ |
|
| 122 | - // AuthMechanism::SCHEME_NULL mechanism |
|
| 123 | - $container->query(NullMechanism::class), |
|
| 124 | - |
|
| 125 | - // AuthMechanism::SCHEME_BUILTIN mechanism |
|
| 126 | - $container->query(Builtin::class), |
|
| 127 | - |
|
| 128 | - // AuthMechanism::SCHEME_PASSWORD mechanisms |
|
| 129 | - $container->query(Password::class), |
|
| 130 | - $container->query(SessionCredentials::class), |
|
| 131 | - $container->query(LoginCredentials::class), |
|
| 132 | - $container->query(UserProvided::class), |
|
| 133 | - $container->query(GlobalAuth::class), |
|
| 134 | - |
|
| 135 | - // AuthMechanism::SCHEME_OAUTH1 mechanisms |
|
| 136 | - $container->query(OAuth1::class), |
|
| 137 | - |
|
| 138 | - // AuthMechanism::SCHEME_OAUTH2 mechanisms |
|
| 139 | - $container->query(OAuth2::class), |
|
| 140 | - |
|
| 141 | - // AuthMechanism::SCHEME_PUBLICKEY mechanisms |
|
| 142 | - $container->query(RSA::class), |
|
| 143 | - $container->query(RSAPrivateKey::class), |
|
| 144 | - |
|
| 145 | - // AuthMechanism::SCHEME_OPENSTACK mechanisms |
|
| 146 | - $container->query(OpenStackV2::class), |
|
| 147 | - $container->query(OpenStackV3::class), |
|
| 148 | - $container->query(Rackspace::class), |
|
| 149 | - |
|
| 150 | - // Specialized mechanisms |
|
| 151 | - $container->query(AccessKey::class), |
|
| 152 | - $container->query(KerberosAuth::class), |
|
| 153 | - ]; |
|
| 154 | - } |
|
| 70 | + public function __construct(array $urlParams = array()) { |
|
| 71 | + parent::__construct('files_external', $urlParams); |
|
| 72 | + |
|
| 73 | + $container = $this->getContainer(); |
|
| 74 | + |
|
| 75 | + $container->registerService(IUserMountCache::class, function (IAppContainer $c) { |
|
| 76 | + return $c->getServer()->query('UserMountCache'); |
|
| 77 | + }); |
|
| 78 | + |
|
| 79 | + $backendService = $container->query(BackendService::class); |
|
| 80 | + $backendService->registerBackendProvider($this); |
|
| 81 | + $backendService->registerAuthMechanismProvider($this); |
|
| 82 | + |
|
| 83 | + // force-load auth mechanisms since some will register hooks |
|
| 84 | + // TODO: obsolete these and use the TokenProvider to get the user's password from the session |
|
| 85 | + $this->getAuthMechanisms(); |
|
| 86 | + |
|
| 87 | + // app developers: do NOT depend on this! it will disappear with oC 9.0! |
|
| 88 | + \OC::$server->getEventDispatcher()->dispatch( |
|
| 89 | + 'OCA\\Files_External::loadAdditionalBackends' |
|
| 90 | + ); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @{inheritdoc} |
|
| 95 | + */ |
|
| 96 | + public function getBackends() { |
|
| 97 | + $container = $this->getContainer(); |
|
| 98 | + |
|
| 99 | + $backends = [ |
|
| 100 | + $container->query(Local::class), |
|
| 101 | + $container->query(FTP::class), |
|
| 102 | + $container->query(DAV::class), |
|
| 103 | + $container->query(OwnCloud::class), |
|
| 104 | + $container->query(SFTP::class), |
|
| 105 | + $container->query(AmazonS3::class), |
|
| 106 | + $container->query(Swift::class), |
|
| 107 | + $container->query(SFTP_Key::class), |
|
| 108 | + $container->query(SMB::class), |
|
| 109 | + $container->query(SMB_OC::class), |
|
| 110 | + ]; |
|
| 111 | + |
|
| 112 | + return $backends; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @{inheritdoc} |
|
| 117 | + */ |
|
| 118 | + public function getAuthMechanisms() { |
|
| 119 | + $container = $this->getContainer(); |
|
| 120 | + |
|
| 121 | + return [ |
|
| 122 | + // AuthMechanism::SCHEME_NULL mechanism |
|
| 123 | + $container->query(NullMechanism::class), |
|
| 124 | + |
|
| 125 | + // AuthMechanism::SCHEME_BUILTIN mechanism |
|
| 126 | + $container->query(Builtin::class), |
|
| 127 | + |
|
| 128 | + // AuthMechanism::SCHEME_PASSWORD mechanisms |
|
| 129 | + $container->query(Password::class), |
|
| 130 | + $container->query(SessionCredentials::class), |
|
| 131 | + $container->query(LoginCredentials::class), |
|
| 132 | + $container->query(UserProvided::class), |
|
| 133 | + $container->query(GlobalAuth::class), |
|
| 134 | + |
|
| 135 | + // AuthMechanism::SCHEME_OAUTH1 mechanisms |
|
| 136 | + $container->query(OAuth1::class), |
|
| 137 | + |
|
| 138 | + // AuthMechanism::SCHEME_OAUTH2 mechanisms |
|
| 139 | + $container->query(OAuth2::class), |
|
| 140 | + |
|
| 141 | + // AuthMechanism::SCHEME_PUBLICKEY mechanisms |
|
| 142 | + $container->query(RSA::class), |
|
| 143 | + $container->query(RSAPrivateKey::class), |
|
| 144 | + |
|
| 145 | + // AuthMechanism::SCHEME_OPENSTACK mechanisms |
|
| 146 | + $container->query(OpenStackV2::class), |
|
| 147 | + $container->query(OpenStackV3::class), |
|
| 148 | + $container->query(Rackspace::class), |
|
| 149 | + |
|
| 150 | + // Specialized mechanisms |
|
| 151 | + $container->query(AccessKey::class), |
|
| 152 | + $container->query(KerberosAuth::class), |
|
| 153 | + ]; |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | 156 | } |