-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
View.php
2161 lines (1967 loc) · 63.8 KB
/
View.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OC\Files;
use Icewind\Streams\CallbackWrapper;
use OC\Files\Mount\MoveableMount;
use OC\Files\Storage\Storage;
use OC\Share\Share;
use OC\User\LazyUser;
use OC\User\Manager as UserManager;
use OC\User\User;
use OCA\Files_Sharing\SharedMount;
use OCP\Constants;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\ConnectionLostException;
use OCP\Files\EmptyFileNameException;
use OCP\Files\FileNameTooLongException;
use OCP\Files\ForbiddenException;
use OCP\Files\InvalidCharacterInPathException;
use OCP\Files\InvalidDirectoryException;
use OCP\Files\InvalidPathException;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
use OCP\Files\ReservedWordException;
use OCP\IUser;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use OCP\Server;
use OCP\Share\IManager;
use OCP\Share\IShare;
use Psr\Log\LoggerInterface;
/**
* Class to provide access to ownCloud filesystem via a "view", and methods for
* working with files within that view (e.g. read, write, delete, etc.). Each
* view is restricted to a set of directories via a virtual root. The default view
* uses the currently logged in user's data directory as root (parts of
* OC_Filesystem are merely a wrapper for OC\Files\View).
*
* Apps that need to access files outside of the user data folders (to modify files
* belonging to a user other than the one currently logged in, for example) should
* use this class directly rather than using OC_Filesystem, or making use of PHP's
* built-in file manipulation functions. This will ensure all hooks and proxies
* are triggered correctly.
*
* Filesystem functions are not called directly; they are passed to the correct
* \OC\Files\Storage\Storage object
*/
class View {
private string $fakeRoot = '';
private ILockingProvider $lockingProvider;
private bool $lockingEnabled;
private bool $updaterEnabled = true;
private UserManager $userManager;
private LoggerInterface $logger;
/**
* @throws \Exception If $root contains an invalid path
*/
public function __construct(string $root = '') {
if (!Filesystem::isValidPath($root)) {
throw new \Exception();
}
$this->fakeRoot = $root;
$this->lockingProvider = \OC::$server->get(ILockingProvider::class);
$this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider);
$this->userManager = \OC::$server->getUserManager();
$this->logger = \OC::$server->get(LoggerInterface::class);
}
/**
* @param ?string $path
* @psalm-template S as string|null
* @psalm-param S $path
* @psalm-return (S is string ? string : null)
*/
public function getAbsolutePath($path = '/'): ?string {
if ($path === null) {
return null;
}
$this->assertPathLength($path);
if ($path === '') {
$path = '/';
}
if ($path[0] !== '/') {
$path = '/' . $path;
}
return $this->fakeRoot . $path;
}
/**
* Change the root to a fake root
*
* @param string $fakeRoot
*/
public function chroot($fakeRoot): void {
if (!$fakeRoot == '') {
if ($fakeRoot[0] !== '/') {
$fakeRoot = '/' . $fakeRoot;
}
}
$this->fakeRoot = $fakeRoot;
}
/**
* Get the fake root
*/
public function getRoot(): string {
return $this->fakeRoot;
}
/**
* get path relative to the root of the view
*
* @param string $path
*/
public function getRelativePath($path): ?string {
$this->assertPathLength($path);
if ($this->fakeRoot == '') {
return $path;
}
if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) {
return '/';
}
// missing slashes can cause wrong matches!
$root = rtrim($this->fakeRoot, '/') . '/';
if (!str_starts_with($path, $root)) {
return null;
} else {
$path = substr($path, strlen($this->fakeRoot));
if (strlen($path) === 0) {
return '/';
} else {
return $path;
}
}
}
/**
* Get the mountpoint of the storage object for a path
* ( note: because a storage is not always mounted inside the fakeroot, the
* returned mountpoint is relative to the absolute root of the filesystem
* and does not take the chroot into account )
*
* @param string $path
*/
public function getMountPoint($path): string {
return Filesystem::getMountPoint($this->getAbsolutePath($path));
}
/**
* Get the mountpoint of the storage object for a path
* ( note: because a storage is not always mounted inside the fakeroot, the
* returned mountpoint is relative to the absolute root of the filesystem
* and does not take the chroot into account )
*
* @param string $path
*/
public function getMount($path): IMountPoint {
return Filesystem::getMountManager()->find($this->getAbsolutePath($path));
}
/**
* Resolve a path to a storage and internal path
*
* @param string $path
* @return array{?\OCP\Files\Storage\IStorage, string} an array consisting of the storage and the internal path
*/
public function resolvePath($path): array {
$a = $this->getAbsolutePath($path);
$p = Filesystem::normalizePath($a);
return Filesystem::resolvePath($p);
}
/**
* Return the path to a local version of the file
* we need this because we can't know if a file is stored local or not from
* outside the filestorage and for some purposes a local file is needed
*
* @param string $path
*/
public function getLocalFile($path): string|false {
$parent = substr($path, 0, strrpos($path, '/') ?: 0);
$path = $this->getAbsolutePath($path);
[$storage, $internalPath] = Filesystem::resolvePath($path);
if (Filesystem::isValidPath($parent) && $storage) {
return $storage->getLocalFile($internalPath);
} else {
return false;
}
}
/**
* the following functions operate with arguments and return values identical
* to those of their PHP built-in equivalents. Mostly they are merely wrappers
* for \OC\Files\Storage\Storage via basicOperation().
*/
public function mkdir($path) {
return $this->basicOperation('mkdir', $path, ['create', 'write']);
}
/**
* remove mount point
*
* @param IMountPoint $mount
* @param string $path relative to data/
*/
protected function removeMount($mount, $path): bool {
if ($mount instanceof MoveableMount) {
// cut of /user/files to get the relative path to data/user/files
$pathParts = explode('/', $path, 4);
$relPath = '/' . $pathParts[3];
$this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true);
\OC_Hook::emit(
Filesystem::CLASSNAME, "umount",
[Filesystem::signal_param_path => $relPath]
);
$this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true);
$result = $mount->removeMount();
$this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true);
if ($result) {
\OC_Hook::emit(
Filesystem::CLASSNAME, "post_umount",
[Filesystem::signal_param_path => $relPath]
);
}
$this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true);
return $result;
} else {
// do not allow deleting the storage's root / the mount point
// because for some storages it might delete the whole contents
// but isn't supposed to work that way
return false;
}
}
public function disableCacheUpdate(): void {
$this->updaterEnabled = false;
}
public function enableCacheUpdate(): void {
$this->updaterEnabled = true;
}
protected function writeUpdate(Storage $storage, string $internalPath, ?int $time = null, ?int $sizeDifference = null): void {
if ($this->updaterEnabled) {
if (is_null($time)) {
$time = time();
}
$storage->getUpdater()->update($internalPath, $time, $sizeDifference);
}
}
protected function removeUpdate(Storage $storage, string $internalPath): void {
if ($this->updaterEnabled) {
$storage->getUpdater()->remove($internalPath);
}
}
protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, string $sourceInternalPath, string $targetInternalPath): void {
if ($this->updaterEnabled) {
$targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}
}
/**
* @param string $path
* @return bool|mixed
*/
public function rmdir($path) {
$absolutePath = $this->getAbsolutePath($path);
$mount = Filesystem::getMountManager()->find($absolutePath);
if ($mount->getInternalPath($absolutePath) === '') {
return $this->removeMount($mount, $absolutePath);
}
if ($this->is_dir($path)) {
$result = $this->basicOperation('rmdir', $path, ['delete']);
} else {
$result = false;
}
if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete
$storage = $mount->getStorage();
$internalPath = $mount->getInternalPath($absolutePath);
$storage->getUpdater()->remove($internalPath);
}
return $result;
}
/**
* @param string $path
* @return resource|false
*/
public function opendir($path) {
return $this->basicOperation('opendir', $path, ['read']);
}
/**
* @param string $path
* @return bool|mixed
*/
public function is_dir($path) {
if ($path == '/') {
return true;
}
return $this->basicOperation('is_dir', $path);
}
/**
* @param string $path
* @return bool|mixed
*/
public function is_file($path) {
if ($path == '/') {
return false;
}
return $this->basicOperation('is_file', $path);
}
/**
* @param string $path
* @return mixed
*/
public function stat($path) {
return $this->basicOperation('stat', $path);
}
/**
* @param string $path
* @return mixed
*/
public function filetype($path) {
return $this->basicOperation('filetype', $path);
}
/**
* @param string $path
* @return mixed
*/
public function filesize(string $path) {
return $this->basicOperation('filesize', $path);
}
/**
* @param string $path
* @return bool|mixed
* @throws InvalidPathException
*/
public function readfile($path) {
$this->assertPathLength($path);
if (ob_get_level()) {
ob_end_clean();
}
$handle = $this->fopen($path, 'rb');
if ($handle) {
$chunkSize = 524288; // 512 kiB chunks
while (!feof($handle)) {
echo fread($handle, $chunkSize);
flush();
$this->checkConnectionStatus();
}
fclose($handle);
return $this->filesize($path);
}
return false;
}
/**
* @param string $path
* @param int $from
* @param int $to
* @return bool|mixed
* @throws InvalidPathException
* @throws \OCP\Files\UnseekableException
*/
public function readfilePart($path, $from, $to) {
$this->assertPathLength($path);
if (ob_get_level()) {
ob_end_clean();
}
$handle = $this->fopen($path, 'rb');
if ($handle) {
$chunkSize = 524288; // 512 kiB chunks
$startReading = true;
if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) {
// forward file handle via chunked fread because fseek seem to have failed
$end = $from + 1;
while (!feof($handle) && ftell($handle) < $end && ftell($handle) !== $from) {
$len = $from - ftell($handle);
if ($len > $chunkSize) {
$len = $chunkSize;
}
$result = fread($handle, $len);
if ($result === false) {
$startReading = false;
break;
}
}
}
if ($startReading) {
$end = $to + 1;
while (!feof($handle) && ftell($handle) < $end) {
$len = $end - ftell($handle);
if ($len > $chunkSize) {
$len = $chunkSize;
}
echo fread($handle, $len);
flush();
$this->checkConnectionStatus();
}
return ftell($handle) - $from;
}
throw new \OCP\Files\UnseekableException('fseek error');
}
return false;
}
private function checkConnectionStatus(): void {
$connectionStatus = \connection_status();
if ($connectionStatus !== CONNECTION_NORMAL) {
throw new ConnectionLostException("Connection lost. Status: $connectionStatus");
}
}
/**
* @param string $path
* @return mixed
*/
public function isCreatable($path) {
return $this->basicOperation('isCreatable', $path);
}
/**
* @param string $path
* @return mixed
*/
public function isReadable($path) {
return $this->basicOperation('isReadable', $path);
}
/**
* @param string $path
* @return mixed
*/
public function isUpdatable($path) {
return $this->basicOperation('isUpdatable', $path);
}
/**
* @param string $path
* @return bool|mixed
*/
public function isDeletable($path) {
$absolutePath = $this->getAbsolutePath($path);
$mount = Filesystem::getMountManager()->find($absolutePath);
if ($mount->getInternalPath($absolutePath) === '') {
return $mount instanceof MoveableMount;
}
return $this->basicOperation('isDeletable', $path);
}
/**
* @param string $path
* @return mixed
*/
public function isSharable($path) {
return $this->basicOperation('isSharable', $path);
}
/**
* @param string $path
* @return bool|mixed
*/
public function file_exists($path) {
if ($path == '/') {
return true;
}
return $this->basicOperation('file_exists', $path);
}
/**
* @param string $path
* @return mixed
*/
public function filemtime($path) {
return $this->basicOperation('filemtime', $path);
}
/**
* @param string $path
* @param int|string $mtime
*/
public function touch($path, $mtime = null): bool {
if (!is_null($mtime) && !is_numeric($mtime)) {
$mtime = strtotime($mtime);
}
$hooks = ['touch'];
if (!$this->file_exists($path)) {
$hooks[] = 'create';
$hooks[] = 'write';
}
try {
$result = $this->basicOperation('touch', $path, $hooks, $mtime);
} catch (\Exception $e) {
$this->logger->info('Error while setting modified time', ['app' => 'core', 'exception' => $e]);
$result = false;
}
if (!$result) {
// If create file fails because of permissions on external storage like SMB folders,
// check file exists and return false if not.
if (!$this->file_exists($path)) {
return false;
}
if (is_null($mtime)) {
$mtime = time();
}
//if native touch fails, we emulate it by changing the mtime in the cache
$this->putFileInfo($path, ['mtime' => floor($mtime)]);
}
return true;
}
/**
* @param string $path
* @return string|false
* @throws LockedException
*/
public function file_get_contents($path) {
return $this->basicOperation('file_get_contents', $path, ['read']);
}
protected function emit_file_hooks_pre(bool $exists, string $path, bool &$run): void {
if (!$exists) {
\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, [
Filesystem::signal_param_path => $this->getHookPath($path),
Filesystem::signal_param_run => &$run,
]);
} else {
\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, [
Filesystem::signal_param_path => $this->getHookPath($path),
Filesystem::signal_param_run => &$run,
]);
}
\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, [
Filesystem::signal_param_path => $this->getHookPath($path),
Filesystem::signal_param_run => &$run,
]);
}
protected function emit_file_hooks_post(bool $exists, string $path): void {
if (!$exists) {
\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, [
Filesystem::signal_param_path => $this->getHookPath($path),
]);
} else {
\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, [
Filesystem::signal_param_path => $this->getHookPath($path),
]);
}
\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, [
Filesystem::signal_param_path => $this->getHookPath($path),
]);
}
/**
* @param string $path
* @param string|resource $data
* @return bool|mixed
* @throws LockedException
*/
public function file_put_contents($path, $data) {
if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier
$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
if (Filesystem::isValidPath($path)
&& !Filesystem::isFileBlacklisted($path)
) {
$path = $this->getRelativePath($absolutePath);
if ($path === null) {
throw new InvalidPathException("Path $absolutePath is not in the expected root");
}
$this->lockFile($path, ILockingProvider::LOCK_SHARED);
$exists = $this->file_exists($path);
$run = true;
if ($this->shouldEmitHooks($path)) {
$this->emit_file_hooks_pre($exists, $path, $run);
}
if (!$run) {
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
return false;
}
try {
$this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE);
} catch (\Exception $e) {
// Release the shared lock before throwing.
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
throw $e;
}
/** @var Storage $storage */
[$storage, $internalPath] = $this->resolvePath($path);
$target = $storage->fopen($internalPath, 'w');
if ($target) {
[, $result] = \OC_Helper::streamCopy($data, $target);
fclose($target);
fclose($data);
$this->writeUpdate($storage, $internalPath);
$this->changeLock($path, ILockingProvider::LOCK_SHARED);
if ($this->shouldEmitHooks($path) && $result !== false) {
$this->emit_file_hooks_post($exists, $path);
}
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
return $result;
} else {
$this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
return false;
}
} else {
return false;
}
} else {
$hooks = $this->file_exists($path) ? ['update', 'write'] : ['create', 'write'];
return $this->basicOperation('file_put_contents', $path, $hooks, $data);
}
}
/**
* @param string $path
* @return bool|mixed
*/
public function unlink($path) {
if ($path === '' || $path === '/') {
// do not allow deleting the root
return false;
}
$postFix = (substr($path, -1) === '/') ? '/' : '';
$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
$mount = Filesystem::getMountManager()->find($absolutePath . $postFix);
if ($mount->getInternalPath($absolutePath) === '') {
return $this->removeMount($mount, $absolutePath);
}
if ($this->is_dir($path)) {
$result = $this->basicOperation('rmdir', $path, ['delete']);
} else {
$result = $this->basicOperation('unlink', $path, ['delete']);
}
if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete
$storage = $mount->getStorage();
$internalPath = $mount->getInternalPath($absolutePath);
$storage->getUpdater()->remove($internalPath);
return true;
} else {
return $result;
}
}
/**
* @param string $directory
* @return bool|mixed
*/
public function deleteAll($directory) {
return $this->rmdir($directory);
}
/**
* Rename/move a file or folder from the source path to target path.
*
* @param string $source source path
* @param string $target target path
*
* @return bool|mixed
* @throws LockedException
*/
public function rename($source, $target) {
$absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source));
$absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target));
if (str_starts_with($absolutePath2, $absolutePath1 . '/')) {
throw new ForbiddenException("Moving a folder into a child folder is forbidden", false);
}
$targetParts = explode('/', $absolutePath2);
$targetUser = $targetParts[1] ?? null;
$result = false;
if (
Filesystem::isValidPath($target)
&& Filesystem::isValidPath($source)
&& !Filesystem::isFileBlacklisted($target)
) {
$source = $this->getRelativePath($absolutePath1);
$target = $this->getRelativePath($absolutePath2);
$exists = $this->file_exists($target);
if ($source == null || $target == null) {
return false;
}
$this->lockFile($source, ILockingProvider::LOCK_SHARED, true);
try {
$this->lockFile($target, ILockingProvider::LOCK_SHARED, true);
$run = true;
if ($this->shouldEmitHooks($source) && (Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target))) {
// if it was a rename from a part file to a regular file it was a write and not a rename operation
$this->emit_file_hooks_pre($exists, $target, $run);
} elseif ($this->shouldEmitHooks($source)) {
$sourcePath = $this->getHookPath($source);
$targetPath = $this->getHookPath($target);
if ($sourcePath !== null && $targetPath !== null) {
\OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::signal_rename,
[
Filesystem::signal_param_oldpath => $sourcePath,
Filesystem::signal_param_newpath => $targetPath,
Filesystem::signal_param_run => &$run
]
);
}
}
if ($run) {
$this->verifyPath(dirname($target), basename($target));
$manager = Filesystem::getMountManager();
$mount1 = $this->getMount($source);
$mount2 = $this->getMount($target);
$storage1 = $mount1->getStorage();
$storage2 = $mount2->getStorage();
$internalPath1 = $mount1->getInternalPath($absolutePath1);
$internalPath2 = $mount2->getInternalPath($absolutePath2);
$this->changeLock($source, ILockingProvider::LOCK_EXCLUSIVE, true);
try {
$this->changeLock($target, ILockingProvider::LOCK_EXCLUSIVE, true);
if ($internalPath1 === '') {
if ($mount1 instanceof MoveableMount) {
$sourceParentMount = $this->getMount(dirname($source));
if ($sourceParentMount === $mount2 && $this->targetIsNotShared($targetUser, $absolutePath2)) {
/**
* @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1
*/
$sourceMountPoint = $mount1->getMountPoint();
$result = $mount1->moveMount($absolutePath2);
$manager->moveMount($sourceMountPoint, $mount1->getMountPoint());
} else {
$result = false;
}
} else {
$result = false;
}
// moving a file/folder within the same mount point
} elseif ($storage1 === $storage2) {
if ($storage1) {
$result = $storage1->rename($internalPath1, $internalPath2);
} else {
$result = false;
}
// moving a file/folder between storages (from $storage1 to $storage2)
} else {
$result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
}
if ((Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target)) && $result !== false) {
// if it was a rename from a part file to a regular file it was a write and not a rename operation
$this->writeUpdate($storage2, $internalPath2);
} elseif ($result) {
if ($internalPath1 !== '') { // don't do a cache update for moved mounts
$this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2);
}
}
} catch (\Exception $e) {
throw $e;
} finally {
$this->changeLock($source, ILockingProvider::LOCK_SHARED, true);
$this->changeLock($target, ILockingProvider::LOCK_SHARED, true);
}
if ((Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target)) && $result !== false) {
if ($this->shouldEmitHooks()) {
$this->emit_file_hooks_post($exists, $target);
}
} elseif ($result) {
if ($this->shouldEmitHooks($source) && $this->shouldEmitHooks($target)) {
$sourcePath = $this->getHookPath($source);
$targetPath = $this->getHookPath($target);
if ($sourcePath !== null && $targetPath !== null) {
\OC_Hook::emit(
Filesystem::CLASSNAME,
Filesystem::signal_post_rename,
[
Filesystem::signal_param_oldpath => $sourcePath,
Filesystem::signal_param_newpath => $targetPath,
]
);
}
}
}
}
} catch (\Exception $e) {
throw $e;
} finally {
$this->unlockFile($source, ILockingProvider::LOCK_SHARED, true);
$this->unlockFile($target, ILockingProvider::LOCK_SHARED, true);
}
}
return $result;
}
/**
* Copy a file/folder from the source path to target path
*
* @param string $source source path
* @param string $target target path
* @param bool $preserveMtime whether to preserve mtime on the copy
*
* @return bool|mixed
*/
public function copy($source, $target, $preserveMtime = false) {
$absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source));
$absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target));
$result = false;
if (
Filesystem::isValidPath($target)
&& Filesystem::isValidPath($source)
&& !Filesystem::isFileBlacklisted($target)
) {
$source = $this->getRelativePath($absolutePath1);
$target = $this->getRelativePath($absolutePath2);
if ($source == null || $target == null) {
return false;
}
$run = true;
$this->lockFile($target, ILockingProvider::LOCK_SHARED);
$this->lockFile($source, ILockingProvider::LOCK_SHARED);
$lockTypePath1 = ILockingProvider::LOCK_SHARED;
$lockTypePath2 = ILockingProvider::LOCK_SHARED;
try {
$exists = $this->file_exists($target);
if ($this->shouldEmitHooks()) {
\OC_Hook::emit(
Filesystem::CLASSNAME,
Filesystem::signal_copy,
[
Filesystem::signal_param_oldpath => $this->getHookPath($source),
Filesystem::signal_param_newpath => $this->getHookPath($target),
Filesystem::signal_param_run => &$run
]
);
$this->emit_file_hooks_pre($exists, $target, $run);
}
if ($run) {
$mount1 = $this->getMount($source);
$mount2 = $this->getMount($target);
$storage1 = $mount1->getStorage();
$internalPath1 = $mount1->getInternalPath($absolutePath1);
$storage2 = $mount2->getStorage();
$internalPath2 = $mount2->getInternalPath($absolutePath2);
$this->changeLock($target, ILockingProvider::LOCK_EXCLUSIVE);
$lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE;
if ($mount1->getMountPoint() == $mount2->getMountPoint()) {
if ($storage1) {
$result = $storage1->copy($internalPath1, $internalPath2);
} else {
$result = false;
}
} else {
$result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2);
}
$this->writeUpdate($storage2, $internalPath2);
$this->changeLock($target, ILockingProvider::LOCK_SHARED);
$lockTypePath2 = ILockingProvider::LOCK_SHARED;
if ($this->shouldEmitHooks() && $result !== false) {
\OC_Hook::emit(
Filesystem::CLASSNAME,
Filesystem::signal_post_copy,
[
Filesystem::signal_param_oldpath => $this->getHookPath($source),
Filesystem::signal_param_newpath => $this->getHookPath($target)
]
);
$this->emit_file_hooks_post($exists, $target);
}
}
} catch (\Exception $e) {
$this->unlockFile($target, $lockTypePath2);
$this->unlockFile($source, $lockTypePath1);
throw $e;
}
$this->unlockFile($target, $lockTypePath2);
$this->unlockFile($source, $lockTypePath1);
}
return $result;
}
/**
* @param string $path
* @param string $mode 'r' or 'w'
* @return resource|false
* @throws LockedException
*/
public function fopen($path, $mode) {
$mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support
$hooks = [];
switch ($mode) {
case 'r':
$hooks[] = 'read';
break;
case 'r+':
case 'w+':
case 'x+':
case 'a+':
$hooks[] = 'read';
$hooks[] = 'write';
break;
case 'w':
case 'x':
case 'a':
$hooks[] = 'write';
break;
default:
$this->logger->error('invalid mode (' . $mode . ') for ' . $path, ['app' => 'core']);
}
if ($mode !== 'r' && $mode !== 'w') {
$this->logger->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends', ['app' => 'core']);
}
$handle = $this->basicOperation('fopen', $path, $hooks, $mode);
if (!is_resource($handle) && $mode === 'r') {
// trying to read a file that isn't on disk, check if the cache is out of sync and rescan if needed
$mount = $this->getMount($path);
$internalPath = $mount->getInternalPath($this->getAbsolutePath($path));
$storage = $mount->getStorage();
if ($storage->getCache()->inCache($internalPath) && !$storage->file_exists($path)) {
$this->writeUpdate($storage, $internalPath);
}
}
return $handle;
}
/**
* @param string $path
* @throws InvalidPathException
*/
public function toTmpFile($path): string|false {
$this->assertPathLength($path);
if (Filesystem::isValidPath($path)) {
$source = $this->fopen($path, 'r');
if ($source) {
$extension = pathinfo($path, PATHINFO_EXTENSION);
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension);
file_put_contents($tmpFile, $source);
return $tmpFile;
} else {
return false;
}
} else {
return false;
}
}
/**
* @param string $tmpFile
* @param string $path
* @return bool|mixed
* @throws InvalidPathException
*/
public function fromTmpFile($tmpFile, $path) {
$this->assertPathLength($path);
if (Filesystem::isValidPath($path)) {