Storage.php 28 KB

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