1010use OC \Files \Cache \Cache ;
1111use OC \Files \Filesystem ;
1212use OC \Files \Mount \MountPoint ;
13+ use OC \Files \SetupManager ;
1314use OC \Files \Storage \FailedStorage ;
1415use OC \Files \Storage \Home ;
1516use OC \ForbiddenException ;
1617use OC \Hooks \PublicEmitter ;
17- use OC \Lock \DBLockingProvider ;
1818use OCA \Files_Sharing \SharedStorage ;
1919use OCP \EventDispatcher \IEventDispatcher ;
2020use OCP \Files \Events \BeforeFileScannedEvent ;
2929use OCP \Files \Storage \IStorage ;
3030use OCP \Files \StorageNotAvailableException ;
3131use OCP \IDBConnection ;
32- use OCP \Lock \ ILockingProvider ;
32+ use OCP \IUser ;
3333use OCP \Lock \LockedException ;
34- use OCP \Server ;
3534use Psr \Log \LoggerInterface ;
3635
3736/**
4443 * @package OC\Files\Utils
4544 */
4645class Scanner extends PublicEmitter {
47- public const MAX_ENTRIES_TO_COMMIT = 10000 ;
48-
49- /**
50- * Whether to use a DB transaction
51- */
52- protected bool $ useTransaction ;
53-
54- /**
55- * Number of entries scanned to commit
56- */
57- protected int $ entriesToCommit = 0 ;
5846
5947 public function __construct (
60- private ?string $ user ,
61- protected ?IDBConnection $ db ,
62- private IEventDispatcher $ dispatcher ,
63- protected LoggerInterface $ logger ,
48+ private readonly ?IUser $ user ,
49+ protected readonly ?IDBConnection $ db ,
50+ private readonly IEventDispatcher $ eventDispatcher ,
51+ protected readonly LoggerInterface $ logger ,
52+ private readonly SetupManager $ setupManager ,
6453 ) {
65- // when DB locking is used, no DB transactions will be used
66- $ this ->useTransaction = !(Server::get (ILockingProvider::class) instanceof DBLockingProvider);
6754 }
6855
6956 /**
@@ -74,8 +61,11 @@ public function __construct(
7461 */
7562 protected function getMounts ($ dir ) {
7663 //TODO: move to the node based fileapi once that's done
77- \OC_Util::tearDownFS ();
78- \OC_Util::setupFS ($ this ->user );
64+ $ this ->setupManager ->tearDown ();
65+
66+ if ($ this ->user !== null ) {
67+ $ this ->setupManager ->setupForUser ($ this ->user );
68+ }
7969
8070 $ mountManager = Filesystem::getMountManager ();
8171 $ mounts = $ mountManager ->findIn ($ dir );
@@ -88,37 +78,32 @@ protected function getMounts($dir) {
8878
8979 /**
9080 * attach listeners to the scanner
91- *
92- * @param MountPoint $mount
9381 */
94- protected function attachListener ($ mount ) {
82+ protected function attachListener (MountPoint $ mount ) {
9583 /** @var \OC\Files\Cache\Scanner $scanner */
9684 $ scanner = $ mount ->getStorage ()->getScanner ();
9785 $ scanner ->listen ('\OC\Files\Cache\Scanner ' , 'scanFile ' , function ($ path ) use ($ mount ): void {
9886 $ this ->emit ('\OC\Files\Utils\Scanner ' , 'scanFile ' , [$ mount ->getMountPoint () . $ path ]);
99- $ this ->dispatcher ->dispatchTyped (new BeforeFileScannedEvent ($ mount ->getMountPoint () . $ path ));
87+ $ this ->eventDispatcher ->dispatchTyped (new BeforeFileScannedEvent ($ mount ->getMountPoint () . $ path ));
10088 });
10189 $ scanner ->listen ('\OC\Files\Cache\Scanner ' , 'scanFolder ' , function ($ path ) use ($ mount ): void {
10290 $ this ->emit ('\OC\Files\Utils\Scanner ' , 'scanFolder ' , [$ mount ->getMountPoint () . $ path ]);
103- $ this ->dispatcher ->dispatchTyped (new BeforeFolderScannedEvent ($ mount ->getMountPoint () . $ path ));
91+ $ this ->eventDispatcher ->dispatchTyped (new BeforeFolderScannedEvent ($ mount ->getMountPoint () . $ path ));
10492 });
10593 $ scanner ->listen ('\OC\Files\Cache\Scanner ' , 'postScanFile ' , function ($ path ) use ($ mount ): void {
10694 $ this ->emit ('\OC\Files\Utils\Scanner ' , 'postScanFile ' , [$ mount ->getMountPoint () . $ path ]);
107- $ this ->dispatcher ->dispatchTyped (new FileScannedEvent ($ mount ->getMountPoint () . $ path ));
95+ $ this ->eventDispatcher ->dispatchTyped (new FileScannedEvent ($ mount ->getMountPoint () . $ path ));
10896 });
10997 $ scanner ->listen ('\OC\Files\Cache\Scanner ' , 'postScanFolder ' , function ($ path ) use ($ mount ): void {
11098 $ this ->emit ('\OC\Files\Utils\Scanner ' , 'postScanFolder ' , [$ mount ->getMountPoint () . $ path ]);
111- $ this ->dispatcher ->dispatchTyped (new FolderScannedEvent ($ mount ->getMountPoint () . $ path ));
99+ $ this ->eventDispatcher ->dispatchTyped (new FolderScannedEvent ($ mount ->getMountPoint () . $ path ));
112100 });
113101 $ scanner ->listen ('\OC\Files\Cache\Scanner ' , 'normalizedNameMismatch ' , function ($ path ) use ($ mount ): void {
114102 $ this ->emit ('\OC\Files\Utils\Scanner ' , 'normalizedNameMismatch ' , [$ path ]);
115103 });
116104 }
117105
118- /**
119- * @param string $dir
120- */
121- public function backgroundScan ($ dir ) {
106+ public function backgroundScan (string $ dir ) {
122107 $ mounts = $ this ->getMounts ($ dir );
123108 foreach ($ mounts as $ mount ) {
124109 try {
@@ -157,13 +142,10 @@ public function backgroundScan($dir) {
157142 }
158143
159144 /**
160- * @param string $dir
161- * @param $recursive
162- * @param callable|null $mountFilter
163145 * @throws ForbiddenException
164146 * @throws NotFoundException
165147 */
166- public function scan ($ dir = '' , $ recursive = \OC \Files \Cache \Scanner::SCAN_RECURSIVE , ?callable $ mountFilter = null ) {
148+ public function scan (string $ dir = '' , $ recursive = \OC \Files \Cache \Scanner::SCAN_RECURSIVE , ?callable $ mountFilter = null ) {
167149 if (!Filesystem::isValidPath ($ dir )) {
168150 throw new \InvalidArgumentException ('Invalid path to scan ' );
169151 }
@@ -214,33 +196,29 @@ public function scan($dir = '', $recursive = \OC\Files\Cache\Scanner::SCAN_RECUR
214196 $ relativePath = $ mount ->getInternalPath ($ dir );
215197 /** @var \OC\Files\Cache\Scanner $scanner */
216198 $ scanner = $ storage ->getScanner ();
217- $ scanner ->setUseTransactions (false );
218199 $ this ->attachListener ($ mount );
219200
220201 $ scanner ->listen ('\OC\Files\Cache\Scanner ' , 'removeFromCache ' , function ($ path ) use ($ storage ): void {
221- $ this ->postProcessEntry ($ storage , $ path );
222- $ this ->dispatcher ->dispatchTyped (new NodeRemovedFromCache ($ storage , $ path ));
202+ $ this ->triggerPropagator ($ storage , $ path );
203+ $ this ->eventDispatcher ->dispatchTyped (new NodeRemovedFromCache ($ storage , $ path ));
223204 });
224205 $ scanner ->listen ('\OC\Files\Cache\Scanner ' , 'updateCache ' , function ($ path ) use ($ storage ): void {
225- $ this ->postProcessEntry ($ storage , $ path );
226- $ this ->dispatcher ->dispatchTyped (new FileCacheUpdated ($ storage , $ path ));
206+ $ this ->triggerPropagator ($ storage , $ path );
207+ $ this ->eventDispatcher ->dispatchTyped (new FileCacheUpdated ($ storage , $ path ));
227208 });
228209 $ scanner ->listen ('\OC\Files\Cache\Scanner ' , 'addToCache ' , function ($ path , $ storageId , $ data , $ fileId ) use ($ storage ): void {
229- $ this ->postProcessEntry ($ storage , $ path );
210+ $ this ->triggerPropagator ($ storage , $ path );
230211 if ($ fileId ) {
231- $ this ->dispatcher ->dispatchTyped (new FileCacheUpdated ($ storage , $ path ));
212+ $ this ->eventDispatcher ->dispatchTyped (new FileCacheUpdated ($ storage , $ path ));
232213 } else {
233- $ this ->dispatcher ->dispatchTyped (new NodeAddedToCache ($ storage , $ path ));
214+ $ this ->eventDispatcher ->dispatchTyped (new NodeAddedToCache ($ storage , $ path ));
234215 }
235216 });
236217
237218 if (!$ storage ->file_exists ($ relativePath )) {
238219 throw new NotFoundException ($ dir );
239220 }
240221
241- if ($ this ->useTransaction ) {
242- $ this ->db ->beginTransaction ();
243- }
244222 try {
245223 $ propagator = $ storage ->getPropagator ();
246224 $ propagator ->beginBatch ();
@@ -263,28 +241,10 @@ public function scan($dir = '', $recursive = \OC\Files\Cache\Scanner::SCAN_RECUR
263241 $ this ->logger ->error ('Storage ' . $ storage ->getId () . ' not available ' , ['exception ' => $ e ]);
264242 $ this ->emit ('\OC\Files\Utils\Scanner ' , 'StorageNotAvailable ' , [$ e ]);
265243 }
266- if ($ this ->useTransaction ) {
267- $ this ->db ->commit ();
268- }
269244 }
270245 }
271246
272247 private function triggerPropagator (IStorage $ storage , $ internalPath ) {
273248 $ storage ->getPropagator ()->propagateChange ($ internalPath , time ());
274249 }
275-
276- private function postProcessEntry (IStorage $ storage , $ internalPath ) {
277- $ this ->triggerPropagator ($ storage , $ internalPath );
278- if ($ this ->useTransaction ) {
279- $ this ->entriesToCommit ++;
280- if ($ this ->entriesToCommit >= self ::MAX_ENTRIES_TO_COMMIT ) {
281- $ propagator = $ storage ->getPropagator ();
282- $ this ->entriesToCommit = 0 ;
283- $ this ->db ->commit ();
284- $ propagator ->commitBatch ();
285- $ this ->db ->beginTransaction ();
286- $ propagator ->beginBatch ();
287- }
288- }
289- }
290250}
0 commit comments