watcher.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. class Test_Files_Sharing_Watcher extends OCA\Files_sharing\Tests\TestCase {
  28. /**
  29. * @var \OC\Files\Storage\Storage
  30. */
  31. private $ownerStorage;
  32. /**
  33. * @var \OC\Files\Cache\Cache
  34. */
  35. private $ownerCache;
  36. /**
  37. * @var \OC\Files\Storage\Storage
  38. */
  39. private $sharedStorage;
  40. /**
  41. * @var \OC\Files\Cache\Cache
  42. */
  43. private $sharedCache;
  44. protected function setUp() {
  45. parent::setUp();
  46. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  47. // prepare user1's dir structure
  48. $textData = "dummy file data\n";
  49. $this->view->mkdir('container');
  50. $this->view->mkdir('container/shareddir');
  51. $this->view->mkdir('container/shareddir/subdir');
  52. list($this->ownerStorage, $internalPath) = $this->view->resolvePath('');
  53. $this->ownerCache = $this->ownerStorage->getCache();
  54. $this->ownerStorage->getScanner()->scan('');
  55. // share "shareddir" with user2
  56. $fileinfo = $this->view->getFileInfo('container/shareddir');
  57. \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  58. self::TEST_FILES_SHARING_API_USER2, 31);
  59. // login as user2
  60. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  61. // retrieve the shared storage
  62. $secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
  63. list($this->sharedStorage, $internalPath) = $secondView->resolvePath('files/shareddir');
  64. $this->sharedCache = $this->sharedStorage->getCache();
  65. }
  66. protected function tearDown() {
  67. if ($this->sharedCache) {
  68. $this->sharedCache->clear();
  69. }
  70. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  71. $fileinfo = $this->view->getFileInfo('container/shareddir');
  72. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  73. self::TEST_FILES_SHARING_API_USER2);
  74. $this->view->deleteAll('container');
  75. $this->ownerCache->clear();
  76. parent::tearDown();
  77. }
  78. /**
  79. * Tests that writing a file using the shared storage will propagate the file
  80. * size to the owner's parent folders.
  81. */
  82. function testFolderSizePropagationToOwnerStorage() {
  83. $initialSizes = self::getOwnerDirSizes('files/container/shareddir');
  84. $textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  85. $dataLen = strlen($textData);
  86. $this->sharedCache->put('bar.txt', array('mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain'));
  87. $this->sharedStorage->file_put_contents('bar.txt', $textData);
  88. $this->sharedCache->put('', array('mtime' => 10, 'storage_mtime' => 10, 'size' => '-1', 'mimetype' => 'httpd/unix-directory'));
  89. // run the propagation code
  90. $result = $this->sharedStorage->getWatcher()->checkUpdate('');
  91. $this->assertTrue($result);
  92. // the owner's parent dirs must have increase size
  93. $newSizes = self::getOwnerDirSizes('files/container/shareddir');
  94. $this->assertEquals($initialSizes[''] + $dataLen, $newSizes['']);
  95. $this->assertEquals($initialSizes['files'] + $dataLen, $newSizes['files']);
  96. $this->assertEquals($initialSizes['files/container'] + $dataLen, $newSizes['files/container']);
  97. $this->assertEquals($initialSizes['files/container/shareddir'] + $dataLen, $newSizes['files/container/shareddir']);
  98. // no more updates
  99. $result = $this->sharedStorage->getWatcher()->checkUpdate('');
  100. $this->assertFalse($result);
  101. }
  102. /**
  103. * Tests that writing a file using the shared storage will propagate the file
  104. * size to the owner's parent folders.
  105. */
  106. function testSubFolderSizePropagationToOwnerStorage() {
  107. $initialSizes = self::getOwnerDirSizes('files/container/shareddir/subdir');
  108. $textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  109. $dataLen = strlen($textData);
  110. $this->sharedCache->put('subdir/bar.txt', array('mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain'));
  111. $this->sharedStorage->file_put_contents('subdir/bar.txt', $textData);
  112. $this->sharedCache->put('subdir', array('mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain'));
  113. // run the propagation code
  114. $result = $this->sharedStorage->getWatcher()->checkUpdate('subdir');
  115. $this->assertTrue($result);
  116. // the owner's parent dirs must have increase size
  117. $newSizes = self::getOwnerDirSizes('files/container/shareddir/subdir');
  118. $this->assertEquals($initialSizes[''] + $dataLen, $newSizes['']);
  119. $this->assertEquals($initialSizes['files'] + $dataLen, $newSizes['files']);
  120. $this->assertEquals($initialSizes['files/container'] + $dataLen, $newSizes['files/container']);
  121. $this->assertEquals($initialSizes['files/container/shareddir'] + $dataLen, $newSizes['files/container/shareddir']);
  122. $this->assertEquals($initialSizes['files/container/shareddir/subdir'] + $dataLen, $newSizes['files/container/shareddir/subdir']);
  123. // no more updates
  124. $result = $this->sharedStorage->getWatcher()->checkUpdate('subdir');
  125. $this->assertFalse($result);
  126. }
  127. /**
  128. * Returns the sizes of the path and its parent dirs in a hash
  129. * where the key is the path and the value is the size.
  130. * @param string $path
  131. */
  132. function getOwnerDirSizes($path) {
  133. $result = array();
  134. while ($path != '' && $path != '' && $path != '.') {
  135. $cachedData = $this->ownerCache->get($path);
  136. $result[$path] = $cachedData['size'];
  137. $path = dirname($path);
  138. }
  139. $cachedData = $this->ownerCache->get('');
  140. $result[''] = $cachedData['size'];
  141. return $result;
  142. }
  143. }