Blame view

sources/lib/private/files/utils/scanner.php 2.41 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
  <?php
  /**
   * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
   * This file is licensed under the Affero General Public License version 3 or
   * later.
   * See the COPYING-README file.
   */
  
  namespace OC\Files\Utils;
  
  use OC\Files\Filesystem;
03e52840d   Kload   Init
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
  use OC\Hooks\PublicEmitter;
  
  /**
   * Class Scanner
   *
   * Hooks available in scope \OC\Utils\Scanner
   *  - scanFile(string $absolutePath)
   *  - scanFolder(string $absolutePath)
   *
   * @package OC\Files\Utils
   */
  class Scanner extends PublicEmitter {
  	/**
  	 * @var string $user
  	 */
  	private $user;
  
  	/**
  	 * @param string $user
  	 */
  	public function __construct($user) {
  		$this->user = $user;
  	}
  
  	/**
  	 * get all storages for $dir
  	 *
  	 * @param string $dir
31b7f2792   Kload   Upgrade to ownclo...
40
  	 * @return \OC\Files\Mount\Mount[]
03e52840d   Kload   Init
41
42
43
44
45
46
  	 */
  	protected function getMounts($dir) {
  		//TODO: move to the node based fileapi once that's done
  		\OC_Util::tearDownFS();
  		\OC_Util::setupFS($this->user);
  		$absolutePath = Filesystem::getView()->getAbsolutePath($dir);
31b7f2792   Kload   Upgrade to ownclo...
47
48
49
  		$mountManager = Filesystem::getMountManager();
  		$mounts = $mountManager->findIn($absolutePath);
  		$mounts[] = $mountManager->find($absolutePath);
03e52840d   Kload   Init
50
51
52
53
54
55
56
57
  		$mounts = array_reverse($mounts); //start with the mount of $dir
  
  		return $mounts;
  	}
  
  	/**
  	 * attach listeners to the scanner
  	 *
31b7f2792   Kload   Upgrade to ownclo...
58
  	 * @param \OC\Files\Mount\Mount $mount
03e52840d   Kload   Init
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  	 */
  	protected function attachListener($mount) {
  		$scanner = $mount->getStorage()->getScanner();
  		$emitter = $this;
  		$scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function ($path) use ($mount, $emitter) {
  			$emitter->emit('\OC\Files\Utils\Scanner', 'scanFile', array($mount->getMountPoint() . $path));
  		});
  		$scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function ($path) use ($mount, $emitter) {
  			$emitter->emit('\OC\Files\Utils\Scanner', 'scanFolder', array($mount->getMountPoint() . $path));
  		});
  	}
  
  	public function backgroundScan($dir) {
  		$mounts = $this->getMounts($dir);
  		foreach ($mounts as $mount) {
31b7f2792   Kload   Upgrade to ownclo...
74
75
76
  			if (is_null($mount->getStorage())) {
  				continue;
  			}
03e52840d   Kload   Init
77
78
79
80
81
82
83
84
85
  			$scanner = $mount->getStorage()->getScanner();
  			$this->attachListener($mount);
  			$scanner->backgroundScan();
  		}
  	}
  
  	public function scan($dir) {
  		$mounts = $this->getMounts($dir);
  		foreach ($mounts as $mount) {
31b7f2792   Kload   Upgrade to ownclo...
86
87
88
  			if (is_null($mount->getStorage())) {
  				continue;
  			}
03e52840d   Kload   Init
89
90
91
92
93
94
  			$scanner = $mount->getStorage()->getScanner();
  			$this->attachListener($mount);
  			$scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG);
  		}
  	}
  }