@@ -50,429 +50,429 @@ |
||
50 | 50 | use OCP\IUserManager; |
51 | 51 | |
52 | 52 | class Folder extends Node implements \OCP\Files\Folder { |
53 | - /** |
|
54 | - * Creates a Folder that represents a non-existing path |
|
55 | - * |
|
56 | - * @param string $path path |
|
57 | - * @return NonExistingFolder non-existing node |
|
58 | - */ |
|
59 | - protected function createNonExistingNode($path) { |
|
60 | - return new NonExistingFolder($this->root, $this->view, $path); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @param string $path path relative to the folder |
|
65 | - * @return string |
|
66 | - * @throws \OCP\Files\NotPermittedException |
|
67 | - */ |
|
68 | - public function getFullPath($path) { |
|
69 | - $path = $this->normalizePath($path); |
|
70 | - if (!$this->isValidPath($path)) { |
|
71 | - throw new NotPermittedException('Invalid path'); |
|
72 | - } |
|
73 | - return $this->path . $path; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * @param string $path |
|
78 | - * @return string|null |
|
79 | - */ |
|
80 | - public function getRelativePath($path) { |
|
81 | - return PathHelper::getRelativePath($this->getPath(), $path); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * check if a node is a (grand-)child of the folder |
|
86 | - * |
|
87 | - * @param \OC\Files\Node\Node $node |
|
88 | - * @return bool |
|
89 | - */ |
|
90 | - public function isSubNode($node) { |
|
91 | - return strpos($node->getPath(), $this->path . '/') === 0; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * get the content of this directory |
|
96 | - * |
|
97 | - * @return Node[] |
|
98 | - * @throws \OCP\Files\NotFoundException |
|
99 | - */ |
|
100 | - public function getDirectoryListing() { |
|
101 | - $folderContent = $this->view->getDirectoryContent($this->path, '', $this->getFileInfo(false)); |
|
102 | - |
|
103 | - return array_map(function (FileInfo $info) { |
|
104 | - if ($info->getMimetype() === FileInfo::MIMETYPE_FOLDER) { |
|
105 | - return new Folder($this->root, $this->view, $info->getPath(), $info, $this); |
|
106 | - } else { |
|
107 | - return new File($this->root, $this->view, $info->getPath(), $info, $this); |
|
108 | - } |
|
109 | - }, $folderContent); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @param string $path |
|
114 | - * @param FileInfo $info |
|
115 | - * @return File|Folder |
|
116 | - */ |
|
117 | - protected function createNode($path, FileInfo $info = null, bool $infoHasSubMountsIncluded = true) { |
|
118 | - if (is_null($info)) { |
|
119 | - $isDir = $this->view->is_dir($path); |
|
120 | - } else { |
|
121 | - $isDir = $info->getType() === FileInfo::TYPE_FOLDER; |
|
122 | - } |
|
123 | - $parent = dirname($path) === $this->getPath() ? $this : null; |
|
124 | - if ($isDir) { |
|
125 | - return new Folder($this->root, $this->view, $path, $info, $parent, $infoHasSubMountsIncluded); |
|
126 | - } else { |
|
127 | - return new File($this->root, $this->view, $path, $info, $parent); |
|
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 | - $parent = dirname($fullPath) === $this->getPath() ? $this : null; |
|
169 | - $node = new Folder($this->root, $this->view, $fullPath, null, $parent); |
|
170 | - $this->sendHooks(['postWrite', 'postCreate'], [$node]); |
|
171 | - return $node; |
|
172 | - } else { |
|
173 | - throw new NotPermittedException('No create permission for folder'); |
|
174 | - } |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * @param string $path |
|
179 | - * @param string | resource | null $content |
|
180 | - * @return \OC\Files\Node\File |
|
181 | - * @throws \OCP\Files\NotPermittedException |
|
182 | - */ |
|
183 | - public function newFile($path, $content = null) { |
|
184 | - if (empty($path)) { |
|
185 | - throw new NotPermittedException('Could not create as provided path is empty'); |
|
186 | - } |
|
187 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
188 | - $fullPath = $this->getFullPath($path); |
|
189 | - $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath); |
|
190 | - $this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]); |
|
191 | - if ($content !== null) { |
|
192 | - $result = $this->view->file_put_contents($fullPath, $content); |
|
193 | - } else { |
|
194 | - $result = $this->view->touch($fullPath); |
|
195 | - } |
|
196 | - if ($result === false) { |
|
197 | - throw new NotPermittedException('Could not create path'); |
|
198 | - } |
|
199 | - $node = new File($this->root, $this->view, $fullPath, null, $this); |
|
200 | - $this->sendHooks(['postWrite', 'postCreate'], [$node]); |
|
201 | - return $node; |
|
202 | - } |
|
203 | - throw new NotPermittedException('No create permission for path'); |
|
204 | - } |
|
205 | - |
|
206 | - private function queryFromOperator(ISearchOperator $operator, string $uid = null): ISearchQuery { |
|
207 | - if ($uid === null) { |
|
208 | - $user = null; |
|
209 | - } else { |
|
210 | - /** @var IUserManager $userManager */ |
|
211 | - $userManager = \OC::$server->query(IUserManager::class); |
|
212 | - $user = $userManager->get($uid); |
|
213 | - } |
|
214 | - return new SearchQuery($operator, 0, 0, [], $user); |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * search for files with the name matching $query |
|
219 | - * |
|
220 | - * @param string|ISearchQuery $query |
|
221 | - * @return \OC\Files\Node\Node[] |
|
222 | - */ |
|
223 | - public function search($query) { |
|
224 | - if (is_string($query)) { |
|
225 | - $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%')); |
|
226 | - } |
|
227 | - |
|
228 | - // search is handled by a single query covering all caches that this folder contains |
|
229 | - // this is done by collect |
|
230 | - |
|
231 | - $limitToHome = $query->limitToHome(); |
|
232 | - if ($limitToHome && count(explode('/', $this->path)) !== 3) { |
|
233 | - throw new \InvalidArgumentException('searching by owner is only allows on the users home folder'); |
|
234 | - } |
|
235 | - |
|
236 | - $rootLength = strlen($this->path); |
|
237 | - $mount = $this->root->getMount($this->path); |
|
238 | - $storage = $mount->getStorage(); |
|
239 | - $internalPath = $mount->getInternalPath($this->path); |
|
240 | - |
|
241 | - // collect all caches for this folder, indexed by their mountpoint relative to this folder |
|
242 | - // and save the mount which is needed later to construct the FileInfo objects |
|
243 | - |
|
244 | - if ($internalPath !== '') { |
|
245 | - // a temporary CacheJail is used to handle filtering down the results to within this folder |
|
246 | - $caches = ['' => new CacheJail($storage->getCache(''), $internalPath)]; |
|
247 | - } else { |
|
248 | - $caches = ['' => $storage->getCache('')]; |
|
249 | - } |
|
250 | - $mountByMountPoint = ['' => $mount]; |
|
251 | - |
|
252 | - if (!$limitToHome) { |
|
253 | - $mounts = $this->root->getMountsIn($this->path); |
|
254 | - foreach ($mounts as $mount) { |
|
255 | - $storage = $mount->getStorage(); |
|
256 | - if ($storage) { |
|
257 | - $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/'); |
|
258 | - $caches[$relativeMountPoint] = $storage->getCache(''); |
|
259 | - $mountByMountPoint[$relativeMountPoint] = $mount; |
|
260 | - } |
|
261 | - } |
|
262 | - } |
|
263 | - |
|
264 | - /** @var QuerySearchHelper $searchHelper */ |
|
265 | - $searchHelper = \OC::$server->get(QuerySearchHelper::class); |
|
266 | - $resultsPerCache = $searchHelper->searchInCaches($query, $caches); |
|
267 | - |
|
268 | - // loop through all results per-cache, constructing the FileInfo object from the CacheEntry and merge them all |
|
269 | - $files = array_merge(...array_map(function (array $results, $relativeMountPoint) use ($mountByMountPoint) { |
|
270 | - $mount = $mountByMountPoint[$relativeMountPoint]; |
|
271 | - return array_map(function (ICacheEntry $result) use ($relativeMountPoint, $mount) { |
|
272 | - return $this->cacheEntryToFileInfo($mount, $relativeMountPoint, $result); |
|
273 | - }, $results); |
|
274 | - }, array_values($resultsPerCache), array_keys($resultsPerCache))); |
|
275 | - |
|
276 | - // don't include this folder in the results |
|
277 | - $files = array_filter($files, function (FileInfo $file) { |
|
278 | - return $file->getPath() !== $this->getPath(); |
|
279 | - }); |
|
280 | - |
|
281 | - // since results were returned per-cache, they are no longer fully sorted |
|
282 | - $order = $query->getOrder(); |
|
283 | - if ($order) { |
|
284 | - usort($files, function (FileInfo $a, FileInfo $b) use ($order) { |
|
285 | - foreach ($order as $orderField) { |
|
286 | - $cmp = $orderField->sortFileInfo($a, $b); |
|
287 | - if ($cmp !== 0) { |
|
288 | - return $cmp; |
|
289 | - } |
|
290 | - } |
|
291 | - return 0; |
|
292 | - }); |
|
293 | - } |
|
294 | - |
|
295 | - return array_map(function (FileInfo $file) { |
|
296 | - return $this->createNode($file->getPath(), $file); |
|
297 | - }, $files); |
|
298 | - } |
|
299 | - |
|
300 | - private function cacheEntryToFileInfo(IMountPoint $mount, string $appendRoot, ICacheEntry $cacheEntry): FileInfo { |
|
301 | - $cacheEntry['internalPath'] = $cacheEntry['path']; |
|
302 | - $cacheEntry['path'] = rtrim($appendRoot . $cacheEntry->getPath(), '/'); |
|
303 | - $subPath = $cacheEntry['path'] !== '' ? '/' . $cacheEntry['path'] : ''; |
|
304 | - return new \OC\Files\FileInfo($this->path . $subPath, $mount->getStorage(), $cacheEntry['internalPath'], $cacheEntry, $mount); |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * search for files by mimetype |
|
309 | - * |
|
310 | - * @param string $mimetype |
|
311 | - * @return Node[] |
|
312 | - */ |
|
313 | - public function searchByMime($mimetype) { |
|
314 | - if (strpos($mimetype, '/') === false) { |
|
315 | - $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype . '/%')); |
|
316 | - } else { |
|
317 | - $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $mimetype)); |
|
318 | - } |
|
319 | - return $this->search($query); |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * search for files by tag |
|
324 | - * |
|
325 | - * @param string|int $tag name or tag id |
|
326 | - * @param string $userId owner of the tags |
|
327 | - * @return Node[] |
|
328 | - */ |
|
329 | - public function searchByTag($tag, $userId) { |
|
330 | - $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'tagname', $tag), $userId); |
|
331 | - return $this->search($query); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * @param int $id |
|
336 | - * @return \OC\Files\Node\Node[] |
|
337 | - */ |
|
338 | - public function getById($id) { |
|
339 | - return $this->root->getByIdInPath((int)$id, $this->getPath()); |
|
340 | - } |
|
341 | - |
|
342 | - protected function getAppDataDirectoryName(): string { |
|
343 | - $instanceId = \OC::$server->getConfig()->getSystemValueString('instanceid'); |
|
344 | - return 'appdata_' . $instanceId; |
|
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * In case the path we are currently in is inside the appdata_* folder, |
|
349 | - * the original getById method does not work, because it can only look inside |
|
350 | - * the user's mount points. But the user has no mount point for the root storage. |
|
351 | - * |
|
352 | - * So in that case we directly check the mount of the root if it contains |
|
353 | - * the id. If it does we check if the path is inside the path we are working |
|
354 | - * in. |
|
355 | - * |
|
356 | - * @param int $id |
|
357 | - * @return array |
|
358 | - */ |
|
359 | - protected function getByIdInRootMount(int $id): array { |
|
360 | - $mount = $this->root->getMount(''); |
|
361 | - $cacheEntry = $mount->getStorage()->getCache($this->path)->get($id); |
|
362 | - if (!$cacheEntry) { |
|
363 | - return []; |
|
364 | - } |
|
365 | - |
|
366 | - $absolutePath = '/' . ltrim($cacheEntry->getPath(), '/'); |
|
367 | - $currentPath = rtrim($this->path, '/') . '/'; |
|
368 | - |
|
369 | - if (strpos($absolutePath, $currentPath) !== 0) { |
|
370 | - return []; |
|
371 | - } |
|
372 | - |
|
373 | - return [$this->root->createNode( |
|
374 | - $absolutePath, new \OC\Files\FileInfo( |
|
375 | - $absolutePath, |
|
376 | - $mount->getStorage(), |
|
377 | - $cacheEntry->getPath(), |
|
378 | - $cacheEntry, |
|
379 | - $mount |
|
380 | - ))]; |
|
381 | - } |
|
382 | - |
|
383 | - public function getFreeSpace() { |
|
384 | - return $this->view->free_space($this->path); |
|
385 | - } |
|
386 | - |
|
387 | - public function delete() { |
|
388 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
389 | - $this->sendHooks(['preDelete']); |
|
390 | - $fileInfo = $this->getFileInfo(); |
|
391 | - $this->view->rmdir($this->path); |
|
392 | - $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo); |
|
393 | - $this->sendHooks(['postDelete'], [$nonExisting]); |
|
394 | - } else { |
|
395 | - throw new NotPermittedException('No delete permission for path'); |
|
396 | - } |
|
397 | - } |
|
398 | - |
|
399 | - /** |
|
400 | - * Add a suffix to the name in case the file exists |
|
401 | - * |
|
402 | - * @param string $name |
|
403 | - * @return string |
|
404 | - * @throws NotPermittedException |
|
405 | - */ |
|
406 | - public function getNonExistingName($name) { |
|
407 | - $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view); |
|
408 | - return trim($this->getRelativePath($uniqueName), '/'); |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * @param int $limit |
|
413 | - * @param int $offset |
|
414 | - * @return \OCP\Files\Node[] |
|
415 | - */ |
|
416 | - public function getRecent($limit, $offset = 0) { |
|
417 | - $filterOutNonEmptyFolder = new SearchBinaryOperator( |
|
418 | - // filter out non empty folders |
|
419 | - ISearchBinaryOperator::OPERATOR_OR, |
|
420 | - [ |
|
421 | - new SearchBinaryOperator( |
|
422 | - ISearchBinaryOperator::OPERATOR_NOT, |
|
423 | - [ |
|
424 | - new SearchComparison( |
|
425 | - ISearchComparison::COMPARE_EQUAL, |
|
426 | - 'mimetype', |
|
427 | - FileInfo::MIMETYPE_FOLDER |
|
428 | - ), |
|
429 | - ] |
|
430 | - ), |
|
431 | - new SearchComparison( |
|
432 | - ISearchComparison::COMPARE_EQUAL, |
|
433 | - 'size', |
|
434 | - 0 |
|
435 | - ), |
|
436 | - ] |
|
437 | - ); |
|
438 | - |
|
439 | - $filterNonRecentFiles = new SearchComparison( |
|
440 | - ISearchComparison::COMPARE_GREATER_THAN, |
|
441 | - 'mtime', |
|
442 | - strtotime("-2 week") |
|
443 | - ); |
|
444 | - if ($offset === 0 && $limit <= 100) { |
|
445 | - $query = new SearchQuery( |
|
446 | - new SearchBinaryOperator( |
|
447 | - ISearchBinaryOperator::OPERATOR_AND, |
|
448 | - [ |
|
449 | - $filterOutNonEmptyFolder, |
|
450 | - $filterNonRecentFiles, |
|
451 | - ], |
|
452 | - ), |
|
453 | - $limit, |
|
454 | - $offset, |
|
455 | - [ |
|
456 | - new SearchOrder( |
|
457 | - ISearchOrder::DIRECTION_DESCENDING, |
|
458 | - 'mtime' |
|
459 | - ), |
|
460 | - ] |
|
461 | - ); |
|
462 | - } else { |
|
463 | - $query = new SearchQuery( |
|
464 | - $filterOutNonEmptyFolder, |
|
465 | - $limit, |
|
466 | - $offset, |
|
467 | - [ |
|
468 | - new SearchOrder( |
|
469 | - ISearchOrder::DIRECTION_DESCENDING, |
|
470 | - 'mtime' |
|
471 | - ), |
|
472 | - ] |
|
473 | - ); |
|
474 | - } |
|
475 | - |
|
476 | - return $this->search($query); |
|
477 | - } |
|
53 | + /** |
|
54 | + * Creates a Folder that represents a non-existing path |
|
55 | + * |
|
56 | + * @param string $path path |
|
57 | + * @return NonExistingFolder non-existing node |
|
58 | + */ |
|
59 | + protected function createNonExistingNode($path) { |
|
60 | + return new NonExistingFolder($this->root, $this->view, $path); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @param string $path path relative to the folder |
|
65 | + * @return string |
|
66 | + * @throws \OCP\Files\NotPermittedException |
|
67 | + */ |
|
68 | + public function getFullPath($path) { |
|
69 | + $path = $this->normalizePath($path); |
|
70 | + if (!$this->isValidPath($path)) { |
|
71 | + throw new NotPermittedException('Invalid path'); |
|
72 | + } |
|
73 | + return $this->path . $path; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * @param string $path |
|
78 | + * @return string|null |
|
79 | + */ |
|
80 | + public function getRelativePath($path) { |
|
81 | + return PathHelper::getRelativePath($this->getPath(), $path); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * check if a node is a (grand-)child of the folder |
|
86 | + * |
|
87 | + * @param \OC\Files\Node\Node $node |
|
88 | + * @return bool |
|
89 | + */ |
|
90 | + public function isSubNode($node) { |
|
91 | + return strpos($node->getPath(), $this->path . '/') === 0; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * get the content of this directory |
|
96 | + * |
|
97 | + * @return Node[] |
|
98 | + * @throws \OCP\Files\NotFoundException |
|
99 | + */ |
|
100 | + public function getDirectoryListing() { |
|
101 | + $folderContent = $this->view->getDirectoryContent($this->path, '', $this->getFileInfo(false)); |
|
102 | + |
|
103 | + return array_map(function (FileInfo $info) { |
|
104 | + if ($info->getMimetype() === FileInfo::MIMETYPE_FOLDER) { |
|
105 | + return new Folder($this->root, $this->view, $info->getPath(), $info, $this); |
|
106 | + } else { |
|
107 | + return new File($this->root, $this->view, $info->getPath(), $info, $this); |
|
108 | + } |
|
109 | + }, $folderContent); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @param string $path |
|
114 | + * @param FileInfo $info |
|
115 | + * @return File|Folder |
|
116 | + */ |
|
117 | + protected function createNode($path, FileInfo $info = null, bool $infoHasSubMountsIncluded = true) { |
|
118 | + if (is_null($info)) { |
|
119 | + $isDir = $this->view->is_dir($path); |
|
120 | + } else { |
|
121 | + $isDir = $info->getType() === FileInfo::TYPE_FOLDER; |
|
122 | + } |
|
123 | + $parent = dirname($path) === $this->getPath() ? $this : null; |
|
124 | + if ($isDir) { |
|
125 | + return new Folder($this->root, $this->view, $path, $info, $parent, $infoHasSubMountsIncluded); |
|
126 | + } else { |
|
127 | + return new File($this->root, $this->view, $path, $info, $parent); |
|
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 | + $parent = dirname($fullPath) === $this->getPath() ? $this : null; |
|
169 | + $node = new Folder($this->root, $this->view, $fullPath, null, $parent); |
|
170 | + $this->sendHooks(['postWrite', 'postCreate'], [$node]); |
|
171 | + return $node; |
|
172 | + } else { |
|
173 | + throw new NotPermittedException('No create permission for folder'); |
|
174 | + } |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * @param string $path |
|
179 | + * @param string | resource | null $content |
|
180 | + * @return \OC\Files\Node\File |
|
181 | + * @throws \OCP\Files\NotPermittedException |
|
182 | + */ |
|
183 | + public function newFile($path, $content = null) { |
|
184 | + if (empty($path)) { |
|
185 | + throw new NotPermittedException('Could not create as provided path is empty'); |
|
186 | + } |
|
187 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
188 | + $fullPath = $this->getFullPath($path); |
|
189 | + $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath); |
|
190 | + $this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]); |
|
191 | + if ($content !== null) { |
|
192 | + $result = $this->view->file_put_contents($fullPath, $content); |
|
193 | + } else { |
|
194 | + $result = $this->view->touch($fullPath); |
|
195 | + } |
|
196 | + if ($result === false) { |
|
197 | + throw new NotPermittedException('Could not create path'); |
|
198 | + } |
|
199 | + $node = new File($this->root, $this->view, $fullPath, null, $this); |
|
200 | + $this->sendHooks(['postWrite', 'postCreate'], [$node]); |
|
201 | + return $node; |
|
202 | + } |
|
203 | + throw new NotPermittedException('No create permission for path'); |
|
204 | + } |
|
205 | + |
|
206 | + private function queryFromOperator(ISearchOperator $operator, string $uid = null): ISearchQuery { |
|
207 | + if ($uid === null) { |
|
208 | + $user = null; |
|
209 | + } else { |
|
210 | + /** @var IUserManager $userManager */ |
|
211 | + $userManager = \OC::$server->query(IUserManager::class); |
|
212 | + $user = $userManager->get($uid); |
|
213 | + } |
|
214 | + return new SearchQuery($operator, 0, 0, [], $user); |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * search for files with the name matching $query |
|
219 | + * |
|
220 | + * @param string|ISearchQuery $query |
|
221 | + * @return \OC\Files\Node\Node[] |
|
222 | + */ |
|
223 | + public function search($query) { |
|
224 | + if (is_string($query)) { |
|
225 | + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%')); |
|
226 | + } |
|
227 | + |
|
228 | + // search is handled by a single query covering all caches that this folder contains |
|
229 | + // this is done by collect |
|
230 | + |
|
231 | + $limitToHome = $query->limitToHome(); |
|
232 | + if ($limitToHome && count(explode('/', $this->path)) !== 3) { |
|
233 | + throw new \InvalidArgumentException('searching by owner is only allows on the users home folder'); |
|
234 | + } |
|
235 | + |
|
236 | + $rootLength = strlen($this->path); |
|
237 | + $mount = $this->root->getMount($this->path); |
|
238 | + $storage = $mount->getStorage(); |
|
239 | + $internalPath = $mount->getInternalPath($this->path); |
|
240 | + |
|
241 | + // collect all caches for this folder, indexed by their mountpoint relative to this folder |
|
242 | + // and save the mount which is needed later to construct the FileInfo objects |
|
243 | + |
|
244 | + if ($internalPath !== '') { |
|
245 | + // a temporary CacheJail is used to handle filtering down the results to within this folder |
|
246 | + $caches = ['' => new CacheJail($storage->getCache(''), $internalPath)]; |
|
247 | + } else { |
|
248 | + $caches = ['' => $storage->getCache('')]; |
|
249 | + } |
|
250 | + $mountByMountPoint = ['' => $mount]; |
|
251 | + |
|
252 | + if (!$limitToHome) { |
|
253 | + $mounts = $this->root->getMountsIn($this->path); |
|
254 | + foreach ($mounts as $mount) { |
|
255 | + $storage = $mount->getStorage(); |
|
256 | + if ($storage) { |
|
257 | + $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/'); |
|
258 | + $caches[$relativeMountPoint] = $storage->getCache(''); |
|
259 | + $mountByMountPoint[$relativeMountPoint] = $mount; |
|
260 | + } |
|
261 | + } |
|
262 | + } |
|
263 | + |
|
264 | + /** @var QuerySearchHelper $searchHelper */ |
|
265 | + $searchHelper = \OC::$server->get(QuerySearchHelper::class); |
|
266 | + $resultsPerCache = $searchHelper->searchInCaches($query, $caches); |
|
267 | + |
|
268 | + // loop through all results per-cache, constructing the FileInfo object from the CacheEntry and merge them all |
|
269 | + $files = array_merge(...array_map(function (array $results, $relativeMountPoint) use ($mountByMountPoint) { |
|
270 | + $mount = $mountByMountPoint[$relativeMountPoint]; |
|
271 | + return array_map(function (ICacheEntry $result) use ($relativeMountPoint, $mount) { |
|
272 | + return $this->cacheEntryToFileInfo($mount, $relativeMountPoint, $result); |
|
273 | + }, $results); |
|
274 | + }, array_values($resultsPerCache), array_keys($resultsPerCache))); |
|
275 | + |
|
276 | + // don't include this folder in the results |
|
277 | + $files = array_filter($files, function (FileInfo $file) { |
|
278 | + return $file->getPath() !== $this->getPath(); |
|
279 | + }); |
|
280 | + |
|
281 | + // since results were returned per-cache, they are no longer fully sorted |
|
282 | + $order = $query->getOrder(); |
|
283 | + if ($order) { |
|
284 | + usort($files, function (FileInfo $a, FileInfo $b) use ($order) { |
|
285 | + foreach ($order as $orderField) { |
|
286 | + $cmp = $orderField->sortFileInfo($a, $b); |
|
287 | + if ($cmp !== 0) { |
|
288 | + return $cmp; |
|
289 | + } |
|
290 | + } |
|
291 | + return 0; |
|
292 | + }); |
|
293 | + } |
|
294 | + |
|
295 | + return array_map(function (FileInfo $file) { |
|
296 | + return $this->createNode($file->getPath(), $file); |
|
297 | + }, $files); |
|
298 | + } |
|
299 | + |
|
300 | + private function cacheEntryToFileInfo(IMountPoint $mount, string $appendRoot, ICacheEntry $cacheEntry): FileInfo { |
|
301 | + $cacheEntry['internalPath'] = $cacheEntry['path']; |
|
302 | + $cacheEntry['path'] = rtrim($appendRoot . $cacheEntry->getPath(), '/'); |
|
303 | + $subPath = $cacheEntry['path'] !== '' ? '/' . $cacheEntry['path'] : ''; |
|
304 | + return new \OC\Files\FileInfo($this->path . $subPath, $mount->getStorage(), $cacheEntry['internalPath'], $cacheEntry, $mount); |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * search for files by mimetype |
|
309 | + * |
|
310 | + * @param string $mimetype |
|
311 | + * @return Node[] |
|
312 | + */ |
|
313 | + public function searchByMime($mimetype) { |
|
314 | + if (strpos($mimetype, '/') === false) { |
|
315 | + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype . '/%')); |
|
316 | + } else { |
|
317 | + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $mimetype)); |
|
318 | + } |
|
319 | + return $this->search($query); |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * search for files by tag |
|
324 | + * |
|
325 | + * @param string|int $tag name or tag id |
|
326 | + * @param string $userId owner of the tags |
|
327 | + * @return Node[] |
|
328 | + */ |
|
329 | + public function searchByTag($tag, $userId) { |
|
330 | + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'tagname', $tag), $userId); |
|
331 | + return $this->search($query); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * @param int $id |
|
336 | + * @return \OC\Files\Node\Node[] |
|
337 | + */ |
|
338 | + public function getById($id) { |
|
339 | + return $this->root->getByIdInPath((int)$id, $this->getPath()); |
|
340 | + } |
|
341 | + |
|
342 | + protected function getAppDataDirectoryName(): string { |
|
343 | + $instanceId = \OC::$server->getConfig()->getSystemValueString('instanceid'); |
|
344 | + return 'appdata_' . $instanceId; |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * In case the path we are currently in is inside the appdata_* folder, |
|
349 | + * the original getById method does not work, because it can only look inside |
|
350 | + * the user's mount points. But the user has no mount point for the root storage. |
|
351 | + * |
|
352 | + * So in that case we directly check the mount of the root if it contains |
|
353 | + * the id. If it does we check if the path is inside the path we are working |
|
354 | + * in. |
|
355 | + * |
|
356 | + * @param int $id |
|
357 | + * @return array |
|
358 | + */ |
|
359 | + protected function getByIdInRootMount(int $id): array { |
|
360 | + $mount = $this->root->getMount(''); |
|
361 | + $cacheEntry = $mount->getStorage()->getCache($this->path)->get($id); |
|
362 | + if (!$cacheEntry) { |
|
363 | + return []; |
|
364 | + } |
|
365 | + |
|
366 | + $absolutePath = '/' . ltrim($cacheEntry->getPath(), '/'); |
|
367 | + $currentPath = rtrim($this->path, '/') . '/'; |
|
368 | + |
|
369 | + if (strpos($absolutePath, $currentPath) !== 0) { |
|
370 | + return []; |
|
371 | + } |
|
372 | + |
|
373 | + return [$this->root->createNode( |
|
374 | + $absolutePath, new \OC\Files\FileInfo( |
|
375 | + $absolutePath, |
|
376 | + $mount->getStorage(), |
|
377 | + $cacheEntry->getPath(), |
|
378 | + $cacheEntry, |
|
379 | + $mount |
|
380 | + ))]; |
|
381 | + } |
|
382 | + |
|
383 | + public function getFreeSpace() { |
|
384 | + return $this->view->free_space($this->path); |
|
385 | + } |
|
386 | + |
|
387 | + public function delete() { |
|
388 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
389 | + $this->sendHooks(['preDelete']); |
|
390 | + $fileInfo = $this->getFileInfo(); |
|
391 | + $this->view->rmdir($this->path); |
|
392 | + $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo); |
|
393 | + $this->sendHooks(['postDelete'], [$nonExisting]); |
|
394 | + } else { |
|
395 | + throw new NotPermittedException('No delete permission for path'); |
|
396 | + } |
|
397 | + } |
|
398 | + |
|
399 | + /** |
|
400 | + * Add a suffix to the name in case the file exists |
|
401 | + * |
|
402 | + * @param string $name |
|
403 | + * @return string |
|
404 | + * @throws NotPermittedException |
|
405 | + */ |
|
406 | + public function getNonExistingName($name) { |
|
407 | + $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view); |
|
408 | + return trim($this->getRelativePath($uniqueName), '/'); |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * @param int $limit |
|
413 | + * @param int $offset |
|
414 | + * @return \OCP\Files\Node[] |
|
415 | + */ |
|
416 | + public function getRecent($limit, $offset = 0) { |
|
417 | + $filterOutNonEmptyFolder = new SearchBinaryOperator( |
|
418 | + // filter out non empty folders |
|
419 | + ISearchBinaryOperator::OPERATOR_OR, |
|
420 | + [ |
|
421 | + new SearchBinaryOperator( |
|
422 | + ISearchBinaryOperator::OPERATOR_NOT, |
|
423 | + [ |
|
424 | + new SearchComparison( |
|
425 | + ISearchComparison::COMPARE_EQUAL, |
|
426 | + 'mimetype', |
|
427 | + FileInfo::MIMETYPE_FOLDER |
|
428 | + ), |
|
429 | + ] |
|
430 | + ), |
|
431 | + new SearchComparison( |
|
432 | + ISearchComparison::COMPARE_EQUAL, |
|
433 | + 'size', |
|
434 | + 0 |
|
435 | + ), |
|
436 | + ] |
|
437 | + ); |
|
438 | + |
|
439 | + $filterNonRecentFiles = new SearchComparison( |
|
440 | + ISearchComparison::COMPARE_GREATER_THAN, |
|
441 | + 'mtime', |
|
442 | + strtotime("-2 week") |
|
443 | + ); |
|
444 | + if ($offset === 0 && $limit <= 100) { |
|
445 | + $query = new SearchQuery( |
|
446 | + new SearchBinaryOperator( |
|
447 | + ISearchBinaryOperator::OPERATOR_AND, |
|
448 | + [ |
|
449 | + $filterOutNonEmptyFolder, |
|
450 | + $filterNonRecentFiles, |
|
451 | + ], |
|
452 | + ), |
|
453 | + $limit, |
|
454 | + $offset, |
|
455 | + [ |
|
456 | + new SearchOrder( |
|
457 | + ISearchOrder::DIRECTION_DESCENDING, |
|
458 | + 'mtime' |
|
459 | + ), |
|
460 | + ] |
|
461 | + ); |
|
462 | + } else { |
|
463 | + $query = new SearchQuery( |
|
464 | + $filterOutNonEmptyFolder, |
|
465 | + $limit, |
|
466 | + $offset, |
|
467 | + [ |
|
468 | + new SearchOrder( |
|
469 | + ISearchOrder::DIRECTION_DESCENDING, |
|
470 | + 'mtime' |
|
471 | + ), |
|
472 | + ] |
|
473 | + ); |
|
474 | + } |
|
475 | + |
|
476 | + return $this->search($query); |
|
477 | + } |
|
478 | 478 | } |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | if (!$this->isValidPath($path)) { |
71 | 71 | throw new NotPermittedException('Invalid path'); |
72 | 72 | } |
73 | - return $this->path . $path; |
|
73 | + return $this->path.$path; |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | /** |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | * @return bool |
89 | 89 | */ |
90 | 90 | public function isSubNode($node) { |
91 | - return strpos($node->getPath(), $this->path . '/') === 0; |
|
91 | + return strpos($node->getPath(), $this->path.'/') === 0; |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | /** |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | public function getDirectoryListing() { |
101 | 101 | $folderContent = $this->view->getDirectoryContent($this->path, '', $this->getFileInfo(false)); |
102 | 102 | |
103 | - return array_map(function (FileInfo $info) { |
|
103 | + return array_map(function(FileInfo $info) { |
|
104 | 104 | if ($info->getMimetype() === FileInfo::MIMETYPE_FOLDER) { |
105 | 105 | return new Folder($this->root, $this->view, $info->getPath(), $info, $this); |
106 | 106 | } else { |
@@ -222,7 +222,7 @@ discard block |
||
222 | 222 | */ |
223 | 223 | public function search($query) { |
224 | 224 | if (is_string($query)) { |
225 | - $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%')); |
|
225 | + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%'.$query.'%')); |
|
226 | 226 | } |
227 | 227 | |
228 | 228 | // search is handled by a single query covering all caches that this folder contains |
@@ -266,22 +266,22 @@ discard block |
||
266 | 266 | $resultsPerCache = $searchHelper->searchInCaches($query, $caches); |
267 | 267 | |
268 | 268 | // loop through all results per-cache, constructing the FileInfo object from the CacheEntry and merge them all |
269 | - $files = array_merge(...array_map(function (array $results, $relativeMountPoint) use ($mountByMountPoint) { |
|
269 | + $files = array_merge(...array_map(function(array $results, $relativeMountPoint) use ($mountByMountPoint) { |
|
270 | 270 | $mount = $mountByMountPoint[$relativeMountPoint]; |
271 | - return array_map(function (ICacheEntry $result) use ($relativeMountPoint, $mount) { |
|
271 | + return array_map(function(ICacheEntry $result) use ($relativeMountPoint, $mount) { |
|
272 | 272 | return $this->cacheEntryToFileInfo($mount, $relativeMountPoint, $result); |
273 | 273 | }, $results); |
274 | 274 | }, array_values($resultsPerCache), array_keys($resultsPerCache))); |
275 | 275 | |
276 | 276 | // don't include this folder in the results |
277 | - $files = array_filter($files, function (FileInfo $file) { |
|
277 | + $files = array_filter($files, function(FileInfo $file) { |
|
278 | 278 | return $file->getPath() !== $this->getPath(); |
279 | 279 | }); |
280 | 280 | |
281 | 281 | // since results were returned per-cache, they are no longer fully sorted |
282 | 282 | $order = $query->getOrder(); |
283 | 283 | if ($order) { |
284 | - usort($files, function (FileInfo $a, FileInfo $b) use ($order) { |
|
284 | + usort($files, function(FileInfo $a, FileInfo $b) use ($order) { |
|
285 | 285 | foreach ($order as $orderField) { |
286 | 286 | $cmp = $orderField->sortFileInfo($a, $b); |
287 | 287 | if ($cmp !== 0) { |
@@ -292,16 +292,16 @@ discard block |
||
292 | 292 | }); |
293 | 293 | } |
294 | 294 | |
295 | - return array_map(function (FileInfo $file) { |
|
295 | + return array_map(function(FileInfo $file) { |
|
296 | 296 | return $this->createNode($file->getPath(), $file); |
297 | 297 | }, $files); |
298 | 298 | } |
299 | 299 | |
300 | 300 | private function cacheEntryToFileInfo(IMountPoint $mount, string $appendRoot, ICacheEntry $cacheEntry): FileInfo { |
301 | 301 | $cacheEntry['internalPath'] = $cacheEntry['path']; |
302 | - $cacheEntry['path'] = rtrim($appendRoot . $cacheEntry->getPath(), '/'); |
|
303 | - $subPath = $cacheEntry['path'] !== '' ? '/' . $cacheEntry['path'] : ''; |
|
304 | - return new \OC\Files\FileInfo($this->path . $subPath, $mount->getStorage(), $cacheEntry['internalPath'], $cacheEntry, $mount); |
|
302 | + $cacheEntry['path'] = rtrim($appendRoot.$cacheEntry->getPath(), '/'); |
|
303 | + $subPath = $cacheEntry['path'] !== '' ? '/'.$cacheEntry['path'] : ''; |
|
304 | + return new \OC\Files\FileInfo($this->path.$subPath, $mount->getStorage(), $cacheEntry['internalPath'], $cacheEntry, $mount); |
|
305 | 305 | } |
306 | 306 | |
307 | 307 | /** |
@@ -312,7 +312,7 @@ discard block |
||
312 | 312 | */ |
313 | 313 | public function searchByMime($mimetype) { |
314 | 314 | if (strpos($mimetype, '/') === false) { |
315 | - $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype . '/%')); |
|
315 | + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype.'/%')); |
|
316 | 316 | } else { |
317 | 317 | $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $mimetype)); |
318 | 318 | } |
@@ -336,12 +336,12 @@ discard block |
||
336 | 336 | * @return \OC\Files\Node\Node[] |
337 | 337 | */ |
338 | 338 | public function getById($id) { |
339 | - return $this->root->getByIdInPath((int)$id, $this->getPath()); |
|
339 | + return $this->root->getByIdInPath((int) $id, $this->getPath()); |
|
340 | 340 | } |
341 | 341 | |
342 | 342 | protected function getAppDataDirectoryName(): string { |
343 | 343 | $instanceId = \OC::$server->getConfig()->getSystemValueString('instanceid'); |
344 | - return 'appdata_' . $instanceId; |
|
344 | + return 'appdata_'.$instanceId; |
|
345 | 345 | } |
346 | 346 | |
347 | 347 | /** |
@@ -363,8 +363,8 @@ discard block |
||
363 | 363 | return []; |
364 | 364 | } |
365 | 365 | |
366 | - $absolutePath = '/' . ltrim($cacheEntry->getPath(), '/'); |
|
367 | - $currentPath = rtrim($this->path, '/') . '/'; |
|
366 | + $absolutePath = '/'.ltrim($cacheEntry->getPath(), '/'); |
|
367 | + $currentPath = rtrim($this->path, '/').'/'; |
|
368 | 368 | |
369 | 369 | if (strpos($absolutePath, $currentPath) !== 0) { |
370 | 370 | return []; |
@@ -71,424 +71,424 @@ |
||
71 | 71 | * @package OC\Files\Node |
72 | 72 | */ |
73 | 73 | class Root extends Folder implements IRootFolder { |
74 | - private Manager $mountManager; |
|
75 | - private PublicEmitter $emitter; |
|
76 | - private ?IUser $user; |
|
77 | - private CappedMemoryCache $userFolderCache; |
|
78 | - private IUserMountCache $userMountCache; |
|
79 | - private LoggerInterface $logger; |
|
80 | - private IUserManager $userManager; |
|
81 | - private IEventDispatcher $eventDispatcher; |
|
82 | - |
|
83 | - /** |
|
84 | - * @param Manager $manager |
|
85 | - * @param View $view |
|
86 | - * @param IUser|null $user |
|
87 | - */ |
|
88 | - public function __construct( |
|
89 | - $manager, |
|
90 | - $view, |
|
91 | - $user, |
|
92 | - IUserMountCache $userMountCache, |
|
93 | - LoggerInterface $logger, |
|
94 | - IUserManager $userManager, |
|
95 | - IEventDispatcher $eventDispatcher |
|
96 | - ) { |
|
97 | - parent::__construct($this, $view, ''); |
|
98 | - $this->mountManager = $manager; |
|
99 | - $this->user = $user; |
|
100 | - $this->emitter = new PublicEmitter(); |
|
101 | - $this->userFolderCache = new CappedMemoryCache(); |
|
102 | - $this->userMountCache = $userMountCache; |
|
103 | - $this->logger = $logger; |
|
104 | - $this->userManager = $userManager; |
|
105 | - $eventDispatcher->addListener(FilesystemTornDownEvent::class, function () { |
|
106 | - $this->userFolderCache = new CappedMemoryCache(); |
|
107 | - }); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Get the user for which the filesystem is setup |
|
112 | - * |
|
113 | - * @return \OC\User\User |
|
114 | - */ |
|
115 | - public function getUser() { |
|
116 | - return $this->user; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * @param string $scope |
|
121 | - * @param string $method |
|
122 | - * @param callable $callback |
|
123 | - */ |
|
124 | - public function listen($scope, $method, callable $callback) { |
|
125 | - $this->emitter->listen($scope, $method, $callback); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * @param string $scope optional |
|
130 | - * @param string $method optional |
|
131 | - * @param callable $callback optional |
|
132 | - */ |
|
133 | - public function removeListener($scope = null, $method = null, callable $callback = null) { |
|
134 | - $this->emitter->removeListener($scope, $method, $callback); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param string $scope |
|
139 | - * @param string $method |
|
140 | - * @param Node[] $arguments |
|
141 | - */ |
|
142 | - public function emit($scope, $method, $arguments = []) { |
|
143 | - $this->emitter->emit($scope, $method, $arguments); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * @param \OC\Files\Storage\Storage $storage |
|
148 | - * @param string $mountPoint |
|
149 | - * @param array $arguments |
|
150 | - */ |
|
151 | - public function mount($storage, $mountPoint, $arguments = []) { |
|
152 | - $mount = new MountPoint($storage, $mountPoint, $arguments); |
|
153 | - $this->mountManager->addMount($mount); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * @param string $mountPoint |
|
158 | - * @return \OC\Files\Mount\MountPoint |
|
159 | - */ |
|
160 | - public function getMount($mountPoint) { |
|
161 | - return $this->mountManager->find($mountPoint); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @param string $mountPoint |
|
166 | - * @return \OC\Files\Mount\MountPoint[] |
|
167 | - */ |
|
168 | - public function getMountsIn($mountPoint) { |
|
169 | - return $this->mountManager->findIn($mountPoint); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @param string $storageId |
|
174 | - * @return \OC\Files\Mount\MountPoint[] |
|
175 | - */ |
|
176 | - public function getMountByStorageId($storageId) { |
|
177 | - return $this->mountManager->findByStorageId($storageId); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @param int $numericId |
|
182 | - * @return MountPoint[] |
|
183 | - */ |
|
184 | - public function getMountByNumericStorageId($numericId) { |
|
185 | - return $this->mountManager->findByNumericId($numericId); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @param \OC\Files\Mount\MountPoint $mount |
|
190 | - */ |
|
191 | - public function unMount($mount) { |
|
192 | - $this->mountManager->remove($mount); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * @param string $path |
|
197 | - * @return Node |
|
198 | - * @throws \OCP\Files\NotPermittedException |
|
199 | - * @throws \OCP\Files\NotFoundException |
|
200 | - */ |
|
201 | - public function get($path) { |
|
202 | - $path = $this->normalizePath($path); |
|
203 | - if ($this->isValidPath($path)) { |
|
204 | - $fullPath = $this->getFullPath($path); |
|
205 | - $fileInfo = $this->view->getFileInfo($fullPath, false); |
|
206 | - if ($fileInfo) { |
|
207 | - return $this->createNode($fullPath, $fileInfo, false); |
|
208 | - } else { |
|
209 | - throw new NotFoundException($path); |
|
210 | - } |
|
211 | - } else { |
|
212 | - throw new NotPermittedException(); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - //most operations can't be done on the root |
|
217 | - |
|
218 | - /** |
|
219 | - * @param string $targetPath |
|
220 | - * @return Node |
|
221 | - * @throws \OCP\Files\NotPermittedException |
|
222 | - */ |
|
223 | - public function rename($targetPath) { |
|
224 | - throw new NotPermittedException(); |
|
225 | - } |
|
226 | - |
|
227 | - public function delete() { |
|
228 | - throw new NotPermittedException(); |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * @param string $targetPath |
|
233 | - * @return Node |
|
234 | - * @throws \OCP\Files\NotPermittedException |
|
235 | - */ |
|
236 | - public function copy($targetPath) { |
|
237 | - throw new NotPermittedException(); |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * @param int $mtime |
|
242 | - * @throws \OCP\Files\NotPermittedException |
|
243 | - */ |
|
244 | - public function touch($mtime = null) { |
|
245 | - throw new NotPermittedException(); |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * @return \OC\Files\Storage\Storage |
|
250 | - * @throws \OCP\Files\NotFoundException |
|
251 | - */ |
|
252 | - public function getStorage() { |
|
253 | - throw new NotFoundException(); |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * @return string |
|
258 | - */ |
|
259 | - public function getPath() { |
|
260 | - return '/'; |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * @return string |
|
265 | - */ |
|
266 | - public function getInternalPath() { |
|
267 | - return ''; |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * @return int |
|
272 | - */ |
|
273 | - public function getId() { |
|
274 | - return 0; |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * @return array |
|
279 | - */ |
|
280 | - public function stat() { |
|
281 | - return []; |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * @return int |
|
286 | - */ |
|
287 | - public function getMTime() { |
|
288 | - return 0; |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * @param bool $includeMounts |
|
293 | - * @return int|float |
|
294 | - */ |
|
295 | - public function getSize($includeMounts = true): int|float { |
|
296 | - return 0; |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * @return string |
|
301 | - */ |
|
302 | - public function getEtag() { |
|
303 | - return ''; |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * @return int |
|
308 | - */ |
|
309 | - public function getPermissions() { |
|
310 | - return \OCP\Constants::PERMISSION_CREATE; |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * @return bool |
|
315 | - */ |
|
316 | - public function isReadable() { |
|
317 | - return false; |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * @return bool |
|
322 | - */ |
|
323 | - public function isUpdateable() { |
|
324 | - return false; |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * @return bool |
|
329 | - */ |
|
330 | - public function isDeletable() { |
|
331 | - return false; |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * @return bool |
|
336 | - */ |
|
337 | - public function isShareable() { |
|
338 | - return false; |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * @return Node |
|
343 | - * @throws \OCP\Files\NotFoundException |
|
344 | - */ |
|
345 | - public function getParent() { |
|
346 | - throw new NotFoundException(); |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * @return string |
|
351 | - */ |
|
352 | - public function getName() { |
|
353 | - return ''; |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * Returns a view to user's files folder |
|
358 | - * |
|
359 | - * @param string $userId user ID |
|
360 | - * @return \OCP\Files\Folder |
|
361 | - * @throws NoUserException |
|
362 | - * @throws NotPermittedException |
|
363 | - */ |
|
364 | - public function getUserFolder($userId) { |
|
365 | - $userObject = $this->userManager->get($userId); |
|
366 | - |
|
367 | - if (is_null($userObject)) { |
|
368 | - $e = new NoUserException('Backends provided no user object'); |
|
369 | - $this->logger->error( |
|
370 | - sprintf( |
|
371 | - 'Backends provided no user object for %s', |
|
372 | - $userId |
|
373 | - ), |
|
374 | - [ |
|
375 | - 'app' => 'files', |
|
376 | - 'exception' => $e, |
|
377 | - ] |
|
378 | - ); |
|
379 | - throw $e; |
|
380 | - } |
|
381 | - |
|
382 | - $userId = $userObject->getUID(); |
|
383 | - |
|
384 | - if (!$this->userFolderCache->hasKey($userId)) { |
|
385 | - if ($this->mountManager->getSetupManager()->isSetupComplete($userObject)) { |
|
386 | - try { |
|
387 | - $folder = $this->get('/' . $userId . '/files'); |
|
388 | - if (!$folder instanceof \OCP\Files\Folder) { |
|
389 | - throw new \Exception("User folder for $userId exists as a file"); |
|
390 | - } |
|
391 | - } catch (NotFoundException $e) { |
|
392 | - if (!$this->nodeExists('/' . $userId)) { |
|
393 | - $this->newFolder('/' . $userId); |
|
394 | - } |
|
395 | - $folder = $this->newFolder('/' . $userId . '/files'); |
|
396 | - } |
|
397 | - } else { |
|
398 | - $folder = new LazyUserFolder($this, $userObject); |
|
399 | - } |
|
400 | - |
|
401 | - $this->userFolderCache->set($userId, $folder); |
|
402 | - } |
|
403 | - |
|
404 | - return $this->userFolderCache->get($userId); |
|
405 | - } |
|
406 | - |
|
407 | - public function getUserMountCache() { |
|
408 | - return $this->userMountCache; |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * @param int $id |
|
413 | - * @return Node[] |
|
414 | - */ |
|
415 | - public function getByIdInPath(int $id, string $path): array { |
|
416 | - $mountCache = $this->getUserMountCache(); |
|
417 | - if (strpos($path, '/', 1) > 0) { |
|
418 | - [, $user] = explode('/', $path); |
|
419 | - } else { |
|
420 | - $user = null; |
|
421 | - } |
|
422 | - $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
423 | - |
|
424 | - // if the mount isn't in the cache yet, perform a setup first, then try again |
|
425 | - if (count($mountsContainingFile) === 0) { |
|
426 | - $this->mountManager->getSetupManager()->setupForPath($path, true); |
|
427 | - $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
428 | - } |
|
429 | - |
|
430 | - // when a user has access through the same storage through multiple paths |
|
431 | - // (such as an external storage that is both mounted for a user and shared to the user) |
|
432 | - // the mount cache will only hold a single entry for the storage |
|
433 | - // this can lead to issues as the different ways the user has access to a storage can have different permissions |
|
434 | - // |
|
435 | - // so instead of using the cached entries directly, we instead filter the current mounts by the rootid of the cache entry |
|
436 | - |
|
437 | - $mountRootIds = array_map(function ($mount) { |
|
438 | - return $mount->getRootId(); |
|
439 | - }, $mountsContainingFile); |
|
440 | - $mountRootPaths = array_map(function ($mount) { |
|
441 | - return $mount->getRootInternalPath(); |
|
442 | - }, $mountsContainingFile); |
|
443 | - $mountProviders = array_unique(array_map(function ($mount) { |
|
444 | - return $mount->getMountProvider(); |
|
445 | - }, $mountsContainingFile)); |
|
446 | - $mountRoots = array_combine($mountRootIds, $mountRootPaths); |
|
447 | - |
|
448 | - $mounts = $this->mountManager->getMountsByMountProvider($path, $mountProviders); |
|
449 | - |
|
450 | - $mountsContainingFile = array_filter($mounts, function ($mount) use ($mountRoots) { |
|
451 | - return isset($mountRoots[$mount->getStorageRootId()]); |
|
452 | - }); |
|
453 | - |
|
454 | - if (count($mountsContainingFile) === 0) { |
|
455 | - if ($user === $this->getAppDataDirectoryName()) { |
|
456 | - $folder = $this->get($path); |
|
457 | - if ($folder instanceof Folder) { |
|
458 | - return $folder->getByIdInRootMount($id); |
|
459 | - } else { |
|
460 | - throw new \Exception("getByIdInPath with non folder"); |
|
461 | - } |
|
462 | - } |
|
463 | - return []; |
|
464 | - } |
|
465 | - |
|
466 | - $nodes = array_map(function (IMountPoint $mount) use ($id, $mountRoots) { |
|
467 | - $rootInternalPath = $mountRoots[$mount->getStorageRootId()]; |
|
468 | - $cacheEntry = $mount->getStorage()->getCache()->get($id); |
|
469 | - if (!$cacheEntry) { |
|
470 | - return null; |
|
471 | - } |
|
472 | - |
|
473 | - // cache jails will hide the "true" internal path |
|
474 | - $internalPath = ltrim($rootInternalPath . '/' . $cacheEntry->getPath(), '/'); |
|
475 | - $pathRelativeToMount = substr($internalPath, strlen($rootInternalPath)); |
|
476 | - $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
|
477 | - $absolutePath = rtrim($mount->getMountPoint() . $pathRelativeToMount, '/'); |
|
478 | - return $this->createNode($absolutePath, new FileInfo( |
|
479 | - $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount, |
|
480 | - \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount)) |
|
481 | - )); |
|
482 | - }, $mountsContainingFile); |
|
483 | - |
|
484 | - $nodes = array_filter($nodes); |
|
485 | - |
|
486 | - $folders = array_filter($nodes, function (Node $node) use ($path) { |
|
487 | - return PathHelper::getRelativePath($path, $node->getPath()) !== null; |
|
488 | - }); |
|
489 | - usort($folders, function ($a, $b) { |
|
490 | - return $b->getPath() <=> $a->getPath(); |
|
491 | - }); |
|
492 | - return $folders; |
|
493 | - } |
|
74 | + private Manager $mountManager; |
|
75 | + private PublicEmitter $emitter; |
|
76 | + private ?IUser $user; |
|
77 | + private CappedMemoryCache $userFolderCache; |
|
78 | + private IUserMountCache $userMountCache; |
|
79 | + private LoggerInterface $logger; |
|
80 | + private IUserManager $userManager; |
|
81 | + private IEventDispatcher $eventDispatcher; |
|
82 | + |
|
83 | + /** |
|
84 | + * @param Manager $manager |
|
85 | + * @param View $view |
|
86 | + * @param IUser|null $user |
|
87 | + */ |
|
88 | + public function __construct( |
|
89 | + $manager, |
|
90 | + $view, |
|
91 | + $user, |
|
92 | + IUserMountCache $userMountCache, |
|
93 | + LoggerInterface $logger, |
|
94 | + IUserManager $userManager, |
|
95 | + IEventDispatcher $eventDispatcher |
|
96 | + ) { |
|
97 | + parent::__construct($this, $view, ''); |
|
98 | + $this->mountManager = $manager; |
|
99 | + $this->user = $user; |
|
100 | + $this->emitter = new PublicEmitter(); |
|
101 | + $this->userFolderCache = new CappedMemoryCache(); |
|
102 | + $this->userMountCache = $userMountCache; |
|
103 | + $this->logger = $logger; |
|
104 | + $this->userManager = $userManager; |
|
105 | + $eventDispatcher->addListener(FilesystemTornDownEvent::class, function () { |
|
106 | + $this->userFolderCache = new CappedMemoryCache(); |
|
107 | + }); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Get the user for which the filesystem is setup |
|
112 | + * |
|
113 | + * @return \OC\User\User |
|
114 | + */ |
|
115 | + public function getUser() { |
|
116 | + return $this->user; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * @param string $scope |
|
121 | + * @param string $method |
|
122 | + * @param callable $callback |
|
123 | + */ |
|
124 | + public function listen($scope, $method, callable $callback) { |
|
125 | + $this->emitter->listen($scope, $method, $callback); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * @param string $scope optional |
|
130 | + * @param string $method optional |
|
131 | + * @param callable $callback optional |
|
132 | + */ |
|
133 | + public function removeListener($scope = null, $method = null, callable $callback = null) { |
|
134 | + $this->emitter->removeListener($scope, $method, $callback); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param string $scope |
|
139 | + * @param string $method |
|
140 | + * @param Node[] $arguments |
|
141 | + */ |
|
142 | + public function emit($scope, $method, $arguments = []) { |
|
143 | + $this->emitter->emit($scope, $method, $arguments); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * @param \OC\Files\Storage\Storage $storage |
|
148 | + * @param string $mountPoint |
|
149 | + * @param array $arguments |
|
150 | + */ |
|
151 | + public function mount($storage, $mountPoint, $arguments = []) { |
|
152 | + $mount = new MountPoint($storage, $mountPoint, $arguments); |
|
153 | + $this->mountManager->addMount($mount); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * @param string $mountPoint |
|
158 | + * @return \OC\Files\Mount\MountPoint |
|
159 | + */ |
|
160 | + public function getMount($mountPoint) { |
|
161 | + return $this->mountManager->find($mountPoint); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @param string $mountPoint |
|
166 | + * @return \OC\Files\Mount\MountPoint[] |
|
167 | + */ |
|
168 | + public function getMountsIn($mountPoint) { |
|
169 | + return $this->mountManager->findIn($mountPoint); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @param string $storageId |
|
174 | + * @return \OC\Files\Mount\MountPoint[] |
|
175 | + */ |
|
176 | + public function getMountByStorageId($storageId) { |
|
177 | + return $this->mountManager->findByStorageId($storageId); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @param int $numericId |
|
182 | + * @return MountPoint[] |
|
183 | + */ |
|
184 | + public function getMountByNumericStorageId($numericId) { |
|
185 | + return $this->mountManager->findByNumericId($numericId); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @param \OC\Files\Mount\MountPoint $mount |
|
190 | + */ |
|
191 | + public function unMount($mount) { |
|
192 | + $this->mountManager->remove($mount); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * @param string $path |
|
197 | + * @return Node |
|
198 | + * @throws \OCP\Files\NotPermittedException |
|
199 | + * @throws \OCP\Files\NotFoundException |
|
200 | + */ |
|
201 | + public function get($path) { |
|
202 | + $path = $this->normalizePath($path); |
|
203 | + if ($this->isValidPath($path)) { |
|
204 | + $fullPath = $this->getFullPath($path); |
|
205 | + $fileInfo = $this->view->getFileInfo($fullPath, false); |
|
206 | + if ($fileInfo) { |
|
207 | + return $this->createNode($fullPath, $fileInfo, false); |
|
208 | + } else { |
|
209 | + throw new NotFoundException($path); |
|
210 | + } |
|
211 | + } else { |
|
212 | + throw new NotPermittedException(); |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + //most operations can't be done on the root |
|
217 | + |
|
218 | + /** |
|
219 | + * @param string $targetPath |
|
220 | + * @return Node |
|
221 | + * @throws \OCP\Files\NotPermittedException |
|
222 | + */ |
|
223 | + public function rename($targetPath) { |
|
224 | + throw new NotPermittedException(); |
|
225 | + } |
|
226 | + |
|
227 | + public function delete() { |
|
228 | + throw new NotPermittedException(); |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * @param string $targetPath |
|
233 | + * @return Node |
|
234 | + * @throws \OCP\Files\NotPermittedException |
|
235 | + */ |
|
236 | + public function copy($targetPath) { |
|
237 | + throw new NotPermittedException(); |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * @param int $mtime |
|
242 | + * @throws \OCP\Files\NotPermittedException |
|
243 | + */ |
|
244 | + public function touch($mtime = null) { |
|
245 | + throw new NotPermittedException(); |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * @return \OC\Files\Storage\Storage |
|
250 | + * @throws \OCP\Files\NotFoundException |
|
251 | + */ |
|
252 | + public function getStorage() { |
|
253 | + throw new NotFoundException(); |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * @return string |
|
258 | + */ |
|
259 | + public function getPath() { |
|
260 | + return '/'; |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * @return string |
|
265 | + */ |
|
266 | + public function getInternalPath() { |
|
267 | + return ''; |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * @return int |
|
272 | + */ |
|
273 | + public function getId() { |
|
274 | + return 0; |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * @return array |
|
279 | + */ |
|
280 | + public function stat() { |
|
281 | + return []; |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * @return int |
|
286 | + */ |
|
287 | + public function getMTime() { |
|
288 | + return 0; |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * @param bool $includeMounts |
|
293 | + * @return int|float |
|
294 | + */ |
|
295 | + public function getSize($includeMounts = true): int|float { |
|
296 | + return 0; |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * @return string |
|
301 | + */ |
|
302 | + public function getEtag() { |
|
303 | + return ''; |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * @return int |
|
308 | + */ |
|
309 | + public function getPermissions() { |
|
310 | + return \OCP\Constants::PERMISSION_CREATE; |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * @return bool |
|
315 | + */ |
|
316 | + public function isReadable() { |
|
317 | + return false; |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * @return bool |
|
322 | + */ |
|
323 | + public function isUpdateable() { |
|
324 | + return false; |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * @return bool |
|
329 | + */ |
|
330 | + public function isDeletable() { |
|
331 | + return false; |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * @return bool |
|
336 | + */ |
|
337 | + public function isShareable() { |
|
338 | + return false; |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * @return Node |
|
343 | + * @throws \OCP\Files\NotFoundException |
|
344 | + */ |
|
345 | + public function getParent() { |
|
346 | + throw new NotFoundException(); |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * @return string |
|
351 | + */ |
|
352 | + public function getName() { |
|
353 | + return ''; |
|
354 | + } |
|
355 | + |
|
356 | + /** |
|
357 | + * Returns a view to user's files folder |
|
358 | + * |
|
359 | + * @param string $userId user ID |
|
360 | + * @return \OCP\Files\Folder |
|
361 | + * @throws NoUserException |
|
362 | + * @throws NotPermittedException |
|
363 | + */ |
|
364 | + public function getUserFolder($userId) { |
|
365 | + $userObject = $this->userManager->get($userId); |
|
366 | + |
|
367 | + if (is_null($userObject)) { |
|
368 | + $e = new NoUserException('Backends provided no user object'); |
|
369 | + $this->logger->error( |
|
370 | + sprintf( |
|
371 | + 'Backends provided no user object for %s', |
|
372 | + $userId |
|
373 | + ), |
|
374 | + [ |
|
375 | + 'app' => 'files', |
|
376 | + 'exception' => $e, |
|
377 | + ] |
|
378 | + ); |
|
379 | + throw $e; |
|
380 | + } |
|
381 | + |
|
382 | + $userId = $userObject->getUID(); |
|
383 | + |
|
384 | + if (!$this->userFolderCache->hasKey($userId)) { |
|
385 | + if ($this->mountManager->getSetupManager()->isSetupComplete($userObject)) { |
|
386 | + try { |
|
387 | + $folder = $this->get('/' . $userId . '/files'); |
|
388 | + if (!$folder instanceof \OCP\Files\Folder) { |
|
389 | + throw new \Exception("User folder for $userId exists as a file"); |
|
390 | + } |
|
391 | + } catch (NotFoundException $e) { |
|
392 | + if (!$this->nodeExists('/' . $userId)) { |
|
393 | + $this->newFolder('/' . $userId); |
|
394 | + } |
|
395 | + $folder = $this->newFolder('/' . $userId . '/files'); |
|
396 | + } |
|
397 | + } else { |
|
398 | + $folder = new LazyUserFolder($this, $userObject); |
|
399 | + } |
|
400 | + |
|
401 | + $this->userFolderCache->set($userId, $folder); |
|
402 | + } |
|
403 | + |
|
404 | + return $this->userFolderCache->get($userId); |
|
405 | + } |
|
406 | + |
|
407 | + public function getUserMountCache() { |
|
408 | + return $this->userMountCache; |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * @param int $id |
|
413 | + * @return Node[] |
|
414 | + */ |
|
415 | + public function getByIdInPath(int $id, string $path): array { |
|
416 | + $mountCache = $this->getUserMountCache(); |
|
417 | + if (strpos($path, '/', 1) > 0) { |
|
418 | + [, $user] = explode('/', $path); |
|
419 | + } else { |
|
420 | + $user = null; |
|
421 | + } |
|
422 | + $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
423 | + |
|
424 | + // if the mount isn't in the cache yet, perform a setup first, then try again |
|
425 | + if (count($mountsContainingFile) === 0) { |
|
426 | + $this->mountManager->getSetupManager()->setupForPath($path, true); |
|
427 | + $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
428 | + } |
|
429 | + |
|
430 | + // when a user has access through the same storage through multiple paths |
|
431 | + // (such as an external storage that is both mounted for a user and shared to the user) |
|
432 | + // the mount cache will only hold a single entry for the storage |
|
433 | + // this can lead to issues as the different ways the user has access to a storage can have different permissions |
|
434 | + // |
|
435 | + // so instead of using the cached entries directly, we instead filter the current mounts by the rootid of the cache entry |
|
436 | + |
|
437 | + $mountRootIds = array_map(function ($mount) { |
|
438 | + return $mount->getRootId(); |
|
439 | + }, $mountsContainingFile); |
|
440 | + $mountRootPaths = array_map(function ($mount) { |
|
441 | + return $mount->getRootInternalPath(); |
|
442 | + }, $mountsContainingFile); |
|
443 | + $mountProviders = array_unique(array_map(function ($mount) { |
|
444 | + return $mount->getMountProvider(); |
|
445 | + }, $mountsContainingFile)); |
|
446 | + $mountRoots = array_combine($mountRootIds, $mountRootPaths); |
|
447 | + |
|
448 | + $mounts = $this->mountManager->getMountsByMountProvider($path, $mountProviders); |
|
449 | + |
|
450 | + $mountsContainingFile = array_filter($mounts, function ($mount) use ($mountRoots) { |
|
451 | + return isset($mountRoots[$mount->getStorageRootId()]); |
|
452 | + }); |
|
453 | + |
|
454 | + if (count($mountsContainingFile) === 0) { |
|
455 | + if ($user === $this->getAppDataDirectoryName()) { |
|
456 | + $folder = $this->get($path); |
|
457 | + if ($folder instanceof Folder) { |
|
458 | + return $folder->getByIdInRootMount($id); |
|
459 | + } else { |
|
460 | + throw new \Exception("getByIdInPath with non folder"); |
|
461 | + } |
|
462 | + } |
|
463 | + return []; |
|
464 | + } |
|
465 | + |
|
466 | + $nodes = array_map(function (IMountPoint $mount) use ($id, $mountRoots) { |
|
467 | + $rootInternalPath = $mountRoots[$mount->getStorageRootId()]; |
|
468 | + $cacheEntry = $mount->getStorage()->getCache()->get($id); |
|
469 | + if (!$cacheEntry) { |
|
470 | + return null; |
|
471 | + } |
|
472 | + |
|
473 | + // cache jails will hide the "true" internal path |
|
474 | + $internalPath = ltrim($rootInternalPath . '/' . $cacheEntry->getPath(), '/'); |
|
475 | + $pathRelativeToMount = substr($internalPath, strlen($rootInternalPath)); |
|
476 | + $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
|
477 | + $absolutePath = rtrim($mount->getMountPoint() . $pathRelativeToMount, '/'); |
|
478 | + return $this->createNode($absolutePath, new FileInfo( |
|
479 | + $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount, |
|
480 | + \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount)) |
|
481 | + )); |
|
482 | + }, $mountsContainingFile); |
|
483 | + |
|
484 | + $nodes = array_filter($nodes); |
|
485 | + |
|
486 | + $folders = array_filter($nodes, function (Node $node) use ($path) { |
|
487 | + return PathHelper::getRelativePath($path, $node->getPath()) !== null; |
|
488 | + }); |
|
489 | + usort($folders, function ($a, $b) { |
|
490 | + return $b->getPath() <=> $a->getPath(); |
|
491 | + }); |
|
492 | + return $folders; |
|
493 | + } |
|
494 | 494 | } |
@@ -33,127 +33,127 @@ |
||
33 | 33 | use OCP\Lock\LockedException; |
34 | 34 | |
35 | 35 | class File extends Node implements \OCP\Files\File { |
36 | - /** |
|
37 | - * Creates a Folder that represents a non-existing path |
|
38 | - * |
|
39 | - * @param string $path path |
|
40 | - * @return NonExistingFile non-existing node |
|
41 | - */ |
|
42 | - protected function createNonExistingNode($path) { |
|
43 | - return new NonExistingFile($this->root, $this->view, $path); |
|
44 | - } |
|
36 | + /** |
|
37 | + * Creates a Folder that represents a non-existing path |
|
38 | + * |
|
39 | + * @param string $path path |
|
40 | + * @return NonExistingFile non-existing node |
|
41 | + */ |
|
42 | + protected function createNonExistingNode($path) { |
|
43 | + return new NonExistingFile($this->root, $this->view, $path); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * @return string |
|
48 | - * @throws NotPermittedException |
|
49 | - * @throws LockedException |
|
50 | - */ |
|
51 | - public function getContent() { |
|
52 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) { |
|
53 | - /** |
|
54 | - * @var \OC\Files\Storage\Storage $storage; |
|
55 | - */ |
|
56 | - return $this->view->file_get_contents($this->path); |
|
57 | - } else { |
|
58 | - throw new NotPermittedException(); |
|
59 | - } |
|
60 | - } |
|
46 | + /** |
|
47 | + * @return string |
|
48 | + * @throws NotPermittedException |
|
49 | + * @throws LockedException |
|
50 | + */ |
|
51 | + public function getContent() { |
|
52 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) { |
|
53 | + /** |
|
54 | + * @var \OC\Files\Storage\Storage $storage; |
|
55 | + */ |
|
56 | + return $this->view->file_get_contents($this->path); |
|
57 | + } else { |
|
58 | + throw new NotPermittedException(); |
|
59 | + } |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @param string|resource $data |
|
64 | - * @throws NotPermittedException |
|
65 | - * @throws \OCP\Files\GenericFileException |
|
66 | - * @throws LockedException |
|
67 | - */ |
|
68 | - public function putContent($data) { |
|
69 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) { |
|
70 | - $this->sendHooks(['preWrite']); |
|
71 | - if ($this->view->file_put_contents($this->path, $data) === false) { |
|
72 | - throw new GenericFileException('file_put_contents failed'); |
|
73 | - } |
|
74 | - $this->fileInfo = null; |
|
75 | - $this->sendHooks(['postWrite']); |
|
76 | - } else { |
|
77 | - throw new NotPermittedException(); |
|
78 | - } |
|
79 | - } |
|
62 | + /** |
|
63 | + * @param string|resource $data |
|
64 | + * @throws NotPermittedException |
|
65 | + * @throws \OCP\Files\GenericFileException |
|
66 | + * @throws LockedException |
|
67 | + */ |
|
68 | + public function putContent($data) { |
|
69 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) { |
|
70 | + $this->sendHooks(['preWrite']); |
|
71 | + if ($this->view->file_put_contents($this->path, $data) === false) { |
|
72 | + throw new GenericFileException('file_put_contents failed'); |
|
73 | + } |
|
74 | + $this->fileInfo = null; |
|
75 | + $this->sendHooks(['postWrite']); |
|
76 | + } else { |
|
77 | + throw new NotPermittedException(); |
|
78 | + } |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * @param string $mode |
|
83 | - * @return resource |
|
84 | - * @throws NotPermittedException |
|
85 | - * @throws LockedException |
|
86 | - */ |
|
87 | - public function fopen($mode) { |
|
88 | - $preHooks = []; |
|
89 | - $postHooks = []; |
|
90 | - $requiredPermissions = \OCP\Constants::PERMISSION_READ; |
|
91 | - switch ($mode) { |
|
92 | - case 'r+': |
|
93 | - case 'rb+': |
|
94 | - case 'w+': |
|
95 | - case 'wb+': |
|
96 | - case 'x+': |
|
97 | - case 'xb+': |
|
98 | - case 'a+': |
|
99 | - case 'ab+': |
|
100 | - case 'w': |
|
101 | - case 'wb': |
|
102 | - case 'x': |
|
103 | - case 'xb': |
|
104 | - case 'a': |
|
105 | - case 'ab': |
|
106 | - $preHooks[] = 'preWrite'; |
|
107 | - $postHooks[] = 'postWrite'; |
|
108 | - $requiredPermissions |= \OCP\Constants::PERMISSION_UPDATE; |
|
109 | - break; |
|
110 | - } |
|
81 | + /** |
|
82 | + * @param string $mode |
|
83 | + * @return resource |
|
84 | + * @throws NotPermittedException |
|
85 | + * @throws LockedException |
|
86 | + */ |
|
87 | + public function fopen($mode) { |
|
88 | + $preHooks = []; |
|
89 | + $postHooks = []; |
|
90 | + $requiredPermissions = \OCP\Constants::PERMISSION_READ; |
|
91 | + switch ($mode) { |
|
92 | + case 'r+': |
|
93 | + case 'rb+': |
|
94 | + case 'w+': |
|
95 | + case 'wb+': |
|
96 | + case 'x+': |
|
97 | + case 'xb+': |
|
98 | + case 'a+': |
|
99 | + case 'ab+': |
|
100 | + case 'w': |
|
101 | + case 'wb': |
|
102 | + case 'x': |
|
103 | + case 'xb': |
|
104 | + case 'a': |
|
105 | + case 'ab': |
|
106 | + $preHooks[] = 'preWrite'; |
|
107 | + $postHooks[] = 'postWrite'; |
|
108 | + $requiredPermissions |= \OCP\Constants::PERMISSION_UPDATE; |
|
109 | + break; |
|
110 | + } |
|
111 | 111 | |
112 | - if ($this->checkPermissions($requiredPermissions)) { |
|
113 | - $this->sendHooks($preHooks); |
|
114 | - $result = $this->view->fopen($this->path, $mode); |
|
115 | - $this->sendHooks($postHooks); |
|
116 | - return $result; |
|
117 | - } else { |
|
118 | - throw new NotPermittedException(); |
|
119 | - } |
|
120 | - } |
|
112 | + if ($this->checkPermissions($requiredPermissions)) { |
|
113 | + $this->sendHooks($preHooks); |
|
114 | + $result = $this->view->fopen($this->path, $mode); |
|
115 | + $this->sendHooks($postHooks); |
|
116 | + return $result; |
|
117 | + } else { |
|
118 | + throw new NotPermittedException(); |
|
119 | + } |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * @throws NotPermittedException |
|
124 | - * @throws \OCP\Files\InvalidPathException |
|
125 | - * @throws \OCP\Files\NotFoundException |
|
126 | - */ |
|
127 | - public function delete() { |
|
128 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
129 | - $this->sendHooks(['preDelete']); |
|
130 | - $fileInfo = $this->getFileInfo(); |
|
131 | - $this->view->unlink($this->path); |
|
132 | - $nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo); |
|
133 | - $this->sendHooks(['postDelete'], [$nonExisting]); |
|
134 | - $this->fileInfo = null; |
|
135 | - } else { |
|
136 | - throw new NotPermittedException(); |
|
137 | - } |
|
138 | - } |
|
122 | + /** |
|
123 | + * @throws NotPermittedException |
|
124 | + * @throws \OCP\Files\InvalidPathException |
|
125 | + * @throws \OCP\Files\NotFoundException |
|
126 | + */ |
|
127 | + public function delete() { |
|
128 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
129 | + $this->sendHooks(['preDelete']); |
|
130 | + $fileInfo = $this->getFileInfo(); |
|
131 | + $this->view->unlink($this->path); |
|
132 | + $nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo); |
|
133 | + $this->sendHooks(['postDelete'], [$nonExisting]); |
|
134 | + $this->fileInfo = null; |
|
135 | + } else { |
|
136 | + throw new NotPermittedException(); |
|
137 | + } |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * @param string $type |
|
142 | - * @param bool $raw |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function hash($type, $raw = false) { |
|
146 | - return $this->view->hash($type, $this->path, $raw); |
|
147 | - } |
|
140 | + /** |
|
141 | + * @param string $type |
|
142 | + * @param bool $raw |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function hash($type, $raw = false) { |
|
146 | + return $this->view->hash($type, $this->path, $raw); |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * @inheritdoc |
|
151 | - */ |
|
152 | - public function getChecksum() { |
|
153 | - return $this->getFileInfo()->getChecksum(); |
|
154 | - } |
|
149 | + /** |
|
150 | + * @inheritdoc |
|
151 | + */ |
|
152 | + public function getChecksum() { |
|
153 | + return $this->getFileInfo()->getChecksum(); |
|
154 | + } |
|
155 | 155 | |
156 | - public function getExtension(): string { |
|
157 | - return $this->getFileInfo()->getExtension(); |
|
158 | - } |
|
156 | + public function getExtension(): string { |
|
157 | + return $this->getFileInfo()->getExtension(); |
|
158 | + } |
|
159 | 159 | } |
@@ -41,436 +41,436 @@ |
||
41 | 41 | |
42 | 42 | // FIXME: this class really should be abstract |
43 | 43 | class Node implements \OCP\Files\Node { |
44 | - /** |
|
45 | - * @var \OC\Files\View $view |
|
46 | - */ |
|
47 | - protected $view; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var \OC\Files\Node\Root $root |
|
51 | - */ |
|
52 | - protected $root; |
|
53 | - |
|
54 | - /** |
|
55 | - * @var string $path |
|
56 | - */ |
|
57 | - protected $path; |
|
58 | - |
|
59 | - protected ?FileInfo $fileInfo; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var Node|null |
|
63 | - */ |
|
64 | - protected $parent; |
|
65 | - |
|
66 | - private bool $infoHasSubMountsIncluded; |
|
67 | - |
|
68 | - /** |
|
69 | - * @param \OC\Files\View $view |
|
70 | - * @param \OCP\Files\IRootFolder $root |
|
71 | - * @param string $path |
|
72 | - * @param FileInfo $fileInfo |
|
73 | - */ |
|
74 | - public function __construct($root, $view, $path, $fileInfo = null, ?Node $parent = null, bool $infoHasSubMountsIncluded = true) { |
|
75 | - $this->view = $view; |
|
76 | - $this->root = $root; |
|
77 | - $this->path = $path; |
|
78 | - $this->fileInfo = $fileInfo; |
|
79 | - $this->parent = $parent; |
|
80 | - $this->infoHasSubMountsIncluded = $infoHasSubMountsIncluded; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Creates a Node of the same type that represents a non-existing path |
|
85 | - * |
|
86 | - * @param string $path path |
|
87 | - * @return Node non-existing node |
|
88 | - * @throws \Exception |
|
89 | - */ |
|
90 | - protected function createNonExistingNode($path) { |
|
91 | - throw new \Exception('Must be implemented by subclasses'); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Returns the matching file info |
|
96 | - * |
|
97 | - * @return FileInfo |
|
98 | - * @throws InvalidPathException |
|
99 | - * @throws NotFoundException |
|
100 | - */ |
|
101 | - public function getFileInfo(bool $includeMountPoint = true) { |
|
102 | - if (!$this->fileInfo) { |
|
103 | - if (!Filesystem::isValidPath($this->path)) { |
|
104 | - throw new InvalidPathException(); |
|
105 | - } |
|
106 | - $fileInfo = $this->view->getFileInfo($this->path, $includeMountPoint); |
|
107 | - $this->infoHasSubMountsIncluded = $includeMountPoint; |
|
108 | - if ($fileInfo instanceof FileInfo) { |
|
109 | - $this->fileInfo = $fileInfo; |
|
110 | - } else { |
|
111 | - throw new NotFoundException(); |
|
112 | - } |
|
113 | - } elseif ($includeMountPoint && !$this->infoHasSubMountsIncluded && $this instanceof Folder) { |
|
114 | - if ($this->fileInfo instanceof \OC\Files\FileInfo) { |
|
115 | - $this->view->addSubMounts($this->fileInfo); |
|
116 | - } |
|
117 | - $this->infoHasSubMountsIncluded = true; |
|
118 | - } |
|
119 | - return $this->fileInfo; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @param string[] $hooks |
|
124 | - */ |
|
125 | - protected function sendHooks($hooks, array $args = null) { |
|
126 | - $args = !empty($args) ? $args : [$this]; |
|
127 | - $dispatcher = \OC::$server->getEventDispatcher(); |
|
128 | - foreach ($hooks as $hook) { |
|
129 | - $this->root->emit('\OC\Files', $hook, $args); |
|
130 | - $dispatcher->dispatch('\OCP\Files::' . $hook, new GenericEvent($args)); |
|
131 | - } |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @param int $permissions |
|
136 | - * @return bool |
|
137 | - * @throws InvalidPathException |
|
138 | - * @throws NotFoundException |
|
139 | - */ |
|
140 | - protected function checkPermissions($permissions) { |
|
141 | - return ($this->getPermissions() & $permissions) === $permissions; |
|
142 | - } |
|
143 | - |
|
144 | - public function delete() { |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param int $mtime |
|
149 | - * @throws InvalidPathException |
|
150 | - * @throws NotFoundException |
|
151 | - * @throws NotPermittedException |
|
152 | - */ |
|
153 | - public function touch($mtime = null) { |
|
154 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) { |
|
155 | - $this->sendHooks(['preTouch']); |
|
156 | - $this->view->touch($this->path, $mtime); |
|
157 | - $this->sendHooks(['postTouch']); |
|
158 | - if ($this->fileInfo) { |
|
159 | - if (is_null($mtime)) { |
|
160 | - $mtime = time(); |
|
161 | - } |
|
162 | - $this->fileInfo['mtime'] = $mtime; |
|
163 | - } |
|
164 | - } else { |
|
165 | - throw new NotPermittedException(); |
|
166 | - } |
|
167 | - } |
|
168 | - |
|
169 | - public function getStorage() { |
|
170 | - $storage = $this->getMountPoint()->getStorage(); |
|
171 | - if (!$storage) { |
|
172 | - throw new \Exception("No storage for node"); |
|
173 | - } |
|
174 | - return $storage; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * @return string |
|
179 | - */ |
|
180 | - public function getPath() { |
|
181 | - return $this->path; |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * @return string |
|
186 | - */ |
|
187 | - public function getInternalPath() { |
|
188 | - return $this->getFileInfo(false)->getInternalPath(); |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * @return int |
|
193 | - * @throws InvalidPathException |
|
194 | - * @throws NotFoundException |
|
195 | - */ |
|
196 | - public function getId() { |
|
197 | - return $this->getFileInfo(false)->getId() ?? -1; |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * @return array |
|
202 | - */ |
|
203 | - public function stat() { |
|
204 | - return $this->view->stat($this->path); |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * @return int |
|
209 | - * @throws InvalidPathException |
|
210 | - * @throws NotFoundException |
|
211 | - */ |
|
212 | - public function getMTime() { |
|
213 | - return $this->getFileInfo()->getMTime(); |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * @param bool $includeMounts |
|
218 | - * @return int|float |
|
219 | - * @throws InvalidPathException |
|
220 | - * @throws NotFoundException |
|
221 | - */ |
|
222 | - public function getSize($includeMounts = true): int|float { |
|
223 | - return $this->getFileInfo()->getSize($includeMounts); |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * @return string |
|
228 | - * @throws InvalidPathException |
|
229 | - * @throws NotFoundException |
|
230 | - */ |
|
231 | - public function getEtag() { |
|
232 | - return $this->getFileInfo()->getEtag(); |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * @return int |
|
237 | - * @throws InvalidPathException |
|
238 | - * @throws NotFoundException |
|
239 | - */ |
|
240 | - public function getPermissions() { |
|
241 | - return $this->getFileInfo(false)->getPermissions(); |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * @return bool |
|
246 | - * @throws InvalidPathException |
|
247 | - * @throws NotFoundException |
|
248 | - */ |
|
249 | - public function isReadable() { |
|
250 | - return $this->getFileInfo(false)->isReadable(); |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * @return bool |
|
255 | - * @throws InvalidPathException |
|
256 | - * @throws NotFoundException |
|
257 | - */ |
|
258 | - public function isUpdateable() { |
|
259 | - return $this->getFileInfo(false)->isUpdateable(); |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * @return bool |
|
264 | - * @throws InvalidPathException |
|
265 | - * @throws NotFoundException |
|
266 | - */ |
|
267 | - public function isDeletable() { |
|
268 | - return $this->getFileInfo(false)->isDeletable(); |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * @return bool |
|
273 | - * @throws InvalidPathException |
|
274 | - * @throws NotFoundException |
|
275 | - */ |
|
276 | - public function isShareable() { |
|
277 | - return $this->getFileInfo(false)->isShareable(); |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * @return bool |
|
282 | - * @throws InvalidPathException |
|
283 | - * @throws NotFoundException |
|
284 | - */ |
|
285 | - public function isCreatable() { |
|
286 | - return $this->getFileInfo(false)->isCreatable(); |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * @return Node |
|
291 | - */ |
|
292 | - public function getParent() { |
|
293 | - if ($this->parent === null) { |
|
294 | - $newPath = dirname($this->path); |
|
295 | - if ($newPath === '' || $newPath === '.' || $newPath === '/') { |
|
296 | - return $this->root; |
|
297 | - } |
|
298 | - |
|
299 | - $this->parent = $this->root->get($newPath); |
|
300 | - } |
|
301 | - |
|
302 | - return $this->parent; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * @return string |
|
307 | - */ |
|
308 | - public function getName() { |
|
309 | - return basename($this->path); |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * @param string $path |
|
314 | - * @return string |
|
315 | - */ |
|
316 | - protected function normalizePath($path) { |
|
317 | - return PathHelper::normalizePath($path); |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * check if the requested path is valid |
|
322 | - * |
|
323 | - * @param string $path |
|
324 | - * @return bool |
|
325 | - */ |
|
326 | - public function isValidPath($path) { |
|
327 | - if (!$path || $path[0] !== '/') { |
|
328 | - $path = '/' . $path; |
|
329 | - } |
|
330 | - if (strstr($path, '/../') || strrchr($path, '/') === '/..') { |
|
331 | - return false; |
|
332 | - } |
|
333 | - return true; |
|
334 | - } |
|
335 | - |
|
336 | - public function isMounted() { |
|
337 | - return $this->getFileInfo(false)->isMounted(); |
|
338 | - } |
|
339 | - |
|
340 | - public function isShared() { |
|
341 | - return $this->getFileInfo(false)->isShared(); |
|
342 | - } |
|
343 | - |
|
344 | - public function getMimeType() { |
|
345 | - return $this->getFileInfo(false)->getMimetype(); |
|
346 | - } |
|
347 | - |
|
348 | - public function getMimePart() { |
|
349 | - return $this->getFileInfo(false)->getMimePart(); |
|
350 | - } |
|
351 | - |
|
352 | - public function getType() { |
|
353 | - return $this->getFileInfo(false)->getType(); |
|
354 | - } |
|
355 | - |
|
356 | - public function isEncrypted() { |
|
357 | - return $this->getFileInfo(false)->isEncrypted(); |
|
358 | - } |
|
359 | - |
|
360 | - public function getMountPoint() { |
|
361 | - return $this->getFileInfo(false)->getMountPoint(); |
|
362 | - } |
|
363 | - |
|
364 | - public function getOwner() { |
|
365 | - return $this->getFileInfo(false)->getOwner(); |
|
366 | - } |
|
367 | - |
|
368 | - public function getChecksum() { |
|
369 | - } |
|
370 | - |
|
371 | - public function getExtension(): string { |
|
372 | - return $this->getFileInfo(false)->getExtension(); |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
377 | - * @throws LockedException |
|
378 | - */ |
|
379 | - public function lock($type) { |
|
380 | - $this->view->lockFile($this->path, $type); |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
385 | - * @throws LockedException |
|
386 | - */ |
|
387 | - public function changeLock($type) { |
|
388 | - $this->view->changeLock($this->path, $type); |
|
389 | - } |
|
390 | - |
|
391 | - /** |
|
392 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
393 | - * @throws LockedException |
|
394 | - */ |
|
395 | - public function unlock($type) { |
|
396 | - $this->view->unlockFile($this->path, $type); |
|
397 | - } |
|
398 | - |
|
399 | - /** |
|
400 | - * @param string $targetPath |
|
401 | - * @return \OC\Files\Node\Node |
|
402 | - * @throws InvalidPathException |
|
403 | - * @throws NotFoundException |
|
404 | - * @throws NotPermittedException if copy not allowed or failed |
|
405 | - */ |
|
406 | - public function copy($targetPath) { |
|
407 | - $targetPath = $this->normalizePath($targetPath); |
|
408 | - $parent = $this->root->get(dirname($targetPath)); |
|
409 | - if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) { |
|
410 | - $nonExisting = $this->createNonExistingNode($targetPath); |
|
411 | - $this->sendHooks(['preCopy'], [$this, $nonExisting]); |
|
412 | - $this->sendHooks(['preWrite'], [$nonExisting]); |
|
413 | - if (!$this->view->copy($this->path, $targetPath)) { |
|
414 | - throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath); |
|
415 | - } |
|
416 | - $targetNode = $this->root->get($targetPath); |
|
417 | - $this->sendHooks(['postCopy'], [$this, $targetNode]); |
|
418 | - $this->sendHooks(['postWrite'], [$targetNode]); |
|
419 | - return $targetNode; |
|
420 | - } else { |
|
421 | - throw new NotPermittedException('No permission to copy to path ' . $targetPath); |
|
422 | - } |
|
423 | - } |
|
424 | - |
|
425 | - /** |
|
426 | - * @param string $targetPath |
|
427 | - * @return \OC\Files\Node\Node |
|
428 | - * @throws InvalidPathException |
|
429 | - * @throws NotFoundException |
|
430 | - * @throws NotPermittedException if move not allowed or failed |
|
431 | - * @throws LockedException |
|
432 | - */ |
|
433 | - public function move($targetPath) { |
|
434 | - $targetPath = $this->normalizePath($targetPath); |
|
435 | - $parent = $this->root->get(dirname($targetPath)); |
|
436 | - if ( |
|
437 | - $parent instanceof Folder and |
|
438 | - $this->isValidPath($targetPath) and |
|
439 | - ( |
|
440 | - $parent->isCreatable() || |
|
441 | - ($parent->getInternalPath() === '' && $parent->getMountPoint() instanceof MoveableMount) |
|
442 | - ) |
|
443 | - ) { |
|
444 | - $nonExisting = $this->createNonExistingNode($targetPath); |
|
445 | - $this->sendHooks(['preRename'], [$this, $nonExisting]); |
|
446 | - $this->sendHooks(['preWrite'], [$nonExisting]); |
|
447 | - if (!$this->view->rename($this->path, $targetPath)) { |
|
448 | - throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath); |
|
449 | - } |
|
450 | - |
|
451 | - $mountPoint = $this->getMountPoint(); |
|
452 | - if ($mountPoint) { |
|
453 | - // update the cached fileinfo with the new (internal) path |
|
454 | - /** @var \OC\Files\FileInfo $oldFileInfo */ |
|
455 | - $oldFileInfo = $this->getFileInfo(); |
|
456 | - $this->fileInfo = new \OC\Files\FileInfo($targetPath, $oldFileInfo->getStorage(), $mountPoint->getInternalPath($targetPath), $oldFileInfo->getData(), $mountPoint, $oldFileInfo->getOwner()); |
|
457 | - } |
|
458 | - |
|
459 | - $targetNode = $this->root->get($targetPath); |
|
460 | - $this->sendHooks(['postRename'], [$this, $targetNode]); |
|
461 | - $this->sendHooks(['postWrite'], [$targetNode]); |
|
462 | - $this->path = $targetPath; |
|
463 | - return $targetNode; |
|
464 | - } else { |
|
465 | - throw new NotPermittedException('No permission to move to path ' . $targetPath); |
|
466 | - } |
|
467 | - } |
|
468 | - |
|
469 | - public function getCreationTime(): int { |
|
470 | - return $this->getFileInfo()->getCreationTime(); |
|
471 | - } |
|
472 | - |
|
473 | - public function getUploadTime(): int { |
|
474 | - return $this->getFileInfo()->getUploadTime(); |
|
475 | - } |
|
44 | + /** |
|
45 | + * @var \OC\Files\View $view |
|
46 | + */ |
|
47 | + protected $view; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var \OC\Files\Node\Root $root |
|
51 | + */ |
|
52 | + protected $root; |
|
53 | + |
|
54 | + /** |
|
55 | + * @var string $path |
|
56 | + */ |
|
57 | + protected $path; |
|
58 | + |
|
59 | + protected ?FileInfo $fileInfo; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var Node|null |
|
63 | + */ |
|
64 | + protected $parent; |
|
65 | + |
|
66 | + private bool $infoHasSubMountsIncluded; |
|
67 | + |
|
68 | + /** |
|
69 | + * @param \OC\Files\View $view |
|
70 | + * @param \OCP\Files\IRootFolder $root |
|
71 | + * @param string $path |
|
72 | + * @param FileInfo $fileInfo |
|
73 | + */ |
|
74 | + public function __construct($root, $view, $path, $fileInfo = null, ?Node $parent = null, bool $infoHasSubMountsIncluded = true) { |
|
75 | + $this->view = $view; |
|
76 | + $this->root = $root; |
|
77 | + $this->path = $path; |
|
78 | + $this->fileInfo = $fileInfo; |
|
79 | + $this->parent = $parent; |
|
80 | + $this->infoHasSubMountsIncluded = $infoHasSubMountsIncluded; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Creates a Node of the same type that represents a non-existing path |
|
85 | + * |
|
86 | + * @param string $path path |
|
87 | + * @return Node non-existing node |
|
88 | + * @throws \Exception |
|
89 | + */ |
|
90 | + protected function createNonExistingNode($path) { |
|
91 | + throw new \Exception('Must be implemented by subclasses'); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Returns the matching file info |
|
96 | + * |
|
97 | + * @return FileInfo |
|
98 | + * @throws InvalidPathException |
|
99 | + * @throws NotFoundException |
|
100 | + */ |
|
101 | + public function getFileInfo(bool $includeMountPoint = true) { |
|
102 | + if (!$this->fileInfo) { |
|
103 | + if (!Filesystem::isValidPath($this->path)) { |
|
104 | + throw new InvalidPathException(); |
|
105 | + } |
|
106 | + $fileInfo = $this->view->getFileInfo($this->path, $includeMountPoint); |
|
107 | + $this->infoHasSubMountsIncluded = $includeMountPoint; |
|
108 | + if ($fileInfo instanceof FileInfo) { |
|
109 | + $this->fileInfo = $fileInfo; |
|
110 | + } else { |
|
111 | + throw new NotFoundException(); |
|
112 | + } |
|
113 | + } elseif ($includeMountPoint && !$this->infoHasSubMountsIncluded && $this instanceof Folder) { |
|
114 | + if ($this->fileInfo instanceof \OC\Files\FileInfo) { |
|
115 | + $this->view->addSubMounts($this->fileInfo); |
|
116 | + } |
|
117 | + $this->infoHasSubMountsIncluded = true; |
|
118 | + } |
|
119 | + return $this->fileInfo; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @param string[] $hooks |
|
124 | + */ |
|
125 | + protected function sendHooks($hooks, array $args = null) { |
|
126 | + $args = !empty($args) ? $args : [$this]; |
|
127 | + $dispatcher = \OC::$server->getEventDispatcher(); |
|
128 | + foreach ($hooks as $hook) { |
|
129 | + $this->root->emit('\OC\Files', $hook, $args); |
|
130 | + $dispatcher->dispatch('\OCP\Files::' . $hook, new GenericEvent($args)); |
|
131 | + } |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @param int $permissions |
|
136 | + * @return bool |
|
137 | + * @throws InvalidPathException |
|
138 | + * @throws NotFoundException |
|
139 | + */ |
|
140 | + protected function checkPermissions($permissions) { |
|
141 | + return ($this->getPermissions() & $permissions) === $permissions; |
|
142 | + } |
|
143 | + |
|
144 | + public function delete() { |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param int $mtime |
|
149 | + * @throws InvalidPathException |
|
150 | + * @throws NotFoundException |
|
151 | + * @throws NotPermittedException |
|
152 | + */ |
|
153 | + public function touch($mtime = null) { |
|
154 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) { |
|
155 | + $this->sendHooks(['preTouch']); |
|
156 | + $this->view->touch($this->path, $mtime); |
|
157 | + $this->sendHooks(['postTouch']); |
|
158 | + if ($this->fileInfo) { |
|
159 | + if (is_null($mtime)) { |
|
160 | + $mtime = time(); |
|
161 | + } |
|
162 | + $this->fileInfo['mtime'] = $mtime; |
|
163 | + } |
|
164 | + } else { |
|
165 | + throw new NotPermittedException(); |
|
166 | + } |
|
167 | + } |
|
168 | + |
|
169 | + public function getStorage() { |
|
170 | + $storage = $this->getMountPoint()->getStorage(); |
|
171 | + if (!$storage) { |
|
172 | + throw new \Exception("No storage for node"); |
|
173 | + } |
|
174 | + return $storage; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * @return string |
|
179 | + */ |
|
180 | + public function getPath() { |
|
181 | + return $this->path; |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * @return string |
|
186 | + */ |
|
187 | + public function getInternalPath() { |
|
188 | + return $this->getFileInfo(false)->getInternalPath(); |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * @return int |
|
193 | + * @throws InvalidPathException |
|
194 | + * @throws NotFoundException |
|
195 | + */ |
|
196 | + public function getId() { |
|
197 | + return $this->getFileInfo(false)->getId() ?? -1; |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * @return array |
|
202 | + */ |
|
203 | + public function stat() { |
|
204 | + return $this->view->stat($this->path); |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * @return int |
|
209 | + * @throws InvalidPathException |
|
210 | + * @throws NotFoundException |
|
211 | + */ |
|
212 | + public function getMTime() { |
|
213 | + return $this->getFileInfo()->getMTime(); |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * @param bool $includeMounts |
|
218 | + * @return int|float |
|
219 | + * @throws InvalidPathException |
|
220 | + * @throws NotFoundException |
|
221 | + */ |
|
222 | + public function getSize($includeMounts = true): int|float { |
|
223 | + return $this->getFileInfo()->getSize($includeMounts); |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * @return string |
|
228 | + * @throws InvalidPathException |
|
229 | + * @throws NotFoundException |
|
230 | + */ |
|
231 | + public function getEtag() { |
|
232 | + return $this->getFileInfo()->getEtag(); |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * @return int |
|
237 | + * @throws InvalidPathException |
|
238 | + * @throws NotFoundException |
|
239 | + */ |
|
240 | + public function getPermissions() { |
|
241 | + return $this->getFileInfo(false)->getPermissions(); |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * @return bool |
|
246 | + * @throws InvalidPathException |
|
247 | + * @throws NotFoundException |
|
248 | + */ |
|
249 | + public function isReadable() { |
|
250 | + return $this->getFileInfo(false)->isReadable(); |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * @return bool |
|
255 | + * @throws InvalidPathException |
|
256 | + * @throws NotFoundException |
|
257 | + */ |
|
258 | + public function isUpdateable() { |
|
259 | + return $this->getFileInfo(false)->isUpdateable(); |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * @return bool |
|
264 | + * @throws InvalidPathException |
|
265 | + * @throws NotFoundException |
|
266 | + */ |
|
267 | + public function isDeletable() { |
|
268 | + return $this->getFileInfo(false)->isDeletable(); |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * @return bool |
|
273 | + * @throws InvalidPathException |
|
274 | + * @throws NotFoundException |
|
275 | + */ |
|
276 | + public function isShareable() { |
|
277 | + return $this->getFileInfo(false)->isShareable(); |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * @return bool |
|
282 | + * @throws InvalidPathException |
|
283 | + * @throws NotFoundException |
|
284 | + */ |
|
285 | + public function isCreatable() { |
|
286 | + return $this->getFileInfo(false)->isCreatable(); |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * @return Node |
|
291 | + */ |
|
292 | + public function getParent() { |
|
293 | + if ($this->parent === null) { |
|
294 | + $newPath = dirname($this->path); |
|
295 | + if ($newPath === '' || $newPath === '.' || $newPath === '/') { |
|
296 | + return $this->root; |
|
297 | + } |
|
298 | + |
|
299 | + $this->parent = $this->root->get($newPath); |
|
300 | + } |
|
301 | + |
|
302 | + return $this->parent; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * @return string |
|
307 | + */ |
|
308 | + public function getName() { |
|
309 | + return basename($this->path); |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * @param string $path |
|
314 | + * @return string |
|
315 | + */ |
|
316 | + protected function normalizePath($path) { |
|
317 | + return PathHelper::normalizePath($path); |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * check if the requested path is valid |
|
322 | + * |
|
323 | + * @param string $path |
|
324 | + * @return bool |
|
325 | + */ |
|
326 | + public function isValidPath($path) { |
|
327 | + if (!$path || $path[0] !== '/') { |
|
328 | + $path = '/' . $path; |
|
329 | + } |
|
330 | + if (strstr($path, '/../') || strrchr($path, '/') === '/..') { |
|
331 | + return false; |
|
332 | + } |
|
333 | + return true; |
|
334 | + } |
|
335 | + |
|
336 | + public function isMounted() { |
|
337 | + return $this->getFileInfo(false)->isMounted(); |
|
338 | + } |
|
339 | + |
|
340 | + public function isShared() { |
|
341 | + return $this->getFileInfo(false)->isShared(); |
|
342 | + } |
|
343 | + |
|
344 | + public function getMimeType() { |
|
345 | + return $this->getFileInfo(false)->getMimetype(); |
|
346 | + } |
|
347 | + |
|
348 | + public function getMimePart() { |
|
349 | + return $this->getFileInfo(false)->getMimePart(); |
|
350 | + } |
|
351 | + |
|
352 | + public function getType() { |
|
353 | + return $this->getFileInfo(false)->getType(); |
|
354 | + } |
|
355 | + |
|
356 | + public function isEncrypted() { |
|
357 | + return $this->getFileInfo(false)->isEncrypted(); |
|
358 | + } |
|
359 | + |
|
360 | + public function getMountPoint() { |
|
361 | + return $this->getFileInfo(false)->getMountPoint(); |
|
362 | + } |
|
363 | + |
|
364 | + public function getOwner() { |
|
365 | + return $this->getFileInfo(false)->getOwner(); |
|
366 | + } |
|
367 | + |
|
368 | + public function getChecksum() { |
|
369 | + } |
|
370 | + |
|
371 | + public function getExtension(): string { |
|
372 | + return $this->getFileInfo(false)->getExtension(); |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
377 | + * @throws LockedException |
|
378 | + */ |
|
379 | + public function lock($type) { |
|
380 | + $this->view->lockFile($this->path, $type); |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
385 | + * @throws LockedException |
|
386 | + */ |
|
387 | + public function changeLock($type) { |
|
388 | + $this->view->changeLock($this->path, $type); |
|
389 | + } |
|
390 | + |
|
391 | + /** |
|
392 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
393 | + * @throws LockedException |
|
394 | + */ |
|
395 | + public function unlock($type) { |
|
396 | + $this->view->unlockFile($this->path, $type); |
|
397 | + } |
|
398 | + |
|
399 | + /** |
|
400 | + * @param string $targetPath |
|
401 | + * @return \OC\Files\Node\Node |
|
402 | + * @throws InvalidPathException |
|
403 | + * @throws NotFoundException |
|
404 | + * @throws NotPermittedException if copy not allowed or failed |
|
405 | + */ |
|
406 | + public function copy($targetPath) { |
|
407 | + $targetPath = $this->normalizePath($targetPath); |
|
408 | + $parent = $this->root->get(dirname($targetPath)); |
|
409 | + if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) { |
|
410 | + $nonExisting = $this->createNonExistingNode($targetPath); |
|
411 | + $this->sendHooks(['preCopy'], [$this, $nonExisting]); |
|
412 | + $this->sendHooks(['preWrite'], [$nonExisting]); |
|
413 | + if (!$this->view->copy($this->path, $targetPath)) { |
|
414 | + throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath); |
|
415 | + } |
|
416 | + $targetNode = $this->root->get($targetPath); |
|
417 | + $this->sendHooks(['postCopy'], [$this, $targetNode]); |
|
418 | + $this->sendHooks(['postWrite'], [$targetNode]); |
|
419 | + return $targetNode; |
|
420 | + } else { |
|
421 | + throw new NotPermittedException('No permission to copy to path ' . $targetPath); |
|
422 | + } |
|
423 | + } |
|
424 | + |
|
425 | + /** |
|
426 | + * @param string $targetPath |
|
427 | + * @return \OC\Files\Node\Node |
|
428 | + * @throws InvalidPathException |
|
429 | + * @throws NotFoundException |
|
430 | + * @throws NotPermittedException if move not allowed or failed |
|
431 | + * @throws LockedException |
|
432 | + */ |
|
433 | + public function move($targetPath) { |
|
434 | + $targetPath = $this->normalizePath($targetPath); |
|
435 | + $parent = $this->root->get(dirname($targetPath)); |
|
436 | + if ( |
|
437 | + $parent instanceof Folder and |
|
438 | + $this->isValidPath($targetPath) and |
|
439 | + ( |
|
440 | + $parent->isCreatable() || |
|
441 | + ($parent->getInternalPath() === '' && $parent->getMountPoint() instanceof MoveableMount) |
|
442 | + ) |
|
443 | + ) { |
|
444 | + $nonExisting = $this->createNonExistingNode($targetPath); |
|
445 | + $this->sendHooks(['preRename'], [$this, $nonExisting]); |
|
446 | + $this->sendHooks(['preWrite'], [$nonExisting]); |
|
447 | + if (!$this->view->rename($this->path, $targetPath)) { |
|
448 | + throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath); |
|
449 | + } |
|
450 | + |
|
451 | + $mountPoint = $this->getMountPoint(); |
|
452 | + if ($mountPoint) { |
|
453 | + // update the cached fileinfo with the new (internal) path |
|
454 | + /** @var \OC\Files\FileInfo $oldFileInfo */ |
|
455 | + $oldFileInfo = $this->getFileInfo(); |
|
456 | + $this->fileInfo = new \OC\Files\FileInfo($targetPath, $oldFileInfo->getStorage(), $mountPoint->getInternalPath($targetPath), $oldFileInfo->getData(), $mountPoint, $oldFileInfo->getOwner()); |
|
457 | + } |
|
458 | + |
|
459 | + $targetNode = $this->root->get($targetPath); |
|
460 | + $this->sendHooks(['postRename'], [$this, $targetNode]); |
|
461 | + $this->sendHooks(['postWrite'], [$targetNode]); |
|
462 | + $this->path = $targetPath; |
|
463 | + return $targetNode; |
|
464 | + } else { |
|
465 | + throw new NotPermittedException('No permission to move to path ' . $targetPath); |
|
466 | + } |
|
467 | + } |
|
468 | + |
|
469 | + public function getCreationTime(): int { |
|
470 | + return $this->getFileInfo()->getCreationTime(); |
|
471 | + } |
|
472 | + |
|
473 | + public function getUploadTime(): int { |
|
474 | + return $this->getFileInfo()->getUploadTime(); |
|
475 | + } |
|
476 | 476 | } |
@@ -86,2132 +86,2132 @@ |
||
86 | 86 | * \OC\Files\Storage\Storage object |
87 | 87 | */ |
88 | 88 | class View { |
89 | - /** @var string */ |
|
90 | - private $fakeRoot = ''; |
|
91 | - |
|
92 | - /** |
|
93 | - * @var \OCP\Lock\ILockingProvider |
|
94 | - */ |
|
95 | - protected $lockingProvider; |
|
96 | - |
|
97 | - private $lockingEnabled; |
|
98 | - |
|
99 | - private $updaterEnabled = true; |
|
100 | - |
|
101 | - /** @var \OC\User\Manager */ |
|
102 | - private $userManager; |
|
103 | - |
|
104 | - private LoggerInterface $logger; |
|
105 | - |
|
106 | - /** |
|
107 | - * @param string $root |
|
108 | - * @throws \Exception If $root contains an invalid path |
|
109 | - */ |
|
110 | - public function __construct($root = '') { |
|
111 | - if (is_null($root)) { |
|
112 | - throw new \InvalidArgumentException('Root can\'t be null'); |
|
113 | - } |
|
114 | - if (!Filesystem::isValidPath($root)) { |
|
115 | - throw new \Exception(); |
|
116 | - } |
|
117 | - |
|
118 | - $this->fakeRoot = $root; |
|
119 | - $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
120 | - $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
121 | - $this->userManager = \OC::$server->getUserManager(); |
|
122 | - $this->logger = \OC::$server->get(LoggerInterface::class); |
|
123 | - } |
|
124 | - |
|
125 | - public function getAbsolutePath($path = '/') { |
|
126 | - if ($path === null) { |
|
127 | - return null; |
|
128 | - } |
|
129 | - $this->assertPathLength($path); |
|
130 | - if ($path === '') { |
|
131 | - $path = '/'; |
|
132 | - } |
|
133 | - if ($path[0] !== '/') { |
|
134 | - $path = '/' . $path; |
|
135 | - } |
|
136 | - return $this->fakeRoot . $path; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * change the root to a fake root |
|
141 | - * |
|
142 | - * @param string $fakeRoot |
|
143 | - * @return boolean|null |
|
144 | - */ |
|
145 | - public function chroot($fakeRoot) { |
|
146 | - if (!$fakeRoot == '') { |
|
147 | - if ($fakeRoot[0] !== '/') { |
|
148 | - $fakeRoot = '/' . $fakeRoot; |
|
149 | - } |
|
150 | - } |
|
151 | - $this->fakeRoot = $fakeRoot; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * get the fake root |
|
156 | - * |
|
157 | - * @return string |
|
158 | - */ |
|
159 | - public function getRoot() { |
|
160 | - return $this->fakeRoot; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * get path relative to the root of the view |
|
165 | - * |
|
166 | - * @param string $path |
|
167 | - * @return string |
|
168 | - */ |
|
169 | - public function getRelativePath($path) { |
|
170 | - $this->assertPathLength($path); |
|
171 | - if ($this->fakeRoot == '') { |
|
172 | - return $path; |
|
173 | - } |
|
174 | - |
|
175 | - if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
176 | - return '/'; |
|
177 | - } |
|
178 | - |
|
179 | - // missing slashes can cause wrong matches! |
|
180 | - $root = rtrim($this->fakeRoot, '/') . '/'; |
|
181 | - |
|
182 | - if (strpos($path, $root) !== 0) { |
|
183 | - return null; |
|
184 | - } else { |
|
185 | - $path = substr($path, strlen($this->fakeRoot)); |
|
186 | - if (strlen($path) === 0) { |
|
187 | - return '/'; |
|
188 | - } else { |
|
189 | - return $path; |
|
190 | - } |
|
191 | - } |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * get the mountpoint of the storage object for a path |
|
196 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
197 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
198 | - * and does not take the chroot into account ) |
|
199 | - * |
|
200 | - * @param string $path |
|
201 | - * @return string |
|
202 | - */ |
|
203 | - public function getMountPoint($path) { |
|
204 | - return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * get the mountpoint of the storage object for a path |
|
209 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
210 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
211 | - * and does not take the chroot into account ) |
|
212 | - * |
|
213 | - * @param string $path |
|
214 | - * @return \OCP\Files\Mount\IMountPoint |
|
215 | - */ |
|
216 | - public function getMount($path) { |
|
217 | - return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * resolve a path to a storage and internal path |
|
222 | - * |
|
223 | - * @param string $path |
|
224 | - * @return array an array consisting of the storage and the internal path |
|
225 | - */ |
|
226 | - public function resolvePath($path) { |
|
227 | - $a = $this->getAbsolutePath($path); |
|
228 | - $p = Filesystem::normalizePath($a); |
|
229 | - return Filesystem::resolvePath($p); |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * return the path to a local version of the file |
|
234 | - * we need this because we can't know if a file is stored local or not from |
|
235 | - * outside the filestorage and for some purposes a local file is needed |
|
236 | - * |
|
237 | - * @param string $path |
|
238 | - * @return string |
|
239 | - */ |
|
240 | - public function getLocalFile($path) { |
|
241 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
242 | - $path = $this->getAbsolutePath($path); |
|
243 | - [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
244 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
245 | - return $storage->getLocalFile($internalPath); |
|
246 | - } else { |
|
247 | - return null; |
|
248 | - } |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * @param string $path |
|
253 | - * @return string |
|
254 | - */ |
|
255 | - public function getLocalFolder($path) { |
|
256 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
257 | - $path = $this->getAbsolutePath($path); |
|
258 | - [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
259 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
260 | - return $storage->getLocalFolder($internalPath); |
|
261 | - } else { |
|
262 | - return null; |
|
263 | - } |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * the following functions operate with arguments and return values identical |
|
268 | - * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
269 | - * for \OC\Files\Storage\Storage via basicOperation(). |
|
270 | - */ |
|
271 | - public function mkdir($path) { |
|
272 | - return $this->basicOperation('mkdir', $path, ['create', 'write']); |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * remove mount point |
|
277 | - * |
|
278 | - * @param IMountPoint $mount |
|
279 | - * @param string $path relative to data/ |
|
280 | - * @return boolean |
|
281 | - */ |
|
282 | - protected function removeMount($mount, $path) { |
|
283 | - if ($mount instanceof MoveableMount) { |
|
284 | - // cut of /user/files to get the relative path to data/user/files |
|
285 | - $pathParts = explode('/', $path, 4); |
|
286 | - $relPath = '/' . $pathParts[3]; |
|
287 | - $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
288 | - \OC_Hook::emit( |
|
289 | - Filesystem::CLASSNAME, "umount", |
|
290 | - [Filesystem::signal_param_path => $relPath] |
|
291 | - ); |
|
292 | - $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
293 | - $result = $mount->removeMount(); |
|
294 | - $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
295 | - if ($result) { |
|
296 | - \OC_Hook::emit( |
|
297 | - Filesystem::CLASSNAME, "post_umount", |
|
298 | - [Filesystem::signal_param_path => $relPath] |
|
299 | - ); |
|
300 | - } |
|
301 | - $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
302 | - return $result; |
|
303 | - } else { |
|
304 | - // do not allow deleting the storage's root / the mount point |
|
305 | - // because for some storages it might delete the whole contents |
|
306 | - // but isn't supposed to work that way |
|
307 | - return false; |
|
308 | - } |
|
309 | - } |
|
310 | - |
|
311 | - public function disableCacheUpdate() { |
|
312 | - $this->updaterEnabled = false; |
|
313 | - } |
|
314 | - |
|
315 | - public function enableCacheUpdate() { |
|
316 | - $this->updaterEnabled = true; |
|
317 | - } |
|
318 | - |
|
319 | - protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
320 | - if ($this->updaterEnabled) { |
|
321 | - if (is_null($time)) { |
|
322 | - $time = time(); |
|
323 | - } |
|
324 | - $storage->getUpdater()->update($internalPath, $time); |
|
325 | - } |
|
326 | - } |
|
327 | - |
|
328 | - protected function removeUpdate(Storage $storage, $internalPath) { |
|
329 | - if ($this->updaterEnabled) { |
|
330 | - $storage->getUpdater()->remove($internalPath); |
|
331 | - } |
|
332 | - } |
|
333 | - |
|
334 | - protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
335 | - if ($this->updaterEnabled) { |
|
336 | - $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
337 | - } |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * @param string $path |
|
342 | - * @return bool|mixed |
|
343 | - */ |
|
344 | - public function rmdir($path) { |
|
345 | - $absolutePath = $this->getAbsolutePath($path); |
|
346 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
347 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
348 | - return $this->removeMount($mount, $absolutePath); |
|
349 | - } |
|
350 | - if ($this->is_dir($path)) { |
|
351 | - $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
352 | - } else { |
|
353 | - $result = false; |
|
354 | - } |
|
355 | - |
|
356 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
357 | - $storage = $mount->getStorage(); |
|
358 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
359 | - $storage->getUpdater()->remove($internalPath); |
|
360 | - } |
|
361 | - return $result; |
|
362 | - } |
|
363 | - |
|
364 | - /** |
|
365 | - * @param string $path |
|
366 | - * @return resource |
|
367 | - */ |
|
368 | - public function opendir($path) { |
|
369 | - return $this->basicOperation('opendir', $path, ['read']); |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * @param string $path |
|
374 | - * @return bool|mixed |
|
375 | - */ |
|
376 | - public function is_dir($path) { |
|
377 | - if ($path == '/') { |
|
378 | - return true; |
|
379 | - } |
|
380 | - return $this->basicOperation('is_dir', $path); |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * @param string $path |
|
385 | - * @return bool|mixed |
|
386 | - */ |
|
387 | - public function is_file($path) { |
|
388 | - if ($path == '/') { |
|
389 | - return false; |
|
390 | - } |
|
391 | - return $this->basicOperation('is_file', $path); |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * @param string $path |
|
396 | - * @return mixed |
|
397 | - */ |
|
398 | - public function stat($path) { |
|
399 | - return $this->basicOperation('stat', $path); |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * @param string $path |
|
404 | - * @return mixed |
|
405 | - */ |
|
406 | - public function filetype($path) { |
|
407 | - return $this->basicOperation('filetype', $path); |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * @param string $path |
|
412 | - * @return mixed |
|
413 | - */ |
|
414 | - public function filesize(string $path) { |
|
415 | - return $this->basicOperation('filesize', $path); |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * @param string $path |
|
420 | - * @return bool|mixed |
|
421 | - * @throws \OCP\Files\InvalidPathException |
|
422 | - */ |
|
423 | - public function readfile($path) { |
|
424 | - $this->assertPathLength($path); |
|
425 | - if (ob_get_level()) { |
|
426 | - ob_end_clean(); |
|
427 | - } |
|
428 | - $handle = $this->fopen($path, 'rb'); |
|
429 | - if ($handle) { |
|
430 | - $chunkSize = 524288; // 512 kB chunks |
|
431 | - while (!feof($handle)) { |
|
432 | - echo fread($handle, $chunkSize); |
|
433 | - flush(); |
|
434 | - } |
|
435 | - fclose($handle); |
|
436 | - return $this->filesize($path); |
|
437 | - } |
|
438 | - return false; |
|
439 | - } |
|
440 | - |
|
441 | - /** |
|
442 | - * @param string $path |
|
443 | - * @param int $from |
|
444 | - * @param int $to |
|
445 | - * @return bool|mixed |
|
446 | - * @throws \OCP\Files\InvalidPathException |
|
447 | - * @throws \OCP\Files\UnseekableException |
|
448 | - */ |
|
449 | - public function readfilePart($path, $from, $to) { |
|
450 | - $this->assertPathLength($path); |
|
451 | - if (ob_get_level()) { |
|
452 | - ob_end_clean(); |
|
453 | - } |
|
454 | - $handle = $this->fopen($path, 'rb'); |
|
455 | - if ($handle) { |
|
456 | - $chunkSize = 524288; // 512 kB chunks |
|
457 | - $startReading = true; |
|
458 | - |
|
459 | - if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
460 | - // forward file handle via chunked fread because fseek seem to have failed |
|
461 | - |
|
462 | - $end = $from + 1; |
|
463 | - while (!feof($handle) && ftell($handle) < $end && ftell($handle) !== $from) { |
|
464 | - $len = $from - ftell($handle); |
|
465 | - if ($len > $chunkSize) { |
|
466 | - $len = $chunkSize; |
|
467 | - } |
|
468 | - $result = fread($handle, $len); |
|
469 | - |
|
470 | - if ($result === false) { |
|
471 | - $startReading = false; |
|
472 | - break; |
|
473 | - } |
|
474 | - } |
|
475 | - } |
|
476 | - |
|
477 | - if ($startReading) { |
|
478 | - $end = $to + 1; |
|
479 | - while (!feof($handle) && ftell($handle) < $end) { |
|
480 | - $len = $end - ftell($handle); |
|
481 | - if ($len > $chunkSize) { |
|
482 | - $len = $chunkSize; |
|
483 | - } |
|
484 | - echo fread($handle, $len); |
|
485 | - flush(); |
|
486 | - } |
|
487 | - return ftell($handle) - $from; |
|
488 | - } |
|
489 | - |
|
490 | - throw new \OCP\Files\UnseekableException('fseek error'); |
|
491 | - } |
|
492 | - return false; |
|
493 | - } |
|
494 | - |
|
495 | - /** |
|
496 | - * @param string $path |
|
497 | - * @return mixed |
|
498 | - */ |
|
499 | - public function isCreatable($path) { |
|
500 | - return $this->basicOperation('isCreatable', $path); |
|
501 | - } |
|
502 | - |
|
503 | - /** |
|
504 | - * @param string $path |
|
505 | - * @return mixed |
|
506 | - */ |
|
507 | - public function isReadable($path) { |
|
508 | - return $this->basicOperation('isReadable', $path); |
|
509 | - } |
|
510 | - |
|
511 | - /** |
|
512 | - * @param string $path |
|
513 | - * @return mixed |
|
514 | - */ |
|
515 | - public function isUpdatable($path) { |
|
516 | - return $this->basicOperation('isUpdatable', $path); |
|
517 | - } |
|
518 | - |
|
519 | - /** |
|
520 | - * @param string $path |
|
521 | - * @return bool|mixed |
|
522 | - */ |
|
523 | - public function isDeletable($path) { |
|
524 | - $absolutePath = $this->getAbsolutePath($path); |
|
525 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
526 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
527 | - return $mount instanceof MoveableMount; |
|
528 | - } |
|
529 | - return $this->basicOperation('isDeletable', $path); |
|
530 | - } |
|
531 | - |
|
532 | - /** |
|
533 | - * @param string $path |
|
534 | - * @return mixed |
|
535 | - */ |
|
536 | - public function isSharable($path) { |
|
537 | - return $this->basicOperation('isSharable', $path); |
|
538 | - } |
|
539 | - |
|
540 | - /** |
|
541 | - * @param string $path |
|
542 | - * @return bool|mixed |
|
543 | - */ |
|
544 | - public function file_exists($path) { |
|
545 | - if ($path == '/') { |
|
546 | - return true; |
|
547 | - } |
|
548 | - return $this->basicOperation('file_exists', $path); |
|
549 | - } |
|
550 | - |
|
551 | - /** |
|
552 | - * @param string $path |
|
553 | - * @return mixed |
|
554 | - */ |
|
555 | - public function filemtime($path) { |
|
556 | - return $this->basicOperation('filemtime', $path); |
|
557 | - } |
|
558 | - |
|
559 | - /** |
|
560 | - * @param string $path |
|
561 | - * @param int|string $mtime |
|
562 | - * @return bool |
|
563 | - */ |
|
564 | - public function touch($path, $mtime = null) { |
|
565 | - if (!is_null($mtime) and !is_numeric($mtime)) { |
|
566 | - $mtime = strtotime($mtime); |
|
567 | - } |
|
568 | - |
|
569 | - $hooks = ['touch']; |
|
570 | - |
|
571 | - if (!$this->file_exists($path)) { |
|
572 | - $hooks[] = 'create'; |
|
573 | - $hooks[] = 'write'; |
|
574 | - } |
|
575 | - try { |
|
576 | - $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
577 | - } catch (\Exception $e) { |
|
578 | - $this->logger->info('Error while setting modified time', ['app' => 'core', 'exception' => $e]); |
|
579 | - $result = false; |
|
580 | - } |
|
581 | - if (!$result) { |
|
582 | - // If create file fails because of permissions on external storage like SMB folders, |
|
583 | - // check file exists and return false if not. |
|
584 | - if (!$this->file_exists($path)) { |
|
585 | - return false; |
|
586 | - } |
|
587 | - if (is_null($mtime)) { |
|
588 | - $mtime = time(); |
|
589 | - } |
|
590 | - //if native touch fails, we emulate it by changing the mtime in the cache |
|
591 | - $this->putFileInfo($path, ['mtime' => floor($mtime)]); |
|
592 | - } |
|
593 | - return true; |
|
594 | - } |
|
595 | - |
|
596 | - /** |
|
597 | - * @param string $path |
|
598 | - * @return mixed |
|
599 | - * @throws LockedException |
|
600 | - */ |
|
601 | - public function file_get_contents($path) { |
|
602 | - return $this->basicOperation('file_get_contents', $path, ['read']); |
|
603 | - } |
|
604 | - |
|
605 | - /** |
|
606 | - * @param bool $exists |
|
607 | - * @param string $path |
|
608 | - * @param bool $run |
|
609 | - */ |
|
610 | - protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
611 | - if (!$exists) { |
|
612 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, [ |
|
613 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
614 | - Filesystem::signal_param_run => &$run, |
|
615 | - ]); |
|
616 | - } else { |
|
617 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, [ |
|
618 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
619 | - Filesystem::signal_param_run => &$run, |
|
620 | - ]); |
|
621 | - } |
|
622 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, [ |
|
623 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
624 | - Filesystem::signal_param_run => &$run, |
|
625 | - ]); |
|
626 | - } |
|
627 | - |
|
628 | - /** |
|
629 | - * @param bool $exists |
|
630 | - * @param string $path |
|
631 | - */ |
|
632 | - protected function emit_file_hooks_post($exists, $path) { |
|
633 | - if (!$exists) { |
|
634 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, [ |
|
635 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
636 | - ]); |
|
637 | - } else { |
|
638 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, [ |
|
639 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
640 | - ]); |
|
641 | - } |
|
642 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, [ |
|
643 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
644 | - ]); |
|
645 | - } |
|
646 | - |
|
647 | - /** |
|
648 | - * @param string $path |
|
649 | - * @param string|resource $data |
|
650 | - * @return bool|mixed |
|
651 | - * @throws LockedException |
|
652 | - */ |
|
653 | - public function file_put_contents($path, $data) { |
|
654 | - if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
655 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
656 | - if (Filesystem::isValidPath($path) |
|
657 | - and !Filesystem::isFileBlacklisted($path) |
|
658 | - ) { |
|
659 | - $path = $this->getRelativePath($absolutePath); |
|
660 | - |
|
661 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
662 | - |
|
663 | - $exists = $this->file_exists($path); |
|
664 | - $run = true; |
|
665 | - if ($this->shouldEmitHooks($path)) { |
|
666 | - $this->emit_file_hooks_pre($exists, $path, $run); |
|
667 | - } |
|
668 | - if (!$run) { |
|
669 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
670 | - return false; |
|
671 | - } |
|
672 | - |
|
673 | - try { |
|
674 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
675 | - } catch (\Exception $e) { |
|
676 | - // Release the shared lock before throwing. |
|
677 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
678 | - throw $e; |
|
679 | - } |
|
680 | - |
|
681 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
682 | - [$storage, $internalPath] = $this->resolvePath($path); |
|
683 | - $target = $storage->fopen($internalPath, 'w'); |
|
684 | - if ($target) { |
|
685 | - [, $result] = \OC_Helper::streamCopy($data, $target); |
|
686 | - fclose($target); |
|
687 | - fclose($data); |
|
688 | - |
|
689 | - $this->writeUpdate($storage, $internalPath); |
|
690 | - |
|
691 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
692 | - |
|
693 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
694 | - $this->emit_file_hooks_post($exists, $path); |
|
695 | - } |
|
696 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
697 | - return $result; |
|
698 | - } else { |
|
699 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
700 | - return false; |
|
701 | - } |
|
702 | - } else { |
|
703 | - return false; |
|
704 | - } |
|
705 | - } else { |
|
706 | - $hooks = $this->file_exists($path) ? ['update', 'write'] : ['create', 'write']; |
|
707 | - return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
708 | - } |
|
709 | - } |
|
710 | - |
|
711 | - /** |
|
712 | - * @param string $path |
|
713 | - * @return bool|mixed |
|
714 | - */ |
|
715 | - public function unlink($path) { |
|
716 | - if ($path === '' || $path === '/') { |
|
717 | - // do not allow deleting the root |
|
718 | - return false; |
|
719 | - } |
|
720 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
721 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
722 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
723 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
724 | - return $this->removeMount($mount, $absolutePath); |
|
725 | - } |
|
726 | - if ($this->is_dir($path)) { |
|
727 | - $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
728 | - } else { |
|
729 | - $result = $this->basicOperation('unlink', $path, ['delete']); |
|
730 | - } |
|
731 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
732 | - $storage = $mount->getStorage(); |
|
733 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
734 | - $storage->getUpdater()->remove($internalPath); |
|
735 | - return true; |
|
736 | - } else { |
|
737 | - return $result; |
|
738 | - } |
|
739 | - } |
|
740 | - |
|
741 | - /** |
|
742 | - * @param string $directory |
|
743 | - * @return bool|mixed |
|
744 | - */ |
|
745 | - public function deleteAll($directory) { |
|
746 | - return $this->rmdir($directory); |
|
747 | - } |
|
748 | - |
|
749 | - /** |
|
750 | - * Rename/move a file or folder from the source path to target path. |
|
751 | - * |
|
752 | - * @param string $source source path |
|
753 | - * @param string $target target path |
|
754 | - * |
|
755 | - * @return bool|mixed |
|
756 | - * @throws LockedException |
|
757 | - */ |
|
758 | - public function rename($source, $target) { |
|
759 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source)); |
|
760 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target)); |
|
761 | - $result = false; |
|
762 | - if ( |
|
763 | - Filesystem::isValidPath($target) |
|
764 | - and Filesystem::isValidPath($source) |
|
765 | - and !Filesystem::isFileBlacklisted($target) |
|
766 | - ) { |
|
767 | - $source = $this->getRelativePath($absolutePath1); |
|
768 | - $target = $this->getRelativePath($absolutePath2); |
|
769 | - $exists = $this->file_exists($target); |
|
770 | - |
|
771 | - if ($source == null or $target == null) { |
|
772 | - return false; |
|
773 | - } |
|
774 | - |
|
775 | - $this->lockFile($source, ILockingProvider::LOCK_SHARED, true); |
|
776 | - try { |
|
777 | - $this->lockFile($target, ILockingProvider::LOCK_SHARED, true); |
|
778 | - |
|
779 | - $run = true; |
|
780 | - if ($this->shouldEmitHooks($source) && (Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target))) { |
|
781 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
782 | - $this->emit_file_hooks_pre($exists, $target, $run); |
|
783 | - } elseif ($this->shouldEmitHooks($source)) { |
|
784 | - \OC_Hook::emit( |
|
785 | - Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
786 | - [ |
|
787 | - Filesystem::signal_param_oldpath => $this->getHookPath($source), |
|
788 | - Filesystem::signal_param_newpath => $this->getHookPath($target), |
|
789 | - Filesystem::signal_param_run => &$run |
|
790 | - ] |
|
791 | - ); |
|
792 | - } |
|
793 | - if ($run) { |
|
794 | - $this->verifyPath(dirname($target), basename($target)); |
|
795 | - |
|
796 | - $manager = Filesystem::getMountManager(); |
|
797 | - $mount1 = $this->getMount($source); |
|
798 | - $mount2 = $this->getMount($target); |
|
799 | - $storage1 = $mount1->getStorage(); |
|
800 | - $storage2 = $mount2->getStorage(); |
|
801 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
802 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
803 | - |
|
804 | - $this->changeLock($source, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
805 | - try { |
|
806 | - $this->changeLock($target, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
807 | - |
|
808 | - if ($internalPath1 === '') { |
|
809 | - if ($mount1 instanceof MoveableMount) { |
|
810 | - $sourceParentMount = $this->getMount(dirname($source)); |
|
811 | - if ($sourceParentMount === $mount2 && $this->targetIsNotShared($storage2, $internalPath2)) { |
|
812 | - /** |
|
813 | - * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
814 | - */ |
|
815 | - $sourceMountPoint = $mount1->getMountPoint(); |
|
816 | - $result = $mount1->moveMount($absolutePath2); |
|
817 | - $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
818 | - } else { |
|
819 | - $result = false; |
|
820 | - } |
|
821 | - } else { |
|
822 | - $result = false; |
|
823 | - } |
|
824 | - // moving a file/folder within the same mount point |
|
825 | - } elseif ($storage1 === $storage2) { |
|
826 | - if ($storage1) { |
|
827 | - $result = $storage1->rename($internalPath1, $internalPath2); |
|
828 | - } else { |
|
829 | - $result = false; |
|
830 | - } |
|
831 | - // moving a file/folder between storages (from $storage1 to $storage2) |
|
832 | - } else { |
|
833 | - $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
834 | - } |
|
835 | - |
|
836 | - if ((Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target)) && $result !== false) { |
|
837 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
838 | - $this->writeUpdate($storage2, $internalPath2); |
|
839 | - } elseif ($result) { |
|
840 | - if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
841 | - $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
842 | - } |
|
843 | - } |
|
844 | - } catch (\Exception $e) { |
|
845 | - throw $e; |
|
846 | - } finally { |
|
847 | - $this->changeLock($source, ILockingProvider::LOCK_SHARED, true); |
|
848 | - $this->changeLock($target, ILockingProvider::LOCK_SHARED, true); |
|
849 | - } |
|
850 | - |
|
851 | - if ((Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target)) && $result !== false) { |
|
852 | - if ($this->shouldEmitHooks()) { |
|
853 | - $this->emit_file_hooks_post($exists, $target); |
|
854 | - } |
|
855 | - } elseif ($result) { |
|
856 | - if ($this->shouldEmitHooks($source) and $this->shouldEmitHooks($target)) { |
|
857 | - \OC_Hook::emit( |
|
858 | - Filesystem::CLASSNAME, |
|
859 | - Filesystem::signal_post_rename, |
|
860 | - [ |
|
861 | - Filesystem::signal_param_oldpath => $this->getHookPath($source), |
|
862 | - Filesystem::signal_param_newpath => $this->getHookPath($target) |
|
863 | - ] |
|
864 | - ); |
|
865 | - } |
|
866 | - } |
|
867 | - } |
|
868 | - } catch (\Exception $e) { |
|
869 | - throw $e; |
|
870 | - } finally { |
|
871 | - $this->unlockFile($source, ILockingProvider::LOCK_SHARED, true); |
|
872 | - $this->unlockFile($target, ILockingProvider::LOCK_SHARED, true); |
|
873 | - } |
|
874 | - } |
|
875 | - return $result; |
|
876 | - } |
|
877 | - |
|
878 | - /** |
|
879 | - * Copy a file/folder from the source path to target path |
|
880 | - * |
|
881 | - * @param string $source source path |
|
882 | - * @param string $target target path |
|
883 | - * @param bool $preserveMtime whether to preserve mtime on the copy |
|
884 | - * |
|
885 | - * @return bool|mixed |
|
886 | - */ |
|
887 | - public function copy($source, $target, $preserveMtime = false) { |
|
888 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source)); |
|
889 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target)); |
|
890 | - $result = false; |
|
891 | - if ( |
|
892 | - Filesystem::isValidPath($target) |
|
893 | - and Filesystem::isValidPath($source) |
|
894 | - and !Filesystem::isFileBlacklisted($target) |
|
895 | - ) { |
|
896 | - $source = $this->getRelativePath($absolutePath1); |
|
897 | - $target = $this->getRelativePath($absolutePath2); |
|
898 | - |
|
899 | - if ($source == null or $target == null) { |
|
900 | - return false; |
|
901 | - } |
|
902 | - $run = true; |
|
903 | - |
|
904 | - $this->lockFile($target, ILockingProvider::LOCK_SHARED); |
|
905 | - $this->lockFile($source, ILockingProvider::LOCK_SHARED); |
|
906 | - $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
907 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
908 | - |
|
909 | - try { |
|
910 | - $exists = $this->file_exists($target); |
|
911 | - if ($this->shouldEmitHooks()) { |
|
912 | - \OC_Hook::emit( |
|
913 | - Filesystem::CLASSNAME, |
|
914 | - Filesystem::signal_copy, |
|
915 | - [ |
|
916 | - Filesystem::signal_param_oldpath => $this->getHookPath($source), |
|
917 | - Filesystem::signal_param_newpath => $this->getHookPath($target), |
|
918 | - Filesystem::signal_param_run => &$run |
|
919 | - ] |
|
920 | - ); |
|
921 | - $this->emit_file_hooks_pre($exists, $target, $run); |
|
922 | - } |
|
923 | - if ($run) { |
|
924 | - $mount1 = $this->getMount($source); |
|
925 | - $mount2 = $this->getMount($target); |
|
926 | - $storage1 = $mount1->getStorage(); |
|
927 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
928 | - $storage2 = $mount2->getStorage(); |
|
929 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
930 | - |
|
931 | - $this->changeLock($target, ILockingProvider::LOCK_EXCLUSIVE); |
|
932 | - $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
933 | - |
|
934 | - if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
935 | - if ($storage1) { |
|
936 | - $result = $storage1->copy($internalPath1, $internalPath2); |
|
937 | - } else { |
|
938 | - $result = false; |
|
939 | - } |
|
940 | - } else { |
|
941 | - $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
942 | - } |
|
943 | - |
|
944 | - $this->writeUpdate($storage2, $internalPath2); |
|
945 | - |
|
946 | - $this->changeLock($target, ILockingProvider::LOCK_SHARED); |
|
947 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
948 | - |
|
949 | - if ($this->shouldEmitHooks() && $result !== false) { |
|
950 | - \OC_Hook::emit( |
|
951 | - Filesystem::CLASSNAME, |
|
952 | - Filesystem::signal_post_copy, |
|
953 | - [ |
|
954 | - Filesystem::signal_param_oldpath => $this->getHookPath($source), |
|
955 | - Filesystem::signal_param_newpath => $this->getHookPath($target) |
|
956 | - ] |
|
957 | - ); |
|
958 | - $this->emit_file_hooks_post($exists, $target); |
|
959 | - } |
|
960 | - } |
|
961 | - } catch (\Exception $e) { |
|
962 | - $this->unlockFile($target, $lockTypePath2); |
|
963 | - $this->unlockFile($source, $lockTypePath1); |
|
964 | - throw $e; |
|
965 | - } |
|
966 | - |
|
967 | - $this->unlockFile($target, $lockTypePath2); |
|
968 | - $this->unlockFile($source, $lockTypePath1); |
|
969 | - } |
|
970 | - return $result; |
|
971 | - } |
|
972 | - |
|
973 | - /** |
|
974 | - * @param string $path |
|
975 | - * @param string $mode 'r' or 'w' |
|
976 | - * @return resource |
|
977 | - * @throws LockedException |
|
978 | - */ |
|
979 | - public function fopen($path, $mode) { |
|
980 | - $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
981 | - $hooks = []; |
|
982 | - switch ($mode) { |
|
983 | - case 'r': |
|
984 | - $hooks[] = 'read'; |
|
985 | - break; |
|
986 | - case 'r+': |
|
987 | - case 'w+': |
|
988 | - case 'x+': |
|
989 | - case 'a+': |
|
990 | - $hooks[] = 'read'; |
|
991 | - $hooks[] = 'write'; |
|
992 | - break; |
|
993 | - case 'w': |
|
994 | - case 'x': |
|
995 | - case 'a': |
|
996 | - $hooks[] = 'write'; |
|
997 | - break; |
|
998 | - default: |
|
999 | - $this->logger->error('invalid mode (' . $mode . ') for ' . $path, ['app' => 'core']); |
|
1000 | - } |
|
1001 | - |
|
1002 | - if ($mode !== 'r' && $mode !== 'w') { |
|
1003 | - $this->logger->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends', ['app' => 'core']); |
|
1004 | - } |
|
1005 | - |
|
1006 | - $handle = $this->basicOperation('fopen', $path, $hooks, $mode); |
|
1007 | - if (!is_resource($handle) && $mode === 'r') { |
|
1008 | - // trying to read a file that isn't on disk, check if the cache is out of sync and rescan if needed |
|
1009 | - $mount = $this->getMount($path); |
|
1010 | - $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
1011 | - $storage = $mount->getStorage(); |
|
1012 | - if ($storage->getCache()->inCache($internalPath) && !$storage->file_exists($path)) { |
|
1013 | - $this->writeUpdate($storage, $internalPath); |
|
1014 | - } |
|
1015 | - } |
|
1016 | - return $handle; |
|
1017 | - } |
|
1018 | - |
|
1019 | - /** |
|
1020 | - * @param string $path |
|
1021 | - * @return bool|string |
|
1022 | - * @throws \OCP\Files\InvalidPathException |
|
1023 | - */ |
|
1024 | - public function toTmpFile($path) { |
|
1025 | - $this->assertPathLength($path); |
|
1026 | - if (Filesystem::isValidPath($path)) { |
|
1027 | - $source = $this->fopen($path, 'r'); |
|
1028 | - if ($source) { |
|
1029 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
1030 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
1031 | - file_put_contents($tmpFile, $source); |
|
1032 | - return $tmpFile; |
|
1033 | - } else { |
|
1034 | - return false; |
|
1035 | - } |
|
1036 | - } else { |
|
1037 | - return false; |
|
1038 | - } |
|
1039 | - } |
|
1040 | - |
|
1041 | - /** |
|
1042 | - * @param string $tmpFile |
|
1043 | - * @param string $path |
|
1044 | - * @return bool|mixed |
|
1045 | - * @throws \OCP\Files\InvalidPathException |
|
1046 | - */ |
|
1047 | - public function fromTmpFile($tmpFile, $path) { |
|
1048 | - $this->assertPathLength($path); |
|
1049 | - if (Filesystem::isValidPath($path)) { |
|
1050 | - // Get directory that the file is going into |
|
1051 | - $filePath = dirname($path); |
|
1052 | - |
|
1053 | - // Create the directories if any |
|
1054 | - if (!$this->file_exists($filePath)) { |
|
1055 | - $result = $this->createParentDirectories($filePath); |
|
1056 | - if ($result === false) { |
|
1057 | - return false; |
|
1058 | - } |
|
1059 | - } |
|
1060 | - |
|
1061 | - $source = fopen($tmpFile, 'r'); |
|
1062 | - if ($source) { |
|
1063 | - $result = $this->file_put_contents($path, $source); |
|
1064 | - // $this->file_put_contents() might have already closed |
|
1065 | - // the resource, so we check it, before trying to close it |
|
1066 | - // to avoid messages in the error log. |
|
1067 | - if (is_resource($source)) { |
|
1068 | - fclose($source); |
|
1069 | - } |
|
1070 | - unlink($tmpFile); |
|
1071 | - return $result; |
|
1072 | - } else { |
|
1073 | - return false; |
|
1074 | - } |
|
1075 | - } else { |
|
1076 | - return false; |
|
1077 | - } |
|
1078 | - } |
|
1079 | - |
|
1080 | - |
|
1081 | - /** |
|
1082 | - * @param string $path |
|
1083 | - * @return mixed |
|
1084 | - * @throws \OCP\Files\InvalidPathException |
|
1085 | - */ |
|
1086 | - public function getMimeType($path) { |
|
1087 | - $this->assertPathLength($path); |
|
1088 | - return $this->basicOperation('getMimeType', $path); |
|
1089 | - } |
|
1090 | - |
|
1091 | - /** |
|
1092 | - * @param string $type |
|
1093 | - * @param string $path |
|
1094 | - * @param bool $raw |
|
1095 | - * @return bool|string |
|
1096 | - */ |
|
1097 | - public function hash($type, $path, $raw = false) { |
|
1098 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1099 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1100 | - if (Filesystem::isValidPath($path)) { |
|
1101 | - $path = $this->getRelativePath($absolutePath); |
|
1102 | - if ($path == null) { |
|
1103 | - return false; |
|
1104 | - } |
|
1105 | - if ($this->shouldEmitHooks($path)) { |
|
1106 | - \OC_Hook::emit( |
|
1107 | - Filesystem::CLASSNAME, |
|
1108 | - Filesystem::signal_read, |
|
1109 | - [Filesystem::signal_param_path => $this->getHookPath($path)] |
|
1110 | - ); |
|
1111 | - } |
|
1112 | - /** @var Storage|null $storage */ |
|
1113 | - [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
1114 | - if ($storage) { |
|
1115 | - return $storage->hash($type, $internalPath, $raw); |
|
1116 | - } |
|
1117 | - } |
|
1118 | - return false; |
|
1119 | - } |
|
1120 | - |
|
1121 | - /** |
|
1122 | - * @param string $path |
|
1123 | - * @return mixed |
|
1124 | - * @throws \OCP\Files\InvalidPathException |
|
1125 | - */ |
|
1126 | - public function free_space($path = '/') { |
|
1127 | - $this->assertPathLength($path); |
|
1128 | - $result = $this->basicOperation('free_space', $path); |
|
1129 | - if ($result === null) { |
|
1130 | - throw new InvalidPathException(); |
|
1131 | - } |
|
1132 | - return $result; |
|
1133 | - } |
|
1134 | - |
|
1135 | - /** |
|
1136 | - * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
1137 | - * |
|
1138 | - * @param string $operation |
|
1139 | - * @param string $path |
|
1140 | - * @param array $hooks (optional) |
|
1141 | - * @param mixed $extraParam (optional) |
|
1142 | - * @return mixed |
|
1143 | - * @throws LockedException |
|
1144 | - * |
|
1145 | - * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
1146 | - * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
1147 | - * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
1148 | - */ |
|
1149 | - private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
1150 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1151 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1152 | - if (Filesystem::isValidPath($path) |
|
1153 | - and !Filesystem::isFileBlacklisted($path) |
|
1154 | - ) { |
|
1155 | - $path = $this->getRelativePath($absolutePath); |
|
1156 | - if ($path == null) { |
|
1157 | - return false; |
|
1158 | - } |
|
1159 | - |
|
1160 | - if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
1161 | - // always a shared lock during pre-hooks so the hook can read the file |
|
1162 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
1163 | - } |
|
1164 | - |
|
1165 | - $run = $this->runHooks($hooks, $path); |
|
1166 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
1167 | - [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
1168 | - if ($run and $storage) { |
|
1169 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1170 | - try { |
|
1171 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1172 | - } catch (LockedException $e) { |
|
1173 | - // release the shared lock we acquired before quitting |
|
1174 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1175 | - throw $e; |
|
1176 | - } |
|
1177 | - } |
|
1178 | - try { |
|
1179 | - if (!is_null($extraParam)) { |
|
1180 | - $result = $storage->$operation($internalPath, $extraParam); |
|
1181 | - } else { |
|
1182 | - $result = $storage->$operation($internalPath); |
|
1183 | - } |
|
1184 | - } catch (\Exception $e) { |
|
1185 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1186 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1187 | - } elseif (in_array('read', $hooks)) { |
|
1188 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1189 | - } |
|
1190 | - throw $e; |
|
1191 | - } |
|
1192 | - |
|
1193 | - if ($result !== false && in_array('delete', $hooks)) { |
|
1194 | - $this->removeUpdate($storage, $internalPath); |
|
1195 | - } |
|
1196 | - if ($result !== false && in_array('write', $hooks, true) && $operation !== 'fopen' && $operation !== 'touch') { |
|
1197 | - $this->writeUpdate($storage, $internalPath); |
|
1198 | - } |
|
1199 | - if ($result !== false && in_array('touch', $hooks)) { |
|
1200 | - $this->writeUpdate($storage, $internalPath, $extraParam); |
|
1201 | - } |
|
1202 | - |
|
1203 | - if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
1204 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
1205 | - } |
|
1206 | - |
|
1207 | - $unlockLater = false; |
|
1208 | - if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
1209 | - $unlockLater = true; |
|
1210 | - // make sure our unlocking callback will still be called if connection is aborted |
|
1211 | - ignore_user_abort(true); |
|
1212 | - $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
1213 | - if (in_array('write', $hooks)) { |
|
1214 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1215 | - } elseif (in_array('read', $hooks)) { |
|
1216 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1217 | - } |
|
1218 | - }); |
|
1219 | - } |
|
1220 | - |
|
1221 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
1222 | - if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
1223 | - $this->runHooks($hooks, $path, true); |
|
1224 | - } |
|
1225 | - } |
|
1226 | - |
|
1227 | - if (!$unlockLater |
|
1228 | - && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
1229 | - ) { |
|
1230 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1231 | - } |
|
1232 | - return $result; |
|
1233 | - } else { |
|
1234 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1235 | - } |
|
1236 | - } |
|
1237 | - return null; |
|
1238 | - } |
|
1239 | - |
|
1240 | - /** |
|
1241 | - * get the path relative to the default root for hook usage |
|
1242 | - * |
|
1243 | - * @param string $path |
|
1244 | - * @return string |
|
1245 | - */ |
|
1246 | - private function getHookPath($path) { |
|
1247 | - if (!Filesystem::getView()) { |
|
1248 | - return $path; |
|
1249 | - } |
|
1250 | - return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
1251 | - } |
|
1252 | - |
|
1253 | - private function shouldEmitHooks($path = '') { |
|
1254 | - if ($path && Cache\Scanner::isPartialFile($path)) { |
|
1255 | - return false; |
|
1256 | - } |
|
1257 | - if (!Filesystem::$loaded) { |
|
1258 | - return false; |
|
1259 | - } |
|
1260 | - $defaultRoot = Filesystem::getRoot(); |
|
1261 | - if ($defaultRoot === null) { |
|
1262 | - return false; |
|
1263 | - } |
|
1264 | - if ($this->fakeRoot === $defaultRoot) { |
|
1265 | - return true; |
|
1266 | - } |
|
1267 | - $fullPath = $this->getAbsolutePath($path); |
|
1268 | - |
|
1269 | - if ($fullPath === $defaultRoot) { |
|
1270 | - return true; |
|
1271 | - } |
|
1272 | - |
|
1273 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
1274 | - } |
|
1275 | - |
|
1276 | - /** |
|
1277 | - * @param string[] $hooks |
|
1278 | - * @param string $path |
|
1279 | - * @param bool $post |
|
1280 | - * @return bool |
|
1281 | - */ |
|
1282 | - private function runHooks($hooks, $path, $post = false) { |
|
1283 | - $relativePath = $path; |
|
1284 | - $path = $this->getHookPath($path); |
|
1285 | - $prefix = $post ? 'post_' : ''; |
|
1286 | - $run = true; |
|
1287 | - if ($this->shouldEmitHooks($relativePath)) { |
|
1288 | - foreach ($hooks as $hook) { |
|
1289 | - if ($hook != 'read') { |
|
1290 | - \OC_Hook::emit( |
|
1291 | - Filesystem::CLASSNAME, |
|
1292 | - $prefix . $hook, |
|
1293 | - [ |
|
1294 | - Filesystem::signal_param_run => &$run, |
|
1295 | - Filesystem::signal_param_path => $path |
|
1296 | - ] |
|
1297 | - ); |
|
1298 | - } elseif (!$post) { |
|
1299 | - \OC_Hook::emit( |
|
1300 | - Filesystem::CLASSNAME, |
|
1301 | - $prefix . $hook, |
|
1302 | - [ |
|
1303 | - Filesystem::signal_param_path => $path |
|
1304 | - ] |
|
1305 | - ); |
|
1306 | - } |
|
1307 | - } |
|
1308 | - } |
|
1309 | - return $run; |
|
1310 | - } |
|
1311 | - |
|
1312 | - /** |
|
1313 | - * check if a file or folder has been updated since $time |
|
1314 | - * |
|
1315 | - * @param string $path |
|
1316 | - * @param int $time |
|
1317 | - * @return bool |
|
1318 | - */ |
|
1319 | - public function hasUpdated($path, $time) { |
|
1320 | - return $this->basicOperation('hasUpdated', $path, [], $time); |
|
1321 | - } |
|
1322 | - |
|
1323 | - /** |
|
1324 | - * @param string $ownerId |
|
1325 | - * @return IUser |
|
1326 | - */ |
|
1327 | - private function getUserObjectForOwner(string $ownerId) { |
|
1328 | - return new LazyUser($ownerId, $this->userManager); |
|
1329 | - } |
|
1330 | - |
|
1331 | - /** |
|
1332 | - * Get file info from cache |
|
1333 | - * |
|
1334 | - * If the file is not in cached it will be scanned |
|
1335 | - * If the file has changed on storage the cache will be updated |
|
1336 | - * |
|
1337 | - * @param \OC\Files\Storage\Storage $storage |
|
1338 | - * @param string $internalPath |
|
1339 | - * @param string $relativePath |
|
1340 | - * @return ICacheEntry|bool |
|
1341 | - */ |
|
1342 | - private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
1343 | - $cache = $storage->getCache($internalPath); |
|
1344 | - $data = $cache->get($internalPath); |
|
1345 | - $watcher = $storage->getWatcher($internalPath); |
|
1346 | - |
|
1347 | - try { |
|
1348 | - // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
1349 | - if (!$data || (isset($data['size']) && $data['size'] === -1)) { |
|
1350 | - if (!$storage->file_exists($internalPath)) { |
|
1351 | - return false; |
|
1352 | - } |
|
1353 | - // don't need to get a lock here since the scanner does it's own locking |
|
1354 | - $scanner = $storage->getScanner($internalPath); |
|
1355 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1356 | - $data = $cache->get($internalPath); |
|
1357 | - } elseif (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
1358 | - $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1359 | - $watcher->update($internalPath, $data); |
|
1360 | - $storage->getPropagator()->propagateChange($internalPath, time()); |
|
1361 | - $data = $cache->get($internalPath); |
|
1362 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1363 | - } |
|
1364 | - } catch (LockedException $e) { |
|
1365 | - // if the file is locked we just use the old cache info |
|
1366 | - } |
|
1367 | - |
|
1368 | - return $data; |
|
1369 | - } |
|
1370 | - |
|
1371 | - /** |
|
1372 | - * get the filesystem info |
|
1373 | - * |
|
1374 | - * @param string $path |
|
1375 | - * @param bool|string $includeMountPoints true to add mountpoint sizes, |
|
1376 | - * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
1377 | - * @return \OC\Files\FileInfo|false False if file does not exist |
|
1378 | - */ |
|
1379 | - public function getFileInfo($path, $includeMountPoints = true) { |
|
1380 | - $this->assertPathLength($path); |
|
1381 | - if (!Filesystem::isValidPath($path)) { |
|
1382 | - return false; |
|
1383 | - } |
|
1384 | - if (Cache\Scanner::isPartialFile($path)) { |
|
1385 | - return $this->getPartFileInfo($path); |
|
1386 | - } |
|
1387 | - $relativePath = $path; |
|
1388 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1389 | - |
|
1390 | - $mount = Filesystem::getMountManager()->find($path); |
|
1391 | - $storage = $mount->getStorage(); |
|
1392 | - $internalPath = $mount->getInternalPath($path); |
|
1393 | - if ($storage) { |
|
1394 | - $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
1395 | - |
|
1396 | - if (!$data instanceof ICacheEntry) { |
|
1397 | - return false; |
|
1398 | - } |
|
1399 | - |
|
1400 | - if ($mount instanceof MoveableMount && $internalPath === '') { |
|
1401 | - $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
1402 | - } |
|
1403 | - $ownerId = $storage->getOwner($internalPath); |
|
1404 | - $owner = null; |
|
1405 | - if ($ownerId !== null && $ownerId !== false) { |
|
1406 | - // ownerId might be null if files are accessed with an access token without file system access |
|
1407 | - $owner = $this->getUserObjectForOwner($ownerId); |
|
1408 | - } |
|
1409 | - $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
1410 | - |
|
1411 | - if (isset($data['fileid'])) { |
|
1412 | - if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
1413 | - //add the sizes of other mount points to the folder |
|
1414 | - $extOnly = ($includeMountPoints === 'ext'); |
|
1415 | - $this->addSubMounts($info, $extOnly); |
|
1416 | - } |
|
1417 | - } |
|
1418 | - |
|
1419 | - return $info; |
|
1420 | - } else { |
|
1421 | - $this->logger->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint(), ['app' => 'core']); |
|
1422 | - } |
|
1423 | - |
|
1424 | - return false; |
|
1425 | - } |
|
1426 | - |
|
1427 | - /** |
|
1428 | - * Extend a FileInfo that was previously requested with `$includeMountPoints = false` to include the sub mounts |
|
1429 | - */ |
|
1430 | - public function addSubMounts(FileInfo $info, $extOnly = false): void { |
|
1431 | - $mounts = Filesystem::getMountManager()->findIn($info->getPath()); |
|
1432 | - $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
1433 | - $subStorage = $mount->getStorage(); |
|
1434 | - return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
1435 | - })); |
|
1436 | - } |
|
1437 | - |
|
1438 | - /** |
|
1439 | - * get the content of a directory |
|
1440 | - * |
|
1441 | - * @param string $directory path under datadirectory |
|
1442 | - * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
1443 | - * @return FileInfo[] |
|
1444 | - */ |
|
1445 | - public function getDirectoryContent($directory, $mimetype_filter = '', \OCP\Files\FileInfo $directoryInfo = null) { |
|
1446 | - $this->assertPathLength($directory); |
|
1447 | - if (!Filesystem::isValidPath($directory)) { |
|
1448 | - return []; |
|
1449 | - } |
|
1450 | - |
|
1451 | - $path = $this->getAbsolutePath($directory); |
|
1452 | - $path = Filesystem::normalizePath($path); |
|
1453 | - $mount = $this->getMount($directory); |
|
1454 | - $storage = $mount->getStorage(); |
|
1455 | - $internalPath = $mount->getInternalPath($path); |
|
1456 | - if (!$storage) { |
|
1457 | - return []; |
|
1458 | - } |
|
1459 | - |
|
1460 | - $cache = $storage->getCache($internalPath); |
|
1461 | - $user = \OC_User::getUser(); |
|
1462 | - |
|
1463 | - if (!$directoryInfo) { |
|
1464 | - $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
1465 | - if (!$data instanceof ICacheEntry || !isset($data['fileid'])) { |
|
1466 | - return []; |
|
1467 | - } |
|
1468 | - } else { |
|
1469 | - $data = $directoryInfo; |
|
1470 | - } |
|
1471 | - |
|
1472 | - if (!($data->getPermissions() & Constants::PERMISSION_READ)) { |
|
1473 | - return []; |
|
1474 | - } |
|
1475 | - |
|
1476 | - $folderId = $data->getId(); |
|
1477 | - $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
1478 | - |
|
1479 | - $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
1480 | - |
|
1481 | - $fileNames = array_map(function (ICacheEntry $content) { |
|
1482 | - return $content->getName(); |
|
1483 | - }, $contents); |
|
1484 | - /** |
|
1485 | - * @var \OC\Files\FileInfo[] $fileInfos |
|
1486 | - */ |
|
1487 | - $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
1488 | - if ($sharingDisabled) { |
|
1489 | - $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1490 | - } |
|
1491 | - $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
1492 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
1493 | - }, $contents); |
|
1494 | - $files = array_combine($fileNames, $fileInfos); |
|
1495 | - |
|
1496 | - //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
1497 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
1498 | - $dirLength = strlen($path); |
|
1499 | - foreach ($mounts as $mount) { |
|
1500 | - $mountPoint = $mount->getMountPoint(); |
|
1501 | - $subStorage = $mount->getStorage(); |
|
1502 | - if ($subStorage) { |
|
1503 | - $subCache = $subStorage->getCache(''); |
|
1504 | - |
|
1505 | - $rootEntry = $subCache->get(''); |
|
1506 | - if (!$rootEntry) { |
|
1507 | - $subScanner = $subStorage->getScanner(); |
|
1508 | - try { |
|
1509 | - $subScanner->scanFile(''); |
|
1510 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
1511 | - continue; |
|
1512 | - } catch (\OCP\Files\StorageInvalidException $e) { |
|
1513 | - continue; |
|
1514 | - } catch (\Exception $e) { |
|
1515 | - // sometimes when the storage is not available it can be any exception |
|
1516 | - $this->logger->error('Exception while scanning storage "' . $subStorage->getId() . '"', [ |
|
1517 | - 'exception' => $e, |
|
1518 | - 'app' => 'core', |
|
1519 | - ]); |
|
1520 | - continue; |
|
1521 | - } |
|
1522 | - $rootEntry = $subCache->get(''); |
|
1523 | - } |
|
1524 | - |
|
1525 | - if ($rootEntry && ($rootEntry->getPermissions() & Constants::PERMISSION_READ)) { |
|
1526 | - $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
1527 | - if ($pos = strpos($relativePath, '/')) { |
|
1528 | - //mountpoint inside subfolder add size to the correct folder |
|
1529 | - $entryName = substr($relativePath, 0, $pos); |
|
1530 | - if (isset($files[$entryName])) { |
|
1531 | - $files[$entryName]->addSubEntry($rootEntry, $mountPoint); |
|
1532 | - } |
|
1533 | - } else { //mountpoint in this folder, add an entry for it |
|
1534 | - $rootEntry['name'] = $relativePath; |
|
1535 | - $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
1536 | - $permissions = $rootEntry['permissions']; |
|
1537 | - // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
1538 | - // for shared files/folders we use the permissions given by the owner |
|
1539 | - if ($mount instanceof MoveableMount) { |
|
1540 | - $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
1541 | - } else { |
|
1542 | - $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
1543 | - } |
|
1544 | - |
|
1545 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
1546 | - |
|
1547 | - // if sharing was disabled for the user we remove the share permissions |
|
1548 | - if (\OCP\Util::isSharingDisabledForUser()) { |
|
1549 | - $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1550 | - } |
|
1551 | - |
|
1552 | - $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
1553 | - $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
1554 | - } |
|
1555 | - } |
|
1556 | - } |
|
1557 | - } |
|
1558 | - |
|
1559 | - if ($mimetype_filter) { |
|
1560 | - $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
1561 | - if (strpos($mimetype_filter, '/')) { |
|
1562 | - return $file->getMimetype() === $mimetype_filter; |
|
1563 | - } else { |
|
1564 | - return $file->getMimePart() === $mimetype_filter; |
|
1565 | - } |
|
1566 | - }); |
|
1567 | - } |
|
1568 | - |
|
1569 | - return array_values($files); |
|
1570 | - } |
|
1571 | - |
|
1572 | - /** |
|
1573 | - * change file metadata |
|
1574 | - * |
|
1575 | - * @param string $path |
|
1576 | - * @param array|\OCP\Files\FileInfo $data |
|
1577 | - * @return int |
|
1578 | - * |
|
1579 | - * returns the fileid of the updated file |
|
1580 | - */ |
|
1581 | - public function putFileInfo($path, $data) { |
|
1582 | - $this->assertPathLength($path); |
|
1583 | - if ($data instanceof FileInfo) { |
|
1584 | - $data = $data->getData(); |
|
1585 | - } |
|
1586 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1587 | - /** |
|
1588 | - * @var \OC\Files\Storage\Storage $storage |
|
1589 | - * @var string $internalPath |
|
1590 | - */ |
|
1591 | - [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
1592 | - if ($storage) { |
|
1593 | - $cache = $storage->getCache($path); |
|
1594 | - |
|
1595 | - if (!$cache->inCache($internalPath)) { |
|
1596 | - $scanner = $storage->getScanner($internalPath); |
|
1597 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1598 | - } |
|
1599 | - |
|
1600 | - return $cache->put($internalPath, $data); |
|
1601 | - } else { |
|
1602 | - return -1; |
|
1603 | - } |
|
1604 | - } |
|
1605 | - |
|
1606 | - /** |
|
1607 | - * search for files with the name matching $query |
|
1608 | - * |
|
1609 | - * @param string $query |
|
1610 | - * @return FileInfo[] |
|
1611 | - */ |
|
1612 | - public function search($query) { |
|
1613 | - return $this->searchCommon('search', ['%' . $query . '%']); |
|
1614 | - } |
|
1615 | - |
|
1616 | - /** |
|
1617 | - * search for files with the name matching $query |
|
1618 | - * |
|
1619 | - * @param string $query |
|
1620 | - * @return FileInfo[] |
|
1621 | - */ |
|
1622 | - public function searchRaw($query) { |
|
1623 | - return $this->searchCommon('search', [$query]); |
|
1624 | - } |
|
1625 | - |
|
1626 | - /** |
|
1627 | - * search for files by mimetype |
|
1628 | - * |
|
1629 | - * @param string $mimetype |
|
1630 | - * @return FileInfo[] |
|
1631 | - */ |
|
1632 | - public function searchByMime($mimetype) { |
|
1633 | - return $this->searchCommon('searchByMime', [$mimetype]); |
|
1634 | - } |
|
1635 | - |
|
1636 | - /** |
|
1637 | - * search for files by tag |
|
1638 | - * |
|
1639 | - * @param string|int $tag name or tag id |
|
1640 | - * @param string $userId owner of the tags |
|
1641 | - * @return FileInfo[] |
|
1642 | - */ |
|
1643 | - public function searchByTag($tag, $userId) { |
|
1644 | - return $this->searchCommon('searchByTag', [$tag, $userId]); |
|
1645 | - } |
|
1646 | - |
|
1647 | - /** |
|
1648 | - * @param string $method cache method |
|
1649 | - * @param array $args |
|
1650 | - * @return FileInfo[] |
|
1651 | - */ |
|
1652 | - private function searchCommon($method, $args) { |
|
1653 | - $files = []; |
|
1654 | - $rootLength = strlen($this->fakeRoot); |
|
1655 | - |
|
1656 | - $mount = $this->getMount(''); |
|
1657 | - $mountPoint = $mount->getMountPoint(); |
|
1658 | - $storage = $mount->getStorage(); |
|
1659 | - $userManager = \OC::$server->getUserManager(); |
|
1660 | - if ($storage) { |
|
1661 | - $cache = $storage->getCache(''); |
|
1662 | - |
|
1663 | - $results = call_user_func_array([$cache, $method], $args); |
|
1664 | - foreach ($results as $result) { |
|
1665 | - if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
1666 | - $internalPath = $result['path']; |
|
1667 | - $path = $mountPoint . $result['path']; |
|
1668 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
1669 | - $owner = $userManager->get($storage->getOwner($internalPath)); |
|
1670 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1671 | - } |
|
1672 | - } |
|
1673 | - |
|
1674 | - $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
1675 | - foreach ($mounts as $mount) { |
|
1676 | - $mountPoint = $mount->getMountPoint(); |
|
1677 | - $storage = $mount->getStorage(); |
|
1678 | - if ($storage) { |
|
1679 | - $cache = $storage->getCache(''); |
|
1680 | - |
|
1681 | - $relativeMountPoint = substr($mountPoint, $rootLength); |
|
1682 | - $results = call_user_func_array([$cache, $method], $args); |
|
1683 | - if ($results) { |
|
1684 | - foreach ($results as $result) { |
|
1685 | - $internalPath = $result['path']; |
|
1686 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
1687 | - $path = rtrim($mountPoint . $internalPath, '/'); |
|
1688 | - $owner = $userManager->get($storage->getOwner($internalPath)); |
|
1689 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1690 | - } |
|
1691 | - } |
|
1692 | - } |
|
1693 | - } |
|
1694 | - } |
|
1695 | - return $files; |
|
1696 | - } |
|
1697 | - |
|
1698 | - /** |
|
1699 | - * Get the owner for a file or folder |
|
1700 | - * |
|
1701 | - * @param string $path |
|
1702 | - * @return string the user id of the owner |
|
1703 | - * @throws NotFoundException |
|
1704 | - */ |
|
1705 | - public function getOwner($path) { |
|
1706 | - $info = $this->getFileInfo($path); |
|
1707 | - if (!$info) { |
|
1708 | - throw new NotFoundException($path . ' not found while trying to get owner'); |
|
1709 | - } |
|
1710 | - |
|
1711 | - if ($info->getOwner() === null) { |
|
1712 | - throw new NotFoundException($path . ' has no owner'); |
|
1713 | - } |
|
1714 | - |
|
1715 | - return $info->getOwner()->getUID(); |
|
1716 | - } |
|
1717 | - |
|
1718 | - /** |
|
1719 | - * get the ETag for a file or folder |
|
1720 | - * |
|
1721 | - * @param string $path |
|
1722 | - * @return string |
|
1723 | - */ |
|
1724 | - public function getETag($path) { |
|
1725 | - /** |
|
1726 | - * @var Storage\Storage $storage |
|
1727 | - * @var string $internalPath |
|
1728 | - */ |
|
1729 | - [$storage, $internalPath] = $this->resolvePath($path); |
|
1730 | - if ($storage) { |
|
1731 | - return $storage->getETag($internalPath); |
|
1732 | - } else { |
|
1733 | - return null; |
|
1734 | - } |
|
1735 | - } |
|
1736 | - |
|
1737 | - /** |
|
1738 | - * Get the path of a file by id, relative to the view |
|
1739 | - * |
|
1740 | - * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
|
1741 | - * |
|
1742 | - * @param int $id |
|
1743 | - * @param int|null $storageId |
|
1744 | - * @return string |
|
1745 | - * @throws NotFoundException |
|
1746 | - */ |
|
1747 | - public function getPath($id, int $storageId = null) { |
|
1748 | - $id = (int)$id; |
|
1749 | - $manager = Filesystem::getMountManager(); |
|
1750 | - $mounts = $manager->findIn($this->fakeRoot); |
|
1751 | - $mounts[] = $manager->find($this->fakeRoot); |
|
1752 | - $mounts = array_filter($mounts); |
|
1753 | - // reverse the array, so we start with the storage this view is in |
|
1754 | - // which is the most likely to contain the file we're looking for |
|
1755 | - $mounts = array_reverse($mounts); |
|
1756 | - |
|
1757 | - // put non-shared mounts in front of the shared mount |
|
1758 | - // this prevents unneeded recursion into shares |
|
1759 | - usort($mounts, function (IMountPoint $a, IMountPoint $b) { |
|
1760 | - return $a instanceof SharedMount && (!$b instanceof SharedMount) ? 1 : -1; |
|
1761 | - }); |
|
1762 | - |
|
1763 | - if (!is_null($storageId)) { |
|
1764 | - $mounts = array_filter($mounts, function (IMountPoint $mount) use ($storageId) { |
|
1765 | - return $mount->getNumericStorageId() === $storageId; |
|
1766 | - }); |
|
1767 | - } |
|
1768 | - |
|
1769 | - foreach ($mounts as $mount) { |
|
1770 | - /** |
|
1771 | - * @var \OC\Files\Mount\MountPoint $mount |
|
1772 | - */ |
|
1773 | - if ($mount->getStorage()) { |
|
1774 | - $cache = $mount->getStorage()->getCache(); |
|
1775 | - $internalPath = $cache->getPathById($id); |
|
1776 | - if (is_string($internalPath)) { |
|
1777 | - $fullPath = $mount->getMountPoint() . $internalPath; |
|
1778 | - if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
1779 | - return $path; |
|
1780 | - } |
|
1781 | - } |
|
1782 | - } |
|
1783 | - } |
|
1784 | - throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
1785 | - } |
|
1786 | - |
|
1787 | - /** |
|
1788 | - * @param string $path |
|
1789 | - * @throws InvalidPathException |
|
1790 | - */ |
|
1791 | - private function assertPathLength($path) { |
|
1792 | - $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
1793 | - // Check for the string length - performed using isset() instead of strlen() |
|
1794 | - // because isset() is about 5x-40x faster. |
|
1795 | - if (isset($path[$maxLen])) { |
|
1796 | - $pathLen = strlen($path); |
|
1797 | - throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
1798 | - } |
|
1799 | - } |
|
1800 | - |
|
1801 | - /** |
|
1802 | - * check if it is allowed to move a mount point to a given target. |
|
1803 | - * It is not allowed to move a mount point into a different mount point or |
|
1804 | - * into an already shared folder |
|
1805 | - * |
|
1806 | - * @param IStorage $targetStorage |
|
1807 | - * @param string $targetInternalPath |
|
1808 | - * @return boolean |
|
1809 | - */ |
|
1810 | - private function targetIsNotShared(IStorage $targetStorage, string $targetInternalPath) { |
|
1811 | - // note: cannot use the view because the target is already locked |
|
1812 | - $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
1813 | - if ($fileId === -1) { |
|
1814 | - // target might not exist, need to check parent instead |
|
1815 | - $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
1816 | - } |
|
1817 | - |
|
1818 | - // check if any of the parents were shared by the current owner (include collections) |
|
1819 | - $shares = Share::getItemShared( |
|
1820 | - 'folder', |
|
1821 | - $fileId, |
|
1822 | - \OC\Share\Constants::FORMAT_NONE, |
|
1823 | - null, |
|
1824 | - true |
|
1825 | - ); |
|
1826 | - |
|
1827 | - if (count($shares) > 0) { |
|
1828 | - $this->logger->debug( |
|
1829 | - 'It is not allowed to move one mount point into a shared folder', |
|
1830 | - ['app' => 'files']); |
|
1831 | - return false; |
|
1832 | - } |
|
1833 | - |
|
1834 | - return true; |
|
1835 | - } |
|
1836 | - |
|
1837 | - /** |
|
1838 | - * Get a fileinfo object for files that are ignored in the cache (part files) |
|
1839 | - * |
|
1840 | - * @param string $path |
|
1841 | - * @return \OCP\Files\FileInfo |
|
1842 | - */ |
|
1843 | - private function getPartFileInfo($path) { |
|
1844 | - $mount = $this->getMount($path); |
|
1845 | - $storage = $mount->getStorage(); |
|
1846 | - $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
1847 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1848 | - return new FileInfo( |
|
1849 | - $this->getAbsolutePath($path), |
|
1850 | - $storage, |
|
1851 | - $internalPath, |
|
1852 | - [ |
|
1853 | - 'fileid' => null, |
|
1854 | - 'mimetype' => $storage->getMimeType($internalPath), |
|
1855 | - 'name' => basename($path), |
|
1856 | - 'etag' => null, |
|
1857 | - 'size' => $storage->filesize($internalPath), |
|
1858 | - 'mtime' => $storage->filemtime($internalPath), |
|
1859 | - 'encrypted' => false, |
|
1860 | - 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
1861 | - ], |
|
1862 | - $mount, |
|
1863 | - $owner |
|
1864 | - ); |
|
1865 | - } |
|
1866 | - |
|
1867 | - /** |
|
1868 | - * @param string $path |
|
1869 | - * @param string $fileName |
|
1870 | - * @throws InvalidPathException |
|
1871 | - */ |
|
1872 | - public function verifyPath($path, $fileName) { |
|
1873 | - try { |
|
1874 | - /** @type \OCP\Files\Storage $storage */ |
|
1875 | - [$storage, $internalPath] = $this->resolvePath($path); |
|
1876 | - $storage->verifyPath($internalPath, $fileName); |
|
1877 | - } catch (ReservedWordException $ex) { |
|
1878 | - $l = \OC::$server->getL10N('lib'); |
|
1879 | - throw new InvalidPathException($l->t('File name is a reserved word')); |
|
1880 | - } catch (InvalidCharacterInPathException $ex) { |
|
1881 | - $l = \OC::$server->getL10N('lib'); |
|
1882 | - throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
1883 | - } catch (FileNameTooLongException $ex) { |
|
1884 | - $l = \OC::$server->getL10N('lib'); |
|
1885 | - throw new InvalidPathException($l->t('File name is too long')); |
|
1886 | - } catch (InvalidDirectoryException $ex) { |
|
1887 | - $l = \OC::$server->getL10N('lib'); |
|
1888 | - throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
1889 | - } catch (EmptyFileNameException $ex) { |
|
1890 | - $l = \OC::$server->getL10N('lib'); |
|
1891 | - throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
1892 | - } |
|
1893 | - } |
|
1894 | - |
|
1895 | - /** |
|
1896 | - * get all parent folders of $path |
|
1897 | - * |
|
1898 | - * @param string $path |
|
1899 | - * @return string[] |
|
1900 | - */ |
|
1901 | - private function getParents($path) { |
|
1902 | - $path = trim($path, '/'); |
|
1903 | - if (!$path) { |
|
1904 | - return []; |
|
1905 | - } |
|
1906 | - |
|
1907 | - $parts = explode('/', $path); |
|
1908 | - |
|
1909 | - // remove the single file |
|
1910 | - array_pop($parts); |
|
1911 | - $result = ['/']; |
|
1912 | - $resultPath = ''; |
|
1913 | - foreach ($parts as $part) { |
|
1914 | - if ($part) { |
|
1915 | - $resultPath .= '/' . $part; |
|
1916 | - $result[] = $resultPath; |
|
1917 | - } |
|
1918 | - } |
|
1919 | - return $result; |
|
1920 | - } |
|
1921 | - |
|
1922 | - /** |
|
1923 | - * Returns the mount point for which to lock |
|
1924 | - * |
|
1925 | - * @param string $absolutePath absolute path |
|
1926 | - * @param bool $useParentMount true to return parent mount instead of whatever |
|
1927 | - * is mounted directly on the given path, false otherwise |
|
1928 | - * @return IMountPoint mount point for which to apply locks |
|
1929 | - */ |
|
1930 | - private function getMountForLock($absolutePath, $useParentMount = false) { |
|
1931 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
1932 | - |
|
1933 | - if ($useParentMount) { |
|
1934 | - // find out if something is mounted directly on the path |
|
1935 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
1936 | - if ($internalPath === '') { |
|
1937 | - // resolve the parent mount instead |
|
1938 | - $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
1939 | - } |
|
1940 | - } |
|
1941 | - |
|
1942 | - return $mount; |
|
1943 | - } |
|
1944 | - |
|
1945 | - /** |
|
1946 | - * Lock the given path |
|
1947 | - * |
|
1948 | - * @param string $path the path of the file to lock, relative to the view |
|
1949 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1950 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1951 | - * |
|
1952 | - * @return bool False if the path is excluded from locking, true otherwise |
|
1953 | - * @throws LockedException if the path is already locked |
|
1954 | - */ |
|
1955 | - private function lockPath($path, $type, $lockMountPoint = false) { |
|
1956 | - $absolutePath = $this->getAbsolutePath($path); |
|
1957 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1958 | - if (!$this->shouldLockFile($absolutePath)) { |
|
1959 | - return false; |
|
1960 | - } |
|
1961 | - |
|
1962 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
1963 | - if ($mount) { |
|
1964 | - try { |
|
1965 | - $storage = $mount->getStorage(); |
|
1966 | - if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
1967 | - $storage->acquireLock( |
|
1968 | - $mount->getInternalPath($absolutePath), |
|
1969 | - $type, |
|
1970 | - $this->lockingProvider |
|
1971 | - ); |
|
1972 | - } |
|
1973 | - } catch (LockedException $e) { |
|
1974 | - // rethrow with the a human-readable path |
|
1975 | - throw new LockedException( |
|
1976 | - $this->getPathRelativeToFiles($absolutePath), |
|
1977 | - $e, |
|
1978 | - $e->getExistingLock() |
|
1979 | - ); |
|
1980 | - } |
|
1981 | - } |
|
1982 | - |
|
1983 | - return true; |
|
1984 | - } |
|
1985 | - |
|
1986 | - /** |
|
1987 | - * Change the lock type |
|
1988 | - * |
|
1989 | - * @param string $path the path of the file to lock, relative to the view |
|
1990 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1991 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1992 | - * |
|
1993 | - * @return bool False if the path is excluded from locking, true otherwise |
|
1994 | - * @throws LockedException if the path is already locked |
|
1995 | - */ |
|
1996 | - public function changeLock($path, $type, $lockMountPoint = false) { |
|
1997 | - $path = Filesystem::normalizePath($path); |
|
1998 | - $absolutePath = $this->getAbsolutePath($path); |
|
1999 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2000 | - if (!$this->shouldLockFile($absolutePath)) { |
|
2001 | - return false; |
|
2002 | - } |
|
2003 | - |
|
2004 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
2005 | - if ($mount) { |
|
2006 | - try { |
|
2007 | - $storage = $mount->getStorage(); |
|
2008 | - if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
2009 | - $storage->changeLock( |
|
2010 | - $mount->getInternalPath($absolutePath), |
|
2011 | - $type, |
|
2012 | - $this->lockingProvider |
|
2013 | - ); |
|
2014 | - } |
|
2015 | - } catch (LockedException $e) { |
|
2016 | - try { |
|
2017 | - // rethrow with the a human-readable path |
|
2018 | - throw new LockedException( |
|
2019 | - $this->getPathRelativeToFiles($absolutePath), |
|
2020 | - $e, |
|
2021 | - $e->getExistingLock() |
|
2022 | - ); |
|
2023 | - } catch (\InvalidArgumentException $ex) { |
|
2024 | - throw new LockedException( |
|
2025 | - $absolutePath, |
|
2026 | - $ex, |
|
2027 | - $e->getExistingLock() |
|
2028 | - ); |
|
2029 | - } |
|
2030 | - } |
|
2031 | - } |
|
2032 | - |
|
2033 | - return true; |
|
2034 | - } |
|
2035 | - |
|
2036 | - /** |
|
2037 | - * Unlock the given path |
|
2038 | - * |
|
2039 | - * @param string $path the path of the file to unlock, relative to the view |
|
2040 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2041 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2042 | - * |
|
2043 | - * @return bool False if the path is excluded from locking, true otherwise |
|
2044 | - * @throws LockedException |
|
2045 | - */ |
|
2046 | - private function unlockPath($path, $type, $lockMountPoint = false) { |
|
2047 | - $absolutePath = $this->getAbsolutePath($path); |
|
2048 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2049 | - if (!$this->shouldLockFile($absolutePath)) { |
|
2050 | - return false; |
|
2051 | - } |
|
2052 | - |
|
2053 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
2054 | - if ($mount) { |
|
2055 | - $storage = $mount->getStorage(); |
|
2056 | - if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
2057 | - $storage->releaseLock( |
|
2058 | - $mount->getInternalPath($absolutePath), |
|
2059 | - $type, |
|
2060 | - $this->lockingProvider |
|
2061 | - ); |
|
2062 | - } |
|
2063 | - } |
|
2064 | - |
|
2065 | - return true; |
|
2066 | - } |
|
2067 | - |
|
2068 | - /** |
|
2069 | - * Lock a path and all its parents up to the root of the view |
|
2070 | - * |
|
2071 | - * @param string $path the path of the file to lock relative to the view |
|
2072 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2073 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2074 | - * |
|
2075 | - * @return bool False if the path is excluded from locking, true otherwise |
|
2076 | - * @throws LockedException |
|
2077 | - */ |
|
2078 | - public function lockFile($path, $type, $lockMountPoint = false) { |
|
2079 | - $absolutePath = $this->getAbsolutePath($path); |
|
2080 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2081 | - if (!$this->shouldLockFile($absolutePath)) { |
|
2082 | - return false; |
|
2083 | - } |
|
2084 | - |
|
2085 | - $this->lockPath($path, $type, $lockMountPoint); |
|
2086 | - |
|
2087 | - $parents = $this->getParents($path); |
|
2088 | - foreach ($parents as $parent) { |
|
2089 | - $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2090 | - } |
|
2091 | - |
|
2092 | - return true; |
|
2093 | - } |
|
2094 | - |
|
2095 | - /** |
|
2096 | - * Unlock a path and all its parents up to the root of the view |
|
2097 | - * |
|
2098 | - * @param string $path the path of the file to lock relative to the view |
|
2099 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2100 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2101 | - * |
|
2102 | - * @return bool False if the path is excluded from locking, true otherwise |
|
2103 | - * @throws LockedException |
|
2104 | - */ |
|
2105 | - public function unlockFile($path, $type, $lockMountPoint = false) { |
|
2106 | - $absolutePath = $this->getAbsolutePath($path); |
|
2107 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2108 | - if (!$this->shouldLockFile($absolutePath)) { |
|
2109 | - return false; |
|
2110 | - } |
|
2111 | - |
|
2112 | - $this->unlockPath($path, $type, $lockMountPoint); |
|
2113 | - |
|
2114 | - $parents = $this->getParents($path); |
|
2115 | - foreach ($parents as $parent) { |
|
2116 | - $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2117 | - } |
|
2118 | - |
|
2119 | - return true; |
|
2120 | - } |
|
2121 | - |
|
2122 | - /** |
|
2123 | - * Only lock files in data/user/files/ |
|
2124 | - * |
|
2125 | - * @param string $path Absolute path to the file/folder we try to (un)lock |
|
2126 | - * @return bool |
|
2127 | - */ |
|
2128 | - protected function shouldLockFile($path) { |
|
2129 | - $path = Filesystem::normalizePath($path); |
|
2130 | - |
|
2131 | - $pathSegments = explode('/', $path); |
|
2132 | - if (isset($pathSegments[2])) { |
|
2133 | - // E.g.: /username/files/path-to-file |
|
2134 | - return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
2135 | - } |
|
2136 | - |
|
2137 | - return strpos($path, '/appdata_') !== 0; |
|
2138 | - } |
|
2139 | - |
|
2140 | - /** |
|
2141 | - * Shortens the given absolute path to be relative to |
|
2142 | - * "$user/files". |
|
2143 | - * |
|
2144 | - * @param string $absolutePath absolute path which is under "files" |
|
2145 | - * |
|
2146 | - * @return string path relative to "files" with trimmed slashes or null |
|
2147 | - * if the path was NOT relative to files |
|
2148 | - * |
|
2149 | - * @throws \InvalidArgumentException if the given path was not under "files" |
|
2150 | - * @since 8.1.0 |
|
2151 | - */ |
|
2152 | - public function getPathRelativeToFiles($absolutePath) { |
|
2153 | - $path = Filesystem::normalizePath($absolutePath); |
|
2154 | - $parts = explode('/', trim($path, '/'), 3); |
|
2155 | - // "$user", "files", "path/to/dir" |
|
2156 | - if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
2157 | - $this->logger->error( |
|
2158 | - '$absolutePath must be relative to "files", value is "{absolutePath}"', |
|
2159 | - [ |
|
2160 | - 'absolutePath' => $absolutePath, |
|
2161 | - ] |
|
2162 | - ); |
|
2163 | - throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
2164 | - } |
|
2165 | - if (isset($parts[2])) { |
|
2166 | - return $parts[2]; |
|
2167 | - } |
|
2168 | - return ''; |
|
2169 | - } |
|
2170 | - |
|
2171 | - /** |
|
2172 | - * @param string $filename |
|
2173 | - * @return array |
|
2174 | - * @throws \OC\User\NoUserException |
|
2175 | - * @throws NotFoundException |
|
2176 | - */ |
|
2177 | - public function getUidAndFilename($filename) { |
|
2178 | - $info = $this->getFileInfo($filename); |
|
2179 | - if (!$info instanceof \OCP\Files\FileInfo) { |
|
2180 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
2181 | - } |
|
2182 | - $uid = $info->getOwner()->getUID(); |
|
2183 | - if ($uid != \OC_User::getUser()) { |
|
2184 | - Filesystem::initMountPoints($uid); |
|
2185 | - $ownerView = new View('/' . $uid . '/files'); |
|
2186 | - try { |
|
2187 | - $filename = $ownerView->getPath($info['fileid']); |
|
2188 | - } catch (NotFoundException $e) { |
|
2189 | - throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
2190 | - } |
|
2191 | - } |
|
2192 | - return [$uid, $filename]; |
|
2193 | - } |
|
2194 | - |
|
2195 | - /** |
|
2196 | - * Creates parent non-existing folders |
|
2197 | - * |
|
2198 | - * @param string $filePath |
|
2199 | - * @return bool |
|
2200 | - */ |
|
2201 | - private function createParentDirectories($filePath) { |
|
2202 | - $directoryParts = explode('/', $filePath); |
|
2203 | - $directoryParts = array_filter($directoryParts); |
|
2204 | - foreach ($directoryParts as $key => $part) { |
|
2205 | - $currentPathElements = array_slice($directoryParts, 0, $key); |
|
2206 | - $currentPath = '/' . implode('/', $currentPathElements); |
|
2207 | - if ($this->is_file($currentPath)) { |
|
2208 | - return false; |
|
2209 | - } |
|
2210 | - if (!$this->file_exists($currentPath)) { |
|
2211 | - $this->mkdir($currentPath); |
|
2212 | - } |
|
2213 | - } |
|
2214 | - |
|
2215 | - return true; |
|
2216 | - } |
|
89 | + /** @var string */ |
|
90 | + private $fakeRoot = ''; |
|
91 | + |
|
92 | + /** |
|
93 | + * @var \OCP\Lock\ILockingProvider |
|
94 | + */ |
|
95 | + protected $lockingProvider; |
|
96 | + |
|
97 | + private $lockingEnabled; |
|
98 | + |
|
99 | + private $updaterEnabled = true; |
|
100 | + |
|
101 | + /** @var \OC\User\Manager */ |
|
102 | + private $userManager; |
|
103 | + |
|
104 | + private LoggerInterface $logger; |
|
105 | + |
|
106 | + /** |
|
107 | + * @param string $root |
|
108 | + * @throws \Exception If $root contains an invalid path |
|
109 | + */ |
|
110 | + public function __construct($root = '') { |
|
111 | + if (is_null($root)) { |
|
112 | + throw new \InvalidArgumentException('Root can\'t be null'); |
|
113 | + } |
|
114 | + if (!Filesystem::isValidPath($root)) { |
|
115 | + throw new \Exception(); |
|
116 | + } |
|
117 | + |
|
118 | + $this->fakeRoot = $root; |
|
119 | + $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
120 | + $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
121 | + $this->userManager = \OC::$server->getUserManager(); |
|
122 | + $this->logger = \OC::$server->get(LoggerInterface::class); |
|
123 | + } |
|
124 | + |
|
125 | + public function getAbsolutePath($path = '/') { |
|
126 | + if ($path === null) { |
|
127 | + return null; |
|
128 | + } |
|
129 | + $this->assertPathLength($path); |
|
130 | + if ($path === '') { |
|
131 | + $path = '/'; |
|
132 | + } |
|
133 | + if ($path[0] !== '/') { |
|
134 | + $path = '/' . $path; |
|
135 | + } |
|
136 | + return $this->fakeRoot . $path; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * change the root to a fake root |
|
141 | + * |
|
142 | + * @param string $fakeRoot |
|
143 | + * @return boolean|null |
|
144 | + */ |
|
145 | + public function chroot($fakeRoot) { |
|
146 | + if (!$fakeRoot == '') { |
|
147 | + if ($fakeRoot[0] !== '/') { |
|
148 | + $fakeRoot = '/' . $fakeRoot; |
|
149 | + } |
|
150 | + } |
|
151 | + $this->fakeRoot = $fakeRoot; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * get the fake root |
|
156 | + * |
|
157 | + * @return string |
|
158 | + */ |
|
159 | + public function getRoot() { |
|
160 | + return $this->fakeRoot; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * get path relative to the root of the view |
|
165 | + * |
|
166 | + * @param string $path |
|
167 | + * @return string |
|
168 | + */ |
|
169 | + public function getRelativePath($path) { |
|
170 | + $this->assertPathLength($path); |
|
171 | + if ($this->fakeRoot == '') { |
|
172 | + return $path; |
|
173 | + } |
|
174 | + |
|
175 | + if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
176 | + return '/'; |
|
177 | + } |
|
178 | + |
|
179 | + // missing slashes can cause wrong matches! |
|
180 | + $root = rtrim($this->fakeRoot, '/') . '/'; |
|
181 | + |
|
182 | + if (strpos($path, $root) !== 0) { |
|
183 | + return null; |
|
184 | + } else { |
|
185 | + $path = substr($path, strlen($this->fakeRoot)); |
|
186 | + if (strlen($path) === 0) { |
|
187 | + return '/'; |
|
188 | + } else { |
|
189 | + return $path; |
|
190 | + } |
|
191 | + } |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * get the mountpoint of the storage object for a path |
|
196 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
197 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
198 | + * and does not take the chroot into account ) |
|
199 | + * |
|
200 | + * @param string $path |
|
201 | + * @return string |
|
202 | + */ |
|
203 | + public function getMountPoint($path) { |
|
204 | + return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * get the mountpoint of the storage object for a path |
|
209 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
210 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
211 | + * and does not take the chroot into account ) |
|
212 | + * |
|
213 | + * @param string $path |
|
214 | + * @return \OCP\Files\Mount\IMountPoint |
|
215 | + */ |
|
216 | + public function getMount($path) { |
|
217 | + return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * resolve a path to a storage and internal path |
|
222 | + * |
|
223 | + * @param string $path |
|
224 | + * @return array an array consisting of the storage and the internal path |
|
225 | + */ |
|
226 | + public function resolvePath($path) { |
|
227 | + $a = $this->getAbsolutePath($path); |
|
228 | + $p = Filesystem::normalizePath($a); |
|
229 | + return Filesystem::resolvePath($p); |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * return the path to a local version of the file |
|
234 | + * we need this because we can't know if a file is stored local or not from |
|
235 | + * outside the filestorage and for some purposes a local file is needed |
|
236 | + * |
|
237 | + * @param string $path |
|
238 | + * @return string |
|
239 | + */ |
|
240 | + public function getLocalFile($path) { |
|
241 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
242 | + $path = $this->getAbsolutePath($path); |
|
243 | + [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
244 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
245 | + return $storage->getLocalFile($internalPath); |
|
246 | + } else { |
|
247 | + return null; |
|
248 | + } |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * @param string $path |
|
253 | + * @return string |
|
254 | + */ |
|
255 | + public function getLocalFolder($path) { |
|
256 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
257 | + $path = $this->getAbsolutePath($path); |
|
258 | + [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
259 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
260 | + return $storage->getLocalFolder($internalPath); |
|
261 | + } else { |
|
262 | + return null; |
|
263 | + } |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * the following functions operate with arguments and return values identical |
|
268 | + * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
269 | + * for \OC\Files\Storage\Storage via basicOperation(). |
|
270 | + */ |
|
271 | + public function mkdir($path) { |
|
272 | + return $this->basicOperation('mkdir', $path, ['create', 'write']); |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * remove mount point |
|
277 | + * |
|
278 | + * @param IMountPoint $mount |
|
279 | + * @param string $path relative to data/ |
|
280 | + * @return boolean |
|
281 | + */ |
|
282 | + protected function removeMount($mount, $path) { |
|
283 | + if ($mount instanceof MoveableMount) { |
|
284 | + // cut of /user/files to get the relative path to data/user/files |
|
285 | + $pathParts = explode('/', $path, 4); |
|
286 | + $relPath = '/' . $pathParts[3]; |
|
287 | + $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
288 | + \OC_Hook::emit( |
|
289 | + Filesystem::CLASSNAME, "umount", |
|
290 | + [Filesystem::signal_param_path => $relPath] |
|
291 | + ); |
|
292 | + $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
293 | + $result = $mount->removeMount(); |
|
294 | + $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
295 | + if ($result) { |
|
296 | + \OC_Hook::emit( |
|
297 | + Filesystem::CLASSNAME, "post_umount", |
|
298 | + [Filesystem::signal_param_path => $relPath] |
|
299 | + ); |
|
300 | + } |
|
301 | + $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
302 | + return $result; |
|
303 | + } else { |
|
304 | + // do not allow deleting the storage's root / the mount point |
|
305 | + // because for some storages it might delete the whole contents |
|
306 | + // but isn't supposed to work that way |
|
307 | + return false; |
|
308 | + } |
|
309 | + } |
|
310 | + |
|
311 | + public function disableCacheUpdate() { |
|
312 | + $this->updaterEnabled = false; |
|
313 | + } |
|
314 | + |
|
315 | + public function enableCacheUpdate() { |
|
316 | + $this->updaterEnabled = true; |
|
317 | + } |
|
318 | + |
|
319 | + protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
320 | + if ($this->updaterEnabled) { |
|
321 | + if (is_null($time)) { |
|
322 | + $time = time(); |
|
323 | + } |
|
324 | + $storage->getUpdater()->update($internalPath, $time); |
|
325 | + } |
|
326 | + } |
|
327 | + |
|
328 | + protected function removeUpdate(Storage $storage, $internalPath) { |
|
329 | + if ($this->updaterEnabled) { |
|
330 | + $storage->getUpdater()->remove($internalPath); |
|
331 | + } |
|
332 | + } |
|
333 | + |
|
334 | + protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
335 | + if ($this->updaterEnabled) { |
|
336 | + $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
337 | + } |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * @param string $path |
|
342 | + * @return bool|mixed |
|
343 | + */ |
|
344 | + public function rmdir($path) { |
|
345 | + $absolutePath = $this->getAbsolutePath($path); |
|
346 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
347 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
348 | + return $this->removeMount($mount, $absolutePath); |
|
349 | + } |
|
350 | + if ($this->is_dir($path)) { |
|
351 | + $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
352 | + } else { |
|
353 | + $result = false; |
|
354 | + } |
|
355 | + |
|
356 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
357 | + $storage = $mount->getStorage(); |
|
358 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
359 | + $storage->getUpdater()->remove($internalPath); |
|
360 | + } |
|
361 | + return $result; |
|
362 | + } |
|
363 | + |
|
364 | + /** |
|
365 | + * @param string $path |
|
366 | + * @return resource |
|
367 | + */ |
|
368 | + public function opendir($path) { |
|
369 | + return $this->basicOperation('opendir', $path, ['read']); |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * @param string $path |
|
374 | + * @return bool|mixed |
|
375 | + */ |
|
376 | + public function is_dir($path) { |
|
377 | + if ($path == '/') { |
|
378 | + return true; |
|
379 | + } |
|
380 | + return $this->basicOperation('is_dir', $path); |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * @param string $path |
|
385 | + * @return bool|mixed |
|
386 | + */ |
|
387 | + public function is_file($path) { |
|
388 | + if ($path == '/') { |
|
389 | + return false; |
|
390 | + } |
|
391 | + return $this->basicOperation('is_file', $path); |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * @param string $path |
|
396 | + * @return mixed |
|
397 | + */ |
|
398 | + public function stat($path) { |
|
399 | + return $this->basicOperation('stat', $path); |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * @param string $path |
|
404 | + * @return mixed |
|
405 | + */ |
|
406 | + public function filetype($path) { |
|
407 | + return $this->basicOperation('filetype', $path); |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * @param string $path |
|
412 | + * @return mixed |
|
413 | + */ |
|
414 | + public function filesize(string $path) { |
|
415 | + return $this->basicOperation('filesize', $path); |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * @param string $path |
|
420 | + * @return bool|mixed |
|
421 | + * @throws \OCP\Files\InvalidPathException |
|
422 | + */ |
|
423 | + public function readfile($path) { |
|
424 | + $this->assertPathLength($path); |
|
425 | + if (ob_get_level()) { |
|
426 | + ob_end_clean(); |
|
427 | + } |
|
428 | + $handle = $this->fopen($path, 'rb'); |
|
429 | + if ($handle) { |
|
430 | + $chunkSize = 524288; // 512 kB chunks |
|
431 | + while (!feof($handle)) { |
|
432 | + echo fread($handle, $chunkSize); |
|
433 | + flush(); |
|
434 | + } |
|
435 | + fclose($handle); |
|
436 | + return $this->filesize($path); |
|
437 | + } |
|
438 | + return false; |
|
439 | + } |
|
440 | + |
|
441 | + /** |
|
442 | + * @param string $path |
|
443 | + * @param int $from |
|
444 | + * @param int $to |
|
445 | + * @return bool|mixed |
|
446 | + * @throws \OCP\Files\InvalidPathException |
|
447 | + * @throws \OCP\Files\UnseekableException |
|
448 | + */ |
|
449 | + public function readfilePart($path, $from, $to) { |
|
450 | + $this->assertPathLength($path); |
|
451 | + if (ob_get_level()) { |
|
452 | + ob_end_clean(); |
|
453 | + } |
|
454 | + $handle = $this->fopen($path, 'rb'); |
|
455 | + if ($handle) { |
|
456 | + $chunkSize = 524288; // 512 kB chunks |
|
457 | + $startReading = true; |
|
458 | + |
|
459 | + if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
460 | + // forward file handle via chunked fread because fseek seem to have failed |
|
461 | + |
|
462 | + $end = $from + 1; |
|
463 | + while (!feof($handle) && ftell($handle) < $end && ftell($handle) !== $from) { |
|
464 | + $len = $from - ftell($handle); |
|
465 | + if ($len > $chunkSize) { |
|
466 | + $len = $chunkSize; |
|
467 | + } |
|
468 | + $result = fread($handle, $len); |
|
469 | + |
|
470 | + if ($result === false) { |
|
471 | + $startReading = false; |
|
472 | + break; |
|
473 | + } |
|
474 | + } |
|
475 | + } |
|
476 | + |
|
477 | + if ($startReading) { |
|
478 | + $end = $to + 1; |
|
479 | + while (!feof($handle) && ftell($handle) < $end) { |
|
480 | + $len = $end - ftell($handle); |
|
481 | + if ($len > $chunkSize) { |
|
482 | + $len = $chunkSize; |
|
483 | + } |
|
484 | + echo fread($handle, $len); |
|
485 | + flush(); |
|
486 | + } |
|
487 | + return ftell($handle) - $from; |
|
488 | + } |
|
489 | + |
|
490 | + throw new \OCP\Files\UnseekableException('fseek error'); |
|
491 | + } |
|
492 | + return false; |
|
493 | + } |
|
494 | + |
|
495 | + /** |
|
496 | + * @param string $path |
|
497 | + * @return mixed |
|
498 | + */ |
|
499 | + public function isCreatable($path) { |
|
500 | + return $this->basicOperation('isCreatable', $path); |
|
501 | + } |
|
502 | + |
|
503 | + /** |
|
504 | + * @param string $path |
|
505 | + * @return mixed |
|
506 | + */ |
|
507 | + public function isReadable($path) { |
|
508 | + return $this->basicOperation('isReadable', $path); |
|
509 | + } |
|
510 | + |
|
511 | + /** |
|
512 | + * @param string $path |
|
513 | + * @return mixed |
|
514 | + */ |
|
515 | + public function isUpdatable($path) { |
|
516 | + return $this->basicOperation('isUpdatable', $path); |
|
517 | + } |
|
518 | + |
|
519 | + /** |
|
520 | + * @param string $path |
|
521 | + * @return bool|mixed |
|
522 | + */ |
|
523 | + public function isDeletable($path) { |
|
524 | + $absolutePath = $this->getAbsolutePath($path); |
|
525 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
526 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
527 | + return $mount instanceof MoveableMount; |
|
528 | + } |
|
529 | + return $this->basicOperation('isDeletable', $path); |
|
530 | + } |
|
531 | + |
|
532 | + /** |
|
533 | + * @param string $path |
|
534 | + * @return mixed |
|
535 | + */ |
|
536 | + public function isSharable($path) { |
|
537 | + return $this->basicOperation('isSharable', $path); |
|
538 | + } |
|
539 | + |
|
540 | + /** |
|
541 | + * @param string $path |
|
542 | + * @return bool|mixed |
|
543 | + */ |
|
544 | + public function file_exists($path) { |
|
545 | + if ($path == '/') { |
|
546 | + return true; |
|
547 | + } |
|
548 | + return $this->basicOperation('file_exists', $path); |
|
549 | + } |
|
550 | + |
|
551 | + /** |
|
552 | + * @param string $path |
|
553 | + * @return mixed |
|
554 | + */ |
|
555 | + public function filemtime($path) { |
|
556 | + return $this->basicOperation('filemtime', $path); |
|
557 | + } |
|
558 | + |
|
559 | + /** |
|
560 | + * @param string $path |
|
561 | + * @param int|string $mtime |
|
562 | + * @return bool |
|
563 | + */ |
|
564 | + public function touch($path, $mtime = null) { |
|
565 | + if (!is_null($mtime) and !is_numeric($mtime)) { |
|
566 | + $mtime = strtotime($mtime); |
|
567 | + } |
|
568 | + |
|
569 | + $hooks = ['touch']; |
|
570 | + |
|
571 | + if (!$this->file_exists($path)) { |
|
572 | + $hooks[] = 'create'; |
|
573 | + $hooks[] = 'write'; |
|
574 | + } |
|
575 | + try { |
|
576 | + $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
577 | + } catch (\Exception $e) { |
|
578 | + $this->logger->info('Error while setting modified time', ['app' => 'core', 'exception' => $e]); |
|
579 | + $result = false; |
|
580 | + } |
|
581 | + if (!$result) { |
|
582 | + // If create file fails because of permissions on external storage like SMB folders, |
|
583 | + // check file exists and return false if not. |
|
584 | + if (!$this->file_exists($path)) { |
|
585 | + return false; |
|
586 | + } |
|
587 | + if (is_null($mtime)) { |
|
588 | + $mtime = time(); |
|
589 | + } |
|
590 | + //if native touch fails, we emulate it by changing the mtime in the cache |
|
591 | + $this->putFileInfo($path, ['mtime' => floor($mtime)]); |
|
592 | + } |
|
593 | + return true; |
|
594 | + } |
|
595 | + |
|
596 | + /** |
|
597 | + * @param string $path |
|
598 | + * @return mixed |
|
599 | + * @throws LockedException |
|
600 | + */ |
|
601 | + public function file_get_contents($path) { |
|
602 | + return $this->basicOperation('file_get_contents', $path, ['read']); |
|
603 | + } |
|
604 | + |
|
605 | + /** |
|
606 | + * @param bool $exists |
|
607 | + * @param string $path |
|
608 | + * @param bool $run |
|
609 | + */ |
|
610 | + protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
611 | + if (!$exists) { |
|
612 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, [ |
|
613 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
614 | + Filesystem::signal_param_run => &$run, |
|
615 | + ]); |
|
616 | + } else { |
|
617 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, [ |
|
618 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
619 | + Filesystem::signal_param_run => &$run, |
|
620 | + ]); |
|
621 | + } |
|
622 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, [ |
|
623 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
624 | + Filesystem::signal_param_run => &$run, |
|
625 | + ]); |
|
626 | + } |
|
627 | + |
|
628 | + /** |
|
629 | + * @param bool $exists |
|
630 | + * @param string $path |
|
631 | + */ |
|
632 | + protected function emit_file_hooks_post($exists, $path) { |
|
633 | + if (!$exists) { |
|
634 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, [ |
|
635 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
636 | + ]); |
|
637 | + } else { |
|
638 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, [ |
|
639 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
640 | + ]); |
|
641 | + } |
|
642 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, [ |
|
643 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
644 | + ]); |
|
645 | + } |
|
646 | + |
|
647 | + /** |
|
648 | + * @param string $path |
|
649 | + * @param string|resource $data |
|
650 | + * @return bool|mixed |
|
651 | + * @throws LockedException |
|
652 | + */ |
|
653 | + public function file_put_contents($path, $data) { |
|
654 | + if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
655 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
656 | + if (Filesystem::isValidPath($path) |
|
657 | + and !Filesystem::isFileBlacklisted($path) |
|
658 | + ) { |
|
659 | + $path = $this->getRelativePath($absolutePath); |
|
660 | + |
|
661 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
662 | + |
|
663 | + $exists = $this->file_exists($path); |
|
664 | + $run = true; |
|
665 | + if ($this->shouldEmitHooks($path)) { |
|
666 | + $this->emit_file_hooks_pre($exists, $path, $run); |
|
667 | + } |
|
668 | + if (!$run) { |
|
669 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
670 | + return false; |
|
671 | + } |
|
672 | + |
|
673 | + try { |
|
674 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
675 | + } catch (\Exception $e) { |
|
676 | + // Release the shared lock before throwing. |
|
677 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
678 | + throw $e; |
|
679 | + } |
|
680 | + |
|
681 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
682 | + [$storage, $internalPath] = $this->resolvePath($path); |
|
683 | + $target = $storage->fopen($internalPath, 'w'); |
|
684 | + if ($target) { |
|
685 | + [, $result] = \OC_Helper::streamCopy($data, $target); |
|
686 | + fclose($target); |
|
687 | + fclose($data); |
|
688 | + |
|
689 | + $this->writeUpdate($storage, $internalPath); |
|
690 | + |
|
691 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
692 | + |
|
693 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
694 | + $this->emit_file_hooks_post($exists, $path); |
|
695 | + } |
|
696 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
697 | + return $result; |
|
698 | + } else { |
|
699 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
700 | + return false; |
|
701 | + } |
|
702 | + } else { |
|
703 | + return false; |
|
704 | + } |
|
705 | + } else { |
|
706 | + $hooks = $this->file_exists($path) ? ['update', 'write'] : ['create', 'write']; |
|
707 | + return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
708 | + } |
|
709 | + } |
|
710 | + |
|
711 | + /** |
|
712 | + * @param string $path |
|
713 | + * @return bool|mixed |
|
714 | + */ |
|
715 | + public function unlink($path) { |
|
716 | + if ($path === '' || $path === '/') { |
|
717 | + // do not allow deleting the root |
|
718 | + return false; |
|
719 | + } |
|
720 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
721 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
722 | + $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
723 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
724 | + return $this->removeMount($mount, $absolutePath); |
|
725 | + } |
|
726 | + if ($this->is_dir($path)) { |
|
727 | + $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
728 | + } else { |
|
729 | + $result = $this->basicOperation('unlink', $path, ['delete']); |
|
730 | + } |
|
731 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
732 | + $storage = $mount->getStorage(); |
|
733 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
734 | + $storage->getUpdater()->remove($internalPath); |
|
735 | + return true; |
|
736 | + } else { |
|
737 | + return $result; |
|
738 | + } |
|
739 | + } |
|
740 | + |
|
741 | + /** |
|
742 | + * @param string $directory |
|
743 | + * @return bool|mixed |
|
744 | + */ |
|
745 | + public function deleteAll($directory) { |
|
746 | + return $this->rmdir($directory); |
|
747 | + } |
|
748 | + |
|
749 | + /** |
|
750 | + * Rename/move a file or folder from the source path to target path. |
|
751 | + * |
|
752 | + * @param string $source source path |
|
753 | + * @param string $target target path |
|
754 | + * |
|
755 | + * @return bool|mixed |
|
756 | + * @throws LockedException |
|
757 | + */ |
|
758 | + public function rename($source, $target) { |
|
759 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source)); |
|
760 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target)); |
|
761 | + $result = false; |
|
762 | + if ( |
|
763 | + Filesystem::isValidPath($target) |
|
764 | + and Filesystem::isValidPath($source) |
|
765 | + and !Filesystem::isFileBlacklisted($target) |
|
766 | + ) { |
|
767 | + $source = $this->getRelativePath($absolutePath1); |
|
768 | + $target = $this->getRelativePath($absolutePath2); |
|
769 | + $exists = $this->file_exists($target); |
|
770 | + |
|
771 | + if ($source == null or $target == null) { |
|
772 | + return false; |
|
773 | + } |
|
774 | + |
|
775 | + $this->lockFile($source, ILockingProvider::LOCK_SHARED, true); |
|
776 | + try { |
|
777 | + $this->lockFile($target, ILockingProvider::LOCK_SHARED, true); |
|
778 | + |
|
779 | + $run = true; |
|
780 | + if ($this->shouldEmitHooks($source) && (Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target))) { |
|
781 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
782 | + $this->emit_file_hooks_pre($exists, $target, $run); |
|
783 | + } elseif ($this->shouldEmitHooks($source)) { |
|
784 | + \OC_Hook::emit( |
|
785 | + Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
786 | + [ |
|
787 | + Filesystem::signal_param_oldpath => $this->getHookPath($source), |
|
788 | + Filesystem::signal_param_newpath => $this->getHookPath($target), |
|
789 | + Filesystem::signal_param_run => &$run |
|
790 | + ] |
|
791 | + ); |
|
792 | + } |
|
793 | + if ($run) { |
|
794 | + $this->verifyPath(dirname($target), basename($target)); |
|
795 | + |
|
796 | + $manager = Filesystem::getMountManager(); |
|
797 | + $mount1 = $this->getMount($source); |
|
798 | + $mount2 = $this->getMount($target); |
|
799 | + $storage1 = $mount1->getStorage(); |
|
800 | + $storage2 = $mount2->getStorage(); |
|
801 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
802 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
803 | + |
|
804 | + $this->changeLock($source, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
805 | + try { |
|
806 | + $this->changeLock($target, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
807 | + |
|
808 | + if ($internalPath1 === '') { |
|
809 | + if ($mount1 instanceof MoveableMount) { |
|
810 | + $sourceParentMount = $this->getMount(dirname($source)); |
|
811 | + if ($sourceParentMount === $mount2 && $this->targetIsNotShared($storage2, $internalPath2)) { |
|
812 | + /** |
|
813 | + * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
814 | + */ |
|
815 | + $sourceMountPoint = $mount1->getMountPoint(); |
|
816 | + $result = $mount1->moveMount($absolutePath2); |
|
817 | + $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
818 | + } else { |
|
819 | + $result = false; |
|
820 | + } |
|
821 | + } else { |
|
822 | + $result = false; |
|
823 | + } |
|
824 | + // moving a file/folder within the same mount point |
|
825 | + } elseif ($storage1 === $storage2) { |
|
826 | + if ($storage1) { |
|
827 | + $result = $storage1->rename($internalPath1, $internalPath2); |
|
828 | + } else { |
|
829 | + $result = false; |
|
830 | + } |
|
831 | + // moving a file/folder between storages (from $storage1 to $storage2) |
|
832 | + } else { |
|
833 | + $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
834 | + } |
|
835 | + |
|
836 | + if ((Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target)) && $result !== false) { |
|
837 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
838 | + $this->writeUpdate($storage2, $internalPath2); |
|
839 | + } elseif ($result) { |
|
840 | + if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
841 | + $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
842 | + } |
|
843 | + } |
|
844 | + } catch (\Exception $e) { |
|
845 | + throw $e; |
|
846 | + } finally { |
|
847 | + $this->changeLock($source, ILockingProvider::LOCK_SHARED, true); |
|
848 | + $this->changeLock($target, ILockingProvider::LOCK_SHARED, true); |
|
849 | + } |
|
850 | + |
|
851 | + if ((Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target)) && $result !== false) { |
|
852 | + if ($this->shouldEmitHooks()) { |
|
853 | + $this->emit_file_hooks_post($exists, $target); |
|
854 | + } |
|
855 | + } elseif ($result) { |
|
856 | + if ($this->shouldEmitHooks($source) and $this->shouldEmitHooks($target)) { |
|
857 | + \OC_Hook::emit( |
|
858 | + Filesystem::CLASSNAME, |
|
859 | + Filesystem::signal_post_rename, |
|
860 | + [ |
|
861 | + Filesystem::signal_param_oldpath => $this->getHookPath($source), |
|
862 | + Filesystem::signal_param_newpath => $this->getHookPath($target) |
|
863 | + ] |
|
864 | + ); |
|
865 | + } |
|
866 | + } |
|
867 | + } |
|
868 | + } catch (\Exception $e) { |
|
869 | + throw $e; |
|
870 | + } finally { |
|
871 | + $this->unlockFile($source, ILockingProvider::LOCK_SHARED, true); |
|
872 | + $this->unlockFile($target, ILockingProvider::LOCK_SHARED, true); |
|
873 | + } |
|
874 | + } |
|
875 | + return $result; |
|
876 | + } |
|
877 | + |
|
878 | + /** |
|
879 | + * Copy a file/folder from the source path to target path |
|
880 | + * |
|
881 | + * @param string $source source path |
|
882 | + * @param string $target target path |
|
883 | + * @param bool $preserveMtime whether to preserve mtime on the copy |
|
884 | + * |
|
885 | + * @return bool|mixed |
|
886 | + */ |
|
887 | + public function copy($source, $target, $preserveMtime = false) { |
|
888 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source)); |
|
889 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target)); |
|
890 | + $result = false; |
|
891 | + if ( |
|
892 | + Filesystem::isValidPath($target) |
|
893 | + and Filesystem::isValidPath($source) |
|
894 | + and !Filesystem::isFileBlacklisted($target) |
|
895 | + ) { |
|
896 | + $source = $this->getRelativePath($absolutePath1); |
|
897 | + $target = $this->getRelativePath($absolutePath2); |
|
898 | + |
|
899 | + if ($source == null or $target == null) { |
|
900 | + return false; |
|
901 | + } |
|
902 | + $run = true; |
|
903 | + |
|
904 | + $this->lockFile($target, ILockingProvider::LOCK_SHARED); |
|
905 | + $this->lockFile($source, ILockingProvider::LOCK_SHARED); |
|
906 | + $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
907 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
908 | + |
|
909 | + try { |
|
910 | + $exists = $this->file_exists($target); |
|
911 | + if ($this->shouldEmitHooks()) { |
|
912 | + \OC_Hook::emit( |
|
913 | + Filesystem::CLASSNAME, |
|
914 | + Filesystem::signal_copy, |
|
915 | + [ |
|
916 | + Filesystem::signal_param_oldpath => $this->getHookPath($source), |
|
917 | + Filesystem::signal_param_newpath => $this->getHookPath($target), |
|
918 | + Filesystem::signal_param_run => &$run |
|
919 | + ] |
|
920 | + ); |
|
921 | + $this->emit_file_hooks_pre($exists, $target, $run); |
|
922 | + } |
|
923 | + if ($run) { |
|
924 | + $mount1 = $this->getMount($source); |
|
925 | + $mount2 = $this->getMount($target); |
|
926 | + $storage1 = $mount1->getStorage(); |
|
927 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
928 | + $storage2 = $mount2->getStorage(); |
|
929 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
930 | + |
|
931 | + $this->changeLock($target, ILockingProvider::LOCK_EXCLUSIVE); |
|
932 | + $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
933 | + |
|
934 | + if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
935 | + if ($storage1) { |
|
936 | + $result = $storage1->copy($internalPath1, $internalPath2); |
|
937 | + } else { |
|
938 | + $result = false; |
|
939 | + } |
|
940 | + } else { |
|
941 | + $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
942 | + } |
|
943 | + |
|
944 | + $this->writeUpdate($storage2, $internalPath2); |
|
945 | + |
|
946 | + $this->changeLock($target, ILockingProvider::LOCK_SHARED); |
|
947 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
948 | + |
|
949 | + if ($this->shouldEmitHooks() && $result !== false) { |
|
950 | + \OC_Hook::emit( |
|
951 | + Filesystem::CLASSNAME, |
|
952 | + Filesystem::signal_post_copy, |
|
953 | + [ |
|
954 | + Filesystem::signal_param_oldpath => $this->getHookPath($source), |
|
955 | + Filesystem::signal_param_newpath => $this->getHookPath($target) |
|
956 | + ] |
|
957 | + ); |
|
958 | + $this->emit_file_hooks_post($exists, $target); |
|
959 | + } |
|
960 | + } |
|
961 | + } catch (\Exception $e) { |
|
962 | + $this->unlockFile($target, $lockTypePath2); |
|
963 | + $this->unlockFile($source, $lockTypePath1); |
|
964 | + throw $e; |
|
965 | + } |
|
966 | + |
|
967 | + $this->unlockFile($target, $lockTypePath2); |
|
968 | + $this->unlockFile($source, $lockTypePath1); |
|
969 | + } |
|
970 | + return $result; |
|
971 | + } |
|
972 | + |
|
973 | + /** |
|
974 | + * @param string $path |
|
975 | + * @param string $mode 'r' or 'w' |
|
976 | + * @return resource |
|
977 | + * @throws LockedException |
|
978 | + */ |
|
979 | + public function fopen($path, $mode) { |
|
980 | + $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
981 | + $hooks = []; |
|
982 | + switch ($mode) { |
|
983 | + case 'r': |
|
984 | + $hooks[] = 'read'; |
|
985 | + break; |
|
986 | + case 'r+': |
|
987 | + case 'w+': |
|
988 | + case 'x+': |
|
989 | + case 'a+': |
|
990 | + $hooks[] = 'read'; |
|
991 | + $hooks[] = 'write'; |
|
992 | + break; |
|
993 | + case 'w': |
|
994 | + case 'x': |
|
995 | + case 'a': |
|
996 | + $hooks[] = 'write'; |
|
997 | + break; |
|
998 | + default: |
|
999 | + $this->logger->error('invalid mode (' . $mode . ') for ' . $path, ['app' => 'core']); |
|
1000 | + } |
|
1001 | + |
|
1002 | + if ($mode !== 'r' && $mode !== 'w') { |
|
1003 | + $this->logger->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends', ['app' => 'core']); |
|
1004 | + } |
|
1005 | + |
|
1006 | + $handle = $this->basicOperation('fopen', $path, $hooks, $mode); |
|
1007 | + if (!is_resource($handle) && $mode === 'r') { |
|
1008 | + // trying to read a file that isn't on disk, check if the cache is out of sync and rescan if needed |
|
1009 | + $mount = $this->getMount($path); |
|
1010 | + $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
1011 | + $storage = $mount->getStorage(); |
|
1012 | + if ($storage->getCache()->inCache($internalPath) && !$storage->file_exists($path)) { |
|
1013 | + $this->writeUpdate($storage, $internalPath); |
|
1014 | + } |
|
1015 | + } |
|
1016 | + return $handle; |
|
1017 | + } |
|
1018 | + |
|
1019 | + /** |
|
1020 | + * @param string $path |
|
1021 | + * @return bool|string |
|
1022 | + * @throws \OCP\Files\InvalidPathException |
|
1023 | + */ |
|
1024 | + public function toTmpFile($path) { |
|
1025 | + $this->assertPathLength($path); |
|
1026 | + if (Filesystem::isValidPath($path)) { |
|
1027 | + $source = $this->fopen($path, 'r'); |
|
1028 | + if ($source) { |
|
1029 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
1030 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
1031 | + file_put_contents($tmpFile, $source); |
|
1032 | + return $tmpFile; |
|
1033 | + } else { |
|
1034 | + return false; |
|
1035 | + } |
|
1036 | + } else { |
|
1037 | + return false; |
|
1038 | + } |
|
1039 | + } |
|
1040 | + |
|
1041 | + /** |
|
1042 | + * @param string $tmpFile |
|
1043 | + * @param string $path |
|
1044 | + * @return bool|mixed |
|
1045 | + * @throws \OCP\Files\InvalidPathException |
|
1046 | + */ |
|
1047 | + public function fromTmpFile($tmpFile, $path) { |
|
1048 | + $this->assertPathLength($path); |
|
1049 | + if (Filesystem::isValidPath($path)) { |
|
1050 | + // Get directory that the file is going into |
|
1051 | + $filePath = dirname($path); |
|
1052 | + |
|
1053 | + // Create the directories if any |
|
1054 | + if (!$this->file_exists($filePath)) { |
|
1055 | + $result = $this->createParentDirectories($filePath); |
|
1056 | + if ($result === false) { |
|
1057 | + return false; |
|
1058 | + } |
|
1059 | + } |
|
1060 | + |
|
1061 | + $source = fopen($tmpFile, 'r'); |
|
1062 | + if ($source) { |
|
1063 | + $result = $this->file_put_contents($path, $source); |
|
1064 | + // $this->file_put_contents() might have already closed |
|
1065 | + // the resource, so we check it, before trying to close it |
|
1066 | + // to avoid messages in the error log. |
|
1067 | + if (is_resource($source)) { |
|
1068 | + fclose($source); |
|
1069 | + } |
|
1070 | + unlink($tmpFile); |
|
1071 | + return $result; |
|
1072 | + } else { |
|
1073 | + return false; |
|
1074 | + } |
|
1075 | + } else { |
|
1076 | + return false; |
|
1077 | + } |
|
1078 | + } |
|
1079 | + |
|
1080 | + |
|
1081 | + /** |
|
1082 | + * @param string $path |
|
1083 | + * @return mixed |
|
1084 | + * @throws \OCP\Files\InvalidPathException |
|
1085 | + */ |
|
1086 | + public function getMimeType($path) { |
|
1087 | + $this->assertPathLength($path); |
|
1088 | + return $this->basicOperation('getMimeType', $path); |
|
1089 | + } |
|
1090 | + |
|
1091 | + /** |
|
1092 | + * @param string $type |
|
1093 | + * @param string $path |
|
1094 | + * @param bool $raw |
|
1095 | + * @return bool|string |
|
1096 | + */ |
|
1097 | + public function hash($type, $path, $raw = false) { |
|
1098 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1099 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1100 | + if (Filesystem::isValidPath($path)) { |
|
1101 | + $path = $this->getRelativePath($absolutePath); |
|
1102 | + if ($path == null) { |
|
1103 | + return false; |
|
1104 | + } |
|
1105 | + if ($this->shouldEmitHooks($path)) { |
|
1106 | + \OC_Hook::emit( |
|
1107 | + Filesystem::CLASSNAME, |
|
1108 | + Filesystem::signal_read, |
|
1109 | + [Filesystem::signal_param_path => $this->getHookPath($path)] |
|
1110 | + ); |
|
1111 | + } |
|
1112 | + /** @var Storage|null $storage */ |
|
1113 | + [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
1114 | + if ($storage) { |
|
1115 | + return $storage->hash($type, $internalPath, $raw); |
|
1116 | + } |
|
1117 | + } |
|
1118 | + return false; |
|
1119 | + } |
|
1120 | + |
|
1121 | + /** |
|
1122 | + * @param string $path |
|
1123 | + * @return mixed |
|
1124 | + * @throws \OCP\Files\InvalidPathException |
|
1125 | + */ |
|
1126 | + public function free_space($path = '/') { |
|
1127 | + $this->assertPathLength($path); |
|
1128 | + $result = $this->basicOperation('free_space', $path); |
|
1129 | + if ($result === null) { |
|
1130 | + throw new InvalidPathException(); |
|
1131 | + } |
|
1132 | + return $result; |
|
1133 | + } |
|
1134 | + |
|
1135 | + /** |
|
1136 | + * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
1137 | + * |
|
1138 | + * @param string $operation |
|
1139 | + * @param string $path |
|
1140 | + * @param array $hooks (optional) |
|
1141 | + * @param mixed $extraParam (optional) |
|
1142 | + * @return mixed |
|
1143 | + * @throws LockedException |
|
1144 | + * |
|
1145 | + * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
1146 | + * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
1147 | + * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
1148 | + */ |
|
1149 | + private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
1150 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1151 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1152 | + if (Filesystem::isValidPath($path) |
|
1153 | + and !Filesystem::isFileBlacklisted($path) |
|
1154 | + ) { |
|
1155 | + $path = $this->getRelativePath($absolutePath); |
|
1156 | + if ($path == null) { |
|
1157 | + return false; |
|
1158 | + } |
|
1159 | + |
|
1160 | + if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
1161 | + // always a shared lock during pre-hooks so the hook can read the file |
|
1162 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
1163 | + } |
|
1164 | + |
|
1165 | + $run = $this->runHooks($hooks, $path); |
|
1166 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
1167 | + [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
1168 | + if ($run and $storage) { |
|
1169 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1170 | + try { |
|
1171 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1172 | + } catch (LockedException $e) { |
|
1173 | + // release the shared lock we acquired before quitting |
|
1174 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1175 | + throw $e; |
|
1176 | + } |
|
1177 | + } |
|
1178 | + try { |
|
1179 | + if (!is_null($extraParam)) { |
|
1180 | + $result = $storage->$operation($internalPath, $extraParam); |
|
1181 | + } else { |
|
1182 | + $result = $storage->$operation($internalPath); |
|
1183 | + } |
|
1184 | + } catch (\Exception $e) { |
|
1185 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1186 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1187 | + } elseif (in_array('read', $hooks)) { |
|
1188 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1189 | + } |
|
1190 | + throw $e; |
|
1191 | + } |
|
1192 | + |
|
1193 | + if ($result !== false && in_array('delete', $hooks)) { |
|
1194 | + $this->removeUpdate($storage, $internalPath); |
|
1195 | + } |
|
1196 | + if ($result !== false && in_array('write', $hooks, true) && $operation !== 'fopen' && $operation !== 'touch') { |
|
1197 | + $this->writeUpdate($storage, $internalPath); |
|
1198 | + } |
|
1199 | + if ($result !== false && in_array('touch', $hooks)) { |
|
1200 | + $this->writeUpdate($storage, $internalPath, $extraParam); |
|
1201 | + } |
|
1202 | + |
|
1203 | + if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
1204 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
1205 | + } |
|
1206 | + |
|
1207 | + $unlockLater = false; |
|
1208 | + if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
1209 | + $unlockLater = true; |
|
1210 | + // make sure our unlocking callback will still be called if connection is aborted |
|
1211 | + ignore_user_abort(true); |
|
1212 | + $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
1213 | + if (in_array('write', $hooks)) { |
|
1214 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1215 | + } elseif (in_array('read', $hooks)) { |
|
1216 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1217 | + } |
|
1218 | + }); |
|
1219 | + } |
|
1220 | + |
|
1221 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
1222 | + if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
1223 | + $this->runHooks($hooks, $path, true); |
|
1224 | + } |
|
1225 | + } |
|
1226 | + |
|
1227 | + if (!$unlockLater |
|
1228 | + && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
1229 | + ) { |
|
1230 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1231 | + } |
|
1232 | + return $result; |
|
1233 | + } else { |
|
1234 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1235 | + } |
|
1236 | + } |
|
1237 | + return null; |
|
1238 | + } |
|
1239 | + |
|
1240 | + /** |
|
1241 | + * get the path relative to the default root for hook usage |
|
1242 | + * |
|
1243 | + * @param string $path |
|
1244 | + * @return string |
|
1245 | + */ |
|
1246 | + private function getHookPath($path) { |
|
1247 | + if (!Filesystem::getView()) { |
|
1248 | + return $path; |
|
1249 | + } |
|
1250 | + return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
1251 | + } |
|
1252 | + |
|
1253 | + private function shouldEmitHooks($path = '') { |
|
1254 | + if ($path && Cache\Scanner::isPartialFile($path)) { |
|
1255 | + return false; |
|
1256 | + } |
|
1257 | + if (!Filesystem::$loaded) { |
|
1258 | + return false; |
|
1259 | + } |
|
1260 | + $defaultRoot = Filesystem::getRoot(); |
|
1261 | + if ($defaultRoot === null) { |
|
1262 | + return false; |
|
1263 | + } |
|
1264 | + if ($this->fakeRoot === $defaultRoot) { |
|
1265 | + return true; |
|
1266 | + } |
|
1267 | + $fullPath = $this->getAbsolutePath($path); |
|
1268 | + |
|
1269 | + if ($fullPath === $defaultRoot) { |
|
1270 | + return true; |
|
1271 | + } |
|
1272 | + |
|
1273 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
1274 | + } |
|
1275 | + |
|
1276 | + /** |
|
1277 | + * @param string[] $hooks |
|
1278 | + * @param string $path |
|
1279 | + * @param bool $post |
|
1280 | + * @return bool |
|
1281 | + */ |
|
1282 | + private function runHooks($hooks, $path, $post = false) { |
|
1283 | + $relativePath = $path; |
|
1284 | + $path = $this->getHookPath($path); |
|
1285 | + $prefix = $post ? 'post_' : ''; |
|
1286 | + $run = true; |
|
1287 | + if ($this->shouldEmitHooks($relativePath)) { |
|
1288 | + foreach ($hooks as $hook) { |
|
1289 | + if ($hook != 'read') { |
|
1290 | + \OC_Hook::emit( |
|
1291 | + Filesystem::CLASSNAME, |
|
1292 | + $prefix . $hook, |
|
1293 | + [ |
|
1294 | + Filesystem::signal_param_run => &$run, |
|
1295 | + Filesystem::signal_param_path => $path |
|
1296 | + ] |
|
1297 | + ); |
|
1298 | + } elseif (!$post) { |
|
1299 | + \OC_Hook::emit( |
|
1300 | + Filesystem::CLASSNAME, |
|
1301 | + $prefix . $hook, |
|
1302 | + [ |
|
1303 | + Filesystem::signal_param_path => $path |
|
1304 | + ] |
|
1305 | + ); |
|
1306 | + } |
|
1307 | + } |
|
1308 | + } |
|
1309 | + return $run; |
|
1310 | + } |
|
1311 | + |
|
1312 | + /** |
|
1313 | + * check if a file or folder has been updated since $time |
|
1314 | + * |
|
1315 | + * @param string $path |
|
1316 | + * @param int $time |
|
1317 | + * @return bool |
|
1318 | + */ |
|
1319 | + public function hasUpdated($path, $time) { |
|
1320 | + return $this->basicOperation('hasUpdated', $path, [], $time); |
|
1321 | + } |
|
1322 | + |
|
1323 | + /** |
|
1324 | + * @param string $ownerId |
|
1325 | + * @return IUser |
|
1326 | + */ |
|
1327 | + private function getUserObjectForOwner(string $ownerId) { |
|
1328 | + return new LazyUser($ownerId, $this->userManager); |
|
1329 | + } |
|
1330 | + |
|
1331 | + /** |
|
1332 | + * Get file info from cache |
|
1333 | + * |
|
1334 | + * If the file is not in cached it will be scanned |
|
1335 | + * If the file has changed on storage the cache will be updated |
|
1336 | + * |
|
1337 | + * @param \OC\Files\Storage\Storage $storage |
|
1338 | + * @param string $internalPath |
|
1339 | + * @param string $relativePath |
|
1340 | + * @return ICacheEntry|bool |
|
1341 | + */ |
|
1342 | + private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
1343 | + $cache = $storage->getCache($internalPath); |
|
1344 | + $data = $cache->get($internalPath); |
|
1345 | + $watcher = $storage->getWatcher($internalPath); |
|
1346 | + |
|
1347 | + try { |
|
1348 | + // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
1349 | + if (!$data || (isset($data['size']) && $data['size'] === -1)) { |
|
1350 | + if (!$storage->file_exists($internalPath)) { |
|
1351 | + return false; |
|
1352 | + } |
|
1353 | + // don't need to get a lock here since the scanner does it's own locking |
|
1354 | + $scanner = $storage->getScanner($internalPath); |
|
1355 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1356 | + $data = $cache->get($internalPath); |
|
1357 | + } elseif (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
1358 | + $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1359 | + $watcher->update($internalPath, $data); |
|
1360 | + $storage->getPropagator()->propagateChange($internalPath, time()); |
|
1361 | + $data = $cache->get($internalPath); |
|
1362 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1363 | + } |
|
1364 | + } catch (LockedException $e) { |
|
1365 | + // if the file is locked we just use the old cache info |
|
1366 | + } |
|
1367 | + |
|
1368 | + return $data; |
|
1369 | + } |
|
1370 | + |
|
1371 | + /** |
|
1372 | + * get the filesystem info |
|
1373 | + * |
|
1374 | + * @param string $path |
|
1375 | + * @param bool|string $includeMountPoints true to add mountpoint sizes, |
|
1376 | + * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
1377 | + * @return \OC\Files\FileInfo|false False if file does not exist |
|
1378 | + */ |
|
1379 | + public function getFileInfo($path, $includeMountPoints = true) { |
|
1380 | + $this->assertPathLength($path); |
|
1381 | + if (!Filesystem::isValidPath($path)) { |
|
1382 | + return false; |
|
1383 | + } |
|
1384 | + if (Cache\Scanner::isPartialFile($path)) { |
|
1385 | + return $this->getPartFileInfo($path); |
|
1386 | + } |
|
1387 | + $relativePath = $path; |
|
1388 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1389 | + |
|
1390 | + $mount = Filesystem::getMountManager()->find($path); |
|
1391 | + $storage = $mount->getStorage(); |
|
1392 | + $internalPath = $mount->getInternalPath($path); |
|
1393 | + if ($storage) { |
|
1394 | + $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
1395 | + |
|
1396 | + if (!$data instanceof ICacheEntry) { |
|
1397 | + return false; |
|
1398 | + } |
|
1399 | + |
|
1400 | + if ($mount instanceof MoveableMount && $internalPath === '') { |
|
1401 | + $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
1402 | + } |
|
1403 | + $ownerId = $storage->getOwner($internalPath); |
|
1404 | + $owner = null; |
|
1405 | + if ($ownerId !== null && $ownerId !== false) { |
|
1406 | + // ownerId might be null if files are accessed with an access token without file system access |
|
1407 | + $owner = $this->getUserObjectForOwner($ownerId); |
|
1408 | + } |
|
1409 | + $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
1410 | + |
|
1411 | + if (isset($data['fileid'])) { |
|
1412 | + if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
1413 | + //add the sizes of other mount points to the folder |
|
1414 | + $extOnly = ($includeMountPoints === 'ext'); |
|
1415 | + $this->addSubMounts($info, $extOnly); |
|
1416 | + } |
|
1417 | + } |
|
1418 | + |
|
1419 | + return $info; |
|
1420 | + } else { |
|
1421 | + $this->logger->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint(), ['app' => 'core']); |
|
1422 | + } |
|
1423 | + |
|
1424 | + return false; |
|
1425 | + } |
|
1426 | + |
|
1427 | + /** |
|
1428 | + * Extend a FileInfo that was previously requested with `$includeMountPoints = false` to include the sub mounts |
|
1429 | + */ |
|
1430 | + public function addSubMounts(FileInfo $info, $extOnly = false): void { |
|
1431 | + $mounts = Filesystem::getMountManager()->findIn($info->getPath()); |
|
1432 | + $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
1433 | + $subStorage = $mount->getStorage(); |
|
1434 | + return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
1435 | + })); |
|
1436 | + } |
|
1437 | + |
|
1438 | + /** |
|
1439 | + * get the content of a directory |
|
1440 | + * |
|
1441 | + * @param string $directory path under datadirectory |
|
1442 | + * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
1443 | + * @return FileInfo[] |
|
1444 | + */ |
|
1445 | + public function getDirectoryContent($directory, $mimetype_filter = '', \OCP\Files\FileInfo $directoryInfo = null) { |
|
1446 | + $this->assertPathLength($directory); |
|
1447 | + if (!Filesystem::isValidPath($directory)) { |
|
1448 | + return []; |
|
1449 | + } |
|
1450 | + |
|
1451 | + $path = $this->getAbsolutePath($directory); |
|
1452 | + $path = Filesystem::normalizePath($path); |
|
1453 | + $mount = $this->getMount($directory); |
|
1454 | + $storage = $mount->getStorage(); |
|
1455 | + $internalPath = $mount->getInternalPath($path); |
|
1456 | + if (!$storage) { |
|
1457 | + return []; |
|
1458 | + } |
|
1459 | + |
|
1460 | + $cache = $storage->getCache($internalPath); |
|
1461 | + $user = \OC_User::getUser(); |
|
1462 | + |
|
1463 | + if (!$directoryInfo) { |
|
1464 | + $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
1465 | + if (!$data instanceof ICacheEntry || !isset($data['fileid'])) { |
|
1466 | + return []; |
|
1467 | + } |
|
1468 | + } else { |
|
1469 | + $data = $directoryInfo; |
|
1470 | + } |
|
1471 | + |
|
1472 | + if (!($data->getPermissions() & Constants::PERMISSION_READ)) { |
|
1473 | + return []; |
|
1474 | + } |
|
1475 | + |
|
1476 | + $folderId = $data->getId(); |
|
1477 | + $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
1478 | + |
|
1479 | + $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
1480 | + |
|
1481 | + $fileNames = array_map(function (ICacheEntry $content) { |
|
1482 | + return $content->getName(); |
|
1483 | + }, $contents); |
|
1484 | + /** |
|
1485 | + * @var \OC\Files\FileInfo[] $fileInfos |
|
1486 | + */ |
|
1487 | + $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
1488 | + if ($sharingDisabled) { |
|
1489 | + $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1490 | + } |
|
1491 | + $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
1492 | + return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
1493 | + }, $contents); |
|
1494 | + $files = array_combine($fileNames, $fileInfos); |
|
1495 | + |
|
1496 | + //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
1497 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
1498 | + $dirLength = strlen($path); |
|
1499 | + foreach ($mounts as $mount) { |
|
1500 | + $mountPoint = $mount->getMountPoint(); |
|
1501 | + $subStorage = $mount->getStorage(); |
|
1502 | + if ($subStorage) { |
|
1503 | + $subCache = $subStorage->getCache(''); |
|
1504 | + |
|
1505 | + $rootEntry = $subCache->get(''); |
|
1506 | + if (!$rootEntry) { |
|
1507 | + $subScanner = $subStorage->getScanner(); |
|
1508 | + try { |
|
1509 | + $subScanner->scanFile(''); |
|
1510 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
1511 | + continue; |
|
1512 | + } catch (\OCP\Files\StorageInvalidException $e) { |
|
1513 | + continue; |
|
1514 | + } catch (\Exception $e) { |
|
1515 | + // sometimes when the storage is not available it can be any exception |
|
1516 | + $this->logger->error('Exception while scanning storage "' . $subStorage->getId() . '"', [ |
|
1517 | + 'exception' => $e, |
|
1518 | + 'app' => 'core', |
|
1519 | + ]); |
|
1520 | + continue; |
|
1521 | + } |
|
1522 | + $rootEntry = $subCache->get(''); |
|
1523 | + } |
|
1524 | + |
|
1525 | + if ($rootEntry && ($rootEntry->getPermissions() & Constants::PERMISSION_READ)) { |
|
1526 | + $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
1527 | + if ($pos = strpos($relativePath, '/')) { |
|
1528 | + //mountpoint inside subfolder add size to the correct folder |
|
1529 | + $entryName = substr($relativePath, 0, $pos); |
|
1530 | + if (isset($files[$entryName])) { |
|
1531 | + $files[$entryName]->addSubEntry($rootEntry, $mountPoint); |
|
1532 | + } |
|
1533 | + } else { //mountpoint in this folder, add an entry for it |
|
1534 | + $rootEntry['name'] = $relativePath; |
|
1535 | + $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
1536 | + $permissions = $rootEntry['permissions']; |
|
1537 | + // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
1538 | + // for shared files/folders we use the permissions given by the owner |
|
1539 | + if ($mount instanceof MoveableMount) { |
|
1540 | + $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
1541 | + } else { |
|
1542 | + $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
1543 | + } |
|
1544 | + |
|
1545 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
1546 | + |
|
1547 | + // if sharing was disabled for the user we remove the share permissions |
|
1548 | + if (\OCP\Util::isSharingDisabledForUser()) { |
|
1549 | + $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1550 | + } |
|
1551 | + |
|
1552 | + $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
1553 | + $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
1554 | + } |
|
1555 | + } |
|
1556 | + } |
|
1557 | + } |
|
1558 | + |
|
1559 | + if ($mimetype_filter) { |
|
1560 | + $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
1561 | + if (strpos($mimetype_filter, '/')) { |
|
1562 | + return $file->getMimetype() === $mimetype_filter; |
|
1563 | + } else { |
|
1564 | + return $file->getMimePart() === $mimetype_filter; |
|
1565 | + } |
|
1566 | + }); |
|
1567 | + } |
|
1568 | + |
|
1569 | + return array_values($files); |
|
1570 | + } |
|
1571 | + |
|
1572 | + /** |
|
1573 | + * change file metadata |
|
1574 | + * |
|
1575 | + * @param string $path |
|
1576 | + * @param array|\OCP\Files\FileInfo $data |
|
1577 | + * @return int |
|
1578 | + * |
|
1579 | + * returns the fileid of the updated file |
|
1580 | + */ |
|
1581 | + public function putFileInfo($path, $data) { |
|
1582 | + $this->assertPathLength($path); |
|
1583 | + if ($data instanceof FileInfo) { |
|
1584 | + $data = $data->getData(); |
|
1585 | + } |
|
1586 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1587 | + /** |
|
1588 | + * @var \OC\Files\Storage\Storage $storage |
|
1589 | + * @var string $internalPath |
|
1590 | + */ |
|
1591 | + [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
1592 | + if ($storage) { |
|
1593 | + $cache = $storage->getCache($path); |
|
1594 | + |
|
1595 | + if (!$cache->inCache($internalPath)) { |
|
1596 | + $scanner = $storage->getScanner($internalPath); |
|
1597 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1598 | + } |
|
1599 | + |
|
1600 | + return $cache->put($internalPath, $data); |
|
1601 | + } else { |
|
1602 | + return -1; |
|
1603 | + } |
|
1604 | + } |
|
1605 | + |
|
1606 | + /** |
|
1607 | + * search for files with the name matching $query |
|
1608 | + * |
|
1609 | + * @param string $query |
|
1610 | + * @return FileInfo[] |
|
1611 | + */ |
|
1612 | + public function search($query) { |
|
1613 | + return $this->searchCommon('search', ['%' . $query . '%']); |
|
1614 | + } |
|
1615 | + |
|
1616 | + /** |
|
1617 | + * search for files with the name matching $query |
|
1618 | + * |
|
1619 | + * @param string $query |
|
1620 | + * @return FileInfo[] |
|
1621 | + */ |
|
1622 | + public function searchRaw($query) { |
|
1623 | + return $this->searchCommon('search', [$query]); |
|
1624 | + } |
|
1625 | + |
|
1626 | + /** |
|
1627 | + * search for files by mimetype |
|
1628 | + * |
|
1629 | + * @param string $mimetype |
|
1630 | + * @return FileInfo[] |
|
1631 | + */ |
|
1632 | + public function searchByMime($mimetype) { |
|
1633 | + return $this->searchCommon('searchByMime', [$mimetype]); |
|
1634 | + } |
|
1635 | + |
|
1636 | + /** |
|
1637 | + * search for files by tag |
|
1638 | + * |
|
1639 | + * @param string|int $tag name or tag id |
|
1640 | + * @param string $userId owner of the tags |
|
1641 | + * @return FileInfo[] |
|
1642 | + */ |
|
1643 | + public function searchByTag($tag, $userId) { |
|
1644 | + return $this->searchCommon('searchByTag', [$tag, $userId]); |
|
1645 | + } |
|
1646 | + |
|
1647 | + /** |
|
1648 | + * @param string $method cache method |
|
1649 | + * @param array $args |
|
1650 | + * @return FileInfo[] |
|
1651 | + */ |
|
1652 | + private function searchCommon($method, $args) { |
|
1653 | + $files = []; |
|
1654 | + $rootLength = strlen($this->fakeRoot); |
|
1655 | + |
|
1656 | + $mount = $this->getMount(''); |
|
1657 | + $mountPoint = $mount->getMountPoint(); |
|
1658 | + $storage = $mount->getStorage(); |
|
1659 | + $userManager = \OC::$server->getUserManager(); |
|
1660 | + if ($storage) { |
|
1661 | + $cache = $storage->getCache(''); |
|
1662 | + |
|
1663 | + $results = call_user_func_array([$cache, $method], $args); |
|
1664 | + foreach ($results as $result) { |
|
1665 | + if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
1666 | + $internalPath = $result['path']; |
|
1667 | + $path = $mountPoint . $result['path']; |
|
1668 | + $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
1669 | + $owner = $userManager->get($storage->getOwner($internalPath)); |
|
1670 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1671 | + } |
|
1672 | + } |
|
1673 | + |
|
1674 | + $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
1675 | + foreach ($mounts as $mount) { |
|
1676 | + $mountPoint = $mount->getMountPoint(); |
|
1677 | + $storage = $mount->getStorage(); |
|
1678 | + if ($storage) { |
|
1679 | + $cache = $storage->getCache(''); |
|
1680 | + |
|
1681 | + $relativeMountPoint = substr($mountPoint, $rootLength); |
|
1682 | + $results = call_user_func_array([$cache, $method], $args); |
|
1683 | + if ($results) { |
|
1684 | + foreach ($results as $result) { |
|
1685 | + $internalPath = $result['path']; |
|
1686 | + $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
1687 | + $path = rtrim($mountPoint . $internalPath, '/'); |
|
1688 | + $owner = $userManager->get($storage->getOwner($internalPath)); |
|
1689 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1690 | + } |
|
1691 | + } |
|
1692 | + } |
|
1693 | + } |
|
1694 | + } |
|
1695 | + return $files; |
|
1696 | + } |
|
1697 | + |
|
1698 | + /** |
|
1699 | + * Get the owner for a file or folder |
|
1700 | + * |
|
1701 | + * @param string $path |
|
1702 | + * @return string the user id of the owner |
|
1703 | + * @throws NotFoundException |
|
1704 | + */ |
|
1705 | + public function getOwner($path) { |
|
1706 | + $info = $this->getFileInfo($path); |
|
1707 | + if (!$info) { |
|
1708 | + throw new NotFoundException($path . ' not found while trying to get owner'); |
|
1709 | + } |
|
1710 | + |
|
1711 | + if ($info->getOwner() === null) { |
|
1712 | + throw new NotFoundException($path . ' has no owner'); |
|
1713 | + } |
|
1714 | + |
|
1715 | + return $info->getOwner()->getUID(); |
|
1716 | + } |
|
1717 | + |
|
1718 | + /** |
|
1719 | + * get the ETag for a file or folder |
|
1720 | + * |
|
1721 | + * @param string $path |
|
1722 | + * @return string |
|
1723 | + */ |
|
1724 | + public function getETag($path) { |
|
1725 | + /** |
|
1726 | + * @var Storage\Storage $storage |
|
1727 | + * @var string $internalPath |
|
1728 | + */ |
|
1729 | + [$storage, $internalPath] = $this->resolvePath($path); |
|
1730 | + if ($storage) { |
|
1731 | + return $storage->getETag($internalPath); |
|
1732 | + } else { |
|
1733 | + return null; |
|
1734 | + } |
|
1735 | + } |
|
1736 | + |
|
1737 | + /** |
|
1738 | + * Get the path of a file by id, relative to the view |
|
1739 | + * |
|
1740 | + * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
|
1741 | + * |
|
1742 | + * @param int $id |
|
1743 | + * @param int|null $storageId |
|
1744 | + * @return string |
|
1745 | + * @throws NotFoundException |
|
1746 | + */ |
|
1747 | + public function getPath($id, int $storageId = null) { |
|
1748 | + $id = (int)$id; |
|
1749 | + $manager = Filesystem::getMountManager(); |
|
1750 | + $mounts = $manager->findIn($this->fakeRoot); |
|
1751 | + $mounts[] = $manager->find($this->fakeRoot); |
|
1752 | + $mounts = array_filter($mounts); |
|
1753 | + // reverse the array, so we start with the storage this view is in |
|
1754 | + // which is the most likely to contain the file we're looking for |
|
1755 | + $mounts = array_reverse($mounts); |
|
1756 | + |
|
1757 | + // put non-shared mounts in front of the shared mount |
|
1758 | + // this prevents unneeded recursion into shares |
|
1759 | + usort($mounts, function (IMountPoint $a, IMountPoint $b) { |
|
1760 | + return $a instanceof SharedMount && (!$b instanceof SharedMount) ? 1 : -1; |
|
1761 | + }); |
|
1762 | + |
|
1763 | + if (!is_null($storageId)) { |
|
1764 | + $mounts = array_filter($mounts, function (IMountPoint $mount) use ($storageId) { |
|
1765 | + return $mount->getNumericStorageId() === $storageId; |
|
1766 | + }); |
|
1767 | + } |
|
1768 | + |
|
1769 | + foreach ($mounts as $mount) { |
|
1770 | + /** |
|
1771 | + * @var \OC\Files\Mount\MountPoint $mount |
|
1772 | + */ |
|
1773 | + if ($mount->getStorage()) { |
|
1774 | + $cache = $mount->getStorage()->getCache(); |
|
1775 | + $internalPath = $cache->getPathById($id); |
|
1776 | + if (is_string($internalPath)) { |
|
1777 | + $fullPath = $mount->getMountPoint() . $internalPath; |
|
1778 | + if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
1779 | + return $path; |
|
1780 | + } |
|
1781 | + } |
|
1782 | + } |
|
1783 | + } |
|
1784 | + throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
1785 | + } |
|
1786 | + |
|
1787 | + /** |
|
1788 | + * @param string $path |
|
1789 | + * @throws InvalidPathException |
|
1790 | + */ |
|
1791 | + private function assertPathLength($path) { |
|
1792 | + $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
1793 | + // Check for the string length - performed using isset() instead of strlen() |
|
1794 | + // because isset() is about 5x-40x faster. |
|
1795 | + if (isset($path[$maxLen])) { |
|
1796 | + $pathLen = strlen($path); |
|
1797 | + throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
1798 | + } |
|
1799 | + } |
|
1800 | + |
|
1801 | + /** |
|
1802 | + * check if it is allowed to move a mount point to a given target. |
|
1803 | + * It is not allowed to move a mount point into a different mount point or |
|
1804 | + * into an already shared folder |
|
1805 | + * |
|
1806 | + * @param IStorage $targetStorage |
|
1807 | + * @param string $targetInternalPath |
|
1808 | + * @return boolean |
|
1809 | + */ |
|
1810 | + private function targetIsNotShared(IStorage $targetStorage, string $targetInternalPath) { |
|
1811 | + // note: cannot use the view because the target is already locked |
|
1812 | + $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
1813 | + if ($fileId === -1) { |
|
1814 | + // target might not exist, need to check parent instead |
|
1815 | + $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
1816 | + } |
|
1817 | + |
|
1818 | + // check if any of the parents were shared by the current owner (include collections) |
|
1819 | + $shares = Share::getItemShared( |
|
1820 | + 'folder', |
|
1821 | + $fileId, |
|
1822 | + \OC\Share\Constants::FORMAT_NONE, |
|
1823 | + null, |
|
1824 | + true |
|
1825 | + ); |
|
1826 | + |
|
1827 | + if (count($shares) > 0) { |
|
1828 | + $this->logger->debug( |
|
1829 | + 'It is not allowed to move one mount point into a shared folder', |
|
1830 | + ['app' => 'files']); |
|
1831 | + return false; |
|
1832 | + } |
|
1833 | + |
|
1834 | + return true; |
|
1835 | + } |
|
1836 | + |
|
1837 | + /** |
|
1838 | + * Get a fileinfo object for files that are ignored in the cache (part files) |
|
1839 | + * |
|
1840 | + * @param string $path |
|
1841 | + * @return \OCP\Files\FileInfo |
|
1842 | + */ |
|
1843 | + private function getPartFileInfo($path) { |
|
1844 | + $mount = $this->getMount($path); |
|
1845 | + $storage = $mount->getStorage(); |
|
1846 | + $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
1847 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1848 | + return new FileInfo( |
|
1849 | + $this->getAbsolutePath($path), |
|
1850 | + $storage, |
|
1851 | + $internalPath, |
|
1852 | + [ |
|
1853 | + 'fileid' => null, |
|
1854 | + 'mimetype' => $storage->getMimeType($internalPath), |
|
1855 | + 'name' => basename($path), |
|
1856 | + 'etag' => null, |
|
1857 | + 'size' => $storage->filesize($internalPath), |
|
1858 | + 'mtime' => $storage->filemtime($internalPath), |
|
1859 | + 'encrypted' => false, |
|
1860 | + 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
1861 | + ], |
|
1862 | + $mount, |
|
1863 | + $owner |
|
1864 | + ); |
|
1865 | + } |
|
1866 | + |
|
1867 | + /** |
|
1868 | + * @param string $path |
|
1869 | + * @param string $fileName |
|
1870 | + * @throws InvalidPathException |
|
1871 | + */ |
|
1872 | + public function verifyPath($path, $fileName) { |
|
1873 | + try { |
|
1874 | + /** @type \OCP\Files\Storage $storage */ |
|
1875 | + [$storage, $internalPath] = $this->resolvePath($path); |
|
1876 | + $storage->verifyPath($internalPath, $fileName); |
|
1877 | + } catch (ReservedWordException $ex) { |
|
1878 | + $l = \OC::$server->getL10N('lib'); |
|
1879 | + throw new InvalidPathException($l->t('File name is a reserved word')); |
|
1880 | + } catch (InvalidCharacterInPathException $ex) { |
|
1881 | + $l = \OC::$server->getL10N('lib'); |
|
1882 | + throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
1883 | + } catch (FileNameTooLongException $ex) { |
|
1884 | + $l = \OC::$server->getL10N('lib'); |
|
1885 | + throw new InvalidPathException($l->t('File name is too long')); |
|
1886 | + } catch (InvalidDirectoryException $ex) { |
|
1887 | + $l = \OC::$server->getL10N('lib'); |
|
1888 | + throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
1889 | + } catch (EmptyFileNameException $ex) { |
|
1890 | + $l = \OC::$server->getL10N('lib'); |
|
1891 | + throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
1892 | + } |
|
1893 | + } |
|
1894 | + |
|
1895 | + /** |
|
1896 | + * get all parent folders of $path |
|
1897 | + * |
|
1898 | + * @param string $path |
|
1899 | + * @return string[] |
|
1900 | + */ |
|
1901 | + private function getParents($path) { |
|
1902 | + $path = trim($path, '/'); |
|
1903 | + if (!$path) { |
|
1904 | + return []; |
|
1905 | + } |
|
1906 | + |
|
1907 | + $parts = explode('/', $path); |
|
1908 | + |
|
1909 | + // remove the single file |
|
1910 | + array_pop($parts); |
|
1911 | + $result = ['/']; |
|
1912 | + $resultPath = ''; |
|
1913 | + foreach ($parts as $part) { |
|
1914 | + if ($part) { |
|
1915 | + $resultPath .= '/' . $part; |
|
1916 | + $result[] = $resultPath; |
|
1917 | + } |
|
1918 | + } |
|
1919 | + return $result; |
|
1920 | + } |
|
1921 | + |
|
1922 | + /** |
|
1923 | + * Returns the mount point for which to lock |
|
1924 | + * |
|
1925 | + * @param string $absolutePath absolute path |
|
1926 | + * @param bool $useParentMount true to return parent mount instead of whatever |
|
1927 | + * is mounted directly on the given path, false otherwise |
|
1928 | + * @return IMountPoint mount point for which to apply locks |
|
1929 | + */ |
|
1930 | + private function getMountForLock($absolutePath, $useParentMount = false) { |
|
1931 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
1932 | + |
|
1933 | + if ($useParentMount) { |
|
1934 | + // find out if something is mounted directly on the path |
|
1935 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
1936 | + if ($internalPath === '') { |
|
1937 | + // resolve the parent mount instead |
|
1938 | + $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
1939 | + } |
|
1940 | + } |
|
1941 | + |
|
1942 | + return $mount; |
|
1943 | + } |
|
1944 | + |
|
1945 | + /** |
|
1946 | + * Lock the given path |
|
1947 | + * |
|
1948 | + * @param string $path the path of the file to lock, relative to the view |
|
1949 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1950 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1951 | + * |
|
1952 | + * @return bool False if the path is excluded from locking, true otherwise |
|
1953 | + * @throws LockedException if the path is already locked |
|
1954 | + */ |
|
1955 | + private function lockPath($path, $type, $lockMountPoint = false) { |
|
1956 | + $absolutePath = $this->getAbsolutePath($path); |
|
1957 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1958 | + if (!$this->shouldLockFile($absolutePath)) { |
|
1959 | + return false; |
|
1960 | + } |
|
1961 | + |
|
1962 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
1963 | + if ($mount) { |
|
1964 | + try { |
|
1965 | + $storage = $mount->getStorage(); |
|
1966 | + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
1967 | + $storage->acquireLock( |
|
1968 | + $mount->getInternalPath($absolutePath), |
|
1969 | + $type, |
|
1970 | + $this->lockingProvider |
|
1971 | + ); |
|
1972 | + } |
|
1973 | + } catch (LockedException $e) { |
|
1974 | + // rethrow with the a human-readable path |
|
1975 | + throw new LockedException( |
|
1976 | + $this->getPathRelativeToFiles($absolutePath), |
|
1977 | + $e, |
|
1978 | + $e->getExistingLock() |
|
1979 | + ); |
|
1980 | + } |
|
1981 | + } |
|
1982 | + |
|
1983 | + return true; |
|
1984 | + } |
|
1985 | + |
|
1986 | + /** |
|
1987 | + * Change the lock type |
|
1988 | + * |
|
1989 | + * @param string $path the path of the file to lock, relative to the view |
|
1990 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1991 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1992 | + * |
|
1993 | + * @return bool False if the path is excluded from locking, true otherwise |
|
1994 | + * @throws LockedException if the path is already locked |
|
1995 | + */ |
|
1996 | + public function changeLock($path, $type, $lockMountPoint = false) { |
|
1997 | + $path = Filesystem::normalizePath($path); |
|
1998 | + $absolutePath = $this->getAbsolutePath($path); |
|
1999 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2000 | + if (!$this->shouldLockFile($absolutePath)) { |
|
2001 | + return false; |
|
2002 | + } |
|
2003 | + |
|
2004 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
2005 | + if ($mount) { |
|
2006 | + try { |
|
2007 | + $storage = $mount->getStorage(); |
|
2008 | + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
2009 | + $storage->changeLock( |
|
2010 | + $mount->getInternalPath($absolutePath), |
|
2011 | + $type, |
|
2012 | + $this->lockingProvider |
|
2013 | + ); |
|
2014 | + } |
|
2015 | + } catch (LockedException $e) { |
|
2016 | + try { |
|
2017 | + // rethrow with the a human-readable path |
|
2018 | + throw new LockedException( |
|
2019 | + $this->getPathRelativeToFiles($absolutePath), |
|
2020 | + $e, |
|
2021 | + $e->getExistingLock() |
|
2022 | + ); |
|
2023 | + } catch (\InvalidArgumentException $ex) { |
|
2024 | + throw new LockedException( |
|
2025 | + $absolutePath, |
|
2026 | + $ex, |
|
2027 | + $e->getExistingLock() |
|
2028 | + ); |
|
2029 | + } |
|
2030 | + } |
|
2031 | + } |
|
2032 | + |
|
2033 | + return true; |
|
2034 | + } |
|
2035 | + |
|
2036 | + /** |
|
2037 | + * Unlock the given path |
|
2038 | + * |
|
2039 | + * @param string $path the path of the file to unlock, relative to the view |
|
2040 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2041 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2042 | + * |
|
2043 | + * @return bool False if the path is excluded from locking, true otherwise |
|
2044 | + * @throws LockedException |
|
2045 | + */ |
|
2046 | + private function unlockPath($path, $type, $lockMountPoint = false) { |
|
2047 | + $absolutePath = $this->getAbsolutePath($path); |
|
2048 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2049 | + if (!$this->shouldLockFile($absolutePath)) { |
|
2050 | + return false; |
|
2051 | + } |
|
2052 | + |
|
2053 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
2054 | + if ($mount) { |
|
2055 | + $storage = $mount->getStorage(); |
|
2056 | + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
2057 | + $storage->releaseLock( |
|
2058 | + $mount->getInternalPath($absolutePath), |
|
2059 | + $type, |
|
2060 | + $this->lockingProvider |
|
2061 | + ); |
|
2062 | + } |
|
2063 | + } |
|
2064 | + |
|
2065 | + return true; |
|
2066 | + } |
|
2067 | + |
|
2068 | + /** |
|
2069 | + * Lock a path and all its parents up to the root of the view |
|
2070 | + * |
|
2071 | + * @param string $path the path of the file to lock relative to the view |
|
2072 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2073 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2074 | + * |
|
2075 | + * @return bool False if the path is excluded from locking, true otherwise |
|
2076 | + * @throws LockedException |
|
2077 | + */ |
|
2078 | + public function lockFile($path, $type, $lockMountPoint = false) { |
|
2079 | + $absolutePath = $this->getAbsolutePath($path); |
|
2080 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2081 | + if (!$this->shouldLockFile($absolutePath)) { |
|
2082 | + return false; |
|
2083 | + } |
|
2084 | + |
|
2085 | + $this->lockPath($path, $type, $lockMountPoint); |
|
2086 | + |
|
2087 | + $parents = $this->getParents($path); |
|
2088 | + foreach ($parents as $parent) { |
|
2089 | + $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2090 | + } |
|
2091 | + |
|
2092 | + return true; |
|
2093 | + } |
|
2094 | + |
|
2095 | + /** |
|
2096 | + * Unlock a path and all its parents up to the root of the view |
|
2097 | + * |
|
2098 | + * @param string $path the path of the file to lock relative to the view |
|
2099 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2100 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2101 | + * |
|
2102 | + * @return bool False if the path is excluded from locking, true otherwise |
|
2103 | + * @throws LockedException |
|
2104 | + */ |
|
2105 | + public function unlockFile($path, $type, $lockMountPoint = false) { |
|
2106 | + $absolutePath = $this->getAbsolutePath($path); |
|
2107 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2108 | + if (!$this->shouldLockFile($absolutePath)) { |
|
2109 | + return false; |
|
2110 | + } |
|
2111 | + |
|
2112 | + $this->unlockPath($path, $type, $lockMountPoint); |
|
2113 | + |
|
2114 | + $parents = $this->getParents($path); |
|
2115 | + foreach ($parents as $parent) { |
|
2116 | + $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2117 | + } |
|
2118 | + |
|
2119 | + return true; |
|
2120 | + } |
|
2121 | + |
|
2122 | + /** |
|
2123 | + * Only lock files in data/user/files/ |
|
2124 | + * |
|
2125 | + * @param string $path Absolute path to the file/folder we try to (un)lock |
|
2126 | + * @return bool |
|
2127 | + */ |
|
2128 | + protected function shouldLockFile($path) { |
|
2129 | + $path = Filesystem::normalizePath($path); |
|
2130 | + |
|
2131 | + $pathSegments = explode('/', $path); |
|
2132 | + if (isset($pathSegments[2])) { |
|
2133 | + // E.g.: /username/files/path-to-file |
|
2134 | + return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
2135 | + } |
|
2136 | + |
|
2137 | + return strpos($path, '/appdata_') !== 0; |
|
2138 | + } |
|
2139 | + |
|
2140 | + /** |
|
2141 | + * Shortens the given absolute path to be relative to |
|
2142 | + * "$user/files". |
|
2143 | + * |
|
2144 | + * @param string $absolutePath absolute path which is under "files" |
|
2145 | + * |
|
2146 | + * @return string path relative to "files" with trimmed slashes or null |
|
2147 | + * if the path was NOT relative to files |
|
2148 | + * |
|
2149 | + * @throws \InvalidArgumentException if the given path was not under "files" |
|
2150 | + * @since 8.1.0 |
|
2151 | + */ |
|
2152 | + public function getPathRelativeToFiles($absolutePath) { |
|
2153 | + $path = Filesystem::normalizePath($absolutePath); |
|
2154 | + $parts = explode('/', trim($path, '/'), 3); |
|
2155 | + // "$user", "files", "path/to/dir" |
|
2156 | + if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
2157 | + $this->logger->error( |
|
2158 | + '$absolutePath must be relative to "files", value is "{absolutePath}"', |
|
2159 | + [ |
|
2160 | + 'absolutePath' => $absolutePath, |
|
2161 | + ] |
|
2162 | + ); |
|
2163 | + throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
2164 | + } |
|
2165 | + if (isset($parts[2])) { |
|
2166 | + return $parts[2]; |
|
2167 | + } |
|
2168 | + return ''; |
|
2169 | + } |
|
2170 | + |
|
2171 | + /** |
|
2172 | + * @param string $filename |
|
2173 | + * @return array |
|
2174 | + * @throws \OC\User\NoUserException |
|
2175 | + * @throws NotFoundException |
|
2176 | + */ |
|
2177 | + public function getUidAndFilename($filename) { |
|
2178 | + $info = $this->getFileInfo($filename); |
|
2179 | + if (!$info instanceof \OCP\Files\FileInfo) { |
|
2180 | + throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
2181 | + } |
|
2182 | + $uid = $info->getOwner()->getUID(); |
|
2183 | + if ($uid != \OC_User::getUser()) { |
|
2184 | + Filesystem::initMountPoints($uid); |
|
2185 | + $ownerView = new View('/' . $uid . '/files'); |
|
2186 | + try { |
|
2187 | + $filename = $ownerView->getPath($info['fileid']); |
|
2188 | + } catch (NotFoundException $e) { |
|
2189 | + throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
2190 | + } |
|
2191 | + } |
|
2192 | + return [$uid, $filename]; |
|
2193 | + } |
|
2194 | + |
|
2195 | + /** |
|
2196 | + * Creates parent non-existing folders |
|
2197 | + * |
|
2198 | + * @param string $filePath |
|
2199 | + * @return bool |
|
2200 | + */ |
|
2201 | + private function createParentDirectories($filePath) { |
|
2202 | + $directoryParts = explode('/', $filePath); |
|
2203 | + $directoryParts = array_filter($directoryParts); |
|
2204 | + foreach ($directoryParts as $key => $part) { |
|
2205 | + $currentPathElements = array_slice($directoryParts, 0, $key); |
|
2206 | + $currentPath = '/' . implode('/', $currentPathElements); |
|
2207 | + if ($this->is_file($currentPath)) { |
|
2208 | + return false; |
|
2209 | + } |
|
2210 | + if (!$this->file_exists($currentPath)) { |
|
2211 | + $this->mkdir($currentPath); |
|
2212 | + } |
|
2213 | + } |
|
2214 | + |
|
2215 | + return true; |
|
2216 | + } |
|
2217 | 2217 | } |
@@ -131,9 +131,9 @@ discard block |
||
131 | 131 | $path = '/'; |
132 | 132 | } |
133 | 133 | if ($path[0] !== '/') { |
134 | - $path = '/' . $path; |
|
134 | + $path = '/'.$path; |
|
135 | 135 | } |
136 | - return $this->fakeRoot . $path; |
|
136 | + return $this->fakeRoot.$path; |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | /** |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | public function chroot($fakeRoot) { |
146 | 146 | if (!$fakeRoot == '') { |
147 | 147 | if ($fakeRoot[0] !== '/') { |
148 | - $fakeRoot = '/' . $fakeRoot; |
|
148 | + $fakeRoot = '/'.$fakeRoot; |
|
149 | 149 | } |
150 | 150 | } |
151 | 151 | $this->fakeRoot = $fakeRoot; |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | } |
178 | 178 | |
179 | 179 | // missing slashes can cause wrong matches! |
180 | - $root = rtrim($this->fakeRoot, '/') . '/'; |
|
180 | + $root = rtrim($this->fakeRoot, '/').'/'; |
|
181 | 181 | |
182 | 182 | if (strpos($path, $root) !== 0) { |
183 | 183 | return null; |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | if ($mount instanceof MoveableMount) { |
284 | 284 | // cut of /user/files to get the relative path to data/user/files |
285 | 285 | $pathParts = explode('/', $path, 4); |
286 | - $relPath = '/' . $pathParts[3]; |
|
286 | + $relPath = '/'.$pathParts[3]; |
|
287 | 287 | $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
288 | 288 | \OC_Hook::emit( |
289 | 289 | Filesystem::CLASSNAME, "umount", |
@@ -719,7 +719,7 @@ discard block |
||
719 | 719 | } |
720 | 720 | $postFix = (substr($path, -1) === '/') ? '/' : ''; |
721 | 721 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
722 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
722 | + $mount = Filesystem::getMountManager()->find($absolutePath.$postFix); |
|
723 | 723 | if ($mount->getInternalPath($absolutePath) === '') { |
724 | 724 | return $this->removeMount($mount, $absolutePath); |
725 | 725 | } |
@@ -996,7 +996,7 @@ discard block |
||
996 | 996 | $hooks[] = 'write'; |
997 | 997 | break; |
998 | 998 | default: |
999 | - $this->logger->error('invalid mode (' . $mode . ') for ' . $path, ['app' => 'core']); |
|
999 | + $this->logger->error('invalid mode ('.$mode.') for '.$path, ['app' => 'core']); |
|
1000 | 1000 | } |
1001 | 1001 | |
1002 | 1002 | if ($mode !== 'r' && $mode !== 'w') { |
@@ -1110,7 +1110,7 @@ discard block |
||
1110 | 1110 | ); |
1111 | 1111 | } |
1112 | 1112 | /** @var Storage|null $storage */ |
1113 | - [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
1113 | + [$storage, $internalPath] = Filesystem::resolvePath($absolutePath.$postFix); |
|
1114 | 1114 | if ($storage) { |
1115 | 1115 | return $storage->hash($type, $internalPath, $raw); |
1116 | 1116 | } |
@@ -1164,7 +1164,7 @@ discard block |
||
1164 | 1164 | |
1165 | 1165 | $run = $this->runHooks($hooks, $path); |
1166 | 1166 | /** @var \OC\Files\Storage\Storage $storage */ |
1167 | - [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
1167 | + [$storage, $internalPath] = Filesystem::resolvePath($absolutePath.$postFix); |
|
1168 | 1168 | if ($run and $storage) { |
1169 | 1169 | if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
1170 | 1170 | try { |
@@ -1209,7 +1209,7 @@ discard block |
||
1209 | 1209 | $unlockLater = true; |
1210 | 1210 | // make sure our unlocking callback will still be called if connection is aborted |
1211 | 1211 | ignore_user_abort(true); |
1212 | - $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
1212 | + $result = CallbackWrapper::wrap($result, null, null, function() use ($hooks, $path) { |
|
1213 | 1213 | if (in_array('write', $hooks)) { |
1214 | 1214 | $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
1215 | 1215 | } elseif (in_array('read', $hooks)) { |
@@ -1270,7 +1270,7 @@ discard block |
||
1270 | 1270 | return true; |
1271 | 1271 | } |
1272 | 1272 | |
1273 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
1273 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot.'/'); |
|
1274 | 1274 | } |
1275 | 1275 | |
1276 | 1276 | /** |
@@ -1289,7 +1289,7 @@ discard block |
||
1289 | 1289 | if ($hook != 'read') { |
1290 | 1290 | \OC_Hook::emit( |
1291 | 1291 | Filesystem::CLASSNAME, |
1292 | - $prefix . $hook, |
|
1292 | + $prefix.$hook, |
|
1293 | 1293 | [ |
1294 | 1294 | Filesystem::signal_param_run => &$run, |
1295 | 1295 | Filesystem::signal_param_path => $path |
@@ -1298,7 +1298,7 @@ discard block |
||
1298 | 1298 | } elseif (!$post) { |
1299 | 1299 | \OC_Hook::emit( |
1300 | 1300 | Filesystem::CLASSNAME, |
1301 | - $prefix . $hook, |
|
1301 | + $prefix.$hook, |
|
1302 | 1302 | [ |
1303 | 1303 | Filesystem::signal_param_path => $path |
1304 | 1304 | ] |
@@ -1385,7 +1385,7 @@ discard block |
||
1385 | 1385 | return $this->getPartFileInfo($path); |
1386 | 1386 | } |
1387 | 1387 | $relativePath = $path; |
1388 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1388 | + $path = Filesystem::normalizePath($this->fakeRoot.'/'.$path); |
|
1389 | 1389 | |
1390 | 1390 | $mount = Filesystem::getMountManager()->find($path); |
1391 | 1391 | $storage = $mount->getStorage(); |
@@ -1418,7 +1418,7 @@ discard block |
||
1418 | 1418 | |
1419 | 1419 | return $info; |
1420 | 1420 | } else { |
1421 | - $this->logger->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint(), ['app' => 'core']); |
|
1421 | + $this->logger->warning('Storage not valid for mountpoint: '.$mount->getMountPoint(), ['app' => 'core']); |
|
1422 | 1422 | } |
1423 | 1423 | |
1424 | 1424 | return false; |
@@ -1429,7 +1429,7 @@ discard block |
||
1429 | 1429 | */ |
1430 | 1430 | public function addSubMounts(FileInfo $info, $extOnly = false): void { |
1431 | 1431 | $mounts = Filesystem::getMountManager()->findIn($info->getPath()); |
1432 | - $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
1432 | + $info->setSubMounts(array_filter($mounts, function(IMountPoint $mount) use ($extOnly) { |
|
1433 | 1433 | $subStorage = $mount->getStorage(); |
1434 | 1434 | return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
1435 | 1435 | })); |
@@ -1478,18 +1478,18 @@ discard block |
||
1478 | 1478 | |
1479 | 1479 | $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
1480 | 1480 | |
1481 | - $fileNames = array_map(function (ICacheEntry $content) { |
|
1481 | + $fileNames = array_map(function(ICacheEntry $content) { |
|
1482 | 1482 | return $content->getName(); |
1483 | 1483 | }, $contents); |
1484 | 1484 | /** |
1485 | 1485 | * @var \OC\Files\FileInfo[] $fileInfos |
1486 | 1486 | */ |
1487 | - $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
1487 | + $fileInfos = array_map(function(ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
1488 | 1488 | if ($sharingDisabled) { |
1489 | 1489 | $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
1490 | 1490 | } |
1491 | 1491 | $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
1492 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
1492 | + return new FileInfo($path.'/'.$content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
1493 | 1493 | }, $contents); |
1494 | 1494 | $files = array_combine($fileNames, $fileInfos); |
1495 | 1495 | |
@@ -1513,7 +1513,7 @@ discard block |
||
1513 | 1513 | continue; |
1514 | 1514 | } catch (\Exception $e) { |
1515 | 1515 | // sometimes when the storage is not available it can be any exception |
1516 | - $this->logger->error('Exception while scanning storage "' . $subStorage->getId() . '"', [ |
|
1516 | + $this->logger->error('Exception while scanning storage "'.$subStorage->getId().'"', [ |
|
1517 | 1517 | 'exception' => $e, |
1518 | 1518 | 'app' => 'core', |
1519 | 1519 | ]); |
@@ -1542,7 +1542,7 @@ discard block |
||
1542 | 1542 | $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
1543 | 1543 | } |
1544 | 1544 | |
1545 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
1545 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path.'/'.$rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
1546 | 1546 | |
1547 | 1547 | // if sharing was disabled for the user we remove the share permissions |
1548 | 1548 | if (\OCP\Util::isSharingDisabledForUser()) { |
@@ -1550,14 +1550,14 @@ discard block |
||
1550 | 1550 | } |
1551 | 1551 | |
1552 | 1552 | $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
1553 | - $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
1553 | + $files[$rootEntry->getName()] = new FileInfo($path.'/'.$rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
1554 | 1554 | } |
1555 | 1555 | } |
1556 | 1556 | } |
1557 | 1557 | } |
1558 | 1558 | |
1559 | 1559 | if ($mimetype_filter) { |
1560 | - $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
1560 | + $files = array_filter($files, function(FileInfo $file) use ($mimetype_filter) { |
|
1561 | 1561 | if (strpos($mimetype_filter, '/')) { |
1562 | 1562 | return $file->getMimetype() === $mimetype_filter; |
1563 | 1563 | } else { |
@@ -1583,7 +1583,7 @@ discard block |
||
1583 | 1583 | if ($data instanceof FileInfo) { |
1584 | 1584 | $data = $data->getData(); |
1585 | 1585 | } |
1586 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1586 | + $path = Filesystem::normalizePath($this->fakeRoot.'/'.$path); |
|
1587 | 1587 | /** |
1588 | 1588 | * @var \OC\Files\Storage\Storage $storage |
1589 | 1589 | * @var string $internalPath |
@@ -1610,7 +1610,7 @@ discard block |
||
1610 | 1610 | * @return FileInfo[] |
1611 | 1611 | */ |
1612 | 1612 | public function search($query) { |
1613 | - return $this->searchCommon('search', ['%' . $query . '%']); |
|
1613 | + return $this->searchCommon('search', ['%'.$query.'%']); |
|
1614 | 1614 | } |
1615 | 1615 | |
1616 | 1616 | /** |
@@ -1662,10 +1662,10 @@ discard block |
||
1662 | 1662 | |
1663 | 1663 | $results = call_user_func_array([$cache, $method], $args); |
1664 | 1664 | foreach ($results as $result) { |
1665 | - if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
1665 | + if (substr($mountPoint.$result['path'], 0, $rootLength + 1) === $this->fakeRoot.'/') { |
|
1666 | 1666 | $internalPath = $result['path']; |
1667 | - $path = $mountPoint . $result['path']; |
|
1668 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
1667 | + $path = $mountPoint.$result['path']; |
|
1668 | + $result['path'] = substr($mountPoint.$result['path'], $rootLength); |
|
1669 | 1669 | $owner = $userManager->get($storage->getOwner($internalPath)); |
1670 | 1670 | $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
1671 | 1671 | } |
@@ -1683,8 +1683,8 @@ discard block |
||
1683 | 1683 | if ($results) { |
1684 | 1684 | foreach ($results as $result) { |
1685 | 1685 | $internalPath = $result['path']; |
1686 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
1687 | - $path = rtrim($mountPoint . $internalPath, '/'); |
|
1686 | + $result['path'] = rtrim($relativeMountPoint.$result['path'], '/'); |
|
1687 | + $path = rtrim($mountPoint.$internalPath, '/'); |
|
1688 | 1688 | $owner = $userManager->get($storage->getOwner($internalPath)); |
1689 | 1689 | $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
1690 | 1690 | } |
@@ -1705,11 +1705,11 @@ discard block |
||
1705 | 1705 | public function getOwner($path) { |
1706 | 1706 | $info = $this->getFileInfo($path); |
1707 | 1707 | if (!$info) { |
1708 | - throw new NotFoundException($path . ' not found while trying to get owner'); |
|
1708 | + throw new NotFoundException($path.' not found while trying to get owner'); |
|
1709 | 1709 | } |
1710 | 1710 | |
1711 | 1711 | if ($info->getOwner() === null) { |
1712 | - throw new NotFoundException($path . ' has no owner'); |
|
1712 | + throw new NotFoundException($path.' has no owner'); |
|
1713 | 1713 | } |
1714 | 1714 | |
1715 | 1715 | return $info->getOwner()->getUID(); |
@@ -1745,7 +1745,7 @@ discard block |
||
1745 | 1745 | * @throws NotFoundException |
1746 | 1746 | */ |
1747 | 1747 | public function getPath($id, int $storageId = null) { |
1748 | - $id = (int)$id; |
|
1748 | + $id = (int) $id; |
|
1749 | 1749 | $manager = Filesystem::getMountManager(); |
1750 | 1750 | $mounts = $manager->findIn($this->fakeRoot); |
1751 | 1751 | $mounts[] = $manager->find($this->fakeRoot); |
@@ -1756,12 +1756,12 @@ discard block |
||
1756 | 1756 | |
1757 | 1757 | // put non-shared mounts in front of the shared mount |
1758 | 1758 | // this prevents unneeded recursion into shares |
1759 | - usort($mounts, function (IMountPoint $a, IMountPoint $b) { |
|
1759 | + usort($mounts, function(IMountPoint $a, IMountPoint $b) { |
|
1760 | 1760 | return $a instanceof SharedMount && (!$b instanceof SharedMount) ? 1 : -1; |
1761 | 1761 | }); |
1762 | 1762 | |
1763 | 1763 | if (!is_null($storageId)) { |
1764 | - $mounts = array_filter($mounts, function (IMountPoint $mount) use ($storageId) { |
|
1764 | + $mounts = array_filter($mounts, function(IMountPoint $mount) use ($storageId) { |
|
1765 | 1765 | return $mount->getNumericStorageId() === $storageId; |
1766 | 1766 | }); |
1767 | 1767 | } |
@@ -1774,7 +1774,7 @@ discard block |
||
1774 | 1774 | $cache = $mount->getStorage()->getCache(); |
1775 | 1775 | $internalPath = $cache->getPathById($id); |
1776 | 1776 | if (is_string($internalPath)) { |
1777 | - $fullPath = $mount->getMountPoint() . $internalPath; |
|
1777 | + $fullPath = $mount->getMountPoint().$internalPath; |
|
1778 | 1778 | if (!is_null($path = $this->getRelativePath($fullPath))) { |
1779 | 1779 | return $path; |
1780 | 1780 | } |
@@ -1809,10 +1809,10 @@ discard block |
||
1809 | 1809 | */ |
1810 | 1810 | private function targetIsNotShared(IStorage $targetStorage, string $targetInternalPath) { |
1811 | 1811 | // note: cannot use the view because the target is already locked |
1812 | - $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
1812 | + $fileId = (int) $targetStorage->getCache()->getId($targetInternalPath); |
|
1813 | 1813 | if ($fileId === -1) { |
1814 | 1814 | // target might not exist, need to check parent instead |
1815 | - $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
1815 | + $fileId = (int) $targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
1816 | 1816 | } |
1817 | 1817 | |
1818 | 1818 | // check if any of the parents were shared by the current owner (include collections) |
@@ -1912,7 +1912,7 @@ discard block |
||
1912 | 1912 | $resultPath = ''; |
1913 | 1913 | foreach ($parts as $part) { |
1914 | 1914 | if ($part) { |
1915 | - $resultPath .= '/' . $part; |
|
1915 | + $resultPath .= '/'.$part; |
|
1916 | 1916 | $result[] = $resultPath; |
1917 | 1917 | } |
1918 | 1918 | } |
@@ -2177,16 +2177,16 @@ discard block |
||
2177 | 2177 | public function getUidAndFilename($filename) { |
2178 | 2178 | $info = $this->getFileInfo($filename); |
2179 | 2179 | if (!$info instanceof \OCP\Files\FileInfo) { |
2180 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
2180 | + throw new NotFoundException($this->getAbsolutePath($filename).' not found'); |
|
2181 | 2181 | } |
2182 | 2182 | $uid = $info->getOwner()->getUID(); |
2183 | 2183 | if ($uid != \OC_User::getUser()) { |
2184 | 2184 | Filesystem::initMountPoints($uid); |
2185 | - $ownerView = new View('/' . $uid . '/files'); |
|
2185 | + $ownerView = new View('/'.$uid.'/files'); |
|
2186 | 2186 | try { |
2187 | 2187 | $filename = $ownerView->getPath($info['fileid']); |
2188 | 2188 | } catch (NotFoundException $e) { |
2189 | - throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
2189 | + throw new NotFoundException('File with id '.$info['fileid'].' not found for user '.$uid); |
|
2190 | 2190 | } |
2191 | 2191 | } |
2192 | 2192 | return [$uid, $filename]; |
@@ -2203,7 +2203,7 @@ discard block |
||
2203 | 2203 | $directoryParts = array_filter($directoryParts); |
2204 | 2204 | foreach ($directoryParts as $key => $part) { |
2205 | 2205 | $currentPathElements = array_slice($directoryParts, 0, $key); |
2206 | - $currentPath = '/' . implode('/', $currentPathElements); |
|
2206 | + $currentPath = '/'.implode('/', $currentPathElements); |
|
2207 | 2207 | if ($this->is_file($currentPath)) { |
2208 | 2208 | return false; |
2209 | 2209 | } |