Welcome to mirror list, hosted at ThFree Co, Russian Federation.

updater.php « cache « files « private « lib - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9f4cbfeff8c5dc79fb06dd17e04c2e4a70f25740 (plain)
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
<?php
/**
 * Copyright (c) 2014 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;

/**
 * Update the cache and propagate changes
 */
class Updater {
	/**
	 * @var \OC\Files\View
	 */
	protected $view;

	/**
	 * @var \OC\Files\Cache\ChangePropagator
	 */
	protected $propagator;

	/**
	 * @param \OC\Files\View $view
	 */
	public function __construct($view) {
		$this->view = $view;
		$this->propagator = new ChangePropagator($view);
	}

	public function propagate($path, $time = null) {
		if (Scanner::isPartialFile($path)) {
			return;
		}
		$this->propagator->addChange($path);
		$this->propagator->propagateChanges($time);
	}

	/**
	 * Update the cache for $path
	 *
	 * @param string $path
	 * @param int $time
	 */
	public function update($path, $time = null) {
		if(Scanner::isPartialFile($path)) {
			return;
		}
		/**
		 * @var \OC\Files\Storage\Storage $storage
		 * @var string $internalPath
		 */
		list($storage, $internalPath) = $this->view->resolvePath($path);
		if ($storage) {
			$this->propagator->addChange($path);
			$cache = $storage->getCache($internalPath);
			$scanner = $storage->getScanner($internalPath);
			$data = $scanner->scan($internalPath, Scanner::SCAN_SHALLOW);
			$this->correctParentStorageMtime($storage, $internalPath);
			$cache->correctFolderSize($internalPath, $data);
			$this->propagator->propagateChanges($time);
		}
	}

	/**
	 * Remove $path from the cache
	 *
	 * @param string $path
	 */
	public function remove($path) {
		if (Scanner::isPartialFile($path)) {
			return;
		}
		/**
		 * @var \OC\Files\Storage\Storage $storage
		 * @var string $internalPath
		 */
		list($storage, $internalPath) = $this->view->resolvePath($path);
		if ($storage) {
			$parent = dirname($internalPath);
			if ($parent === '.') {
				$parent = '';
			}
			$this->propagator->addChange($path);
			$cache = $storage->getCache($internalPath);
			$cache->remove($internalPath);
			$cache->correctFolderSize($parent);
			$this->correctParentStorageMtime($storage, $internalPath);
			$this->propagator->propagateChanges();
		}
	}

	/**
	 * @param string $source
	 * @param string $target
	 */
	public function rename($source, $target) {
		if (Scanner::isPartialFile($source) or Scanner::isPartialFile($target)) {
			return;
		}
		/**
		 * @var \OC\Files\Storage\Storage $sourceStorage
		 * @var \OC\Files\Storage\Storage $targetStorage
		 * @var string $sourceInternalPath
		 * @var string $targetInternalPath
		 */
		list($sourceStorage, $sourceInternalPath) = $this->view->resolvePath($source);
		// if it's a moved mountpoint we dont need to do anything
		if ($sourceInternalPath === '') {
			return;
		}
		list($targetStorage, $targetInternalPath) = $this->view->resolvePath($target);

		if ($sourceStorage && $targetStorage) {
			if ($sourceStorage === $targetStorage) {
				$cache = $sourceStorage->getCache($sourceInternalPath);
				$cache->move($sourceInternalPath, $targetInternalPath);

				if (pathinfo($sourceInternalPath, PATHINFO_EXTENSION) !== pathinfo($targetInternalPath, PATHINFO_EXTENSION)) {
					// handle mime type change
					$mimeType = $sourceStorage->getMimeType($targetInternalPath);
					$fileId = $cache->getId($targetInternalPath);
					$cache->update($fileId, array('mimetype' => $mimeType));
				}

				$cache->correctFolderSize($sourceInternalPath);
				$cache->correctFolderSize($targetInternalPath);
				$this->correctParentStorageMtime($sourceStorage, $sourceInternalPath);
				$this->correctParentStorageMtime($targetStorage, $targetInternalPath);
				$this->propagator->addChange($source);
				$this->propagator->addChange($target);
			} else {
				$this->remove($source);
				$this->update($target);
			}
			$this->propagator->propagateChanges();
		}
	}

	/**
	 * update the storage_mtime of the parent
	 *
	 * @param \OC\Files\Storage\Storage $storage
	 * @param string $internalPath
	 */
	private function correctParentStorageMtime($storage, $internalPath) {
		$cache = $storage->getCache();
		$parentId = $cache->getParentId($internalPath);
		$parent = dirname($internalPath);
		if ($parentId != -1) {
			$cache->update($parentId, array('storage_mtime' => $storage->filemtime($parent)));
		}
	}

	public function __destruct() {
		// propagate any leftover changes
		$this->propagator->propagateChanges();
	}
}