Blame view

sources/lib/private/files/cache/cache.php 18.6 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  <?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;
  
  /**
   * Metadata cache for the filesystem
   *
   * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
   */
  class Cache {
  	const NOT_FOUND = 0;
  	const PARTIAL = 1; //only partial data available, file not cached in the database
  	const SHALLOW = 2; //folder in cache, but not all child files are completely scanned
  	const COMPLETE = 3;
  
  	/**
  	 * @var array partial data for the cache
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
25
  	protected $partial = array();
03e52840d   Kload   Init
26
27
28
29
  
  	/**
  	 * @var string
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
30
  	protected $storageId;
03e52840d   Kload   Init
31
32
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
33
  	 * @var Storage $storageCache
03e52840d   Kload   Init
34
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
35
  	protected $storageCache;
03e52840d   Kload   Init
36

6d9380f96   Cédric Dupont   Update sources OC...
37
38
  	protected static $mimetypeIds = array();
  	protected static $mimetypes = array();
03e52840d   Kload   Init
39
40
41
42
43
44
45
46
47
48
49
50
51
  
  	/**
  	 * @param \OC\Files\Storage\Storage|string $storage
  	 */
  	public function __construct($storage) {
  		if ($storage instanceof \OC\Files\Storage\Storage) {
  			$this->storageId = $storage->getId();
  		} else {
  			$this->storageId = $storage;
  		}
  		if (strlen($this->storageId) > 64) {
  			$this->storageId = md5($this->storageId);
  		}
31b7f2792   Kload   Upgrade to ownclo...
52
  		$this->storageCache = new Storage($storage);
03e52840d   Kload   Init
53
54
55
  	}
  
  	public function getNumericStorageId() {
31b7f2792   Kload   Upgrade to ownclo...
56
  		return $this->storageCache->getNumericId();
03e52840d   Kload   Init
57
58
59
60
61
62
63
64
65
  	}
  
  	/**
  	 * normalize mimetypes
  	 *
  	 * @param string $mime
  	 * @return int
  	 */
  	public function getMimetypeId($mime) {
31b7f2792   Kload   Upgrade to ownclo...
66
67
68
  		if (empty($mime)) {
  			// Can not insert empty string into Oracle NOT NULL column.
  			$mime = 'application/octet-stream';
03e52840d   Kload   Init
69
  		}
31b7f2792   Kload   Upgrade to ownclo...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  		if (empty(self::$mimetypeIds)) {
  			$this->loadMimetypes();
  		}
  		
  		if (!isset(self::$mimetypeIds[$mime])) {
  			try{
  				$result = \OC_DB::executeAudited('INSERT INTO `*PREFIX*mimetypes`(`mimetype`) VALUES(?)', array($mime));
  				self::$mimetypeIds[$mime] = \OC_DB::insertid('*PREFIX*mimetypes');
  				self::$mimetypes[self::$mimetypeIds[$mime]] = $mime;
  			}
  			catch (\Doctrine\DBAL\DBALException $e){
  				\OC_Log::write('core', 'Exception during mimetype insertion: ' . $e->getmessage(), \OC_Log::DEBUG);
  				return -1;
  			}
  		} 
  				
  		return self::$mimetypeIds[$mime];
03e52840d   Kload   Init
87
88
89
  	}
  
  	public function getMimetype($id) {
31b7f2792   Kload   Upgrade to ownclo...
90
91
  		if (empty(self::$mimetypes)) {
  			$this->loadMimetypes();
03e52840d   Kload   Init
92
  		}
31b7f2792   Kload   Upgrade to ownclo...
93
94
95
96
97
98
99
100
101
102
103
104
  
  		return isset(self::$mimetypes[$id]) ? self::$mimetypes[$id] : null;
  	}
  
  	public function loadMimetypes(){
  			$result = \OC_DB::executeAudited('SELECT `id`, `mimetype` FROM `*PREFIX*mimetypes`', array());
  			if ($result) {
  				while ($row = $result->fetchRow()) {
  					self::$mimetypeIds[$row['mimetype']] = $row['id'];
  					self::$mimetypes[$row['id']] = $row['mimetype'];
  				}
  			}
03e52840d   Kload   Init
105
106
107
108
109
110
  	}
  
  	/**
  	 * get the stored metadata of a file or folder
  	 *
  	 * @param string/int $file
6d9380f96   Cédric Dupont   Update sources OC...
111
  	 * @return array|false
03e52840d   Kload   Init
112
113
114
115
116
117
118
  	 */
  	public function get($file) {
  		if (is_string($file) or $file == '') {
  			// normalize file
  			$file = $this->normalize($file);
  
  			$where = 'WHERE `storage` = ? AND `path_hash` = ?';
31b7f2792   Kload   Upgrade to ownclo...
119
  			$params = array($this->getNumericStorageId(), md5($file));
03e52840d   Kload   Init
120
121
122
123
  		} else { //file id
  			$where = 'WHERE `fileid` = ?';
  			$params = array($file);
  		}
31b7f2792   Kload   Upgrade to ownclo...
124
  		$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
6d9380f96   Cédric Dupont   Update sources OC...
125
  					   `storage_mtime`, `encrypted`, `unencrypted_size`, `etag`, `permissions`
31b7f2792   Kload   Upgrade to ownclo...
126
127
  				FROM `*PREFIX*filecache` ' . $where;
  		$result = \OC_DB::executeAudited($sql, $params);
03e52840d   Kload   Init
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  		$data = $result->fetchRow();
  
  		//FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO
  		//PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false
  		if ($data === null) {
  			$data = false;
  		}
  
  		//merge partial data
  		if (!$data and  is_string($file)) {
  			if (isset($this->partial[$file])) {
  				$data = $this->partial[$file];
  			}
  		} else {
  			//fix types
  			$data['fileid'] = (int)$data['fileid'];
6d9380f96   Cédric Dupont   Update sources OC...
144
  			$data['size'] = 0 + $data['size'];
03e52840d   Kload   Init
145
  			$data['mtime'] = (int)$data['mtime'];
31b7f2792   Kload   Upgrade to ownclo...
146
  			$data['storage_mtime'] = (int)$data['storage_mtime'];
03e52840d   Kload   Init
147
  			$data['encrypted'] = (bool)$data['encrypted'];
6d9380f96   Cédric Dupont   Update sources OC...
148
              $data['unencrypted_size'] = 0 + $data['unencrypted_size'];
03e52840d   Kload   Init
149
150
151
  			$data['storage'] = $this->storageId;
  			$data['mimetype'] = $this->getMimetype($data['mimetype']);
  			$data['mimepart'] = $this->getMimetype($data['mimepart']);
31b7f2792   Kload   Upgrade to ownclo...
152
153
154
  			if ($data['storage_mtime'] == 0) {
  				$data['storage_mtime'] = $data['mtime'];
  			}
6d9380f96   Cédric Dupont   Update sources OC...
155
  			$data['permissions'] = (int)$data['permissions'];
03e52840d   Kload   Init
156
157
158
159
160
161
162
163
164
165
166
167
168
  		}
  
  		return $data;
  	}
  
  	/**
  	 * get the metadata of all files stored in $folder
  	 *
  	 * @param string $folder
  	 * @return array
  	 */
  	public function getFolderContents($folder) {
  		$fileId = $this->getId($folder);
6d9380f96   Cédric Dupont   Update sources OC...
169
170
171
172
173
174
175
176
177
178
  		return $this->getFolderContentsById($fileId);
  	}
  
  	/**
  	 * get the metadata of all files stored in $folder
  	 *
  	 * @param int $fileId the file id of the folder
  	 * @return array
  	 */
  	public function getFolderContentsById($fileId) {
03e52840d   Kload   Init
179
  		if ($fileId > -1) {
31b7f2792   Kload   Upgrade to ownclo...
180
  			$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
6d9380f96   Cédric Dupont   Update sources OC...
181
  						   `storage_mtime`, `encrypted`, `unencrypted_size`, `etag`, `permissions`
31b7f2792   Kload   Upgrade to ownclo...
182
183
  					FROM `*PREFIX*filecache` WHERE `parent` = ? ORDER BY `name` ASC';
  			$result = \OC_DB::executeAudited($sql,array($fileId));
03e52840d   Kload   Init
184
185
186
187
  			$files = $result->fetchAll();
  			foreach ($files as &$file) {
  				$file['mimetype'] = $this->getMimetype($file['mimetype']);
  				$file['mimepart'] = $this->getMimetype($file['mimepart']);
31b7f2792   Kload   Upgrade to ownclo...
188
189
190
  				if ($file['storage_mtime'] == 0) {
  					$file['storage_mtime'] = $file['mtime'];
  				}
a293d369c   Kload   Update sources to...
191
  				if ($file['encrypted'] or ($file['unencrypted_size'] > 0 and $file['mimetype'] === 'httpd/unix-directory')) {
31b7f2792   Kload   Upgrade to ownclo...
192
193
194
  					$file['encrypted_size'] = $file['size'];
  					$file['size'] = $file['unencrypted_size'];
  				}
6d9380f96   Cédric Dupont   Update sources OC...
195
  				$file['permissions'] = (int)$file['permissions'];
03e52840d   Kload   Init
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
225
226
227
228
229
230
231
232
233
  			}
  			return $files;
  		} else {
  			return array();
  		}
  	}
  
  	/**
  	 * store meta data for a file or folder
  	 *
  	 * @param string $file
  	 * @param array $data
  	 *
  	 * @return int file id
  	 */
  	public function put($file, array $data) {
  		if (($id = $this->getId($file)) > -1) {
  			$this->update($id, $data);
  			return $id;
  		} else {
  			// normalize file
  			$file = $this->normalize($file);
  
  			if (isset($this->partial[$file])) { //add any saved partial data
  				$data = array_merge($this->partial[$file], $data);
  				unset($this->partial[$file]);
  			}
  
  			$requiredFields = array('size', 'mtime', 'mimetype');
  			foreach ($requiredFields as $field) {
  				if (!isset($data[$field])) { //data not complete save as partial and return
  					$this->partial[$file] = $data;
  					return -1;
  				}
  			}
  
  			$data['path'] = $file;
  			$data['parent'] = $this->getParentId($file);
31b7f2792   Kload   Upgrade to ownclo...
234
  			$data['name'] = \OC_Util::basename($file);
03e52840d   Kload   Init
235
236
237
  
  			list($queryParts, $params) = $this->buildParts($data);
  			$queryParts[] = '`storage`';
31b7f2792   Kload   Upgrade to ownclo...
238
  			$params[] = $this->getNumericStorageId();
03e52840d   Kload   Init
239
  			$valuesPlaceholder = array_fill(0, count($queryParts), '?');
31b7f2792   Kload   Upgrade to ownclo...
240
241
242
  			$sql = 'INSERT INTO `*PREFIX*filecache` (' . implode(', ', $queryParts) . ')'
  				. ' VALUES (' . implode(', ', $valuesPlaceholder) . ')';
  			\OC_DB::executeAudited($sql, $params);
03e52840d   Kload   Init
243
244
245
246
247
248
249
250
251
252
253
254
  
  			return (int)\OC_DB::insertid('*PREFIX*filecache');
  		}
  	}
  
  	/**
  	 * update the metadata in the cache
  	 *
  	 * @param int $id
  	 * @param array $data
  	 */
  	public function update($id, array $data) {
31b7f2792   Kload   Upgrade to ownclo...
255

03e52840d   Kload   Init
256
257
258
259
260
261
262
263
264
265
266
267
  		if(isset($data['path'])) {
  			// normalize path
  			$data['path'] = $this->normalize($data['path']);
  		}
  
  		if(isset($data['name'])) {
  			// normalize path
  			$data['name'] = $this->normalize($data['name']);
  		}
  
  		list($queryParts, $params) = $this->buildParts($data);
  		$params[] = $id;
31b7f2792   Kload   Upgrade to ownclo...
268
269
  		$sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? WHERE `fileid` = ?';
  		\OC_DB::executeAudited($sql, $params);
03e52840d   Kload   Init
270
271
272
273
274
275
276
277
278
  	}
  
  	/**
  	 * extract query parts and params array from data array
  	 *
  	 * @param array $data
  	 * @return array
  	 */
  	function buildParts(array $data) {
6d9380f96   Cédric Dupont   Update sources OC...
279
280
281
  		$fields = array(
  			'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted', 'unencrypted_size',
  			'etag', 'permissions');
03e52840d   Kload   Init
282
283
284
285
286
287
288
289
290
291
292
  		$params = array();
  		$queryParts = array();
  		foreach ($data as $name => $value) {
  			if (array_search($name, $fields) !== false) {
  				if ($name === 'path') {
  					$params[] = md5($value);
  					$queryParts[] = '`path_hash`';
  				} elseif ($name === 'mimetype') {
  					$params[] = $this->getMimetypeId(substr($value, 0, strpos($value, '/')));
  					$queryParts[] = '`mimepart`';
  					$value = $this->getMimetypeId($value);
31b7f2792   Kload   Upgrade to ownclo...
293
294
295
296
297
298
299
300
  				} elseif ($name === 'storage_mtime') {
  					if (!isset($data['mtime'])) {
  						$params[] = $value;
  						$queryParts[] = '`mtime`';
  					}
  				} elseif ($name === 'encrypted') {
  					// Boolean to integer conversion
  					$value = $value ? 1 : 0;
03e52840d   Kload   Init
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
  				}
  				$params[] = $value;
  				$queryParts[] = '`' . $name . '`';
  			}
  		}
  		return array($queryParts, $params);
  	}
  
  	/**
  	 * get the file id for a file
  	 *
  	 * @param string $file
  	 * @return int
  	 */
  	public function getId($file) {
31b7f2792   Kload   Upgrade to ownclo...
316
  		// normalize file
03e52840d   Kload   Init
317
318
319
  		$file = $this->normalize($file);
  
  		$pathHash = md5($file);
31b7f2792   Kload   Upgrade to ownclo...
320
321
  		$sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?';
  		$result = \OC_DB::executeAudited($sql, array($this->getNumericStorageId(), $pathHash));
03e52840d   Kload   Init
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
  		if ($row = $result->fetchRow()) {
  			return $row['fileid'];
  		} else {
  			return -1;
  		}
  	}
  
  	/**
  	 * get the id of the parent folder of a file
  	 *
  	 * @param string $file
  	 * @return int
  	 */
  	public function getParentId($file) {
  		if ($file === '') {
  			return -1;
  		} else {
  			$parent = dirname($file);
  			if ($parent === '.') {
  				$parent = '';
  			}
  			return $this->getId($parent);
  		}
  	}
  
  	/**
  	 * check if a file is available in the cache
  	 *
  	 * @param string $file
  	 * @return bool
  	 */
  	public function inCache($file) {
  		return $this->getId($file) != -1;
  	}
  
  	/**
  	 * remove a file or folder from the cache
  	 *
  	 * @param string $file
  	 */
  	public function remove($file) {
  		$entry = $this->get($file);
  		if ($entry['mimetype'] === 'httpd/unix-directory') {
  			$children = $this->getFolderContents($file);
  			foreach ($children as $child) {
  				$this->remove($child['path']);
  			}
  		}
31b7f2792   Kload   Upgrade to ownclo...
370
371
372
  		
  		$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
  		\OC_DB::executeAudited($sql, array($entry['fileid']));
03e52840d   Kload   Init
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
  	}
  
  	/**
  	 * Move a file or folder in the cache
  	 *
  	 * @param string $source
  	 * @param string $target
  	 */
  	public function move($source, $target) {
  		// normalize source and target
  		$source = $this->normalize($source);
  		$target = $this->normalize($target);
  
  		$sourceData = $this->get($source);
  		$sourceId = $sourceData['fileid'];
  		$newParentId = $this->getParentId($target);
  
  		if ($sourceData['mimetype'] === 'httpd/unix-directory') {
  			//find all child entries
31b7f2792   Kload   Upgrade to ownclo...
392
393
  			$sql = 'SELECT `path`, `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path` LIKE ?';
  			$result = \OC_DB::executeAudited($sql, array($this->getNumericStorageId(), $source . '/%'));
03e52840d   Kload   Init
394
395
396
397
398
399
  			$childEntries = $result->fetchAll();
  			$sourceLength = strlen($source);
  			$query = \OC_DB::prepare('UPDATE `*PREFIX*filecache` SET `path` = ?, `path_hash` = ? WHERE `fileid` = ?');
  
  			foreach ($childEntries as $child) {
  				$targetPath = $target . substr($child['path'], $sourceLength);
31b7f2792   Kload   Upgrade to ownclo...
400
  				\OC_DB::executeAudited($query, array($targetPath, md5($targetPath), $child['fileid']));
03e52840d   Kload   Init
401
402
  			}
  		}
31b7f2792   Kload   Upgrade to ownclo...
403
404
  		$sql = 'UPDATE `*PREFIX*filecache` SET `path` = ?, `path_hash` = ?, `name` = ?, `parent` =? WHERE `fileid` = ?';
  		\OC_DB::executeAudited($sql, array($target, md5($target), basename($target), $newParentId, $sourceId));
03e52840d   Kload   Init
405
406
407
408
409
410
  	}
  
  	/**
  	 * remove all entries for files that are stored on the storage from the cache
  	 */
  	public function clear() {
31b7f2792   Kload   Upgrade to ownclo...
411
412
413
414
415
  		$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?';
  		\OC_DB::executeAudited($sql, array($this->getNumericStorageId()));
  
  		$sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?';
  		\OC_DB::executeAudited($sql, array($this->storageId));
03e52840d   Kload   Init
416
417
418
419
420
421
422
423
424
425
426
427
  	}
  
  	/**
  	 * @param string $file
  	 *
  	 * @return int, Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
  	 */
  	public function getStatus($file) {
  		// normalize file
  		$file = $this->normalize($file);
  
  		$pathHash = md5($file);
31b7f2792   Kload   Upgrade to ownclo...
428
429
  		$sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?';
  		$result = \OC_DB::executeAudited($sql, array($this->getNumericStorageId(), $pathHash));
03e52840d   Kload   Init
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
  		if ($row = $result->fetchRow()) {
  			if ((int)$row['size'] === -1) {
  				return self::SHALLOW;
  			} else {
  				return self::COMPLETE;
  			}
  		} else {
  			if (isset($this->partial[$file])) {
  				return self::PARTIAL;
  			} else {
  				return self::NOT_FOUND;
  			}
  		}
  	}
  
  	/**
  	 * search for files matching $pattern
  	 *
  	 * @param string $pattern
6d9380f96   Cédric Dupont   Update sources OC...
449
  	 * @return array an array of file data
03e52840d   Kload   Init
450
451
  	 */
  	public function search($pattern) {
31b7f2792   Kload   Upgrade to ownclo...
452

03e52840d   Kload   Init
453
454
  		// normalize pattern
  		$pattern = $this->normalize($pattern);
6d9380f96   Cédric Dupont   Update sources OC...
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
  
  		$sql = '
  			SELECT `fileid`, `storage`, `path`, `parent`, `name`,
  				`mimetype`, `mimepart`, `size`, `mtime`, `encrypted`,
  				`unencrypted_size`, `etag`, `permissions`
  			FROM `*PREFIX*filecache`
  			WHERE `storage` = ? AND ';
  		$dbtype = \OC_Config::getValue( 'dbtype', 'sqlite' );
  		if($dbtype === 'oci') {
  			//remove starting and ending % from the pattern
  			$pattern = '^'.str_replace('%', '.*', $pattern).'$';
  			$sql .= 'REGEXP_LIKE(`name`, ?, \'i\')';
  		} else if($dbtype === 'pgsql') {
  			$sql .= '`name` ILIKE ?';
  		} else if ($dbtype === 'mysql') {
  			$sql .= '`name` COLLATE utf8_general_ci LIKE ?';
  		} else {
  			$sql .= '`name` LIKE ?';
  		}
  		$result = \OC_DB::executeAudited($sql,
  			array($this->getNumericStorageId(), $pattern)
  		);
03e52840d   Kload   Init
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
  		$files = array();
  		while ($row = $result->fetchRow()) {
  			$row['mimetype'] = $this->getMimetype($row['mimetype']);
  			$row['mimepart'] = $this->getMimetype($row['mimepart']);
  			$files[] = $row;
  		}
  		return $files;
  	}
  
  	/**
  	 * search for files by mimetype
  	 *
  	 * @param string $mimetype
  	 * @return array
  	 */
  	public function searchByMime($mimetype) {
  		if (strpos($mimetype, '/')) {
  			$where = '`mimetype` = ?';
  		} else {
  			$where = '`mimepart` = ?';
  		}
6d9380f96   Cédric Dupont   Update sources OC...
498
  		$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`, `unencrypted_size`, `etag`, `permissions`
31b7f2792   Kload   Upgrade to ownclo...
499
  				FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?';
03e52840d   Kload   Init
500
  		$mimetype = $this->getMimetypeId($mimetype);
31b7f2792   Kload   Upgrade to ownclo...
501
  		$result = \OC_DB::executeAudited($sql, array($mimetype, $this->getNumericStorageId()));
03e52840d   Kload   Init
502
503
504
505
506
507
508
509
510
511
512
513
  		$files = array();
  		while ($row = $result->fetchRow()) {
  			$row['mimetype'] = $this->getMimetype($row['mimetype']);
  			$row['mimepart'] = $this->getMimetype($row['mimepart']);
  			$files[] = $row;
  		}
  		return $files;
  	}
  
  	/**
  	 * update the folder size and the size of all parent folders
  	 *
6d9380f96   Cédric Dupont   Update sources OC...
514
515
  	 * @param string|boolean $path
  	 * @param array $data (optional) meta data of the folder
03e52840d   Kload   Init
516
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
517
518
  	public function correctFolderSize($path, $data = null) {
  		$this->calculateFolderSize($path, $data);
03e52840d   Kload   Init
519
520
521
522
523
524
525
526
527
528
529
530
531
  		if ($path !== '') {
  			$parent = dirname($path);
  			if ($parent === '.' or $parent === '/') {
  				$parent = '';
  			}
  			$this->correctFolderSize($parent);
  		}
  	}
  
  	/**
  	 * get the size of a folder and set it in the cache
  	 *
  	 * @param string $path
6d9380f96   Cédric Dupont   Update sources OC...
532
  	 * @param array $entry (optional) meta data of the folder
03e52840d   Kload   Init
533
534
  	 * @return int
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
535
  	public function calculateFolderSize($path, $entry = null) {
03e52840d   Kload   Init
536
  		$totalSize = 0;
6d9380f96   Cédric Dupont   Update sources OC...
537
538
539
  		if (is_null($entry) or !isset($entry['fileid'])) {
  			$entry = $this->get($path);
  		}
03e52840d   Kload   Init
540
541
  		if ($entry && $entry['mimetype'] === 'httpd/unix-directory') {
  			$id = $entry['fileid'];
a293d369c   Kload   Update sources to...
542
543
544
  			$sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2, ' .
  				'SUM(`unencrypted_size`) AS f3 ' .
  				'FROM `*PREFIX*filecache` ' .
31b7f2792   Kload   Upgrade to ownclo...
545
546
  				'WHERE `parent` = ? AND `storage` = ?';
  			$result = \OC_DB::executeAudited($sql, array($id, $this->getNumericStorageId()));
03e52840d   Kload   Init
547
  			if ($row = $result->fetchRow()) {
a293d369c   Kload   Update sources to...
548
  				list($sum, $min, $unencryptedSum) = array_values($row);
6d9380f96   Cédric Dupont   Update sources OC...
549
550
551
  				$sum = 0 + $sum;
  				$min = 0 + $min;
  				$unencryptedSum = 0 + $unencryptedSum;
03e52840d   Kload   Init
552
553
554
555
556
  				if ($min === -1) {
  					$totalSize = $min;
  				} else {
  					$totalSize = $sum;
  				}
a293d369c   Kload   Update sources to...
557
  				$update = array();
03e52840d   Kload   Init
558
  				if ($entry['size'] !== $totalSize) {
a293d369c   Kload   Update sources to...
559
560
  					$update['size'] = $totalSize;
  				}
6d9380f96   Cédric Dupont   Update sources OC...
561
  				if (!isset($entry['unencrypted_size']) or $entry['unencrypted_size'] !== $unencryptedSum) {
a293d369c   Kload   Update sources to...
562
563
564
565
566
567
568
  					$update['unencrypted_size'] = $unencryptedSum;
  				}
  				if (count($update) > 0) {
  					$this->update($id, $update);
  				}
  				if ($totalSize !== -1 and $unencryptedSum > 0) {
  					$totalSize = $unencryptedSum;
03e52840d   Kload   Init
569
  				}
03e52840d   Kload   Init
570
571
572
573
574
575
576
577
578
579
580
  			}
  		}
  		return $totalSize;
  	}
  
  	/**
  	 * get all file ids on the files on the storage
  	 *
  	 * @return int[]
  	 */
  	public function getAll() {
31b7f2792   Kload   Upgrade to ownclo...
581
582
  		$sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?';
  		$result = \OC_DB::executeAudited($sql, array($this->getNumericStorageId()));
03e52840d   Kload   Init
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
  		$ids = array();
  		while ($row = $result->fetchRow()) {
  			$ids[] = $row['fileid'];
  		}
  		return $ids;
  	}
  
  	/**
  	 * find a folder in the cache which has not been fully scanned
  	 *
  	 * If multiply incomplete folders are in the cache, the one with the highest id will be returned,
  	 * use the one with the highest id gives the best result with the background scanner, since that is most
  	 * likely the folder where we stopped scanning previously
  	 *
  	 * @return string|bool the path of the folder or false when no folder matched
  	 */
  	public function getIncomplete() {
31b7f2792   Kload   Upgrade to ownclo...
600
601
602
  		$query = \OC_DB::prepare('SELECT `path` FROM `*PREFIX*filecache`'
  			. ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC',1);
  		$result = \OC_DB::executeAudited($query, array($this->getNumericStorageId()));
03e52840d   Kload   Init
603
604
605
606
607
608
609
610
  		if ($row = $result->fetchRow()) {
  			return $row['path'];
  		} else {
  			return false;
  		}
  	}
  
  	/**
837968727   Kload   [enh] Upgrade to ...
611
612
613
  	 * get the path of a file on this storage by it's id
  	 *
  	 * @param int $id
6d9380f96   Cédric Dupont   Update sources OC...
614
  	 * @return string|null
837968727   Kload   [enh] Upgrade to ...
615
616
617
618
619
  	 */
  	public function getPathById($id) {
  		$sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ? AND `storage` = ?';
  		$result = \OC_DB::executeAudited($sql, array($id, $this->getNumericStorageId()));
  		if ($row = $result->fetchRow()) {
6d9380f96   Cédric Dupont   Update sources OC...
620
621
622
623
  			// Oracle stores empty strings as null...
  			if ($row['path'] === null) {
  				return '';
  			}
837968727   Kload   [enh] Upgrade to ...
624
625
626
627
628
629
630
  			return $row['path'];
  		} else {
  			return null;
  		}
  	}
  
  	/**
03e52840d   Kload   Init
631
  	 * get the storage id of the storage for a file and the internal path of the file
837968727   Kload   [enh] Upgrade to ...
632
633
  	 * unlike getPathById this does not limit the search to files on this storage and
  	 * instead does a global search in the cache table
03e52840d   Kload   Init
634
  	 *
31b7f2792   Kload   Upgrade to ownclo...
635
  	 * @param int $id
03e52840d   Kload   Init
636
637
638
  	 * @return array, first element holding the storage id, second the path
  	 */
  	static public function getById($id) {
31b7f2792   Kload   Upgrade to ownclo...
639
640
  		$sql = 'SELECT `storage`, `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?';
  		$result = \OC_DB::executeAudited($sql, array($id));
03e52840d   Kload   Init
641
642
643
644
645
646
  		if ($row = $result->fetchRow()) {
  			$numericId = $row['storage'];
  			$path = $row['path'];
  		} else {
  			return null;
  		}
31b7f2792   Kload   Upgrade to ownclo...
647
648
  		if ($id = Storage::getStorageId($numericId)) {
  			return array($id, $path);
03e52840d   Kload   Init
649
650
651
652
653
654
655
  		} else {
  			return null;
  		}
  	}
  
  	/**
  	 * normalize the given path
6d9380f96   Cédric Dupont   Update sources OC...
656
  	 * @param string $path
03e52840d   Kload   Init
657
658
659
  	 * @return string
  	 */
  	public function normalize($path) {
31b7f2792   Kload   Upgrade to ownclo...
660

03e52840d   Kload   Init
661
662
  		return \OC_Util::normalizeUnicode($path);
  	}
03e52840d   Kload   Init
663
  }