Blame view

sources/lib/private/files/utils/scanner.php 3.58 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
  <?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;
6d9380f96   Cédric Dupont   Update sources OC...
10
11
  use OC\Files\View;
  use OC\Files\Cache\ChangePropagator;
03e52840d   Kload   Init
12
  use OC\Files\Filesystem;
6d9380f96   Cédric Dupont   Update sources OC...
13
  use OC\ForbiddenException;
03e52840d   Kload   Init
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  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;
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
32
33
34
35
36
  	 * @var \OC\Files\Cache\ChangePropagator
  	 */
  	protected $propagator;
  
  	/**
03e52840d   Kload   Init
37
38
39
40
  	 * @param string $user
  	 */
  	public function __construct($user) {
  		$this->user = $user;
6d9380f96   Cédric Dupont   Update sources OC...
41
  		$this->propagator = new ChangePropagator(new View(''));
03e52840d   Kload   Init
42
43
44
45
46
47
  	}
  
  	/**
  	 * get all storages for $dir
  	 *
  	 * @param string $dir
31b7f2792   Kload   Upgrade to ownclo...
48
  	 * @return \OC\Files\Mount\Mount[]
03e52840d   Kload   Init
49
50
51
52
53
54
  	 */
  	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...
55
56
57
  		$mountManager = Filesystem::getMountManager();
  		$mounts = $mountManager->findIn($absolutePath);
  		$mounts[] = $mountManager->find($absolutePath);
03e52840d   Kload   Init
58
59
60
61
62
63
64
65
  		$mounts = array_reverse($mounts); //start with the mount of $dir
  
  		return $mounts;
  	}
  
  	/**
  	 * attach listeners to the scanner
  	 *
31b7f2792   Kload   Upgrade to ownclo...
66
  	 * @param \OC\Files\Mount\Mount $mount
03e52840d   Kload   Init
67
68
69
70
71
72
73
74
75
76
  	 */
  	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));
  		});
6d9380f96   Cédric Dupont   Update sources OC...
77
78
79
80
81
82
83
84
85
  
  		// propagate etag and mtimes when files are changed or removed
  		$propagator = $this->propagator;
  		$propagatorListener = function ($path) use ($mount, $propagator) {
  			$fullPath = Filesystem::normalizePath($mount->getMountPoint() . $path);
  			$propagator->addChange($fullPath);
  		};
  		$scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', $propagatorListener);
  		$scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', $propagatorListener);
03e52840d   Kload   Init
86
  	}
6d9380f96   Cédric Dupont   Update sources OC...
87
88
89
  	/**
  	 * @param string $dir
  	 */
03e52840d   Kload   Init
90
91
92
  	public function backgroundScan($dir) {
  		$mounts = $this->getMounts($dir);
  		foreach ($mounts as $mount) {
31b7f2792   Kload   Upgrade to ownclo...
93
94
95
  			if (is_null($mount->getStorage())) {
  				continue;
  			}
03e52840d   Kload   Init
96
97
98
99
  			$scanner = $mount->getStorage()->getScanner();
  			$this->attachListener($mount);
  			$scanner->backgroundScan();
  		}
6d9380f96   Cédric Dupont   Update sources OC...
100
  		$this->propagator->propagateChanges(time());
03e52840d   Kload   Init
101
  	}
6d9380f96   Cédric Dupont   Update sources OC...
102
103
104
105
  	/**
  	 * @param string $dir
  	 * @throws \OC\ForbiddenException
  	 */
03e52840d   Kload   Init
106
107
108
  	public function scan($dir) {
  		$mounts = $this->getMounts($dir);
  		foreach ($mounts as $mount) {
31b7f2792   Kload   Upgrade to ownclo...
109
110
111
  			if (is_null($mount->getStorage())) {
  				continue;
  			}
6d9380f96   Cédric Dupont   Update sources OC...
112
113
114
115
116
117
118
119
  			$storage = $mount->getStorage();
  			// if the home storage isn't writable then the scanner is run as the wrong user
  			if ($storage->instanceOfStorage('\OC\Files\Storage\Home') and
  				(!$storage->isCreatable('') or !$storage->isCreatable('files'))
  			) {
  				throw new ForbiddenException();
  			}
  			$scanner = $storage->getScanner();
03e52840d   Kload   Init
120
  			$this->attachListener($mount);
6d9380f96   Cédric Dupont   Update sources OC...
121
  			$scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE);
03e52840d   Kload   Init
122
  		}
6d9380f96   Cédric Dupont   Update sources OC...
123
  		$this->propagator->propagateChanges(time());
03e52840d   Kload   Init
124
125
  	}
  }