Completed
Pull Request — master (#7556)
by Robin
14:57
created
apps/files_external/lib/Lib/Storage/SMB.php 1 patch
Indentation   +474 added lines, -474 removed lines patch added patch discarded remove patch
@@ -55,478 +55,478 @@
 block discarded – undo
55 55
 use OCP\Util;
56 56
 
57 57
 class SMB extends Common implements INotifyStorage {
58
-	/**
59
-	 * @var \Icewind\SMB\Server
60
-	 */
61
-	protected $server;
62
-
63
-	/**
64
-	 * @var \Icewind\SMB\Share
65
-	 */
66
-	protected $share;
67
-
68
-	/**
69
-	 * @var string
70
-	 */
71
-	protected $root;
72
-
73
-	/**
74
-	 * @var \Icewind\SMB\FileInfo[]
75
-	 */
76
-	protected $statCache;
77
-
78
-	public function __construct($params) {
79
-		if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) {
80
-			if (Server::NativeAvailable()) {
81
-				$this->server = new NativeServer($params['host'], $params['user'], $params['password']);
82
-			} else {
83
-				$this->server = new Server($params['host'], $params['user'], $params['password']);
84
-			}
85
-			$this->share = $this->server->getShare(trim($params['share'], '/'));
86
-
87
-			$this->root = isset($params['root']) ? $params['root'] : '/';
88
-			if (!$this->root || $this->root[0] !== '/') {
89
-				$this->root = '/' . $this->root;
90
-			}
91
-			if (substr($this->root, -1, 1) !== '/') {
92
-				$this->root .= '/';
93
-			}
94
-		} else {
95
-			throw new \Exception('Invalid configuration');
96
-		}
97
-		$this->statCache = new CappedMemoryCache();
98
-		parent::__construct($params);
99
-	}
100
-
101
-	/**
102
-	 * @return string
103
-	 */
104
-	public function getId() {
105
-		// FIXME: double slash to keep compatible with the old storage ids,
106
-		// failure to do so will lead to creation of a new storage id and
107
-		// loss of shares from the storage
108
-		return 'smb::' . $this->server->getUser() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root;
109
-	}
110
-
111
-	/**
112
-	 * @param string $path
113
-	 * @return string
114
-	 */
115
-	protected function buildPath($path) {
116
-		return Filesystem::normalizePath($this->root . '/' . $path, true, false, true);
117
-	}
118
-
119
-	protected function relativePath($fullPath) {
120
-		if ($fullPath === $this->root) {
121
-			return '';
122
-		} else if (substr($fullPath, 0, strlen($this->root)) === $this->root) {
123
-			return substr($fullPath, strlen($this->root));
124
-		} else {
125
-			return null;
126
-		}
127
-	}
128
-
129
-	/**
130
-	 * @param string $path
131
-	 * @return \Icewind\SMB\IFileInfo
132
-	 * @throws StorageNotAvailableException
133
-	 */
134
-	protected function getFileInfo($path) {
135
-		try {
136
-			$path = $this->buildPath($path);
137
-			if (!isset($this->statCache[$path])) {
138
-				$this->statCache[$path] = $this->share->stat($path);
139
-			}
140
-			return $this->statCache[$path];
141
-		} catch (ConnectException $e) {
142
-			throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
143
-		}
144
-	}
145
-
146
-	/**
147
-	 * @param string $path
148
-	 * @return \Icewind\SMB\IFileInfo[]
149
-	 * @throws StorageNotAvailableException
150
-	 */
151
-	protected function getFolderContents($path) {
152
-		try {
153
-			$path = $this->buildPath($path);
154
-			$files = $this->share->dir($path);
155
-			foreach ($files as $file) {
156
-				$this->statCache[$path . '/' . $file->getName()] = $file;
157
-			}
158
-			return array_filter($files, function (IFileInfo $file) {
159
-				return !$file->isHidden();
160
-			});
161
-		} catch (ConnectException $e) {
162
-			throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
163
-		}
164
-	}
165
-
166
-	/**
167
-	 * @param \Icewind\SMB\IFileInfo $info
168
-	 * @return array
169
-	 */
170
-	protected function formatInfo($info) {
171
-		$result = [
172
-			'size' => $info->getSize(),
173
-			'mtime' => $info->getMTime(),
174
-		];
175
-		if ($info->isDirectory()) {
176
-			$result['type'] = 'dir';
177
-		} else {
178
-			$result['type'] = 'file';
179
-		}
180
-		return $result;
181
-	}
182
-
183
-	/**
184
-	 * Rename the files. If the source or the target is the root, the rename won't happen.
185
-	 *
186
-	 * @param string $source the old name of the path
187
-	 * @param string $target the new name of the path
188
-	 * @return bool true if the rename is successful, false otherwise
189
-	 */
190
-	public function rename($source, $target) {
191
-		if ($this->isRootDir($source) || $this->isRootDir($target)) {
192
-			return false;
193
-		}
194
-
195
-		$absoluteSource = $this->buildPath($source);
196
-		$absoluteTarget = $this->buildPath($target);
197
-		try {
198
-			$result = $this->share->rename($absoluteSource, $absoluteTarget);
199
-		} catch (AlreadyExistsException $e) {
200
-			$this->remove($target);
201
-			$result = $this->share->rename($absoluteSource, $absoluteTarget);
202
-		} catch (\Exception $e) {
203
-			\OC::$server->getLogger()->logException($e, ['level' => Util::WARN]);
204
-			return false;
205
-		}
206
-		unset($this->statCache[$absoluteSource], $this->statCache[$absoluteTarget]);
207
-		return $result;
208
-	}
209
-
210
-	public function stat($path) {
211
-		try {
212
-			$result = $this->formatInfo($this->getFileInfo($path));
213
-		} catch (ForbiddenException $e) {
214
-			return false;
215
-		} catch (NotFoundException $e) {
216
-			return false;
217
-		}
218
-		if ($this->remoteIsShare() && $this->isRootDir($path)) {
219
-			$result['mtime'] = $this->shareMTime();
220
-		}
221
-		return $result;
222
-	}
223
-
224
-	/**
225
-	 * get the best guess for the modification time of the share
226
-	 *
227
-	 * @return int
228
-	 */
229
-	private function shareMTime() {
230
-		$highestMTime = 0;
231
-		$files = $this->share->dir($this->root);
232
-		foreach ($files as $fileInfo) {
233
-			if ($fileInfo->getMTime() > $highestMTime) {
234
-				$highestMTime = $fileInfo->getMTime();
235
-			}
236
-		}
237
-		return $highestMTime;
238
-	}
239
-
240
-	/**
241
-	 * Check if the path is our root dir (not the smb one)
242
-	 *
243
-	 * @param string $path the path
244
-	 * @return bool
245
-	 */
246
-	private function isRootDir($path) {
247
-		return $path === '' || $path === '/' || $path === '.';
248
-	}
249
-
250
-	/**
251
-	 * Check if our root points to a smb share
252
-	 *
253
-	 * @return bool true if our root points to a share false otherwise
254
-	 */
255
-	private function remoteIsShare() {
256
-		return $this->share->getName() && (!$this->root || $this->root === '/');
257
-	}
258
-
259
-	/**
260
-	 * @param string $path
261
-	 * @return bool
262
-	 */
263
-	public function unlink($path) {
264
-		if ($this->isRootDir($path)) {
265
-			return false;
266
-		}
267
-
268
-		try {
269
-			if ($this->is_dir($path)) {
270
-				return $this->rmdir($path);
271
-			} else {
272
-				$path = $this->buildPath($path);
273
-				unset($this->statCache[$path]);
274
-				$this->share->del($path);
275
-				return true;
276
-			}
277
-		} catch (NotFoundException $e) {
278
-			return false;
279
-		} catch (ForbiddenException $e) {
280
-			return false;
281
-		} catch (ConnectException $e) {
282
-			throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
283
-		}
284
-	}
285
-
286
-	/**
287
-	 * check if a file or folder has been updated since $time
288
-	 *
289
-	 * @param string $path
290
-	 * @param int $time
291
-	 * @return bool
292
-	 */
293
-	public function hasUpdated($path, $time) {
294
-		if (!$path and $this->root === '/') {
295
-			// mtime doesn't work for shares, but giving the nature of the backend,
296
-			// doing a full update is still just fast enough
297
-			return true;
298
-		} else {
299
-			$actualTime = $this->filemtime($path);
300
-			return $actualTime > $time;
301
-		}
302
-	}
303
-
304
-	/**
305
-	 * @param string $path
306
-	 * @param string $mode
307
-	 * @return resource|false
308
-	 */
309
-	public function fopen($path, $mode) {
310
-		$fullPath = $this->buildPath($path);
311
-		try {
312
-			switch ($mode) {
313
-				case 'r':
314
-				case 'rb':
315
-					if (!$this->file_exists($path)) {
316
-						return false;
317
-					}
318
-					return $this->share->read($fullPath);
319
-				case 'w':
320
-				case 'wb':
321
-					$source = $this->share->write($fullPath);
322
-					return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) {
323
-						unset($this->statCache[$fullPath]);
324
-					});
325
-				case 'a':
326
-				case 'ab':
327
-				case 'r+':
328
-				case 'w+':
329
-				case 'wb+':
330
-				case 'a+':
331
-				case 'x':
332
-				case 'x+':
333
-				case 'c':
334
-				case 'c+':
335
-					//emulate these
336
-					if (strrpos($path, '.') !== false) {
337
-						$ext = substr($path, strrpos($path, '.'));
338
-					} else {
339
-						$ext = '';
340
-					}
341
-					if ($this->file_exists($path)) {
342
-						if (!$this->isUpdatable($path)) {
343
-							return false;
344
-						}
345
-						$tmpFile = $this->getCachedFile($path);
346
-					} else {
347
-						if (!$this->isCreatable(dirname($path))) {
348
-							return false;
349
-						}
350
-						$tmpFile = \OCP\Files::tmpFile($ext);
351
-					}
352
-					$source = fopen($tmpFile, $mode);
353
-					$share = $this->share;
354
-					return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) {
355
-						unset($this->statCache[$fullPath]);
356
-						$share->put($tmpFile, $fullPath);
357
-						unlink($tmpFile);
358
-					});
359
-			}
360
-			return false;
361
-		} catch (NotFoundException $e) {
362
-			return false;
363
-		} catch (ForbiddenException $e) {
364
-			return false;
365
-		} catch (ConnectException $e) {
366
-			throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
367
-		}
368
-	}
369
-
370
-	public function rmdir($path) {
371
-		if ($this->isRootDir($path)) {
372
-			return false;
373
-		}
374
-
375
-		try {
376
-			$this->statCache = array();
377
-			$content = $this->share->dir($this->buildPath($path));
378
-			foreach ($content as $file) {
379
-				if ($file->isDirectory()) {
380
-					$this->rmdir($path . '/' . $file->getName());
381
-				} else {
382
-					$this->share->del($file->getPath());
383
-				}
384
-			}
385
-			$this->share->rmdir($this->buildPath($path));
386
-			return true;
387
-		} catch (NotFoundException $e) {
388
-			return false;
389
-		} catch (ForbiddenException $e) {
390
-			return false;
391
-		} catch (ConnectException $e) {
392
-			throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
393
-		}
394
-	}
395
-
396
-	public function touch($path, $time = null) {
397
-		try {
398
-			if (!$this->file_exists($path)) {
399
-				$fh = $this->share->write($this->buildPath($path));
400
-				fclose($fh);
401
-				return true;
402
-			}
403
-			return false;
404
-		} catch (ConnectException $e) {
405
-			throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
406
-		}
407
-	}
408
-
409
-	public function opendir($path) {
410
-		try {
411
-			$files = $this->getFolderContents($path);
412
-		} catch (NotFoundException $e) {
413
-			return false;
414
-		} catch (ForbiddenException $e) {
415
-			return false;
416
-		}
417
-		$names = array_map(function ($info) {
418
-			/** @var \Icewind\SMB\IFileInfo $info */
419
-			return $info->getName();
420
-		}, $files);
421
-		return IteratorDirectory::wrap($names);
422
-	}
423
-
424
-	public function filetype($path) {
425
-		try {
426
-			return $this->getFileInfo($path)->isDirectory() ? 'dir' : 'file';
427
-		} catch (NotFoundException $e) {
428
-			return false;
429
-		} catch (ForbiddenException $e) {
430
-			return false;
431
-		}
432
-	}
433
-
434
-	public function mkdir($path) {
435
-		$path = $this->buildPath($path);
436
-		try {
437
-			$this->share->mkdir($path);
438
-			return true;
439
-		} catch (ConnectException $e) {
440
-			throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
441
-		} catch (Exception $e) {
442
-			return false;
443
-		}
444
-	}
445
-
446
-	public function file_exists($path) {
447
-		try {
448
-			$this->getFileInfo($path);
449
-			return true;
450
-		} catch (NotFoundException $e) {
451
-			return false;
452
-		} catch (ForbiddenException $e) {
453
-			return false;
454
-		} catch (ConnectException $e) {
455
-			throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
456
-		}
457
-	}
458
-
459
-	public function isReadable($path) {
460
-		try {
461
-			$info = $this->getFileInfo($path);
462
-			return !$info->isHidden();
463
-		} catch (NotFoundException $e) {
464
-			return false;
465
-		} catch (ForbiddenException $e) {
466
-			return false;
467
-		}
468
-	}
469
-
470
-	public function isUpdatable($path) {
471
-		try {
472
-			$info = $this->getFileInfo($path);
473
-			// following windows behaviour for read-only folders: they can be written into
474
-			// (https://support.microsoft.com/en-us/kb/326549 - "cause" section)
475
-			return !$info->isHidden() && (!$info->isReadOnly() || $this->is_dir($path));
476
-		} catch (NotFoundException $e) {
477
-			return false;
478
-		} catch (ForbiddenException $e) {
479
-			return false;
480
-		}
481
-	}
482
-
483
-	public function isDeletable($path) {
484
-		try {
485
-			$info = $this->getFileInfo($path);
486
-			return !$info->isHidden() && !$info->isReadOnly();
487
-		} catch (NotFoundException $e) {
488
-			return false;
489
-		} catch (ForbiddenException $e) {
490
-			return false;
491
-		}
492
-	}
493
-
494
-	/**
495
-	 * check if smbclient is installed
496
-	 */
497
-	public static function checkDependencies() {
498
-		return (
499
-			(bool)\OC_Helper::findBinaryPath('smbclient')
500
-			|| Server::NativeAvailable()
501
-		) ? true : ['smbclient'];
502
-	}
503
-
504
-	/**
505
-	 * Test a storage for availability
506
-	 *
507
-	 * @return bool
508
-	 */
509
-	public function test() {
510
-		try {
511
-			return parent::test();
512
-		} catch (Exception $e) {
513
-			return false;
514
-		}
515
-	}
516
-
517
-	public function listen($path, callable $callback) {
518
-		$this->notify($path)->listen(function (IChange $change) use ($callback) {
519
-			if ($change instanceof IRenameChange) {
520
-				return $callback($change->getType(), $change->getPath(), $change->getTargetPath());
521
-			} else {
522
-				return $callback($change->getType(), $change->getPath());
523
-			}
524
-		});
525
-	}
526
-
527
-	public function notify($path) {
528
-		$path = '/' . ltrim($path, '/');
529
-		$shareNotifyHandler = $this->share->notify($this->buildPath($path));
530
-		return new SMBNotifyHandler($shareNotifyHandler, $this->root);
531
-	}
58
+    /**
59
+     * @var \Icewind\SMB\Server
60
+     */
61
+    protected $server;
62
+
63
+    /**
64
+     * @var \Icewind\SMB\Share
65
+     */
66
+    protected $share;
67
+
68
+    /**
69
+     * @var string
70
+     */
71
+    protected $root;
72
+
73
+    /**
74
+     * @var \Icewind\SMB\FileInfo[]
75
+     */
76
+    protected $statCache;
77
+
78
+    public function __construct($params) {
79
+        if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) {
80
+            if (Server::NativeAvailable()) {
81
+                $this->server = new NativeServer($params['host'], $params['user'], $params['password']);
82
+            } else {
83
+                $this->server = new Server($params['host'], $params['user'], $params['password']);
84
+            }
85
+            $this->share = $this->server->getShare(trim($params['share'], '/'));
86
+
87
+            $this->root = isset($params['root']) ? $params['root'] : '/';
88
+            if (!$this->root || $this->root[0] !== '/') {
89
+                $this->root = '/' . $this->root;
90
+            }
91
+            if (substr($this->root, -1, 1) !== '/') {
92
+                $this->root .= '/';
93
+            }
94
+        } else {
95
+            throw new \Exception('Invalid configuration');
96
+        }
97
+        $this->statCache = new CappedMemoryCache();
98
+        parent::__construct($params);
99
+    }
100
+
101
+    /**
102
+     * @return string
103
+     */
104
+    public function getId() {
105
+        // FIXME: double slash to keep compatible with the old storage ids,
106
+        // failure to do so will lead to creation of a new storage id and
107
+        // loss of shares from the storage
108
+        return 'smb::' . $this->server->getUser() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root;
109
+    }
110
+
111
+    /**
112
+     * @param string $path
113
+     * @return string
114
+     */
115
+    protected function buildPath($path) {
116
+        return Filesystem::normalizePath($this->root . '/' . $path, true, false, true);
117
+    }
118
+
119
+    protected function relativePath($fullPath) {
120
+        if ($fullPath === $this->root) {
121
+            return '';
122
+        } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) {
123
+            return substr($fullPath, strlen($this->root));
124
+        } else {
125
+            return null;
126
+        }
127
+    }
128
+
129
+    /**
130
+     * @param string $path
131
+     * @return \Icewind\SMB\IFileInfo
132
+     * @throws StorageNotAvailableException
133
+     */
134
+    protected function getFileInfo($path) {
135
+        try {
136
+            $path = $this->buildPath($path);
137
+            if (!isset($this->statCache[$path])) {
138
+                $this->statCache[$path] = $this->share->stat($path);
139
+            }
140
+            return $this->statCache[$path];
141
+        } catch (ConnectException $e) {
142
+            throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
143
+        }
144
+    }
145
+
146
+    /**
147
+     * @param string $path
148
+     * @return \Icewind\SMB\IFileInfo[]
149
+     * @throws StorageNotAvailableException
150
+     */
151
+    protected function getFolderContents($path) {
152
+        try {
153
+            $path = $this->buildPath($path);
154
+            $files = $this->share->dir($path);
155
+            foreach ($files as $file) {
156
+                $this->statCache[$path . '/' . $file->getName()] = $file;
157
+            }
158
+            return array_filter($files, function (IFileInfo $file) {
159
+                return !$file->isHidden();
160
+            });
161
+        } catch (ConnectException $e) {
162
+            throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
163
+        }
164
+    }
165
+
166
+    /**
167
+     * @param \Icewind\SMB\IFileInfo $info
168
+     * @return array
169
+     */
170
+    protected function formatInfo($info) {
171
+        $result = [
172
+            'size' => $info->getSize(),
173
+            'mtime' => $info->getMTime(),
174
+        ];
175
+        if ($info->isDirectory()) {
176
+            $result['type'] = 'dir';
177
+        } else {
178
+            $result['type'] = 'file';
179
+        }
180
+        return $result;
181
+    }
182
+
183
+    /**
184
+     * Rename the files. If the source or the target is the root, the rename won't happen.
185
+     *
186
+     * @param string $source the old name of the path
187
+     * @param string $target the new name of the path
188
+     * @return bool true if the rename is successful, false otherwise
189
+     */
190
+    public function rename($source, $target) {
191
+        if ($this->isRootDir($source) || $this->isRootDir($target)) {
192
+            return false;
193
+        }
194
+
195
+        $absoluteSource = $this->buildPath($source);
196
+        $absoluteTarget = $this->buildPath($target);
197
+        try {
198
+            $result = $this->share->rename($absoluteSource, $absoluteTarget);
199
+        } catch (AlreadyExistsException $e) {
200
+            $this->remove($target);
201
+            $result = $this->share->rename($absoluteSource, $absoluteTarget);
202
+        } catch (\Exception $e) {
203
+            \OC::$server->getLogger()->logException($e, ['level' => Util::WARN]);
204
+            return false;
205
+        }
206
+        unset($this->statCache[$absoluteSource], $this->statCache[$absoluteTarget]);
207
+        return $result;
208
+    }
209
+
210
+    public function stat($path) {
211
+        try {
212
+            $result = $this->formatInfo($this->getFileInfo($path));
213
+        } catch (ForbiddenException $e) {
214
+            return false;
215
+        } catch (NotFoundException $e) {
216
+            return false;
217
+        }
218
+        if ($this->remoteIsShare() && $this->isRootDir($path)) {
219
+            $result['mtime'] = $this->shareMTime();
220
+        }
221
+        return $result;
222
+    }
223
+
224
+    /**
225
+     * get the best guess for the modification time of the share
226
+     *
227
+     * @return int
228
+     */
229
+    private function shareMTime() {
230
+        $highestMTime = 0;
231
+        $files = $this->share->dir($this->root);
232
+        foreach ($files as $fileInfo) {
233
+            if ($fileInfo->getMTime() > $highestMTime) {
234
+                $highestMTime = $fileInfo->getMTime();
235
+            }
236
+        }
237
+        return $highestMTime;
238
+    }
239
+
240
+    /**
241
+     * Check if the path is our root dir (not the smb one)
242
+     *
243
+     * @param string $path the path
244
+     * @return bool
245
+     */
246
+    private function isRootDir($path) {
247
+        return $path === '' || $path === '/' || $path === '.';
248
+    }
249
+
250
+    /**
251
+     * Check if our root points to a smb share
252
+     *
253
+     * @return bool true if our root points to a share false otherwise
254
+     */
255
+    private function remoteIsShare() {
256
+        return $this->share->getName() && (!$this->root || $this->root === '/');
257
+    }
258
+
259
+    /**
260
+     * @param string $path
261
+     * @return bool
262
+     */
263
+    public function unlink($path) {
264
+        if ($this->isRootDir($path)) {
265
+            return false;
266
+        }
267
+
268
+        try {
269
+            if ($this->is_dir($path)) {
270
+                return $this->rmdir($path);
271
+            } else {
272
+                $path = $this->buildPath($path);
273
+                unset($this->statCache[$path]);
274
+                $this->share->del($path);
275
+                return true;
276
+            }
277
+        } catch (NotFoundException $e) {
278
+            return false;
279
+        } catch (ForbiddenException $e) {
280
+            return false;
281
+        } catch (ConnectException $e) {
282
+            throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
283
+        }
284
+    }
285
+
286
+    /**
287
+     * check if a file or folder has been updated since $time
288
+     *
289
+     * @param string $path
290
+     * @param int $time
291
+     * @return bool
292
+     */
293
+    public function hasUpdated($path, $time) {
294
+        if (!$path and $this->root === '/') {
295
+            // mtime doesn't work for shares, but giving the nature of the backend,
296
+            // doing a full update is still just fast enough
297
+            return true;
298
+        } else {
299
+            $actualTime = $this->filemtime($path);
300
+            return $actualTime > $time;
301
+        }
302
+    }
303
+
304
+    /**
305
+     * @param string $path
306
+     * @param string $mode
307
+     * @return resource|false
308
+     */
309
+    public function fopen($path, $mode) {
310
+        $fullPath = $this->buildPath($path);
311
+        try {
312
+            switch ($mode) {
313
+                case 'r':
314
+                case 'rb':
315
+                    if (!$this->file_exists($path)) {
316
+                        return false;
317
+                    }
318
+                    return $this->share->read($fullPath);
319
+                case 'w':
320
+                case 'wb':
321
+                    $source = $this->share->write($fullPath);
322
+                    return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) {
323
+                        unset($this->statCache[$fullPath]);
324
+                    });
325
+                case 'a':
326
+                case 'ab':
327
+                case 'r+':
328
+                case 'w+':
329
+                case 'wb+':
330
+                case 'a+':
331
+                case 'x':
332
+                case 'x+':
333
+                case 'c':
334
+                case 'c+':
335
+                    //emulate these
336
+                    if (strrpos($path, '.') !== false) {
337
+                        $ext = substr($path, strrpos($path, '.'));
338
+                    } else {
339
+                        $ext = '';
340
+                    }
341
+                    if ($this->file_exists($path)) {
342
+                        if (!$this->isUpdatable($path)) {
343
+                            return false;
344
+                        }
345
+                        $tmpFile = $this->getCachedFile($path);
346
+                    } else {
347
+                        if (!$this->isCreatable(dirname($path))) {
348
+                            return false;
349
+                        }
350
+                        $tmpFile = \OCP\Files::tmpFile($ext);
351
+                    }
352
+                    $source = fopen($tmpFile, $mode);
353
+                    $share = $this->share;
354
+                    return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) {
355
+                        unset($this->statCache[$fullPath]);
356
+                        $share->put($tmpFile, $fullPath);
357
+                        unlink($tmpFile);
358
+                    });
359
+            }
360
+            return false;
361
+        } catch (NotFoundException $e) {
362
+            return false;
363
+        } catch (ForbiddenException $e) {
364
+            return false;
365
+        } catch (ConnectException $e) {
366
+            throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
367
+        }
368
+    }
369
+
370
+    public function rmdir($path) {
371
+        if ($this->isRootDir($path)) {
372
+            return false;
373
+        }
374
+
375
+        try {
376
+            $this->statCache = array();
377
+            $content = $this->share->dir($this->buildPath($path));
378
+            foreach ($content as $file) {
379
+                if ($file->isDirectory()) {
380
+                    $this->rmdir($path . '/' . $file->getName());
381
+                } else {
382
+                    $this->share->del($file->getPath());
383
+                }
384
+            }
385
+            $this->share->rmdir($this->buildPath($path));
386
+            return true;
387
+        } catch (NotFoundException $e) {
388
+            return false;
389
+        } catch (ForbiddenException $e) {
390
+            return false;
391
+        } catch (ConnectException $e) {
392
+            throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
393
+        }
394
+    }
395
+
396
+    public function touch($path, $time = null) {
397
+        try {
398
+            if (!$this->file_exists($path)) {
399
+                $fh = $this->share->write($this->buildPath($path));
400
+                fclose($fh);
401
+                return true;
402
+            }
403
+            return false;
404
+        } catch (ConnectException $e) {
405
+            throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
406
+        }
407
+    }
408
+
409
+    public function opendir($path) {
410
+        try {
411
+            $files = $this->getFolderContents($path);
412
+        } catch (NotFoundException $e) {
413
+            return false;
414
+        } catch (ForbiddenException $e) {
415
+            return false;
416
+        }
417
+        $names = array_map(function ($info) {
418
+            /** @var \Icewind\SMB\IFileInfo $info */
419
+            return $info->getName();
420
+        }, $files);
421
+        return IteratorDirectory::wrap($names);
422
+    }
423
+
424
+    public function filetype($path) {
425
+        try {
426
+            return $this->getFileInfo($path)->isDirectory() ? 'dir' : 'file';
427
+        } catch (NotFoundException $e) {
428
+            return false;
429
+        } catch (ForbiddenException $e) {
430
+            return false;
431
+        }
432
+    }
433
+
434
+    public function mkdir($path) {
435
+        $path = $this->buildPath($path);
436
+        try {
437
+            $this->share->mkdir($path);
438
+            return true;
439
+        } catch (ConnectException $e) {
440
+            throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
441
+        } catch (Exception $e) {
442
+            return false;
443
+        }
444
+    }
445
+
446
+    public function file_exists($path) {
447
+        try {
448
+            $this->getFileInfo($path);
449
+            return true;
450
+        } catch (NotFoundException $e) {
451
+            return false;
452
+        } catch (ForbiddenException $e) {
453
+            return false;
454
+        } catch (ConnectException $e) {
455
+            throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e);
456
+        }
457
+    }
458
+
459
+    public function isReadable($path) {
460
+        try {
461
+            $info = $this->getFileInfo($path);
462
+            return !$info->isHidden();
463
+        } catch (NotFoundException $e) {
464
+            return false;
465
+        } catch (ForbiddenException $e) {
466
+            return false;
467
+        }
468
+    }
469
+
470
+    public function isUpdatable($path) {
471
+        try {
472
+            $info = $this->getFileInfo($path);
473
+            // following windows behaviour for read-only folders: they can be written into
474
+            // (https://support.microsoft.com/en-us/kb/326549 - "cause" section)
475
+            return !$info->isHidden() && (!$info->isReadOnly() || $this->is_dir($path));
476
+        } catch (NotFoundException $e) {
477
+            return false;
478
+        } catch (ForbiddenException $e) {
479
+            return false;
480
+        }
481
+    }
482
+
483
+    public function isDeletable($path) {
484
+        try {
485
+            $info = $this->getFileInfo($path);
486
+            return !$info->isHidden() && !$info->isReadOnly();
487
+        } catch (NotFoundException $e) {
488
+            return false;
489
+        } catch (ForbiddenException $e) {
490
+            return false;
491
+        }
492
+    }
493
+
494
+    /**
495
+     * check if smbclient is installed
496
+     */
497
+    public static function checkDependencies() {
498
+        return (
499
+            (bool)\OC_Helper::findBinaryPath('smbclient')
500
+            || Server::NativeAvailable()
501
+        ) ? true : ['smbclient'];
502
+    }
503
+
504
+    /**
505
+     * Test a storage for availability
506
+     *
507
+     * @return bool
508
+     */
509
+    public function test() {
510
+        try {
511
+            return parent::test();
512
+        } catch (Exception $e) {
513
+            return false;
514
+        }
515
+    }
516
+
517
+    public function listen($path, callable $callback) {
518
+        $this->notify($path)->listen(function (IChange $change) use ($callback) {
519
+            if ($change instanceof IRenameChange) {
520
+                return $callback($change->getType(), $change->getPath(), $change->getTargetPath());
521
+            } else {
522
+                return $callback($change->getType(), $change->getPath());
523
+            }
524
+        });
525
+    }
526
+
527
+    public function notify($path) {
528
+        $path = '/' . ltrim($path, '/');
529
+        $shareNotifyHandler = $this->share->notify($this->buildPath($path));
530
+        return new SMBNotifyHandler($shareNotifyHandler, $this->root);
531
+    }
532 532
 }
Please login to merge, or discard this patch.