ScannerTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Files\Cache;
  8. use OC;
  9. use OC\Files\Cache\Cache;
  10. use OC\Files\Cache\CacheEntry;
  11. use OC\Files\Cache\Scanner;
  12. use OC\Files\Storage\Storage;
  13. use OC\Files\Storage\Temporary;
  14. use OCP\Files\Cache\IScanner;
  15. use Test\TestCase;
  16. /**
  17. * Class ScannerTest
  18. *
  19. * @group DB
  20. *
  21. * @package Test\Files\Cache
  22. */
  23. class ScannerTest extends TestCase {
  24. private Storage $storage;
  25. private Scanner $scanner;
  26. private Cache $cache;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->storage = new Temporary([]);
  30. $this->scanner = new Scanner($this->storage);
  31. $this->cache = new Cache($this->storage);
  32. }
  33. protected function tearDown(): void {
  34. $this->cache->clear();
  35. parent::tearDown();
  36. }
  37. public function testFile() {
  38. $data = "dummy file data\n";
  39. $this->storage->file_put_contents('foo.txt', $data);
  40. $this->scanner->scanFile('foo.txt');
  41. $this->assertEquals($this->cache->inCache('foo.txt'), true);
  42. $cachedData = $this->cache->get('foo.txt');
  43. $this->assertEquals($cachedData['size'], strlen($data));
  44. $this->assertEquals($cachedData['mimetype'], 'text/plain');
  45. $this->assertNotEquals($cachedData['parent'], -1); //parent folders should be scanned automatically
  46. $data = file_get_contents(OC::$SERVERROOT . '/core/img/logo/logo.png');
  47. $this->storage->file_put_contents('foo.png', $data);
  48. $this->scanner->scanFile('foo.png');
  49. $this->assertEquals($this->cache->inCache('foo.png'), true);
  50. $cachedData = $this->cache->get('foo.png');
  51. $this->assertEquals($cachedData['size'], strlen($data));
  52. $this->assertEquals($cachedData['mimetype'], 'image/png');
  53. }
  54. public function testFile4Byte() {
  55. $data = "dummy file data\n";
  56. $this->storage->file_put_contents('foo🙈.txt', $data);
  57. if (OC::$server->getDatabaseConnection()->supports4ByteText()) {
  58. $this->assertNotNull($this->scanner->scanFile('foo🙈.txt'));
  59. $this->assertTrue($this->cache->inCache('foo🙈.txt'), true);
  60. $cachedData = $this->cache->get('foo🙈.txt');
  61. $this->assertEquals(strlen($data), $cachedData['size']);
  62. $this->assertEquals('text/plain', $cachedData['mimetype']);
  63. $this->assertNotEquals(-1, $cachedData['parent']); //parent folders should be scanned automatically
  64. } else {
  65. $this->assertNull($this->scanner->scanFile('foo🙈.txt'));
  66. $this->assertFalse($this->cache->inCache('foo🙈.txt'), true);
  67. }
  68. }
  69. public function testFileInvalidChars() {
  70. $data = "dummy file data\n";
  71. $this->storage->file_put_contents("foo\nbar.txt", $data);
  72. $this->assertNull($this->scanner->scanFile("foo\nbar.txt"));
  73. $this->assertFalse($this->cache->inCache("foo\nbar.txt"), true);
  74. }
  75. private function fillTestFolders() {
  76. $textData = "dummy file data\n";
  77. $imgData = file_get_contents(OC::$SERVERROOT . '/core/img/logo/logo.png');
  78. $this->storage->mkdir('folder');
  79. $this->storage->file_put_contents('foo.txt', $textData);
  80. $this->storage->file_put_contents('foo.png', $imgData);
  81. $this->storage->file_put_contents('folder/bar.txt', $textData);
  82. }
  83. public function testFolder() {
  84. $this->fillTestFolders();
  85. $this->scanner->scan('');
  86. $this->assertEquals($this->cache->inCache(''), true);
  87. $this->assertEquals($this->cache->inCache('foo.txt'), true);
  88. $this->assertEquals($this->cache->inCache('foo.png'), true);
  89. $this->assertEquals($this->cache->inCache('folder'), true);
  90. $this->assertEquals($this->cache->inCache('folder/bar.txt'), true);
  91. $cachedDataText = $this->cache->get('foo.txt');
  92. $cachedDataText2 = $this->cache->get('foo.txt');
  93. $cachedDataImage = $this->cache->get('foo.png');
  94. $cachedDataFolder = $this->cache->get('');
  95. $cachedDataFolder2 = $this->cache->get('folder');
  96. $this->assertEquals($cachedDataImage['parent'], $cachedDataText['parent']);
  97. $this->assertEquals($cachedDataFolder['fileid'], $cachedDataImage['parent']);
  98. $this->assertEquals($cachedDataFolder['size'], $cachedDataImage['size'] + $cachedDataText['size'] + $cachedDataText2['size']);
  99. $this->assertEquals($cachedDataFolder2['size'], $cachedDataText2['size']);
  100. }
  101. public function testShallow() {
  102. $this->fillTestFolders();
  103. $this->scanner->scan('', IScanner::SCAN_SHALLOW);
  104. $this->assertEquals($this->cache->inCache(''), true);
  105. $this->assertEquals($this->cache->inCache('foo.txt'), true);
  106. $this->assertEquals($this->cache->inCache('foo.png'), true);
  107. $this->assertEquals($this->cache->inCache('folder'), true);
  108. $this->assertEquals($this->cache->inCache('folder/bar.txt'), false);
  109. $cachedDataFolder = $this->cache->get('');
  110. $cachedDataFolder2 = $this->cache->get('folder');
  111. $this->assertEquals(-1, $cachedDataFolder['size']);
  112. $this->assertEquals(-1, $cachedDataFolder2['size']);
  113. $this->scanner->scan('folder', IScanner::SCAN_SHALLOW);
  114. $cachedDataFolder2 = $this->cache->get('folder');
  115. $this->assertNotEquals($cachedDataFolder2['size'], -1);
  116. $this->cache->correctFolderSize('folder');
  117. $cachedDataFolder = $this->cache->get('');
  118. $this->assertNotEquals($cachedDataFolder['size'], -1);
  119. }
  120. public function testBackgroundScan() {
  121. $this->fillTestFolders();
  122. $this->storage->mkdir('folder2');
  123. $this->storage->file_put_contents('folder2/bar.txt', 'foobar');
  124. $this->scanner->scan('', IScanner::SCAN_SHALLOW);
  125. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  126. $this->assertFalse($this->cache->inCache('folder/2bar.txt'));
  127. $cachedData = $this->cache->get('');
  128. $this->assertEquals(-1, $cachedData['size']);
  129. $this->scanner->backgroundScan();
  130. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  131. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  132. $cachedData = $this->cache->get('');
  133. $this->assertnotEquals(-1, $cachedData['size']);
  134. $this->assertFalse($this->cache->getIncomplete());
  135. }
  136. public function testBackgroundScanOnlyRecurseIncomplete() {
  137. $this->fillTestFolders();
  138. $this->storage->mkdir('folder2');
  139. $this->storage->file_put_contents('folder2/bar.txt', 'foobar');
  140. $this->scanner->scan('', IScanner::SCAN_SHALLOW);
  141. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  142. $this->assertFalse($this->cache->inCache('folder/2bar.txt'));
  143. $this->assertFalse($this->cache->inCache('folder2/bar.txt'));
  144. $this->cache->put('folder2', ['size' => 1]); // mark as complete
  145. $cachedData = $this->cache->get('');
  146. $this->assertEquals(-1, $cachedData['size']);
  147. $this->scanner->scan('', IScanner::SCAN_RECURSIVE_INCOMPLETE, IScanner::REUSE_ETAG | IScanner::REUSE_SIZE);
  148. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  149. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  150. $this->assertFalse($this->cache->inCache('folder2/bar.txt'));
  151. $cachedData = $this->cache->get('');
  152. $this->assertNotEquals(-1, $cachedData['size']);
  153. $this->assertFalse($this->cache->getIncomplete());
  154. }
  155. public function testBackgroundScanNestedIncompleteFolders() {
  156. $this->storage->mkdir('folder');
  157. $this->scanner->backgroundScan();
  158. $this->storage->mkdir('folder/subfolder1');
  159. $this->storage->mkdir('folder/subfolder2');
  160. $this->storage->mkdir('folder/subfolder1/subfolder3');
  161. $this->cache->put('folder', ['size' => -1]);
  162. $this->cache->put('folder/subfolder1', ['size' => -1]);
  163. // do a scan to get the folders into the cache.
  164. $this->scanner->backgroundScan();
  165. $this->assertTrue($this->cache->inCache('folder/subfolder1/subfolder3'));
  166. $this->storage->file_put_contents('folder/subfolder1/bar1.txt', 'foobar');
  167. $this->storage->file_put_contents('folder/subfolder1/subfolder3/bar3.txt', 'foobar');
  168. $this->storage->file_put_contents('folder/subfolder2/bar2.txt', 'foobar');
  169. //mark folders as incomplete.
  170. $this->cache->put('folder/subfolder1', ['size' => -1]);
  171. $this->cache->put('folder/subfolder2', ['size' => -1]);
  172. $this->cache->put('folder/subfolder1/subfolder3', ['size' => -1]);
  173. $this->scanner->backgroundScan();
  174. $this->assertTrue($this->cache->inCache('folder/subfolder1/bar1.txt'));
  175. $this->assertTrue($this->cache->inCache('folder/subfolder2/bar2.txt'));
  176. $this->assertTrue($this->cache->inCache('folder/subfolder1/subfolder3/bar3.txt'));
  177. //check if folder sizes are correct.
  178. $this->assertEquals(18, $this->cache->get('folder')['size']);
  179. $this->assertEquals(12, $this->cache->get('folder/subfolder1')['size']);
  180. $this->assertEquals(6, $this->cache->get('folder/subfolder1/subfolder3')['size']);
  181. $this->assertEquals(6, $this->cache->get('folder/subfolder2')['size']);
  182. }
  183. public function testReuseExisting() {
  184. $this->fillTestFolders();
  185. $this->scanner->scan('');
  186. $oldData = $this->cache->get('');
  187. $this->storage->unlink('folder/bar.txt');
  188. $this->cache->put('folder', ['mtime' => $this->storage->filemtime('folder'), 'storage_mtime' => $this->storage->filemtime('folder')]);
  189. $this->scanner->scan('', IScanner::SCAN_SHALLOW, IScanner::REUSE_SIZE);
  190. $newData = $this->cache->get('');
  191. $this->assertIsString($oldData['etag']);
  192. $this->assertIsString($newData['etag']);
  193. $this->assertNotSame($oldData['etag'], $newData['etag']);
  194. $this->assertEquals($oldData['size'], $newData['size']);
  195. $oldData = $newData;
  196. $this->scanner->scan('', IScanner::SCAN_SHALLOW, IScanner::REUSE_ETAG);
  197. $newData = $this->cache->get('');
  198. $this->assertSame($oldData['etag'], $newData['etag']);
  199. $this->assertEquals(-1, $newData['size']);
  200. $this->scanner->scan('', IScanner::SCAN_RECURSIVE);
  201. $oldData = $this->cache->get('');
  202. $this->assertNotEquals(-1, $oldData['size']);
  203. $this->scanner->scanFile('', IScanner::REUSE_ETAG + IScanner::REUSE_SIZE);
  204. $newData = $this->cache->get('');
  205. $this->assertSame($oldData['etag'], $newData['etag']);
  206. $this->assertEquals($oldData['size'], $newData['size']);
  207. $this->scanner->scan('', IScanner::SCAN_RECURSIVE, IScanner::REUSE_ETAG + IScanner::REUSE_SIZE);
  208. $newData = $this->cache->get('');
  209. $this->assertSame($oldData['etag'], $newData['etag']);
  210. $this->assertEquals($oldData['size'], $newData['size']);
  211. $this->scanner->scan('', IScanner::SCAN_SHALLOW, IScanner::REUSE_ETAG + IScanner::REUSE_SIZE);
  212. $newData = $this->cache->get('');
  213. $this->assertSame($oldData['etag'], $newData['etag']);
  214. $this->assertEquals($oldData['size'], $newData['size']);
  215. }
  216. public function testRemovedFile() {
  217. $this->fillTestFolders();
  218. $this->scanner->scan('');
  219. $this->assertTrue($this->cache->inCache('foo.txt'));
  220. $this->storage->unlink('foo.txt');
  221. $this->scanner->scan('', IScanner::SCAN_SHALLOW);
  222. $this->assertFalse($this->cache->inCache('foo.txt'));
  223. }
  224. public function testRemovedFolder() {
  225. $this->fillTestFolders();
  226. $this->scanner->scan('');
  227. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  228. $this->storage->rmdir('/folder');
  229. $this->scanner->scan('', IScanner::SCAN_SHALLOW);
  230. $this->assertFalse($this->cache->inCache('folder'));
  231. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  232. }
  233. public function testScanRemovedFile() {
  234. $this->fillTestFolders();
  235. $this->scanner->scan('');
  236. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  237. $this->storage->unlink('folder/bar.txt');
  238. $this->scanner->scanFile('folder/bar.txt');
  239. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  240. }
  241. public function testETagRecreation() {
  242. $this->fillTestFolders();
  243. $this->scanner->scan('folder/bar.txt');
  244. // manipulate etag to simulate an empty etag
  245. $this->scanner->scan('', IScanner::SCAN_SHALLOW, IScanner::REUSE_ETAG);
  246. /** @var CacheEntry $data0 */
  247. $data0 = $this->cache->get('folder/bar.txt');
  248. $this->assertIsString($data0['etag']);
  249. $data1 = $this->cache->get('folder');
  250. $this->assertIsString($data1['etag']);
  251. $data2 = $this->cache->get('');
  252. $this->assertIsString($data2['etag']);
  253. $data0['etag'] = '';
  254. $this->cache->put('folder/bar.txt', $data0->getData());
  255. // rescan
  256. $this->scanner->scan('folder/bar.txt', IScanner::SCAN_SHALLOW, IScanner::REUSE_ETAG);
  257. // verify cache content
  258. $newData0 = $this->cache->get('folder/bar.txt');
  259. $this->assertIsString($newData0['etag']);
  260. $this->assertNotEmpty($newData0['etag']);
  261. }
  262. public function testRepairParent() {
  263. $this->fillTestFolders();
  264. $this->scanner->scan('');
  265. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  266. $oldFolderId = $this->cache->getId('folder');
  267. // delete the folder without removing the children
  268. $query = OC::$server->getDatabaseConnection()->getQueryBuilder();
  269. $query->delete('filecache')
  270. ->where($query->expr()->eq('fileid', $query->createNamedParameter($oldFolderId)));
  271. $query->execute();
  272. $cachedData = $this->cache->get('folder/bar.txt');
  273. $this->assertEquals($oldFolderId, $cachedData['parent']);
  274. $this->assertFalse($this->cache->inCache('folder'));
  275. $this->scanner->scan('');
  276. $this->assertTrue($this->cache->inCache('folder'));
  277. $newFolderId = $this->cache->getId('folder');
  278. $this->assertNotEquals($oldFolderId, $newFolderId);
  279. $cachedData = $this->cache->get('folder/bar.txt');
  280. $this->assertEquals($newFolderId, $cachedData['parent']);
  281. }
  282. public function testRepairParentShallow() {
  283. $this->fillTestFolders();
  284. $this->scanner->scan('');
  285. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  286. $oldFolderId = $this->cache->getId('folder');
  287. // delete the folder without removing the children
  288. $query = OC::$server->getDatabaseConnection()->getQueryBuilder();
  289. $query->delete('filecache')
  290. ->where($query->expr()->eq('fileid', $query->createNamedParameter($oldFolderId)));
  291. $query->execute();
  292. $cachedData = $this->cache->get('folder/bar.txt');
  293. $this->assertEquals($oldFolderId, $cachedData['parent']);
  294. $this->assertFalse($this->cache->inCache('folder'));
  295. $this->scanner->scan('folder', IScanner::SCAN_SHALLOW);
  296. $this->assertTrue($this->cache->inCache('folder'));
  297. $newFolderId = $this->cache->getId('folder');
  298. $this->assertNotEquals($oldFolderId, $newFolderId);
  299. $cachedData = $this->cache->get('folder/bar.txt');
  300. $this->assertEquals($newFolderId, $cachedData['parent']);
  301. }
  302. /**
  303. * @dataProvider dataTestIsPartialFile
  304. *
  305. * @param string $path
  306. * @param bool $expected
  307. */
  308. public function testIsPartialFile($path, $expected) {
  309. $this->assertSame($expected,
  310. $this->scanner->isPartialFile($path)
  311. );
  312. }
  313. public function dataTestIsPartialFile() {
  314. return [
  315. ['foo.txt.part', true],
  316. ['/sub/folder/foo.txt.part', true],
  317. ['/sub/folder.part/foo.txt', true],
  318. ['foo.txt', false],
  319. ['/sub/folder/foo.txt', false],
  320. ];
  321. }
  322. public function testNoETagUnscannedFolder() {
  323. $this->fillTestFolders();
  324. $this->scanner->scan('');
  325. $oldFolderEntry = $this->cache->get('folder');
  326. // create a new file in a folder by keeping the mtime unchanged, but mark the folder as unscanned
  327. $this->storage->file_put_contents('folder/new.txt', 'foo');
  328. $this->storage->touch('folder', $oldFolderEntry->getMTime());
  329. $this->cache->update($oldFolderEntry->getId(), ['size' => -1]);
  330. $this->scanner->scan('');
  331. $this->cache->inCache('folder/new.txt');
  332. $newFolderEntry = $this->cache->get('folder');
  333. $this->assertNotEquals($newFolderEntry->getEtag(), $oldFolderEntry->getEtag());
  334. }
  335. public function testNoETagUnscannedSubFolder() {
  336. $this->fillTestFolders();
  337. $this->storage->mkdir('folder/sub');
  338. $this->scanner->scan('');
  339. $oldFolderEntry1 = $this->cache->get('folder');
  340. $oldFolderEntry2 = $this->cache->get('folder/sub');
  341. // create a new file in a folder by keeping the mtime unchanged, but mark the folder as unscanned
  342. $this->storage->file_put_contents('folder/sub/new.txt', 'foo');
  343. $this->storage->touch('folder/sub', $oldFolderEntry1->getMTime());
  344. // we only mark the direct parent as unscanned, which is the current "notify" behavior
  345. $this->cache->update($oldFolderEntry2->getId(), ['size' => -1]);
  346. $this->scanner->scan('');
  347. $this->cache->inCache('folder/new.txt');
  348. $newFolderEntry1 = $this->cache->get('folder');
  349. $this->assertNotEquals($newFolderEntry1->getEtag(), $oldFolderEntry1->getEtag());
  350. $newFolderEntry2 = $this->cache->get('folder/sub');
  351. $this->assertNotEquals($newFolderEntry2->getEtag(), $oldFolderEntry2->getEtag());
  352. }
  353. }