Passed
Push — master ( 83ed1f...db0268 )
by John
16:12 queued 12s
created

ObjectStoreScanner::getIncomplete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
nc 2
nop 0
dl 0
loc 19
rs 9.8333
c 1
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Jörn Friedrich Dreyer <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 *
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
namespace OC\Files\ObjectStore;
28
29
use OC\Files\Cache\Scanner;
30
use OCP\DB\QueryBuilder\IQueryBuilder;
31
use OCP\Files\FileInfo;
32
33
class ObjectStoreScanner extends Scanner {
34
	public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true, $data = null) {
35
		return [];
36
	}
37
38
	public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) {
39
		return [];
40
	}
41
42
	protected function scanChildren($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $folderId = null, $lock = true, array $data = []) {
43
		return 0;
44
	}
45
46
	public function backgroundScan() {
47
		$lastPath = null;
48
		// find any path marked as unscanned and run the scanner until no more paths are unscanned (or we get stuck)
49
		// we sort by path DESC to ensure that contents of a folder are handled before the parent folder
50
		while (($path = $this->getIncomplete()) !== false && $path !== $lastPath) {
51
			$this->runBackgroundScanJob(function () use ($path) {
52
				$item = $this->cache->get($path);
53
				if ($item && $item->getMimeType() !== FileInfo::MIMETYPE_FOLDER) {
54
					$fh = $this->storage->fopen($path, 'r');
55
					if ($fh) {
56
						$stat = fstat($fh);
57
						if ($stat['size']) {
58
							$this->cache->update($item->getId(), ['size' => $stat['size']]);
59
						}
60
					}
61
				}
62
			}, $path);
63
			// FIXME: this won't proceed with the next item, needs revamping of getIncomplete()
64
			// to make this possible
65
			$lastPath = $path;
66
		}
67
	}
68
69
	/**
70
	 * Unlike the default Cache::getIncomplete this one sorts by path.
71
	 *
72
	 * This is needed since self::backgroundScan doesn't fix child entries when running on a parent folder.
73
	 * By sorting by path we ensure that we encounter the child entries first.
74
	 *
75
	 * @return false|string
76
	 * @throws \OCP\DB\Exception
77
	 */
78
	private function getIncomplete() {
79
		$query = $this->connection->getQueryBuilder();
80
		$query->select('path')
81
			->from('filecache')
82
			->where($query->expr()->eq('storage', $query->createNamedParameter($this->cache->getNumericStorageId(), IQueryBuilder::PARAM_INT)))
83
			->andWhere($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
84
			->orderBy('path', 'DESC')
85
			->setMaxResults(1);
86
87
		$result = $query->executeQuery();
88
		$path = $result->fetchOne();
89
		$result->closeCursor();
90
91
		if ($path === false) {
92
			return false;
93
		}
94
95
		// Make sure Oracle does not continue with null for empty strings
96
		return (string)$path;
97
	}
98
}
99