1<?php
2/**
3 * @author Andreas Fischer <bantu@owncloud.com>
4 * @author Björn Schießle <bjoern@schiessle.org>
5 * @author Jörn Friedrich Dreyer <jfd@butonic.de>
6 * @author Morris Jobke <hey@morrisjobke.de>
7 * @author Robin Appelman <icewind@owncloud.com>
8 * @author Thomas Müller <thomas.mueller@tmit.eu>
9 * @author Vincent Petry <pvince81@owncloud.com>
10 *
11 * @copyright Copyright (c) 2018, ownCloud GmbH
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
28namespace OC\Files\Cache;
29
30use OCP\Files\Cache\ICacheEntry;
31
32class HomeCache extends Cache {
33	/**
34	 * get the size of a folder and set it in the cache
35	 *
36	 * @param string $path
37	 * @param array $entry (optional) meta data of the folder
38	 * @return int
39	 */
40	public function calculateFolderSize($path, $entry = null) {
41		if ($path !== '/' and $path !== '' and $path !== 'files' and $path !== 'files_trashbin' and $path !== 'files_versions') {
42			return parent::calculateFolderSize($path, $entry);
43		} elseif ($path === '' or $path === '/') {
44			// since the size of / isn't used (the size of /files is used instead) there is no use in calculating it
45			return 0;
46		}
47
48		$totalSize = 0;
49		if ($entry === null) {
50			$entry = $this->get($path);
51		}
52		if ($entry && $entry['mimetype'] === 'httpd/unix-directory') {
53			$id = $entry['fileid'];
54			$sql = 'SELECT SUM(`size`) AS f1 ' .
55			   'FROM `*PREFIX*filecache` ' .
56				'WHERE `parent` = ? AND `storage` = ? AND `size` >= 0';
57			$result = \OC_DB::executeAudited($sql, [$id, $this->getNumericStorageId()]);
58			if ($row = $result->fetchRow()) {
59				$result->closeCursor();
60				list($sum) = \array_values($row);
61				$totalSize = 0 + $sum;
62				$entry['size'] += 0;
63				if ($entry['size'] !== $totalSize) {
64					$this->update($id, ['size' => $totalSize]);
65				}
66			}
67		}
68		return $totalSize;
69	}
70
71	/**
72	 * @param string $path
73	 * @return ICacheEntry
74	 */
75	public function get($path) {
76		$data = parent::get($path);
77		if ($path === '' or $path === '/') {
78			// only the size of the "files" dir counts
79			$filesData = parent::get('files');
80
81			if (isset($filesData['size'])) {
82				$data['size'] = $filesData['size'];
83			}
84		}
85		return $data;
86	}
87
88	/**
89	 * Returns false because the home cache shouldn't have any relevant incomplete entries.
90	 *
91	 * @return false
92	 */
93	public function getIncomplete() {
94		return false;
95	}
96}
97