Passed
Push — master ( c96044...9809b7 )
by Roeland
10:53 queued 10s
created
lib/private/Files/Node/Folder.php 2 patches
Indentation   +526 added lines, -526 removed lines patch added patch discarded remove patch
@@ -42,537 +42,537 @@
 block discarded – undo
42 42
 use OCP\Files\Search\ISearchQuery;
43 43
 
44 44
 class Folder extends Node implements \OCP\Files\Folder {
45
-	/**
46
-	 * Creates a Folder that represents a non-existing path
47
-	 *
48
-	 * @param string $path path
49
-	 * @return string non-existing node class
50
-	 */
51
-	protected function createNonExistingNode($path) {
52
-		return new NonExistingFolder($this->root, $this->view, $path);
53
-	}
54
-
55
-	/**
56
-	 * @param string $path path relative to the folder
57
-	 * @return string
58
-	 * @throws \OCP\Files\NotPermittedException
59
-	 */
60
-	public function getFullPath($path) {
61
-		if (!$this->isValidPath($path)) {
62
-			throw new NotPermittedException('Invalid path');
63
-		}
64
-		return $this->path . $this->normalizePath($path);
65
-	}
66
-
67
-	/**
68
-	 * @param string $path
69
-	 * @return string
70
-	 */
71
-	public function getRelativePath($path) {
72
-		if ($this->path === '' or $this->path === '/') {
73
-			return $this->normalizePath($path);
74
-		}
75
-		if ($path === $this->path) {
76
-			return '/';
77
-		} elseif (strpos($path, $this->path . '/') !== 0) {
78
-			return null;
79
-		} else {
80
-			$path = substr($path, strlen($this->path));
81
-			return $this->normalizePath($path);
82
-		}
83
-	}
84
-
85
-	/**
86
-	 * check if a node is a (grand-)child of the folder
87
-	 *
88
-	 * @param \OC\Files\Node\Node $node
89
-	 * @return bool
90
-	 */
91
-	public function isSubNode($node) {
92
-		return strpos($node->getPath(), $this->path . '/') === 0;
93
-	}
94
-
95
-	/**
96
-	 * get the content of this directory
97
-	 *
98
-	 * @throws \OCP\Files\NotFoundException
99
-	 * @return Node[]
100
-	 */
101
-	public function getDirectoryListing() {
102
-		$folderContent = $this->view->getDirectoryContent($this->path);
103
-
104
-		return array_map(function (FileInfo $info) {
105
-			if ($info->getMimetype() === 'httpd/unix-directory') {
106
-				return new Folder($this->root, $this->view, $info->getPath(), $info);
107
-			} else {
108
-				return new File($this->root, $this->view, $info->getPath(), $info);
109
-			}
110
-		}, $folderContent);
111
-	}
112
-
113
-	/**
114
-	 * @param string $path
115
-	 * @param FileInfo $info
116
-	 * @return File|Folder
117
-	 */
118
-	protected function createNode($path, FileInfo $info = null) {
119
-		if (is_null($info)) {
120
-			$isDir = $this->view->is_dir($path);
121
-		} else {
122
-			$isDir = $info->getType() === FileInfo::TYPE_FOLDER;
123
-		}
124
-		if ($isDir) {
125
-			return new Folder($this->root, $this->view, $path, $info);
126
-		} else {
127
-			return new File($this->root, $this->view, $path, $info);
128
-		}
129
-	}
130
-
131
-	/**
132
-	 * Get the node at $path
133
-	 *
134
-	 * @param string $path
135
-	 * @return \OC\Files\Node\Node
136
-	 * @throws \OCP\Files\NotFoundException
137
-	 */
138
-	public function get($path) {
139
-		return $this->root->get($this->getFullPath($path));
140
-	}
141
-
142
-	/**
143
-	 * @param string $path
144
-	 * @return bool
145
-	 */
146
-	public function nodeExists($path) {
147
-		try {
148
-			$this->get($path);
149
-			return true;
150
-		} catch (NotFoundException $e) {
151
-			return false;
152
-		}
153
-	}
154
-
155
-	/**
156
-	 * @param string $path
157
-	 * @return \OC\Files\Node\Folder
158
-	 * @throws \OCP\Files\NotPermittedException
159
-	 */
160
-	public function newFolder($path) {
161
-		if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
162
-			$fullPath = $this->getFullPath($path);
163
-			$nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath);
164
-			$this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]);
165
-			if (!$this->view->mkdir($fullPath)) {
166
-				throw new NotPermittedException('Could not create folder');
167
-			}
168
-			$node = new Folder($this->root, $this->view, $fullPath);
169
-			$this->sendHooks(['postWrite', 'postCreate'], [$node]);
170
-			return $node;
171
-		} else {
172
-			throw new NotPermittedException('No create permission for folder');
173
-		}
174
-	}
175
-
176
-	/**
177
-	 * @param string $path
178
-	 * @param string | resource | null $content
179
-	 * @return \OC\Files\Node\File
180
-	 * @throws \OCP\Files\NotPermittedException
181
-	 */
182
-	public function newFile($path, $content = null) {
183
-		if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
184
-			$fullPath = $this->getFullPath($path);
185
-			$nonExisting = new NonExistingFile($this->root, $this->view, $fullPath);
186
-			$this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]);
187
-			if ($content !== null) {
188
-				$result = $this->view->file_put_contents($fullPath, $content);
189
-			} else {
190
-				$result = $this->view->touch($fullPath);
191
-			}
192
-			if (!$result) {
193
-				throw new NotPermittedException('Could not create path');
194
-			}
195
-			$node = new File($this->root, $this->view, $fullPath);
196
-			$this->sendHooks(['postWrite', 'postCreate'], [$node]);
197
-			return $node;
198
-		}
199
-		throw new NotPermittedException('No create permission for path');
200
-	}
201
-
202
-	/**
203
-	 * search for files with the name matching $query
204
-	 *
205
-	 * @param string|ISearchQuery $query
206
-	 * @return \OC\Files\Node\Node[]
207
-	 */
208
-	public function search($query) {
209
-		if (is_string($query)) {
210
-			return $this->searchCommon('search', ['%' . $query . '%']);
211
-		} else {
212
-			return $this->searchCommon('searchQuery', [$query]);
213
-		}
214
-	}
215
-
216
-	/**
217
-	 * search for files by mimetype
218
-	 *
219
-	 * @param string $mimetype
220
-	 * @return Node[]
221
-	 */
222
-	public function searchByMime($mimetype) {
223
-		return $this->searchCommon('searchByMime', [$mimetype]);
224
-	}
225
-
226
-	/**
227
-	 * search for files by tag
228
-	 *
229
-	 * @param string|int $tag name or tag id
230
-	 * @param string $userId owner of the tags
231
-	 * @return Node[]
232
-	 */
233
-	public function searchByTag($tag, $userId) {
234
-		return $this->searchCommon('searchByTag', [$tag, $userId]);
235
-	}
236
-
237
-	/**
238
-	 * @param string $method cache method
239
-	 * @param array $args call args
240
-	 * @return \OC\Files\Node\Node[]
241
-	 */
242
-	private function searchCommon($method, $args) {
243
-		$limitToHome = ($method === 'searchQuery')? $args[0]->limitToHome(): false;
244
-		if ($limitToHome && count(explode('/', $this->path)) !== 3) {
245
-			throw new \InvalidArgumentException('searching by owner is only allows on the users home folder');
246
-		}
247
-
248
-		$files = [];
249
-		$rootLength = strlen($this->path);
250
-		$mount = $this->root->getMount($this->path);
251
-		$storage = $mount->getStorage();
252
-		$internalPath = $mount->getInternalPath($this->path);
253
-		$internalPath = rtrim($internalPath, '/');
254
-		if ($internalPath !== '') {
255
-			$internalPath = $internalPath . '/';
256
-		}
257
-		$internalRootLength = strlen($internalPath);
258
-
259
-		$cache = $storage->getCache('');
260
-
261
-		$results = call_user_func_array([$cache, $method], $args);
262
-		foreach ($results as $result) {
263
-			if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) {
264
-				$result['internalPath'] = $result['path'];
265
-				$result['path'] = substr($result['path'], $internalRootLength);
266
-				$result['storage'] = $storage;
267
-				$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
268
-			}
269
-		}
270
-
271
-		if (!$limitToHome) {
272
-			$mounts = $this->root->getMountsIn($this->path);
273
-			foreach ($mounts as $mount) {
274
-				$storage = $mount->getStorage();
275
-				if ($storage) {
276
-					$cache = $storage->getCache('');
277
-
278
-					$relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/');
279
-					$results = call_user_func_array([$cache, $method], $args);
280
-					foreach ($results as $result) {
281
-						$result['internalPath'] = $result['path'];
282
-						$result['path'] = $relativeMountPoint . $result['path'];
283
-						$result['storage'] = $storage;
284
-						$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage,
285
-							$result['internalPath'], $result, $mount);
286
-					}
287
-				}
288
-			}
289
-		}
290
-
291
-		return array_map(function (FileInfo $file) {
292
-			return $this->createNode($file->getPath(), $file);
293
-		}, $files);
294
-	}
295
-
296
-	/**
297
-	 * @param int $id
298
-	 * @return \OC\Files\Node\Node[]
299
-	 */
300
-	public function getById($id) {
301
-		$mountCache = $this->root->getUserMountCache();
302
-		if (strpos($this->getPath(), '/', 1) > 0) {
303
-			list(, $user) = explode('/', $this->getPath());
304
-		} else {
305
-			$user = null;
306
-		}
307
-		$mountsContainingFile = $mountCache->getMountsForFileId((int)$id, $user);
308
-		$mounts = $this->root->getMountsIn($this->path);
309
-		$mounts[] = $this->root->getMount($this->path);
310
-		/** @var IMountPoint[] $folderMounts */
311
-		$folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) {
312
-			return $mountPoint->getMountPoint();
313
-		}, $mounts), $mounts);
314
-
315
-		/** @var ICachedMountInfo[] $mountsContainingFile */
316
-		$mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) {
317
-			return isset($folderMounts[$cachedMountInfo->getMountPoint()]);
318
-		}));
319
-
320
-		if (count($mountsContainingFile) === 0) {
321
-			if ($user === $this->getAppDataDirectoryName()) {
322
-				return $this->getByIdInRootMount((int) $id);
323
-			}
324
-			return [];
325
-		}
326
-
327
-		$nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($folderMounts, $id) {
328
-			$mount = $folderMounts[$cachedMountInfo->getMountPoint()];
329
-			$cacheEntry = $mount->getStorage()->getCache()->get((int)$id);
330
-			if (!$cacheEntry) {
331
-				return null;
332
-			}
333
-
334
-			// cache jails will hide the "true" internal path
335
-			$internalPath = ltrim($cachedMountInfo->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/');
336
-			$pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath()));
337
-			$pathRelativeToMount = ltrim($pathRelativeToMount, '/');
338
-			$absolutePath = rtrim($cachedMountInfo->getMountPoint() . $pathRelativeToMount, '/');
339
-			return $this->root->createNode($absolutePath, new \OC\Files\FileInfo(
340
-				$absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount,
341
-				\OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount))
342
-			));
343
-		}, $mountsContainingFile);
344
-
345
-		$nodes = array_filter($nodes);
346
-
347
-		return array_filter($nodes, function (Node $node) {
348
-			return $this->getRelativePath($node->getPath());
349
-		});
350
-	}
351
-
352
-	protected function getAppDataDirectoryName(): string {
353
-		$instanceId = \OC::$server->getConfig()->getSystemValueString('instanceid');
354
-		return 'appdata_' . $instanceId;
355
-	}
356
-
357
-	/**
358
-	 * In case the path we are currently in is inside the appdata_* folder,
359
-	 * the original getById method does not work, because it can only look inside
360
-	 * the user's mount points. But the user has no mount point for the root storage.
361
-	 *
362
-	 * So in that case we directly check the mount of the root if it contains
363
-	 * the id. If it does we check if the path is inside the path we are working
364
-	 * in.
365
-	 *
366
-	 * @param int $id
367
-	 * @return array
368
-	 */
369
-	protected function getByIdInRootMount(int $id): array {
370
-		$mount = $this->root->getMount('');
371
-		$cacheEntry = $mount->getStorage()->getCache($this->path)->get($id);
372
-		if (!$cacheEntry) {
373
-			return [];
374
-		}
375
-
376
-		$absolutePath = '/' . ltrim($cacheEntry->getPath(), '/');
377
-		$currentPath = rtrim($this->path, '/') . '/';
378
-
379
-		if (strpos($absolutePath, $currentPath) !== 0) {
380
-			return [];
381
-		}
382
-
383
-		return [$this->root->createNode(
384
-			$absolutePath, new \OC\Files\FileInfo(
385
-				$absolutePath,
386
-				$mount->getStorage(),
387
-				$cacheEntry->getPath(),
388
-				$cacheEntry,
389
-				$mount
390
-		))];
391
-	}
392
-
393
-	public function getFreeSpace() {
394
-		return $this->view->free_space($this->path);
395
-	}
396
-
397
-	public function delete() {
398
-		if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
399
-			$this->sendHooks(['preDelete']);
400
-			$fileInfo = $this->getFileInfo();
401
-			$this->view->rmdir($this->path);
402
-			$nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo);
403
-			$this->sendHooks(['postDelete'], [$nonExisting]);
404
-			$this->exists = false;
405
-		} else {
406
-			throw new NotPermittedException('No delete permission for path');
407
-		}
408
-	}
409
-
410
-	/**
411
-	 * Add a suffix to the name in case the file exists
412
-	 *
413
-	 * @param string $name
414
-	 * @return string
415
-	 * @throws NotPermittedException
416
-	 */
417
-	public function getNonExistingName($name) {
418
-		$uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view);
419
-		return trim($this->getRelativePath($uniqueName), '/');
420
-	}
421
-
422
-	/**
423
-	 * @param int $limit
424
-	 * @param int $offset
425
-	 * @return \OCP\Files\Node[]
426
-	 */
427
-	public function getRecent($limit, $offset = 0) {
428
-		$mimetypeLoader = \OC::$server->getMimeTypeLoader();
429
-		$mounts = $this->root->getMountsIn($this->path);
430
-		$mounts[] = $this->getMountPoint();
431
-
432
-		$mounts = array_filter($mounts, function (IMountPoint $mount) {
433
-			return $mount->getStorage();
434
-		});
435
-		$storageIds = array_map(function (IMountPoint $mount) {
436
-			return $mount->getStorage()->getCache()->getNumericStorageId();
437
-		}, $mounts);
438
-		/** @var IMountPoint[] $mountMap */
439
-		$mountMap = array_combine($storageIds, $mounts);
440
-		$folderMimetype = $mimetypeLoader->getId(FileInfo::MIMETYPE_FOLDER);
441
-
442
-		/*
45
+    /**
46
+     * Creates a Folder that represents a non-existing path
47
+     *
48
+     * @param string $path path
49
+     * @return string non-existing node class
50
+     */
51
+    protected function createNonExistingNode($path) {
52
+        return new NonExistingFolder($this->root, $this->view, $path);
53
+    }
54
+
55
+    /**
56
+     * @param string $path path relative to the folder
57
+     * @return string
58
+     * @throws \OCP\Files\NotPermittedException
59
+     */
60
+    public function getFullPath($path) {
61
+        if (!$this->isValidPath($path)) {
62
+            throw new NotPermittedException('Invalid path');
63
+        }
64
+        return $this->path . $this->normalizePath($path);
65
+    }
66
+
67
+    /**
68
+     * @param string $path
69
+     * @return string
70
+     */
71
+    public function getRelativePath($path) {
72
+        if ($this->path === '' or $this->path === '/') {
73
+            return $this->normalizePath($path);
74
+        }
75
+        if ($path === $this->path) {
76
+            return '/';
77
+        } elseif (strpos($path, $this->path . '/') !== 0) {
78
+            return null;
79
+        } else {
80
+            $path = substr($path, strlen($this->path));
81
+            return $this->normalizePath($path);
82
+        }
83
+    }
84
+
85
+    /**
86
+     * check if a node is a (grand-)child of the folder
87
+     *
88
+     * @param \OC\Files\Node\Node $node
89
+     * @return bool
90
+     */
91
+    public function isSubNode($node) {
92
+        return strpos($node->getPath(), $this->path . '/') === 0;
93
+    }
94
+
95
+    /**
96
+     * get the content of this directory
97
+     *
98
+     * @throws \OCP\Files\NotFoundException
99
+     * @return Node[]
100
+     */
101
+    public function getDirectoryListing() {
102
+        $folderContent = $this->view->getDirectoryContent($this->path);
103
+
104
+        return array_map(function (FileInfo $info) {
105
+            if ($info->getMimetype() === 'httpd/unix-directory') {
106
+                return new Folder($this->root, $this->view, $info->getPath(), $info);
107
+            } else {
108
+                return new File($this->root, $this->view, $info->getPath(), $info);
109
+            }
110
+        }, $folderContent);
111
+    }
112
+
113
+    /**
114
+     * @param string $path
115
+     * @param FileInfo $info
116
+     * @return File|Folder
117
+     */
118
+    protected function createNode($path, FileInfo $info = null) {
119
+        if (is_null($info)) {
120
+            $isDir = $this->view->is_dir($path);
121
+        } else {
122
+            $isDir = $info->getType() === FileInfo::TYPE_FOLDER;
123
+        }
124
+        if ($isDir) {
125
+            return new Folder($this->root, $this->view, $path, $info);
126
+        } else {
127
+            return new File($this->root, $this->view, $path, $info);
128
+        }
129
+    }
130
+
131
+    /**
132
+     * Get the node at $path
133
+     *
134
+     * @param string $path
135
+     * @return \OC\Files\Node\Node
136
+     * @throws \OCP\Files\NotFoundException
137
+     */
138
+    public function get($path) {
139
+        return $this->root->get($this->getFullPath($path));
140
+    }
141
+
142
+    /**
143
+     * @param string $path
144
+     * @return bool
145
+     */
146
+    public function nodeExists($path) {
147
+        try {
148
+            $this->get($path);
149
+            return true;
150
+        } catch (NotFoundException $e) {
151
+            return false;
152
+        }
153
+    }
154
+
155
+    /**
156
+     * @param string $path
157
+     * @return \OC\Files\Node\Folder
158
+     * @throws \OCP\Files\NotPermittedException
159
+     */
160
+    public function newFolder($path) {
161
+        if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
162
+            $fullPath = $this->getFullPath($path);
163
+            $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath);
164
+            $this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]);
165
+            if (!$this->view->mkdir($fullPath)) {
166
+                throw new NotPermittedException('Could not create folder');
167
+            }
168
+            $node = new Folder($this->root, $this->view, $fullPath);
169
+            $this->sendHooks(['postWrite', 'postCreate'], [$node]);
170
+            return $node;
171
+        } else {
172
+            throw new NotPermittedException('No create permission for folder');
173
+        }
174
+    }
175
+
176
+    /**
177
+     * @param string $path
178
+     * @param string | resource | null $content
179
+     * @return \OC\Files\Node\File
180
+     * @throws \OCP\Files\NotPermittedException
181
+     */
182
+    public function newFile($path, $content = null) {
183
+        if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
184
+            $fullPath = $this->getFullPath($path);
185
+            $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath);
186
+            $this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]);
187
+            if ($content !== null) {
188
+                $result = $this->view->file_put_contents($fullPath, $content);
189
+            } else {
190
+                $result = $this->view->touch($fullPath);
191
+            }
192
+            if (!$result) {
193
+                throw new NotPermittedException('Could not create path');
194
+            }
195
+            $node = new File($this->root, $this->view, $fullPath);
196
+            $this->sendHooks(['postWrite', 'postCreate'], [$node]);
197
+            return $node;
198
+        }
199
+        throw new NotPermittedException('No create permission for path');
200
+    }
201
+
202
+    /**
203
+     * search for files with the name matching $query
204
+     *
205
+     * @param string|ISearchQuery $query
206
+     * @return \OC\Files\Node\Node[]
207
+     */
208
+    public function search($query) {
209
+        if (is_string($query)) {
210
+            return $this->searchCommon('search', ['%' . $query . '%']);
211
+        } else {
212
+            return $this->searchCommon('searchQuery', [$query]);
213
+        }
214
+    }
215
+
216
+    /**
217
+     * search for files by mimetype
218
+     *
219
+     * @param string $mimetype
220
+     * @return Node[]
221
+     */
222
+    public function searchByMime($mimetype) {
223
+        return $this->searchCommon('searchByMime', [$mimetype]);
224
+    }
225
+
226
+    /**
227
+     * search for files by tag
228
+     *
229
+     * @param string|int $tag name or tag id
230
+     * @param string $userId owner of the tags
231
+     * @return Node[]
232
+     */
233
+    public function searchByTag($tag, $userId) {
234
+        return $this->searchCommon('searchByTag', [$tag, $userId]);
235
+    }
236
+
237
+    /**
238
+     * @param string $method cache method
239
+     * @param array $args call args
240
+     * @return \OC\Files\Node\Node[]
241
+     */
242
+    private function searchCommon($method, $args) {
243
+        $limitToHome = ($method === 'searchQuery')? $args[0]->limitToHome(): false;
244
+        if ($limitToHome && count(explode('/', $this->path)) !== 3) {
245
+            throw new \InvalidArgumentException('searching by owner is only allows on the users home folder');
246
+        }
247
+
248
+        $files = [];
249
+        $rootLength = strlen($this->path);
250
+        $mount = $this->root->getMount($this->path);
251
+        $storage = $mount->getStorage();
252
+        $internalPath = $mount->getInternalPath($this->path);
253
+        $internalPath = rtrim($internalPath, '/');
254
+        if ($internalPath !== '') {
255
+            $internalPath = $internalPath . '/';
256
+        }
257
+        $internalRootLength = strlen($internalPath);
258
+
259
+        $cache = $storage->getCache('');
260
+
261
+        $results = call_user_func_array([$cache, $method], $args);
262
+        foreach ($results as $result) {
263
+            if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) {
264
+                $result['internalPath'] = $result['path'];
265
+                $result['path'] = substr($result['path'], $internalRootLength);
266
+                $result['storage'] = $storage;
267
+                $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
268
+            }
269
+        }
270
+
271
+        if (!$limitToHome) {
272
+            $mounts = $this->root->getMountsIn($this->path);
273
+            foreach ($mounts as $mount) {
274
+                $storage = $mount->getStorage();
275
+                if ($storage) {
276
+                    $cache = $storage->getCache('');
277
+
278
+                    $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/');
279
+                    $results = call_user_func_array([$cache, $method], $args);
280
+                    foreach ($results as $result) {
281
+                        $result['internalPath'] = $result['path'];
282
+                        $result['path'] = $relativeMountPoint . $result['path'];
283
+                        $result['storage'] = $storage;
284
+                        $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage,
285
+                            $result['internalPath'], $result, $mount);
286
+                    }
287
+                }
288
+            }
289
+        }
290
+
291
+        return array_map(function (FileInfo $file) {
292
+            return $this->createNode($file->getPath(), $file);
293
+        }, $files);
294
+    }
295
+
296
+    /**
297
+     * @param int $id
298
+     * @return \OC\Files\Node\Node[]
299
+     */
300
+    public function getById($id) {
301
+        $mountCache = $this->root->getUserMountCache();
302
+        if (strpos($this->getPath(), '/', 1) > 0) {
303
+            list(, $user) = explode('/', $this->getPath());
304
+        } else {
305
+            $user = null;
306
+        }
307
+        $mountsContainingFile = $mountCache->getMountsForFileId((int)$id, $user);
308
+        $mounts = $this->root->getMountsIn($this->path);
309
+        $mounts[] = $this->root->getMount($this->path);
310
+        /** @var IMountPoint[] $folderMounts */
311
+        $folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) {
312
+            return $mountPoint->getMountPoint();
313
+        }, $mounts), $mounts);
314
+
315
+        /** @var ICachedMountInfo[] $mountsContainingFile */
316
+        $mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) {
317
+            return isset($folderMounts[$cachedMountInfo->getMountPoint()]);
318
+        }));
319
+
320
+        if (count($mountsContainingFile) === 0) {
321
+            if ($user === $this->getAppDataDirectoryName()) {
322
+                return $this->getByIdInRootMount((int) $id);
323
+            }
324
+            return [];
325
+        }
326
+
327
+        $nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($folderMounts, $id) {
328
+            $mount = $folderMounts[$cachedMountInfo->getMountPoint()];
329
+            $cacheEntry = $mount->getStorage()->getCache()->get((int)$id);
330
+            if (!$cacheEntry) {
331
+                return null;
332
+            }
333
+
334
+            // cache jails will hide the "true" internal path
335
+            $internalPath = ltrim($cachedMountInfo->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/');
336
+            $pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath()));
337
+            $pathRelativeToMount = ltrim($pathRelativeToMount, '/');
338
+            $absolutePath = rtrim($cachedMountInfo->getMountPoint() . $pathRelativeToMount, '/');
339
+            return $this->root->createNode($absolutePath, new \OC\Files\FileInfo(
340
+                $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount,
341
+                \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount))
342
+            ));
343
+        }, $mountsContainingFile);
344
+
345
+        $nodes = array_filter($nodes);
346
+
347
+        return array_filter($nodes, function (Node $node) {
348
+            return $this->getRelativePath($node->getPath());
349
+        });
350
+    }
351
+
352
+    protected function getAppDataDirectoryName(): string {
353
+        $instanceId = \OC::$server->getConfig()->getSystemValueString('instanceid');
354
+        return 'appdata_' . $instanceId;
355
+    }
356
+
357
+    /**
358
+     * In case the path we are currently in is inside the appdata_* folder,
359
+     * the original getById method does not work, because it can only look inside
360
+     * the user's mount points. But the user has no mount point for the root storage.
361
+     *
362
+     * So in that case we directly check the mount of the root if it contains
363
+     * the id. If it does we check if the path is inside the path we are working
364
+     * in.
365
+     *
366
+     * @param int $id
367
+     * @return array
368
+     */
369
+    protected function getByIdInRootMount(int $id): array {
370
+        $mount = $this->root->getMount('');
371
+        $cacheEntry = $mount->getStorage()->getCache($this->path)->get($id);
372
+        if (!$cacheEntry) {
373
+            return [];
374
+        }
375
+
376
+        $absolutePath = '/' . ltrim($cacheEntry->getPath(), '/');
377
+        $currentPath = rtrim($this->path, '/') . '/';
378
+
379
+        if (strpos($absolutePath, $currentPath) !== 0) {
380
+            return [];
381
+        }
382
+
383
+        return [$this->root->createNode(
384
+            $absolutePath, new \OC\Files\FileInfo(
385
+                $absolutePath,
386
+                $mount->getStorage(),
387
+                $cacheEntry->getPath(),
388
+                $cacheEntry,
389
+                $mount
390
+        ))];
391
+    }
392
+
393
+    public function getFreeSpace() {
394
+        return $this->view->free_space($this->path);
395
+    }
396
+
397
+    public function delete() {
398
+        if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
399
+            $this->sendHooks(['preDelete']);
400
+            $fileInfo = $this->getFileInfo();
401
+            $this->view->rmdir($this->path);
402
+            $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo);
403
+            $this->sendHooks(['postDelete'], [$nonExisting]);
404
+            $this->exists = false;
405
+        } else {
406
+            throw new NotPermittedException('No delete permission for path');
407
+        }
408
+    }
409
+
410
+    /**
411
+     * Add a suffix to the name in case the file exists
412
+     *
413
+     * @param string $name
414
+     * @return string
415
+     * @throws NotPermittedException
416
+     */
417
+    public function getNonExistingName($name) {
418
+        $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view);
419
+        return trim($this->getRelativePath($uniqueName), '/');
420
+    }
421
+
422
+    /**
423
+     * @param int $limit
424
+     * @param int $offset
425
+     * @return \OCP\Files\Node[]
426
+     */
427
+    public function getRecent($limit, $offset = 0) {
428
+        $mimetypeLoader = \OC::$server->getMimeTypeLoader();
429
+        $mounts = $this->root->getMountsIn($this->path);
430
+        $mounts[] = $this->getMountPoint();
431
+
432
+        $mounts = array_filter($mounts, function (IMountPoint $mount) {
433
+            return $mount->getStorage();
434
+        });
435
+        $storageIds = array_map(function (IMountPoint $mount) {
436
+            return $mount->getStorage()->getCache()->getNumericStorageId();
437
+        }, $mounts);
438
+        /** @var IMountPoint[] $mountMap */
439
+        $mountMap = array_combine($storageIds, $mounts);
440
+        $folderMimetype = $mimetypeLoader->getId(FileInfo::MIMETYPE_FOLDER);
441
+
442
+        /*
443 443
 		 * Construct an array of the storage id with their prefix path
444 444
 		 * This helps us to filter in the final query
445 445
 		 */
446
-		$filters = array_map(function (IMountPoint $mount) {
447
-			$storage = $mount->getStorage();
448
-
449
-			$storageId = $storage->getCache()->getNumericStorageId();
450
-			$prefix = '';
451
-
452
-			if ($storage->instanceOfStorage(Jail::class)) {
453
-				$prefix = $storage->getUnJailedPath('');
454
-			}
455
-
456
-			return [
457
-				'storageId' => $storageId,
458
-				'pathPrefix' => $prefix,
459
-			];
460
-		}, $mounts);
461
-
462
-		// Search in batches of 500 entries
463
-		$searchLimit = 500;
464
-		$results = [];
465
-		$searchResultCount = 0;
466
-		$count = 0;
467
-		do {
468
-			$searchResult = $this->recentSearch($searchLimit, $offset, $folderMimetype, $filters);
469
-
470
-			// Exit condition if there are no more results
471
-			if (count($searchResult) === 0) {
472
-				break;
473
-			}
474
-
475
-			$searchResultCount += count($searchResult);
476
-
477
-			$parseResult = $this->recentParse($searchResult, $mountMap, $mimetypeLoader);
478
-
479
-			foreach ($parseResult as $result) {
480
-				$results[] = $result;
481
-			}
482
-
483
-			$offset += $searchLimit;
484
-			$count++;
485
-		} while (count($results) < $limit && ($searchResultCount < (3 * $limit) || $count < 5));
486
-
487
-		return array_slice($results, 0, $limit);
488
-	}
489
-
490
-	private function recentSearch($limit, $offset, $folderMimetype, $filters) {
491
-		$dbconn = \OC::$server->getDatabaseConnection();
492
-		$builder = $dbconn->getQueryBuilder();
493
-		$query = $builder
494
-			->select('f.*')
495
-			->from('filecache', 'f');
496
-
497
-		/*
446
+        $filters = array_map(function (IMountPoint $mount) {
447
+            $storage = $mount->getStorage();
448
+
449
+            $storageId = $storage->getCache()->getNumericStorageId();
450
+            $prefix = '';
451
+
452
+            if ($storage->instanceOfStorage(Jail::class)) {
453
+                $prefix = $storage->getUnJailedPath('');
454
+            }
455
+
456
+            return [
457
+                'storageId' => $storageId,
458
+                'pathPrefix' => $prefix,
459
+            ];
460
+        }, $mounts);
461
+
462
+        // Search in batches of 500 entries
463
+        $searchLimit = 500;
464
+        $results = [];
465
+        $searchResultCount = 0;
466
+        $count = 0;
467
+        do {
468
+            $searchResult = $this->recentSearch($searchLimit, $offset, $folderMimetype, $filters);
469
+
470
+            // Exit condition if there are no more results
471
+            if (count($searchResult) === 0) {
472
+                break;
473
+            }
474
+
475
+            $searchResultCount += count($searchResult);
476
+
477
+            $parseResult = $this->recentParse($searchResult, $mountMap, $mimetypeLoader);
478
+
479
+            foreach ($parseResult as $result) {
480
+                $results[] = $result;
481
+            }
482
+
483
+            $offset += $searchLimit;
484
+            $count++;
485
+        } while (count($results) < $limit && ($searchResultCount < (3 * $limit) || $count < 5));
486
+
487
+        return array_slice($results, 0, $limit);
488
+    }
489
+
490
+    private function recentSearch($limit, $offset, $folderMimetype, $filters) {
491
+        $dbconn = \OC::$server->getDatabaseConnection();
492
+        $builder = $dbconn->getQueryBuilder();
493
+        $query = $builder
494
+            ->select('f.*')
495
+            ->from('filecache', 'f');
496
+
497
+        /*
498 498
 		 * Here is where we construct the filtering.
499 499
 		 * Note that this is expensive filtering as it is a lot of like queries.
500 500
 		 * However the alternative is we do this filtering and parsing later in php with the risk of looping endlessly
501 501
 		 */
502
-		$storageFilters = $builder->expr()->orX();
503
-		foreach ($filters as $filter) {
504
-			$storageFilter = $builder->expr()->andX(
505
-				$builder->expr()->eq('f.storage', $builder->createNamedParameter($filter['storageId']))
506
-			);
507
-
508
-			if ($filter['pathPrefix'] !== '') {
509
-				$storageFilter->add(
510
-					$builder->expr()->like('f.path', $builder->createNamedParameter($dbconn->escapeLikeParameter($filter['pathPrefix']) . '/%'))
511
-				);
512
-			}
513
-
514
-			$storageFilters->add($storageFilter);
515
-		}
516
-
517
-		$query->andWhere($storageFilters);
518
-
519
-		$query->andWhere($builder->expr()->orX(
520
-			// handle non empty folders separate
521
-				$builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)),
522
-				$builder->expr()->eq('f.size', new Literal(0))
523
-			))
524
-			->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_versions/%')))
525
-			->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_trashbin/%')))
526
-			->orderBy('f.mtime', 'DESC')
527
-			->setMaxResults($limit)
528
-			->setFirstResult($offset);
529
-
530
-		return $query->execute()->fetchAll();
531
-	}
532
-
533
-	private function recentParse($result, $mountMap, $mimetypeLoader) {
534
-		$files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) {
535
-			$mount = $mountMap[$entry['storage']];
536
-			$entry['internalPath'] = $entry['path'];
537
-			$entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']);
538
-			$entry['mimepart'] = $mimetypeLoader->getMimetypeById($entry['mimepart']);
539
-			$path = $this->getAbsolutePath($mount, $entry['path']);
540
-			if (is_null($path)) {
541
-				return null;
542
-			}
543
-			$fileInfo = new \OC\Files\FileInfo($path, $mount->getStorage(), $entry['internalPath'], $entry, $mount);
544
-			return $this->root->createNode($fileInfo->getPath(), $fileInfo);
545
-		}, $result));
546
-
547
-		return array_values(array_filter($files, function (Node $node) {
548
-			$cacheEntry = $node->getMountPoint()->getStorage()->getCache()->get($node->getId());
549
-			if (!$cacheEntry) {
550
-				return false;
551
-			}
552
-			$relative = $this->getRelativePath($node->getPath());
553
-			return $relative !== null && $relative !== '/'
554
-				&& ($cacheEntry->getPermissions() & \OCP\Constants::PERMISSION_READ) === \OCP\Constants::PERMISSION_READ;
555
-		}));
556
-	}
557
-
558
-	private function getAbsolutePath(IMountPoint $mount, $path) {
559
-		$storage = $mount->getStorage();
560
-		if ($storage->instanceOfStorage('\OC\Files\Storage\Wrapper\Jail')) {
561
-			if ($storage->instanceOfStorage(SharedStorage::class)) {
562
-				$storage->getSourceStorage();
563
-			}
564
-			/** @var \OC\Files\Storage\Wrapper\Jail $storage */
565
-			$jailRoot = $storage->getUnjailedPath('');
566
-			$rootLength = strlen($jailRoot) + 1;
567
-			if ($path === $jailRoot) {
568
-				return $mount->getMountPoint();
569
-			} elseif (substr($path, 0, $rootLength) === $jailRoot . '/') {
570
-				return $mount->getMountPoint() . substr($path, $rootLength);
571
-			} else {
572
-				return null;
573
-			}
574
-		} else {
575
-			return $mount->getMountPoint() . $path;
576
-		}
577
-	}
502
+        $storageFilters = $builder->expr()->orX();
503
+        foreach ($filters as $filter) {
504
+            $storageFilter = $builder->expr()->andX(
505
+                $builder->expr()->eq('f.storage', $builder->createNamedParameter($filter['storageId']))
506
+            );
507
+
508
+            if ($filter['pathPrefix'] !== '') {
509
+                $storageFilter->add(
510
+                    $builder->expr()->like('f.path', $builder->createNamedParameter($dbconn->escapeLikeParameter($filter['pathPrefix']) . '/%'))
511
+                );
512
+            }
513
+
514
+            $storageFilters->add($storageFilter);
515
+        }
516
+
517
+        $query->andWhere($storageFilters);
518
+
519
+        $query->andWhere($builder->expr()->orX(
520
+            // handle non empty folders separate
521
+                $builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)),
522
+                $builder->expr()->eq('f.size', new Literal(0))
523
+            ))
524
+            ->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_versions/%')))
525
+            ->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_trashbin/%')))
526
+            ->orderBy('f.mtime', 'DESC')
527
+            ->setMaxResults($limit)
528
+            ->setFirstResult($offset);
529
+
530
+        return $query->execute()->fetchAll();
531
+    }
532
+
533
+    private function recentParse($result, $mountMap, $mimetypeLoader) {
534
+        $files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) {
535
+            $mount = $mountMap[$entry['storage']];
536
+            $entry['internalPath'] = $entry['path'];
537
+            $entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']);
538
+            $entry['mimepart'] = $mimetypeLoader->getMimetypeById($entry['mimepart']);
539
+            $path = $this->getAbsolutePath($mount, $entry['path']);
540
+            if (is_null($path)) {
541
+                return null;
542
+            }
543
+            $fileInfo = new \OC\Files\FileInfo($path, $mount->getStorage(), $entry['internalPath'], $entry, $mount);
544
+            return $this->root->createNode($fileInfo->getPath(), $fileInfo);
545
+        }, $result));
546
+
547
+        return array_values(array_filter($files, function (Node $node) {
548
+            $cacheEntry = $node->getMountPoint()->getStorage()->getCache()->get($node->getId());
549
+            if (!$cacheEntry) {
550
+                return false;
551
+            }
552
+            $relative = $this->getRelativePath($node->getPath());
553
+            return $relative !== null && $relative !== '/'
554
+                && ($cacheEntry->getPermissions() & \OCP\Constants::PERMISSION_READ) === \OCP\Constants::PERMISSION_READ;
555
+        }));
556
+    }
557
+
558
+    private function getAbsolutePath(IMountPoint $mount, $path) {
559
+        $storage = $mount->getStorage();
560
+        if ($storage->instanceOfStorage('\OC\Files\Storage\Wrapper\Jail')) {
561
+            if ($storage->instanceOfStorage(SharedStorage::class)) {
562
+                $storage->getSourceStorage();
563
+            }
564
+            /** @var \OC\Files\Storage\Wrapper\Jail $storage */
565
+            $jailRoot = $storage->getUnjailedPath('');
566
+            $rootLength = strlen($jailRoot) + 1;
567
+            if ($path === $jailRoot) {
568
+                return $mount->getMountPoint();
569
+            } elseif (substr($path, 0, $rootLength) === $jailRoot . '/') {
570
+                return $mount->getMountPoint() . substr($path, $rootLength);
571
+            } else {
572
+                return null;
573
+            }
574
+        } else {
575
+            return $mount->getMountPoint() . $path;
576
+        }
577
+    }
578 578
 }
Please login to merge, or discard this patch.
Spacing   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -61,7 +61,7 @@  discard block
 block discarded – undo
61 61
 		if (!$this->isValidPath($path)) {
62 62
 			throw new NotPermittedException('Invalid path');
63 63
 		}
64
-		return $this->path . $this->normalizePath($path);
64
+		return $this->path.$this->normalizePath($path);
65 65
 	}
66 66
 
67 67
 	/**
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
 		}
75 75
 		if ($path === $this->path) {
76 76
 			return '/';
77
-		} elseif (strpos($path, $this->path . '/') !== 0) {
77
+		} elseif (strpos($path, $this->path.'/') !== 0) {
78 78
 			return null;
79 79
 		} else {
80 80
 			$path = substr($path, strlen($this->path));
@@ -89,7 +89,7 @@  discard block
 block discarded – undo
89 89
 	 * @return bool
90 90
 	 */
91 91
 	public function isSubNode($node) {
92
-		return strpos($node->getPath(), $this->path . '/') === 0;
92
+		return strpos($node->getPath(), $this->path.'/') === 0;
93 93
 	}
94 94
 
95 95
 	/**
@@ -101,7 +101,7 @@  discard block
 block discarded – undo
101 101
 	public function getDirectoryListing() {
102 102
 		$folderContent = $this->view->getDirectoryContent($this->path);
103 103
 
104
-		return array_map(function (FileInfo $info) {
104
+		return array_map(function(FileInfo $info) {
105 105
 			if ($info->getMimetype() === 'httpd/unix-directory') {
106 106
 				return new Folder($this->root, $this->view, $info->getPath(), $info);
107 107
 			} else {
@@ -207,7 +207,7 @@  discard block
 block discarded – undo
207 207
 	 */
208 208
 	public function search($query) {
209 209
 		if (is_string($query)) {
210
-			return $this->searchCommon('search', ['%' . $query . '%']);
210
+			return $this->searchCommon('search', ['%'.$query.'%']);
211 211
 		} else {
212 212
 			return $this->searchCommon('searchQuery', [$query]);
213 213
 		}
@@ -240,7 +240,7 @@  discard block
 block discarded – undo
240 240
 	 * @return \OC\Files\Node\Node[]
241 241
 	 */
242 242
 	private function searchCommon($method, $args) {
243
-		$limitToHome = ($method === 'searchQuery')? $args[0]->limitToHome(): false;
243
+		$limitToHome = ($method === 'searchQuery') ? $args[0]->limitToHome() : false;
244 244
 		if ($limitToHome && count(explode('/', $this->path)) !== 3) {
245 245
 			throw new \InvalidArgumentException('searching by owner is only allows on the users home folder');
246 246
 		}
@@ -252,7 +252,7 @@  discard block
 block discarded – undo
252 252
 		$internalPath = $mount->getInternalPath($this->path);
253 253
 		$internalPath = rtrim($internalPath, '/');
254 254
 		if ($internalPath !== '') {
255
-			$internalPath = $internalPath . '/';
255
+			$internalPath = $internalPath.'/';
256 256
 		}
257 257
 		$internalRootLength = strlen($internalPath);
258 258
 
@@ -264,7 +264,7 @@  discard block
 block discarded – undo
264 264
 				$result['internalPath'] = $result['path'];
265 265
 				$result['path'] = substr($result['path'], $internalRootLength);
266 266
 				$result['storage'] = $storage;
267
-				$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
267
+				$files[] = new \OC\Files\FileInfo($this->path.'/'.$result['path'], $storage, $result['internalPath'], $result, $mount);
268 268
 			}
269 269
 		}
270 270
 
@@ -279,16 +279,16 @@  discard block
 block discarded – undo
279 279
 					$results = call_user_func_array([$cache, $method], $args);
280 280
 					foreach ($results as $result) {
281 281
 						$result['internalPath'] = $result['path'];
282
-						$result['path'] = $relativeMountPoint . $result['path'];
282
+						$result['path'] = $relativeMountPoint.$result['path'];
283 283
 						$result['storage'] = $storage;
284
-						$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage,
284
+						$files[] = new \OC\Files\FileInfo($this->path.'/'.$result['path'], $storage,
285 285
 							$result['internalPath'], $result, $mount);
286 286
 					}
287 287
 				}
288 288
 			}
289 289
 		}
290 290
 
291
-		return array_map(function (FileInfo $file) {
291
+		return array_map(function(FileInfo $file) {
292 292
 			return $this->createNode($file->getPath(), $file);
293 293
 		}, $files);
294 294
 	}
@@ -304,16 +304,16 @@  discard block
 block discarded – undo
304 304
 		} else {
305 305
 			$user = null;
306 306
 		}
307
-		$mountsContainingFile = $mountCache->getMountsForFileId((int)$id, $user);
307
+		$mountsContainingFile = $mountCache->getMountsForFileId((int) $id, $user);
308 308
 		$mounts = $this->root->getMountsIn($this->path);
309 309
 		$mounts[] = $this->root->getMount($this->path);
310 310
 		/** @var IMountPoint[] $folderMounts */
311
-		$folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) {
311
+		$folderMounts = array_combine(array_map(function(IMountPoint $mountPoint) {
312 312
 			return $mountPoint->getMountPoint();
313 313
 		}, $mounts), $mounts);
314 314
 
315 315
 		/** @var ICachedMountInfo[] $mountsContainingFile */
316
-		$mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) {
316
+		$mountsContainingFile = array_values(array_filter($mountsContainingFile, function(ICachedMountInfo $cachedMountInfo) use ($folderMounts) {
317 317
 			return isset($folderMounts[$cachedMountInfo->getMountPoint()]);
318 318
 		}));
319 319
 
@@ -324,18 +324,18 @@  discard block
 block discarded – undo
324 324
 			return [];
325 325
 		}
326 326
 
327
-		$nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($folderMounts, $id) {
327
+		$nodes = array_map(function(ICachedMountInfo $cachedMountInfo) use ($folderMounts, $id) {
328 328
 			$mount = $folderMounts[$cachedMountInfo->getMountPoint()];
329
-			$cacheEntry = $mount->getStorage()->getCache()->get((int)$id);
329
+			$cacheEntry = $mount->getStorage()->getCache()->get((int) $id);
330 330
 			if (!$cacheEntry) {
331 331
 				return null;
332 332
 			}
333 333
 
334 334
 			// cache jails will hide the "true" internal path
335
-			$internalPath = ltrim($cachedMountInfo->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/');
335
+			$internalPath = ltrim($cachedMountInfo->getRootInternalPath().'/'.$cacheEntry->getPath(), '/');
336 336
 			$pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath()));
337 337
 			$pathRelativeToMount = ltrim($pathRelativeToMount, '/');
338
-			$absolutePath = rtrim($cachedMountInfo->getMountPoint() . $pathRelativeToMount, '/');
338
+			$absolutePath = rtrim($cachedMountInfo->getMountPoint().$pathRelativeToMount, '/');
339 339
 			return $this->root->createNode($absolutePath, new \OC\Files\FileInfo(
340 340
 				$absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount,
341 341
 				\OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount))
@@ -344,14 +344,14 @@  discard block
 block discarded – undo
344 344
 
345 345
 		$nodes = array_filter($nodes);
346 346
 
347
-		return array_filter($nodes, function (Node $node) {
347
+		return array_filter($nodes, function(Node $node) {
348 348
 			return $this->getRelativePath($node->getPath());
349 349
 		});
350 350
 	}
351 351
 
352 352
 	protected function getAppDataDirectoryName(): string {
353 353
 		$instanceId = \OC::$server->getConfig()->getSystemValueString('instanceid');
354
-		return 'appdata_' . $instanceId;
354
+		return 'appdata_'.$instanceId;
355 355
 	}
356 356
 
357 357
 	/**
@@ -373,8 +373,8 @@  discard block
 block discarded – undo
373 373
 			return [];
374 374
 		}
375 375
 
376
-		$absolutePath = '/' . ltrim($cacheEntry->getPath(), '/');
377
-		$currentPath = rtrim($this->path, '/') . '/';
376
+		$absolutePath = '/'.ltrim($cacheEntry->getPath(), '/');
377
+		$currentPath = rtrim($this->path, '/').'/';
378 378
 
379 379
 		if (strpos($absolutePath, $currentPath) !== 0) {
380 380
 			return [];
@@ -429,10 +429,10 @@  discard block
 block discarded – undo
429 429
 		$mounts = $this->root->getMountsIn($this->path);
430 430
 		$mounts[] = $this->getMountPoint();
431 431
 
432
-		$mounts = array_filter($mounts, function (IMountPoint $mount) {
432
+		$mounts = array_filter($mounts, function(IMountPoint $mount) {
433 433
 			return $mount->getStorage();
434 434
 		});
435
-		$storageIds = array_map(function (IMountPoint $mount) {
435
+		$storageIds = array_map(function(IMountPoint $mount) {
436 436
 			return $mount->getStorage()->getCache()->getNumericStorageId();
437 437
 		}, $mounts);
438 438
 		/** @var IMountPoint[] $mountMap */
@@ -443,7 +443,7 @@  discard block
 block discarded – undo
443 443
 		 * Construct an array of the storage id with their prefix path
444 444
 		 * This helps us to filter in the final query
445 445
 		 */
446
-		$filters = array_map(function (IMountPoint $mount) {
446
+		$filters = array_map(function(IMountPoint $mount) {
447 447
 			$storage = $mount->getStorage();
448 448
 
449 449
 			$storageId = $storage->getCache()->getNumericStorageId();
@@ -507,7 +507,7 @@  discard block
 block discarded – undo
507 507
 
508 508
 			if ($filter['pathPrefix'] !== '') {
509 509
 				$storageFilter->add(
510
-					$builder->expr()->like('f.path', $builder->createNamedParameter($dbconn->escapeLikeParameter($filter['pathPrefix']) . '/%'))
510
+					$builder->expr()->like('f.path', $builder->createNamedParameter($dbconn->escapeLikeParameter($filter['pathPrefix']).'/%'))
511 511
 				);
512 512
 			}
513 513
 
@@ -531,7 +531,7 @@  discard block
 block discarded – undo
531 531
 	}
532 532
 
533 533
 	private function recentParse($result, $mountMap, $mimetypeLoader) {
534
-		$files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) {
534
+		$files = array_filter(array_map(function(array $entry) use ($mountMap, $mimetypeLoader) {
535 535
 			$mount = $mountMap[$entry['storage']];
536 536
 			$entry['internalPath'] = $entry['path'];
537 537
 			$entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']);
@@ -544,7 +544,7 @@  discard block
 block discarded – undo
544 544
 			return $this->root->createNode($fileInfo->getPath(), $fileInfo);
545 545
 		}, $result));
546 546
 
547
-		return array_values(array_filter($files, function (Node $node) {
547
+		return array_values(array_filter($files, function(Node $node) {
548 548
 			$cacheEntry = $node->getMountPoint()->getStorage()->getCache()->get($node->getId());
549 549
 			if (!$cacheEntry) {
550 550
 				return false;
@@ -566,13 +566,13 @@  discard block
 block discarded – undo
566 566
 			$rootLength = strlen($jailRoot) + 1;
567 567
 			if ($path === $jailRoot) {
568 568
 				return $mount->getMountPoint();
569
-			} elseif (substr($path, 0, $rootLength) === $jailRoot . '/') {
570
-				return $mount->getMountPoint() . substr($path, $rootLength);
569
+			} elseif (substr($path, 0, $rootLength) === $jailRoot.'/') {
570
+				return $mount->getMountPoint().substr($path, $rootLength);
571 571
 			} else {
572 572
 				return null;
573 573
 			}
574 574
 		} else {
575
-			return $mount->getMountPoint() . $path;
575
+			return $mount->getMountPoint().$path;
576 576
 		}
577 577
 	}
578 578
 }
Please login to merge, or discard this patch.
apps/files_sharing/lib/SharedStorage.php 1 patch
Indentation   +460 added lines, -460 removed lines patch added patch discarded remove patch
@@ -50,464 +50,464 @@
 block discarded – undo
50 50
  */
51 51
 class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage, IDisableEncryptionStorage {
52 52
 
53
-	/** @var \OCP\Share\IShare */
54
-	private $superShare;
55
-
56
-	/** @var \OCP\Share\IShare[] */
57
-	private $groupedShares;
58
-
59
-	/**
60
-	 * @var \OC\Files\View
61
-	 */
62
-	private $ownerView;
63
-
64
-	private $initialized = false;
65
-
66
-	/**
67
-	 * @var ICacheEntry
68
-	 */
69
-	private $sourceRootInfo;
70
-
71
-	/** @var string */
72
-	private $user;
73
-
74
-	/**
75
-	 * @var \OCP\ILogger
76
-	 */
77
-	private $logger;
78
-
79
-	/** @var  IStorage */
80
-	private $nonMaskedStorage;
81
-
82
-	private $options;
83
-
84
-	/** @var boolean */
85
-	private $sharingDisabledForUser;
86
-
87
-	public function __construct($arguments) {
88
-		$this->ownerView = $arguments['ownerView'];
89
-		$this->logger = \OC::$server->getLogger();
90
-
91
-		$this->superShare = $arguments['superShare'];
92
-		$this->groupedShares = $arguments['groupedShares'];
93
-
94
-		$this->user = $arguments['user'];
95
-		if (isset($arguments['sharingDisabledForUser'])) {
96
-			$this->sharingDisabledForUser = $arguments['sharingDisabledForUser'];
97
-		} else {
98
-			$this->sharingDisabledForUser = false;
99
-		}
100
-
101
-		parent::__construct([
102
-			'storage' => null,
103
-			'root' => null,
104
-		]);
105
-	}
106
-
107
-	/**
108
-	 * @return ICacheEntry
109
-	 */
110
-	private function getSourceRootInfo() {
111
-		if (is_null($this->sourceRootInfo)) {
112
-			if (is_null($this->superShare->getNodeCacheEntry())) {
113
-				$this->init();
114
-				$this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath);
115
-			} else {
116
-				$this->sourceRootInfo = $this->superShare->getNodeCacheEntry();
117
-			}
118
-		}
119
-		return $this->sourceRootInfo;
120
-	}
121
-
122
-	private function init() {
123
-		if ($this->initialized) {
124
-			return;
125
-		}
126
-		$this->initialized = true;
127
-		try {
128
-			Filesystem::initMountPoints($this->superShare->getShareOwner());
129
-			$sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
130
-			list($this->nonMaskedStorage, $this->rootPath) = $this->ownerView->resolvePath($sourcePath);
131
-			$this->storage = new PermissionsMask([
132
-				'storage' => $this->nonMaskedStorage,
133
-				'mask' => $this->superShare->getPermissions()
134
-			]);
135
-		} catch (NotFoundException $e) {
136
-			// original file not accessible or deleted, set FailedStorage
137
-			$this->storage = new FailedStorage(['exception' => $e]);
138
-			$this->cache = new FailedCache();
139
-			$this->rootPath = '';
140
-		} catch (NoUserException $e) {
141
-			// sharer user deleted, set FailedStorage
142
-			$this->storage = new FailedStorage(['exception' => $e]);
143
-			$this->cache = new FailedCache();
144
-			$this->rootPath = '';
145
-		} catch (\Exception $e) {
146
-			$this->storage = new FailedStorage(['exception' => $e]);
147
-			$this->cache = new FailedCache();
148
-			$this->rootPath = '';
149
-			$this->logger->logException($e);
150
-		}
151
-
152
-		if (!$this->nonMaskedStorage) {
153
-			$this->nonMaskedStorage = $this->storage;
154
-		}
155
-	}
156
-
157
-	/**
158
-	 * @inheritdoc
159
-	 */
160
-	public function instanceOfStorage($class) {
161
-		if ($class === '\OC\Files\Storage\Common') {
162
-			return true;
163
-		}
164
-		if (in_array($class, ['\OC\Files\Storage\Home', '\OC\Files\ObjectStore\HomeObjectStoreStorage'])) {
165
-			return false;
166
-		}
167
-		return parent::instanceOfStorage($class);
168
-	}
169
-
170
-	/**
171
-	 * @return string
172
-	 */
173
-	public function getShareId() {
174
-		return $this->superShare->getId();
175
-	}
176
-
177
-	private function isValid() {
178
-		return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE;
179
-	}
180
-
181
-	/**
182
-	 * get id of the mount point
183
-	 *
184
-	 * @return string
185
-	 */
186
-	public function getId() {
187
-		return 'shared::' . $this->getMountPoint();
188
-	}
189
-
190
-	/**
191
-	 * Get the permissions granted for a shared file
192
-	 *
193
-	 * @param string $target Shared target file path
194
-	 * @return int CRUDS permissions granted
195
-	 */
196
-	public function getPermissions($target = '') {
197
-		if (!$this->isValid()) {
198
-			return 0;
199
-		}
200
-		$permissions = parent::getPermissions($target) & $this->superShare->getPermissions();
201
-
202
-		// part files and the mount point always have delete permissions
203
-		if ($target === '' || pathinfo($target, PATHINFO_EXTENSION) === 'part') {
204
-			$permissions |= \OCP\Constants::PERMISSION_DELETE;
205
-		}
206
-
207
-		if ($this->sharingDisabledForUser) {
208
-			$permissions &= ~\OCP\Constants::PERMISSION_SHARE;
209
-		}
210
-
211
-		return $permissions;
212
-	}
213
-
214
-	public function isCreatable($path) {
215
-		return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE);
216
-	}
217
-
218
-	public function isReadable($path) {
219
-		if (!$this->isValid()) {
220
-			return false;
221
-		}
222
-		if (!$this->file_exists($path)) {
223
-			return false;
224
-		}
225
-		/** @var IStorage $storage */
226
-		/** @var string $internalPath */
227
-		list($storage, $internalPath) = $this->resolvePath($path);
228
-		return $storage->isReadable($internalPath);
229
-	}
230
-
231
-	public function isUpdatable($path) {
232
-		return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE);
233
-	}
234
-
235
-	public function isDeletable($path) {
236
-		return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE);
237
-	}
238
-
239
-	public function isSharable($path) {
240
-		if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) {
241
-			return false;
242
-		}
243
-		return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE);
244
-	}
245
-
246
-	public function fopen($path, $mode) {
247
-		if ($source = $this->getUnjailedPath($path)) {
248
-			switch ($mode) {
249
-				case 'r+':
250
-				case 'rb+':
251
-				case 'w+':
252
-				case 'wb+':
253
-				case 'x+':
254
-				case 'xb+':
255
-				case 'a+':
256
-				case 'ab+':
257
-				case 'w':
258
-				case 'wb':
259
-				case 'x':
260
-				case 'xb':
261
-				case 'a':
262
-				case 'ab':
263
-					$creatable = $this->isCreatable(dirname($path));
264
-					$updatable = $this->isUpdatable($path);
265
-					// if neither permissions given, no need to continue
266
-					if (!$creatable && !$updatable) {
267
-						if (pathinfo($path, PATHINFO_EXTENSION) === 'part') {
268
-							$updatable = $this->isUpdatable(dirname($path));
269
-						}
270
-
271
-						if (!$updatable) {
272
-							return false;
273
-						}
274
-					}
275
-
276
-					$exists = $this->file_exists($path);
277
-					// if a file exists, updatable permissions are required
278
-					if ($exists && !$updatable) {
279
-						return false;
280
-					}
281
-
282
-					// part file is allowed if !$creatable but the final file is $updatable
283
-					if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') {
284
-						if (!$exists && !$creatable) {
285
-							return false;
286
-						}
287
-					}
288
-			}
289
-			$info = [
290
-				'target' => $this->getMountPoint() . $path,
291
-				'source' => $source,
292
-				'mode' => $mode,
293
-			];
294
-			\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info);
295
-			return $this->nonMaskedStorage->fopen($this->getUnjailedPath($path), $mode);
296
-		}
297
-		return false;
298
-	}
299
-
300
-	/**
301
-	 * see http://php.net/manual/en/function.rename.php
302
-	 *
303
-	 * @param string $path1
304
-	 * @param string $path2
305
-	 * @return bool
306
-	 */
307
-	public function rename($path1, $path2) {
308
-		$this->init();
309
-		$isPartFile = pathinfo($path1, PATHINFO_EXTENSION) === 'part';
310
-		$targetExists = $this->file_exists($path2);
311
-		$sameFodler = dirname($path1) === dirname($path2);
312
-
313
-		if ($targetExists || ($sameFodler && !$isPartFile)) {
314
-			if (!$this->isUpdatable('')) {
315
-				return false;
316
-			}
317
-		} else {
318
-			if (!$this->isCreatable('')) {
319
-				return false;
320
-			}
321
-		}
322
-
323
-		return $this->nonMaskedStorage->rename($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
324
-	}
325
-
326
-	/**
327
-	 * return mount point of share, relative to data/user/files
328
-	 *
329
-	 * @return string
330
-	 */
331
-	public function getMountPoint() {
332
-		return $this->superShare->getTarget();
333
-	}
334
-
335
-	/**
336
-	 * @param string $path
337
-	 */
338
-	public function setMountPoint($path) {
339
-		$this->superShare->setTarget($path);
340
-
341
-		foreach ($this->groupedShares as $share) {
342
-			$share->setTarget($path);
343
-		}
344
-	}
345
-
346
-	/**
347
-	 * get the user who shared the file
348
-	 *
349
-	 * @return string
350
-	 */
351
-	public function getSharedFrom() {
352
-		return $this->superShare->getShareOwner();
353
-	}
354
-
355
-	/**
356
-	 * @return \OCP\Share\IShare
357
-	 */
358
-	public function getShare() {
359
-		return $this->superShare;
360
-	}
361
-
362
-	/**
363
-	 * return share type, can be "file" or "folder"
364
-	 *
365
-	 * @return string
366
-	 */
367
-	public function getItemType() {
368
-		return $this->superShare->getNodeType();
369
-	}
370
-
371
-	/**
372
-	 * @param string $path
373
-	 * @param null $storage
374
-	 * @return Cache
375
-	 */
376
-	public function getCache($path = '', $storage = null) {
377
-		if ($this->cache) {
378
-			return $this->cache;
379
-		}
380
-		if (!$storage) {
381
-			$storage = $this;
382
-		}
383
-		$sourceRoot  = $this->getSourceRootInfo();
384
-		if ($this->storage instanceof FailedStorage) {
385
-			return new FailedCache();
386
-		}
387
-
388
-		$this->cache = new \OCA\Files_Sharing\Cache($storage, $sourceRoot, $this->superShare);
389
-		return $this->cache;
390
-	}
391
-
392
-	public function getScanner($path = '', $storage = null) {
393
-		if (!$storage) {
394
-			$storage = $this;
395
-		}
396
-		return new \OCA\Files_Sharing\Scanner($storage);
397
-	}
398
-
399
-	public function getOwner($path) {
400
-		return $this->superShare->getShareOwner();
401
-	}
402
-
403
-	/**
404
-	 * unshare complete storage, also the grouped shares
405
-	 *
406
-	 * @return bool
407
-	 */
408
-	public function unshareStorage() {
409
-		foreach ($this->groupedShares as $share) {
410
-			\OC::$server->getShareManager()->deleteFromSelf($share, $this->user);
411
-		}
412
-		return true;
413
-	}
414
-
415
-	/**
416
-	 * @param string $path
417
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
418
-	 * @param \OCP\Lock\ILockingProvider $provider
419
-	 * @throws \OCP\Lock\LockedException
420
-	 */
421
-	public function acquireLock($path, $type, ILockingProvider $provider) {
422
-		/** @var \OCP\Files\Storage $targetStorage */
423
-		list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
424
-		$targetStorage->acquireLock($targetInternalPath, $type, $provider);
425
-		// lock the parent folders of the owner when locking the share as recipient
426
-		if ($path === '') {
427
-			$sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
428
-			$this->ownerView->lockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
429
-		}
430
-	}
431
-
432
-	/**
433
-	 * @param string $path
434
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
435
-	 * @param \OCP\Lock\ILockingProvider $provider
436
-	 */
437
-	public function releaseLock($path, $type, ILockingProvider $provider) {
438
-		/** @var \OCP\Files\Storage $targetStorage */
439
-		list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
440
-		$targetStorage->releaseLock($targetInternalPath, $type, $provider);
441
-		// unlock the parent folders of the owner when unlocking the share as recipient
442
-		if ($path === '') {
443
-			$sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
444
-			$this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
445
-		}
446
-	}
447
-
448
-	/**
449
-	 * @param string $path
450
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
451
-	 * @param \OCP\Lock\ILockingProvider $provider
452
-	 */
453
-	public function changeLock($path, $type, ILockingProvider $provider) {
454
-		/** @var \OCP\Files\Storage $targetStorage */
455
-		list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
456
-		$targetStorage->changeLock($targetInternalPath, $type, $provider);
457
-	}
458
-
459
-	/**
460
-	 * @return array [ available, last_checked ]
461
-	 */
462
-	public function getAvailability() {
463
-		// shares do not participate in availability logic
464
-		return [
465
-			'available' => true,
466
-			'last_checked' => 0
467
-		];
468
-	}
469
-
470
-	/**
471
-	 * @param bool $available
472
-	 */
473
-	public function setAvailability($available) {
474
-		// shares do not participate in availability logic
475
-	}
476
-
477
-	public function getSourceStorage() {
478
-		$this->init();
479
-		return $this->nonMaskedStorage;
480
-	}
481
-
482
-	public function getWrapperStorage() {
483
-		$this->init();
484
-		return $this->storage;
485
-	}
486
-
487
-	public function file_get_contents($path) {
488
-		$info = [
489
-			'target' => $this->getMountPoint() . '/' . $path,
490
-			'source' => $this->getUnjailedPath($path),
491
-		];
492
-		\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
493
-		return parent::file_get_contents($path);
494
-	}
495
-
496
-	public function file_put_contents($path, $data) {
497
-		$info = [
498
-			'target' => $this->getMountPoint() . '/' . $path,
499
-			'source' => $this->getUnjailedPath($path),
500
-		];
501
-		\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
502
-		return parent::file_put_contents($path, $data);
503
-	}
504
-
505
-	public function setMountOptions(array $options) {
506
-		$this->mountOptions = $options;
507
-	}
508
-
509
-	public function getUnjailedPath($path) {
510
-		$this->init();
511
-		return parent::getUnjailedPath($path);
512
-	}
53
+    /** @var \OCP\Share\IShare */
54
+    private $superShare;
55
+
56
+    /** @var \OCP\Share\IShare[] */
57
+    private $groupedShares;
58
+
59
+    /**
60
+     * @var \OC\Files\View
61
+     */
62
+    private $ownerView;
63
+
64
+    private $initialized = false;
65
+
66
+    /**
67
+     * @var ICacheEntry
68
+     */
69
+    private $sourceRootInfo;
70
+
71
+    /** @var string */
72
+    private $user;
73
+
74
+    /**
75
+     * @var \OCP\ILogger
76
+     */
77
+    private $logger;
78
+
79
+    /** @var  IStorage */
80
+    private $nonMaskedStorage;
81
+
82
+    private $options;
83
+
84
+    /** @var boolean */
85
+    private $sharingDisabledForUser;
86
+
87
+    public function __construct($arguments) {
88
+        $this->ownerView = $arguments['ownerView'];
89
+        $this->logger = \OC::$server->getLogger();
90
+
91
+        $this->superShare = $arguments['superShare'];
92
+        $this->groupedShares = $arguments['groupedShares'];
93
+
94
+        $this->user = $arguments['user'];
95
+        if (isset($arguments['sharingDisabledForUser'])) {
96
+            $this->sharingDisabledForUser = $arguments['sharingDisabledForUser'];
97
+        } else {
98
+            $this->sharingDisabledForUser = false;
99
+        }
100
+
101
+        parent::__construct([
102
+            'storage' => null,
103
+            'root' => null,
104
+        ]);
105
+    }
106
+
107
+    /**
108
+     * @return ICacheEntry
109
+     */
110
+    private function getSourceRootInfo() {
111
+        if (is_null($this->sourceRootInfo)) {
112
+            if (is_null($this->superShare->getNodeCacheEntry())) {
113
+                $this->init();
114
+                $this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath);
115
+            } else {
116
+                $this->sourceRootInfo = $this->superShare->getNodeCacheEntry();
117
+            }
118
+        }
119
+        return $this->sourceRootInfo;
120
+    }
121
+
122
+    private function init() {
123
+        if ($this->initialized) {
124
+            return;
125
+        }
126
+        $this->initialized = true;
127
+        try {
128
+            Filesystem::initMountPoints($this->superShare->getShareOwner());
129
+            $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
130
+            list($this->nonMaskedStorage, $this->rootPath) = $this->ownerView->resolvePath($sourcePath);
131
+            $this->storage = new PermissionsMask([
132
+                'storage' => $this->nonMaskedStorage,
133
+                'mask' => $this->superShare->getPermissions()
134
+            ]);
135
+        } catch (NotFoundException $e) {
136
+            // original file not accessible or deleted, set FailedStorage
137
+            $this->storage = new FailedStorage(['exception' => $e]);
138
+            $this->cache = new FailedCache();
139
+            $this->rootPath = '';
140
+        } catch (NoUserException $e) {
141
+            // sharer user deleted, set FailedStorage
142
+            $this->storage = new FailedStorage(['exception' => $e]);
143
+            $this->cache = new FailedCache();
144
+            $this->rootPath = '';
145
+        } catch (\Exception $e) {
146
+            $this->storage = new FailedStorage(['exception' => $e]);
147
+            $this->cache = new FailedCache();
148
+            $this->rootPath = '';
149
+            $this->logger->logException($e);
150
+        }
151
+
152
+        if (!$this->nonMaskedStorage) {
153
+            $this->nonMaskedStorage = $this->storage;
154
+        }
155
+    }
156
+
157
+    /**
158
+     * @inheritdoc
159
+     */
160
+    public function instanceOfStorage($class) {
161
+        if ($class === '\OC\Files\Storage\Common') {
162
+            return true;
163
+        }
164
+        if (in_array($class, ['\OC\Files\Storage\Home', '\OC\Files\ObjectStore\HomeObjectStoreStorage'])) {
165
+            return false;
166
+        }
167
+        return parent::instanceOfStorage($class);
168
+    }
169
+
170
+    /**
171
+     * @return string
172
+     */
173
+    public function getShareId() {
174
+        return $this->superShare->getId();
175
+    }
176
+
177
+    private function isValid() {
178
+        return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE;
179
+    }
180
+
181
+    /**
182
+     * get id of the mount point
183
+     *
184
+     * @return string
185
+     */
186
+    public function getId() {
187
+        return 'shared::' . $this->getMountPoint();
188
+    }
189
+
190
+    /**
191
+     * Get the permissions granted for a shared file
192
+     *
193
+     * @param string $target Shared target file path
194
+     * @return int CRUDS permissions granted
195
+     */
196
+    public function getPermissions($target = '') {
197
+        if (!$this->isValid()) {
198
+            return 0;
199
+        }
200
+        $permissions = parent::getPermissions($target) & $this->superShare->getPermissions();
201
+
202
+        // part files and the mount point always have delete permissions
203
+        if ($target === '' || pathinfo($target, PATHINFO_EXTENSION) === 'part') {
204
+            $permissions |= \OCP\Constants::PERMISSION_DELETE;
205
+        }
206
+
207
+        if ($this->sharingDisabledForUser) {
208
+            $permissions &= ~\OCP\Constants::PERMISSION_SHARE;
209
+        }
210
+
211
+        return $permissions;
212
+    }
213
+
214
+    public function isCreatable($path) {
215
+        return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE);
216
+    }
217
+
218
+    public function isReadable($path) {
219
+        if (!$this->isValid()) {
220
+            return false;
221
+        }
222
+        if (!$this->file_exists($path)) {
223
+            return false;
224
+        }
225
+        /** @var IStorage $storage */
226
+        /** @var string $internalPath */
227
+        list($storage, $internalPath) = $this->resolvePath($path);
228
+        return $storage->isReadable($internalPath);
229
+    }
230
+
231
+    public function isUpdatable($path) {
232
+        return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE);
233
+    }
234
+
235
+    public function isDeletable($path) {
236
+        return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE);
237
+    }
238
+
239
+    public function isSharable($path) {
240
+        if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) {
241
+            return false;
242
+        }
243
+        return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE);
244
+    }
245
+
246
+    public function fopen($path, $mode) {
247
+        if ($source = $this->getUnjailedPath($path)) {
248
+            switch ($mode) {
249
+                case 'r+':
250
+                case 'rb+':
251
+                case 'w+':
252
+                case 'wb+':
253
+                case 'x+':
254
+                case 'xb+':
255
+                case 'a+':
256
+                case 'ab+':
257
+                case 'w':
258
+                case 'wb':
259
+                case 'x':
260
+                case 'xb':
261
+                case 'a':
262
+                case 'ab':
263
+                    $creatable = $this->isCreatable(dirname($path));
264
+                    $updatable = $this->isUpdatable($path);
265
+                    // if neither permissions given, no need to continue
266
+                    if (!$creatable && !$updatable) {
267
+                        if (pathinfo($path, PATHINFO_EXTENSION) === 'part') {
268
+                            $updatable = $this->isUpdatable(dirname($path));
269
+                        }
270
+
271
+                        if (!$updatable) {
272
+                            return false;
273
+                        }
274
+                    }
275
+
276
+                    $exists = $this->file_exists($path);
277
+                    // if a file exists, updatable permissions are required
278
+                    if ($exists && !$updatable) {
279
+                        return false;
280
+                    }
281
+
282
+                    // part file is allowed if !$creatable but the final file is $updatable
283
+                    if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') {
284
+                        if (!$exists && !$creatable) {
285
+                            return false;
286
+                        }
287
+                    }
288
+            }
289
+            $info = [
290
+                'target' => $this->getMountPoint() . $path,
291
+                'source' => $source,
292
+                'mode' => $mode,
293
+            ];
294
+            \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info);
295
+            return $this->nonMaskedStorage->fopen($this->getUnjailedPath($path), $mode);
296
+        }
297
+        return false;
298
+    }
299
+
300
+    /**
301
+     * see http://php.net/manual/en/function.rename.php
302
+     *
303
+     * @param string $path1
304
+     * @param string $path2
305
+     * @return bool
306
+     */
307
+    public function rename($path1, $path2) {
308
+        $this->init();
309
+        $isPartFile = pathinfo($path1, PATHINFO_EXTENSION) === 'part';
310
+        $targetExists = $this->file_exists($path2);
311
+        $sameFodler = dirname($path1) === dirname($path2);
312
+
313
+        if ($targetExists || ($sameFodler && !$isPartFile)) {
314
+            if (!$this->isUpdatable('')) {
315
+                return false;
316
+            }
317
+        } else {
318
+            if (!$this->isCreatable('')) {
319
+                return false;
320
+            }
321
+        }
322
+
323
+        return $this->nonMaskedStorage->rename($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
324
+    }
325
+
326
+    /**
327
+     * return mount point of share, relative to data/user/files
328
+     *
329
+     * @return string
330
+     */
331
+    public function getMountPoint() {
332
+        return $this->superShare->getTarget();
333
+    }
334
+
335
+    /**
336
+     * @param string $path
337
+     */
338
+    public function setMountPoint($path) {
339
+        $this->superShare->setTarget($path);
340
+
341
+        foreach ($this->groupedShares as $share) {
342
+            $share->setTarget($path);
343
+        }
344
+    }
345
+
346
+    /**
347
+     * get the user who shared the file
348
+     *
349
+     * @return string
350
+     */
351
+    public function getSharedFrom() {
352
+        return $this->superShare->getShareOwner();
353
+    }
354
+
355
+    /**
356
+     * @return \OCP\Share\IShare
357
+     */
358
+    public function getShare() {
359
+        return $this->superShare;
360
+    }
361
+
362
+    /**
363
+     * return share type, can be "file" or "folder"
364
+     *
365
+     * @return string
366
+     */
367
+    public function getItemType() {
368
+        return $this->superShare->getNodeType();
369
+    }
370
+
371
+    /**
372
+     * @param string $path
373
+     * @param null $storage
374
+     * @return Cache
375
+     */
376
+    public function getCache($path = '', $storage = null) {
377
+        if ($this->cache) {
378
+            return $this->cache;
379
+        }
380
+        if (!$storage) {
381
+            $storage = $this;
382
+        }
383
+        $sourceRoot  = $this->getSourceRootInfo();
384
+        if ($this->storage instanceof FailedStorage) {
385
+            return new FailedCache();
386
+        }
387
+
388
+        $this->cache = new \OCA\Files_Sharing\Cache($storage, $sourceRoot, $this->superShare);
389
+        return $this->cache;
390
+    }
391
+
392
+    public function getScanner($path = '', $storage = null) {
393
+        if (!$storage) {
394
+            $storage = $this;
395
+        }
396
+        return new \OCA\Files_Sharing\Scanner($storage);
397
+    }
398
+
399
+    public function getOwner($path) {
400
+        return $this->superShare->getShareOwner();
401
+    }
402
+
403
+    /**
404
+     * unshare complete storage, also the grouped shares
405
+     *
406
+     * @return bool
407
+     */
408
+    public function unshareStorage() {
409
+        foreach ($this->groupedShares as $share) {
410
+            \OC::$server->getShareManager()->deleteFromSelf($share, $this->user);
411
+        }
412
+        return true;
413
+    }
414
+
415
+    /**
416
+     * @param string $path
417
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
418
+     * @param \OCP\Lock\ILockingProvider $provider
419
+     * @throws \OCP\Lock\LockedException
420
+     */
421
+    public function acquireLock($path, $type, ILockingProvider $provider) {
422
+        /** @var \OCP\Files\Storage $targetStorage */
423
+        list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
424
+        $targetStorage->acquireLock($targetInternalPath, $type, $provider);
425
+        // lock the parent folders of the owner when locking the share as recipient
426
+        if ($path === '') {
427
+            $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
428
+            $this->ownerView->lockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
429
+        }
430
+    }
431
+
432
+    /**
433
+     * @param string $path
434
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
435
+     * @param \OCP\Lock\ILockingProvider $provider
436
+     */
437
+    public function releaseLock($path, $type, ILockingProvider $provider) {
438
+        /** @var \OCP\Files\Storage $targetStorage */
439
+        list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
440
+        $targetStorage->releaseLock($targetInternalPath, $type, $provider);
441
+        // unlock the parent folders of the owner when unlocking the share as recipient
442
+        if ($path === '') {
443
+            $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
444
+            $this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
445
+        }
446
+    }
447
+
448
+    /**
449
+     * @param string $path
450
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
451
+     * @param \OCP\Lock\ILockingProvider $provider
452
+     */
453
+    public function changeLock($path, $type, ILockingProvider $provider) {
454
+        /** @var \OCP\Files\Storage $targetStorage */
455
+        list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
456
+        $targetStorage->changeLock($targetInternalPath, $type, $provider);
457
+    }
458
+
459
+    /**
460
+     * @return array [ available, last_checked ]
461
+     */
462
+    public function getAvailability() {
463
+        // shares do not participate in availability logic
464
+        return [
465
+            'available' => true,
466
+            'last_checked' => 0
467
+        ];
468
+    }
469
+
470
+    /**
471
+     * @param bool $available
472
+     */
473
+    public function setAvailability($available) {
474
+        // shares do not participate in availability logic
475
+    }
476
+
477
+    public function getSourceStorage() {
478
+        $this->init();
479
+        return $this->nonMaskedStorage;
480
+    }
481
+
482
+    public function getWrapperStorage() {
483
+        $this->init();
484
+        return $this->storage;
485
+    }
486
+
487
+    public function file_get_contents($path) {
488
+        $info = [
489
+            'target' => $this->getMountPoint() . '/' . $path,
490
+            'source' => $this->getUnjailedPath($path),
491
+        ];
492
+        \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
493
+        return parent::file_get_contents($path);
494
+    }
495
+
496
+    public function file_put_contents($path, $data) {
497
+        $info = [
498
+            'target' => $this->getMountPoint() . '/' . $path,
499
+            'source' => $this->getUnjailedPath($path),
500
+        ];
501
+        \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
502
+        return parent::file_put_contents($path, $data);
503
+    }
504
+
505
+    public function setMountOptions(array $options) {
506
+        $this->mountOptions = $options;
507
+    }
508
+
509
+    public function getUnjailedPath($path) {
510
+        $this->init();
511
+        return parent::getUnjailedPath($path);
512
+    }
513 513
 }
Please login to merge, or discard this patch.