Blame view

sources/lib/private/files/cache/updater.php 6.33 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  <?php
  /**
   * Copyright (c) 2012 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\Cache;
  
  /**
   * listen to filesystem hooks and change the cache accordingly
   */
  class Updater {
  
  	/**
  	 * resolve a path to a storage and internal path
  	 *
31b7f2792   Kload   Upgrade to ownclo...
19
  	 * @param string $path the relative path
6d9380f96   Cédric Dupont   Update sources OC...
20
  	 * @return array an array consisting of the storage and the internal path
03e52840d   Kload   Init
21
22
23
24
25
  	 */
  	static public function resolvePath($path) {
  		$view = \OC\Files\Filesystem::getView();
  		return $view->resolvePath($path);
  	}
31b7f2792   Kload   Upgrade to ownclo...
26
27
28
29
30
  	/**
  	 * perform a write update
  	 *
  	 * @param string $path the relative path of the file
  	 */
03e52840d   Kload   Init
31
32
33
34
35
36
37
38
39
  	static public function writeUpdate($path) {
  		/**
  		 * @var \OC\Files\Storage\Storage $storage
  		 * @var string $internalPath
  		 */
  		list($storage, $internalPath) = self::resolvePath($path);
  		if ($storage) {
  			$cache = $storage->getCache($internalPath);
  			$scanner = $storage->getScanner($internalPath);
6d9380f96   Cédric Dupont   Update sources OC...
40
41
  			$data = $scanner->scan($internalPath, Scanner::SCAN_SHALLOW);
  			$cache->correctFolderSize($internalPath, $data);
03e52840d   Kload   Init
42
  			self::correctFolder($path, $storage->filemtime($internalPath));
31b7f2792   Kload   Upgrade to ownclo...
43
  			self::correctParentStorageMtime($storage, $internalPath);
03e52840d   Kload   Init
44
45
  		}
  	}
31b7f2792   Kload   Upgrade to ownclo...
46
47
48
49
50
  	/**
  	 * perform a delete update
  	 *
  	 * @param string $path the relative path of the file
  	 */
03e52840d   Kload   Init
51
52
53
54
55
56
57
  	static public function deleteUpdate($path) {
  		/**
  		 * @var \OC\Files\Storage\Storage $storage
  		 * @var string $internalPath
  		 */
  		list($storage, $internalPath) = self::resolvePath($path);
  		if ($storage) {
31b7f2792   Kload   Upgrade to ownclo...
58
59
60
61
  			$parent = dirname($internalPath);
  			if ($parent === '.') {
  				$parent = '';
  			}
03e52840d   Kload   Init
62
63
  			$cache = $storage->getCache($internalPath);
  			$cache->remove($internalPath);
31b7f2792   Kload   Upgrade to ownclo...
64
  			$cache->correctFolderSize($parent);
03e52840d   Kload   Init
65
  			self::correctFolder($path, time());
31b7f2792   Kload   Upgrade to ownclo...
66
  			self::correctParentStorageMtime($storage, $internalPath);
03e52840d   Kload   Init
67
68
  		}
  	}
31b7f2792   Kload   Upgrade to ownclo...
69
70
71
72
73
74
  	/**
  	 * preform a rename update
  	 *
  	 * @param string $from the relative path of the source file
  	 * @param string $to the relative path of the target file
  	 */
03e52840d   Kload   Init
75
76
77
78
79
80
81
82
  	static public function renameUpdate($from, $to) {
  		/**
  		 * @var \OC\Files\Storage\Storage $storageFrom
  		 * @var \OC\Files\Storage\Storage $storageTo
  		 * @var string $internalFrom
  		 * @var string $internalTo
  		 */
  		list($storageFrom, $internalFrom) = self::resolvePath($from);
6d9380f96   Cédric Dupont   Update sources OC...
83
84
85
86
  		// if it's a moved mountpoint we dont need to do anything
  		if ($internalFrom === '') {
  			return;
  		}
03e52840d   Kload   Init
87
88
89
90
91
  		list($storageTo, $internalTo) = self::resolvePath($to);
  		if ($storageFrom && $storageTo) {
  			if ($storageFrom === $storageTo) {
  				$cache = $storageFrom->getCache($internalFrom);
  				$cache->move($internalFrom, $internalTo);
31b7f2792   Kload   Upgrade to ownclo...
92
93
94
95
96
97
  				if (pathinfo($internalFrom, PATHINFO_EXTENSION) !== pathinfo($internalTo, PATHINFO_EXTENSION)) {
  					// redetect mime type change
  					$mimeType = $storageTo->getMimeType($internalTo);
  					$fileId = $storageTo->getCache()->getId($internalTo);
  					$storageTo->getCache()->update($fileId, array('mimetype' => $mimeType));
  				}
03e52840d   Kload   Init
98
99
100
101
  				$cache->correctFolderSize($internalFrom);
  				$cache->correctFolderSize($internalTo);
  				self::correctFolder($from, time());
  				self::correctFolder($to, time());
31b7f2792   Kload   Upgrade to ownclo...
102
103
  				self::correctParentStorageMtime($storageFrom, $internalFrom);
  				self::correctParentStorageMtime($storageTo, $internalTo);
03e52840d   Kload   Init
104
105
106
107
108
109
110
111
  			} else {
  				self::deleteUpdate($from);
  				self::writeUpdate($to);
  			}
  		}
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
112
  	 * get file owner and path
03e52840d   Kload   Init
113
  	 * @param string $filename
f7d878ff1   kload   [enh] Update to 7...
114
  	 * @return string[] with the owner's uid and the owner's path
03e52840d   Kload   Init
115
116
  	 */
  	private static function getUidAndFilename($filename) {
31b7f2792   Kload   Upgrade to ownclo...
117

03e52840d   Kload   Init
118
119
  		$uid = \OC\Files\Filesystem::getOwner($filename);
  		\OC\Files\Filesystem::initMountPoints($uid);
31b7f2792   Kload   Upgrade to ownclo...
120

f7d878ff1   kload   [enh] Update to 7...
121
  		$filename = (strpos($filename, '/') !== 0) ? '/' . $filename : $filename;
31b7f2792   Kload   Upgrade to ownclo...
122
  		if ($uid != \OCP\User::getUser()) {
03e52840d   Kload   Init
123
  			$info = \OC\Files\Filesystem::getFileInfo($filename);
6d9380f96   Cédric Dupont   Update sources OC...
124
  			if (!$info) {
f7d878ff1   kload   [enh] Update to 7...
125
  				return array($uid, '/files' . $filename);
6d9380f96   Cédric Dupont   Update sources OC...
126
  			}
31b7f2792   Kload   Upgrade to ownclo...
127
  			$ownerView = new \OC\Files\View('/' . $uid . '/files');
03e52840d   Kload   Init
128
129
  			$filename = $ownerView->getPath($info['fileid']);
  		}
f7d878ff1   kload   [enh] Update to 7...
130
  		return array($uid, '/files' . $filename);
03e52840d   Kload   Init
131
132
133
134
135
136
137
138
139
  	}
  
  	/**
  	 * Update the mtime and ETag of all parent folders
  	 *
  	 * @param string $path
  	 * @param string $time
  	 */
  	static public function correctFolder($path, $time) {
f7d878ff1   kload   [enh] Update to 7...
140
  		if ($path !== '' && $path !== '/' && $path !== '\\') {
03e52840d   Kload   Init
141
142
143
144
145
146
  			list($owner, $realPath) = self::getUidAndFilename(dirname($path));
  
  			/**
  			 * @var \OC\Files\Storage\Storage $storage
  			 * @var string $internalPath
  			 */
03e52840d   Kload   Init
147
148
149
150
151
152
153
154
  			$view = new \OC\Files\View('/' . $owner);
  
  			list($storage, $internalPath) = $view->resolvePath($realPath);
  			$cache = $storage->getCache();
  			$id = $cache->getId($internalPath);
  
  			while ($id !== -1) {
  				$cache->update($id, array('mtime' => $time, 'etag' => $storage->getETag($internalPath)));
31b7f2792   Kload   Upgrade to ownclo...
155
156
  				if ($realPath !== '') {
  					$realPath = dirname($realPath);
6d9380f96   Cédric Dupont   Update sources OC...
157
  					if ($realPath === DIRECTORY_SEPARATOR) {
31b7f2792   Kload   Upgrade to ownclo...
158
159
160
161
  						$realPath = "";
  					}
  					// check storage for parent in case we change the storage in this step
  					list($storage, $internalPath) = $view->resolvePath($realPath);
03e52840d   Kload   Init
162
163
164
165
166
  					$cache = $storage->getCache();
  					$id = $cache->getId($internalPath);
  				} else {
  					$id = -1;
  				}
03e52840d   Kload   Init
167
  			}
31b7f2792   Kload   Upgrade to ownclo...
168
169
  		}
  	}
03e52840d   Kload   Init
170

31b7f2792   Kload   Upgrade to ownclo...
171
172
173
174
175
176
177
178
179
180
  	/**
  	 * update the storage_mtime of the parent
  	 *
  	 * @param \OC\Files\Storage\Storage $storage
  	 * @param string $internalPath
  	 */
  	static private function correctParentStorageMtime($storage, $internalPath) {
  		$cache = $storage->getCache();
  		$parentId = $cache->getParentId($internalPath);
  		$parent = dirname($internalPath);
f7d878ff1   kload   [enh] Update to 7...
181
182
183
184
  
  		if ($parent === '.' || $parent === '/' || $parent === '\\') {
  			$parent = '';
  		}
31b7f2792   Kload   Upgrade to ownclo...
185
186
  		if ($parentId != -1) {
  			$cache->update($parentId, array('storage_mtime' => $storage->filemtime($parent)));
03e52840d   Kload   Init
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
  		}
  	}
  
  	/**
  	 * @param array $params
  	 */
  	static public function writeHook($params) {
  		self::writeUpdate($params['path']);
  	}
  
  	/**
  	 * @param array $params
  	 */
  	static public function touchHook($params) {
  		$path = $params['path'];
  		list($storage, $internalPath) = self::resolvePath($path);
  		$cache = $storage->getCache();
  		$id = $cache->getId($internalPath);
  		if ($id !== -1) {
  			$cache->update($id, array('etag' => $storage->getETag($internalPath)));
  		}
  		self::writeUpdate($path);
  	}
  
  	/**
  	 * @param array $params
  	 */
  	static public function renameHook($params) {
  		self::renameUpdate($params['oldpath'], $params['newpath']);
  	}
  
  	/**
  	 * @param array $params
  	 */
  	static public function deleteHook($params) {
  		self::deleteUpdate($params['path']);
  	}
  }