You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Storage.php 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Bjoern Schiessle <bjoern@schiessle.org>
  8. * @author Björn Schießle <bjoern@schiessle.org>
  9. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  10. * @author Felix Moeller <mail@felixmoeller.de>
  11. * @author Felix Nieuwenhuizen <felix@tdlrali.com>
  12. * @author Joas Schilling <coding@schilljs.com>
  13. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  14. * @author Julius Härtl <jus@bitgrid.net>
  15. * @author Liam JACK <liamjack@users.noreply.github.com>
  16. * @author Lukas Reschke <lukas@statuscode.ch>
  17. * @author Morris Jobke <hey@morrisjobke.de>
  18. * @author Robin Appelman <robin@icewind.nl>
  19. * @author Robin McCorkell <robin@mccorkell.me.uk>
  20. * @author Roeland Jago Douma <roeland@famdouma.nl>
  21. * @author Thomas Müller <thomas.mueller@tmit.eu>
  22. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  23. * @author Vincent Petry <vincent@nextcloud.com>
  24. *
  25. * @license AGPL-3.0
  26. *
  27. * This code is free software: you can redistribute it and/or modify
  28. * it under the terms of the GNU Affero General Public License, version 3,
  29. * as published by the Free Software Foundation.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU Affero General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU Affero General Public License, version 3,
  37. * along with this program. If not, see <http://www.gnu.org/licenses/>
  38. *
  39. */
  40. /**
  41. * Versions
  42. *
  43. * A class to handle the versioning of files.
  44. */
  45. namespace OCA\Files_Versions;
  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 OCA\Files_Versions\Versions\IVersionManager;
  52. use OCP\Files\NotFoundException;
  53. use OCP\Files\StorageNotAvailableException;
  54. use OCP\IUser;
  55. use OCP\Lock\ILockingProvider;
  56. use OCP\User;
  57. class Storage {
  58. public const DEFAULTENABLED = true;
  59. public const DEFAULTMAXSIZE = 50; // unit: percentage; 50% of available disk space/quota
  60. public const VERSIONS_ROOT = 'files_versions/';
  61. public const DELETE_TRIGGER_MASTER_REMOVED = 0;
  62. public const DELETE_TRIGGER_RETENTION_CONSTRAINT = 1;
  63. public const DELETE_TRIGGER_QUOTA_EXCEEDED = 2;
  64. // files for which we can remove the versions after the delete operation was successful
  65. private static $deletedFiles = [];
  66. private static $sourcePathAndUser = [];
  67. private static $max_versions_per_interval = [
  68. //first 10sec, one version every 2sec
  69. 1 => ['intervalEndsAfter' => 10, 'step' => 2],
  70. //next minute, one version every 10sec
  71. 2 => ['intervalEndsAfter' => 60, 'step' => 10],
  72. //next hour, one version every minute
  73. 3 => ['intervalEndsAfter' => 3600, 'step' => 60],
  74. //next 24h, one version every hour
  75. 4 => ['intervalEndsAfter' => 86400, 'step' => 3600],
  76. //next 30days, one version per day
  77. 5 => ['intervalEndsAfter' => 2592000, 'step' => 86400],
  78. //until the end one version per week
  79. 6 => ['intervalEndsAfter' => -1, 'step' => 604800],
  80. ];
  81. /** @var \OCA\Files_Versions\AppInfo\Application */
  82. private static $application;
  83. /**
  84. * get the UID of the owner of the file and the path to the file relative to
  85. * owners files folder
  86. *
  87. * @param string $filename
  88. * @return array
  89. * @throws \OC\User\NoUserException
  90. */
  91. public static function getUidAndFilename($filename) {
  92. $uid = Filesystem::getOwner($filename);
  93. $userManager = \OC::$server->getUserManager();
  94. // if the user with the UID doesn't exists, e.g. because the UID points
  95. // to a remote user with a federated cloud ID we use the current logged-in
  96. // user. We need a valid local user to create the versions
  97. if (!$userManager->userExists($uid)) {
  98. $uid = User::getUser();
  99. }
  100. Filesystem::initMountPoints($uid);
  101. if ($uid !== User::getUser()) {
  102. $info = Filesystem::getFileInfo($filename);
  103. $ownerView = new View('/'.$uid.'/files');
  104. try {
  105. $filename = $ownerView->getPath($info['fileid']);
  106. // make sure that the file name doesn't end with a trailing slash
  107. // can for example happen single files shared across servers
  108. $filename = rtrim($filename, '/');
  109. } catch (NotFoundException $e) {
  110. $filename = null;
  111. }
  112. }
  113. return [$uid, $filename];
  114. }
  115. /**
  116. * Remember the owner and the owner path of the source file
  117. *
  118. * @param string $source source path
  119. */
  120. public static function setSourcePathAndUser($source) {
  121. [$uid, $path] = self::getUidAndFilename($source);
  122. self::$sourcePathAndUser[$source] = ['uid' => $uid, 'path' => $path];
  123. }
  124. /**
  125. * Gets the owner and the owner path from the source path
  126. *
  127. * @param string $source source path
  128. * @return array with user id and path
  129. */
  130. public static function getSourcePathAndUser($source) {
  131. if (isset(self::$sourcePathAndUser[$source])) {
  132. $uid = self::$sourcePathAndUser[$source]['uid'];
  133. $path = self::$sourcePathAndUser[$source]['path'];
  134. unset(self::$sourcePathAndUser[$source]);
  135. } else {
  136. $uid = $path = false;
  137. }
  138. return [$uid, $path];
  139. }
  140. /**
  141. * get current size of all versions from a given user
  142. *
  143. * @param string $user user who owns the versions
  144. * @return int versions size
  145. */
  146. private static function getVersionsSize($user) {
  147. $view = new View('/' . $user);
  148. $fileInfo = $view->getFileInfo('/files_versions');
  149. return isset($fileInfo['size']) ? $fileInfo['size'] : 0;
  150. }
  151. /**
  152. * store a new version of a file.
  153. */
  154. public static function store($filename) {
  155. // if the file gets streamed we need to remove the .part extension
  156. // to get the right target
  157. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  158. if ($ext === 'part') {
  159. $filename = substr($filename, 0, -5);
  160. }
  161. // we only handle existing files
  162. if (! Filesystem::file_exists($filename) || Filesystem::is_dir($filename)) {
  163. return false;
  164. }
  165. [$uid, $filename] = self::getUidAndFilename($filename);
  166. $files_view = new View('/'.$uid .'/files');
  167. $eventDispatcher = \OC::$server->getEventDispatcher();
  168. $fileInfo = $files_view->getFileInfo($filename);
  169. $id = $fileInfo->getId();
  170. $nodes = \OC::$server->getRootFolder()->getUserFolder($uid)->getById($id);
  171. foreach ($nodes as $node) {
  172. $event = new CreateVersionEvent($node);
  173. $eventDispatcher->dispatch('OCA\Files_Versions::createVersion', $event);
  174. if ($event->shouldCreateVersion() === false) {
  175. return false;
  176. }
  177. }
  178. // no use making versions for empty files
  179. if ($fileInfo->getSize() === 0) {
  180. return false;
  181. }
  182. /** @var IVersionManager $versionManager */
  183. $versionManager = \OC::$server->query(IVersionManager::class);
  184. $userManager = \OC::$server->getUserManager();
  185. $user = $userManager->get($uid);
  186. $versionManager->createVersion($user, $fileInfo);
  187. }
  188. /**
  189. * mark file as deleted so that we can remove the versions if the file is gone
  190. * @param string $path
  191. */
  192. public static function markDeletedFile($path) {
  193. [$uid, $filename] = self::getUidAndFilename($path);
  194. self::$deletedFiles[$path] = [
  195. 'uid' => $uid,
  196. 'filename' => $filename];
  197. }
  198. /**
  199. * delete the version from the storage and cache
  200. *
  201. * @param View $view
  202. * @param string $path
  203. */
  204. protected static function deleteVersion($view, $path) {
  205. $view->unlink($path);
  206. /**
  207. * @var \OC\Files\Storage\Storage $storage
  208. * @var string $internalPath
  209. */
  210. [$storage, $internalPath] = $view->resolvePath($path);
  211. $cache = $storage->getCache($internalPath);
  212. $cache->remove($internalPath);
  213. }
  214. /**
  215. * Delete versions of a file
  216. */
  217. public static function delete($path) {
  218. $deletedFile = self::$deletedFiles[$path];
  219. $uid = $deletedFile['uid'];
  220. $filename = $deletedFile['filename'];
  221. if (!Filesystem::file_exists($path)) {
  222. $view = new View('/' . $uid . '/files_versions');
  223. $versions = self::getVersions($uid, $filename);
  224. if (!empty($versions)) {
  225. foreach ($versions as $v) {
  226. \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED]);
  227. self::deleteVersion($view, $filename . '.v' . $v['version']);
  228. \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED]);
  229. }
  230. }
  231. }
  232. unset(self::$deletedFiles[$path]);
  233. }
  234. /**
  235. * Rename or copy versions of a file of the given paths
  236. *
  237. * @param string $sourcePath source path of the file to move, relative to
  238. * the currently logged in user's "files" folder
  239. * @param string $targetPath target path of the file to move, relative to
  240. * the currently logged in user's "files" folder
  241. * @param string $operation can be 'copy' or 'rename'
  242. */
  243. public static function renameOrCopy($sourcePath, $targetPath, $operation) {
  244. [$sourceOwner, $sourcePath] = self::getSourcePathAndUser($sourcePath);
  245. // it was a upload of a existing file if no old path exists
  246. // in this case the pre-hook already called the store method and we can
  247. // stop here
  248. if ($sourcePath === false) {
  249. return true;
  250. }
  251. [$targetOwner, $targetPath] = self::getUidAndFilename($targetPath);
  252. $sourcePath = ltrim($sourcePath, '/');
  253. $targetPath = ltrim($targetPath, '/');
  254. $rootView = new View('');
  255. // did we move a directory ?
  256. if ($rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) {
  257. // does the directory exists for versions too ?
  258. if ($rootView->is_dir('/' . $sourceOwner . '/files_versions/' . $sourcePath)) {
  259. // create missing dirs if necessary
  260. self::createMissingDirectories($targetPath, new View('/'. $targetOwner));
  261. // move the directory containing the versions
  262. $rootView->$operation(
  263. '/' . $sourceOwner . '/files_versions/' . $sourcePath,
  264. '/' . $targetOwner . '/files_versions/' . $targetPath
  265. );
  266. }
  267. } elseif ($versions = Storage::getVersions($sourceOwner, '/' . $sourcePath)) {
  268. // create missing dirs if necessary
  269. self::createMissingDirectories($targetPath, new View('/'. $targetOwner));
  270. foreach ($versions as $v) {
  271. // move each version one by one to the target directory
  272. $rootView->$operation(
  273. '/' . $sourceOwner . '/files_versions/' . $sourcePath.'.v' . $v['version'],
  274. '/' . $targetOwner . '/files_versions/' . $targetPath.'.v'.$v['version']
  275. );
  276. }
  277. }
  278. // if we moved versions directly for a file, schedule expiration check for that file
  279. if (!$rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) {
  280. self::scheduleExpire($targetOwner, $targetPath);
  281. }
  282. }
  283. /**
  284. * Rollback to an old version of a file.
  285. *
  286. * @param string $file file name
  287. * @param int $revision revision timestamp
  288. * @return bool
  289. */
  290. public static function rollback(string $file, int $revision, IUser $user) {
  291. // add expected leading slash
  292. $filename = '/' . ltrim($file, '/');
  293. // Fetch the userfolder to trigger view hooks
  294. $userFolder = \OC::$server->getUserFolder($user->getUID());
  295. $users_view = new View('/'.$user->getUID());
  296. $files_view = new View('/'. $user->getUID().'/files');
  297. $versionCreated = false;
  298. $fileInfo = $files_view->getFileInfo($file);
  299. // check if user has the permissions to revert a version
  300. if (!$fileInfo->isUpdateable()) {
  301. return false;
  302. }
  303. //first create a new version
  304. $version = 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename);
  305. if (!$users_view->file_exists($version)) {
  306. $users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename));
  307. $versionCreated = true;
  308. }
  309. $fileToRestore = 'files_versions' . $filename . '.v' . $revision;
  310. // Restore encrypted version of the old file for the newly restored file
  311. // This has to happen manually here since the file is manually copied below
  312. $oldVersion = $users_view->getFileInfo($fileToRestore)->getEncryptedVersion();
  313. $oldFileInfo = $users_view->getFileInfo($fileToRestore);
  314. $cache = $fileInfo->getStorage()->getCache();
  315. $cache->update(
  316. $fileInfo->getId(), [
  317. 'encrypted' => $oldVersion,
  318. 'encryptedVersion' => $oldVersion,
  319. 'size' => $oldFileInfo->getSize()
  320. ]
  321. );
  322. // rollback
  323. if (self::copyFileContents($users_view, $fileToRestore, 'files' . $filename)) {
  324. $files_view->touch($file, $revision);
  325. Storage::scheduleExpire($user->getUID(), $file);
  326. $node = $userFolder->get($file);
  327. // TODO: move away from those legacy hooks!
  328. \OC_Hook::emit('\OCP\Versions', 'rollback', [
  329. 'path' => $filename,
  330. 'revision' => $revision,
  331. 'node' => $node,
  332. ]);
  333. return true;
  334. } elseif ($versionCreated) {
  335. self::deleteVersion($users_view, $version);
  336. }
  337. return false;
  338. }
  339. /**
  340. * Stream copy file contents from $path1 to $path2
  341. *
  342. * @param View $view view to use for copying
  343. * @param string $path1 source file to copy
  344. * @param string $path2 target file
  345. *
  346. * @return bool true for success, false otherwise
  347. */
  348. private static function copyFileContents($view, $path1, $path2) {
  349. /** @var \OC\Files\Storage\Storage $storage1 */
  350. [$storage1, $internalPath1] = $view->resolvePath($path1);
  351. /** @var \OC\Files\Storage\Storage $storage2 */
  352. [$storage2, $internalPath2] = $view->resolvePath($path2);
  353. $view->lockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
  354. $view->lockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);
  355. // TODO add a proper way of overwriting a file while maintaining file ids
  356. if ($storage1->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage') || $storage2->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage')) {
  357. $source = $storage1->fopen($internalPath1, 'r');
  358. $target = $storage2->fopen($internalPath2, 'w');
  359. [, $result] = \OC_Helper::streamCopy($source, $target);
  360. fclose($source);
  361. fclose($target);
  362. if ($result !== false) {
  363. $storage1->unlink($internalPath1);
  364. }
  365. } else {
  366. $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
  367. }
  368. $view->unlockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
  369. $view->unlockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);
  370. return ($result !== false);
  371. }
  372. /**
  373. * get a list of all available versions of a file in descending chronological order
  374. * @param string $uid user id from the owner of the file
  375. * @param string $filename file to find versions of, relative to the user files dir
  376. * @param string $userFullPath
  377. * @return array versions newest version first
  378. */
  379. public static function getVersions($uid, $filename, $userFullPath = '') {
  380. $versions = [];
  381. if (empty($filename)) {
  382. return $versions;
  383. }
  384. // fetch for old versions
  385. $view = new View('/' . $uid . '/');
  386. $pathinfo = pathinfo($filename);
  387. $versionedFile = $pathinfo['basename'];
  388. $dir = Filesystem::normalizePath(self::VERSIONS_ROOT . '/' . $pathinfo['dirname']);
  389. $dirContent = false;
  390. if ($view->is_dir($dir)) {
  391. $dirContent = $view->opendir($dir);
  392. }
  393. if ($dirContent === false) {
  394. return $versions;
  395. }
  396. if (is_resource($dirContent)) {
  397. while (($entryName = readdir($dirContent)) !== false) {
  398. if (!Filesystem::isIgnoredDir($entryName)) {
  399. $pathparts = pathinfo($entryName);
  400. $filename = $pathparts['filename'];
  401. if ($filename === $versionedFile) {
  402. $pathparts = pathinfo($entryName);
  403. $timestamp = substr($pathparts['extension'], 1);
  404. $filename = $pathparts['filename'];
  405. $key = $timestamp . '#' . $filename;
  406. $versions[$key]['version'] = $timestamp;
  407. $versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp($timestamp);
  408. if (empty($userFullPath)) {
  409. $versions[$key]['preview'] = '';
  410. } else {
  411. $versions[$key]['preview'] = \OC::$server->getURLGenerator('files_version.Preview.getPreview', ['file' => $userFullPath, 'version' => $timestamp]);
  412. }
  413. $versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename);
  414. $versions[$key]['name'] = $versionedFile;
  415. $versions[$key]['size'] = $view->filesize($dir . '/' . $entryName);
  416. $versions[$key]['mimetype'] = \OC::$server->getMimeTypeDetector()->detectPath($versionedFile);
  417. }
  418. }
  419. }
  420. closedir($dirContent);
  421. }
  422. // sort with newest version first
  423. krsort($versions);
  424. return $versions;
  425. }
  426. /**
  427. * Expire versions that older than max version retention time
  428. * @param string $uid
  429. */
  430. public static function expireOlderThanMaxForUser($uid) {
  431. $expiration = self::getExpiration();
  432. $threshold = $expiration->getMaxAgeAsTimestamp();
  433. $versions = self::getAllVersions($uid);
  434. if (!$threshold || empty($versions['all'])) {
  435. return;
  436. }
  437. $toDelete = [];
  438. foreach (array_reverse($versions['all']) as $key => $version) {
  439. if ((int)$version['version'] < $threshold) {
  440. $toDelete[$key] = $version;
  441. } else {
  442. //Versions are sorted by time - nothing mo to iterate.
  443. break;
  444. }
  445. }
  446. $view = new View('/' . $uid . '/files_versions');
  447. if (!empty($toDelete)) {
  448. foreach ($toDelete as $version) {
  449. \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT]);
  450. self::deleteVersion($view, $version['path'] . '.v' . $version['version']);
  451. \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT]);
  452. }
  453. }
  454. }
  455. /**
  456. * translate a timestamp into a string like "5 days ago"
  457. * @param int $timestamp
  458. * @return string for example "5 days ago"
  459. */
  460. private static function getHumanReadableTimestamp($timestamp) {
  461. $diff = time() - $timestamp;
  462. if ($diff < 60) { // first minute
  463. return $diff . " seconds ago";
  464. } elseif ($diff < 3600) { //first hour
  465. return round($diff / 60) . " minutes ago";
  466. } elseif ($diff < 86400) { // first day
  467. return round($diff / 3600) . " hours ago";
  468. } elseif ($diff < 604800) { //first week
  469. return round($diff / 86400) . " days ago";
  470. } elseif ($diff < 2419200) { //first month
  471. return round($diff / 604800) . " weeks ago";
  472. } elseif ($diff < 29030400) { // first year
  473. return round($diff / 2419200) . " months ago";
  474. } else {
  475. return round($diff / 29030400) . " years ago";
  476. }
  477. }
  478. /**
  479. * returns all stored file versions from a given user
  480. * @param string $uid id of the user
  481. * @return array with contains two arrays 'all' which contains all versions sorted by age and 'by_file' which contains all versions sorted by filename
  482. */
  483. private static function getAllVersions($uid) {
  484. $view = new View('/' . $uid . '/');
  485. $dirs = [self::VERSIONS_ROOT];
  486. $versions = [];
  487. while (!empty($dirs)) {
  488. $dir = array_pop($dirs);
  489. $files = $view->getDirectoryContent($dir);
  490. foreach ($files as $file) {
  491. $fileData = $file->getData();
  492. $filePath = $dir . '/' . $fileData['name'];
  493. if ($file['type'] === 'dir') {
  494. $dirs[] = $filePath;
  495. } else {
  496. $versionsBegin = strrpos($filePath, '.v');
  497. $relPathStart = strlen(self::VERSIONS_ROOT);
  498. $version = substr($filePath, $versionsBegin + 2);
  499. $relpath = substr($filePath, $relPathStart, $versionsBegin - $relPathStart);
  500. $key = $version . '#' . $relpath;
  501. $versions[$key] = ['path' => $relpath, 'timestamp' => $version];
  502. }
  503. }
  504. }
  505. // newest version first
  506. krsort($versions);
  507. $result = [
  508. 'all' => [],
  509. 'by_file' => [],
  510. ];
  511. foreach ($versions as $key => $value) {
  512. $size = $view->filesize(self::VERSIONS_ROOT.'/'.$value['path'].'.v'.$value['timestamp']);
  513. $filename = $value['path'];
  514. $result['all'][$key]['version'] = $value['timestamp'];
  515. $result['all'][$key]['path'] = $filename;
  516. $result['all'][$key]['size'] = $size;
  517. $result['by_file'][$filename][$key]['version'] = $value['timestamp'];
  518. $result['by_file'][$filename][$key]['path'] = $filename;
  519. $result['by_file'][$filename][$key]['size'] = $size;
  520. }
  521. return $result;
  522. }
  523. /**
  524. * get list of files we want to expire
  525. * @param array $versions list of versions
  526. * @param integer $time
  527. * @param bool $quotaExceeded is versions storage limit reached
  528. * @return array containing the list of to deleted versions and the size of them
  529. */
  530. protected static function getExpireList($time, $versions, $quotaExceeded = false) {
  531. $expiration = self::getExpiration();
  532. if ($expiration->shouldAutoExpire()) {
  533. [$toDelete, $size] = self::getAutoExpireList($time, $versions);
  534. } else {
  535. $size = 0;
  536. $toDelete = []; // versions we want to delete
  537. }
  538. foreach ($versions as $key => $version) {
  539. if ($expiration->isExpired($version['version'], $quotaExceeded) && !isset($toDelete[$key])) {
  540. $size += $version['size'];
  541. $toDelete[$key] = $version['path'] . '.v' . $version['version'];
  542. }
  543. }
  544. return [$toDelete, $size];
  545. }
  546. /**
  547. * get list of files we want to expire
  548. * @param array $versions list of versions
  549. * @param integer $time
  550. * @return array containing the list of to deleted versions and the size of them
  551. */
  552. protected static function getAutoExpireList($time, $versions) {
  553. $size = 0;
  554. $toDelete = []; // versions we want to delete
  555. $interval = 1;
  556. $step = Storage::$max_versions_per_interval[$interval]['step'];
  557. if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) {
  558. $nextInterval = -1;
  559. } else {
  560. $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
  561. }
  562. $firstVersion = reset($versions);
  563. if ($firstVersion === false) {
  564. return [$toDelete, $size];
  565. }
  566. $firstKey = key($versions);
  567. $prevTimestamp = $firstVersion['version'];
  568. $nextVersion = $firstVersion['version'] - $step;
  569. unset($versions[$firstKey]);
  570. foreach ($versions as $key => $version) {
  571. $newInterval = true;
  572. while ($newInterval) {
  573. if ($nextInterval === -1 || $prevTimestamp > $nextInterval) {
  574. if ($version['version'] > $nextVersion) {
  575. //distance between two version too small, mark to delete
  576. $toDelete[$key] = $version['path'] . '.v' . $version['version'];
  577. $size += $version['size'];
  578. \OC::$server->getLogger()->info('Mark to expire '. $version['path'] .' next version should be ' . $nextVersion . " or smaller. (prevTimestamp: " . $prevTimestamp . "; step: " . $step, ['app' => 'files_versions']);
  579. } else {
  580. $nextVersion = $version['version'] - $step;
  581. $prevTimestamp = $version['version'];
  582. }
  583. $newInterval = false; // version checked so we can move to the next one
  584. } else { // time to move on to the next interval
  585. $interval++;
  586. $step = Storage::$max_versions_per_interval[$interval]['step'];
  587. $nextVersion = $prevTimestamp - $step;
  588. if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) {
  589. $nextInterval = -1;
  590. } else {
  591. $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
  592. }
  593. $newInterval = true; // we changed the interval -> check same version with new interval
  594. }
  595. }
  596. }
  597. return [$toDelete, $size];
  598. }
  599. /**
  600. * Schedule versions expiration for the given file
  601. *
  602. * @param string $uid owner of the file
  603. * @param string $fileName file/folder for which to schedule expiration
  604. */
  605. public static function scheduleExpire($uid, $fileName) {
  606. // let the admin disable auto expire
  607. $expiration = self::getExpiration();
  608. if ($expiration->isEnabled()) {
  609. $command = new Expire($uid, $fileName);
  610. \OC::$server->getCommandBus()->push($command);
  611. }
  612. }
  613. /**
  614. * Expire versions which exceed the quota.
  615. *
  616. * This will setup the filesystem for the given user but will not
  617. * tear it down afterwards.
  618. *
  619. * @param string $filename path to file to expire
  620. * @param string $uid user for which to expire the version
  621. * @return bool|int|null
  622. */
  623. public static function expire($filename, $uid) {
  624. $expiration = self::getExpiration();
  625. if ($expiration->isEnabled()) {
  626. // get available disk space for user
  627. $user = \OC::$server->getUserManager()->get($uid);
  628. if (is_null($user)) {
  629. \OC::$server->getLogger()->error('Backends provided no user object for ' . $uid, ['app' => 'files_versions']);
  630. throw new \OC\User\NoUserException('Backends provided no user object for ' . $uid);
  631. }
  632. \OC_Util::setupFS($uid);
  633. try {
  634. if (!Filesystem::file_exists($filename)) {
  635. return false;
  636. }
  637. } catch (StorageNotAvailableException $e) {
  638. // if we can't check that the file hasn't been deleted we can only assume that it hasn't
  639. // note that this `StorageNotAvailableException` is about the file the versions originate from,
  640. // not the storage that the versions are stored on
  641. }
  642. if (empty($filename)) {
  643. // file maybe renamed or deleted
  644. return false;
  645. }
  646. $versionsFileview = new View('/'.$uid.'/files_versions');
  647. $softQuota = true;
  648. $quota = $user->getQuota();
  649. if ($quota === null || $quota === 'none') {
  650. $quota = Filesystem::free_space('/');
  651. $softQuota = false;
  652. } else {
  653. $quota = \OCP\Util::computerFileSize($quota);
  654. }
  655. // make sure that we have the current size of the version history
  656. $versionsSize = self::getVersionsSize($uid);
  657. // calculate available space for version history
  658. // subtract size of files and current versions size from quota
  659. if ($quota >= 0) {
  660. if ($softQuota) {
  661. $userFolder = \OC::$server->getUserFolder($uid);
  662. if (is_null($userFolder)) {
  663. $availableSpace = 0;
  664. } else {
  665. $free = $quota - $userFolder->getSize(false); // remaining free space for user
  666. if ($free > 0) {
  667. $availableSpace = ($free * self::DEFAULTMAXSIZE / 100) - $versionsSize; // how much space can be used for versions
  668. } else {
  669. $availableSpace = $free - $versionsSize;
  670. }
  671. }
  672. } else {
  673. $availableSpace = $quota;
  674. }
  675. } else {
  676. $availableSpace = PHP_INT_MAX;
  677. }
  678. $allVersions = Storage::getVersions($uid, $filename);
  679. $time = time();
  680. [$toDelete, $sizeOfDeletedVersions] = self::getExpireList($time, $allVersions, $availableSpace <= 0);
  681. $availableSpace = $availableSpace + $sizeOfDeletedVersions;
  682. $versionsSize = $versionsSize - $sizeOfDeletedVersions;
  683. // if still not enough free space we rearrange the versions from all files
  684. if ($availableSpace <= 0) {
  685. $result = self::getAllVersions($uid);
  686. $allVersions = $result['all'];
  687. foreach ($result['by_file'] as $versions) {
  688. [$toDeleteNew, $size] = self::getExpireList($time, $versions, $availableSpace <= 0);
  689. $toDelete = array_merge($toDelete, $toDeleteNew);
  690. $sizeOfDeletedVersions += $size;
  691. }
  692. $availableSpace = $availableSpace + $sizeOfDeletedVersions;
  693. $versionsSize = $versionsSize - $sizeOfDeletedVersions;
  694. }
  695. $logger = \OC::$server->getLogger();
  696. foreach ($toDelete as $key => $path) {
  697. \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]);
  698. self::deleteVersion($versionsFileview, $path);
  699. \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]);
  700. unset($allVersions[$key]); // update array with the versions we keep
  701. $logger->info('Expire: ' . $path, ['app' => 'files_versions']);
  702. }
  703. // Check if enough space is available after versions are rearranged.
  704. // If not we delete the oldest versions until we meet the size limit for versions,
  705. // but always keep the two latest versions
  706. $numOfVersions = count($allVersions) - 2 ;
  707. $i = 0;
  708. // sort oldest first and make sure that we start at the first element
  709. ksort($allVersions);
  710. reset($allVersions);
  711. while ($availableSpace < 0 && $i < $numOfVersions) {
  712. $version = current($allVersions);
  713. \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]);
  714. self::deleteVersion($versionsFileview, $version['path'] . '.v' . $version['version']);
  715. \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]);
  716. \OC::$server->getLogger()->info('running out of space! Delete oldest version: ' . $version['path'].'.v'.$version['version'], ['app' => 'files_versions']);
  717. $versionsSize -= $version['size'];
  718. $availableSpace += $version['size'];
  719. next($allVersions);
  720. $i++;
  721. }
  722. return $versionsSize; // finally return the new size of the version history
  723. }
  724. return false;
  725. }
  726. /**
  727. * Create recursively missing directories inside of files_versions
  728. * that match the given path to a file.
  729. *
  730. * @param string $filename $path to a file, relative to the user's
  731. * "files" folder
  732. * @param View $view view on data/user/
  733. */
  734. public static function createMissingDirectories($filename, $view) {
  735. $dirname = Filesystem::normalizePath(dirname($filename));
  736. $dirParts = explode('/', $dirname);
  737. $dir = "/files_versions";
  738. foreach ($dirParts as $part) {
  739. $dir = $dir . '/' . $part;
  740. if (!$view->file_exists($dir)) {
  741. $view->mkdir($dir);
  742. }
  743. }
  744. }
  745. /**
  746. * Static workaround
  747. * @return Expiration
  748. */
  749. protected static function getExpiration() {
  750. if (self::$application === null) {
  751. self::$application = \OC::$server->query(Application::class);
  752. }
  753. return self::$application->getContainer()->query(Expiration::class);
  754. }
  755. }