@@ -81,2084 +81,2084 @@ |
||
81 | 81 | * \OC\Files\Storage\Storage object |
82 | 82 | */ |
83 | 83 | class View { |
84 | - /** @var string */ |
|
85 | - private $fakeRoot = ''; |
|
86 | - |
|
87 | - /** |
|
88 | - * @var \OCP\Lock\ILockingProvider |
|
89 | - */ |
|
90 | - protected $lockingProvider; |
|
91 | - |
|
92 | - private $lockingEnabled; |
|
93 | - |
|
94 | - private $updaterEnabled = true; |
|
95 | - |
|
96 | - /** @var \OC\User\Manager */ |
|
97 | - private $userManager; |
|
98 | - |
|
99 | - /** @var \OCP\ILogger */ |
|
100 | - private $logger; |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $root |
|
104 | - * @throws \Exception If $root contains an invalid path |
|
105 | - */ |
|
106 | - public function __construct($root = '') { |
|
107 | - if (is_null($root)) { |
|
108 | - throw new \InvalidArgumentException('Root can\'t be null'); |
|
109 | - } |
|
110 | - if (!Filesystem::isValidPath($root)) { |
|
111 | - throw new \Exception(); |
|
112 | - } |
|
113 | - |
|
114 | - $this->fakeRoot = $root; |
|
115 | - $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
116 | - $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
117 | - $this->userManager = \OC::$server->getUserManager(); |
|
118 | - $this->logger = \OC::$server->getLogger(); |
|
119 | - } |
|
120 | - |
|
121 | - public function getAbsolutePath($path = '/') { |
|
122 | - if ($path === null) { |
|
123 | - return null; |
|
124 | - } |
|
125 | - $this->assertPathLength($path); |
|
126 | - if ($path === '') { |
|
127 | - $path = '/'; |
|
128 | - } |
|
129 | - if ($path[0] !== '/') { |
|
130 | - $path = '/' . $path; |
|
131 | - } |
|
132 | - return $this->fakeRoot . $path; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * change the root to a fake root |
|
137 | - * |
|
138 | - * @param string $fakeRoot |
|
139 | - * @return boolean|null |
|
140 | - */ |
|
141 | - public function chroot($fakeRoot) { |
|
142 | - if (!$fakeRoot == '') { |
|
143 | - if ($fakeRoot[0] !== '/') { |
|
144 | - $fakeRoot = '/' . $fakeRoot; |
|
145 | - } |
|
146 | - } |
|
147 | - $this->fakeRoot = $fakeRoot; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * get the fake root |
|
152 | - * |
|
153 | - * @return string |
|
154 | - */ |
|
155 | - public function getRoot() { |
|
156 | - return $this->fakeRoot; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * get path relative to the root of the view |
|
161 | - * |
|
162 | - * @param string $path |
|
163 | - * @return string |
|
164 | - */ |
|
165 | - public function getRelativePath($path) { |
|
166 | - $this->assertPathLength($path); |
|
167 | - if ($this->fakeRoot == '') { |
|
168 | - return $path; |
|
169 | - } |
|
170 | - |
|
171 | - if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
172 | - return '/'; |
|
173 | - } |
|
174 | - |
|
175 | - // missing slashes can cause wrong matches! |
|
176 | - $root = rtrim($this->fakeRoot, '/') . '/'; |
|
177 | - |
|
178 | - if (strpos($path, $root) !== 0) { |
|
179 | - return null; |
|
180 | - } else { |
|
181 | - $path = substr($path, strlen($this->fakeRoot)); |
|
182 | - if (strlen($path) === 0) { |
|
183 | - return '/'; |
|
184 | - } else { |
|
185 | - return $path; |
|
186 | - } |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * get the mountpoint of the storage object for a path |
|
192 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
193 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
194 | - * and does not take the chroot into account ) |
|
195 | - * |
|
196 | - * @param string $path |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - public function getMountPoint($path) { |
|
200 | - return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * get the mountpoint of the storage object for a path |
|
205 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
206 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
207 | - * and does not take the chroot into account ) |
|
208 | - * |
|
209 | - * @param string $path |
|
210 | - * @return \OCP\Files\Mount\IMountPoint |
|
211 | - */ |
|
212 | - public function getMount($path) { |
|
213 | - return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * resolve a path to a storage and internal path |
|
218 | - * |
|
219 | - * @param string $path |
|
220 | - * @return array an array consisting of the storage and the internal path |
|
221 | - */ |
|
222 | - public function resolvePath($path) { |
|
223 | - $a = $this->getAbsolutePath($path); |
|
224 | - $p = Filesystem::normalizePath($a); |
|
225 | - return Filesystem::resolvePath($p); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * return the path to a local version of the file |
|
230 | - * we need this because we can't know if a file is stored local or not from |
|
231 | - * outside the filestorage and for some purposes a local file is needed |
|
232 | - * |
|
233 | - * @param string $path |
|
234 | - * @return string |
|
235 | - */ |
|
236 | - public function getLocalFile($path) { |
|
237 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
238 | - $path = $this->getAbsolutePath($path); |
|
239 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
240 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
241 | - return $storage->getLocalFile($internalPath); |
|
242 | - } else { |
|
243 | - return null; |
|
244 | - } |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * @param string $path |
|
249 | - * @return string |
|
250 | - */ |
|
251 | - public function getLocalFolder($path) { |
|
252 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
253 | - $path = $this->getAbsolutePath($path); |
|
254 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
255 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
256 | - return $storage->getLocalFolder($internalPath); |
|
257 | - } else { |
|
258 | - return null; |
|
259 | - } |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * the following functions operate with arguments and return values identical |
|
264 | - * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
265 | - * for \OC\Files\Storage\Storage via basicOperation(). |
|
266 | - */ |
|
267 | - public function mkdir($path) { |
|
268 | - return $this->basicOperation('mkdir', $path, array('create', 'write')); |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * remove mount point |
|
273 | - * |
|
274 | - * @param \OC\Files\Mount\MoveableMount $mount |
|
275 | - * @param string $path relative to data/ |
|
276 | - * @return boolean |
|
277 | - */ |
|
278 | - protected function removeMount($mount, $path) { |
|
279 | - if ($mount instanceof MoveableMount) { |
|
280 | - // cut of /user/files to get the relative path to data/user/files |
|
281 | - $pathParts = explode('/', $path, 4); |
|
282 | - $relPath = '/' . $pathParts[3]; |
|
283 | - $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
284 | - \OC_Hook::emit( |
|
285 | - Filesystem::CLASSNAME, "umount", |
|
286 | - array(Filesystem::signal_param_path => $relPath) |
|
287 | - ); |
|
288 | - $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
289 | - $result = $mount->removeMount(); |
|
290 | - $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
291 | - if ($result) { |
|
292 | - \OC_Hook::emit( |
|
293 | - Filesystem::CLASSNAME, "post_umount", |
|
294 | - array(Filesystem::signal_param_path => $relPath) |
|
295 | - ); |
|
296 | - } |
|
297 | - $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
298 | - return $result; |
|
299 | - } else { |
|
300 | - // do not allow deleting the storage's root / the mount point |
|
301 | - // because for some storages it might delete the whole contents |
|
302 | - // but isn't supposed to work that way |
|
303 | - return false; |
|
304 | - } |
|
305 | - } |
|
306 | - |
|
307 | - public function disableCacheUpdate() { |
|
308 | - $this->updaterEnabled = false; |
|
309 | - } |
|
310 | - |
|
311 | - public function enableCacheUpdate() { |
|
312 | - $this->updaterEnabled = true; |
|
313 | - } |
|
314 | - |
|
315 | - protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
316 | - if ($this->updaterEnabled) { |
|
317 | - if (is_null($time)) { |
|
318 | - $time = time(); |
|
319 | - } |
|
320 | - $storage->getUpdater()->update($internalPath, $time); |
|
321 | - } |
|
322 | - } |
|
323 | - |
|
324 | - protected function removeUpdate(Storage $storage, $internalPath) { |
|
325 | - if ($this->updaterEnabled) { |
|
326 | - $storage->getUpdater()->remove($internalPath); |
|
327 | - } |
|
328 | - } |
|
329 | - |
|
330 | - protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
331 | - if ($this->updaterEnabled) { |
|
332 | - $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
333 | - } |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * @param string $path |
|
338 | - * @return bool|mixed |
|
339 | - */ |
|
340 | - public function rmdir($path) { |
|
341 | - $absolutePath = $this->getAbsolutePath($path); |
|
342 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
343 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
344 | - return $this->removeMount($mount, $absolutePath); |
|
345 | - } |
|
346 | - if ($this->is_dir($path)) { |
|
347 | - $result = $this->basicOperation('rmdir', $path, array('delete')); |
|
348 | - } else { |
|
349 | - $result = false; |
|
350 | - } |
|
351 | - |
|
352 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
353 | - $storage = $mount->getStorage(); |
|
354 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
355 | - $storage->getUpdater()->remove($internalPath); |
|
356 | - } |
|
357 | - return $result; |
|
358 | - } |
|
359 | - |
|
360 | - /** |
|
361 | - * @param string $path |
|
362 | - * @return resource |
|
363 | - */ |
|
364 | - public function opendir($path) { |
|
365 | - return $this->basicOperation('opendir', $path, array('read')); |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * @param string $path |
|
370 | - * @return bool|mixed |
|
371 | - */ |
|
372 | - public function is_dir($path) { |
|
373 | - if ($path == '/') { |
|
374 | - return true; |
|
375 | - } |
|
376 | - return $this->basicOperation('is_dir', $path); |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * @param string $path |
|
381 | - * @return bool|mixed |
|
382 | - */ |
|
383 | - public function is_file($path) { |
|
384 | - if ($path == '/') { |
|
385 | - return false; |
|
386 | - } |
|
387 | - return $this->basicOperation('is_file', $path); |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * @param string $path |
|
392 | - * @return mixed |
|
393 | - */ |
|
394 | - public function stat($path) { |
|
395 | - return $this->basicOperation('stat', $path); |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * @param string $path |
|
400 | - * @return mixed |
|
401 | - */ |
|
402 | - public function filetype($path) { |
|
403 | - return $this->basicOperation('filetype', $path); |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * @param string $path |
|
408 | - * @return mixed |
|
409 | - */ |
|
410 | - public function filesize($path) { |
|
411 | - return $this->basicOperation('filesize', $path); |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * @param string $path |
|
416 | - * @return bool|mixed |
|
417 | - * @throws \OCP\Files\InvalidPathException |
|
418 | - */ |
|
419 | - public function readfile($path) { |
|
420 | - $this->assertPathLength($path); |
|
421 | - @ob_end_clean(); |
|
422 | - $handle = $this->fopen($path, 'rb'); |
|
423 | - if ($handle) { |
|
424 | - $chunkSize = 8192; // 8 kB chunks |
|
425 | - while (!feof($handle)) { |
|
426 | - echo fread($handle, $chunkSize); |
|
427 | - flush(); |
|
428 | - } |
|
429 | - fclose($handle); |
|
430 | - return $this->filesize($path); |
|
431 | - } |
|
432 | - return false; |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * @param string $path |
|
437 | - * @param int $from |
|
438 | - * @param int $to |
|
439 | - * @return bool|mixed |
|
440 | - * @throws \OCP\Files\InvalidPathException |
|
441 | - * @throws \OCP\Files\UnseekableException |
|
442 | - */ |
|
443 | - public function readfilePart($path, $from, $to) { |
|
444 | - $this->assertPathLength($path); |
|
445 | - @ob_end_clean(); |
|
446 | - $handle = $this->fopen($path, 'rb'); |
|
447 | - if ($handle) { |
|
448 | - $chunkSize = 8192; // 8 kB chunks |
|
449 | - $startReading = true; |
|
450 | - |
|
451 | - if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
452 | - // forward file handle via chunked fread because fseek seem to have failed |
|
453 | - |
|
454 | - $end = $from + 1; |
|
455 | - while (!feof($handle) && ftell($handle) < $end) { |
|
456 | - $len = $from - ftell($handle); |
|
457 | - if ($len > $chunkSize) { |
|
458 | - $len = $chunkSize; |
|
459 | - } |
|
460 | - $result = fread($handle, $len); |
|
461 | - |
|
462 | - if ($result === false) { |
|
463 | - $startReading = false; |
|
464 | - break; |
|
465 | - } |
|
466 | - } |
|
467 | - } |
|
468 | - |
|
469 | - if ($startReading) { |
|
470 | - $end = $to + 1; |
|
471 | - while (!feof($handle) && ftell($handle) < $end) { |
|
472 | - $len = $end - ftell($handle); |
|
473 | - if ($len > $chunkSize) { |
|
474 | - $len = $chunkSize; |
|
475 | - } |
|
476 | - echo fread($handle, $len); |
|
477 | - flush(); |
|
478 | - } |
|
479 | - return ftell($handle) - $from; |
|
480 | - } |
|
481 | - |
|
482 | - throw new \OCP\Files\UnseekableException('fseek error'); |
|
483 | - } |
|
484 | - return false; |
|
485 | - } |
|
486 | - |
|
487 | - /** |
|
488 | - * @param string $path |
|
489 | - * @return mixed |
|
490 | - */ |
|
491 | - public function isCreatable($path) { |
|
492 | - return $this->basicOperation('isCreatable', $path); |
|
493 | - } |
|
494 | - |
|
495 | - /** |
|
496 | - * @param string $path |
|
497 | - * @return mixed |
|
498 | - */ |
|
499 | - public function isReadable($path) { |
|
500 | - return $this->basicOperation('isReadable', $path); |
|
501 | - } |
|
502 | - |
|
503 | - /** |
|
504 | - * @param string $path |
|
505 | - * @return mixed |
|
506 | - */ |
|
507 | - public function isUpdatable($path) { |
|
508 | - return $this->basicOperation('isUpdatable', $path); |
|
509 | - } |
|
510 | - |
|
511 | - /** |
|
512 | - * @param string $path |
|
513 | - * @return bool|mixed |
|
514 | - */ |
|
515 | - public function isDeletable($path) { |
|
516 | - $absolutePath = $this->getAbsolutePath($path); |
|
517 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
518 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
519 | - return $mount instanceof MoveableMount; |
|
520 | - } |
|
521 | - return $this->basicOperation('isDeletable', $path); |
|
522 | - } |
|
523 | - |
|
524 | - /** |
|
525 | - * @param string $path |
|
526 | - * @return mixed |
|
527 | - */ |
|
528 | - public function isSharable($path) { |
|
529 | - return $this->basicOperation('isSharable', $path); |
|
530 | - } |
|
531 | - |
|
532 | - /** |
|
533 | - * @param string $path |
|
534 | - * @return bool|mixed |
|
535 | - */ |
|
536 | - public function file_exists($path) { |
|
537 | - if ($path == '/') { |
|
538 | - return true; |
|
539 | - } |
|
540 | - return $this->basicOperation('file_exists', $path); |
|
541 | - } |
|
542 | - |
|
543 | - /** |
|
544 | - * @param string $path |
|
545 | - * @return mixed |
|
546 | - */ |
|
547 | - public function filemtime($path) { |
|
548 | - return $this->basicOperation('filemtime', $path); |
|
549 | - } |
|
550 | - |
|
551 | - /** |
|
552 | - * @param string $path |
|
553 | - * @param int|string $mtime |
|
554 | - * @return bool |
|
555 | - */ |
|
556 | - public function touch($path, $mtime = null) { |
|
557 | - if (!is_null($mtime) and !is_numeric($mtime)) { |
|
558 | - $mtime = strtotime($mtime); |
|
559 | - } |
|
560 | - |
|
561 | - $hooks = array('touch'); |
|
562 | - |
|
563 | - if (!$this->file_exists($path)) { |
|
564 | - $hooks[] = 'create'; |
|
565 | - $hooks[] = 'write'; |
|
566 | - } |
|
567 | - $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
568 | - if (!$result) { |
|
569 | - // If create file fails because of permissions on external storage like SMB folders, |
|
570 | - // check file exists and return false if not. |
|
571 | - if (!$this->file_exists($path)) { |
|
572 | - return false; |
|
573 | - } |
|
574 | - if (is_null($mtime)) { |
|
575 | - $mtime = time(); |
|
576 | - } |
|
577 | - //if native touch fails, we emulate it by changing the mtime in the cache |
|
578 | - $this->putFileInfo($path, array('mtime' => floor($mtime))); |
|
579 | - } |
|
580 | - return true; |
|
581 | - } |
|
582 | - |
|
583 | - /** |
|
584 | - * @param string $path |
|
585 | - * @return mixed |
|
586 | - */ |
|
587 | - public function file_get_contents($path) { |
|
588 | - return $this->basicOperation('file_get_contents', $path, array('read')); |
|
589 | - } |
|
590 | - |
|
591 | - /** |
|
592 | - * @param bool $exists |
|
593 | - * @param string $path |
|
594 | - * @param bool $run |
|
595 | - */ |
|
596 | - protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
597 | - if (!$exists) { |
|
598 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array( |
|
599 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
600 | - Filesystem::signal_param_run => &$run, |
|
601 | - )); |
|
602 | - } else { |
|
603 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array( |
|
604 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
605 | - Filesystem::signal_param_run => &$run, |
|
606 | - )); |
|
607 | - } |
|
608 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array( |
|
609 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
610 | - Filesystem::signal_param_run => &$run, |
|
611 | - )); |
|
612 | - } |
|
613 | - |
|
614 | - /** |
|
615 | - * @param bool $exists |
|
616 | - * @param string $path |
|
617 | - */ |
|
618 | - protected function emit_file_hooks_post($exists, $path) { |
|
619 | - if (!$exists) { |
|
620 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array( |
|
621 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
622 | - )); |
|
623 | - } else { |
|
624 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array( |
|
625 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
626 | - )); |
|
627 | - } |
|
628 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array( |
|
629 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
630 | - )); |
|
631 | - } |
|
632 | - |
|
633 | - /** |
|
634 | - * @param string $path |
|
635 | - * @param mixed $data |
|
636 | - * @return bool|mixed |
|
637 | - * @throws \Exception |
|
638 | - */ |
|
639 | - public function file_put_contents($path, $data) { |
|
640 | - if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
641 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
642 | - if (Filesystem::isValidPath($path) |
|
643 | - and !Filesystem::isFileBlacklisted($path) |
|
644 | - ) { |
|
645 | - $path = $this->getRelativePath($absolutePath); |
|
646 | - |
|
647 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
648 | - |
|
649 | - $exists = $this->file_exists($path); |
|
650 | - $run = true; |
|
651 | - if ($this->shouldEmitHooks($path)) { |
|
652 | - $this->emit_file_hooks_pre($exists, $path, $run); |
|
653 | - } |
|
654 | - if (!$run) { |
|
655 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
656 | - return false; |
|
657 | - } |
|
658 | - |
|
659 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
660 | - |
|
661 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
662 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
663 | - $target = $storage->fopen($internalPath, 'w'); |
|
664 | - if ($target) { |
|
665 | - list (, $result) = \OC_Helper::streamCopy($data, $target); |
|
666 | - fclose($target); |
|
667 | - fclose($data); |
|
668 | - |
|
669 | - $this->writeUpdate($storage, $internalPath); |
|
670 | - |
|
671 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
672 | - |
|
673 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
674 | - $this->emit_file_hooks_post($exists, $path); |
|
675 | - } |
|
676 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
677 | - return $result; |
|
678 | - } else { |
|
679 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
680 | - return false; |
|
681 | - } |
|
682 | - } else { |
|
683 | - return false; |
|
684 | - } |
|
685 | - } else { |
|
686 | - $hooks = $this->file_exists($path) ? array('update', 'write') : array('create', 'write'); |
|
687 | - return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
688 | - } |
|
689 | - } |
|
690 | - |
|
691 | - /** |
|
692 | - * @param string $path |
|
693 | - * @return bool|mixed |
|
694 | - */ |
|
695 | - public function unlink($path) { |
|
696 | - if ($path === '' || $path === '/') { |
|
697 | - // do not allow deleting the root |
|
698 | - return false; |
|
699 | - } |
|
700 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
701 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
702 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
703 | - if ($mount and $mount->getInternalPath($absolutePath) === '') { |
|
704 | - return $this->removeMount($mount, $absolutePath); |
|
705 | - } |
|
706 | - if ($this->is_dir($path)) { |
|
707 | - $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
708 | - } else { |
|
709 | - $result = $this->basicOperation('unlink', $path, ['delete']); |
|
710 | - } |
|
711 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
712 | - $storage = $mount->getStorage(); |
|
713 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
714 | - $storage->getUpdater()->remove($internalPath); |
|
715 | - return true; |
|
716 | - } else { |
|
717 | - return $result; |
|
718 | - } |
|
719 | - } |
|
720 | - |
|
721 | - /** |
|
722 | - * @param string $directory |
|
723 | - * @return bool|mixed |
|
724 | - */ |
|
725 | - public function deleteAll($directory) { |
|
726 | - return $this->rmdir($directory); |
|
727 | - } |
|
728 | - |
|
729 | - /** |
|
730 | - * Rename/move a file or folder from the source path to target path. |
|
731 | - * |
|
732 | - * @param string $path1 source path |
|
733 | - * @param string $path2 target path |
|
734 | - * |
|
735 | - * @return bool|mixed |
|
736 | - */ |
|
737 | - public function rename($path1, $path2) { |
|
738 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
739 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
740 | - $result = false; |
|
741 | - if ( |
|
742 | - Filesystem::isValidPath($path2) |
|
743 | - and Filesystem::isValidPath($path1) |
|
744 | - and !Filesystem::isFileBlacklisted($path2) |
|
745 | - ) { |
|
746 | - $path1 = $this->getRelativePath($absolutePath1); |
|
747 | - $path2 = $this->getRelativePath($absolutePath2); |
|
748 | - $exists = $this->file_exists($path2); |
|
749 | - |
|
750 | - if ($path1 == null or $path2 == null) { |
|
751 | - return false; |
|
752 | - } |
|
753 | - |
|
754 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
755 | - try { |
|
756 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
757 | - |
|
758 | - $run = true; |
|
759 | - if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
|
760 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
761 | - $this->emit_file_hooks_pre($exists, $path2, $run); |
|
762 | - } elseif ($this->shouldEmitHooks($path1)) { |
|
763 | - \OC_Hook::emit( |
|
764 | - Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
765 | - array( |
|
766 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
767 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
768 | - Filesystem::signal_param_run => &$run |
|
769 | - ) |
|
770 | - ); |
|
771 | - } |
|
772 | - if ($run) { |
|
773 | - $this->verifyPath(dirname($path2), basename($path2)); |
|
774 | - |
|
775 | - $manager = Filesystem::getMountManager(); |
|
776 | - $mount1 = $this->getMount($path1); |
|
777 | - $mount2 = $this->getMount($path2); |
|
778 | - $storage1 = $mount1->getStorage(); |
|
779 | - $storage2 = $mount2->getStorage(); |
|
780 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
781 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
782 | - |
|
783 | - $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
784 | - try { |
|
785 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
786 | - |
|
787 | - if ($internalPath1 === '') { |
|
788 | - if ($mount1 instanceof MoveableMount) { |
|
789 | - if ($this->isTargetAllowed($absolutePath2)) { |
|
790 | - /** |
|
791 | - * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
792 | - */ |
|
793 | - $sourceMountPoint = $mount1->getMountPoint(); |
|
794 | - $result = $mount1->moveMount($absolutePath2); |
|
795 | - $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
796 | - } else { |
|
797 | - $result = false; |
|
798 | - } |
|
799 | - } else { |
|
800 | - $result = false; |
|
801 | - } |
|
802 | - // moving a file/folder within the same mount point |
|
803 | - } elseif ($storage1 === $storage2) { |
|
804 | - if ($storage1) { |
|
805 | - $result = $storage1->rename($internalPath1, $internalPath2); |
|
806 | - } else { |
|
807 | - $result = false; |
|
808 | - } |
|
809 | - // moving a file/folder between storages (from $storage1 to $storage2) |
|
810 | - } else { |
|
811 | - $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
812 | - } |
|
813 | - |
|
814 | - if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
815 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
816 | - $this->writeUpdate($storage2, $internalPath2); |
|
817 | - } else if ($result) { |
|
818 | - if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
819 | - $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
820 | - } |
|
821 | - } |
|
822 | - } catch(\Exception $e) { |
|
823 | - throw $e; |
|
824 | - } finally { |
|
825 | - $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
|
826 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
|
827 | - } |
|
828 | - |
|
829 | - if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
830 | - if ($this->shouldEmitHooks()) { |
|
831 | - $this->emit_file_hooks_post($exists, $path2); |
|
832 | - } |
|
833 | - } elseif ($result) { |
|
834 | - if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
|
835 | - \OC_Hook::emit( |
|
836 | - Filesystem::CLASSNAME, |
|
837 | - Filesystem::signal_post_rename, |
|
838 | - array( |
|
839 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
840 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
841 | - ) |
|
842 | - ); |
|
843 | - } |
|
844 | - } |
|
845 | - } |
|
846 | - } catch(\Exception $e) { |
|
847 | - throw $e; |
|
848 | - } finally { |
|
849 | - $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
850 | - $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
851 | - } |
|
852 | - } |
|
853 | - return $result; |
|
854 | - } |
|
855 | - |
|
856 | - /** |
|
857 | - * Copy a file/folder from the source path to target path |
|
858 | - * |
|
859 | - * @param string $path1 source path |
|
860 | - * @param string $path2 target path |
|
861 | - * @param bool $preserveMtime whether to preserve mtime on the copy |
|
862 | - * |
|
863 | - * @return bool|mixed |
|
864 | - */ |
|
865 | - public function copy($path1, $path2, $preserveMtime = false) { |
|
866 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
867 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
868 | - $result = false; |
|
869 | - if ( |
|
870 | - Filesystem::isValidPath($path2) |
|
871 | - and Filesystem::isValidPath($path1) |
|
872 | - and !Filesystem::isFileBlacklisted($path2) |
|
873 | - ) { |
|
874 | - $path1 = $this->getRelativePath($absolutePath1); |
|
875 | - $path2 = $this->getRelativePath($absolutePath2); |
|
876 | - |
|
877 | - if ($path1 == null or $path2 == null) { |
|
878 | - return false; |
|
879 | - } |
|
880 | - $run = true; |
|
881 | - |
|
882 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
|
883 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
|
884 | - $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
885 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
886 | - |
|
887 | - try { |
|
888 | - |
|
889 | - $exists = $this->file_exists($path2); |
|
890 | - if ($this->shouldEmitHooks()) { |
|
891 | - \OC_Hook::emit( |
|
892 | - Filesystem::CLASSNAME, |
|
893 | - Filesystem::signal_copy, |
|
894 | - array( |
|
895 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
896 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
897 | - Filesystem::signal_param_run => &$run |
|
898 | - ) |
|
899 | - ); |
|
900 | - $this->emit_file_hooks_pre($exists, $path2, $run); |
|
901 | - } |
|
902 | - if ($run) { |
|
903 | - $mount1 = $this->getMount($path1); |
|
904 | - $mount2 = $this->getMount($path2); |
|
905 | - $storage1 = $mount1->getStorage(); |
|
906 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
907 | - $storage2 = $mount2->getStorage(); |
|
908 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
909 | - |
|
910 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
911 | - $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
912 | - |
|
913 | - if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
914 | - if ($storage1) { |
|
915 | - $result = $storage1->copy($internalPath1, $internalPath2); |
|
916 | - } else { |
|
917 | - $result = false; |
|
918 | - } |
|
919 | - } else { |
|
920 | - $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
921 | - } |
|
922 | - |
|
923 | - $this->writeUpdate($storage2, $internalPath2); |
|
924 | - |
|
925 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
|
926 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
927 | - |
|
928 | - if ($this->shouldEmitHooks() && $result !== false) { |
|
929 | - \OC_Hook::emit( |
|
930 | - Filesystem::CLASSNAME, |
|
931 | - Filesystem::signal_post_copy, |
|
932 | - array( |
|
933 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
934 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
935 | - ) |
|
936 | - ); |
|
937 | - $this->emit_file_hooks_post($exists, $path2); |
|
938 | - } |
|
939 | - |
|
940 | - } |
|
941 | - } catch (\Exception $e) { |
|
942 | - $this->unlockFile($path2, $lockTypePath2); |
|
943 | - $this->unlockFile($path1, $lockTypePath1); |
|
944 | - throw $e; |
|
945 | - } |
|
946 | - |
|
947 | - $this->unlockFile($path2, $lockTypePath2); |
|
948 | - $this->unlockFile($path1, $lockTypePath1); |
|
949 | - |
|
950 | - } |
|
951 | - return $result; |
|
952 | - } |
|
953 | - |
|
954 | - /** |
|
955 | - * @param string $path |
|
956 | - * @param string $mode 'r' or 'w' |
|
957 | - * @return resource |
|
958 | - */ |
|
959 | - public function fopen($path, $mode) { |
|
960 | - $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
961 | - $hooks = array(); |
|
962 | - switch ($mode) { |
|
963 | - case 'r': |
|
964 | - $hooks[] = 'read'; |
|
965 | - break; |
|
966 | - case 'r+': |
|
967 | - case 'w+': |
|
968 | - case 'x+': |
|
969 | - case 'a+': |
|
970 | - $hooks[] = 'read'; |
|
971 | - $hooks[] = 'write'; |
|
972 | - break; |
|
973 | - case 'w': |
|
974 | - case 'x': |
|
975 | - case 'a': |
|
976 | - $hooks[] = 'write'; |
|
977 | - break; |
|
978 | - default: |
|
979 | - \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, ILogger::ERROR); |
|
980 | - } |
|
981 | - |
|
982 | - if ($mode !== 'r' && $mode !== 'w') { |
|
983 | - \OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends'); |
|
984 | - } |
|
985 | - |
|
986 | - return $this->basicOperation('fopen', $path, $hooks, $mode); |
|
987 | - } |
|
988 | - |
|
989 | - /** |
|
990 | - * @param string $path |
|
991 | - * @return bool|string |
|
992 | - * @throws \OCP\Files\InvalidPathException |
|
993 | - */ |
|
994 | - public function toTmpFile($path) { |
|
995 | - $this->assertPathLength($path); |
|
996 | - if (Filesystem::isValidPath($path)) { |
|
997 | - $source = $this->fopen($path, 'r'); |
|
998 | - if ($source) { |
|
999 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
1000 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
1001 | - file_put_contents($tmpFile, $source); |
|
1002 | - return $tmpFile; |
|
1003 | - } else { |
|
1004 | - return false; |
|
1005 | - } |
|
1006 | - } else { |
|
1007 | - return false; |
|
1008 | - } |
|
1009 | - } |
|
1010 | - |
|
1011 | - /** |
|
1012 | - * @param string $tmpFile |
|
1013 | - * @param string $path |
|
1014 | - * @return bool|mixed |
|
1015 | - * @throws \OCP\Files\InvalidPathException |
|
1016 | - */ |
|
1017 | - public function fromTmpFile($tmpFile, $path) { |
|
1018 | - $this->assertPathLength($path); |
|
1019 | - if (Filesystem::isValidPath($path)) { |
|
1020 | - |
|
1021 | - // Get directory that the file is going into |
|
1022 | - $filePath = dirname($path); |
|
1023 | - |
|
1024 | - // Create the directories if any |
|
1025 | - if (!$this->file_exists($filePath)) { |
|
1026 | - $result = $this->createParentDirectories($filePath); |
|
1027 | - if ($result === false) { |
|
1028 | - return false; |
|
1029 | - } |
|
1030 | - } |
|
1031 | - |
|
1032 | - $source = fopen($tmpFile, 'r'); |
|
1033 | - if ($source) { |
|
1034 | - $result = $this->file_put_contents($path, $source); |
|
1035 | - // $this->file_put_contents() might have already closed |
|
1036 | - // the resource, so we check it, before trying to close it |
|
1037 | - // to avoid messages in the error log. |
|
1038 | - if (is_resource($source)) { |
|
1039 | - fclose($source); |
|
1040 | - } |
|
1041 | - unlink($tmpFile); |
|
1042 | - return $result; |
|
1043 | - } else { |
|
1044 | - return false; |
|
1045 | - } |
|
1046 | - } else { |
|
1047 | - return false; |
|
1048 | - } |
|
1049 | - } |
|
1050 | - |
|
1051 | - |
|
1052 | - /** |
|
1053 | - * @param string $path |
|
1054 | - * @return mixed |
|
1055 | - * @throws \OCP\Files\InvalidPathException |
|
1056 | - */ |
|
1057 | - public function getMimeType($path) { |
|
1058 | - $this->assertPathLength($path); |
|
1059 | - return $this->basicOperation('getMimeType', $path); |
|
1060 | - } |
|
1061 | - |
|
1062 | - /** |
|
1063 | - * @param string $type |
|
1064 | - * @param string $path |
|
1065 | - * @param bool $raw |
|
1066 | - * @return bool|null|string |
|
1067 | - */ |
|
1068 | - public function hash($type, $path, $raw = false) { |
|
1069 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1070 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1071 | - if (Filesystem::isValidPath($path)) { |
|
1072 | - $path = $this->getRelativePath($absolutePath); |
|
1073 | - if ($path == null) { |
|
1074 | - return false; |
|
1075 | - } |
|
1076 | - if ($this->shouldEmitHooks($path)) { |
|
1077 | - \OC_Hook::emit( |
|
1078 | - Filesystem::CLASSNAME, |
|
1079 | - Filesystem::signal_read, |
|
1080 | - array(Filesystem::signal_param_path => $this->getHookPath($path)) |
|
1081 | - ); |
|
1082 | - } |
|
1083 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
1084 | - if ($storage) { |
|
1085 | - return $storage->hash($type, $internalPath, $raw); |
|
1086 | - } |
|
1087 | - } |
|
1088 | - return null; |
|
1089 | - } |
|
1090 | - |
|
1091 | - /** |
|
1092 | - * @param string $path |
|
1093 | - * @return mixed |
|
1094 | - * @throws \OCP\Files\InvalidPathException |
|
1095 | - */ |
|
1096 | - public function free_space($path = '/') { |
|
1097 | - $this->assertPathLength($path); |
|
1098 | - $result = $this->basicOperation('free_space', $path); |
|
1099 | - if ($result === null) { |
|
1100 | - throw new InvalidPathException(); |
|
1101 | - } |
|
1102 | - return $result; |
|
1103 | - } |
|
1104 | - |
|
1105 | - /** |
|
1106 | - * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
1107 | - * |
|
1108 | - * @param string $operation |
|
1109 | - * @param string $path |
|
1110 | - * @param array $hooks (optional) |
|
1111 | - * @param mixed $extraParam (optional) |
|
1112 | - * @return mixed |
|
1113 | - * @throws \Exception |
|
1114 | - * |
|
1115 | - * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
1116 | - * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
1117 | - * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
1118 | - */ |
|
1119 | - private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
1120 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1121 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1122 | - if (Filesystem::isValidPath($path) |
|
1123 | - and !Filesystem::isFileBlacklisted($path) |
|
1124 | - ) { |
|
1125 | - $path = $this->getRelativePath($absolutePath); |
|
1126 | - if ($path == null) { |
|
1127 | - return false; |
|
1128 | - } |
|
1129 | - |
|
1130 | - if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
1131 | - // always a shared lock during pre-hooks so the hook can read the file |
|
1132 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
1133 | - } |
|
1134 | - |
|
1135 | - $run = $this->runHooks($hooks, $path); |
|
1136 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
1137 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
1138 | - if ($run and $storage) { |
|
1139 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1140 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1141 | - } |
|
1142 | - try { |
|
1143 | - if (!is_null($extraParam)) { |
|
1144 | - $result = $storage->$operation($internalPath, $extraParam); |
|
1145 | - } else { |
|
1146 | - $result = $storage->$operation($internalPath); |
|
1147 | - } |
|
1148 | - } catch (\Exception $e) { |
|
1149 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1150 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1151 | - } else if (in_array('read', $hooks)) { |
|
1152 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1153 | - } |
|
1154 | - throw $e; |
|
1155 | - } |
|
1156 | - |
|
1157 | - if ($result && in_array('delete', $hooks) and $result) { |
|
1158 | - $this->removeUpdate($storage, $internalPath); |
|
1159 | - } |
|
1160 | - if ($result && in_array('write', $hooks) and $operation !== 'fopen') { |
|
1161 | - $this->writeUpdate($storage, $internalPath); |
|
1162 | - } |
|
1163 | - if ($result && in_array('touch', $hooks)) { |
|
1164 | - $this->writeUpdate($storage, $internalPath, $extraParam); |
|
1165 | - } |
|
1166 | - |
|
1167 | - if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
1168 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
1169 | - } |
|
1170 | - |
|
1171 | - $unlockLater = false; |
|
1172 | - if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
1173 | - $unlockLater = true; |
|
1174 | - // make sure our unlocking callback will still be called if connection is aborted |
|
1175 | - ignore_user_abort(true); |
|
1176 | - $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
1177 | - if (in_array('write', $hooks)) { |
|
1178 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1179 | - } else if (in_array('read', $hooks)) { |
|
1180 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1181 | - } |
|
1182 | - }); |
|
1183 | - } |
|
1184 | - |
|
1185 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
1186 | - if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
1187 | - $this->runHooks($hooks, $path, true); |
|
1188 | - } |
|
1189 | - } |
|
1190 | - |
|
1191 | - if (!$unlockLater |
|
1192 | - && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
1193 | - ) { |
|
1194 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1195 | - } |
|
1196 | - return $result; |
|
1197 | - } else { |
|
1198 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1199 | - } |
|
1200 | - } |
|
1201 | - return null; |
|
1202 | - } |
|
1203 | - |
|
1204 | - /** |
|
1205 | - * get the path relative to the default root for hook usage |
|
1206 | - * |
|
1207 | - * @param string $path |
|
1208 | - * @return string |
|
1209 | - */ |
|
1210 | - private function getHookPath($path) { |
|
1211 | - if (!Filesystem::getView()) { |
|
1212 | - return $path; |
|
1213 | - } |
|
1214 | - return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
1215 | - } |
|
1216 | - |
|
1217 | - private function shouldEmitHooks($path = '') { |
|
1218 | - if ($path && Cache\Scanner::isPartialFile($path)) { |
|
1219 | - return false; |
|
1220 | - } |
|
1221 | - if (!Filesystem::$loaded) { |
|
1222 | - return false; |
|
1223 | - } |
|
1224 | - $defaultRoot = Filesystem::getRoot(); |
|
1225 | - if ($defaultRoot === null) { |
|
1226 | - return false; |
|
1227 | - } |
|
1228 | - if ($this->fakeRoot === $defaultRoot) { |
|
1229 | - return true; |
|
1230 | - } |
|
1231 | - $fullPath = $this->getAbsolutePath($path); |
|
1232 | - |
|
1233 | - if ($fullPath === $defaultRoot) { |
|
1234 | - return true; |
|
1235 | - } |
|
1236 | - |
|
1237 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
1238 | - } |
|
1239 | - |
|
1240 | - /** |
|
1241 | - * @param string[] $hooks |
|
1242 | - * @param string $path |
|
1243 | - * @param bool $post |
|
1244 | - * @return bool |
|
1245 | - */ |
|
1246 | - private function runHooks($hooks, $path, $post = false) { |
|
1247 | - $relativePath = $path; |
|
1248 | - $path = $this->getHookPath($path); |
|
1249 | - $prefix = $post ? 'post_' : ''; |
|
1250 | - $run = true; |
|
1251 | - if ($this->shouldEmitHooks($relativePath)) { |
|
1252 | - foreach ($hooks as $hook) { |
|
1253 | - if ($hook != 'read') { |
|
1254 | - \OC_Hook::emit( |
|
1255 | - Filesystem::CLASSNAME, |
|
1256 | - $prefix . $hook, |
|
1257 | - array( |
|
1258 | - Filesystem::signal_param_run => &$run, |
|
1259 | - Filesystem::signal_param_path => $path |
|
1260 | - ) |
|
1261 | - ); |
|
1262 | - } elseif (!$post) { |
|
1263 | - \OC_Hook::emit( |
|
1264 | - Filesystem::CLASSNAME, |
|
1265 | - $prefix . $hook, |
|
1266 | - array( |
|
1267 | - Filesystem::signal_param_path => $path |
|
1268 | - ) |
|
1269 | - ); |
|
1270 | - } |
|
1271 | - } |
|
1272 | - } |
|
1273 | - return $run; |
|
1274 | - } |
|
1275 | - |
|
1276 | - /** |
|
1277 | - * check if a file or folder has been updated since $time |
|
1278 | - * |
|
1279 | - * @param string $path |
|
1280 | - * @param int $time |
|
1281 | - * @return bool |
|
1282 | - */ |
|
1283 | - public function hasUpdated($path, $time) { |
|
1284 | - return $this->basicOperation('hasUpdated', $path, array(), $time); |
|
1285 | - } |
|
1286 | - |
|
1287 | - /** |
|
1288 | - * @param string $ownerId |
|
1289 | - * @return \OC\User\User |
|
1290 | - */ |
|
1291 | - private function getUserObjectForOwner($ownerId) { |
|
1292 | - $owner = $this->userManager->get($ownerId); |
|
1293 | - if ($owner instanceof IUser) { |
|
1294 | - return $owner; |
|
1295 | - } else { |
|
1296 | - return new User($ownerId, null); |
|
1297 | - } |
|
1298 | - } |
|
1299 | - |
|
1300 | - /** |
|
1301 | - * Get file info from cache |
|
1302 | - * |
|
1303 | - * If the file is not in cached it will be scanned |
|
1304 | - * If the file has changed on storage the cache will be updated |
|
1305 | - * |
|
1306 | - * @param \OC\Files\Storage\Storage $storage |
|
1307 | - * @param string $internalPath |
|
1308 | - * @param string $relativePath |
|
1309 | - * @return ICacheEntry|bool |
|
1310 | - */ |
|
1311 | - private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
1312 | - $cache = $storage->getCache($internalPath); |
|
1313 | - $data = $cache->get($internalPath); |
|
1314 | - $watcher = $storage->getWatcher($internalPath); |
|
1315 | - |
|
1316 | - try { |
|
1317 | - // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
1318 | - if (!$data || $data['size'] === -1) { |
|
1319 | - $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1320 | - if (!$storage->file_exists($internalPath)) { |
|
1321 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1322 | - return false; |
|
1323 | - } |
|
1324 | - $scanner = $storage->getScanner($internalPath); |
|
1325 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1326 | - $data = $cache->get($internalPath); |
|
1327 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1328 | - } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
1329 | - $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1330 | - $watcher->update($internalPath, $data); |
|
1331 | - $storage->getPropagator()->propagateChange($internalPath, time()); |
|
1332 | - $data = $cache->get($internalPath); |
|
1333 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1334 | - } |
|
1335 | - } catch (LockedException $e) { |
|
1336 | - // if the file is locked we just use the old cache info |
|
1337 | - } |
|
1338 | - |
|
1339 | - return $data; |
|
1340 | - } |
|
1341 | - |
|
1342 | - /** |
|
1343 | - * get the filesystem info |
|
1344 | - * |
|
1345 | - * @param string $path |
|
1346 | - * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
|
1347 | - * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
1348 | - * defaults to true |
|
1349 | - * @return \OC\Files\FileInfo|false False if file does not exist |
|
1350 | - */ |
|
1351 | - public function getFileInfo($path, $includeMountPoints = true) { |
|
1352 | - $this->assertPathLength($path); |
|
1353 | - if (!Filesystem::isValidPath($path)) { |
|
1354 | - return false; |
|
1355 | - } |
|
1356 | - if (Cache\Scanner::isPartialFile($path)) { |
|
1357 | - return $this->getPartFileInfo($path); |
|
1358 | - } |
|
1359 | - $relativePath = $path; |
|
1360 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1361 | - |
|
1362 | - $mount = Filesystem::getMountManager()->find($path); |
|
1363 | - if (!$mount) { |
|
1364 | - return false; |
|
1365 | - } |
|
1366 | - $storage = $mount->getStorage(); |
|
1367 | - $internalPath = $mount->getInternalPath($path); |
|
1368 | - if ($storage) { |
|
1369 | - $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
1370 | - |
|
1371 | - if (!$data instanceof ICacheEntry) { |
|
1372 | - return false; |
|
1373 | - } |
|
1374 | - |
|
1375 | - if ($mount instanceof MoveableMount && $internalPath === '') { |
|
1376 | - $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
1377 | - } |
|
1378 | - |
|
1379 | - $owner = $this->getUserObjectForOwner($storage->getOwner($internalPath)); |
|
1380 | - $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
1381 | - |
|
1382 | - if ($data and isset($data['fileid'])) { |
|
1383 | - if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
1384 | - //add the sizes of other mount points to the folder |
|
1385 | - $extOnly = ($includeMountPoints === 'ext'); |
|
1386 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
1387 | - $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
1388 | - $subStorage = $mount->getStorage(); |
|
1389 | - return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
1390 | - })); |
|
1391 | - } |
|
1392 | - } |
|
1393 | - |
|
1394 | - return $info; |
|
1395 | - } |
|
1396 | - |
|
1397 | - return false; |
|
1398 | - } |
|
1399 | - |
|
1400 | - /** |
|
1401 | - * get the content of a directory |
|
1402 | - * |
|
1403 | - * @param string $directory path under datadirectory |
|
1404 | - * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
1405 | - * @return FileInfo[] |
|
1406 | - */ |
|
1407 | - public function getDirectoryContent($directory, $mimetype_filter = '') { |
|
1408 | - $this->assertPathLength($directory); |
|
1409 | - if (!Filesystem::isValidPath($directory)) { |
|
1410 | - return []; |
|
1411 | - } |
|
1412 | - $path = $this->getAbsolutePath($directory); |
|
1413 | - $path = Filesystem::normalizePath($path); |
|
1414 | - $mount = $this->getMount($directory); |
|
1415 | - if (!$mount) { |
|
1416 | - return []; |
|
1417 | - } |
|
1418 | - $storage = $mount->getStorage(); |
|
1419 | - $internalPath = $mount->getInternalPath($path); |
|
1420 | - if ($storage) { |
|
1421 | - $cache = $storage->getCache($internalPath); |
|
1422 | - $user = \OC_User::getUser(); |
|
1423 | - |
|
1424 | - $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
1425 | - |
|
1426 | - if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) { |
|
1427 | - return []; |
|
1428 | - } |
|
1429 | - |
|
1430 | - $folderId = $data['fileid']; |
|
1431 | - $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
1432 | - |
|
1433 | - $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
1434 | - /** |
|
1435 | - * @var \OC\Files\FileInfo[] $files |
|
1436 | - */ |
|
1437 | - $files = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
1438 | - if ($sharingDisabled) { |
|
1439 | - $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1440 | - } |
|
1441 | - $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
1442 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
1443 | - }, $contents); |
|
1444 | - |
|
1445 | - //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
1446 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
1447 | - $dirLength = strlen($path); |
|
1448 | - foreach ($mounts as $mount) { |
|
1449 | - $mountPoint = $mount->getMountPoint(); |
|
1450 | - $subStorage = $mount->getStorage(); |
|
1451 | - if ($subStorage) { |
|
1452 | - $subCache = $subStorage->getCache(''); |
|
1453 | - |
|
1454 | - $rootEntry = $subCache->get(''); |
|
1455 | - if (!$rootEntry) { |
|
1456 | - $subScanner = $subStorage->getScanner(''); |
|
1457 | - try { |
|
1458 | - $subScanner->scanFile(''); |
|
1459 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
1460 | - continue; |
|
1461 | - } catch (\OCP\Files\StorageInvalidException $e) { |
|
1462 | - continue; |
|
1463 | - } catch (\Exception $e) { |
|
1464 | - // sometimes when the storage is not available it can be any exception |
|
1465 | - \OC::$server->getLogger()->logException($e, [ |
|
1466 | - 'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"', |
|
1467 | - 'level' => ILogger::ERROR, |
|
1468 | - 'app' => 'lib', |
|
1469 | - ]); |
|
1470 | - continue; |
|
1471 | - } |
|
1472 | - $rootEntry = $subCache->get(''); |
|
1473 | - } |
|
1474 | - |
|
1475 | - if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) { |
|
1476 | - $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
1477 | - if ($pos = strpos($relativePath, '/')) { |
|
1478 | - //mountpoint inside subfolder add size to the correct folder |
|
1479 | - $entryName = substr($relativePath, 0, $pos); |
|
1480 | - foreach ($files as &$entry) { |
|
1481 | - if ($entry->getName() === $entryName) { |
|
1482 | - $entry->addSubEntry($rootEntry, $mountPoint); |
|
1483 | - } |
|
1484 | - } |
|
1485 | - } else { //mountpoint in this folder, add an entry for it |
|
1486 | - $rootEntry['name'] = $relativePath; |
|
1487 | - $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
1488 | - $permissions = $rootEntry['permissions']; |
|
1489 | - // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
1490 | - // for shared files/folders we use the permissions given by the owner |
|
1491 | - if ($mount instanceof MoveableMount) { |
|
1492 | - $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
1493 | - } else { |
|
1494 | - $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
1495 | - } |
|
1496 | - |
|
1497 | - //remove any existing entry with the same name |
|
1498 | - foreach ($files as $i => $file) { |
|
1499 | - if ($file['name'] === $rootEntry['name']) { |
|
1500 | - unset($files[$i]); |
|
1501 | - break; |
|
1502 | - } |
|
1503 | - } |
|
1504 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
1505 | - |
|
1506 | - // if sharing was disabled for the user we remove the share permissions |
|
1507 | - if (\OCP\Util::isSharingDisabledForUser()) { |
|
1508 | - $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1509 | - } |
|
1510 | - |
|
1511 | - $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
1512 | - $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
1513 | - } |
|
1514 | - } |
|
1515 | - } |
|
1516 | - } |
|
1517 | - |
|
1518 | - if ($mimetype_filter) { |
|
1519 | - $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
1520 | - if (strpos($mimetype_filter, '/')) { |
|
1521 | - return $file->getMimetype() === $mimetype_filter; |
|
1522 | - } else { |
|
1523 | - return $file->getMimePart() === $mimetype_filter; |
|
1524 | - } |
|
1525 | - }); |
|
1526 | - } |
|
1527 | - |
|
1528 | - return $files; |
|
1529 | - } else { |
|
1530 | - return []; |
|
1531 | - } |
|
1532 | - } |
|
1533 | - |
|
1534 | - /** |
|
1535 | - * change file metadata |
|
1536 | - * |
|
1537 | - * @param string $path |
|
1538 | - * @param array|\OCP\Files\FileInfo $data |
|
1539 | - * @return int |
|
1540 | - * |
|
1541 | - * returns the fileid of the updated file |
|
1542 | - */ |
|
1543 | - public function putFileInfo($path, $data) { |
|
1544 | - $this->assertPathLength($path); |
|
1545 | - if ($data instanceof FileInfo) { |
|
1546 | - $data = $data->getData(); |
|
1547 | - } |
|
1548 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1549 | - /** |
|
1550 | - * @var \OC\Files\Storage\Storage $storage |
|
1551 | - * @var string $internalPath |
|
1552 | - */ |
|
1553 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
1554 | - if ($storage) { |
|
1555 | - $cache = $storage->getCache($path); |
|
1556 | - |
|
1557 | - if (!$cache->inCache($internalPath)) { |
|
1558 | - $scanner = $storage->getScanner($internalPath); |
|
1559 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1560 | - } |
|
1561 | - |
|
1562 | - return $cache->put($internalPath, $data); |
|
1563 | - } else { |
|
1564 | - return -1; |
|
1565 | - } |
|
1566 | - } |
|
1567 | - |
|
1568 | - /** |
|
1569 | - * search for files with the name matching $query |
|
1570 | - * |
|
1571 | - * @param string $query |
|
1572 | - * @return FileInfo[] |
|
1573 | - */ |
|
1574 | - public function search($query) { |
|
1575 | - return $this->searchCommon('search', array('%' . $query . '%')); |
|
1576 | - } |
|
1577 | - |
|
1578 | - /** |
|
1579 | - * search for files with the name matching $query |
|
1580 | - * |
|
1581 | - * @param string $query |
|
1582 | - * @return FileInfo[] |
|
1583 | - */ |
|
1584 | - public function searchRaw($query) { |
|
1585 | - return $this->searchCommon('search', array($query)); |
|
1586 | - } |
|
1587 | - |
|
1588 | - /** |
|
1589 | - * search for files by mimetype |
|
1590 | - * |
|
1591 | - * @param string $mimetype |
|
1592 | - * @return FileInfo[] |
|
1593 | - */ |
|
1594 | - public function searchByMime($mimetype) { |
|
1595 | - return $this->searchCommon('searchByMime', array($mimetype)); |
|
1596 | - } |
|
1597 | - |
|
1598 | - /** |
|
1599 | - * search for files by tag |
|
1600 | - * |
|
1601 | - * @param string|int $tag name or tag id |
|
1602 | - * @param string $userId owner of the tags |
|
1603 | - * @return FileInfo[] |
|
1604 | - */ |
|
1605 | - public function searchByTag($tag, $userId) { |
|
1606 | - return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
1607 | - } |
|
1608 | - |
|
1609 | - /** |
|
1610 | - * @param string $method cache method |
|
1611 | - * @param array $args |
|
1612 | - * @return FileInfo[] |
|
1613 | - */ |
|
1614 | - private function searchCommon($method, $args) { |
|
1615 | - $files = array(); |
|
1616 | - $rootLength = strlen($this->fakeRoot); |
|
1617 | - |
|
1618 | - $mount = $this->getMount(''); |
|
1619 | - $mountPoint = $mount->getMountPoint(); |
|
1620 | - $storage = $mount->getStorage(); |
|
1621 | - if ($storage) { |
|
1622 | - $cache = $storage->getCache(''); |
|
1623 | - |
|
1624 | - $results = call_user_func_array(array($cache, $method), $args); |
|
1625 | - foreach ($results as $result) { |
|
1626 | - if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
1627 | - $internalPath = $result['path']; |
|
1628 | - $path = $mountPoint . $result['path']; |
|
1629 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
1630 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1631 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1632 | - } |
|
1633 | - } |
|
1634 | - |
|
1635 | - $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
1636 | - foreach ($mounts as $mount) { |
|
1637 | - $mountPoint = $mount->getMountPoint(); |
|
1638 | - $storage = $mount->getStorage(); |
|
1639 | - if ($storage) { |
|
1640 | - $cache = $storage->getCache(''); |
|
1641 | - |
|
1642 | - $relativeMountPoint = substr($mountPoint, $rootLength); |
|
1643 | - $results = call_user_func_array(array($cache, $method), $args); |
|
1644 | - if ($results) { |
|
1645 | - foreach ($results as $result) { |
|
1646 | - $internalPath = $result['path']; |
|
1647 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
1648 | - $path = rtrim($mountPoint . $internalPath, '/'); |
|
1649 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1650 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1651 | - } |
|
1652 | - } |
|
1653 | - } |
|
1654 | - } |
|
1655 | - } |
|
1656 | - return $files; |
|
1657 | - } |
|
1658 | - |
|
1659 | - /** |
|
1660 | - * Get the owner for a file or folder |
|
1661 | - * |
|
1662 | - * @param string $path |
|
1663 | - * @return string the user id of the owner |
|
1664 | - * @throws NotFoundException |
|
1665 | - */ |
|
1666 | - public function getOwner($path) { |
|
1667 | - $info = $this->getFileInfo($path); |
|
1668 | - if (!$info) { |
|
1669 | - throw new NotFoundException($path . ' not found while trying to get owner'); |
|
1670 | - } |
|
1671 | - return $info->getOwner()->getUID(); |
|
1672 | - } |
|
1673 | - |
|
1674 | - /** |
|
1675 | - * get the ETag for a file or folder |
|
1676 | - * |
|
1677 | - * @param string $path |
|
1678 | - * @return string |
|
1679 | - */ |
|
1680 | - public function getETag($path) { |
|
1681 | - /** |
|
1682 | - * @var Storage\Storage $storage |
|
1683 | - * @var string $internalPath |
|
1684 | - */ |
|
1685 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
1686 | - if ($storage) { |
|
1687 | - return $storage->getETag($internalPath); |
|
1688 | - } else { |
|
1689 | - return null; |
|
1690 | - } |
|
1691 | - } |
|
1692 | - |
|
1693 | - /** |
|
1694 | - * Get the path of a file by id, relative to the view |
|
1695 | - * |
|
1696 | - * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
|
1697 | - * |
|
1698 | - * @param int $id |
|
1699 | - * @throws NotFoundException |
|
1700 | - * @return string |
|
1701 | - */ |
|
1702 | - public function getPath($id) { |
|
1703 | - $id = (int)$id; |
|
1704 | - $manager = Filesystem::getMountManager(); |
|
1705 | - $mounts = $manager->findIn($this->fakeRoot); |
|
1706 | - $mounts[] = $manager->find($this->fakeRoot); |
|
1707 | - // reverse the array so we start with the storage this view is in |
|
1708 | - // which is the most likely to contain the file we're looking for |
|
1709 | - $mounts = array_reverse($mounts); |
|
1710 | - foreach ($mounts as $mount) { |
|
1711 | - /** |
|
1712 | - * @var \OC\Files\Mount\MountPoint $mount |
|
1713 | - */ |
|
1714 | - if ($mount->getStorage()) { |
|
1715 | - $cache = $mount->getStorage()->getCache(); |
|
1716 | - $internalPath = $cache->getPathById($id); |
|
1717 | - if (is_string($internalPath)) { |
|
1718 | - $fullPath = $mount->getMountPoint() . $internalPath; |
|
1719 | - if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
1720 | - return $path; |
|
1721 | - } |
|
1722 | - } |
|
1723 | - } |
|
1724 | - } |
|
1725 | - throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
1726 | - } |
|
1727 | - |
|
1728 | - /** |
|
1729 | - * @param string $path |
|
1730 | - * @throws InvalidPathException |
|
1731 | - */ |
|
1732 | - private function assertPathLength($path) { |
|
1733 | - $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
1734 | - // Check for the string length - performed using isset() instead of strlen() |
|
1735 | - // because isset() is about 5x-40x faster. |
|
1736 | - if (isset($path[$maxLen])) { |
|
1737 | - $pathLen = strlen($path); |
|
1738 | - throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
1739 | - } |
|
1740 | - } |
|
1741 | - |
|
1742 | - /** |
|
1743 | - * check if it is allowed to move a mount point to a given target. |
|
1744 | - * It is not allowed to move a mount point into a different mount point or |
|
1745 | - * into an already shared folder |
|
1746 | - * |
|
1747 | - * @param string $target path |
|
1748 | - * @return boolean |
|
1749 | - */ |
|
1750 | - private function isTargetAllowed($target) { |
|
1751 | - |
|
1752 | - list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target); |
|
1753 | - if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
1754 | - \OCP\Util::writeLog('files', |
|
1755 | - 'It is not allowed to move one mount point into another one', |
|
1756 | - ILogger::DEBUG); |
|
1757 | - return false; |
|
1758 | - } |
|
1759 | - |
|
1760 | - // note: cannot use the view because the target is already locked |
|
1761 | - $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
1762 | - if ($fileId === -1) { |
|
1763 | - // target might not exist, need to check parent instead |
|
1764 | - $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
1765 | - } |
|
1766 | - |
|
1767 | - // check if any of the parents were shared by the current owner (include collections) |
|
1768 | - $shares = \OCP\Share::getItemShared( |
|
1769 | - 'folder', |
|
1770 | - $fileId, |
|
1771 | - \OCP\Share::FORMAT_NONE, |
|
1772 | - null, |
|
1773 | - true |
|
1774 | - ); |
|
1775 | - |
|
1776 | - if (count($shares) > 0) { |
|
1777 | - \OCP\Util::writeLog('files', |
|
1778 | - 'It is not allowed to move one mount point into a shared folder', |
|
1779 | - ILogger::DEBUG); |
|
1780 | - return false; |
|
1781 | - } |
|
1782 | - |
|
1783 | - return true; |
|
1784 | - } |
|
1785 | - |
|
1786 | - /** |
|
1787 | - * Get a fileinfo object for files that are ignored in the cache (part files) |
|
1788 | - * |
|
1789 | - * @param string $path |
|
1790 | - * @return \OCP\Files\FileInfo |
|
1791 | - */ |
|
1792 | - private function getPartFileInfo($path) { |
|
1793 | - $mount = $this->getMount($path); |
|
1794 | - $storage = $mount->getStorage(); |
|
1795 | - $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
1796 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1797 | - return new FileInfo( |
|
1798 | - $this->getAbsolutePath($path), |
|
1799 | - $storage, |
|
1800 | - $internalPath, |
|
1801 | - [ |
|
1802 | - 'fileid' => null, |
|
1803 | - 'mimetype' => $storage->getMimeType($internalPath), |
|
1804 | - 'name' => basename($path), |
|
1805 | - 'etag' => null, |
|
1806 | - 'size' => $storage->filesize($internalPath), |
|
1807 | - 'mtime' => $storage->filemtime($internalPath), |
|
1808 | - 'encrypted' => false, |
|
1809 | - 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
1810 | - ], |
|
1811 | - $mount, |
|
1812 | - $owner |
|
1813 | - ); |
|
1814 | - } |
|
1815 | - |
|
1816 | - /** |
|
1817 | - * @param string $path |
|
1818 | - * @param string $fileName |
|
1819 | - * @throws InvalidPathException |
|
1820 | - */ |
|
1821 | - public function verifyPath($path, $fileName) { |
|
1822 | - try { |
|
1823 | - /** @type \OCP\Files\Storage $storage */ |
|
1824 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
1825 | - $storage->verifyPath($internalPath, $fileName); |
|
1826 | - } catch (ReservedWordException $ex) { |
|
1827 | - $l = \OC::$server->getL10N('lib'); |
|
1828 | - throw new InvalidPathException($l->t('File name is a reserved word')); |
|
1829 | - } catch (InvalidCharacterInPathException $ex) { |
|
1830 | - $l = \OC::$server->getL10N('lib'); |
|
1831 | - throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
1832 | - } catch (FileNameTooLongException $ex) { |
|
1833 | - $l = \OC::$server->getL10N('lib'); |
|
1834 | - throw new InvalidPathException($l->t('File name is too long')); |
|
1835 | - } catch (InvalidDirectoryException $ex) { |
|
1836 | - $l = \OC::$server->getL10N('lib'); |
|
1837 | - throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
1838 | - } catch (EmptyFileNameException $ex) { |
|
1839 | - $l = \OC::$server->getL10N('lib'); |
|
1840 | - throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
1841 | - } |
|
1842 | - } |
|
1843 | - |
|
1844 | - /** |
|
1845 | - * get all parent folders of $path |
|
1846 | - * |
|
1847 | - * @param string $path |
|
1848 | - * @return string[] |
|
1849 | - */ |
|
1850 | - private function getParents($path) { |
|
1851 | - $path = trim($path, '/'); |
|
1852 | - if (!$path) { |
|
1853 | - return []; |
|
1854 | - } |
|
1855 | - |
|
1856 | - $parts = explode('/', $path); |
|
1857 | - |
|
1858 | - // remove the single file |
|
1859 | - array_pop($parts); |
|
1860 | - $result = array('/'); |
|
1861 | - $resultPath = ''; |
|
1862 | - foreach ($parts as $part) { |
|
1863 | - if ($part) { |
|
1864 | - $resultPath .= '/' . $part; |
|
1865 | - $result[] = $resultPath; |
|
1866 | - } |
|
1867 | - } |
|
1868 | - return $result; |
|
1869 | - } |
|
1870 | - |
|
1871 | - /** |
|
1872 | - * Returns the mount point for which to lock |
|
1873 | - * |
|
1874 | - * @param string $absolutePath absolute path |
|
1875 | - * @param bool $useParentMount true to return parent mount instead of whatever |
|
1876 | - * is mounted directly on the given path, false otherwise |
|
1877 | - * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
|
1878 | - */ |
|
1879 | - private function getMountForLock($absolutePath, $useParentMount = false) { |
|
1880 | - $results = []; |
|
1881 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
1882 | - if (!$mount) { |
|
1883 | - return $results; |
|
1884 | - } |
|
1885 | - |
|
1886 | - if ($useParentMount) { |
|
1887 | - // find out if something is mounted directly on the path |
|
1888 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
1889 | - if ($internalPath === '') { |
|
1890 | - // resolve the parent mount instead |
|
1891 | - $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
1892 | - } |
|
1893 | - } |
|
1894 | - |
|
1895 | - return $mount; |
|
1896 | - } |
|
1897 | - |
|
1898 | - /** |
|
1899 | - * Lock the given path |
|
1900 | - * |
|
1901 | - * @param string $path the path of the file to lock, relative to the view |
|
1902 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1903 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1904 | - * |
|
1905 | - * @return bool False if the path is excluded from locking, true otherwise |
|
1906 | - * @throws \OCP\Lock\LockedException if the path is already locked |
|
1907 | - */ |
|
1908 | - private function lockPath($path, $type, $lockMountPoint = false) { |
|
1909 | - $absolutePath = $this->getAbsolutePath($path); |
|
1910 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1911 | - if (!$this->shouldLockFile($absolutePath)) { |
|
1912 | - return false; |
|
1913 | - } |
|
1914 | - |
|
1915 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
1916 | - if ($mount) { |
|
1917 | - try { |
|
1918 | - $storage = $mount->getStorage(); |
|
1919 | - if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
1920 | - $storage->acquireLock( |
|
1921 | - $mount->getInternalPath($absolutePath), |
|
1922 | - $type, |
|
1923 | - $this->lockingProvider |
|
1924 | - ); |
|
1925 | - } |
|
1926 | - } catch (\OCP\Lock\LockedException $e) { |
|
1927 | - // rethrow with the a human-readable path |
|
1928 | - throw new \OCP\Lock\LockedException( |
|
1929 | - $this->getPathRelativeToFiles($absolutePath), |
|
1930 | - $e |
|
1931 | - ); |
|
1932 | - } |
|
1933 | - } |
|
1934 | - |
|
1935 | - return true; |
|
1936 | - } |
|
1937 | - |
|
1938 | - /** |
|
1939 | - * Change the lock type |
|
1940 | - * |
|
1941 | - * @param string $path the path of the file to lock, relative to the view |
|
1942 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1943 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1944 | - * |
|
1945 | - * @return bool False if the path is excluded from locking, true otherwise |
|
1946 | - * @throws \OCP\Lock\LockedException if the path is already locked |
|
1947 | - */ |
|
1948 | - public function changeLock($path, $type, $lockMountPoint = false) { |
|
1949 | - $path = Filesystem::normalizePath($path); |
|
1950 | - $absolutePath = $this->getAbsolutePath($path); |
|
1951 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1952 | - if (!$this->shouldLockFile($absolutePath)) { |
|
1953 | - return false; |
|
1954 | - } |
|
1955 | - |
|
1956 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
1957 | - if ($mount) { |
|
1958 | - try { |
|
1959 | - $storage = $mount->getStorage(); |
|
1960 | - if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
1961 | - $storage->changeLock( |
|
1962 | - $mount->getInternalPath($absolutePath), |
|
1963 | - $type, |
|
1964 | - $this->lockingProvider |
|
1965 | - ); |
|
1966 | - } |
|
1967 | - } catch (\OCP\Lock\LockedException $e) { |
|
1968 | - try { |
|
1969 | - // rethrow with the a human-readable path |
|
1970 | - throw new \OCP\Lock\LockedException( |
|
1971 | - $this->getPathRelativeToFiles($absolutePath), |
|
1972 | - $e |
|
1973 | - ); |
|
1974 | - } catch (\InvalidArgumentException $e) { |
|
1975 | - throw new \OCP\Lock\LockedException( |
|
1976 | - $absolutePath, |
|
1977 | - $e |
|
1978 | - ); |
|
1979 | - } |
|
1980 | - } |
|
1981 | - } |
|
1982 | - |
|
1983 | - return true; |
|
1984 | - } |
|
1985 | - |
|
1986 | - /** |
|
1987 | - * Unlock the given path |
|
1988 | - * |
|
1989 | - * @param string $path the path of the file to unlock, 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 | - */ |
|
1995 | - private function unlockPath($path, $type, $lockMountPoint = false) { |
|
1996 | - $absolutePath = $this->getAbsolutePath($path); |
|
1997 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1998 | - if (!$this->shouldLockFile($absolutePath)) { |
|
1999 | - return false; |
|
2000 | - } |
|
2001 | - |
|
2002 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
2003 | - if ($mount) { |
|
2004 | - $storage = $mount->getStorage(); |
|
2005 | - if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
2006 | - $storage->releaseLock( |
|
2007 | - $mount->getInternalPath($absolutePath), |
|
2008 | - $type, |
|
2009 | - $this->lockingProvider |
|
2010 | - ); |
|
2011 | - } |
|
2012 | - } |
|
2013 | - |
|
2014 | - return true; |
|
2015 | - } |
|
2016 | - |
|
2017 | - /** |
|
2018 | - * Lock a path and all its parents up to the root of the view |
|
2019 | - * |
|
2020 | - * @param string $path the path of the file to lock relative to the view |
|
2021 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2022 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2023 | - * |
|
2024 | - * @return bool False if the path is excluded from locking, true otherwise |
|
2025 | - */ |
|
2026 | - public function lockFile($path, $type, $lockMountPoint = false) { |
|
2027 | - $absolutePath = $this->getAbsolutePath($path); |
|
2028 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2029 | - if (!$this->shouldLockFile($absolutePath)) { |
|
2030 | - return false; |
|
2031 | - } |
|
2032 | - |
|
2033 | - $this->lockPath($path, $type, $lockMountPoint); |
|
2034 | - |
|
2035 | - $parents = $this->getParents($path); |
|
2036 | - foreach ($parents as $parent) { |
|
2037 | - $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2038 | - } |
|
2039 | - |
|
2040 | - return true; |
|
2041 | - } |
|
2042 | - |
|
2043 | - /** |
|
2044 | - * Unlock a path and all its parents up to the root of the view |
|
2045 | - * |
|
2046 | - * @param string $path the path of the file to lock relative to the view |
|
2047 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2048 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2049 | - * |
|
2050 | - * @return bool False if the path is excluded from locking, true otherwise |
|
2051 | - */ |
|
2052 | - public function unlockFile($path, $type, $lockMountPoint = false) { |
|
2053 | - $absolutePath = $this->getAbsolutePath($path); |
|
2054 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2055 | - if (!$this->shouldLockFile($absolutePath)) { |
|
2056 | - return false; |
|
2057 | - } |
|
2058 | - |
|
2059 | - $this->unlockPath($path, $type, $lockMountPoint); |
|
2060 | - |
|
2061 | - $parents = $this->getParents($path); |
|
2062 | - foreach ($parents as $parent) { |
|
2063 | - $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2064 | - } |
|
2065 | - |
|
2066 | - return true; |
|
2067 | - } |
|
2068 | - |
|
2069 | - /** |
|
2070 | - * Only lock files in data/user/files/ |
|
2071 | - * |
|
2072 | - * @param string $path Absolute path to the file/folder we try to (un)lock |
|
2073 | - * @return bool |
|
2074 | - */ |
|
2075 | - protected function shouldLockFile($path) { |
|
2076 | - $path = Filesystem::normalizePath($path); |
|
2077 | - |
|
2078 | - $pathSegments = explode('/', $path); |
|
2079 | - if (isset($pathSegments[2])) { |
|
2080 | - // E.g.: /username/files/path-to-file |
|
2081 | - return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
2082 | - } |
|
2083 | - |
|
2084 | - return strpos($path, '/appdata_') !== 0; |
|
2085 | - } |
|
2086 | - |
|
2087 | - /** |
|
2088 | - * Shortens the given absolute path to be relative to |
|
2089 | - * "$user/files". |
|
2090 | - * |
|
2091 | - * @param string $absolutePath absolute path which is under "files" |
|
2092 | - * |
|
2093 | - * @return string path relative to "files" with trimmed slashes or null |
|
2094 | - * if the path was NOT relative to files |
|
2095 | - * |
|
2096 | - * @throws \InvalidArgumentException if the given path was not under "files" |
|
2097 | - * @since 8.1.0 |
|
2098 | - */ |
|
2099 | - public function getPathRelativeToFiles($absolutePath) { |
|
2100 | - $path = Filesystem::normalizePath($absolutePath); |
|
2101 | - $parts = explode('/', trim($path, '/'), 3); |
|
2102 | - // "$user", "files", "path/to/dir" |
|
2103 | - if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
2104 | - $this->logger->error( |
|
2105 | - '$absolutePath must be relative to "files", value is "%s"', |
|
2106 | - [ |
|
2107 | - $absolutePath |
|
2108 | - ] |
|
2109 | - ); |
|
2110 | - throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
2111 | - } |
|
2112 | - if (isset($parts[2])) { |
|
2113 | - return $parts[2]; |
|
2114 | - } |
|
2115 | - return ''; |
|
2116 | - } |
|
2117 | - |
|
2118 | - /** |
|
2119 | - * @param string $filename |
|
2120 | - * @return array |
|
2121 | - * @throws \OC\User\NoUserException |
|
2122 | - * @throws NotFoundException |
|
2123 | - */ |
|
2124 | - public function getUidAndFilename($filename) { |
|
2125 | - $info = $this->getFileInfo($filename); |
|
2126 | - if (!$info instanceof \OCP\Files\FileInfo) { |
|
2127 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
2128 | - } |
|
2129 | - $uid = $info->getOwner()->getUID(); |
|
2130 | - if ($uid != \OCP\User::getUser()) { |
|
2131 | - Filesystem::initMountPoints($uid); |
|
2132 | - $ownerView = new View('/' . $uid . '/files'); |
|
2133 | - try { |
|
2134 | - $filename = $ownerView->getPath($info['fileid']); |
|
2135 | - } catch (NotFoundException $e) { |
|
2136 | - throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
2137 | - } |
|
2138 | - } |
|
2139 | - return [$uid, $filename]; |
|
2140 | - } |
|
2141 | - |
|
2142 | - /** |
|
2143 | - * Creates parent non-existing folders |
|
2144 | - * |
|
2145 | - * @param string $filePath |
|
2146 | - * @return bool |
|
2147 | - */ |
|
2148 | - private function createParentDirectories($filePath) { |
|
2149 | - $directoryParts = explode('/', $filePath); |
|
2150 | - $directoryParts = array_filter($directoryParts); |
|
2151 | - foreach ($directoryParts as $key => $part) { |
|
2152 | - $currentPathElements = array_slice($directoryParts, 0, $key); |
|
2153 | - $currentPath = '/' . implode('/', $currentPathElements); |
|
2154 | - if ($this->is_file($currentPath)) { |
|
2155 | - return false; |
|
2156 | - } |
|
2157 | - if (!$this->file_exists($currentPath)) { |
|
2158 | - $this->mkdir($currentPath); |
|
2159 | - } |
|
2160 | - } |
|
2161 | - |
|
2162 | - return true; |
|
2163 | - } |
|
84 | + /** @var string */ |
|
85 | + private $fakeRoot = ''; |
|
86 | + |
|
87 | + /** |
|
88 | + * @var \OCP\Lock\ILockingProvider |
|
89 | + */ |
|
90 | + protected $lockingProvider; |
|
91 | + |
|
92 | + private $lockingEnabled; |
|
93 | + |
|
94 | + private $updaterEnabled = true; |
|
95 | + |
|
96 | + /** @var \OC\User\Manager */ |
|
97 | + private $userManager; |
|
98 | + |
|
99 | + /** @var \OCP\ILogger */ |
|
100 | + private $logger; |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $root |
|
104 | + * @throws \Exception If $root contains an invalid path |
|
105 | + */ |
|
106 | + public function __construct($root = '') { |
|
107 | + if (is_null($root)) { |
|
108 | + throw new \InvalidArgumentException('Root can\'t be null'); |
|
109 | + } |
|
110 | + if (!Filesystem::isValidPath($root)) { |
|
111 | + throw new \Exception(); |
|
112 | + } |
|
113 | + |
|
114 | + $this->fakeRoot = $root; |
|
115 | + $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
116 | + $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
117 | + $this->userManager = \OC::$server->getUserManager(); |
|
118 | + $this->logger = \OC::$server->getLogger(); |
|
119 | + } |
|
120 | + |
|
121 | + public function getAbsolutePath($path = '/') { |
|
122 | + if ($path === null) { |
|
123 | + return null; |
|
124 | + } |
|
125 | + $this->assertPathLength($path); |
|
126 | + if ($path === '') { |
|
127 | + $path = '/'; |
|
128 | + } |
|
129 | + if ($path[0] !== '/') { |
|
130 | + $path = '/' . $path; |
|
131 | + } |
|
132 | + return $this->fakeRoot . $path; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * change the root to a fake root |
|
137 | + * |
|
138 | + * @param string $fakeRoot |
|
139 | + * @return boolean|null |
|
140 | + */ |
|
141 | + public function chroot($fakeRoot) { |
|
142 | + if (!$fakeRoot == '') { |
|
143 | + if ($fakeRoot[0] !== '/') { |
|
144 | + $fakeRoot = '/' . $fakeRoot; |
|
145 | + } |
|
146 | + } |
|
147 | + $this->fakeRoot = $fakeRoot; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * get the fake root |
|
152 | + * |
|
153 | + * @return string |
|
154 | + */ |
|
155 | + public function getRoot() { |
|
156 | + return $this->fakeRoot; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * get path relative to the root of the view |
|
161 | + * |
|
162 | + * @param string $path |
|
163 | + * @return string |
|
164 | + */ |
|
165 | + public function getRelativePath($path) { |
|
166 | + $this->assertPathLength($path); |
|
167 | + if ($this->fakeRoot == '') { |
|
168 | + return $path; |
|
169 | + } |
|
170 | + |
|
171 | + if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
172 | + return '/'; |
|
173 | + } |
|
174 | + |
|
175 | + // missing slashes can cause wrong matches! |
|
176 | + $root = rtrim($this->fakeRoot, '/') . '/'; |
|
177 | + |
|
178 | + if (strpos($path, $root) !== 0) { |
|
179 | + return null; |
|
180 | + } else { |
|
181 | + $path = substr($path, strlen($this->fakeRoot)); |
|
182 | + if (strlen($path) === 0) { |
|
183 | + return '/'; |
|
184 | + } else { |
|
185 | + return $path; |
|
186 | + } |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * get the mountpoint of the storage object for a path |
|
192 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
193 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
194 | + * and does not take the chroot into account ) |
|
195 | + * |
|
196 | + * @param string $path |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + public function getMountPoint($path) { |
|
200 | + return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * get the mountpoint of the storage object for a path |
|
205 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
206 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
207 | + * and does not take the chroot into account ) |
|
208 | + * |
|
209 | + * @param string $path |
|
210 | + * @return \OCP\Files\Mount\IMountPoint |
|
211 | + */ |
|
212 | + public function getMount($path) { |
|
213 | + return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * resolve a path to a storage and internal path |
|
218 | + * |
|
219 | + * @param string $path |
|
220 | + * @return array an array consisting of the storage and the internal path |
|
221 | + */ |
|
222 | + public function resolvePath($path) { |
|
223 | + $a = $this->getAbsolutePath($path); |
|
224 | + $p = Filesystem::normalizePath($a); |
|
225 | + return Filesystem::resolvePath($p); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * return the path to a local version of the file |
|
230 | + * we need this because we can't know if a file is stored local or not from |
|
231 | + * outside the filestorage and for some purposes a local file is needed |
|
232 | + * |
|
233 | + * @param string $path |
|
234 | + * @return string |
|
235 | + */ |
|
236 | + public function getLocalFile($path) { |
|
237 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
238 | + $path = $this->getAbsolutePath($path); |
|
239 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
240 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
241 | + return $storage->getLocalFile($internalPath); |
|
242 | + } else { |
|
243 | + return null; |
|
244 | + } |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * @param string $path |
|
249 | + * @return string |
|
250 | + */ |
|
251 | + public function getLocalFolder($path) { |
|
252 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
253 | + $path = $this->getAbsolutePath($path); |
|
254 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
255 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
256 | + return $storage->getLocalFolder($internalPath); |
|
257 | + } else { |
|
258 | + return null; |
|
259 | + } |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * the following functions operate with arguments and return values identical |
|
264 | + * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
265 | + * for \OC\Files\Storage\Storage via basicOperation(). |
|
266 | + */ |
|
267 | + public function mkdir($path) { |
|
268 | + return $this->basicOperation('mkdir', $path, array('create', 'write')); |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * remove mount point |
|
273 | + * |
|
274 | + * @param \OC\Files\Mount\MoveableMount $mount |
|
275 | + * @param string $path relative to data/ |
|
276 | + * @return boolean |
|
277 | + */ |
|
278 | + protected function removeMount($mount, $path) { |
|
279 | + if ($mount instanceof MoveableMount) { |
|
280 | + // cut of /user/files to get the relative path to data/user/files |
|
281 | + $pathParts = explode('/', $path, 4); |
|
282 | + $relPath = '/' . $pathParts[3]; |
|
283 | + $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
284 | + \OC_Hook::emit( |
|
285 | + Filesystem::CLASSNAME, "umount", |
|
286 | + array(Filesystem::signal_param_path => $relPath) |
|
287 | + ); |
|
288 | + $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
289 | + $result = $mount->removeMount(); |
|
290 | + $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
291 | + if ($result) { |
|
292 | + \OC_Hook::emit( |
|
293 | + Filesystem::CLASSNAME, "post_umount", |
|
294 | + array(Filesystem::signal_param_path => $relPath) |
|
295 | + ); |
|
296 | + } |
|
297 | + $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
298 | + return $result; |
|
299 | + } else { |
|
300 | + // do not allow deleting the storage's root / the mount point |
|
301 | + // because for some storages it might delete the whole contents |
|
302 | + // but isn't supposed to work that way |
|
303 | + return false; |
|
304 | + } |
|
305 | + } |
|
306 | + |
|
307 | + public function disableCacheUpdate() { |
|
308 | + $this->updaterEnabled = false; |
|
309 | + } |
|
310 | + |
|
311 | + public function enableCacheUpdate() { |
|
312 | + $this->updaterEnabled = true; |
|
313 | + } |
|
314 | + |
|
315 | + protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
316 | + if ($this->updaterEnabled) { |
|
317 | + if (is_null($time)) { |
|
318 | + $time = time(); |
|
319 | + } |
|
320 | + $storage->getUpdater()->update($internalPath, $time); |
|
321 | + } |
|
322 | + } |
|
323 | + |
|
324 | + protected function removeUpdate(Storage $storage, $internalPath) { |
|
325 | + if ($this->updaterEnabled) { |
|
326 | + $storage->getUpdater()->remove($internalPath); |
|
327 | + } |
|
328 | + } |
|
329 | + |
|
330 | + protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
331 | + if ($this->updaterEnabled) { |
|
332 | + $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
333 | + } |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * @param string $path |
|
338 | + * @return bool|mixed |
|
339 | + */ |
|
340 | + public function rmdir($path) { |
|
341 | + $absolutePath = $this->getAbsolutePath($path); |
|
342 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
343 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
344 | + return $this->removeMount($mount, $absolutePath); |
|
345 | + } |
|
346 | + if ($this->is_dir($path)) { |
|
347 | + $result = $this->basicOperation('rmdir', $path, array('delete')); |
|
348 | + } else { |
|
349 | + $result = false; |
|
350 | + } |
|
351 | + |
|
352 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
353 | + $storage = $mount->getStorage(); |
|
354 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
355 | + $storage->getUpdater()->remove($internalPath); |
|
356 | + } |
|
357 | + return $result; |
|
358 | + } |
|
359 | + |
|
360 | + /** |
|
361 | + * @param string $path |
|
362 | + * @return resource |
|
363 | + */ |
|
364 | + public function opendir($path) { |
|
365 | + return $this->basicOperation('opendir', $path, array('read')); |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * @param string $path |
|
370 | + * @return bool|mixed |
|
371 | + */ |
|
372 | + public function is_dir($path) { |
|
373 | + if ($path == '/') { |
|
374 | + return true; |
|
375 | + } |
|
376 | + return $this->basicOperation('is_dir', $path); |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * @param string $path |
|
381 | + * @return bool|mixed |
|
382 | + */ |
|
383 | + public function is_file($path) { |
|
384 | + if ($path == '/') { |
|
385 | + return false; |
|
386 | + } |
|
387 | + return $this->basicOperation('is_file', $path); |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * @param string $path |
|
392 | + * @return mixed |
|
393 | + */ |
|
394 | + public function stat($path) { |
|
395 | + return $this->basicOperation('stat', $path); |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * @param string $path |
|
400 | + * @return mixed |
|
401 | + */ |
|
402 | + public function filetype($path) { |
|
403 | + return $this->basicOperation('filetype', $path); |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * @param string $path |
|
408 | + * @return mixed |
|
409 | + */ |
|
410 | + public function filesize($path) { |
|
411 | + return $this->basicOperation('filesize', $path); |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * @param string $path |
|
416 | + * @return bool|mixed |
|
417 | + * @throws \OCP\Files\InvalidPathException |
|
418 | + */ |
|
419 | + public function readfile($path) { |
|
420 | + $this->assertPathLength($path); |
|
421 | + @ob_end_clean(); |
|
422 | + $handle = $this->fopen($path, 'rb'); |
|
423 | + if ($handle) { |
|
424 | + $chunkSize = 8192; // 8 kB chunks |
|
425 | + while (!feof($handle)) { |
|
426 | + echo fread($handle, $chunkSize); |
|
427 | + flush(); |
|
428 | + } |
|
429 | + fclose($handle); |
|
430 | + return $this->filesize($path); |
|
431 | + } |
|
432 | + return false; |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * @param string $path |
|
437 | + * @param int $from |
|
438 | + * @param int $to |
|
439 | + * @return bool|mixed |
|
440 | + * @throws \OCP\Files\InvalidPathException |
|
441 | + * @throws \OCP\Files\UnseekableException |
|
442 | + */ |
|
443 | + public function readfilePart($path, $from, $to) { |
|
444 | + $this->assertPathLength($path); |
|
445 | + @ob_end_clean(); |
|
446 | + $handle = $this->fopen($path, 'rb'); |
|
447 | + if ($handle) { |
|
448 | + $chunkSize = 8192; // 8 kB chunks |
|
449 | + $startReading = true; |
|
450 | + |
|
451 | + if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
452 | + // forward file handle via chunked fread because fseek seem to have failed |
|
453 | + |
|
454 | + $end = $from + 1; |
|
455 | + while (!feof($handle) && ftell($handle) < $end) { |
|
456 | + $len = $from - ftell($handle); |
|
457 | + if ($len > $chunkSize) { |
|
458 | + $len = $chunkSize; |
|
459 | + } |
|
460 | + $result = fread($handle, $len); |
|
461 | + |
|
462 | + if ($result === false) { |
|
463 | + $startReading = false; |
|
464 | + break; |
|
465 | + } |
|
466 | + } |
|
467 | + } |
|
468 | + |
|
469 | + if ($startReading) { |
|
470 | + $end = $to + 1; |
|
471 | + while (!feof($handle) && ftell($handle) < $end) { |
|
472 | + $len = $end - ftell($handle); |
|
473 | + if ($len > $chunkSize) { |
|
474 | + $len = $chunkSize; |
|
475 | + } |
|
476 | + echo fread($handle, $len); |
|
477 | + flush(); |
|
478 | + } |
|
479 | + return ftell($handle) - $from; |
|
480 | + } |
|
481 | + |
|
482 | + throw new \OCP\Files\UnseekableException('fseek error'); |
|
483 | + } |
|
484 | + return false; |
|
485 | + } |
|
486 | + |
|
487 | + /** |
|
488 | + * @param string $path |
|
489 | + * @return mixed |
|
490 | + */ |
|
491 | + public function isCreatable($path) { |
|
492 | + return $this->basicOperation('isCreatable', $path); |
|
493 | + } |
|
494 | + |
|
495 | + /** |
|
496 | + * @param string $path |
|
497 | + * @return mixed |
|
498 | + */ |
|
499 | + public function isReadable($path) { |
|
500 | + return $this->basicOperation('isReadable', $path); |
|
501 | + } |
|
502 | + |
|
503 | + /** |
|
504 | + * @param string $path |
|
505 | + * @return mixed |
|
506 | + */ |
|
507 | + public function isUpdatable($path) { |
|
508 | + return $this->basicOperation('isUpdatable', $path); |
|
509 | + } |
|
510 | + |
|
511 | + /** |
|
512 | + * @param string $path |
|
513 | + * @return bool|mixed |
|
514 | + */ |
|
515 | + public function isDeletable($path) { |
|
516 | + $absolutePath = $this->getAbsolutePath($path); |
|
517 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
518 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
519 | + return $mount instanceof MoveableMount; |
|
520 | + } |
|
521 | + return $this->basicOperation('isDeletable', $path); |
|
522 | + } |
|
523 | + |
|
524 | + /** |
|
525 | + * @param string $path |
|
526 | + * @return mixed |
|
527 | + */ |
|
528 | + public function isSharable($path) { |
|
529 | + return $this->basicOperation('isSharable', $path); |
|
530 | + } |
|
531 | + |
|
532 | + /** |
|
533 | + * @param string $path |
|
534 | + * @return bool|mixed |
|
535 | + */ |
|
536 | + public function file_exists($path) { |
|
537 | + if ($path == '/') { |
|
538 | + return true; |
|
539 | + } |
|
540 | + return $this->basicOperation('file_exists', $path); |
|
541 | + } |
|
542 | + |
|
543 | + /** |
|
544 | + * @param string $path |
|
545 | + * @return mixed |
|
546 | + */ |
|
547 | + public function filemtime($path) { |
|
548 | + return $this->basicOperation('filemtime', $path); |
|
549 | + } |
|
550 | + |
|
551 | + /** |
|
552 | + * @param string $path |
|
553 | + * @param int|string $mtime |
|
554 | + * @return bool |
|
555 | + */ |
|
556 | + public function touch($path, $mtime = null) { |
|
557 | + if (!is_null($mtime) and !is_numeric($mtime)) { |
|
558 | + $mtime = strtotime($mtime); |
|
559 | + } |
|
560 | + |
|
561 | + $hooks = array('touch'); |
|
562 | + |
|
563 | + if (!$this->file_exists($path)) { |
|
564 | + $hooks[] = 'create'; |
|
565 | + $hooks[] = 'write'; |
|
566 | + } |
|
567 | + $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
568 | + if (!$result) { |
|
569 | + // If create file fails because of permissions on external storage like SMB folders, |
|
570 | + // check file exists and return false if not. |
|
571 | + if (!$this->file_exists($path)) { |
|
572 | + return false; |
|
573 | + } |
|
574 | + if (is_null($mtime)) { |
|
575 | + $mtime = time(); |
|
576 | + } |
|
577 | + //if native touch fails, we emulate it by changing the mtime in the cache |
|
578 | + $this->putFileInfo($path, array('mtime' => floor($mtime))); |
|
579 | + } |
|
580 | + return true; |
|
581 | + } |
|
582 | + |
|
583 | + /** |
|
584 | + * @param string $path |
|
585 | + * @return mixed |
|
586 | + */ |
|
587 | + public function file_get_contents($path) { |
|
588 | + return $this->basicOperation('file_get_contents', $path, array('read')); |
|
589 | + } |
|
590 | + |
|
591 | + /** |
|
592 | + * @param bool $exists |
|
593 | + * @param string $path |
|
594 | + * @param bool $run |
|
595 | + */ |
|
596 | + protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
597 | + if (!$exists) { |
|
598 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array( |
|
599 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
600 | + Filesystem::signal_param_run => &$run, |
|
601 | + )); |
|
602 | + } else { |
|
603 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array( |
|
604 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
605 | + Filesystem::signal_param_run => &$run, |
|
606 | + )); |
|
607 | + } |
|
608 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array( |
|
609 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
610 | + Filesystem::signal_param_run => &$run, |
|
611 | + )); |
|
612 | + } |
|
613 | + |
|
614 | + /** |
|
615 | + * @param bool $exists |
|
616 | + * @param string $path |
|
617 | + */ |
|
618 | + protected function emit_file_hooks_post($exists, $path) { |
|
619 | + if (!$exists) { |
|
620 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array( |
|
621 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
622 | + )); |
|
623 | + } else { |
|
624 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array( |
|
625 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
626 | + )); |
|
627 | + } |
|
628 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array( |
|
629 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
630 | + )); |
|
631 | + } |
|
632 | + |
|
633 | + /** |
|
634 | + * @param string $path |
|
635 | + * @param mixed $data |
|
636 | + * @return bool|mixed |
|
637 | + * @throws \Exception |
|
638 | + */ |
|
639 | + public function file_put_contents($path, $data) { |
|
640 | + if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
641 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
642 | + if (Filesystem::isValidPath($path) |
|
643 | + and !Filesystem::isFileBlacklisted($path) |
|
644 | + ) { |
|
645 | + $path = $this->getRelativePath($absolutePath); |
|
646 | + |
|
647 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
648 | + |
|
649 | + $exists = $this->file_exists($path); |
|
650 | + $run = true; |
|
651 | + if ($this->shouldEmitHooks($path)) { |
|
652 | + $this->emit_file_hooks_pre($exists, $path, $run); |
|
653 | + } |
|
654 | + if (!$run) { |
|
655 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
656 | + return false; |
|
657 | + } |
|
658 | + |
|
659 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
660 | + |
|
661 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
662 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
663 | + $target = $storage->fopen($internalPath, 'w'); |
|
664 | + if ($target) { |
|
665 | + list (, $result) = \OC_Helper::streamCopy($data, $target); |
|
666 | + fclose($target); |
|
667 | + fclose($data); |
|
668 | + |
|
669 | + $this->writeUpdate($storage, $internalPath); |
|
670 | + |
|
671 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
672 | + |
|
673 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
674 | + $this->emit_file_hooks_post($exists, $path); |
|
675 | + } |
|
676 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
677 | + return $result; |
|
678 | + } else { |
|
679 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
680 | + return false; |
|
681 | + } |
|
682 | + } else { |
|
683 | + return false; |
|
684 | + } |
|
685 | + } else { |
|
686 | + $hooks = $this->file_exists($path) ? array('update', 'write') : array('create', 'write'); |
|
687 | + return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
688 | + } |
|
689 | + } |
|
690 | + |
|
691 | + /** |
|
692 | + * @param string $path |
|
693 | + * @return bool|mixed |
|
694 | + */ |
|
695 | + public function unlink($path) { |
|
696 | + if ($path === '' || $path === '/') { |
|
697 | + // do not allow deleting the root |
|
698 | + return false; |
|
699 | + } |
|
700 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
701 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
702 | + $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
703 | + if ($mount and $mount->getInternalPath($absolutePath) === '') { |
|
704 | + return $this->removeMount($mount, $absolutePath); |
|
705 | + } |
|
706 | + if ($this->is_dir($path)) { |
|
707 | + $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
708 | + } else { |
|
709 | + $result = $this->basicOperation('unlink', $path, ['delete']); |
|
710 | + } |
|
711 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
712 | + $storage = $mount->getStorage(); |
|
713 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
714 | + $storage->getUpdater()->remove($internalPath); |
|
715 | + return true; |
|
716 | + } else { |
|
717 | + return $result; |
|
718 | + } |
|
719 | + } |
|
720 | + |
|
721 | + /** |
|
722 | + * @param string $directory |
|
723 | + * @return bool|mixed |
|
724 | + */ |
|
725 | + public function deleteAll($directory) { |
|
726 | + return $this->rmdir($directory); |
|
727 | + } |
|
728 | + |
|
729 | + /** |
|
730 | + * Rename/move a file or folder from the source path to target path. |
|
731 | + * |
|
732 | + * @param string $path1 source path |
|
733 | + * @param string $path2 target path |
|
734 | + * |
|
735 | + * @return bool|mixed |
|
736 | + */ |
|
737 | + public function rename($path1, $path2) { |
|
738 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
739 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
740 | + $result = false; |
|
741 | + if ( |
|
742 | + Filesystem::isValidPath($path2) |
|
743 | + and Filesystem::isValidPath($path1) |
|
744 | + and !Filesystem::isFileBlacklisted($path2) |
|
745 | + ) { |
|
746 | + $path1 = $this->getRelativePath($absolutePath1); |
|
747 | + $path2 = $this->getRelativePath($absolutePath2); |
|
748 | + $exists = $this->file_exists($path2); |
|
749 | + |
|
750 | + if ($path1 == null or $path2 == null) { |
|
751 | + return false; |
|
752 | + } |
|
753 | + |
|
754 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
755 | + try { |
|
756 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
757 | + |
|
758 | + $run = true; |
|
759 | + if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
|
760 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
761 | + $this->emit_file_hooks_pre($exists, $path2, $run); |
|
762 | + } elseif ($this->shouldEmitHooks($path1)) { |
|
763 | + \OC_Hook::emit( |
|
764 | + Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
765 | + array( |
|
766 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
767 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
768 | + Filesystem::signal_param_run => &$run |
|
769 | + ) |
|
770 | + ); |
|
771 | + } |
|
772 | + if ($run) { |
|
773 | + $this->verifyPath(dirname($path2), basename($path2)); |
|
774 | + |
|
775 | + $manager = Filesystem::getMountManager(); |
|
776 | + $mount1 = $this->getMount($path1); |
|
777 | + $mount2 = $this->getMount($path2); |
|
778 | + $storage1 = $mount1->getStorage(); |
|
779 | + $storage2 = $mount2->getStorage(); |
|
780 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
781 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
782 | + |
|
783 | + $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
784 | + try { |
|
785 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
786 | + |
|
787 | + if ($internalPath1 === '') { |
|
788 | + if ($mount1 instanceof MoveableMount) { |
|
789 | + if ($this->isTargetAllowed($absolutePath2)) { |
|
790 | + /** |
|
791 | + * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
792 | + */ |
|
793 | + $sourceMountPoint = $mount1->getMountPoint(); |
|
794 | + $result = $mount1->moveMount($absolutePath2); |
|
795 | + $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
796 | + } else { |
|
797 | + $result = false; |
|
798 | + } |
|
799 | + } else { |
|
800 | + $result = false; |
|
801 | + } |
|
802 | + // moving a file/folder within the same mount point |
|
803 | + } elseif ($storage1 === $storage2) { |
|
804 | + if ($storage1) { |
|
805 | + $result = $storage1->rename($internalPath1, $internalPath2); |
|
806 | + } else { |
|
807 | + $result = false; |
|
808 | + } |
|
809 | + // moving a file/folder between storages (from $storage1 to $storage2) |
|
810 | + } else { |
|
811 | + $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
812 | + } |
|
813 | + |
|
814 | + if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
815 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
816 | + $this->writeUpdate($storage2, $internalPath2); |
|
817 | + } else if ($result) { |
|
818 | + if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
819 | + $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
820 | + } |
|
821 | + } |
|
822 | + } catch(\Exception $e) { |
|
823 | + throw $e; |
|
824 | + } finally { |
|
825 | + $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
|
826 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
|
827 | + } |
|
828 | + |
|
829 | + if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
830 | + if ($this->shouldEmitHooks()) { |
|
831 | + $this->emit_file_hooks_post($exists, $path2); |
|
832 | + } |
|
833 | + } elseif ($result) { |
|
834 | + if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
|
835 | + \OC_Hook::emit( |
|
836 | + Filesystem::CLASSNAME, |
|
837 | + Filesystem::signal_post_rename, |
|
838 | + array( |
|
839 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
840 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
841 | + ) |
|
842 | + ); |
|
843 | + } |
|
844 | + } |
|
845 | + } |
|
846 | + } catch(\Exception $e) { |
|
847 | + throw $e; |
|
848 | + } finally { |
|
849 | + $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
850 | + $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
851 | + } |
|
852 | + } |
|
853 | + return $result; |
|
854 | + } |
|
855 | + |
|
856 | + /** |
|
857 | + * Copy a file/folder from the source path to target path |
|
858 | + * |
|
859 | + * @param string $path1 source path |
|
860 | + * @param string $path2 target path |
|
861 | + * @param bool $preserveMtime whether to preserve mtime on the copy |
|
862 | + * |
|
863 | + * @return bool|mixed |
|
864 | + */ |
|
865 | + public function copy($path1, $path2, $preserveMtime = false) { |
|
866 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
867 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
868 | + $result = false; |
|
869 | + if ( |
|
870 | + Filesystem::isValidPath($path2) |
|
871 | + and Filesystem::isValidPath($path1) |
|
872 | + and !Filesystem::isFileBlacklisted($path2) |
|
873 | + ) { |
|
874 | + $path1 = $this->getRelativePath($absolutePath1); |
|
875 | + $path2 = $this->getRelativePath($absolutePath2); |
|
876 | + |
|
877 | + if ($path1 == null or $path2 == null) { |
|
878 | + return false; |
|
879 | + } |
|
880 | + $run = true; |
|
881 | + |
|
882 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
|
883 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
|
884 | + $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
885 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
886 | + |
|
887 | + try { |
|
888 | + |
|
889 | + $exists = $this->file_exists($path2); |
|
890 | + if ($this->shouldEmitHooks()) { |
|
891 | + \OC_Hook::emit( |
|
892 | + Filesystem::CLASSNAME, |
|
893 | + Filesystem::signal_copy, |
|
894 | + array( |
|
895 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
896 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
897 | + Filesystem::signal_param_run => &$run |
|
898 | + ) |
|
899 | + ); |
|
900 | + $this->emit_file_hooks_pre($exists, $path2, $run); |
|
901 | + } |
|
902 | + if ($run) { |
|
903 | + $mount1 = $this->getMount($path1); |
|
904 | + $mount2 = $this->getMount($path2); |
|
905 | + $storage1 = $mount1->getStorage(); |
|
906 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
907 | + $storage2 = $mount2->getStorage(); |
|
908 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
909 | + |
|
910 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
911 | + $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
912 | + |
|
913 | + if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
914 | + if ($storage1) { |
|
915 | + $result = $storage1->copy($internalPath1, $internalPath2); |
|
916 | + } else { |
|
917 | + $result = false; |
|
918 | + } |
|
919 | + } else { |
|
920 | + $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
921 | + } |
|
922 | + |
|
923 | + $this->writeUpdate($storage2, $internalPath2); |
|
924 | + |
|
925 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
|
926 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
927 | + |
|
928 | + if ($this->shouldEmitHooks() && $result !== false) { |
|
929 | + \OC_Hook::emit( |
|
930 | + Filesystem::CLASSNAME, |
|
931 | + Filesystem::signal_post_copy, |
|
932 | + array( |
|
933 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
934 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
935 | + ) |
|
936 | + ); |
|
937 | + $this->emit_file_hooks_post($exists, $path2); |
|
938 | + } |
|
939 | + |
|
940 | + } |
|
941 | + } catch (\Exception $e) { |
|
942 | + $this->unlockFile($path2, $lockTypePath2); |
|
943 | + $this->unlockFile($path1, $lockTypePath1); |
|
944 | + throw $e; |
|
945 | + } |
|
946 | + |
|
947 | + $this->unlockFile($path2, $lockTypePath2); |
|
948 | + $this->unlockFile($path1, $lockTypePath1); |
|
949 | + |
|
950 | + } |
|
951 | + return $result; |
|
952 | + } |
|
953 | + |
|
954 | + /** |
|
955 | + * @param string $path |
|
956 | + * @param string $mode 'r' or 'w' |
|
957 | + * @return resource |
|
958 | + */ |
|
959 | + public function fopen($path, $mode) { |
|
960 | + $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
961 | + $hooks = array(); |
|
962 | + switch ($mode) { |
|
963 | + case 'r': |
|
964 | + $hooks[] = 'read'; |
|
965 | + break; |
|
966 | + case 'r+': |
|
967 | + case 'w+': |
|
968 | + case 'x+': |
|
969 | + case 'a+': |
|
970 | + $hooks[] = 'read'; |
|
971 | + $hooks[] = 'write'; |
|
972 | + break; |
|
973 | + case 'w': |
|
974 | + case 'x': |
|
975 | + case 'a': |
|
976 | + $hooks[] = 'write'; |
|
977 | + break; |
|
978 | + default: |
|
979 | + \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, ILogger::ERROR); |
|
980 | + } |
|
981 | + |
|
982 | + if ($mode !== 'r' && $mode !== 'w') { |
|
983 | + \OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends'); |
|
984 | + } |
|
985 | + |
|
986 | + return $this->basicOperation('fopen', $path, $hooks, $mode); |
|
987 | + } |
|
988 | + |
|
989 | + /** |
|
990 | + * @param string $path |
|
991 | + * @return bool|string |
|
992 | + * @throws \OCP\Files\InvalidPathException |
|
993 | + */ |
|
994 | + public function toTmpFile($path) { |
|
995 | + $this->assertPathLength($path); |
|
996 | + if (Filesystem::isValidPath($path)) { |
|
997 | + $source = $this->fopen($path, 'r'); |
|
998 | + if ($source) { |
|
999 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
1000 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
1001 | + file_put_contents($tmpFile, $source); |
|
1002 | + return $tmpFile; |
|
1003 | + } else { |
|
1004 | + return false; |
|
1005 | + } |
|
1006 | + } else { |
|
1007 | + return false; |
|
1008 | + } |
|
1009 | + } |
|
1010 | + |
|
1011 | + /** |
|
1012 | + * @param string $tmpFile |
|
1013 | + * @param string $path |
|
1014 | + * @return bool|mixed |
|
1015 | + * @throws \OCP\Files\InvalidPathException |
|
1016 | + */ |
|
1017 | + public function fromTmpFile($tmpFile, $path) { |
|
1018 | + $this->assertPathLength($path); |
|
1019 | + if (Filesystem::isValidPath($path)) { |
|
1020 | + |
|
1021 | + // Get directory that the file is going into |
|
1022 | + $filePath = dirname($path); |
|
1023 | + |
|
1024 | + // Create the directories if any |
|
1025 | + if (!$this->file_exists($filePath)) { |
|
1026 | + $result = $this->createParentDirectories($filePath); |
|
1027 | + if ($result === false) { |
|
1028 | + return false; |
|
1029 | + } |
|
1030 | + } |
|
1031 | + |
|
1032 | + $source = fopen($tmpFile, 'r'); |
|
1033 | + if ($source) { |
|
1034 | + $result = $this->file_put_contents($path, $source); |
|
1035 | + // $this->file_put_contents() might have already closed |
|
1036 | + // the resource, so we check it, before trying to close it |
|
1037 | + // to avoid messages in the error log. |
|
1038 | + if (is_resource($source)) { |
|
1039 | + fclose($source); |
|
1040 | + } |
|
1041 | + unlink($tmpFile); |
|
1042 | + return $result; |
|
1043 | + } else { |
|
1044 | + return false; |
|
1045 | + } |
|
1046 | + } else { |
|
1047 | + return false; |
|
1048 | + } |
|
1049 | + } |
|
1050 | + |
|
1051 | + |
|
1052 | + /** |
|
1053 | + * @param string $path |
|
1054 | + * @return mixed |
|
1055 | + * @throws \OCP\Files\InvalidPathException |
|
1056 | + */ |
|
1057 | + public function getMimeType($path) { |
|
1058 | + $this->assertPathLength($path); |
|
1059 | + return $this->basicOperation('getMimeType', $path); |
|
1060 | + } |
|
1061 | + |
|
1062 | + /** |
|
1063 | + * @param string $type |
|
1064 | + * @param string $path |
|
1065 | + * @param bool $raw |
|
1066 | + * @return bool|null|string |
|
1067 | + */ |
|
1068 | + public function hash($type, $path, $raw = false) { |
|
1069 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1070 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1071 | + if (Filesystem::isValidPath($path)) { |
|
1072 | + $path = $this->getRelativePath($absolutePath); |
|
1073 | + if ($path == null) { |
|
1074 | + return false; |
|
1075 | + } |
|
1076 | + if ($this->shouldEmitHooks($path)) { |
|
1077 | + \OC_Hook::emit( |
|
1078 | + Filesystem::CLASSNAME, |
|
1079 | + Filesystem::signal_read, |
|
1080 | + array(Filesystem::signal_param_path => $this->getHookPath($path)) |
|
1081 | + ); |
|
1082 | + } |
|
1083 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
1084 | + if ($storage) { |
|
1085 | + return $storage->hash($type, $internalPath, $raw); |
|
1086 | + } |
|
1087 | + } |
|
1088 | + return null; |
|
1089 | + } |
|
1090 | + |
|
1091 | + /** |
|
1092 | + * @param string $path |
|
1093 | + * @return mixed |
|
1094 | + * @throws \OCP\Files\InvalidPathException |
|
1095 | + */ |
|
1096 | + public function free_space($path = '/') { |
|
1097 | + $this->assertPathLength($path); |
|
1098 | + $result = $this->basicOperation('free_space', $path); |
|
1099 | + if ($result === null) { |
|
1100 | + throw new InvalidPathException(); |
|
1101 | + } |
|
1102 | + return $result; |
|
1103 | + } |
|
1104 | + |
|
1105 | + /** |
|
1106 | + * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
1107 | + * |
|
1108 | + * @param string $operation |
|
1109 | + * @param string $path |
|
1110 | + * @param array $hooks (optional) |
|
1111 | + * @param mixed $extraParam (optional) |
|
1112 | + * @return mixed |
|
1113 | + * @throws \Exception |
|
1114 | + * |
|
1115 | + * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
1116 | + * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
1117 | + * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
1118 | + */ |
|
1119 | + private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
1120 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1121 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1122 | + if (Filesystem::isValidPath($path) |
|
1123 | + and !Filesystem::isFileBlacklisted($path) |
|
1124 | + ) { |
|
1125 | + $path = $this->getRelativePath($absolutePath); |
|
1126 | + if ($path == null) { |
|
1127 | + return false; |
|
1128 | + } |
|
1129 | + |
|
1130 | + if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
1131 | + // always a shared lock during pre-hooks so the hook can read the file |
|
1132 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
1133 | + } |
|
1134 | + |
|
1135 | + $run = $this->runHooks($hooks, $path); |
|
1136 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
1137 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
1138 | + if ($run and $storage) { |
|
1139 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1140 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1141 | + } |
|
1142 | + try { |
|
1143 | + if (!is_null($extraParam)) { |
|
1144 | + $result = $storage->$operation($internalPath, $extraParam); |
|
1145 | + } else { |
|
1146 | + $result = $storage->$operation($internalPath); |
|
1147 | + } |
|
1148 | + } catch (\Exception $e) { |
|
1149 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1150 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1151 | + } else if (in_array('read', $hooks)) { |
|
1152 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1153 | + } |
|
1154 | + throw $e; |
|
1155 | + } |
|
1156 | + |
|
1157 | + if ($result && in_array('delete', $hooks) and $result) { |
|
1158 | + $this->removeUpdate($storage, $internalPath); |
|
1159 | + } |
|
1160 | + if ($result && in_array('write', $hooks) and $operation !== 'fopen') { |
|
1161 | + $this->writeUpdate($storage, $internalPath); |
|
1162 | + } |
|
1163 | + if ($result && in_array('touch', $hooks)) { |
|
1164 | + $this->writeUpdate($storage, $internalPath, $extraParam); |
|
1165 | + } |
|
1166 | + |
|
1167 | + if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
1168 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
1169 | + } |
|
1170 | + |
|
1171 | + $unlockLater = false; |
|
1172 | + if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
1173 | + $unlockLater = true; |
|
1174 | + // make sure our unlocking callback will still be called if connection is aborted |
|
1175 | + ignore_user_abort(true); |
|
1176 | + $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
1177 | + if (in_array('write', $hooks)) { |
|
1178 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1179 | + } else if (in_array('read', $hooks)) { |
|
1180 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1181 | + } |
|
1182 | + }); |
|
1183 | + } |
|
1184 | + |
|
1185 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
1186 | + if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
1187 | + $this->runHooks($hooks, $path, true); |
|
1188 | + } |
|
1189 | + } |
|
1190 | + |
|
1191 | + if (!$unlockLater |
|
1192 | + && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
1193 | + ) { |
|
1194 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1195 | + } |
|
1196 | + return $result; |
|
1197 | + } else { |
|
1198 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1199 | + } |
|
1200 | + } |
|
1201 | + return null; |
|
1202 | + } |
|
1203 | + |
|
1204 | + /** |
|
1205 | + * get the path relative to the default root for hook usage |
|
1206 | + * |
|
1207 | + * @param string $path |
|
1208 | + * @return string |
|
1209 | + */ |
|
1210 | + private function getHookPath($path) { |
|
1211 | + if (!Filesystem::getView()) { |
|
1212 | + return $path; |
|
1213 | + } |
|
1214 | + return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
1215 | + } |
|
1216 | + |
|
1217 | + private function shouldEmitHooks($path = '') { |
|
1218 | + if ($path && Cache\Scanner::isPartialFile($path)) { |
|
1219 | + return false; |
|
1220 | + } |
|
1221 | + if (!Filesystem::$loaded) { |
|
1222 | + return false; |
|
1223 | + } |
|
1224 | + $defaultRoot = Filesystem::getRoot(); |
|
1225 | + if ($defaultRoot === null) { |
|
1226 | + return false; |
|
1227 | + } |
|
1228 | + if ($this->fakeRoot === $defaultRoot) { |
|
1229 | + return true; |
|
1230 | + } |
|
1231 | + $fullPath = $this->getAbsolutePath($path); |
|
1232 | + |
|
1233 | + if ($fullPath === $defaultRoot) { |
|
1234 | + return true; |
|
1235 | + } |
|
1236 | + |
|
1237 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
1238 | + } |
|
1239 | + |
|
1240 | + /** |
|
1241 | + * @param string[] $hooks |
|
1242 | + * @param string $path |
|
1243 | + * @param bool $post |
|
1244 | + * @return bool |
|
1245 | + */ |
|
1246 | + private function runHooks($hooks, $path, $post = false) { |
|
1247 | + $relativePath = $path; |
|
1248 | + $path = $this->getHookPath($path); |
|
1249 | + $prefix = $post ? 'post_' : ''; |
|
1250 | + $run = true; |
|
1251 | + if ($this->shouldEmitHooks($relativePath)) { |
|
1252 | + foreach ($hooks as $hook) { |
|
1253 | + if ($hook != 'read') { |
|
1254 | + \OC_Hook::emit( |
|
1255 | + Filesystem::CLASSNAME, |
|
1256 | + $prefix . $hook, |
|
1257 | + array( |
|
1258 | + Filesystem::signal_param_run => &$run, |
|
1259 | + Filesystem::signal_param_path => $path |
|
1260 | + ) |
|
1261 | + ); |
|
1262 | + } elseif (!$post) { |
|
1263 | + \OC_Hook::emit( |
|
1264 | + Filesystem::CLASSNAME, |
|
1265 | + $prefix . $hook, |
|
1266 | + array( |
|
1267 | + Filesystem::signal_param_path => $path |
|
1268 | + ) |
|
1269 | + ); |
|
1270 | + } |
|
1271 | + } |
|
1272 | + } |
|
1273 | + return $run; |
|
1274 | + } |
|
1275 | + |
|
1276 | + /** |
|
1277 | + * check if a file or folder has been updated since $time |
|
1278 | + * |
|
1279 | + * @param string $path |
|
1280 | + * @param int $time |
|
1281 | + * @return bool |
|
1282 | + */ |
|
1283 | + public function hasUpdated($path, $time) { |
|
1284 | + return $this->basicOperation('hasUpdated', $path, array(), $time); |
|
1285 | + } |
|
1286 | + |
|
1287 | + /** |
|
1288 | + * @param string $ownerId |
|
1289 | + * @return \OC\User\User |
|
1290 | + */ |
|
1291 | + private function getUserObjectForOwner($ownerId) { |
|
1292 | + $owner = $this->userManager->get($ownerId); |
|
1293 | + if ($owner instanceof IUser) { |
|
1294 | + return $owner; |
|
1295 | + } else { |
|
1296 | + return new User($ownerId, null); |
|
1297 | + } |
|
1298 | + } |
|
1299 | + |
|
1300 | + /** |
|
1301 | + * Get file info from cache |
|
1302 | + * |
|
1303 | + * If the file is not in cached it will be scanned |
|
1304 | + * If the file has changed on storage the cache will be updated |
|
1305 | + * |
|
1306 | + * @param \OC\Files\Storage\Storage $storage |
|
1307 | + * @param string $internalPath |
|
1308 | + * @param string $relativePath |
|
1309 | + * @return ICacheEntry|bool |
|
1310 | + */ |
|
1311 | + private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
1312 | + $cache = $storage->getCache($internalPath); |
|
1313 | + $data = $cache->get($internalPath); |
|
1314 | + $watcher = $storage->getWatcher($internalPath); |
|
1315 | + |
|
1316 | + try { |
|
1317 | + // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
1318 | + if (!$data || $data['size'] === -1) { |
|
1319 | + $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1320 | + if (!$storage->file_exists($internalPath)) { |
|
1321 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1322 | + return false; |
|
1323 | + } |
|
1324 | + $scanner = $storage->getScanner($internalPath); |
|
1325 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1326 | + $data = $cache->get($internalPath); |
|
1327 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1328 | + } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
1329 | + $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1330 | + $watcher->update($internalPath, $data); |
|
1331 | + $storage->getPropagator()->propagateChange($internalPath, time()); |
|
1332 | + $data = $cache->get($internalPath); |
|
1333 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1334 | + } |
|
1335 | + } catch (LockedException $e) { |
|
1336 | + // if the file is locked we just use the old cache info |
|
1337 | + } |
|
1338 | + |
|
1339 | + return $data; |
|
1340 | + } |
|
1341 | + |
|
1342 | + /** |
|
1343 | + * get the filesystem info |
|
1344 | + * |
|
1345 | + * @param string $path |
|
1346 | + * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
|
1347 | + * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
1348 | + * defaults to true |
|
1349 | + * @return \OC\Files\FileInfo|false False if file does not exist |
|
1350 | + */ |
|
1351 | + public function getFileInfo($path, $includeMountPoints = true) { |
|
1352 | + $this->assertPathLength($path); |
|
1353 | + if (!Filesystem::isValidPath($path)) { |
|
1354 | + return false; |
|
1355 | + } |
|
1356 | + if (Cache\Scanner::isPartialFile($path)) { |
|
1357 | + return $this->getPartFileInfo($path); |
|
1358 | + } |
|
1359 | + $relativePath = $path; |
|
1360 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1361 | + |
|
1362 | + $mount = Filesystem::getMountManager()->find($path); |
|
1363 | + if (!$mount) { |
|
1364 | + return false; |
|
1365 | + } |
|
1366 | + $storage = $mount->getStorage(); |
|
1367 | + $internalPath = $mount->getInternalPath($path); |
|
1368 | + if ($storage) { |
|
1369 | + $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
1370 | + |
|
1371 | + if (!$data instanceof ICacheEntry) { |
|
1372 | + return false; |
|
1373 | + } |
|
1374 | + |
|
1375 | + if ($mount instanceof MoveableMount && $internalPath === '') { |
|
1376 | + $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
1377 | + } |
|
1378 | + |
|
1379 | + $owner = $this->getUserObjectForOwner($storage->getOwner($internalPath)); |
|
1380 | + $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
1381 | + |
|
1382 | + if ($data and isset($data['fileid'])) { |
|
1383 | + if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
1384 | + //add the sizes of other mount points to the folder |
|
1385 | + $extOnly = ($includeMountPoints === 'ext'); |
|
1386 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
1387 | + $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
1388 | + $subStorage = $mount->getStorage(); |
|
1389 | + return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
1390 | + })); |
|
1391 | + } |
|
1392 | + } |
|
1393 | + |
|
1394 | + return $info; |
|
1395 | + } |
|
1396 | + |
|
1397 | + return false; |
|
1398 | + } |
|
1399 | + |
|
1400 | + /** |
|
1401 | + * get the content of a directory |
|
1402 | + * |
|
1403 | + * @param string $directory path under datadirectory |
|
1404 | + * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
1405 | + * @return FileInfo[] |
|
1406 | + */ |
|
1407 | + public function getDirectoryContent($directory, $mimetype_filter = '') { |
|
1408 | + $this->assertPathLength($directory); |
|
1409 | + if (!Filesystem::isValidPath($directory)) { |
|
1410 | + return []; |
|
1411 | + } |
|
1412 | + $path = $this->getAbsolutePath($directory); |
|
1413 | + $path = Filesystem::normalizePath($path); |
|
1414 | + $mount = $this->getMount($directory); |
|
1415 | + if (!$mount) { |
|
1416 | + return []; |
|
1417 | + } |
|
1418 | + $storage = $mount->getStorage(); |
|
1419 | + $internalPath = $mount->getInternalPath($path); |
|
1420 | + if ($storage) { |
|
1421 | + $cache = $storage->getCache($internalPath); |
|
1422 | + $user = \OC_User::getUser(); |
|
1423 | + |
|
1424 | + $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
1425 | + |
|
1426 | + if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) { |
|
1427 | + return []; |
|
1428 | + } |
|
1429 | + |
|
1430 | + $folderId = $data['fileid']; |
|
1431 | + $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
1432 | + |
|
1433 | + $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
1434 | + /** |
|
1435 | + * @var \OC\Files\FileInfo[] $files |
|
1436 | + */ |
|
1437 | + $files = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
1438 | + if ($sharingDisabled) { |
|
1439 | + $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1440 | + } |
|
1441 | + $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
1442 | + return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
1443 | + }, $contents); |
|
1444 | + |
|
1445 | + //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
1446 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
1447 | + $dirLength = strlen($path); |
|
1448 | + foreach ($mounts as $mount) { |
|
1449 | + $mountPoint = $mount->getMountPoint(); |
|
1450 | + $subStorage = $mount->getStorage(); |
|
1451 | + if ($subStorage) { |
|
1452 | + $subCache = $subStorage->getCache(''); |
|
1453 | + |
|
1454 | + $rootEntry = $subCache->get(''); |
|
1455 | + if (!$rootEntry) { |
|
1456 | + $subScanner = $subStorage->getScanner(''); |
|
1457 | + try { |
|
1458 | + $subScanner->scanFile(''); |
|
1459 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
1460 | + continue; |
|
1461 | + } catch (\OCP\Files\StorageInvalidException $e) { |
|
1462 | + continue; |
|
1463 | + } catch (\Exception $e) { |
|
1464 | + // sometimes when the storage is not available it can be any exception |
|
1465 | + \OC::$server->getLogger()->logException($e, [ |
|
1466 | + 'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"', |
|
1467 | + 'level' => ILogger::ERROR, |
|
1468 | + 'app' => 'lib', |
|
1469 | + ]); |
|
1470 | + continue; |
|
1471 | + } |
|
1472 | + $rootEntry = $subCache->get(''); |
|
1473 | + } |
|
1474 | + |
|
1475 | + if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) { |
|
1476 | + $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
1477 | + if ($pos = strpos($relativePath, '/')) { |
|
1478 | + //mountpoint inside subfolder add size to the correct folder |
|
1479 | + $entryName = substr($relativePath, 0, $pos); |
|
1480 | + foreach ($files as &$entry) { |
|
1481 | + if ($entry->getName() === $entryName) { |
|
1482 | + $entry->addSubEntry($rootEntry, $mountPoint); |
|
1483 | + } |
|
1484 | + } |
|
1485 | + } else { //mountpoint in this folder, add an entry for it |
|
1486 | + $rootEntry['name'] = $relativePath; |
|
1487 | + $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
1488 | + $permissions = $rootEntry['permissions']; |
|
1489 | + // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
1490 | + // for shared files/folders we use the permissions given by the owner |
|
1491 | + if ($mount instanceof MoveableMount) { |
|
1492 | + $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
1493 | + } else { |
|
1494 | + $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
1495 | + } |
|
1496 | + |
|
1497 | + //remove any existing entry with the same name |
|
1498 | + foreach ($files as $i => $file) { |
|
1499 | + if ($file['name'] === $rootEntry['name']) { |
|
1500 | + unset($files[$i]); |
|
1501 | + break; |
|
1502 | + } |
|
1503 | + } |
|
1504 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
1505 | + |
|
1506 | + // if sharing was disabled for the user we remove the share permissions |
|
1507 | + if (\OCP\Util::isSharingDisabledForUser()) { |
|
1508 | + $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1509 | + } |
|
1510 | + |
|
1511 | + $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
1512 | + $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
1513 | + } |
|
1514 | + } |
|
1515 | + } |
|
1516 | + } |
|
1517 | + |
|
1518 | + if ($mimetype_filter) { |
|
1519 | + $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
1520 | + if (strpos($mimetype_filter, '/')) { |
|
1521 | + return $file->getMimetype() === $mimetype_filter; |
|
1522 | + } else { |
|
1523 | + return $file->getMimePart() === $mimetype_filter; |
|
1524 | + } |
|
1525 | + }); |
|
1526 | + } |
|
1527 | + |
|
1528 | + return $files; |
|
1529 | + } else { |
|
1530 | + return []; |
|
1531 | + } |
|
1532 | + } |
|
1533 | + |
|
1534 | + /** |
|
1535 | + * change file metadata |
|
1536 | + * |
|
1537 | + * @param string $path |
|
1538 | + * @param array|\OCP\Files\FileInfo $data |
|
1539 | + * @return int |
|
1540 | + * |
|
1541 | + * returns the fileid of the updated file |
|
1542 | + */ |
|
1543 | + public function putFileInfo($path, $data) { |
|
1544 | + $this->assertPathLength($path); |
|
1545 | + if ($data instanceof FileInfo) { |
|
1546 | + $data = $data->getData(); |
|
1547 | + } |
|
1548 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1549 | + /** |
|
1550 | + * @var \OC\Files\Storage\Storage $storage |
|
1551 | + * @var string $internalPath |
|
1552 | + */ |
|
1553 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
1554 | + if ($storage) { |
|
1555 | + $cache = $storage->getCache($path); |
|
1556 | + |
|
1557 | + if (!$cache->inCache($internalPath)) { |
|
1558 | + $scanner = $storage->getScanner($internalPath); |
|
1559 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1560 | + } |
|
1561 | + |
|
1562 | + return $cache->put($internalPath, $data); |
|
1563 | + } else { |
|
1564 | + return -1; |
|
1565 | + } |
|
1566 | + } |
|
1567 | + |
|
1568 | + /** |
|
1569 | + * search for files with the name matching $query |
|
1570 | + * |
|
1571 | + * @param string $query |
|
1572 | + * @return FileInfo[] |
|
1573 | + */ |
|
1574 | + public function search($query) { |
|
1575 | + return $this->searchCommon('search', array('%' . $query . '%')); |
|
1576 | + } |
|
1577 | + |
|
1578 | + /** |
|
1579 | + * search for files with the name matching $query |
|
1580 | + * |
|
1581 | + * @param string $query |
|
1582 | + * @return FileInfo[] |
|
1583 | + */ |
|
1584 | + public function searchRaw($query) { |
|
1585 | + return $this->searchCommon('search', array($query)); |
|
1586 | + } |
|
1587 | + |
|
1588 | + /** |
|
1589 | + * search for files by mimetype |
|
1590 | + * |
|
1591 | + * @param string $mimetype |
|
1592 | + * @return FileInfo[] |
|
1593 | + */ |
|
1594 | + public function searchByMime($mimetype) { |
|
1595 | + return $this->searchCommon('searchByMime', array($mimetype)); |
|
1596 | + } |
|
1597 | + |
|
1598 | + /** |
|
1599 | + * search for files by tag |
|
1600 | + * |
|
1601 | + * @param string|int $tag name or tag id |
|
1602 | + * @param string $userId owner of the tags |
|
1603 | + * @return FileInfo[] |
|
1604 | + */ |
|
1605 | + public function searchByTag($tag, $userId) { |
|
1606 | + return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
1607 | + } |
|
1608 | + |
|
1609 | + /** |
|
1610 | + * @param string $method cache method |
|
1611 | + * @param array $args |
|
1612 | + * @return FileInfo[] |
|
1613 | + */ |
|
1614 | + private function searchCommon($method, $args) { |
|
1615 | + $files = array(); |
|
1616 | + $rootLength = strlen($this->fakeRoot); |
|
1617 | + |
|
1618 | + $mount = $this->getMount(''); |
|
1619 | + $mountPoint = $mount->getMountPoint(); |
|
1620 | + $storage = $mount->getStorage(); |
|
1621 | + if ($storage) { |
|
1622 | + $cache = $storage->getCache(''); |
|
1623 | + |
|
1624 | + $results = call_user_func_array(array($cache, $method), $args); |
|
1625 | + foreach ($results as $result) { |
|
1626 | + if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
1627 | + $internalPath = $result['path']; |
|
1628 | + $path = $mountPoint . $result['path']; |
|
1629 | + $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
1630 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1631 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1632 | + } |
|
1633 | + } |
|
1634 | + |
|
1635 | + $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
1636 | + foreach ($mounts as $mount) { |
|
1637 | + $mountPoint = $mount->getMountPoint(); |
|
1638 | + $storage = $mount->getStorage(); |
|
1639 | + if ($storage) { |
|
1640 | + $cache = $storage->getCache(''); |
|
1641 | + |
|
1642 | + $relativeMountPoint = substr($mountPoint, $rootLength); |
|
1643 | + $results = call_user_func_array(array($cache, $method), $args); |
|
1644 | + if ($results) { |
|
1645 | + foreach ($results as $result) { |
|
1646 | + $internalPath = $result['path']; |
|
1647 | + $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
1648 | + $path = rtrim($mountPoint . $internalPath, '/'); |
|
1649 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1650 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1651 | + } |
|
1652 | + } |
|
1653 | + } |
|
1654 | + } |
|
1655 | + } |
|
1656 | + return $files; |
|
1657 | + } |
|
1658 | + |
|
1659 | + /** |
|
1660 | + * Get the owner for a file or folder |
|
1661 | + * |
|
1662 | + * @param string $path |
|
1663 | + * @return string the user id of the owner |
|
1664 | + * @throws NotFoundException |
|
1665 | + */ |
|
1666 | + public function getOwner($path) { |
|
1667 | + $info = $this->getFileInfo($path); |
|
1668 | + if (!$info) { |
|
1669 | + throw new NotFoundException($path . ' not found while trying to get owner'); |
|
1670 | + } |
|
1671 | + return $info->getOwner()->getUID(); |
|
1672 | + } |
|
1673 | + |
|
1674 | + /** |
|
1675 | + * get the ETag for a file or folder |
|
1676 | + * |
|
1677 | + * @param string $path |
|
1678 | + * @return string |
|
1679 | + */ |
|
1680 | + public function getETag($path) { |
|
1681 | + /** |
|
1682 | + * @var Storage\Storage $storage |
|
1683 | + * @var string $internalPath |
|
1684 | + */ |
|
1685 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
1686 | + if ($storage) { |
|
1687 | + return $storage->getETag($internalPath); |
|
1688 | + } else { |
|
1689 | + return null; |
|
1690 | + } |
|
1691 | + } |
|
1692 | + |
|
1693 | + /** |
|
1694 | + * Get the path of a file by id, relative to the view |
|
1695 | + * |
|
1696 | + * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
|
1697 | + * |
|
1698 | + * @param int $id |
|
1699 | + * @throws NotFoundException |
|
1700 | + * @return string |
|
1701 | + */ |
|
1702 | + public function getPath($id) { |
|
1703 | + $id = (int)$id; |
|
1704 | + $manager = Filesystem::getMountManager(); |
|
1705 | + $mounts = $manager->findIn($this->fakeRoot); |
|
1706 | + $mounts[] = $manager->find($this->fakeRoot); |
|
1707 | + // reverse the array so we start with the storage this view is in |
|
1708 | + // which is the most likely to contain the file we're looking for |
|
1709 | + $mounts = array_reverse($mounts); |
|
1710 | + foreach ($mounts as $mount) { |
|
1711 | + /** |
|
1712 | + * @var \OC\Files\Mount\MountPoint $mount |
|
1713 | + */ |
|
1714 | + if ($mount->getStorage()) { |
|
1715 | + $cache = $mount->getStorage()->getCache(); |
|
1716 | + $internalPath = $cache->getPathById($id); |
|
1717 | + if (is_string($internalPath)) { |
|
1718 | + $fullPath = $mount->getMountPoint() . $internalPath; |
|
1719 | + if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
1720 | + return $path; |
|
1721 | + } |
|
1722 | + } |
|
1723 | + } |
|
1724 | + } |
|
1725 | + throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
1726 | + } |
|
1727 | + |
|
1728 | + /** |
|
1729 | + * @param string $path |
|
1730 | + * @throws InvalidPathException |
|
1731 | + */ |
|
1732 | + private function assertPathLength($path) { |
|
1733 | + $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
1734 | + // Check for the string length - performed using isset() instead of strlen() |
|
1735 | + // because isset() is about 5x-40x faster. |
|
1736 | + if (isset($path[$maxLen])) { |
|
1737 | + $pathLen = strlen($path); |
|
1738 | + throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
1739 | + } |
|
1740 | + } |
|
1741 | + |
|
1742 | + /** |
|
1743 | + * check if it is allowed to move a mount point to a given target. |
|
1744 | + * It is not allowed to move a mount point into a different mount point or |
|
1745 | + * into an already shared folder |
|
1746 | + * |
|
1747 | + * @param string $target path |
|
1748 | + * @return boolean |
|
1749 | + */ |
|
1750 | + private function isTargetAllowed($target) { |
|
1751 | + |
|
1752 | + list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target); |
|
1753 | + if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
1754 | + \OCP\Util::writeLog('files', |
|
1755 | + 'It is not allowed to move one mount point into another one', |
|
1756 | + ILogger::DEBUG); |
|
1757 | + return false; |
|
1758 | + } |
|
1759 | + |
|
1760 | + // note: cannot use the view because the target is already locked |
|
1761 | + $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
1762 | + if ($fileId === -1) { |
|
1763 | + // target might not exist, need to check parent instead |
|
1764 | + $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
1765 | + } |
|
1766 | + |
|
1767 | + // check if any of the parents were shared by the current owner (include collections) |
|
1768 | + $shares = \OCP\Share::getItemShared( |
|
1769 | + 'folder', |
|
1770 | + $fileId, |
|
1771 | + \OCP\Share::FORMAT_NONE, |
|
1772 | + null, |
|
1773 | + true |
|
1774 | + ); |
|
1775 | + |
|
1776 | + if (count($shares) > 0) { |
|
1777 | + \OCP\Util::writeLog('files', |
|
1778 | + 'It is not allowed to move one mount point into a shared folder', |
|
1779 | + ILogger::DEBUG); |
|
1780 | + return false; |
|
1781 | + } |
|
1782 | + |
|
1783 | + return true; |
|
1784 | + } |
|
1785 | + |
|
1786 | + /** |
|
1787 | + * Get a fileinfo object for files that are ignored in the cache (part files) |
|
1788 | + * |
|
1789 | + * @param string $path |
|
1790 | + * @return \OCP\Files\FileInfo |
|
1791 | + */ |
|
1792 | + private function getPartFileInfo($path) { |
|
1793 | + $mount = $this->getMount($path); |
|
1794 | + $storage = $mount->getStorage(); |
|
1795 | + $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
1796 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1797 | + return new FileInfo( |
|
1798 | + $this->getAbsolutePath($path), |
|
1799 | + $storage, |
|
1800 | + $internalPath, |
|
1801 | + [ |
|
1802 | + 'fileid' => null, |
|
1803 | + 'mimetype' => $storage->getMimeType($internalPath), |
|
1804 | + 'name' => basename($path), |
|
1805 | + 'etag' => null, |
|
1806 | + 'size' => $storage->filesize($internalPath), |
|
1807 | + 'mtime' => $storage->filemtime($internalPath), |
|
1808 | + 'encrypted' => false, |
|
1809 | + 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
1810 | + ], |
|
1811 | + $mount, |
|
1812 | + $owner |
|
1813 | + ); |
|
1814 | + } |
|
1815 | + |
|
1816 | + /** |
|
1817 | + * @param string $path |
|
1818 | + * @param string $fileName |
|
1819 | + * @throws InvalidPathException |
|
1820 | + */ |
|
1821 | + public function verifyPath($path, $fileName) { |
|
1822 | + try { |
|
1823 | + /** @type \OCP\Files\Storage $storage */ |
|
1824 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
1825 | + $storage->verifyPath($internalPath, $fileName); |
|
1826 | + } catch (ReservedWordException $ex) { |
|
1827 | + $l = \OC::$server->getL10N('lib'); |
|
1828 | + throw new InvalidPathException($l->t('File name is a reserved word')); |
|
1829 | + } catch (InvalidCharacterInPathException $ex) { |
|
1830 | + $l = \OC::$server->getL10N('lib'); |
|
1831 | + throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
1832 | + } catch (FileNameTooLongException $ex) { |
|
1833 | + $l = \OC::$server->getL10N('lib'); |
|
1834 | + throw new InvalidPathException($l->t('File name is too long')); |
|
1835 | + } catch (InvalidDirectoryException $ex) { |
|
1836 | + $l = \OC::$server->getL10N('lib'); |
|
1837 | + throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
1838 | + } catch (EmptyFileNameException $ex) { |
|
1839 | + $l = \OC::$server->getL10N('lib'); |
|
1840 | + throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
1841 | + } |
|
1842 | + } |
|
1843 | + |
|
1844 | + /** |
|
1845 | + * get all parent folders of $path |
|
1846 | + * |
|
1847 | + * @param string $path |
|
1848 | + * @return string[] |
|
1849 | + */ |
|
1850 | + private function getParents($path) { |
|
1851 | + $path = trim($path, '/'); |
|
1852 | + if (!$path) { |
|
1853 | + return []; |
|
1854 | + } |
|
1855 | + |
|
1856 | + $parts = explode('/', $path); |
|
1857 | + |
|
1858 | + // remove the single file |
|
1859 | + array_pop($parts); |
|
1860 | + $result = array('/'); |
|
1861 | + $resultPath = ''; |
|
1862 | + foreach ($parts as $part) { |
|
1863 | + if ($part) { |
|
1864 | + $resultPath .= '/' . $part; |
|
1865 | + $result[] = $resultPath; |
|
1866 | + } |
|
1867 | + } |
|
1868 | + return $result; |
|
1869 | + } |
|
1870 | + |
|
1871 | + /** |
|
1872 | + * Returns the mount point for which to lock |
|
1873 | + * |
|
1874 | + * @param string $absolutePath absolute path |
|
1875 | + * @param bool $useParentMount true to return parent mount instead of whatever |
|
1876 | + * is mounted directly on the given path, false otherwise |
|
1877 | + * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
|
1878 | + */ |
|
1879 | + private function getMountForLock($absolutePath, $useParentMount = false) { |
|
1880 | + $results = []; |
|
1881 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
1882 | + if (!$mount) { |
|
1883 | + return $results; |
|
1884 | + } |
|
1885 | + |
|
1886 | + if ($useParentMount) { |
|
1887 | + // find out if something is mounted directly on the path |
|
1888 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
1889 | + if ($internalPath === '') { |
|
1890 | + // resolve the parent mount instead |
|
1891 | + $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
1892 | + } |
|
1893 | + } |
|
1894 | + |
|
1895 | + return $mount; |
|
1896 | + } |
|
1897 | + |
|
1898 | + /** |
|
1899 | + * Lock the given path |
|
1900 | + * |
|
1901 | + * @param string $path the path of the file to lock, relative to the view |
|
1902 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1903 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1904 | + * |
|
1905 | + * @return bool False if the path is excluded from locking, true otherwise |
|
1906 | + * @throws \OCP\Lock\LockedException if the path is already locked |
|
1907 | + */ |
|
1908 | + private function lockPath($path, $type, $lockMountPoint = false) { |
|
1909 | + $absolutePath = $this->getAbsolutePath($path); |
|
1910 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1911 | + if (!$this->shouldLockFile($absolutePath)) { |
|
1912 | + return false; |
|
1913 | + } |
|
1914 | + |
|
1915 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
1916 | + if ($mount) { |
|
1917 | + try { |
|
1918 | + $storage = $mount->getStorage(); |
|
1919 | + if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
1920 | + $storage->acquireLock( |
|
1921 | + $mount->getInternalPath($absolutePath), |
|
1922 | + $type, |
|
1923 | + $this->lockingProvider |
|
1924 | + ); |
|
1925 | + } |
|
1926 | + } catch (\OCP\Lock\LockedException $e) { |
|
1927 | + // rethrow with the a human-readable path |
|
1928 | + throw new \OCP\Lock\LockedException( |
|
1929 | + $this->getPathRelativeToFiles($absolutePath), |
|
1930 | + $e |
|
1931 | + ); |
|
1932 | + } |
|
1933 | + } |
|
1934 | + |
|
1935 | + return true; |
|
1936 | + } |
|
1937 | + |
|
1938 | + /** |
|
1939 | + * Change the lock type |
|
1940 | + * |
|
1941 | + * @param string $path the path of the file to lock, relative to the view |
|
1942 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1943 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1944 | + * |
|
1945 | + * @return bool False if the path is excluded from locking, true otherwise |
|
1946 | + * @throws \OCP\Lock\LockedException if the path is already locked |
|
1947 | + */ |
|
1948 | + public function changeLock($path, $type, $lockMountPoint = false) { |
|
1949 | + $path = Filesystem::normalizePath($path); |
|
1950 | + $absolutePath = $this->getAbsolutePath($path); |
|
1951 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1952 | + if (!$this->shouldLockFile($absolutePath)) { |
|
1953 | + return false; |
|
1954 | + } |
|
1955 | + |
|
1956 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
1957 | + if ($mount) { |
|
1958 | + try { |
|
1959 | + $storage = $mount->getStorage(); |
|
1960 | + if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
1961 | + $storage->changeLock( |
|
1962 | + $mount->getInternalPath($absolutePath), |
|
1963 | + $type, |
|
1964 | + $this->lockingProvider |
|
1965 | + ); |
|
1966 | + } |
|
1967 | + } catch (\OCP\Lock\LockedException $e) { |
|
1968 | + try { |
|
1969 | + // rethrow with the a human-readable path |
|
1970 | + throw new \OCP\Lock\LockedException( |
|
1971 | + $this->getPathRelativeToFiles($absolutePath), |
|
1972 | + $e |
|
1973 | + ); |
|
1974 | + } catch (\InvalidArgumentException $e) { |
|
1975 | + throw new \OCP\Lock\LockedException( |
|
1976 | + $absolutePath, |
|
1977 | + $e |
|
1978 | + ); |
|
1979 | + } |
|
1980 | + } |
|
1981 | + } |
|
1982 | + |
|
1983 | + return true; |
|
1984 | + } |
|
1985 | + |
|
1986 | + /** |
|
1987 | + * Unlock the given path |
|
1988 | + * |
|
1989 | + * @param string $path the path of the file to unlock, 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 | + */ |
|
1995 | + private function unlockPath($path, $type, $lockMountPoint = false) { |
|
1996 | + $absolutePath = $this->getAbsolutePath($path); |
|
1997 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1998 | + if (!$this->shouldLockFile($absolutePath)) { |
|
1999 | + return false; |
|
2000 | + } |
|
2001 | + |
|
2002 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
2003 | + if ($mount) { |
|
2004 | + $storage = $mount->getStorage(); |
|
2005 | + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
2006 | + $storage->releaseLock( |
|
2007 | + $mount->getInternalPath($absolutePath), |
|
2008 | + $type, |
|
2009 | + $this->lockingProvider |
|
2010 | + ); |
|
2011 | + } |
|
2012 | + } |
|
2013 | + |
|
2014 | + return true; |
|
2015 | + } |
|
2016 | + |
|
2017 | + /** |
|
2018 | + * Lock a path and all its parents up to the root of the view |
|
2019 | + * |
|
2020 | + * @param string $path the path of the file to lock relative to the view |
|
2021 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2022 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2023 | + * |
|
2024 | + * @return bool False if the path is excluded from locking, true otherwise |
|
2025 | + */ |
|
2026 | + public function lockFile($path, $type, $lockMountPoint = false) { |
|
2027 | + $absolutePath = $this->getAbsolutePath($path); |
|
2028 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2029 | + if (!$this->shouldLockFile($absolutePath)) { |
|
2030 | + return false; |
|
2031 | + } |
|
2032 | + |
|
2033 | + $this->lockPath($path, $type, $lockMountPoint); |
|
2034 | + |
|
2035 | + $parents = $this->getParents($path); |
|
2036 | + foreach ($parents as $parent) { |
|
2037 | + $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2038 | + } |
|
2039 | + |
|
2040 | + return true; |
|
2041 | + } |
|
2042 | + |
|
2043 | + /** |
|
2044 | + * Unlock a path and all its parents up to the root of the view |
|
2045 | + * |
|
2046 | + * @param string $path the path of the file to lock relative to the view |
|
2047 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2048 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2049 | + * |
|
2050 | + * @return bool False if the path is excluded from locking, true otherwise |
|
2051 | + */ |
|
2052 | + public function unlockFile($path, $type, $lockMountPoint = false) { |
|
2053 | + $absolutePath = $this->getAbsolutePath($path); |
|
2054 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2055 | + if (!$this->shouldLockFile($absolutePath)) { |
|
2056 | + return false; |
|
2057 | + } |
|
2058 | + |
|
2059 | + $this->unlockPath($path, $type, $lockMountPoint); |
|
2060 | + |
|
2061 | + $parents = $this->getParents($path); |
|
2062 | + foreach ($parents as $parent) { |
|
2063 | + $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2064 | + } |
|
2065 | + |
|
2066 | + return true; |
|
2067 | + } |
|
2068 | + |
|
2069 | + /** |
|
2070 | + * Only lock files in data/user/files/ |
|
2071 | + * |
|
2072 | + * @param string $path Absolute path to the file/folder we try to (un)lock |
|
2073 | + * @return bool |
|
2074 | + */ |
|
2075 | + protected function shouldLockFile($path) { |
|
2076 | + $path = Filesystem::normalizePath($path); |
|
2077 | + |
|
2078 | + $pathSegments = explode('/', $path); |
|
2079 | + if (isset($pathSegments[2])) { |
|
2080 | + // E.g.: /username/files/path-to-file |
|
2081 | + return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
2082 | + } |
|
2083 | + |
|
2084 | + return strpos($path, '/appdata_') !== 0; |
|
2085 | + } |
|
2086 | + |
|
2087 | + /** |
|
2088 | + * Shortens the given absolute path to be relative to |
|
2089 | + * "$user/files". |
|
2090 | + * |
|
2091 | + * @param string $absolutePath absolute path which is under "files" |
|
2092 | + * |
|
2093 | + * @return string path relative to "files" with trimmed slashes or null |
|
2094 | + * if the path was NOT relative to files |
|
2095 | + * |
|
2096 | + * @throws \InvalidArgumentException if the given path was not under "files" |
|
2097 | + * @since 8.1.0 |
|
2098 | + */ |
|
2099 | + public function getPathRelativeToFiles($absolutePath) { |
|
2100 | + $path = Filesystem::normalizePath($absolutePath); |
|
2101 | + $parts = explode('/', trim($path, '/'), 3); |
|
2102 | + // "$user", "files", "path/to/dir" |
|
2103 | + if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
2104 | + $this->logger->error( |
|
2105 | + '$absolutePath must be relative to "files", value is "%s"', |
|
2106 | + [ |
|
2107 | + $absolutePath |
|
2108 | + ] |
|
2109 | + ); |
|
2110 | + throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
2111 | + } |
|
2112 | + if (isset($parts[2])) { |
|
2113 | + return $parts[2]; |
|
2114 | + } |
|
2115 | + return ''; |
|
2116 | + } |
|
2117 | + |
|
2118 | + /** |
|
2119 | + * @param string $filename |
|
2120 | + * @return array |
|
2121 | + * @throws \OC\User\NoUserException |
|
2122 | + * @throws NotFoundException |
|
2123 | + */ |
|
2124 | + public function getUidAndFilename($filename) { |
|
2125 | + $info = $this->getFileInfo($filename); |
|
2126 | + if (!$info instanceof \OCP\Files\FileInfo) { |
|
2127 | + throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
2128 | + } |
|
2129 | + $uid = $info->getOwner()->getUID(); |
|
2130 | + if ($uid != \OCP\User::getUser()) { |
|
2131 | + Filesystem::initMountPoints($uid); |
|
2132 | + $ownerView = new View('/' . $uid . '/files'); |
|
2133 | + try { |
|
2134 | + $filename = $ownerView->getPath($info['fileid']); |
|
2135 | + } catch (NotFoundException $e) { |
|
2136 | + throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
2137 | + } |
|
2138 | + } |
|
2139 | + return [$uid, $filename]; |
|
2140 | + } |
|
2141 | + |
|
2142 | + /** |
|
2143 | + * Creates parent non-existing folders |
|
2144 | + * |
|
2145 | + * @param string $filePath |
|
2146 | + * @return bool |
|
2147 | + */ |
|
2148 | + private function createParentDirectories($filePath) { |
|
2149 | + $directoryParts = explode('/', $filePath); |
|
2150 | + $directoryParts = array_filter($directoryParts); |
|
2151 | + foreach ($directoryParts as $key => $part) { |
|
2152 | + $currentPathElements = array_slice($directoryParts, 0, $key); |
|
2153 | + $currentPath = '/' . implode('/', $currentPathElements); |
|
2154 | + if ($this->is_file($currentPath)) { |
|
2155 | + return false; |
|
2156 | + } |
|
2157 | + if (!$this->file_exists($currentPath)) { |
|
2158 | + $this->mkdir($currentPath); |
|
2159 | + } |
|
2160 | + } |
|
2161 | + |
|
2162 | + return true; |
|
2163 | + } |
|
2164 | 2164 | } |
@@ -36,247 +36,247 @@ |
||
36 | 36 | use OCP\ILogger; |
37 | 37 | |
38 | 38 | class MountPoint implements IMountPoint { |
39 | - /** |
|
40 | - * @var \OC\Files\Storage\Storage $storage |
|
41 | - */ |
|
42 | - protected $storage = null; |
|
43 | - protected $class; |
|
44 | - protected $storageId; |
|
45 | - protected $rootId = null; |
|
39 | + /** |
|
40 | + * @var \OC\Files\Storage\Storage $storage |
|
41 | + */ |
|
42 | + protected $storage = null; |
|
43 | + protected $class; |
|
44 | + protected $storageId; |
|
45 | + protected $rootId = null; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Configuration options for the storage backend |
|
49 | - * |
|
50 | - * @var array |
|
51 | - */ |
|
52 | - protected $arguments = array(); |
|
53 | - protected $mountPoint; |
|
47 | + /** |
|
48 | + * Configuration options for the storage backend |
|
49 | + * |
|
50 | + * @var array |
|
51 | + */ |
|
52 | + protected $arguments = array(); |
|
53 | + protected $mountPoint; |
|
54 | 54 | |
55 | - /** |
|
56 | - * Mount specific options |
|
57 | - * |
|
58 | - * @var array |
|
59 | - */ |
|
60 | - protected $mountOptions = array(); |
|
55 | + /** |
|
56 | + * Mount specific options |
|
57 | + * |
|
58 | + * @var array |
|
59 | + */ |
|
60 | + protected $mountOptions = array(); |
|
61 | 61 | |
62 | - /** |
|
63 | - * @var \OC\Files\Storage\StorageFactory $loader |
|
64 | - */ |
|
65 | - private $loader; |
|
62 | + /** |
|
63 | + * @var \OC\Files\Storage\StorageFactory $loader |
|
64 | + */ |
|
65 | + private $loader; |
|
66 | 66 | |
67 | - /** |
|
68 | - * Specified whether the storage is invalid after failing to |
|
69 | - * instantiate it. |
|
70 | - * |
|
71 | - * @var bool |
|
72 | - */ |
|
73 | - private $invalidStorage = false; |
|
67 | + /** |
|
68 | + * Specified whether the storage is invalid after failing to |
|
69 | + * instantiate it. |
|
70 | + * |
|
71 | + * @var bool |
|
72 | + */ |
|
73 | + private $invalidStorage = false; |
|
74 | 74 | |
75 | - /** @var int|null */ |
|
76 | - protected $mountId; |
|
75 | + /** @var int|null */ |
|
76 | + protected $mountId; |
|
77 | 77 | |
78 | - /** |
|
79 | - * @param string|\OC\Files\Storage\Storage $storage |
|
80 | - * @param string $mountpoint |
|
81 | - * @param array $arguments (optional) configuration for the storage backend |
|
82 | - * @param \OCP\Files\Storage\IStorageFactory $loader |
|
83 | - * @param array $mountOptions mount specific options |
|
84 | - * @param int|null $mountId |
|
85 | - * @throws \Exception |
|
86 | - */ |
|
87 | - public function __construct($storage, $mountpoint, $arguments = null, $loader = null, $mountOptions = null, $mountId = null) { |
|
88 | - if (is_null($arguments)) { |
|
89 | - $arguments = array(); |
|
90 | - } |
|
91 | - if (is_null($loader)) { |
|
92 | - $this->loader = new StorageFactory(); |
|
93 | - } else { |
|
94 | - $this->loader = $loader; |
|
95 | - } |
|
78 | + /** |
|
79 | + * @param string|\OC\Files\Storage\Storage $storage |
|
80 | + * @param string $mountpoint |
|
81 | + * @param array $arguments (optional) configuration for the storage backend |
|
82 | + * @param \OCP\Files\Storage\IStorageFactory $loader |
|
83 | + * @param array $mountOptions mount specific options |
|
84 | + * @param int|null $mountId |
|
85 | + * @throws \Exception |
|
86 | + */ |
|
87 | + public function __construct($storage, $mountpoint, $arguments = null, $loader = null, $mountOptions = null, $mountId = null) { |
|
88 | + if (is_null($arguments)) { |
|
89 | + $arguments = array(); |
|
90 | + } |
|
91 | + if (is_null($loader)) { |
|
92 | + $this->loader = new StorageFactory(); |
|
93 | + } else { |
|
94 | + $this->loader = $loader; |
|
95 | + } |
|
96 | 96 | |
97 | - if (!is_null($mountOptions)) { |
|
98 | - $this->mountOptions = $mountOptions; |
|
99 | - } |
|
97 | + if (!is_null($mountOptions)) { |
|
98 | + $this->mountOptions = $mountOptions; |
|
99 | + } |
|
100 | 100 | |
101 | - $mountpoint = $this->formatPath($mountpoint); |
|
102 | - $this->mountPoint = $mountpoint; |
|
103 | - if ($storage instanceof Storage) { |
|
104 | - $this->class = get_class($storage); |
|
105 | - $this->storage = $this->loader->wrap($this, $storage); |
|
106 | - } else { |
|
107 | - // Update old classes to new namespace |
|
108 | - if (strpos($storage, 'OC_Filestorage_') !== false) { |
|
109 | - $storage = '\OC\Files\Storage\\' . substr($storage, 15); |
|
110 | - } |
|
111 | - $this->class = $storage; |
|
112 | - $this->arguments = $arguments; |
|
113 | - } |
|
114 | - $this->mountId = $mountId; |
|
115 | - } |
|
101 | + $mountpoint = $this->formatPath($mountpoint); |
|
102 | + $this->mountPoint = $mountpoint; |
|
103 | + if ($storage instanceof Storage) { |
|
104 | + $this->class = get_class($storage); |
|
105 | + $this->storage = $this->loader->wrap($this, $storage); |
|
106 | + } else { |
|
107 | + // Update old classes to new namespace |
|
108 | + if (strpos($storage, 'OC_Filestorage_') !== false) { |
|
109 | + $storage = '\OC\Files\Storage\\' . substr($storage, 15); |
|
110 | + } |
|
111 | + $this->class = $storage; |
|
112 | + $this->arguments = $arguments; |
|
113 | + } |
|
114 | + $this->mountId = $mountId; |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * get complete path to the mount point, relative to data/ |
|
119 | - * |
|
120 | - * @return string |
|
121 | - */ |
|
122 | - public function getMountPoint() { |
|
123 | - return $this->mountPoint; |
|
124 | - } |
|
117 | + /** |
|
118 | + * get complete path to the mount point, relative to data/ |
|
119 | + * |
|
120 | + * @return string |
|
121 | + */ |
|
122 | + public function getMountPoint() { |
|
123 | + return $this->mountPoint; |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * Sets the mount point path, relative to data/ |
|
128 | - * |
|
129 | - * @param string $mountPoint new mount point |
|
130 | - */ |
|
131 | - public function setMountPoint($mountPoint) { |
|
132 | - $this->mountPoint = $this->formatPath($mountPoint); |
|
133 | - } |
|
126 | + /** |
|
127 | + * Sets the mount point path, relative to data/ |
|
128 | + * |
|
129 | + * @param string $mountPoint new mount point |
|
130 | + */ |
|
131 | + public function setMountPoint($mountPoint) { |
|
132 | + $this->mountPoint = $this->formatPath($mountPoint); |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * create the storage that is mounted |
|
137 | - */ |
|
138 | - private function createStorage() { |
|
139 | - if ($this->invalidStorage) { |
|
140 | - return; |
|
141 | - } |
|
135 | + /** |
|
136 | + * create the storage that is mounted |
|
137 | + */ |
|
138 | + private function createStorage() { |
|
139 | + if ($this->invalidStorage) { |
|
140 | + return; |
|
141 | + } |
|
142 | 142 | |
143 | - if (class_exists($this->class)) { |
|
144 | - try { |
|
145 | - $class = $this->class; |
|
146 | - // prevent recursion by setting the storage before applying wrappers |
|
147 | - $this->storage = new $class($this->arguments); |
|
148 | - $this->storage = $this->loader->wrap($this, $this->storage); |
|
149 | - } catch (\Exception $exception) { |
|
150 | - $this->storage = null; |
|
151 | - $this->invalidStorage = true; |
|
152 | - if ($this->mountPoint === '/') { |
|
153 | - // the root storage could not be initialized, show the user! |
|
154 | - throw new \Exception('The root storage could not be initialized. Please contact your local administrator.', $exception->getCode(), $exception); |
|
155 | - } else { |
|
156 | - \OC::$server->getLogger()->logException($exception, ['level' => ILogger::ERROR]); |
|
157 | - } |
|
158 | - return; |
|
159 | - } |
|
160 | - } else { |
|
161 | - \OCP\Util::writeLog('core', 'storage backend ' . $this->class . ' not found', ILogger::ERROR); |
|
162 | - $this->invalidStorage = true; |
|
163 | - return; |
|
164 | - } |
|
165 | - } |
|
143 | + if (class_exists($this->class)) { |
|
144 | + try { |
|
145 | + $class = $this->class; |
|
146 | + // prevent recursion by setting the storage before applying wrappers |
|
147 | + $this->storage = new $class($this->arguments); |
|
148 | + $this->storage = $this->loader->wrap($this, $this->storage); |
|
149 | + } catch (\Exception $exception) { |
|
150 | + $this->storage = null; |
|
151 | + $this->invalidStorage = true; |
|
152 | + if ($this->mountPoint === '/') { |
|
153 | + // the root storage could not be initialized, show the user! |
|
154 | + throw new \Exception('The root storage could not be initialized. Please contact your local administrator.', $exception->getCode(), $exception); |
|
155 | + } else { |
|
156 | + \OC::$server->getLogger()->logException($exception, ['level' => ILogger::ERROR]); |
|
157 | + } |
|
158 | + return; |
|
159 | + } |
|
160 | + } else { |
|
161 | + \OCP\Util::writeLog('core', 'storage backend ' . $this->class . ' not found', ILogger::ERROR); |
|
162 | + $this->invalidStorage = true; |
|
163 | + return; |
|
164 | + } |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * @return \OC\Files\Storage\Storage |
|
169 | - */ |
|
170 | - public function getStorage() { |
|
171 | - if (is_null($this->storage)) { |
|
172 | - $this->createStorage(); |
|
173 | - } |
|
174 | - return $this->storage; |
|
175 | - } |
|
167 | + /** |
|
168 | + * @return \OC\Files\Storage\Storage |
|
169 | + */ |
|
170 | + public function getStorage() { |
|
171 | + if (is_null($this->storage)) { |
|
172 | + $this->createStorage(); |
|
173 | + } |
|
174 | + return $this->storage; |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * @return string |
|
179 | - */ |
|
180 | - public function getStorageId() { |
|
181 | - if (!$this->storageId) { |
|
182 | - if (is_null($this->storage)) { |
|
183 | - $storage = $this->createStorage(); //FIXME: start using exceptions |
|
184 | - if (is_null($storage)) { |
|
185 | - return null; |
|
186 | - } |
|
177 | + /** |
|
178 | + * @return string |
|
179 | + */ |
|
180 | + public function getStorageId() { |
|
181 | + if (!$this->storageId) { |
|
182 | + if (is_null($this->storage)) { |
|
183 | + $storage = $this->createStorage(); //FIXME: start using exceptions |
|
184 | + if (is_null($storage)) { |
|
185 | + return null; |
|
186 | + } |
|
187 | 187 | |
188 | - $this->storage = $storage; |
|
189 | - } |
|
190 | - $this->storageId = $this->storage->getId(); |
|
191 | - if (strlen($this->storageId) > 64) { |
|
192 | - $this->storageId = md5($this->storageId); |
|
193 | - } |
|
194 | - } |
|
195 | - return $this->storageId; |
|
196 | - } |
|
188 | + $this->storage = $storage; |
|
189 | + } |
|
190 | + $this->storageId = $this->storage->getId(); |
|
191 | + if (strlen($this->storageId) > 64) { |
|
192 | + $this->storageId = md5($this->storageId); |
|
193 | + } |
|
194 | + } |
|
195 | + return $this->storageId; |
|
196 | + } |
|
197 | 197 | |
198 | - /** |
|
199 | - * @return int |
|
200 | - */ |
|
201 | - public function getNumericStorageId() { |
|
202 | - return $this->getStorage()->getStorageCache()->getNumericId(); |
|
203 | - } |
|
198 | + /** |
|
199 | + * @return int |
|
200 | + */ |
|
201 | + public function getNumericStorageId() { |
|
202 | + return $this->getStorage()->getStorageCache()->getNumericId(); |
|
203 | + } |
|
204 | 204 | |
205 | - /** |
|
206 | - * @param string $path |
|
207 | - * @return string |
|
208 | - */ |
|
209 | - public function getInternalPath($path) { |
|
210 | - $path = Filesystem::normalizePath($path, true, false, true); |
|
211 | - if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) { |
|
212 | - $internalPath = ''; |
|
213 | - } else { |
|
214 | - $internalPath = substr($path, strlen($this->mountPoint)); |
|
215 | - } |
|
216 | - // substr returns false instead of an empty string, we always want a string |
|
217 | - return (string)$internalPath; |
|
218 | - } |
|
205 | + /** |
|
206 | + * @param string $path |
|
207 | + * @return string |
|
208 | + */ |
|
209 | + public function getInternalPath($path) { |
|
210 | + $path = Filesystem::normalizePath($path, true, false, true); |
|
211 | + if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) { |
|
212 | + $internalPath = ''; |
|
213 | + } else { |
|
214 | + $internalPath = substr($path, strlen($this->mountPoint)); |
|
215 | + } |
|
216 | + // substr returns false instead of an empty string, we always want a string |
|
217 | + return (string)$internalPath; |
|
218 | + } |
|
219 | 219 | |
220 | - /** |
|
221 | - * @param string $path |
|
222 | - * @return string |
|
223 | - */ |
|
224 | - private function formatPath($path) { |
|
225 | - $path = Filesystem::normalizePath($path); |
|
226 | - if (strlen($path) > 1) { |
|
227 | - $path .= '/'; |
|
228 | - } |
|
229 | - return $path; |
|
230 | - } |
|
220 | + /** |
|
221 | + * @param string $path |
|
222 | + * @return string |
|
223 | + */ |
|
224 | + private function formatPath($path) { |
|
225 | + $path = Filesystem::normalizePath($path); |
|
226 | + if (strlen($path) > 1) { |
|
227 | + $path .= '/'; |
|
228 | + } |
|
229 | + return $path; |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * @param callable $wrapper |
|
234 | - */ |
|
235 | - public function wrapStorage($wrapper) { |
|
236 | - $storage = $this->getStorage(); |
|
237 | - // storage can be null if it couldn't be initialized |
|
238 | - if ($storage != null) { |
|
239 | - $this->storage = $wrapper($this->mountPoint, $storage, $this); |
|
240 | - } |
|
241 | - } |
|
232 | + /** |
|
233 | + * @param callable $wrapper |
|
234 | + */ |
|
235 | + public function wrapStorage($wrapper) { |
|
236 | + $storage = $this->getStorage(); |
|
237 | + // storage can be null if it couldn't be initialized |
|
238 | + if ($storage != null) { |
|
239 | + $this->storage = $wrapper($this->mountPoint, $storage, $this); |
|
240 | + } |
|
241 | + } |
|
242 | 242 | |
243 | - /** |
|
244 | - * Get a mount option |
|
245 | - * |
|
246 | - * @param string $name Name of the mount option to get |
|
247 | - * @param mixed $default Default value for the mount option |
|
248 | - * @return mixed |
|
249 | - */ |
|
250 | - public function getOption($name, $default) { |
|
251 | - return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default; |
|
252 | - } |
|
243 | + /** |
|
244 | + * Get a mount option |
|
245 | + * |
|
246 | + * @param string $name Name of the mount option to get |
|
247 | + * @param mixed $default Default value for the mount option |
|
248 | + * @return mixed |
|
249 | + */ |
|
250 | + public function getOption($name, $default) { |
|
251 | + return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default; |
|
252 | + } |
|
253 | 253 | |
254 | - /** |
|
255 | - * Get all options for the mount |
|
256 | - * |
|
257 | - * @return array |
|
258 | - */ |
|
259 | - public function getOptions() { |
|
260 | - return $this->mountOptions; |
|
261 | - } |
|
254 | + /** |
|
255 | + * Get all options for the mount |
|
256 | + * |
|
257 | + * @return array |
|
258 | + */ |
|
259 | + public function getOptions() { |
|
260 | + return $this->mountOptions; |
|
261 | + } |
|
262 | 262 | |
263 | - /** |
|
264 | - * Get the file id of the root of the storage |
|
265 | - * |
|
266 | - * @return int |
|
267 | - */ |
|
268 | - public function getStorageRootId() { |
|
269 | - if (is_null($this->rootId)) { |
|
270 | - $this->rootId = (int)$this->getStorage()->getCache()->getId(''); |
|
271 | - } |
|
272 | - return $this->rootId; |
|
273 | - } |
|
263 | + /** |
|
264 | + * Get the file id of the root of the storage |
|
265 | + * |
|
266 | + * @return int |
|
267 | + */ |
|
268 | + public function getStorageRootId() { |
|
269 | + if (is_null($this->rootId)) { |
|
270 | + $this->rootId = (int)$this->getStorage()->getCache()->getId(''); |
|
271 | + } |
|
272 | + return $this->rootId; |
|
273 | + } |
|
274 | 274 | |
275 | - public function getMountId() { |
|
276 | - return $this->mountId; |
|
277 | - } |
|
275 | + public function getMountId() { |
|
276 | + return $this->mountId; |
|
277 | + } |
|
278 | 278 | |
279 | - public function getMountType() { |
|
280 | - return ''; |
|
281 | - } |
|
279 | + public function getMountType() { |
|
280 | + return ''; |
|
281 | + } |
|
282 | 282 | } |
@@ -33,107 +33,107 @@ |
||
33 | 33 | * Mount provider for object store home storages |
34 | 34 | */ |
35 | 35 | class ObjectHomeMountProvider implements IHomeMountProvider { |
36 | - /** |
|
37 | - * @var IConfig |
|
38 | - */ |
|
39 | - private $config; |
|
40 | - |
|
41 | - /** |
|
42 | - * ObjectStoreHomeMountProvider constructor. |
|
43 | - * |
|
44 | - * @param IConfig $config |
|
45 | - */ |
|
46 | - public function __construct(IConfig $config) { |
|
47 | - $this->config = $config; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * Get the cache mount for a user |
|
52 | - * |
|
53 | - * @param IUser $user |
|
54 | - * @param IStorageFactory $loader |
|
55 | - * @return \OCP\Files\Mount\IMountPoint |
|
56 | - */ |
|
57 | - public function getHomeMountForUser(IUser $user, IStorageFactory $loader) { |
|
58 | - |
|
59 | - $config = $this->getMultiBucketObjectStoreConfig($user); |
|
60 | - if ($config === null) { |
|
61 | - $config = $this->getSingleBucketObjectStoreConfig($user); |
|
62 | - } |
|
63 | - |
|
64 | - if ($config === null) { |
|
65 | - return null; |
|
66 | - } |
|
67 | - |
|
68 | - return new MountPoint('\OC\Files\ObjectStore\HomeObjectStoreStorage', '/' . $user->getUID(), $config['arguments'], $loader); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @param IUser $user |
|
73 | - * @return array|null |
|
74 | - */ |
|
75 | - private function getSingleBucketObjectStoreConfig(IUser $user) { |
|
76 | - $config = $this->config->getSystemValue('objectstore'); |
|
77 | - if (!is_array($config)) { |
|
78 | - return null; |
|
79 | - } |
|
80 | - |
|
81 | - // sanity checks |
|
82 | - if (empty($config['class'])) { |
|
83 | - \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); |
|
84 | - } |
|
85 | - if (!isset($config['arguments'])) { |
|
86 | - $config['arguments'] = []; |
|
87 | - } |
|
88 | - // instantiate object store implementation |
|
89 | - $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
90 | - |
|
91 | - $config['arguments']['user'] = $user; |
|
92 | - |
|
93 | - return $config; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @param IUser $user |
|
98 | - * @return array|null |
|
99 | - */ |
|
100 | - private function getMultiBucketObjectStoreConfig(IUser $user) { |
|
101 | - $config = $this->config->getSystemValue('objectstore_multibucket'); |
|
102 | - if (!is_array($config)) { |
|
103 | - return null; |
|
104 | - } |
|
105 | - |
|
106 | - // sanity checks |
|
107 | - if (empty($config['class'])) { |
|
108 | - \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); |
|
109 | - } |
|
110 | - if (!isset($config['arguments'])) { |
|
111 | - $config['arguments'] = []; |
|
112 | - } |
|
113 | - $config['arguments']['user'] = $user; |
|
114 | - |
|
115 | - $bucket = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null); |
|
116 | - |
|
117 | - if ($bucket === null) { |
|
118 | - /* |
|
36 | + /** |
|
37 | + * @var IConfig |
|
38 | + */ |
|
39 | + private $config; |
|
40 | + |
|
41 | + /** |
|
42 | + * ObjectStoreHomeMountProvider constructor. |
|
43 | + * |
|
44 | + * @param IConfig $config |
|
45 | + */ |
|
46 | + public function __construct(IConfig $config) { |
|
47 | + $this->config = $config; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * Get the cache mount for a user |
|
52 | + * |
|
53 | + * @param IUser $user |
|
54 | + * @param IStorageFactory $loader |
|
55 | + * @return \OCP\Files\Mount\IMountPoint |
|
56 | + */ |
|
57 | + public function getHomeMountForUser(IUser $user, IStorageFactory $loader) { |
|
58 | + |
|
59 | + $config = $this->getMultiBucketObjectStoreConfig($user); |
|
60 | + if ($config === null) { |
|
61 | + $config = $this->getSingleBucketObjectStoreConfig($user); |
|
62 | + } |
|
63 | + |
|
64 | + if ($config === null) { |
|
65 | + return null; |
|
66 | + } |
|
67 | + |
|
68 | + return new MountPoint('\OC\Files\ObjectStore\HomeObjectStoreStorage', '/' . $user->getUID(), $config['arguments'], $loader); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @param IUser $user |
|
73 | + * @return array|null |
|
74 | + */ |
|
75 | + private function getSingleBucketObjectStoreConfig(IUser $user) { |
|
76 | + $config = $this->config->getSystemValue('objectstore'); |
|
77 | + if (!is_array($config)) { |
|
78 | + return null; |
|
79 | + } |
|
80 | + |
|
81 | + // sanity checks |
|
82 | + if (empty($config['class'])) { |
|
83 | + \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); |
|
84 | + } |
|
85 | + if (!isset($config['arguments'])) { |
|
86 | + $config['arguments'] = []; |
|
87 | + } |
|
88 | + // instantiate object store implementation |
|
89 | + $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
90 | + |
|
91 | + $config['arguments']['user'] = $user; |
|
92 | + |
|
93 | + return $config; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @param IUser $user |
|
98 | + * @return array|null |
|
99 | + */ |
|
100 | + private function getMultiBucketObjectStoreConfig(IUser $user) { |
|
101 | + $config = $this->config->getSystemValue('objectstore_multibucket'); |
|
102 | + if (!is_array($config)) { |
|
103 | + return null; |
|
104 | + } |
|
105 | + |
|
106 | + // sanity checks |
|
107 | + if (empty($config['class'])) { |
|
108 | + \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); |
|
109 | + } |
|
110 | + if (!isset($config['arguments'])) { |
|
111 | + $config['arguments'] = []; |
|
112 | + } |
|
113 | + $config['arguments']['user'] = $user; |
|
114 | + |
|
115 | + $bucket = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null); |
|
116 | + |
|
117 | + if ($bucket === null) { |
|
118 | + /* |
|
119 | 119 | * Use any provided bucket argument as prefix |
120 | 120 | * and add the mapping from username => bucket |
121 | 121 | */ |
122 | - if (!isset($config['arguments']['bucket'])) { |
|
123 | - $config['arguments']['bucket'] = ''; |
|
124 | - } |
|
125 | - $mapper = new \OC\Files\ObjectStore\Mapper($user); |
|
126 | - $numBuckets = isset($config['arguments']['num_buckets']) ? $config['arguments']['num_buckets'] : 64; |
|
127 | - $config['arguments']['bucket'] .= $mapper->getBucket($numBuckets); |
|
128 | - |
|
129 | - $this->config->setUserValue($user->getUID(), 'homeobjectstore', 'bucket', $config['arguments']['bucket']); |
|
130 | - } else { |
|
131 | - $config['arguments']['bucket'] = $bucket; |
|
132 | - } |
|
133 | - |
|
134 | - // instantiate object store implementation |
|
135 | - $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
136 | - |
|
137 | - return $config; |
|
138 | - } |
|
122 | + if (!isset($config['arguments']['bucket'])) { |
|
123 | + $config['arguments']['bucket'] = ''; |
|
124 | + } |
|
125 | + $mapper = new \OC\Files\ObjectStore\Mapper($user); |
|
126 | + $numBuckets = isset($config['arguments']['num_buckets']) ? $config['arguments']['num_buckets'] : 64; |
|
127 | + $config['arguments']['bucket'] .= $mapper->getBucket($numBuckets); |
|
128 | + |
|
129 | + $this->config->setUserValue($user->getUID(), 'homeobjectstore', 'bucket', $config['arguments']['bucket']); |
|
130 | + } else { |
|
131 | + $config['arguments']['bucket'] = $bucket; |
|
132 | + } |
|
133 | + |
|
134 | + // instantiate object store implementation |
|
135 | + $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
136 | + |
|
137 | + return $config; |
|
138 | + } |
|
139 | 139 | } |
@@ -48,981 +48,981 @@ |
||
48 | 48 | |
49 | 49 | class Encryption extends Wrapper { |
50 | 50 | |
51 | - use LocalTempFileTrait; |
|
52 | - |
|
53 | - /** @var string */ |
|
54 | - private $mountPoint; |
|
55 | - |
|
56 | - /** @var \OC\Encryption\Util */ |
|
57 | - private $util; |
|
58 | - |
|
59 | - /** @var \OCP\Encryption\IManager */ |
|
60 | - private $encryptionManager; |
|
61 | - |
|
62 | - /** @var \OCP\ILogger */ |
|
63 | - private $logger; |
|
64 | - |
|
65 | - /** @var string */ |
|
66 | - private $uid; |
|
67 | - |
|
68 | - /** @var array */ |
|
69 | - protected $unencryptedSize; |
|
70 | - |
|
71 | - /** @var \OCP\Encryption\IFile */ |
|
72 | - private $fileHelper; |
|
73 | - |
|
74 | - /** @var IMountPoint */ |
|
75 | - private $mount; |
|
76 | - |
|
77 | - /** @var IStorage */ |
|
78 | - private $keyStorage; |
|
79 | - |
|
80 | - /** @var Update */ |
|
81 | - private $update; |
|
82 | - |
|
83 | - /** @var Manager */ |
|
84 | - private $mountManager; |
|
85 | - |
|
86 | - /** @var array remember for which path we execute the repair step to avoid recursions */ |
|
87 | - private $fixUnencryptedSizeOf = array(); |
|
88 | - |
|
89 | - /** @var ArrayCache */ |
|
90 | - private $arrayCache; |
|
91 | - |
|
92 | - /** |
|
93 | - * @param array $parameters |
|
94 | - * @param IManager $encryptionManager |
|
95 | - * @param Util $util |
|
96 | - * @param ILogger $logger |
|
97 | - * @param IFile $fileHelper |
|
98 | - * @param string $uid |
|
99 | - * @param IStorage $keyStorage |
|
100 | - * @param Update $update |
|
101 | - * @param Manager $mountManager |
|
102 | - * @param ArrayCache $arrayCache |
|
103 | - */ |
|
104 | - public function __construct( |
|
105 | - $parameters, |
|
106 | - IManager $encryptionManager = null, |
|
107 | - Util $util = null, |
|
108 | - ILogger $logger = null, |
|
109 | - IFile $fileHelper = null, |
|
110 | - $uid = null, |
|
111 | - IStorage $keyStorage = null, |
|
112 | - Update $update = null, |
|
113 | - Manager $mountManager = null, |
|
114 | - ArrayCache $arrayCache = null |
|
115 | - ) { |
|
116 | - |
|
117 | - $this->mountPoint = $parameters['mountPoint']; |
|
118 | - $this->mount = $parameters['mount']; |
|
119 | - $this->encryptionManager = $encryptionManager; |
|
120 | - $this->util = $util; |
|
121 | - $this->logger = $logger; |
|
122 | - $this->uid = $uid; |
|
123 | - $this->fileHelper = $fileHelper; |
|
124 | - $this->keyStorage = $keyStorage; |
|
125 | - $this->unencryptedSize = array(); |
|
126 | - $this->update = $update; |
|
127 | - $this->mountManager = $mountManager; |
|
128 | - $this->arrayCache = $arrayCache; |
|
129 | - parent::__construct($parameters); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * see http://php.net/manual/en/function.filesize.php |
|
134 | - * The result for filesize when called on a folder is required to be 0 |
|
135 | - * |
|
136 | - * @param string $path |
|
137 | - * @return int |
|
138 | - */ |
|
139 | - public function filesize($path) { |
|
140 | - $fullPath = $this->getFullPath($path); |
|
141 | - |
|
142 | - /** @var CacheEntry $info */ |
|
143 | - $info = $this->getCache()->get($path); |
|
144 | - if (isset($this->unencryptedSize[$fullPath])) { |
|
145 | - $size = $this->unencryptedSize[$fullPath]; |
|
146 | - // update file cache |
|
147 | - if ($info instanceof ICacheEntry) { |
|
148 | - $info = $info->getData(); |
|
149 | - $info['encrypted'] = $info['encryptedVersion']; |
|
150 | - } else { |
|
151 | - if (!is_array($info)) { |
|
152 | - $info = []; |
|
153 | - } |
|
154 | - $info['encrypted'] = true; |
|
155 | - } |
|
156 | - |
|
157 | - $info['size'] = $size; |
|
158 | - $this->getCache()->put($path, $info); |
|
159 | - |
|
160 | - return $size; |
|
161 | - } |
|
162 | - |
|
163 | - if (isset($info['fileid']) && $info['encrypted']) { |
|
164 | - return $this->verifyUnencryptedSize($path, $info['size']); |
|
165 | - } |
|
166 | - |
|
167 | - return $this->storage->filesize($path); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * @param string $path |
|
172 | - * @return array |
|
173 | - */ |
|
174 | - public function getMetaData($path) { |
|
175 | - $data = $this->storage->getMetaData($path); |
|
176 | - if (is_null($data)) { |
|
177 | - return null; |
|
178 | - } |
|
179 | - $fullPath = $this->getFullPath($path); |
|
180 | - $info = $this->getCache()->get($path); |
|
181 | - |
|
182 | - if (isset($this->unencryptedSize[$fullPath])) { |
|
183 | - $data['encrypted'] = true; |
|
184 | - $data['size'] = $this->unencryptedSize[$fullPath]; |
|
185 | - } else { |
|
186 | - if (isset($info['fileid']) && $info['encrypted']) { |
|
187 | - $data['size'] = $this->verifyUnencryptedSize($path, $info['size']); |
|
188 | - $data['encrypted'] = true; |
|
189 | - } |
|
190 | - } |
|
191 | - |
|
192 | - if (isset($info['encryptedVersion']) && $info['encryptedVersion'] > 1) { |
|
193 | - $data['encryptedVersion'] = $info['encryptedVersion']; |
|
194 | - } |
|
195 | - |
|
196 | - return $data; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * see http://php.net/manual/en/function.file_get_contents.php |
|
201 | - * |
|
202 | - * @param string $path |
|
203 | - * @return string |
|
204 | - */ |
|
205 | - public function file_get_contents($path) { |
|
206 | - |
|
207 | - $encryptionModule = $this->getEncryptionModule($path); |
|
208 | - |
|
209 | - if ($encryptionModule) { |
|
210 | - $handle = $this->fopen($path, "r"); |
|
211 | - if (!$handle) { |
|
212 | - return false; |
|
213 | - } |
|
214 | - $data = stream_get_contents($handle); |
|
215 | - fclose($handle); |
|
216 | - return $data; |
|
217 | - } |
|
218 | - return $this->storage->file_get_contents($path); |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * see http://php.net/manual/en/function.file_put_contents.php |
|
223 | - * |
|
224 | - * @param string $path |
|
225 | - * @param string $data |
|
226 | - * @return bool |
|
227 | - */ |
|
228 | - public function file_put_contents($path, $data) { |
|
229 | - // file put content will always be translated to a stream write |
|
230 | - $handle = $this->fopen($path, 'w'); |
|
231 | - if (is_resource($handle)) { |
|
232 | - $written = fwrite($handle, $data); |
|
233 | - fclose($handle); |
|
234 | - return $written; |
|
235 | - } |
|
236 | - |
|
237 | - return false; |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * see http://php.net/manual/en/function.unlink.php |
|
242 | - * |
|
243 | - * @param string $path |
|
244 | - * @return bool |
|
245 | - */ |
|
246 | - public function unlink($path) { |
|
247 | - $fullPath = $this->getFullPath($path); |
|
248 | - if ($this->util->isExcluded($fullPath)) { |
|
249 | - return $this->storage->unlink($path); |
|
250 | - } |
|
251 | - |
|
252 | - $encryptionModule = $this->getEncryptionModule($path); |
|
253 | - if ($encryptionModule) { |
|
254 | - $this->keyStorage->deleteAllFileKeys($this->getFullPath($path)); |
|
255 | - } |
|
256 | - |
|
257 | - return $this->storage->unlink($path); |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * see http://php.net/manual/en/function.rename.php |
|
262 | - * |
|
263 | - * @param string $path1 |
|
264 | - * @param string $path2 |
|
265 | - * @return bool |
|
266 | - */ |
|
267 | - public function rename($path1, $path2) { |
|
268 | - |
|
269 | - $result = $this->storage->rename($path1, $path2); |
|
270 | - |
|
271 | - if ($result && |
|
272 | - // versions always use the keys from the original file, so we can skip |
|
273 | - // this step for versions |
|
274 | - $this->isVersion($path2) === false && |
|
275 | - $this->encryptionManager->isEnabled()) { |
|
276 | - $source = $this->getFullPath($path1); |
|
277 | - if (!$this->util->isExcluded($source)) { |
|
278 | - $target = $this->getFullPath($path2); |
|
279 | - if (isset($this->unencryptedSize[$source])) { |
|
280 | - $this->unencryptedSize[$target] = $this->unencryptedSize[$source]; |
|
281 | - } |
|
282 | - $this->keyStorage->renameKeys($source, $target); |
|
283 | - $module = $this->getEncryptionModule($path2); |
|
284 | - if ($module) { |
|
285 | - $module->update($target, $this->uid, []); |
|
286 | - } |
|
287 | - } |
|
288 | - } |
|
289 | - |
|
290 | - return $result; |
|
291 | - } |
|
292 | - |
|
293 | - /** |
|
294 | - * see http://php.net/manual/en/function.rmdir.php |
|
295 | - * |
|
296 | - * @param string $path |
|
297 | - * @return bool |
|
298 | - */ |
|
299 | - public function rmdir($path) { |
|
300 | - $result = $this->storage->rmdir($path); |
|
301 | - $fullPath = $this->getFullPath($path); |
|
302 | - if ($result && |
|
303 | - $this->util->isExcluded($fullPath) === false && |
|
304 | - $this->encryptionManager->isEnabled() |
|
305 | - ) { |
|
306 | - $this->keyStorage->deleteAllFileKeys($fullPath); |
|
307 | - } |
|
308 | - |
|
309 | - return $result; |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * check if a file can be read |
|
314 | - * |
|
315 | - * @param string $path |
|
316 | - * @return bool |
|
317 | - */ |
|
318 | - public function isReadable($path) { |
|
319 | - |
|
320 | - $isReadable = true; |
|
321 | - |
|
322 | - $metaData = $this->getMetaData($path); |
|
323 | - if ( |
|
324 | - !$this->is_dir($path) && |
|
325 | - isset($metaData['encrypted']) && |
|
326 | - $metaData['encrypted'] === true |
|
327 | - ) { |
|
328 | - $fullPath = $this->getFullPath($path); |
|
329 | - $module = $this->getEncryptionModule($path); |
|
330 | - $isReadable = $module->isReadable($fullPath, $this->uid); |
|
331 | - } |
|
332 | - |
|
333 | - return $this->storage->isReadable($path) && $isReadable; |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * see http://php.net/manual/en/function.copy.php |
|
338 | - * |
|
339 | - * @param string $path1 |
|
340 | - * @param string $path2 |
|
341 | - * @return bool |
|
342 | - */ |
|
343 | - public function copy($path1, $path2) { |
|
344 | - |
|
345 | - $source = $this->getFullPath($path1); |
|
346 | - |
|
347 | - if ($this->util->isExcluded($source)) { |
|
348 | - return $this->storage->copy($path1, $path2); |
|
349 | - } |
|
350 | - |
|
351 | - // need to stream copy file by file in case we copy between a encrypted |
|
352 | - // and a unencrypted storage |
|
353 | - $this->unlink($path2); |
|
354 | - return $this->copyFromStorage($this, $path1, $path2); |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * see http://php.net/manual/en/function.fopen.php |
|
359 | - * |
|
360 | - * @param string $path |
|
361 | - * @param string $mode |
|
362 | - * @return resource|bool |
|
363 | - * @throws GenericEncryptionException |
|
364 | - * @throws ModuleDoesNotExistsException |
|
365 | - */ |
|
366 | - public function fopen($path, $mode) { |
|
367 | - |
|
368 | - // check if the file is stored in the array cache, this means that we |
|
369 | - // copy a file over to the versions folder, in this case we don't want to |
|
370 | - // decrypt it |
|
371 | - if ($this->arrayCache->hasKey('encryption_copy_version_' . $path)) { |
|
372 | - $this->arrayCache->remove('encryption_copy_version_' . $path); |
|
373 | - return $this->storage->fopen($path, $mode); |
|
374 | - } |
|
375 | - |
|
376 | - $encryptionEnabled = $this->encryptionManager->isEnabled(); |
|
377 | - $shouldEncrypt = false; |
|
378 | - $encryptionModule = null; |
|
379 | - $header = $this->getHeader($path); |
|
380 | - $signed = isset($header['signed']) && $header['signed'] === 'true'; |
|
381 | - $fullPath = $this->getFullPath($path); |
|
382 | - $encryptionModuleId = $this->util->getEncryptionModuleId($header); |
|
383 | - |
|
384 | - if ($this->util->isExcluded($fullPath) === false) { |
|
385 | - |
|
386 | - $size = $unencryptedSize = 0; |
|
387 | - $realFile = $this->util->stripPartialFileExtension($path); |
|
388 | - $targetExists = $this->file_exists($realFile) || $this->file_exists($path); |
|
389 | - $targetIsEncrypted = false; |
|
390 | - if ($targetExists) { |
|
391 | - // in case the file exists we require the explicit module as |
|
392 | - // specified in the file header - otherwise we need to fail hard to |
|
393 | - // prevent data loss on client side |
|
394 | - if (!empty($encryptionModuleId)) { |
|
395 | - $targetIsEncrypted = true; |
|
396 | - $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
397 | - } |
|
398 | - |
|
399 | - if ($this->file_exists($path)) { |
|
400 | - $size = $this->storage->filesize($path); |
|
401 | - $unencryptedSize = $this->filesize($path); |
|
402 | - } else { |
|
403 | - $size = $unencryptedSize = 0; |
|
404 | - } |
|
405 | - } |
|
406 | - |
|
407 | - try { |
|
408 | - |
|
409 | - if ( |
|
410 | - $mode === 'w' |
|
411 | - || $mode === 'w+' |
|
412 | - || $mode === 'wb' |
|
413 | - || $mode === 'wb+' |
|
414 | - ) { |
|
415 | - // don't overwrite encrypted files if encryption is not enabled |
|
416 | - if ($targetIsEncrypted && $encryptionEnabled === false) { |
|
417 | - throw new GenericEncryptionException('Tried to access encrypted file but encryption is not enabled'); |
|
418 | - } |
|
419 | - if ($encryptionEnabled) { |
|
420 | - // if $encryptionModuleId is empty, the default module will be used |
|
421 | - $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
422 | - $shouldEncrypt = $encryptionModule->shouldEncrypt($fullPath); |
|
423 | - $signed = true; |
|
424 | - } |
|
425 | - } else { |
|
426 | - $info = $this->getCache()->get($path); |
|
427 | - // only get encryption module if we found one in the header |
|
428 | - // or if file should be encrypted according to the file cache |
|
429 | - if (!empty($encryptionModuleId)) { |
|
430 | - $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
431 | - $shouldEncrypt = true; |
|
432 | - } else if (empty($encryptionModuleId) && $info['encrypted'] === true) { |
|
433 | - // we come from a old installation. No header and/or no module defined |
|
434 | - // but the file is encrypted. In this case we need to use the |
|
435 | - // OC_DEFAULT_MODULE to read the file |
|
436 | - $encryptionModule = $this->encryptionManager->getEncryptionModule('OC_DEFAULT_MODULE'); |
|
437 | - $shouldEncrypt = true; |
|
438 | - $targetIsEncrypted = true; |
|
439 | - } |
|
440 | - } |
|
441 | - } catch (ModuleDoesNotExistsException $e) { |
|
442 | - $this->logger->logException($e, [ |
|
443 | - 'message' => 'Encryption module "' . $encryptionModuleId . '" not found, file will be stored unencrypted', |
|
444 | - 'level' => ILogger::WARN, |
|
445 | - 'app' => 'core', |
|
446 | - ]); |
|
447 | - } |
|
448 | - |
|
449 | - // encryption disabled on write of new file and write to existing unencrypted file -> don't encrypt |
|
450 | - if (!$encryptionEnabled || !$this->shouldEncrypt($path)) { |
|
451 | - if (!$targetExists || !$targetIsEncrypted) { |
|
452 | - $shouldEncrypt = false; |
|
453 | - } |
|
454 | - } |
|
455 | - |
|
456 | - if ($shouldEncrypt === true && $encryptionModule !== null) { |
|
457 | - $headerSize = $this->getHeaderSize($path); |
|
458 | - $source = $this->storage->fopen($path, $mode); |
|
459 | - if (!is_resource($source)) { |
|
460 | - return false; |
|
461 | - } |
|
462 | - $handle = \OC\Files\Stream\Encryption::wrap($source, $path, $fullPath, $header, |
|
463 | - $this->uid, $encryptionModule, $this->storage, $this, $this->util, $this->fileHelper, $mode, |
|
464 | - $size, $unencryptedSize, $headerSize, $signed); |
|
465 | - return $handle; |
|
466 | - } |
|
467 | - |
|
468 | - } |
|
469 | - |
|
470 | - return $this->storage->fopen($path, $mode); |
|
471 | - } |
|
472 | - |
|
473 | - |
|
474 | - /** |
|
475 | - * perform some plausibility checks if the the unencrypted size is correct. |
|
476 | - * If not, we calculate the correct unencrypted size and return it |
|
477 | - * |
|
478 | - * @param string $path internal path relative to the storage root |
|
479 | - * @param int $unencryptedSize size of the unencrypted file |
|
480 | - * |
|
481 | - * @return int unencrypted size |
|
482 | - */ |
|
483 | - protected function verifyUnencryptedSize($path, $unencryptedSize) { |
|
484 | - |
|
485 | - $size = $this->storage->filesize($path); |
|
486 | - $result = $unencryptedSize; |
|
487 | - |
|
488 | - if ($unencryptedSize < 0 || |
|
489 | - ($size > 0 && $unencryptedSize === $size) |
|
490 | - ) { |
|
491 | - // check if we already calculate the unencrypted size for the |
|
492 | - // given path to avoid recursions |
|
493 | - if (isset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]) === false) { |
|
494 | - $this->fixUnencryptedSizeOf[$this->getFullPath($path)] = true; |
|
495 | - try { |
|
496 | - $result = $this->fixUnencryptedSize($path, $size, $unencryptedSize); |
|
497 | - } catch (\Exception $e) { |
|
498 | - $this->logger->error('Couldn\'t re-calculate unencrypted size for '. $path); |
|
499 | - $this->logger->logException($e); |
|
500 | - } |
|
501 | - unset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]); |
|
502 | - } |
|
503 | - } |
|
504 | - |
|
505 | - return $result; |
|
506 | - } |
|
507 | - |
|
508 | - /** |
|
509 | - * calculate the unencrypted size |
|
510 | - * |
|
511 | - * @param string $path internal path relative to the storage root |
|
512 | - * @param int $size size of the physical file |
|
513 | - * @param int $unencryptedSize size of the unencrypted file |
|
514 | - * |
|
515 | - * @return int calculated unencrypted size |
|
516 | - */ |
|
517 | - protected function fixUnencryptedSize($path, $size, $unencryptedSize) { |
|
518 | - |
|
519 | - $headerSize = $this->getHeaderSize($path); |
|
520 | - $header = $this->getHeader($path); |
|
521 | - $encryptionModule = $this->getEncryptionModule($path); |
|
522 | - |
|
523 | - $stream = $this->storage->fopen($path, 'r'); |
|
524 | - |
|
525 | - // if we couldn't open the file we return the old unencrypted size |
|
526 | - if (!is_resource($stream)) { |
|
527 | - $this->logger->error('Could not open ' . $path . '. Recalculation of unencrypted size aborted.'); |
|
528 | - return $unencryptedSize; |
|
529 | - } |
|
530 | - |
|
531 | - $newUnencryptedSize = 0; |
|
532 | - $size -= $headerSize; |
|
533 | - $blockSize = $this->util->getBlockSize(); |
|
534 | - |
|
535 | - // if a header exists we skip it |
|
536 | - if ($headerSize > 0) { |
|
537 | - fread($stream, $headerSize); |
|
538 | - } |
|
539 | - |
|
540 | - // fast path, else the calculation for $lastChunkNr is bogus |
|
541 | - if ($size === 0) { |
|
542 | - return 0; |
|
543 | - } |
|
544 | - |
|
545 | - $signed = isset($header['signed']) && $header['signed'] === 'true'; |
|
546 | - $unencryptedBlockSize = $encryptionModule->getUnencryptedBlockSize($signed); |
|
547 | - |
|
548 | - // calculate last chunk nr |
|
549 | - // next highest is end of chunks, one subtracted is last one |
|
550 | - // we have to read the last chunk, we can't just calculate it (because of padding etc) |
|
551 | - |
|
552 | - $lastChunkNr = ceil($size/ $blockSize)-1; |
|
553 | - // calculate last chunk position |
|
554 | - $lastChunkPos = ($lastChunkNr * $blockSize); |
|
555 | - // try to fseek to the last chunk, if it fails we have to read the whole file |
|
556 | - if (@fseek($stream, $lastChunkPos, SEEK_CUR) === 0) { |
|
557 | - $newUnencryptedSize += $lastChunkNr * $unencryptedBlockSize; |
|
558 | - } |
|
559 | - |
|
560 | - $lastChunkContentEncrypted=''; |
|
561 | - $count = $blockSize; |
|
562 | - |
|
563 | - while ($count > 0) { |
|
564 | - $data=fread($stream, $blockSize); |
|
565 | - $count=strlen($data); |
|
566 | - $lastChunkContentEncrypted .= $data; |
|
567 | - if(strlen($lastChunkContentEncrypted) > $blockSize) { |
|
568 | - $newUnencryptedSize += $unencryptedBlockSize; |
|
569 | - $lastChunkContentEncrypted=substr($lastChunkContentEncrypted, $blockSize); |
|
570 | - } |
|
571 | - } |
|
572 | - |
|
573 | - fclose($stream); |
|
574 | - |
|
575 | - // we have to decrypt the last chunk to get it actual size |
|
576 | - $encryptionModule->begin($this->getFullPath($path), $this->uid, 'r', $header, []); |
|
577 | - $decryptedLastChunk = $encryptionModule->decrypt($lastChunkContentEncrypted, $lastChunkNr . 'end'); |
|
578 | - $decryptedLastChunk .= $encryptionModule->end($this->getFullPath($path), $lastChunkNr . 'end'); |
|
579 | - |
|
580 | - // calc the real file size with the size of the last chunk |
|
581 | - $newUnencryptedSize += strlen($decryptedLastChunk); |
|
582 | - |
|
583 | - $this->updateUnencryptedSize($this->getFullPath($path), $newUnencryptedSize); |
|
584 | - |
|
585 | - // write to cache if applicable |
|
586 | - $cache = $this->storage->getCache(); |
|
587 | - if ($cache) { |
|
588 | - $entry = $cache->get($path); |
|
589 | - $cache->update($entry['fileid'], ['size' => $newUnencryptedSize]); |
|
590 | - } |
|
591 | - |
|
592 | - return $newUnencryptedSize; |
|
593 | - } |
|
594 | - |
|
595 | - /** |
|
596 | - * @param Storage\IStorage $sourceStorage |
|
597 | - * @param string $sourceInternalPath |
|
598 | - * @param string $targetInternalPath |
|
599 | - * @param bool $preserveMtime |
|
600 | - * @return bool |
|
601 | - */ |
|
602 | - public function moveFromStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = true) { |
|
603 | - if ($sourceStorage === $this) { |
|
604 | - return $this->rename($sourceInternalPath, $targetInternalPath); |
|
605 | - } |
|
606 | - |
|
607 | - // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed: |
|
608 | - // - call $this->storage->moveFromStorage() instead of $this->copyBetweenStorage |
|
609 | - // - copy the file cache update from $this->copyBetweenStorage to this method |
|
610 | - // - copy the copyKeys() call from $this->copyBetweenStorage to this method |
|
611 | - // - remove $this->copyBetweenStorage |
|
612 | - |
|
613 | - if (!$sourceStorage->isDeletable($sourceInternalPath)) { |
|
614 | - return false; |
|
615 | - } |
|
616 | - |
|
617 | - $result = $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, true); |
|
618 | - if ($result) { |
|
619 | - if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
620 | - $result &= $sourceStorage->rmdir($sourceInternalPath); |
|
621 | - } else { |
|
622 | - $result &= $sourceStorage->unlink($sourceInternalPath); |
|
623 | - } |
|
624 | - } |
|
625 | - return $result; |
|
626 | - } |
|
627 | - |
|
628 | - |
|
629 | - /** |
|
630 | - * @param Storage\IStorage $sourceStorage |
|
631 | - * @param string $sourceInternalPath |
|
632 | - * @param string $targetInternalPath |
|
633 | - * @param bool $preserveMtime |
|
634 | - * @param bool $isRename |
|
635 | - * @return bool |
|
636 | - */ |
|
637 | - public function copyFromStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false, $isRename = false) { |
|
638 | - |
|
639 | - // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed: |
|
640 | - // - call $this->storage->copyFromStorage() instead of $this->copyBetweenStorage |
|
641 | - // - copy the file cache update from $this->copyBetweenStorage to this method |
|
642 | - // - copy the copyKeys() call from $this->copyBetweenStorage to this method |
|
643 | - // - remove $this->copyBetweenStorage |
|
644 | - |
|
645 | - return $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename); |
|
646 | - } |
|
647 | - |
|
648 | - /** |
|
649 | - * Update the encrypted cache version in the database |
|
650 | - * |
|
651 | - * @param Storage\IStorage $sourceStorage |
|
652 | - * @param string $sourceInternalPath |
|
653 | - * @param string $targetInternalPath |
|
654 | - * @param bool $isRename |
|
655 | - * @param bool $keepEncryptionVersion |
|
656 | - */ |
|
657 | - private function updateEncryptedVersion(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, $keepEncryptionVersion) { |
|
658 | - $isEncrypted = $this->encryptionManager->isEnabled() && $this->shouldEncrypt($targetInternalPath); |
|
659 | - $cacheInformation = [ |
|
660 | - 'encrypted' => $isEncrypted, |
|
661 | - ]; |
|
662 | - if($isEncrypted) { |
|
663 | - $encryptedVersion = $sourceStorage->getCache()->get($sourceInternalPath)['encryptedVersion']; |
|
664 | - |
|
665 | - // In case of a move operation from an unencrypted to an encrypted |
|
666 | - // storage the old encrypted version would stay with "0" while the |
|
667 | - // correct value would be "1". Thus we manually set the value to "1" |
|
668 | - // for those cases. |
|
669 | - // See also https://github.com/owncloud/core/issues/23078 |
|
670 | - if($encryptedVersion === 0 || !$keepEncryptionVersion) { |
|
671 | - $encryptedVersion = 1; |
|
672 | - } |
|
673 | - |
|
674 | - $cacheInformation['encryptedVersion'] = $encryptedVersion; |
|
675 | - } |
|
676 | - |
|
677 | - // in case of a rename we need to manipulate the source cache because |
|
678 | - // this information will be kept for the new target |
|
679 | - if ($isRename) { |
|
680 | - $sourceStorage->getCache()->put($sourceInternalPath, $cacheInformation); |
|
681 | - } else { |
|
682 | - $this->getCache()->put($targetInternalPath, $cacheInformation); |
|
683 | - } |
|
684 | - } |
|
685 | - |
|
686 | - /** |
|
687 | - * copy file between two storages |
|
688 | - * |
|
689 | - * @param Storage\IStorage $sourceStorage |
|
690 | - * @param string $sourceInternalPath |
|
691 | - * @param string $targetInternalPath |
|
692 | - * @param bool $preserveMtime |
|
693 | - * @param bool $isRename |
|
694 | - * @return bool |
|
695 | - * @throws \Exception |
|
696 | - */ |
|
697 | - private function copyBetweenStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename) { |
|
698 | - |
|
699 | - // for versions we have nothing to do, because versions should always use the |
|
700 | - // key from the original file. Just create a 1:1 copy and done |
|
701 | - if ($this->isVersion($targetInternalPath) || |
|
702 | - $this->isVersion($sourceInternalPath)) { |
|
703 | - // remember that we try to create a version so that we can detect it during |
|
704 | - // fopen($sourceInternalPath) and by-pass the encryption in order to |
|
705 | - // create a 1:1 copy of the file |
|
706 | - $this->arrayCache->set('encryption_copy_version_' . $sourceInternalPath, true); |
|
707 | - $result = $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
708 | - $this->arrayCache->remove('encryption_copy_version_' . $sourceInternalPath); |
|
709 | - if ($result) { |
|
710 | - $info = $this->getCache('', $sourceStorage)->get($sourceInternalPath); |
|
711 | - // make sure that we update the unencrypted size for the version |
|
712 | - if (isset($info['encrypted']) && $info['encrypted'] === true) { |
|
713 | - $this->updateUnencryptedSize( |
|
714 | - $this->getFullPath($targetInternalPath), |
|
715 | - $info['size'] |
|
716 | - ); |
|
717 | - } |
|
718 | - $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, true); |
|
719 | - } |
|
720 | - return $result; |
|
721 | - } |
|
722 | - |
|
723 | - // first copy the keys that we reuse the existing file key on the target location |
|
724 | - // and don't create a new one which would break versions for example. |
|
725 | - $mount = $this->mountManager->findByStorageId($sourceStorage->getId()); |
|
726 | - if (count($mount) === 1) { |
|
727 | - $mountPoint = $mount[0]->getMountPoint(); |
|
728 | - $source = $mountPoint . '/' . $sourceInternalPath; |
|
729 | - $target = $this->getFullPath($targetInternalPath); |
|
730 | - $this->copyKeys($source, $target); |
|
731 | - } else { |
|
732 | - $this->logger->error('Could not find mount point, can\'t keep encryption keys'); |
|
733 | - } |
|
734 | - |
|
735 | - if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
736 | - $dh = $sourceStorage->opendir($sourceInternalPath); |
|
737 | - $result = $this->mkdir($targetInternalPath); |
|
738 | - if (is_resource($dh)) { |
|
739 | - while ($result and ($file = readdir($dh)) !== false) { |
|
740 | - if (!Filesystem::isIgnoredDir($file)) { |
|
741 | - $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, false, $isRename); |
|
742 | - } |
|
743 | - } |
|
744 | - } |
|
745 | - } else { |
|
746 | - try { |
|
747 | - $source = $sourceStorage->fopen($sourceInternalPath, 'r'); |
|
748 | - $target = $this->fopen($targetInternalPath, 'w'); |
|
749 | - list(, $result) = \OC_Helper::streamCopy($source, $target); |
|
750 | - fclose($source); |
|
751 | - fclose($target); |
|
752 | - } catch (\Exception $e) { |
|
753 | - fclose($source); |
|
754 | - fclose($target); |
|
755 | - throw $e; |
|
756 | - } |
|
757 | - if($result) { |
|
758 | - if ($preserveMtime) { |
|
759 | - $this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath)); |
|
760 | - } |
|
761 | - $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, false); |
|
762 | - } else { |
|
763 | - // delete partially written target file |
|
764 | - $this->unlink($targetInternalPath); |
|
765 | - // delete cache entry that was created by fopen |
|
766 | - $this->getCache()->remove($targetInternalPath); |
|
767 | - } |
|
768 | - } |
|
769 | - return (bool)$result; |
|
770 | - |
|
771 | - } |
|
772 | - |
|
773 | - /** |
|
774 | - * get the path to a local version of the file. |
|
775 | - * The local version of the file can be temporary and doesn't have to be persistent across requests |
|
776 | - * |
|
777 | - * @param string $path |
|
778 | - * @return string |
|
779 | - */ |
|
780 | - public function getLocalFile($path) { |
|
781 | - if ($this->encryptionManager->isEnabled()) { |
|
782 | - $cachedFile = $this->getCachedFile($path); |
|
783 | - if (is_string($cachedFile)) { |
|
784 | - return $cachedFile; |
|
785 | - } |
|
786 | - } |
|
787 | - return $this->storage->getLocalFile($path); |
|
788 | - } |
|
789 | - |
|
790 | - /** |
|
791 | - * Returns the wrapped storage's value for isLocal() |
|
792 | - * |
|
793 | - * @return bool wrapped storage's isLocal() value |
|
794 | - */ |
|
795 | - public function isLocal() { |
|
796 | - if ($this->encryptionManager->isEnabled()) { |
|
797 | - return false; |
|
798 | - } |
|
799 | - return $this->storage->isLocal(); |
|
800 | - } |
|
801 | - |
|
802 | - /** |
|
803 | - * see http://php.net/manual/en/function.stat.php |
|
804 | - * only the following keys are required in the result: size and mtime |
|
805 | - * |
|
806 | - * @param string $path |
|
807 | - * @return array |
|
808 | - */ |
|
809 | - public function stat($path) { |
|
810 | - $stat = $this->storage->stat($path); |
|
811 | - $fileSize = $this->filesize($path); |
|
812 | - $stat['size'] = $fileSize; |
|
813 | - $stat[7] = $fileSize; |
|
814 | - return $stat; |
|
815 | - } |
|
816 | - |
|
817 | - /** |
|
818 | - * see http://php.net/manual/en/function.hash.php |
|
819 | - * |
|
820 | - * @param string $type |
|
821 | - * @param string $path |
|
822 | - * @param bool $raw |
|
823 | - * @return string |
|
824 | - */ |
|
825 | - public function hash($type, $path, $raw = false) { |
|
826 | - $fh = $this->fopen($path, 'rb'); |
|
827 | - $ctx = hash_init($type); |
|
828 | - hash_update_stream($ctx, $fh); |
|
829 | - fclose($fh); |
|
830 | - return hash_final($ctx, $raw); |
|
831 | - } |
|
832 | - |
|
833 | - /** |
|
834 | - * return full path, including mount point |
|
835 | - * |
|
836 | - * @param string $path relative to mount point |
|
837 | - * @return string full path including mount point |
|
838 | - */ |
|
839 | - protected function getFullPath($path) { |
|
840 | - return Filesystem::normalizePath($this->mountPoint . '/' . $path); |
|
841 | - } |
|
842 | - |
|
843 | - /** |
|
844 | - * read first block of encrypted file, typically this will contain the |
|
845 | - * encryption header |
|
846 | - * |
|
847 | - * @param string $path |
|
848 | - * @return string |
|
849 | - */ |
|
850 | - protected function readFirstBlock($path) { |
|
851 | - $firstBlock = ''; |
|
852 | - if ($this->storage->file_exists($path)) { |
|
853 | - $handle = $this->storage->fopen($path, 'r'); |
|
854 | - $firstBlock = fread($handle, $this->util->getHeaderSize()); |
|
855 | - fclose($handle); |
|
856 | - } |
|
857 | - return $firstBlock; |
|
858 | - } |
|
859 | - |
|
860 | - /** |
|
861 | - * return header size of given file |
|
862 | - * |
|
863 | - * @param string $path |
|
864 | - * @return int |
|
865 | - */ |
|
866 | - protected function getHeaderSize($path) { |
|
867 | - $headerSize = 0; |
|
868 | - $realFile = $this->util->stripPartialFileExtension($path); |
|
869 | - if ($this->storage->file_exists($realFile)) { |
|
870 | - $path = $realFile; |
|
871 | - } |
|
872 | - $firstBlock = $this->readFirstBlock($path); |
|
873 | - |
|
874 | - if (substr($firstBlock, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) { |
|
875 | - $headerSize = $this->util->getHeaderSize(); |
|
876 | - } |
|
877 | - |
|
878 | - return $headerSize; |
|
879 | - } |
|
880 | - |
|
881 | - /** |
|
882 | - * parse raw header to array |
|
883 | - * |
|
884 | - * @param string $rawHeader |
|
885 | - * @return array |
|
886 | - */ |
|
887 | - protected function parseRawHeader($rawHeader) { |
|
888 | - $result = array(); |
|
889 | - if (substr($rawHeader, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) { |
|
890 | - $header = $rawHeader; |
|
891 | - $endAt = strpos($header, Util::HEADER_END); |
|
892 | - if ($endAt !== false) { |
|
893 | - $header = substr($header, 0, $endAt + strlen(Util::HEADER_END)); |
|
894 | - |
|
895 | - // +1 to not start with an ':' which would result in empty element at the beginning |
|
896 | - $exploded = explode(':', substr($header, strlen(Util::HEADER_START)+1)); |
|
897 | - |
|
898 | - $element = array_shift($exploded); |
|
899 | - while ($element !== Util::HEADER_END) { |
|
900 | - $result[$element] = array_shift($exploded); |
|
901 | - $element = array_shift($exploded); |
|
902 | - } |
|
903 | - } |
|
904 | - } |
|
905 | - |
|
906 | - return $result; |
|
907 | - } |
|
908 | - |
|
909 | - /** |
|
910 | - * read header from file |
|
911 | - * |
|
912 | - * @param string $path |
|
913 | - * @return array |
|
914 | - */ |
|
915 | - protected function getHeader($path) { |
|
916 | - $realFile = $this->util->stripPartialFileExtension($path); |
|
917 | - $exists = $this->storage->file_exists($realFile); |
|
918 | - if ($exists) { |
|
919 | - $path = $realFile; |
|
920 | - } |
|
921 | - |
|
922 | - $firstBlock = $this->readFirstBlock($path); |
|
923 | - $result = $this->parseRawHeader($firstBlock); |
|
924 | - |
|
925 | - // if the header doesn't contain a encryption module we check if it is a |
|
926 | - // legacy file. If true, we add the default encryption module |
|
927 | - if (!isset($result[Util::HEADER_ENCRYPTION_MODULE_KEY])) { |
|
928 | - if (!empty($result)) { |
|
929 | - $result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE'; |
|
930 | - } else if ($exists) { |
|
931 | - // if the header was empty we have to check first if it is a encrypted file at all |
|
932 | - // We would do query to filecache only if we know that entry in filecache exists |
|
933 | - $info = $this->getCache()->get($path); |
|
934 | - if (isset($info['encrypted']) && $info['encrypted'] === true) { |
|
935 | - $result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE'; |
|
936 | - } |
|
937 | - } |
|
938 | - } |
|
939 | - |
|
940 | - return $result; |
|
941 | - } |
|
942 | - |
|
943 | - /** |
|
944 | - * read encryption module needed to read/write the file located at $path |
|
945 | - * |
|
946 | - * @param string $path |
|
947 | - * @return null|\OCP\Encryption\IEncryptionModule |
|
948 | - * @throws ModuleDoesNotExistsException |
|
949 | - * @throws \Exception |
|
950 | - */ |
|
951 | - protected function getEncryptionModule($path) { |
|
952 | - $encryptionModule = null; |
|
953 | - $header = $this->getHeader($path); |
|
954 | - $encryptionModuleId = $this->util->getEncryptionModuleId($header); |
|
955 | - if (!empty($encryptionModuleId)) { |
|
956 | - try { |
|
957 | - $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
958 | - } catch (ModuleDoesNotExistsException $e) { |
|
959 | - $this->logger->critical('Encryption module defined in "' . $path . '" not loaded!'); |
|
960 | - throw $e; |
|
961 | - } |
|
962 | - } |
|
963 | - |
|
964 | - return $encryptionModule; |
|
965 | - } |
|
966 | - |
|
967 | - /** |
|
968 | - * @param string $path |
|
969 | - * @param int $unencryptedSize |
|
970 | - */ |
|
971 | - public function updateUnencryptedSize($path, $unencryptedSize) { |
|
972 | - $this->unencryptedSize[$path] = $unencryptedSize; |
|
973 | - } |
|
974 | - |
|
975 | - /** |
|
976 | - * copy keys to new location |
|
977 | - * |
|
978 | - * @param string $source path relative to data/ |
|
979 | - * @param string $target path relative to data/ |
|
980 | - * @return bool |
|
981 | - */ |
|
982 | - protected function copyKeys($source, $target) { |
|
983 | - if (!$this->util->isExcluded($source)) { |
|
984 | - return $this->keyStorage->copyKeys($source, $target); |
|
985 | - } |
|
986 | - |
|
987 | - return false; |
|
988 | - } |
|
989 | - |
|
990 | - /** |
|
991 | - * check if path points to a files version |
|
992 | - * |
|
993 | - * @param $path |
|
994 | - * @return bool |
|
995 | - */ |
|
996 | - protected function isVersion($path) { |
|
997 | - $normalized = Filesystem::normalizePath($path); |
|
998 | - return substr($normalized, 0, strlen('/files_versions/')) === '/files_versions/'; |
|
999 | - } |
|
1000 | - |
|
1001 | - /** |
|
1002 | - * check if the given storage should be encrypted or not |
|
1003 | - * |
|
1004 | - * @param $path |
|
1005 | - * @return bool |
|
1006 | - */ |
|
1007 | - protected function shouldEncrypt($path) { |
|
1008 | - $fullPath = $this->getFullPath($path); |
|
1009 | - $mountPointConfig = $this->mount->getOption('encrypt', true); |
|
1010 | - if ($mountPointConfig === false) { |
|
1011 | - return false; |
|
1012 | - } |
|
1013 | - |
|
1014 | - try { |
|
1015 | - $encryptionModule = $this->getEncryptionModule($fullPath); |
|
1016 | - } catch (ModuleDoesNotExistsException $e) { |
|
1017 | - return false; |
|
1018 | - } |
|
1019 | - |
|
1020 | - if ($encryptionModule === null) { |
|
1021 | - $encryptionModule = $this->encryptionManager->getEncryptionModule(); |
|
1022 | - } |
|
1023 | - |
|
1024 | - return $encryptionModule->shouldEncrypt($fullPath); |
|
1025 | - |
|
1026 | - } |
|
51 | + use LocalTempFileTrait; |
|
52 | + |
|
53 | + /** @var string */ |
|
54 | + private $mountPoint; |
|
55 | + |
|
56 | + /** @var \OC\Encryption\Util */ |
|
57 | + private $util; |
|
58 | + |
|
59 | + /** @var \OCP\Encryption\IManager */ |
|
60 | + private $encryptionManager; |
|
61 | + |
|
62 | + /** @var \OCP\ILogger */ |
|
63 | + private $logger; |
|
64 | + |
|
65 | + /** @var string */ |
|
66 | + private $uid; |
|
67 | + |
|
68 | + /** @var array */ |
|
69 | + protected $unencryptedSize; |
|
70 | + |
|
71 | + /** @var \OCP\Encryption\IFile */ |
|
72 | + private $fileHelper; |
|
73 | + |
|
74 | + /** @var IMountPoint */ |
|
75 | + private $mount; |
|
76 | + |
|
77 | + /** @var IStorage */ |
|
78 | + private $keyStorage; |
|
79 | + |
|
80 | + /** @var Update */ |
|
81 | + private $update; |
|
82 | + |
|
83 | + /** @var Manager */ |
|
84 | + private $mountManager; |
|
85 | + |
|
86 | + /** @var array remember for which path we execute the repair step to avoid recursions */ |
|
87 | + private $fixUnencryptedSizeOf = array(); |
|
88 | + |
|
89 | + /** @var ArrayCache */ |
|
90 | + private $arrayCache; |
|
91 | + |
|
92 | + /** |
|
93 | + * @param array $parameters |
|
94 | + * @param IManager $encryptionManager |
|
95 | + * @param Util $util |
|
96 | + * @param ILogger $logger |
|
97 | + * @param IFile $fileHelper |
|
98 | + * @param string $uid |
|
99 | + * @param IStorage $keyStorage |
|
100 | + * @param Update $update |
|
101 | + * @param Manager $mountManager |
|
102 | + * @param ArrayCache $arrayCache |
|
103 | + */ |
|
104 | + public function __construct( |
|
105 | + $parameters, |
|
106 | + IManager $encryptionManager = null, |
|
107 | + Util $util = null, |
|
108 | + ILogger $logger = null, |
|
109 | + IFile $fileHelper = null, |
|
110 | + $uid = null, |
|
111 | + IStorage $keyStorage = null, |
|
112 | + Update $update = null, |
|
113 | + Manager $mountManager = null, |
|
114 | + ArrayCache $arrayCache = null |
|
115 | + ) { |
|
116 | + |
|
117 | + $this->mountPoint = $parameters['mountPoint']; |
|
118 | + $this->mount = $parameters['mount']; |
|
119 | + $this->encryptionManager = $encryptionManager; |
|
120 | + $this->util = $util; |
|
121 | + $this->logger = $logger; |
|
122 | + $this->uid = $uid; |
|
123 | + $this->fileHelper = $fileHelper; |
|
124 | + $this->keyStorage = $keyStorage; |
|
125 | + $this->unencryptedSize = array(); |
|
126 | + $this->update = $update; |
|
127 | + $this->mountManager = $mountManager; |
|
128 | + $this->arrayCache = $arrayCache; |
|
129 | + parent::__construct($parameters); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * see http://php.net/manual/en/function.filesize.php |
|
134 | + * The result for filesize when called on a folder is required to be 0 |
|
135 | + * |
|
136 | + * @param string $path |
|
137 | + * @return int |
|
138 | + */ |
|
139 | + public function filesize($path) { |
|
140 | + $fullPath = $this->getFullPath($path); |
|
141 | + |
|
142 | + /** @var CacheEntry $info */ |
|
143 | + $info = $this->getCache()->get($path); |
|
144 | + if (isset($this->unencryptedSize[$fullPath])) { |
|
145 | + $size = $this->unencryptedSize[$fullPath]; |
|
146 | + // update file cache |
|
147 | + if ($info instanceof ICacheEntry) { |
|
148 | + $info = $info->getData(); |
|
149 | + $info['encrypted'] = $info['encryptedVersion']; |
|
150 | + } else { |
|
151 | + if (!is_array($info)) { |
|
152 | + $info = []; |
|
153 | + } |
|
154 | + $info['encrypted'] = true; |
|
155 | + } |
|
156 | + |
|
157 | + $info['size'] = $size; |
|
158 | + $this->getCache()->put($path, $info); |
|
159 | + |
|
160 | + return $size; |
|
161 | + } |
|
162 | + |
|
163 | + if (isset($info['fileid']) && $info['encrypted']) { |
|
164 | + return $this->verifyUnencryptedSize($path, $info['size']); |
|
165 | + } |
|
166 | + |
|
167 | + return $this->storage->filesize($path); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * @param string $path |
|
172 | + * @return array |
|
173 | + */ |
|
174 | + public function getMetaData($path) { |
|
175 | + $data = $this->storage->getMetaData($path); |
|
176 | + if (is_null($data)) { |
|
177 | + return null; |
|
178 | + } |
|
179 | + $fullPath = $this->getFullPath($path); |
|
180 | + $info = $this->getCache()->get($path); |
|
181 | + |
|
182 | + if (isset($this->unencryptedSize[$fullPath])) { |
|
183 | + $data['encrypted'] = true; |
|
184 | + $data['size'] = $this->unencryptedSize[$fullPath]; |
|
185 | + } else { |
|
186 | + if (isset($info['fileid']) && $info['encrypted']) { |
|
187 | + $data['size'] = $this->verifyUnencryptedSize($path, $info['size']); |
|
188 | + $data['encrypted'] = true; |
|
189 | + } |
|
190 | + } |
|
191 | + |
|
192 | + if (isset($info['encryptedVersion']) && $info['encryptedVersion'] > 1) { |
|
193 | + $data['encryptedVersion'] = $info['encryptedVersion']; |
|
194 | + } |
|
195 | + |
|
196 | + return $data; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * see http://php.net/manual/en/function.file_get_contents.php |
|
201 | + * |
|
202 | + * @param string $path |
|
203 | + * @return string |
|
204 | + */ |
|
205 | + public function file_get_contents($path) { |
|
206 | + |
|
207 | + $encryptionModule = $this->getEncryptionModule($path); |
|
208 | + |
|
209 | + if ($encryptionModule) { |
|
210 | + $handle = $this->fopen($path, "r"); |
|
211 | + if (!$handle) { |
|
212 | + return false; |
|
213 | + } |
|
214 | + $data = stream_get_contents($handle); |
|
215 | + fclose($handle); |
|
216 | + return $data; |
|
217 | + } |
|
218 | + return $this->storage->file_get_contents($path); |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * see http://php.net/manual/en/function.file_put_contents.php |
|
223 | + * |
|
224 | + * @param string $path |
|
225 | + * @param string $data |
|
226 | + * @return bool |
|
227 | + */ |
|
228 | + public function file_put_contents($path, $data) { |
|
229 | + // file put content will always be translated to a stream write |
|
230 | + $handle = $this->fopen($path, 'w'); |
|
231 | + if (is_resource($handle)) { |
|
232 | + $written = fwrite($handle, $data); |
|
233 | + fclose($handle); |
|
234 | + return $written; |
|
235 | + } |
|
236 | + |
|
237 | + return false; |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * see http://php.net/manual/en/function.unlink.php |
|
242 | + * |
|
243 | + * @param string $path |
|
244 | + * @return bool |
|
245 | + */ |
|
246 | + public function unlink($path) { |
|
247 | + $fullPath = $this->getFullPath($path); |
|
248 | + if ($this->util->isExcluded($fullPath)) { |
|
249 | + return $this->storage->unlink($path); |
|
250 | + } |
|
251 | + |
|
252 | + $encryptionModule = $this->getEncryptionModule($path); |
|
253 | + if ($encryptionModule) { |
|
254 | + $this->keyStorage->deleteAllFileKeys($this->getFullPath($path)); |
|
255 | + } |
|
256 | + |
|
257 | + return $this->storage->unlink($path); |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * see http://php.net/manual/en/function.rename.php |
|
262 | + * |
|
263 | + * @param string $path1 |
|
264 | + * @param string $path2 |
|
265 | + * @return bool |
|
266 | + */ |
|
267 | + public function rename($path1, $path2) { |
|
268 | + |
|
269 | + $result = $this->storage->rename($path1, $path2); |
|
270 | + |
|
271 | + if ($result && |
|
272 | + // versions always use the keys from the original file, so we can skip |
|
273 | + // this step for versions |
|
274 | + $this->isVersion($path2) === false && |
|
275 | + $this->encryptionManager->isEnabled()) { |
|
276 | + $source = $this->getFullPath($path1); |
|
277 | + if (!$this->util->isExcluded($source)) { |
|
278 | + $target = $this->getFullPath($path2); |
|
279 | + if (isset($this->unencryptedSize[$source])) { |
|
280 | + $this->unencryptedSize[$target] = $this->unencryptedSize[$source]; |
|
281 | + } |
|
282 | + $this->keyStorage->renameKeys($source, $target); |
|
283 | + $module = $this->getEncryptionModule($path2); |
|
284 | + if ($module) { |
|
285 | + $module->update($target, $this->uid, []); |
|
286 | + } |
|
287 | + } |
|
288 | + } |
|
289 | + |
|
290 | + return $result; |
|
291 | + } |
|
292 | + |
|
293 | + /** |
|
294 | + * see http://php.net/manual/en/function.rmdir.php |
|
295 | + * |
|
296 | + * @param string $path |
|
297 | + * @return bool |
|
298 | + */ |
|
299 | + public function rmdir($path) { |
|
300 | + $result = $this->storage->rmdir($path); |
|
301 | + $fullPath = $this->getFullPath($path); |
|
302 | + if ($result && |
|
303 | + $this->util->isExcluded($fullPath) === false && |
|
304 | + $this->encryptionManager->isEnabled() |
|
305 | + ) { |
|
306 | + $this->keyStorage->deleteAllFileKeys($fullPath); |
|
307 | + } |
|
308 | + |
|
309 | + return $result; |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * check if a file can be read |
|
314 | + * |
|
315 | + * @param string $path |
|
316 | + * @return bool |
|
317 | + */ |
|
318 | + public function isReadable($path) { |
|
319 | + |
|
320 | + $isReadable = true; |
|
321 | + |
|
322 | + $metaData = $this->getMetaData($path); |
|
323 | + if ( |
|
324 | + !$this->is_dir($path) && |
|
325 | + isset($metaData['encrypted']) && |
|
326 | + $metaData['encrypted'] === true |
|
327 | + ) { |
|
328 | + $fullPath = $this->getFullPath($path); |
|
329 | + $module = $this->getEncryptionModule($path); |
|
330 | + $isReadable = $module->isReadable($fullPath, $this->uid); |
|
331 | + } |
|
332 | + |
|
333 | + return $this->storage->isReadable($path) && $isReadable; |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * see http://php.net/manual/en/function.copy.php |
|
338 | + * |
|
339 | + * @param string $path1 |
|
340 | + * @param string $path2 |
|
341 | + * @return bool |
|
342 | + */ |
|
343 | + public function copy($path1, $path2) { |
|
344 | + |
|
345 | + $source = $this->getFullPath($path1); |
|
346 | + |
|
347 | + if ($this->util->isExcluded($source)) { |
|
348 | + return $this->storage->copy($path1, $path2); |
|
349 | + } |
|
350 | + |
|
351 | + // need to stream copy file by file in case we copy between a encrypted |
|
352 | + // and a unencrypted storage |
|
353 | + $this->unlink($path2); |
|
354 | + return $this->copyFromStorage($this, $path1, $path2); |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * see http://php.net/manual/en/function.fopen.php |
|
359 | + * |
|
360 | + * @param string $path |
|
361 | + * @param string $mode |
|
362 | + * @return resource|bool |
|
363 | + * @throws GenericEncryptionException |
|
364 | + * @throws ModuleDoesNotExistsException |
|
365 | + */ |
|
366 | + public function fopen($path, $mode) { |
|
367 | + |
|
368 | + // check if the file is stored in the array cache, this means that we |
|
369 | + // copy a file over to the versions folder, in this case we don't want to |
|
370 | + // decrypt it |
|
371 | + if ($this->arrayCache->hasKey('encryption_copy_version_' . $path)) { |
|
372 | + $this->arrayCache->remove('encryption_copy_version_' . $path); |
|
373 | + return $this->storage->fopen($path, $mode); |
|
374 | + } |
|
375 | + |
|
376 | + $encryptionEnabled = $this->encryptionManager->isEnabled(); |
|
377 | + $shouldEncrypt = false; |
|
378 | + $encryptionModule = null; |
|
379 | + $header = $this->getHeader($path); |
|
380 | + $signed = isset($header['signed']) && $header['signed'] === 'true'; |
|
381 | + $fullPath = $this->getFullPath($path); |
|
382 | + $encryptionModuleId = $this->util->getEncryptionModuleId($header); |
|
383 | + |
|
384 | + if ($this->util->isExcluded($fullPath) === false) { |
|
385 | + |
|
386 | + $size = $unencryptedSize = 0; |
|
387 | + $realFile = $this->util->stripPartialFileExtension($path); |
|
388 | + $targetExists = $this->file_exists($realFile) || $this->file_exists($path); |
|
389 | + $targetIsEncrypted = false; |
|
390 | + if ($targetExists) { |
|
391 | + // in case the file exists we require the explicit module as |
|
392 | + // specified in the file header - otherwise we need to fail hard to |
|
393 | + // prevent data loss on client side |
|
394 | + if (!empty($encryptionModuleId)) { |
|
395 | + $targetIsEncrypted = true; |
|
396 | + $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
397 | + } |
|
398 | + |
|
399 | + if ($this->file_exists($path)) { |
|
400 | + $size = $this->storage->filesize($path); |
|
401 | + $unencryptedSize = $this->filesize($path); |
|
402 | + } else { |
|
403 | + $size = $unencryptedSize = 0; |
|
404 | + } |
|
405 | + } |
|
406 | + |
|
407 | + try { |
|
408 | + |
|
409 | + if ( |
|
410 | + $mode === 'w' |
|
411 | + || $mode === 'w+' |
|
412 | + || $mode === 'wb' |
|
413 | + || $mode === 'wb+' |
|
414 | + ) { |
|
415 | + // don't overwrite encrypted files if encryption is not enabled |
|
416 | + if ($targetIsEncrypted && $encryptionEnabled === false) { |
|
417 | + throw new GenericEncryptionException('Tried to access encrypted file but encryption is not enabled'); |
|
418 | + } |
|
419 | + if ($encryptionEnabled) { |
|
420 | + // if $encryptionModuleId is empty, the default module will be used |
|
421 | + $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
422 | + $shouldEncrypt = $encryptionModule->shouldEncrypt($fullPath); |
|
423 | + $signed = true; |
|
424 | + } |
|
425 | + } else { |
|
426 | + $info = $this->getCache()->get($path); |
|
427 | + // only get encryption module if we found one in the header |
|
428 | + // or if file should be encrypted according to the file cache |
|
429 | + if (!empty($encryptionModuleId)) { |
|
430 | + $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
431 | + $shouldEncrypt = true; |
|
432 | + } else if (empty($encryptionModuleId) && $info['encrypted'] === true) { |
|
433 | + // we come from a old installation. No header and/or no module defined |
|
434 | + // but the file is encrypted. In this case we need to use the |
|
435 | + // OC_DEFAULT_MODULE to read the file |
|
436 | + $encryptionModule = $this->encryptionManager->getEncryptionModule('OC_DEFAULT_MODULE'); |
|
437 | + $shouldEncrypt = true; |
|
438 | + $targetIsEncrypted = true; |
|
439 | + } |
|
440 | + } |
|
441 | + } catch (ModuleDoesNotExistsException $e) { |
|
442 | + $this->logger->logException($e, [ |
|
443 | + 'message' => 'Encryption module "' . $encryptionModuleId . '" not found, file will be stored unencrypted', |
|
444 | + 'level' => ILogger::WARN, |
|
445 | + 'app' => 'core', |
|
446 | + ]); |
|
447 | + } |
|
448 | + |
|
449 | + // encryption disabled on write of new file and write to existing unencrypted file -> don't encrypt |
|
450 | + if (!$encryptionEnabled || !$this->shouldEncrypt($path)) { |
|
451 | + if (!$targetExists || !$targetIsEncrypted) { |
|
452 | + $shouldEncrypt = false; |
|
453 | + } |
|
454 | + } |
|
455 | + |
|
456 | + if ($shouldEncrypt === true && $encryptionModule !== null) { |
|
457 | + $headerSize = $this->getHeaderSize($path); |
|
458 | + $source = $this->storage->fopen($path, $mode); |
|
459 | + if (!is_resource($source)) { |
|
460 | + return false; |
|
461 | + } |
|
462 | + $handle = \OC\Files\Stream\Encryption::wrap($source, $path, $fullPath, $header, |
|
463 | + $this->uid, $encryptionModule, $this->storage, $this, $this->util, $this->fileHelper, $mode, |
|
464 | + $size, $unencryptedSize, $headerSize, $signed); |
|
465 | + return $handle; |
|
466 | + } |
|
467 | + |
|
468 | + } |
|
469 | + |
|
470 | + return $this->storage->fopen($path, $mode); |
|
471 | + } |
|
472 | + |
|
473 | + |
|
474 | + /** |
|
475 | + * perform some plausibility checks if the the unencrypted size is correct. |
|
476 | + * If not, we calculate the correct unencrypted size and return it |
|
477 | + * |
|
478 | + * @param string $path internal path relative to the storage root |
|
479 | + * @param int $unencryptedSize size of the unencrypted file |
|
480 | + * |
|
481 | + * @return int unencrypted size |
|
482 | + */ |
|
483 | + protected function verifyUnencryptedSize($path, $unencryptedSize) { |
|
484 | + |
|
485 | + $size = $this->storage->filesize($path); |
|
486 | + $result = $unencryptedSize; |
|
487 | + |
|
488 | + if ($unencryptedSize < 0 || |
|
489 | + ($size > 0 && $unencryptedSize === $size) |
|
490 | + ) { |
|
491 | + // check if we already calculate the unencrypted size for the |
|
492 | + // given path to avoid recursions |
|
493 | + if (isset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]) === false) { |
|
494 | + $this->fixUnencryptedSizeOf[$this->getFullPath($path)] = true; |
|
495 | + try { |
|
496 | + $result = $this->fixUnencryptedSize($path, $size, $unencryptedSize); |
|
497 | + } catch (\Exception $e) { |
|
498 | + $this->logger->error('Couldn\'t re-calculate unencrypted size for '. $path); |
|
499 | + $this->logger->logException($e); |
|
500 | + } |
|
501 | + unset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]); |
|
502 | + } |
|
503 | + } |
|
504 | + |
|
505 | + return $result; |
|
506 | + } |
|
507 | + |
|
508 | + /** |
|
509 | + * calculate the unencrypted size |
|
510 | + * |
|
511 | + * @param string $path internal path relative to the storage root |
|
512 | + * @param int $size size of the physical file |
|
513 | + * @param int $unencryptedSize size of the unencrypted file |
|
514 | + * |
|
515 | + * @return int calculated unencrypted size |
|
516 | + */ |
|
517 | + protected function fixUnencryptedSize($path, $size, $unencryptedSize) { |
|
518 | + |
|
519 | + $headerSize = $this->getHeaderSize($path); |
|
520 | + $header = $this->getHeader($path); |
|
521 | + $encryptionModule = $this->getEncryptionModule($path); |
|
522 | + |
|
523 | + $stream = $this->storage->fopen($path, 'r'); |
|
524 | + |
|
525 | + // if we couldn't open the file we return the old unencrypted size |
|
526 | + if (!is_resource($stream)) { |
|
527 | + $this->logger->error('Could not open ' . $path . '. Recalculation of unencrypted size aborted.'); |
|
528 | + return $unencryptedSize; |
|
529 | + } |
|
530 | + |
|
531 | + $newUnencryptedSize = 0; |
|
532 | + $size -= $headerSize; |
|
533 | + $blockSize = $this->util->getBlockSize(); |
|
534 | + |
|
535 | + // if a header exists we skip it |
|
536 | + if ($headerSize > 0) { |
|
537 | + fread($stream, $headerSize); |
|
538 | + } |
|
539 | + |
|
540 | + // fast path, else the calculation for $lastChunkNr is bogus |
|
541 | + if ($size === 0) { |
|
542 | + return 0; |
|
543 | + } |
|
544 | + |
|
545 | + $signed = isset($header['signed']) && $header['signed'] === 'true'; |
|
546 | + $unencryptedBlockSize = $encryptionModule->getUnencryptedBlockSize($signed); |
|
547 | + |
|
548 | + // calculate last chunk nr |
|
549 | + // next highest is end of chunks, one subtracted is last one |
|
550 | + // we have to read the last chunk, we can't just calculate it (because of padding etc) |
|
551 | + |
|
552 | + $lastChunkNr = ceil($size/ $blockSize)-1; |
|
553 | + // calculate last chunk position |
|
554 | + $lastChunkPos = ($lastChunkNr * $blockSize); |
|
555 | + // try to fseek to the last chunk, if it fails we have to read the whole file |
|
556 | + if (@fseek($stream, $lastChunkPos, SEEK_CUR) === 0) { |
|
557 | + $newUnencryptedSize += $lastChunkNr * $unencryptedBlockSize; |
|
558 | + } |
|
559 | + |
|
560 | + $lastChunkContentEncrypted=''; |
|
561 | + $count = $blockSize; |
|
562 | + |
|
563 | + while ($count > 0) { |
|
564 | + $data=fread($stream, $blockSize); |
|
565 | + $count=strlen($data); |
|
566 | + $lastChunkContentEncrypted .= $data; |
|
567 | + if(strlen($lastChunkContentEncrypted) > $blockSize) { |
|
568 | + $newUnencryptedSize += $unencryptedBlockSize; |
|
569 | + $lastChunkContentEncrypted=substr($lastChunkContentEncrypted, $blockSize); |
|
570 | + } |
|
571 | + } |
|
572 | + |
|
573 | + fclose($stream); |
|
574 | + |
|
575 | + // we have to decrypt the last chunk to get it actual size |
|
576 | + $encryptionModule->begin($this->getFullPath($path), $this->uid, 'r', $header, []); |
|
577 | + $decryptedLastChunk = $encryptionModule->decrypt($lastChunkContentEncrypted, $lastChunkNr . 'end'); |
|
578 | + $decryptedLastChunk .= $encryptionModule->end($this->getFullPath($path), $lastChunkNr . 'end'); |
|
579 | + |
|
580 | + // calc the real file size with the size of the last chunk |
|
581 | + $newUnencryptedSize += strlen($decryptedLastChunk); |
|
582 | + |
|
583 | + $this->updateUnencryptedSize($this->getFullPath($path), $newUnencryptedSize); |
|
584 | + |
|
585 | + // write to cache if applicable |
|
586 | + $cache = $this->storage->getCache(); |
|
587 | + if ($cache) { |
|
588 | + $entry = $cache->get($path); |
|
589 | + $cache->update($entry['fileid'], ['size' => $newUnencryptedSize]); |
|
590 | + } |
|
591 | + |
|
592 | + return $newUnencryptedSize; |
|
593 | + } |
|
594 | + |
|
595 | + /** |
|
596 | + * @param Storage\IStorage $sourceStorage |
|
597 | + * @param string $sourceInternalPath |
|
598 | + * @param string $targetInternalPath |
|
599 | + * @param bool $preserveMtime |
|
600 | + * @return bool |
|
601 | + */ |
|
602 | + public function moveFromStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = true) { |
|
603 | + if ($sourceStorage === $this) { |
|
604 | + return $this->rename($sourceInternalPath, $targetInternalPath); |
|
605 | + } |
|
606 | + |
|
607 | + // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed: |
|
608 | + // - call $this->storage->moveFromStorage() instead of $this->copyBetweenStorage |
|
609 | + // - copy the file cache update from $this->copyBetweenStorage to this method |
|
610 | + // - copy the copyKeys() call from $this->copyBetweenStorage to this method |
|
611 | + // - remove $this->copyBetweenStorage |
|
612 | + |
|
613 | + if (!$sourceStorage->isDeletable($sourceInternalPath)) { |
|
614 | + return false; |
|
615 | + } |
|
616 | + |
|
617 | + $result = $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, true); |
|
618 | + if ($result) { |
|
619 | + if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
620 | + $result &= $sourceStorage->rmdir($sourceInternalPath); |
|
621 | + } else { |
|
622 | + $result &= $sourceStorage->unlink($sourceInternalPath); |
|
623 | + } |
|
624 | + } |
|
625 | + return $result; |
|
626 | + } |
|
627 | + |
|
628 | + |
|
629 | + /** |
|
630 | + * @param Storage\IStorage $sourceStorage |
|
631 | + * @param string $sourceInternalPath |
|
632 | + * @param string $targetInternalPath |
|
633 | + * @param bool $preserveMtime |
|
634 | + * @param bool $isRename |
|
635 | + * @return bool |
|
636 | + */ |
|
637 | + public function copyFromStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false, $isRename = false) { |
|
638 | + |
|
639 | + // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed: |
|
640 | + // - call $this->storage->copyFromStorage() instead of $this->copyBetweenStorage |
|
641 | + // - copy the file cache update from $this->copyBetweenStorage to this method |
|
642 | + // - copy the copyKeys() call from $this->copyBetweenStorage to this method |
|
643 | + // - remove $this->copyBetweenStorage |
|
644 | + |
|
645 | + return $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename); |
|
646 | + } |
|
647 | + |
|
648 | + /** |
|
649 | + * Update the encrypted cache version in the database |
|
650 | + * |
|
651 | + * @param Storage\IStorage $sourceStorage |
|
652 | + * @param string $sourceInternalPath |
|
653 | + * @param string $targetInternalPath |
|
654 | + * @param bool $isRename |
|
655 | + * @param bool $keepEncryptionVersion |
|
656 | + */ |
|
657 | + private function updateEncryptedVersion(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, $keepEncryptionVersion) { |
|
658 | + $isEncrypted = $this->encryptionManager->isEnabled() && $this->shouldEncrypt($targetInternalPath); |
|
659 | + $cacheInformation = [ |
|
660 | + 'encrypted' => $isEncrypted, |
|
661 | + ]; |
|
662 | + if($isEncrypted) { |
|
663 | + $encryptedVersion = $sourceStorage->getCache()->get($sourceInternalPath)['encryptedVersion']; |
|
664 | + |
|
665 | + // In case of a move operation from an unencrypted to an encrypted |
|
666 | + // storage the old encrypted version would stay with "0" while the |
|
667 | + // correct value would be "1". Thus we manually set the value to "1" |
|
668 | + // for those cases. |
|
669 | + // See also https://github.com/owncloud/core/issues/23078 |
|
670 | + if($encryptedVersion === 0 || !$keepEncryptionVersion) { |
|
671 | + $encryptedVersion = 1; |
|
672 | + } |
|
673 | + |
|
674 | + $cacheInformation['encryptedVersion'] = $encryptedVersion; |
|
675 | + } |
|
676 | + |
|
677 | + // in case of a rename we need to manipulate the source cache because |
|
678 | + // this information will be kept for the new target |
|
679 | + if ($isRename) { |
|
680 | + $sourceStorage->getCache()->put($sourceInternalPath, $cacheInformation); |
|
681 | + } else { |
|
682 | + $this->getCache()->put($targetInternalPath, $cacheInformation); |
|
683 | + } |
|
684 | + } |
|
685 | + |
|
686 | + /** |
|
687 | + * copy file between two storages |
|
688 | + * |
|
689 | + * @param Storage\IStorage $sourceStorage |
|
690 | + * @param string $sourceInternalPath |
|
691 | + * @param string $targetInternalPath |
|
692 | + * @param bool $preserveMtime |
|
693 | + * @param bool $isRename |
|
694 | + * @return bool |
|
695 | + * @throws \Exception |
|
696 | + */ |
|
697 | + private function copyBetweenStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename) { |
|
698 | + |
|
699 | + // for versions we have nothing to do, because versions should always use the |
|
700 | + // key from the original file. Just create a 1:1 copy and done |
|
701 | + if ($this->isVersion($targetInternalPath) || |
|
702 | + $this->isVersion($sourceInternalPath)) { |
|
703 | + // remember that we try to create a version so that we can detect it during |
|
704 | + // fopen($sourceInternalPath) and by-pass the encryption in order to |
|
705 | + // create a 1:1 copy of the file |
|
706 | + $this->arrayCache->set('encryption_copy_version_' . $sourceInternalPath, true); |
|
707 | + $result = $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
708 | + $this->arrayCache->remove('encryption_copy_version_' . $sourceInternalPath); |
|
709 | + if ($result) { |
|
710 | + $info = $this->getCache('', $sourceStorage)->get($sourceInternalPath); |
|
711 | + // make sure that we update the unencrypted size for the version |
|
712 | + if (isset($info['encrypted']) && $info['encrypted'] === true) { |
|
713 | + $this->updateUnencryptedSize( |
|
714 | + $this->getFullPath($targetInternalPath), |
|
715 | + $info['size'] |
|
716 | + ); |
|
717 | + } |
|
718 | + $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, true); |
|
719 | + } |
|
720 | + return $result; |
|
721 | + } |
|
722 | + |
|
723 | + // first copy the keys that we reuse the existing file key on the target location |
|
724 | + // and don't create a new one which would break versions for example. |
|
725 | + $mount = $this->mountManager->findByStorageId($sourceStorage->getId()); |
|
726 | + if (count($mount) === 1) { |
|
727 | + $mountPoint = $mount[0]->getMountPoint(); |
|
728 | + $source = $mountPoint . '/' . $sourceInternalPath; |
|
729 | + $target = $this->getFullPath($targetInternalPath); |
|
730 | + $this->copyKeys($source, $target); |
|
731 | + } else { |
|
732 | + $this->logger->error('Could not find mount point, can\'t keep encryption keys'); |
|
733 | + } |
|
734 | + |
|
735 | + if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
736 | + $dh = $sourceStorage->opendir($sourceInternalPath); |
|
737 | + $result = $this->mkdir($targetInternalPath); |
|
738 | + if (is_resource($dh)) { |
|
739 | + while ($result and ($file = readdir($dh)) !== false) { |
|
740 | + if (!Filesystem::isIgnoredDir($file)) { |
|
741 | + $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, false, $isRename); |
|
742 | + } |
|
743 | + } |
|
744 | + } |
|
745 | + } else { |
|
746 | + try { |
|
747 | + $source = $sourceStorage->fopen($sourceInternalPath, 'r'); |
|
748 | + $target = $this->fopen($targetInternalPath, 'w'); |
|
749 | + list(, $result) = \OC_Helper::streamCopy($source, $target); |
|
750 | + fclose($source); |
|
751 | + fclose($target); |
|
752 | + } catch (\Exception $e) { |
|
753 | + fclose($source); |
|
754 | + fclose($target); |
|
755 | + throw $e; |
|
756 | + } |
|
757 | + if($result) { |
|
758 | + if ($preserveMtime) { |
|
759 | + $this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath)); |
|
760 | + } |
|
761 | + $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, false); |
|
762 | + } else { |
|
763 | + // delete partially written target file |
|
764 | + $this->unlink($targetInternalPath); |
|
765 | + // delete cache entry that was created by fopen |
|
766 | + $this->getCache()->remove($targetInternalPath); |
|
767 | + } |
|
768 | + } |
|
769 | + return (bool)$result; |
|
770 | + |
|
771 | + } |
|
772 | + |
|
773 | + /** |
|
774 | + * get the path to a local version of the file. |
|
775 | + * The local version of the file can be temporary and doesn't have to be persistent across requests |
|
776 | + * |
|
777 | + * @param string $path |
|
778 | + * @return string |
|
779 | + */ |
|
780 | + public function getLocalFile($path) { |
|
781 | + if ($this->encryptionManager->isEnabled()) { |
|
782 | + $cachedFile = $this->getCachedFile($path); |
|
783 | + if (is_string($cachedFile)) { |
|
784 | + return $cachedFile; |
|
785 | + } |
|
786 | + } |
|
787 | + return $this->storage->getLocalFile($path); |
|
788 | + } |
|
789 | + |
|
790 | + /** |
|
791 | + * Returns the wrapped storage's value for isLocal() |
|
792 | + * |
|
793 | + * @return bool wrapped storage's isLocal() value |
|
794 | + */ |
|
795 | + public function isLocal() { |
|
796 | + if ($this->encryptionManager->isEnabled()) { |
|
797 | + return false; |
|
798 | + } |
|
799 | + return $this->storage->isLocal(); |
|
800 | + } |
|
801 | + |
|
802 | + /** |
|
803 | + * see http://php.net/manual/en/function.stat.php |
|
804 | + * only the following keys are required in the result: size and mtime |
|
805 | + * |
|
806 | + * @param string $path |
|
807 | + * @return array |
|
808 | + */ |
|
809 | + public function stat($path) { |
|
810 | + $stat = $this->storage->stat($path); |
|
811 | + $fileSize = $this->filesize($path); |
|
812 | + $stat['size'] = $fileSize; |
|
813 | + $stat[7] = $fileSize; |
|
814 | + return $stat; |
|
815 | + } |
|
816 | + |
|
817 | + /** |
|
818 | + * see http://php.net/manual/en/function.hash.php |
|
819 | + * |
|
820 | + * @param string $type |
|
821 | + * @param string $path |
|
822 | + * @param bool $raw |
|
823 | + * @return string |
|
824 | + */ |
|
825 | + public function hash($type, $path, $raw = false) { |
|
826 | + $fh = $this->fopen($path, 'rb'); |
|
827 | + $ctx = hash_init($type); |
|
828 | + hash_update_stream($ctx, $fh); |
|
829 | + fclose($fh); |
|
830 | + return hash_final($ctx, $raw); |
|
831 | + } |
|
832 | + |
|
833 | + /** |
|
834 | + * return full path, including mount point |
|
835 | + * |
|
836 | + * @param string $path relative to mount point |
|
837 | + * @return string full path including mount point |
|
838 | + */ |
|
839 | + protected function getFullPath($path) { |
|
840 | + return Filesystem::normalizePath($this->mountPoint . '/' . $path); |
|
841 | + } |
|
842 | + |
|
843 | + /** |
|
844 | + * read first block of encrypted file, typically this will contain the |
|
845 | + * encryption header |
|
846 | + * |
|
847 | + * @param string $path |
|
848 | + * @return string |
|
849 | + */ |
|
850 | + protected function readFirstBlock($path) { |
|
851 | + $firstBlock = ''; |
|
852 | + if ($this->storage->file_exists($path)) { |
|
853 | + $handle = $this->storage->fopen($path, 'r'); |
|
854 | + $firstBlock = fread($handle, $this->util->getHeaderSize()); |
|
855 | + fclose($handle); |
|
856 | + } |
|
857 | + return $firstBlock; |
|
858 | + } |
|
859 | + |
|
860 | + /** |
|
861 | + * return header size of given file |
|
862 | + * |
|
863 | + * @param string $path |
|
864 | + * @return int |
|
865 | + */ |
|
866 | + protected function getHeaderSize($path) { |
|
867 | + $headerSize = 0; |
|
868 | + $realFile = $this->util->stripPartialFileExtension($path); |
|
869 | + if ($this->storage->file_exists($realFile)) { |
|
870 | + $path = $realFile; |
|
871 | + } |
|
872 | + $firstBlock = $this->readFirstBlock($path); |
|
873 | + |
|
874 | + if (substr($firstBlock, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) { |
|
875 | + $headerSize = $this->util->getHeaderSize(); |
|
876 | + } |
|
877 | + |
|
878 | + return $headerSize; |
|
879 | + } |
|
880 | + |
|
881 | + /** |
|
882 | + * parse raw header to array |
|
883 | + * |
|
884 | + * @param string $rawHeader |
|
885 | + * @return array |
|
886 | + */ |
|
887 | + protected function parseRawHeader($rawHeader) { |
|
888 | + $result = array(); |
|
889 | + if (substr($rawHeader, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) { |
|
890 | + $header = $rawHeader; |
|
891 | + $endAt = strpos($header, Util::HEADER_END); |
|
892 | + if ($endAt !== false) { |
|
893 | + $header = substr($header, 0, $endAt + strlen(Util::HEADER_END)); |
|
894 | + |
|
895 | + // +1 to not start with an ':' which would result in empty element at the beginning |
|
896 | + $exploded = explode(':', substr($header, strlen(Util::HEADER_START)+1)); |
|
897 | + |
|
898 | + $element = array_shift($exploded); |
|
899 | + while ($element !== Util::HEADER_END) { |
|
900 | + $result[$element] = array_shift($exploded); |
|
901 | + $element = array_shift($exploded); |
|
902 | + } |
|
903 | + } |
|
904 | + } |
|
905 | + |
|
906 | + return $result; |
|
907 | + } |
|
908 | + |
|
909 | + /** |
|
910 | + * read header from file |
|
911 | + * |
|
912 | + * @param string $path |
|
913 | + * @return array |
|
914 | + */ |
|
915 | + protected function getHeader($path) { |
|
916 | + $realFile = $this->util->stripPartialFileExtension($path); |
|
917 | + $exists = $this->storage->file_exists($realFile); |
|
918 | + if ($exists) { |
|
919 | + $path = $realFile; |
|
920 | + } |
|
921 | + |
|
922 | + $firstBlock = $this->readFirstBlock($path); |
|
923 | + $result = $this->parseRawHeader($firstBlock); |
|
924 | + |
|
925 | + // if the header doesn't contain a encryption module we check if it is a |
|
926 | + // legacy file. If true, we add the default encryption module |
|
927 | + if (!isset($result[Util::HEADER_ENCRYPTION_MODULE_KEY])) { |
|
928 | + if (!empty($result)) { |
|
929 | + $result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE'; |
|
930 | + } else if ($exists) { |
|
931 | + // if the header was empty we have to check first if it is a encrypted file at all |
|
932 | + // We would do query to filecache only if we know that entry in filecache exists |
|
933 | + $info = $this->getCache()->get($path); |
|
934 | + if (isset($info['encrypted']) && $info['encrypted'] === true) { |
|
935 | + $result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE'; |
|
936 | + } |
|
937 | + } |
|
938 | + } |
|
939 | + |
|
940 | + return $result; |
|
941 | + } |
|
942 | + |
|
943 | + /** |
|
944 | + * read encryption module needed to read/write the file located at $path |
|
945 | + * |
|
946 | + * @param string $path |
|
947 | + * @return null|\OCP\Encryption\IEncryptionModule |
|
948 | + * @throws ModuleDoesNotExistsException |
|
949 | + * @throws \Exception |
|
950 | + */ |
|
951 | + protected function getEncryptionModule($path) { |
|
952 | + $encryptionModule = null; |
|
953 | + $header = $this->getHeader($path); |
|
954 | + $encryptionModuleId = $this->util->getEncryptionModuleId($header); |
|
955 | + if (!empty($encryptionModuleId)) { |
|
956 | + try { |
|
957 | + $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
958 | + } catch (ModuleDoesNotExistsException $e) { |
|
959 | + $this->logger->critical('Encryption module defined in "' . $path . '" not loaded!'); |
|
960 | + throw $e; |
|
961 | + } |
|
962 | + } |
|
963 | + |
|
964 | + return $encryptionModule; |
|
965 | + } |
|
966 | + |
|
967 | + /** |
|
968 | + * @param string $path |
|
969 | + * @param int $unencryptedSize |
|
970 | + */ |
|
971 | + public function updateUnencryptedSize($path, $unencryptedSize) { |
|
972 | + $this->unencryptedSize[$path] = $unencryptedSize; |
|
973 | + } |
|
974 | + |
|
975 | + /** |
|
976 | + * copy keys to new location |
|
977 | + * |
|
978 | + * @param string $source path relative to data/ |
|
979 | + * @param string $target path relative to data/ |
|
980 | + * @return bool |
|
981 | + */ |
|
982 | + protected function copyKeys($source, $target) { |
|
983 | + if (!$this->util->isExcluded($source)) { |
|
984 | + return $this->keyStorage->copyKeys($source, $target); |
|
985 | + } |
|
986 | + |
|
987 | + return false; |
|
988 | + } |
|
989 | + |
|
990 | + /** |
|
991 | + * check if path points to a files version |
|
992 | + * |
|
993 | + * @param $path |
|
994 | + * @return bool |
|
995 | + */ |
|
996 | + protected function isVersion($path) { |
|
997 | + $normalized = Filesystem::normalizePath($path); |
|
998 | + return substr($normalized, 0, strlen('/files_versions/')) === '/files_versions/'; |
|
999 | + } |
|
1000 | + |
|
1001 | + /** |
|
1002 | + * check if the given storage should be encrypted or not |
|
1003 | + * |
|
1004 | + * @param $path |
|
1005 | + * @return bool |
|
1006 | + */ |
|
1007 | + protected function shouldEncrypt($path) { |
|
1008 | + $fullPath = $this->getFullPath($path); |
|
1009 | + $mountPointConfig = $this->mount->getOption('encrypt', true); |
|
1010 | + if ($mountPointConfig === false) { |
|
1011 | + return false; |
|
1012 | + } |
|
1013 | + |
|
1014 | + try { |
|
1015 | + $encryptionModule = $this->getEncryptionModule($fullPath); |
|
1016 | + } catch (ModuleDoesNotExistsException $e) { |
|
1017 | + return false; |
|
1018 | + } |
|
1019 | + |
|
1020 | + if ($encryptionModule === null) { |
|
1021 | + $encryptionModule = $this->encryptionManager->getEncryptionModule(); |
|
1022 | + } |
|
1023 | + |
|
1024 | + return $encryptionModule->shouldEncrypt($fullPath); |
|
1025 | + |
|
1026 | + } |
|
1027 | 1027 | |
1028 | 1028 | } |
@@ -48,418 +48,418 @@ |
||
48 | 48 | * for local filestore, we only have to map the paths |
49 | 49 | */ |
50 | 50 | class Local extends \OC\Files\Storage\Common { |
51 | - protected $datadir; |
|
52 | - |
|
53 | - protected $dataDirLength; |
|
54 | - |
|
55 | - protected $allowSymlinks = false; |
|
56 | - |
|
57 | - protected $realDataDir; |
|
58 | - |
|
59 | - public function __construct($arguments) { |
|
60 | - if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) { |
|
61 | - throw new \InvalidArgumentException('No data directory set for local storage'); |
|
62 | - } |
|
63 | - $this->datadir = str_replace('//', '/', $arguments['datadir']); |
|
64 | - // some crazy code uses a local storage on root... |
|
65 | - if ($this->datadir === '/') { |
|
66 | - $this->realDataDir = $this->datadir; |
|
67 | - } else { |
|
68 | - $realPath = realpath($this->datadir) ?: $this->datadir; |
|
69 | - $this->realDataDir = rtrim($realPath, '/') . '/'; |
|
70 | - } |
|
71 | - if (substr($this->datadir, -1) !== '/') { |
|
72 | - $this->datadir .= '/'; |
|
73 | - } |
|
74 | - $this->dataDirLength = strlen($this->realDataDir); |
|
75 | - } |
|
76 | - |
|
77 | - public function __destruct() { |
|
78 | - } |
|
79 | - |
|
80 | - public function getId() { |
|
81 | - return 'local::' . $this->datadir; |
|
82 | - } |
|
83 | - |
|
84 | - public function mkdir($path) { |
|
85 | - return @mkdir($this->getSourcePath($path), 0777, true); |
|
86 | - } |
|
87 | - |
|
88 | - public function rmdir($path) { |
|
89 | - if (!$this->isDeletable($path)) { |
|
90 | - return false; |
|
91 | - } |
|
92 | - try { |
|
93 | - $it = new \RecursiveIteratorIterator( |
|
94 | - new \RecursiveDirectoryIterator($this->getSourcePath($path)), |
|
95 | - \RecursiveIteratorIterator::CHILD_FIRST |
|
96 | - ); |
|
97 | - /** |
|
98 | - * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach |
|
99 | - * This bug is fixed in PHP 5.5.9 or before |
|
100 | - * See #8376 |
|
101 | - */ |
|
102 | - $it->rewind(); |
|
103 | - while ($it->valid()) { |
|
104 | - /** |
|
105 | - * @var \SplFileInfo $file |
|
106 | - */ |
|
107 | - $file = $it->current(); |
|
108 | - if (in_array($file->getBasename(), array('.', '..'))) { |
|
109 | - $it->next(); |
|
110 | - continue; |
|
111 | - } elseif ($file->isDir()) { |
|
112 | - rmdir($file->getPathname()); |
|
113 | - } elseif ($file->isFile() || $file->isLink()) { |
|
114 | - unlink($file->getPathname()); |
|
115 | - } |
|
116 | - $it->next(); |
|
117 | - } |
|
118 | - return rmdir($this->getSourcePath($path)); |
|
119 | - } catch (\UnexpectedValueException $e) { |
|
120 | - return false; |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - public function opendir($path) { |
|
125 | - return opendir($this->getSourcePath($path)); |
|
126 | - } |
|
127 | - |
|
128 | - public function is_dir($path) { |
|
129 | - if (substr($path, -1) == '/') { |
|
130 | - $path = substr($path, 0, -1); |
|
131 | - } |
|
132 | - return is_dir($this->getSourcePath($path)); |
|
133 | - } |
|
134 | - |
|
135 | - public function is_file($path) { |
|
136 | - return is_file($this->getSourcePath($path)); |
|
137 | - } |
|
138 | - |
|
139 | - public function stat($path) { |
|
140 | - clearstatcache(); |
|
141 | - $fullPath = $this->getSourcePath($path); |
|
142 | - $statResult = stat($fullPath); |
|
143 | - if (PHP_INT_SIZE === 4 && !$this->is_dir($path)) { |
|
144 | - $filesize = $this->filesize($path); |
|
145 | - $statResult['size'] = $filesize; |
|
146 | - $statResult[7] = $filesize; |
|
147 | - } |
|
148 | - return $statResult; |
|
149 | - } |
|
150 | - |
|
151 | - public function filetype($path) { |
|
152 | - $filetype = filetype($this->getSourcePath($path)); |
|
153 | - if ($filetype == 'link') { |
|
154 | - $filetype = filetype(realpath($this->getSourcePath($path))); |
|
155 | - } |
|
156 | - return $filetype; |
|
157 | - } |
|
158 | - |
|
159 | - public function filesize($path) { |
|
160 | - if ($this->is_dir($path)) { |
|
161 | - return 0; |
|
162 | - } |
|
163 | - $fullPath = $this->getSourcePath($path); |
|
164 | - if (PHP_INT_SIZE === 4) { |
|
165 | - $helper = new \OC\LargeFileHelper; |
|
166 | - return $helper->getFileSize($fullPath); |
|
167 | - } |
|
168 | - return filesize($fullPath); |
|
169 | - } |
|
170 | - |
|
171 | - public function isReadable($path) { |
|
172 | - return is_readable($this->getSourcePath($path)); |
|
173 | - } |
|
174 | - |
|
175 | - public function isUpdatable($path) { |
|
176 | - return is_writable($this->getSourcePath($path)); |
|
177 | - } |
|
178 | - |
|
179 | - public function file_exists($path) { |
|
180 | - return file_exists($this->getSourcePath($path)); |
|
181 | - } |
|
182 | - |
|
183 | - public function filemtime($path) { |
|
184 | - $fullPath = $this->getSourcePath($path); |
|
185 | - clearstatcache(true, $fullPath); |
|
186 | - if (!$this->file_exists($path)) { |
|
187 | - return false; |
|
188 | - } |
|
189 | - if (PHP_INT_SIZE === 4) { |
|
190 | - $helper = new \OC\LargeFileHelper(); |
|
191 | - return $helper->getFileMtime($fullPath); |
|
192 | - } |
|
193 | - return filemtime($fullPath); |
|
194 | - } |
|
195 | - |
|
196 | - public function touch($path, $mtime = null) { |
|
197 | - // sets the modification time of the file to the given value. |
|
198 | - // If mtime is nil the current time is set. |
|
199 | - // note that the access time of the file always changes to the current time. |
|
200 | - if ($this->file_exists($path) and !$this->isUpdatable($path)) { |
|
201 | - return false; |
|
202 | - } |
|
203 | - if (!is_null($mtime)) { |
|
204 | - $result = touch($this->getSourcePath($path), $mtime); |
|
205 | - } else { |
|
206 | - $result = touch($this->getSourcePath($path)); |
|
207 | - } |
|
208 | - if ($result) { |
|
209 | - clearstatcache(true, $this->getSourcePath($path)); |
|
210 | - } |
|
211 | - |
|
212 | - return $result; |
|
213 | - } |
|
214 | - |
|
215 | - public function file_get_contents($path) { |
|
216 | - return file_get_contents($this->getSourcePath($path)); |
|
217 | - } |
|
218 | - |
|
219 | - public function file_put_contents($path, $data) { |
|
220 | - return file_put_contents($this->getSourcePath($path), $data); |
|
221 | - } |
|
222 | - |
|
223 | - public function unlink($path) { |
|
224 | - if ($this->is_dir($path)) { |
|
225 | - return $this->rmdir($path); |
|
226 | - } else if ($this->is_file($path)) { |
|
227 | - return unlink($this->getSourcePath($path)); |
|
228 | - } else { |
|
229 | - return false; |
|
230 | - } |
|
231 | - |
|
232 | - } |
|
233 | - |
|
234 | - public function rename($path1, $path2) { |
|
235 | - $srcParent = dirname($path1); |
|
236 | - $dstParent = dirname($path2); |
|
237 | - |
|
238 | - if (!$this->isUpdatable($srcParent)) { |
|
239 | - \OCP\Util::writeLog('core', 'unable to rename, source directory is not writable : ' . $srcParent, ILogger::ERROR); |
|
240 | - return false; |
|
241 | - } |
|
242 | - |
|
243 | - if (!$this->isUpdatable($dstParent)) { |
|
244 | - \OCP\Util::writeLog('core', 'unable to rename, destination directory is not writable : ' . $dstParent, ILogger::ERROR); |
|
245 | - return false; |
|
246 | - } |
|
247 | - |
|
248 | - if (!$this->file_exists($path1)) { |
|
249 | - \OCP\Util::writeLog('core', 'unable to rename, file does not exists : ' . $path1, ILogger::ERROR); |
|
250 | - return false; |
|
251 | - } |
|
252 | - |
|
253 | - if ($this->is_dir($path2)) { |
|
254 | - $this->rmdir($path2); |
|
255 | - } else if ($this->is_file($path2)) { |
|
256 | - $this->unlink($path2); |
|
257 | - } |
|
258 | - |
|
259 | - if ($this->is_dir($path1)) { |
|
260 | - // we can't move folders across devices, use copy instead |
|
261 | - $stat1 = stat(dirname($this->getSourcePath($path1))); |
|
262 | - $stat2 = stat(dirname($this->getSourcePath($path2))); |
|
263 | - if ($stat1['dev'] !== $stat2['dev']) { |
|
264 | - $result = $this->copy($path1, $path2); |
|
265 | - if ($result) { |
|
266 | - $result &= $this->rmdir($path1); |
|
267 | - } |
|
268 | - return $result; |
|
269 | - } |
|
270 | - } |
|
271 | - |
|
272 | - return rename($this->getSourcePath($path1), $this->getSourcePath($path2)); |
|
273 | - } |
|
274 | - |
|
275 | - public function copy($path1, $path2) { |
|
276 | - if ($this->is_dir($path1)) { |
|
277 | - return parent::copy($path1, $path2); |
|
278 | - } else { |
|
279 | - return copy($this->getSourcePath($path1), $this->getSourcePath($path2)); |
|
280 | - } |
|
281 | - } |
|
282 | - |
|
283 | - public function fopen($path, $mode) { |
|
284 | - return fopen($this->getSourcePath($path), $mode); |
|
285 | - } |
|
286 | - |
|
287 | - public function hash($type, $path, $raw = false) { |
|
288 | - return hash_file($type, $this->getSourcePath($path), $raw); |
|
289 | - } |
|
290 | - |
|
291 | - public function free_space($path) { |
|
292 | - $sourcePath = $this->getSourcePath($path); |
|
293 | - // using !is_dir because $sourcePath might be a part file or |
|
294 | - // non-existing file, so we'd still want to use the parent dir |
|
295 | - // in such cases |
|
296 | - if (!is_dir($sourcePath)) { |
|
297 | - // disk_free_space doesn't work on files |
|
298 | - $sourcePath = dirname($sourcePath); |
|
299 | - } |
|
300 | - $space = @disk_free_space($sourcePath); |
|
301 | - if ($space === false || is_null($space)) { |
|
302 | - return \OCP\Files\FileInfo::SPACE_UNKNOWN; |
|
303 | - } |
|
304 | - return $space; |
|
305 | - } |
|
306 | - |
|
307 | - public function search($query) { |
|
308 | - return $this->searchInDir($query); |
|
309 | - } |
|
310 | - |
|
311 | - public function getLocalFile($path) { |
|
312 | - return $this->getSourcePath($path); |
|
313 | - } |
|
314 | - |
|
315 | - public function getLocalFolder($path) { |
|
316 | - return $this->getSourcePath($path); |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * @param string $query |
|
321 | - * @param string $dir |
|
322 | - * @return array |
|
323 | - */ |
|
324 | - protected function searchInDir($query, $dir = '') { |
|
325 | - $files = array(); |
|
326 | - $physicalDir = $this->getSourcePath($dir); |
|
327 | - foreach (scandir($physicalDir) as $item) { |
|
328 | - if (\OC\Files\Filesystem::isIgnoredDir($item)) |
|
329 | - continue; |
|
330 | - $physicalItem = $physicalDir . '/' . $item; |
|
331 | - |
|
332 | - if (strstr(strtolower($item), strtolower($query)) !== false) { |
|
333 | - $files[] = $dir . '/' . $item; |
|
334 | - } |
|
335 | - if (is_dir($physicalItem)) { |
|
336 | - $files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item)); |
|
337 | - } |
|
338 | - } |
|
339 | - return $files; |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * check if a file or folder has been updated since $time |
|
344 | - * |
|
345 | - * @param string $path |
|
346 | - * @param int $time |
|
347 | - * @return bool |
|
348 | - */ |
|
349 | - public function hasUpdated($path, $time) { |
|
350 | - if ($this->file_exists($path)) { |
|
351 | - return $this->filemtime($path) > $time; |
|
352 | - } else { |
|
353 | - return true; |
|
354 | - } |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Get the source path (on disk) of a given path |
|
359 | - * |
|
360 | - * @param string $path |
|
361 | - * @return string |
|
362 | - * @throws ForbiddenException |
|
363 | - */ |
|
364 | - public function getSourcePath($path) { |
|
365 | - $fullPath = $this->datadir . $path; |
|
366 | - $currentPath = $path; |
|
367 | - if ($this->allowSymlinks || $currentPath === '') { |
|
368 | - return $fullPath; |
|
369 | - } |
|
370 | - $pathToResolve = $fullPath; |
|
371 | - $realPath = realpath($pathToResolve); |
|
372 | - while ($realPath === false) { // for non existing files check the parent directory |
|
373 | - $currentPath = dirname($currentPath); |
|
374 | - if ($currentPath === '' || $currentPath === '.') { |
|
375 | - return $fullPath; |
|
376 | - } |
|
377 | - $realPath = realpath($this->datadir . $currentPath); |
|
378 | - } |
|
379 | - if ($realPath) { |
|
380 | - $realPath = $realPath . '/'; |
|
381 | - } |
|
382 | - if (substr($realPath, 0, $this->dataDirLength) === $this->realDataDir) { |
|
383 | - return $fullPath; |
|
384 | - } |
|
385 | - |
|
386 | - \OCP\Util::writeLog('core', "Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", ILogger::ERROR); |
|
387 | - throw new ForbiddenException('Following symlinks is not allowed', false); |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * {@inheritdoc} |
|
392 | - */ |
|
393 | - public function isLocal() { |
|
394 | - return true; |
|
395 | - } |
|
396 | - |
|
397 | - /** |
|
398 | - * get the ETag for a file or folder |
|
399 | - * |
|
400 | - * @param string $path |
|
401 | - * @return string |
|
402 | - */ |
|
403 | - public function getETag($path) { |
|
404 | - if ($this->is_file($path)) { |
|
405 | - $stat = $this->stat($path); |
|
406 | - return md5( |
|
407 | - $stat['mtime'] . |
|
408 | - $stat['ino'] . |
|
409 | - $stat['dev'] . |
|
410 | - $stat['size'] |
|
411 | - ); |
|
412 | - } else { |
|
413 | - return parent::getETag($path); |
|
414 | - } |
|
415 | - } |
|
416 | - |
|
417 | - /** |
|
418 | - * @param IStorage $sourceStorage |
|
419 | - * @param string $sourceInternalPath |
|
420 | - * @param string $targetInternalPath |
|
421 | - * @param bool $preserveMtime |
|
422 | - * @return bool |
|
423 | - */ |
|
424 | - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) { |
|
425 | - if ($sourceStorage->instanceOfStorage(Local::class)) { |
|
426 | - if ($sourceStorage->instanceOfStorage(Jail::class)) { |
|
427 | - /** |
|
428 | - * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage |
|
429 | - */ |
|
430 | - $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath); |
|
431 | - } |
|
432 | - /** |
|
433 | - * @var \OC\Files\Storage\Local $sourceStorage |
|
434 | - */ |
|
435 | - $rootStorage = new Local(['datadir' => '/']); |
|
436 | - return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath)); |
|
437 | - } else { |
|
438 | - return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
439 | - } |
|
440 | - } |
|
441 | - |
|
442 | - /** |
|
443 | - * @param IStorage $sourceStorage |
|
444 | - * @param string $sourceInternalPath |
|
445 | - * @param string $targetInternalPath |
|
446 | - * @return bool |
|
447 | - */ |
|
448 | - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
449 | - if ($sourceStorage->instanceOfStorage(Local::class)) { |
|
450 | - if ($sourceStorage->instanceOfStorage(Jail::class)) { |
|
451 | - /** |
|
452 | - * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage |
|
453 | - */ |
|
454 | - $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath); |
|
455 | - } |
|
456 | - /** |
|
457 | - * @var \OC\Files\Storage\Local $sourceStorage |
|
458 | - */ |
|
459 | - $rootStorage = new Local(['datadir' => '/']); |
|
460 | - return $rootStorage->rename($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath)); |
|
461 | - } else { |
|
462 | - return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
463 | - } |
|
464 | - } |
|
51 | + protected $datadir; |
|
52 | + |
|
53 | + protected $dataDirLength; |
|
54 | + |
|
55 | + protected $allowSymlinks = false; |
|
56 | + |
|
57 | + protected $realDataDir; |
|
58 | + |
|
59 | + public function __construct($arguments) { |
|
60 | + if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) { |
|
61 | + throw new \InvalidArgumentException('No data directory set for local storage'); |
|
62 | + } |
|
63 | + $this->datadir = str_replace('//', '/', $arguments['datadir']); |
|
64 | + // some crazy code uses a local storage on root... |
|
65 | + if ($this->datadir === '/') { |
|
66 | + $this->realDataDir = $this->datadir; |
|
67 | + } else { |
|
68 | + $realPath = realpath($this->datadir) ?: $this->datadir; |
|
69 | + $this->realDataDir = rtrim($realPath, '/') . '/'; |
|
70 | + } |
|
71 | + if (substr($this->datadir, -1) !== '/') { |
|
72 | + $this->datadir .= '/'; |
|
73 | + } |
|
74 | + $this->dataDirLength = strlen($this->realDataDir); |
|
75 | + } |
|
76 | + |
|
77 | + public function __destruct() { |
|
78 | + } |
|
79 | + |
|
80 | + public function getId() { |
|
81 | + return 'local::' . $this->datadir; |
|
82 | + } |
|
83 | + |
|
84 | + public function mkdir($path) { |
|
85 | + return @mkdir($this->getSourcePath($path), 0777, true); |
|
86 | + } |
|
87 | + |
|
88 | + public function rmdir($path) { |
|
89 | + if (!$this->isDeletable($path)) { |
|
90 | + return false; |
|
91 | + } |
|
92 | + try { |
|
93 | + $it = new \RecursiveIteratorIterator( |
|
94 | + new \RecursiveDirectoryIterator($this->getSourcePath($path)), |
|
95 | + \RecursiveIteratorIterator::CHILD_FIRST |
|
96 | + ); |
|
97 | + /** |
|
98 | + * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach |
|
99 | + * This bug is fixed in PHP 5.5.9 or before |
|
100 | + * See #8376 |
|
101 | + */ |
|
102 | + $it->rewind(); |
|
103 | + while ($it->valid()) { |
|
104 | + /** |
|
105 | + * @var \SplFileInfo $file |
|
106 | + */ |
|
107 | + $file = $it->current(); |
|
108 | + if (in_array($file->getBasename(), array('.', '..'))) { |
|
109 | + $it->next(); |
|
110 | + continue; |
|
111 | + } elseif ($file->isDir()) { |
|
112 | + rmdir($file->getPathname()); |
|
113 | + } elseif ($file->isFile() || $file->isLink()) { |
|
114 | + unlink($file->getPathname()); |
|
115 | + } |
|
116 | + $it->next(); |
|
117 | + } |
|
118 | + return rmdir($this->getSourcePath($path)); |
|
119 | + } catch (\UnexpectedValueException $e) { |
|
120 | + return false; |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + public function opendir($path) { |
|
125 | + return opendir($this->getSourcePath($path)); |
|
126 | + } |
|
127 | + |
|
128 | + public function is_dir($path) { |
|
129 | + if (substr($path, -1) == '/') { |
|
130 | + $path = substr($path, 0, -1); |
|
131 | + } |
|
132 | + return is_dir($this->getSourcePath($path)); |
|
133 | + } |
|
134 | + |
|
135 | + public function is_file($path) { |
|
136 | + return is_file($this->getSourcePath($path)); |
|
137 | + } |
|
138 | + |
|
139 | + public function stat($path) { |
|
140 | + clearstatcache(); |
|
141 | + $fullPath = $this->getSourcePath($path); |
|
142 | + $statResult = stat($fullPath); |
|
143 | + if (PHP_INT_SIZE === 4 && !$this->is_dir($path)) { |
|
144 | + $filesize = $this->filesize($path); |
|
145 | + $statResult['size'] = $filesize; |
|
146 | + $statResult[7] = $filesize; |
|
147 | + } |
|
148 | + return $statResult; |
|
149 | + } |
|
150 | + |
|
151 | + public function filetype($path) { |
|
152 | + $filetype = filetype($this->getSourcePath($path)); |
|
153 | + if ($filetype == 'link') { |
|
154 | + $filetype = filetype(realpath($this->getSourcePath($path))); |
|
155 | + } |
|
156 | + return $filetype; |
|
157 | + } |
|
158 | + |
|
159 | + public function filesize($path) { |
|
160 | + if ($this->is_dir($path)) { |
|
161 | + return 0; |
|
162 | + } |
|
163 | + $fullPath = $this->getSourcePath($path); |
|
164 | + if (PHP_INT_SIZE === 4) { |
|
165 | + $helper = new \OC\LargeFileHelper; |
|
166 | + return $helper->getFileSize($fullPath); |
|
167 | + } |
|
168 | + return filesize($fullPath); |
|
169 | + } |
|
170 | + |
|
171 | + public function isReadable($path) { |
|
172 | + return is_readable($this->getSourcePath($path)); |
|
173 | + } |
|
174 | + |
|
175 | + public function isUpdatable($path) { |
|
176 | + return is_writable($this->getSourcePath($path)); |
|
177 | + } |
|
178 | + |
|
179 | + public function file_exists($path) { |
|
180 | + return file_exists($this->getSourcePath($path)); |
|
181 | + } |
|
182 | + |
|
183 | + public function filemtime($path) { |
|
184 | + $fullPath = $this->getSourcePath($path); |
|
185 | + clearstatcache(true, $fullPath); |
|
186 | + if (!$this->file_exists($path)) { |
|
187 | + return false; |
|
188 | + } |
|
189 | + if (PHP_INT_SIZE === 4) { |
|
190 | + $helper = new \OC\LargeFileHelper(); |
|
191 | + return $helper->getFileMtime($fullPath); |
|
192 | + } |
|
193 | + return filemtime($fullPath); |
|
194 | + } |
|
195 | + |
|
196 | + public function touch($path, $mtime = null) { |
|
197 | + // sets the modification time of the file to the given value. |
|
198 | + // If mtime is nil the current time is set. |
|
199 | + // note that the access time of the file always changes to the current time. |
|
200 | + if ($this->file_exists($path) and !$this->isUpdatable($path)) { |
|
201 | + return false; |
|
202 | + } |
|
203 | + if (!is_null($mtime)) { |
|
204 | + $result = touch($this->getSourcePath($path), $mtime); |
|
205 | + } else { |
|
206 | + $result = touch($this->getSourcePath($path)); |
|
207 | + } |
|
208 | + if ($result) { |
|
209 | + clearstatcache(true, $this->getSourcePath($path)); |
|
210 | + } |
|
211 | + |
|
212 | + return $result; |
|
213 | + } |
|
214 | + |
|
215 | + public function file_get_contents($path) { |
|
216 | + return file_get_contents($this->getSourcePath($path)); |
|
217 | + } |
|
218 | + |
|
219 | + public function file_put_contents($path, $data) { |
|
220 | + return file_put_contents($this->getSourcePath($path), $data); |
|
221 | + } |
|
222 | + |
|
223 | + public function unlink($path) { |
|
224 | + if ($this->is_dir($path)) { |
|
225 | + return $this->rmdir($path); |
|
226 | + } else if ($this->is_file($path)) { |
|
227 | + return unlink($this->getSourcePath($path)); |
|
228 | + } else { |
|
229 | + return false; |
|
230 | + } |
|
231 | + |
|
232 | + } |
|
233 | + |
|
234 | + public function rename($path1, $path2) { |
|
235 | + $srcParent = dirname($path1); |
|
236 | + $dstParent = dirname($path2); |
|
237 | + |
|
238 | + if (!$this->isUpdatable($srcParent)) { |
|
239 | + \OCP\Util::writeLog('core', 'unable to rename, source directory is not writable : ' . $srcParent, ILogger::ERROR); |
|
240 | + return false; |
|
241 | + } |
|
242 | + |
|
243 | + if (!$this->isUpdatable($dstParent)) { |
|
244 | + \OCP\Util::writeLog('core', 'unable to rename, destination directory is not writable : ' . $dstParent, ILogger::ERROR); |
|
245 | + return false; |
|
246 | + } |
|
247 | + |
|
248 | + if (!$this->file_exists($path1)) { |
|
249 | + \OCP\Util::writeLog('core', 'unable to rename, file does not exists : ' . $path1, ILogger::ERROR); |
|
250 | + return false; |
|
251 | + } |
|
252 | + |
|
253 | + if ($this->is_dir($path2)) { |
|
254 | + $this->rmdir($path2); |
|
255 | + } else if ($this->is_file($path2)) { |
|
256 | + $this->unlink($path2); |
|
257 | + } |
|
258 | + |
|
259 | + if ($this->is_dir($path1)) { |
|
260 | + // we can't move folders across devices, use copy instead |
|
261 | + $stat1 = stat(dirname($this->getSourcePath($path1))); |
|
262 | + $stat2 = stat(dirname($this->getSourcePath($path2))); |
|
263 | + if ($stat1['dev'] !== $stat2['dev']) { |
|
264 | + $result = $this->copy($path1, $path2); |
|
265 | + if ($result) { |
|
266 | + $result &= $this->rmdir($path1); |
|
267 | + } |
|
268 | + return $result; |
|
269 | + } |
|
270 | + } |
|
271 | + |
|
272 | + return rename($this->getSourcePath($path1), $this->getSourcePath($path2)); |
|
273 | + } |
|
274 | + |
|
275 | + public function copy($path1, $path2) { |
|
276 | + if ($this->is_dir($path1)) { |
|
277 | + return parent::copy($path1, $path2); |
|
278 | + } else { |
|
279 | + return copy($this->getSourcePath($path1), $this->getSourcePath($path2)); |
|
280 | + } |
|
281 | + } |
|
282 | + |
|
283 | + public function fopen($path, $mode) { |
|
284 | + return fopen($this->getSourcePath($path), $mode); |
|
285 | + } |
|
286 | + |
|
287 | + public function hash($type, $path, $raw = false) { |
|
288 | + return hash_file($type, $this->getSourcePath($path), $raw); |
|
289 | + } |
|
290 | + |
|
291 | + public function free_space($path) { |
|
292 | + $sourcePath = $this->getSourcePath($path); |
|
293 | + // using !is_dir because $sourcePath might be a part file or |
|
294 | + // non-existing file, so we'd still want to use the parent dir |
|
295 | + // in such cases |
|
296 | + if (!is_dir($sourcePath)) { |
|
297 | + // disk_free_space doesn't work on files |
|
298 | + $sourcePath = dirname($sourcePath); |
|
299 | + } |
|
300 | + $space = @disk_free_space($sourcePath); |
|
301 | + if ($space === false || is_null($space)) { |
|
302 | + return \OCP\Files\FileInfo::SPACE_UNKNOWN; |
|
303 | + } |
|
304 | + return $space; |
|
305 | + } |
|
306 | + |
|
307 | + public function search($query) { |
|
308 | + return $this->searchInDir($query); |
|
309 | + } |
|
310 | + |
|
311 | + public function getLocalFile($path) { |
|
312 | + return $this->getSourcePath($path); |
|
313 | + } |
|
314 | + |
|
315 | + public function getLocalFolder($path) { |
|
316 | + return $this->getSourcePath($path); |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * @param string $query |
|
321 | + * @param string $dir |
|
322 | + * @return array |
|
323 | + */ |
|
324 | + protected function searchInDir($query, $dir = '') { |
|
325 | + $files = array(); |
|
326 | + $physicalDir = $this->getSourcePath($dir); |
|
327 | + foreach (scandir($physicalDir) as $item) { |
|
328 | + if (\OC\Files\Filesystem::isIgnoredDir($item)) |
|
329 | + continue; |
|
330 | + $physicalItem = $physicalDir . '/' . $item; |
|
331 | + |
|
332 | + if (strstr(strtolower($item), strtolower($query)) !== false) { |
|
333 | + $files[] = $dir . '/' . $item; |
|
334 | + } |
|
335 | + if (is_dir($physicalItem)) { |
|
336 | + $files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item)); |
|
337 | + } |
|
338 | + } |
|
339 | + return $files; |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * check if a file or folder has been updated since $time |
|
344 | + * |
|
345 | + * @param string $path |
|
346 | + * @param int $time |
|
347 | + * @return bool |
|
348 | + */ |
|
349 | + public function hasUpdated($path, $time) { |
|
350 | + if ($this->file_exists($path)) { |
|
351 | + return $this->filemtime($path) > $time; |
|
352 | + } else { |
|
353 | + return true; |
|
354 | + } |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Get the source path (on disk) of a given path |
|
359 | + * |
|
360 | + * @param string $path |
|
361 | + * @return string |
|
362 | + * @throws ForbiddenException |
|
363 | + */ |
|
364 | + public function getSourcePath($path) { |
|
365 | + $fullPath = $this->datadir . $path; |
|
366 | + $currentPath = $path; |
|
367 | + if ($this->allowSymlinks || $currentPath === '') { |
|
368 | + return $fullPath; |
|
369 | + } |
|
370 | + $pathToResolve = $fullPath; |
|
371 | + $realPath = realpath($pathToResolve); |
|
372 | + while ($realPath === false) { // for non existing files check the parent directory |
|
373 | + $currentPath = dirname($currentPath); |
|
374 | + if ($currentPath === '' || $currentPath === '.') { |
|
375 | + return $fullPath; |
|
376 | + } |
|
377 | + $realPath = realpath($this->datadir . $currentPath); |
|
378 | + } |
|
379 | + if ($realPath) { |
|
380 | + $realPath = $realPath . '/'; |
|
381 | + } |
|
382 | + if (substr($realPath, 0, $this->dataDirLength) === $this->realDataDir) { |
|
383 | + return $fullPath; |
|
384 | + } |
|
385 | + |
|
386 | + \OCP\Util::writeLog('core', "Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", ILogger::ERROR); |
|
387 | + throw new ForbiddenException('Following symlinks is not allowed', false); |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * {@inheritdoc} |
|
392 | + */ |
|
393 | + public function isLocal() { |
|
394 | + return true; |
|
395 | + } |
|
396 | + |
|
397 | + /** |
|
398 | + * get the ETag for a file or folder |
|
399 | + * |
|
400 | + * @param string $path |
|
401 | + * @return string |
|
402 | + */ |
|
403 | + public function getETag($path) { |
|
404 | + if ($this->is_file($path)) { |
|
405 | + $stat = $this->stat($path); |
|
406 | + return md5( |
|
407 | + $stat['mtime'] . |
|
408 | + $stat['ino'] . |
|
409 | + $stat['dev'] . |
|
410 | + $stat['size'] |
|
411 | + ); |
|
412 | + } else { |
|
413 | + return parent::getETag($path); |
|
414 | + } |
|
415 | + } |
|
416 | + |
|
417 | + /** |
|
418 | + * @param IStorage $sourceStorage |
|
419 | + * @param string $sourceInternalPath |
|
420 | + * @param string $targetInternalPath |
|
421 | + * @param bool $preserveMtime |
|
422 | + * @return bool |
|
423 | + */ |
|
424 | + public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) { |
|
425 | + if ($sourceStorage->instanceOfStorage(Local::class)) { |
|
426 | + if ($sourceStorage->instanceOfStorage(Jail::class)) { |
|
427 | + /** |
|
428 | + * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage |
|
429 | + */ |
|
430 | + $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath); |
|
431 | + } |
|
432 | + /** |
|
433 | + * @var \OC\Files\Storage\Local $sourceStorage |
|
434 | + */ |
|
435 | + $rootStorage = new Local(['datadir' => '/']); |
|
436 | + return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath)); |
|
437 | + } else { |
|
438 | + return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
439 | + } |
|
440 | + } |
|
441 | + |
|
442 | + /** |
|
443 | + * @param IStorage $sourceStorage |
|
444 | + * @param string $sourceInternalPath |
|
445 | + * @param string $targetInternalPath |
|
446 | + * @return bool |
|
447 | + */ |
|
448 | + public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
449 | + if ($sourceStorage->instanceOfStorage(Local::class)) { |
|
450 | + if ($sourceStorage->instanceOfStorage(Jail::class)) { |
|
451 | + /** |
|
452 | + * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage |
|
453 | + */ |
|
454 | + $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath); |
|
455 | + } |
|
456 | + /** |
|
457 | + * @var \OC\Files\Storage\Local $sourceStorage |
|
458 | + */ |
|
459 | + $rootStorage = new Local(['datadir' => '/']); |
|
460 | + return $rootStorage->rename($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath)); |
|
461 | + } else { |
|
462 | + return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
463 | + } |
|
464 | + } |
|
465 | 465 | } |
@@ -58,801 +58,801 @@ |
||
58 | 58 | * @package OC\Files\Storage |
59 | 59 | */ |
60 | 60 | class DAV extends Common { |
61 | - /** @var string */ |
|
62 | - protected $password; |
|
63 | - /** @var string */ |
|
64 | - protected $user; |
|
65 | - /** @var string */ |
|
66 | - protected $authType; |
|
67 | - /** @var string */ |
|
68 | - protected $host; |
|
69 | - /** @var bool */ |
|
70 | - protected $secure; |
|
71 | - /** @var string */ |
|
72 | - protected $root; |
|
73 | - /** @var string */ |
|
74 | - protected $certPath; |
|
75 | - /** @var bool */ |
|
76 | - protected $ready; |
|
77 | - /** @var Client */ |
|
78 | - protected $client; |
|
79 | - /** @var ArrayCache */ |
|
80 | - protected $statCache; |
|
81 | - /** @var \OCP\Http\Client\IClientService */ |
|
82 | - protected $httpClientService; |
|
83 | - |
|
84 | - /** |
|
85 | - * @param array $params |
|
86 | - * @throws \Exception |
|
87 | - */ |
|
88 | - public function __construct($params) { |
|
89 | - $this->statCache = new ArrayCache(); |
|
90 | - $this->httpClientService = \OC::$server->getHTTPClientService(); |
|
91 | - if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
|
92 | - $host = $params['host']; |
|
93 | - //remove leading http[s], will be generated in createBaseUri() |
|
94 | - if (substr($host, 0, 8) == "https://") $host = substr($host, 8); |
|
95 | - else if (substr($host, 0, 7) == "http://") $host = substr($host, 7); |
|
96 | - $this->host = $host; |
|
97 | - $this->user = $params['user']; |
|
98 | - $this->password = $params['password']; |
|
99 | - if (isset($params['authType'])) { |
|
100 | - $this->authType = $params['authType']; |
|
101 | - } |
|
102 | - if (isset($params['secure'])) { |
|
103 | - if (is_string($params['secure'])) { |
|
104 | - $this->secure = ($params['secure'] === 'true'); |
|
105 | - } else { |
|
106 | - $this->secure = (bool)$params['secure']; |
|
107 | - } |
|
108 | - } else { |
|
109 | - $this->secure = false; |
|
110 | - } |
|
111 | - if ($this->secure === true) { |
|
112 | - // inject mock for testing |
|
113 | - $certManager = \OC::$server->getCertificateManager(); |
|
114 | - if (is_null($certManager)) { //no user |
|
115 | - $certManager = \OC::$server->getCertificateManager(null); |
|
116 | - } |
|
117 | - $certPath = $certManager->getAbsoluteBundlePath(); |
|
118 | - if (file_exists($certPath)) { |
|
119 | - $this->certPath = $certPath; |
|
120 | - } |
|
121 | - } |
|
122 | - $this->root = $params['root'] ?? '/'; |
|
123 | - $this->root = '/' . ltrim($this->root, '/'); |
|
124 | - $this->root = rtrim($this->root, '/') . '/'; |
|
125 | - } else { |
|
126 | - throw new \Exception('Invalid webdav storage configuration'); |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - protected function init() { |
|
131 | - if ($this->ready) { |
|
132 | - return; |
|
133 | - } |
|
134 | - $this->ready = true; |
|
135 | - |
|
136 | - $settings = [ |
|
137 | - 'baseUri' => $this->createBaseUri(), |
|
138 | - 'userName' => $this->user, |
|
139 | - 'password' => $this->password, |
|
140 | - ]; |
|
141 | - if (isset($this->authType)) { |
|
142 | - $settings['authType'] = $this->authType; |
|
143 | - } |
|
144 | - |
|
145 | - $proxy = \OC::$server->getConfig()->getSystemValue('proxy', ''); |
|
146 | - if ($proxy !== '') { |
|
147 | - $settings['proxy'] = $proxy; |
|
148 | - } |
|
149 | - |
|
150 | - $this->client = new Client($settings); |
|
151 | - $this->client->setThrowExceptions(true); |
|
152 | - if ($this->secure === true && $this->certPath) { |
|
153 | - $this->client->addCurlSetting(CURLOPT_CAINFO, $this->certPath); |
|
154 | - } |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Clear the stat cache |
|
159 | - */ |
|
160 | - public function clearStatCache() { |
|
161 | - $this->statCache->clear(); |
|
162 | - } |
|
163 | - |
|
164 | - /** {@inheritdoc} */ |
|
165 | - public function getId() { |
|
166 | - return 'webdav::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
167 | - } |
|
168 | - |
|
169 | - /** {@inheritdoc} */ |
|
170 | - public function createBaseUri() { |
|
171 | - $baseUri = 'http'; |
|
172 | - if ($this->secure) { |
|
173 | - $baseUri .= 's'; |
|
174 | - } |
|
175 | - $baseUri .= '://' . $this->host . $this->root; |
|
176 | - return $baseUri; |
|
177 | - } |
|
178 | - |
|
179 | - /** {@inheritdoc} */ |
|
180 | - public function mkdir($path) { |
|
181 | - $this->init(); |
|
182 | - $path = $this->cleanPath($path); |
|
183 | - $result = $this->simpleResponse('MKCOL', $path, null, 201); |
|
184 | - if ($result) { |
|
185 | - $this->statCache->set($path, true); |
|
186 | - } |
|
187 | - return $result; |
|
188 | - } |
|
189 | - |
|
190 | - /** {@inheritdoc} */ |
|
191 | - public function rmdir($path) { |
|
192 | - $this->init(); |
|
193 | - $path = $this->cleanPath($path); |
|
194 | - // FIXME: some WebDAV impl return 403 when trying to DELETE |
|
195 | - // a non-empty folder |
|
196 | - $result = $this->simpleResponse('DELETE', $path . '/', null, 204); |
|
197 | - $this->statCache->clear($path . '/'); |
|
198 | - $this->statCache->remove($path); |
|
199 | - return $result; |
|
200 | - } |
|
201 | - |
|
202 | - /** {@inheritdoc} */ |
|
203 | - public function opendir($path) { |
|
204 | - $this->init(); |
|
205 | - $path = $this->cleanPath($path); |
|
206 | - try { |
|
207 | - $response = $this->client->propFind( |
|
208 | - $this->encodePath($path), |
|
209 | - ['{DAV:}href'], |
|
210 | - 1 |
|
211 | - ); |
|
212 | - if ($response === false) { |
|
213 | - return false; |
|
214 | - } |
|
215 | - $content = []; |
|
216 | - $files = array_keys($response); |
|
217 | - array_shift($files); //the first entry is the current directory |
|
218 | - |
|
219 | - if (!$this->statCache->hasKey($path)) { |
|
220 | - $this->statCache->set($path, true); |
|
221 | - } |
|
222 | - foreach ($files as $file) { |
|
223 | - $file = urldecode($file); |
|
224 | - // do not store the real entry, we might not have all properties |
|
225 | - if (!$this->statCache->hasKey($path)) { |
|
226 | - $this->statCache->set($file, true); |
|
227 | - } |
|
228 | - $file = basename($file); |
|
229 | - $content[] = $file; |
|
230 | - } |
|
231 | - return IteratorDirectory::wrap($content); |
|
232 | - } catch (\Exception $e) { |
|
233 | - $this->convertException($e, $path); |
|
234 | - } |
|
235 | - return false; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Propfind call with cache handling. |
|
240 | - * |
|
241 | - * First checks if information is cached. |
|
242 | - * If not, request it from the server then store to cache. |
|
243 | - * |
|
244 | - * @param string $path path to propfind |
|
245 | - * |
|
246 | - * @return array|boolean propfind response or false if the entry was not found |
|
247 | - * |
|
248 | - * @throws ClientHttpException |
|
249 | - */ |
|
250 | - protected function propfind($path) { |
|
251 | - $path = $this->cleanPath($path); |
|
252 | - $cachedResponse = $this->statCache->get($path); |
|
253 | - // we either don't know it, or we know it exists but need more details |
|
254 | - if (is_null($cachedResponse) || $cachedResponse === true) { |
|
255 | - $this->init(); |
|
256 | - try { |
|
257 | - $response = $this->client->propFind( |
|
258 | - $this->encodePath($path), |
|
259 | - array( |
|
260 | - '{DAV:}getlastmodified', |
|
261 | - '{DAV:}getcontentlength', |
|
262 | - '{DAV:}getcontenttype', |
|
263 | - '{http://owncloud.org/ns}permissions', |
|
264 | - '{http://open-collaboration-services.org/ns}share-permissions', |
|
265 | - '{DAV:}resourcetype', |
|
266 | - '{DAV:}getetag', |
|
267 | - ) |
|
268 | - ); |
|
269 | - $this->statCache->set($path, $response); |
|
270 | - } catch (ClientHttpException $e) { |
|
271 | - if ($e->getHttpStatus() === 404) { |
|
272 | - $this->statCache->clear($path . '/'); |
|
273 | - $this->statCache->set($path, false); |
|
274 | - return false; |
|
275 | - } |
|
276 | - $this->convertException($e, $path); |
|
277 | - } catch (\Exception $e) { |
|
278 | - $this->convertException($e, $path); |
|
279 | - } |
|
280 | - } else { |
|
281 | - $response = $cachedResponse; |
|
282 | - } |
|
283 | - return $response; |
|
284 | - } |
|
285 | - |
|
286 | - /** {@inheritdoc} */ |
|
287 | - public function filetype($path) { |
|
288 | - try { |
|
289 | - $response = $this->propfind($path); |
|
290 | - if ($response === false) { |
|
291 | - return false; |
|
292 | - } |
|
293 | - $responseType = []; |
|
294 | - if (isset($response["{DAV:}resourcetype"])) { |
|
295 | - /** @var ResourceType[] $response */ |
|
296 | - $responseType = $response["{DAV:}resourcetype"]->getValue(); |
|
297 | - } |
|
298 | - return (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file'; |
|
299 | - } catch (\Exception $e) { |
|
300 | - $this->convertException($e, $path); |
|
301 | - } |
|
302 | - return false; |
|
303 | - } |
|
304 | - |
|
305 | - /** {@inheritdoc} */ |
|
306 | - public function file_exists($path) { |
|
307 | - try { |
|
308 | - $path = $this->cleanPath($path); |
|
309 | - $cachedState = $this->statCache->get($path); |
|
310 | - if ($cachedState === false) { |
|
311 | - // we know the file doesn't exist |
|
312 | - return false; |
|
313 | - } else if (!is_null($cachedState)) { |
|
314 | - return true; |
|
315 | - } |
|
316 | - // need to get from server |
|
317 | - return ($this->propfind($path) !== false); |
|
318 | - } catch (\Exception $e) { |
|
319 | - $this->convertException($e, $path); |
|
320 | - } |
|
321 | - return false; |
|
322 | - } |
|
323 | - |
|
324 | - /** {@inheritdoc} */ |
|
325 | - public function unlink($path) { |
|
326 | - $this->init(); |
|
327 | - $path = $this->cleanPath($path); |
|
328 | - $result = $this->simpleResponse('DELETE', $path, null, 204); |
|
329 | - $this->statCache->clear($path . '/'); |
|
330 | - $this->statCache->remove($path); |
|
331 | - return $result; |
|
332 | - } |
|
333 | - |
|
334 | - /** {@inheritdoc} */ |
|
335 | - public function fopen($path, $mode) { |
|
336 | - $this->init(); |
|
337 | - $path = $this->cleanPath($path); |
|
338 | - switch ($mode) { |
|
339 | - case 'r': |
|
340 | - case 'rb': |
|
341 | - try { |
|
342 | - $response = $this->httpClientService |
|
343 | - ->newClient() |
|
344 | - ->get($this->createBaseUri() . $this->encodePath($path), [ |
|
345 | - 'auth' => [$this->user, $this->password], |
|
346 | - 'stream' => true |
|
347 | - ]); |
|
348 | - } catch (\GuzzleHttp\Exception\ClientException $e) { |
|
349 | - if ($e->getResponse() instanceof ResponseInterface |
|
350 | - && $e->getResponse()->getStatusCode() === 404) { |
|
351 | - return false; |
|
352 | - } else { |
|
353 | - throw $e; |
|
354 | - } |
|
355 | - } |
|
356 | - |
|
357 | - if ($response->getStatusCode() !== Http::STATUS_OK) { |
|
358 | - if ($response->getStatusCode() === Http::STATUS_LOCKED) { |
|
359 | - throw new \OCP\Lock\LockedException($path); |
|
360 | - } else { |
|
361 | - Util::writeLog("webdav client", 'Guzzle get returned status code ' . $response->getStatusCode(), ILogger::ERROR); |
|
362 | - } |
|
363 | - } |
|
364 | - |
|
365 | - return $response->getBody(); |
|
366 | - case 'w': |
|
367 | - case 'wb': |
|
368 | - case 'a': |
|
369 | - case 'ab': |
|
370 | - case 'r+': |
|
371 | - case 'w+': |
|
372 | - case 'wb+': |
|
373 | - case 'a+': |
|
374 | - case 'x': |
|
375 | - case 'x+': |
|
376 | - case 'c': |
|
377 | - case 'c+': |
|
378 | - //emulate these |
|
379 | - $tempManager = \OC::$server->getTempManager(); |
|
380 | - if (strrpos($path, '.') !== false) { |
|
381 | - $ext = substr($path, strrpos($path, '.')); |
|
382 | - } else { |
|
383 | - $ext = ''; |
|
384 | - } |
|
385 | - if ($this->file_exists($path)) { |
|
386 | - if (!$this->isUpdatable($path)) { |
|
387 | - return false; |
|
388 | - } |
|
389 | - if ($mode === 'w' or $mode === 'w+') { |
|
390 | - $tmpFile = $tempManager->getTemporaryFile($ext); |
|
391 | - } else { |
|
392 | - $tmpFile = $this->getCachedFile($path); |
|
393 | - } |
|
394 | - } else { |
|
395 | - if (!$this->isCreatable(dirname($path))) { |
|
396 | - return false; |
|
397 | - } |
|
398 | - $tmpFile = $tempManager->getTemporaryFile($ext); |
|
399 | - } |
|
400 | - $handle = fopen($tmpFile, $mode); |
|
401 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
402 | - $this->writeBack($tmpFile, $path); |
|
403 | - }); |
|
404 | - } |
|
405 | - } |
|
406 | - |
|
407 | - /** |
|
408 | - * @param string $tmpFile |
|
409 | - */ |
|
410 | - public function writeBack($tmpFile, $path) { |
|
411 | - $this->uploadFile($tmpFile, $path); |
|
412 | - unlink($tmpFile); |
|
413 | - } |
|
414 | - |
|
415 | - /** {@inheritdoc} */ |
|
416 | - public function free_space($path) { |
|
417 | - $this->init(); |
|
418 | - $path = $this->cleanPath($path); |
|
419 | - try { |
|
420 | - // TODO: cacheable ? |
|
421 | - $response = $this->client->propfind($this->encodePath($path), ['{DAV:}quota-available-bytes']); |
|
422 | - if ($response === false) { |
|
423 | - return FileInfo::SPACE_UNKNOWN; |
|
424 | - } |
|
425 | - if (isset($response['{DAV:}quota-available-bytes'])) { |
|
426 | - return (int)$response['{DAV:}quota-available-bytes']; |
|
427 | - } else { |
|
428 | - return FileInfo::SPACE_UNKNOWN; |
|
429 | - } |
|
430 | - } catch (\Exception $e) { |
|
431 | - return FileInfo::SPACE_UNKNOWN; |
|
432 | - } |
|
433 | - } |
|
434 | - |
|
435 | - /** {@inheritdoc} */ |
|
436 | - public function touch($path, $mtime = null) { |
|
437 | - $this->init(); |
|
438 | - if (is_null($mtime)) { |
|
439 | - $mtime = time(); |
|
440 | - } |
|
441 | - $path = $this->cleanPath($path); |
|
442 | - |
|
443 | - // if file exists, update the mtime, else create a new empty file |
|
444 | - if ($this->file_exists($path)) { |
|
445 | - try { |
|
446 | - $this->statCache->remove($path); |
|
447 | - $this->client->proppatch($this->encodePath($path), ['{DAV:}lastmodified' => $mtime]); |
|
448 | - // non-owncloud clients might not have accepted the property, need to recheck it |
|
449 | - $response = $this->client->propfind($this->encodePath($path), ['{DAV:}getlastmodified'], 0); |
|
450 | - if ($response === false) { |
|
451 | - return false; |
|
452 | - } |
|
453 | - if (isset($response['{DAV:}getlastmodified'])) { |
|
454 | - $remoteMtime = strtotime($response['{DAV:}getlastmodified']); |
|
455 | - if ($remoteMtime !== $mtime) { |
|
456 | - // server has not accepted the mtime |
|
457 | - return false; |
|
458 | - } |
|
459 | - } |
|
460 | - } catch (ClientHttpException $e) { |
|
461 | - if ($e->getHttpStatus() === 501) { |
|
462 | - return false; |
|
463 | - } |
|
464 | - $this->convertException($e, $path); |
|
465 | - return false; |
|
466 | - } catch (\Exception $e) { |
|
467 | - $this->convertException($e, $path); |
|
468 | - return false; |
|
469 | - } |
|
470 | - } else { |
|
471 | - $this->file_put_contents($path, ''); |
|
472 | - } |
|
473 | - return true; |
|
474 | - } |
|
475 | - |
|
476 | - /** |
|
477 | - * @param string $path |
|
478 | - * @param string $data |
|
479 | - * @return int |
|
480 | - */ |
|
481 | - public function file_put_contents($path, $data) { |
|
482 | - $path = $this->cleanPath($path); |
|
483 | - $result = parent::file_put_contents($path, $data); |
|
484 | - $this->statCache->remove($path); |
|
485 | - return $result; |
|
486 | - } |
|
487 | - |
|
488 | - /** |
|
489 | - * @param string $path |
|
490 | - * @param string $target |
|
491 | - */ |
|
492 | - protected function uploadFile($path, $target) { |
|
493 | - $this->init(); |
|
494 | - |
|
495 | - // invalidate |
|
496 | - $target = $this->cleanPath($target); |
|
497 | - $this->statCache->remove($target); |
|
498 | - $source = fopen($path, 'r'); |
|
499 | - |
|
500 | - $this->httpClientService |
|
501 | - ->newClient() |
|
502 | - ->put($this->createBaseUri() . $this->encodePath($target), [ |
|
503 | - 'body' => $source, |
|
504 | - 'auth' => [$this->user, $this->password] |
|
505 | - ]); |
|
506 | - |
|
507 | - $this->removeCachedFile($target); |
|
508 | - } |
|
509 | - |
|
510 | - /** {@inheritdoc} */ |
|
511 | - public function rename($path1, $path2) { |
|
512 | - $this->init(); |
|
513 | - $path1 = $this->cleanPath($path1); |
|
514 | - $path2 = $this->cleanPath($path2); |
|
515 | - try { |
|
516 | - // overwrite directory ? |
|
517 | - if ($this->is_dir($path2)) { |
|
518 | - // needs trailing slash in destination |
|
519 | - $path2 = rtrim($path2, '/') . '/'; |
|
520 | - } |
|
521 | - $this->client->request( |
|
522 | - 'MOVE', |
|
523 | - $this->encodePath($path1), |
|
524 | - null, |
|
525 | - [ |
|
526 | - 'Destination' => $this->createBaseUri() . $this->encodePath($path2), |
|
527 | - ] |
|
528 | - ); |
|
529 | - $this->statCache->clear($path1 . '/'); |
|
530 | - $this->statCache->clear($path2 . '/'); |
|
531 | - $this->statCache->set($path1, false); |
|
532 | - $this->statCache->set($path2, true); |
|
533 | - $this->removeCachedFile($path1); |
|
534 | - $this->removeCachedFile($path2); |
|
535 | - return true; |
|
536 | - } catch (\Exception $e) { |
|
537 | - $this->convertException($e); |
|
538 | - } |
|
539 | - return false; |
|
540 | - } |
|
541 | - |
|
542 | - /** {@inheritdoc} */ |
|
543 | - public function copy($path1, $path2) { |
|
544 | - $this->init(); |
|
545 | - $path1 = $this->cleanPath($path1); |
|
546 | - $path2 = $this->cleanPath($path2); |
|
547 | - try { |
|
548 | - // overwrite directory ? |
|
549 | - if ($this->is_dir($path2)) { |
|
550 | - // needs trailing slash in destination |
|
551 | - $path2 = rtrim($path2, '/') . '/'; |
|
552 | - } |
|
553 | - $this->client->request( |
|
554 | - 'COPY', |
|
555 | - $this->encodePath($path1), |
|
556 | - null, |
|
557 | - [ |
|
558 | - 'Destination' => $this->createBaseUri() . $this->encodePath($path2), |
|
559 | - ] |
|
560 | - ); |
|
561 | - $this->statCache->clear($path2 . '/'); |
|
562 | - $this->statCache->set($path2, true); |
|
563 | - $this->removeCachedFile($path2); |
|
564 | - return true; |
|
565 | - } catch (\Exception $e) { |
|
566 | - $this->convertException($e); |
|
567 | - } |
|
568 | - return false; |
|
569 | - } |
|
570 | - |
|
571 | - /** {@inheritdoc} */ |
|
572 | - public function stat($path) { |
|
573 | - try { |
|
574 | - $response = $this->propfind($path); |
|
575 | - if (!$response) { |
|
576 | - return false; |
|
577 | - } |
|
578 | - return [ |
|
579 | - 'mtime' => strtotime($response['{DAV:}getlastmodified']), |
|
580 | - 'size' => (int)isset($response['{DAV:}getcontentlength']) ? $response['{DAV:}getcontentlength'] : 0, |
|
581 | - ]; |
|
582 | - } catch (\Exception $e) { |
|
583 | - $this->convertException($e, $path); |
|
584 | - } |
|
585 | - return array(); |
|
586 | - } |
|
587 | - |
|
588 | - /** {@inheritdoc} */ |
|
589 | - public function getMimeType($path) { |
|
590 | - $remoteMimetype = $this->getMimeTypeFromRemote($path); |
|
591 | - if ($remoteMimetype === 'application/octet-stream') { |
|
592 | - return \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
593 | - } else { |
|
594 | - return $remoteMimetype; |
|
595 | - } |
|
596 | - } |
|
597 | - |
|
598 | - public function getMimeTypeFromRemote($path) { |
|
599 | - try { |
|
600 | - $response = $this->propfind($path); |
|
601 | - if ($response === false) { |
|
602 | - return false; |
|
603 | - } |
|
604 | - $responseType = []; |
|
605 | - if (isset($response["{DAV:}resourcetype"])) { |
|
606 | - /** @var ResourceType[] $response */ |
|
607 | - $responseType = $response["{DAV:}resourcetype"]->getValue(); |
|
608 | - } |
|
609 | - $type = (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file'; |
|
610 | - if ($type == 'dir') { |
|
611 | - return 'httpd/unix-directory'; |
|
612 | - } elseif (isset($response['{DAV:}getcontenttype'])) { |
|
613 | - return $response['{DAV:}getcontenttype']; |
|
614 | - } else { |
|
615 | - return 'application/octet-stream'; |
|
616 | - } |
|
617 | - } catch (\Exception $e) { |
|
618 | - return false; |
|
619 | - } |
|
620 | - } |
|
621 | - |
|
622 | - /** |
|
623 | - * @param string $path |
|
624 | - * @return string |
|
625 | - */ |
|
626 | - public function cleanPath($path) { |
|
627 | - if ($path === '') { |
|
628 | - return $path; |
|
629 | - } |
|
630 | - $path = Filesystem::normalizePath($path); |
|
631 | - // remove leading slash |
|
632 | - return substr($path, 1); |
|
633 | - } |
|
634 | - |
|
635 | - /** |
|
636 | - * URL encodes the given path but keeps the slashes |
|
637 | - * |
|
638 | - * @param string $path to encode |
|
639 | - * @return string encoded path |
|
640 | - */ |
|
641 | - protected function encodePath($path) { |
|
642 | - // slashes need to stay |
|
643 | - return str_replace('%2F', '/', rawurlencode($path)); |
|
644 | - } |
|
645 | - |
|
646 | - /** |
|
647 | - * @param string $method |
|
648 | - * @param string $path |
|
649 | - * @param string|resource|null $body |
|
650 | - * @param int $expected |
|
651 | - * @return bool |
|
652 | - * @throws StorageInvalidException |
|
653 | - * @throws StorageNotAvailableException |
|
654 | - */ |
|
655 | - protected function simpleResponse($method, $path, $body, $expected) { |
|
656 | - $path = $this->cleanPath($path); |
|
657 | - try { |
|
658 | - $response = $this->client->request($method, $this->encodePath($path), $body); |
|
659 | - return $response['statusCode'] == $expected; |
|
660 | - } catch (ClientHttpException $e) { |
|
661 | - if ($e->getHttpStatus() === 404 && $method === 'DELETE') { |
|
662 | - $this->statCache->clear($path . '/'); |
|
663 | - $this->statCache->set($path, false); |
|
664 | - return false; |
|
665 | - } |
|
666 | - |
|
667 | - $this->convertException($e, $path); |
|
668 | - } catch (\Exception $e) { |
|
669 | - $this->convertException($e, $path); |
|
670 | - } |
|
671 | - return false; |
|
672 | - } |
|
673 | - |
|
674 | - /** |
|
675 | - * check if curl is installed |
|
676 | - */ |
|
677 | - public static function checkDependencies() { |
|
678 | - return true; |
|
679 | - } |
|
680 | - |
|
681 | - /** {@inheritdoc} */ |
|
682 | - public function isUpdatable($path) { |
|
683 | - return (bool)($this->getPermissions($path) & Constants::PERMISSION_UPDATE); |
|
684 | - } |
|
685 | - |
|
686 | - /** {@inheritdoc} */ |
|
687 | - public function isCreatable($path) { |
|
688 | - return (bool)($this->getPermissions($path) & Constants::PERMISSION_CREATE); |
|
689 | - } |
|
690 | - |
|
691 | - /** {@inheritdoc} */ |
|
692 | - public function isSharable($path) { |
|
693 | - return (bool)($this->getPermissions($path) & Constants::PERMISSION_SHARE); |
|
694 | - } |
|
695 | - |
|
696 | - /** {@inheritdoc} */ |
|
697 | - public function isDeletable($path) { |
|
698 | - return (bool)($this->getPermissions($path) & Constants::PERMISSION_DELETE); |
|
699 | - } |
|
700 | - |
|
701 | - /** {@inheritdoc} */ |
|
702 | - public function getPermissions($path) { |
|
703 | - $this->init(); |
|
704 | - $path = $this->cleanPath($path); |
|
705 | - $response = $this->propfind($path); |
|
706 | - if ($response === false) { |
|
707 | - return 0; |
|
708 | - } |
|
709 | - if (isset($response['{http://owncloud.org/ns}permissions'])) { |
|
710 | - return $this->parsePermissions($response['{http://owncloud.org/ns}permissions']); |
|
711 | - } else if ($this->is_dir($path)) { |
|
712 | - return Constants::PERMISSION_ALL; |
|
713 | - } else if ($this->file_exists($path)) { |
|
714 | - return Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE; |
|
715 | - } else { |
|
716 | - return 0; |
|
717 | - } |
|
718 | - } |
|
719 | - |
|
720 | - /** {@inheritdoc} */ |
|
721 | - public function getETag($path) { |
|
722 | - $this->init(); |
|
723 | - $path = $this->cleanPath($path); |
|
724 | - $response = $this->propfind($path); |
|
725 | - if ($response === false) { |
|
726 | - return null; |
|
727 | - } |
|
728 | - if (isset($response['{DAV:}getetag'])) { |
|
729 | - return trim($response['{DAV:}getetag'], '"'); |
|
730 | - } |
|
731 | - return parent::getEtag($path); |
|
732 | - } |
|
733 | - |
|
734 | - /** |
|
735 | - * @param string $permissionsString |
|
736 | - * @return int |
|
737 | - */ |
|
738 | - protected function parsePermissions($permissionsString) { |
|
739 | - $permissions = Constants::PERMISSION_READ; |
|
740 | - if (strpos($permissionsString, 'R') !== false) { |
|
741 | - $permissions |= Constants::PERMISSION_SHARE; |
|
742 | - } |
|
743 | - if (strpos($permissionsString, 'D') !== false) { |
|
744 | - $permissions |= Constants::PERMISSION_DELETE; |
|
745 | - } |
|
746 | - if (strpos($permissionsString, 'W') !== false) { |
|
747 | - $permissions |= Constants::PERMISSION_UPDATE; |
|
748 | - } |
|
749 | - if (strpos($permissionsString, 'CK') !== false) { |
|
750 | - $permissions |= Constants::PERMISSION_CREATE; |
|
751 | - $permissions |= Constants::PERMISSION_UPDATE; |
|
752 | - } |
|
753 | - return $permissions; |
|
754 | - } |
|
755 | - |
|
756 | - /** |
|
757 | - * check if a file or folder has been updated since $time |
|
758 | - * |
|
759 | - * @param string $path |
|
760 | - * @param int $time |
|
761 | - * @throws \OCP\Files\StorageNotAvailableException |
|
762 | - * @return bool |
|
763 | - */ |
|
764 | - public function hasUpdated($path, $time) { |
|
765 | - $this->init(); |
|
766 | - $path = $this->cleanPath($path); |
|
767 | - try { |
|
768 | - // force refresh for $path |
|
769 | - $this->statCache->remove($path); |
|
770 | - $response = $this->propfind($path); |
|
771 | - if ($response === false) { |
|
772 | - if ($path === '') { |
|
773 | - // if root is gone it means the storage is not available |
|
774 | - throw new StorageNotAvailableException('root is gone'); |
|
775 | - } |
|
776 | - return false; |
|
777 | - } |
|
778 | - if (isset($response['{DAV:}getetag'])) { |
|
779 | - $cachedData = $this->getCache()->get($path); |
|
780 | - $etag = null; |
|
781 | - if (isset($response['{DAV:}getetag'])) { |
|
782 | - $etag = trim($response['{DAV:}getetag'], '"'); |
|
783 | - } |
|
784 | - if (!empty($etag) && $cachedData['etag'] !== $etag) { |
|
785 | - return true; |
|
786 | - } else if (isset($response['{http://open-collaboration-services.org/ns}share-permissions'])) { |
|
787 | - $sharePermissions = (int)$response['{http://open-collaboration-services.org/ns}share-permissions']; |
|
788 | - return $sharePermissions !== $cachedData['permissions']; |
|
789 | - } else if (isset($response['{http://owncloud.org/ns}permissions'])) { |
|
790 | - $permissions = $this->parsePermissions($response['{http://owncloud.org/ns}permissions']); |
|
791 | - return $permissions !== $cachedData['permissions']; |
|
792 | - } else { |
|
793 | - return false; |
|
794 | - } |
|
795 | - } else { |
|
796 | - $remoteMtime = strtotime($response['{DAV:}getlastmodified']); |
|
797 | - return $remoteMtime > $time; |
|
798 | - } |
|
799 | - } catch (ClientHttpException $e) { |
|
800 | - if ($e->getHttpStatus() === 405) { |
|
801 | - if ($path === '') { |
|
802 | - // if root is gone it means the storage is not available |
|
803 | - throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
804 | - } |
|
805 | - return false; |
|
806 | - } |
|
807 | - $this->convertException($e, $path); |
|
808 | - return false; |
|
809 | - } catch (\Exception $e) { |
|
810 | - $this->convertException($e, $path); |
|
811 | - return false; |
|
812 | - } |
|
813 | - } |
|
814 | - |
|
815 | - /** |
|
816 | - * Interpret the given exception and decide whether it is due to an |
|
817 | - * unavailable storage, invalid storage or other. |
|
818 | - * This will either throw StorageInvalidException, StorageNotAvailableException |
|
819 | - * or do nothing. |
|
820 | - * |
|
821 | - * @param Exception $e sabre exception |
|
822 | - * @param string $path optional path from the operation |
|
823 | - * |
|
824 | - * @throws StorageInvalidException if the storage is invalid, for example |
|
825 | - * when the authentication expired or is invalid |
|
826 | - * @throws StorageNotAvailableException if the storage is not available, |
|
827 | - * which might be temporary |
|
828 | - */ |
|
829 | - protected function convertException(Exception $e, $path = '') { |
|
830 | - \OC::$server->getLogger()->logException($e, ['app' => 'files_external']); |
|
831 | - if ($e instanceof ClientHttpException) { |
|
832 | - if ($e->getHttpStatus() === Http::STATUS_LOCKED) { |
|
833 | - throw new \OCP\Lock\LockedException($path); |
|
834 | - } |
|
835 | - if ($e->getHttpStatus() === Http::STATUS_UNAUTHORIZED) { |
|
836 | - // either password was changed or was invalid all along |
|
837 | - throw new StorageInvalidException(get_class($e) . ': ' . $e->getMessage()); |
|
838 | - } else if ($e->getHttpStatus() === Http::STATUS_METHOD_NOT_ALLOWED) { |
|
839 | - // ignore exception for MethodNotAllowed, false will be returned |
|
840 | - return; |
|
841 | - } |
|
842 | - throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
843 | - } else if ($e instanceof ClientException) { |
|
844 | - // connection timeout or refused, server could be temporarily down |
|
845 | - throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
846 | - } else if ($e instanceof \InvalidArgumentException) { |
|
847 | - // parse error because the server returned HTML instead of XML, |
|
848 | - // possibly temporarily down |
|
849 | - throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
850 | - } else if (($e instanceof StorageNotAvailableException) || ($e instanceof StorageInvalidException)) { |
|
851 | - // rethrow |
|
852 | - throw $e; |
|
853 | - } |
|
854 | - |
|
855 | - // TODO: only log for now, but in the future need to wrap/rethrow exception |
|
856 | - } |
|
61 | + /** @var string */ |
|
62 | + protected $password; |
|
63 | + /** @var string */ |
|
64 | + protected $user; |
|
65 | + /** @var string */ |
|
66 | + protected $authType; |
|
67 | + /** @var string */ |
|
68 | + protected $host; |
|
69 | + /** @var bool */ |
|
70 | + protected $secure; |
|
71 | + /** @var string */ |
|
72 | + protected $root; |
|
73 | + /** @var string */ |
|
74 | + protected $certPath; |
|
75 | + /** @var bool */ |
|
76 | + protected $ready; |
|
77 | + /** @var Client */ |
|
78 | + protected $client; |
|
79 | + /** @var ArrayCache */ |
|
80 | + protected $statCache; |
|
81 | + /** @var \OCP\Http\Client\IClientService */ |
|
82 | + protected $httpClientService; |
|
83 | + |
|
84 | + /** |
|
85 | + * @param array $params |
|
86 | + * @throws \Exception |
|
87 | + */ |
|
88 | + public function __construct($params) { |
|
89 | + $this->statCache = new ArrayCache(); |
|
90 | + $this->httpClientService = \OC::$server->getHTTPClientService(); |
|
91 | + if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
|
92 | + $host = $params['host']; |
|
93 | + //remove leading http[s], will be generated in createBaseUri() |
|
94 | + if (substr($host, 0, 8) == "https://") $host = substr($host, 8); |
|
95 | + else if (substr($host, 0, 7) == "http://") $host = substr($host, 7); |
|
96 | + $this->host = $host; |
|
97 | + $this->user = $params['user']; |
|
98 | + $this->password = $params['password']; |
|
99 | + if (isset($params['authType'])) { |
|
100 | + $this->authType = $params['authType']; |
|
101 | + } |
|
102 | + if (isset($params['secure'])) { |
|
103 | + if (is_string($params['secure'])) { |
|
104 | + $this->secure = ($params['secure'] === 'true'); |
|
105 | + } else { |
|
106 | + $this->secure = (bool)$params['secure']; |
|
107 | + } |
|
108 | + } else { |
|
109 | + $this->secure = false; |
|
110 | + } |
|
111 | + if ($this->secure === true) { |
|
112 | + // inject mock for testing |
|
113 | + $certManager = \OC::$server->getCertificateManager(); |
|
114 | + if (is_null($certManager)) { //no user |
|
115 | + $certManager = \OC::$server->getCertificateManager(null); |
|
116 | + } |
|
117 | + $certPath = $certManager->getAbsoluteBundlePath(); |
|
118 | + if (file_exists($certPath)) { |
|
119 | + $this->certPath = $certPath; |
|
120 | + } |
|
121 | + } |
|
122 | + $this->root = $params['root'] ?? '/'; |
|
123 | + $this->root = '/' . ltrim($this->root, '/'); |
|
124 | + $this->root = rtrim($this->root, '/') . '/'; |
|
125 | + } else { |
|
126 | + throw new \Exception('Invalid webdav storage configuration'); |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + protected function init() { |
|
131 | + if ($this->ready) { |
|
132 | + return; |
|
133 | + } |
|
134 | + $this->ready = true; |
|
135 | + |
|
136 | + $settings = [ |
|
137 | + 'baseUri' => $this->createBaseUri(), |
|
138 | + 'userName' => $this->user, |
|
139 | + 'password' => $this->password, |
|
140 | + ]; |
|
141 | + if (isset($this->authType)) { |
|
142 | + $settings['authType'] = $this->authType; |
|
143 | + } |
|
144 | + |
|
145 | + $proxy = \OC::$server->getConfig()->getSystemValue('proxy', ''); |
|
146 | + if ($proxy !== '') { |
|
147 | + $settings['proxy'] = $proxy; |
|
148 | + } |
|
149 | + |
|
150 | + $this->client = new Client($settings); |
|
151 | + $this->client->setThrowExceptions(true); |
|
152 | + if ($this->secure === true && $this->certPath) { |
|
153 | + $this->client->addCurlSetting(CURLOPT_CAINFO, $this->certPath); |
|
154 | + } |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Clear the stat cache |
|
159 | + */ |
|
160 | + public function clearStatCache() { |
|
161 | + $this->statCache->clear(); |
|
162 | + } |
|
163 | + |
|
164 | + /** {@inheritdoc} */ |
|
165 | + public function getId() { |
|
166 | + return 'webdav::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
167 | + } |
|
168 | + |
|
169 | + /** {@inheritdoc} */ |
|
170 | + public function createBaseUri() { |
|
171 | + $baseUri = 'http'; |
|
172 | + if ($this->secure) { |
|
173 | + $baseUri .= 's'; |
|
174 | + } |
|
175 | + $baseUri .= '://' . $this->host . $this->root; |
|
176 | + return $baseUri; |
|
177 | + } |
|
178 | + |
|
179 | + /** {@inheritdoc} */ |
|
180 | + public function mkdir($path) { |
|
181 | + $this->init(); |
|
182 | + $path = $this->cleanPath($path); |
|
183 | + $result = $this->simpleResponse('MKCOL', $path, null, 201); |
|
184 | + if ($result) { |
|
185 | + $this->statCache->set($path, true); |
|
186 | + } |
|
187 | + return $result; |
|
188 | + } |
|
189 | + |
|
190 | + /** {@inheritdoc} */ |
|
191 | + public function rmdir($path) { |
|
192 | + $this->init(); |
|
193 | + $path = $this->cleanPath($path); |
|
194 | + // FIXME: some WebDAV impl return 403 when trying to DELETE |
|
195 | + // a non-empty folder |
|
196 | + $result = $this->simpleResponse('DELETE', $path . '/', null, 204); |
|
197 | + $this->statCache->clear($path . '/'); |
|
198 | + $this->statCache->remove($path); |
|
199 | + return $result; |
|
200 | + } |
|
201 | + |
|
202 | + /** {@inheritdoc} */ |
|
203 | + public function opendir($path) { |
|
204 | + $this->init(); |
|
205 | + $path = $this->cleanPath($path); |
|
206 | + try { |
|
207 | + $response = $this->client->propFind( |
|
208 | + $this->encodePath($path), |
|
209 | + ['{DAV:}href'], |
|
210 | + 1 |
|
211 | + ); |
|
212 | + if ($response === false) { |
|
213 | + return false; |
|
214 | + } |
|
215 | + $content = []; |
|
216 | + $files = array_keys($response); |
|
217 | + array_shift($files); //the first entry is the current directory |
|
218 | + |
|
219 | + if (!$this->statCache->hasKey($path)) { |
|
220 | + $this->statCache->set($path, true); |
|
221 | + } |
|
222 | + foreach ($files as $file) { |
|
223 | + $file = urldecode($file); |
|
224 | + // do not store the real entry, we might not have all properties |
|
225 | + if (!$this->statCache->hasKey($path)) { |
|
226 | + $this->statCache->set($file, true); |
|
227 | + } |
|
228 | + $file = basename($file); |
|
229 | + $content[] = $file; |
|
230 | + } |
|
231 | + return IteratorDirectory::wrap($content); |
|
232 | + } catch (\Exception $e) { |
|
233 | + $this->convertException($e, $path); |
|
234 | + } |
|
235 | + return false; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Propfind call with cache handling. |
|
240 | + * |
|
241 | + * First checks if information is cached. |
|
242 | + * If not, request it from the server then store to cache. |
|
243 | + * |
|
244 | + * @param string $path path to propfind |
|
245 | + * |
|
246 | + * @return array|boolean propfind response or false if the entry was not found |
|
247 | + * |
|
248 | + * @throws ClientHttpException |
|
249 | + */ |
|
250 | + protected function propfind($path) { |
|
251 | + $path = $this->cleanPath($path); |
|
252 | + $cachedResponse = $this->statCache->get($path); |
|
253 | + // we either don't know it, or we know it exists but need more details |
|
254 | + if (is_null($cachedResponse) || $cachedResponse === true) { |
|
255 | + $this->init(); |
|
256 | + try { |
|
257 | + $response = $this->client->propFind( |
|
258 | + $this->encodePath($path), |
|
259 | + array( |
|
260 | + '{DAV:}getlastmodified', |
|
261 | + '{DAV:}getcontentlength', |
|
262 | + '{DAV:}getcontenttype', |
|
263 | + '{http://owncloud.org/ns}permissions', |
|
264 | + '{http://open-collaboration-services.org/ns}share-permissions', |
|
265 | + '{DAV:}resourcetype', |
|
266 | + '{DAV:}getetag', |
|
267 | + ) |
|
268 | + ); |
|
269 | + $this->statCache->set($path, $response); |
|
270 | + } catch (ClientHttpException $e) { |
|
271 | + if ($e->getHttpStatus() === 404) { |
|
272 | + $this->statCache->clear($path . '/'); |
|
273 | + $this->statCache->set($path, false); |
|
274 | + return false; |
|
275 | + } |
|
276 | + $this->convertException($e, $path); |
|
277 | + } catch (\Exception $e) { |
|
278 | + $this->convertException($e, $path); |
|
279 | + } |
|
280 | + } else { |
|
281 | + $response = $cachedResponse; |
|
282 | + } |
|
283 | + return $response; |
|
284 | + } |
|
285 | + |
|
286 | + /** {@inheritdoc} */ |
|
287 | + public function filetype($path) { |
|
288 | + try { |
|
289 | + $response = $this->propfind($path); |
|
290 | + if ($response === false) { |
|
291 | + return false; |
|
292 | + } |
|
293 | + $responseType = []; |
|
294 | + if (isset($response["{DAV:}resourcetype"])) { |
|
295 | + /** @var ResourceType[] $response */ |
|
296 | + $responseType = $response["{DAV:}resourcetype"]->getValue(); |
|
297 | + } |
|
298 | + return (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file'; |
|
299 | + } catch (\Exception $e) { |
|
300 | + $this->convertException($e, $path); |
|
301 | + } |
|
302 | + return false; |
|
303 | + } |
|
304 | + |
|
305 | + /** {@inheritdoc} */ |
|
306 | + public function file_exists($path) { |
|
307 | + try { |
|
308 | + $path = $this->cleanPath($path); |
|
309 | + $cachedState = $this->statCache->get($path); |
|
310 | + if ($cachedState === false) { |
|
311 | + // we know the file doesn't exist |
|
312 | + return false; |
|
313 | + } else if (!is_null($cachedState)) { |
|
314 | + return true; |
|
315 | + } |
|
316 | + // need to get from server |
|
317 | + return ($this->propfind($path) !== false); |
|
318 | + } catch (\Exception $e) { |
|
319 | + $this->convertException($e, $path); |
|
320 | + } |
|
321 | + return false; |
|
322 | + } |
|
323 | + |
|
324 | + /** {@inheritdoc} */ |
|
325 | + public function unlink($path) { |
|
326 | + $this->init(); |
|
327 | + $path = $this->cleanPath($path); |
|
328 | + $result = $this->simpleResponse('DELETE', $path, null, 204); |
|
329 | + $this->statCache->clear($path . '/'); |
|
330 | + $this->statCache->remove($path); |
|
331 | + return $result; |
|
332 | + } |
|
333 | + |
|
334 | + /** {@inheritdoc} */ |
|
335 | + public function fopen($path, $mode) { |
|
336 | + $this->init(); |
|
337 | + $path = $this->cleanPath($path); |
|
338 | + switch ($mode) { |
|
339 | + case 'r': |
|
340 | + case 'rb': |
|
341 | + try { |
|
342 | + $response = $this->httpClientService |
|
343 | + ->newClient() |
|
344 | + ->get($this->createBaseUri() . $this->encodePath($path), [ |
|
345 | + 'auth' => [$this->user, $this->password], |
|
346 | + 'stream' => true |
|
347 | + ]); |
|
348 | + } catch (\GuzzleHttp\Exception\ClientException $e) { |
|
349 | + if ($e->getResponse() instanceof ResponseInterface |
|
350 | + && $e->getResponse()->getStatusCode() === 404) { |
|
351 | + return false; |
|
352 | + } else { |
|
353 | + throw $e; |
|
354 | + } |
|
355 | + } |
|
356 | + |
|
357 | + if ($response->getStatusCode() !== Http::STATUS_OK) { |
|
358 | + if ($response->getStatusCode() === Http::STATUS_LOCKED) { |
|
359 | + throw new \OCP\Lock\LockedException($path); |
|
360 | + } else { |
|
361 | + Util::writeLog("webdav client", 'Guzzle get returned status code ' . $response->getStatusCode(), ILogger::ERROR); |
|
362 | + } |
|
363 | + } |
|
364 | + |
|
365 | + return $response->getBody(); |
|
366 | + case 'w': |
|
367 | + case 'wb': |
|
368 | + case 'a': |
|
369 | + case 'ab': |
|
370 | + case 'r+': |
|
371 | + case 'w+': |
|
372 | + case 'wb+': |
|
373 | + case 'a+': |
|
374 | + case 'x': |
|
375 | + case 'x+': |
|
376 | + case 'c': |
|
377 | + case 'c+': |
|
378 | + //emulate these |
|
379 | + $tempManager = \OC::$server->getTempManager(); |
|
380 | + if (strrpos($path, '.') !== false) { |
|
381 | + $ext = substr($path, strrpos($path, '.')); |
|
382 | + } else { |
|
383 | + $ext = ''; |
|
384 | + } |
|
385 | + if ($this->file_exists($path)) { |
|
386 | + if (!$this->isUpdatable($path)) { |
|
387 | + return false; |
|
388 | + } |
|
389 | + if ($mode === 'w' or $mode === 'w+') { |
|
390 | + $tmpFile = $tempManager->getTemporaryFile($ext); |
|
391 | + } else { |
|
392 | + $tmpFile = $this->getCachedFile($path); |
|
393 | + } |
|
394 | + } else { |
|
395 | + if (!$this->isCreatable(dirname($path))) { |
|
396 | + return false; |
|
397 | + } |
|
398 | + $tmpFile = $tempManager->getTemporaryFile($ext); |
|
399 | + } |
|
400 | + $handle = fopen($tmpFile, $mode); |
|
401 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
402 | + $this->writeBack($tmpFile, $path); |
|
403 | + }); |
|
404 | + } |
|
405 | + } |
|
406 | + |
|
407 | + /** |
|
408 | + * @param string $tmpFile |
|
409 | + */ |
|
410 | + public function writeBack($tmpFile, $path) { |
|
411 | + $this->uploadFile($tmpFile, $path); |
|
412 | + unlink($tmpFile); |
|
413 | + } |
|
414 | + |
|
415 | + /** {@inheritdoc} */ |
|
416 | + public function free_space($path) { |
|
417 | + $this->init(); |
|
418 | + $path = $this->cleanPath($path); |
|
419 | + try { |
|
420 | + // TODO: cacheable ? |
|
421 | + $response = $this->client->propfind($this->encodePath($path), ['{DAV:}quota-available-bytes']); |
|
422 | + if ($response === false) { |
|
423 | + return FileInfo::SPACE_UNKNOWN; |
|
424 | + } |
|
425 | + if (isset($response['{DAV:}quota-available-bytes'])) { |
|
426 | + return (int)$response['{DAV:}quota-available-bytes']; |
|
427 | + } else { |
|
428 | + return FileInfo::SPACE_UNKNOWN; |
|
429 | + } |
|
430 | + } catch (\Exception $e) { |
|
431 | + return FileInfo::SPACE_UNKNOWN; |
|
432 | + } |
|
433 | + } |
|
434 | + |
|
435 | + /** {@inheritdoc} */ |
|
436 | + public function touch($path, $mtime = null) { |
|
437 | + $this->init(); |
|
438 | + if (is_null($mtime)) { |
|
439 | + $mtime = time(); |
|
440 | + } |
|
441 | + $path = $this->cleanPath($path); |
|
442 | + |
|
443 | + // if file exists, update the mtime, else create a new empty file |
|
444 | + if ($this->file_exists($path)) { |
|
445 | + try { |
|
446 | + $this->statCache->remove($path); |
|
447 | + $this->client->proppatch($this->encodePath($path), ['{DAV:}lastmodified' => $mtime]); |
|
448 | + // non-owncloud clients might not have accepted the property, need to recheck it |
|
449 | + $response = $this->client->propfind($this->encodePath($path), ['{DAV:}getlastmodified'], 0); |
|
450 | + if ($response === false) { |
|
451 | + return false; |
|
452 | + } |
|
453 | + if (isset($response['{DAV:}getlastmodified'])) { |
|
454 | + $remoteMtime = strtotime($response['{DAV:}getlastmodified']); |
|
455 | + if ($remoteMtime !== $mtime) { |
|
456 | + // server has not accepted the mtime |
|
457 | + return false; |
|
458 | + } |
|
459 | + } |
|
460 | + } catch (ClientHttpException $e) { |
|
461 | + if ($e->getHttpStatus() === 501) { |
|
462 | + return false; |
|
463 | + } |
|
464 | + $this->convertException($e, $path); |
|
465 | + return false; |
|
466 | + } catch (\Exception $e) { |
|
467 | + $this->convertException($e, $path); |
|
468 | + return false; |
|
469 | + } |
|
470 | + } else { |
|
471 | + $this->file_put_contents($path, ''); |
|
472 | + } |
|
473 | + return true; |
|
474 | + } |
|
475 | + |
|
476 | + /** |
|
477 | + * @param string $path |
|
478 | + * @param string $data |
|
479 | + * @return int |
|
480 | + */ |
|
481 | + public function file_put_contents($path, $data) { |
|
482 | + $path = $this->cleanPath($path); |
|
483 | + $result = parent::file_put_contents($path, $data); |
|
484 | + $this->statCache->remove($path); |
|
485 | + return $result; |
|
486 | + } |
|
487 | + |
|
488 | + /** |
|
489 | + * @param string $path |
|
490 | + * @param string $target |
|
491 | + */ |
|
492 | + protected function uploadFile($path, $target) { |
|
493 | + $this->init(); |
|
494 | + |
|
495 | + // invalidate |
|
496 | + $target = $this->cleanPath($target); |
|
497 | + $this->statCache->remove($target); |
|
498 | + $source = fopen($path, 'r'); |
|
499 | + |
|
500 | + $this->httpClientService |
|
501 | + ->newClient() |
|
502 | + ->put($this->createBaseUri() . $this->encodePath($target), [ |
|
503 | + 'body' => $source, |
|
504 | + 'auth' => [$this->user, $this->password] |
|
505 | + ]); |
|
506 | + |
|
507 | + $this->removeCachedFile($target); |
|
508 | + } |
|
509 | + |
|
510 | + /** {@inheritdoc} */ |
|
511 | + public function rename($path1, $path2) { |
|
512 | + $this->init(); |
|
513 | + $path1 = $this->cleanPath($path1); |
|
514 | + $path2 = $this->cleanPath($path2); |
|
515 | + try { |
|
516 | + // overwrite directory ? |
|
517 | + if ($this->is_dir($path2)) { |
|
518 | + // needs trailing slash in destination |
|
519 | + $path2 = rtrim($path2, '/') . '/'; |
|
520 | + } |
|
521 | + $this->client->request( |
|
522 | + 'MOVE', |
|
523 | + $this->encodePath($path1), |
|
524 | + null, |
|
525 | + [ |
|
526 | + 'Destination' => $this->createBaseUri() . $this->encodePath($path2), |
|
527 | + ] |
|
528 | + ); |
|
529 | + $this->statCache->clear($path1 . '/'); |
|
530 | + $this->statCache->clear($path2 . '/'); |
|
531 | + $this->statCache->set($path1, false); |
|
532 | + $this->statCache->set($path2, true); |
|
533 | + $this->removeCachedFile($path1); |
|
534 | + $this->removeCachedFile($path2); |
|
535 | + return true; |
|
536 | + } catch (\Exception $e) { |
|
537 | + $this->convertException($e); |
|
538 | + } |
|
539 | + return false; |
|
540 | + } |
|
541 | + |
|
542 | + /** {@inheritdoc} */ |
|
543 | + public function copy($path1, $path2) { |
|
544 | + $this->init(); |
|
545 | + $path1 = $this->cleanPath($path1); |
|
546 | + $path2 = $this->cleanPath($path2); |
|
547 | + try { |
|
548 | + // overwrite directory ? |
|
549 | + if ($this->is_dir($path2)) { |
|
550 | + // needs trailing slash in destination |
|
551 | + $path2 = rtrim($path2, '/') . '/'; |
|
552 | + } |
|
553 | + $this->client->request( |
|
554 | + 'COPY', |
|
555 | + $this->encodePath($path1), |
|
556 | + null, |
|
557 | + [ |
|
558 | + 'Destination' => $this->createBaseUri() . $this->encodePath($path2), |
|
559 | + ] |
|
560 | + ); |
|
561 | + $this->statCache->clear($path2 . '/'); |
|
562 | + $this->statCache->set($path2, true); |
|
563 | + $this->removeCachedFile($path2); |
|
564 | + return true; |
|
565 | + } catch (\Exception $e) { |
|
566 | + $this->convertException($e); |
|
567 | + } |
|
568 | + return false; |
|
569 | + } |
|
570 | + |
|
571 | + /** {@inheritdoc} */ |
|
572 | + public function stat($path) { |
|
573 | + try { |
|
574 | + $response = $this->propfind($path); |
|
575 | + if (!$response) { |
|
576 | + return false; |
|
577 | + } |
|
578 | + return [ |
|
579 | + 'mtime' => strtotime($response['{DAV:}getlastmodified']), |
|
580 | + 'size' => (int)isset($response['{DAV:}getcontentlength']) ? $response['{DAV:}getcontentlength'] : 0, |
|
581 | + ]; |
|
582 | + } catch (\Exception $e) { |
|
583 | + $this->convertException($e, $path); |
|
584 | + } |
|
585 | + return array(); |
|
586 | + } |
|
587 | + |
|
588 | + /** {@inheritdoc} */ |
|
589 | + public function getMimeType($path) { |
|
590 | + $remoteMimetype = $this->getMimeTypeFromRemote($path); |
|
591 | + if ($remoteMimetype === 'application/octet-stream') { |
|
592 | + return \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
593 | + } else { |
|
594 | + return $remoteMimetype; |
|
595 | + } |
|
596 | + } |
|
597 | + |
|
598 | + public function getMimeTypeFromRemote($path) { |
|
599 | + try { |
|
600 | + $response = $this->propfind($path); |
|
601 | + if ($response === false) { |
|
602 | + return false; |
|
603 | + } |
|
604 | + $responseType = []; |
|
605 | + if (isset($response["{DAV:}resourcetype"])) { |
|
606 | + /** @var ResourceType[] $response */ |
|
607 | + $responseType = $response["{DAV:}resourcetype"]->getValue(); |
|
608 | + } |
|
609 | + $type = (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file'; |
|
610 | + if ($type == 'dir') { |
|
611 | + return 'httpd/unix-directory'; |
|
612 | + } elseif (isset($response['{DAV:}getcontenttype'])) { |
|
613 | + return $response['{DAV:}getcontenttype']; |
|
614 | + } else { |
|
615 | + return 'application/octet-stream'; |
|
616 | + } |
|
617 | + } catch (\Exception $e) { |
|
618 | + return false; |
|
619 | + } |
|
620 | + } |
|
621 | + |
|
622 | + /** |
|
623 | + * @param string $path |
|
624 | + * @return string |
|
625 | + */ |
|
626 | + public function cleanPath($path) { |
|
627 | + if ($path === '') { |
|
628 | + return $path; |
|
629 | + } |
|
630 | + $path = Filesystem::normalizePath($path); |
|
631 | + // remove leading slash |
|
632 | + return substr($path, 1); |
|
633 | + } |
|
634 | + |
|
635 | + /** |
|
636 | + * URL encodes the given path but keeps the slashes |
|
637 | + * |
|
638 | + * @param string $path to encode |
|
639 | + * @return string encoded path |
|
640 | + */ |
|
641 | + protected function encodePath($path) { |
|
642 | + // slashes need to stay |
|
643 | + return str_replace('%2F', '/', rawurlencode($path)); |
|
644 | + } |
|
645 | + |
|
646 | + /** |
|
647 | + * @param string $method |
|
648 | + * @param string $path |
|
649 | + * @param string|resource|null $body |
|
650 | + * @param int $expected |
|
651 | + * @return bool |
|
652 | + * @throws StorageInvalidException |
|
653 | + * @throws StorageNotAvailableException |
|
654 | + */ |
|
655 | + protected function simpleResponse($method, $path, $body, $expected) { |
|
656 | + $path = $this->cleanPath($path); |
|
657 | + try { |
|
658 | + $response = $this->client->request($method, $this->encodePath($path), $body); |
|
659 | + return $response['statusCode'] == $expected; |
|
660 | + } catch (ClientHttpException $e) { |
|
661 | + if ($e->getHttpStatus() === 404 && $method === 'DELETE') { |
|
662 | + $this->statCache->clear($path . '/'); |
|
663 | + $this->statCache->set($path, false); |
|
664 | + return false; |
|
665 | + } |
|
666 | + |
|
667 | + $this->convertException($e, $path); |
|
668 | + } catch (\Exception $e) { |
|
669 | + $this->convertException($e, $path); |
|
670 | + } |
|
671 | + return false; |
|
672 | + } |
|
673 | + |
|
674 | + /** |
|
675 | + * check if curl is installed |
|
676 | + */ |
|
677 | + public static function checkDependencies() { |
|
678 | + return true; |
|
679 | + } |
|
680 | + |
|
681 | + /** {@inheritdoc} */ |
|
682 | + public function isUpdatable($path) { |
|
683 | + return (bool)($this->getPermissions($path) & Constants::PERMISSION_UPDATE); |
|
684 | + } |
|
685 | + |
|
686 | + /** {@inheritdoc} */ |
|
687 | + public function isCreatable($path) { |
|
688 | + return (bool)($this->getPermissions($path) & Constants::PERMISSION_CREATE); |
|
689 | + } |
|
690 | + |
|
691 | + /** {@inheritdoc} */ |
|
692 | + public function isSharable($path) { |
|
693 | + return (bool)($this->getPermissions($path) & Constants::PERMISSION_SHARE); |
|
694 | + } |
|
695 | + |
|
696 | + /** {@inheritdoc} */ |
|
697 | + public function isDeletable($path) { |
|
698 | + return (bool)($this->getPermissions($path) & Constants::PERMISSION_DELETE); |
|
699 | + } |
|
700 | + |
|
701 | + /** {@inheritdoc} */ |
|
702 | + public function getPermissions($path) { |
|
703 | + $this->init(); |
|
704 | + $path = $this->cleanPath($path); |
|
705 | + $response = $this->propfind($path); |
|
706 | + if ($response === false) { |
|
707 | + return 0; |
|
708 | + } |
|
709 | + if (isset($response['{http://owncloud.org/ns}permissions'])) { |
|
710 | + return $this->parsePermissions($response['{http://owncloud.org/ns}permissions']); |
|
711 | + } else if ($this->is_dir($path)) { |
|
712 | + return Constants::PERMISSION_ALL; |
|
713 | + } else if ($this->file_exists($path)) { |
|
714 | + return Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE; |
|
715 | + } else { |
|
716 | + return 0; |
|
717 | + } |
|
718 | + } |
|
719 | + |
|
720 | + /** {@inheritdoc} */ |
|
721 | + public function getETag($path) { |
|
722 | + $this->init(); |
|
723 | + $path = $this->cleanPath($path); |
|
724 | + $response = $this->propfind($path); |
|
725 | + if ($response === false) { |
|
726 | + return null; |
|
727 | + } |
|
728 | + if (isset($response['{DAV:}getetag'])) { |
|
729 | + return trim($response['{DAV:}getetag'], '"'); |
|
730 | + } |
|
731 | + return parent::getEtag($path); |
|
732 | + } |
|
733 | + |
|
734 | + /** |
|
735 | + * @param string $permissionsString |
|
736 | + * @return int |
|
737 | + */ |
|
738 | + protected function parsePermissions($permissionsString) { |
|
739 | + $permissions = Constants::PERMISSION_READ; |
|
740 | + if (strpos($permissionsString, 'R') !== false) { |
|
741 | + $permissions |= Constants::PERMISSION_SHARE; |
|
742 | + } |
|
743 | + if (strpos($permissionsString, 'D') !== false) { |
|
744 | + $permissions |= Constants::PERMISSION_DELETE; |
|
745 | + } |
|
746 | + if (strpos($permissionsString, 'W') !== false) { |
|
747 | + $permissions |= Constants::PERMISSION_UPDATE; |
|
748 | + } |
|
749 | + if (strpos($permissionsString, 'CK') !== false) { |
|
750 | + $permissions |= Constants::PERMISSION_CREATE; |
|
751 | + $permissions |= Constants::PERMISSION_UPDATE; |
|
752 | + } |
|
753 | + return $permissions; |
|
754 | + } |
|
755 | + |
|
756 | + /** |
|
757 | + * check if a file or folder has been updated since $time |
|
758 | + * |
|
759 | + * @param string $path |
|
760 | + * @param int $time |
|
761 | + * @throws \OCP\Files\StorageNotAvailableException |
|
762 | + * @return bool |
|
763 | + */ |
|
764 | + public function hasUpdated($path, $time) { |
|
765 | + $this->init(); |
|
766 | + $path = $this->cleanPath($path); |
|
767 | + try { |
|
768 | + // force refresh for $path |
|
769 | + $this->statCache->remove($path); |
|
770 | + $response = $this->propfind($path); |
|
771 | + if ($response === false) { |
|
772 | + if ($path === '') { |
|
773 | + // if root is gone it means the storage is not available |
|
774 | + throw new StorageNotAvailableException('root is gone'); |
|
775 | + } |
|
776 | + return false; |
|
777 | + } |
|
778 | + if (isset($response['{DAV:}getetag'])) { |
|
779 | + $cachedData = $this->getCache()->get($path); |
|
780 | + $etag = null; |
|
781 | + if (isset($response['{DAV:}getetag'])) { |
|
782 | + $etag = trim($response['{DAV:}getetag'], '"'); |
|
783 | + } |
|
784 | + if (!empty($etag) && $cachedData['etag'] !== $etag) { |
|
785 | + return true; |
|
786 | + } else if (isset($response['{http://open-collaboration-services.org/ns}share-permissions'])) { |
|
787 | + $sharePermissions = (int)$response['{http://open-collaboration-services.org/ns}share-permissions']; |
|
788 | + return $sharePermissions !== $cachedData['permissions']; |
|
789 | + } else if (isset($response['{http://owncloud.org/ns}permissions'])) { |
|
790 | + $permissions = $this->parsePermissions($response['{http://owncloud.org/ns}permissions']); |
|
791 | + return $permissions !== $cachedData['permissions']; |
|
792 | + } else { |
|
793 | + return false; |
|
794 | + } |
|
795 | + } else { |
|
796 | + $remoteMtime = strtotime($response['{DAV:}getlastmodified']); |
|
797 | + return $remoteMtime > $time; |
|
798 | + } |
|
799 | + } catch (ClientHttpException $e) { |
|
800 | + if ($e->getHttpStatus() === 405) { |
|
801 | + if ($path === '') { |
|
802 | + // if root is gone it means the storage is not available |
|
803 | + throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
804 | + } |
|
805 | + return false; |
|
806 | + } |
|
807 | + $this->convertException($e, $path); |
|
808 | + return false; |
|
809 | + } catch (\Exception $e) { |
|
810 | + $this->convertException($e, $path); |
|
811 | + return false; |
|
812 | + } |
|
813 | + } |
|
814 | + |
|
815 | + /** |
|
816 | + * Interpret the given exception and decide whether it is due to an |
|
817 | + * unavailable storage, invalid storage or other. |
|
818 | + * This will either throw StorageInvalidException, StorageNotAvailableException |
|
819 | + * or do nothing. |
|
820 | + * |
|
821 | + * @param Exception $e sabre exception |
|
822 | + * @param string $path optional path from the operation |
|
823 | + * |
|
824 | + * @throws StorageInvalidException if the storage is invalid, for example |
|
825 | + * when the authentication expired or is invalid |
|
826 | + * @throws StorageNotAvailableException if the storage is not available, |
|
827 | + * which might be temporary |
|
828 | + */ |
|
829 | + protected function convertException(Exception $e, $path = '') { |
|
830 | + \OC::$server->getLogger()->logException($e, ['app' => 'files_external']); |
|
831 | + if ($e instanceof ClientHttpException) { |
|
832 | + if ($e->getHttpStatus() === Http::STATUS_LOCKED) { |
|
833 | + throw new \OCP\Lock\LockedException($path); |
|
834 | + } |
|
835 | + if ($e->getHttpStatus() === Http::STATUS_UNAUTHORIZED) { |
|
836 | + // either password was changed or was invalid all along |
|
837 | + throw new StorageInvalidException(get_class($e) . ': ' . $e->getMessage()); |
|
838 | + } else if ($e->getHttpStatus() === Http::STATUS_METHOD_NOT_ALLOWED) { |
|
839 | + // ignore exception for MethodNotAllowed, false will be returned |
|
840 | + return; |
|
841 | + } |
|
842 | + throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
843 | + } else if ($e instanceof ClientException) { |
|
844 | + // connection timeout or refused, server could be temporarily down |
|
845 | + throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
846 | + } else if ($e instanceof \InvalidArgumentException) { |
|
847 | + // parse error because the server returned HTML instead of XML, |
|
848 | + // possibly temporarily down |
|
849 | + throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
850 | + } else if (($e instanceof StorageNotAvailableException) || ($e instanceof StorageInvalidException)) { |
|
851 | + // rethrow |
|
852 | + throw $e; |
|
853 | + } |
|
854 | + |
|
855 | + // TODO: only log for now, but in the future need to wrap/rethrow exception |
|
856 | + } |
|
857 | 857 | } |
858 | 858 |
@@ -71,744 +71,744 @@ |
||
71 | 71 | */ |
72 | 72 | abstract class Common implements Storage, ILockingStorage { |
73 | 73 | |
74 | - use LocalTempFileTrait; |
|
75 | - |
|
76 | - protected $cache; |
|
77 | - protected $scanner; |
|
78 | - protected $watcher; |
|
79 | - protected $propagator; |
|
80 | - protected $storageCache; |
|
81 | - protected $updater; |
|
82 | - |
|
83 | - protected $mountOptions = []; |
|
84 | - protected $owner = null; |
|
85 | - |
|
86 | - private $shouldLogLocks = null; |
|
87 | - private $logger; |
|
88 | - |
|
89 | - public function __construct($parameters) { |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Remove a file or folder |
|
94 | - * |
|
95 | - * @param string $path |
|
96 | - * @return bool |
|
97 | - */ |
|
98 | - protected function remove($path) { |
|
99 | - if ($this->is_dir($path)) { |
|
100 | - return $this->rmdir($path); |
|
101 | - } else if ($this->is_file($path)) { |
|
102 | - return $this->unlink($path); |
|
103 | - } else { |
|
104 | - return false; |
|
105 | - } |
|
106 | - } |
|
107 | - |
|
108 | - public function is_dir($path) { |
|
109 | - return $this->filetype($path) === 'dir'; |
|
110 | - } |
|
111 | - |
|
112 | - public function is_file($path) { |
|
113 | - return $this->filetype($path) === 'file'; |
|
114 | - } |
|
115 | - |
|
116 | - public function filesize($path) { |
|
117 | - if ($this->is_dir($path)) { |
|
118 | - return 0; //by definition |
|
119 | - } else { |
|
120 | - $stat = $this->stat($path); |
|
121 | - if (isset($stat['size'])) { |
|
122 | - return $stat['size']; |
|
123 | - } else { |
|
124 | - return 0; |
|
125 | - } |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - public function isReadable($path) { |
|
130 | - // at least check whether it exists |
|
131 | - // subclasses might want to implement this more thoroughly |
|
132 | - return $this->file_exists($path); |
|
133 | - } |
|
134 | - |
|
135 | - public function isUpdatable($path) { |
|
136 | - // at least check whether it exists |
|
137 | - // subclasses might want to implement this more thoroughly |
|
138 | - // a non-existing file/folder isn't updatable |
|
139 | - return $this->file_exists($path); |
|
140 | - } |
|
141 | - |
|
142 | - public function isCreatable($path) { |
|
143 | - if ($this->is_dir($path) && $this->isUpdatable($path)) { |
|
144 | - return true; |
|
145 | - } |
|
146 | - return false; |
|
147 | - } |
|
148 | - |
|
149 | - public function isDeletable($path) { |
|
150 | - if ($path === '' || $path === '/') { |
|
151 | - return false; |
|
152 | - } |
|
153 | - $parent = dirname($path); |
|
154 | - return $this->isUpdatable($parent) && $this->isUpdatable($path); |
|
155 | - } |
|
156 | - |
|
157 | - public function isSharable($path) { |
|
158 | - return $this->isReadable($path); |
|
159 | - } |
|
160 | - |
|
161 | - public function getPermissions($path) { |
|
162 | - $permissions = 0; |
|
163 | - if ($this->isCreatable($path)) { |
|
164 | - $permissions |= \OCP\Constants::PERMISSION_CREATE; |
|
165 | - } |
|
166 | - if ($this->isReadable($path)) { |
|
167 | - $permissions |= \OCP\Constants::PERMISSION_READ; |
|
168 | - } |
|
169 | - if ($this->isUpdatable($path)) { |
|
170 | - $permissions |= \OCP\Constants::PERMISSION_UPDATE; |
|
171 | - } |
|
172 | - if ($this->isDeletable($path)) { |
|
173 | - $permissions |= \OCP\Constants::PERMISSION_DELETE; |
|
174 | - } |
|
175 | - if ($this->isSharable($path)) { |
|
176 | - $permissions |= \OCP\Constants::PERMISSION_SHARE; |
|
177 | - } |
|
178 | - return $permissions; |
|
179 | - } |
|
180 | - |
|
181 | - public function filemtime($path) { |
|
182 | - $stat = $this->stat($path); |
|
183 | - if (isset($stat['mtime']) && $stat['mtime'] > 0) { |
|
184 | - return $stat['mtime']; |
|
185 | - } else { |
|
186 | - return 0; |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - public function file_get_contents($path) { |
|
191 | - $handle = $this->fopen($path, "r"); |
|
192 | - if (!$handle) { |
|
193 | - return false; |
|
194 | - } |
|
195 | - $data = stream_get_contents($handle); |
|
196 | - fclose($handle); |
|
197 | - return $data; |
|
198 | - } |
|
199 | - |
|
200 | - public function file_put_contents($path, $data) { |
|
201 | - $handle = $this->fopen($path, "w"); |
|
202 | - $this->removeCachedFile($path); |
|
203 | - $count = fwrite($handle, $data); |
|
204 | - fclose($handle); |
|
205 | - return $count; |
|
206 | - } |
|
207 | - |
|
208 | - public function rename($path1, $path2) { |
|
209 | - $this->remove($path2); |
|
210 | - |
|
211 | - $this->removeCachedFile($path1); |
|
212 | - return $this->copy($path1, $path2) and $this->remove($path1); |
|
213 | - } |
|
214 | - |
|
215 | - public function copy($path1, $path2) { |
|
216 | - if ($this->is_dir($path1)) { |
|
217 | - $this->remove($path2); |
|
218 | - $dir = $this->opendir($path1); |
|
219 | - $this->mkdir($path2); |
|
220 | - while ($file = readdir($dir)) { |
|
221 | - if (!Filesystem::isIgnoredDir($file)) { |
|
222 | - if (!$this->copy($path1 . '/' . $file, $path2 . '/' . $file)) { |
|
223 | - return false; |
|
224 | - } |
|
225 | - } |
|
226 | - } |
|
227 | - closedir($dir); |
|
228 | - return true; |
|
229 | - } else { |
|
230 | - $source = $this->fopen($path1, 'r'); |
|
231 | - $target = $this->fopen($path2, 'w'); |
|
232 | - list(, $result) = \OC_Helper::streamCopy($source, $target); |
|
233 | - if (!$result) { |
|
234 | - \OC::$server->getLogger()->warning("Failed to write data while copying $path1 to $path2"); |
|
235 | - } |
|
236 | - $this->removeCachedFile($path2); |
|
237 | - return $result; |
|
238 | - } |
|
239 | - } |
|
240 | - |
|
241 | - public function getMimeType($path) { |
|
242 | - if ($this->is_dir($path)) { |
|
243 | - return 'httpd/unix-directory'; |
|
244 | - } elseif ($this->file_exists($path)) { |
|
245 | - return \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
246 | - } else { |
|
247 | - return false; |
|
248 | - } |
|
249 | - } |
|
250 | - |
|
251 | - public function hash($type, $path, $raw = false) { |
|
252 | - $fh = $this->fopen($path, 'rb'); |
|
253 | - $ctx = hash_init($type); |
|
254 | - hash_update_stream($ctx, $fh); |
|
255 | - fclose($fh); |
|
256 | - return hash_final($ctx, $raw); |
|
257 | - } |
|
258 | - |
|
259 | - public function search($query) { |
|
260 | - return $this->searchInDir($query); |
|
261 | - } |
|
262 | - |
|
263 | - public function getLocalFile($path) { |
|
264 | - return $this->getCachedFile($path); |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * @param string $path |
|
269 | - * @param string $target |
|
270 | - */ |
|
271 | - private function addLocalFolder($path, $target) { |
|
272 | - $dh = $this->opendir($path); |
|
273 | - if (is_resource($dh)) { |
|
274 | - while (($file = readdir($dh)) !== false) { |
|
275 | - if (!\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
276 | - if ($this->is_dir($path . '/' . $file)) { |
|
277 | - mkdir($target . '/' . $file); |
|
278 | - $this->addLocalFolder($path . '/' . $file, $target . '/' . $file); |
|
279 | - } else { |
|
280 | - $tmp = $this->toTmpFile($path . '/' . $file); |
|
281 | - rename($tmp, $target . '/' . $file); |
|
282 | - } |
|
283 | - } |
|
284 | - } |
|
285 | - } |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * @param string $query |
|
290 | - * @param string $dir |
|
291 | - * @return array |
|
292 | - */ |
|
293 | - protected function searchInDir($query, $dir = '') { |
|
294 | - $files = array(); |
|
295 | - $dh = $this->opendir($dir); |
|
296 | - if (is_resource($dh)) { |
|
297 | - while (($item = readdir($dh)) !== false) { |
|
298 | - if (\OC\Files\Filesystem::isIgnoredDir($item)) continue; |
|
299 | - if (strstr(strtolower($item), strtolower($query)) !== false) { |
|
300 | - $files[] = $dir . '/' . $item; |
|
301 | - } |
|
302 | - if ($this->is_dir($dir . '/' . $item)) { |
|
303 | - $files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item)); |
|
304 | - } |
|
305 | - } |
|
306 | - } |
|
307 | - closedir($dh); |
|
308 | - return $files; |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * check if a file or folder has been updated since $time |
|
313 | - * |
|
314 | - * The method is only used to check if the cache needs to be updated. Storage backends that don't support checking |
|
315 | - * the mtime should always return false here. As a result storage implementations that always return false expect |
|
316 | - * exclusive access to the backend and will not pick up files that have been added in a way that circumvents |
|
317 | - * ownClouds filesystem. |
|
318 | - * |
|
319 | - * @param string $path |
|
320 | - * @param int $time |
|
321 | - * @return bool |
|
322 | - */ |
|
323 | - public function hasUpdated($path, $time) { |
|
324 | - return $this->filemtime($path) > $time; |
|
325 | - } |
|
326 | - |
|
327 | - public function getCache($path = '', $storage = null) { |
|
328 | - if (!$storage) { |
|
329 | - $storage = $this; |
|
330 | - } |
|
331 | - if (!isset($storage->cache)) { |
|
332 | - $storage->cache = new Cache($storage); |
|
333 | - } |
|
334 | - return $storage->cache; |
|
335 | - } |
|
336 | - |
|
337 | - public function getScanner($path = '', $storage = null) { |
|
338 | - if (!$storage) { |
|
339 | - $storage = $this; |
|
340 | - } |
|
341 | - if (!isset($storage->scanner)) { |
|
342 | - $storage->scanner = new Scanner($storage); |
|
343 | - } |
|
344 | - return $storage->scanner; |
|
345 | - } |
|
346 | - |
|
347 | - public function getWatcher($path = '', $storage = null) { |
|
348 | - if (!$storage) { |
|
349 | - $storage = $this; |
|
350 | - } |
|
351 | - if (!isset($this->watcher)) { |
|
352 | - $this->watcher = new Watcher($storage); |
|
353 | - $globalPolicy = \OC::$server->getConfig()->getSystemValue('filesystem_check_changes', Watcher::CHECK_NEVER); |
|
354 | - $this->watcher->setPolicy((int)$this->getMountOption('filesystem_check_changes', $globalPolicy)); |
|
355 | - } |
|
356 | - return $this->watcher; |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * get a propagator instance for the cache |
|
361 | - * |
|
362 | - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher |
|
363 | - * @return \OC\Files\Cache\Propagator |
|
364 | - */ |
|
365 | - public function getPropagator($storage = null) { |
|
366 | - if (!$storage) { |
|
367 | - $storage = $this; |
|
368 | - } |
|
369 | - if (!isset($storage->propagator)) { |
|
370 | - $storage->propagator = new Propagator($storage, \OC::$server->getDatabaseConnection()); |
|
371 | - } |
|
372 | - return $storage->propagator; |
|
373 | - } |
|
374 | - |
|
375 | - public function getUpdater($storage = null) { |
|
376 | - if (!$storage) { |
|
377 | - $storage = $this; |
|
378 | - } |
|
379 | - if (!isset($storage->updater)) { |
|
380 | - $storage->updater = new Updater($storage); |
|
381 | - } |
|
382 | - return $storage->updater; |
|
383 | - } |
|
384 | - |
|
385 | - public function getStorageCache($storage = null) { |
|
386 | - if (!$storage) { |
|
387 | - $storage = $this; |
|
388 | - } |
|
389 | - if (!isset($this->storageCache)) { |
|
390 | - $this->storageCache = new \OC\Files\Cache\Storage($storage); |
|
391 | - } |
|
392 | - return $this->storageCache; |
|
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * get the owner of a path |
|
397 | - * |
|
398 | - * @param string $path The path to get the owner |
|
399 | - * @return string|false uid or false |
|
400 | - */ |
|
401 | - public function getOwner($path) { |
|
402 | - if ($this->owner === null) { |
|
403 | - $this->owner = \OC_User::getUser(); |
|
404 | - } |
|
405 | - |
|
406 | - return $this->owner; |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * get the ETag for a file or folder |
|
411 | - * |
|
412 | - * @param string $path |
|
413 | - * @return string |
|
414 | - */ |
|
415 | - public function getETag($path) { |
|
416 | - return uniqid(); |
|
417 | - } |
|
418 | - |
|
419 | - /** |
|
420 | - * clean a path, i.e. remove all redundant '.' and '..' |
|
421 | - * making sure that it can't point to higher than '/' |
|
422 | - * |
|
423 | - * @param string $path The path to clean |
|
424 | - * @return string cleaned path |
|
425 | - */ |
|
426 | - public function cleanPath($path) { |
|
427 | - if (strlen($path) == 0 or $path[0] != '/') { |
|
428 | - $path = '/' . $path; |
|
429 | - } |
|
430 | - |
|
431 | - $output = array(); |
|
432 | - foreach (explode('/', $path) as $chunk) { |
|
433 | - if ($chunk == '..') { |
|
434 | - array_pop($output); |
|
435 | - } else if ($chunk == '.') { |
|
436 | - } else { |
|
437 | - $output[] = $chunk; |
|
438 | - } |
|
439 | - } |
|
440 | - return implode('/', $output); |
|
441 | - } |
|
442 | - |
|
443 | - /** |
|
444 | - * Test a storage for availability |
|
445 | - * |
|
446 | - * @return bool |
|
447 | - */ |
|
448 | - public function test() { |
|
449 | - try { |
|
450 | - if ($this->stat('')) { |
|
451 | - return true; |
|
452 | - } |
|
453 | - \OC::$server->getLogger()->info("External storage not available: stat() failed"); |
|
454 | - return false; |
|
455 | - } catch (\Exception $e) { |
|
456 | - \OC::$server->getLogger()->info("External storage not available: " . $e->getMessage()); |
|
457 | - \OC::$server->getLogger()->logException($e, ['level' => ILogger::DEBUG]); |
|
458 | - return false; |
|
459 | - } |
|
460 | - } |
|
461 | - |
|
462 | - /** |
|
463 | - * get the free space in the storage |
|
464 | - * |
|
465 | - * @param string $path |
|
466 | - * @return int|false |
|
467 | - */ |
|
468 | - public function free_space($path) { |
|
469 | - return \OCP\Files\FileInfo::SPACE_UNKNOWN; |
|
470 | - } |
|
471 | - |
|
472 | - /** |
|
473 | - * {@inheritdoc} |
|
474 | - */ |
|
475 | - public function isLocal() { |
|
476 | - // the common implementation returns a temporary file by |
|
477 | - // default, which is not local |
|
478 | - return false; |
|
479 | - } |
|
480 | - |
|
481 | - /** |
|
482 | - * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class |
|
483 | - * |
|
484 | - * @param string $class |
|
485 | - * @return bool |
|
486 | - */ |
|
487 | - public function instanceOfStorage($class) { |
|
488 | - if (ltrim($class, '\\') === 'OC\Files\Storage\Shared') { |
|
489 | - // FIXME Temporary fix to keep existing checks working |
|
490 | - $class = '\OCA\Files_Sharing\SharedStorage'; |
|
491 | - } |
|
492 | - return is_a($this, $class); |
|
493 | - } |
|
494 | - |
|
495 | - /** |
|
496 | - * A custom storage implementation can return an url for direct download of a give file. |
|
497 | - * |
|
498 | - * For now the returned array can hold the parameter url - in future more attributes might follow. |
|
499 | - * |
|
500 | - * @param string $path |
|
501 | - * @return array|false |
|
502 | - */ |
|
503 | - public function getDirectDownload($path) { |
|
504 | - return []; |
|
505 | - } |
|
506 | - |
|
507 | - /** |
|
508 | - * @inheritdoc |
|
509 | - * @throws InvalidPathException |
|
510 | - */ |
|
511 | - public function verifyPath($path, $fileName) { |
|
512 | - |
|
513 | - // verify empty and dot files |
|
514 | - $trimmed = trim($fileName); |
|
515 | - if ($trimmed === '') { |
|
516 | - throw new EmptyFileNameException(); |
|
517 | - } |
|
518 | - |
|
519 | - if (\OC\Files\Filesystem::isIgnoredDir($trimmed)) { |
|
520 | - throw new InvalidDirectoryException(); |
|
521 | - } |
|
522 | - |
|
523 | - if (!\OC::$server->getDatabaseConnection()->supports4ByteText()) { |
|
524 | - // verify database - e.g. mysql only 3-byte chars |
|
525 | - if (preg_match('%(?: |
|
74 | + use LocalTempFileTrait; |
|
75 | + |
|
76 | + protected $cache; |
|
77 | + protected $scanner; |
|
78 | + protected $watcher; |
|
79 | + protected $propagator; |
|
80 | + protected $storageCache; |
|
81 | + protected $updater; |
|
82 | + |
|
83 | + protected $mountOptions = []; |
|
84 | + protected $owner = null; |
|
85 | + |
|
86 | + private $shouldLogLocks = null; |
|
87 | + private $logger; |
|
88 | + |
|
89 | + public function __construct($parameters) { |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Remove a file or folder |
|
94 | + * |
|
95 | + * @param string $path |
|
96 | + * @return bool |
|
97 | + */ |
|
98 | + protected function remove($path) { |
|
99 | + if ($this->is_dir($path)) { |
|
100 | + return $this->rmdir($path); |
|
101 | + } else if ($this->is_file($path)) { |
|
102 | + return $this->unlink($path); |
|
103 | + } else { |
|
104 | + return false; |
|
105 | + } |
|
106 | + } |
|
107 | + |
|
108 | + public function is_dir($path) { |
|
109 | + return $this->filetype($path) === 'dir'; |
|
110 | + } |
|
111 | + |
|
112 | + public function is_file($path) { |
|
113 | + return $this->filetype($path) === 'file'; |
|
114 | + } |
|
115 | + |
|
116 | + public function filesize($path) { |
|
117 | + if ($this->is_dir($path)) { |
|
118 | + return 0; //by definition |
|
119 | + } else { |
|
120 | + $stat = $this->stat($path); |
|
121 | + if (isset($stat['size'])) { |
|
122 | + return $stat['size']; |
|
123 | + } else { |
|
124 | + return 0; |
|
125 | + } |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + public function isReadable($path) { |
|
130 | + // at least check whether it exists |
|
131 | + // subclasses might want to implement this more thoroughly |
|
132 | + return $this->file_exists($path); |
|
133 | + } |
|
134 | + |
|
135 | + public function isUpdatable($path) { |
|
136 | + // at least check whether it exists |
|
137 | + // subclasses might want to implement this more thoroughly |
|
138 | + // a non-existing file/folder isn't updatable |
|
139 | + return $this->file_exists($path); |
|
140 | + } |
|
141 | + |
|
142 | + public function isCreatable($path) { |
|
143 | + if ($this->is_dir($path) && $this->isUpdatable($path)) { |
|
144 | + return true; |
|
145 | + } |
|
146 | + return false; |
|
147 | + } |
|
148 | + |
|
149 | + public function isDeletable($path) { |
|
150 | + if ($path === '' || $path === '/') { |
|
151 | + return false; |
|
152 | + } |
|
153 | + $parent = dirname($path); |
|
154 | + return $this->isUpdatable($parent) && $this->isUpdatable($path); |
|
155 | + } |
|
156 | + |
|
157 | + public function isSharable($path) { |
|
158 | + return $this->isReadable($path); |
|
159 | + } |
|
160 | + |
|
161 | + public function getPermissions($path) { |
|
162 | + $permissions = 0; |
|
163 | + if ($this->isCreatable($path)) { |
|
164 | + $permissions |= \OCP\Constants::PERMISSION_CREATE; |
|
165 | + } |
|
166 | + if ($this->isReadable($path)) { |
|
167 | + $permissions |= \OCP\Constants::PERMISSION_READ; |
|
168 | + } |
|
169 | + if ($this->isUpdatable($path)) { |
|
170 | + $permissions |= \OCP\Constants::PERMISSION_UPDATE; |
|
171 | + } |
|
172 | + if ($this->isDeletable($path)) { |
|
173 | + $permissions |= \OCP\Constants::PERMISSION_DELETE; |
|
174 | + } |
|
175 | + if ($this->isSharable($path)) { |
|
176 | + $permissions |= \OCP\Constants::PERMISSION_SHARE; |
|
177 | + } |
|
178 | + return $permissions; |
|
179 | + } |
|
180 | + |
|
181 | + public function filemtime($path) { |
|
182 | + $stat = $this->stat($path); |
|
183 | + if (isset($stat['mtime']) && $stat['mtime'] > 0) { |
|
184 | + return $stat['mtime']; |
|
185 | + } else { |
|
186 | + return 0; |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + public function file_get_contents($path) { |
|
191 | + $handle = $this->fopen($path, "r"); |
|
192 | + if (!$handle) { |
|
193 | + return false; |
|
194 | + } |
|
195 | + $data = stream_get_contents($handle); |
|
196 | + fclose($handle); |
|
197 | + return $data; |
|
198 | + } |
|
199 | + |
|
200 | + public function file_put_contents($path, $data) { |
|
201 | + $handle = $this->fopen($path, "w"); |
|
202 | + $this->removeCachedFile($path); |
|
203 | + $count = fwrite($handle, $data); |
|
204 | + fclose($handle); |
|
205 | + return $count; |
|
206 | + } |
|
207 | + |
|
208 | + public function rename($path1, $path2) { |
|
209 | + $this->remove($path2); |
|
210 | + |
|
211 | + $this->removeCachedFile($path1); |
|
212 | + return $this->copy($path1, $path2) and $this->remove($path1); |
|
213 | + } |
|
214 | + |
|
215 | + public function copy($path1, $path2) { |
|
216 | + if ($this->is_dir($path1)) { |
|
217 | + $this->remove($path2); |
|
218 | + $dir = $this->opendir($path1); |
|
219 | + $this->mkdir($path2); |
|
220 | + while ($file = readdir($dir)) { |
|
221 | + if (!Filesystem::isIgnoredDir($file)) { |
|
222 | + if (!$this->copy($path1 . '/' . $file, $path2 . '/' . $file)) { |
|
223 | + return false; |
|
224 | + } |
|
225 | + } |
|
226 | + } |
|
227 | + closedir($dir); |
|
228 | + return true; |
|
229 | + } else { |
|
230 | + $source = $this->fopen($path1, 'r'); |
|
231 | + $target = $this->fopen($path2, 'w'); |
|
232 | + list(, $result) = \OC_Helper::streamCopy($source, $target); |
|
233 | + if (!$result) { |
|
234 | + \OC::$server->getLogger()->warning("Failed to write data while copying $path1 to $path2"); |
|
235 | + } |
|
236 | + $this->removeCachedFile($path2); |
|
237 | + return $result; |
|
238 | + } |
|
239 | + } |
|
240 | + |
|
241 | + public function getMimeType($path) { |
|
242 | + if ($this->is_dir($path)) { |
|
243 | + return 'httpd/unix-directory'; |
|
244 | + } elseif ($this->file_exists($path)) { |
|
245 | + return \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
246 | + } else { |
|
247 | + return false; |
|
248 | + } |
|
249 | + } |
|
250 | + |
|
251 | + public function hash($type, $path, $raw = false) { |
|
252 | + $fh = $this->fopen($path, 'rb'); |
|
253 | + $ctx = hash_init($type); |
|
254 | + hash_update_stream($ctx, $fh); |
|
255 | + fclose($fh); |
|
256 | + return hash_final($ctx, $raw); |
|
257 | + } |
|
258 | + |
|
259 | + public function search($query) { |
|
260 | + return $this->searchInDir($query); |
|
261 | + } |
|
262 | + |
|
263 | + public function getLocalFile($path) { |
|
264 | + return $this->getCachedFile($path); |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * @param string $path |
|
269 | + * @param string $target |
|
270 | + */ |
|
271 | + private function addLocalFolder($path, $target) { |
|
272 | + $dh = $this->opendir($path); |
|
273 | + if (is_resource($dh)) { |
|
274 | + while (($file = readdir($dh)) !== false) { |
|
275 | + if (!\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
276 | + if ($this->is_dir($path . '/' . $file)) { |
|
277 | + mkdir($target . '/' . $file); |
|
278 | + $this->addLocalFolder($path . '/' . $file, $target . '/' . $file); |
|
279 | + } else { |
|
280 | + $tmp = $this->toTmpFile($path . '/' . $file); |
|
281 | + rename($tmp, $target . '/' . $file); |
|
282 | + } |
|
283 | + } |
|
284 | + } |
|
285 | + } |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * @param string $query |
|
290 | + * @param string $dir |
|
291 | + * @return array |
|
292 | + */ |
|
293 | + protected function searchInDir($query, $dir = '') { |
|
294 | + $files = array(); |
|
295 | + $dh = $this->opendir($dir); |
|
296 | + if (is_resource($dh)) { |
|
297 | + while (($item = readdir($dh)) !== false) { |
|
298 | + if (\OC\Files\Filesystem::isIgnoredDir($item)) continue; |
|
299 | + if (strstr(strtolower($item), strtolower($query)) !== false) { |
|
300 | + $files[] = $dir . '/' . $item; |
|
301 | + } |
|
302 | + if ($this->is_dir($dir . '/' . $item)) { |
|
303 | + $files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item)); |
|
304 | + } |
|
305 | + } |
|
306 | + } |
|
307 | + closedir($dh); |
|
308 | + return $files; |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * check if a file or folder has been updated since $time |
|
313 | + * |
|
314 | + * The method is only used to check if the cache needs to be updated. Storage backends that don't support checking |
|
315 | + * the mtime should always return false here. As a result storage implementations that always return false expect |
|
316 | + * exclusive access to the backend and will not pick up files that have been added in a way that circumvents |
|
317 | + * ownClouds filesystem. |
|
318 | + * |
|
319 | + * @param string $path |
|
320 | + * @param int $time |
|
321 | + * @return bool |
|
322 | + */ |
|
323 | + public function hasUpdated($path, $time) { |
|
324 | + return $this->filemtime($path) > $time; |
|
325 | + } |
|
326 | + |
|
327 | + public function getCache($path = '', $storage = null) { |
|
328 | + if (!$storage) { |
|
329 | + $storage = $this; |
|
330 | + } |
|
331 | + if (!isset($storage->cache)) { |
|
332 | + $storage->cache = new Cache($storage); |
|
333 | + } |
|
334 | + return $storage->cache; |
|
335 | + } |
|
336 | + |
|
337 | + public function getScanner($path = '', $storage = null) { |
|
338 | + if (!$storage) { |
|
339 | + $storage = $this; |
|
340 | + } |
|
341 | + if (!isset($storage->scanner)) { |
|
342 | + $storage->scanner = new Scanner($storage); |
|
343 | + } |
|
344 | + return $storage->scanner; |
|
345 | + } |
|
346 | + |
|
347 | + public function getWatcher($path = '', $storage = null) { |
|
348 | + if (!$storage) { |
|
349 | + $storage = $this; |
|
350 | + } |
|
351 | + if (!isset($this->watcher)) { |
|
352 | + $this->watcher = new Watcher($storage); |
|
353 | + $globalPolicy = \OC::$server->getConfig()->getSystemValue('filesystem_check_changes', Watcher::CHECK_NEVER); |
|
354 | + $this->watcher->setPolicy((int)$this->getMountOption('filesystem_check_changes', $globalPolicy)); |
|
355 | + } |
|
356 | + return $this->watcher; |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * get a propagator instance for the cache |
|
361 | + * |
|
362 | + * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher |
|
363 | + * @return \OC\Files\Cache\Propagator |
|
364 | + */ |
|
365 | + public function getPropagator($storage = null) { |
|
366 | + if (!$storage) { |
|
367 | + $storage = $this; |
|
368 | + } |
|
369 | + if (!isset($storage->propagator)) { |
|
370 | + $storage->propagator = new Propagator($storage, \OC::$server->getDatabaseConnection()); |
|
371 | + } |
|
372 | + return $storage->propagator; |
|
373 | + } |
|
374 | + |
|
375 | + public function getUpdater($storage = null) { |
|
376 | + if (!$storage) { |
|
377 | + $storage = $this; |
|
378 | + } |
|
379 | + if (!isset($storage->updater)) { |
|
380 | + $storage->updater = new Updater($storage); |
|
381 | + } |
|
382 | + return $storage->updater; |
|
383 | + } |
|
384 | + |
|
385 | + public function getStorageCache($storage = null) { |
|
386 | + if (!$storage) { |
|
387 | + $storage = $this; |
|
388 | + } |
|
389 | + if (!isset($this->storageCache)) { |
|
390 | + $this->storageCache = new \OC\Files\Cache\Storage($storage); |
|
391 | + } |
|
392 | + return $this->storageCache; |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * get the owner of a path |
|
397 | + * |
|
398 | + * @param string $path The path to get the owner |
|
399 | + * @return string|false uid or false |
|
400 | + */ |
|
401 | + public function getOwner($path) { |
|
402 | + if ($this->owner === null) { |
|
403 | + $this->owner = \OC_User::getUser(); |
|
404 | + } |
|
405 | + |
|
406 | + return $this->owner; |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * get the ETag for a file or folder |
|
411 | + * |
|
412 | + * @param string $path |
|
413 | + * @return string |
|
414 | + */ |
|
415 | + public function getETag($path) { |
|
416 | + return uniqid(); |
|
417 | + } |
|
418 | + |
|
419 | + /** |
|
420 | + * clean a path, i.e. remove all redundant '.' and '..' |
|
421 | + * making sure that it can't point to higher than '/' |
|
422 | + * |
|
423 | + * @param string $path The path to clean |
|
424 | + * @return string cleaned path |
|
425 | + */ |
|
426 | + public function cleanPath($path) { |
|
427 | + if (strlen($path) == 0 or $path[0] != '/') { |
|
428 | + $path = '/' . $path; |
|
429 | + } |
|
430 | + |
|
431 | + $output = array(); |
|
432 | + foreach (explode('/', $path) as $chunk) { |
|
433 | + if ($chunk == '..') { |
|
434 | + array_pop($output); |
|
435 | + } else if ($chunk == '.') { |
|
436 | + } else { |
|
437 | + $output[] = $chunk; |
|
438 | + } |
|
439 | + } |
|
440 | + return implode('/', $output); |
|
441 | + } |
|
442 | + |
|
443 | + /** |
|
444 | + * Test a storage for availability |
|
445 | + * |
|
446 | + * @return bool |
|
447 | + */ |
|
448 | + public function test() { |
|
449 | + try { |
|
450 | + if ($this->stat('')) { |
|
451 | + return true; |
|
452 | + } |
|
453 | + \OC::$server->getLogger()->info("External storage not available: stat() failed"); |
|
454 | + return false; |
|
455 | + } catch (\Exception $e) { |
|
456 | + \OC::$server->getLogger()->info("External storage not available: " . $e->getMessage()); |
|
457 | + \OC::$server->getLogger()->logException($e, ['level' => ILogger::DEBUG]); |
|
458 | + return false; |
|
459 | + } |
|
460 | + } |
|
461 | + |
|
462 | + /** |
|
463 | + * get the free space in the storage |
|
464 | + * |
|
465 | + * @param string $path |
|
466 | + * @return int|false |
|
467 | + */ |
|
468 | + public function free_space($path) { |
|
469 | + return \OCP\Files\FileInfo::SPACE_UNKNOWN; |
|
470 | + } |
|
471 | + |
|
472 | + /** |
|
473 | + * {@inheritdoc} |
|
474 | + */ |
|
475 | + public function isLocal() { |
|
476 | + // the common implementation returns a temporary file by |
|
477 | + // default, which is not local |
|
478 | + return false; |
|
479 | + } |
|
480 | + |
|
481 | + /** |
|
482 | + * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class |
|
483 | + * |
|
484 | + * @param string $class |
|
485 | + * @return bool |
|
486 | + */ |
|
487 | + public function instanceOfStorage($class) { |
|
488 | + if (ltrim($class, '\\') === 'OC\Files\Storage\Shared') { |
|
489 | + // FIXME Temporary fix to keep existing checks working |
|
490 | + $class = '\OCA\Files_Sharing\SharedStorage'; |
|
491 | + } |
|
492 | + return is_a($this, $class); |
|
493 | + } |
|
494 | + |
|
495 | + /** |
|
496 | + * A custom storage implementation can return an url for direct download of a give file. |
|
497 | + * |
|
498 | + * For now the returned array can hold the parameter url - in future more attributes might follow. |
|
499 | + * |
|
500 | + * @param string $path |
|
501 | + * @return array|false |
|
502 | + */ |
|
503 | + public function getDirectDownload($path) { |
|
504 | + return []; |
|
505 | + } |
|
506 | + |
|
507 | + /** |
|
508 | + * @inheritdoc |
|
509 | + * @throws InvalidPathException |
|
510 | + */ |
|
511 | + public function verifyPath($path, $fileName) { |
|
512 | + |
|
513 | + // verify empty and dot files |
|
514 | + $trimmed = trim($fileName); |
|
515 | + if ($trimmed === '') { |
|
516 | + throw new EmptyFileNameException(); |
|
517 | + } |
|
518 | + |
|
519 | + if (\OC\Files\Filesystem::isIgnoredDir($trimmed)) { |
|
520 | + throw new InvalidDirectoryException(); |
|
521 | + } |
|
522 | + |
|
523 | + if (!\OC::$server->getDatabaseConnection()->supports4ByteText()) { |
|
524 | + // verify database - e.g. mysql only 3-byte chars |
|
525 | + if (preg_match('%(?: |
|
526 | 526 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
527 | 527 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
528 | 528 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
529 | 529 | )%xs', $fileName)) { |
530 | - throw new InvalidCharacterInPathException(); |
|
531 | - } |
|
532 | - } |
|
533 | - |
|
534 | - if (isset($fileName[255])) { |
|
535 | - throw new FileNameTooLongException(); |
|
536 | - } |
|
537 | - |
|
538 | - // NOTE: $path will remain unverified for now |
|
539 | - $this->verifyPosixPath($fileName); |
|
540 | - } |
|
541 | - |
|
542 | - /** |
|
543 | - * @param string $fileName |
|
544 | - * @throws InvalidPathException |
|
545 | - */ |
|
546 | - protected function verifyPosixPath($fileName) { |
|
547 | - $fileName = trim($fileName); |
|
548 | - $this->scanForInvalidCharacters($fileName, "\\/"); |
|
549 | - $reservedNames = ['*']; |
|
550 | - if (in_array($fileName, $reservedNames)) { |
|
551 | - throw new ReservedWordException(); |
|
552 | - } |
|
553 | - } |
|
554 | - |
|
555 | - /** |
|
556 | - * @param string $fileName |
|
557 | - * @param string $invalidChars |
|
558 | - * @throws InvalidPathException |
|
559 | - */ |
|
560 | - private function scanForInvalidCharacters($fileName, $invalidChars) { |
|
561 | - foreach (str_split($invalidChars) as $char) { |
|
562 | - if (strpos($fileName, $char) !== false) { |
|
563 | - throw new InvalidCharacterInPathException(); |
|
564 | - } |
|
565 | - } |
|
566 | - |
|
567 | - $sanitizedFileName = filter_var($fileName, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); |
|
568 | - if ($sanitizedFileName !== $fileName) { |
|
569 | - throw new InvalidCharacterInPathException(); |
|
570 | - } |
|
571 | - } |
|
572 | - |
|
573 | - /** |
|
574 | - * @param array $options |
|
575 | - */ |
|
576 | - public function setMountOptions(array $options) { |
|
577 | - $this->mountOptions = $options; |
|
578 | - } |
|
579 | - |
|
580 | - /** |
|
581 | - * @param string $name |
|
582 | - * @param mixed $default |
|
583 | - * @return mixed |
|
584 | - */ |
|
585 | - public function getMountOption($name, $default = null) { |
|
586 | - return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default; |
|
587 | - } |
|
588 | - |
|
589 | - /** |
|
590 | - * @param IStorage $sourceStorage |
|
591 | - * @param string $sourceInternalPath |
|
592 | - * @param string $targetInternalPath |
|
593 | - * @param bool $preserveMtime |
|
594 | - * @return bool |
|
595 | - */ |
|
596 | - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) { |
|
597 | - if ($sourceStorage === $this) { |
|
598 | - return $this->copy($sourceInternalPath, $targetInternalPath); |
|
599 | - } |
|
600 | - |
|
601 | - if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
602 | - $dh = $sourceStorage->opendir($sourceInternalPath); |
|
603 | - $result = $this->mkdir($targetInternalPath); |
|
604 | - if (is_resource($dh)) { |
|
605 | - while ($result and ($file = readdir($dh)) !== false) { |
|
606 | - if (!Filesystem::isIgnoredDir($file)) { |
|
607 | - $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file); |
|
608 | - } |
|
609 | - } |
|
610 | - } |
|
611 | - } else { |
|
612 | - $source = $sourceStorage->fopen($sourceInternalPath, 'r'); |
|
613 | - // TODO: call fopen in a way that we execute again all storage wrappers |
|
614 | - // to avoid that we bypass storage wrappers which perform important actions |
|
615 | - // for this operation. Same is true for all other operations which |
|
616 | - // are not the same as the original one.Once this is fixed we also |
|
617 | - // need to adjust the encryption wrapper. |
|
618 | - $target = $this->fopen($targetInternalPath, 'w'); |
|
619 | - list(, $result) = \OC_Helper::streamCopy($source, $target); |
|
620 | - if ($result and $preserveMtime) { |
|
621 | - $this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath)); |
|
622 | - } |
|
623 | - fclose($source); |
|
624 | - fclose($target); |
|
625 | - |
|
626 | - if (!$result) { |
|
627 | - // delete partially written target file |
|
628 | - $this->unlink($targetInternalPath); |
|
629 | - // delete cache entry that was created by fopen |
|
630 | - $this->getCache()->remove($targetInternalPath); |
|
631 | - } |
|
632 | - } |
|
633 | - return (bool)$result; |
|
634 | - } |
|
635 | - |
|
636 | - /** |
|
637 | - * @param IStorage $sourceStorage |
|
638 | - * @param string $sourceInternalPath |
|
639 | - * @param string $targetInternalPath |
|
640 | - * @return bool |
|
641 | - */ |
|
642 | - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
643 | - if ($sourceStorage === $this) { |
|
644 | - return $this->rename($sourceInternalPath, $targetInternalPath); |
|
645 | - } |
|
646 | - |
|
647 | - if (!$sourceStorage->isDeletable($sourceInternalPath)) { |
|
648 | - return false; |
|
649 | - } |
|
650 | - |
|
651 | - $result = $this->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, true); |
|
652 | - if ($result) { |
|
653 | - if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
654 | - $result &= $sourceStorage->rmdir($sourceInternalPath); |
|
655 | - } else { |
|
656 | - $result &= $sourceStorage->unlink($sourceInternalPath); |
|
657 | - } |
|
658 | - } |
|
659 | - return $result; |
|
660 | - } |
|
661 | - |
|
662 | - /** |
|
663 | - * @inheritdoc |
|
664 | - */ |
|
665 | - public function getMetaData($path) { |
|
666 | - $permissions = $this->getPermissions($path); |
|
667 | - if (!$permissions & \OCP\Constants::PERMISSION_READ) { |
|
668 | - //can't read, nothing we can do |
|
669 | - return null; |
|
670 | - } |
|
671 | - |
|
672 | - $data = []; |
|
673 | - $data['mimetype'] = $this->getMimeType($path); |
|
674 | - $data['mtime'] = $this->filemtime($path); |
|
675 | - if ($data['mtime'] === false) { |
|
676 | - $data['mtime'] = time(); |
|
677 | - } |
|
678 | - if ($data['mimetype'] == 'httpd/unix-directory') { |
|
679 | - $data['size'] = -1; //unknown |
|
680 | - } else { |
|
681 | - $data['size'] = $this->filesize($path); |
|
682 | - } |
|
683 | - $data['etag'] = $this->getETag($path); |
|
684 | - $data['storage_mtime'] = $data['mtime']; |
|
685 | - $data['permissions'] = $permissions; |
|
686 | - |
|
687 | - return $data; |
|
688 | - } |
|
689 | - |
|
690 | - /** |
|
691 | - * @param string $path |
|
692 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
693 | - * @param \OCP\Lock\ILockingProvider $provider |
|
694 | - * @throws \OCP\Lock\LockedException |
|
695 | - */ |
|
696 | - public function acquireLock($path, $type, ILockingProvider $provider) { |
|
697 | - $logger = $this->getLockLogger(); |
|
698 | - if ($logger) { |
|
699 | - $typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive'; |
|
700 | - $logger->info( |
|
701 | - sprintf( |
|
702 | - 'acquire %s lock on "%s" on storage "%s"', |
|
703 | - $typeString, |
|
704 | - $path, |
|
705 | - $this->getId() |
|
706 | - ), |
|
707 | - [ |
|
708 | - 'app' => 'locking', |
|
709 | - ] |
|
710 | - ); |
|
711 | - } |
|
712 | - try { |
|
713 | - $provider->acquireLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type); |
|
714 | - } catch (LockedException $e) { |
|
715 | - if ($logger) { |
|
716 | - $logger->logException($e); |
|
717 | - } |
|
718 | - throw $e; |
|
719 | - } |
|
720 | - } |
|
721 | - |
|
722 | - /** |
|
723 | - * @param string $path |
|
724 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
725 | - * @param \OCP\Lock\ILockingProvider $provider |
|
726 | - * @throws \OCP\Lock\LockedException |
|
727 | - */ |
|
728 | - public function releaseLock($path, $type, ILockingProvider $provider) { |
|
729 | - $logger = $this->getLockLogger(); |
|
730 | - if ($logger) { |
|
731 | - $typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive'; |
|
732 | - $logger->info( |
|
733 | - sprintf( |
|
734 | - 'release %s lock on "%s" on storage "%s"', |
|
735 | - $typeString, |
|
736 | - $path, |
|
737 | - $this->getId() |
|
738 | - ), |
|
739 | - [ |
|
740 | - 'app' => 'locking', |
|
741 | - ] |
|
742 | - ); |
|
743 | - } |
|
744 | - try { |
|
745 | - $provider->releaseLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type); |
|
746 | - } catch (LockedException $e) { |
|
747 | - if ($logger) { |
|
748 | - $logger->logException($e); |
|
749 | - } |
|
750 | - throw $e; |
|
751 | - } |
|
752 | - } |
|
753 | - |
|
754 | - /** |
|
755 | - * @param string $path |
|
756 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
757 | - * @param \OCP\Lock\ILockingProvider $provider |
|
758 | - * @throws \OCP\Lock\LockedException |
|
759 | - */ |
|
760 | - public function changeLock($path, $type, ILockingProvider $provider) { |
|
761 | - $logger = $this->getLockLogger(); |
|
762 | - if ($logger) { |
|
763 | - $typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive'; |
|
764 | - $logger->info( |
|
765 | - sprintf( |
|
766 | - 'change lock on "%s" to %s on storage "%s"', |
|
767 | - $path, |
|
768 | - $typeString, |
|
769 | - $this->getId() |
|
770 | - ), |
|
771 | - [ |
|
772 | - 'app' => 'locking', |
|
773 | - ] |
|
774 | - ); |
|
775 | - } |
|
776 | - try { |
|
777 | - $provider->changeLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type); |
|
778 | - } catch (LockedException $e) { |
|
779 | - if ($logger) { |
|
780 | - $logger->logException($e); |
|
781 | - } |
|
782 | - throw $e; |
|
783 | - } |
|
784 | - } |
|
785 | - |
|
786 | - private function getLockLogger() { |
|
787 | - if (is_null($this->shouldLogLocks)) { |
|
788 | - $this->shouldLogLocks = \OC::$server->getConfig()->getSystemValue('filelocking.debug', false); |
|
789 | - $this->logger = $this->shouldLogLocks ? \OC::$server->getLogger() : null; |
|
790 | - } |
|
791 | - return $this->logger; |
|
792 | - } |
|
793 | - |
|
794 | - /** |
|
795 | - * @return array [ available, last_checked ] |
|
796 | - */ |
|
797 | - public function getAvailability() { |
|
798 | - return $this->getStorageCache()->getAvailability(); |
|
799 | - } |
|
800 | - |
|
801 | - /** |
|
802 | - * @param bool $isAvailable |
|
803 | - */ |
|
804 | - public function setAvailability($isAvailable) { |
|
805 | - $this->getStorageCache()->setAvailability($isAvailable); |
|
806 | - } |
|
807 | - |
|
808 | - /** |
|
809 | - * @return bool |
|
810 | - */ |
|
811 | - public function needsPartFile() { |
|
812 | - return true; |
|
813 | - } |
|
530 | + throw new InvalidCharacterInPathException(); |
|
531 | + } |
|
532 | + } |
|
533 | + |
|
534 | + if (isset($fileName[255])) { |
|
535 | + throw new FileNameTooLongException(); |
|
536 | + } |
|
537 | + |
|
538 | + // NOTE: $path will remain unverified for now |
|
539 | + $this->verifyPosixPath($fileName); |
|
540 | + } |
|
541 | + |
|
542 | + /** |
|
543 | + * @param string $fileName |
|
544 | + * @throws InvalidPathException |
|
545 | + */ |
|
546 | + protected function verifyPosixPath($fileName) { |
|
547 | + $fileName = trim($fileName); |
|
548 | + $this->scanForInvalidCharacters($fileName, "\\/"); |
|
549 | + $reservedNames = ['*']; |
|
550 | + if (in_array($fileName, $reservedNames)) { |
|
551 | + throw new ReservedWordException(); |
|
552 | + } |
|
553 | + } |
|
554 | + |
|
555 | + /** |
|
556 | + * @param string $fileName |
|
557 | + * @param string $invalidChars |
|
558 | + * @throws InvalidPathException |
|
559 | + */ |
|
560 | + private function scanForInvalidCharacters($fileName, $invalidChars) { |
|
561 | + foreach (str_split($invalidChars) as $char) { |
|
562 | + if (strpos($fileName, $char) !== false) { |
|
563 | + throw new InvalidCharacterInPathException(); |
|
564 | + } |
|
565 | + } |
|
566 | + |
|
567 | + $sanitizedFileName = filter_var($fileName, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); |
|
568 | + if ($sanitizedFileName !== $fileName) { |
|
569 | + throw new InvalidCharacterInPathException(); |
|
570 | + } |
|
571 | + } |
|
572 | + |
|
573 | + /** |
|
574 | + * @param array $options |
|
575 | + */ |
|
576 | + public function setMountOptions(array $options) { |
|
577 | + $this->mountOptions = $options; |
|
578 | + } |
|
579 | + |
|
580 | + /** |
|
581 | + * @param string $name |
|
582 | + * @param mixed $default |
|
583 | + * @return mixed |
|
584 | + */ |
|
585 | + public function getMountOption($name, $default = null) { |
|
586 | + return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default; |
|
587 | + } |
|
588 | + |
|
589 | + /** |
|
590 | + * @param IStorage $sourceStorage |
|
591 | + * @param string $sourceInternalPath |
|
592 | + * @param string $targetInternalPath |
|
593 | + * @param bool $preserveMtime |
|
594 | + * @return bool |
|
595 | + */ |
|
596 | + public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) { |
|
597 | + if ($sourceStorage === $this) { |
|
598 | + return $this->copy($sourceInternalPath, $targetInternalPath); |
|
599 | + } |
|
600 | + |
|
601 | + if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
602 | + $dh = $sourceStorage->opendir($sourceInternalPath); |
|
603 | + $result = $this->mkdir($targetInternalPath); |
|
604 | + if (is_resource($dh)) { |
|
605 | + while ($result and ($file = readdir($dh)) !== false) { |
|
606 | + if (!Filesystem::isIgnoredDir($file)) { |
|
607 | + $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file); |
|
608 | + } |
|
609 | + } |
|
610 | + } |
|
611 | + } else { |
|
612 | + $source = $sourceStorage->fopen($sourceInternalPath, 'r'); |
|
613 | + // TODO: call fopen in a way that we execute again all storage wrappers |
|
614 | + // to avoid that we bypass storage wrappers which perform important actions |
|
615 | + // for this operation. Same is true for all other operations which |
|
616 | + // are not the same as the original one.Once this is fixed we also |
|
617 | + // need to adjust the encryption wrapper. |
|
618 | + $target = $this->fopen($targetInternalPath, 'w'); |
|
619 | + list(, $result) = \OC_Helper::streamCopy($source, $target); |
|
620 | + if ($result and $preserveMtime) { |
|
621 | + $this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath)); |
|
622 | + } |
|
623 | + fclose($source); |
|
624 | + fclose($target); |
|
625 | + |
|
626 | + if (!$result) { |
|
627 | + // delete partially written target file |
|
628 | + $this->unlink($targetInternalPath); |
|
629 | + // delete cache entry that was created by fopen |
|
630 | + $this->getCache()->remove($targetInternalPath); |
|
631 | + } |
|
632 | + } |
|
633 | + return (bool)$result; |
|
634 | + } |
|
635 | + |
|
636 | + /** |
|
637 | + * @param IStorage $sourceStorage |
|
638 | + * @param string $sourceInternalPath |
|
639 | + * @param string $targetInternalPath |
|
640 | + * @return bool |
|
641 | + */ |
|
642 | + public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
643 | + if ($sourceStorage === $this) { |
|
644 | + return $this->rename($sourceInternalPath, $targetInternalPath); |
|
645 | + } |
|
646 | + |
|
647 | + if (!$sourceStorage->isDeletable($sourceInternalPath)) { |
|
648 | + return false; |
|
649 | + } |
|
650 | + |
|
651 | + $result = $this->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, true); |
|
652 | + if ($result) { |
|
653 | + if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
654 | + $result &= $sourceStorage->rmdir($sourceInternalPath); |
|
655 | + } else { |
|
656 | + $result &= $sourceStorage->unlink($sourceInternalPath); |
|
657 | + } |
|
658 | + } |
|
659 | + return $result; |
|
660 | + } |
|
661 | + |
|
662 | + /** |
|
663 | + * @inheritdoc |
|
664 | + */ |
|
665 | + public function getMetaData($path) { |
|
666 | + $permissions = $this->getPermissions($path); |
|
667 | + if (!$permissions & \OCP\Constants::PERMISSION_READ) { |
|
668 | + //can't read, nothing we can do |
|
669 | + return null; |
|
670 | + } |
|
671 | + |
|
672 | + $data = []; |
|
673 | + $data['mimetype'] = $this->getMimeType($path); |
|
674 | + $data['mtime'] = $this->filemtime($path); |
|
675 | + if ($data['mtime'] === false) { |
|
676 | + $data['mtime'] = time(); |
|
677 | + } |
|
678 | + if ($data['mimetype'] == 'httpd/unix-directory') { |
|
679 | + $data['size'] = -1; //unknown |
|
680 | + } else { |
|
681 | + $data['size'] = $this->filesize($path); |
|
682 | + } |
|
683 | + $data['etag'] = $this->getETag($path); |
|
684 | + $data['storage_mtime'] = $data['mtime']; |
|
685 | + $data['permissions'] = $permissions; |
|
686 | + |
|
687 | + return $data; |
|
688 | + } |
|
689 | + |
|
690 | + /** |
|
691 | + * @param string $path |
|
692 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
693 | + * @param \OCP\Lock\ILockingProvider $provider |
|
694 | + * @throws \OCP\Lock\LockedException |
|
695 | + */ |
|
696 | + public function acquireLock($path, $type, ILockingProvider $provider) { |
|
697 | + $logger = $this->getLockLogger(); |
|
698 | + if ($logger) { |
|
699 | + $typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive'; |
|
700 | + $logger->info( |
|
701 | + sprintf( |
|
702 | + 'acquire %s lock on "%s" on storage "%s"', |
|
703 | + $typeString, |
|
704 | + $path, |
|
705 | + $this->getId() |
|
706 | + ), |
|
707 | + [ |
|
708 | + 'app' => 'locking', |
|
709 | + ] |
|
710 | + ); |
|
711 | + } |
|
712 | + try { |
|
713 | + $provider->acquireLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type); |
|
714 | + } catch (LockedException $e) { |
|
715 | + if ($logger) { |
|
716 | + $logger->logException($e); |
|
717 | + } |
|
718 | + throw $e; |
|
719 | + } |
|
720 | + } |
|
721 | + |
|
722 | + /** |
|
723 | + * @param string $path |
|
724 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
725 | + * @param \OCP\Lock\ILockingProvider $provider |
|
726 | + * @throws \OCP\Lock\LockedException |
|
727 | + */ |
|
728 | + public function releaseLock($path, $type, ILockingProvider $provider) { |
|
729 | + $logger = $this->getLockLogger(); |
|
730 | + if ($logger) { |
|
731 | + $typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive'; |
|
732 | + $logger->info( |
|
733 | + sprintf( |
|
734 | + 'release %s lock on "%s" on storage "%s"', |
|
735 | + $typeString, |
|
736 | + $path, |
|
737 | + $this->getId() |
|
738 | + ), |
|
739 | + [ |
|
740 | + 'app' => 'locking', |
|
741 | + ] |
|
742 | + ); |
|
743 | + } |
|
744 | + try { |
|
745 | + $provider->releaseLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type); |
|
746 | + } catch (LockedException $e) { |
|
747 | + if ($logger) { |
|
748 | + $logger->logException($e); |
|
749 | + } |
|
750 | + throw $e; |
|
751 | + } |
|
752 | + } |
|
753 | + |
|
754 | + /** |
|
755 | + * @param string $path |
|
756 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
757 | + * @param \OCP\Lock\ILockingProvider $provider |
|
758 | + * @throws \OCP\Lock\LockedException |
|
759 | + */ |
|
760 | + public function changeLock($path, $type, ILockingProvider $provider) { |
|
761 | + $logger = $this->getLockLogger(); |
|
762 | + if ($logger) { |
|
763 | + $typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive'; |
|
764 | + $logger->info( |
|
765 | + sprintf( |
|
766 | + 'change lock on "%s" to %s on storage "%s"', |
|
767 | + $path, |
|
768 | + $typeString, |
|
769 | + $this->getId() |
|
770 | + ), |
|
771 | + [ |
|
772 | + 'app' => 'locking', |
|
773 | + ] |
|
774 | + ); |
|
775 | + } |
|
776 | + try { |
|
777 | + $provider->changeLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type); |
|
778 | + } catch (LockedException $e) { |
|
779 | + if ($logger) { |
|
780 | + $logger->logException($e); |
|
781 | + } |
|
782 | + throw $e; |
|
783 | + } |
|
784 | + } |
|
785 | + |
|
786 | + private function getLockLogger() { |
|
787 | + if (is_null($this->shouldLogLocks)) { |
|
788 | + $this->shouldLogLocks = \OC::$server->getConfig()->getSystemValue('filelocking.debug', false); |
|
789 | + $this->logger = $this->shouldLogLocks ? \OC::$server->getLogger() : null; |
|
790 | + } |
|
791 | + return $this->logger; |
|
792 | + } |
|
793 | + |
|
794 | + /** |
|
795 | + * @return array [ available, last_checked ] |
|
796 | + */ |
|
797 | + public function getAvailability() { |
|
798 | + return $this->getStorageCache()->getAvailability(); |
|
799 | + } |
|
800 | + |
|
801 | + /** |
|
802 | + * @param bool $isAvailable |
|
803 | + */ |
|
804 | + public function setAvailability($isAvailable) { |
|
805 | + $this->getStorageCache()->setAvailability($isAvailable); |
|
806 | + } |
|
807 | + |
|
808 | + /** |
|
809 | + * @return bool |
|
810 | + */ |
|
811 | + public function needsPartFile() { |
|
812 | + return true; |
|
813 | + } |
|
814 | 814 | } |
@@ -53,501 +53,501 @@ |
||
53 | 53 | * @package OC\Files\Cache |
54 | 54 | */ |
55 | 55 | class Scanner extends BasicEmitter implements IScanner { |
56 | - /** |
|
57 | - * @var \OC\Files\Storage\Storage $storage |
|
58 | - */ |
|
59 | - protected $storage; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var string $storageId |
|
63 | - */ |
|
64 | - protected $storageId; |
|
65 | - |
|
66 | - /** |
|
67 | - * @var \OC\Files\Cache\Cache $cache |
|
68 | - */ |
|
69 | - protected $cache; |
|
70 | - |
|
71 | - /** |
|
72 | - * @var boolean $cacheActive If true, perform cache operations, if false, do not affect cache |
|
73 | - */ |
|
74 | - protected $cacheActive; |
|
75 | - |
|
76 | - /** |
|
77 | - * @var bool $useTransactions whether to use transactions |
|
78 | - */ |
|
79 | - protected $useTransactions = true; |
|
80 | - |
|
81 | - /** |
|
82 | - * @var \OCP\Lock\ILockingProvider |
|
83 | - */ |
|
84 | - protected $lockingProvider; |
|
85 | - |
|
86 | - public function __construct(\OC\Files\Storage\Storage $storage) { |
|
87 | - $this->storage = $storage; |
|
88 | - $this->storageId = $this->storage->getId(); |
|
89 | - $this->cache = $storage->getCache(); |
|
90 | - $this->cacheActive = !\OC::$server->getConfig()->getSystemValue('filesystem_cache_readonly', false); |
|
91 | - $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Whether to wrap the scanning of a folder in a database transaction |
|
96 | - * On default transactions are used |
|
97 | - * |
|
98 | - * @param bool $useTransactions |
|
99 | - */ |
|
100 | - public function setUseTransactions($useTransactions) { |
|
101 | - $this->useTransactions = $useTransactions; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * get all the metadata of a file or folder |
|
106 | - * * |
|
107 | - * |
|
108 | - * @param string $path |
|
109 | - * @return array an array of metadata of the file |
|
110 | - */ |
|
111 | - protected function getData($path) { |
|
112 | - $data = $this->storage->getMetaData($path); |
|
113 | - if (is_null($data)) { |
|
114 | - \OCP\Util::writeLog(Scanner::class, "!!! Path '$path' is not accessible or present !!!", ILogger::DEBUG); |
|
115 | - } |
|
116 | - return $data; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * scan a single file and store it in the cache |
|
121 | - * |
|
122 | - * @param string $file |
|
123 | - * @param int $reuseExisting |
|
124 | - * @param int $parentId |
|
125 | - * @param array | null $cacheData existing data in the cache for the file to be scanned |
|
126 | - * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
127 | - * @return array an array of metadata of the scanned file |
|
128 | - * @throws \OC\ServerNotAvailableException |
|
129 | - * @throws \OCP\Lock\LockedException |
|
130 | - */ |
|
131 | - public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true) { |
|
132 | - if ($file !== '') { |
|
133 | - try { |
|
134 | - $this->storage->verifyPath(dirname($file), basename($file)); |
|
135 | - } catch (\Exception $e) { |
|
136 | - return null; |
|
137 | - } |
|
138 | - } |
|
139 | - // only proceed if $file is not a partial file nor a blacklisted file |
|
140 | - if (!self::isPartialFile($file) and !Filesystem::isFileBlacklisted($file)) { |
|
141 | - |
|
142 | - //acquire a lock |
|
143 | - if ($lock) { |
|
144 | - if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
145 | - $this->storage->acquireLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
146 | - } |
|
147 | - } |
|
148 | - |
|
149 | - try { |
|
150 | - $data = $this->getData($file); |
|
151 | - } catch (ForbiddenException $e) { |
|
152 | - if ($lock) { |
|
153 | - if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
154 | - $this->storage->releaseLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
155 | - } |
|
156 | - } |
|
157 | - |
|
158 | - return null; |
|
159 | - } |
|
160 | - |
|
161 | - try { |
|
162 | - if ($data) { |
|
163 | - |
|
164 | - // pre-emit only if it was a file. By that we avoid counting/treating folders as files |
|
165 | - if ($data['mimetype'] !== 'httpd/unix-directory') { |
|
166 | - $this->emit('\OC\Files\Cache\Scanner', 'scanFile', array($file, $this->storageId)); |
|
167 | - \OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_file', array('path' => $file, 'storage' => $this->storageId)); |
|
168 | - } |
|
169 | - |
|
170 | - $parent = dirname($file); |
|
171 | - if ($parent === '.' or $parent === '/') { |
|
172 | - $parent = ''; |
|
173 | - } |
|
174 | - if ($parentId === -1) { |
|
175 | - $parentId = $this->cache->getParentId($file); |
|
176 | - } |
|
177 | - |
|
178 | - // scan the parent if it's not in the cache (id -1) and the current file is not the root folder |
|
179 | - if ($file and $parentId === -1) { |
|
180 | - $parentData = $this->scanFile($parent); |
|
181 | - if (!$parentData) { |
|
182 | - return null; |
|
183 | - } |
|
184 | - $parentId = $parentData['fileid']; |
|
185 | - } |
|
186 | - if ($parent) { |
|
187 | - $data['parent'] = $parentId; |
|
188 | - } |
|
189 | - if (is_null($cacheData)) { |
|
190 | - /** @var CacheEntry $cacheData */ |
|
191 | - $cacheData = $this->cache->get($file); |
|
192 | - } |
|
193 | - if ($cacheData and $reuseExisting and isset($cacheData['fileid'])) { |
|
194 | - // prevent empty etag |
|
195 | - if (empty($cacheData['etag'])) { |
|
196 | - $etag = $data['etag']; |
|
197 | - } else { |
|
198 | - $etag = $cacheData['etag']; |
|
199 | - } |
|
200 | - $fileId = $cacheData['fileid']; |
|
201 | - $data['fileid'] = $fileId; |
|
202 | - // only reuse data if the file hasn't explicitly changed |
|
203 | - if (isset($data['storage_mtime']) && isset($cacheData['storage_mtime']) && $data['storage_mtime'] === $cacheData['storage_mtime']) { |
|
204 | - $data['mtime'] = $cacheData['mtime']; |
|
205 | - if (($reuseExisting & self::REUSE_SIZE) && ($data['size'] === -1)) { |
|
206 | - $data['size'] = $cacheData['size']; |
|
207 | - } |
|
208 | - if ($reuseExisting & self::REUSE_ETAG) { |
|
209 | - $data['etag'] = $etag; |
|
210 | - } |
|
211 | - } |
|
212 | - // Only update metadata that has changed |
|
213 | - $newData = array_diff_assoc($data, $cacheData->getData()); |
|
214 | - } else { |
|
215 | - $newData = $data; |
|
216 | - $fileId = -1; |
|
217 | - } |
|
218 | - if (!empty($newData)) { |
|
219 | - // Reset the checksum if the data has changed |
|
220 | - $newData['checksum'] = ''; |
|
221 | - $data['fileid'] = $this->addToCache($file, $newData, $fileId); |
|
222 | - } |
|
223 | - if (isset($cacheData['size'])) { |
|
224 | - $data['oldSize'] = $cacheData['size']; |
|
225 | - } else { |
|
226 | - $data['oldSize'] = 0; |
|
227 | - } |
|
228 | - |
|
229 | - if (isset($cacheData['encrypted'])) { |
|
230 | - $data['encrypted'] = $cacheData['encrypted']; |
|
231 | - } |
|
232 | - |
|
233 | - // post-emit only if it was a file. By that we avoid counting/treating folders as files |
|
234 | - if ($data['mimetype'] !== 'httpd/unix-directory') { |
|
235 | - $this->emit('\OC\Files\Cache\Scanner', 'postScanFile', array($file, $this->storageId)); |
|
236 | - \OC_Hook::emit('\OC\Files\Cache\Scanner', 'post_scan_file', array('path' => $file, 'storage' => $this->storageId)); |
|
237 | - } |
|
238 | - |
|
239 | - } else { |
|
240 | - $this->removeFromCache($file); |
|
241 | - } |
|
242 | - } catch (\Exception $e) { |
|
243 | - if ($lock) { |
|
244 | - if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
245 | - $this->storage->releaseLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
246 | - } |
|
247 | - } |
|
248 | - throw $e; |
|
249 | - } |
|
250 | - |
|
251 | - //release the acquired lock |
|
252 | - if ($lock) { |
|
253 | - if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
254 | - $this->storage->releaseLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
255 | - } |
|
256 | - } |
|
257 | - |
|
258 | - if ($data && !isset($data['encrypted'])) { |
|
259 | - $data['encrypted'] = false; |
|
260 | - } |
|
261 | - return $data; |
|
262 | - } |
|
263 | - |
|
264 | - return null; |
|
265 | - } |
|
266 | - |
|
267 | - protected function removeFromCache($path) { |
|
268 | - \OC_Hook::emit('Scanner', 'removeFromCache', array('file' => $path)); |
|
269 | - $this->emit('\OC\Files\Cache\Scanner', 'removeFromCache', array($path)); |
|
270 | - if ($this->cacheActive) { |
|
271 | - $this->cache->remove($path); |
|
272 | - } |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * @param string $path |
|
277 | - * @param array $data |
|
278 | - * @param int $fileId |
|
279 | - * @return int the id of the added file |
|
280 | - */ |
|
281 | - protected function addToCache($path, $data, $fileId = -1) { |
|
282 | - if (isset($data['scan_permissions'])) { |
|
283 | - $data['permissions'] = $data['scan_permissions']; |
|
284 | - } |
|
285 | - \OC_Hook::emit('Scanner', 'addToCache', array('file' => $path, 'data' => $data)); |
|
286 | - $this->emit('\OC\Files\Cache\Scanner', 'addToCache', array($path, $this->storageId, $data)); |
|
287 | - if ($this->cacheActive) { |
|
288 | - if ($fileId !== -1) { |
|
289 | - $this->cache->update($fileId, $data); |
|
290 | - return $fileId; |
|
291 | - } else { |
|
292 | - return $this->cache->put($path, $data); |
|
293 | - } |
|
294 | - } else { |
|
295 | - return -1; |
|
296 | - } |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * @param string $path |
|
301 | - * @param array $data |
|
302 | - * @param int $fileId |
|
303 | - */ |
|
304 | - protected function updateCache($path, $data, $fileId = -1) { |
|
305 | - \OC_Hook::emit('Scanner', 'addToCache', array('file' => $path, 'data' => $data)); |
|
306 | - $this->emit('\OC\Files\Cache\Scanner', 'updateCache', array($path, $this->storageId, $data)); |
|
307 | - if ($this->cacheActive) { |
|
308 | - if ($fileId !== -1) { |
|
309 | - $this->cache->update($fileId, $data); |
|
310 | - } else { |
|
311 | - $this->cache->put($path, $data); |
|
312 | - } |
|
313 | - } |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * scan a folder and all it's children |
|
318 | - * |
|
319 | - * @param string $path |
|
320 | - * @param bool $recursive |
|
321 | - * @param int $reuse |
|
322 | - * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
323 | - * @return array an array of the meta data of the scanned file or folder |
|
324 | - */ |
|
325 | - public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) { |
|
326 | - if ($reuse === -1) { |
|
327 | - $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : self::REUSE_ETAG; |
|
328 | - } |
|
329 | - if ($lock) { |
|
330 | - if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
331 | - $this->storage->acquireLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
332 | - $this->storage->acquireLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
333 | - } |
|
334 | - } |
|
335 | - $data = $this->scanFile($path, $reuse, -1, null, $lock); |
|
336 | - if ($data and $data['mimetype'] === 'httpd/unix-directory') { |
|
337 | - $size = $this->scanChildren($path, $recursive, $reuse, $data['fileid'], $lock); |
|
338 | - $data['size'] = $size; |
|
339 | - } |
|
340 | - if ($lock) { |
|
341 | - if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
342 | - $this->storage->releaseLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
343 | - $this->storage->releaseLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
344 | - } |
|
345 | - } |
|
346 | - return $data; |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * Get the children currently in the cache |
|
351 | - * |
|
352 | - * @param int $folderId |
|
353 | - * @return array[] |
|
354 | - */ |
|
355 | - protected function getExistingChildren($folderId) { |
|
356 | - $existingChildren = array(); |
|
357 | - $children = $this->cache->getFolderContentsById($folderId); |
|
358 | - foreach ($children as $child) { |
|
359 | - $existingChildren[$child['name']] = $child; |
|
360 | - } |
|
361 | - return $existingChildren; |
|
362 | - } |
|
363 | - |
|
364 | - /** |
|
365 | - * Get the children from the storage |
|
366 | - * |
|
367 | - * @param string $folder |
|
368 | - * @return string[] |
|
369 | - */ |
|
370 | - protected function getNewChildren($folder) { |
|
371 | - $children = array(); |
|
372 | - if ($dh = $this->storage->opendir($folder)) { |
|
373 | - if (is_resource($dh)) { |
|
374 | - while (($file = readdir($dh)) !== false) { |
|
375 | - if (!Filesystem::isIgnoredDir($file)) { |
|
376 | - $children[] = trim(\OC\Files\Filesystem::normalizePath($file), '/'); |
|
377 | - } |
|
378 | - } |
|
379 | - } |
|
380 | - } |
|
381 | - return $children; |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * scan all the files and folders in a folder |
|
386 | - * |
|
387 | - * @param string $path |
|
388 | - * @param bool $recursive |
|
389 | - * @param int $reuse |
|
390 | - * @param int $folderId id for the folder to be scanned |
|
391 | - * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
392 | - * @return int the size of the scanned folder or -1 if the size is unknown at this stage |
|
393 | - */ |
|
394 | - protected function scanChildren($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $folderId = null, $lock = true) { |
|
395 | - if ($reuse === -1) { |
|
396 | - $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : self::REUSE_ETAG; |
|
397 | - } |
|
398 | - $this->emit('\OC\Files\Cache\Scanner', 'scanFolder', array($path, $this->storageId)); |
|
399 | - $size = 0; |
|
400 | - if (!is_null($folderId)) { |
|
401 | - $folderId = $this->cache->getId($path); |
|
402 | - } |
|
403 | - $childQueue = $this->handleChildren($path, $recursive, $reuse, $folderId, $lock, $size); |
|
404 | - |
|
405 | - foreach ($childQueue as $child => $childId) { |
|
406 | - $childSize = $this->scanChildren($child, $recursive, $reuse, $childId, $lock); |
|
407 | - if ($childSize === -1) { |
|
408 | - $size = -1; |
|
409 | - } else if ($size !== -1) { |
|
410 | - $size += $childSize; |
|
411 | - } |
|
412 | - } |
|
413 | - if ($this->cacheActive) { |
|
414 | - $this->cache->update($folderId, array('size' => $size)); |
|
415 | - } |
|
416 | - $this->emit('\OC\Files\Cache\Scanner', 'postScanFolder', array($path, $this->storageId)); |
|
417 | - return $size; |
|
418 | - } |
|
419 | - |
|
420 | - private function handleChildren($path, $recursive, $reuse, $folderId, $lock, &$size) { |
|
421 | - // we put this in it's own function so it cleans up the memory before we start recursing |
|
422 | - $existingChildren = $this->getExistingChildren($folderId); |
|
423 | - $newChildren = $this->getNewChildren($path); |
|
424 | - |
|
425 | - if ($this->useTransactions) { |
|
426 | - \OC::$server->getDatabaseConnection()->beginTransaction(); |
|
427 | - } |
|
428 | - |
|
429 | - $exceptionOccurred = false; |
|
430 | - $childQueue = []; |
|
431 | - foreach ($newChildren as $file) { |
|
432 | - $child = $path ? $path . '/' . $file : $file; |
|
433 | - try { |
|
434 | - $existingData = isset($existingChildren[$file]) ? $existingChildren[$file] : null; |
|
435 | - $data = $this->scanFile($child, $reuse, $folderId, $existingData, $lock); |
|
436 | - if ($data) { |
|
437 | - if ($data['mimetype'] === 'httpd/unix-directory' and $recursive === self::SCAN_RECURSIVE) { |
|
438 | - $childQueue[$child] = $data['fileid']; |
|
439 | - } else if ($data['mimetype'] === 'httpd/unix-directory' and $recursive === self::SCAN_RECURSIVE_INCOMPLETE and $data['size'] === -1) { |
|
440 | - // only recurse into folders which aren't fully scanned |
|
441 | - $childQueue[$child] = $data['fileid']; |
|
442 | - } else if ($data['size'] === -1) { |
|
443 | - $size = -1; |
|
444 | - } else if ($size !== -1) { |
|
445 | - $size += $data['size']; |
|
446 | - } |
|
447 | - } |
|
448 | - } catch (\Doctrine\DBAL\DBALException $ex) { |
|
449 | - // might happen if inserting duplicate while a scanning |
|
450 | - // process is running in parallel |
|
451 | - // log and ignore |
|
452 | - if ($this->useTransactions) { |
|
453 | - \OC::$server->getDatabaseConnection()->rollback(); |
|
454 | - \OC::$server->getDatabaseConnection()->beginTransaction(); |
|
455 | - } |
|
456 | - \OC::$server->getLogger()->logException($ex, [ |
|
457 | - 'message' => 'Exception while scanning file "' . $child . '"', |
|
458 | - 'level' => ILogger::DEBUG, |
|
459 | - 'app' => 'core', |
|
460 | - ]); |
|
461 | - $exceptionOccurred = true; |
|
462 | - } catch (\OCP\Lock\LockedException $e) { |
|
463 | - if ($this->useTransactions) { |
|
464 | - \OC::$server->getDatabaseConnection()->rollback(); |
|
465 | - } |
|
466 | - throw $e; |
|
467 | - } |
|
468 | - } |
|
469 | - $removedChildren = \array_diff(array_keys($existingChildren), $newChildren); |
|
470 | - foreach ($removedChildren as $childName) { |
|
471 | - $child = $path ? $path . '/' . $childName : $childName; |
|
472 | - $this->removeFromCache($child); |
|
473 | - } |
|
474 | - if ($this->useTransactions) { |
|
475 | - \OC::$server->getDatabaseConnection()->commit(); |
|
476 | - } |
|
477 | - if ($exceptionOccurred) { |
|
478 | - // It might happen that the parallel scan process has already |
|
479 | - // inserted mimetypes but those weren't available yet inside the transaction |
|
480 | - // To make sure to have the updated mime types in such cases, |
|
481 | - // we reload them here |
|
482 | - \OC::$server->getMimeTypeLoader()->reset(); |
|
483 | - } |
|
484 | - return $childQueue; |
|
485 | - } |
|
486 | - |
|
487 | - /** |
|
488 | - * check if the file should be ignored when scanning |
|
489 | - * NOTE: files with a '.part' extension are ignored as well! |
|
490 | - * prevents unfinished put requests to be scanned |
|
491 | - * |
|
492 | - * @param string $file |
|
493 | - * @return boolean |
|
494 | - */ |
|
495 | - public static function isPartialFile($file) { |
|
496 | - if (pathinfo($file, PATHINFO_EXTENSION) === 'part') { |
|
497 | - return true; |
|
498 | - } |
|
499 | - if (strpos($file, '.part/') !== false) { |
|
500 | - return true; |
|
501 | - } |
|
502 | - |
|
503 | - return false; |
|
504 | - } |
|
505 | - |
|
506 | - /** |
|
507 | - * walk over any folders that are not fully scanned yet and scan them |
|
508 | - */ |
|
509 | - public function backgroundScan() { |
|
510 | - if (!$this->cache->inCache('')) { |
|
511 | - $this->runBackgroundScanJob(function () { |
|
512 | - $this->scan('', self::SCAN_RECURSIVE, self::REUSE_ETAG); |
|
513 | - }, ''); |
|
514 | - } else { |
|
515 | - $lastPath = null; |
|
516 | - while (($path = $this->cache->getIncomplete()) !== false && $path !== $lastPath) { |
|
517 | - $this->runBackgroundScanJob(function () use ($path) { |
|
518 | - $this->scan($path, self::SCAN_RECURSIVE_INCOMPLETE, self::REUSE_ETAG | self::REUSE_SIZE); |
|
519 | - }, $path); |
|
520 | - // FIXME: this won't proceed with the next item, needs revamping of getIncomplete() |
|
521 | - // to make this possible |
|
522 | - $lastPath = $path; |
|
523 | - } |
|
524 | - } |
|
525 | - } |
|
526 | - |
|
527 | - private function runBackgroundScanJob(callable $callback, $path) { |
|
528 | - try { |
|
529 | - $callback(); |
|
530 | - \OC_Hook::emit('Scanner', 'correctFolderSize', array('path' => $path)); |
|
531 | - if ($this->cacheActive && $this->cache instanceof Cache) { |
|
532 | - $this->cache->correctFolderSize($path); |
|
533 | - } |
|
534 | - } catch (\OCP\Files\StorageInvalidException $e) { |
|
535 | - // skip unavailable storages |
|
536 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
537 | - // skip unavailable storages |
|
538 | - } catch (\OCP\Files\ForbiddenException $e) { |
|
539 | - // skip forbidden storages |
|
540 | - } catch (\OCP\Lock\LockedException $e) { |
|
541 | - // skip unavailable storages |
|
542 | - } |
|
543 | - } |
|
544 | - |
|
545 | - /** |
|
546 | - * Set whether the cache is affected by scan operations |
|
547 | - * |
|
548 | - * @param boolean $active The active state of the cache |
|
549 | - */ |
|
550 | - public function setCacheActive($active) { |
|
551 | - $this->cacheActive = $active; |
|
552 | - } |
|
56 | + /** |
|
57 | + * @var \OC\Files\Storage\Storage $storage |
|
58 | + */ |
|
59 | + protected $storage; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var string $storageId |
|
63 | + */ |
|
64 | + protected $storageId; |
|
65 | + |
|
66 | + /** |
|
67 | + * @var \OC\Files\Cache\Cache $cache |
|
68 | + */ |
|
69 | + protected $cache; |
|
70 | + |
|
71 | + /** |
|
72 | + * @var boolean $cacheActive If true, perform cache operations, if false, do not affect cache |
|
73 | + */ |
|
74 | + protected $cacheActive; |
|
75 | + |
|
76 | + /** |
|
77 | + * @var bool $useTransactions whether to use transactions |
|
78 | + */ |
|
79 | + protected $useTransactions = true; |
|
80 | + |
|
81 | + /** |
|
82 | + * @var \OCP\Lock\ILockingProvider |
|
83 | + */ |
|
84 | + protected $lockingProvider; |
|
85 | + |
|
86 | + public function __construct(\OC\Files\Storage\Storage $storage) { |
|
87 | + $this->storage = $storage; |
|
88 | + $this->storageId = $this->storage->getId(); |
|
89 | + $this->cache = $storage->getCache(); |
|
90 | + $this->cacheActive = !\OC::$server->getConfig()->getSystemValue('filesystem_cache_readonly', false); |
|
91 | + $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Whether to wrap the scanning of a folder in a database transaction |
|
96 | + * On default transactions are used |
|
97 | + * |
|
98 | + * @param bool $useTransactions |
|
99 | + */ |
|
100 | + public function setUseTransactions($useTransactions) { |
|
101 | + $this->useTransactions = $useTransactions; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * get all the metadata of a file or folder |
|
106 | + * * |
|
107 | + * |
|
108 | + * @param string $path |
|
109 | + * @return array an array of metadata of the file |
|
110 | + */ |
|
111 | + protected function getData($path) { |
|
112 | + $data = $this->storage->getMetaData($path); |
|
113 | + if (is_null($data)) { |
|
114 | + \OCP\Util::writeLog(Scanner::class, "!!! Path '$path' is not accessible or present !!!", ILogger::DEBUG); |
|
115 | + } |
|
116 | + return $data; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * scan a single file and store it in the cache |
|
121 | + * |
|
122 | + * @param string $file |
|
123 | + * @param int $reuseExisting |
|
124 | + * @param int $parentId |
|
125 | + * @param array | null $cacheData existing data in the cache for the file to be scanned |
|
126 | + * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
127 | + * @return array an array of metadata of the scanned file |
|
128 | + * @throws \OC\ServerNotAvailableException |
|
129 | + * @throws \OCP\Lock\LockedException |
|
130 | + */ |
|
131 | + public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true) { |
|
132 | + if ($file !== '') { |
|
133 | + try { |
|
134 | + $this->storage->verifyPath(dirname($file), basename($file)); |
|
135 | + } catch (\Exception $e) { |
|
136 | + return null; |
|
137 | + } |
|
138 | + } |
|
139 | + // only proceed if $file is not a partial file nor a blacklisted file |
|
140 | + if (!self::isPartialFile($file) and !Filesystem::isFileBlacklisted($file)) { |
|
141 | + |
|
142 | + //acquire a lock |
|
143 | + if ($lock) { |
|
144 | + if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
145 | + $this->storage->acquireLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
146 | + } |
|
147 | + } |
|
148 | + |
|
149 | + try { |
|
150 | + $data = $this->getData($file); |
|
151 | + } catch (ForbiddenException $e) { |
|
152 | + if ($lock) { |
|
153 | + if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
154 | + $this->storage->releaseLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
155 | + } |
|
156 | + } |
|
157 | + |
|
158 | + return null; |
|
159 | + } |
|
160 | + |
|
161 | + try { |
|
162 | + if ($data) { |
|
163 | + |
|
164 | + // pre-emit only if it was a file. By that we avoid counting/treating folders as files |
|
165 | + if ($data['mimetype'] !== 'httpd/unix-directory') { |
|
166 | + $this->emit('\OC\Files\Cache\Scanner', 'scanFile', array($file, $this->storageId)); |
|
167 | + \OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_file', array('path' => $file, 'storage' => $this->storageId)); |
|
168 | + } |
|
169 | + |
|
170 | + $parent = dirname($file); |
|
171 | + if ($parent === '.' or $parent === '/') { |
|
172 | + $parent = ''; |
|
173 | + } |
|
174 | + if ($parentId === -1) { |
|
175 | + $parentId = $this->cache->getParentId($file); |
|
176 | + } |
|
177 | + |
|
178 | + // scan the parent if it's not in the cache (id -1) and the current file is not the root folder |
|
179 | + if ($file and $parentId === -1) { |
|
180 | + $parentData = $this->scanFile($parent); |
|
181 | + if (!$parentData) { |
|
182 | + return null; |
|
183 | + } |
|
184 | + $parentId = $parentData['fileid']; |
|
185 | + } |
|
186 | + if ($parent) { |
|
187 | + $data['parent'] = $parentId; |
|
188 | + } |
|
189 | + if (is_null($cacheData)) { |
|
190 | + /** @var CacheEntry $cacheData */ |
|
191 | + $cacheData = $this->cache->get($file); |
|
192 | + } |
|
193 | + if ($cacheData and $reuseExisting and isset($cacheData['fileid'])) { |
|
194 | + // prevent empty etag |
|
195 | + if (empty($cacheData['etag'])) { |
|
196 | + $etag = $data['etag']; |
|
197 | + } else { |
|
198 | + $etag = $cacheData['etag']; |
|
199 | + } |
|
200 | + $fileId = $cacheData['fileid']; |
|
201 | + $data['fileid'] = $fileId; |
|
202 | + // only reuse data if the file hasn't explicitly changed |
|
203 | + if (isset($data['storage_mtime']) && isset($cacheData['storage_mtime']) && $data['storage_mtime'] === $cacheData['storage_mtime']) { |
|
204 | + $data['mtime'] = $cacheData['mtime']; |
|
205 | + if (($reuseExisting & self::REUSE_SIZE) && ($data['size'] === -1)) { |
|
206 | + $data['size'] = $cacheData['size']; |
|
207 | + } |
|
208 | + if ($reuseExisting & self::REUSE_ETAG) { |
|
209 | + $data['etag'] = $etag; |
|
210 | + } |
|
211 | + } |
|
212 | + // Only update metadata that has changed |
|
213 | + $newData = array_diff_assoc($data, $cacheData->getData()); |
|
214 | + } else { |
|
215 | + $newData = $data; |
|
216 | + $fileId = -1; |
|
217 | + } |
|
218 | + if (!empty($newData)) { |
|
219 | + // Reset the checksum if the data has changed |
|
220 | + $newData['checksum'] = ''; |
|
221 | + $data['fileid'] = $this->addToCache($file, $newData, $fileId); |
|
222 | + } |
|
223 | + if (isset($cacheData['size'])) { |
|
224 | + $data['oldSize'] = $cacheData['size']; |
|
225 | + } else { |
|
226 | + $data['oldSize'] = 0; |
|
227 | + } |
|
228 | + |
|
229 | + if (isset($cacheData['encrypted'])) { |
|
230 | + $data['encrypted'] = $cacheData['encrypted']; |
|
231 | + } |
|
232 | + |
|
233 | + // post-emit only if it was a file. By that we avoid counting/treating folders as files |
|
234 | + if ($data['mimetype'] !== 'httpd/unix-directory') { |
|
235 | + $this->emit('\OC\Files\Cache\Scanner', 'postScanFile', array($file, $this->storageId)); |
|
236 | + \OC_Hook::emit('\OC\Files\Cache\Scanner', 'post_scan_file', array('path' => $file, 'storage' => $this->storageId)); |
|
237 | + } |
|
238 | + |
|
239 | + } else { |
|
240 | + $this->removeFromCache($file); |
|
241 | + } |
|
242 | + } catch (\Exception $e) { |
|
243 | + if ($lock) { |
|
244 | + if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
245 | + $this->storage->releaseLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
246 | + } |
|
247 | + } |
|
248 | + throw $e; |
|
249 | + } |
|
250 | + |
|
251 | + //release the acquired lock |
|
252 | + if ($lock) { |
|
253 | + if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
254 | + $this->storage->releaseLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
255 | + } |
|
256 | + } |
|
257 | + |
|
258 | + if ($data && !isset($data['encrypted'])) { |
|
259 | + $data['encrypted'] = false; |
|
260 | + } |
|
261 | + return $data; |
|
262 | + } |
|
263 | + |
|
264 | + return null; |
|
265 | + } |
|
266 | + |
|
267 | + protected function removeFromCache($path) { |
|
268 | + \OC_Hook::emit('Scanner', 'removeFromCache', array('file' => $path)); |
|
269 | + $this->emit('\OC\Files\Cache\Scanner', 'removeFromCache', array($path)); |
|
270 | + if ($this->cacheActive) { |
|
271 | + $this->cache->remove($path); |
|
272 | + } |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * @param string $path |
|
277 | + * @param array $data |
|
278 | + * @param int $fileId |
|
279 | + * @return int the id of the added file |
|
280 | + */ |
|
281 | + protected function addToCache($path, $data, $fileId = -1) { |
|
282 | + if (isset($data['scan_permissions'])) { |
|
283 | + $data['permissions'] = $data['scan_permissions']; |
|
284 | + } |
|
285 | + \OC_Hook::emit('Scanner', 'addToCache', array('file' => $path, 'data' => $data)); |
|
286 | + $this->emit('\OC\Files\Cache\Scanner', 'addToCache', array($path, $this->storageId, $data)); |
|
287 | + if ($this->cacheActive) { |
|
288 | + if ($fileId !== -1) { |
|
289 | + $this->cache->update($fileId, $data); |
|
290 | + return $fileId; |
|
291 | + } else { |
|
292 | + return $this->cache->put($path, $data); |
|
293 | + } |
|
294 | + } else { |
|
295 | + return -1; |
|
296 | + } |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * @param string $path |
|
301 | + * @param array $data |
|
302 | + * @param int $fileId |
|
303 | + */ |
|
304 | + protected function updateCache($path, $data, $fileId = -1) { |
|
305 | + \OC_Hook::emit('Scanner', 'addToCache', array('file' => $path, 'data' => $data)); |
|
306 | + $this->emit('\OC\Files\Cache\Scanner', 'updateCache', array($path, $this->storageId, $data)); |
|
307 | + if ($this->cacheActive) { |
|
308 | + if ($fileId !== -1) { |
|
309 | + $this->cache->update($fileId, $data); |
|
310 | + } else { |
|
311 | + $this->cache->put($path, $data); |
|
312 | + } |
|
313 | + } |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * scan a folder and all it's children |
|
318 | + * |
|
319 | + * @param string $path |
|
320 | + * @param bool $recursive |
|
321 | + * @param int $reuse |
|
322 | + * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
323 | + * @return array an array of the meta data of the scanned file or folder |
|
324 | + */ |
|
325 | + public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) { |
|
326 | + if ($reuse === -1) { |
|
327 | + $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : self::REUSE_ETAG; |
|
328 | + } |
|
329 | + if ($lock) { |
|
330 | + if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
331 | + $this->storage->acquireLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
332 | + $this->storage->acquireLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
333 | + } |
|
334 | + } |
|
335 | + $data = $this->scanFile($path, $reuse, -1, null, $lock); |
|
336 | + if ($data and $data['mimetype'] === 'httpd/unix-directory') { |
|
337 | + $size = $this->scanChildren($path, $recursive, $reuse, $data['fileid'], $lock); |
|
338 | + $data['size'] = $size; |
|
339 | + } |
|
340 | + if ($lock) { |
|
341 | + if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
342 | + $this->storage->releaseLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
343 | + $this->storage->releaseLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
344 | + } |
|
345 | + } |
|
346 | + return $data; |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * Get the children currently in the cache |
|
351 | + * |
|
352 | + * @param int $folderId |
|
353 | + * @return array[] |
|
354 | + */ |
|
355 | + protected function getExistingChildren($folderId) { |
|
356 | + $existingChildren = array(); |
|
357 | + $children = $this->cache->getFolderContentsById($folderId); |
|
358 | + foreach ($children as $child) { |
|
359 | + $existingChildren[$child['name']] = $child; |
|
360 | + } |
|
361 | + return $existingChildren; |
|
362 | + } |
|
363 | + |
|
364 | + /** |
|
365 | + * Get the children from the storage |
|
366 | + * |
|
367 | + * @param string $folder |
|
368 | + * @return string[] |
|
369 | + */ |
|
370 | + protected function getNewChildren($folder) { |
|
371 | + $children = array(); |
|
372 | + if ($dh = $this->storage->opendir($folder)) { |
|
373 | + if (is_resource($dh)) { |
|
374 | + while (($file = readdir($dh)) !== false) { |
|
375 | + if (!Filesystem::isIgnoredDir($file)) { |
|
376 | + $children[] = trim(\OC\Files\Filesystem::normalizePath($file), '/'); |
|
377 | + } |
|
378 | + } |
|
379 | + } |
|
380 | + } |
|
381 | + return $children; |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * scan all the files and folders in a folder |
|
386 | + * |
|
387 | + * @param string $path |
|
388 | + * @param bool $recursive |
|
389 | + * @param int $reuse |
|
390 | + * @param int $folderId id for the folder to be scanned |
|
391 | + * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
392 | + * @return int the size of the scanned folder or -1 if the size is unknown at this stage |
|
393 | + */ |
|
394 | + protected function scanChildren($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $folderId = null, $lock = true) { |
|
395 | + if ($reuse === -1) { |
|
396 | + $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : self::REUSE_ETAG; |
|
397 | + } |
|
398 | + $this->emit('\OC\Files\Cache\Scanner', 'scanFolder', array($path, $this->storageId)); |
|
399 | + $size = 0; |
|
400 | + if (!is_null($folderId)) { |
|
401 | + $folderId = $this->cache->getId($path); |
|
402 | + } |
|
403 | + $childQueue = $this->handleChildren($path, $recursive, $reuse, $folderId, $lock, $size); |
|
404 | + |
|
405 | + foreach ($childQueue as $child => $childId) { |
|
406 | + $childSize = $this->scanChildren($child, $recursive, $reuse, $childId, $lock); |
|
407 | + if ($childSize === -1) { |
|
408 | + $size = -1; |
|
409 | + } else if ($size !== -1) { |
|
410 | + $size += $childSize; |
|
411 | + } |
|
412 | + } |
|
413 | + if ($this->cacheActive) { |
|
414 | + $this->cache->update($folderId, array('size' => $size)); |
|
415 | + } |
|
416 | + $this->emit('\OC\Files\Cache\Scanner', 'postScanFolder', array($path, $this->storageId)); |
|
417 | + return $size; |
|
418 | + } |
|
419 | + |
|
420 | + private function handleChildren($path, $recursive, $reuse, $folderId, $lock, &$size) { |
|
421 | + // we put this in it's own function so it cleans up the memory before we start recursing |
|
422 | + $existingChildren = $this->getExistingChildren($folderId); |
|
423 | + $newChildren = $this->getNewChildren($path); |
|
424 | + |
|
425 | + if ($this->useTransactions) { |
|
426 | + \OC::$server->getDatabaseConnection()->beginTransaction(); |
|
427 | + } |
|
428 | + |
|
429 | + $exceptionOccurred = false; |
|
430 | + $childQueue = []; |
|
431 | + foreach ($newChildren as $file) { |
|
432 | + $child = $path ? $path . '/' . $file : $file; |
|
433 | + try { |
|
434 | + $existingData = isset($existingChildren[$file]) ? $existingChildren[$file] : null; |
|
435 | + $data = $this->scanFile($child, $reuse, $folderId, $existingData, $lock); |
|
436 | + if ($data) { |
|
437 | + if ($data['mimetype'] === 'httpd/unix-directory' and $recursive === self::SCAN_RECURSIVE) { |
|
438 | + $childQueue[$child] = $data['fileid']; |
|
439 | + } else if ($data['mimetype'] === 'httpd/unix-directory' and $recursive === self::SCAN_RECURSIVE_INCOMPLETE and $data['size'] === -1) { |
|
440 | + // only recurse into folders which aren't fully scanned |
|
441 | + $childQueue[$child] = $data['fileid']; |
|
442 | + } else if ($data['size'] === -1) { |
|
443 | + $size = -1; |
|
444 | + } else if ($size !== -1) { |
|
445 | + $size += $data['size']; |
|
446 | + } |
|
447 | + } |
|
448 | + } catch (\Doctrine\DBAL\DBALException $ex) { |
|
449 | + // might happen if inserting duplicate while a scanning |
|
450 | + // process is running in parallel |
|
451 | + // log and ignore |
|
452 | + if ($this->useTransactions) { |
|
453 | + \OC::$server->getDatabaseConnection()->rollback(); |
|
454 | + \OC::$server->getDatabaseConnection()->beginTransaction(); |
|
455 | + } |
|
456 | + \OC::$server->getLogger()->logException($ex, [ |
|
457 | + 'message' => 'Exception while scanning file "' . $child . '"', |
|
458 | + 'level' => ILogger::DEBUG, |
|
459 | + 'app' => 'core', |
|
460 | + ]); |
|
461 | + $exceptionOccurred = true; |
|
462 | + } catch (\OCP\Lock\LockedException $e) { |
|
463 | + if ($this->useTransactions) { |
|
464 | + \OC::$server->getDatabaseConnection()->rollback(); |
|
465 | + } |
|
466 | + throw $e; |
|
467 | + } |
|
468 | + } |
|
469 | + $removedChildren = \array_diff(array_keys($existingChildren), $newChildren); |
|
470 | + foreach ($removedChildren as $childName) { |
|
471 | + $child = $path ? $path . '/' . $childName : $childName; |
|
472 | + $this->removeFromCache($child); |
|
473 | + } |
|
474 | + if ($this->useTransactions) { |
|
475 | + \OC::$server->getDatabaseConnection()->commit(); |
|
476 | + } |
|
477 | + if ($exceptionOccurred) { |
|
478 | + // It might happen that the parallel scan process has already |
|
479 | + // inserted mimetypes but those weren't available yet inside the transaction |
|
480 | + // To make sure to have the updated mime types in such cases, |
|
481 | + // we reload them here |
|
482 | + \OC::$server->getMimeTypeLoader()->reset(); |
|
483 | + } |
|
484 | + return $childQueue; |
|
485 | + } |
|
486 | + |
|
487 | + /** |
|
488 | + * check if the file should be ignored when scanning |
|
489 | + * NOTE: files with a '.part' extension are ignored as well! |
|
490 | + * prevents unfinished put requests to be scanned |
|
491 | + * |
|
492 | + * @param string $file |
|
493 | + * @return boolean |
|
494 | + */ |
|
495 | + public static function isPartialFile($file) { |
|
496 | + if (pathinfo($file, PATHINFO_EXTENSION) === 'part') { |
|
497 | + return true; |
|
498 | + } |
|
499 | + if (strpos($file, '.part/') !== false) { |
|
500 | + return true; |
|
501 | + } |
|
502 | + |
|
503 | + return false; |
|
504 | + } |
|
505 | + |
|
506 | + /** |
|
507 | + * walk over any folders that are not fully scanned yet and scan them |
|
508 | + */ |
|
509 | + public function backgroundScan() { |
|
510 | + if (!$this->cache->inCache('')) { |
|
511 | + $this->runBackgroundScanJob(function () { |
|
512 | + $this->scan('', self::SCAN_RECURSIVE, self::REUSE_ETAG); |
|
513 | + }, ''); |
|
514 | + } else { |
|
515 | + $lastPath = null; |
|
516 | + while (($path = $this->cache->getIncomplete()) !== false && $path !== $lastPath) { |
|
517 | + $this->runBackgroundScanJob(function () use ($path) { |
|
518 | + $this->scan($path, self::SCAN_RECURSIVE_INCOMPLETE, self::REUSE_ETAG | self::REUSE_SIZE); |
|
519 | + }, $path); |
|
520 | + // FIXME: this won't proceed with the next item, needs revamping of getIncomplete() |
|
521 | + // to make this possible |
|
522 | + $lastPath = $path; |
|
523 | + } |
|
524 | + } |
|
525 | + } |
|
526 | + |
|
527 | + private function runBackgroundScanJob(callable $callback, $path) { |
|
528 | + try { |
|
529 | + $callback(); |
|
530 | + \OC_Hook::emit('Scanner', 'correctFolderSize', array('path' => $path)); |
|
531 | + if ($this->cacheActive && $this->cache instanceof Cache) { |
|
532 | + $this->cache->correctFolderSize($path); |
|
533 | + } |
|
534 | + } catch (\OCP\Files\StorageInvalidException $e) { |
|
535 | + // skip unavailable storages |
|
536 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
537 | + // skip unavailable storages |
|
538 | + } catch (\OCP\Files\ForbiddenException $e) { |
|
539 | + // skip forbidden storages |
|
540 | + } catch (\OCP\Lock\LockedException $e) { |
|
541 | + // skip unavailable storages |
|
542 | + } |
|
543 | + } |
|
544 | + |
|
545 | + /** |
|
546 | + * Set whether the cache is affected by scan operations |
|
547 | + * |
|
548 | + * @param boolean $active The active state of the cache |
|
549 | + */ |
|
550 | + public function setCacheActive($active) { |
|
551 | + $this->cacheActive = $active; |
|
552 | + } |
|
553 | 553 | } |
@@ -31,54 +31,54 @@ |
||
31 | 31 | use OCP\Log\IWriter; |
32 | 32 | |
33 | 33 | class LogFactory implements ILogFactory { |
34 | - /** @var IServerContainer */ |
|
35 | - private $c; |
|
34 | + /** @var IServerContainer */ |
|
35 | + private $c; |
|
36 | 36 | |
37 | - public function __construct(IServerContainer $c) { |
|
38 | - $this->c = $c; |
|
39 | - } |
|
37 | + public function __construct(IServerContainer $c) { |
|
38 | + $this->c = $c; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * @param $type |
|
43 | - * @return \OC\Log\Errorlog|File|\stdClass |
|
44 | - * @throws \OCP\AppFramework\QueryException |
|
45 | - */ |
|
46 | - public function get(string $type):IWriter { |
|
47 | - switch (strtolower($type)) { |
|
48 | - case 'errorlog': |
|
49 | - return new Errorlog(); |
|
50 | - case 'syslog': |
|
51 | - return $this->c->resolve(Syslog::class); |
|
52 | - case 'file': |
|
53 | - return $this->buildLogFile(); |
|
41 | + /** |
|
42 | + * @param $type |
|
43 | + * @return \OC\Log\Errorlog|File|\stdClass |
|
44 | + * @throws \OCP\AppFramework\QueryException |
|
45 | + */ |
|
46 | + public function get(string $type):IWriter { |
|
47 | + switch (strtolower($type)) { |
|
48 | + case 'errorlog': |
|
49 | + return new Errorlog(); |
|
50 | + case 'syslog': |
|
51 | + return $this->c->resolve(Syslog::class); |
|
52 | + case 'file': |
|
53 | + return $this->buildLogFile(); |
|
54 | 54 | |
55 | - // Backwards compatibility for old and fallback for unknown log types |
|
56 | - case 'owncloud': |
|
57 | - case 'nextcloud': |
|
58 | - default: |
|
59 | - return $this->buildLogFile(); |
|
60 | - } |
|
61 | - } |
|
55 | + // Backwards compatibility for old and fallback for unknown log types |
|
56 | + case 'owncloud': |
|
57 | + case 'nextcloud': |
|
58 | + default: |
|
59 | + return $this->buildLogFile(); |
|
60 | + } |
|
61 | + } |
|
62 | 62 | |
63 | - public function getCustomLogger(string $path):ILogger { |
|
64 | - $systemConfig = null; |
|
65 | - $iconfig = $this->c->getConfig(); |
|
66 | - if($iconfig instanceof AllConfig) { |
|
67 | - // Log is bound to SystemConfig, but fetches it from \OC::$server if not supplied |
|
68 | - $systemConfig = $iconfig->getSystemConfig(); |
|
69 | - } |
|
70 | - $log = $this->buildLogFile($path); |
|
71 | - return new Log($log, $systemConfig); |
|
72 | - } |
|
63 | + public function getCustomLogger(string $path):ILogger { |
|
64 | + $systemConfig = null; |
|
65 | + $iconfig = $this->c->getConfig(); |
|
66 | + if($iconfig instanceof AllConfig) { |
|
67 | + // Log is bound to SystemConfig, but fetches it from \OC::$server if not supplied |
|
68 | + $systemConfig = $iconfig->getSystemConfig(); |
|
69 | + } |
|
70 | + $log = $this->buildLogFile($path); |
|
71 | + return new Log($log, $systemConfig); |
|
72 | + } |
|
73 | 73 | |
74 | - protected function buildLogFile(string $logFile = ''):File { |
|
75 | - $config = $this->c->getConfig(); |
|
76 | - $defaultLogFile = $config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log'; |
|
77 | - if($logFile === '') { |
|
78 | - $logFile = $config->getSystemValue('logfile', $defaultLogFile); |
|
79 | - } |
|
80 | - $fallback = $defaultLogFile !== $logFile ? $defaultLogFile : ''; |
|
74 | + protected function buildLogFile(string $logFile = ''):File { |
|
75 | + $config = $this->c->getConfig(); |
|
76 | + $defaultLogFile = $config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log'; |
|
77 | + if($logFile === '') { |
|
78 | + $logFile = $config->getSystemValue('logfile', $defaultLogFile); |
|
79 | + } |
|
80 | + $fallback = $defaultLogFile !== $logFile ? $defaultLogFile : ''; |
|
81 | 81 | |
82 | - return new File($logFile, $fallback, $config); |
|
83 | - } |
|
82 | + return new File($logFile, $fallback, $config); |
|
83 | + } |
|
84 | 84 | } |