Completed
Push — master ( a0132a...a06ba6 )
by Morris
69:55 queued 52:37
created

Storage::getExpireList()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 6
nop 3
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Bart Visscher <[email protected]>
7
 * @author Bjoern Schiessle <[email protected]>
8
 * @author Björn Schießle <[email protected]>
9
 * @author Carlos Damken <[email protected]>
10
 * @author Felix Moeller <[email protected]>
11
 * @author Georg Ehrke <[email protected]>
12
 * @author Joas Schilling <[email protected]>
13
 * @author Jörn Friedrich Dreyer <[email protected]>
14
 * @author Lukas Reschke <[email protected]>
15
 * @author Morris Jobke <[email protected]>
16
 * @author Robin Appelman <[email protected]>
17
 * @author Robin McCorkell <[email protected]>
18
 * @author Thomas Müller <[email protected]>
19
 * @author Victor Dubiniuk <[email protected]>
20
 * @author Vincent Petry <[email protected]>
21
 *
22
 * @license AGPL-3.0
23
 *
24
 * This code is free software: you can redistribute it and/or modify
25
 * it under the terms of the GNU Affero General Public License, version 3,
26
 * as published by the Free Software Foundation.
27
 *
28
 * This program is distributed in the hope that it will be useful,
29
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31
 * GNU Affero General Public License for more details.
32
 *
33
 * You should have received a copy of the GNU Affero General Public License, version 3,
34
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
35
 *
36
 */
37
38
/**
39
 * Versions
40
 *
41
 * A class to handle the versioning of files.
42
 */
43
44
namespace OCA\Files_Versions;
45
46
use OC\Files\Filesystem;
47
use OC\Files\View;
48
use OCA\Files_Versions\AppInfo\Application;
49
use OCA\Files_Versions\Command\Expire;
50
use OCA\Files_Versions\Events\CreateVersionEvent;
51
use OCP\Files\NotFoundException;
52
use OCP\Lock\ILockingProvider;
53
use OCP\User;
54
55
class Storage {
56
57
	const DEFAULTENABLED=true;
58
	const DEFAULTMAXSIZE=50; // unit: percentage; 50% of available disk space/quota
59
	const VERSIONS_ROOT = 'files_versions/';
60
61
	const DELETE_TRIGGER_MASTER_REMOVED = 0;
62
	const DELETE_TRIGGER_RETENTION_CONSTRAINT = 1;
63
	const DELETE_TRIGGER_QUOTA_EXCEEDED = 2;
64
65
	// files for which we can remove the versions after the delete operation was successful
66
	private static $deletedFiles = array();
67
68
	private static $sourcePathAndUser = array();
69
70
	private static $max_versions_per_interval = array(
0 ignored issues
show
Unused Code introduced by
The property $max_versions_per_interval is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
71
		//first 10sec, one version every 2sec
72
		1 => array('intervalEndsAfter' => 10,      'step' => 2),
73
		//next minute, one version every 10sec
74
		2 => array('intervalEndsAfter' => 60,      'step' => 10),
75
		//next hour, one version every minute
76
		3 => array('intervalEndsAfter' => 3600,    'step' => 60),
77
		//next 24h, one version every hour
78
		4 => array('intervalEndsAfter' => 86400,   'step' => 3600),
79
		//next 30days, one version per day
80
		5 => array('intervalEndsAfter' => 2592000, 'step' => 86400),
81
		//until the end one version per week
82
		6 => array('intervalEndsAfter' => -1,      'step' => 604800),
83
	);
84
85
	/** @var \OCA\Files_Versions\AppInfo\Application */
86
	private static $application;
87
88
	/**
89
	 * get the UID of the owner of the file and the path to the file relative to
90
	 * owners files folder
91
	 *
92
	 * @param string $filename
93
	 * @return array
94
	 * @throws \OC\User\NoUserException
95
	 */
96 View Code Duplication
	public static function getUidAndFilename($filename) {
97
		$uid = Filesystem::getOwner($filename);
98
		$userManager = \OC::$server->getUserManager();
99
		// if the user with the UID doesn't exists, e.g. because the UID points
100
		// to a remote user with a federated cloud ID we use the current logged-in
101
		// user. We need a valid local user to create the versions
102
		if (!$userManager->userExists($uid)) {
103
			$uid = User::getUser();
104
		}
105
		Filesystem::initMountPoints($uid);
106
		if ( $uid !== User::getUser() ) {
107
			$info = Filesystem::getFileInfo($filename);
108
			$ownerView = new View('/'.$uid.'/files');
109
			try {
110
				$filename = $ownerView->getPath($info['fileid']);
111
				// make sure that the file name doesn't end with a trailing slash
112
				// can for example happen single files shared across servers
113
				$filename = rtrim($filename, '/');
114
			} catch (NotFoundException $e) {
115
				$filename = null;
116
			}
117
		}
118
		return [$uid, $filename];
119
	}
120
121
	/**
122
	 * Remember the owner and the owner path of the source file
123
	 *
124
	 * @param string $source source path
125
	 */
126
	public static function setSourcePathAndUser($source) {
127
		list($uid, $path) = self::getUidAndFilename($source);
128
		self::$sourcePathAndUser[$source] = array('uid' => $uid, 'path' => $path);
129
	}
130
131
	/**
132
	 * Gets the owner and the owner path from the source path
133
	 *
134
	 * @param string $source source path
135
	 * @return array with user id and path
136
	 */
137
	public static function getSourcePathAndUser($source) {
138
139
		if (isset(self::$sourcePathAndUser[$source])) {
140
			$uid = self::$sourcePathAndUser[$source]['uid'];
141
			$path = self::$sourcePathAndUser[$source]['path'];
142
			unset(self::$sourcePathAndUser[$source]);
143
		} else {
144
			$uid = $path = false;
145
		}
146
		return array($uid, $path);
147
	}
148
149
	/**
150
	 * get current size of all versions from a given user
151
	 *
152
	 * @param string $user user who owns the versions
153
	 * @return int versions size
154
	 */
155
	private static function getVersionsSize($user) {
156
		$view = new View('/' . $user);
157
		$fileInfo = $view->getFileInfo('/files_versions');
158
		return isset($fileInfo['size']) ? $fileInfo['size'] : 0;
159
	}
160
161
	/**
162
	 * store a new version of a file.
163
	 */
164
	public static function store($filename) {
165
166
		// if the file gets streamed we need to remove the .part extension
167
		// to get the right target
168
		$ext = pathinfo($filename, PATHINFO_EXTENSION);
169
		if ($ext === 'part') {
170
			$filename = substr($filename, 0, strlen($filename) - 5);
171
		}
172
173
		// we only handle existing files
174
		if (! Filesystem::file_exists($filename) || Filesystem::is_dir($filename)) {
175
			return false;
176
		}
177
178
		list($uid, $filename) = self::getUidAndFilename($filename);
179
180
		$files_view = new View('/'.$uid .'/files');
181
		$users_view = new View('/'.$uid);
182
183
		$eventDispatcher = \OC::$server->getEventDispatcher();
184
		$id = $files_view->getFileInfo($filename)->getId();
185
		$nodes = \OC::$server->getRootFolder()->getById($id);
186
		foreach ($nodes as $node) {
187
			$event = new CreateVersionEvent($node);
188
			$eventDispatcher->dispatch('OCA\Files_Versions::createVersion', $event);
189
			if ($event->shouldCreateVersion() === false) {
190
				return false;
191
			}
192
		}
193
194
		// no use making versions for empty files
195
		if ($files_view->filesize($filename) === 0) {
196
			return false;
197
		}
198
199
		// create all parent folders
200
		self::createMissingDirectories($filename, $users_view);
201
202
		self::scheduleExpire($uid, $filename);
203
204
		// store a new version of a file
205
		$mtime = $users_view->filemtime('files/' . $filename);
206
		$users_view->copy('files/' . $filename, 'files_versions/' . $filename . '.v' . $mtime);
207
		// call getFileInfo to enforce a file cache entry for the new version
208
		$users_view->getFileInfo('files_versions/' . $filename . '.v' . $mtime);
209
	}
210
211
212
	/**
213
	 * mark file as deleted so that we can remove the versions if the file is gone
214
	 * @param string $path
215
	 */
216
	public static function markDeletedFile($path) {
217
		list($uid, $filename) = self::getUidAndFilename($path);
218
		self::$deletedFiles[$path] = array(
219
			'uid' => $uid,
220
			'filename' => $filename);
221
	}
222
223
	/**
224
	 * delete the version from the storage and cache
225
	 *
226
	 * @param View $view
227
	 * @param string $path
228
	 */
229
	protected static function deleteVersion($view, $path) {
230
		$view->unlink($path);
231
		/**
232
		 * @var \OC\Files\Storage\Storage $storage
233
		 * @var string $internalPath
234
		 */
235
		list($storage, $internalPath) = $view->resolvePath($path);
236
		$cache = $storage->getCache($internalPath);
237
		$cache->remove($internalPath);
238
	}
239
240
	/**
241
	 * Delete versions of a file
242
	 */
243
	public static function delete($path) {
244
245
		$deletedFile = self::$deletedFiles[$path];
246
		$uid = $deletedFile['uid'];
247
		$filename = $deletedFile['filename'];
248
249
		if (!Filesystem::file_exists($path)) {
250
251
			$view = new View('/' . $uid . '/files_versions');
252
253
			$versions = self::getVersions($uid, $filename);
254
			if (!empty($versions)) {
255
				foreach ($versions as $v) {
256
					\OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED));
257
					self::deleteVersion($view, $filename . '.v' . $v['version']);
258
					\OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED));
259
				}
260
			}
261
		}
262
		unset(self::$deletedFiles[$path]);
263
	}
264
265
	/**
266
	 * Rename or copy versions of a file of the given paths
267
	 *
268
	 * @param string $sourcePath source path of the file to move, relative to
269
	 * the currently logged in user's "files" folder
270
	 * @param string $targetPath target path of the file to move, relative to
271
	 * the currently logged in user's "files" folder
272
	 * @param string $operation can be 'copy' or 'rename'
273
	 */
274
	public static function renameOrCopy($sourcePath, $targetPath, $operation) {
275
		list($sourceOwner, $sourcePath) = self::getSourcePathAndUser($sourcePath);
276
277
		// it was a upload of a existing file if no old path exists
278
		// in this case the pre-hook already called the store method and we can
279
		// stop here
280
		if ($sourcePath === false) {
281
			return true;
282
		}
283
284
		list($targetOwner, $targetPath) = self::getUidAndFilename($targetPath);
285
286
		$sourcePath = ltrim($sourcePath, '/');
287
		$targetPath = ltrim($targetPath, '/');
288
289
		$rootView = new View('');
290
291
		// did we move a directory ?
292
		if ($rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) {
293
			// does the directory exists for versions too ?
294
			if ($rootView->is_dir('/' . $sourceOwner . '/files_versions/' . $sourcePath)) {
295
				// create missing dirs if necessary
296
				self::createMissingDirectories($targetPath, new View('/'. $targetOwner));
297
298
				// move the directory containing the versions
299
				$rootView->$operation(
300
					'/' . $sourceOwner . '/files_versions/' . $sourcePath,
301
					'/' . $targetOwner . '/files_versions/' . $targetPath
302
				);
303
			}
304
		} else if ($versions = Storage::getVersions($sourceOwner, '/' . $sourcePath)) {
305
			// create missing dirs if necessary
306
			self::createMissingDirectories($targetPath, new View('/'. $targetOwner));
307
308
			foreach ($versions as $v) {
309
				// move each version one by one to the target directory
310
				$rootView->$operation(
311
					'/' . $sourceOwner . '/files_versions/' . $sourcePath.'.v' . $v['version'],
312
					'/' . $targetOwner . '/files_versions/' . $targetPath.'.v'.$v['version']
313
				);
314
			}
315
		}
316
317
		// if we moved versions directly for a file, schedule expiration check for that file
318
		if (!$rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) {
319
			self::scheduleExpire($targetOwner, $targetPath);
320
		}
321
322
	}
323
324
	/**
325
	 * Rollback to an old version of a file.
326
	 *
327
	 * @param string $file file name
328
	 * @param int $revision revision timestamp
329
	 * @return bool
330
	 */
331
	public static function rollback($file, $revision) {
332
333
		// add expected leading slash
334
		$file = '/' . ltrim($file, '/');
335
		list($uid, $filename) = self::getUidAndFilename($file);
336
		if ($uid === null || trim($filename, '/') === '') {
337
			return false;
338
		}
339
340
		$users_view = new View('/'.$uid);
341
		$files_view = new View('/'. User::getUser().'/files');
342
343
		$versionCreated = false;
344
345
		$fileInfo = $files_view->getFileInfo($file);
346
347
		// check if user has the permissions to revert a version
348
		if (!$fileInfo->isUpdateable()) {
349
			return false;
350
		}
351
352
		//first create a new version
353
		$version = 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename);
354
		if (!$users_view->file_exists($version)) {
355
			$users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename));
356
			$versionCreated = true;
357
		}
358
359
		$fileToRestore =  'files_versions' . $filename . '.v' . $revision;
360
361
		// Restore encrypted version of the old file for the newly restored file
362
		// This has to happen manually here since the file is manually copied below
363
		$oldVersion = $users_view->getFileInfo($fileToRestore)->getEncryptedVersion();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface OCP\Files\FileInfo as the method getEncryptedVersion() does only exist in the following implementations of said interface: OC\Files\FileInfo.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
364
		$oldFileInfo = $users_view->getFileInfo($fileToRestore);
365
		$cache = $fileInfo->getStorage()->getCache();
366
		$cache->update(
367
			$fileInfo->getId(), [
368
				'encrypted' => $oldVersion,
369
				'encryptedVersion' => $oldVersion,
370
				'size' => $oldFileInfo->getSize()
371
			]
372
		);
373
374
		// rollback
375
		if (self::copyFileContents($users_view, $fileToRestore, 'files' . $filename)) {
376
			$files_view->touch($file, $revision);
377
			Storage::scheduleExpire($uid, $file);
378
			\OC_Hook::emit('\OCP\Versions', 'rollback', array(
379
				'path' => $filename,
380
				'revision' => $revision,
381
			));
382
			return true;
383
		} else if ($versionCreated) {
384
			self::deleteVersion($users_view, $version);
385
		}
386
387
		return false;
388
389
	}
390
391
	/**
392
	 * Stream copy file contents from $path1 to $path2
393
	 *
394
	 * @param View $view view to use for copying
395
	 * @param string $path1 source file to copy
396
	 * @param string $path2 target file
397
	 *
398
	 * @return bool true for success, false otherwise
399
	 */
400
	private static function copyFileContents($view, $path1, $path2) {
401
		/** @var \OC\Files\Storage\Storage $storage1 */
402
		list($storage1, $internalPath1) = $view->resolvePath($path1);
403
		/** @var \OC\Files\Storage\Storage $storage2 */
404
		list($storage2, $internalPath2) = $view->resolvePath($path2);
405
406
		$view->lockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
407
		$view->lockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);
408
409
		// TODO add a proper way of overwriting a file while maintaining file ids
410
		if ($storage1->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage') || $storage2->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage')) {
411
			$source = $storage1->fopen($internalPath1, 'r');
412
			$target = $storage2->fopen($internalPath2, 'w');
413
			list(, $result) = \OC_Helper::streamCopy($source, $target);
0 ignored issues
show
Security Bug introduced by
It seems like $source can also be of type false; however, OC_Helper::streamCopy() does only seem to accept resource, did you maybe forget to handle an error condition?
Loading history...
Security Bug introduced by
It seems like $target can also be of type false; however, OC_Helper::streamCopy() does only seem to accept resource, did you maybe forget to handle an error condition?
Loading history...
414
			fclose($source);
415
			fclose($target);
416
417
			if ($result !== false) {
418
				$storage1->unlink($internalPath1);
419
			}
420
		} else {
421
			$result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
422
		}
423
424
		$view->unlockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
425
		$view->unlockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);
426
427
		return ($result !== false);
428
	}
429
430
	/**
431
	 * get a list of all available versions of a file in descending chronological order
432
	 * @param string $uid user id from the owner of the file
433
	 * @param string $filename file to find versions of, relative to the user files dir
434
	 * @param string $userFullPath
435
	 * @return array versions newest version first
436
	 */
437
	public static function getVersions($uid, $filename, $userFullPath = '') {
438
		$versions = array();
439
		if (empty($filename)) {
440
			return $versions;
441
		}
442
		// fetch for old versions
443
		$view = new View('/' . $uid . '/');
444
445
		$pathinfo = pathinfo($filename);
446
		$versionedFile = $pathinfo['basename'];
447
448
		$dir = Filesystem::normalizePath(self::VERSIONS_ROOT . '/' . $pathinfo['dirname']);
449
450
		$dirContent = false;
451
		if ($view->is_dir($dir)) {
452
			$dirContent = $view->opendir($dir);
453
		}
454
455
		if ($dirContent === false) {
456
			return $versions;
457
		}
458
459
		if (is_resource($dirContent)) {
460
			while (($entryName = readdir($dirContent)) !== false) {
461
				if (!Filesystem::isIgnoredDir($entryName)) {
462
					$pathparts = pathinfo($entryName);
463
					$filename = $pathparts['filename'];
464
					if ($filename === $versionedFile) {
465
						$pathparts = pathinfo($entryName);
466
						$timestamp = substr($pathparts['extension'], 1);
467
						$filename = $pathparts['filename'];
468
						$key = $timestamp . '#' . $filename;
469
						$versions[$key]['version'] = $timestamp;
470
						$versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp($timestamp);
471
						if (empty($userFullPath)) {
472
							$versions[$key]['preview'] = '';
473
						} else {
474
							$versions[$key]['preview'] = \OC::$server->getURLGenerator('files_version.Preview.getPreview', ['file' => $userFullPath, 'version' => $timestamp]);
475
						}
476
						$versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename);
477
						$versions[$key]['name'] = $versionedFile;
478
						$versions[$key]['size'] = $view->filesize($dir . '/' . $entryName);
479
						$versions[$key]['mimetype'] = \OC::$server->getMimeTypeDetector()->detectPath($versionedFile);
480
					}
481
				}
482
			}
483
			closedir($dirContent);
484
		}
485
486
		// sort with newest version first
487
		krsort($versions);
488
489
		return $versions;
490
	}
491
492
	/**
493
	 * Expire versions that older than max version retention time
494
	 * @param string $uid
495
	 */
496
	public static function expireOlderThanMaxForUser($uid){
497
		$expiration = self::getExpiration();
498
		$threshold = $expiration->getMaxAgeAsTimestamp();
499
		$versions = self::getAllVersions($uid);
500
		if (!$threshold || !array_key_exists('all', $versions)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $threshold of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
501
			return;
502
		}
503
504
		$toDelete = [];
505
		foreach (array_reverse($versions['all']) as $key => $version) {
506
			if (intval($version['version'])<$threshold) {
507
				$toDelete[$key] = $version;
508
			} else {
509
				//Versions are sorted by time - nothing mo to iterate.
510
				break;
511
			}
512
		}
513
514
		$view = new View('/' . $uid . '/files_versions');
515
		if (!empty($toDelete)) {
516
			foreach ($toDelete as $version) {
517
				\OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT));
518
				self::deleteVersion($view, $version['path'] . '.v' . $version['version']);
519
				\OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT));
520
			}
521
		}
522
	}
523
524
	/**
525
	 * translate a timestamp into a string like "5 days ago"
526
	 * @param int $timestamp
527
	 * @return string for example "5 days ago"
528
	 */
529
	private static function getHumanReadableTimestamp($timestamp) {
530
531
		$diff = time() - $timestamp;
532
533
		if ($diff < 60) { // first minute
534
			return  $diff . " seconds ago";
535
		} elseif ($diff < 3600) { //first hour
536
			return round($diff / 60) . " minutes ago";
537
		} elseif ($diff < 86400) { // first day
538
			return round($diff / 3600) . " hours ago";
539
		} elseif ($diff < 604800) { //first week
540
			return round($diff / 86400) . " days ago";
541
		} elseif ($diff < 2419200) { //first month
542
			return round($diff / 604800) . " weeks ago";
543
		} elseif ($diff < 29030400) { // first year
544
			return round($diff / 2419200) . " months ago";
545
		} else {
546
			return round($diff / 29030400) . " years ago";
547
		}
548
549
	}
550
551
	/**
552
	 * returns all stored file versions from a given user
553
	 * @param string $uid id of the user
554
	 * @return array with contains two arrays 'all' which contains all versions sorted by age and 'by_file' which contains all versions sorted by filename
555
	 */
556
	private static function getAllVersions($uid) {
557
		$view = new View('/' . $uid . '/');
558
		$dirs = array(self::VERSIONS_ROOT);
559
		$versions = array();
560
561
		while (!empty($dirs)) {
562
			$dir = array_pop($dirs);
563
			$files = $view->getDirectoryContent($dir);
564
565
			foreach ($files as $file) {
566
				$fileData = $file->getData();
567
				$filePath = $dir . '/' . $fileData['name'];
568
				if ($file['type'] === 'dir') {
569
					array_push($dirs, $filePath);
570
				} else {
571
					$versionsBegin = strrpos($filePath, '.v');
572
					$relPathStart = strlen(self::VERSIONS_ROOT);
573
					$version = substr($filePath, $versionsBegin + 2);
574
					$relpath = substr($filePath, $relPathStart, $versionsBegin - $relPathStart);
575
					$key = $version . '#' . $relpath;
576
					$versions[$key] = array('path' => $relpath, 'timestamp' => $version);
577
				}
578
			}
579
		}
580
581
		// newest version first
582
		krsort($versions);
583
584
		$result = array();
585
586
		foreach ($versions as $key => $value) {
587
			$size = $view->filesize(self::VERSIONS_ROOT.'/'.$value['path'].'.v'.$value['timestamp']);
588
			$filename = $value['path'];
589
590
			$result['all'][$key]['version'] = $value['timestamp'];
591
			$result['all'][$key]['path'] = $filename;
592
			$result['all'][$key]['size'] = $size;
593
594
			$result['by_file'][$filename][$key]['version'] = $value['timestamp'];
595
			$result['by_file'][$filename][$key]['path'] = $filename;
596
			$result['by_file'][$filename][$key]['size'] = $size;
597
		}
598
599
		return $result;
600
	}
601
602
	/**
603
	 * get list of files we want to expire
604
	 * @param array $versions list of versions
605
	 * @param integer $time
606
	 * @param bool $quotaExceeded is versions storage limit reached
607
	 * @return array containing the list of to deleted versions and the size of them
608
	 */
609
	protected static function getExpireList($time, $versions, $quotaExceeded = false) {
610
		$expiration = self::getExpiration();
611
612
		if ($expiration->shouldAutoExpire()) {
613
			list($toDelete, $size) = self::getAutoExpireList($time, $versions);
614
		} else {
615
			$size = 0;
616
			$toDelete = [];  // versions we want to delete
617
		}
618
619
		foreach ($versions as $key => $version) {
620
			if ($expiration->isExpired($version['version'], $quotaExceeded) && !isset($toDelete[$key])) {
621
				$size += $version['size'];
622
				$toDelete[$key] = $version['path'] . '.v' . $version['version'];
623
			}
624
		}
625
626
		return [$toDelete, $size];
627
	}
628
629
	/**
630
	 * get list of files we want to expire
631
	 * @param array $versions list of versions
632
	 * @param integer $time
633
	 * @return array containing the list of to deleted versions and the size of them
634
	 */
635
	protected static function getAutoExpireList($time, $versions) {
636
		$size = 0;
637
		$toDelete = array();  // versions we want to delete
638
639
		$interval = 1;
640
		$step = Storage::$max_versions_per_interval[$interval]['step'];
641 View Code Duplication
		if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) {
642
			$nextInterval = -1;
643
		} else {
644
			$nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
645
		}
646
647
		$firstVersion = reset($versions);
648
		$firstKey = key($versions);
649
		$prevTimestamp = $firstVersion['version'];
650
		$nextVersion = $firstVersion['version'] - $step;
651
		unset($versions[$firstKey]);
652
653
		foreach ($versions as $key => $version) {
654
			$newInterval = true;
655
			while ($newInterval) {
656
				if ($nextInterval === -1 || $prevTimestamp > $nextInterval) {
657
					if ($version['version'] > $nextVersion) {
658
						//distance between two version too small, mark to delete
659
						$toDelete[$key] = $version['path'] . '.v' . $version['version'];
660
						$size += $version['size'];
661
						\OCP\Util::writeLog('files_versions', 'Mark to expire '. $version['path'] .' next version should be ' . $nextVersion . " or smaller. (prevTimestamp: " . $prevTimestamp . "; step: " . $step, \OCP\Util::INFO);
662
					} else {
663
						$nextVersion = $version['version'] - $step;
664
						$prevTimestamp = $version['version'];
665
					}
666
					$newInterval = false; // version checked so we can move to the next one
667
				} else { // time to move on to the next interval
668
					$interval++;
669
					$step = Storage::$max_versions_per_interval[$interval]['step'];
670
					$nextVersion = $prevTimestamp - $step;
671 View Code Duplication
					if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) {
672
						$nextInterval = -1;
673
					} else {
674
						$nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
675
					}
676
					$newInterval = true; // we changed the interval -> check same version with new interval
677
				}
678
			}
679
		}
680
681
		return array($toDelete, $size);
682
	}
683
684
	/**
685
	 * Schedule versions expiration for the given file
686
	 *
687
	 * @param string $uid owner of the file
688
	 * @param string $fileName file/folder for which to schedule expiration
689
	 */
690
	private static function scheduleExpire($uid, $fileName) {
691
		// let the admin disable auto expire
692
		$expiration = self::getExpiration();
693
		if ($expiration->isEnabled()) {
694
			$command = new Expire($uid, $fileName);
695
			\OC::$server->getCommandBus()->push($command);
696
		}
697
	}
698
699
	/**
700
	 * Expire versions which exceed the quota.
701
	 *
702
	 * This will setup the filesystem for the given user but will not
703
	 * tear it down afterwards.
704
	 *
705
	 * @param string $filename path to file to expire
706
	 * @param string $uid user for which to expire the version
707
	 * @return bool|int|null
708
	 */
709
	public static function expire($filename, $uid) {
710
		$expiration = self::getExpiration();
711
712
		if ($expiration->isEnabled()) {
713
			// get available disk space for user
714
			$user = \OC::$server->getUserManager()->get($uid);
715 View Code Duplication
			if (is_null($user)) {
716
				\OCP\Util::writeLog('files_versions', 'Backends provided no user object for ' . $uid, \OCP\Util::ERROR);
717
				throw new \OC\User\NoUserException('Backends provided no user object for ' . $uid);
718
			}
719
720
			\OC_Util::setupFS($uid);
721
722
			if (!Filesystem::file_exists($filename)) {
723
				return false;
724
			}
725
726
			if (empty($filename)) {
727
				// file maybe renamed or deleted
728
				return false;
729
			}
730
			$versionsFileview = new View('/'.$uid.'/files_versions');
731
732
			$softQuota = true;
733
			$quota = $user->getQuota();
734
			if ( $quota === null || $quota === 'none' ) {
735
				$quota = Filesystem::free_space('/');
736
				$softQuota = false;
737
			} else {
738
				$quota = \OCP\Util::computerFileSize($quota);
739
			}
740
741
			// make sure that we have the current size of the version history
742
			$versionsSize = self::getVersionsSize($uid);
743
744
			// calculate available space for version history
745
			// subtract size of files and current versions size from quota
746
			if ($quota >= 0) {
747
				if ($softQuota) {
748
					$files_view = new View('/' . $uid . '/files');
749
					$rootInfo = $files_view->getFileInfo('/', false);
750
					$free = $quota - $rootInfo['size']; // remaining free space for user
751
					if ($free > 0) {
752
						$availableSpace = ($free * self::DEFAULTMAXSIZE / 100) - $versionsSize; // how much space can be used for versions
753
					} else {
754
						$availableSpace = $free - $versionsSize;
755
					}
756
				} else {
757
					$availableSpace = $quota;
758
				}
759
			} else {
760
				$availableSpace = PHP_INT_MAX;
761
			}
762
763
			$allVersions = Storage::getVersions($uid, $filename);
764
765
			$time = time();
766
			list($toDelete, $sizeOfDeletedVersions) = self::getExpireList($time, $allVersions, $availableSpace <= 0);
767
768
			$availableSpace = $availableSpace + $sizeOfDeletedVersions;
769
			$versionsSize = $versionsSize - $sizeOfDeletedVersions;
770
771
			// if still not enough free space we rearrange the versions from all files
772
			if ($availableSpace <= 0) {
773
				$result = Storage::getAllVersions($uid);
774
				$allVersions = $result['all'];
775
776
				foreach ($result['by_file'] as $versions) {
777
					list($toDeleteNew, $size) = self::getExpireList($time, $versions, $availableSpace <= 0);
778
					$toDelete = array_merge($toDelete, $toDeleteNew);
779
					$sizeOfDeletedVersions += $size;
780
				}
781
				$availableSpace = $availableSpace + $sizeOfDeletedVersions;
782
				$versionsSize = $versionsSize - $sizeOfDeletedVersions;
783
			}
784
785
			foreach($toDelete as $key => $path) {
786
				\OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
787
				self::deleteVersion($versionsFileview, $path);
788
				\OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
789
				unset($allVersions[$key]); // update array with the versions we keep
790
				\OCP\Util::writeLog('files_versions', "Expire: " . $path, \OCP\Util::INFO);
791
			}
792
793
			// Check if enough space is available after versions are rearranged.
794
			// If not we delete the oldest versions until we meet the size limit for versions,
795
			// but always keep the two latest versions
796
			$numOfVersions = count($allVersions) -2 ;
797
			$i = 0;
798
			// sort oldest first and make sure that we start at the first element
799
			ksort($allVersions);
800
			reset($allVersions);
801
			while ($availableSpace < 0 && $i < $numOfVersions) {
802
				$version = current($allVersions);
803
				\OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
804
				self::deleteVersion($versionsFileview, $version['path'] . '.v' . $version['version']);
805
				\OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
806
				\OCP\Util::writeLog('files_versions', 'running out of space! Delete oldest version: ' . $version['path'].'.v'.$version['version'] , \OCP\Util::INFO);
807
				$versionsSize -= $version['size'];
808
				$availableSpace += $version['size'];
809
				next($allVersions);
810
				$i++;
811
			}
812
813
			return $versionsSize; // finally return the new size of the version history
814
		}
815
816
		return false;
817
	}
818
819
	/**
820
	 * Create recursively missing directories inside of files_versions
821
	 * that match the given path to a file.
822
	 *
823
	 * @param string $filename $path to a file, relative to the user's
824
	 * "files" folder
825
	 * @param View $view view on data/user/
826
	 */
827
	private static function createMissingDirectories($filename, $view) {
828
		$dirname = Filesystem::normalizePath(dirname($filename));
829
		$dirParts = explode('/', $dirname);
830
		$dir = "/files_versions";
831
		foreach ($dirParts as $part) {
832
			$dir = $dir . '/' . $part;
833
			if (!$view->file_exists($dir)) {
834
				$view->mkdir($dir);
835
			}
836
		}
837
	}
838
839
	/**
840
	 * Static workaround
841
	 * @return Expiration
842
	 */
843
	protected static function getExpiration(){
844
		if (is_null(self::$application)) {
845
			self::$application = new Application();
846
		}
847
		return self::$application->getContainer()->query('Expiration');
848
	}
849
850
}
851